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()
33 #define MAX_PROFILER_TEXT_ROWS 20
46 void add(const std::string &name, float value)
48 JMutexAutoLock lock(m_mutex);
50 /* No average shall have been used; mark add used as -2 */
51 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
52 if(n == m_avgcounts.end())
53 m_avgcounts[name] = -2;
57 assert(n->second == -2);
61 std::map<std::string, float>::iterator n = m_data.find(name);
69 void avg(const std::string &name, float value)
71 JMutexAutoLock lock(m_mutex);
72 int &count = m_avgcounts[name];
75 count = MYMAX(count, 0) + 1;
76 m_data[name] += value;
81 JMutexAutoLock lock(m_mutex);
82 for(std::map<std::string, float>::iterator
84 i != m_data.end(); ++i)
91 void print(std::ostream &o)
96 float getValue(const std::string &name) const
98 std::map<std::string, float>::const_iterator numerator = m_data.find(name);
99 if (numerator == m_data.end())
102 std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
103 if (denominator != m_avgcounts.end()){
104 if (denominator->second >= 1)
105 return numerator->second / denominator->second;
108 return numerator->second;
111 void printPage(std::ostream &o, u32 page, u32 pagecount)
113 JMutexAutoLock lock(m_mutex);
115 u32 minindex, maxindex;
116 paging(m_data.size(), page, pagecount, minindex, maxindex);
118 for(std::map<std::string, float>::iterator
120 i != m_data.end(); ++i)
132 std::string name = i->first;
134 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
135 if(n != m_avgcounts.end()){
137 avgcount = n->second;
141 s32 space = clampsize - name.size();
142 for(s32 j=0; j<space; j++)
144 if(j%2 == 0 && j < space - 1)
149 o<<(i->second / avgcount);
154 typedef std::map<std::string, float> GraphValues;
156 void graphAdd(const std::string &id, float value)
158 JMutexAutoLock lock(m_mutex);
159 std::map<std::string, float>::iterator i =
160 m_graphvalues.find(id);
161 if(i == m_graphvalues.end())
162 m_graphvalues[id] = value;
166 void graphGet(GraphValues &result)
168 JMutexAutoLock lock(m_mutex);
169 result = m_graphvalues;
170 m_graphvalues.clear();
173 void remove(const std::string& name)
175 JMutexAutoLock lock(m_mutex);
176 m_avgcounts.erase(name);
182 std::map<std::string, float> m_data;
183 std::map<std::string, int> m_avgcounts;
184 std::map<std::string, float> m_graphvalues;
187 enum ScopeProfilerType{
196 ScopeProfiler(Profiler *profiler, const std::string &name,
197 enum ScopeProfilerType type = SPT_ADD):
198 m_profiler(profiler),
204 m_timer = new TimeTaker(m_name.c_str());
207 ScopeProfiler(Profiler *profiler, const char *name,
208 enum ScopeProfilerType type = SPT_ADD):
209 m_profiler(profiler),
215 m_timer = new TimeTaker(m_name.c_str());
221 float duration_ms = m_timer->stop(true);
222 float duration = duration_ms / 1000.0;
226 m_profiler->add(m_name, duration);
229 m_profiler->avg(m_name, duration);
232 m_profiler->graphAdd(m_name, duration);
240 Profiler *m_profiler;
243 enum ScopeProfilerType m_type;