2 * Copyright (c) 2021 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>
36 const unsigned int NANOSECONDS_PER_MICROSECOND = 1000u;
37 const float MICROSECONDS_TO_SECOND = 1e-6;
38 } // unnamed namespace
40 PerformanceServer::PerformanceServer(AdaptorInternalServices& adaptorServices,
41 const EnvironmentOptions& environmentOptions)
42 : mEnvironmentOptions(environmentOptions),
43 mKernelTrace(adaptorServices.GetKernelTraceInterface()),
44 mSystemTrace(adaptorServices.GetSystemTraceInterface()),
46 #if defined(NETWORK_LOGGING_ENABLED)
47 mNetworkServer(adaptorServices, environmentOptions),
48 mNetworkControlEnabled(mEnvironmentOptions.GetNetworkControlMode()),
50 mStatContextManager(*this),
51 mStatisticsLogBitmask(0),
52 mPerformanceOutputBitmask(0),
53 mLoggingEnabled(false),
54 mLogFunctionInstalled(false)
56 SetLogging(mEnvironmentOptions.GetPerformanceStatsLoggingOptions(),
57 mEnvironmentOptions.GetPerformanceTimeStampOutput(),
58 mEnvironmentOptions.GetPerformanceStatsLoggingFrequency());
60 #if defined(NETWORK_LOGGING_ENABLED)
61 if(mNetworkControlEnabled)
63 mLoggingEnabled = true;
64 mNetworkServer.Start();
69 PerformanceServer::~PerformanceServer()
71 #if defined(NETWORK_LOGGING_ENABLED)
72 if(mNetworkControlEnabled)
74 mNetworkServer.Stop();
78 if(mLogFunctionInstalled)
80 mEnvironmentOptions.UnInstallLogFunction();
84 void PerformanceServer::SetLogging(unsigned int statisticsLogOptions,
85 unsigned int timeStampOutput,
86 unsigned int logFrequency)
88 mStatisticsLogBitmask = statisticsLogOptions;
89 mPerformanceOutputBitmask = timeStampOutput;
91 mStatContextManager.SetLoggingLevel(mStatisticsLogBitmask, logFrequency);
93 if((mStatisticsLogBitmask == 0) && (mPerformanceOutputBitmask == 0))
95 mLoggingEnabled = false;
99 mLoggingEnabled = true;
103 void PerformanceServer::SetLoggingFrequency(unsigned int logFrequency, ContextId contextId)
105 mStatContextManager.SetLoggingFrequency(logFrequency, contextId);
108 void PerformanceServer::EnableLogging(bool enable, ContextId contextId)
110 mStatContextManager.EnableLogging(enable, contextId);
113 PerformanceInterface::ContextId PerformanceServer::AddContext(const char* name)
115 // for adding custom contexts
116 return mStatContextManager.AddContext(name, PerformanceMarker::CUSTOM_EVENTS);
119 void PerformanceServer::RemoveContext(ContextId contextId)
121 mStatContextManager.RemoveContext(contextId);
124 void PerformanceServer::AddMarker(MarkerType markerType, ContextId contextId)
126 // called only for custom markers
133 // Get the time stamp
134 uint64_t timeStamp = 0;
135 TimeService::GetNanoseconds(timeStamp);
136 timeStamp /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
139 PerformanceMarker marker(markerType, FrameTimeStamp(0, timeStamp));
141 // get the marker description for this context, e.g SIZE_NEGOTIATION_START
142 const char* const description = mStatContextManager.GetMarkerDescription(markerType, contextId);
145 LogMarker(marker, description);
147 // Add custom marker to statistics context manager
148 mStatContextManager.AddCustomMarker(marker, contextId);
151 void PerformanceServer::AddMarker(MarkerType markerType)
153 // called only for internal markers
160 if(markerType == VSYNC)
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)
166 mEnvironmentOptions.InstallLogFunction();
167 mLogFunctionInstalled = true;
172 uint64_t timeStamp = 0;
173 TimeService::GetNanoseconds(timeStamp);
174 timeStamp /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
177 PerformanceMarker marker(markerType, FrameTimeStamp(0, timeStamp));
180 LogMarker(marker, marker.GetName());
182 // Add internal marker to statistics context manager
183 mStatContextManager.AddInternalMarker(marker);
186 void PerformanceServer::LogContextStatistics(const char* const text)
188 Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, text);
191 void PerformanceServer::LogMarker(const PerformanceMarker& marker, const char* const description)
193 #if defined(NETWORK_LOGGING_ENABLED)
194 // log to the network ( this is thread safe )
195 if(mNetworkControlEnabled)
197 mNetworkServer.TransmitMarker(marker, description);
201 // log to kernel trace
202 if(mPerformanceOutputBitmask & OUTPUT_KERNEL_TRACE)
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);
210 // log to system trace
211 if(mPerformanceOutputBitmask & OUTPUT_SYSTEM_TRACE)
213 // System tracing implementation may not be thread safe
214 Mutex::ScopedLock lock(mLogMutex);
216 mSystemTrace.Trace(marker, description);
219 // log to Dali log ( this is thread safe )
220 if(mPerformanceOutputBitmask & OUTPUT_DALI_LOG)
222 Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo,
223 "%.6f (seconds), %s\n",
224 float(marker.GetTimeStamp().microseconds) * MICROSECONDS_TO_SECOND,
229 } // namespace Adaptor
231 } // namespace Internal