blob: c6ea5c24c79ca52b5c73c41bbbe3bba7cbb448fe [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
25#include <common/config.h>
Willy Tarreau78ff5d02009-10-01 11:05:26 +020026#include <common/time.h>
Christopher Faulet94b71232017-10-12 09:49:09 +020027#include <common/hathreads.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010028#include <types/freq_ctr.h>
29
Willy Tarreau7f062c42009-03-05 18:43:00 +010030
31/* Update a frequency counter by <inc> incremental units. It is automatically
32 * rotated if the period is over. It is important that it correctly initializes
33 * a null area.
34 */
Christopher Fauletde2075f2017-09-01 12:18:36 +020035static inline unsigned int update_freq_ctr(struct freq_ctr *ctr, unsigned int inc)
Willy Tarreau7f062c42009-03-05 18:43:00 +010036{
Christopher Faulet94b71232017-10-12 09:49:09 +020037 unsigned int elapsed;
38 unsigned int tot_inc;
39 unsigned int curr_sec;
40
41 do {
42 /* remove the bit, used for the lock */
43 curr_sec = ctr->curr_sec & 0x7fffffff;
Willy Tarreau7f062c42009-03-05 18:43:00 +010044 }
Christopher Faulet94b71232017-10-12 09:49:09 +020045 while (!HA_ATOMIC_CAS(&ctr->curr_sec, &curr_sec, curr_sec | 0x8000000));
Willy Tarreau7f062c42009-03-05 18:43:00 +010046
Christopher Faulet94b71232017-10-12 09:49:09 +020047 elapsed = (now.tv_sec & 0x7fffffff)- curr_sec;
48 if (unlikely(elapsed)) {
49 ctr->prev_ctr = ctr->curr_ctr;
50 ctr->curr_ctr = 0;
51 if (likely(elapsed != 1)) {
52 /* we missed more than one second */
53 ctr->prev_ctr = 0;
54 }
Willy Tarreau2970b0b2010-06-20 07:15:43 +020055 }
Christopher Faulet94b71232017-10-12 09:49:09 +020056
57 ctr->curr_ctr += inc;
58 tot_inc = ctr->curr_ctr;
59
60 /* release the lock and update the time in case of rotate. */
61 HA_ATOMIC_STORE(&ctr->curr_sec, now.tv_sec & 0x7fffffff);
62 return tot_inc;
63 /* Note: later we may want to propagate the update to other counters */
Willy Tarreau2970b0b2010-06-20 07:15:43 +020064}
65
66/* Update a frequency counter by <inc> incremental units. It is automatically
67 * rotated if the period is over. It is important that it correctly initializes
68 * a null area. This one works on frequency counters which have a period
69 * different from one second.
70 */
Christopher Fauletde2075f2017-09-01 12:18:36 +020071static inline unsigned int update_freq_ctr_period(struct freq_ctr_period *ctr,
72 unsigned int period, unsigned int inc)
Willy Tarreau2970b0b2010-06-20 07:15:43 +020073{
Christopher Faulet94b71232017-10-12 09:49:09 +020074 unsigned int tot_inc;
75 unsigned int curr_tick;
76
77 do {
78 /* remove the bit, used for the lock */
79 curr_tick = (ctr->curr_tick >> 1) << 1;
80 }
81 while (!HA_ATOMIC_CAS(&ctr->curr_tick, &curr_tick, curr_tick | 0x1));
82
83 if (now_ms - curr_tick >= period) {
84 ctr->prev_ctr = ctr->curr_ctr;
85 ctr->curr_ctr = 0;
86 curr_tick += period;
87 if (likely(now_ms - curr_tick >= period)) {
88 /* we missed at least two periods */
89 ctr->prev_ctr = 0;
90 curr_tick = now_ms;
91 }
Willy Tarreau2970b0b2010-06-20 07:15:43 +020092 }
Christopher Faulet94b71232017-10-12 09:49:09 +020093
94 ctr->curr_ctr += inc;
95 tot_inc = ctr->curr_ctr;
96 /* release the lock and update the time in case of rotate. */
97 HA_ATOMIC_STORE(&ctr->curr_tick, (curr_tick >> 1) << 1);
98 return tot_inc;
Willy Tarreau2970b0b2010-06-20 07:15:43 +020099 /* Note: later we may want to propagate the update to other counters */
100}
101
Willy Tarreau7f062c42009-03-05 18:43:00 +0100102/* Read a frequency counter taking history into account for missing time in
103 * current period.
104 */
105unsigned int read_freq_ctr(struct freq_ctr *ctr);
106
Willy Tarreau79584222009-03-06 09:18:27 +0100107/* returns the number of remaining events that can occur on this freq counter
108 * while respecting <freq> and taking into account that <pend> events are
109 * already known to be pending. Returns 0 if limit was reached.
110 */
111unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
112
113/* return the expected wait time in ms before the next event may occur,
114 * respecting frequency <freq>, and assuming there may already be some pending
115 * events. It returns zero if we can proceed immediately, otherwise the wait
116 * time, which will be rounded down 1ms for better accuracy, with a minimum
117 * of one ms.
118 */
119unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
120
Willy Tarreau2970b0b2010-06-20 07:15:43 +0200121/* process freq counters over configurable periods */
122unsigned int read_freq_ctr_period(struct freq_ctr_period *ctr, unsigned int period);
123unsigned int freq_ctr_remain_period(struct freq_ctr_period *ctr, unsigned int period,
124 unsigned int freq, unsigned int pend);
125
Willy Tarreau2438f2b2014-06-16 20:24:22 +0200126/* While the functions above report average event counts per period, we are
127 * also interested in average values per event. For this we use a different
128 * method. The principle is to rely on a long tail which sums the new value
129 * with a fraction of the previous value, resulting in a sliding window of
130 * infinite length depending on the precision we're interested in.
131 *
132 * The idea is that we always keep (N-1)/N of the sum and add the new sampled
133 * value. The sum over N values can be computed with a simple program for a
134 * constant value 1 at each iteration :
135 *
136 * N
137 * ,---
138 * \ N - 1 e - 1
139 * > ( --------- )^x ~= N * -----
140 * / N e
141 * '---
142 * x = 1
143 *
144 * Note: I'm not sure how to demonstrate this but at least this is easily
145 * verified with a simple program, the sum equals N * 0.632120 for any N
146 * moderately large (tens to hundreds).
147 *
148 * Inserting a constant sample value V here simply results in :
149 *
150 * sum = V * N * (e - 1) / e
151 *
152 * But we don't want to integrate over a small period, but infinitely. Let's
153 * cut the infinity in P periods of N values. Each period M is exactly the same
154 * as period M-1 with a factor of ((N-1)/N)^N applied. A test shows that given a
155 * large N :
156 *
157 * N - 1 1
158 * ( ------- )^N ~= ---
159 * N e
160 *
161 * Our sum is now a sum of each factor times :
162 *
163 * N*P P
164 * ,--- ,---
165 * \ N - 1 e - 1 \ 1
166 * > v ( --------- )^x ~= VN * ----- * > ---
167 * / N e / e^x
168 * '--- '---
169 * x = 1 x = 0
170 *
171 * For P "large enough", in tests we get this :
172 *
173 * P
174 * ,---
175 * \ 1 e
176 * > --- ~= -----
177 * / e^x e - 1
178 * '---
179 * x = 0
180 *
181 * This simplifies the sum above :
182 *
183 * N*P
184 * ,---
185 * \ N - 1
186 * > v ( --------- )^x = VN
187 * / N
188 * '---
189 * x = 1
190 *
191 * So basically by summing values and applying the last result an (N-1)/N factor
192 * we just get N times the values over the long term, so we can recover the
Willy Tarreau37585812016-11-25 11:55:10 +0100193 * constant value V by dividing by N. In order to limit the impact of integer
194 * overflows, we'll use this equivalence which saves us one multiply :
195 *
196 * N - 1 1 x0
197 * x1 = x0 * ------- = x0 * ( 1 - --- ) = x0 - ----
198 * N N N
199 *
200 * And given that x0 is discrete here we'll have to saturate the values before
201 * performing the divide, so the value insertion will become :
202 *
203 * x0 + N - 1
204 * x1 = x0 - ------------
205 * N
Willy Tarreau2438f2b2014-06-16 20:24:22 +0200206 *
207 * A value added at the entry of the sliding window of N values will thus be
208 * reduced to 1/e or 36.7% after N terms have been added. After a second batch,
209 * it will only be 1/e^2, or 13.5%, and so on. So practically speaking, each
210 * old period of N values represents only a quickly fading ratio of the global
211 * sum :
212 *
213 * period ratio
214 * 1 36.7%
215 * 2 13.5%
216 * 3 4.98%
217 * 4 1.83%
218 * 5 0.67%
219 * 6 0.25%
220 * 7 0.09%
221 * 8 0.033%
222 * 9 0.012%
223 * 10 0.0045%
224 *
225 * So after 10N samples, the initial value has already faded out by a factor of
226 * 22026, which is quite fast. If the sliding window is 1024 samples wide, it
227 * means that a sample will only count for 1/22k of its initial value after 10k
228 * samples went after it, which results in half of the value it would represent
229 * using an arithmetic mean. The benefit of this method is that it's very cheap
230 * in terms of computations when N is a power of two. This is very well suited
231 * to record response times as large values will fade out faster than with an
232 * arithmetic mean and will depend on sample count and not time.
233 *
234 * Demonstrating all the above assumptions with maths instead of a program is
235 * left as an exercise for the reader.
236 */
237
238/* Adds sample value <v> to sliding window sum <sum> configured for <n> samples.
239 * The sample is returned. Better if <n> is a power of two.
240 */
241static inline unsigned int swrate_add(unsigned int *sum, unsigned int n, unsigned int v)
242{
Willy Tarreau37585812016-11-25 11:55:10 +0100243 return *sum = *sum - (*sum + n - 1) / n + v;
Willy Tarreau2438f2b2014-06-16 20:24:22 +0200244}
245
246/* Returns the average sample value for the sum <sum> over a sliding window of
247 * <n> samples. Better if <n> is a power of two. It must be the same <n> as the
248 * one used above in all additions.
249 */
250static inline unsigned int swrate_avg(unsigned int sum, unsigned int n)
251{
252 return (sum + n - 1) / n;
253}
254
Willy Tarreau7f062c42009-03-05 18:43:00 +0100255#endif /* _PROTO_FREQ_CTR_H */
256
257/*
258 * Local variables:
259 * c-indent-level: 8
260 * c-basic-offset: 8
261 * End:
262 */