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