]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
add linear constructor
authorSvetlana Shmidt <sshmidt@google.com>
Fri, 24 Jul 2020 10:04:18 +0000 (15:04 +0500)
committerFlorian Forster <octo@google.com>
Mon, 27 Jul 2020 07:52:25 +0000 (09:52 +0200)
src/daemon/distribution.c [new file with mode: 0644]
src/daemon/distribution.h

diff --git a/src/daemon/distribution.c b/src/daemon/distribution.c
new file mode 100644 (file)
index 0000000..fbd123a
--- /dev/null
@@ -0,0 +1,66 @@
+//
+// Created by Svetlana Shmidt on 24.07.2020.
+//
+
+#include "distribution.h"
+
+typedef struct bucket_s {
+  uint64_t bucket_counter;
+  double minimum, maximum;
+} bucket_t;
+
+struct distribution_s {
+  bucket_t *tree;
+  size_t num_buckets;
+};
+
+size_t left_child_index(size_t node_index, size_t left, size_t right) {
+  return node_index + 1;
+}
+
+size_t right_child_index(size_t node_index, size_t left, size_t right) {
+  size_t mid = (left + right) / 2;
+  return node_index + 2 * (mid - left + 1);
+}
+
+bucket_t merge_buckets(bucket_t left_child, bucket_t right_child) {
+  return (bucket_t) {
+      .bucket_counter = left_child.bucket_counter + right_child.bucket_counter,
+      .minimum = left_child.minimum,
+      .maximum = right_child.maximum,
+  };
+}
+
+void build_tree(distribution_t *d, bucket_t *buckets, size_t node_index, size_t left, size_t right) {
+  if (left > right)
+    return;
+  if (left == right) {
+    d->tree[node_index] = buckets[left];
+    return;
+  }
+  size_t mid = (left + right) / 2;
+  size_t left_child = left_child_index(node_index, left, right);
+  size_t right_child = right_child_index(node_index, left, right);
+  build_tree(d, buckets, left_child, left, mid);
+  build_tree(d, buckets, right_child, mid + 1, right);
+  d->tree[node_index] = merge_buckets(d->tree[left_child], d->tree[right_child]);
+}
+
+distribution_t* distribution_new_linear(size_t num_buckets, double size) {
+  bucket_t *bucket_array = calloc(num_buckets, sizeof(bucket_t));
+  for (size_t i = 0; i < num_buckets; i++) {
+    bucket_array[i].minimum = i * size;
+    bucket_array[i].maximum = (i + 1) * size;
+  }
+
+  distribution_t *new_distribution = calloc(1, sizeof(distribution_t));
+  new_distribution->tree = calloc(2 * num_buckets - 1, sizeof(bucket_t));
+  new_distribution->num_buckets = num_buckets;
+  build_tree(new_distribution, bucket_array, 0, 0, num_buckets - 1);
+  free(bucket_array);
+  return new_distribution;
+}
+
+int main() {
+  distribution_t *p = distribution_new_linear(5, 2);
+}
\ No newline at end of file
index c1c6dc75c5d99eadc5db8b1920a64d21b57f66b9..fd1872e3035fbf33ae2cc2720ee08e2e5a1dd2bc 100644 (file)
@@ -5,4 +5,20 @@
 #ifndef COLLECTD_DISTRIBUTION_H
 #define COLLECTD_DISTRIBUTION_H
 
+#include <stdlib.h>
+
+struct distribution_s;
+typedef struct distribution_s distribution_t;
+
+//constructor functions:
+distribution_t* distribution_new_linear(size_t num_buckets, double size);
+distribution_t* distribution_new_exponential(size_t num_buckets, double initial_size, double factor);
+distribution_t* distribution_new_custom(size_t num_buckets, double  *custom_buckets_sizes);
+
+void distribution_update(distribution_t *dist, double gauge);
+double distribution_percentile(distribution_t *dist, double percent);
+double distribution_average(distribution_t *dist);
+distribution_t distribution_clone(distribution_t *dist);
+void distribution_destroy(distribution_t *d);
+
 #endif // COLLECTD_DISTRIBUTION_H