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 "stat-context-manager.h"
22 #include <dali/integration-api/debug.h>
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;
41 StatContextManager::StatContextManager( StatContextLogInterface& logInterface )
42 : mLogInterface( logInterface ),
44 mStatisticsLogBitmask(0),
45 mLogFrequency( DEFAULT_LOG_FREQUENCY )
48 mStatContexts.Reserve(4); // intially reserve enough for 3 internal + 1 custom
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 );
57 StatContextManager::~StatContextManager()
59 for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
61 StatContext* context = *it;
64 mStatContexts.Clear();
66 PerformanceInterface::ContextId StatContextManager::AddContext( const char* const name,
67 PerformanceMarker::MarkerFilter type )
69 unsigned int contextId = mNextContextId++;
71 DALI_ASSERT_DEBUG( NULL == GetContext( contextId ) );
73 // logging enabled by default
74 StatContext* statContext = new StatContext( contextId, name, type, mLogFrequency , mLogInterface );
76 // check to see if custom markers are enabled
77 if( ! ( mStatisticsLogBitmask & PerformanceInterface::LOG_CUSTOM_MARKERS ) )
79 statContext->EnableLogging( false );
82 mStatContexts.PushBack( statContext );
87 void StatContextManager::AddInternalMarker( const PerformanceMarker& marker )
89 // log to the stat contexts, can be called from multiple threads so we
91 Mutex::ScopedLock lock( mDataMutex );
92 for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
94 StatContext* context = *it;
95 context->ProcessInternalMarker( marker );
99 void StatContextManager::AddCustomMarker( const PerformanceMarker& marker, PerformanceInterface::ContextId contextId )
101 // log to the stat contexts, can be called from multiple threads so we
103 Mutex::ScopedLock lock( mDataMutex );
104 StatContext* context = GetContext( contextId );
107 context->ProcessCustomMarker( marker );
111 void StatContextManager::RemoveContext(PerformanceInterface::ContextId contextId )
113 for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
115 StatContext* context = *it;
117 if( context->GetId() == contextId )
120 mStatContexts.Erase( it );
127 void StatContextManager::EnableLogging( bool enable, PerformanceInterface::ContextId contextId )
129 StatContext* context = GetContext( contextId );
132 context->EnableLogging( enable );
136 void StatContextManager::SetLoggingLevel( unsigned int statisticsLogOptions, unsigned int logFrequency)
138 mStatisticsLogBitmask = statisticsLogOptions;
140 if( mStatisticsLogBitmask == PerformanceInterface::LOG_EVERYTHING )
142 mStatisticsLogBitmask = 0xFFFFFFFF; // enable everything
145 mLogFrequency = logFrequency;
147 // currently uses DALI_LOG_PERFORMANCE_STATS_FREQ environment variable to determine to log frequency
148 // if it's not set it will be zero
149 if( mLogFrequency == 0 )
151 mLogFrequency = DEFAULT_LOG_FREQUENCY;
153 EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_UPDATE_RENDER, mUpdateStats );
154 EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_UPDATE_RENDER, mRenderStats );
155 EnableLogging( mStatisticsLogBitmask & PerformanceInterface::LOG_EVENT_PROCESS, mEventStats );
157 for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
159 StatContext* context = *it;
160 context->SetLogFrequency( mLogFrequency );
164 void StatContextManager::SetLoggingFrequency( unsigned int logFrequency,
165 PerformanceInterface::ContextId contextId )
167 StatContext* context = GetContext( contextId );
170 if( logFrequency == 0 )
172 logFrequency = DEFAULT_LOG_FREQUENCY;
174 context->SetLogFrequency( logFrequency );
177 const char* const StatContextManager::GetContextName(PerformanceInterface::ContextId contextId) const
179 StatContext* context = GetContext(contextId);
182 return context->GetName();
184 return "context not found";
187 const char* const StatContextManager::GetMarkerDescription( PerformanceInterface::MarkerType type, PerformanceInterface::ContextId contextId ) const
189 StatContext* context = GetContext(contextId);
192 return context->GetMarkerDescription( type );
194 return "context not found";
198 StatContext* StatContextManager::GetContext( PerformanceInterface::ContextId contextId ) const
200 for( StatContexts::Iterator it = mStatContexts.Begin(), itEnd = mStatContexts.End(); it != itEnd; ++it )
202 StatContext* context = *it;
204 if( context->GetId() == contextId )
214 } // namespace Internal
216 } // namespace Adaptor