]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: freq_ctr: make read_freq_ctr_period() use freq_ctr_total()
authorWilly Tarreau <w@1wt.eu>
Sat, 10 Apr 2021 22:57:33 +0000 (00:57 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 11 Apr 2021 09:11:03 +0000 (11:11 +0200)
This one is the easiest to implement, it just requires a call and a
divide of the result. Anti-flapping correction for low-rates was
preserved.

Now calls using a constant period will be able to use a reciprocal
multiply for the period instead of a divide.

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

index e0ac70992170d760296029d583c0c74dd9336b49..60166147453d0c32efd2ffa8509daf1b2fc251a6 100644 (file)
@@ -122,6 +122,26 @@ static inline unsigned int update_freq_ctr_period(struct freq_ctr_period *ctr,
  */
 unsigned int read_freq_ctr(struct freq_ctr *ctr);
 
+/* Reads a frequency counter taking history into account for missing time in
+ * current period. The period has to be passed in number of ticks and must
+ * match the one used to feed the counter. The counter value is reported for
+ * current global date. The return value has the same precision as one input
+ * data sample, so low rates over the period will be inaccurate but still
+ * appropriate for max checking. One trick we use for low values is to specially
+ * handle the case where the rate is between 0 and 1 in order to avoid flapping
+ * while waiting for the next event.
+ *
+ * For immediate limit checking, it's recommended to use freq_ctr_period_remain()
+ * instead which does not have the flapping correction, so that even frequencies
+ * as low as one event/period are properly handled.
+ */
+static inline uint read_freq_ctr_period(struct freq_ctr_period *ctr, uint period)
+{
+       ullong total = freq_ctr_total(ctr, period, -1);
+
+       return div64_32(total, period);
+}
+
 /* returns the number of remaining events that can occur on this freq counter
  * while respecting <freq> and taking into account that <pend> events are
  * already known to be pending. Returns 0 if limit was reached.
index 76a8ec1ceda78dd287709f7c517cadde3858bdd0..a2ca040acd34c862ffc8e5a80772fefe476deb1a 100644 (file)
@@ -166,64 +166,6 @@ unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned
        return MAX(wait, 1);
 }
 
-/* Reads a frequency counter taking history into account for missing time in
- * current period. The period has to be passed in number of ticks and must
- * match the one used to feed the counter. The counter value is reported for
- * current global date. The return value has the same precision as one input
- * data sample, so low rates over the period will be inaccurate but still
- * appropriate for max checking. One trick we use for low values is to specially
- * handle the case where the rate is between 0 and 1 in order to avoid flapping
- * while waiting for the next event.
- *
- * For immediate limit checking, it's recommended to use freq_ctr_period_remain()
- * instead which does not have the flapping correction, so that even frequencies
- * as low as one event/period are properly handled.
- *
- * For measures over a 1-second period, it's better to use the implicit functions
- * above.
- */
-unsigned int read_freq_ctr_period(struct freq_ctr_period *ctr, unsigned int period)
-{
-       unsigned int _curr, _past, curr, past;
-       unsigned int remain, _curr_tick, curr_tick;
-
-       while (1) {
-               _curr = ctr->curr_ctr;
-               __ha_compiler_barrier();
-               _past = ctr->prev_ctr;
-               __ha_compiler_barrier();
-               _curr_tick = ctr->curr_tick;
-               __ha_compiler_barrier();
-               if (_curr_tick & 0x1)
-                       continue;
-               curr = ctr->curr_ctr;
-               __ha_compiler_barrier();
-               past = ctr->prev_ctr;
-               __ha_compiler_barrier();
-               curr_tick = ctr->curr_tick;
-               __ha_compiler_barrier();
-               if (_curr == curr && _past == past && _curr_tick == curr_tick)
-                       break;
-       };
-
-       remain = curr_tick + period - global_now_ms;
-       if (unlikely((int)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;
-               if ((int)remain < 0)
-                       return 0;
-               past = curr;
-               curr = 0;
-       }
-       if (past <= 1 && !curr)
-               return past; /* very low rate, avoid flapping */
-
-       curr += div64_32((unsigned long long)past * remain, period);
-       return curr;
-}
-
 /* Returns the number of remaining events that can occur on this freq counter
  * while respecting <freq> events per period, and taking into account that
  * <pend> events are already known to be pending. Returns 0 if limit was reached.