blob: edea699a751a5847decac94a2df67c4ce20c00cf [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/time.h>
16#include <haproxy/tools.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010017
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020018/* Returns the total number of events over the current + last period, including
19 * a number of already pending events <pend>. The average frequency will be
20 * obtained by dividing the output by <period>. This is essentially made to
21 * ease implementation of higher-level read functions.
22 *
23 * As a special case, if pend < 0, it's assumed there are no pending
24 * events and a flapping correction must be applied at the end. This is used by
25 * read_freq_ctr_period() to avoid reporting ups and downs on low-frequency
26 * events when the past value is <= 1.
27 */
Willy Tarreaub4476c62021-04-28 17:44:37 +020028ullong freq_ctr_total(const struct freq_ctr *ctr, uint period, int pend)
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020029{
Willy Tarreau55a09752021-07-15 15:45:44 +020030 ullong curr, past, old_curr, old_past;
31 uint tick, old_tick;
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020032 int remain;
33
Willy Tarreau55a09752021-07-15 15:45:44 +020034 tick = HA_ATOMIC_LOAD(&ctr->curr_tick);
35 curr = HA_ATOMIC_LOAD(&ctr->curr_ctr);
36 past = HA_ATOMIC_LOAD(&ctr->prev_ctr);
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020037
Willy Tarreau55a09752021-07-15 15:45:44 +020038 while (1) {
39 if (tick & 0x1) // change in progress
40 goto redo0;
41
42 old_tick = tick;
43 old_curr = curr;
44 old_past = past;
45
46 /* now let's load the values a second time and make sure they
47 * did not change, which will indicate it was a stable reading.
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020048 */
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020049
Willy Tarreau55a09752021-07-15 15:45:44 +020050 tick = HA_ATOMIC_LOAD(&ctr->curr_tick);
51 if (tick & 0x1) // change in progress
52 goto redo0;
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020053
Willy Tarreau55a09752021-07-15 15:45:44 +020054 if (tick != old_tick)
55 goto redo1;
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020056
Willy Tarreau55a09752021-07-15 15:45:44 +020057 curr = HA_ATOMIC_LOAD(&ctr->curr_ctr);
58 if (curr != old_curr)
59 goto redo2;
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020060
Willy Tarreau55a09752021-07-15 15:45:44 +020061 past = HA_ATOMIC_LOAD(&ctr->prev_ctr);
62 if (past != old_past)
63 goto redo3;
64
65 /* all values match between two loads, they're stable, let's
66 * quit now.
67 */
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020068 break;
Willy Tarreau55a09752021-07-15 15:45:44 +020069 redo0:
70 tick = HA_ATOMIC_LOAD(&ctr->curr_tick);
71 redo1:
72 curr = HA_ATOMIC_LOAD(&ctr->curr_ctr);
73 redo2:
74 past = HA_ATOMIC_LOAD(&ctr->prev_ctr);
75 redo3:
76 __ha_cpu_relax();
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020077 };
78
Willy Tarreau55a09752021-07-15 15:45:44 +020079 remain = tick + period - HA_ATOMIC_LOAD(&global_now_ms);
Willy Tarreauf3a9f8d2021-04-11 00:38:06 +020080 if (unlikely(remain < 0)) {
81 /* We're past the first period, check if we can still report a
82 * part of last period or if we're too far away.
83 */
84 remain += period;
85 past = (remain >= 0) ? curr : 0;
86 curr = 0;
87 }
88
89 if (pend < 0) {
90 /* enable flapping correction at very low rates */
91 pend = 0;
92 if (!curr && past <= 1)
93 return past * period;
94 }
95
96 /* compute the total number of confirmed events over the period */
97 return past * remain + (curr + pend) * period;
98}
Willy Tarreau7f062c42009-03-05 18:43:00 +010099
100/*
101 * Local variables:
102 * c-indent-level: 8
103 * c-basic-offset: 8
104 * End:
105 */