Skip to content

Commit 4aae034

Browse files
deps: update histogram to 0.12.1
1 parent cb05e4d commit 4aae034

3 files changed

Lines changed: 67 additions & 20 deletions

File tree

‎deps/histogram/include/hdr/hdr_histogram.h‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ int64_t hdr_add_while_correcting_for_coordinated_omission(
265265
struct hdr_histogram* h, struct hdr_histogram* from, int64_t expected_interval);
266266

267267
/**
268-
* Get minimum value from the histogram. Will return 2^63-1 if the histogram
269-
* is empty.
268+
* Get minimum value from the histogram. Will return 0 if the histogram is
269+
* empty, like hdr_max; a recorded 0 also gives 0, so use hdr_total_count to
270+
* tell the two apart.
270271
*
271272
* @param h "This" pointer
272273
*/
@@ -292,35 +293,41 @@ int64_t hdr_total_count(const struct hdr_histogram* h);
292293
* Get the value at a specific percentile.
293294
*
294295
* @param h "This" pointer.
295-
* @param percentile The percentile to get the value for
296+
* @param percentile The percentile to get the value for. Above 100 gives the value at
297+
* 100; below 0 and -inf give the first recorded value. Exactly 0 returns the lowest
298+
* equivalent value of that bucket.
299+
* @return the value at the percentile, or 0 if no values have been recorded.
296300
*/
297301
int64_t hdr_value_at_percentile(const struct hdr_histogram* h, double percentile);
298302

299303
/**
300304
* Get the values at the given percentiles.
301305
*
302306
* @param h "This" pointer.
303-
* @param percentiles The ordered percentiles array to get the values for.
307+
* @param percentiles The ordered percentiles array to get the values for, with the
308+
* same out-of-range handling as hdr_value_at_percentile.
304309
* @param length Number of elements in the arrays.
305310
* @param values Destination array containing the values at the given percentiles.
306311
* The values array should be allocated by the caller.
307-
* @return 0 on success, ENOMEM if the provided destination array is null.
312+
* Percentiles that cannot be resolved get the same value hdr_value_at_percentile
313+
* returns for them, so all are 0 on an empty histogram.
314+
* @return 0 on success, EINVAL if percentiles or values is null.
308315
*/
309316
int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percentiles, int64_t *values, size_t length);
310317

311318
/**
312319
* Gets the standard deviation for the values in the histogram.
313320
*
314321
* @param h "This" pointer
315-
* @return The standard deviation
322+
* @return The standard deviation, or 0.0 if the histogram is empty.
316323
*/
317324
double hdr_stddev(const struct hdr_histogram* h);
318325

319326
/**
320327
* Gets the mean for the values in the histogram.
321328
*
322329
* @param h "This" pointer
323-
* @return The mean
330+
* @return The mean, or 0.0 if the histogram is empty.
324331
*/
325332
double hdr_mean(const struct hdr_histogram* h);
326333

‎deps/histogram/include/hdr/hdr_histogram_version.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
#ifndef HDR_HISTOGRAM_VERSION_H
88
#define HDR_HISTOGRAM_VERSION_H
99

10-
#define HDR_HISTOGRAM_VERSION "0.12.0"
10+
#define HDR_HISTOGRAM_VERSION "0.12.1"
1111

1212
#endif // HDR_HISTOGRAM_VERSION_H

‎deps/histogram/src/hdr_histogram.c‎

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ int64_t hdr_total_count(const struct hdr_histogram* h)
780780

