2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 #include "performance-server.h"
21 #include <base/log-options.h>
32 #define TIME_FMT "%0.2f ms" // 2 decimal places, e.g. 5.34 ms
33 #define TOTAL_TIME_FMT "%0.1f secs" // 1 decimal place, e.g. 4.5 seconds
38 const unsigned int DEFAULT_LOG_FREQUENCEY = 2; ///< default log frequency = 2
39 const unsigned int MILLISECONDS_PER_SECOND = 1000; ///< 1000 milliseconds per second
40 const unsigned int MICROSECONDS_PER_SECOND = 1000000; ///< 1000000 microseconds per second
44 PerformanceServer::PerformanceServer( AdaptorInternalServices& adaptorServices,
45 const LogOptions& logOptions)
46 :mLoggingEnabled( false),
47 mLogFunctionInstalled( false ),
48 mLogFrequencyMicroseconds( 0),
49 mPlatformAbstraction( adaptorServices.GetPlatformAbstractionInterface() ),
50 mLogOptions(logOptions),
51 mKernelTrace( adaptorServices.GetKernelTraceInterface() )
53 SetLogging( mLogOptions.GetPerformanceLoggingLevel(), mLogOptions.GetFrameRateLoggingFrequency());
56 PerformanceServer::~PerformanceServer()
58 if( mLogFunctionInstalled )
60 mLogOptions.UnInstallLogFunction();
63 void PerformanceServer::SetLogging( unsigned int level, unsigned int interval)
67 mLoggingEnabled = false;
72 mLogFrequencyMicroseconds = interval * MICROSECONDS_PER_SECOND;
74 if( mLogFrequencyMicroseconds == 0 )
76 mLogFrequencyMicroseconds = DEFAULT_LOG_FREQUENCEY * MICROSECONDS_PER_SECOND;
78 mLoggingEnabled = true;
82 void PerformanceServer::AddMarker( PerformanceMarker::MarkerType markerType )
84 if( !mLoggingEnabled )
89 unsigned int seconds(0);
90 unsigned int microseconds(0);
93 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
96 PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ));
98 AddMarkerToLog( marker );
101 void PerformanceServer::AddMarkerToLog( PerformanceMarker marker )
103 // Add Marker can be called from any thread
104 boost::mutex::scoped_lock sharedDatalock( mDataMutex );
107 mMarkers.PushBack( marker );
109 if( mLogLevel & LOG_EVENTS_TO_KERNEL )
111 mKernelTrace.Trace(marker.GetName());
114 // only log on the v-sync thread, so we have less impact on update/render
115 if( marker.GetType() != PerformanceMarker::V_SYNC )
120 // log out every mLogFrequency.
121 // check difference between first and last frame
122 unsigned int microseconds = PerformanceMarker::MicrosecondDiff( mMarkers[0], marker );
124 if( microseconds >= mLogFrequencyMicroseconds )
129 // reset data for update / render statistics
130 mUpdateStats.Reset();
131 mRenderStats.Reset();
136 void PerformanceServer::LogMarker(const char* name, const FrameTimeStats& frameStats)
138 // make sure log function is installed, note this will be called only from v-sync thread
139 // if the v-sync thread has already installed one, it won't make any difference.
140 if(! mLogFunctionInstalled )
142 mLogOptions.InstallLogFunction();
143 mLogFunctionInstalled = true;
146 // this will always log regardless of debug / release mode
147 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
148 "%s , min " TIME_FMT ", max " TIME_FMT ", total (" TOTAL_TIME_FMT "), avg " TIME_FMT "\n",
150 frameStats.GetMinTime() * MILLISECONDS_PER_SECOND,
151 frameStats.GetMaxTime() * MILLISECONDS_PER_SECOND,
152 frameStats.GetTotalTime(),
153 frameStats.GetRollingAverageTime() * MILLISECONDS_PER_SECOND);
156 void PerformanceServer::LogMarkers()
158 // insert time stamps into a frame-time-stats object, based on type
159 for( MarkerVector::SizeType i = 0; i < mMarkers.Size(); ++i)
161 const PerformanceMarker& marker = mMarkers[i];
162 switch( marker.GetType() )
164 case PerformanceMarker::UPDATE_START:
166 mUpdateStats.StartTime( marker.GetTimeStamp() );
169 case PerformanceMarker::UPDATE_END:
171 mUpdateStats.EndTime( marker.GetTimeStamp() );
174 case PerformanceMarker::RENDER_START:
176 mRenderStats.StartTime( marker.GetTimeStamp() );
179 case PerformanceMarker::RENDER_END:
181 mRenderStats.EndTime( marker.GetTimeStamp() );
184 case PerformanceMarker::PROCESS_EVENTS_START:
186 mEventStats.StartTime( marker.GetTimeStamp() );
189 case PerformanceMarker::PROCESS_EVENTS_END:
191 mEventStats.EndTime( marker.GetTimeStamp() );
200 if( mLogLevel & LOG_UPDATE_RENDER )
202 LogMarker("Update",mUpdateStats);
203 LogMarker("Render",mRenderStats);
205 if( mLogLevel & LOG_EVENT_PROCESS )
207 LogMarker("Event",mEventStats);
212 } // namespace Internal
214 } // namespace Adaptor