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.
22 #include "irrlichttypes.h"
28 #include "threading/mutex_auto_lock.h"
29 #include "util/timetaker.h"
30 #include "util/numeric.h" // paging()
32 #define MAX_PROFILER_TEXT_ROWS 20
36 extern Profiler *g_profiler;
47 void add(const std::string &name, float value)
49 MutexAutoLock lock(m_mutex);
51 /* No average shall have been used; mark add used as -2 */
52 std::map<std::string, int>::iterator n = m_avgcounts.find(name);
53 if(n == m_avgcounts.end())
54 m_avgcounts[name] = -2;
58 assert(n->second == -2);
62 std::map<std::string, float>::iterator n = m_data.find(name);
70 void avg(const std::string &name, float value)
72 MutexAutoLock lock(m_mutex);
73 int &count = m_avgcounts[name];
76 count = MYMAX(count, 0) + 1;
77 m_data[name] += value;
82 MutexAutoLock lock(m_mutex);
83 for (auto &it : m_data) {
89 void print(std::ostream &o)
94 float getValue(const std::string &name) const
96 std::map<std::string, float>::const_iterator numerator = m_data.find(name);
97 if (numerator == m_data.end())
100 std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
101 if (denominator != m_avgcounts.end()){
102 if (denominator->second >= 1)
103 return numerator->second / denominator->second;
106 return numerator->second;
109 void printPage(std::ostream &o, u32 page, u32 pagecount)
111 MutexAutoLock lock(m_mutex);
113 u32 minindex, maxindex;
114 paging(m_data.size(), page, pagecount, minindex, maxindex);
116 for (std::map<std::string, float>::const_iterator i = m_data.begin();
117 i != m_data.end(); ++i) {
128 std::map<std::string, int>::const_iterator n = m_avgcounts.find(i->first);
129 if (n != m_avgcounts.end()) {
131 avgcount = n->second;
133 o << " " << i->first << ": ";
135 s32 space = clampsize - i->first.size();
136 for(s32 j = 0; j < space; j++) {
137 if (j % 2 == 0 && j < space - 1)
142 o << (i->second / avgcount);
147 typedef std::map<std::string, float> GraphValues;
149 void graphAdd(const std::string &id, float value)
151 MutexAutoLock lock(m_mutex);
152 std::map<std::string, float>::iterator i =
153 m_graphvalues.find(id);
154 if(i == m_graphvalues.end())
155 m_graphvalues[id] = value;
159 void graphGet(GraphValues &result)
161 MutexAutoLock lock(m_mutex);
162 result = m_graphvalues;
163 m_graphvalues.clear();
166 void remove(const std::string& name)
168 MutexAutoLock lock(m_mutex);
169 m_avgcounts.erase(name);
175 std::map<std::string, float> m_data;
176 std::map<std::string, int> m_avgcounts;
177 std::map<std::string, float> m_graphvalues;
180 enum ScopeProfilerType{
189 ScopeProfiler(Profiler *profiler, const std::string &name,
190 ScopeProfilerType type = SPT_ADD);
193 Profiler *m_profiler = nullptr;
195 TimeTaker *m_timer = nullptr;
196 enum ScopeProfilerType m_type;