Minetest  5.4.0
profiler.h
Go to the documentation of this file.
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 
29 #include "util/timetaker.h"
30 #include "util/numeric.h" // paging()
31 
32 // Global profiler
33 class Profiler;
34 extern Profiler *g_profiler;
35 
36 /*
37  Time profiler
38 */
39 
40 class Profiler
41 {
42 public:
43  Profiler();
44 
45  void add(const std::string &name, float value);
46  void avg(const std::string &name, float value);
47  void clear();
48 
49  float getValue(const std::string &name) const;
50  int getAvgCount(const std::string &name) const;
51  u64 getElapsedMs() const;
52 
53  typedef std::map<std::string, float> GraphValues;
54 
55  // Returns the line count
56  int print(std::ostream &o, u32 page = 1, u32 pagecount = 1);
57  void getPage(GraphValues &o, u32 page, u32 pagecount);
58 
59 
60  void graphAdd(const std::string &id, float value)
61  {
62  MutexAutoLock lock(m_mutex);
63  std::map<std::string, float>::iterator i =
64  m_graphvalues.find(id);
65  if(i == m_graphvalues.end())
66  m_graphvalues[id] = value;
67  else
68  i->second += value;
69  }
70  void graphGet(GraphValues &result)
71  {
72  MutexAutoLock lock(m_mutex);
73  result = m_graphvalues;
74  m_graphvalues.clear();
75  }
76 
77  void remove(const std::string& name)
78  {
79  MutexAutoLock lock(m_mutex);
80  m_avgcounts.erase(name);
81  m_data.erase(name);
82  }
83 
84 private:
85  std::mutex m_mutex;
86  std::map<std::string, float> m_data;
87  std::map<std::string, int> m_avgcounts;
88  std::map<std::string, float> m_graphvalues;
90 };
91 
96 };
97 
99 {
100 public:
101  ScopeProfiler(Profiler *profiler, const std::string &name,
102  ScopeProfilerType type = SPT_ADD);
103  ~ScopeProfiler();
104 private:
105  Profiler *m_profiler = nullptr;
106  std::string m_name;
107  TimeTaker *m_timer = nullptr;
109 };
Definition: profiler.h:41
u64 m_start_time
Definition: profiler.h:89
std::map< std::string, float > m_data
Definition: profiler.h:86
void graphAdd(const std::string &id, float value)
Definition: profiler.h:60
void getPage(GraphValues &o, u32 page, u32 pagecount)
Definition: profiler.cpp:163
void avg(const std::string &name, float value)
Definition: profiler.cpp:86
void add(const std::string &name, float value)
Definition: profiler.cpp:63
void remove(const std::string &name)
Definition: profiler.h:77
int getAvgCount(const std::string &name) const
Definition: profiler.cpp:121
Profiler()
Definition: profiler.cpp:58
void clear()
Definition: profiler.cpp:96
std::map< std::string, int > m_avgcounts
Definition: profiler.h:87
float getValue(const std::string &name) const
Definition: profiler.cpp:106
std::mutex m_mutex
Definition: profiler.h:85
u64 getElapsedMs() const
Definition: profiler.cpp:131
int print(std::ostream &o, u32 page=1, u32 pagecount=1)
Definition: profiler.cpp:136
std::map< std::string, float > m_graphvalues
Definition: profiler.h:88
void graphGet(GraphValues &result)
Definition: profiler.h:70
std::map< std::string, float > GraphValues
Definition: profiler.h:53
Definition: profiler.h:99
~ScopeProfiler()
Definition: profiler.cpp:35
std::string m_name
Definition: profiler.h:106
ScopeProfiler(Profiler *profiler, const std::string &name, ScopeProfilerType type=SPT_ADD)
Definition: profiler.cpp:25
Profiler * m_profiler
Definition: profiler.h:105
TimeTaker * m_timer
Definition: profiler.h:107
enum ScopeProfilerType m_type
Definition: profiler.h:108
Definition: timetaker.h:30
std::unique_lock< std::mutex > MutexAutoLock
Definition: mutex_auto_lock.h:29
Profiler * g_profiler
Definition: profiler.cpp:24
ScopeProfilerType
Definition: profiler.h:92
@ SPT_AVG
Definition: profiler.h:94
@ SPT_ADD
Definition: profiler.h:93
@ SPT_GRAPH_ADD
Definition: profiler.h:95