Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Event rate calculation functions. |
| 3 | * |
| 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
| 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 | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * Local variables: |
| 116 | * c-indent-level: 8 |
| 117 | * c-basic-offset: 8 |
| 118 | * End: |
| 119 | */ |