Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Event rate calculation functions. |
| 3 | * |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 5 | * |
| 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 Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 13 | #include <haproxy/api.h> |
Willy Tarreau | 6634794 | 2020-06-01 12:18:08 +0200 | [diff] [blame] | 14 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 15 | #include <haproxy/time.h> |
| 16 | #include <haproxy/tools.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 17 | |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 18 | /* 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 Tarreau | b4476c6 | 2021-04-28 17:44:37 +0200 | [diff] [blame] | 28 | ullong freq_ctr_total(const struct freq_ctr *ctr, uint period, int pend) |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 29 | { |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 30 | ullong curr, past, old_curr, old_past; |
| 31 | uint tick, old_tick; |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 32 | int remain; |
| 33 | |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 34 | tick = HA_ATOMIC_LOAD(&ctr->curr_tick); |
| 35 | curr = HA_ATOMIC_LOAD(&ctr->curr_ctr); |
| 36 | past = HA_ATOMIC_LOAD(&ctr->prev_ctr); |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 38 | 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 Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 48 | */ |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 50 | tick = HA_ATOMIC_LOAD(&ctr->curr_tick); |
| 51 | if (tick & 0x1) // change in progress |
| 52 | goto redo0; |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 54 | if (tick != old_tick) |
| 55 | goto redo1; |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 56 | |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 57 | curr = HA_ATOMIC_LOAD(&ctr->curr_ctr); |
| 58 | if (curr != old_curr) |
| 59 | goto redo2; |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 61 | 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 Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 68 | break; |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 69 | 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 Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Willy Tarreau | 55a0975 | 2021-07-15 15:45:44 +0200 | [diff] [blame] | 79 | remain = tick + period - HA_ATOMIC_LOAD(&global_now_ms); |
Willy Tarreau | f3a9f8d | 2021-04-11 00:38:06 +0200 | [diff] [blame] | 80 | 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 Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Local variables: |
| 102 | * c-indent-level: 8 |
| 103 | * c-basic-offset: 8 |
| 104 | * End: |
| 105 | */ |