utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.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
29 /*
30         Time profiler
31 */
32
33 class Profiler
34 {
35 public:
36         Profiler()
37         {
38                 m_mutex.Init();
39         }
40
41         void add(const std::string &name, float value)
42         {
43                 JMutexAutoLock lock(m_mutex);
44                 {
45                         /* No average shall have been used; mark add used as -2 */
46                         core::map<std::string, int>::Node *n = m_avgcounts.find(name);
47                         if(n == NULL)
48                                 m_avgcounts[name] = -2;
49                         else{
50                                 if(n->getValue() == -1)
51                                         n->setValue(-2);
52                                 assert(n->getValue() == -2);
53                         }
54                 }
55                 {
56                         core::map<std::string, float>::Node *n = m_data.find(name);
57                         if(n == NULL)
58                                 m_data[name] = value;
59                         else
60                                 n->setValue(n->getValue() + value);
61                 }
62         }
63
64         void avg(const std::string &name, float value)
65         {
66                 JMutexAutoLock lock(m_mutex);
67                 {
68                         core::map<std::string, int>::Node *n = m_avgcounts.find(name);
69                         if(n == NULL)
70                                 m_avgcounts[name] = 1;
71                         else{
72                                 /* No add shall have been used */
73                                 assert(n->getValue() != -2);
74                                 if(n->getValue() <= 0)
75                                         n->setValue(1);
76                                 else
77                                         n->setValue(n->getValue() + 1);
78                         }
79                 }
80                 {
81                         core::map<std::string, float>::Node *n = m_data.find(name);
82                         if(n == NULL)
83                                 m_data[name] = value;
84                         else
85                                 n->setValue(n->getValue() + value);
86                 }
87         }
88
89         void clear()
90         {
91                 JMutexAutoLock lock(m_mutex);
92                 for(core::map<std::string, float>::Iterator
93                                 i = m_data.getIterator();
94                                 i.atEnd() == false; i++)
95                 {
96                         i.getNode()->setValue(0);
97                 }
98                 m_avgcounts.clear();
99         }
100
101         void print(std::ostream &o)
102         {
103                 JMutexAutoLock lock(m_mutex);
104                 for(core::map<std::string, float>::Iterator
105                                 i = m_data.getIterator();
106                                 i.atEnd() == false; i++)
107                 {
108                         std::string name = i.getNode()->getKey();
109                         int avgcount = 1;
110                         core::map<std::string, int>::Node *n = m_avgcounts.find(name);
111                         if(n){
112                                 if(n->getValue() >= 1)
113                                         avgcount = n->getValue();
114                         }
115                         o<<"  "<<name<<": ";
116                         s32 clampsize = 40;
117                         s32 space = clampsize - name.size();
118                         for(s32 j=0; j<space; j++)
119                         {
120                                 if(j%2 == 0 && j < space - 1)
121                                         o<<"-";
122                                 else
123                                         o<<" ";
124                         }
125                         o<<(i.getNode()->getValue() / avgcount);
126                         o<<std::endl;
127                 }
128         }
129
130 private:
131         JMutex m_mutex;
132         core::map<std::string, float> m_data;
133         core::map<std::string, int> m_avgcounts;
134 };
135
136 enum ScopeProfilerType{
137         SPT_ADD,
138         SPT_AVG
139 };
140
141 class ScopeProfiler
142 {
143 public:
144         ScopeProfiler(Profiler *profiler, const std::string &name,
145                         enum ScopeProfilerType type = SPT_ADD):
146                 m_profiler(profiler),
147                 m_name(name),
148                 m_timer(NULL),
149                 m_type(type)
150         {
151                 if(m_profiler)
152                         m_timer = new TimeTaker(m_name.c_str());
153         }
154         // name is copied
155         ScopeProfiler(Profiler *profiler, const char *name,
156                         enum ScopeProfilerType type = SPT_ADD):
157                 m_profiler(profiler),
158                 m_name(name),
159                 m_timer(NULL),
160                 m_type(type)
161         {
162                 if(m_profiler)
163                         m_timer = new TimeTaker(m_name.c_str());
164         }
165         ~ScopeProfiler()
166         {
167                 if(m_timer)
168                 {
169                         float duration_ms = m_timer->stop(true);
170                         float duration = duration_ms / 1000.0;
171                         if(m_profiler){
172                                 switch(m_type){
173                                 case SPT_ADD:
174                                         m_profiler->add(m_name, duration);
175                                         break;
176                                 case SPT_AVG:
177                                         m_profiler->avg(m_name, duration);
178                                         break;
179                                 }
180                         }
181                         delete m_timer;
182                 }
183         }
184 private:
185         Profiler *m_profiler;
186         std::string m_name;
187         TimeTaker *m_timer;
188         enum ScopeProfilerType m_type;
189 };
190
191 #endif
192