LatticeHashForest
Loading...
Searching...
No Matches
profiling.hpp
Go to the documentation of this file.
1
6#ifndef LHF_PROFILING_H
7#define LHF_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
19#include "lhf_config.hpp"
20
21namespace lhf {
22
27 using Count = uint64_t;
28 using String = std::string;
29 using TimePoint = std::chrono::steady_clock::time_point;
30 template <typename K, typename V> using Map = std::map<K, V>;
31
32 using Mutex = std::mutex;
33 using WriteLock = std::lock_guard<std::mutex>;
34 using ThreadID = std::thread::id;
35
36 std::mutex mutex;
37
38 struct Duration {
39 bool started = false;
42 long double duration = 0;
43
44 long double get_curr_duration_ms() {
45 using namespace std::chrono;
46 return (duration_cast<microseconds>(t2.time_since_epoch()).count() -
47 duration_cast<microseconds>(t1.time_since_epoch()).count()) /
48 1000.0;
49 }
50
51 long double get_cumul_duration_ms() { return duration; }
52 };
53
56
57 // Timer Functions
58
59 static inline const ThreadID currthread() {
60 return std::this_thread::get_id();
61 }
62
64 if (timers.count(std::this_thread::get_id()) == 0) {
65 timers[std::this_thread::get_id()] = {};
66 }
67
68 if (timers[std::this_thread::get_id()].count(s) == 0) {
69 timers[std::this_thread::get_id()][s] = Duration{};
70 }
71
72 return timers[std::this_thread::get_id()][s];
73 }
74
75 void timer_start(const String &s) {
77 auto &d = get_timer(s);
78 assert(!d.started && "timer already started");
79 d.started = true;
80 d.t1 = std::chrono::steady_clock::now();
81 }
82
83 void timer_end(const String &s) {
85 auto &d = get_timer(s);
86 assert(d.started && "timer already stopped");
87 d.started = false;
88 d.t2 = std::chrono::steady_clock::now();
89 d.duration += d.get_curr_duration_ms();
90 }
91
92 // Counter Functions
93
95 if (counters.count(s) == 0) {
96 counters[s] = 0;
97 }
98
99 return counters[s];
100 }
101
102 void inc_counter(const String &s) {
103 WriteLock m(mutex);
104 get_counter(s)++;
105 }
106
107 // dump
108
109 String dump() const {
110 using namespace std;
111 stringstream s;
112
113 if (counters.size() < 1 && timers.size() < 1) {
114 s << endl << "Profiler: No statistics generated" << endl;
115 return s.str();
116 }
117
118 s << endl << "Profiler Statistics:" << endl;
119 for (auto k : counters) {
120 s << " "
121 << "'" << k.first << "'"
122 << ": " << k.second << endl;
123 }
124 for (auto t : timers) {
125 if (timers.size() > 1) {
126 s << "Thread " << t.first << ":" << endl;
127 }
128 for (auto k : t.second) {
129 s << " "
130 << "'" << k.first << "'"
131 << ": " << k.second.get_cumul_duration_ms() << " ms" << endl;
132 }
133 }
134
135 return s.str();
136 }
137};
138
143 std::string key;
145
148 }
149
152 }
153};
154
172#ifdef LHF_ENABLE_PERFORMANCE_METRICS
173#define __lhf_calc_time(__stat, __key) \
174 auto __LHF_TIMER_OBJECT__ = __CalcTime((__stat), (__key))
175#define __lhf_calc_functime(__stat) \
176 auto __LHF_TIMER_OBJECT__ = __CalcTime((__stat), __func__)
177#else
178#define __lhf_calc_time(__stat, __key)
179#define __lhf_calc_functime(__stat)
180#endif
181
182}
183
184#endif
Any configurable constants go into this file.
Definition lhf.hpp:40
Utility class for enabling code-based profiling.
Definition profiling.hpp:26
std::thread::id ThreadID
Definition profiling.hpp:34
static const ThreadID currthread()
Definition profiling.hpp:59
Count & get_counter(const String &s)
Definition profiling.hpp:94
void timer_end(const String &s)
Definition profiling.hpp:83
std::lock_guard< std::mutex > WriteLock
Definition profiling.hpp:33
Duration & get_timer(const String &s)
Definition profiling.hpp:63
void inc_counter(const String &s)
Map< String, Count > counters
Definition profiling.hpp:54
std::chrono::steady_clock::time_point TimePoint
Definition profiling.hpp:29
Map< ThreadID, Map< String, Duration > > timers
Definition profiling.hpp:55
void timer_start(const String &s)
Definition profiling.hpp:75
The object used to enable the duration capturing mechanism.
__CalcTime(PerformanceStatistics &stat, const std::string &key)
std::string key
PerformanceStatistics & stat