blob: 28f59369cfd62849ab83463e8e42ce33ce2d113d [file] [log] [blame]
Willy Tarreau7f062c42009-03-05 18:43:00 +01001/*
2 * Event rate calculation functions.
3 *
Willy Tarreau2970b0b2010-06-20 07:15:43 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau7f062c42009-03-05 18:43:00 +01005 *
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 Tarreau2970b0b2010-06-20 07:15:43 +0200113/* Reads a frequency counter taking history into account for missing time in
114 * current period. The period has to be passed in number of ticks and must
115 * match the one used to feed the counter. The counter value is reported for
116 * current date (now_ms). The return value has the same precision as one input
117 * data sample, so low rates over the period will be inaccurate but still
118 * appropriate for max checking. One trick we use for low values is to specially
119 * handle the case where the rate is between 0 and 1 in order to avoid flapping
120 * while waiting for the next event.
121 *
122 * For immediate limit checking, it's recommended to use freq_ctr_period_remain()
123 * instead which does not have the flapping correction, so that even frequencies
124 * as low as one event/period are properly handled.
125 *
126 * For measures over a 1-second period, it's better to use the implicit functions
127 * above.
128 */
129unsigned int read_freq_ctr_period(struct freq_ctr_period *ctr, unsigned int period)
130{
131 unsigned int curr, past;
132 unsigned int remain;
133
134 curr = ctr->curr_ctr;
135 past = ctr->prev_ctr;
136
137 remain = ctr->curr_tick + period - now_ms;
138 if (unlikely((int)remain < 0)) {
139 /* We're past the first period, check if we can still report a
140 * part of last period or if we're too far away.
141 */
142 remain += period;
143 if ((int)remain < 0)
144 return 0;
145 past = curr;
146 curr = 0;
147 }
148 if (past <= 1 && !curr)
149 return past; /* very low rate, avoid flapping */
150
151 curr += div64_32((unsigned long long)past * remain, period);
152 return curr;
153}
154
155/* Returns the number of remaining events that can occur on this freq counter
156 * while respecting <freq> events per period, and taking into account that
157 * <pend> events are already known to be pending. Returns 0 if limit was reached.
158 */
159unsigned int freq_ctr_remain_period(struct freq_ctr_period *ctr, unsigned int period,
160 unsigned int freq, unsigned int pend)
161{
162 unsigned int curr, past;
163 unsigned int remain;
164
165 curr = ctr->curr_ctr;
166 past = ctr->prev_ctr;
167
168 remain = ctr->curr_tick + period - now_ms;
169 if (likely((int)remain < 0)) {
170 /* We're past the first period, check if we can still report a
171 * part of last period or if we're too far away.
172 */
173 past = curr;
174 curr = 0;
175 remain += period;
176 if ((int)remain < 0)
177 past = 0;
178 }
179 if (likely(past))
180 curr += div64_32((unsigned long long)past * remain, period);
181
182 curr += pend;
183 freq -= curr;
184 if ((int)freq < 0)
185 freq = 0;
186 return freq;
187}
188
Willy Tarreau7f062c42009-03-05 18:43:00 +0100189
190/*
191 * Local variables:
192 * c-indent-level: 8
193 * c-basic-offset: 8
194 * End:
195 */