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