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 "jthread/jmutex.h"
28 #include "jthread/jmutexautolock.h"
29 #include "util/timetaker.h"
30 #include "util/numeric.h" // paging()
31 #include "debug.h" // assert()
44 void add(const std::string &name, float value)
46 JMutexAutoLock lock(m_mutex);
48 /* No average shall have been used; mark add used as -2 */
49 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
50 if(n == m_avgcounts.end())
51 m_avgcounts[name] = -2;
55 assert(n->second == -2);
59 std::map<std::string, float>::iterator n = m_data.find(name);
67 void avg(const std::string &name, float value)
69 JMutexAutoLock lock(m_mutex);
71 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
72 if(n == m_avgcounts.end())
73 m_avgcounts[name] = 1;
75 /* No add shall have been used */
76 assert(n->second != -2);
77 n->second = MYMAX(n->second, 0) + 1;
81 std::map<std::string, float>::iterator n = m_data.find(name);
91 JMutexAutoLock lock(m_mutex);
92 for(std::map<std::string, float>::iterator
94 i != m_data.end(); ++i)
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(std::map<std::string, float>::iterator
115 i != m_data.end(); ++i)
127 std::string name = i->first;
129 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
130 if(n != m_avgcounts.end()){
132 avgcount = n->second;
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->second / avgcount);
149 typedef std::map<std::string, float> GraphValues;
151 void graphAdd(const std::string &id, float value)
153 JMutexAutoLock lock(m_mutex);
154 std::map<std::string, float>::iterator i =
155 m_graphvalues.find(id);
156 if(i == m_graphvalues.end())
157 m_graphvalues[id] = value;
161 void graphGet(GraphValues &result)
163 JMutexAutoLock lock(m_mutex);
164 result = m_graphvalues;
165 m_graphvalues.clear();
168 void remove(const std::string& name)
170 JMutexAutoLock lock(m_mutex);
171 m_avgcounts.erase(name);
177 std::map<std::string, float> m_data;
178 std::map<std::string, int> m_avgcounts;
179 std::map<std::string, float> m_graphvalues;
182 enum ScopeProfilerType{
191 ScopeProfiler(Profiler *profiler, const std::string &name,
192 enum ScopeProfilerType type = SPT_ADD):
193 m_profiler(profiler),
199 m_timer = new TimeTaker(m_name.c_str());
202 ScopeProfiler(Profiler *profiler, const char *name,
203 enum ScopeProfilerType type = SPT_ADD):
204 m_profiler(profiler),
210 m_timer = new TimeTaker(m_name.c_str());
216 float duration_ms = m_timer->stop(true);
217 float duration = duration_ms / 1000.0;
221 m_profiler->add(m_name, duration);
224 m_profiler->avg(m_name, duration);
227 m_profiler->graphAdd(m_name, duration);
235 Profiler *m_profiler;
238 enum ScopeProfilerType m_type;