2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "performance-server.h"
22 #include <base/environment-options.h>
23 #include <dali/integration-api/platform-abstraction.h>
34 #define TIME_FMT "%0.2f ms" // 2 decimal places, e.g. 5.34 ms
35 #define TOTAL_TIME_FMT "%0.1f secs" // 1 decimal place, e.g. 4.5 seconds
40 const unsigned int DEFAULT_LOG_FREQUENCEY = 2; ///< default log frequency = 2
41 const unsigned int MILLISECONDS_PER_SECOND = 1000; ///< 1000 milliseconds per second
42 const unsigned int MICROSECONDS_PER_SECOND = 1000000; ///< 1000000 microseconds per second
46 PerformanceServer::PerformanceServer( AdaptorInternalServices& adaptorServices,
47 const EnvironmentOptions& environmentOptions)
48 :mLoggingEnabled( false),
49 mLogFunctionInstalled( false ),
50 mLogFrequencyMicroseconds( 0),
51 mPlatformAbstraction( adaptorServices.GetPlatformAbstractionInterface() ),
52 mEnvironmentOptions(environmentOptions),
53 mKernelTrace( adaptorServices.GetKernelTraceInterface() )
55 SetLogging( mEnvironmentOptions.GetPerformanceLoggingLevel(), mEnvironmentOptions.GetFrameRateLoggingFrequency());
58 PerformanceServer::~PerformanceServer()
60 if( mLogFunctionInstalled )
62 mEnvironmentOptions.UnInstallLogFunction();
65 void PerformanceServer::SetLogging( unsigned int level, unsigned int interval)
69 mLoggingEnabled = false;
74 mLogFrequencyMicroseconds = interval * MICROSECONDS_PER_SECOND;
76 if( mLogFrequencyMicroseconds == 0 )
78 mLogFrequencyMicroseconds = DEFAULT_LOG_FREQUENCEY * MICROSECONDS_PER_SECOND;
80 mLoggingEnabled = true;
84 void PerformanceServer::AddMarker( PerformanceMarker::MarkerType markerType )
86 if( !mLoggingEnabled )
91 unsigned int seconds(0);
92 unsigned int microseconds(0);
95 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
98 PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ));
100 AddMarkerToLog( marker );
103 void PerformanceServer::AddMarkerToLog( PerformanceMarker marker )
105 // Add Marker can be called from any thread
106 boost::mutex::scoped_lock sharedDatalock( mDataMutex );
109 mMarkers.PushBack( marker );
111 if( mLogLevel & LOG_EVENTS_TO_KERNEL )
113 mKernelTrace.Trace(marker.GetName());
116 // only log on the v-sync thread, so we have less impact on update/render
117 if( marker.GetType() != PerformanceMarker::V_SYNC )
122 // log out every mLogFrequency.
123 // check difference between first and last frame
124 unsigned int microseconds = PerformanceMarker::MicrosecondDiff( mMarkers[0], marker );
126 if( microseconds >= mLogFrequencyMicroseconds )
131 // reset data for update / render statistics
132 mUpdateStats.Reset();
133 mRenderStats.Reset();
138 void PerformanceServer::LogMarker(const char* name, const FrameTimeStats& frameStats)
140 // make sure log function is installed, note this will be called only from v-sync thread
141 // if the v-sync thread has already installed one, it won't make any difference.
142 if(! mLogFunctionInstalled )
144 mEnvironmentOptions.InstallLogFunction();
145 mLogFunctionInstalled = true;
148 // this will always log regardless of debug / release mode
149 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
150 "%s , min " TIME_FMT ", max " TIME_FMT ", total (" TOTAL_TIME_FMT "), avg " TIME_FMT "\n",
152 frameStats.GetMinTime() * MILLISECONDS_PER_SECOND,
153 frameStats.GetMaxTime() * MILLISECONDS_PER_SECOND,
154 frameStats.GetTotalTime(),
155 frameStats.GetRollingAverageTime() * MILLISECONDS_PER_SECOND);
158 void PerformanceServer::LogMarkers()
160 // insert time stamps into a frame-time-stats object, based on type
161 for( MarkerVector::SizeType i = 0; i < mMarkers.Size(); ++i)
163 const PerformanceMarker& marker = mMarkers[i];
164 switch( marker.GetType() )
166 case PerformanceMarker::UPDATE_START:
168 mUpdateStats.StartTime( marker.GetTimeStamp() );
171 case PerformanceMarker::UPDATE_END:
173 mUpdateStats.EndTime( marker.GetTimeStamp() );
176 case PerformanceMarker::RENDER_START:
178 mRenderStats.StartTime( marker.GetTimeStamp() );
181 case PerformanceMarker::RENDER_END:
183 mRenderStats.EndTime( marker.GetTimeStamp() );
186 case PerformanceMarker::PROCESS_EVENTS_START:
188 mEventStats.StartTime( marker.GetTimeStamp() );
191 case PerformanceMarker::PROCESS_EVENTS_END:
193 mEventStats.EndTime( marker.GetTimeStamp() );
202 if( mLogLevel & LOG_UPDATE_RENDER )
204 LogMarker("Update",mUpdateStats);
205 LogMarker("Render",mRenderStats);
207 if( mLogLevel & LOG_EVENT_PROCESS )
209 LogMarker("Event",mEventStats);
214 } // namespace Internal
216 } // namespace Adaptor