]> rtime.felk.cvut.cz Git - can-benchmark.git/blobdiff - latester/histogram.h
Add first version of latency tester
[can-benchmark.git] / latester / histogram.h
diff --git a/latester/histogram.h b/latester/histogram.h
new file mode 100644 (file)
index 0000000..ad7591c
--- /dev/null
@@ -0,0 +1,54 @@
+#ifndef HISTOGRAM_H
+#define HISTOGRAM_H
+
+struct histogram {
+       unsigned *data;
+       unsigned allocated;
+       unsigned resolution;
+};
+
+int histogram_init(struct histogram *h,
+                  unsigned max_value,
+                  unsigned resolution)
+{
+       size_t mem;
+       h->allocated = max_value/resolution + 1;
+       h->resolution = resolution;
+       mem = h->allocated*sizeof(*h->data);
+       h->data = malloc(mem);
+       if (h->data) {
+               memset(h->data, 0, mem);
+               return 0;
+       } else
+               return -1;
+}
+
+void histogram_add(struct histogram *h, unsigned value)
+{
+       unsigned index = value / h->resolution;
+       if (index >= h->allocated)
+               index = h->allocated - 1;
+       h->data[index]++;
+}
+
+void histogram_fprint(struct histogram *h, FILE *f)
+{
+       unsigned long long sum = 0, cum;
+       unsigned i;
+       for (i = 0; i < h->allocated; i++)
+               sum += h->data[i];
+       cum = sum;
+       for (i = 0; i < h->allocated; i++) {
+               if (h->data[i] != 0) {
+                       if (!getenv("CANPING_MS"))
+                               fprintf(f, "%d %lld\n", i*h->resolution, cum);
+                       else
+                               fprintf(f, "%g %lld\n", 1e-3*(i*h->resolution), cum);
+               }
+               cum -= h->data[i];
+       }
+}
+
+
+
+#endif