kern_rate.c revision 1.1.6.2 1 1.1.6.2 haad /* $NetBSD: kern_rate.c,v 1.1.6.2 2008/10/19 22:17:27 haad Exp $ */
2 1.1.6.2 haad
3 1.1.6.2 haad /*-
4 1.1.6.2 haad * Copyright (c) 2000, 2004, 2005, 2007, 2008 The NetBSD Foundation, Inc.
5 1.1.6.2 haad * All rights reserved.
6 1.1.6.2 haad *
7 1.1.6.2 haad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.6.2 haad * by Christopher G. Demetriou.
9 1.1.6.2 haad *
10 1.1.6.2 haad * Redistribution and use in source and binary forms, with or without
11 1.1.6.2 haad * modification, are permitted provided that the following conditions
12 1.1.6.2 haad * are met:
13 1.1.6.2 haad * 1. Redistributions of source code must retain the above copyright
14 1.1.6.2 haad * notice, this list of conditions and the following disclaimer.
15 1.1.6.2 haad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.6.2 haad * notice, this list of conditions and the following disclaimer in the
17 1.1.6.2 haad * documentation and/or other materials provided with the distribution.
18 1.1.6.2 haad *
19 1.1.6.2 haad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.6.2 haad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.6.2 haad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.6.2 haad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.6.2 haad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.6.2 haad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.6.2 haad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.6.2 haad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.6.2 haad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.6.2 haad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.6.2 haad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.6.2 haad */
31 1.1.6.2 haad
32 1.1.6.2 haad #include <sys/cdefs.h>
33 1.1.6.2 haad __KERNEL_RCSID(0, "$NetBSD: kern_rate.c,v 1.1.6.2 2008/10/19 22:17:27 haad Exp $");
34 1.1.6.2 haad
35 1.1.6.2 haad #include <sys/param.h>
36 1.1.6.2 haad #include <sys/time.h>
37 1.1.6.2 haad
38 1.1.6.2 haad /*
39 1.1.6.2 haad * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
40 1.1.6.2 haad * for usage and rationale.
41 1.1.6.2 haad */
42 1.1.6.2 haad int
43 1.1.6.2 haad ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
44 1.1.6.2 haad {
45 1.1.6.2 haad struct timeval tv, delta;
46 1.1.6.2 haad int rv = 0;
47 1.1.6.2 haad
48 1.1.6.2 haad getmicrouptime(&tv);
49 1.1.6.2 haad timersub(&tv, lasttime, &delta);
50 1.1.6.2 haad
51 1.1.6.2 haad /*
52 1.1.6.2 haad * check for 0,0 is so that the message will be seen at least once,
53 1.1.6.2 haad * even if interval is huge.
54 1.1.6.2 haad */
55 1.1.6.2 haad if (timercmp(&delta, mininterval, >=) ||
56 1.1.6.2 haad (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
57 1.1.6.2 haad *lasttime = tv;
58 1.1.6.2 haad rv = 1;
59 1.1.6.2 haad }
60 1.1.6.2 haad
61 1.1.6.2 haad return (rv);
62 1.1.6.2 haad }
63 1.1.6.2 haad
64 1.1.6.2 haad /*
65 1.1.6.2 haad * ppsratecheck(): packets (or events) per second limitation.
66 1.1.6.2 haad */
67 1.1.6.2 haad int
68 1.1.6.2 haad ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
69 1.1.6.2 haad {
70 1.1.6.2 haad struct timeval tv, delta;
71 1.1.6.2 haad int rv;
72 1.1.6.2 haad
73 1.1.6.2 haad getmicrouptime(&tv);
74 1.1.6.2 haad timersub(&tv, lasttime, &delta);
75 1.1.6.2 haad
76 1.1.6.2 haad /*
77 1.1.6.2 haad * check for 0,0 is so that the message will be seen at least once.
78 1.1.6.2 haad * if more than one second have passed since the last update of
79 1.1.6.2 haad * lasttime, reset the counter.
80 1.1.6.2 haad *
81 1.1.6.2 haad * we do increment *curpps even in *curpps < maxpps case, as some may
82 1.1.6.2 haad * try to use *curpps for stat purposes as well.
83 1.1.6.2 haad */
84 1.1.6.2 haad if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
85 1.1.6.2 haad delta.tv_sec >= 1) {
86 1.1.6.2 haad *lasttime = tv;
87 1.1.6.2 haad *curpps = 0;
88 1.1.6.2 haad }
89 1.1.6.2 haad if (maxpps < 0)
90 1.1.6.2 haad rv = 1;
91 1.1.6.2 haad else if (*curpps < maxpps)
92 1.1.6.2 haad rv = 1;
93 1.1.6.2 haad else
94 1.1.6.2 haad rv = 0;
95 1.1.6.2 haad
96 1.1.6.2 haad #if 1 /*DIAGNOSTIC?*/
97 1.1.6.2 haad /* be careful about wrap-around */
98 1.1.6.2 haad if (*curpps + 1 > *curpps)
99 1.1.6.2 haad *curpps = *curpps + 1;
100 1.1.6.2 haad #else
101 1.1.6.2 haad /*
102 1.1.6.2 haad * assume that there's not too many calls to this function.
103 1.1.6.2 haad * not sure if the assumption holds, as it depends on *caller's*
104 1.1.6.2 haad * behavior, not the behavior of this function.
105 1.1.6.2 haad * IMHO it is wrong to make assumption on the caller's behavior,
106 1.1.6.2 haad * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
107 1.1.6.2 haad */
108 1.1.6.2 haad *curpps = *curpps + 1;
109 1.1.6.2 haad #endif
110 1.1.6.2 haad
111 1.1.6.2 haad return (rv);
112 1.1.6.2 haad }
113