2 * Copyright (c) 2015 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/stat-context-manager.h>
22 #include <dali/integration-api/debug.h>
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;
38 StatContextManager::StatContextManager(StatContextLogInterface& logInterface)
39 : mLogInterface(logInterface),
41 mStatisticsLogBitmask(0),
42 mLogFrequency(DEFAULT_LOG_FREQUENCY)
44 mStatContexts.Reserve(4); // intially reserve enough for 3 internal + 1 custom
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);
52 StatContextManager::~StatContextManager()
54 for(StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it)
56 StatContext* context = *it;
59 mStatContexts.Clear();
61 PerformanceInterface::ContextId StatContextManager::AddContext(const char* const name,
62 PerformanceMarker::MarkerFilter type)
64 unsigned int contextId = mNextContextId++;
66 DALI_ASSERT_DEBUG(NULL == GetContext(contextId));
68 // logging enabled by default
69 StatContext* statContext = new StatContext(contextId, name, type, mLogFrequency, mLogInterface);
71 // check to see if custom markers are enabled
72 if(!(mStatisticsLogBitmask & PerformanceInterface::LOG_CUSTOM_MARKERS))
74 statContext->EnableLogging(false);
77 mStatContexts.PushBack(statContext);
82 void StatContextManager::AddInternalMarker(const PerformanceMarker& marker)
84 // log to the stat contexts, can be called from multiple threads so we
86 Mutex::ScopedLock lock(mDataMutex);
87 for(StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it)
89 StatContext* context = *it;
90 context->ProcessInternalMarker(marker);
94 void StatContextManager::AddCustomMarker(const PerformanceMarker& marker, PerformanceInterface::ContextId contextId)
96 // log to the stat contexts, can be called from multiple threads so we
98 Mutex::ScopedLock lock(mDataMutex);
99 StatContext* context = GetContext(contextId);
102 context->ProcessCustomMarker(marker);
106 void StatContextManager::RemoveContext(PerformanceInterface::ContextId contextId)
108 for(StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it)
110 StatContext* context = *it;
112 if(context->GetId() == contextId)
115 mStatContexts.Erase(it);
121 void StatContextManager::EnableLogging(bool enable, PerformanceInterface::ContextId contextId)
123 StatContext* context = GetContext(contextId);
126 context->EnableLogging(enable);
130 void StatContextManager::SetLoggingLevel(unsigned int statisticsLogOptions, unsigned int logFrequency)
132 mStatisticsLogBitmask = statisticsLogOptions;
134 if(mStatisticsLogBitmask == PerformanceInterface::LOG_EVERYTHING)
136 mStatisticsLogBitmask = 0xFFFFFFFF; // enable everything
139 mLogFrequency = logFrequency;
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)
145 mLogFrequency = DEFAULT_LOG_FREQUENCY;
147 EnableLogging(mStatisticsLogBitmask & PerformanceInterface::LOG_UPDATE_RENDER, mUpdateStats);
148 EnableLogging(mStatisticsLogBitmask & PerformanceInterface::LOG_UPDATE_RENDER, mRenderStats);
149 EnableLogging(mStatisticsLogBitmask & PerformanceInterface::LOG_EVENT_PROCESS, mEventStats);
151 for(StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it)
153 StatContext* context = *it;
154 context->SetLogFrequency(mLogFrequency);
158 void StatContextManager::SetLoggingFrequency(unsigned int logFrequency,
159 PerformanceInterface::ContextId contextId)
161 StatContext* context = GetContext(contextId);
164 if(logFrequency == 0)
166 logFrequency = DEFAULT_LOG_FREQUENCY;
168 context->SetLogFrequency(logFrequency);
171 const char* StatContextManager::GetContextName(PerformanceInterface::ContextId contextId) const
173 StatContext* context = GetContext(contextId);
176 return context->GetName();
178 return "context not found";
181 const char* StatContextManager::GetMarkerDescription(PerformanceInterface::MarkerType type, PerformanceInterface::ContextId contextId) const
183 StatContext* context = GetContext(contextId);
186 return context->GetMarkerDescription(type);
188 return "context not found";
191 StatContext* StatContextManager::GetContext(PerformanceInterface::ContextId contextId) const
193 for(StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it)
195 StatContext* context = *it;
197 if(context->GetId() == contextId)
206 } // namespace Adaptor
208 } // namespace Internal