]> git.lizzy.rs Git - minetest.git/blob - src/profiler.h
Change Minetest-c55 to Minetest
[minetest.git] / src / profiler.h
1 /*
2 Minetest
3 Copyright (C) 2011 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_bloated.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
31 /*
32         Time profiler
33 */
34
35 class Profiler
36 {
37 public:
38         Profiler()
39         {
40                 m_mutex.Init();
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                         core::map<std::string, int>::Node *n = m_avgcounts.find(name);
49                         if(n == NULL)
50                                 m_avgcounts[name] = -2;
51                         else{
52                                 if(n->getValue() == -1)
53                                         n->setValue(-2);
54                                 assert(n->getValue() == -2);
55                         }
56                 }
57                 {
58                         core::map<std::string, float>::Node *n = m_data.find(name);
59                         if(n == NULL)
60                                 m_data[name] = value;
61                         else
62                                 n->setValue(n->getValue() + value);
63                 }
64         }
65
66         void avg(const std::string &name, float value)
67         {
68                 JMutexAutoLock lock(m_mutex);
69                 {
70                         core::map<std::string, int>::Node *n = m_avgcounts.find(name);
71                         if(n == NULL)
72                                 m_avgcounts[name] = 1;
73                         else{
74                                 /* No add shall have been used */
75                                 assert(n->getValue() != -2);
76                                 if(n->getValue() <= 0)
77                                         n->setValue(1);
78                                 else
79                                         n->setValue(n->getValue() + 1);
80                         }
81                 }
82                 {
83                         core::map<std::string, float>::Node *n = m_data.find(name);
84                         if(n == NULL)
85                                 m_data[name] = value;
86                         else
87                                 n->setValue(n->getValue() + value);
88                 }
89         }
90
91         void clear()
92         {
93                 JMutexAutoLock lock(m_mutex);
94                 for(core::map<std::string, float>::Iterator
95                                 i = m_data.getIterator();
96                                 i.atEnd() == false; i++)
97                 {
98                         i.getNode()->setValue(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(core::map<std::string, float>::Iterator
116                                 i = m_data.getIterator();
117                                 i.atEnd() == false; 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.getNode()->getKey();
130                         int avgcount = 1;
131                         core::map<std::string, int>::Node *n = m_avgcounts.find(name);
132                         if(n){
133                                 if(n->getValue() >= 1)
134                                         avgcount = n->getValue();
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.getNode()->getValue() / 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 private:
171         JMutex m_mutex;
172         core::map<std::string, float> m_data;
173         core::map<std::string, int> m_avgcounts;
174         std::map<std::string, float> m_graphvalues;
175 };
176
177 enum ScopeProfilerType{
178         SPT_ADD,
179         SPT_AVG,
180         SPT_GRAPH_ADD
181 };
182
183 class ScopeProfiler
184 {
185 public:
186         ScopeProfiler(Profiler *profiler, const std::string &name,
187                         enum ScopeProfilerType type = SPT_ADD):
188                 m_profiler(profiler),
189                 m_name(name),
190                 m_timer(NULL),
191                 m_type(type)
192         {
193                 if(m_profiler)
194                         m_timer = new TimeTaker(m_name.c_str());
195         }
196         // name is copied
197         ScopeProfiler(Profiler *profiler, const char *name,
198                         enum ScopeProfilerType type = SPT_ADD):
199                 m_profiler(profiler),
200                 m_name(name),
201                 m_timer(NULL),
202                 m_type(type)
203         {
204                 if(m_profiler)
205                         m_timer = new TimeTaker(m_name.c_str());
206         }
207         ~ScopeProfiler()
208         {
209                 if(m_timer)
210                 {
211                         float duration_ms = m_timer->stop(true);
212                         float duration = duration_ms / 1000.0;
213                         if(m_profiler){
214                                 switch(m_type){
215                                 case SPT_ADD:
216                                         m_profiler->add(m_name, duration);
217                                         break;
218                                 case SPT_AVG:
219                                         m_profiler->avg(m_name, duration);
220                                         break;
221                                 case SPT_GRAPH_ADD:
222                                         m_profiler->graphAdd(m_name, duration);
223                                         break;
224                                 }
225                         }
226                         delete m_timer;
227                 }
228         }
229 private:
230         Profiler *m_profiler;
231         std::string m_name;
232         TimeTaker *m_timer;
233         enum ScopeProfilerType m_type;
234 };
235
236 #endif
237