(PanGesture)Added environment variable to change pan gesture prediction mode
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include "performance-server.h"
19
20 // INTERNAL INCLUDES
21 #include <base/environment-options.h>
22
23 namespace Dali
24 {
25
26 namespace Internal
27 {
28
29 namespace Adaptor
30 {
31
32 #define TIME_FMT "%0.2f ms" // 2 decimal places, e.g. 5.34 ms
33 #define TOTAL_TIME_FMT "%0.1f secs" // 1 decimal place, e.g. 4.5 seconds
34
35
36 namespace
37 {
38 const unsigned int DEFAULT_LOG_FREQUENCEY = 2;        ///< default log frequency = 2
39 const unsigned int MILLISECONDS_PER_SECOND = 1000;    ///< 1000 milliseconds per second
40 const unsigned int MICROSECONDS_PER_SECOND = 1000000; ///< 1000000 microseconds per second
41 }
42
43
44 PerformanceServer::PerformanceServer( AdaptorInternalServices& adaptorServices,
45                                       const EnvironmentOptions& environmentOptions)
46 :mLoggingEnabled( false),
47  mLogFunctionInstalled( false ),
48  mLogFrequencyMicroseconds( 0),
49  mPlatformAbstraction( adaptorServices.GetPlatformAbstractionInterface() ),
50  mEnvironmentOptions(environmentOptions),
51  mKernelTrace( adaptorServices.GetKernelTraceInterface() )
52 {
53   SetLogging( mEnvironmentOptions.GetPerformanceLoggingLevel(), mEnvironmentOptions.GetFrameRateLoggingFrequency());
54 }
55
56 PerformanceServer::~PerformanceServer()
57 {
58   if( mLogFunctionInstalled )
59   {
60     mEnvironmentOptions.UnInstallLogFunction();
61   }
62 }
63 void PerformanceServer::SetLogging( unsigned int level, unsigned int interval)
64 {
65   if( level == 0)
66   {
67     mLoggingEnabled = false;
68     return;
69   }
70   mLogLevel = level;
71
72   mLogFrequencyMicroseconds = interval * MICROSECONDS_PER_SECOND;
73
74   if( mLogFrequencyMicroseconds == 0 )
75   {
76     mLogFrequencyMicroseconds = DEFAULT_LOG_FREQUENCEY * MICROSECONDS_PER_SECOND;
77   }
78   mLoggingEnabled = true;
79
80 }
81
82 void PerformanceServer::AddMarker( PerformanceMarker::MarkerType markerType )
83 {
84   if( !mLoggingEnabled )
85   {
86     return;
87   }
88
89   unsigned int seconds(0);
90   unsigned int microseconds(0);
91
92   // get the time
93   mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
94
95   // create a marker
96   PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ));
97
98   AddMarkerToLog( marker );
99 }
100
101 void PerformanceServer::AddMarkerToLog( PerformanceMarker marker )
102 {
103   // Add Marker can be called from any thread
104   boost::mutex::scoped_lock sharedDatalock( mDataMutex );
105
106   // store the marker
107   mMarkers.PushBack( marker );
108
109   if( mLogLevel & LOG_EVENTS_TO_KERNEL )
110   {
111     mKernelTrace.Trace(marker.GetName());
112   }
113
114   // only log on the v-sync thread, so we have less impact on update/render
115   if( marker.GetType() != PerformanceMarker::V_SYNC )
116   {
117     return;
118   }
119
120   // log out every mLogFrequency.
121   // check difference between first and last frame
122   unsigned int microseconds = PerformanceMarker::MicrosecondDiff( mMarkers[0], marker );
123
124   if( microseconds  >=  mLogFrequencyMicroseconds )
125   {
126     LogMarkers( );
127     mMarkers.Clear();
128
129     // reset data for update / render statistics
130     mUpdateStats.Reset();
131     mRenderStats.Reset();
132     mEventStats.Reset();
133   }
134 }
135
136 void PerformanceServer::LogMarker(const char* name, const FrameTimeStats& frameStats)
137 {
138   // make sure log function is installed, note this will be called only from v-sync thread
139   // if the v-sync thread has already installed one, it won't make any difference.
140   if(! mLogFunctionInstalled )
141   {
142     mEnvironmentOptions.InstallLogFunction();
143     mLogFunctionInstalled = true;
144   }
145
146   // this will always log regardless of debug / release mode
147   Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
148                                     "%s , min " TIME_FMT ", max " TIME_FMT ", total (" TOTAL_TIME_FMT "), avg " TIME_FMT "\n",
149                                      name,
150                                      frameStats.GetMinTime() * MILLISECONDS_PER_SECOND,
151                                      frameStats.GetMaxTime() * MILLISECONDS_PER_SECOND,
152                                      frameStats.GetTotalTime(),
153                                      frameStats.GetRollingAverageTime() * MILLISECONDS_PER_SECOND);
154 }
155
156 void PerformanceServer::LogMarkers()
157 {
158   // insert time stamps into a frame-time-stats object, based on type
159   for( MarkerVector::SizeType i = 0; i < mMarkers.Size(); ++i)
160   {
161     const PerformanceMarker& marker = mMarkers[i];
162     switch( marker.GetType() )
163     {
164       case PerformanceMarker::UPDATE_START:
165       {
166         mUpdateStats.StartTime( marker.GetTimeStamp() );
167         break;
168       }
169       case PerformanceMarker::UPDATE_END:
170       {
171         mUpdateStats.EndTime( marker.GetTimeStamp() );
172         break;
173       }
174       case PerformanceMarker::RENDER_START:
175       {
176         mRenderStats.StartTime( marker.GetTimeStamp() );
177         break;
178       }
179       case PerformanceMarker::RENDER_END:
180       {
181         mRenderStats.EndTime( marker.GetTimeStamp() );
182         break;
183       }
184       case PerformanceMarker::PROCESS_EVENTS_START:
185       {
186         mEventStats.StartTime( marker.GetTimeStamp() );
187         break;
188       }
189       case PerformanceMarker::PROCESS_EVENTS_END:
190       {
191         mEventStats.EndTime( marker.GetTimeStamp() );
192         break;
193       }
194       default:
195       {
196         break;
197       }
198     }
199   }
200   if( mLogLevel & LOG_UPDATE_RENDER )
201   {
202     LogMarker("Update",mUpdateStats);
203     LogMarker("Render",mRenderStats);
204   }
205   if( mLogLevel & LOG_EVENT_PROCESS )
206   {
207     LogMarker("Event",mEventStats);
208   }
209
210 }
211
212 } // namespace Internal
213
214 } // namespace Adaptor
215
216 } // namespace Dali