Performance fix + SAO factorization
[oweals/minetest.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 <map>
26
27 #include "threading/mutex.h"
28 #include "threading/mutex_auto_lock.h"
29 #include "util/timetaker.h"
30 #include "util/numeric.h"      // paging()
31 #include "debug.h"             // assert()
32
33 #define MAX_PROFILER_TEXT_ROWS 20
34
35 // Global profiler
36 class Profiler;
37 extern Profiler *g_profiler;
38
39 /*
40         Time profiler
41 */
42
43 class Profiler
44 {
45 public:
46         Profiler()
47         {
48         }
49
50         void add(const std::string &name, float value)
51         {
52                 MutexAutoLock lock(m_mutex);
53                 {
54                         /* No average shall have been used; mark add used as -2 */
55                         std::map<std::string, int>::iterator n = m_avgcounts.find(name);
56                         if(n == m_avgcounts.end())
57                                 m_avgcounts[name] = -2;
58                         else{
59                                 if(n->second == -1)
60                                         n->second = -2;
61                                 assert(n->second == -2);
62                         }
63                 }
64                 {
65                         std::map<std::string, float>::iterator n = m_data.find(name);
66                         if(n == m_data.end())
67                                 m_data[name] = value;
68                         else
69                                 n->second += value;
70                 }
71         }
72
73         void avg(const std::string &name, float value)
74         {
75                 MutexAutoLock lock(m_mutex);
76                 int &count = m_avgcounts[name];
77
78                 assert(count != -2);
79                 count = MYMAX(count, 0) + 1;
80                 m_data[name] += value;
81         }
82
83         void clear()
84         {
85                 MutexAutoLock lock(m_mutex);
86                 for(std::map<std::string, float>::iterator
87                                 i = m_data.begin();
88                                 i != m_data.end(); ++i)
89                 {
90                         i->second = 0;
91                 }
92                 m_avgcounts.clear();
93         }
94
95         void print(std::ostream &o)
96         {
97                 printPage(o, 1, 1);
98         }
99
100         float getValue(const std::string &name) const
101         {
102                 std::map<std::string, float>::const_iterator numerator = m_data.find(name);
103                 if (numerator == m_data.end())
104                         return 0.f;
105
106                 std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
107                 if (denominator != m_avgcounts.end()){
108                         if (denominator->second >= 1)
109                                 return numerator->second / denominator->second;
110                 }
111
112                 return numerator->second;
113         }
114
115         void printPage(std::ostream &o, u32 page, u32 pagecount)
116         {
117                 MutexAutoLock lock(m_mutex);
118
119                 u32 minindex, maxindex;
120                 paging(m_data.size(), page, pagecount, minindex, maxindex);
121
122                 for(std::map<std::string, float>::iterator
123                                 i = m_data.begin();
124                                 i != m_data.end(); ++i)
125                 {
126                         if(maxindex == 0)
127                                 break;
128                         maxindex--;
129
130                         if(minindex != 0)
131                         {
132                                 minindex--;
133                                 continue;
134                         }
135
136                         std::string name = i->first;
137                         int avgcount = 1;
138                         std::map<std::string, int>::iterator n = m_avgcounts.find(name);
139                         if(n != m_avgcounts.end()){
140                                 if(n->second >= 1)
141                                         avgcount = n->second;
142                         }
143                         o<<"  "<<name<<": ";
144                         s32 clampsize = 40;
145                         s32 space = clampsize - name.size();
146                         for(s32 j=0; j<space; j++)
147                         {
148                                 if(j%2 == 0 && j < space - 1)
149                                         o<<"-";
150                                 else
151                                         o<<" ";
152                         }
153                         o<<(i->second / avgcount);
154                         o<<std::endl;
155                 }
156         }
157
158         typedef std::map<std::string, float> GraphValues;
159
160         void graphAdd(const std::string &id, float value)
161         {
162                 MutexAutoLock lock(m_mutex);
163                 std::map<std::string, float>::iterator i =
164                                 m_graphvalues.find(id);
165                 if(i == m_graphvalues.end())
166                         m_graphvalues[id] = value;
167                 else
168                         i->second += value;
169         }
170         void graphGet(GraphValues &result)
171         {
172                 MutexAutoLock lock(m_mutex);
173                 result = m_graphvalues;
174                 m_graphvalues.clear();
175         }
176
177         void remove(const std::string& name)
178         {
179                 MutexAutoLock lock(m_mutex);
180                 m_avgcounts.erase(name);
181                 m_data.erase(name);
182         }
183
184 private:
185         Mutex m_mutex;
186         std::map<std::string, float> m_data;
187         std::map<std::string, int> m_avgcounts;
188         std::map<std::string, float> m_graphvalues;
189 };
190
191 enum ScopeProfilerType{
192         SPT_ADD,
193         SPT_AVG,
194         SPT_GRAPH_ADD
195 };
196
197 class ScopeProfiler
198 {
199 public:
200         ScopeProfiler(Profiler *profiler, const std::string &name,
201                         enum ScopeProfilerType type = SPT_ADD):
202                 m_profiler(profiler),
203                 m_name(name),
204                 m_timer(NULL),
205                 m_type(type)
206         {
207                 if(m_profiler)
208                         m_timer = new TimeTaker(m_name.c_str());
209         }
210         // name is copied
211         ScopeProfiler(Profiler *profiler, const char *name,
212                         enum ScopeProfilerType type = SPT_ADD):
213                 m_profiler(profiler),
214                 m_name(name),
215                 m_timer(NULL),
216                 m_type(type)
217         {
218                 if(m_profiler)
219                         m_timer = new TimeTaker(m_name.c_str());
220         }
221         ~ScopeProfiler()
222         {
223                 if(m_timer)
224                 {
225                         float duration_ms = m_timer->stop(true);
226                         float duration = duration_ms / 1000.0;
227                         if(m_profiler){
228                                 switch(m_type){
229                                 case SPT_ADD:
230                                         m_profiler->add(m_name, duration);
231                                         break;
232                                 case SPT_AVG:
233                                         m_profiler->avg(m_name, duration);
234                                         break;
235                                 case SPT_GRAPH_ADD:
236                                         m_profiler->graphAdd(m_name, duration);
237                                         break;
238                                 }
239                         }
240                         delete m_timer;
241                 }
242         }
243 private:
244         Profiler *m_profiler;
245         std::string m_name;
246         TimeTaker *m_timer;
247         enum ScopeProfilerType m_type;
248 };
249
250 #endif
251