]> git.lizzy.rs Git - minetest.git/blob - src/profiler.h
fade4c924779e9167e3ab36866166859aa2cb65c
[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 #pragma once
21
22 #include "irrlichttypes.h"
23 #include <string>
24 #include <map>
25
26 #include "threading/mutex_auto_lock.h"
27 #include "util/timetaker.h"
28 #include "util/numeric.h"      // paging()
29 #include "debug.h"             // assert()
30
31 #define MAX_PROFILER_TEXT_ROWS 20
32
33 // Global profiler
34 class Profiler;
35 extern Profiler *g_profiler;
36
37 /*
38         Time profiler
39 */
40
41 class Profiler
42 {
43 public:
44         Profiler() {}
45
46         void add(const std::string &name, float value)
47         {
48                 MutexAutoLock 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                 MutexAutoLock 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                 MutexAutoLock 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                 MutexAutoLock 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>::const_iterator i = m_data.begin();
119                                 i != m_data.end(); ++i) {
120                         if (maxindex == 0)
121                                 break;
122                         maxindex--;
123
124                         if (minindex != 0) {
125                                 minindex--;
126                                 continue;
127                         }
128
129                         int avgcount = 1;
130                         std::map<std::string, int>::const_iterator n = m_avgcounts.find(i->first);
131                         if (n != m_avgcounts.end()) {
132                                 if(n->second >= 1)
133                                         avgcount = n->second;
134                         }
135                         o << "  " << i->first << ": ";
136                         s32 clampsize = 40;
137                         s32 space = clampsize - i->first.size();
138                         for(s32 j = 0; j < space; j++) {
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                 MutexAutoLock 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                 MutexAutoLock lock(m_mutex);
164                 result = m_graphvalues;
165                 m_graphvalues.clear();
166         }
167
168         void remove(const std::string& name)
169         {
170                 MutexAutoLock lock(m_mutex);
171                 m_avgcounts.erase(name);
172                 m_data.erase(name);
173         }
174
175 private:
176         std::mutex 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                         ScopeProfilerType type = SPT_ADD);
193         ~ScopeProfiler();
194 private:
195         Profiler *m_profiler = nullptr;
196         std::string m_name;
197         TimeTaker *m_timer = nullptr;
198         enum ScopeProfilerType m_type;
199 };