blob: 54aa78f02ad69e2b983c519a73e1ddccee7de55d [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreau66347942020-06-01 12:18:08 +020014#include <haproxy/freq_ctr.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <haproxy/tools.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010016
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020017/* Returns the total number of events over the current + last period, including
18 * a number of already pending events <pend>. The average frequency will be
19 * obtained by dividing the output by <period>. This is essentially made to
20 * ease implementation of higher-level read functions.
21 *
22 * As a special case, if pend < 0, it's assumed there are no pending
23 * events and a flapping correction must be applied at the end. This is used by
24 * read_freq_ctr_period() to avoid reporting ups and downs on low-frequency
25 * events when the past value is <= 1.
26 */
Willy Tarreaub4476c62021-04-28 17:44:37 +020027ullong freq_ctr_total(const struct freq_ctr *ctr, uint period, int pend)
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020028{
Willy Tarreau55a09752021-07-15 15:45:44 +020029 ullong curr, past, old_curr, old_past;
30 uint tick, old_tick;
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020031 int remain;
32
Willy Tarreau55a09752021-07-15 15:45:44 +020033 tick = HA_ATOMIC_LOAD(&ctr->curr_tick);
34 curr = HA_ATOMIC_LOAD(&ctr->curr_ctr);
35 past = HA_ATOMIC_LOAD(&ctr->prev_ctr);
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020036
Willy Tarreau55a09752021-07-15 15:45:44 +020037 while (1) {
38 if (tick & 0x1) // change in progress
39 goto redo0;
40
41 old_tick = tick;
42 old_curr = curr;
43 old_past = past;
44
45 /* now let's load the values a second time and make sure they
46 * did not change, which will indicate it was a stable reading.
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020047 */
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020048
Willy Tarreau55a09752021-07-15 15:45:44 +020049 tick = HA_ATOMIC_LOAD(&ctr->curr_tick);
50 if (tick & 0x1) // change in progress
51 goto redo0;
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020052
Willy Tarreau55a09752021-07-15 15:45:44 +020053 if (tick != old_tick)
54 goto redo1;
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020055
Willy Tarreau55a09752021-07-15 15:45:44 +020056 curr = HA_ATOMIC_LOAD(&ctr->curr_ctr);
57 if (curr != old_curr)
58 goto redo2;
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020059
Willy Tarreau55a09752021-07-15 15:45:44 +020060 past = HA_ATOMIC_LOAD(&ctr->prev_ctr);
61 if (past != old_past)
62 goto redo3;
63
64 /* all values match between two loads, they're stable, let's
65 * quit now.
66 */
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020067 break;
Willy Tarreau55a09752021-07-15 15:45:44 +020068 redo0:
69 tick = HA_ATOMIC_LOAD(&ctr->curr_tick);
70 redo1:
71 curr = HA_ATOMIC_LOAD(&ctr->curr_ctr);
72 redo2:
73 past = HA_ATOMIC_LOAD(&ctr->prev_ctr);
74 redo3:
75 __ha_cpu_relax();
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020076 };
77
Willy Tarreau55a09752021-07-15 15:45:44 +020078 remain = tick + period - HA_ATOMIC_LOAD(&global_now_ms);
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020079 if (unlikely(remain < 0)) {
80 /* We're past the first period, check if we can still report a
81 * part of last period or if we're too far away.
82 */
83 remain += period;
84 past = (remain >= 0) ? curr : 0;
85 curr = 0;
86 }
87
88 if (pend < 0) {
89 /* enable flapping correction at very low rates */
90 pend = 0;
91 if (!curr && past <= 1)
92 return past * period;
93 }
94
95 /* compute the total number of confirmed events over the period */
96 return past * remain + (curr + pend) * period;
97}
Willy Tarreau7f062c42009-03-05 18:43:00 +010098
99/*
100 * Local variables:
101 * c-indent-level: 8
102 * c-basic-offset: 8
103 * End:
104 */