]> git.lizzy.rs Git - dragonfireclient.git/blob - src/profiler.h
Trigger on_rightclick regardless on the formspec meta field
[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 "threading/mutex_auto_lock.h"
28 #include "util/timetaker.h"
29 #include "util/numeric.h"      // paging()
30 #include "debug.h"             // assert()
31
32 #define MAX_PROFILER_TEXT_ROWS 20
33
34 // Global profiler
35 class Profiler;
36 extern Profiler *g_profiler;
37
38 /*
39         Time profiler
40 */
41
42 class Profiler
43 {
44 public:
45         Profiler() {}
46
47         void add(const std::string &name, float value)
48         {
49                 MutexAutoLock lock(m_mutex);
50                 {
51                         /* No average shall have been used; mark add used as -2 */
52                         std::map<std::string, int>::iterator n = m_avgcounts.find(name);
53                         if(n == m_avgcounts.end())
54                                 m_avgcounts[name] = -2;
55                         else{
56                                 if(n->second == -1)
57                                         n->second = -2;
58                                 assert(n->second == -2);
59                         }
60                 }
61                 {
62                         std::map<std::string, float>::iterator n = m_data.find(name);
63                         if(n == m_data.end())
64                                 m_data[name] = value;
65                         else
66                                 n->second += value;
67                 }
68         }
69
70         void avg(const std::string &name, float value)
71         {
72                 MutexAutoLock lock(m_mutex);
73                 int &count = m_avgcounts[name];
74
75                 assert(count != -2);
76                 count = MYMAX(count, 0) + 1;
77                 m_data[name] += value;
78         }
79
80         void clear()
81         {
82                 MutexAutoLock lock(m_mutex);
83                 for(std::map<std::string, float>::iterator
84                                 i = m_data.begin();
85                                 i != m_data.end(); ++i)
86                 {
87                         i->second = 0;
88                 }
89                 m_avgcounts.clear();
90         }
91
92         void print(std::ostream &o)
93         {
94                 printPage(o, 1, 1);
95         }
96
97         float getValue(const std::string &name) const
98         {
99                 std::map<std::string, float>::const_iterator numerator = m_data.find(name);
100                 if (numerator == m_data.end())
101                         return 0.f;
102
103                 std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
104                 if (denominator != m_avgcounts.end()){
105                         if (denominator->second >= 1)
106                                 return numerator->second / denominator->second;
107                 }
108
109                 return numerator->second;
110         }
111
112         void printPage(std::ostream &o, u32 page, u32 pagecount)
113         {
114                 MutexAutoLock lock(m_mutex);
115
116                 u32 minindex, maxindex;
117                 paging(m_data.size(), page, pagecount, minindex, maxindex);
118
119                 for (std::map<std::string, float>::const_iterator i = m_data.begin();
120                                 i != m_data.end(); ++i) {
121                         if (maxindex == 0)
122                                 break;
123                         maxindex--;
124
125                         if (minindex != 0) {
126                                 minindex--;
127                                 continue;
128                         }
129
130                         int avgcount = 1;
131                         std::map<std::string, int>::const_iterator n = m_avgcounts.find(i->first);
132                         if (n != m_avgcounts.end()) {
133                                 if(n->second >= 1)
134                                         avgcount = n->second;
135                         }
136                         o << "  " << i->first << ": ";
137                         s32 clampsize = 40;
138                         s32 space = clampsize - i->first.size();
139                         for(s32 j = 0; j < space; j++) {
140                                 if (j % 2 == 0 && j < space - 1)
141                                         o << "-";
142                                 else
143                                         o << " ";
144                         }
145                         o << (i->second / avgcount);
146                         o << std::endl;
147                 }
148         }
149
150         typedef std::map<std::string, float> GraphValues;
151
152         void graphAdd(const std::string &id, float value)
153         {
154                 MutexAutoLock lock(m_mutex);
155                 std::map<std::string, float>::iterator i =
156                                 m_graphvalues.find(id);
157                 if(i == m_graphvalues.end())
158                         m_graphvalues[id] = value;
159                 else
160                         i->second += value;
161         }
162         void graphGet(GraphValues &result)
163         {
164                 MutexAutoLock lock(m_mutex);
165                 result = m_graphvalues;
166                 m_graphvalues.clear();
167         }
168
169         void remove(const std::string& name)
170         {
171                 MutexAutoLock lock(m_mutex);
172                 m_avgcounts.erase(name);
173                 m_data.erase(name);
174         }
175
176 private:
177         std::mutex m_mutex;
178         std::map<std::string, float> m_data;
179         std::map<std::string, int> m_avgcounts;
180         std::map<std::string, float> m_graphvalues;
181 };
182
183 enum ScopeProfilerType{
184         SPT_ADD,
185         SPT_AVG,
186         SPT_GRAPH_ADD
187 };
188
189 class ScopeProfiler
190 {
191 public:
192         ScopeProfiler(Profiler *profiler, const std::string &name,
193                         ScopeProfilerType type = SPT_ADD);
194         ~ScopeProfiler();
195 private:
196         Profiler *m_profiler = nullptr;
197         std::string m_name;
198         TimeTaker *m_timer = nullptr;
199         enum ScopeProfilerType m_type;
200 };
201
202 #endif
203