Split rate-checking routines into their own module for easier reuse.
This commit is contained in:
parent
e481a6bdeb
commit
44367b7199
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: files,v 1.917 2008/09/25 16:23:45 pooka Exp $
|
||||
# $NetBSD: files,v 1.918 2008/09/25 17:17:10 pooka Exp $
|
||||
|
||||
# @(#)files.newconf 7.5 (Berkeley) 5/10/93
|
||||
|
||||
|
@ -1397,6 +1397,7 @@ file kern/kern_pmf.c
|
|||
file kern/kern_proc.c
|
||||
file kern/kern_prot.c
|
||||
file kern/kern_ras.c
|
||||
file kern/kern_rate.c
|
||||
file kern/kern_resource.c
|
||||
file kern/kern_runq.c
|
||||
file kern/kern_rwlock.c
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
/* $NetBSD: kern_rate.c,v 1.1 2008/09/25 17:17:10 pooka Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2000, 2004, 2005, 2007, 2008 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Christopher G. Demetriou.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: kern_rate.c,v 1.1 2008/09/25 17:17:10 pooka Exp $");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
/*
|
||||
* ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
|
||||
* for usage and rationale.
|
||||
*/
|
||||
int
|
||||
ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
|
||||
{
|
||||
struct timeval tv, delta;
|
||||
int rv = 0;
|
||||
|
||||
getmicrouptime(&tv);
|
||||
timersub(&tv, lasttime, &delta);
|
||||
|
||||
/*
|
||||
* check for 0,0 is so that the message will be seen at least once,
|
||||
* even if interval is huge.
|
||||
*/
|
||||
if (timercmp(&delta, mininterval, >=) ||
|
||||
(lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
|
||||
*lasttime = tv;
|
||||
rv = 1;
|
||||
}
|
||||
|
||||
return (rv);
|
||||
}
|
||||
|
||||
/*
|
||||
* ppsratecheck(): packets (or events) per second limitation.
|
||||
*/
|
||||
int
|
||||
ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
|
||||
{
|
||||
struct timeval tv, delta;
|
||||
int rv;
|
||||
|
||||
getmicrouptime(&tv);
|
||||
timersub(&tv, lasttime, &delta);
|
||||
|
||||
/*
|
||||
* check for 0,0 is so that the message will be seen at least once.
|
||||
* if more than one second have passed since the last update of
|
||||
* lasttime, reset the counter.
|
||||
*
|
||||
* we do increment *curpps even in *curpps < maxpps case, as some may
|
||||
* try to use *curpps for stat purposes as well.
|
||||
*/
|
||||
if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
|
||||
delta.tv_sec >= 1) {
|
||||
*lasttime = tv;
|
||||
*curpps = 0;
|
||||
}
|
||||
if (maxpps < 0)
|
||||
rv = 1;
|
||||
else if (*curpps < maxpps)
|
||||
rv = 1;
|
||||
else
|
||||
rv = 0;
|
||||
|
||||
#if 1 /*DIAGNOSTIC?*/
|
||||
/* be careful about wrap-around */
|
||||
if (*curpps + 1 > *curpps)
|
||||
*curpps = *curpps + 1;
|
||||
#else
|
||||
/*
|
||||
* assume that there's not too many calls to this function.
|
||||
* not sure if the assumption holds, as it depends on *caller's*
|
||||
* behavior, not the behavior of this function.
|
||||
* IMHO it is wrong to make assumption on the caller's behavior,
|
||||
* so the above #if is #if 1, not #ifdef DIAGNOSTIC.
|
||||
*/
|
||||
*curpps = *curpps + 1;
|
||||
#endif
|
||||
|
||||
return (rv);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: kern_time.c,v 1.152 2008/09/23 16:44:49 christos Exp $ */
|
||||
/* $NetBSD: kern_time.c,v 1.153 2008/09/25 17:17:10 pooka Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2000, 2004, 2005, 2007, 2008 The NetBSD Foundation, Inc.
|
||||
|
@ -61,7 +61,7 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.152 2008/09/23 16:44:49 christos Exp $");
|
||||
__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.153 2008/09/25 17:17:10 pooka Exp $");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/resourcevar.h>
|
||||
|
@ -1368,79 +1368,3 @@ timer_intr(void *cookie)
|
|||
}
|
||||
mutex_spin_exit(&timer_lock);
|
||||
}
|
||||
|
||||
/*
|
||||
* ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
|
||||
* for usage and rationale.
|
||||
*/
|
||||
int
|
||||
ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
|
||||
{
|
||||
struct timeval tv, delta;
|
||||
int rv = 0;
|
||||
|
||||
getmicrouptime(&tv);
|
||||
timersub(&tv, lasttime, &delta);
|
||||
|
||||
/*
|
||||
* check for 0,0 is so that the message will be seen at least once,
|
||||
* even if interval is huge.
|
||||
*/
|
||||
if (timercmp(&delta, mininterval, >=) ||
|
||||
(lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
|
||||
*lasttime = tv;
|
||||
rv = 1;
|
||||
}
|
||||
|
||||
return (rv);
|
||||
}
|
||||
|
||||
/*
|
||||
* ppsratecheck(): packets (or events) per second limitation.
|
||||
*/
|
||||
int
|
||||
ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
|
||||
{
|
||||
struct timeval tv, delta;
|
||||
int rv;
|
||||
|
||||
getmicrouptime(&tv);
|
||||
timersub(&tv, lasttime, &delta);
|
||||
|
||||
/*
|
||||
* check for 0,0 is so that the message will be seen at least once.
|
||||
* if more than one second have passed since the last update of
|
||||
* lasttime, reset the counter.
|
||||
*
|
||||
* we do increment *curpps even in *curpps < maxpps case, as some may
|
||||
* try to use *curpps for stat purposes as well.
|
||||
*/
|
||||
if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
|
||||
delta.tv_sec >= 1) {
|
||||
*lasttime = tv;
|
||||
*curpps = 0;
|
||||
}
|
||||
if (maxpps < 0)
|
||||
rv = 1;
|
||||
else if (*curpps < maxpps)
|
||||
rv = 1;
|
||||
else
|
||||
rv = 0;
|
||||
|
||||
#if 1 /*DIAGNOSTIC?*/
|
||||
/* be careful about wrap-around */
|
||||
if (*curpps + 1 > *curpps)
|
||||
*curpps = *curpps + 1;
|
||||
#else
|
||||
/*
|
||||
* assume that there's not too many calls to this function.
|
||||
* not sure if the assumption holds, as it depends on *caller's*
|
||||
* behavior, not the behavior of this function.
|
||||
* IMHO it is wrong to make assumption on the caller's behavior,
|
||||
* so the above #if is #if 1, not #ifdef DIAGNOSTIC.
|
||||
*/
|
||||
*curpps = *curpps + 1;
|
||||
#endif
|
||||
|
||||
return (rv);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue