histogram_data: check bucket_shift when merging

When merging data from one spdk_histogram_data to
another, the merging is only valid if the bucket_shift
for each structure is the same.  Otherwise we are
combining data points that cover different ranges
of values.

So check that the bucket_shifts are the same before
merging. Change the return type to int to
return -EINVAL if structures with different
bucket_shifts are attempted to be merged.

Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: If98e2d03384d85f478965956da2a42cfcff4713d
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15813
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
This commit is contained in:
Jim Harris 2022-12-07 10:10:55 +00:00 committed by Tomasz Zawadzki
parent 69bec87a91
commit 3327bb4391
2 changed files with 23 additions and 2 deletions

View File

@ -179,15 +179,25 @@ spdk_histogram_data_iterate(const struct spdk_histogram_data *histogram,
} }
} }
static inline void static inline int
spdk_histogram_data_merge(const struct spdk_histogram_data *dst, spdk_histogram_data_merge(const struct spdk_histogram_data *dst,
const struct spdk_histogram_data *src) const struct spdk_histogram_data *src)
{ {
uint64_t i; uint64_t i;
/* Histograms with different bucket_shift values cannot be simply
* merged, because the buckets represent different ranges of
* values.
*/
if (dst->bucket_shift != src->bucket_shift) {
return -EINVAL;
}
for (i = 0; i < SPDK_HISTOGRAM_NUM_BUCKETS(dst); i++) { for (i = 0; i < SPDK_HISTOGRAM_NUM_BUCKETS(dst); i++) {
dst->bucket[i] += src->bucket[i]; dst->bucket[i] += src->bucket[i];
} }
return 0;
} }
static inline struct spdk_histogram_data * static inline struct spdk_histogram_data *

View File

@ -82,6 +82,7 @@ histogram_merge(void)
struct spdk_histogram_data *h1, *h2; struct spdk_histogram_data *h1, *h2;
uint64_t *values = g_values; uint64_t *values = g_values;
uint32_t i; uint32_t i;
int rc;
h1 = spdk_histogram_data_alloc(); h1 = spdk_histogram_data_alloc();
h2 = spdk_histogram_data_alloc(); h2 = spdk_histogram_data_alloc();
@ -91,7 +92,8 @@ histogram_merge(void)
spdk_histogram_data_tally(h2, g_values[i]); spdk_histogram_data_tally(h2, g_values[i]);
} }
spdk_histogram_data_merge(h1, h2); rc = spdk_histogram_data_merge(h1, h2);
CU_ASSERT(rc == 0);
g_total = 0; g_total = 0;
g_number_of_merged_histograms = 2; g_number_of_merged_histograms = 2;
@ -99,6 +101,15 @@ histogram_merge(void)
spdk_histogram_data_free(h1); spdk_histogram_data_free(h1);
spdk_histogram_data_free(h2); spdk_histogram_data_free(h2);
h1 = spdk_histogram_data_alloc_sized(SPDK_HISTOGRAM_BUCKET_SHIFT_DEFAULT);
h2 = spdk_histogram_data_alloc_sized(SPDK_HISTOGRAM_BUCKET_SHIFT_DEFAULT - 1);
rc = spdk_histogram_data_merge(h1, h2);
CU_ASSERT(rc == -EINVAL);
spdk_histogram_data_free(h1);
spdk_histogram_data_free(h2);
} }
int int