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