forked from shaygalon/memcache-perf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectionStats.h
335 lines (266 loc) · 10.4 KB
/
ConnectionStats.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* -*- c++ -*- */
#ifndef CONNECTIONSTATS_H
#define CONNECTIONSTATS_H
#include <algorithm>
#include <inttypes.h>
#include <vector>
#include <iostream>
#include "AgentStats.h"
#include "Operation.h"
#include "LogHistogramSampler.h"
using namespace std;
#ifdef STATIC_ALLOC_SAMPLER
// Static allocation
class ConnectionStats {
public:
static int details[];
static int ndetails;
LogHistogramSampler get_sampler;
LogHistogramSampler set_sampler;
LogHistogramSampler op_sampler;
uint64_t rx_bytes, tx_bytes;
uint64_t gets, sets, get_misses;
uint64_t skips;
uint64_t gets_dyn[MAX_INTERVALS], sets_dyn[MAX_INTERVALS];
double start, stop;
bool sampling;
bool plotall;
int n_intervals;
// Constructor
ConnectionStats(bool _sampling = true, int n_intervals = 1) :
get_sampler(LOGSAMPLER_BINS, n_intervals), set_sampler(LOGSAMPLER_BINS, n_intervals), op_sampler(LOGSAMPLER_BINS, n_intervals),
rx_bytes(0), tx_bytes(0), gets(0), sets(0), start(0), stop(0), plotall(false),
get_misses(0), skips(0), sampling(_sampling) {
this->n_intervals = n_intervals;
for(int i = 0; i < MAX_INTERVALS; i++) {
gets_dyn[i] = 0;
sets_dyn[i] = 0;
}
}
// Logging functions
void log_get(Operation& op) { if (sampling) get_sampler.sample(op); gets++; gets_dyn[op.interval]++;}
void log_set(Operation& op) { if (sampling) set_sampler.sample(op); sets++; sets_dyn[op.interval]++;}
void log_op (double op) { if (sampling) op_sampler.sample(op); }
// Get overall qps
double get_qps() {
return (gets + sets) / (stop - start);
}
double get_nth(double nth, int interval = 0) {
double get_val=get_sampler.get_nth(nth, interval);
double set_val=set_sampler.total(interval)>0 ? set_sampler.get_nth(nth, interval):0.0;
double ret_val=get_val>set_val?get_val:set_val;
return ret_val;
}
double get_avg(int interval = 0) {
double get_val=get_sampler.average(interval);
double set_val=set_sampler.total(interval)>0 ? set_sampler.average(interval):0.0;
double ret_val=get_val>set_val?get_val:set_val;
return ret_val;
}
// Accumulate
void accumulate(const ConnectionStats &cs) {
get_sampler.accumulate(cs.get_sampler);
set_sampler.accumulate(cs.set_sampler);
op_sampler.accumulate(cs.op_sampler);
rx_bytes += cs.rx_bytes;
tx_bytes += cs.tx_bytes;
gets += cs.gets;
sets += cs.sets;
for(int i = 0; i < n_intervals; i++) {
gets_dyn[i] += cs.gets_dyn[i];
sets_dyn[i] += cs.sets_dyn[i];
}
get_misses += cs.get_misses;
skips += cs.skips;
start = cs.start;
stop = cs.stop;
}
// Accumulate - send from agent to master
void accumulate(const AgentStats &as) {
rx_bytes += as.rx_bytes;
tx_bytes += as.tx_bytes;
gets += as.gets;
sets += as.sets;
get_misses += as.get_misses;
skips += as.skips;
start = as.start;
stop = as.stop;
for(int i = 0; i < n_intervals; i++) {
gets_dyn[i] += as.gets_dyn[i];
sets_dyn[i] += as.sets_dyn[i];
}
#ifdef LOGSAMPLER_BINS
for (int i = 0; i < n_intervals; i++) {
for (int j=0; j<LOGSAMPLER_BINS; j++)
get_sampler.bins[i][j] += as.get_bins[i][j];
get_sampler.sum[i] += as.get_sum[i];
get_sampler.sum_sq[i] += as.get_sum_sq[i];
}
#endif
}
static void print_header(bool newline=true) {
int i;
printf("%-7s %7s %7s %7s",
"#type", "avg", "std", "min");
for (i=0; i<ndetails; i++) {
char buf[8];
sprintf(buf,"p%d",details[i]);
printf(" %7s",buf);
}
if (newline)
printf("\n");
}
void print_stats(const char *tag, LogHistogramSampler &sampler,
bool newline = true, bool plotit=false, int interval = 0) {
int i;
if (sampler.total(interval) == 0) {
printf("%-7s %7.1f %7.1f %7.1f",
tag, 0.0, 0.0, 0.0);
for (i=0; i<ndetails; i++) {
printf(" %7.1f",0.0);
}
if (newline) printf("\n");
return;
}
printf("%-7s %7.1f %7.1f %7.1f",
tag, sampler.average(interval), sampler.stddev(interval),
sampler.get_nth(0, interval));
for (i=0; i<ndetails; i++) {
printf(" %7.1f",sampler.get_nth(details[i], interval));
}
if (newline)
printf("\n");
if (plotit || plotall)
sampler.plot(tag,get_qps());
}
};
#else
// Dynamic allocation
class ConnectionStats {
public:
static int details[];
static int ndetails;
LogHistogramSampler get_sampler;
LogHistogramSampler set_sampler;
LogHistogramSampler op_sampler;
uint64_t rx_bytes, tx_bytes;
uint64_t gets, sets, get_misses;
uint64_t skips;
uint64_t *gets_dyn, *sets_dyn;
double start, stop;
bool sampling;
bool plotall;
int n_intervals;
// Constructor
ConnectionStats(bool _sampling = true, int n_intervals = 1) :
get_sampler(LOGSAMPLER_BINS, n_intervals), set_sampler(LOGSAMPLER_BINS, n_intervals), op_sampler(LOGSAMPLER_BINS, n_intervals),
rx_bytes(0), tx_bytes(0), gets(0), sets(0), start(0), stop(0), plotall(false),
get_misses(0), skips(0), sampling(_sampling) {
this->n_intervals = n_intervals;
gets_dyn = new uint64_t[n_intervals] ();
sets_dyn = new uint64_t[n_intervals] ();
}
// Destructor
~ConnectionStats() {
delete[] gets_dyn;
delete[] sets_dyn;
}
// Logging functions
void log_get(Operation& op) { if (sampling) get_sampler.sample(op); gets++; gets_dyn[op.interval]++;}
void log_set(Operation& op) { if (sampling) set_sampler.sample(op); sets++; sets_dyn[op.interval]++;}
void log_op (double op) { if (sampling) op_sampler.sample(op); }
// Get overall qps
double get_qps() {
return (gets + sets) / (stop - start);
}
double get_nth(double nth, int interval = 0) {
double get_val=get_sampler.get_nth(nth, interval);
double set_val=set_sampler.total(interval)>0 ? set_sampler.get_nth(nth, interval):0.0;
double ret_val=get_val>set_val?get_val:set_val;
return ret_val;
}
double get_avg(int interval = 0) {
double get_val=get_sampler.average(interval);
double set_val=set_sampler.total(interval)>0 ? set_sampler.average(interval):0.0;
double ret_val=get_val>set_val?get_val:set_val;
return ret_val;
}
// Accumulate
void accumulate(const ConnectionStats &cs) {
get_sampler.accumulate(cs.get_sampler);
set_sampler.accumulate(cs.set_sampler);
op_sampler.accumulate(cs.op_sampler);
rx_bytes += cs.rx_bytes;
tx_bytes += cs.tx_bytes;
gets += cs.gets;
sets += cs.sets;
for(int i = 0; i < n_intervals; i++) {
gets_dyn[i] += cs.gets_dyn[i];
sets_dyn[i] += cs.sets_dyn[i];
}
get_misses += cs.get_misses;
skips += cs.skips;
start = cs.start;
stop = cs.stop;
}
// Accumulate - send from agent to master
void accumulate(const AgentStats &as) {
rx_bytes += as.bs.rx_bytes;
tx_bytes += as.bs.tx_bytes;
gets += as.bs.gets;
sets += as.bs.sets;
get_misses += as.bs.get_misses;
skips += as.bs.skips;
start = as.bs.start;
stop = as.bs.stop;
for(int i = 0; i < n_intervals; i++) {
gets_dyn[i] += as.gets_dyn[i];
sets_dyn[i] += as.sets_dyn[i];
}
#ifdef LOGSAMPLER_BINS
for (int i = 0; i < n_intervals; i++) {
for (int j=0; j<LOGSAMPLER_BINS; j++)
get_sampler.bins[i][j] += as.get_bins[i][j];
get_sampler.sum[i] += as.get_sum[i];
get_sampler.sum_sq[i] += as.get_sum_sq[i];
}
#endif
}
static void print_header(bool newline=true) {
int i;
printf("%-7s %7s %7s %7s",
"#type", "avg", "std", "min");
for (i=0; i<ndetails; i++) {
char buf[8];
sprintf(buf,"p%d",details[i]);
printf(" %7s",buf);
}
if (newline)
printf("\n");
}
void print_stats(const char *tag, LogHistogramSampler &sampler,
bool newline = true, bool plotit=false, int interval = 0) {
int i;
if (sampler.total(interval) == 0) {
printf("%-7s %7.1f %7.1f %7.1f",
tag, 0.0, 0.0, 0.0);
for (i=0; i<ndetails; i++) {
printf(" %7.1f",0.0);
}
if (newline) printf("\n");
return;
}
printf("%-7s %7.1f %7.1f %7.1f",
tag, sampler.average(interval), sampler.stddev(interval),
sampler.get_nth(0, interval));
for (i=0; i<ndetails; i++) {
printf(" %7.1f",sampler.get_nth(details[i], interval));
}
if (newline)
printf("\n");
if (plotit || plotall)
sampler.plot(tag,get_qps());
}
};
#endif
#endif // CONNECTIONSTATS_H