Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.50
      1 /*	$NetBSD: vmstat.c,v 1.50 2003/05/15 01:00:07 itojun 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.50 2003/05/15 01:00:07 itojun 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/user.h>
     51 #include <sys/namei.h>
     52 #include <sys/sysctl.h>
     53 #include <sys/device.h>
     54 
     55 #include <uvm/uvm_extern.h>
     56 
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <util.h>
     60 
     61 #include "systat.h"
     62 #include "extern.h"
     63 #include "dkstats.h"
     64 #include "utmpentry.h"
     65 
     66 static struct Info {
     67 	struct	uvmexp_sysctl uvmexp;
     68 	struct	vmtotal Total;
     69 	struct	nchstats nchstats;
     70 	long	nchcount;
     71 	long	*intrcnt;
     72 	u_int64_t	*evcnt;
     73 } s, s1, s2, z;
     74 
     75 #define	cnt s.Cnt
     76 #define oldcnt s1.Cnt
     77 #define	total s.Total
     78 #define	nchtotal s.nchstats
     79 #define	oldnchtotal s1.nchstats
     80 
     81 static	enum state { BOOT, TIME, RUN } state = TIME;
     82 
     83 static void allocinfo(struct Info *);
     84 static void copyinfo(struct Info *, struct Info *);
     85 static float cputime(int);
     86 static void dinfo(int, int, int);
     87 static void getinfo(struct Info *, enum state);
     88 static void putint(int, int, int, int);
     89 static void putfloat(double, int, int, int, int, int);
     90 static int ucount(void);
     91 
     92 static	char buf[26];
     93 static	u_int64_t t;
     94 static	double etime;
     95 static	float hertz;
     96 static	int nintr;
     97 static	long *intrloc;
     98 static	char **intrname;
     99 static	int nextintsrow;
    100 static	int disk_horiz = 1;
    101 
    102 WINDOW *
    103 openvmstat(void)
    104 {
    105 	return (stdscr);
    106 }
    107 
    108 void
    109 closevmstat(WINDOW *w)
    110 {
    111 
    112 	if (w == NULL)
    113 		return;
    114 	wclear(w);
    115 	wrefresh(w);
    116 }
    117 
    118 
    119 static struct nlist namelist[] = {
    120 #define	X_NCHSTATS	0
    121 	{ "_nchstats" },
    122 #define	X_INTRNAMES	1
    123 	{ "_intrnames" },
    124 #define	X_EINTRNAMES	2
    125 	{ "_eintrnames" },
    126 #define	X_INTRCNT	3
    127 	{ "_intrcnt" },
    128 #define	X_EINTRCNT	4
    129 	{ "_eintrcnt" },
    130 #define	X_ALLEVENTS	5
    131 	{ "_allevents" },
    132 	{ "" },
    133 };
    134 
    135 /*
    136  * These constants define where the major pieces are laid out
    137  */
    138 #define STATROW		 0	/* uses 1 row and 68 cols */
    139 #define STATCOL		 2
    140 #define MEMROW		 9	/* uses 4 rows and 31 cols */
    141 #define MEMCOL		 0
    142 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
    143 #define PAGECOL		54
    144 #define INTSROW		 9	/* uses all rows to bottom and 17 cols */
    145 #define INTSCOL		40
    146 #define INTSCOLEND	(VMSTATCOL - 0)
    147 #define PROCSROW	 2	/* uses 2 rows and 20 cols */
    148 #define PROCSCOL	 0
    149 #define GENSTATROW	 2	/* uses 2 rows and 30 cols */
    150 #define GENSTATCOL	18
    151 #define VMSTATROW	 7	/* uses 17 rows and 15 cols */
    152 #define VMSTATCOL	64
    153 #define GRAPHROW	 5	/* uses 3 rows and 51 cols */
    154 #define GRAPHCOL	 0
    155 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
    156 #define NAMEICOL	 0
    157 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
    158 #define DISKCOL		 0
    159 #define DISKCOLWIDTH	 6
    160 #define DISKCOLEND	INTSCOL
    161 
    162 typedef struct intr_evcnt intr_evcnt_t;
    163 struct intr_evcnt {
    164 	char		*ie_group;
    165 	char		*ie_name;
    166 	u_int64_t	*ie_count;	/* kernel address... */
    167 	int		ie_loc;		/* screen row */
    168 } *ie_head;
    169 int nevcnt;
    170 
    171 static void
    172 get_interrupt_events(void)
    173 {
    174 	struct evcntlist allevents;
    175 	struct evcnt evcnt, *evptr;
    176 	intr_evcnt_t *ie;
    177 
    178 	if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents))
    179 		return;
    180 	evptr = allevents.tqh_first;
    181 	for (; evptr != NULL; evptr = evcnt.ev_list.tqe_next) {
    182 		if (!KREAD(evptr, &evcnt, sizeof evcnt))
    183 			return;
    184 		if (evcnt.ev_type != EVCNT_TYPE_INTR)
    185 			continue;
    186 		ie_head = realloc(ie_head, sizeof *ie * (nevcnt + 1));
    187 		if (ie_head == NULL) {
    188 			error("realloc failed");
    189 			die(0);
    190 		}
    191 		ie = ie_head + nevcnt;
    192 		ie->ie_group = malloc(evcnt.ev_grouplen + 1);
    193 		ie->ie_name = malloc(evcnt.ev_namelen + 1);
    194 		if (ie->ie_group == NULL || ie->ie_name == NULL)
    195 			return;
    196 		if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1))
    197 			return;
    198 		if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1))
    199 			return;
    200 		ie->ie_count = &evptr->ev_count;
    201 		ie->ie_loc = 0;
    202 		nevcnt++;
    203 	}
    204 }
    205 
    206 int
    207 initvmstat(void)
    208 {
    209 	char *intrnamebuf, *cp;
    210 	int i;
    211 
    212 	if (namelist[0].n_type == 0) {
    213 		if (kvm_nlist(kd, namelist)) {
    214 			nlisterr(namelist);
    215 			return(0);
    216 		}
    217 		if (namelist[0].n_type == 0) {
    218 			error("No namelist");
    219 			return(0);
    220 		}
    221 	}
    222 	hertz = stathz ? stathz : hz;
    223 	if (!dkinit(1))
    224 		return(0);
    225 
    226 	/* Old style interrupt counts - deprecated */
    227 	nintr = (namelist[X_EINTRCNT].n_value -
    228 		namelist[X_INTRCNT].n_value) / sizeof (long);
    229 	intrloc = calloc(nintr, sizeof (long));
    230 	intrname = calloc(nintr, sizeof (long));
    231 	intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
    232 		namelist[X_INTRNAMES].n_value);
    233 	if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
    234 		error("Out of memory\n");
    235 		if (intrnamebuf)
    236 			free(intrnamebuf);
    237 		if (intrname)
    238 			free(intrname);
    239 		if (intrloc)
    240 			free(intrloc);
    241 		nintr = 0;
    242 		return(0);
    243 	}
    244 	NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
    245 		NVAL(X_INTRNAMES));
    246 	for (cp = intrnamebuf, i = 0; i < nintr; i++) {
    247 		intrname[i] = cp;
    248 		cp += strlen(cp) + 1;
    249 	}
    250 
    251 	/* event counter interrupt counts */
    252 	get_interrupt_events();
    253 
    254 	nextintsrow = INTSROW + 1;
    255 	allocinfo(&s);
    256 	allocinfo(&s1);
    257 	allocinfo(&s2);
    258 	allocinfo(&z);
    259 
    260 	getinfo(&s2, RUN);
    261 	copyinfo(&s2, &s1);
    262 	return(1);
    263 }
    264 
    265 void
    266 fetchvmstat(void)
    267 {
    268 	time_t now;
    269 
    270 	time(&now);
    271 	strlcpy(buf, ctime(&now), sizeof(buf));
    272 	buf[19] = '\0';
    273 	getinfo(&s, state);
    274 }
    275 
    276 static void
    277 print_ie_title(int i)
    278 {
    279 	int width, name_width, group_width;
    280 
    281 	width = INTSCOLEND - (INTSCOL + 9);
    282 	if (width <= 0)
    283 		return;
    284 
    285 	move(ie_head[i].ie_loc, INTSCOL + 9);
    286 	group_width = strlen(ie_head[i].ie_group);
    287 	name_width = strlen(ie_head[i].ie_name);
    288 	width -= group_width + 1 + name_width;
    289 	if (width < 0) {
    290 		/*
    291 		 * Screen to narrow for full strings
    292 		 * This is all rather horrid, in some cases there are a lot
    293 		 * of events in the same group, and in others the event
    294 		 * name is "intr".  There are also names which need 7 or 8
    295 		 * columns before they become meaningful.
    296 		 * This is a bad compromise.
    297 		 */
    298 		width = -width;
    299 		group_width -= (width + 1) / 2;
    300 		name_width -= width / 2;
    301 		/* some have the 'useful' name "intr", display their group */
    302 		if (strcasecmp(ie_head[i].ie_name, "intr") == 0) {
    303 			 group_width += name_width + 1;
    304 			 name_width = 0;
    305 		} else {
    306 			if (group_width <= 3 || name_width < 0) {
    307 				/* don't display group */
    308 				name_width += group_width + 1;
    309 				group_width = 0;
    310 			}
    311 		}
    312 	}
    313 
    314 	if (group_width != 0) {
    315 		printw("%-.*s", group_width, ie_head[i].ie_group);
    316 		if (name_width != 0)
    317 			printw(" ");
    318 	}
    319 	if (name_width != 0)
    320 		printw("%-.*s", name_width, ie_head[i].ie_name);
    321 }
    322 
    323 void
    324 labelvmstat(void)
    325 {
    326 	int i;
    327 
    328 	clear();
    329 	mvprintw(STATROW, STATCOL + 4, "users    Load");
    330 	mvprintw(MEMROW, MEMCOL,     "          memory totals (in kB)");
    331 	mvprintw(MEMROW + 1, MEMCOL, "         real   virtual    free");
    332 	mvprintw(MEMROW + 2, MEMCOL, "Active");
    333 	mvprintw(MEMROW + 3, MEMCOL, "All");
    334 
    335 	mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
    336 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    337 	mvprintw(PAGEROW + 2, PAGECOL, "ops");
    338 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    339 
    340 	mvprintw(INTSROW, INTSCOL + 9, "Interrupts");
    341 
    342 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
    343 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
    344 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
    345 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
    346 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
    347 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
    348 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
    349 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
    350 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
    351 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
    352 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
    353 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
    354 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
    355 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
    356 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
    357 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
    358 	if (LINES - 1 > VMSTATROW + 16)
    359 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
    360 
    361 	mvprintw(GENSTATROW, GENSTATCOL, " Csw   Trp   Sys  Int  Sof   Flt");
    362 
    363 	mvprintw(GRAPHROW, GRAPHCOL,
    364 		"    . %% Sy    . %% Us    . %% Ni    . %% In    . %% Id");
    365 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
    366 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    367 		"|    |    |    |    |    |    |    |    |    |    |");
    368 
    369 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    370 	mvprintw(NAMEIROW + 1, NAMEICOL,
    371 		"    Calls     hits    %%     hits     %%");
    372 	mvprintw(DISKROW, DISKCOL, "Discs:");
    373 	if (disk_horiz) {
    374 		mvprintw(DISKROW + 1, DISKCOL + 1, "seeks");
    375 		mvprintw(DISKROW + 2, DISKCOL + 1, "xfers");
    376 		mvprintw(DISKROW + 3, DISKCOL + 1, "bytes");
    377 		mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy");
    378 	} else {
    379 		mvprintw(DISKROW, DISKCOL + 1 + 1 * DISKCOLWIDTH, "seeks");
    380 		mvprintw(DISKROW, DISKCOL + 1 + 2 * DISKCOLWIDTH, "xfers");
    381 		mvprintw(DISKROW, DISKCOL + 1 + 3 * DISKCOLWIDTH, "bytes");
    382 		mvprintw(DISKROW, DISKCOL + 1 + 4 * DISKCOLWIDTH, "%%busy");
    383 	}
    384 	for (i = 0; i < nintr; i++) {
    385 		if (intrloc[i] == 0)
    386 			continue;
    387 		mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
    388 			INTSCOLEND - (INTSCOL + 9), intrname[i]);
    389 	}
    390 	for (i = 0; i < nevcnt; i++) {
    391 		if (ie_head[i].ie_loc == 0)
    392 			continue;
    393 		print_ie_title(i);
    394 	}
    395 }
    396 
    397 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    398 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    399 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    400 	if(state == TIME) s1.nchstats.fld = t;}
    401 #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
    402 #define MAXFAIL 5
    403 
    404 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
    405 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
    406 
    407 void
    408 showvmstat(void)
    409 {
    410 	float f1, f2;
    411 	int psiz, inttotal;
    412 	int i, l, r, c;
    413 	static int failcnt = 0;
    414 	static int relabel = 0;
    415 	static int last_disks = 0;
    416 
    417 	if (relabel) {
    418 		labelvmstat();
    419 		relabel = 0;
    420 	}
    421 
    422 	if (state == TIME) {
    423 		dkswap();
    424 		etime = cur.cp_etime;
    425 		/* < 5 ticks - ignore this trash */
    426 		if ((etime * hertz) < 1.0) {
    427 			if (failcnt++ > MAXFAIL)
    428 				return;
    429 			clear();
    430 			mvprintw(2, 10, "The alternate system clock has died!");
    431 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    432 			move(CMDLINE, 0);
    433 			refresh();
    434 			failcnt = 0;
    435 			sleep(5);
    436 			command("pigs");
    437 			return;
    438 		}
    439 	} else
    440 		etime = 1.0;
    441 
    442 	failcnt = 0;
    443 	inttotal = 0;
    444 	for (i = 0; i < nintr; i++) {
    445 		if (s.intrcnt[i] == 0)
    446 			continue;
    447 		if (intrloc[i] == 0) {
    448 			if (nextintsrow == LINES)
    449 				continue;
    450 			intrloc[i] = nextintsrow++;
    451 			mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
    452 				INTSCOLEND - (INTSCOL + 9), intrname[i]);
    453 		}
    454 		X(intrcnt);
    455 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    456 		inttotal += l;
    457 		putint(l, intrloc[i], INTSCOL, 8);
    458 	}
    459 	for (i = 0; i < nevcnt; i++) {
    460 		if (s.evcnt[i] == 0)
    461 			continue;
    462 		if (ie_head[i].ie_loc == 0) {
    463 			if (nextintsrow == LINES)
    464 				continue;
    465 			ie_head[i].ie_loc = nextintsrow++;
    466 			print_ie_title(i);
    467 		}
    468 		X(evcnt);
    469 		l = (int)((float)s.evcnt[i]/etime + 0.5);
    470 		inttotal += l;
    471 		putint(l, ie_head[i].ie_loc, INTSCOL, 8);
    472 	}
    473 	putint(inttotal, INTSROW, INTSCOL, 8);
    474 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
    475 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
    476 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
    477 	    nchtotal.ncs_miss + nchtotal.ncs_long;
    478 	if (state == TIME)
    479 		s1.nchcount = s.nchcount;
    480 
    481 	psiz = 0;
    482 	f2 = 0.0;
    483 
    484 	/*
    485 	 * Last CPU state not calculated yet.
    486 	 */
    487 	for (c = 0; c < CPUSTATES; c++) {
    488 		i = cpuorder[c];
    489 		f1 = cputime(i);
    490 		f2 += f1;
    491 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    492 		if (c == 0)
    493 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    494 		else
    495 			putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
    496 		mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
    497 		psiz += l;
    498 	}
    499 
    500 	putint(ucount(), STATROW, STATCOL, 3);
    501 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    502 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    503 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    504 	mvaddstr(STATROW, STATCOL + 53, buf);
    505 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
    506 
    507 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
    508 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
    509 	    MEMROW + 2, MEMCOL + 16, 7);
    510 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
    511 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
    512 	    MEMROW + 3, MEMCOL + 16, 7);
    513 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
    514 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
    515 	    MEMROW + 3, MEMCOL + 24, 7);
    516 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    517 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
    518 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
    519 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
    520 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
    521 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
    522 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
    523 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
    524 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
    525 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
    526 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
    527 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
    528 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
    529 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
    530 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
    531 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
    532 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
    533 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
    534 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
    535 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
    536 	if (LINES - 1 > VMSTATROW + 16)
    537 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
    538 
    539 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
    540 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
    541 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
    542 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
    543 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
    544 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
    545 
    546 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 4);
    547 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 4, 6);
    548 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 6);
    549 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 16, 5);
    550 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 21, 5);
    551 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 26, 6);
    552 	for (l = 0, i = 0, r = DISKROW, c = DISKCOL; i < dk_ndrive; i++) {
    553 		if (!dk_select[i])
    554 			continue;
    555 		if (disk_horiz)
    556 			c += DISKCOLWIDTH;
    557 		else
    558 			r++;
    559 		if (c + DISKCOLWIDTH > DISKCOLEND) {
    560 			if (disk_horiz && LINES - 1 - DISKROW >
    561 					(DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
    562 				disk_horiz = 0;
    563 				relabel = 1;
    564 			}
    565 			break;
    566 		}
    567 		if (r >= LINES - 1) {
    568 			if (!disk_horiz && LINES - 1 - DISKROW <
    569 					(DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
    570 				disk_horiz = 1;
    571 				relabel = 1;
    572 			}
    573 			break;
    574 		}
    575 		l++;
    576 
    577 		dinfo(i, r, c);
    578 	}
    579 	/* blank out if we lost any disks */
    580 	for (i = l; i < last_disks; i++) {
    581 		int j;
    582 		if (disk_horiz)
    583 			c += DISKCOLWIDTH;
    584 		else
    585 			r++;
    586 		for (j = 0; j < 5; j++) {
    587 			mvprintw(r, c, "%*s", DISKCOLWIDTH, "");
    588 			if (disk_horiz)
    589 				r++;
    590 			else
    591 				c += DISKCOLWIDTH;
    592 		}
    593 	}
    594 	last_disks = l;
    595 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    596 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    597 #define nz(x)	((x) ? (x) : 1)
    598 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    599 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    600 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    601 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    602 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    603 #undef nz
    604 }
    605 
    606 void
    607 vmstat_boot(char *args)
    608 {
    609 	copyinfo(&z, &s1);
    610 	state = BOOT;
    611 }
    612 
    613 void
    614 vmstat_run(char *args)
    615 {
    616 	copyinfo(&s1, &s2);
    617 	state = RUN;
    618 }
    619 
    620 void
    621 vmstat_time(char *args)
    622 {
    623 	state = TIME;
    624 }
    625 
    626 void
    627 vmstat_zero(char *args)
    628 {
    629 	if (state == RUN)
    630 		getinfo(&s1, RUN);
    631 }
    632 
    633 /* calculate number of users on the system */
    634 static int
    635 ucount(void)
    636 {
    637 	static int onusers = -1;
    638 	static struct utmpentry *oehead = NULL;
    639 	int nusers = 0;
    640 	struct utmpentry *ehead;
    641 
    642 	nusers = getutentries(NULL, &ehead);
    643 	if (oehead != ehead) {
    644 		freeutentries(oehead);
    645 		oehead = ehead;
    646 	}
    647 
    648 	if (nusers != onusers) {
    649 		if (nusers == 1)
    650 			mvprintw(STATROW, STATCOL + 8, " ");
    651 		else
    652 			mvprintw(STATROW, STATCOL + 8, "s");
    653 	}
    654 	onusers = nusers;
    655 	return (nusers);
    656 }
    657 
    658 static float
    659 cputime(int indx)
    660 {
    661 	double t;
    662 	int i;
    663 
    664 	t = 0;
    665 	for (i = 0; i < CPUSTATES; i++)
    666 		t += cur.cp_time[i];
    667 	if (t == 0.0)
    668 		t = 1.0;
    669 	return (cur.cp_time[indx] * 100.0 / t);
    670 }
    671 
    672 static void
    673 puthumanint(u_int64_t n, int l, int c, int w)
    674 {
    675 	char b[128];
    676 
    677 	if (move(l, c) != OK)
    678 		return;
    679 	if (n == 0) {
    680 		hline(' ', w);
    681 		return;
    682 	}
    683 	if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
    684 		hline('*', w);
    685 		return;
    686 	}
    687 	hline(' ', w - strlen(b));
    688 	mvaddstr(l, c + w - strlen(b), b);
    689 }
    690 
    691 static void
    692 putint(int n, int l, int c, int w)
    693 {
    694 	char b[128];
    695 
    696 	if (move(l, c) != OK)
    697 		return;
    698 	if (n == 0) {
    699 		hline(' ', w);
    700 		return;
    701 	}
    702 	(void)snprintf(b, sizeof b, "%*d", w, n);
    703 	if (strlen(b) > w) {
    704 		hline('*', w);
    705 		return;
    706 	}
    707 	addstr(b);
    708 }
    709 
    710 static void
    711 putfloat(double f, int l, int c, int w, int d, int nz)
    712 {
    713 	char b[128];
    714 
    715 	if (move(l, c) != OK)
    716 		return;
    717 	if (nz && f == 0.0) {
    718 		hline(' ', w);
    719 		return;
    720 	}
    721 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
    722 	if (strlen(b) > w) {
    723 		hline('*', w);
    724 		return;
    725 	}
    726 	addstr(b);
    727 }
    728 
    729 static void
    730 getinfo(struct Info *s, enum state st)
    731 {
    732 	int mib[2];
    733 	size_t size;
    734 	int i;
    735 
    736 	dkreadstats();
    737 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    738 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    739 	for (i = 0; i < nevcnt; i++)
    740 		KREAD(ie_head[i].ie_count, &s->evcnt[i], sizeof s->evcnt[i]);
    741 	size = sizeof(s->uvmexp);
    742 	mib[0] = CTL_VM;
    743 	mib[1] = VM_UVMEXP2;
    744 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
    745 		error("can't get uvmexp: %s\n", strerror(errno));
    746 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
    747 	}
    748 	size = sizeof(s->Total);
    749 	mib[0] = CTL_VM;
    750 	mib[1] = VM_METER;
    751 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    752 		error("Can't get kernel info: %s\n", strerror(errno));
    753 		memset(&s->Total, 0, sizeof(s->Total));
    754 	}
    755 }
    756 
    757 static void
    758 allocinfo(struct Info *s)
    759 {
    760 
    761 	if ((s->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
    762 		error("calloc failed");
    763 		die(0);
    764 	}
    765 	if ((s->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
    766 		error("calloc failed");
    767 		die(0);
    768 	}
    769 }
    770 
    771 static void
    772 copyinfo(struct Info *from, struct Info *to)
    773 {
    774 	long *intrcnt;
    775 	u_int64_t *evcnt;
    776 
    777 	intrcnt = to->intrcnt;
    778 	evcnt = to->evcnt;
    779 	*to = *from;
    780 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
    781 	memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
    782 }
    783 
    784 static void
    785 dinfo(int dn, int r, int c)
    786 {
    787 	double atime;
    788 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
    789 
    790 	mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
    791 	ADV;
    792 
    793 	putint((int)(cur.dk_seek[dn]/etime+0.5), r, c, DISKCOLWIDTH);
    794 	ADV;
    795 	putint((int)((cur.dk_rxfer[dn]+cur.dk_wxfer[dn])/etime+0.5),
    796 	    r, c, DISKCOLWIDTH);
    797 	ADV;
    798 	puthumanint((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / etime + 0.5,
    799 		    r, c, DISKCOLWIDTH);
    800 	ADV;
    801 
    802 	/* time busy in disk activity */
    803 	atime = cur.dk_time[dn].tv_sec + cur.dk_time[dn].tv_usec / 1000000.0;
    804 	atime = atime * 100.0 / etime;
    805 	if (atime >= 100)
    806 		putint(100, r, c, DISKCOLWIDTH);
    807 	else
    808 		putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
    809 #undef ADV
    810 }
    811