blob: d85e7210ff2b910f46195f8fc6ea4c1b3debeb37 [file] [log] [blame]
Willy Tarreau7f062c42009-03-05 18:43:00 +01001/*
2 include/proto/freq_ctr.h
3 This file contains macros and inline functions for frequency counters.
4
5 Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation, version 2.1
10 exclusively.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#ifndef _PROTO_FREQ_CTR_H
23#define _PROTO_FREQ_CTR_H
24
25#include <common/config.h>
Willy Tarreau78ff5d02009-10-01 11:05:26 +020026#include <common/time.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010027#include <types/freq_ctr.h>
28
29/* Rotate a frequency counter when current period is over. Must not be called
30 * during a valid period. It is important that it correctly initializes a null
31 * area.
32 */
33static inline void rotate_freq_ctr(struct freq_ctr *ctr)
34{
35 ctr->prev_ctr = ctr->curr_ctr;
36 if (likely(now.tv_sec - ctr->curr_sec != 1)) {
37 /* we missed more than one second */
38 ctr->prev_ctr = 0;
39 }
40 ctr->curr_sec = now.tv_sec;
41 ctr->curr_ctr = 0; /* leave it at the end to help gcc optimize it away */
42}
43
44/* Update a frequency counter by <inc> incremental units. It is automatically
45 * rotated if the period is over. It is important that it correctly initializes
46 * a null area.
47 */
48static inline void update_freq_ctr(struct freq_ctr *ctr, unsigned int inc)
49{
50 if (likely(ctr->curr_sec == now.tv_sec)) {
51 ctr->curr_ctr += inc;
52 return;
53 }
54 rotate_freq_ctr(ctr);
55 ctr->curr_ctr = inc;
56 /* Note: later we may want to propagate the update to other counters */
57}
58
Willy Tarreau2970b0b2010-06-20 07:15:43 +020059/* Rotate a frequency counter when current period is over. Must not be called
60 * during a valid period. It is important that it correctly initializes a null
61 * area. This one works on frequency counters which have a period different
62 * from one second.
63 */
64static inline void rotate_freq_ctr_period(struct freq_ctr_period *ctr,
65 unsigned int period)
66{
67 ctr->prev_ctr = ctr->curr_ctr;
68 ctr->curr_tick += period;
69 if (likely(now_ms - ctr->curr_tick >= period)) {
70 /* we missed at least two periods */
71 ctr->prev_ctr = 0;
72 ctr->curr_tick = now_ms;
73 }
74 ctr->curr_ctr = 0; /* leave it at the end to help gcc optimize it away */
75}
76
77/* Update a frequency counter by <inc> incremental units. It is automatically
78 * rotated if the period is over. It is important that it correctly initializes
79 * a null area. This one works on frequency counters which have a period
80 * different from one second.
81 */
82static inline void update_freq_ctr_period(struct freq_ctr_period *ctr,
83 unsigned int period, unsigned int inc)
84{
85 if (likely(now_ms - ctr->curr_tick < period)) {
86 ctr->curr_ctr += inc;
87 return;
88 }
89 rotate_freq_ctr_period(ctr, period);
90 ctr->curr_ctr = inc;
91 /* Note: later we may want to propagate the update to other counters */
92}
93
Willy Tarreau7f062c42009-03-05 18:43:00 +010094/* Read a frequency counter taking history into account for missing time in
95 * current period.
96 */
97unsigned int read_freq_ctr(struct freq_ctr *ctr);
98
Willy Tarreau79584222009-03-06 09:18:27 +010099/* returns the number of remaining events that can occur on this freq counter
100 * while respecting <freq> and taking into account that <pend> events are
101 * already known to be pending. Returns 0 if limit was reached.
102 */
103unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
104
105/* return the expected wait time in ms before the next event may occur,
106 * respecting frequency <freq>, and assuming there may already be some pending
107 * events. It returns zero if we can proceed immediately, otherwise the wait
108 * time, which will be rounded down 1ms for better accuracy, with a minimum
109 * of one ms.
110 */
111unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
112
Willy Tarreau2970b0b2010-06-20 07:15:43 +0200113/* process freq counters over configurable periods */
114unsigned int read_freq_ctr_period(struct freq_ctr_period *ctr, unsigned int period);
115unsigned int freq_ctr_remain_period(struct freq_ctr_period *ctr, unsigned int period,
116 unsigned int freq, unsigned int pend);
117
Willy Tarreau7f062c42009-03-05 18:43:00 +0100118#endif /* _PROTO_FREQ_CTR_H */
119
120/*
121 * Local variables:
122 * c-indent-level: 8
123 * c-basic-offset: 8
124 * End:
125 */