3 Copyright (C) 2011 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 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
50 m_avgcounts[name] = -2;
52 if(n->getValue() == -1)
54 assert(n->getValue() == -2);
58 core::map<std::string, float>::Node *n = m_data.find(name);
62 n->setValue(n->getValue() + value);
66 void avg(const std::string &name, float value)
68 JMutexAutoLock lock(m_mutex);
70 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
72 m_avgcounts[name] = 1;
74 /* No add shall have been used */
75 assert(n->getValue() != -2);
76 if(n->getValue() <= 0)
79 n->setValue(n->getValue() + 1);
83 core::map<std::string, float>::Node *n = m_data.find(name);
87 n->setValue(n->getValue() + value);
93 JMutexAutoLock lock(m_mutex);
94 for(core::map<std::string, float>::Iterator
95 i = m_data.getIterator();
96 i.atEnd() == false; i++)
98 i.getNode()->setValue(0);
103 void print(std::ostream &o)
108 void printPage(std::ostream &o, u32 page, u32 pagecount)
110 JMutexAutoLock lock(m_mutex);
112 u32 minindex, maxindex;
113 paging(m_data.size(), page, pagecount, minindex, maxindex);
115 for(core::map<std::string, float>::Iterator
116 i = m_data.getIterator();
117 i.atEnd() == false; i++)
129 std::string name = i.getNode()->getKey();
131 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
133 if(n->getValue() >= 1)
134 avgcount = n->getValue();
138 s32 space = clampsize - name.size();
139 for(s32 j=0; j<space; j++)
141 if(j%2 == 0 && j < space - 1)
146 o<<(i.getNode()->getValue() / avgcount);
151 typedef std::map<std::string, float> GraphValues;
153 void graphAdd(const std::string &id, float value)
155 JMutexAutoLock lock(m_mutex);
156 std::map<std::string, float>::iterator i =
157 m_graphvalues.find(id);
158 if(i == m_graphvalues.end())
159 m_graphvalues[id] = value;
163 void graphGet(GraphValues &result)
165 JMutexAutoLock lock(m_mutex);
166 result = m_graphvalues;
167 m_graphvalues.clear();
172 core::map<std::string, float> m_data;
173 core::map<std::string, int> m_avgcounts;
174 std::map<std::string, float> m_graphvalues;
177 enum ScopeProfilerType{
186 ScopeProfiler(Profiler *profiler, const std::string &name,
187 enum ScopeProfilerType type = SPT_ADD):
188 m_profiler(profiler),
194 m_timer = new TimeTaker(m_name.c_str());
197 ScopeProfiler(Profiler *profiler, const char *name,
198 enum ScopeProfilerType type = SPT_ADD):
199 m_profiler(profiler),
205 m_timer = new TimeTaker(m_name.c_str());
211 float duration_ms = m_timer->stop(true);
212 float duration = duration_ms / 1000.0;
216 m_profiler->add(m_name, duration);
219 m_profiler->avg(m_name, duration);
222 m_profiler->graphAdd(m_name, duration);
230 Profiler *m_profiler;
233 enum ScopeProfilerType m_type;