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_bloated.h"
26 #include <jmutexautolock.h>
28 #include "util/timetaker.h"
29 #include "util/numeric.h" // paging()
43 void add(const std::string &name, float value)
45 JMutexAutoLock lock(m_mutex);
47 /* No average shall have been used; mark add used as -2 */
48 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
49 if(n == m_avgcounts.end())
50 m_avgcounts[name] = -2;
54 assert(n->second == -2);
58 std::map<std::string, float>::iterator n = m_data.find(name);
66 void avg(const std::string &name, float value)
68 JMutexAutoLock lock(m_mutex);
70 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
71 if(n == m_avgcounts.end())
72 m_avgcounts[name] = 1;
74 /* No add shall have been used */
75 assert(n->second != -2);
76 n->second = (std::max)(n->second, 0) + 1;
80 std::map<std::string, float>::iterator n = m_data.find(name);
90 JMutexAutoLock lock(m_mutex);
91 for(std::map<std::string, float>::iterator
93 i != m_data.end(); ++i)
100 void print(std::ostream &o)
105 void printPage(std::ostream &o, u32 page, u32 pagecount)
107 JMutexAutoLock lock(m_mutex);
109 u32 minindex, maxindex;
110 paging(m_data.size(), page, pagecount, minindex, maxindex);
112 for(std::map<std::string, float>::iterator
114 i != m_data.end(); ++i)
126 std::string name = i->first;
128 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
129 if(n != m_avgcounts.end()){
131 avgcount = n->second;
135 s32 space = clampsize - name.size();
136 for(s32 j=0; j<space; j++)
138 if(j%2 == 0 && j < space - 1)
143 o<<(i->second / avgcount);
148 typedef std::map<std::string, float> GraphValues;
150 void graphAdd(const std::string &id, float value)
152 JMutexAutoLock lock(m_mutex);
153 std::map<std::string, float>::iterator i =
154 m_graphvalues.find(id);
155 if(i == m_graphvalues.end())
156 m_graphvalues[id] = value;
160 void graphGet(GraphValues &result)
162 JMutexAutoLock lock(m_mutex);
163 result = m_graphvalues;
164 m_graphvalues.clear();
169 std::map<std::string, float> m_data;
170 std::map<std::string, int> m_avgcounts;
171 std::map<std::string, float> m_graphvalues;
174 enum ScopeProfilerType{
183 ScopeProfiler(Profiler *profiler, const std::string &name,
184 enum ScopeProfilerType type = SPT_ADD):
185 m_profiler(profiler),
191 m_timer = new TimeTaker(m_name.c_str());
194 ScopeProfiler(Profiler *profiler, const char *name,
195 enum ScopeProfilerType type = SPT_ADD):
196 m_profiler(profiler),
202 m_timer = new TimeTaker(m_name.c_str());
208 float duration_ms = m_timer->stop(true);
209 float duration = duration_ms / 1000.0;
213 m_profiler->add(m_name, duration);
216 m_profiler->avg(m_name, duration);
219 m_profiler->graphAdd(m_name, duration);
227 Profiler *m_profiler;
230 enum ScopeProfilerType m_type;