781781
int64_t hdr_min(const struct hdr_histogram* h)
782782
{
783-
if (0 < hdr_count_at_index(h, 0))
783+
if (0 == hdr_total_count(h) || 0 < hdr_count_at_index(h, 0))
784784
{
785785
return 0;
786786
}
@@ -914,36 +914,60 @@ static int64_t get_value_from_idx_up_to_count(const struct hdr_histogram* h, int
914914
}
915915

916916

917+
/* Target cumulative count in [1, total_count]; NaN/+inf -> total_count, -inf/negative -> 1 */
918+
static int64_t count_at_percentile(int64_t total_count, double percentile)
919+
{
920+
const double requested = percentile < 100.0 ? percentile : 100.0;
921+
const double cc = ((requested / 100) * (double) total_count) + 0.5;
922+
if (!(cc >= 1.0)) return 1;
923+
if (cc >= (double) total_count) return total_count;
924+
return (int64_t) cc;
925+
}
926+
917927
int64_t hdr_value_at_percentile(const struct hdr_histogram* h, double percentile)
918928
{
919-
double requested_percentile = percentile < 100.0 ? percentile : 100.0;
920-
int64_t count_at_percentile =
921-
(int64_t) (((requested_percentile / 100) * h->total_count) + 0.5);
922-
int64_t value_from_idx = get_value_from_idx_up_to_count(h, count_at_percentile);
929+
/* empty: the scan's not-found 0 must not be rounded up to highest_equivalent_value(0) */
930+
if (HDR_UNLIKELY(h->total_count == 0))
931+
{
932+
return 0;
933+
}
934+
int64_t value_from_idx =
935+
get_value_from_idx_up_to_count(h, count_at_percentile(h->total_count, percentile));
923936
if (percentile == 0.0)
924937
{
925938
return lowest_equivalent_value(h, value_from_idx);
926939
}
927940
return highest_equivalent_value(h, value_from_idx);
928941
}
929942

943+
/* p0 is the bucket's lowest value, matching hdr_value_at_percentile */
944+
static int64_t percentile_bound(const struct hdr_histogram* h, double percentile, int64_t value)
945+
{
946+
return percentile == 0.0 ? lowest_equivalent_value(h, value) : highest_equivalent_value(h, value);
947+
}
948+
930949
int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percentiles, int64_t *values, size_t length)
931950
{
932951
if (NULL == percentiles || NULL == values)
933952
{
934953
return EINVAL;
935954
}
955+
if (HDR_UNLIKELY(h->total_count == 0))
956+
{
957+
for (size_t i = 0; i < length; i++)
958+
{
959+
values[i] = 0;
960+
}
961+
return 0;
962+
}
936963

937964
struct hdr_iter iter;
938965
const int64_t total_count = h->total_count;
939966
// to avoid allocations we use the values array for intermediate computation
940967
// i.e. to store the expected cumulative count at each percentile
941968
for (size_t i = 0; i < length; i++)
942969
{
943-
const double requested_percentile = percentiles[i] < 100.0 ? percentiles[i] : 100.0;
944-
const int64_t count_at_percentile =
945-
(int64_t) (((requested_percentile / 100) * total_count) + 0.5);
946-
values[i] = count_at_percentile > 1 ? count_at_percentile : 1;
970+
values[i] = count_at_percentile(total_count, percentiles[i]);
947971
}
948972

949973
uint64_t total = 0; /* unsigned: no signed-overflow UB when a hostile block sum is added at once */
@@ -974,7 +998,7 @@ int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percen
974998
total += (uint64_t)counts[j];
975999
while (at_pos < length && (int64_t)total >= values[at_pos])
9761000
{
977-
values[at_pos] = highest_equivalent_value(h, hdr_value_at_index(h, j));
1001+
values[at_pos] = percentile_bound(h, percentiles[at_pos], hdr_value_at_index(h, j));
9781002
at_pos++;
9791003
}
9801004
}
@@ -990,7 +1014,7 @@ int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percen
9901014
total += (uint64_t)counts[idx];
9911015
while (at_pos < length && (int64_t)total >= values[at_pos])
9921016
{
993-
values[at_pos] = highest_equivalent_value(h, hdr_value_at_index(h, idx));
1017+
values[at_pos] = percentile_bound(h, percentiles[at_pos], hdr_value_at_index(h, idx));
9941018
at_pos++;
9951019
}
9961020
}
@@ -1005,11 +1029,17 @@ int hdr_value_at_percentiles(const struct hdr_histogram *h, const double *percen
10051029
total += (uint64_t)iter.count;
10061030
while (at_pos < length && (int64_t)total >= values[at_pos])
10071031
{
1008-
values[at_pos] = highest_equivalent_value(h, iter.value);
1032+
values[at_pos] = percentile_bound(h, percentiles[at_pos], iter.value);
10091033
at_pos++;
10101034
}
10111035
}
10121036
}
1037+
/* unreached targets (counts below total_count) get the singular's not-found value */
1038+
for (; at_pos < length; at_pos++)
1039+
{
1040+
values[at_pos] = percentiles[at_pos] == 0.0 ?
1041+
lowest_equivalent_value(h, 0) : highest_equivalent_value(h, 0);
1042+
}
10131043
return 0;
10141044
}
10151045

@@ -1020,6 +1050,11 @@ double hdr_mean(const struct hdr_histogram* h)
10201050
int64_t count = 0;
10211051
int64_t total_count = h->total_count;
10221052

1053+
if (total_count == 0)
1054+
{
1055+
return 0.0;
1056+
}
1057+
10231058
hdr_iter_init(&iter, h);
10241059

10251060
while (hdr_iter_next(&iter) && count < total_count)
@@ -1037,6 +1072,11 @@ double hdr_mean(const struct hdr_histogram* h)
10371072

10381073
double hdr_stddev(const struct hdr_histogram* h)
10391074
{
1075+
if (h->total_count == 0)
1076+
{
1077+
return 0.0;
1078+
}
1079+
10401080
double mean = hdr_mean(h);
10411081
double geometric_dev_total = 0.0;
10421082

0 commit comments

Comments
 (0)