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