From eec60f14ddbfd77d864d03c8db3f052c74a1f2f9 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 19 Mar 2026 15:08:29 +0100 Subject: [PATCH] MINOR: freq_ctr: add a function to add values with a peak Sometimes it's desirable to observe fading away peak values, where a new value that is higher than the historical one instantly replaces it, otherwise contributes to it. It is convenient when trying to observe certain phenomenons like peak queue sizes. The new function swrate_add_peak_local() does that to a private variable (no atomic ops involved as it's not worth the cost since such use cases are typically local). --- include/haproxy/freq_ctr.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/haproxy/freq_ctr.h b/include/haproxy/freq_ctr.h index dfb187483..228f37d7a 100644 --- a/include/haproxy/freq_ctr.h +++ b/include/haproxy/freq_ctr.h @@ -403,6 +403,25 @@ static inline uint swrate_add_scaled_opportunistic(uint *sum, uint n, uint v, ui return new_sum; } +/* Like swrate_add() except that if is beyond the current average, the + * average is replaced by the peak. This is essentially used to measure peak + * loads in the scheduler, reason why it is provided as a local variant that + * does not involve atomic operations. + */ +static inline uint swrate_add_peak_local(uint *sum, uint n, uint v) +{ + uint old_sum, new_sum; + + old_sum = *sum; + if (v * n > old_sum) + new_sum = v * n; + else + new_sum = old_sum - (old_sum + n - 1) / n + v; + + *sum = new_sum; + return new_sum; +} + /* Returns the average sample value for the sum over a sliding window of * samples. Better if is a power of two. It must be the same as the * one used above in all additions.