diff options
author | Jun-ichiro itojun Hagino <itojun@cvs.openbsd.org> | 2000-10-10 13:36:50 +0000 |
---|---|---|
committer | Jun-ichiro itojun Hagino <itojun@cvs.openbsd.org> | 2000-10-10 13:36:50 +0000 |
commit | 2bc25b1faf3d13c495afd17a2ffd62083dc347e8 (patch) | |
tree | 7bbbba22ea296c6702ce3feb283de25a33efed79 /sys/kern | |
parent | 6f653ae3ec4f5bc8bb4a1407a578f5974f6d3a51 (diff) |
implement ppsratecheck(9). from netbsd.
refer mono_time only once in ratecheck(9).
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/kern_time.c | 66 |
1 files changed, 62 insertions, 4 deletions
diff --git a/sys/kern/kern_time.c b/sys/kern/kern_time.c index 8ced1259dd9..955c7b5ffa6 100644 --- a/sys/kern/kern_time.c +++ b/sys/kern/kern_time.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_time.c,v 1.22 2000/07/07 15:19:04 art Exp $ */ +/* $OpenBSD: kern_time.c,v 1.23 2000/10/10 13:36:49 itojun Exp $ */ /* $NetBSD: kern_time.c,v 1.20 1996/02/18 11:57:06 fvdl Exp $ */ /* @@ -607,11 +607,14 @@ ratecheck(lasttime, mininterval) struct timeval *lasttime; const struct timeval *mininterval; { - struct timeval delta; + struct timeval tv, delta; int s, rv = 0; s = splclock(); - timersub(&mono_time, lasttime, &delta); + tv = mono_time; + splx(s); + + timersub(&tv, lasttime, &delta); /* * check for 0,0 is so that the message will be seen at least once, @@ -619,10 +622,65 @@ ratecheck(lasttime, mininterval) */ if (timercmp(&delta, mininterval, >=) || (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { - *lasttime = mono_time; + *lasttime = tv; rv = 1; } + + return (rv); +} + +/* + * ppsratecheck(): packets (or events) per second limitation. + */ +int +ppsratecheck(lasttime, curpps, maxpps) + struct timeval *lasttime; + int *curpps; + int maxpps; /* maximum pps allowed */ +{ + struct timeval tv, delta; + int s, rv; + + s = splclock(); + tv = mono_time; splx(s); + 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; + rv = 1; + } else 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); } |