From aa33f20e272c05d5110bcb5ab1b49221724472af Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sat, 8 May 2021 08:12:37 +0200 Subject: [PATCH] MINOR: freq_ctr: add new functions to report float measurements For stats reporting it can be convenient to report floats at low rates instead of discrete integers. We do have quite some precision since we currently divide counters by number of milliseconds, so we can usually add 3 digits after the decimal point. --- include/haproxy/freq_ctr.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/haproxy/freq_ctr.h b/include/haproxy/freq_ctr.h index 93c3eaeba..a59a3f9b0 100644 --- a/include/haproxy/freq_ctr.h +++ b/include/haproxy/freq_ctr.h @@ -104,6 +104,16 @@ static inline uint read_freq_ctr_period(struct freq_ctr *ctr, uint period) return div64_32(total, period); } +/* same as read_freq_ctr_period() above except that floats are used for the + * output so that low rates can be more precise. + */ +static inline double read_freq_ctr_period_flt(struct freq_ctr *ctr, uint period) +{ + ullong total = freq_ctr_total(ctr, period, -1); + + return (double)total / (double)period; +} + /* Read a 1-sec frequency counter taking history into account for missing time * in current period. */ @@ -112,6 +122,14 @@ static inline unsigned int read_freq_ctr(struct freq_ctr *ctr) return read_freq_ctr_period(ctr, MS_TO_TICKS(1000)); } +/* same as read_freq_ctr() above except that floats are used for the + * output so that low rates can be more precise. + */ +static inline double read_freq_ctr_flt(struct freq_ctr *ctr) +{ + return read_freq_ctr_period_flt(ctr, MS_TO_TICKS(1000)); +} + /* Returns the number of remaining events that can occur on this freq counter * while respecting events per period, and taking into account that * events are already known to be pending. Returns 0 if limit was reached.