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>::iterator
124 i != m_data.end(); ++i)
136 std::string name = i->first;
138 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
139 if(n != m_avgcounts.end()){
141 avgcount = n->second;
145 s32 space = clampsize - name.size();
146 for(s32 j=0; j<space; j++)
148 if(j%2 == 0 && j < space - 1)
153 o<<(i->second / avgcount);
158 typedef std::map<std::string, float> GraphValues;
160 void graphAdd(const std::string &id, float value)
162 MutexAutoLock lock(m_mutex);
163 std::map<std::string, float>::iterator i =
164 m_graphvalues.find(id);
165 if(i == m_graphvalues.end())
166 m_graphvalues[id] = value;
170 void graphGet(GraphValues &result)
172 MutexAutoLock lock(m_mutex);
173 result = m_graphvalues;
174 m_graphvalues.clear();
177 void remove(const std::string& name)
179 MutexAutoLock lock(m_mutex);
180 m_avgcounts.erase(name);
186 std::map<std::string, float> m_data;
187 std::map<std::string, int> m_avgcounts;
188 std::map<std::string, float> m_graphvalues;
191 enum ScopeProfilerType{
200 ScopeProfiler(Profiler *profiler, const std::string &name,
201 enum ScopeProfilerType type = SPT_ADD):
202 m_profiler(profiler),
208 m_timer = new TimeTaker(m_name.c_str());
211 ScopeProfiler(Profiler *profiler, const char *name,
212 enum ScopeProfilerType type = SPT_ADD):
213 m_profiler(profiler),
219 m_timer = new TimeTaker(m_name.c_str());
225 float duration_ms = m_timer->stop(true);
226 float duration = duration_ms / 1000.0;
230 m_profiler->add(m_name, duration);
233 m_profiler->avg(m_name, duration);
236 m_profiler->graphAdd(m_name, duration);
244 Profiler *m_profiler;
247 enum ScopeProfilerType m_type;