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.h>
25 #include <dali/integration-api/platform-abstraction.h>
33 #define TIME_FMT "%0.2f ms" // 2 decimal places, e.g. 5.34 ms
34 #define TOTAL_TIME_FMT "%0.1f secs" // 1 decimal place, e.g. 4.5 seconds
38 const unsigned int MILLISECONDS_PER_SECOND = 1000; ///< 1000 milliseconds per second
39 const char* const UNKNOWN_CONTEXT_NAME = "UNKNOWN_CONTEXT_NAME";
40 const unsigned int MICROSECONDS_PER_SECOND = 1000000; ///< 1000000 microseconds per second
41 const unsigned int CONTEXT_LOG_SIZE = 120;
45 StatContext::StatContext(unsigned int id,
46 const char* const contextName,
47 PerformanceMarker::MarkerFilter contextType,
48 unsigned int logFrequencySeconds,
49 StatContextLogInterface& logInterface)
50 : mInitialMarker(PerformanceInterface::VSYNC),
52 mLogInterface(logInterface),
53 mNamePlusStart(std::string(contextName) + "_START"),
54 mNamePlusEnd(std::string(contextName) + "_END"),
56 mLogFrequencyMicroseconds(logFrequencySeconds * MICROSECONDS_PER_SECOND),
57 mFilterType(contextType),
58 mLoggingEnabled(true),
59 mInitialMarkerSet(false)
61 mTempLogBuffer = new char[CONTEXT_LOG_SIZE];
64 StatContext::~StatContext()
66 delete[] mTempLogBuffer;
68 unsigned int StatContext::GetId() const
73 const char* StatContext::GetName() const
78 const char* StatContext::GetMarkerDescription(PerformanceInterface::MarkerType type) const
80 if(type == PerformanceInterface::START)
82 return mNamePlusStart.c_str();
84 else if(type == PerformanceInterface::END)
86 return mNamePlusEnd.c_str();
88 return UNKNOWN_CONTEXT_NAME;
90 void StatContext::SetLogFrequency(unsigned int logFrequencySeconds)
92 mLogFrequencyMicroseconds = logFrequencySeconds * MICROSECONDS_PER_SECOND;
95 void StatContext::EnableLogging(bool enableLogging)
97 mLoggingEnabled = enableLogging;
100 void StatContext::ProcessCustomMarker(const PerformanceMarker& marker)
102 // this marker has come from the application PerformanceLogger API
103 RecordMarker(marker);
106 void StatContext::ProcessInternalMarker(const PerformanceMarker& marker)
108 // this marker has come from DALi internal not the application
109 // see if this context is for update, render or event
110 if(marker.IsFilterEnabled(mFilterType))
112 RecordMarker(marker);
114 // V_SYNC is always processed
115 if(marker.GetType() == PerformanceInterface::VSYNC)
121 void StatContext::RecordMarker(const PerformanceMarker& marker)
123 if(marker.GetEventType() == PerformanceMarker::START_TIMED_EVENT)
125 mStats.StartTime(marker.GetTimeStamp());
127 else if(marker.GetEventType() == PerformanceMarker::END_TIMED_EVENT)
129 mStats.EndTime(marker.GetTimeStamp());
133 void StatContext::FrameTick(const PerformanceMarker& marker)
135 // wait until we've got some data
136 if(!mInitialMarkerSet)
138 mInitialMarker = marker;
139 mInitialMarkerSet = true;
142 // log out every mLogFrequency.
143 // check difference between first and last frame
144 unsigned int microseconds = PerformanceMarker::MicrosecondDiff(mInitialMarker, marker);
146 if(microseconds < mLogFrequencyMicroseconds)
155 mStats.Reset(); // reset data for statistics
156 mInitialMarkerSet = false; // need to restart the timer
159 void StatContext::LogMarker()
161 float mean, standardDeviation;
162 mStats.CalculateMean(mean, standardDeviation);
164 snprintf(mTempLogBuffer,
166 "%s, min " TIME_FMT ", max " TIME_FMT ", total (" TOTAL_TIME_FMT "), avg " TIME_FMT ", std dev " TIME_FMT "\n",
167 mName ? mName : UNKNOWN_CONTEXT_NAME,
168 mStats.GetMinTime() * MILLISECONDS_PER_SECOND,
169 mStats.GetMaxTime() * MILLISECONDS_PER_SECOND,
170 mStats.GetTotalTime(),
171 mean * MILLISECONDS_PER_SECOND,
172 standardDeviation * MILLISECONDS_PER_SECOND);
174 mLogInterface.LogContextStatistics(mTempLogBuffer);
177 } // namespace Adaptor
179 } // namespace Internal