6 unsigned out_below, out_above;
11 int histogram_init(struct histogram *h,
16 h->allocated = max_value/resolution + 1;
17 h->resolution = resolution;
18 h->out_below = h->out_above = 0;
19 mem = h->allocated*sizeof(*h->data);
20 h->data = malloc(mem);
22 memset(h->data, 0, mem);
28 void histogram_add(struct histogram *h, int value)
34 unsigned index = value / h->resolution;
35 if (index >= h->allocated) {
37 index = h->allocated - 1;
42 void histogram_fprint(struct histogram *h, FILE *f)
44 unsigned long long sum = 0, cum;
50 for (i = 0; i < h->allocated; i++)
53 for (i = 0; i < h->allocated; i++) {
54 if (h->data[i] != 0) {
55 fprintf(f, "%g %lld\n", 1e-3*(i*h->resolution), cum);
62 struct histogram_stats {
64 unsigned long long sum;
66 unsigned below, above; /* Count of data out of histogram range */
67 unsigned percentile[101];
70 void histogram_stats(struct histogram *h, struct histogram_stats *s)
72 unsigned long long sum;
78 memset(s, 0, sizeof(*s));
80 for (i = 0; i < h->allocated; i++) {
81 s->count += h->data[i];
82 s->sum += h->data[i] * i * h->resolution;
87 s->avg = s->sum / s->count;
89 for (i = 0, j = 0, sum = 0; i < 100; i++) {
90 while (sum <= i * s->count / 100)
93 s->percentile[i] = (j-1) * h->resolution;
95 while (sum < i * s->count / 100)
97 s->percentile[100] = (j-1) * h->resolution;
99 s->below = h->out_below;
100 s->above = h->out_above;