Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.77
      1 /*	$NetBSD: vmstat.c,v 1.77 2010/04/30 16:21:05 njoly 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.77 2010/04/30 16:21:05 njoly 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");
    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 
    450 	PUTRATE(us, us1, uvmexp->swtch, GENSTATROW + 1, GENSTATCOL - 1, 7);
    451 	PUTRATE(us, us1, uvmexp->traps, GENSTATROW + 1, GENSTATCOL + 7, 6);
    452 	PUTRATE(us, us1, uvmexp->syscalls, GENSTATROW + 1, GENSTATCOL + 14, 6);
    453 	PUTRATE(us, us1, uvmexp->intrs, GENSTATROW + 1, GENSTATCOL + 21, 5);
    454 	PUTRATE(us, us1, uvmexp->softs, GENSTATROW + 1, GENSTATCOL + 27, 6);
    455 	PUTRATE(us, us1, uvmexp->faults, GENSTATROW + 1, GENSTATCOL + 34, 6);
    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->pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
    474 	PUTRATE(us, us1, uvmexp->pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
    475 }
    476 
    477 void
    478 showvmstat(void)
    479 {
    480 	int inttotal;
    481 	int i, l, r, c;
    482 	static int failcnt = 0;
    483 	static int relabel = 0;
    484 	static int last_disks = 0;
    485 	static char pigs[] = "pigs";
    486 
    487 	if (relabel) {
    488 		labelvmstat();
    489 		relabel = 0;
    490 	}
    491 
    492 	cpuswap();
    493 	if (display_mode == TIME) {
    494 		drvswap();
    495 		etime = cur.cp_etime;
    496 		/* < 5 ticks - ignore this trash */
    497 		if ((etime * hertz) < 1.0) {
    498 			if (failcnt++ <= MAXFAIL)
    499 				return;
    500 			clear();
    501 			mvprintw(2, 10, "The alternate system clock has died!");
    502 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    503 			move(CMDLINE, 0);
    504 			refresh();
    505 			failcnt = 0;
    506 			sleep(5);
    507 			command(pigs);
    508 			return;
    509 		}
    510 	} else
    511 		etime = 1.0;
    512 
    513 	show_vmstat_top(&s.Total, &s.uvmexp, &s1.uvmexp);
    514 
    515 	/* Memory totals */
    516 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
    517 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 9);
    518 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
    519 	    MEMROW + 2, MEMCOL + 16, 9);
    520 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free),
    521 	    MEMROW + 3, MEMCOL + 6, 9);
    522 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
    523 	    MEMROW + 3, MEMCOL + 16, 9);
    524 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 9);
    525 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
    526 	    MEMROW + 3, MEMCOL + 26, 9);
    527 #undef pgtokb
    528 
    529 	/* Namei cache */
    530 	Z(s, s1, ncs_goodhits); Z(s, s1, ncs_badhits); Z(s, s1, ncs_miss);
    531 	Z(s, s1, ncs_long); Z(s, s1, ncs_pass2); Z(s, s1, ncs_2passes);
    532 	s.nchcount = s.nchstats.ncs_goodhits + s.nchstats.ncs_badhits +
    533 	    s.nchstats.ncs_miss + s.nchstats.ncs_long +
    534 	    s.nchstats.ncs_pass2 + s.nchstats.ncs_2passes;
    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 < (int)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 	int nusers = 0;
    683 	struct utmpentry *ehead;
    684 
    685 	nusers = getutentries(NULL, &ehead);
    686 
    687 	if (nusers != onusers) {
    688 		if (nusers == 1)
    689 			mvprintw(STATROW, STATCOL + 8, " ");
    690 		else
    691 			mvprintw(STATROW, STATCOL + 8, "s");
    692 	}
    693 	onusers = nusers;
    694 	return (nusers);
    695 }
    696 
    697 static float
    698 cputime(int indx)
    699 {
    700 	double t;
    701 	int i;
    702 
    703 	t = 0;
    704 	for (i = 0; i < CPUSTATES; i++)
    705 		t += cur.cp_time[i];
    706 	if (t == 0.0)
    707 		t = 1.0;
    708 	return (cur.cp_time[indx] * 100.0 / t);
    709 }
    710 
    711 void
    712 puthumanint(u_int64_t n, int l, int c, int w)
    713 {
    714 	char b[128];
    715 
    716 	if (move(l, c) != OK)
    717 		return;
    718 	if (n == 0) {
    719 		hline(' ', w);
    720 		return;
    721 	}
    722 	if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
    723 		hline('*', w);
    724 		return;
    725 	}
    726 	printw("%*s", w, b);
    727 }
    728 
    729 void
    730 putint(int n, int l, int c, int w)
    731 {
    732 	char b[128];
    733 
    734 	if (move(l, c) != OK)
    735 		return;
    736 	if (n == 0) {
    737 		hline(' ', w);
    738 		return;
    739 	}
    740 	(void)snprintf(b, sizeof b, "%*d", w, n);
    741 	if ((int)strlen(b) > w) {
    742 		if (display_mode == TIME)
    743 			hline('*', w);
    744 		else
    745 			puthumanint(n, l, c, w);
    746 		return;
    747 	}
    748 	addstr(b);
    749 }
    750 
    751 void
    752 putfloat(double f, int l, int c, int w, int d, int nz)
    753 {
    754 	char b[128];
    755 
    756 	if (move(l, c) != OK)
    757 		return;
    758 	if (nz && f == 0.0) {
    759 		hline(' ', w);
    760 		return;
    761 	}
    762 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
    763 	if ((int)strlen(b) > w) {
    764 		hline('*', w);
    765 		return;
    766 	}
    767 	addstr(b);
    768 }
    769 
    770 static void
    771 getinfo(struct Info *stats)
    772 {
    773 	int mib[2];
    774 	size_t size;
    775 	int i;
    776 
    777 	cpureadstats();
    778 	drvreadstats();
    779 	NREAD(X_NCHSTATS, &stats->nchstats, sizeof stats->nchstats);
    780 	if (nintr)
    781 		NREAD(X_INTRCNT, stats->intrcnt, nintr * LONG);
    782 	for (i = 0; i < nevcnt; i++)
    783 		KREAD(ie_head[i].ie_count, &stats->evcnt[i],
    784 		      sizeof stats->evcnt[i]);
    785 	size = sizeof(stats->uvmexp);
    786 	mib[0] = CTL_VM;
    787 	mib[1] = VM_UVMEXP2;
    788 	if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) {
    789 		error("can't get uvmexp: %s\n", strerror(errno));
    790 		memset(&stats->uvmexp, 0, sizeof(stats->uvmexp));
    791 	}
    792 	size = sizeof(stats->Total);
    793 	mib[0] = CTL_VM;
    794 	mib[1] = VM_METER;
    795 	if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) {
    796 		error("Can't get kernel info: %s\n", strerror(errno));
    797 		memset(&stats->Total, 0, sizeof(stats->Total));
    798 	}
    799 }
    800 
    801 static void
    802 allocinfo(struct Info *stats)
    803 {
    804 
    805 	if (nintr &&
    806 	    (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
    807 		error("calloc failed");
    808 		die(0);
    809 	}
    810 	if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
    811 		error("calloc failed");
    812 		die(0);
    813 	}
    814 }
    815 
    816 static void
    817 copyinfo(struct Info *from, struct Info *to)
    818 {
    819 	long *intrcnt;
    820 	u_int64_t *evcnt;
    821 
    822 	intrcnt = to->intrcnt;
    823 	evcnt = to->evcnt;
    824 	*to = *from;
    825 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt);
    826 	memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
    827 }
    828 
    829 static void
    830 dinfo(int dn, int r, int c)
    831 {
    832 	double atime;
    833 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
    834 
    835 	mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
    836 	ADV;
    837 
    838 	putint((int)(cur.seek[dn]/etime+0.5), r, c, DISKCOLWIDTH);
    839 	ADV;
    840 	putint((int)((cur.rxfer[dn]+cur.wxfer[dn])/etime+0.5),
    841 	    r, c, DISKCOLWIDTH);
    842 	ADV;
    843 	puthumanint((cur.rbytes[dn] + cur.wbytes[dn]) / etime + 0.5,
    844 		    r, c, DISKCOLWIDTH);
    845 	ADV;
    846 
    847 	/* time busy in disk activity */
    848 	atime = cur.time[dn].tv_sec + cur.time[dn].tv_usec / 1000000.0;
    849 	atime = atime * 100.0 / etime;
    850 	if (atime >= 100)
    851 		putint(100, r, c, DISKCOLWIDTH);
    852 	else
    853 		putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
    854 }
    855