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 "common_irrlicht.h"
27 #include <jmutexautolock.h>
42 void add(const std::string &name, float value)
44 JMutexAutoLock lock(m_mutex);
46 /* No average shall have been used; mark add used as -2 */
47 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
49 m_avgcounts[name] = -2;
51 if(n->getValue() == -1)
53 assert(n->getValue() == -2);
57 core::map<std::string, float>::Node *n = m_data.find(name);
61 n->setValue(n->getValue() + value);
65 void avg(const std::string &name, float value)
67 JMutexAutoLock lock(m_mutex);
69 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
71 m_avgcounts[name] = 1;
73 /* No add shall have been used */
74 assert(n->getValue() != -2);
75 if(n->getValue() <= 0)
78 n->setValue(n->getValue() + 1);
82 core::map<std::string, float>::Node *n = m_data.find(name);
86 n->setValue(n->getValue() + value);
92 JMutexAutoLock lock(m_mutex);
93 for(core::map<std::string, float>::Iterator
94 i = m_data.getIterator();
95 i.atEnd() == false; i++)
97 i.getNode()->setValue(0);
102 void print(std::ostream &o)
107 void printPage(std::ostream &o, u32 page, u32 pagecount)
109 JMutexAutoLock lock(m_mutex);
111 u32 minindex, maxindex;
112 paging(m_data.size(), page, pagecount, minindex, maxindex);
114 for(core::map<std::string, float>::Iterator
115 i = m_data.getIterator();
116 i.atEnd() == false; i++)
128 std::string name = i.getNode()->getKey();
130 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
132 if(n->getValue() >= 1)
133 avgcount = n->getValue();
137 s32 space = clampsize - name.size();
138 for(s32 j=0; j<space; j++)
140 if(j%2 == 0 && j < space - 1)
145 o<<(i.getNode()->getValue() / avgcount);
150 typedef std::map<std::string, float> GraphValues;
152 void graphAdd(const std::string &id, float value)
154 JMutexAutoLock 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 JMutexAutoLock lock(m_mutex);
165 result = m_graphvalues;
166 m_graphvalues.clear();
171 core::map<std::string, float> m_data;
172 core::map<std::string, int> m_avgcounts;
173 std::map<std::string, float> m_graphvalues;
176 enum ScopeProfilerType{
185 ScopeProfiler(Profiler *profiler, const std::string &name,
186 enum ScopeProfilerType type = SPT_ADD):
187 m_profiler(profiler),
193 m_timer = new TimeTaker(m_name.c_str());
196 ScopeProfiler(Profiler *profiler, const char *name,
197 enum ScopeProfilerType type = SPT_ADD):
198 m_profiler(profiler),
204 m_timer = new TimeTaker(m_name.c_str());
210 float duration_ms = m_timer->stop(true);
211 float duration = duration_ms / 1000.0;
215 m_profiler->add(m_name, duration);
218 m_profiler->avg(m_name, duration);
221 m_profiler->graphAdd(m_name, duration);
229 Profiler *m_profiler;
232 enum ScopeProfilerType m_type;