]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: freq_ctr: add a generic function to report the total value
authorWilly Tarreau <w@1wt.eu>
Sat, 10 Apr 2021 22:38:06 +0000 (00:38 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 11 Apr 2021 09:10:57 +0000 (11:10 +0200)
Most of the functions designed to read a counter over a period go through
the same complex loop and only differ in the way they use the returned
values, so it was worth implementing all this into freq_ctr_total() which
returns the total number of events over a period so that the caller can
finish its operation using a divide or a remaining time calculation. As
a special case, read_freq_ctr_period() doesn't take pending events but
requires to enable an anti-flapping correction at very low frequencies.
Thus the function implements it when pend<0.

Thanks to this function it will be possible to reimplement the other ones
as inline and merge the per-second ones with the arbitrary period ones
without always adding the cost of a 64 bit divide.

include/haproxy/freq_ctr.h
src/freq_ctr.c

index 87926253969b9b2f6c7d04ee48e9fd291041b3b5..e0ac70992170d760296029d583c0c74dd9336b49 100644 (file)
@@ -27,6 +27,8 @@
 #include <haproxy/intops.h>
 #include <haproxy/time.h>
 
+/* exported functions from freq_ctr.c */
+ullong freq_ctr_total(struct freq_ctr_period *ctr, uint period, int pend);
 
 /* Update a frequency counter by <inc> incremental units. It is automatically
  * rotated if the period is over. It is important that it correctly initializes
index 42fbc1cea60cb27458121f3394a34d58a291a347..76a8ec1ceda78dd287709f7c517cadde3858bdd0 100644 (file)
@@ -274,6 +274,67 @@ unsigned int freq_ctr_remain_period(struct freq_ctr_period *ctr, unsigned int pe
        return freq;
 }
 
+/* Returns the total number of events over the current + last period, including
+ * a number of already pending events <pend>. The average frequency will be
+ * obtained by dividing the output by <period>. This is essentially made to
+ * ease implementation of higher-level read functions.
+ *
+ * As a special case, if pend < 0, it's assumed there are no pending
+ * events and a flapping correction must be applied at the end. This is used by
+ * read_freq_ctr_period() to avoid reporting ups and downs on low-frequency
+ * events when the past value is <= 1.
+ */
+ullong freq_ctr_total(struct freq_ctr_period *ctr, uint period, int pend)
+{
+       ullong curr, past;
+       uint curr_tick;
+       int remain;
+
+       for (;; __ha_cpu_relax()) {
+               curr = ctr->curr_ctr;
+               past = ctr->prev_ctr;
+               curr_tick = ctr->curr_tick;
+
+               /* now let's make sure the second loads retrieve the most
+                * up-to-date values. If no value changed after a load barrier,
+                * we're certain the values we got were stable.
+                */
+               __ha_barrier_load();
+
+               if (curr_tick & 0x1)
+                       continue;
+
+               if (curr != ctr->curr_ctr)
+                       continue;
+
+               if (past != ctr->prev_ctr)
+                       continue;
+
+               if (curr_tick != ctr->curr_tick)
+                       continue;
+               break;
+       };
+
+       remain = curr_tick + period - global_now_ms;
+       if (unlikely(remain < 0)) {
+               /* We're past the first period, check if we can still report a
+                * part of last period or if we're too far away.
+                */
+               remain += period;
+               past = (remain >= 0) ? curr : 0;
+               curr = 0;
+       }
+
+       if (pend < 0) {
+               /* enable flapping correction at very low rates */
+               pend = 0;
+               if (!curr && past <= 1)
+                       return past * period;
+       }
+
+       /* compute the total number of confirmed events over the period */
+       return past * remain + (curr + pend) * period;
+}
 
 /*
  * Local variables: