diff --git a/src/freq_ctr.c b/src/freq_ctr.c index 602ad9be6..9b001e326 100644 --- a/src/freq_ctr.c +++ b/src/freq_ctr.c @@ -147,8 +147,9 @@ ullong freq_ctr_total(const struct freq_ctr *ctr, uint period, int pend) } /* Returns the excess of events (may be negative) over the current period for - * target frequency . It returns 0 if the counter is in the future. The - * result considers the position of the current time within the current period. + * target frequency . It returns 0 if the counter is in the future or if + * the counter is empty. The result considers the position of the current time + * within the current period. * * The caller may safely add new events if result is negative or null. */ @@ -195,6 +196,11 @@ int freq_ctr_overshoot_period(const struct freq_ctr *ctr, uint period, uint freq __ha_cpu_relax(); }; + if (!curr && !tick) { + /* The counter is empty, there is no overshoot */ + return 0; + } + elapsed = HA_ATOMIC_LOAD(&global_now_ms) - tick; if (unlikely(elapsed < 0)) { /* The counter is in the future, there is no overshoot */