summaryrefslogtreecommitdiffstats
path: root/usr.bin/netstat
diff options
context:
space:
mode:
authorbde <bde@FreeBSD.org>2007-02-27 04:54:33 +0000
committerbde <bde@FreeBSD.org>2007-02-27 04:54:33 +0000
commit3ee0d09a443f80fc57dc791114562e66b6856c71 (patch)
treeff742b8b2e502413667eeed2507cb703f839ea09 /usr.bin/netstat
parent05b92097cb751ac3e6ba126eed272f954f9c7210 (diff)
downloadFreeBSD-src-3ee0d09a443f80fc57dc791114562e66b6856c71.zip
FreeBSD-src-3ee0d09a443f80fc57dc791114562e66b6856c71.tar.gz
Use a periodic itimer instead of repeated calls to alarm() in
sidewaysintpr(). This increases the accuracy of the per-interval counts when they are interpreted as rates. Repeated calls to alarm(n) give an average interval that is about 2 ticks larger than n and has a large variance. Periodic itimers normally get the average almost right but have similarly large variance (due to scheduling delays). Statistics utilities should use clock_gettime() to determine the actual interval, but it is still useful to maximize the accuracy of the interval, especially for cases like netstat -w where counts are displayed so the program cannot hide the inaccuracy in a rate conversion.
Diffstat (limited to 'usr.bin/netstat')
-rw-r--r--usr.bin/netstat/if.c16
1 files changed, 9 insertions, 7 deletions
diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c
index 80f7be0..280e1bf 100644
--- a/usr.bin/netstat/if.c
+++ b/usr.bin/netstat/if.c
@@ -531,6 +531,7 @@ sidewaysintpr(unsigned interval1, u_long off)
struct ifnet ifnet;
u_long firstifnet;
struct ifnethead ifnethead;
+ struct itimerval interval_it;
struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
int line;
int oldmask, first;
@@ -583,7 +584,10 @@ sidewaysintpr(unsigned interval1, u_long off)
(void)signal(SIGALRM, catchalarm);
signalled = NO;
- (void)alarm(interval1);
+ interval_it.it_interval.tv_sec = interval1;
+ interval_it.it_interval.tv_usec = 0;
+ interval_it.it_value = interval_it.it_interval;
+ setitimer(ITIMER_REAL, &interval_it, NULL);
first = 1;
banner:
printf("%17s %14s %16s", "input",
@@ -668,12 +672,10 @@ loop:
putchar('\n');
fflush(stdout);
oldmask = sigblock(sigmask(SIGALRM));
- if (! signalled) {
+ while (!signalled)
sigpause(0);
- }
- sigsetmask(oldmask);
signalled = NO;
- (void)alarm(interval1);
+ sigsetmask(oldmask);
line++;
first = 0;
if (line == 21)
@@ -684,8 +686,8 @@ loop:
}
/*
- * Called if an interval expires before sidewaysintpr has completed a loop.
- * Sets a flag to not wait for the alarm.
+ * Set a flag to indicate that a signal from the periodic itimer has been
+ * caught.
*/
static void
catchalarm(int signo __unused)
OpenPOWER on IntegriCloud