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