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 PerformanceServer::PerformanceServer( AdaptorInternalServices& adaptorServices,
35 const EnvironmentOptions& environmentOptions)
36 :mPlatformAbstraction( adaptorServices.GetPlatformAbstractionInterface() ),
37 mEnvironmentOptions( environmentOptions ),
38 mKernelTrace( adaptorServices.GetKernelTraceInterface() ),
39 mStatContextManager( *this ),
40 mStatisticsLogBitmask( 0 ),
41 mLoggingEnabled( false ),
42 mLogFunctionInstalled( false )
44 SetLogging( mEnvironmentOptions.GetPerformanceStatsLoggingOptions(),
45 mEnvironmentOptions.GetPerformanceTimeStampOutput(),
46 mEnvironmentOptions.GetPerformanceStatsLoggingFrequency());
49 PerformanceServer::~PerformanceServer()
51 if( mLogFunctionInstalled )
53 mEnvironmentOptions.UnInstallLogFunction();
57 void PerformanceServer::SetLogging( unsigned int statisticsLogOptions,
58 unsigned int timeStampOutput,
59 unsigned int logFrequency )
61 if( ( statisticsLogOptions == 0) && ( timeStampOutput == 0 ))
63 mLoggingEnabled = false;
67 mStatisticsLogBitmask = statisticsLogOptions;
68 mPerformanceOutputBitmask = timeStampOutput;
70 mStatContextManager.SetLoggingLevel( mStatisticsLogBitmask, logFrequency);
72 mLoggingEnabled = true;
75 void PerformanceServer::SetLoggingFrequency( unsigned int logFrequency, ContextId contextId )
77 mStatContextManager.SetLoggingFrequency( logFrequency, contextId );
80 void PerformanceServer::EnableLogging( bool enable, ContextId contextId )
82 mStatContextManager.EnableLogging( enable, contextId );
85 PerformanceInterface::ContextId PerformanceServer::AddContext( const char* name )
87 // for adding custom contexts
88 return mStatContextManager.AddContext( name, PerformanceMarker::CUSTOM_EVENTS );
91 void PerformanceServer::RemoveContext( ContextId contextId )
93 mStatContextManager.RemoveContext( contextId );
96 void PerformanceServer::AddMarker( MarkerType markerType, ContextId contextId )
98 // called only for custom markers
100 if( !mLoggingEnabled )
105 // This is only called from main event thread, but may overlap with internal AddMarker calls
106 // from other threads ( update, render etc).
107 boost::mutex::scoped_lock sharedDatalock( mDataMutex );
109 // Get the time stamp
110 unsigned int seconds = 0;
111 unsigned int microseconds = 0;
112 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
115 PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
117 // get the marker description for this context, e.g SIZE_NEGOTIATION_START
118 const char* const description = mStatContextManager.GetMarkerDescription( markerType, contextId );
121 LogMarker( marker, description );
123 // Add custom marker to statistics context manager
124 mStatContextManager.AddCustomMarker( marker, contextId );
128 void PerformanceServer::AddMarker( MarkerType markerType )
130 // called only for internal markers
132 if( !mLoggingEnabled )
137 // AddMarker can be called from multiple threads, to avoid problems
138 // with updating contexts and the kernel trace, lock here.
139 boost::mutex::scoped_lock sharedDatalock( mDataMutex );
141 if( markerType == VSYNC )
143 // make sure log function is installed, note this will be called only from v-sync thread
144 // if the v-sync thread has already installed one, it won't make any difference.
145 if( ! mLogFunctionInstalled )
147 mEnvironmentOptions.InstallLogFunction();
148 mLogFunctionInstalled = true;
153 unsigned int seconds = 0;
154 unsigned int microseconds = 0;
155 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
158 PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
161 LogMarker(marker, marker.GetName() );
163 // Add internal marker to statistics context manager
164 mStatContextManager.AddInternalMarker( marker );
168 void PerformanceServer::LogContextStatistics( const char* const text )
170 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, text );
173 void PerformanceServer::LogMarker( const PerformanceMarker& marker, const char* const description )
178 // log to kernel trace
179 if( mPerformanceOutputBitmask & OUTPUT_KERNEL_TRACE )
181 // name will be something like UPDATE_START or UPDATE_END
182 mKernelTrace.Trace( description );
186 if ( mPerformanceOutputBitmask & OUTPUT_DALI_LOG )
188 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
189 "%d.%06d (seconds), %s\n",
190 marker.GetTimeStamp().seconds,
191 marker.GetTimeStamp().microseconds,
197 } // namespace Internal
199 } // namespace Adaptor