1 .\" $NetBSD: ratecheck.9,v 1.10 2007/02/20 08:26:35 wiz Exp $
3 .\" Copyright (c) 2000 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Christopher G. Demetriou.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd function to help implement rate-limited actions
39 .Fn ratecheck "struct timeval *lasttime" "const struct timeval *mininterval"
43 function provides a simple time interval check which can be used
44 when implementing time-based rate-limited actions.
45 If the difference between the current monotonically-increasing
50 is less than the value given by the
52 argument, zero is returned.
55 is set to the current time and a non-zero value is returned.
57 The motivation for implementing
59 was to provide a mechanism that could be used to add rate limiting to
60 diagnostic message output.
61 If printed too often, diagnostic messages can keep the system from
63 If the repeated messages can be caused by deliberate user action
64 or network events, they can be exploited to cause denial of system service.
66 Note that using a very short time interval (less than a second)
69 defeats the purpose of this function.
70 (It doesn't take much to flood a 9600 baud serial console with
71 output, for instance.)
73 Here is a simple example of use of the
78 * The following variables could be global, in a device softc, etc.,
79 * depending on the exact usage.
81 struct timeval drv_lasterr1time; /* time of last err1 message */
82 long drv_err1count; /* # of err1 errs since last msg */
83 struct timeval drv_lasterr2time; /* time of last err2 message */
84 long drv_err2count; /* # of err2 errs since last msg */
87 * The following variable will often be global or shared by all
88 * instances of a driver. It should be initialized, so it can be
89 * patched. Allowing it to be set via an option might be nice,
90 * but could lead to an insane proliferation of options.
92 struct timeval drv_errintvl = { 5, 0 }; /* 5 seconds */
94 /* error handling/reporting function */
96 drv_errhandler(int err1, int err2)
100 * Note that you should NOT use the same last-event
101 * time variable for dissimilar messages!
104 /* handle err1 condition */
108 if (ratecheck(\*[Am]drv_lasterr1notice,
109 \*[Am]drv_errinterval)) {
110 printf("drv: %ld err1 errors occurred",
116 /* handle err2 condition */
120 if (ratecheck(\*[Am]drv_lasterr2notice,
121 \*[Am]drv_errinterval)) {
122 printf("drv: %ld err2 errors occurred",
141 may not work as expected, if
143 is less than the hardware clock interrupt interval