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_auto_lock.h"
28 #include "util/timetaker.h"
29 #include "util/numeric.h" // paging()
30 #include "debug.h" // assert()
32 #define MAX_PROFILER_TEXT_ROWS 20
36 extern Profiler *g_profiler;
47 void add(const std::string &name, float value)
49 MutexAutoLock lock(m_mutex);
51 /* No average shall have been used; mark add used as -2 */
52 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
53 if(n == m_avgcounts.end())
54 m_avgcounts[name] = -2;
58 assert(n->second == -2);
62 std::map<std::string, float>::iterator n = m_data.find(name);
70 void avg(const std::string &name, float value)
72 MutexAutoLock lock(m_mutex);
73 int &count = m_avgcounts[name];
76 count = MYMAX(count, 0) + 1;
77 m_data[name] += value;
82 MutexAutoLock lock(m_mutex);
83 for(std::map<std::string, float>::iterator
85 i != m_data.end(); ++i)
92 void print(std::ostream &o)
97 float getValue(const std::string &name) const
99 std::map<std::string, float>::const_iterator numerator = m_data.find(name);
100 if (numerator == m_data.end())
103 std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
104 if (denominator != m_avgcounts.end()){
105 if (denominator->second >= 1)
106 return numerator->second / denominator->second;
109 return numerator->second;
112 void printPage(std::ostream &o, u32 page, u32 pagecount)
114 MutexAutoLock lock(m_mutex);
116 u32 minindex, maxindex;
117 paging(m_data.size(), page, pagecount, minindex, maxindex);
119 for (std::map<std::string, float>::const_iterator i = m_data.begin();
120 i != m_data.end(); ++i) {
131 std::map<std::string, int>::const_iterator n = m_avgcounts.find(i->first);
132 if (n != m_avgcounts.end()) {
134 avgcount = n->second;
136 o << " " << i->first << ": ";
138 s32 space = clampsize - i->first.size();
139 for(s32 j = 0; j < space; j++) {
140 if (j % 2 == 0 && j < space - 1)
145 o << (i->second / avgcount);
150 typedef std::map<std::string, float> GraphValues;
152 void graphAdd(const std::string &id, float value)
154 MutexAutoLock lock(m_mutex);
155 std::map<std::string, float>::iterator i =
156 m_graphvalues.find(id);
157 if(i == m_graphvalues.end())
158 m_graphvalues[id] = value;
162 void graphGet(GraphValues &result)
164 MutexAutoLock lock(m_mutex);
165 result = m_graphvalues;
166 m_graphvalues.clear();
169 void remove(const std::string& name)
171 MutexAutoLock lock(m_mutex);
172 m_avgcounts.erase(name);
178 std::map<std::string, float> m_data;
179 std::map<std::string, int> m_avgcounts;
180 std::map<std::string, float> m_graphvalues;
183 enum ScopeProfilerType{
192 ScopeProfiler(Profiler *profiler, const std::string &name,
193 ScopeProfilerType type = SPT_ADD);
196 Profiler *m_profiler = nullptr;
198 TimeTaker *m_timer = nullptr;
199 enum ScopeProfilerType m_type;