blob: caa2b6bc4460ed8b0539f82fc227a6f75d14e6fb [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>
26#include <types/freq_ctr.h>
27
28/* Rotate a frequency counter when current period is over. Must not be called
29 * during a valid period. It is important that it correctly initializes a null
30 * area.
31 */
32static inline void rotate_freq_ctr(struct freq_ctr *ctr)
33{
34 ctr->prev_ctr = ctr->curr_ctr;
35 if (likely(now.tv_sec - ctr->curr_sec != 1)) {
36 /* we missed more than one second */
37 ctr->prev_ctr = 0;
38 }
39 ctr->curr_sec = now.tv_sec;
40 ctr->curr_ctr = 0; /* leave it at the end to help gcc optimize it away */
41}
42
43/* Update a frequency counter by <inc> incremental units. It is automatically
44 * rotated if the period is over. It is important that it correctly initializes
45 * a null area.
46 */
47static inline void update_freq_ctr(struct freq_ctr *ctr, unsigned int inc)
48{
49 if (likely(ctr->curr_sec == now.tv_sec)) {
50 ctr->curr_ctr += inc;
51 return;
52 }
53 rotate_freq_ctr(ctr);
54 ctr->curr_ctr = inc;
55 /* Note: later we may want to propagate the update to other counters */
56}
57
58/* Read a frequency counter taking history into account for missing time in
59 * current period.
60 */
61unsigned int read_freq_ctr(struct freq_ctr *ctr);
62
63#endif /* _PROTO_FREQ_CTR_H */
64
65/*
66 * Local variables:
67 * c-indent-level: 8
68 * c-basic-offset: 8
69 * End:
70 */