-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprobe_utils.h
214 lines (189 loc) · 4.53 KB
/
probe_utils.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// -*- mode: c++ -*-
#ifndef PROBE_UTILS_H
#define PROBE_UTILS_H
#include <cstring>
#include <cmath>
#include <mutex>
#include <set>
#include <iostream>
#include <iomanip>
#include <limits>
template <typename T> void OnProbe(T t);
#define PROBE_PROXY(klass) void klass::operator()() const { OnProbe(*this); }
namespace agg {
// aggregations
#define AGG(ins) decltype(global.ins)::Value ins = global.ins
template <typename Impl>
class Agg : public Impl {
public:
struct Value : public Impl {
Agg *parent;
Value(Agg &agg) : parent(&agg) {
parent->Add(this);
}
~Value() {
parent->Remove(this);
}
};
void Add(Value *node) {
std::lock_guard<std::mutex> _(m);
values.insert(node);
}
void Remove(Value *node) {
std::lock_guard<std::mutex> _(m);
(*this) << *node;
values.erase(node);
}
Impl operator()() {
Impl o;
o << *this;
std::lock_guard<std::mutex> _(m);
for (Value *e: values) {
o << *e;
}
return o;
}
protected:
std::set<Value *> values;
std::mutex m;
};
struct Sum {
long sum = 0;
Sum &operator<<(long value) {
sum += value;
return *this;
}
Sum &operator<<(const Sum &rhs) {
sum += rhs.sum;
return *this;
}
};
static inline std::ostream &operator<<(std::ostream &out, const Sum &s)
{
out << s.sum;
return out;
}
struct Average {
long sum = 0;
long cnt = 0;
Average &operator<<(long value) {
sum += value;
cnt++;
return *this;
}
Average &operator<<(const Average &rhs) {
sum += rhs.sum;
cnt += rhs.cnt;
return *this;
}
};
static inline std::ostream &operator<<(std::ostream &out, const Average &avg)
{
out << 1.0l * avg.sum / avg.cnt;
return out;
}
template <int N = 128, int Offset = 0, int Bucket = 100>
struct Histogram {
long hist[N];
Histogram() {
memset(hist, 0, sizeof(long) * N);
}
Histogram &operator<<(long value) {
long idx = (value - Offset) / Bucket;
if (idx >= 0 && idx < N) hist[idx]++;
return *this;
}
Histogram &operator<<(const Histogram &rhs) {
for (int i = 0; i < N; i++) hist[i] += rhs.hist[i];
return *this;
}
int CalculatePercentile(double scale) {
size_t total_nr = Count();
long medium_idx = total_nr * scale;
for (int i = 0; i < N; i++) {
medium_idx -= hist[i];
if (medium_idx < 0)
return i * Bucket + Offset;
}
return Offset; // 0?
}
int CalculateMedian() { return CalculatePercentile(0.5); }
size_t Count() {
size_t total_nr = 0;
for (int i = 0; i < N; i++)
total_nr += hist[i];
return total_nr;
}
};
template <int N, int Offset, int Bucket>
std::ostream &operator<<(std::ostream &out, const Histogram<N, Offset, Bucket>& h)
{
long last = std::numeric_limits<long>::max();
bool repeat = false;
long unit = 0;
for (int i = 0; i < N; i++)
if (unit < h.hist[i] / 100) unit = h.hist[i] / 100;
for (int i = 0; i < N; i++) {
if (last != h.hist[i]) {
long start = i * Bucket + Offset;
out << std::setw(10) << start
<< " - "
<< std::setw(10) << start + Bucket
<< ": "
<< std::setw(10) << h.hist[i] << " ";
if (unit > 0)
for (int j = 0; j < h.hist[i] / unit; j++)
out << '@';
out << std::endl;
last = h.hist[i];
repeat = false;
} else {
if (!repeat) {
out << "..." << std::endl;
repeat = true;
}
}
}
return out;
}
template <int N = 10, int Offset = 0, int Base = 2>
struct LogHistogram {
static constexpr int kNrBins = N;
long hist[N];
LogHistogram() {
memset(hist, 0, sizeof(long) * N);
}
LogHistogram &operator<<(long value) {
long idx = std::log2(value) / std::log2(Base) - Offset;
if (idx >= 0 && idx < N) hist[idx]++;
return *this;
}
LogHistogram &operator<<(const LogHistogram &rhs) {
for (int i = 0; i < N; i++) hist[i] += rhs.hist[i];
return *this;
}
};
template <int N = 10, int Offset, int Base = 2>
std::ostream &operator<<(std::ostream &out, const LogHistogram<N, Offset, Base> &h)
{
long unit = 0;
for (int i = 0; i < N; i++)
if (unit < h.hist[i] / 100) unit = h.hist[i] / 100;
long start = long(std::pow(Base, Offset));
for (int i = 0; i < N; i++) {
long end = start * Base;
out << std::setw(10) << start
<< " - "
<< std::setw(10) << end
<< ": "
<< std::setw(10) << h.hist[i] << " ";
if (unit > 0)
for (int j = 0; j < h.hist[i] / unit; j++)
out << '@';
out << std::endl;
start = end;
}
return out;
}
}
#endif /* PROBE_UTILS_H */