Multilevel Deduplication Engine (MDE)
Loading...
Searching...
No Matches
profiling.hpp
Go to the documentation of this file.
1
6#ifndef MDE_PROFILING_H
7#define MDE_PROFILING_H
8
9#include <cassert>
10#include <chrono>
11#include <cstdint>
12#include <iostream>
13#include <map>
14#include <mutex>
15#include <sstream>
16#include <string>
17#include <thread>
18
19namespace mde {
20
25 using Count = uint64_t;
26 using String = std::string;
27 using TimePoint = std::chrono::steady_clock::time_point;
28 template <typename K, typename V> using Map = std::map<K, V>;
29
30 using Mutex = std::mutex;
31 using WriteLock = std::lock_guard<std::mutex>;
32 using ThreadID = std::thread::id;
33
34 std::mutex mutex;
35
36 struct Duration {
37 bool started = false;
40 long double duration = 0;
41
42 long double get_curr_duration_ms() {
43 using namespace std::chrono;
44 return (duration_cast<microseconds>(t2.time_since_epoch()).count() -
45 duration_cast<microseconds>(t1.time_since_epoch()).count()) /
46 1000.0;
47 }
48
49 long double get_cumul_duration_ms() { return duration; }
50 };
51
54
55 // Timer Functions
56
57 static inline const ThreadID currthread() {
58 return std::this_thread::get_id();
59 }
60
62 if (timers.count(std::this_thread::get_id()) == 0) {
63 timers[std::this_thread::get_id()] = {};
64 }
65
66 if (timers[std::this_thread::get_id()].count(s) == 0) {
67 timers[std::this_thread::get_id()][s] = Duration{};
68 }
69
70 return timers[std::this_thread::get_id()][s];
71 }
72
73 void timer_start(const String &s) {
75 auto &d = get_timer(s);
76 assert(!d.started && "timer already started");
77 d.started = true;
78 d.t1 = std::chrono::steady_clock::now();
79 }
80
81 void timer_end(const String &s) {
83 auto &d = get_timer(s);
84 assert(d.started && "timer already stopped");
85 d.started = false;
86 d.t2 = std::chrono::steady_clock::now();
87 d.duration += d.get_curr_duration_ms();
88 }
89
90 // Counter Functions
91
93 if (counters.count(s) == 0) {
94 counters[s] = 0;
95 }
96
97 return counters[s];
98 }
99
100 void inc_counter(const String &s) {
102 get_counter(s)++;
103 }
104
105 // dump
106
107 String dump() const {
108 using namespace std;
109 stringstream s;
110
111 if (counters.size() < 1 && timers.size() < 1) {
112 s << endl << "Profiler: No statistics generated" << endl;
113 return s.str();
114 }
115
116 s << endl << "Profiler Statistics:" << endl;
117 for (auto k : counters) {
118 s << " "
119 << "'" << k.first << "'"
120 << ": " << k.second << endl;
121 }
122 for (auto t : timers) {
123 if (timers.size() > 1) {
124 s << "Thread " << t.first << ":" << endl;
125 }
126 for (auto k : t.second) {
127 s << " "
128 << "'" << k.first << "'"
129 << ": " << k.second.get_cumul_duration_ms() << " ms" << endl;
130 }
131 }
132
133 return s.str();
134 }
135};
136
141 std::string key;
143
146 }
147
150 }
151};
152
170#ifdef MDE_ENABLE_PERFORMANCE_METRICS
171#define __mde_calc_time(__stat, __key) \
172 auto __MDE_TIMER_OBJECT__ = __CalcTime((__stat), (__key))
173#define __mde_calc_functime(__stat) \
174 auto __MDE_TIMER_OBJECT__ = __CalcTime((__stat), __func__)
175#else
176#define __mde_calc_time(__stat, __key)
177#define __mde_calc_functime(__stat)
178#endif
179
180}
181
182#endif
Definition mde.hpp:16
std::hash< T > DefaultHash
Utility class for enabling code-based profiling.
Definition profiling.hpp:24
Count & get_counter(const String &s)
Definition profiling.hpp:92
void timer_start(const String &s)
Definition profiling.hpp:73
Map< String, Count > counters
Definition profiling.hpp:52
std::chrono::steady_clock::time_point TimePoint
Definition profiling.hpp:27
std::thread::id ThreadID
Definition profiling.hpp:32
void timer_end(const String &s)
Definition profiling.hpp:81
Duration & get_timer(const String &s)
Definition profiling.hpp:61
Map< ThreadID, Map< String, Duration > > timers
Definition profiling.hpp:53
std::lock_guard< std::mutex > WriteLock
Definition profiling.hpp:31
void inc_counter(const String &s)
static const ThreadID currthread()
Definition profiling.hpp:57
The object used to enable the duration capturing mechanism.
std::string key
PerformanceStatistics & stat
__CalcTime(PerformanceStatistics &stat, const std::string &key)