Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.20
      1 /*	$NetBSD: vmstat.c,v 1.20 1999/09/30 13:16:27 soren Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1983, 1989, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 1/12/94";
     40 #endif
     41 __RCSID("$NetBSD: vmstat.c,v 1.20 1999/09/30 13:16:27 soren Exp $");
     42 #endif /* not lint */
     43 
     44 /*
     45  * Cursed vmstat -- from Robert Elz.
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/dkstat.h>
     50 #include <sys/buf.h>
     51 #include <sys/stat.h>
     52 #include <sys/time.h>
     53 #include <sys/user.h>
     54 #include <sys/proc.h>
     55 #include <sys/namei.h>
     56 #include <sys/sysctl.h>
     57 
     58 #include <vm/vm.h>
     59 
     60 #include <uvm/uvm_extern.h>
     61 
     62 #include <ctype.h>
     63 #include <err.h>
     64 #include <nlist.h>
     65 #include <paths.h>
     66 #include <signal.h>
     67 #include <stdlib.h>
     68 #include <string.h>
     69 #include <utmp.h>
     70 #include <unistd.h>
     71 
     72 #include "systat.h"
     73 #include "extern.h"
     74 
     75 static struct Info {
     76 	long	time[CPUSTATES];
     77 	struct	uvmexp uvmexp;
     78 	struct	vmtotal Total;
     79 	struct	nchstats nchstats;
     80 	long	nchcount;
     81 	long	*intrcnt;
     82 } s, s1, s2, z;
     83 
     84 #include "dkstats.h"
     85 extern struct _disk	cur;
     86 
     87 
     88 #define	cnt s.Cnt
     89 #define oldcnt s1.Cnt
     90 #define	total s.Total
     91 #define	nchtotal s.nchstats
     92 #define	oldnchtotal s1.nchstats
     93 
     94 static	enum state { BOOT, TIME, RUN } state = TIME;
     95 
     96 static void allocinfo __P((struct Info *));
     97 static void copyinfo __P((struct Info *, struct Info *));
     98 static float cputime __P((int));
     99 static void dinfo __P((int, int));
    100 static void getinfo __P((struct Info *, enum state));
    101 static void putint __P((int, int, int, int));
    102 static void putfloat __P((double, int, int, int, int, int));
    103 static int ucount __P((void));
    104 
    105 static	int ut;
    106 static	char buf[26];
    107 static	time_t t;
    108 static	double etime;
    109 static	float hertz;
    110 static	int nintr;
    111 static	long *intrloc;
    112 static	char **intrname;
    113 static	int nextintsrow;
    114 
    115 struct	utmp utmp;
    116 
    117 WINDOW *
    118 openkre()
    119 {
    120 
    121 	ut = open(_PATH_UTMP, O_RDONLY);
    122 	if (ut < 0)
    123 		error("No utmp");
    124 	return (stdscr);
    125 }
    126 
    127 void
    128 closekre(w)
    129 	WINDOW *w;
    130 {
    131 
    132 	(void) close(ut);
    133 	if (w == NULL)
    134 		return;
    135 	wclear(w);
    136 	wrefresh(w);
    137 }
    138 
    139 
    140 static struct nlist namelist[] = {
    141 #define X_CPTIME	0
    142 	{ "_cp_time" },
    143 #define X_TOTAL		1
    144 	{ "_total" },
    145 #define	X_NCHSTATS	2
    146 	{ "_nchstats" },
    147 #define	X_INTRNAMES	3
    148 	{ "_intrnames" },
    149 #define	X_EINTRNAMES	4
    150 	{ "_eintrnames" },
    151 #define	X_INTRCNT	5
    152 	{ "_intrcnt" },
    153 #define	X_EINTRCNT	6
    154 	{ "_eintrcnt" },
    155 	{ "" },
    156 };
    157 
    158 /*
    159  * These constants define where the major pieces are laid out
    160  */
    161 #define STATROW		 0	/* uses 1 row and 68 cols */
    162 #define STATCOL		 2
    163 #define MEMROW		 2	/* uses 4 rows and 31 cols */
    164 #define MEMCOL		 0
    165 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
    166 #define PAGECOL		36
    167 #define INTSROW		 2	/* uses all rows to bottom and 17 cols */
    168 #define INTSCOL		63
    169 #define PROCSROW	 7	/* uses 2 rows and 20 cols */
    170 #define PROCSCOL	 0
    171 #define GENSTATROW	 7	/* uses 2 rows and 30 cols */
    172 #define GENSTATCOL	20
    173 #define VMSTATROW	 7	/* uses 17 rows and 12 cols */
    174 #define VMSTATCOL	48
    175 #define GRAPHROW	10	/* uses 3 rows and 51 cols */
    176 #define GRAPHCOL	 0
    177 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
    178 #define NAMEICOL	 0
    179 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
    180 #define DISKCOL		 0
    181 
    182 #define	DRIVESPACE	 9	/* max # for space */
    183 
    184 #if DK_NDRIVE > DRIVESPACE
    185 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
    186 #else
    187 #define	MAXDRIVES	DK_NDRIVE	 /* max # to display */
    188 #endif
    189 
    190 int
    191 initkre()
    192 {
    193 	char *intrnamebuf, *cp;
    194 	int i;
    195 	static int once = 0;
    196 	extern gid_t egid;
    197 
    198 	if (namelist[0].n_type == 0) {
    199 		if (kvm_nlist(kd, namelist)) {
    200 			nlisterr(namelist);
    201 			return(0);
    202 		}
    203 		if (namelist[0].n_type == 0) {
    204 			error("No namelist");
    205 			return(0);
    206 		}
    207 	}
    208 	hertz = stathz ? stathz : hz;
    209 	if (! dkinit(1, egid))
    210 		return(0);
    211 	if (dk_ndrive && !once) {
    212 #define	allocate(e, t) \
    213 	s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    214 	s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    215 	s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    216 	z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
    217 		once = 1;
    218 #undef allocate
    219 	}
    220 	if (nintr == 0) {
    221 		nintr = (namelist[X_EINTRCNT].n_value -
    222 			namelist[X_INTRCNT].n_value) / sizeof (long);
    223 		intrloc = calloc(nintr, sizeof (long));
    224 		intrname = calloc(nintr, sizeof (long));
    225 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
    226 			namelist[X_INTRNAMES].n_value);
    227 		if (intrnamebuf == 0 || intrname == 0 || intrloc == 0) {
    228 			error("Out of memory\n");
    229 			if (intrnamebuf)
    230 				free(intrnamebuf);
    231 			if (intrname)
    232 				free(intrname);
    233 			if (intrloc)
    234 				free(intrloc);
    235 			nintr = 0;
    236 			return(0);
    237 		}
    238 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
    239 			NVAL(X_INTRNAMES));
    240 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
    241 			intrname[i] = cp;
    242 			cp += strlen(cp) + 1;
    243 		}
    244 		nextintsrow = INTSROW + 2;
    245 		allocinfo(&s);
    246 		allocinfo(&s1);
    247 		allocinfo(&s2);
    248 		allocinfo(&z);
    249 	}
    250 	getinfo(&s2, RUN);
    251 	copyinfo(&s2, &s1);
    252 	return(1);
    253 }
    254 
    255 void
    256 fetchkre()
    257 {
    258 	time_t now;
    259 
    260 	time(&now);
    261 	strcpy(buf, ctime(&now));
    262 	buf[19] = '\0';
    263 	getinfo(&s, state);
    264 }
    265 
    266 void
    267 labelkre()
    268 {
    269 	int i, j;
    270 
    271 	clear();
    272 	mvprintw(STATROW, STATCOL + 4, "users    Load");
    273 	mvprintw(MEMROW, MEMCOL,     "          memory totals (in KB)");
    274 	mvprintw(MEMROW + 1, MEMCOL, "         real   virtual    free");
    275 	mvprintw(MEMROW + 2, MEMCOL, "Active");
    276 	mvprintw(MEMROW + 3, MEMCOL, "All");
    277 
    278 	mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
    279 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    280 	mvprintw(PAGEROW + 2, PAGECOL, "ops");
    281 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    282 
    283 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
    284 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
    285 
    286 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
    287 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
    288 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
    289 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
    290 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
    291 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
    292 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
    293 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
    294 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
    295 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
    296 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
    297 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
    298 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
    299 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
    300 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
    301 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
    302 	if (LINES - 1 > VMSTATROW + 16)
    303 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
    304 
    305 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
    306 
    307 	mvprintw(GRAPHROW, GRAPHCOL,
    308 		"    . %% Sys    . %% User    . %% Nice    . %% Idle");
    309 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
    310 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    311 		"|    |    |    |    |    |    |    |    |    |    |");
    312 
    313 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    314 	mvprintw(NAMEIROW + 1, NAMEICOL,
    315 		"    Calls     hits    %%     hits     %%");
    316 	mvprintw(DISKROW, DISKCOL, "Discs");
    317 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
    318 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
    319 	mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
    320 	mvprintw(DISKROW + 4, DISKCOL, "  sec");
    321 	j = 0;
    322 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
    323 		if (dk_select[i]) {
    324 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
    325 				" %4.4s", dr_name[j]);
    326 			j++;
    327 		}
    328 	for (i = 0; i < nintr; i++) {
    329 		if (intrloc[i] == 0)
    330 			continue;
    331 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
    332 	}
    333 }
    334 
    335 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    336 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    337 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    338 	if(state == TIME) s1.nchstats.fld = t;}
    339 #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
    340 #define MAXFAIL 5
    341 
    342 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', ' ' };
    343 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_IDLE };
    344 
    345 void
    346 showkre()
    347 {
    348 	float f1, f2;
    349 	int psiz, inttotal;
    350 	int i, l, c;
    351 	static int failcnt = 0;
    352 
    353 	if (state == TIME)
    354 		dkswap();
    355 	etime = 0;
    356 	for(i = 0; i < CPUSTATES; i++) {
    357 		X(time);
    358 		etime += s.time[i];
    359 	}
    360 	if (etime < 5.0) {	/* < 5 ticks - ignore this trash */
    361 		if (failcnt++ >= MAXFAIL) {
    362 			clear();
    363 			mvprintw(2, 10, "The alternate system clock has died!");
    364 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    365 			move(CMDLINE, 0);
    366 			refresh();
    367 			failcnt = 0;
    368 			sleep(5);
    369 			command("pigs");
    370 		}
    371 		return;
    372 	}
    373 	failcnt = 0;
    374 	etime /= hertz;
    375 	inttotal = 0;
    376 	for (i = 0; i < nintr; i++) {
    377 		if (s.intrcnt[i] == 0)
    378 			continue;
    379 		if (intrloc[i] == 0) {
    380 			if (nextintsrow == LINES)
    381 				continue;
    382 			intrloc[i] = nextintsrow++;
    383 			mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
    384 				intrname[i]);
    385 		}
    386 		X(intrcnt);
    387 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    388 		inttotal += l;
    389 		putint(l, intrloc[i], INTSCOL, 8);
    390 	}
    391 	putint(inttotal, INTSROW + 1, INTSCOL, 8);
    392 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
    393 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
    394 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
    395 	    nchtotal.ncs_miss + nchtotal.ncs_long;
    396 	if (state == TIME)
    397 		s1.nchcount = s.nchcount;
    398 
    399 	psiz = 0;
    400 	f2 = 0.0;
    401 
    402 	/*
    403 	 * Last CPU state not calculated yet.
    404 	 */
    405 	for (c = 0; c < CPUSTATES - 1; c++) {
    406 		i = cpuorder[c];
    407 		f1 = cputime(i);
    408 		f2 += f1;
    409 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    410 		if (c == 0)
    411 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    412 		else
    413 			putfloat(f1, GRAPHROW, GRAPHCOL + 12 * c,
    414 				5, 1, 0);
    415 		move(GRAPHROW + 2, psiz);
    416 		psiz += l;
    417 		while (l-- > 0)
    418 			addch(cpuchar[c]);
    419 	}
    420 
    421 	putint(ucount(), STATROW, STATCOL, 3);
    422 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    423 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    424 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    425 	mvaddstr(STATROW, STATCOL + 53, buf);
    426 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
    427 
    428 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
    429 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
    430 	    MEMROW + 2, MEMCOL + 16, 7);
    431 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
    432 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
    433 	    MEMROW + 3, MEMCOL + 16, 7);
    434 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
    435 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
    436 	    MEMROW + 3, MEMCOL + 24, 7);
    437 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    438 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
    439 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
    440 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
    441 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
    442 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
    443 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
    444 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
    445 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
    446 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
    447 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
    448 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
    449 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
    450 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
    451 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
    452 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
    453 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
    454 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
    455 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
    456 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
    457 	if (LINES - 1 > VMSTATROW + 16)
    458 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
    459 
    460 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
    461 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
    462 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
    463 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
    464 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
    465 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
    466 
    467 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
    468 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 5);
    469 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 5);
    470 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 15, 5);
    471 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 20, 5);
    472 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    473 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
    474 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
    475 		if (dk_select[i]) {
    476 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
    477 				" %4.4s", dr_name[i]);
    478 			dinfo(i, ++c);
    479 		}
    480 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    481 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    482 #define nz(x)	((x) ? (x) : 1)
    483 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    484 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    485 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    486 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    487 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    488 #undef nz
    489 }
    490 
    491 int
    492 cmdkre(cmd, args)
    493 	char *cmd, *args;
    494 {
    495 
    496 	if (prefix(cmd, "run")) {
    497 		copyinfo(&s2, &s1);
    498 		state = RUN;
    499 		return (1);
    500 	}
    501 	if (prefix(cmd, "boot")) {
    502 		state = BOOT;
    503 		copyinfo(&z, &s1);
    504 		return (1);
    505 	}
    506 	if (prefix(cmd, "time")) {
    507 		state = TIME;
    508 		return (1);
    509 	}
    510 	if (prefix(cmd, "zero")) {
    511 		if (state == RUN)
    512 			getinfo(&s1, RUN);
    513 		return (1);
    514 	}
    515 	return (dkcmd(cmd, args));
    516 }
    517 
    518 /* calculate number of users on the system */
    519 static int
    520 ucount()
    521 {
    522 	int nusers = 0;
    523 
    524 	if (ut < 0)
    525 		return (0);
    526 	while (read(ut, &utmp, sizeof(utmp)))
    527 		if (utmp.ut_name[0] != '\0')
    528 			nusers++;
    529 
    530 	lseek(ut, (off_t)0, SEEK_SET);
    531 	return (nusers);
    532 }
    533 
    534 static float
    535 cputime(indx)
    536 	int indx;
    537 {
    538 	double t;
    539 	int i;
    540 
    541 	t = 0;
    542 	for (i = 0; i < CPUSTATES; i++)
    543 		t += s.time[i];
    544 	if (t == 0.0)
    545 		t = 1.0;
    546 	return (s.time[indx] * 100.0 / t);
    547 }
    548 
    549 static void
    550 putint(n, l, c, w)
    551 	int n, l, c, w;
    552 {
    553 	char b[128];
    554 
    555 	move(l, c);
    556 	if (n == 0) {
    557 		while (w-- > 0)
    558 			addch(' ');
    559 		return;
    560 	}
    561 	(void)snprintf(b, sizeof b, "%*d", w, n);
    562 	if (strlen(b) > w) {
    563 		while (w-- > 0)
    564 			addch('*');
    565 		return;
    566 	}
    567 	addstr(b);
    568 }
    569 
    570 static void
    571 putfloat(f, l, c, w, d, nz)
    572 	double f;
    573 	int l, c, w, d, nz;
    574 {
    575 	char b[128];
    576 
    577 	move(l, c);
    578 	if (nz && f == 0.0) {
    579 		while (--w >= 0)
    580 			addch(' ');
    581 		return;
    582 	}
    583 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
    584 	if (strlen(b) > w) {
    585 		while (--w >= 0)
    586 			addch('*');
    587 		return;
    588 	}
    589 	addstr(b);
    590 }
    591 
    592 static void
    593 getinfo(s, st)
    594 	struct Info *s;
    595 	enum state st;
    596 {
    597 	int mib[2];
    598 	size_t size;
    599 	extern int errno;
    600 
    601 	dkreadstats();
    602 	NREAD(X_CPTIME, s->time, sizeof s->time);
    603 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    604 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    605 	size = sizeof(s->uvmexp);
    606 	mib[0] = CTL_VM;
    607 	mib[1] = VM_UVMEXP;
    608 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
    609 		error("can't get uvmexp: %s\n", strerror(errno));
    610 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
    611 	}
    612 	size = sizeof(s->Total);
    613 	mib[0] = CTL_VM;
    614 	mib[1] = VM_METER;
    615 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    616 		error("Can't get kernel info: %s\n", strerror(errno));
    617 		memset(&s->Total, 0, sizeof(s->Total));
    618 	}
    619 }
    620 
    621 static void
    622 allocinfo(s)
    623 	struct Info *s;
    624 {
    625 
    626 	s->intrcnt = (long *) malloc(nintr * sizeof(long));
    627 	if (s->intrcnt == NULL)
    628 		errx(2, "out of memory");
    629 }
    630 
    631 static void
    632 copyinfo(from, to)
    633 	struct Info *from, *to;
    634 {
    635 	long *intrcnt;
    636 
    637 	intrcnt = to->intrcnt;
    638 	*to = *from;
    639 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
    640 }
    641 
    642 static void
    643 dinfo(dn, c)
    644 	int dn, c;
    645 {
    646 	double words, atime;
    647 
    648 	c = DISKCOL + c * 5;
    649 
    650 	/* time busy in disk activity */
    651 	atime = (double)cur.dk_time[dn].tv_sec +
    652 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    653 
    654 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    655 
    656 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
    657 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
    658 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
    659 	putfloat(atime/etime, DISKROW + 4, c, 5, 2, 1);
    660 }
    661