blob: 3df930fc4bd754e48e4eb6da34765598073a1a7b [file] [log] [blame]
Willy Tarreau7f062c42009-03-05 18:43:00 +01001/*
2 * Event rate calculation functions.
3 *
4 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/config.h>
14#include <common/standard.h>
15#include <common/time.h>
Willy Tarreau79584222009-03-06 09:18:27 +010016#include <common/tools.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010017#include <proto/freq_ctr.h>
18
19/* Read a frequency counter taking history into account for missing time in
20 * current period. Current second is sub-divided in 1000 chunks of one ms,
21 * and the missing ones are read proportionally from previous value. The
22 * return value has the same precision as one input data sample, so low rates
23 * will be inaccurate still appropriate for max checking. One trick we use for
24 * low values is to specially handle the case where the rate is between 0 and 1
25 * in order to avoid flapping while waiting for the next event.
Willy Tarreau79584222009-03-06 09:18:27 +010026 *
27 * For immediate limit checking, it's recommended to use freq_ctr_remain() and
28 * next_event_delay() instead which do not have the flapping correction, so
29 * that even frequencies as low as one event/period are properly handled.
Willy Tarreau7f062c42009-03-05 18:43:00 +010030 */
31unsigned int read_freq_ctr(struct freq_ctr *ctr)
32{
Willy Tarreau3d8c5532009-03-06 14:29:25 +010033 unsigned int curr, past;
34 unsigned int age;
Willy Tarreau7f062c42009-03-05 18:43:00 +010035
Willy Tarreau3d8c5532009-03-06 14:29:25 +010036 age = now.tv_sec - ctr->curr_sec;
37 if (unlikely(age > 1))
38 return 0;
39
40 curr = 0;
41 past = ctr->curr_ctr;
42 if (likely(!age)) {
43 curr = past;
44 past = ctr->prev_ctr;
45 }
Willy Tarreau7f062c42009-03-05 18:43:00 +010046
Willy Tarreau3d8c5532009-03-06 14:29:25 +010047 if (past <= 1 && !curr)
48 return past; /* very low rate, avoid flapping */
49
50 return curr + mul32hi(past, ~curr_sec_ms_scaled);
Willy Tarreau7f062c42009-03-05 18:43:00 +010051}
52
Willy Tarreau79584222009-03-06 09:18:27 +010053/* returns the number of remaining events that can occur on this freq counter
54 * while respecting <freq> and taking into account that <pend> events are
55 * already known to be pending. Returns 0 if limit was reached.
56 */
57unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend)
58{
Willy Tarreau3d8c5532009-03-06 14:29:25 +010059 unsigned int curr, past;
60 unsigned int age;
Willy Tarreau79584222009-03-06 09:18:27 +010061
Willy Tarreau3d8c5532009-03-06 14:29:25 +010062 past = 0;
63 curr = 0;
64 age = now.tv_sec - ctr->curr_sec;
Willy Tarreau79584222009-03-06 09:18:27 +010065
Willy Tarreau3d8c5532009-03-06 14:29:25 +010066 if (likely(age <= 1)) {
67 past = ctr->curr_ctr;
68 if (likely(!age)) {
69 curr = past;
70 past = ctr->prev_ctr;
71 }
72 curr += mul32hi(past, ~curr_sec_ms_scaled);
73 }
74 curr += pend;
75
76 if (curr >= freq)
Willy Tarreau79584222009-03-06 09:18:27 +010077 return 0;
Willy Tarreau3d8c5532009-03-06 14:29:25 +010078 return freq - curr;
Willy Tarreau79584222009-03-06 09:18:27 +010079}
80
81/* return the expected wait time in ms before the next event may occur,
82 * respecting frequency <freq>, and assuming there may already be some pending
83 * events. It returns zero if we can proceed immediately, otherwise the wait
84 * time, which will be rounded down 1ms for better accuracy, with a minimum
85 * of one ms.
86 */
87unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend)
88{
Willy Tarreau3d8c5532009-03-06 14:29:25 +010089 unsigned int curr, past;
90 unsigned int wait, age;
Willy Tarreau79584222009-03-06 09:18:27 +010091
Willy Tarreau3d8c5532009-03-06 14:29:25 +010092 past = 0;
93 curr = 0;
94 age = now.tv_sec - ctr->curr_sec;
Willy Tarreau79584222009-03-06 09:18:27 +010095
Willy Tarreau3d8c5532009-03-06 14:29:25 +010096 if (likely(age <= 1)) {
97 past = ctr->curr_ctr;
98 if (likely(!age)) {
99 curr = past;
100 past = ctr->prev_ctr;
101 }
102 curr += mul32hi(past, ~curr_sec_ms_scaled);
103 }
104 curr += pend;
Willy Tarreau79584222009-03-06 09:18:27 +0100105
Willy Tarreau3d8c5532009-03-06 14:29:25 +0100106 if (curr < freq)
Willy Tarreau79584222009-03-06 09:18:27 +0100107 return 0;
108
Willy Tarreau3d8c5532009-03-06 14:29:25 +0100109 wait = 999 / curr;
Willy Tarreau79584222009-03-06 09:18:27 +0100110 return MAX(wait, 1);
111}
112
Willy Tarreau7f062c42009-03-05 18:43:00 +0100113
114/*
115 * Local variables:
116 * c-indent-level: 8
117 * c-basic-offset: 8
118 * End:
119 */