Clean up log messages everywhere
[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                 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(core::map<std::string, float>::Iterator
114                                 i = m_data.getIterator();
115                                 i.atEnd() == false; 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.getNode()->getKey();
128                         int avgcount = 1;
129                         core::map<std::string, int>::Node *n = m_avgcounts.find(name);
130                         if(n){
131                                 if(n->getValue() >= 1)
132                                         avgcount = n->getValue();
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.getNode()->getValue() / avgcount);
145                         o<<std::endl;
146                 }
147         }
148
149 private:
150         JMutex m_mutex;
151         core::map<std::string, float> m_data;
152         core::map<std::string, int> m_avgcounts;
153 };
154
155 enum ScopeProfilerType{
156         SPT_ADD,
157         SPT_AVG
158 };
159
160 class ScopeProfiler
161 {
162 public:
163         ScopeProfiler(Profiler *profiler, const std::string &name,
164                         enum ScopeProfilerType type = SPT_ADD):
165                 m_profiler(profiler),
166                 m_name(name),
167                 m_timer(NULL),
168                 m_type(type)
169         {
170                 if(m_profiler)
171                         m_timer = new TimeTaker(m_name.c_str());
172         }
173         // name is copied
174         ScopeProfiler(Profiler *profiler, const char *name,
175                         enum ScopeProfilerType type = SPT_ADD):
176                 m_profiler(profiler),
177                 m_name(name),
178                 m_timer(NULL),
179                 m_type(type)
180         {
181                 if(m_profiler)
182                         m_timer = new TimeTaker(m_name.c_str());
183         }
184         ~ScopeProfiler()
185         {
186                 if(m_timer)
187                 {
188                         float duration_ms = m_timer->stop(true);
189                         float duration = duration_ms / 1000.0;
190                         if(m_profiler){
191                                 switch(m_type){
192                                 case SPT_ADD:
193                                         m_profiler->add(m_name, duration);
194                                         break;
195                                 case SPT_AVG:
196                                         m_profiler->avg(m_name, duration);
197                                         break;
198                                 }
199                         }
200                         delete m_timer;
201                 }
202         }
203 private:
204         Profiler *m_profiler;
205         std::string m_name;
206         TimeTaker *m_timer;
207         enum ScopeProfilerType m_type;
208 };
209
210 #endif
211