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 | |
| 13 | #include <common/config.h> |
| 14 | #include <common/standard.h> |
| 15 | #include <common/time.h> |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 16 | #include <common/tools.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 17 | #include <proto/freq_ctr.h> |
| 18 | |
| 19 | /* Read a frequency counter taking history into account for missing time in |
| 20 | * current period. Current second is sub-divided in 1000 chunks of one ms, |
| 21 | * and the missing ones are read proportionally from previous value. The |
| 22 | * return value has the same precision as one input data sample, so low rates |
| 23 | * will be inaccurate still appropriate for max checking. One trick we use for |
| 24 | * low values is to specially handle the case where the rate is between 0 and 1 |
| 25 | * in order to avoid flapping while waiting for the next event. |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 26 | * |
| 27 | * For immediate limit checking, it's recommended to use freq_ctr_remain() and |
| 28 | * next_event_delay() instead which do not have the flapping correction, so |
| 29 | * that even frequencies as low as one event/period are properly handled. |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 30 | */ |
| 31 | unsigned int read_freq_ctr(struct freq_ctr *ctr) |
| 32 | { |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 33 | unsigned int curr, past; |
| 34 | unsigned int age; |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 35 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 36 | age = now.tv_sec - ctr->curr_sec; |
| 37 | if (unlikely(age > 1)) |
| 38 | return 0; |
| 39 | |
| 40 | curr = 0; |
| 41 | past = ctr->curr_ctr; |
| 42 | if (likely(!age)) { |
| 43 | curr = past; |
| 44 | past = ctr->prev_ctr; |
| 45 | } |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 46 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 47 | if (past <= 1 && !curr) |
| 48 | return past; /* very low rate, avoid flapping */ |
| 49 | |
| 50 | return curr + mul32hi(past, ~curr_sec_ms_scaled); |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 53 | /* returns the number of remaining events that can occur on this freq counter |
| 54 | * while respecting <freq> and taking into account that <pend> events are |
| 55 | * already known to be pending. Returns 0 if limit was reached. |
| 56 | */ |
| 57 | unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend) |
| 58 | { |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 59 | unsigned int curr, past; |
| 60 | unsigned int age; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 61 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 62 | past = 0; |
| 63 | curr = 0; |
| 64 | age = now.tv_sec - ctr->curr_sec; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 65 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 66 | if (likely(age <= 1)) { |
| 67 | past = ctr->curr_ctr; |
| 68 | if (likely(!age)) { |
| 69 | curr = past; |
| 70 | past = ctr->prev_ctr; |
| 71 | } |
| 72 | curr += mul32hi(past, ~curr_sec_ms_scaled); |
| 73 | } |
| 74 | curr += pend; |
| 75 | |
| 76 | if (curr >= freq) |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 77 | return 0; |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 78 | return freq - curr; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* return the expected wait time in ms before the next event may occur, |
| 82 | * respecting frequency <freq>, and assuming there may already be some pending |
| 83 | * events. It returns zero if we can proceed immediately, otherwise the wait |
| 84 | * time, which will be rounded down 1ms for better accuracy, with a minimum |
| 85 | * of one ms. |
| 86 | */ |
| 87 | unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend) |
| 88 | { |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 89 | unsigned int curr, past; |
| 90 | unsigned int wait, age; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 91 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 92 | past = 0; |
| 93 | curr = 0; |
| 94 | age = now.tv_sec - ctr->curr_sec; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 95 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 96 | if (likely(age <= 1)) { |
| 97 | past = ctr->curr_ctr; |
| 98 | if (likely(!age)) { |
| 99 | curr = past; |
| 100 | past = ctr->prev_ctr; |
| 101 | } |
| 102 | curr += mul32hi(past, ~curr_sec_ms_scaled); |
| 103 | } |
| 104 | curr += pend; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 105 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 106 | if (curr < freq) |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 107 | return 0; |
| 108 | |
Willy Tarreau | 3d8c553 | 2009-03-06 14:29:25 +0100 | [diff] [blame] | 109 | wait = 999 / curr; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 110 | return MAX(wait, 1); |
| 111 | } |
| 112 | |
Willy Tarreau | 2970b0b | 2010-06-20 07:15:43 +0200 | [diff] [blame] | 113 | /* Reads a frequency counter taking history into account for missing time in |
| 114 | * current period. The period has to be passed in number of ticks and must |
| 115 | * match the one used to feed the counter. The counter value is reported for |
| 116 | * current date (now_ms). The return value has the same precision as one input |
| 117 | * data sample, so low rates over the period will be inaccurate but still |
| 118 | * appropriate for max checking. One trick we use for low values is to specially |
| 119 | * handle the case where the rate is between 0 and 1 in order to avoid flapping |
| 120 | * while waiting for the next event. |
| 121 | * |
| 122 | * For immediate limit checking, it's recommended to use freq_ctr_period_remain() |
| 123 | * instead which does not have the flapping correction, so that even frequencies |
| 124 | * as low as one event/period are properly handled. |
| 125 | * |
| 126 | * For measures over a 1-second period, it's better to use the implicit functions |
| 127 | * above. |
| 128 | */ |
| 129 | unsigned int read_freq_ctr_period(struct freq_ctr_period *ctr, unsigned int period) |
| 130 | { |
| 131 | unsigned int curr, past; |
| 132 | unsigned int remain; |
| 133 | |
| 134 | curr = ctr->curr_ctr; |
| 135 | past = ctr->prev_ctr; |
| 136 | |
| 137 | remain = ctr->curr_tick + period - now_ms; |
| 138 | if (unlikely((int)remain < 0)) { |
| 139 | /* We're past the first period, check if we can still report a |
| 140 | * part of last period or if we're too far away. |
| 141 | */ |
| 142 | remain += period; |
| 143 | if ((int)remain < 0) |
| 144 | return 0; |
| 145 | past = curr; |
| 146 | curr = 0; |
| 147 | } |
| 148 | if (past <= 1 && !curr) |
| 149 | return past; /* very low rate, avoid flapping */ |
| 150 | |
| 151 | curr += div64_32((unsigned long long)past * remain, period); |
| 152 | return curr; |
| 153 | } |
| 154 | |
| 155 | /* Returns the number of remaining events that can occur on this freq counter |
| 156 | * while respecting <freq> events per period, and taking into account that |
| 157 | * <pend> events are already known to be pending. Returns 0 if limit was reached. |
| 158 | */ |
| 159 | unsigned int freq_ctr_remain_period(struct freq_ctr_period *ctr, unsigned int period, |
| 160 | unsigned int freq, unsigned int pend) |
| 161 | { |
| 162 | unsigned int curr, past; |
| 163 | unsigned int remain; |
| 164 | |
| 165 | curr = ctr->curr_ctr; |
| 166 | past = ctr->prev_ctr; |
| 167 | |
| 168 | remain = ctr->curr_tick + period - now_ms; |
| 169 | if (likely((int)remain < 0)) { |
| 170 | /* We're past the first period, check if we can still report a |
| 171 | * part of last period or if we're too far away. |
| 172 | */ |
| 173 | past = curr; |
| 174 | curr = 0; |
| 175 | remain += period; |
| 176 | if ((int)remain < 0) |
| 177 | past = 0; |
| 178 | } |
| 179 | if (likely(past)) |
| 180 | curr += div64_32((unsigned long long)past * remain, period); |
| 181 | |
| 182 | curr += pend; |
| 183 | freq -= curr; |
| 184 | if ((int)freq < 0) |
| 185 | freq = 0; |
| 186 | return freq; |
| 187 | } |
| 188 | |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * Local variables: |
| 192 | * c-indent-level: 8 |
| 193 | * c-basic-offset: 8 |
| 194 | * End: |
| 195 | */ |