]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stats: Prepare for more accurate moving averages
authorMarcin Deranek <marcin.deranek@booking.com>
Fri, 15 May 2020 16:26:18 +0000 (18:26 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 16 May 2020 20:40:00 +0000 (22:40 +0200)
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.

include/proto/freq_ctr.h

index ff13ea8f4aa164cc8a74951cebc921befa8786fb..63febb24a73d8f34746e432bda2ece753042b3c2 100644 (file)
@@ -263,6 +263,24 @@ static inline unsigned int swrate_add(unsigned int *sum, unsigned int n, unsigne
        return new_sum;
 }
 
+/* Adds sample value <v> to sliding window sum <sum> configured for <n> samples.
+ * The sample is returned. Better if <n> 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
+ * <n> 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 <v> spanning <s> samples to sliding window sum <sum>
  * configured for <n> samples, where <n> is supposed to be "much larger" than
  * <s>. The sample is returned. Better if <n> is a power of two. Note that this