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