From: Willy Tarreau Date: Sat, 10 Apr 2021 21:36:07 +0000 (+0200) Subject: MINOR: freq_ctr: add the missing next_event_delay_period() X-Git-Tag: v2.4-dev17~146 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d209c87142c96b78ad478339408294d37c326422;p=thirdparty%2Fhaproxy.git MINOR: freq_ctr: add the missing next_event_delay_period() There was still no function to compute a wait time for periods, let's implement it on top of freq_ctr_total() as we'll soon need it for the per-second one. The divide here is applied on the frequency so that it will be replaced with a reciprocal multiply when constant. --- diff --git a/include/haproxy/freq_ctr.h b/include/haproxy/freq_ctr.h index 3060f2a12d..c0a954f6d3 100644 --- a/include/haproxy/freq_ctr.h +++ b/include/haproxy/freq_ctr.h @@ -170,6 +170,31 @@ static inline uint freq_ctr_remain_period(struct freq_ctr_period *ctr, uint peri */ unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend); +/* return the expected wait time in ms before the next event may occur, + * respecting frequency , and assuming there may already be some pending + * events. It returns zero if we can proceed immediately, otherwise the wait + * time, which will be rounded down 1ms for better accuracy, with a minimum + * of one ms. + */ +static inline uint next_event_delay_period(struct freq_ctr_period *ctr, uint period, uint freq, uint pend) +{ + ullong total = freq_ctr_total(ctr, period, pend); + ullong limit = (ullong)freq * period; + uint wait; + + if (total < limit) + return 0; + + /* too many events already, let's count how long to wait before they're + * processed. For this we'll subtract from the number of pending events + * the ones programmed for the current period, to know how long to wait + * for the next period. Each event takes period/freq ticks. + */ + total -= limit; + wait = div64_32(total, (freq ? freq : 1)); + return MAX(wait, 1); +} + /* process freq counters over configurable periods */ unsigned int read_freq_ctr_period(struct freq_ctr_period *ctr, unsigned int period);