]> rtime.felk.cvut.cz Git - can-benchmark.git/blobdiff - latester/histogram.h
latester: Fix GCC 4.9 warnings
[can-benchmark.git] / latester / histogram.h
index 4a9aade0bc67c793cc89d22491eca22d8a90844b..b4546e4a76ef72003105f050efc4d8f0dadd14dd 100644 (file)
@@ -23,8 +23,9 @@ int histogram_init(struct histogram *h,
                return -1;
 }
 
-void histogram_add(struct histogram *h, unsigned value)
+void histogram_add(struct histogram *h, int value)
 {
+       if (value < 0) value = 0;
        unsigned index = value / h->resolution;
        if (index >= h->allocated)
                index = h->allocated - 1;
@@ -35,6 +36,10 @@ void histogram_fprint(struct histogram *h, FILE *f)
 {
        unsigned long long sum = 0, cum;
        unsigned i;
+
+       if (!f)
+               return;
+
        for (i = 0; i < h->allocated; i++)
                sum += h->data[i];
        cum = sum;
@@ -47,5 +52,41 @@ void histogram_fprint(struct histogram *h, FILE *f)
 }
 
 
+struct histogram_stats {
+       unsigned count;
+       unsigned long long sum;
+       unsigned avg;
+       unsigned percentile[101];
+};
+
+void histogram_stats(struct histogram *h, struct histogram_stats *s)
+{
+       unsigned long long sum;
+       unsigned i, j;
+
+       if (!s)
+               return;
+
+       memset(s, 0, sizeof(*s));
+
+       for (i = 0; i < h->allocated; i++) {
+               s->count += h->data[i];
+               s->sum += h->data[i] * i * h->resolution;
+       }
+       if (s->count == 0)
+               return;
+
+       s->avg = s->sum / s->count;
+
+       for (i = 0, j = 0, sum = 0; i < 100; i++) {
+               while (sum <= i * s->count / 100)
+                       sum += h->data[j++];
+
+               s->percentile[i] = (j-1) * h->resolution;
+       }
+       while (sum < i * s->count / 100)
+               sum += h->data[j++];
+       s->percentile[100] = (j-1) * h->resolution;
+}
 
 #endif