10 int histogram_init(struct histogram *h,
15 h->allocated = max_value/resolution + 1;
16 h->resolution = resolution;
17 mem = h->allocated*sizeof(*h->data);
18 h->data = malloc(mem);
20 memset(h->data, 0, mem);
26 void histogram_add(struct histogram *h, int value)
28 if (value < 0) value = 0;
29 unsigned index = value / h->resolution;
30 if (index >= h->allocated)
31 index = h->allocated - 1;
35 void histogram_fprint(struct histogram *h, FILE *f)
37 unsigned long long sum = 0, cum;
43 for (i = 0; i < h->allocated; i++)
46 for (i = 0; i < h->allocated; i++) {
47 if (h->data[i] != 0) {
48 fprintf(f, "%g %lld\n", 1e-3*(i*h->resolution), cum);
55 struct histogram_stats {
57 unsigned long long sum;
59 unsigned percentile[101];
62 void histogram_stats(struct histogram *h, struct histogram_stats *s)
64 unsigned long long sum;
70 memset(s, 0, sizeof(*s));
72 for (i = 0; i < h->allocated; i++) {
73 s->count += h->data[i];
74 s->sum += h->data[i] * i * h->resolution;
79 s->avg = s->sum / s->count;
81 for (i = 0, j = 0, sum = 0; i < 100; i++) {
82 while (sum <= i * s->count / 100)
85 s->percentile[i] = (j-1) * h->resolution;
87 while (sum < i * s->count / 100)
89 s->percentile[100] = (j-1) * h->resolution;