2009-03-05 17:43:00 +00:00
|
|
|
/*
|
|
|
|
* Event rate calculation functions.
|
|
|
|
*
|
|
|
|
* Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common/config.h>
|
|
|
|
#include <common/standard.h>
|
|
|
|
#include <common/time.h>
|
2009-03-06 08:18:27 +00:00
|
|
|
#include <common/tools.h>
|
2009-03-05 17:43:00 +00:00
|
|
|
#include <proto/freq_ctr.h>
|
|
|
|
|
|
|
|
/* Read a frequency counter taking history into account for missing time in
|
|
|
|
* current period. Current second is sub-divided in 1000 chunks of one ms,
|
|
|
|
* and the missing ones are read proportionally from previous value. The
|
|
|
|
* return value has the same precision as one input data sample, so low rates
|
|
|
|
* will be inaccurate 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.
|
2009-03-06 08:18:27 +00:00
|
|
|
*
|
|
|
|
* For immediate limit checking, it's recommended to use freq_ctr_remain() and
|
|
|
|
* next_event_delay() instead which do not have the flapping correction, so
|
|
|
|
* that even frequencies as low as one event/period are properly handled.
|
2009-03-05 17:43:00 +00:00
|
|
|
*/
|
|
|
|
unsigned int read_freq_ctr(struct freq_ctr *ctr)
|
|
|
|
{
|
2009-03-06 13:29:25 +00:00
|
|
|
unsigned int curr, past;
|
|
|
|
unsigned int age;
|
2009-03-05 17:43:00 +00:00
|
|
|
|
2009-03-06 13:29:25 +00:00
|
|
|
age = now.tv_sec - ctr->curr_sec;
|
|
|
|
if (unlikely(age > 1))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
curr = 0;
|
|
|
|
past = ctr->curr_ctr;
|
|
|
|
if (likely(!age)) {
|
|
|
|
curr = past;
|
|
|
|
past = ctr->prev_ctr;
|
|
|
|
}
|
2009-03-05 17:43:00 +00:00
|
|
|
|
2009-03-06 13:29:25 +00:00
|
|
|
if (past <= 1 && !curr)
|
|
|
|
return past; /* very low rate, avoid flapping */
|
|
|
|
|
|
|
|
return curr + mul32hi(past, ~curr_sec_ms_scaled);
|
2009-03-05 17:43:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-06 08:18:27 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend)
|
|
|
|
{
|
2009-03-06 13:29:25 +00:00
|
|
|
unsigned int curr, past;
|
|
|
|
unsigned int age;
|
|
|
|
|
|
|
|
past = 0;
|
|
|
|
curr = 0;
|
|
|
|
age = now.tv_sec - ctr->curr_sec;
|
2009-03-06 08:18:27 +00:00
|
|
|
|
2009-03-06 13:29:25 +00:00
|
|
|
if (likely(age <= 1)) {
|
|
|
|
past = ctr->curr_ctr;
|
|
|
|
if (likely(!age)) {
|
|
|
|
curr = past;
|
|
|
|
past = ctr->prev_ctr;
|
|
|
|
}
|
|
|
|
curr += mul32hi(past, ~curr_sec_ms_scaled);
|
|
|
|
}
|
|
|
|
curr += pend;
|
2009-03-06 08:18:27 +00:00
|
|
|
|
2009-03-06 13:29:25 +00:00
|
|
|
if (curr >= freq)
|
2009-03-06 08:18:27 +00:00
|
|
|
return 0;
|
2009-03-06 13:29:25 +00:00
|
|
|
return freq - curr;
|
2009-03-06 08:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return the expected wait time in ms before the next event may occur,
|
|
|
|
* respecting frequency <freq>, 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.
|
|
|
|
*/
|
|
|
|
unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend)
|
|
|
|
{
|
2009-03-06 13:29:25 +00:00
|
|
|
unsigned int curr, past;
|
|
|
|
unsigned int wait, age;
|
2009-03-06 08:18:27 +00:00
|
|
|
|
2009-03-06 13:29:25 +00:00
|
|
|
past = 0;
|
|
|
|
curr = 0;
|
|
|
|
age = now.tv_sec - ctr->curr_sec;
|
2009-03-06 08:18:27 +00:00
|
|
|
|
2009-03-06 13:29:25 +00:00
|
|
|
if (likely(age <= 1)) {
|
|
|
|
past = ctr->curr_ctr;
|
|
|
|
if (likely(!age)) {
|
|
|
|
curr = past;
|
|
|
|
past = ctr->prev_ctr;
|
|
|
|
}
|
|
|
|
curr += mul32hi(past, ~curr_sec_ms_scaled);
|
|
|
|
}
|
|
|
|
curr += pend;
|
2009-03-06 08:18:27 +00:00
|
|
|
|
2009-03-06 13:29:25 +00:00
|
|
|
if (curr < freq)
|
2009-03-06 08:18:27 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-03-06 13:29:25 +00:00
|
|
|
wait = 999 / curr;
|
2009-03-06 08:18:27 +00:00
|
|
|
return MAX(wait, 1);
|
|
|
|
}
|
|
|
|
|
2009-03-05 17:43:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|