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