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