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