Merge branch devel/master (1.0.49) into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / base / performance-logging / performance-server.cpp
1 /*
2  * Copyright (c) 2014 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
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
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 )
48 {
49   SetLogging( mEnvironmentOptions.GetPerformanceStatsLoggingOptions(),
50               mEnvironmentOptions.GetPerformanceTimeStampOutput(),
51               mEnvironmentOptions.GetPerformanceStatsLoggingFrequency());
52
53   if( mNetworkControlEnabled )
54   {
55     mLoggingEnabled  = true;
56     mNetworkServer.Start();
57   }
58 }
59
60 PerformanceServer::~PerformanceServer()
61 {
62   if( mNetworkControlEnabled )
63   {
64     mNetworkServer.Stop();
65   }
66
67   if( mLogFunctionInstalled )
68   {
69     mEnvironmentOptions.UnInstallLogFunction();
70   }
71 }
72
73 void PerformanceServer::SetLogging( unsigned int statisticsLogOptions,
74                                     unsigned int timeStampOutput,
75                                     unsigned int logFrequency )
76 {
77   mStatisticsLogBitmask = statisticsLogOptions;
78   mPerformanceOutputBitmask = timeStampOutput;
79
80   mStatContextManager.SetLoggingLevel( mStatisticsLogBitmask, logFrequency);
81
82   if( ( mStatisticsLogBitmask == 0) && ( mPerformanceOutputBitmask == 0 ))
83   {
84     mLoggingEnabled = false;
85   }
86   else
87   {
88     mLoggingEnabled = true;
89   }
90 }
91
92 void PerformanceServer::SetLoggingFrequency( unsigned int logFrequency, ContextId contextId )
93 {
94   mStatContextManager.SetLoggingFrequency( logFrequency, contextId );
95 }
96
97 void PerformanceServer::EnableLogging( bool enable, ContextId contextId )
98 {
99   mStatContextManager.EnableLogging( enable, contextId );
100 }
101
102 PerformanceInterface::ContextId PerformanceServer::AddContext( const char* name )
103 {
104   // for adding custom contexts
105   return mStatContextManager.AddContext( name, PerformanceMarker::CUSTOM_EVENTS );
106 }
107
108 void PerformanceServer::RemoveContext( ContextId contextId )
109 {
110   mStatContextManager.RemoveContext( contextId );
111 }
112
113 void PerformanceServer::AddMarker( MarkerType markerType, ContextId contextId )
114 {
115   // called only for custom markers
116
117   if( !mLoggingEnabled )
118   {
119     return;
120   }
121
122   // Get the time stamp
123   unsigned int seconds = 0;
124   unsigned int microseconds = 0;
125   mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
126
127   // Create a marker
128   PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
129
130   // get the marker description for this context, e.g SIZE_NEGOTIATION_START
131   const char* const description = mStatContextManager.GetMarkerDescription( markerType, contextId );
132
133   // log it
134   LogMarker( marker, description );
135
136   // Add custom marker to statistics context manager
137   mStatContextManager.AddCustomMarker( marker, contextId );
138 }
139
140 void PerformanceServer::AddMarker( MarkerType markerType )
141 {
142   // called only for internal markers
143
144   if( !mLoggingEnabled )
145   {
146     return;
147   }
148
149   if( markerType == VSYNC )
150   {
151     // make sure log function is installed, note this will be called only from v-sync thread
152     // if the v-sync thread has already installed one, it won't make any difference.
153     if( ! mLogFunctionInstalled )
154     {
155       mEnvironmentOptions.InstallLogFunction();
156       mLogFunctionInstalled = true;
157     }
158   }
159
160   // Get the time
161   unsigned int seconds = 0;
162   unsigned int microseconds = 0;
163   mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
164
165   // Create a marker
166   PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
167
168   // log it
169   LogMarker(marker, marker.GetName() );
170
171   // Add internal marker to statistics context manager
172   mStatContextManager.AddInternalMarker( marker );
173
174 }
175
176 void PerformanceServer::LogContextStatistics( const char* const text )
177 {
178   Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, text );
179 }
180
181 void PerformanceServer::LogMarker( const PerformanceMarker& marker, const char* const description )
182 {
183   // log to the network ( this is thread safe )
184   if( mNetworkControlEnabled )
185   {
186     mNetworkServer.TransmitMarker( marker, description );
187   }
188
189   // log to kernel trace
190   if( mPerformanceOutputBitmask & OUTPUT_KERNEL_TRACE )
191   {
192     // Kernel tracing implementation may not be thread safe
193     Mutex::ScopedLock lock( mLogMutex );
194     // description will be something like UPDATE_START or UPDATE_END
195     mKernelTrace.Trace( marker, description );
196   }
197
198   // log to system trace
199   if( mPerformanceOutputBitmask & OUTPUT_SYSTEM_TRACE )
200   {
201     // System  tracing implementation may not be thread safe
202     Mutex::ScopedLock lock( mLogMutex );
203
204     mSystemTrace.Trace( marker, description );
205   }
206
207   // log to Dali log ( this is thread safe )
208   if ( mPerformanceOutputBitmask & OUTPUT_DALI_LOG )
209   {
210     Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
211                                     "%d.%06d (seconds), %s\n",
212                                     marker.GetTimeStamp().seconds,
213                                     marker.GetTimeStamp().microseconds,
214                                     description);
215   }
216 }
217
218
219 } // namespace Internal
220
221 } // namespace Adaptor
222
223 } // namespace Dali