Merge "Set proper locale to harfbuzz" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / base / performance-logging / performance-server.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "performance-server.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/platform-abstraction.h>
23
24 // INTERNAL INCLUDES
25 #include <base/environment-options.h>
26 #include <base/time-service.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace Adaptor
35 {
36
37 namespace
38 {
39 const unsigned int NANOSECONDS_PER_MICROSECOND = 1000u;
40 const float        MICROSECONDS_TO_SECOND = 1e-6;
41 } // unnamed namespace
42
43 PerformanceServer::PerformanceServer( AdaptorInternalServices& adaptorServices,
44                                       const EnvironmentOptions& environmentOptions)
45 : mEnvironmentOptions( environmentOptions ),
46   mKernelTrace( adaptorServices.GetKernelTraceInterface() ),
47   mSystemTrace( adaptorServices.GetSystemTraceInterface() ),
48   mLogMutex(),
49 #if defined(NETWORK_LOGGING_ENABLED)
50   mNetworkServer( adaptorServices, environmentOptions ),
51   mNetworkControlEnabled( mEnvironmentOptions.GetNetworkControlMode()),
52 #endif
53   mStatContextManager( *this ),
54   mStatisticsLogBitmask( 0 ),
55   mPerformanceOutputBitmask( 0 ),
56   mLoggingEnabled( false ),
57   mLogFunctionInstalled( false )
58 {
59   SetLogging( mEnvironmentOptions.GetPerformanceStatsLoggingOptions(),
60               mEnvironmentOptions.GetPerformanceTimeStampOutput(),
61               mEnvironmentOptions.GetPerformanceStatsLoggingFrequency());
62
63 #if defined(NETWORK_LOGGING_ENABLED)
64   if( mNetworkControlEnabled )
65   {
66     mLoggingEnabled  = true;
67     mNetworkServer.Start();
68   }
69 #endif
70 }
71
72 PerformanceServer::~PerformanceServer()
73 {
74 #if defined(NETWORK_LOGGING_ENABLED)
75   if( mNetworkControlEnabled )
76   {
77     mNetworkServer.Stop();
78   }
79 #endif
80
81   if( mLogFunctionInstalled )
82   {
83     mEnvironmentOptions.UnInstallLogFunction();
84   }
85 }
86
87 void PerformanceServer::SetLogging( unsigned int statisticsLogOptions,
88                                     unsigned int timeStampOutput,
89                                     unsigned int logFrequency )
90 {
91   mStatisticsLogBitmask = statisticsLogOptions;
92   mPerformanceOutputBitmask = timeStampOutput;
93
94   mStatContextManager.SetLoggingLevel( mStatisticsLogBitmask, logFrequency);
95
96   if( ( mStatisticsLogBitmask == 0) && ( mPerformanceOutputBitmask == 0 ))
97   {
98     mLoggingEnabled = false;
99   }
100   else
101   {
102     mLoggingEnabled = true;
103   }
104 }
105
106 void PerformanceServer::SetLoggingFrequency( unsigned int logFrequency, ContextId contextId )
107 {
108   mStatContextManager.SetLoggingFrequency( logFrequency, contextId );
109 }
110
111 void PerformanceServer::EnableLogging( bool enable, ContextId contextId )
112 {
113   mStatContextManager.EnableLogging( enable, contextId );
114 }
115
116 PerformanceInterface::ContextId PerformanceServer::AddContext( const char* name )
117 {
118   // for adding custom contexts
119   return mStatContextManager.AddContext( name, PerformanceMarker::CUSTOM_EVENTS );
120 }
121
122 void PerformanceServer::RemoveContext( ContextId contextId )
123 {
124   mStatContextManager.RemoveContext( contextId );
125 }
126
127 void PerformanceServer::AddMarker( MarkerType markerType, ContextId contextId )
128 {
129   // called only for custom markers
130
131   if( !mLoggingEnabled )
132   {
133     return;
134   }
135
136   // Get the time stamp
137   uint64_t timeStamp = 0;
138   TimeService::GetNanoseconds( timeStamp );
139   timeStamp /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
140
141   // Create a marker
142   PerformanceMarker marker( markerType, FrameTimeStamp( 0, timeStamp ) );
143
144   // get the marker description for this context, e.g SIZE_NEGOTIATION_START
145   const char* const description = mStatContextManager.GetMarkerDescription( markerType, contextId );
146
147   // log it
148   LogMarker( marker, description );
149
150   // Add custom marker to statistics context manager
151   mStatContextManager.AddCustomMarker( marker, contextId );
152 }
153
154 void PerformanceServer::AddMarker( MarkerType markerType )
155 {
156   // called only for internal markers
157
158   if( !mLoggingEnabled )
159   {
160     return;
161   }
162
163   if( markerType == VSYNC )
164   {
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 )
168     {
169       mEnvironmentOptions.InstallLogFunction();
170       mLogFunctionInstalled = true;
171     }
172   }
173
174   // Get the time
175   uint64_t timeStamp = 0;
176   TimeService::GetNanoseconds( timeStamp );
177   timeStamp /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
178
179   // Create a marker
180   PerformanceMarker marker( markerType, FrameTimeStamp( 0, timeStamp ) );
181
182   // log it
183   LogMarker(marker, marker.GetName() );
184
185   // Add internal marker to statistics context manager
186   mStatContextManager.AddInternalMarker( marker );
187
188 }
189
190 void PerformanceServer::LogContextStatistics( const char* const text )
191 {
192   Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, text );
193 }
194
195 void PerformanceServer::LogMarker( const PerformanceMarker& marker, const char* const description )
196 {
197 #if defined(NETWORK_LOGGING_ENABLED)
198   // log to the network ( this is thread safe )
199   if( mNetworkControlEnabled )
200   {
201     mNetworkServer.TransmitMarker( marker, description );
202   }
203 #endif
204
205   // log to kernel trace
206   if( mPerformanceOutputBitmask & OUTPUT_KERNEL_TRACE )
207   {
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 );
212   }
213
214   // log to system trace
215   if( mPerformanceOutputBitmask & OUTPUT_SYSTEM_TRACE )
216   {
217     // System  tracing implementation may not be thread safe
218     Mutex::ScopedLock lock( mLogMutex );
219
220     mSystemTrace.Trace( marker, description );
221   }
222
223   // log to Dali log ( this is thread safe )
224   if ( mPerformanceOutputBitmask & OUTPUT_DALI_LOG )
225   {
226     Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
227                                     "%.6f (seconds), %s\n",
228                                     float( marker.GetTimeStamp().microseconds ) * MICROSECONDS_TO_SECOND,
229                                     description );
230
231   }
232 }
233
234
235 } // namespace Internal
236
237 } // namespace Adaptor
238
239 } // namespace Dali