]> git.lizzy.rs Git - dragonfireclient.git/blob - src/profiler.h
Implement proper font handling
[dragonfireclient.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 "jthread/jmutex.h"
28 #include "jthread/jmutexautolock.h"
29 #include "util/timetaker.h"
30 #include "util/numeric.h" // paging()
31 #include "debug.h" // assert()
32
33 /*
34         Time profiler
35 */
36
37 class Profiler
38 {
39 public:
40         Profiler()
41         {
42         }
43
44         void add(const std::string &name, float value)
45         {
46                 JMutexAutoLock lock(m_mutex);
47                 {
48                         /* No average shall have been used; mark add used as -2 */
49                         std::map<std::string, int>::iterator n = m_avgcounts.find(name);
50                         if(n == m_avgcounts.end())
51                                 m_avgcounts[name] = -2;
52                         else{
53                                 if(n->second == -1)
54                                         n->second = -2;
55                                 assert(n->second == -2);
56                         }
57                 }
58                 {
59                         std::map<std::string, float>::iterator n = m_data.find(name);
60                         if(n == m_data.end())
61                                 m_data[name] = value;
62                         else
63                                 n->second += value;
64                 }
65         }
66
67         void avg(const std::string &name, float value)
68         {
69                 JMutexAutoLock lock(m_mutex);
70                 {
71                         std::map<std::string, int>::iterator n = m_avgcounts.find(name);
72                         if(n == m_avgcounts.end())
73                                 m_avgcounts[name] = 1;
74                         else{
75                                 /* No add shall have been used */
76                                 assert(n->second != -2);
77                                 n->second = MYMAX(n->second, 0) + 1;
78                         }
79                 }
80                 {
81                         std::map<std::string, float>::iterator n = m_data.find(name);
82                         if(n == m_data.end())
83                                 m_data[name] = value;
84                         else
85                                 n->second += value;
86                 }
87         }
88
89         void clear()
90         {
91                 JMutexAutoLock lock(m_mutex);
92                 for(std::map<std::string, float>::iterator
93                                 i = m_data.begin();
94                                 i != m_data.end(); ++i)
95                 {
96                         i->second = 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(std::map<std::string, float>::iterator
114                                 i = m_data.begin();
115                                 i != m_data.end(); ++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->first;
128                         int avgcount = 1;
129                         std::map<std::string, int>::iterator n = m_avgcounts.find(name);
130                         if(n != m_avgcounts.end()){
131                                 if(n->second >= 1)
132                                         avgcount = n->second;
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->second / avgcount);
145                         o<<std::endl;
146                 }
147         }
148
149         typedef std::map<std::string, float> GraphValues;
150
151         void graphAdd(const std::string &id, float value)
152         {
153                 JMutexAutoLock lock(m_mutex);
154                 std::map<std::string, float>::iterator i =
155                                 m_graphvalues.find(id);
156                 if(i == m_graphvalues.end())
157                         m_graphvalues[id] = value;
158                 else
159                         i->second += value;
160         }
161         void graphGet(GraphValues &result)
162         {
163                 JMutexAutoLock lock(m_mutex);
164                 result = m_graphvalues;
165                 m_graphvalues.clear();
166         }
167
168         void remove(const std::string& name)
169         {
170                 JMutexAutoLock lock(m_mutex);
171                 m_avgcounts.erase(name);
172                 m_data.erase(name);
173         }
174
175 private:
176         JMutex m_mutex;
177         std::map<std::string, float> m_data;
178         std::map<std::string, int> m_avgcounts;
179         std::map<std::string, float> m_graphvalues;
180 };
181
182 enum ScopeProfilerType{
183         SPT_ADD,
184         SPT_AVG,
185         SPT_GRAPH_ADD
186 };
187
188 class ScopeProfiler
189 {
190 public:
191         ScopeProfiler(Profiler *profiler, const std::string &name,
192                         enum ScopeProfilerType type = SPT_ADD):
193                 m_profiler(profiler),
194                 m_name(name),
195                 m_timer(NULL),
196                 m_type(type)
197         {
198                 if(m_profiler)
199                         m_timer = new TimeTaker(m_name.c_str());
200         }
201         // name is copied
202         ScopeProfiler(Profiler *profiler, const char *name,
203                         enum ScopeProfilerType type = SPT_ADD):
204                 m_profiler(profiler),
205                 m_name(name),
206                 m_timer(NULL),
207                 m_type(type)
208         {
209                 if(m_profiler)
210                         m_timer = new TimeTaker(m_name.c_str());
211         }
212         ~ScopeProfiler()
213         {
214                 if(m_timer)
215                 {
216                         float duration_ms = m_timer->stop(true);
217                         float duration = duration_ms / 1000.0;
218                         if(m_profiler){
219                                 switch(m_type){
220                                 case SPT_ADD:
221                                         m_profiler->add(m_name, duration);
222                                         break;
223                                 case SPT_AVG:
224                                         m_profiler->avg(m_name, duration);
225                                         break;
226                                 case SPT_GRAPH_ADD:
227                                         m_profiler->graphAdd(m_name, duration);
228                                         break;
229                                 }
230                         }
231                         delete m_timer;
232                 }
233         }
234 private:
235         Profiler *m_profiler;
236         std::string m_name;
237         TimeTaker *m_timer;
238         enum ScopeProfilerType m_type;
239 };
240
241 #endif
242