diff options
Diffstat (limited to 'usr.bin/systat/pigs.c')
-rw-r--r-- | usr.bin/systat/pigs.c | 79 |
1 files changed, 34 insertions, 45 deletions
diff --git a/usr.bin/systat/pigs.c b/usr.bin/systat/pigs.c index b17c9d8ab3e..ba8f7ce65b3 100644 --- a/usr.bin/systat/pigs.c +++ b/usr.bin/systat/pigs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pigs.c,v 1.11 2001/12/07 07:57:35 pvalchev Exp $ */ +/* $OpenBSD: pigs.c,v 1.12 2001/12/07 09:18:08 deraadt Exp $ */ /* $NetBSD: pigs.c,v 1.3 1995/04/29 05:54:50 cgd Exp $ */ /*- @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)pigs.c 8.2 (Berkeley) 9/23/93"; #endif -static char rcsid[] = "$OpenBSD: pigs.c,v 1.11 2001/12/07 07:57:35 pvalchev Exp $"; +static char rcsid[] = "$OpenBSD: pigs.c,v 1.12 2001/12/07 09:18:08 deraadt Exp $"; #endif /* not lint */ /* @@ -47,6 +47,7 @@ static char rcsid[] = "$OpenBSD: pigs.c,v 1.11 2001/12/07 07:57:35 pvalchev Exp #include <sys/param.h> #include <sys/dkstat.h> +#include <sys/resource.h> #include <sys/dir.h> #include <sys/time.h> #include <sys/proc.h> @@ -54,7 +55,6 @@ static char rcsid[] = "$OpenBSD: pigs.c,v 1.11 2001/12/07 07:57:35 pvalchev Exp #include <curses.h> #include <math.h> -#include <nlist.h> #include <pwd.h> #include <err.h> #include <stdlib.h> @@ -72,7 +72,6 @@ static struct p_times { } *pt; static long stime[CPUSTATES]; -static int fscale; static double lccpu; WINDOW * @@ -115,7 +114,7 @@ showpigs() total = 1.0; factor = 50.0/total; - qsort(pt, nproc + 1, sizeof (struct p_times), compar); + qsort(pt, nproc + 1, sizeof (struct p_times), compar); y = 1; i = nproc + 1; if (i > wnd->_maxy-1) @@ -124,8 +123,7 @@ showpigs() if (pt[k].pt_kp == NULL) { uname = ""; pname = "<idle>"; - } - else { + } else { ep = &pt[k].pt_kp->kp_eproc; uname = user_from_uid(ep->e_ucred.cr_uid, 0); pname = pt[k].pt_kp->kp_proc.p_comm; @@ -142,38 +140,27 @@ showpigs() wmove(wnd, y, 0); wclrtobot(wnd); } -static struct nlist namelist[] = { -#define X_FIRST 0 -#define X_CPTIME 0 - { "_cp_time" }, -#define X_CCPU 1 - { "_ccpu" }, -#define X_FSCALE 2 - { "_fscale" }, - - { "" } -}; +struct loadavg sysload; int initpigs() { + static int sysload_mib[] = {CTL_VM, VM_LOADAVG}; + static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME }; + static int ccpu_mib[] = { CTL_KERN, KERN_CCPU }; + size_t size; fixpt_t ccpu; - int ret; - - if (namelist[X_FIRST].n_type == 0) { - if ((ret = kvm_nlist(kd, namelist)) == -1) - errx(1, "%s", kvm_geterr(kd)); - else if (ret) - nlisterr(namelist); - if (namelist[X_FIRST].n_type == 0) { - error("namelist failed"); - return(0); - } - } - KREAD(NPTR(X_CPTIME), stime, sizeof (stime)); - NREAD(X_CCPU, &ccpu, LONG); - NREAD(X_FSCALE, &fscale, LONG); - lccpu = log((double) ccpu / fscale); + + size = sizeof(stime); + (void) sysctl(cp_time_mib, 2, &stime, &size, NULL, 0); + + size = sizeof(sysload); + (void) sysctl(sysload_mib, 2, &sysload, &size, NULL, 0); + + size = sizeof(ccpu); + (void) sysctl(ccpu_mib, 2, &ccpu, &size, NULL, 0); + + lccpu = log((double) ccpu / sysload.fscale); return(1); } @@ -181,17 +168,17 @@ initpigs() void fetchpigs() { - int i; - float time; - struct proc *pp; - float *pctp; + static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME }; + static int lastnproc = 0; struct kinfo_proc *kpp; long ctime[CPUSTATES]; + float time; double t; - static int lastnproc = 0; + int i; + size_t size; + struct proc *pp; + float *pctp; - if (namelist[X_FIRST].n_type == 0) - return; if ((kpp = kvm_getprocs(kd, KERN_PROC_KTHREAD, 0, &nproc)) == NULL) { error("%s", kvm_geterr(kd)); if (pt) @@ -203,7 +190,7 @@ fetchpigs() if ((pt = malloc((nproc + 1) * sizeof(struct p_times))) == NULL) { error("Out of memory"); - die(0); + die(); } } lastnproc = nproc; @@ -218,13 +205,15 @@ fetchpigs() if (time == 0 || (pp->p_flag & P_INMEM) == 0) *pctp = 0; else - *pctp = ((double) pp->p_pctcpu / - fscale) / (1.0 - exp(time * lccpu)); + *pctp = ((double) pp->p_pctcpu / + sysload.fscale) / (1.0 - exp(time * lccpu)); } /* * and for the imaginary "idle" process */ - KREAD(NPTR(X_CPTIME), ctime, sizeof (ctime)); + size = sizeof(ctime); + (void) sysctl(cp_time_mib, 2, &ctime, &size, NULL, 0); + t = 0; for (i = 0; i < CPUSTATES; i++) t += ctime[i] - stime[i]; |