-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimonitoring.h
105 lines (83 loc) · 3 KB
/
minimonitoring.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef MINIMONITORING_H
#define MINIMONITORING_H
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#include <stack>
#include <chrono>
#include <map>
#include <tuple>
static std::ofstream& operator<<(std::ofstream& ofs, const std::vector<std::string>& args) {
std::string sep = "";
for (const auto &s : args) {
ofs << sep;
ofs << s;
sep = ",";
}
return ofs;
}
class MiniMon {
public:
MiniMon() = default;
void start() {
_entries.push(std::chrono::high_resolution_clock::now());
}
void stop( const std::string& n, uint32_t p, uint64_t e = 1,
uint64_t f = 0, uint64_t r = 0, uint64_t w = 0 ) {
auto& top = _entries.top();
_store[ {n,p,e,f} ].apply( std::chrono::high_resolution_clock::now() - top );
_entries.pop();
}
double get(const std::string& n) {
double res = 0.0;
const auto it = _store.lower_bound({n,0,0,0});
const auto itend = _store.upper_bound({n,UINT32_MAX,UINT32_MAX,UINT64_MAX});
while (it != itend)
res += it->second.runtime_sum.count();
return res;
}
void print(uint32_t id, const std::vector<std::string>& tags) const {
/* print out log to individual files */
{
std::ofstream file;
std::ostringstream file_name;
file_name << "overview_" << std::setw(5) << std::setfill('0') << id << ".csv";
file.open(file_name.str());
file << "# tag;function_name;par;elements;flops;num_calls;avg_runtime;min_runtime;max_runtime"
<< std::endl;
for (auto& e : _store) {
file << tags << ";" <<
std::get<0>(e.first) << ";" <<
std::get<1>(e.first) << ";" <<
std::get<2>(e.first) << ";" <<
std::get<3>(e.first) << ";" <<
e.second.num << ";" <<
e.second.runtime_sum.count() / e.second.num << ";" <<
e.second.runtime_min.count() << ";" <<
e.second.runtime_max.count() << std::endl;
}
file.close();
}
}
private:
using time_point_t = std::chrono::time_point<std::chrono::high_resolution_clock>;
using time_diff_t = std::chrono::duration<double>;
struct MiniMonValue {
time_diff_t runtime_sum;
time_diff_t runtime_min;
time_diff_t runtime_max;
uint32_t num;
//std::numeric_limits<int>::max()
MiniMonValue( ) : runtime_sum(0.0), runtime_min(1.0e300), runtime_max(0.0), num(0) {}
void apply( time_diff_t value ) {
runtime_sum += value;
runtime_min= std::min( runtime_min, value );
runtime_max= std::max( runtime_max, value );
num += 1;
}
};
std::map<std::tuple<std::string,uint32_t,uint64_t,uint64_t>,MiniMonValue> _store;
std::stack<time_point_t, std::vector<time_point_t>> _entries;
};
#endif /* MINIMONITORING_H */