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