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);
73 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
74 if(n == m_avgcounts.end())
75 m_avgcounts[name] = 1;
77 /* No add shall have been used */
78 assert(n->second != -2);
79 n->second = MYMAX(n->second, 0) + 1;
83 std::map<std::string, float>::iterator n = m_data.find(name);
93 JMutexAutoLock lock(m_mutex);
94 for(std::map<std::string, float>::iterator
96 i != m_data.end(); ++i)
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(std::map<std::string, float>::iterator
117 i != m_data.end(); ++i)
129 std::string name = i->first;
131 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
132 if(n != m_avgcounts.end()){
134 avgcount = n->second;
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->second / 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();
170 void remove(const std::string& name)
172 JMutexAutoLock lock(m_mutex);
173 m_avgcounts.erase(name);
179 std::map<std::string, float> m_data;
180 std::map<std::string, int> m_avgcounts;
181 std::map<std::string, float> m_graphvalues;
184 enum ScopeProfilerType{
193 ScopeProfiler(Profiler *profiler, const std::string &name,
194 enum ScopeProfilerType type = SPT_ADD):
195 m_profiler(profiler),
201 m_timer = new TimeTaker(m_name.c_str());
204 ScopeProfiler(Profiler *profiler, const char *name,
205 enum ScopeProfilerType type = SPT_ADD):
206 m_profiler(profiler),
212 m_timer = new TimeTaker(m_name.c_str());
218 float duration_ms = m_timer->stop(true);
219 float duration = duration_ms / 1000.0;
223 m_profiler->add(m_name, duration);
226 m_profiler->avg(m_name, duration);
229 m_profiler->graphAdd(m_name, duration);
237 Profiler *m_profiler;
240 enum ScopeProfilerType m_type;