From: Marcin Deranek Date: Fri, 15 May 2020 16:26:18 +0000 (+0200) Subject: MINOR: stats: Prepare for more accurate moving averages X-Git-Tag: v2.2-dev8~51 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4dc2b57d516ce30cfc5272f911cd4b3e32725580;p=thirdparty%2Fhaproxy.git MINOR: stats: Prepare for more accurate moving averages Add swrate_add_dynamic function which is similar to swrate_add, but more accurate when calculating moving averages when not enough samples have been processed yet. --- diff --git a/include/proto/freq_ctr.h b/include/proto/freq_ctr.h index ff13ea8f4a..63febb24a7 100644 --- a/include/proto/freq_ctr.h +++ b/include/proto/freq_ctr.h @@ -263,6 +263,24 @@ static inline unsigned int swrate_add(unsigned int *sum, unsigned int n, unsigne return new_sum; } +/* Adds sample value to sliding window sum configured for samples. + * The sample is returned. Better if is a power of two. This function is + * thread-safe. + * This function should give better accuracy than swrate_add when number of + * samples collected is lower than nominal window size. In such circumstances + * should be set to 0. + */ +static inline unsigned int swrate_add_dynamic(unsigned int *sum, unsigned int n, unsigned int v) +{ + unsigned int new_sum, old_sum; + + old_sum = *sum; + do { + new_sum = old_sum - (n ? (old_sum + n - 1) / n : 0) + v; + } while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum)); + return new_sum; +} + /* Adds sample value spanning samples to sliding window sum * configured for samples, where is supposed to be "much larger" than * . The sample is returned. Better if is a power of two. Note that this