-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf_log.h
160 lines (137 loc) · 3.73 KB
/
perf_log.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef PERF_LOG_H
#define PERF_LOG_H
#include <vector>
#include <map>
#include <string>
#include <iomanip>
#include <iostream>
#define LOG_FREQ 1
class PerfTotal {
public:
int64_t total;
int64_t count;
PerfTotal() : total(0), count(0) { }
void log(int64_t x) {
total += x;
count++;
}
void set(int64_t x) {
total = x;
count++;
}
};
class PerfHist {
public:
std::vector<int64_t> buckets;
double min;
double max;
double bucket_size;
PerfHist() : buckets(100), min(0), max(100), bucket_size(1) {}
PerfHist(double min, double max, int64_t num_buckets) : buckets(num_buckets), min(min), max(max), bucket_size((max - min) / (double) num_buckets)
{
std::fill(buckets.begin(), buckets.end(), 0);
}
void log(double x) {
int64_t bucket = (x - min) / bucket_size;
bucket = std::min(bucket, (int64_t) buckets.size() - 1);
buckets[bucket]++;
}
void print(std::string s) {
std::cout << s << std::endl;
for(uint64_t i = 0; i < buckets.size(); i++) {
std::cout << "\033[1;34m" << std::setw(8) << min + bucket_size * i << "\033[0m";
}
std::cout << std::endl;
for(uint64_t i = 0; i < buckets.size(); i++) {
std::cout << "\033[1;34m" << std::setw(8) << buckets[i] << "\033[0m";
}
std::cout << std::endl;
}
};
class PerfSequence {
public:
std::vector<double> sequence;
PerfSequence() : sequence() {
sequence.reserve(1e9);
}
void log(double x) {
sequence.push_back(x);
}
};
class PerfLog {
public:
std::map<std::string, PerfTotal> tot_classes;
std::map<std::string, PerfHist> hist_classes;
std::map<std::string, PerfSequence> sequence_classes;
static PerfLog& get()
{
static PerfLog instance;
return instance;
}
void clear() {
tot_classes.clear();
hist_classes.clear();
sequence_classes.clear();
}
//
// Total logger stuff
//
void log_total(std::string s, int64_t x) {
tot_classes[s].log(x);
}
void set_total(std::string s, int64_t x) {
tot_classes[s].set(x);
}
int64_t get_total(std::string s) const {
if(tot_classes.count(s) == 0)
return 0;
return tot_classes.at(s).total;
}
int64_t get_count(std::string s) const {
if(tot_classes.count(s) == 0)
return 0;
return tot_classes.at(s).count;
}
//
// Histogram logger stuff
//
void add_histogram(std::string s, double min, double max, int num_buckets) {
hist_classes.emplace(s,PerfHist(min, max, num_buckets));
}
void log_hist(std::string s, double x) {
hist_classes[s].log(x);
}
const PerfHist get_hist(std::string s) const {
if(hist_classes.count(s) == 0)
return PerfHist();
auto to_ret = hist_classes.at(s);
return to_ret;
}
//
// Sequence Logger stuff
//
void add_sequence(std::string s) {
sequence_classes.emplace(s, PerfSequence());
}
void log_sequence(std::string s, double x) {
sequence_classes[s].log(x);
}
const std::vector<double>& get_sequence(std::string s) const {
return sequence_classes.at(s).sequence;
}
void print_sequence(std::string s) const {
std::cout << "=======" << std::endl;
std::cout << s << std::endl;
std::cout << "=======" << std::endl;
auto to_print = sequence_classes.at(s).sequence;
for(auto a : to_print) {
std::cout << a << std::endl;
}
}
private:
PerfLog(){}
public:
PerfLog(PerfLog const&) = delete;
void operator=(PerfLog const&) = delete;
};
#endif