From e831f308767ce32664a22696b5fb4da7fb4d071b Mon Sep 17 00:00:00 2001 From: Garrett Wollman Date: Wed, 11 Dec 1996 19:11:52 +0000 Subject: [PATCH] Convert this program to use sysctl rather than kvm for some of its statistics. Unfortunately, the coverage of sysctl isn't good enough to do all of them :-( . --- libexec/rpc.rstatd/rstat_proc.c | 180 ++++++++++++-------------------- 1 file changed, 69 insertions(+), 111 deletions(-) diff --git a/libexec/rpc.rstatd/rstat_proc.c b/libexec/rpc.rstatd/rstat_proc.c index 613ce51cc47b..451e6c29ffb4 100644 --- a/libexec/rpc.rstatd/rstat_proc.c +++ b/libexec/rpc.rstatd/rstat_proc.c @@ -29,7 +29,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)rpc.rstatd.c 1.1 86/09/25 Copyr 1984 Sun Micro";*/ /*static char sccsid[] = "from: @(#)rstat_proc.c 2.2 88/08/01 4.0 RPCSRC";*/ -static char rcsid[] = "$Id: rstat_proc.c,v 1.2 1994/10/15 13:39:54 davidg Exp $"; +static char rcsid[] = "$Id: rstat_proc.c,v 1.3 1995/05/30 05:46:28 rgrimes Exp $"; #endif /* @@ -38,6 +38,15 @@ static char rcsid[] = "$Id: rstat_proc.c,v 1.2 1994/10/15 13:39:54 davidg Exp $" * Copyright (c) 1984 by Sun Microsystems, Inc. */ +#include +#include +#include +#include +#include +#include +#include +#include + #include #include #include @@ -48,17 +57,10 @@ static char rcsid[] = "$Id: rstat_proc.c,v 1.2 1994/10/15 13:39:54 davidg Exp $" #include #include -#include -#include -#include -#include -#ifdef BSD -#include -#include -#else -#include -#endif #include +#include + +#include #undef FSHIFT /* Use protocol's shift and scale values */ #undef FSCALE @@ -74,21 +76,12 @@ struct nlist nl[] = { { "_cp_time" }, #define X_CNT 1 { "_cnt" }, -#define X_IFNET 2 - { "_ifnet" }, -#define X_DKXFER 3 +#define X_DKXFER 2 { "_dk_xfer" }, -#define X_BOOTTIME 4 - { "_boottime" }, -#define X_HZ 5 - { "_hz" }, -#ifdef vax -#define X_AVENRUN 6 - { "_avenrun" }, -#endif - "", +#define X_DKNDRIVE 3 + { "_dk_ndrive" }, + { "" }, }; -int firstifnet, numintfs; /* chain of ethernet interfaces */ int stats_service(); extern int from_inetd; @@ -106,49 +99,15 @@ static stat_is_init = 0; static kvm_t *kd; extern int errno; -#if defined(BSD) static int cp_time_xlat[RSTAT_CPUSTATES] = { CP_USER, CP_NICE, CP_SYS, CP_IDLE }; static long bsd_cp_time[CPUSTATES]; -#endif #ifndef FSCALE #define FSCALE (1 << 8) #endif -#ifndef BSD -/* - * BSD has the kvm facility for getting info from the - * kernel. If you aren't on BSD, this surfices. - */ -int kmem; - -kvm_read(kd, off, addr, size) - void * kd; - unsigned long off, size; - char *addr; -{ - int len; - if (lseek(kmem, (long)off, 0) == -1) - return(-1); - return(read(kmem, addr, size)); -} - -kvm_nlist(kd, nl) - void * kd; - struct nlist *nl; -{ - int n = nlist("/vmunix", nl); - if (nl[0].n_value == 0) - return(n); - - if ((kmem = open("/dev/kmem", 0)) < 0) - return(-1); - return(0); -} -#endif - stat_init() { stat_is_init = 1; @@ -213,10 +172,14 @@ void updatestat() { int off, i, hz; + struct clockinfo clockrate; struct vmmeter cnt; - struct ifnet ifnet; + struct ifmibdata ifmd; double avrun[3]; struct timeval tm, btm; + int mib[6]; + size_t len; + int ifcount; #ifdef DEBUG fprintf(stderr, "entering updatestat\n"); @@ -234,11 +197,15 @@ updatestat() } sincelastreq++; - if (kvm_read(kd, (long)nl[X_HZ].n_value, (char *)&hz, sizeof hz) != sizeof hz) { - syslog(LOG_ERR, "rstat: can't read hz from kmem\n"); + mib[0] = CTL_KERN; + mib[1] = KERN_CLOCKRATE; + len = sizeof clockrate; + if (sysctl(mib, 2, &clockrate, &len, 0, 0) < 0) { + syslog(LOG_ERR, "sysctl(kern.clockrate): %m"); exit(1); } -#if defined(BSD) + hz = clockrate.hz; + if (kvm_read(kd, (long)nl[X_CPTIME].n_value, (char *)bsd_cp_time, sizeof(bsd_cp_time)) != sizeof(bsd_cp_time)) { syslog(LOG_ERR, "rstat: can't read cp_time from kmem\n"); @@ -246,30 +213,21 @@ updatestat() } for(i = 0; i < RSTAT_CPUSTATES ; i++) stats_all.s1.cp_time[i] = bsd_cp_time[cp_time_xlat[i]]; -#else - if (kvm_read(kd, (long)nl[X_CPTIME].n_value, (char *)stats_all.s1.cp_time, sizeof (stats_all.s1.cp_time)) - != sizeof (stats_all.s1.cp_time)) { - syslog(LOG_ERR, "rstat: can't read cp_time from kmem\n"); - exit(1); - } -#endif -#ifdef vax - if (kvm_read(kd, (long)nl[X_AVENRUN].n_value, (char *)avrun, sizeof (avrun)) != sizeof (avrun)) { - syslog(LOG_ERR, "rstat: can't read avenrun from kmem\n"); - exit(1); - } -#endif -#ifdef BSD + (void)getloadavg(avrun, sizeof(avrun) / sizeof(avrun[0])); -#endif + stats_all.s2.avenrun[0] = avrun[0] * FSCALE; stats_all.s2.avenrun[1] = avrun[1] * FSCALE; stats_all.s2.avenrun[2] = avrun[2] * FSCALE; - if (kvm_read(kd, (long)nl[X_BOOTTIME].n_value, (char *)&btm, sizeof (stats_all.s2.boottime)) - != sizeof (stats_all.s2.boottime)) { - syslog(LOG_ERR, "rstat: can't read boottime from kmem\n"); + + mib[0] = CTL_KERN; + mib[1] = KERN_BOOTTIME; + len = sizeof btm; + if (sysctl(mib, 2, &btm, &len, 0, 0) < 0) { + syslog(LOG_ERR, "sysctl(kern.boottime): %m"); exit(1); } + stats_all.s2.boottime.tv_sec = btm.tv_sec; stats_all.s2.boottime.tv_usec = btm.tv_usec; @@ -279,6 +237,7 @@ updatestat() stats_all.s1.cp_time[1], stats_all.s1.cp_time[2], stats_all.s1.cp_time[3]); #endif + /* XXX - should use sysctl */ if (kvm_read(kd, (long)nl[X_CNT].n_value, (char *)&cnt, sizeof cnt) != sizeof cnt) { syslog(LOG_ERR, "rstat: can't read cnt from kmem\n"); exit(1); @@ -293,28 +252,45 @@ updatestat() hz*(tm.tv_usec - btm.tv_usec)/1000000; stats_all.s2.v_swtch = cnt.v_swtch; + /* XXX - should use sysctl */ if (kvm_read(kd, (long)nl[X_DKXFER].n_value, (char *)stats_all.s1.dk_xfer, sizeof (stats_all.s1.dk_xfer)) != sizeof (stats_all.s1.dk_xfer)) { syslog(LOG_ERR, "rstat: can't read dk_xfer from kmem\n"); exit(1); } + mib[0] = CTL_NET; + mib[1] = PF_LINK; + mib[2] = NETLINK_GENERIC; + mib[3] = IFMIB_SYSTEM; + mib[4] = IFMIB_IFCOUNT; + len = sizeof ifcount; + if (sysctl(mib, 5, &ifcount, &len, 0, 0) < 0) { + syslog(LOG_ERR, "sysctl(net.link.generic.system.ifcount): %m"); + exit(1); + } + stats_all.s1.if_ipackets = 0; stats_all.s1.if_opackets = 0; stats_all.s1.if_ierrors = 0; stats_all.s1.if_oerrors = 0; stats_all.s1.if_collisions = 0; - for (off = firstifnet, i = 0; off && i < numintfs; i++) { - if (kvm_read(kd, off, (char *)&ifnet, sizeof ifnet) != sizeof ifnet) { - syslog(LOG_ERR, "rstat: can't read ifnet from kmem\n"); + for (i = 1; i <= ifcount; i++) { + len = sizeof ifmd; + mib[3] = IFMIB_IFDATA; + mib[4] = i; + mib[5] = IFDATA_GENERAL; + if (sysctl(mib, 6, &ifmd, &len, 0, 0) < 0) { + syslog(LOG_ERR, "sysctl(net.link.ifdata.%d.general)" + ": %m", i); exit(1); } - stats_all.s1.if_ipackets += ifnet.if_data.ifi_ipackets; - stats_all.s1.if_opackets += ifnet.if_data.ifi_opackets; - stats_all.s1.if_ierrors += ifnet.if_data.ifi_ierrors; - stats_all.s1.if_oerrors += ifnet.if_data.ifi_oerrors; - stats_all.s1.if_collisions += ifnet.if_data.ifi_collisions; - off = (int) ifnet.if_next; + + stats_all.s1.if_ipackets += ifmd.ifmd_data.ifi_ipackets; + stats_all.s1.if_opackets += ifmd.ifmd_data.ifi_opackets; + stats_all.s1.if_ierrors += ifmd.ifmd_data.ifi_ierrors; + stats_all.s1.if_oerrors += ifmd.ifmd_data.ifi_oerrors; + stats_all.s1.if_collisions += ifmd.ifmd_data.ifi_collisions; } gettimeofday((struct timeval *)&stats_all.s3.curtime, (struct timezone *) 0); @@ -338,22 +314,6 @@ setup() syslog(LOG_ERR, "rstatd: Can't get namelist. %d", en); exit (1); } - - if (kvm_read(kd, (long)nl[X_IFNET].n_value, &firstifnet, - sizeof(int)) != sizeof(int)) { - syslog(LOG_ERR, "rstat: can't read firstifnet from kmem\n"); - exit(1); - } - - numintfs = 0; - for (off = firstifnet; off;) { - if (kvm_read(kd, off, (char *)&ifnet, sizeof ifnet) != sizeof ifnet) { - syslog(LOG_ERR, "rstat: can't read ifnet from kmem\n"); - exit(1); - } - numintfs++; - off = (int) ifnet.if_next; - } } /* @@ -362,21 +322,19 @@ setup() havedisk() { int i, cnt; - long xfer[DK_NDRIVE]; + int dk_ndrive; if (kvm_nlist(kd, nl) != 0) { syslog(LOG_ERR, "rstatd: Can't get namelist.(d)"); exit (1); } - if (kvm_read(kd, (long)nl[X_DKXFER].n_value, (char *)xfer, sizeof xfer)!= sizeof xfer) { + if (kvm_read(kd, (long)nl[X_DKNDRIVE].n_value, (char *)&dk_ndrive, + sizeof dk_ndrive)!= sizeof dk_ndrive) { syslog(LOG_ERR, "rstat: can't read kmem\n"); exit(1); } - cnt = 0; - for (i=0; i < DK_NDRIVE; i++) - cnt += xfer[i]; - return (cnt != 0); + return (dk_ndrive != 0); } void