3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef PROFILER_HEADER
21 #define PROFILER_HEADER
23 #include "irrlichttypes.h"
27 #include "threading/mutex.h"
28 #include "threading/mutex_auto_lock.h"
29 #include "util/timetaker.h"
30 #include "util/numeric.h" // paging()
31 #include "debug.h" // assert()
33 #define MAX_PROFILER_TEXT_ROWS 20
37 extern Profiler *g_profiler;
50 void add(const std::string &name, float value)
52 MutexAutoLock lock(m_mutex);
54 /* No average shall have been used; mark add used as -2 */
55 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
56 if(n == m_avgcounts.end())
57 m_avgcounts[name] = -2;
61 assert(n->second == -2);
65 std::map<std::string, float>::iterator n = m_data.find(name);
73 void avg(const std::string &name, float value)
75 MutexAutoLock lock(m_mutex);
76 int &count = m_avgcounts[name];
79 count = MYMAX(count, 0) + 1;
80 m_data[name] += value;
85 MutexAutoLock lock(m_mutex);
86 for(std::map<std::string, float>::iterator
88 i != m_data.end(); ++i)
95 void print(std::ostream &o)
100 float getValue(const std::string &name) const
102 std::map<std::string, float>::const_iterator numerator = m_data.find(name);
103 if (numerator == m_data.end())
106 std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
107 if (denominator != m_avgcounts.end()){
108 if (denominator->second >= 1)
109 return numerator->second / denominator->second;
112 return numerator->second;
115 void printPage(std::ostream &o, u32 page, u32 pagecount)
117 MutexAutoLock lock(m_mutex);
119 u32 minindex, maxindex;
120 paging(m_data.size(), page, pagecount, minindex, maxindex);
122 for (std::map<std::string, float>::const_iterator i = m_data.begin();
123 i != m_data.end(); ++i) {
134 std::map<std::string, int>::const_iterator n = m_avgcounts.find(i->first);
135 if (n != m_avgcounts.end()) {
137 avgcount = n->second;
139 o << " " << i->first << ": ";
141 s32 space = clampsize - i->first.size();
142 for(s32 j = 0; j < space; j++) {
143 if (j % 2 == 0 && j < space - 1)
148 o << (i->second / avgcount);
153 typedef std::map<std::string, float> GraphValues;
155 void graphAdd(const std::string &id, float value)
157 MutexAutoLock lock(m_mutex);
158 std::map<std::string, float>::iterator i =
159 m_graphvalues.find(id);
160 if(i == m_graphvalues.end())
161 m_graphvalues[id] = value;
165 void graphGet(GraphValues &result)
167 MutexAutoLock lock(m_mutex);
168 result = m_graphvalues;
169 m_graphvalues.clear();
172 void remove(const std::string& name)
174 MutexAutoLock lock(m_mutex);
175 m_avgcounts.erase(name);
181 std::map<std::string, float> m_data;
182 std::map<std::string, int> m_avgcounts;
183 std::map<std::string, float> m_graphvalues;
186 enum ScopeProfilerType{
195 ScopeProfiler(Profiler *profiler, const std::string &name,
196 enum ScopeProfilerType type = SPT_ADD):
197 m_profiler(profiler),
203 m_timer = new TimeTaker(m_name.c_str());
206 ScopeProfiler(Profiler *profiler, const char *name,
207 enum ScopeProfilerType type = SPT_ADD):
208 m_profiler(profiler),
214 m_timer = new TimeTaker(m_name.c_str());
220 float duration_ms = m_timer->stop(true);
221 float duration = duration_ms / 1000.0;
225 m_profiler->add(m_name, duration);
228 m_profiler->avg(m_name, duration);
231 m_profiler->graphAdd(m_name, duration);
239 Profiler *m_profiler;
242 enum ScopeProfilerType m_type;