]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
add comparison for distributions
authorSvetlana Shmidt <sshmidt@google.com>
Mon, 7 Sep 2020 10:55:34 +0000 (10:55 +0000)
committerSvetlana Shmidt <sshmidt@google.com>
Mon, 7 Sep 2020 10:55:34 +0000 (10:55 +0000)
src/daemon/distribution.c
src/daemon/distribution.h

index 2bc8a22f5a1c7f70c4f9a5b497f5c7a11583c2a1..ceb3f4f7e1b5c46ddb06af7c12386824cd12b3a3 100644 (file)
@@ -340,18 +340,39 @@ double distribution_squared_deviation_sum(distribution_t *dist) {
   return squared_deviation_sum;
 }
 
-/* TODO(bkjg): add tests for this function */
-int distribution_sub(distribution_t *d1, distribution_t *d2) {
+static int compare_longint(uint64_t a, uint64_t b) {
+  return a == b ? 0 : a < b ? -1 : 1;
+}
+
+int distribution_cmp(distribution_t *d1, distribution_t *d2) {
   if (d1 == NULL || d2 == NULL) {
     return EINVAL;
   }
-
-  if (d1->num_buckets != d2->num_buckets) {
+  if (d1-> num_buckets != d2->num_buckets) {
     return EINVAL;
   }
+  for (size_t i = 0; i < tree_size(d1->num_buckets); i++) {
+    if (d1->tree[i].maximum != d2->tree[i].maximum) { // will it work with doubles?
+      return EINVAL;
+    }
+  }
 
   pthread_mutex_lock(&d1->mutex);
-  pthread_mutex_lock(&d2->mutex);
+  pthread_mutex_unlock(&d2->mutex);
+  int comparison = compare_longint(d1->tree[0].bucket_counter, d2->tree[0].bucket_counter);
+  for (size_t i = 1; i < tree_size(d1->num_buckets); i++) {
+    if (comparison != compare_longint(d1->tree[i].bucket_counter, d2->tree[i].bucket_counter)) {
+      comparison = ERANGE;
+      break;
+    }
+  }
+  pthread_mutex_unlock(&d2->mutex);
+  pthread_mutex_unlock(&d1->mutex);
+  return comparison;
+}
+
+/* TODO(bkjg): add tests for this function */
+int distribution_sub(distribution_t *d1, distribution_t *d2) {
 
   if (d1->total_sum < d2->total_sum) {
     d1->total_sum = d2->total_sum - d1->total_sum;
index 28d6d1454b01a216fe29a204ba35292f719a9781..37b6619bf1da5eb8f5202e1180338a6e3d51a633 100644 (file)
@@ -118,6 +118,14 @@ double distribution_squared_deviation_sum(distribution_t *dist);
 
 void destroy_buckets_array(buckets_array_t buckets_array);
 
+/** return EINVAL if bucket boundaries are not the same or NULL pointer received
+ *  return -1 if d1 < d2
+ *  return 0  if d1 == d2
+ *  return 1  if d1 > d2
+ *  return ERANGE if distributions can't be compared
+ */
+int distribution_cmp(distribution_t *d1, distribution_t *d2);
+
 /* TODO(bkjg): add description */
 int distribution_sub(distribution_t *d1, distribution_t *d2);
 #endif // COLLECTD_DISTRIBUTION_H
\ No newline at end of file