]> git.lizzy.rs Git - minetest.git/blob - src/profiler.h
Move files to subdirectories (#6599)
[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 <cassert>
24 #include <string>
25 #include <map>
26 #include <ostream>
27
28 #include "threading/mutex_auto_lock.h"
29 #include "util/timetaker.h"
30 #include "util/numeric.h"      // paging()
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() = default;
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 (auto &it : m_data) {
84                         it.second = 0;
85                 }
86                 m_avgcounts.clear();
87         }
88
89         void print(std::ostream &o)
90         {
91                 printPage(o, 1, 1);
92         }
93
94         float getValue(const std::string &name) const
95         {
96                 std::map<std::string, float>::const_iterator numerator = m_data.find(name);
97                 if (numerator == m_data.end())
98                         return 0.f;
99
100                 std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
101                 if (denominator != m_avgcounts.end()){
102                         if (denominator->second >= 1)
103                                 return numerator->second / denominator->second;
104                 }
105
106                 return numerator->second;
107         }
108
109         void printPage(std::ostream &o, u32 page, u32 pagecount)
110         {
111                 MutexAutoLock lock(m_mutex);
112
113                 u32 minindex, maxindex;
114                 paging(m_data.size(), page, pagecount, minindex, maxindex);
115
116                 for (std::map<std::string, float>::const_iterator i = m_data.begin();
117                                 i != m_data.end(); ++i) {
118                         if (maxindex == 0)
119                                 break;
120                         maxindex--;
121
122                         if (minindex != 0) {
123                                 minindex--;
124                                 continue;
125                         }
126
127                         int avgcount = 1;
128                         std::map<std::string, int>::const_iterator n = m_avgcounts.find(i->first);
129                         if (n != m_avgcounts.end()) {
130                                 if(n->second >= 1)
131                                         avgcount = n->second;
132                         }
133                         o << "  " << i->first << ": ";
134                         s32 clampsize = 40;
135                         s32 space = clampsize - i->first.size();
136                         for(s32 j = 0; j < space; j++) {
137                                 if (j % 2 == 0 && j < space - 1)
138                                         o << "-";
139                                 else
140                                         o << " ";
141                         }
142                         o << (i->second / avgcount);
143                         o << std::endl;
144                 }
145         }
146
147         typedef std::map<std::string, float> GraphValues;
148
149         void graphAdd(const std::string &id, float value)
150         {
151                 MutexAutoLock lock(m_mutex);
152                 std::map<std::string, float>::iterator i =
153                                 m_graphvalues.find(id);
154                 if(i == m_graphvalues.end())
155                         m_graphvalues[id] = value;
156                 else
157                         i->second += value;
158         }
159         void graphGet(GraphValues &result)
160         {
161                 MutexAutoLock lock(m_mutex);
162                 result = m_graphvalues;
163                 m_graphvalues.clear();
164         }
165
166         void remove(const std::string& name)
167         {
168                 MutexAutoLock lock(m_mutex);
169                 m_avgcounts.erase(name);
170                 m_data.erase(name);
171         }
172
173 private:
174         std::mutex m_mutex;
175         std::map<std::string, float> m_data;
176         std::map<std::string, int> m_avgcounts;
177         std::map<std::string, float> m_graphvalues;
178 };
179
180 enum ScopeProfilerType{
181         SPT_ADD,
182         SPT_AVG,
183         SPT_GRAPH_ADD
184 };
185
186 class ScopeProfiler
187 {
188 public:
189         ScopeProfiler(Profiler *profiler, const std::string &name,
190                         ScopeProfilerType type = SPT_ADD);
191         ~ScopeProfiler();
192 private:
193         Profiler *m_profiler = nullptr;
194         std::string m_name;
195         TimeTaker *m_timer = nullptr;
196         enum ScopeProfilerType m_type;
197 };