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 mSystemTrace( adaptorServices.GetSystemTraceInterface() ),
42 mNetworkServer( adaptorServices, environmentOptions ),
43 mStatContextManager( *this ),
44 mStatisticsLogBitmask( 0 ),
45 mNetworkControlEnabled( mEnvironmentOptions.GetNetworkControlMode()),
46 mLoggingEnabled( false ),
47 mLogFunctionInstalled( false )
49 SetLogging( mEnvironmentOptions.GetPerformanceStatsLoggingOptions(),
50 mEnvironmentOptions.GetPerformanceTimeStampOutput(),
51 mEnvironmentOptions.GetPerformanceStatsLoggingFrequency());
53 if( mNetworkControlEnabled )
55 mLoggingEnabled = true;
56 mNetworkServer.Start();
60 PerformanceServer::~PerformanceServer()
62 if( mNetworkControlEnabled )
64 mNetworkServer.Stop();
67 if( mLogFunctionInstalled )
69 mEnvironmentOptions.UnInstallLogFunction();
73 void PerformanceServer::SetLogging( unsigned int statisticsLogOptions,
74 unsigned int timeStampOutput,
75 unsigned int logFrequency )
77 mStatisticsLogBitmask = statisticsLogOptions;
78 mPerformanceOutputBitmask = timeStampOutput;
80 mStatContextManager.SetLoggingLevel( mStatisticsLogBitmask, logFrequency);
82 if( ( mStatisticsLogBitmask == 0) && ( mPerformanceOutputBitmask == 0 ))
84 mLoggingEnabled = false;
88 mLoggingEnabled = true;
92 void PerformanceServer::SetLoggingFrequency( unsigned int logFrequency, ContextId contextId )
94 mStatContextManager.SetLoggingFrequency( logFrequency, contextId );
97 void PerformanceServer::EnableLogging( bool enable, ContextId contextId )
99 mStatContextManager.EnableLogging( enable, contextId );
102 PerformanceInterface::ContextId PerformanceServer::AddContext( const char* name )
104 // for adding custom contexts
105 return mStatContextManager.AddContext( name, PerformanceMarker::CUSTOM_EVENTS );
108 void PerformanceServer::RemoveContext( ContextId contextId )
110 mStatContextManager.RemoveContext( contextId );
113 void PerformanceServer::AddMarker( MarkerType markerType, ContextId contextId )
115 // called only for custom markers
117 if( !mLoggingEnabled )
122 // This is only called from main event thread, but may overlap with internal AddMarker calls
123 // from other threads ( update, render etc).
124 boost::mutex::scoped_lock sharedDatalock( mDataMutex );
126 // Get the time stamp
127 unsigned int seconds = 0;
128 unsigned int microseconds = 0;
129 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
132 PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
134 // get the marker description for this context, e.g SIZE_NEGOTIATION_START
135 const char* const description = mStatContextManager.GetMarkerDescription( markerType, contextId );
138 LogMarker( marker, description );
140 // Add custom marker to statistics context manager
141 mStatContextManager.AddCustomMarker( marker, contextId );
145 void PerformanceServer::AddMarker( MarkerType markerType )
147 // called only for internal markers
149 if( !mLoggingEnabled )
154 // AddMarker can be called from multiple threads, to avoid problems
155 // with updating contexts and the kernel trace, lock here.
156 boost::mutex::scoped_lock sharedDatalock( mDataMutex );
158 if( markerType == VSYNC )
160 // make sure log function is installed, note this will be called only from v-sync thread
161 // if the v-sync thread has already installed one, it won't make any difference.
162 if( ! mLogFunctionInstalled )
164 mEnvironmentOptions.InstallLogFunction();
165 mLogFunctionInstalled = true;
170 unsigned int seconds = 0;
171 unsigned int microseconds = 0;
172 mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
175 PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
178 LogMarker(marker, marker.GetName() );
180 // Add internal marker to statistics context manager
181 mStatContextManager.AddInternalMarker( marker );
185 void PerformanceServer::LogContextStatistics( const char* const text )
187 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, text );
190 void PerformanceServer::LogMarker( const PerformanceMarker& marker, const char* const description )
192 // log to the network ( this is thread safe)
193 if( mNetworkControlEnabled )
195 mNetworkServer.TransmitMarker( marker, description );
198 // log to kernel trace
199 if( mPerformanceOutputBitmask & OUTPUT_KERNEL_TRACE )
201 // description will be something like UPDATE_START or UPDATE_END
202 mKernelTrace.Trace( marker, description );
205 // log to system trace
206 if( mPerformanceOutputBitmask & OUTPUT_SYSTEM_TRACE )
208 mSystemTrace.Trace( marker, description );
212 if ( mPerformanceOutputBitmask & OUTPUT_DALI_LOG )
214 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
215 "%d.%06d (seconds), %s\n",
216 marker.GetTimeStamp().seconds,
217 marker.GetTimeStamp().microseconds,
223 } // namespace Internal
225 } // namespace Adaptor