Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.31.2.1
      1 /*	$NetBSD: vmstat.c,v 1.31.2.1 2000/09/01 16:38:18 ad 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.31.2.1 2000/09/01 16:38:18 ad 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/sched.h>
     57 #include <sys/sysctl.h>
     58 
     59 #include <vm/vm.h>
     60 
     61 #include <uvm/uvm_extern.h>
     62 
     63 #include <ctype.h>
     64 #include <err.h>
     65 #include <nlist.h>
     66 #include <paths.h>
     67 #include <signal.h>
     68 #include <stdlib.h>
     69 #include <string.h>
     70 #include <utmp.h>
     71 #include <unistd.h>
     72 
     73 #include "systat.h"
     74 #include "extern.h"
     75 
     76 static struct Info {
     77 	u_int64_t time[CPUSTATES];
     78 	struct	uvmexp uvmexp;
     79 	struct	vmtotal Total;
     80 	struct	nchstats nchstats;
     81 	long	nchcount;
     82 	long	*intrcnt;
     83 } s, s1, s2, z;
     84 
     85 #include "dkstats.h"
     86 extern struct _disk	cur;
     87 
     88 
     89 #define	cnt s.Cnt
     90 #define oldcnt s1.Cnt
     91 #define	total s.Total
     92 #define	nchtotal s.nchstats
     93 #define	oldnchtotal s1.nchstats
     94 
     95 static	enum state { BOOT, TIME, RUN } state = TIME;
     96 
     97 static void allocinfo(struct Info *);
     98 static void copyinfo(struct Info *, struct Info *);
     99 static float cputime(int);
    100 static void dinfo(int, int);
    101 static void getinfo(struct Info *, enum state);
    102 static void putint(int, int, int, int);
    103 static void putfloat(double, int, int, int, int, int);
    104 static int ucount(void);
    105 
    106 static	int ut;
    107 static	char buf[26];
    108 static	u_int64_t t;
    109 static	double etime;
    110 static	float hertz;
    111 static	int nintr;
    112 static	long *intrloc;
    113 static	char **intrname;
    114 static	int nextintsrow;
    115 
    116 struct	utmp utmp;
    117 
    118 WINDOW *
    119 openvmstat(void)
    120 {
    121 
    122 	ut = open(_PATH_UTMP, O_RDONLY);
    123 	if (ut < 0)
    124 		error("No utmp");
    125 	return (stdscr);
    126 }
    127 
    128 void
    129 closevmstat(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_TOTAL		0
    142 	{ "_total" },
    143 #define	X_NCHSTATS	1
    144 	{ "_nchstats" },
    145 #define	X_INTRNAMES	2
    146 	{ "_intrnames" },
    147 #define	X_EINTRNAMES	3
    148 	{ "_eintrnames" },
    149 #define	X_INTRCNT	4
    150 	{ "_intrcnt" },
    151 #define	X_EINTRCNT	5
    152 	{ "_eintrcnt" },
    153 	{ "" },
    154 };
    155 
    156 /*
    157  * These constants define where the major pieces are laid out
    158  */
    159 #define STATROW		 0	/* uses 1 row and 68 cols */
    160 #define STATCOL		 2
    161 #define MEMROW		 2	/* uses 4 rows and 31 cols */
    162 #define MEMCOL		 0
    163 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
    164 #define PAGECOL		36
    165 #define INTSROW		 2	/* uses all rows to bottom and 17 cols */
    166 #define INTSCOL		63
    167 #define PROCSROW	 7	/* uses 2 rows and 20 cols */
    168 #define PROCSCOL	 0
    169 #define GENSTATROW	 7	/* uses 2 rows and 30 cols */
    170 #define GENSTATCOL	20
    171 #define VMSTATROW	 7	/* uses 17 rows and 12 cols */
    172 #define VMSTATCOL	48
    173 #define GRAPHROW	10	/* uses 3 rows and 51 cols */
    174 #define GRAPHCOL	 0
    175 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
    176 #define NAMEICOL	 0
    177 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
    178 #define DISKCOL		 0
    179 
    180 #define	DRIVESPACE	 9	/* max # for space */
    181 
    182 #if DK_NDRIVE > DRIVESPACE
    183 #define	MAXDRIVES	DRIVESPACE	 /* max # to display */
    184 #else
    185 #define	MAXDRIVES	DK_NDRIVE	 /* max # to display */
    186 #endif
    187 
    188 int
    189 initvmstat(void)
    190 {
    191 	char *intrnamebuf, *cp;
    192 	int i;
    193 	static int once = 0;
    194 	extern gid_t egid;
    195 
    196 	if (namelist[0].n_type == 0) {
    197 		if (kvm_nlist(kd, namelist)) {
    198 			nlisterr(namelist);
    199 			return(0);
    200 		}
    201 		if (namelist[0].n_type == 0) {
    202 			error("No namelist");
    203 			return(0);
    204 		}
    205 	}
    206 	hertz = stathz ? stathz : hz;
    207 	if (! dkinit(1, egid))
    208 		return(0);
    209 	if (dk_ndrive && !once) {
    210 #define	allocate(e, t) \
    211 	s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    212 	s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    213 	s2./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \
    214 	z./**/e = (t *)calloc(dk_ndrive, sizeof (t));
    215 		once = 1;
    216 #undef allocate
    217 	}
    218 	if (nintr == 0) {
    219 		nintr = (namelist[X_EINTRCNT].n_value -
    220 			namelist[X_INTRCNT].n_value) / sizeof (long);
    221 		intrloc = calloc(nintr, sizeof (long));
    222 		intrname = calloc(nintr, sizeof (long));
    223 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
    224 			namelist[X_INTRNAMES].n_value);
    225 		if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
    226 			error("Out of memory\n");
    227 			if (intrnamebuf)
    228 				free(intrnamebuf);
    229 			if (intrname)
    230 				free(intrname);
    231 			if (intrloc)
    232 				free(intrloc);
    233 			nintr = 0;
    234 			return(0);
    235 		}
    236 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
    237 			NVAL(X_INTRNAMES));
    238 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
    239 			intrname[i] = cp;
    240 			cp += strlen(cp) + 1;
    241 		}
    242 		nextintsrow = INTSROW + 2;
    243 		allocinfo(&s);
    244 		allocinfo(&s1);
    245 		allocinfo(&s2);
    246 		allocinfo(&z);
    247 	}
    248 	getinfo(&s2, RUN);
    249 	copyinfo(&s2, &s1);
    250 	return(1);
    251 }
    252 
    253 void
    254 fetchvmstat(void)
    255 {
    256 	time_t now;
    257 
    258 	time(&now);
    259 	strcpy(buf, ctime(&now));
    260 	buf[19] = '\0';
    261 	getinfo(&s, state);
    262 }
    263 
    264 void
    265 labelvmstat(void)
    266 {
    267 	int i, j;
    268 
    269 	clear();
    270 	mvprintw(STATROW, STATCOL + 4, "users    Load");
    271 	mvprintw(MEMROW, MEMCOL,     "          memory totals (in KB)");
    272 	mvprintw(MEMROW + 1, MEMCOL, "         real   virtual    free");
    273 	mvprintw(MEMROW + 2, MEMCOL, "Active");
    274 	mvprintw(MEMROW + 3, MEMCOL, "All");
    275 
    276 	mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
    277 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    278 	mvprintw(PAGEROW + 2, PAGECOL, "ops");
    279 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    280 
    281 	mvprintw(INTSROW, INTSCOL + 3, " Interrupts");
    282 	mvprintw(INTSROW + 1, INTSCOL + 9, "total");
    283 
    284 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
    285 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
    286 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
    287 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
    288 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
    289 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
    290 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
    291 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
    292 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
    293 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
    294 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
    295 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
    296 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
    297 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
    298 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
    299 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
    300 	if (LINES - 1 > VMSTATROW + 16)
    301 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
    302 
    303 	mvprintw(GENSTATROW, GENSTATCOL, "  Csw  Trp  Sys  Int  Sof  Flt");
    304 
    305 	mvprintw(GRAPHROW, GRAPHCOL,
    306 		"    . %% Sy    . %% Us    . %% Ni    . %% In    . %% Id");
    307 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
    308 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    309 		"|    |    |    |    |    |    |    |    |    |    |");
    310 
    311 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    312 	mvprintw(NAMEIROW + 1, NAMEICOL,
    313 		"    Calls     hits    %%     hits     %%");
    314 	mvprintw(DISKROW, DISKCOL, "Discs");
    315 	mvprintw(DISKROW + 1, DISKCOL, "seeks");
    316 	mvprintw(DISKROW + 2, DISKCOL, "xfers");
    317 	mvprintw(DISKROW + 3, DISKCOL, "Kbyte");
    318 	mvprintw(DISKROW + 4, DISKCOL, "%%busy");
    319 	j = 0;
    320 	for (i = 0; i < dk_ndrive && j < MAXDRIVES; i++)
    321 		if (dk_select[i]) {
    322 			mvprintw(DISKROW, DISKCOL + 5 + 5 * j,
    323 				" %4.4s", dr_name[j]);
    324 			j++;
    325 		}
    326 	for (i = 0; i < nintr; i++) {
    327 		if (intrloc[i] == 0)
    328 			continue;
    329 		mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s", intrname[i]);
    330 	}
    331 }
    332 
    333 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    334 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    335 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    336 	if(state == TIME) s1.nchstats.fld = t;}
    337 #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
    338 #define MAXFAIL 5
    339 
    340 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
    341 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
    342 
    343 void
    344 showvmstat(void)
    345 {
    346 	float f1, f2;
    347 	int psiz, inttotal;
    348 	int i, l, c;
    349 	static int failcnt = 0;
    350 
    351 	if (state == TIME)
    352 		dkswap();
    353 	etime = 0;
    354 	for(i = 0; i < CPUSTATES; i++) {
    355 		X(time);
    356 		etime += s.time[i];
    357 	}
    358 	if (etime < 1.0) {	/* < 5 ticks - ignore this trash */
    359 		if (failcnt++ >= MAXFAIL) {
    360 			clear();
    361 			mvprintw(2, 10, "The alternate system clock has died!");
    362 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    363 			move(CMDLINE, 0);
    364 			refresh();
    365 			failcnt = 0;
    366 			sleep(5);
    367 			command("pigs");
    368 		}
    369 		return;
    370 	}
    371 	failcnt = 0;
    372 	etime /= hertz;
    373 	inttotal = 0;
    374 	for (i = 0; i < nintr; i++) {
    375 		if (s.intrcnt[i] == 0)
    376 			continue;
    377 		if (intrloc[i] == 0) {
    378 			if (nextintsrow == LINES)
    379 				continue;
    380 			intrloc[i] = nextintsrow++;
    381 			mvprintw(intrloc[i], INTSCOL + 9, "%-8.8s",
    382 				intrname[i]);
    383 		}
    384 		X(intrcnt);
    385 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    386 		inttotal += l;
    387 		putint(l, intrloc[i], INTSCOL, 8);
    388 	}
    389 	putint(inttotal, INTSROW + 1, INTSCOL, 8);
    390 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
    391 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
    392 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
    393 	    nchtotal.ncs_miss + nchtotal.ncs_long;
    394 	if (state == TIME)
    395 		s1.nchcount = s.nchcount;
    396 
    397 	psiz = 0;
    398 	f2 = 0.0;
    399 
    400 	/*
    401 	 * Last CPU state not calculated yet.
    402 	 */
    403 	for (c = 0; c < CPUSTATES; c++) {
    404 		i = cpuorder[c];
    405 		f1 = cputime(i);
    406 		f2 += f1;
    407 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    408 		if (c == 0)
    409 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    410 		else
    411 			putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
    412 		mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
    413 		psiz += l;
    414 	}
    415 
    416 	putint(ucount(), STATROW, STATCOL, 3);
    417 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    418 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    419 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    420 	mvaddstr(STATROW, STATCOL + 53, buf);
    421 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
    422 
    423 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
    424 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
    425 	    MEMROW + 2, MEMCOL + 16, 7);
    426 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
    427 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
    428 	    MEMROW + 3, MEMCOL + 16, 7);
    429 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
    430 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
    431 	    MEMROW + 3, MEMCOL + 24, 7);
    432 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    433 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
    434 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
    435 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
    436 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
    437 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
    438 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
    439 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
    440 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
    441 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
    442 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
    443 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
    444 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
    445 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
    446 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
    447 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
    448 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
    449 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
    450 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
    451 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
    452 	if (LINES - 1 > VMSTATROW + 16)
    453 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
    454 
    455 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
    456 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
    457 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
    458 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
    459 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
    460 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
    461 
    462 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 5);
    463 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 5, 5);
    464 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 5);
    465 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 15, 5);
    466 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 20, 5);
    467 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 25, 5);
    468 	mvprintw(DISKROW, DISKCOL + 5, "                              ");
    469 	for (i = 0, c = 0; i < dk_ndrive && c < MAXDRIVES; i++)
    470 		if (dk_select[i]) {
    471 			mvprintw(DISKROW, DISKCOL + 5 + 5 * c,
    472 				" %4.4s", dr_name[i]);
    473 			dinfo(i, ++c);
    474 		}
    475 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    476 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    477 #define nz(x)	((x) ? (x) : 1)
    478 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    479 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    480 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    481 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    482 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    483 #undef nz
    484 }
    485 
    486 void
    487 vmstat_boot(char *args)
    488 {
    489 	copyinfo(&z, &s1);
    490 	state = BOOT;
    491 }
    492 
    493 void
    494 vmstat_run(char *args)
    495 {
    496 	copyinfo(&s1, &s2);
    497 	state = RUN;
    498 }
    499 
    500 void
    501 vmstat_time (args)
    502 	char * args;
    503 {
    504 	state = TIME;
    505 }
    506 
    507 void
    508 vmstat_zero(char *args)
    509 {
    510 	if (state == RUN)
    511 		getinfo(&s1, RUN);
    512 }
    513 
    514 /* calculate number of users on the system */
    515 static int
    516 ucount(void)
    517 {
    518 	int nusers = 0, onusers = -1;
    519 
    520 	if (ut < 0)
    521 		return (0);
    522 	while (read(ut, &utmp, sizeof(utmp)))
    523 		if (utmp.ut_name[0] != '\0')
    524 			nusers++;
    525 
    526 	if (nusers != onusers) {
    527 		if (nusers == 1)
    528 			mvprintw(STATROW, STATCOL + 8, " ");
    529 		else
    530 			mvprintw(STATROW, STATCOL + 8, "s");
    531 	}
    532 	lseek(ut, (off_t)0, SEEK_SET);
    533 	onusers = nusers;
    534 	return (nusers);
    535 }
    536 
    537 static float
    538 cputime(int indx)
    539 {
    540 	double t;
    541 	int i;
    542 
    543 	t = 0;
    544 	for (i = 0; i < CPUSTATES; i++)
    545 		t += s.time[i];
    546 	if (t == 0.0)
    547 		t = 1.0;
    548 	return (s.time[indx] * 100.0 / t);
    549 }
    550 
    551 static void
    552 putint(int n, int l, int c, int w)
    553 {
    554 	char b[128];
    555 
    556 	move(l, c);
    557 	if (n == 0) {
    558 		hline(' ', w);
    559 		return;
    560 	}
    561 	(void)snprintf(b, sizeof b, "%*d", w, n);
    562 	if (strlen(b) > w) {
    563 		hline('*', w);
    564 		return;
    565 	}
    566 	addstr(b);
    567 }
    568 
    569 static void
    570 putfloat(double f, int l, int c, int w, int d, int nz)
    571 {
    572 	char b[128];
    573 
    574 	move(l, c);
    575 	if (nz && f == 0.0) {
    576 		hline(' ', w);
    577 		return;
    578 	}
    579 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
    580 	if (strlen(b) > w) {
    581 		hline('*', w);
    582 		return;
    583 	}
    584 	addstr(b);
    585 }
    586 
    587 static void
    588 getinfo(struct Info *s, enum state st)
    589 {
    590 	int mib[2];
    591 	size_t size;
    592 
    593 	dkreadstats();
    594 	(void) fetch_cptime(s->time);
    595 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    596 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    597 	size = sizeof(s->uvmexp);
    598 	mib[0] = CTL_VM;
    599 	mib[1] = VM_UVMEXP;
    600 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
    601 		error("can't get uvmexp: %s\n", strerror(errno));
    602 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
    603 	}
    604 	size = sizeof(s->Total);
    605 	mib[0] = CTL_VM;
    606 	mib[1] = VM_METER;
    607 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    608 		error("Can't get kernel info: %s\n", strerror(errno));
    609 		memset(&s->Total, 0, sizeof(s->Total));
    610 	}
    611 }
    612 
    613 static void
    614 allocinfo(struct Info *s)
    615 {
    616 
    617 	if ((s->intrcnt = malloc(nintr * sizeof(long))) == NULL) {
    618 		error("malloc failed");
    619 		die(0);
    620 	}
    621 }
    622 
    623 static void
    624 copyinfo(struct Info *from, struct Info *to)
    625 {
    626 	long *intrcnt;
    627 
    628 	intrcnt = to->intrcnt;
    629 	*to = *from;
    630 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
    631 }
    632 
    633 static void
    634 dinfo(int dn, int c)
    635 {
    636 	double words, atime;
    637 
    638 	c = DISKCOL + c * 5;
    639 
    640 	/* time busy in disk activity */
    641 	atime = (double)cur.dk_time[dn].tv_sec +
    642 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    643 
    644 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    645 
    646 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
    647 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
    648 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
    649 	putfloat(atime*100.0/etime, DISKROW + 4, c, 5, 1, 1);
    650 }
    651