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 <dali/integration-api/platform-abstraction.h>
25 #include <base/environment-options.h>
36 PerformanceServer::PerformanceServer( AdaptorInternalServices& adaptorServices,
37 const EnvironmentOptions& environmentOptions)
38 :mPlatformAbstraction( adaptorServices.GetPlatformAbstractionInterface() ),
39 mEnvironmentOptions( environmentOptions ),
40 mKernelTrace( adaptorServices.GetKernelTraceInterface() ),
41 mStatContextManager( *this ),
42 mStatisticsLogBitmask( 0 ),
43 mLoggingEnabled( false ),
44 mLogFunctionInstalled( false )
46 SetLogging( mEnvironmentOptions.GetPerformanceStatsLoggingOptions(),
47 mEnvironmentOptions.GetPerformanceTimeStampOutput(),
48 mEnvironmentOptions.GetPerformanceStatsLoggingFrequency());
51 PerformanceServer::~PerformanceServer()
53 if( mLogFunctionInstalled )
55 mEnvironmentOptions.UnInstallLogFunction();
59 void PerformanceServer::SetLogging( unsigned int statisticsLogOptions,
60 unsigned int timeStampOutput,
61 unsigned int logFrequency )
63 if( ( statisticsLogOptions == 0) && ( timeStampOutput == 0 ))
65 mLoggingEnabled = false;
69 mStatisticsLogBitmask = statisticsLogOptions;
70 mPerformanceOutputBitmask = timeStampOutput;
72 mStatContextManager.SetLoggingLevel( mStatisticsLogBitmask, logFrequency);
74 mLoggingEnabled = true;
77 void PerformanceServer::SetLoggingFrequency( unsigned int logFrequency, ContextId contextId )
79 mStatContextManager.SetLoggingFrequency( logFrequency, contextId );
82 void PerformanceServer::EnableLogging( bool enable, ContextId contextId )
84 mStatContextManager.EnableLogging( enable, contextId );
87 PerformanceInterface::ContextId PerformanceServer::AddContext( const char* name )
89 // for adding custom contexts
90 return mStatContextManager.AddContext( name, PerformanceMarker::CUSTOM_EVENTS );
93 void PerformanceServer::RemoveContext( ContextId contextId )
95 mStatContextManager.RemoveContext( contextId );
98 void PerformanceServer::AddMarker( MarkerType markerType, ContextId contextId )
100 // called only for custom markers
102 if( !mLoggingEnabled )
107 // This is only called from main event thread, but may overlap with internal AddMarker calls
108 // from other threads ( update, render etc).
109 boost::mutex::scoped_lock sharedDatalock( mDataMutex );
111 // Get the time stamp
112 unsigned int seconds = 0;
113 unsigned int microseconds = 0;
114 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
117 PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
119 // get the marker description for this context, e.g SIZE_NEGOTIATION_START
120 const char* const description = mStatContextManager.GetMarkerDescription( markerType, contextId );
123 LogMarker( marker, description );
125 // Add custom marker to statistics context manager
126 mStatContextManager.AddCustomMarker( marker, contextId );
130 void PerformanceServer::AddMarker( MarkerType markerType )
132 // called only for internal markers
134 if( !mLoggingEnabled )
139 // AddMarker can be called from multiple threads, to avoid problems
140 // with updating contexts and the kernel trace, lock here.
141 boost::mutex::scoped_lock sharedDatalock( mDataMutex );
143 if( markerType == VSYNC )
145 // make sure log function is installed, note this will be called only from v-sync thread
146 // if the v-sync thread has already installed one, it won't make any difference.
147 if( ! mLogFunctionInstalled )
149 mEnvironmentOptions.InstallLogFunction();
150 mLogFunctionInstalled = true;
155 unsigned int seconds = 0;
156 unsigned int microseconds = 0;
157 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
160 PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
163 LogMarker(marker, marker.GetName() );
165 // Add internal marker to statistics context manager
166 mStatContextManager.AddInternalMarker( marker );
170 void PerformanceServer::LogContextStatistics( const char* const text )
172 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, text );
175 void PerformanceServer::LogMarker( const PerformanceMarker& marker, const char* const description )
180 // log to kernel trace
181 if( mPerformanceOutputBitmask & OUTPUT_KERNEL_TRACE )
183 // name will be something like UPDATE_START or UPDATE_END
184 mKernelTrace.Trace( description );
188 if ( mPerformanceOutputBitmask & OUTPUT_DALI_LOG )
190 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
191 "%d.%06d (seconds), %s\n",
192 marker.GetTimeStamp().seconds,
193 marker.GetTimeStamp().microseconds,
199 } // namespace Internal
201 } // namespace Adaptor