mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-09-21 13:51:26 +02:00
[OPTIM] freq_ctr: do not rotate the counters when reading
It's easier to take the counter's age into account when consulting it than to rotate it first. It also saves some CPU cycles and avoids the multiply for outdated counters, finally saving CPU cycles here too when multiple operations need to read the same counter. The freq_ctr code has also shrinked by one third consecutively to these optimizations.
This commit is contained in:
parent
ec22b2c27a
commit
3d8c5531d8
@ -30,15 +30,24 @@
|
|||||||
*/
|
*/
|
||||||
unsigned int read_freq_ctr(struct freq_ctr *ctr)
|
unsigned int read_freq_ctr(struct freq_ctr *ctr)
|
||||||
{
|
{
|
||||||
unsigned int cur;
|
unsigned int curr, past;
|
||||||
if (unlikely(ctr->curr_sec != now.tv_sec))
|
unsigned int age;
|
||||||
rotate_freq_ctr(ctr);
|
|
||||||
|
|
||||||
cur = ctr->curr_ctr;
|
age = now.tv_sec - ctr->curr_sec;
|
||||||
if (ctr->prev_ctr <= 1 && !ctr->curr_ctr)
|
if (unlikely(age > 1))
|
||||||
return ctr->prev_ctr; /* very low rate, avoid flapping */
|
return 0;
|
||||||
|
|
||||||
return cur + mul32hi(ctr->prev_ctr, ~curr_sec_ms_scaled);
|
curr = 0;
|
||||||
|
past = ctr->curr_ctr;
|
||||||
|
if (likely(!age)) {
|
||||||
|
curr = past;
|
||||||
|
past = ctr->prev_ctr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (past <= 1 && !curr)
|
||||||
|
return past; /* very low rate, avoid flapping */
|
||||||
|
|
||||||
|
return curr + mul32hi(past, ~curr_sec_ms_scaled);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* returns the number of remaining events that can occur on this freq counter
|
/* returns the number of remaining events that can occur on this freq counter
|
||||||
@ -47,16 +56,26 @@ unsigned int read_freq_ctr(struct freq_ctr *ctr)
|
|||||||
*/
|
*/
|
||||||
unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend)
|
unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend)
|
||||||
{
|
{
|
||||||
unsigned int cur;
|
unsigned int curr, past;
|
||||||
if (unlikely(ctr->curr_sec != now.tv_sec))
|
unsigned int age;
|
||||||
rotate_freq_ctr(ctr);
|
|
||||||
|
|
||||||
cur = mul32hi(ctr->prev_ctr, ~curr_sec_ms_scaled);
|
past = 0;
|
||||||
cur += ctr->curr_ctr + pend;
|
curr = 0;
|
||||||
|
age = now.tv_sec - ctr->curr_sec;
|
||||||
|
|
||||||
if (cur >= freq)
|
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;
|
||||||
|
|
||||||
|
if (curr >= freq)
|
||||||
return 0;
|
return 0;
|
||||||
return freq - cur;
|
return freq - curr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return the expected wait time in ms before the next event may occur,
|
/* return the expected wait time in ms before the next event may occur,
|
||||||
@ -67,18 +86,27 @@ unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned i
|
|||||||
*/
|
*/
|
||||||
unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend)
|
unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend)
|
||||||
{
|
{
|
||||||
unsigned int cur, wait;
|
unsigned int curr, past;
|
||||||
|
unsigned int wait, age;
|
||||||
|
|
||||||
if (unlikely(ctr->curr_sec != now.tv_sec))
|
past = 0;
|
||||||
rotate_freq_ctr(ctr);
|
curr = 0;
|
||||||
|
age = now.tv_sec - ctr->curr_sec;
|
||||||
|
|
||||||
cur = mul32hi(ctr->prev_ctr, ~curr_sec_ms_scaled);
|
if (likely(age <= 1)) {
|
||||||
cur += ctr->curr_ctr + pend;
|
past = ctr->curr_ctr;
|
||||||
|
if (likely(!age)) {
|
||||||
|
curr = past;
|
||||||
|
past = ctr->prev_ctr;
|
||||||
|
}
|
||||||
|
curr += mul32hi(past, ~curr_sec_ms_scaled);
|
||||||
|
}
|
||||||
|
curr += pend;
|
||||||
|
|
||||||
if (cur < freq)
|
if (curr < freq)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
wait = 999 / cur;
|
wait = 999 / curr;
|
||||||
return MAX(wait, 1);
|
return MAX(wait, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user