]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Add implementation of distribution_sub to the distribution.c file
authorBarbara Kaczorowska <bkjg@google.com>
Tue, 18 Aug 2020 23:21:51 +0000 (23:21 +0000)
committerBarbara Kaczorowska <bkjg@google.com>
Wed, 19 Aug 2020 07:43:14 +0000 (07:43 +0000)
src/daemon/distribution.c
src/daemon/distribution.h

index 545c370a5c9ced43df334a60c6b6f059d646586c..a0731423ef5d07286079c011e011560d791858b9 100644 (file)
@@ -336,3 +336,47 @@ double distribution_squared_deviation_sum(distribution_t *dist) {
   pthread_mutex_unlock(&dist->mutex);
   return squared_deviation_sum;
 }
+
+/* TODO(bkjg): add tests for this function */
+int distribution_sub(distribution_t *d1, distribution_t *d2) {
+  if (d1 == NULL || d2 == NULL) {
+    return EINVAL;
+  }
+
+  if (d1->num_buckets != d2->num_buckets) {
+    return EINVAL;
+  }
+
+  pthread_mutex_lock(&d1->mutex);
+  pthread_mutex_lock(&d2->mutex);
+
+  if (d1->total_sum < d2->total_sum) {
+    for (size_t i = 0; i < tree_size(d1->num_buckets); ++i) {
+      if (d1->tree[i].maximum != d2->tree[i].maximum ||
+          d1->tree[i].bucket_counter > d2->tree[i].bucket_counter) {
+        pthread_mutex_unlock(&d2->mutex);
+        pthread_mutex_unlock(&d1->mutex);
+        return EINVAL;
+      }
+
+      d1->tree[i].bucket_counter =
+          d2->tree[i].bucket_counter - d1->tree[i].bucket_counter;
+    }
+  } else {
+    for (size_t i = 0; i < tree_size(d1->num_buckets); ++i) {
+      if (d1->tree[i].maximum != d2->tree[i].maximum ||
+          d1->tree[i].bucket_counter < d2->tree[i].bucket_counter) {
+        pthread_mutex_unlock(&d2->mutex);
+        pthread_mutex_unlock(&d1->mutex);
+        return EINVAL;
+      }
+
+      d1->tree[i].bucket_counter -= d2->tree[i].bucket_counter;
+    }
+  }
+
+  pthread_mutex_unlock(&d2->mutex);
+  pthread_mutex_unlock(&d1->mutex);
+
+  return 0;
+}
index 9a80b6f0a1d883e75f8865c37896e6c16d929e38..f57e66904bcd2d891129b1cdd333b793afc9b07f 100644 (file)
@@ -118,4 +118,6 @@ double distribution_squared_deviation_sum(distribution_t *dist);
 
 void destroy_buckets_array(buckets_array_t buckets_array);
 
+/* TODO(bkjg): add description */
+int distribution_sub(distribution_t *d1, distribution_t *d2);
 #endif // COLLECTD_DISTRIBUTION_H