-
Notifications
You must be signed in to change notification settings - Fork 33
/
htgram.c
329 lines (269 loc) · 8.58 KB
/
htgram.c
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
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include "htgram.h"
struct htgram_bin_st {
int64_t start;
int64_t width;
uint64_t count;
};
struct htgram_st {
int64_t bin_start;
int64_t bin_start_width;
double bin_width_growth;
size_t num_bins;
struct htgram_bin_st *bins;
uint64_t lt_count; // For data points < the bins.
uint64_t gt_count; // For data points > the bins.
// For data points > the bins, there may be another
// histogram instead of using gt_count.
//
HTGRAM_HANDLE next;
};
static void emit_bar(int64_t start, int64_t width, uint64_t count,
uint64_t max_count, uint64_t tot_count, uint64_t run_count,
char *buf, int buf_len,
int plus_spaces, int equal_spaces, int space_spaces);
HTGRAM_HANDLE htgram_mk(int64_t bin_start,
int64_t bin_start_width,
double bin_width_growth,
size_t num_bins,
HTGRAM_HANDLE next) {
struct htgram_st *h = calloc(sizeof(struct htgram_st), 1);
if (h == NULL) {
return NULL;
}
h->bin_start = bin_start;
h->bin_start_width = bin_start_width;
h->bin_width_growth = bin_width_growth;
h->num_bins = num_bins;
h->next = next;
if (num_bins > 0) {
h->bins = calloc(sizeof(struct htgram_bin_st), num_bins);
if (h->bins == NULL) {
free(h);
return NULL;
}
int64_t r = bin_start;
int64_t w = bin_start_width;
for (size_t i = 0; i < num_bins; i++) {
h->bins[i].start = r;
h->bins[i].width = w;
r = r + w;
w = w * bin_width_growth;
}
}
return h;
}
void htgram_destroy(HTGRAM_HANDLE h) {
if (h->bins != NULL) {
free(h->bins);
}
if (h->next != NULL) {
htgram_destroy(h->next);
}
free(h);
}
int64_t htgram_get_bin_start(HTGRAM_HANDLE h) {
return h->bin_start;
}
int64_t htgram_get_bin_start_width(HTGRAM_HANDLE h) {
return h->bin_start_width;
}
double htgram_get_bin_width_growth(HTGRAM_HANDLE h) {
return h->bin_width_growth;
}
size_t htgram_get_num_bins(HTGRAM_HANDLE h) {
return h->num_bins;
}
void htgram_incr(HTGRAM_HANDLE h, int64_t data_point, uint64_t count) {
if (data_point < h->bin_start) {
h->lt_count += count;
return;
}
size_t i = 0;
if (h->bin_width_growth == 1.0) {
i = data_point / h->bin_start_width;
}
while (i < h->num_bins) {
if (data_point < (h->bins[i].start +
h->bins[i].width)) {
h->bins[i].count += count;
return;
}
i++;
}
if (h->next != NULL) {
htgram_incr(h->next, data_point, count);
return;
}
h->gt_count += count;
}
bool htgram_get_bin_data(HTGRAM_HANDLE h, int bin_index,
int64_t *out_bin_start,
int64_t *out_bin_width,
uint64_t *out_bin_count) {
if (bin_index < 0) {
*out_bin_count = h->lt_count;
return false;
}
if (bin_index >= (int) h->num_bins) {
if (h->next != NULL) {
return htgram_get_bin_data(h->next, bin_index - h->num_bins,
out_bin_start,
out_bin_width,
out_bin_count);
}
*out_bin_count = h->gt_count;
return false;
}
*out_bin_start = h->bins[bin_index].start;
*out_bin_width = h->bins[bin_index].width;
*out_bin_count = h->bins[bin_index].count;
return true;
}
void htgram_reset(HTGRAM_HANDLE h) {
for (size_t i = 0; i < h->num_bins; i++) {
h->bins[i].count = 0;
}
h->lt_count = 0;
h->gt_count = 0;
if (h->next != NULL) {
htgram_reset(h->next);
}
}
void htgram_add(HTGRAM_HANDLE agg, HTGRAM_HANDLE x) {
int64_t astart;
int64_t awidth;
uint64_t acount;
int64_t xstart;
int64_t xwidth;
uint64_t xcount;
int i = 0;
while (htgram_get_bin_data(agg, i, &astart, &awidth, &acount) &&
htgram_get_bin_data(x, i, &xstart, &xwidth, &xcount)) {
assert(astart == xstart);
assert(awidth == xwidth);
htgram_incr(agg, astart, xcount);
i++;
}
}
void htgram_dump(HTGRAM_HANDLE h,
HTGRAM_DUMP_CALLBACK dump_callback, void *dump_callback_data) {
if (h == NULL) {
return;
}
int64_t max_start = 0;
int64_t max_width = 0;
uint64_t max_count = 0;
int end_num_bins = 0; // Max non-zero bin.
int beg_bin = INT_MAX;
uint64_t tot_count = 0;
uint64_t run_count; // Cummulative count.
int64_t start;
int64_t width;
uint64_t count;
int num_bins = 0;
while (htgram_get_bin_data(h, num_bins, &start, &width, &count)) {
num_bins++;
if (count > 0) {
if (max_start < start) {
max_start = start;
}
if (max_width < width) {
max_width = width;
}
if (max_count < count) {
max_count = count;
}
if (end_num_bins < num_bins) {
end_num_bins = num_bins;
}
if (beg_bin > num_bins - 1) {
beg_bin = num_bins - 1;
}
tot_count = tot_count + count;
}
}
// Columns in a row look like "START+WIDTH=COUNT PCT% BAR_GRAPH_LINE"
//
int max_plus = 0; // Width of the 'plus' column ('+').
int max_equal = 0; // Width of the 'equal' column ('=').
int max_space = 0; // Width of the 'space' column (' ').
char buf[2000];
run_count = 0;
for (int i = beg_bin; i < end_num_bins + 1 && i < num_bins; i++) {
if (htgram_get_bin_data(h, i, &start, &width, &count)) {
emit_bar(start, width, count, max_count, tot_count, run_count,
buf, sizeof(buf) - 1, 0, 0, 0);
char *s0 = strchr(buf, '+');
assert(s0 != NULL);
if (max_plus < s0 - buf) {
max_plus = s0 - buf;
}
char *s1 = strchr(s0, '=');
assert(s1 != NULL);
if (max_equal < s1 - s0) {
max_equal = s1 - s0;
}
char *s2 = strchr(s1, '%');
assert(s2 != NULL);
if (max_space < s2 - s1) {
max_space = s2 - s1;
}
run_count += count;
}
}
run_count = 0;
for (int i = beg_bin; i < end_num_bins + 1 && i < num_bins; i++) {
if (htgram_get_bin_data(h, i, &start, &width, &count)) {
emit_bar(start, width, count, max_count, tot_count, run_count,
buf, sizeof(buf) - 1, 0, 0, 0);
char *s0 = strchr(buf, '+');
char *s1 = strchr(s0, '=');
char *s2 = strchr(s1, '%');
emit_bar(start, width, count, max_count, tot_count, run_count,
buf, sizeof(buf) - 1,
max_plus - (s0 - buf),
max_equal - (s1 - s0),
max_space - (s2 - s1));
dump_callback(h, buf, dump_callback_data);
run_count += count;
}
}
}
static void fill_spaces(char *buf, int buf_len, int num_spaces) {
int i;
for (i = 0; i < num_spaces && i < buf_len - 1; i++) {
buf[i] = ' ';
}
buf[i] = '\0';
}
static void emit_bar(int64_t start, int64_t width, uint64_t count,
uint64_t max_count, uint64_t tot_count, uint64_t run_count,
char *buf, int buf_len,
int plus_spaces, int equal_spaces, int space_spaces) {
char bar_buf[25];
char plus_buf[40];
char equal_buf[40];
char space_buf[40];
size_t j = 0;
if (count > 0) {
uint64_t bar = (((sizeof(bar_buf) - 1) * count) / max_count);
while (j < bar && j < sizeof(bar_buf) - 1) {
bar_buf[j] = '*';
j++;
}
}
bar_buf[j] = '\0';
fill_spaces(plus_buf, sizeof(plus_buf), plus_spaces);
fill_spaces(equal_buf, sizeof(equal_buf), equal_spaces);
fill_spaces(space_buf, sizeof(space_buf), space_spaces);
snprintf(buf, buf_len - 1, "%s%lld+%lld%s=%llu %s%.2f%% %s",
plus_buf, start, width, equal_buf, count, space_buf,
100.0 * ((float) (count + run_count) / (float) tot_count), bar_buf);
}