blob: 896a4694e4f33ad5fe327ff698a1c40ea5da09cd [file] [log] [blame]
Willy Tarreau7f062c42009-03-05 18:43:00 +01001/*
Willy Tarreau2438f2b2014-06-16 20:24:22 +02002 * include/proto/freq_ctr.h
3 * This file contains macros and inline functions for frequency counters.
4 *
5 * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreau7f062c42009-03-05 18:43:00 +010021
22#ifndef _PROTO_FREQ_CTR_H
23#define _PROTO_FREQ_CTR_H
24
Willy Tarreau5775d092020-05-28 17:00:53 +020025#include <haproxy/atomic.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Willy Tarreauc7f64e72020-03-06 18:44:55 +010027#include <common/standard.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020028#include <haproxy/time.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010029#include <types/freq_ctr.h>
30
Willy Tarreau7f062c42009-03-05 18:43:00 +010031
32/* Update a frequency counter by <inc> incremental units. It is automatically
33 * rotated if the period is over. It is important that it correctly initializes
34 * a null area.
35 */
Christopher Fauletde2075f2017-09-01 12:18:36 +020036static inline unsigned int update_freq_ctr(struct freq_ctr *ctr, unsigned int inc)
Willy Tarreau7f062c42009-03-05 18:43:00 +010037{
Emeric Brun6e012862017-10-30 18:04:28 +010038 int elapsed;
Christopher Faulet94b71232017-10-12 09:49:09 +020039 unsigned int curr_sec;
40
Willy Tarreau0d6c75d2019-05-25 19:54:40 +020041
42 /* we manipulate curr_ctr using atomic ops out of the lock, since
43 * it's the most frequent access. However if we detect that a change
44 * is needed, it's done under the date lock. We don't care whether
45 * the value we're adding is considered as part of the current or
46 * new period if another thread starts to rotate the period while
47 * we operate, since timing variations would have resulted in the
48 * same uncertainty as well.
49 */
50 curr_sec = ctr->curr_sec;
51 if (curr_sec == (now.tv_sec & 0x7fffffff))
52 return _HA_ATOMIC_ADD(&ctr->curr_ctr, inc);
53
Christopher Faulet94b71232017-10-12 09:49:09 +020054 do {
55 /* remove the bit, used for the lock */
Willy Tarreau0d6c75d2019-05-25 19:54:40 +020056 curr_sec &= 0x7fffffff;
57 } while (!_HA_ATOMIC_CAS(&ctr->curr_sec, &curr_sec, curr_sec | 0x80000000));
Olivier Houchardd5f9b192019-03-08 18:47:59 +010058 __ha_barrier_atomic_store();
Willy Tarreau7f062c42009-03-05 18:43:00 +010059
Christopher Faulet94b71232017-10-12 09:49:09 +020060 elapsed = (now.tv_sec & 0x7fffffff)- curr_sec;
Emeric Brun6e012862017-10-30 18:04:28 +010061 if (unlikely(elapsed > 0)) {
Christopher Faulet94b71232017-10-12 09:49:09 +020062 ctr->prev_ctr = ctr->curr_ctr;
Willy Tarreau0d6c75d2019-05-25 19:54:40 +020063 _HA_ATOMIC_SUB(&ctr->curr_ctr, ctr->prev_ctr);
Christopher Faulet94b71232017-10-12 09:49:09 +020064 if (likely(elapsed != 1)) {
65 /* we missed more than one second */
66 ctr->prev_ctr = 0;
67 }
Emeric Brun6e012862017-10-30 18:04:28 +010068 curr_sec = now.tv_sec;
Willy Tarreau2970b0b2010-06-20 07:15:43 +020069 }
Christopher Faulet94b71232017-10-12 09:49:09 +020070
Christopher Faulet94b71232017-10-12 09:49:09 +020071 /* release the lock and update the time in case of rotate. */
Olivier Houchardd5f9b192019-03-08 18:47:59 +010072 _HA_ATOMIC_STORE(&ctr->curr_sec, curr_sec & 0x7fffffff);
Willy Tarreau0d6c75d2019-05-25 19:54:40 +020073
74 return _HA_ATOMIC_ADD(&ctr->curr_ctr, inc);
Willy Tarreau2970b0b2010-06-20 07:15:43 +020075}
76
77/* Update a frequency counter by <inc> incremental units. It is automatically
78 * rotated if the period is over. It is important that it correctly initializes
79 * a null area. This one works on frequency counters which have a period
80 * different from one second.
81 */
Christopher Fauletde2075f2017-09-01 12:18:36 +020082static inline unsigned int update_freq_ctr_period(struct freq_ctr_period *ctr,
83 unsigned int period, unsigned int inc)
Willy Tarreau2970b0b2010-06-20 07:15:43 +020084{
Christopher Faulet94b71232017-10-12 09:49:09 +020085 unsigned int curr_tick;
86
Willy Tarreau0d6c75d2019-05-25 19:54:40 +020087 curr_tick = ctr->curr_tick;
88 if (now_ms - curr_tick < period)
89 return _HA_ATOMIC_ADD(&ctr->curr_ctr, inc);
90
Christopher Faulet94b71232017-10-12 09:49:09 +020091 do {
92 /* remove the bit, used for the lock */
Willy Tarreau0d6c75d2019-05-25 19:54:40 +020093 curr_tick &= ~1;
94 } while (!_HA_ATOMIC_CAS(&ctr->curr_tick, &curr_tick, curr_tick | 0x1));
Olivier Houchardd5f9b192019-03-08 18:47:59 +010095 __ha_barrier_atomic_store();
Christopher Faulet94b71232017-10-12 09:49:09 +020096
97 if (now_ms - curr_tick >= period) {
98 ctr->prev_ctr = ctr->curr_ctr;
Willy Tarreau0d6c75d2019-05-25 19:54:40 +020099 _HA_ATOMIC_SUB(&ctr->curr_ctr, ctr->prev_ctr);
Christopher Faulet94b71232017-10-12 09:49:09 +0200100 curr_tick += period;
101 if (likely(now_ms - curr_tick >= period)) {
102 /* we missed at least two periods */
103 ctr->prev_ctr = 0;
104 curr_tick = now_ms;
105 }
Willy Tarreau0d6c75d2019-05-25 19:54:40 +0200106 curr_tick &= ~1;
Willy Tarreau2970b0b2010-06-20 07:15:43 +0200107 }
Christopher Faulet94b71232017-10-12 09:49:09 +0200108
Christopher Faulet94b71232017-10-12 09:49:09 +0200109 /* release the lock and update the time in case of rotate. */
Willy Tarreau0d6c75d2019-05-25 19:54:40 +0200110 _HA_ATOMIC_STORE(&ctr->curr_tick, curr_tick);
111
112 return _HA_ATOMIC_ADD(&ctr->curr_ctr, inc);
Willy Tarreau2970b0b2010-06-20 07:15:43 +0200113}
114
Willy Tarreau7f062c42009-03-05 18:43:00 +0100115/* Read a frequency counter taking history into account for missing time in
116 * current period.
117 */
118unsigned int read_freq_ctr(struct freq_ctr *ctr);
119
Willy Tarreau79584222009-03-06 09:18:27 +0100120/* returns the number of remaining events that can occur on this freq counter
121 * while respecting <freq> and taking into account that <pend> events are
122 * already known to be pending. Returns 0 if limit was reached.
123 */
124unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
125
126/* return the expected wait time in ms before the next event may occur,
127 * respecting frequency <freq>, and assuming there may already be some pending
128 * events. It returns zero if we can proceed immediately, otherwise the wait
129 * time, which will be rounded down 1ms for better accuracy, with a minimum
130 * of one ms.
131 */
132unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
133
Willy Tarreau2970b0b2010-06-20 07:15:43 +0200134/* process freq counters over configurable periods */
135unsigned int read_freq_ctr_period(struct freq_ctr_period *ctr, unsigned int period);
136unsigned int freq_ctr_remain_period(struct freq_ctr_period *ctr, unsigned int period,
137 unsigned int freq, unsigned int pend);
138
Willy Tarreau2438f2b2014-06-16 20:24:22 +0200139/* While the functions above report average event counts per period, we are
140 * also interested in average values per event. For this we use a different
141 * method. The principle is to rely on a long tail which sums the new value
142 * with a fraction of the previous value, resulting in a sliding window of
143 * infinite length depending on the precision we're interested in.
144 *
145 * The idea is that we always keep (N-1)/N of the sum and add the new sampled
146 * value. The sum over N values can be computed with a simple program for a
147 * constant value 1 at each iteration :
148 *
149 * N
150 * ,---
151 * \ N - 1 e - 1
152 * > ( --------- )^x ~= N * -----
153 * / N e
154 * '---
155 * x = 1
156 *
157 * Note: I'm not sure how to demonstrate this but at least this is easily
158 * verified with a simple program, the sum equals N * 0.632120 for any N
159 * moderately large (tens to hundreds).
160 *
161 * Inserting a constant sample value V here simply results in :
162 *
163 * sum = V * N * (e - 1) / e
164 *
165 * But we don't want to integrate over a small period, but infinitely. Let's
166 * cut the infinity in P periods of N values. Each period M is exactly the same
167 * as period M-1 with a factor of ((N-1)/N)^N applied. A test shows that given a
168 * large N :
169 *
170 * N - 1 1
171 * ( ------- )^N ~= ---
172 * N e
173 *
174 * Our sum is now a sum of each factor times :
175 *
176 * N*P P
177 * ,--- ,---
178 * \ N - 1 e - 1 \ 1
179 * > v ( --------- )^x ~= VN * ----- * > ---
180 * / N e / e^x
181 * '--- '---
182 * x = 1 x = 0
183 *
184 * For P "large enough", in tests we get this :
185 *
186 * P
187 * ,---
188 * \ 1 e
189 * > --- ~= -----
190 * / e^x e - 1
191 * '---
192 * x = 0
193 *
194 * This simplifies the sum above :
195 *
196 * N*P
197 * ,---
198 * \ N - 1
199 * > v ( --------- )^x = VN
200 * / N
201 * '---
202 * x = 1
203 *
204 * So basically by summing values and applying the last result an (N-1)/N factor
205 * we just get N times the values over the long term, so we can recover the
Willy Tarreau37585812016-11-25 11:55:10 +0100206 * constant value V by dividing by N. In order to limit the impact of integer
207 * overflows, we'll use this equivalence which saves us one multiply :
208 *
209 * N - 1 1 x0
210 * x1 = x0 * ------- = x0 * ( 1 - --- ) = x0 - ----
211 * N N N
212 *
213 * And given that x0 is discrete here we'll have to saturate the values before
214 * performing the divide, so the value insertion will become :
215 *
216 * x0 + N - 1
217 * x1 = x0 - ------------
218 * N
Willy Tarreau2438f2b2014-06-16 20:24:22 +0200219 *
220 * A value added at the entry of the sliding window of N values will thus be
221 * reduced to 1/e or 36.7% after N terms have been added. After a second batch,
222 * it will only be 1/e^2, or 13.5%, and so on. So practically speaking, each
223 * old period of N values represents only a quickly fading ratio of the global
224 * sum :
225 *
226 * period ratio
227 * 1 36.7%
228 * 2 13.5%
229 * 3 4.98%
230 * 4 1.83%
231 * 5 0.67%
232 * 6 0.25%
233 * 7 0.09%
234 * 8 0.033%
235 * 9 0.012%
236 * 10 0.0045%
237 *
238 * So after 10N samples, the initial value has already faded out by a factor of
239 * 22026, which is quite fast. If the sliding window is 1024 samples wide, it
240 * means that a sample will only count for 1/22k of its initial value after 10k
241 * samples went after it, which results in half of the value it would represent
242 * using an arithmetic mean. The benefit of this method is that it's very cheap
243 * in terms of computations when N is a power of two. This is very well suited
244 * to record response times as large values will fade out faster than with an
245 * arithmetic mean and will depend on sample count and not time.
246 *
247 * Demonstrating all the above assumptions with maths instead of a program is
248 * left as an exercise for the reader.
249 */
250
251/* Adds sample value <v> to sliding window sum <sum> configured for <n> samples.
Christopher Faulete2e8c672019-11-08 14:40:18 +0100252 * The sample is returned. Better if <n> is a power of two. This function is
253 * thread-safe.
Willy Tarreau2438f2b2014-06-16 20:24:22 +0200254 */
255static inline unsigned int swrate_add(unsigned int *sum, unsigned int n, unsigned int v)
256{
Christopher Faulete2e8c672019-11-08 14:40:18 +0100257 unsigned int new_sum, old_sum;
258
259 old_sum = *sum;
260 do {
261 new_sum = old_sum - (old_sum + n - 1) / n + v;
262 } while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum));
263 return new_sum;
Willy Tarreau2438f2b2014-06-16 20:24:22 +0200264}
265
Marcin Deranek4dc2b572020-05-15 18:26:18 +0200266/* Adds sample value <v> to sliding window sum <sum> configured for <n> samples.
267 * The sample is returned. Better if <n> is a power of two. This function is
268 * thread-safe.
269 * This function should give better accuracy than swrate_add when number of
270 * samples collected is lower than nominal window size. In such circumstances
271 * <n> should be set to 0.
272 */
273static inline unsigned int swrate_add_dynamic(unsigned int *sum, unsigned int n, unsigned int v)
274{
275 unsigned int new_sum, old_sum;
276
277 old_sum = *sum;
278 do {
279 new_sum = old_sum - (n ? (old_sum + n - 1) / n : 0) + v;
280 } while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum));
281 return new_sum;
282}
283
Willy Tarreau627505d2018-10-17 09:24:56 +0200284/* Adds sample value <v> spanning <s> samples to sliding window sum <sum>
285 * configured for <n> samples, where <n> is supposed to be "much larger" than
286 * <s>. The sample is returned. Better if <n> is a power of two. Note that this
287 * is only an approximate. Indeed, as can be seen with two samples only over a
288 * 8-sample window, the original function would return :
289 * sum1 = sum - (sum + 7) / 8 + v
290 * sum2 = sum1 - (sum1 + 7) / 8 + v
291 * = (sum - (sum + 7) / 8 + v) - (sum - (sum + 7) / 8 + v + 7) / 8 + v
292 * ~= 7sum/8 - 7/8 + v - sum/8 + sum/64 - 7/64 - v/8 - 7/8 + v
293 * ~= (3sum/4 + sum/64) - (7/4 + 7/64) + 15v/8
294 *
295 * while the function below would return :
296 * sum = sum + 2*v - (sum + 8) * 2 / 8
297 * = 3sum/4 + 2v - 2
298 *
299 * this presents an error of ~ (sum/64 + 9/64 + v/8) = (sum+n+1)/(n^s) + v/n
300 *
301 * Thus the simplified function effectively replaces a part of the history with
302 * a linear sum instead of applying the exponential one. But as long as s/n is
303 * "small enough", the error fades away and remains small for both small and
Christopher Faulete2e8c672019-11-08 14:40:18 +0100304 * large values of n and s (typically < 0.2% measured). This function is
305 * thread-safe.
Willy Tarreau627505d2018-10-17 09:24:56 +0200306 */
307static inline unsigned int swrate_add_scaled(unsigned int *sum, unsigned int n, unsigned int v, unsigned int s)
308{
Christopher Faulete2e8c672019-11-08 14:40:18 +0100309 unsigned int new_sum, old_sum;
310
311 old_sum = *sum;
312 do {
313 new_sum = old_sum + v * s - div64_32((unsigned long long)(old_sum + n) * s, n);
314 } while (!_HA_ATOMIC_CAS(sum, &old_sum, new_sum));
315 return new_sum;
Willy Tarreau627505d2018-10-17 09:24:56 +0200316}
317
Willy Tarreau2438f2b2014-06-16 20:24:22 +0200318/* Returns the average sample value for the sum <sum> over a sliding window of
319 * <n> samples. Better if <n> is a power of two. It must be the same <n> as the
320 * one used above in all additions.
321 */
322static inline unsigned int swrate_avg(unsigned int sum, unsigned int n)
323{
324 return (sum + n - 1) / n;
325}
326
Willy Tarreau7f062c42009-03-05 18:43:00 +0100327#endif /* _PROTO_FREQ_CTR_H */
328
329/*
330 * Local variables:
331 * c-indent-level: 8
332 * c-basic-offset: 8
333 * End:
334 */