22a67d1f5650169d8a483690555fd4a2878b5a94
[platform/core/uifw/dali-adaptor.git] / adaptors / base / performance-logging / statistics / stat-context-manager.cpp
1 /*
2  * Copyright (c) 2015 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 "stat-context-manager.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace Adaptor
31 {
32
33 namespace
34 {
35 const char* const UPDATE_CONTEXT_NAME = "Update";
36 const char* const RENDER_CONTEXT_NAME = "Render";
37 const char* const EVENT_CONTEXT_NAME = "Event";
38 const unsigned int DEFAULT_LOG_FREQUENCY = 2;
39 }
40
41 StatContextManager::StatContextManager( StatContextLogInterface& logInterface )
42 : mLogInterface( logInterface ),
43   mNextContextId( 0 ),
44   mStatisticsLogBitmask(0),
45   mLogFrequency( DEFAULT_LOG_FREQUENCY )
46 {
47
48   mStatContexts.Reserve(4); // intially reserve enough for 3 internal + 1 custom
49
50   // Add defaults
51   mUpdateStats = AddContext( UPDATE_CONTEXT_NAME, PerformanceMarker::UPDATE );
52   mRenderStats = AddContext( RENDER_CONTEXT_NAME, PerformanceMarker::RENDER );
53   mEventStats = AddContext( EVENT_CONTEXT_NAME,   PerformanceMarker::EVENT_PROCESS );
54
55 }
56
57 StatContextManager::~StatContextManager()
58 {
59   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
60   {
61     StatContext* context = *it;
62     delete context;
63   }
64   mStatContexts.Clear();
65 }
66 PerformanceInterface::ContextId StatContextManager::AddContext( const char* const name,
67                                                                 PerformanceMarker::MarkerFilter type  )
68 {
69   unsigned int contextId = mNextContextId++;
70
71   DALI_ASSERT_DEBUG( NULL == GetContext( contextId ) );
72
73   // logging enabled by default
74   StatContext* statContext = new StatContext( contextId, name, type,  mLogFrequency , mLogInterface );
75
76   // check to see if custom markers are enabled
77   if( ! ( mStatisticsLogBitmask & PerformanceInterface::LOG_CUSTOM_MARKERS ) )
78   {
79     statContext->EnableLogging( false );
80   }
81
82   mStatContexts.PushBack( statContext );
83
84   return contextId;
85 }
86
87 void StatContextManager::AddInternalMarker( const PerformanceMarker& marker )
88 {
89   // log to the stat contexts, can be called from multiple threads so we
90   // protect the data
91   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
92   {
93     StatContext* context = *it;
94     context->ProcessInternalMarker( marker );
95   }
96 }
97
98 void StatContextManager::AddCustomMarker( const PerformanceMarker& marker, PerformanceInterface::ContextId contextId )
99 {
100   // log to the stat contexts, can be called from multiple threads so we
101   // protect the data
102   StatContext* context = GetContext( contextId );
103   if( context )
104   {
105     context->ProcessCustomMarker( marker );
106   }
107 }
108
109 void StatContextManager::RemoveContext(PerformanceInterface::ContextId contextId )
110 {
111   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
112   {
113     StatContext* context = *it;
114
115     if( context->GetId() == contextId )
116     {
117       delete context;
118       mStatContexts.Erase( it );
119       return;
120     }
121   }
122 }
123
124
125 void StatContextManager::EnableLogging( bool enable, PerformanceInterface::ContextId contextId )
126 {
127   StatContext* context = GetContext( contextId );
128   if( context )
129   {
130     context->EnableLogging( enable );
131   }
132 }
133
134 void StatContextManager::SetLoggingLevel(  unsigned int statisticsLogOptions, unsigned int logFrequency)
135 {
136   mStatisticsLogBitmask = statisticsLogOptions;
137
138   if( mStatisticsLogBitmask == PerformanceInterface::LOG_EVERYTHING )
139   {
140     mStatisticsLogBitmask = 0xFFFFFFFF; // enable everything
141   }
142
143   mLogFrequency = logFrequency;
144
145   // currently uses DALI_LOG_PERFORMANCE_STATS_FREQ environment variable to determine to log frequency
146   // if it's not set it will be zero
147   if( mLogFrequency == 0 )
148   {
149     mLogFrequency = DEFAULT_LOG_FREQUENCY;
150   }
151   EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_UPDATE_RENDER, mUpdateStats );
152   EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_UPDATE_RENDER, mRenderStats );
153   EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_EVENT_PROCESS, mEventStats );
154
155   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
156   {
157      StatContext* context = *it;
158      context->SetLogFrequency( mLogFrequency );
159   }
160 }
161
162 void StatContextManager::SetLoggingFrequency( unsigned int logFrequency,
163                                               PerformanceInterface::ContextId contextId  )
164 {
165   StatContext* context = GetContext( contextId );
166   if( context )
167   {
168     if( logFrequency == 0 )
169     {
170       logFrequency = DEFAULT_LOG_FREQUENCY;
171     }
172     context->SetLogFrequency( logFrequency );
173   }
174 }
175 const char* const StatContextManager::GetContextName(PerformanceInterface::ContextId contextId) const
176 {
177   StatContext* context = GetContext(contextId);
178   if( context )
179   {
180     return context->GetName();
181   }
182   return "context not found";
183 }
184
185 const char* const StatContextManager::GetMarkerDescription( PerformanceInterface::MarkerType type, PerformanceInterface::ContextId contextId ) const
186 {
187   StatContext* context = GetContext(contextId);
188   if( context )
189   {
190     return context->GetMarkerDescription( type );
191   }
192   return "context not found";
193 }
194
195
196 StatContext* StatContextManager::GetContext( PerformanceInterface::ContextId contextId ) const
197 {
198   for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
199   {
200     StatContext* context = *it;
201
202     if( context->GetId() == contextId )
203     {
204       return context;
205     }
206   }
207
208   return NULL;
209 }
210
211
212 } // namespace Internal
213
214 } // namespace Adaptor
215
216 } // namespace Dali
217
218