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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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>
41 void add(const std::string &name, float value)
43 JMutexAutoLock lock(m_mutex);
45 /* No average shall have been used; mark add used as -2 */
46 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
48 m_avgcounts[name] = -2;
50 if(n->getValue() == -1)
52 assert(n->getValue() == -2);
56 core::map<std::string, float>::Node *n = m_data.find(name);
60 n->setValue(n->getValue() + value);
64 void avg(const std::string &name, float value)
66 JMutexAutoLock lock(m_mutex);
68 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
70 m_avgcounts[name] = 1;
72 /* No add shall have been used */
73 assert(n->getValue() != -2);
74 if(n->getValue() <= 0)
77 n->setValue(n->getValue() + 1);
81 core::map<std::string, float>::Node *n = m_data.find(name);
85 n->setValue(n->getValue() + value);
91 JMutexAutoLock lock(m_mutex);
92 for(core::map<std::string, float>::Iterator
93 i = m_data.getIterator();
94 i.atEnd() == false; i++)
96 i.getNode()->setValue(0);
101 void print(std::ostream &o)
106 void printPage(std::ostream &o, u32 page, u32 pagecount)
108 JMutexAutoLock lock(m_mutex);
110 u32 minindex, maxindex;
111 paging(m_data.size(), page, pagecount, minindex, maxindex);
113 for(core::map<std::string, float>::Iterator
114 i = m_data.getIterator();
115 i.atEnd() == false; i++)
127 std::string name = i.getNode()->getKey();
129 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
131 if(n->getValue() >= 1)
132 avgcount = n->getValue();
136 s32 space = clampsize - name.size();
137 for(s32 j=0; j<space; j++)
139 if(j%2 == 0 && j < space - 1)
144 o<<(i.getNode()->getValue() / avgcount);
151 core::map<std::string, float> m_data;
152 core::map<std::string, int> m_avgcounts;
155 enum ScopeProfilerType{
163 ScopeProfiler(Profiler *profiler, const std::string &name,
164 enum ScopeProfilerType type = SPT_ADD):
165 m_profiler(profiler),
171 m_timer = new TimeTaker(m_name.c_str());
174 ScopeProfiler(Profiler *profiler, const char *name,
175 enum ScopeProfilerType type = SPT_ADD):
176 m_profiler(profiler),
182 m_timer = new TimeTaker(m_name.c_str());
188 float duration_ms = m_timer->stop(true);
189 float duration = duration_ms / 1000.0;
193 m_profiler->add(m_name, duration);
196 m_profiler->avg(m_name, duration);
204 Profiler *m_profiler;
207 enum ScopeProfilerType m_type;