Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.23
      1 /*	$NetBSD: vmstat.c,v 1.23 1999/12/20 23:11:51 jwise 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.23 1999/12/20 23:11:51 jwise 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 == NULL || 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 void
    492 vmstat_boot (args)
    493 	char *args;
    494 {
    495 	copyinfo(&z, &s1);
    496 	state = BOOT;
    497 }
    498 
    499 void
    500 vmstat_run (args)
    501 	char *args;
    502 {
    503 	copyinfo(&s1, &s2);
    504 	state = RUN;
    505 }
    506 
    507 void
    508 vmstat_time (args)
    509 	char * args;
    510 {
    511 	state = TIME;
    512 }
    513 
    514 void
    515 vmstat_zero (args)
    516 	char *args;
    517 {
    518 	if (state == RUN)
    519 		getinfo(&s1, RUN);
    520 }
    521 
    522 /* calculate number of users on the system */
    523 static int
    524 ucount()
    525 {
    526 	int nusers = 0, onusers = -1;
    527 
    528 	if (ut < 0)
    529 		return (0);
    530 	while (read(ut, &utmp, sizeof(utmp)))
    531 		if (utmp.ut_name[0] != '\0')
    532 			nusers++;
    533 
    534 	if (nusers != onusers) {
    535 		if (nusers == 1)
    536 			mvprintw(STATROW, STATCOL + 8, " ");
    537 		else
    538 			mvprintw(STATROW, STATCOL + 8, "s");
    539 	}
    540 	lseek(ut, (off_t)0, SEEK_SET);
    541 	onusers = nusers;
    542 	return (nusers);
    543 }
    544 
    545 static float
    546 cputime(indx)
    547 	int indx;
    548 {
    549 	double t;
    550 	int i;
    551 
    552 	t = 0;
    553 	for (i = 0; i < CPUSTATES; i++)
    554 		t += s.time[i];
    555 	if (t == 0.0)
    556 		t = 1.0;
    557 	return (s.time[indx] * 100.0 / t);
    558 }
    559 
    560 static void
    561 putint(n, l, c, w)
    562 	int n, l, c, w;
    563 {
    564 	char b[128];
    565 
    566 	move(l, c);
    567 	if (n == 0) {
    568 		while (w-- > 0)
    569 			addch(' ');
    570 		return;
    571 	}
    572 	(void)snprintf(b, sizeof b, "%*d", w, n);
    573 	if (strlen(b) > w) {
    574 		while (w-- > 0)
    575 			addch('*');
    576 		return;
    577 	}
    578 	addstr(b);
    579 }
    580 
    581 static void
    582 putfloat(f, l, c, w, d, nz)
    583 	double f;
    584 	int l, c, w, d, nz;
    585 {
    586 	char b[128];
    587 
    588 	move(l, c);
    589 	if (nz && f == 0.0) {
    590 		while (--w >= 0)
    591 			addch(' ');
    592 		return;
    593 	}
    594 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
    595 	if (strlen(b) > w) {
    596 		while (--w >= 0)
    597 			addch('*');
    598 		return;
    599 	}
    600 	addstr(b);
    601 }
    602 
    603 static void
    604 getinfo(s, st)
    605 	struct Info *s;
    606 	enum state st;
    607 {
    608 	int mib[2];
    609 	size_t size;
    610 	extern int errno;
    611 
    612 	dkreadstats();
    613 	NREAD(X_CPTIME, s->time, sizeof s->time);
    614 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    615 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    616 	size = sizeof(s->uvmexp);
    617 	mib[0] = CTL_VM;
    618 	mib[1] = VM_UVMEXP;
    619 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
    620 		error("can't get uvmexp: %s\n", strerror(errno));
    621 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
    622 	}
    623 	size = sizeof(s->Total);
    624 	mib[0] = CTL_VM;
    625 	mib[1] = VM_METER;
    626 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    627 		error("Can't get kernel info: %s\n", strerror(errno));
    628 		memset(&s->Total, 0, sizeof(s->Total));
    629 	}
    630 }
    631 
    632 static void
    633 allocinfo(s)
    634 	struct Info *s;
    635 {
    636 
    637 	if ((s->intrcnt = malloc(nintr * sizeof(long))) == NULL) {
    638 		error("malloc failed");
    639 		die(0);
    640 	}
    641 }
    642 
    643 static void
    644 copyinfo(from, to)
    645 	struct Info *from, *to;
    646 {
    647 	long *intrcnt;
    648 
    649 	intrcnt = to->intrcnt;
    650 	*to = *from;
    651 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
    652 }
    653 
    654 static void
    655 dinfo(dn, c)
    656 	int dn, c;
    657 {
    658 	double words, atime;
    659 
    660 	c = DISKCOL + c * 5;
    661 
    662 	/* time busy in disk activity */
    663 	atime = (double)cur.dk_time[dn].tv_sec +
    664 		((double)cur.dk_time[dn].tv_usec / (double)1000000);
    665 
    666 	words = cur.dk_bytes[dn] / 1024.0;	/* # of K transferred */
    667 
    668 	putint((int)((float)cur.dk_seek[dn]/etime+0.5), DISKROW + 1, c, 5);
    669 	putint((int)((float)cur.dk_xfer[dn]/etime+0.5), DISKROW + 2, c, 5);
    670 	putint((int)(words/etime + 0.5), DISKROW + 3, c, 5);
    671 	putfloat(atime/etime, DISKROW + 4, c, 5, 2, 1);
    672 }
    673