]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
change distribution_sub function in order d1 >= d2
authorSvetlana Shmidt <sshmidt@google.com>
Mon, 7 Sep 2020 11:01:41 +0000 (11:01 +0000)
committerSvetlana Shmidt <sshmidt@google.com>
Mon, 7 Sep 2020 11:01:41 +0000 (11:01 +0000)
src/daemon/distribution.c

index ceb3f4f7e1b5c46ddb06af7c12386824cd12b3a3..a6f04f12a14a181c7fbef8433fbfa03fb1fa4449 100644 (file)
@@ -374,31 +374,20 @@ int distribution_cmp(distribution_t *d1, distribution_t *d2) {
 /* 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;
-    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 {
-    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;
-    }
+  int cmp_status = distribution_cmp(d1, d2);
+  if (cmp_status != 1 && cmp_status != 0) { // i.e. d1 < d2 or can't compare
+    if (cmp_status == -1)
+      cmp_status = ERANGE;
+    return cmp_status;
+  }
+
+  pthread_mutex_lock(&d1->mutex);
+  pthread_mutex_lock(&d2->mutex);
+
+  d1->total_sum -= d2->total_sum;
+  d1->total_square_sum -= d2->total_square_sum;
+  for (size_t i = 0; i < tree_size(d1->num_buckets); i++) {
+    d1->tree[i].bucket_counter -= d2.tree[i].bucket_counter;
   }
 
   pthread_mutex_unlock(&d2->mutex);