]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: freq_ctr: add a function to add values with a peak
authorWilly Tarreau <w@1wt.eu>
Thu, 19 Mar 2026 14:08:29 +0000 (15:08 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 19 Mar 2026 15:24:31 +0000 (16:24 +0100)
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

index dfb1874831c9c0437268303a151ca02527e6d6d5..228f37d7a8fae1463043938fde030771428eb931 100644 (file)
@@ -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 <v> 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 <sum> over a sliding window of
  * <n> samples. Better if <n> is a power of two. It must be the same <n> as the
  * one used above in all additions.