Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
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.
9 :
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.
14 :
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.
18 : */
19 :
20 : #ifndef PROFILER_HEADER
21 : #define PROFILER_HEADER
22 :
23 : #include "irrlichttypes.h"
24 : #include <string>
25 : #include <map>
26 :
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()
32 :
33 : #define MAX_PROFILER_TEXT_ROWS 20
34 :
35 : // Global profiler
36 : class Profiler;
37 : extern Profiler *g_profiler;
38 :
39 : /*
40 : Time profiler
41 : */
42 :
43 7 : class Profiler
44 : {
45 : public:
46 7 : Profiler()
47 7 : {
48 7 : }
49 :
50 3539 : void add(const std::string &name, float value)
51 : {
52 7078 : JMutexAutoLock lock(m_mutex);
53 : {
54 : /* No average shall have been used; mark add used as -2 */
55 3539 : std::map<std::string, int>::iterator n = m_avgcounts.find(name);
56 3539 : if(n == m_avgcounts.end())
57 30 : m_avgcounts[name] = -2;
58 : else{
59 3509 : if(n->second == -1)
60 0 : n->second = -2;
61 : assert(n->second == -2);
62 : }
63 : }
64 : {
65 3539 : std::map<std::string, float>::iterator n = m_data.find(name);
66 3539 : if(n == m_data.end())
67 5 : m_data[name] = value;
68 : else
69 3534 : n->second += value;
70 : }
71 3539 : }
72 :
73 931582 : void avg(const std::string &name, float value)
74 : {
75 1863174 : JMutexAutoLock lock(m_mutex);
76 931592 : int &count = m_avgcounts[name];
77 :
78 : assert(count != -2);
79 931592 : count = MYMAX(count, 0) + 1;
80 931592 : m_data[name] += value;
81 931592 : }
82 :
83 8 : void clear()
84 : {
85 16 : JMutexAutoLock lock(m_mutex);
86 685 : for(std::map<std::string, float>::iterator
87 8 : i = m_data.begin();
88 462 : i != m_data.end(); ++i)
89 : {
90 223 : i->second = 0;
91 : }
92 8 : m_avgcounts.clear();
93 8 : }
94 :
95 0 : void print(std::ostream &o)
96 : {
97 0 : printPage(o, 1, 1);
98 0 : }
99 :
100 0 : float getValue(const std::string &name) const
101 : {
102 0 : std::map<std::string, float>::const_iterator numerator = m_data.find(name);
103 0 : if (numerator == m_data.end())
104 0 : return 0.f;
105 :
106 0 : std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
107 0 : if (denominator != m_avgcounts.end()){
108 0 : if (denominator->second >= 1)
109 0 : return numerator->second / denominator->second;
110 : }
111 :
112 0 : return numerator->second;
113 : }
114 :
115 0 : void printPage(std::ostream &o, u32 page, u32 pagecount)
116 : {
117 0 : JMutexAutoLock lock(m_mutex);
118 :
119 : u32 minindex, maxindex;
120 0 : paging(m_data.size(), page, pagecount, minindex, maxindex);
121 :
122 0 : for(std::map<std::string, float>::iterator
123 0 : i = m_data.begin();
124 0 : i != m_data.end(); ++i)
125 : {
126 0 : if(maxindex == 0)
127 0 : break;
128 0 : maxindex--;
129 :
130 0 : if(minindex != 0)
131 : {
132 0 : minindex--;
133 0 : continue;
134 : }
135 :
136 0 : std::string name = i->first;
137 0 : int avgcount = 1;
138 0 : std::map<std::string, int>::iterator n = m_avgcounts.find(name);
139 0 : if(n != m_avgcounts.end()){
140 0 : if(n->second >= 1)
141 0 : avgcount = n->second;
142 : }
143 0 : o<<" "<<name<<": ";
144 0 : s32 clampsize = 40;
145 0 : s32 space = clampsize - name.size();
146 0 : for(s32 j=0; j<space; j++)
147 : {
148 0 : if(j%2 == 0 && j < space - 1)
149 0 : o<<"-";
150 : else
151 0 : o<<" ";
152 : }
153 0 : o<<(i->second / avgcount);
154 0 : o<<std::endl;
155 : }
156 0 : }
157 :
158 : typedef std::map<std::string, float> GraphValues;
159 :
160 17111 : void graphAdd(const std::string &id, float value)
161 : {
162 34222 : JMutexAutoLock lock(m_mutex);
163 : std::map<std::string, float>::iterator i =
164 17111 : m_graphvalues.find(id);
165 17111 : if(i == m_graphvalues.end())
166 7012 : m_graphvalues[id] = value;
167 : else
168 10099 : i->second += value;
169 17111 : }
170 1167 : void graphGet(GraphValues &result)
171 : {
172 2334 : JMutexAutoLock lock(m_mutex);
173 1167 : result = m_graphvalues;
174 1167 : m_graphvalues.clear();
175 1167 : }
176 :
177 : void remove(const std::string& name)
178 : {
179 : JMutexAutoLock lock(m_mutex);
180 : m_avgcounts.erase(name);
181 : m_data.erase(name);
182 : }
183 :
184 : private:
185 : JMutex m_mutex;
186 : std::map<std::string, float> m_data;
187 : std::map<std::string, int> m_avgcounts;
188 : std::map<std::string, float> m_graphvalues;
189 : };
190 :
191 : enum ScopeProfilerType{
192 : SPT_ADD,
193 : SPT_AVG,
194 : SPT_GRAPH_ADD
195 : };
196 :
197 : class ScopeProfiler
198 : {
199 : public:
200 2332 : ScopeProfiler(Profiler *profiler, const std::string &name,
201 : enum ScopeProfilerType type = SPT_ADD):
202 : m_profiler(profiler),
203 : m_name(name),
204 : m_timer(NULL),
205 2332 : m_type(type)
206 : {
207 2332 : if(m_profiler)
208 2332 : m_timer = new TimeTaker(m_name.c_str());
209 2332 : }
210 : // name is copied
211 63525 : ScopeProfiler(Profiler *profiler, const char *name,
212 : enum ScopeProfilerType type = SPT_ADD):
213 : m_profiler(profiler),
214 : m_name(name),
215 : m_timer(NULL),
216 63525 : m_type(type)
217 : {
218 63525 : if(m_profiler)
219 63525 : m_timer = new TimeTaker(m_name.c_str());
220 63525 : }
221 65857 : ~ScopeProfiler()
222 65857 : {
223 65857 : if(m_timer)
224 : {
225 65857 : float duration_ms = m_timer->stop(true);
226 65857 : float duration = duration_ms / 1000.0;
227 65857 : if(m_profiler){
228 65857 : switch(m_type){
229 : case SPT_ADD:
230 2194 : m_profiler->add(m_name, duration);
231 2194 : break;
232 : case SPT_AVG:
233 63663 : m_profiler->avg(m_name, duration);
234 63663 : break;
235 : case SPT_GRAPH_ADD:
236 0 : m_profiler->graphAdd(m_name, duration);
237 0 : break;
238 : }
239 : }
240 65857 : delete m_timer;
241 : }
242 65857 : }
243 : private:
244 : Profiler *m_profiler;
245 : std::string m_name;
246 : TimeTaker *m_timer;
247 : enum ScopeProfilerType m_type;
248 : };
249 :
250 : #endif
251 :
|