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 | |
| 18 | /* Read a frequency counter taking history into account for missing time in |
| 19 | * current period. Current second is sub-divided in 1000 chunks of one ms, |
| 20 | * and the missing ones are read proportionally from previous value. The |
| 21 | * return value has the same precision as one input data sample, so low rates |
| 22 | * will be inaccurate still appropriate for max checking. One trick we use for |
| 23 | * low values is to specially handle the case where the rate is between 0 and 1 |
| 24 | * in order to avoid flapping while waiting for the next event. |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 25 | * |
| 26 | * For immediate limit checking, it's recommended to use freq_ctr_remain() and |
| 27 | * next_event_delay() instead which do not have the flapping correction, so |
| 28 | * that even frequencies as low as one event/period are properly handled. |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 29 | */ |
| 30 | unsigned int read_freq_ctr(struct freq_ctr *ctr) |
| 31 | { |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 32 | unsigned int curr, past, _curr, _past; |
| 33 | unsigned int age, curr_sec, _curr_sec; |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 34 | |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 35 | while (1) { |
| 36 | _curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 37 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 38 | _past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 39 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 40 | _curr_sec = ctr->curr_sec; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 41 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 42 | if (_curr_sec & 0x80000000) |
| 43 | continue; |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 44 | curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 45 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 46 | past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 47 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 48 | curr_sec = ctr->curr_sec; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 49 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 50 | if (_curr == curr && _past == past && _curr_sec == curr_sec) |
| 51 | break; |
| 52 | } |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | a1ecbca | 2021-03-17 19:10:23 +0100 | [diff] [blame] | 54 | age = (global_now >> 32) - curr_sec; |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 55 | if (unlikely(age > 1)) |
| 56 | return 0; |
| 57 | |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 58 | if (unlikely(age)) { |
| 59 | past = curr; |
| 60 | curr = 0; |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 61 | } |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 62 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 63 | if (past <= 1 && !curr) |
| 64 | return past; /* very low rate, avoid flapping */ |
| 65 | |
Willy Tarreau | eab777c | 2012-12-29 21:50:07 +0100 | [diff] [blame] | 66 | return curr + mul32hi(past, ms_left_scaled); |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 69 | /* returns the number of remaining events that can occur on this freq counter |
| 70 | * while respecting <freq> and taking into account that <pend> events are |
| 71 | * already known to be pending. Returns 0 if limit was reached. |
| 72 | */ |
| 73 | unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend) |
| 74 | { |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 75 | unsigned int curr, past, _curr, _past; |
| 76 | unsigned int age, curr_sec, _curr_sec; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 78 | while (1) { |
| 79 | _curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 80 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 81 | _past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 82 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 83 | _curr_sec = ctr->curr_sec; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 84 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 85 | if (_curr_sec & 0x80000000) |
| 86 | continue; |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 87 | curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 88 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 89 | past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 90 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 91 | curr_sec = ctr->curr_sec; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 92 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 93 | if (_curr == curr && _past == past && _curr_sec == curr_sec) |
| 94 | break; |
| 95 | } |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 96 | |
Willy Tarreau | a1ecbca | 2021-03-17 19:10:23 +0100 | [diff] [blame] | 97 | age = (global_now >> 32) - curr_sec; |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 98 | if (unlikely(age > 1)) |
| 99 | curr = 0; |
| 100 | else { |
| 101 | if (unlikely(age == 1)) { |
| 102 | past = curr; |
| 103 | curr = 0; |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 104 | } |
Willy Tarreau | eab777c | 2012-12-29 21:50:07 +0100 | [diff] [blame] | 105 | curr += mul32hi(past, ms_left_scaled); |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 106 | } |
| 107 | curr += pend; |
| 108 | |
| 109 | if (curr >= freq) |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 110 | return 0; |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 111 | return freq - curr; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* return the expected wait time in ms before the next event may occur, |
| 115 | * respecting frequency <freq>, and assuming there may already be some pending |
| 116 | * events. It returns zero if we can proceed immediately, otherwise the wait |
| 117 | * time, which will be rounded down 1ms for better accuracy, with a minimum |
| 118 | * of one ms. |
| 119 | */ |
| 120 | unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend) |
| 121 | { |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 122 | unsigned int curr, past, _curr, _past; |
| 123 | unsigned int wait, age, curr_sec, _curr_sec; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 125 | while (1) { |
| 126 | _curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 127 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 128 | _past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 129 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 130 | _curr_sec = ctr->curr_sec; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 131 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 132 | if (_curr_sec & 0x80000000) |
| 133 | continue; |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 134 | curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 135 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 136 | past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 137 | __ha_compiler_barrier(); |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 138 | curr_sec = ctr->curr_sec; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 139 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 140 | if (_curr == curr && _past == past && _curr_sec == curr_sec) |
| 141 | break; |
| 142 | } |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 143 | |
Willy Tarreau | a1ecbca | 2021-03-17 19:10:23 +0100 | [diff] [blame] | 144 | age = (global_now >> 32) - curr_sec; |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 145 | if (unlikely(age > 1)) |
| 146 | curr = 0; |
| 147 | else { |
| 148 | if (unlikely(age == 1)) { |
| 149 | past = curr; |
| 150 | curr = 0; |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 151 | } |
Willy Tarreau | eab777c | 2012-12-29 21:50:07 +0100 | [diff] [blame] | 152 | curr += mul32hi(past, ms_left_scaled); |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 153 | } |
| 154 | curr += pend; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 155 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 156 | if (curr < freq) |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 157 | return 0; |
| 158 | |
Willy Tarreau | e4d247e | 2021-02-09 17:39:08 +0100 | [diff] [blame] | 159 | /* too many events already, let's count how long to wait before they're |
| 160 | * processed. For this we'll subtract from the number of pending events |
| 161 | * the ones programmed for the current period, to know how long to wait |
| 162 | * for the next period. Each event takes 1/freq sec, thus 1000/freq ms. |
| 163 | */ |
| 164 | curr -= freq; |
| 165 | wait = curr * 1000 / (freq ? freq : 1); |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 166 | return MAX(wait, 1); |
| 167 | } |
| 168 | |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 169 | /* Reads a frequency counter taking history into account for missing time in |
| 170 | * current period. The period has to be passed in number of ticks and must |
| 171 | * match the one used to feed the counter. The counter value is reported for |
Willy Tarreau | a1ecbca | 2021-03-17 19:10:23 +0100 | [diff] [blame] | 172 | * current global date. The return value has the same precision as one input |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 173 | * data sample, so low rates over the period will be inaccurate but still |
| 174 | * appropriate for max checking. One trick we use for low values is to specially |
| 175 | * handle the case where the rate is between 0 and 1 in order to avoid flapping |
| 176 | * while waiting for the next event. |
| 177 | * |
| 178 | * For immediate limit checking, it's recommended to use freq_ctr_period_remain() |
| 179 | * instead which does not have the flapping correction, so that even frequencies |
| 180 | * as low as one event/period are properly handled. |
| 181 | * |
| 182 | * For measures over a 1-second period, it's better to use the implicit functions |
| 183 | * above. |
| 184 | */ |
| 185 | unsigned int read_freq_ctr_period(struct freq_ctr_period *ctr, unsigned int period) |
| 186 | { |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 187 | unsigned int _curr, _past, curr, past; |
| 188 | unsigned int remain, _curr_tick, curr_tick; |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 189 | |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 190 | while (1) { |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 191 | _curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 192 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 193 | _past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 194 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 195 | _curr_tick = ctr->curr_tick; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 196 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 197 | if (_curr_tick & 0x1) |
| 198 | continue; |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 199 | curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 200 | __ha_compiler_barrier(); |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 201 | past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 202 | __ha_compiler_barrier(); |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 203 | curr_tick = ctr->curr_tick; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 204 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 205 | if (_curr == curr && _past == past && _curr_tick == curr_tick) |
| 206 | break; |
| 207 | }; |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | a1ecbca | 2021-03-17 19:10:23 +0100 | [diff] [blame] | 209 | remain = curr_tick + period - (uint32_t)global_now / 1000; |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 210 | if (unlikely((int)remain < 0)) { |
| 211 | /* We're past the first period, check if we can still report a |
| 212 | * part of last period or if we're too far away. |
| 213 | */ |
| 214 | remain += period; |
| 215 | if ((int)remain < 0) |
| 216 | return 0; |
| 217 | past = curr; |
| 218 | curr = 0; |
| 219 | } |
| 220 | if (past <= 1 && !curr) |
| 221 | return past; /* very low rate, avoid flapping */ |
| 222 | |
| 223 | curr += div64_32((unsigned long long)past * remain, period); |
| 224 | return curr; |
| 225 | } |
| 226 | |
| 227 | /* Returns the number of remaining events that can occur on this freq counter |
| 228 | * while respecting <freq> events per period, and taking into account that |
| 229 | * <pend> events are already known to be pending. Returns 0 if limit was reached. |
| 230 | */ |
| 231 | unsigned int freq_ctr_remain_period(struct freq_ctr_period *ctr, unsigned int period, |
| 232 | unsigned int freq, unsigned int pend) |
| 233 | { |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 234 | unsigned int _curr, _past, curr, past; |
| 235 | unsigned int remain, _curr_tick, curr_tick; |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 236 | |
Willy Tarreau | a06a580 | 2017-10-31 17:54:15 +0100 | [diff] [blame] | 237 | while (1) { |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 238 | _curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 239 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 240 | _past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 241 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 242 | _curr_tick = ctr->curr_tick; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 243 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 244 | if (_curr_tick & 0x1) |
| 245 | continue; |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 246 | curr = ctr->curr_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 247 | __ha_compiler_barrier(); |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 248 | past = ctr->prev_ctr; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 249 | __ha_compiler_barrier(); |
Christopher Faulet | 94b7123 | 2017-10-12 09:49:09 +0200 | [diff] [blame] | 250 | curr_tick = ctr->curr_tick; |
Willy Tarreau | 9453ecd | 2020-05-28 15:29:33 +0200 | [diff] [blame] | 251 | __ha_compiler_barrier(); |
Emeric Brun | 6e01286 | 2017-10-30 18:04:28 +0100 | [diff] [blame] | 252 | if (_curr == curr && _past == past && _curr_tick == curr_tick) |
| 253 | break; |
| 254 | }; |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 255 | |
Willy Tarreau | a1ecbca | 2021-03-17 19:10:23 +0100 | [diff] [blame] | 256 | remain = curr_tick + period - (uint32_t)global_now / 1000; |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 257 | if (likely((int)remain < 0)) { |
| 258 | /* We're past the first period, check if we can still report a |
| 259 | * part of last period or if we're too far away. |
| 260 | */ |
| 261 | past = curr; |
| 262 | curr = 0; |
| 263 | remain += period; |
| 264 | if ((int)remain < 0) |
| 265 | past = 0; |
| 266 | } |
| 267 | if (likely(past)) |
| 268 | curr += div64_32((unsigned long long)past * remain, period); |
| 269 | |
| 270 | curr += pend; |
| 271 | freq -= curr; |
| 272 | if ((int)freq < 0) |
| 273 | freq = 0; |
| 274 | return freq; |
| 275 | } |
| 276 | |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * Local variables: |
| 280 | * c-indent-level: 8 |
| 281 | * c-basic-offset: 8 |
| 282 | * End: |
| 283 | */ |