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> |
| 16 | #include <proto/freq_ctr.h> |
| 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. |
| 25 | */ |
| 26 | unsigned int read_freq_ctr(struct freq_ctr *ctr) |
| 27 | { |
| 28 | unsigned int cur; |
| 29 | if (unlikely(ctr->curr_sec != now.tv_sec)) |
| 30 | rotate_freq_ctr(ctr); |
| 31 | |
| 32 | cur = ctr->curr_ctr; |
| 33 | if (ctr->prev_ctr <= 1 && !ctr->curr_ctr) |
| 34 | return ctr->prev_ctr; /* very low rate, avoid flapping */ |
| 35 | |
| 36 | return cur + mul32hi(ctr->prev_ctr, ~curr_sec_ms_scaled); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * Local variables: |
| 42 | * c-indent-level: 8 |
| 43 | * c-basic-offset: 8 |
| 44 | * End: |
| 45 | */ |