2 * Copyright (c) 2017 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 <dali/internal/system/common/performance-server.h>
22 #include <dali/integration-api/platform-abstraction.h>
25 #include <dali/internal/system/common/environment-options.h>
26 #include <dali/internal/system/common/time-service.h>
39 const unsigned int NANOSECONDS_PER_MICROSECOND = 1000u;
40 const float MICROSECONDS_TO_SECOND = 1e-6;
41 } // unnamed namespace
43 PerformanceServer::PerformanceServer( AdaptorInternalServices& adaptorServices,
44 const EnvironmentOptions& environmentOptions)
45 : mEnvironmentOptions( environmentOptions ),
46 mKernelTrace( adaptorServices.GetKernelTraceInterface() ),
47 mSystemTrace( adaptorServices.GetSystemTraceInterface() ),
49 #if defined(NETWORK_LOGGING_ENABLED)
50 mNetworkServer( adaptorServices, environmentOptions ),
51 mNetworkControlEnabled( mEnvironmentOptions.GetNetworkControlMode()),
53 mStatContextManager( *this ),
54 mStatisticsLogBitmask( 0 ),
55 mPerformanceOutputBitmask( 0 ),
56 mLoggingEnabled( false ),
57 mLogFunctionInstalled( false )
59 SetLogging( mEnvironmentOptions.GetPerformanceStatsLoggingOptions(),
60 mEnvironmentOptions.GetPerformanceTimeStampOutput(),
61 mEnvironmentOptions.GetPerformanceStatsLoggingFrequency());
63 #if defined(NETWORK_LOGGING_ENABLED)
64 if( mNetworkControlEnabled )
66 mLoggingEnabled = true;
67 mNetworkServer.Start();
72 PerformanceServer::~PerformanceServer()
74 #if defined(NETWORK_LOGGING_ENABLED)
75 if( mNetworkControlEnabled )
77 mNetworkServer.Stop();
81 if( mLogFunctionInstalled )
83 mEnvironmentOptions.UnInstallLogFunction();
87 void PerformanceServer::SetLogging( unsigned int statisticsLogOptions,
88 unsigned int timeStampOutput,
89 unsigned int logFrequency )
91 mStatisticsLogBitmask = statisticsLogOptions;
92 mPerformanceOutputBitmask = timeStampOutput;
94 mStatContextManager.SetLoggingLevel( mStatisticsLogBitmask, logFrequency);
96 if( ( mStatisticsLogBitmask == 0) && ( mPerformanceOutputBitmask == 0 ))
98 mLoggingEnabled = false;
102 mLoggingEnabled = true;
106 void PerformanceServer::SetLoggingFrequency( unsigned int logFrequency, ContextId contextId )
108 mStatContextManager.SetLoggingFrequency( logFrequency, contextId );
111 void PerformanceServer::EnableLogging( bool enable, ContextId contextId )
113 mStatContextManager.EnableLogging( enable, contextId );
116 PerformanceInterface::ContextId PerformanceServer::AddContext( const char* name )
118 // for adding custom contexts
119 return mStatContextManager.AddContext( name, PerformanceMarker::CUSTOM_EVENTS );
122 void PerformanceServer::RemoveContext( ContextId contextId )
124 mStatContextManager.RemoveContext( contextId );
127 void PerformanceServer::AddMarker( MarkerType markerType, ContextId contextId )
129 // called only for custom markers
131 if( !mLoggingEnabled )
136 // Get the time stamp
137 uint64_t timeStamp = 0;
138 TimeService::GetNanoseconds( timeStamp );
139 timeStamp /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
142 PerformanceMarker marker( markerType, FrameTimeStamp( 0, timeStamp ) );
144 // get the marker description for this context, e.g SIZE_NEGOTIATION_START
145 const char* const description = mStatContextManager.GetMarkerDescription( markerType, contextId );
148 LogMarker( marker, description );
150 // Add custom marker to statistics context manager
151 mStatContextManager.AddCustomMarker( marker, contextId );
154 void PerformanceServer::AddMarker( MarkerType markerType )
156 // called only for internal markers
158 if( !mLoggingEnabled )
163 if( markerType == VSYNC )
165 // make sure log function is installed, note this will be called only from v-sync thread
166 // if the v-sync thread has already installed one, it won't make any difference.
167 if( ! mLogFunctionInstalled )
169 mEnvironmentOptions.InstallLogFunction();
170 mLogFunctionInstalled = true;
175 uint64_t timeStamp = 0;
176 TimeService::GetNanoseconds( timeStamp );
177 timeStamp /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
180 PerformanceMarker marker( markerType, FrameTimeStamp( 0, timeStamp ) );
183 LogMarker(marker, marker.GetName() );
185 // Add internal marker to statistics context manager
186 mStatContextManager.AddInternalMarker( marker );
190 void PerformanceServer::LogContextStatistics( const char* const text )
192 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, text );
195 void PerformanceServer::LogMarker( const PerformanceMarker& marker, const char* const description )
197 #if defined(NETWORK_LOGGING_ENABLED)
198 // log to the network ( this is thread safe )
199 if( mNetworkControlEnabled )
201 mNetworkServer.TransmitMarker( marker, description );
205 // log to kernel trace
206 if( mPerformanceOutputBitmask & OUTPUT_KERNEL_TRACE )
208 // Kernel tracing implementation may not be thread safe
209 Mutex::ScopedLock lock( mLogMutex );
210 // description will be something like UPDATE_START or UPDATE_END
211 mKernelTrace.Trace( marker, description );
214 // log to system trace
215 if( mPerformanceOutputBitmask & OUTPUT_SYSTEM_TRACE )
217 // System tracing implementation may not be thread safe
218 Mutex::ScopedLock lock( mLogMutex );
220 mSystemTrace.Trace( marker, description );
223 // log to Dali log ( this is thread safe )
224 if ( mPerformanceOutputBitmask & OUTPUT_DALI_LOG )
226 Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
227 "%.6f (seconds), %s\n",
228 float( marker.GetTimeStamp().microseconds ) * MICROSECONDS_TO_SECOND,
235 } // namespace Internal
237 } // namespace Adaptor