Debian Codegraph @

common/Profiler.h

raw
1 /*
2 * Profiler.h
3 * ----------
4 * Purpose: Performance measuring
5 * Notes : (currently none)
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10
11 #pragma once
12
13 #include "openmpt/all/BuildSettings.hpp"
14
15
16 #include "mpt/mutex/mutex.hpp"
17
18 #include <string>
19 #include <vector>
20
21
22 OPENMPT_NAMESPACE_BEGIN
23
24
25 #if defined(MODPLUG_TRACKER)
26
27 //#define USE_PROFILER
28
29 #endif
30
31 #ifdef USE_PROFILER
32
33 class Profiler
34 {
35 public:
36 enum Category
37 {
38 GUI,
39 Audio,
40 Notify,
41 CategoriesCount
42 };
43 static std::vector<std::string> GetCategoryNames()
44 {
45 std::vector<std::string> ret;
46 ret.push_back("GUI");
47 ret.push_back("Audio");
48 ret.push_back("Notify");
49 return ret;
50 }
51 public:
52 static void Update();
53 static std::string DumpProfiles();
54 static std::vector<double> DumpCategories();
55 };
56
57
58 class Profile
59 {
60 private:
61 mutable mpt::mutex datamutex;
62 public:
63 struct Data
64 {
65 uint64 Calls;
66 uint64 Sum;
67 int64 Overhead;
68 uint64 Start;
69 };
70 public:
71 Data data;
72 uint64 EnterTime;
73 Profiler::Category Category;
74 const char * const Name;
75 uint64 GetTime() const;
76 uint64 GetFrequency() const;
77 public:
78 Profile(Profiler::Category category, const char *name);
79 ~Profile();
80 void Reset();
81 void Enter();
82 void Leave();
83 class Scope
84 {
85 private:
86 Profile &profile;
87 public:
88 Scope(Profile &p) : profile(p) { profile.Enter(); }
89 ~Scope() { profile.Leave(); }
90 };
91 public:
92 Data GetAndResetData();
93 };
94
95
96 #define OPENMPT_PROFILE_SCOPE(cat, name) \
97 static Profile OPENMPT_PROFILE_VAR(cat, name);\
98 Profile::Scope OPENMPT_PROFILE_SCOPE_VAR(OPENMPT_PROFILE_VAR); \
99 /**/
100
101
102 #define OPENMPT_PROFILE_FUNCTION(cat) OPENMPT_PROFILE_SCOPE(cat, __func__)
103
104
105 #else // !USE_PROFILER
106
107
108 class Profiler
109 {
110 public:
111 enum Category
112 {
113 CategoriesCount
114 };
115 static std::vector<std::string> GetCategoryNames() { return std::vector<std::string>(); }
116 public:
117 static void Update() { }
118 static std::string DumpProfiles() { return std::string(); }
119 static std::vector<double> DumpCategories() { return std::vector<double>(); }
120 };
121 #define OPENMPT_PROFILE_SCOPE(cat, name) do { } while(0)
122 #define OPENMPT_PROFILE_FUNCTION(cat) do { } while(0)
123
124
125 #endif // USE_PROFILER
126
127
128 OPENMPT_NAMESPACE_END