]> git.lizzy.rs Git - dragonfireclient.git/blob - src/profiler.h
Fix f6 debug/profiler display
[dragonfireclient.git] / src / profiler.h
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 /*
36         Time profiler
37 */
38
39 class Profiler
40 {
41 public:
42         Profiler()
43         {
44         }
45
46         void add(const std::string &name, float value)
47         {
48                 JMutexAutoLock lock(m_mutex);
49                 {
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;
54                         else{
55                                 if(n->second == -1)
56                                         n->second = -2;
57                                 assert(n->second == -2);
58                         }
59                 }
60                 {
61                         std::map<std::string, float>::iterator n = m_data.find(name);
62                         if(n == m_data.end())
63                                 m_data[name] = value;
64                         else
65                                 n->second += value;
66                 }
67         }
68
69         void avg(const std::string &name, float value)
70         {
71                 JMutexAutoLock lock(m_mutex);
72                 {
73                         std::map<std::string, int>::iterator n = m_avgcounts.find(name);
74                         if(n == m_avgcounts.end())
75                                 m_avgcounts[name] = 1;
76                         else{
77                                 /* No add shall have been used */
78                                 assert(n->second != -2);
79                                 n->second = MYMAX(n->second, 0) + 1;
80                         }
81                 }
82                 {
83                         std::map<std::string, float>::iterator n = m_data.find(name);
84                         if(n == m_data.end())
85                                 m_data[name] = value;
86                         else
87                                 n->second += value;
88                 }
89         }
90
91         void clear()
92         {
93                 JMutexAutoLock lock(m_mutex);
94                 for(std::map<std::string, float>::iterator
95                                 i = m_data.begin();
96                                 i != m_data.end(); ++i)
97                 {
98                         i->second = 0;
99                 }
100                 m_avgcounts.clear();
101         }
102
103         void print(std::ostream &o)
104         {
105                 printPage(o, 1, 1);
106         }
107
108         void printPage(std::ostream &o, u32 page, u32 pagecount)
109         {
110                 JMutexAutoLock lock(m_mutex);
111
112                 u32 minindex, maxindex;
113                 paging(m_data.size(), page, pagecount, minindex, maxindex);
114
115                 for(std::map<std::string, float>::iterator
116                                 i = m_data.begin();
117                                 i != m_data.end(); ++i)
118                 {
119                         if(maxindex == 0)
120                                 break;
121                         maxindex--;
122
123                         if(minindex != 0)
124                         {
125                                 minindex--;
126                                 continue;
127                         }
128
129                         std::string name = i->first;
130                         int avgcount = 1;
131                         std::map<std::string, int>::iterator n = m_avgcounts.find(name);
132                         if(n != m_avgcounts.end()){
133                                 if(n->second >= 1)
134                                         avgcount = n->second;
135                         }
136                         o<<"  "<<name<<": ";
137                         s32 clampsize = 40;
138                         s32 space = clampsize - name.size();
139                         for(s32 j=0; j<space; j++)
140                         {
141                                 if(j%2 == 0 && j < space - 1)
142                                         o<<"-";
143                                 else
144                                         o<<" ";
145                         }
146                         o<<(i->second / avgcount);
147                         o<<std::endl;
148                 }
149         }
150
151         typedef std::map<std::string, float> GraphValues;
152
153         void graphAdd(const std::string &id, float value)
154         {
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;
160                 else
161                         i->second += value;
162         }
163         void graphGet(GraphValues &result)
164         {
165                 JMutexAutoLock lock(m_mutex);
166                 result = m_graphvalues;
167                 m_graphvalues.clear();
168         }
169
170         void remove(const std::string& name)
171         {
172                 JMutexAutoLock lock(m_mutex);
173                 m_avgcounts.erase(name);
174                 m_data.erase(name);
175         }
176
177 private:
178         JMutex m_mutex;
179         std::map<std::string, float> m_data;
180         std::map<std::string, int> m_avgcounts;
181         std::map<std::string, float> m_graphvalues;
182 };
183
184 enum ScopeProfilerType{
185         SPT_ADD,
186         SPT_AVG,
187         SPT_GRAPH_ADD
188 };
189
190 class ScopeProfiler
191 {
192 public:
193         ScopeProfiler(Profiler *profiler, const std::string &name,
194                         enum ScopeProfilerType type = SPT_ADD):
195                 m_profiler(profiler),
196                 m_name(name),
197                 m_timer(NULL),
198                 m_type(type)
199         {
200                 if(m_profiler)
201                         m_timer = new TimeTaker(m_name.c_str());
202         }
203         // name is copied
204         ScopeProfiler(Profiler *profiler, const char *name,
205                         enum ScopeProfilerType type = SPT_ADD):
206                 m_profiler(profiler),
207                 m_name(name),
208                 m_timer(NULL),
209                 m_type(type)
210         {
211                 if(m_profiler)
212                         m_timer = new TimeTaker(m_name.c_str());
213         }
214         ~ScopeProfiler()
215         {
216                 if(m_timer)
217                 {
218                         float duration_ms = m_timer->stop(true);
219                         float duration = duration_ms / 1000.0;
220                         if(m_profiler){
221                                 switch(m_type){
222                                 case SPT_ADD:
223                                         m_profiler->add(m_name, duration);
224                                         break;
225                                 case SPT_AVG:
226                                         m_profiler->avg(m_name, duration);
227                                         break;
228                                 case SPT_GRAPH_ADD:
229                                         m_profiler->graphAdd(m_name, duration);
230                                         break;
231                                 }
232                         }
233                         delete m_timer;
234                 }
235         }
236 private:
237         Profiler *m_profiler;
238         std::string m_name;
239         TimeTaker *m_timer;
240         enum ScopeProfilerType m_type;
241 };
242
243 #endif
244