From: Svetlana Shmidt Date: Mon, 7 Sep 2020 10:55:34 +0000 (+0000) Subject: add comparison for distributions X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1cf431dda662444c313d9986c10fd2ddd233d418;p=thirdparty%2Fcollectd.git add comparison for distributions --- diff --git a/src/daemon/distribution.c b/src/daemon/distribution.c index 2bc8a22f5..ceb3f4f7e 100644 --- a/src/daemon/distribution.c +++ b/src/daemon/distribution.c @@ -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; diff --git a/src/daemon/distribution.h b/src/daemon/distribution.h index 28d6d1454..37b6619bf 100644 --- a/src/daemon/distribution.h +++ b/src/daemon/distribution.h @@ -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