diff --git a/include/proto/freq_ctr.h b/include/proto/freq_ctr.h index ff13ea8f4..63febb24a 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