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