blob: 7add5148070fdec6e6306c71cf8ca54d481eca9a [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
59/* Read a frequency counter taking history into account for missing time in
60 * current period.
61 */
62unsigned int read_freq_ctr(struct freq_ctr *ctr);
63
Willy Tarreau79584222009-03-06 09:18:27 +010064/* returns the number of remaining events that can occur on this freq counter
65 * while respecting <freq> and taking into account that <pend> events are
66 * already known to be pending. Returns 0 if limit was reached.
67 */
68unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
69
70/* return the expected wait time in ms before the next event may occur,
71 * respecting frequency <freq>, and assuming there may already be some pending
72 * events. It returns zero if we can proceed immediately, otherwise the wait
73 * time, which will be rounded down 1ms for better accuracy, with a minimum
74 * of one ms.
75 */
76unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
77
Willy Tarreau7f062c42009-03-05 18:43:00 +010078#endif /* _PROTO_FREQ_CTR_H */
79
80/*
81 * Local variables:
82 * c-indent-level: 8
83 * c-basic-offset: 8
84 * End:
85 */