2
include/proto/freq_ctr.h
3
This file contains macros and inline functions for frequency counters.
5
Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
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
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.
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
22
#ifndef _PROTO_FREQ_CTR_H
23
#define _PROTO_FREQ_CTR_H
25
#include <common/config.h>
26
#include <types/freq_ctr.h>
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
32
static inline void rotate_freq_ctr(struct freq_ctr *ctr)
34
ctr->prev_ctr = ctr->curr_ctr;
35
if (likely(now.tv_sec - ctr->curr_sec != 1)) {
36
/* we missed more than one second */
39
ctr->curr_sec = now.tv_sec;
40
ctr->curr_ctr = 0; /* leave it at the end to help gcc optimize it away */
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
47
static inline void update_freq_ctr(struct freq_ctr *ctr, unsigned int inc)
49
if (likely(ctr->curr_sec == now.tv_sec)) {
55
/* Note: later we may want to propagate the update to other counters */
58
/* Read a frequency counter taking history into account for missing time in
61
unsigned int read_freq_ctr(struct freq_ctr *ctr);
63
/* returns the number of remaining events that can occur on this freq counter
64
* while respecting <freq> and taking into account that <pend> events are
65
* already known to be pending. Returns 0 if limit was reached.
67
unsigned int freq_ctr_remain(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
69
/* return the expected wait time in ms before the next event may occur,
70
* respecting frequency <freq>, and assuming there may already be some pending
71
* events. It returns zero if we can proceed immediately, otherwise the wait
72
* time, which will be rounded down 1ms for better accuracy, with a minimum
75
unsigned int next_event_delay(struct freq_ctr *ctr, unsigned int freq, unsigned int pend);
77
#endif /* _PROTO_FREQ_CTR_H */