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