Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.64
      1 /*	$NetBSD: vmstat.c,v 1.64 2006/03/20 20:36:38 dsl 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.64 2006/03/20 20:36:38 dsl 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 "dkstats.h"
     59 #include "tpstats.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 tinfo(int, int, int);
     79 static void getinfo(struct Info *);
     80 static int ucount(void);
     81 
     82 static	char buf[26];
     83 static	u_int64_t temp;
     84 double etime;
     85 static	float hertz;
     86 static	int nintr;
     87 static	long *intrloc;
     88 static	char **intrname;
     89 static	int nextintsrow;
     90 static	int disk_horiz = 1;
     91 
     92 WINDOW *
     93 openvmstat(void)
     94 {
     95 	return (stdscr);
     96 }
     97 
     98 void
     99 closevmstat(WINDOW *w)
    100 {
    101 
    102 	if (w == NULL)
    103 		return;
    104 	wclear(w);
    105 	wrefresh(w);
    106 }
    107 
    108 
    109 static struct nlist namelist[] = {
    110 #define	X_NCHSTATS	0
    111 	{ "_nchstats" },
    112 #define	X_INTRNAMES	1
    113 	{ "_intrnames" },
    114 #define	X_EINTRNAMES	2
    115 	{ "_eintrnames" },
    116 #define	X_INTRCNT	3
    117 	{ "_intrcnt" },
    118 #define	X_EINTRCNT	4
    119 	{ "_eintrcnt" },
    120 #define	X_ALLEVENTS	5
    121 	{ "_allevents" },
    122 	{ NULL }
    123 };
    124 
    125 /*
    126  * These constants define where the major pieces are laid out
    127  */
    128 #define STATROW		 0	/* uses 1 row and 68 cols */
    129 #define STATCOL		 2
    130 #define MEMROW		 9	/* uses 4 rows and 31 cols */
    131 #define MEMCOL		 0
    132 #define PAGEROW		 2	/* uses 4 rows and 26 cols */
    133 #define PAGECOL		54
    134 #define INTSROW		 9	/* uses all rows to bottom and 17 cols */
    135 #define INTSCOL		40
    136 #define INTSCOLEND	(VMSTATCOL - 0)
    137 #define PROCSROW	 2	/* uses 2 rows and 20 cols */
    138 #define PROCSCOL	 0
    139 #define GENSTATROW	 2	/* uses 2 rows and 30 cols */
    140 #define GENSTATCOL	17
    141 #define VMSTATROW	 7	/* uses 17 rows and 15 cols */
    142 #define VMSTATCOL	64
    143 #define GRAPHROW	 5	/* uses 3 rows and 51 cols */
    144 #define GRAPHCOL	 0
    145 #define NAMEIROW	14	/* uses 3 rows and 38 cols */
    146 #define NAMEICOL	 0
    147 #define DISKROW		18	/* uses 5 rows and 50 cols (for 9 drives) */
    148 #define DISKCOL		 0
    149 #define DISKCOLWIDTH	 6
    150 #define DISKCOLEND	INTSCOL
    151 
    152 typedef struct intr_evcnt intr_evcnt_t;
    153 struct intr_evcnt {
    154 	char		*ie_group;
    155 	char		*ie_name;
    156 	u_int64_t	*ie_count;	/* kernel address... */
    157 	int		ie_loc;		/* screen row */
    158 } *ie_head;
    159 int nevcnt;
    160 
    161 static void
    162 get_interrupt_events(void)
    163 {
    164 	struct evcntlist allevents;
    165 	struct evcnt evcnt, *evptr;
    166 	intr_evcnt_t *ie;
    167 	intr_evcnt_t *n;
    168 
    169 	if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents))
    170 		return;
    171 	evptr = TAILQ_FIRST(&allevents);
    172 	for (; evptr != NULL; evptr = TAILQ_NEXT(&evcnt, ev_list)) {
    173 		if (!KREAD(evptr, &evcnt, sizeof evcnt))
    174 			return;
    175 		if (evcnt.ev_type != EVCNT_TYPE_INTR)
    176 			continue;
    177 		n = realloc(ie_head, sizeof *ie * (nevcnt + 1));
    178 		if (n == NULL) {
    179 			error("realloc failed");
    180 			die(0);
    181 		}
    182 		ie_head = n;
    183 		ie = ie_head + nevcnt;
    184 		ie->ie_group = malloc(evcnt.ev_grouplen + 1);
    185 		ie->ie_name = malloc(evcnt.ev_namelen + 1);
    186 		if (ie->ie_group == NULL || ie->ie_name == NULL)
    187 			return;
    188 		if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1))
    189 			return;
    190 		if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1))
    191 			return;
    192 		ie->ie_count = &evptr->ev_count;
    193 		ie->ie_loc = 0;
    194 		nevcnt++;
    195 	}
    196 }
    197 
    198 int
    199 initvmstat(void)
    200 {
    201 	static char *intrnamebuf;
    202 	char *cp;
    203 	int i;
    204 
    205 	if (intrnamebuf)
    206 		free(intrnamebuf);
    207 	if (intrname)
    208 		free(intrname);
    209 	if (intrloc)
    210 		free(intrloc);
    211 
    212 	if (namelist[0].n_type == 0) {
    213 		if (kvm_nlist(kd, namelist) &&
    214 		    (namelist[X_NCHSTATS].n_type == 0 ||
    215 		     namelist[X_ALLEVENTS].n_type == 0)) {
    216 			nlisterr(namelist);
    217 			return(0);
    218 		}
    219 		if (namelist[0].n_type == 0) {
    220 			error("No namelist");
    221 			return(0);
    222 		}
    223 	}
    224 	hertz = stathz ? stathz : hz;
    225 	if (!dkinit(1))
    226 		return(0);
    227 	if (!tpinit(1))
    228 		return(0);
    229 
    230 	/* Old style interrupt counts - deprecated */
    231 	nintr = (namelist[X_EINTRCNT].n_value -
    232 		namelist[X_INTRCNT].n_value) / sizeof (long);
    233 	if (nintr) {
    234 		intrloc = calloc(nintr, sizeof (long));
    235 		intrname = calloc(nintr, sizeof (long));
    236 		intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
    237 				     namelist[X_INTRNAMES].n_value);
    238 		if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
    239 			error("Out of memory\n");
    240 			nintr = 0;
    241 			return(0);
    242 		}
    243 		NREAD(X_INTRNAMES, intrnamebuf, NVAL(X_EINTRNAMES) -
    244 		      NVAL(X_INTRNAMES));
    245 		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
    246 			intrname[i] = cp;
    247 			cp += strlen(cp) + 1;
    248 		}
    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);
    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);
    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_top(void)
    325 {
    326 
    327 	clear();
    328 
    329 	mvprintw(STATROW, STATCOL + 4, "users    Load");
    330 
    331 	mvprintw(GENSTATROW, GENSTATCOL, "   Csw    Trp    Sys   Int   Sof    Flt");
    332 
    333 	mvprintw(GRAPHROW, GRAPHCOL,
    334 		"    . %% Sy    . %% Us    . %% Ni    . %% In    . %% Id");
    335 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
    336 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    337 		"|    |    |    |    |    |    |    |    |    |    |");
    338 
    339 	mvprintw(PAGEROW, PAGECOL + 8, "PAGING   SWAPPING ");
    340 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    341 	mvprintw(PAGEROW + 2, PAGECOL + 2, "ops");
    342 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    343 }
    344 
    345 void
    346 labelvmstat(void)
    347 {
    348 	int i;
    349 
    350 	/* Top few lines first */
    351 
    352 	labelvmstat_top();
    353 
    354 	/* Left hand column */
    355 
    356 	mvprintw(MEMROW, MEMCOL,     "           memory totals (in kB)");
    357 	mvprintw(MEMROW + 1, MEMCOL, "          real  virtual     free");
    358 	mvprintw(MEMROW + 2, MEMCOL, "Active");
    359 	mvprintw(MEMROW + 3, MEMCOL, "All");
    360 
    361 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    362 	mvprintw(NAMEIROW + 1, NAMEICOL,
    363 		"    Calls     hits    %%     hits     %%");
    364 
    365 	mvprintw(DISKROW, DISKCOL, "Disks:");
    366 	if (disk_horiz) {
    367 		mvprintw(DISKROW + 1, DISKCOL + 1, "seeks");
    368 		mvprintw(DISKROW + 2, DISKCOL + 1, "xfers");
    369 		mvprintw(DISKROW + 3, DISKCOL + 1, "bytes");
    370 		mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy");
    371 	} else {
    372 		mvprintw(DISKROW, DISKCOL + 1 + 1 * DISKCOLWIDTH, "seeks");
    373 		mvprintw(DISKROW, DISKCOL + 1 + 2 * DISKCOLWIDTH, "xfers");
    374 		mvprintw(DISKROW, DISKCOL + 1 + 3 * DISKCOLWIDTH, "bytes");
    375 		mvprintw(DISKROW, DISKCOL + 1 + 4 * DISKCOLWIDTH, "%%busy");
    376 	}
    377 
    378 	/* Middle column */
    379 
    380 	mvprintw(INTSROW, INTSCOL + 9, "Interrupts");
    381 	for (i = 0; i < nintr; i++) {
    382 		if (intrloc[i] == 0)
    383 			continue;
    384 		mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
    385 			INTSCOLEND - (INTSCOL + 9), intrname[i]);
    386 	}
    387 	for (i = 0; i < nevcnt; i++) {
    388 		if (ie_head[i].ie_loc == 0)
    389 			continue;
    390 		print_ie_title(i);
    391 	}
    392 
    393 	/* Right hand column */
    394 
    395 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
    396 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
    397 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
    398 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
    399 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
    400 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
    401 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
    402 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
    403 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
    404 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
    405 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
    406 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
    407 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
    408 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
    409 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
    410 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
    411 
    412 	if (LINES - 1 > VMSTATROW + 16)
    413 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
    414 }
    415 
    416 #define X(s, s1, fld)	{temp = (s).fld[i]; (s).fld[i] -= (s1).fld[i]; \
    417 			if (display_mode == TIME) (s1).fld[i] = temp;}
    418 #define Z(s, s1, fld)	{temp = (s).nchstats.fld; \
    419 			(s).nchstats.fld -= (s1).nchstats.fld; \
    420 			if (display_mode == TIME) (s1).nchstats.fld = temp;}
    421 #define PUTRATE(s, s1, fld, l, c, w) \
    422 			{temp = (s).fld; (s).fld -= (s1).fld; \
    423 			if (display_mode == TIME) (s1).fld = temp; \
    424 			putint((int)((float)(s).fld/etime + 0.5), l, c, w);}
    425 #define MAXFAIL 5
    426 
    427 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
    428 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
    429 
    430 void
    431 show_vmstat_top(vmtotal_t *Total, uvmexp_sysctl_t *uvm, uvmexp_sysctl_t *uvm1)
    432 {
    433 	float f1, f2;
    434 	int psiz;
    435 	int i, l, c;
    436 	struct {
    437 		struct uvmexp_sysctl *uvmexp;
    438 	} us, us1;
    439 
    440 	us.uvmexp = uvm;
    441 	us1.uvmexp = uvm1;
    442 
    443 	putint(ucount(), STATROW, STATCOL, 3);
    444 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    445 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    446 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    447 	mvaddstr(STATROW, STATCOL + 53, buf);
    448 
    449 	putint(Total->t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    450 	putint(Total->t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
    451 	putint(Total->t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
    452 	putint(Total->t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
    453 
    454 	PUTRATE(us, us1, uvmexp->swtch, GENSTATROW + 1, GENSTATCOL - 1, 7);
    455 	PUTRATE(us, us1, uvmexp->traps, GENSTATROW + 1, GENSTATCOL + 6, 7);
    456 	PUTRATE(us, us1, uvmexp->syscalls, GENSTATROW + 1, GENSTATCOL + 13, 7);
    457 	PUTRATE(us, us1, uvmexp->intrs, GENSTATROW + 1, GENSTATCOL + 20, 6);
    458 	PUTRATE(us, us1, uvmexp->softs, GENSTATROW + 1, GENSTATCOL + 26, 6);
    459 	PUTRATE(us, us1, uvmexp->faults, GENSTATROW + 1, GENSTATCOL + 32, 7);
    460 
    461 	/* Last CPU state not calculated yet. */
    462 	for (f2 = 0.0, psiz = 0, c = 0; c < CPUSTATES; c++) {
    463 		i = cpuorder[c];
    464 		f1 = cputime(i);
    465 		f2 += f1;
    466 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    467 		if (c == 0)
    468 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    469 		else
    470 			putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
    471 		mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
    472 		psiz += l;
    473 	}
    474 
    475 	PUTRATE(us, us1, uvmexp->pageins, PAGEROW + 2, PAGECOL + 5, 5);
    476 	PUTRATE(us, us1, uvmexp->pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
    477 	PUTRATE(us, us1, uvmexp->swapins, PAGEROW + 2, PAGECOL + 15, 5);
    478 	PUTRATE(us, us1, uvmexp->swapouts, PAGEROW + 2, PAGECOL + 20, 5);
    479 	PUTRATE(us, us1, uvmexp->pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
    480 	PUTRATE(us, us1, uvmexp->pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
    481 }
    482 
    483 void
    484 showvmstat(void)
    485 {
    486 	int inttotal;
    487 	int i, l, r, c;
    488 	static int failcnt = 0;
    489 	static int relabel = 0;
    490 	static int last_disks = 0;
    491 	static char pigs[] = "pigs";
    492 
    493 	if (relabel) {
    494 		labelvmstat();
    495 		relabel = 0;
    496 	}
    497 
    498 	cpuswap();
    499 	if (display_mode == TIME) {
    500 		dkswap();
    501 		tpswap();
    502 		etime = cur.cp_etime;
    503 		/* < 5 ticks - ignore this trash */
    504 		if ((etime * hertz) < 1.0) {
    505 			if (failcnt++ > MAXFAIL)
    506 				return;
    507 			clear();
    508 			mvprintw(2, 10, "The alternate system clock has died!");
    509 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    510 			move(CMDLINE, 0);
    511 			refresh();
    512 			failcnt = 0;
    513 			sleep(5);
    514 			command(pigs);
    515 			return;
    516 		}
    517 	} else
    518 		etime = 1.0;
    519 
    520 	show_vmstat_top(&s.Total, &s.uvmexp, &s1.uvmexp);
    521 
    522 	/* Memory totals */
    523 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
    524 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 8);
    525 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
    526 	    MEMROW + 2, MEMCOL + 15, 8);
    527 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 8);
    528 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
    529 	    MEMROW + 3, MEMCOL + 15, 8);
    530 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 8);
    531 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
    532 	    MEMROW + 3, MEMCOL + 24, 8);
    533 #undef pgtokb
    534 
    535 	/* Namei cache */
    536 	Z(s, s1, ncs_goodhits); Z(s, s1, ncs_badhits); Z(s, s1, ncs_miss);
    537 	Z(s, s1, ncs_long); Z(s, s1, ncs_pass2); Z(s, s1, ncs_2passes);
    538 	s.nchcount = s.nchstats.ncs_goodhits + s.nchstats.ncs_badhits +
    539 	    s.nchstats.ncs_miss + s.nchstats.ncs_long;
    540 	if (display_mode == TIME)
    541 		s1.nchcount = s.nchcount;
    542 
    543 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    544 	putint(s.nchstats.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    545 #define nz(x)	((x) ? (x) : 1)
    546 	putfloat(s.nchstats.ncs_goodhits * 100.0 / nz(s.nchcount),
    547 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    548 	putint(s.nchstats.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    549 	putfloat(s.nchstats.ncs_pass2 * 100.0 / nz(s.nchcount),
    550 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    551 #undef nz
    552 
    553 	/* Disks */
    554 	for (l = 0, i = 0, r = DISKROW, c = DISKCOL;
    555 	     i < (dk_ndrive + tp_ndrive); i++) {
    556 		if (i < dk_ndrive) {
    557 			if (!dk_select[i])
    558 				continue;
    559 		} else {
    560 			if (!tp_select[i - dk_ndrive])
    561 				continue;
    562 		}
    563 
    564 		if (disk_horiz)
    565 			c += DISKCOLWIDTH;
    566 		else
    567 			r++;
    568 		if (c + DISKCOLWIDTH > DISKCOLEND) {
    569 			if (disk_horiz && LINES - 1 - DISKROW >
    570 			    (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
    571 				disk_horiz = 0;
    572 				relabel = 1;
    573 			}
    574 			break;
    575 		}
    576 		if (r >= LINES - 1) {
    577 			if (!disk_horiz && LINES - 1 - DISKROW <
    578 			    (DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
    579 				disk_horiz = 1;
    580 				relabel = 1;
    581 			}
    582 			break;
    583 		}
    584 		l++;
    585 
    586 		if (i < dk_ndrive)
    587 			dinfo(i, r, c);
    588 		else
    589 			tinfo(i - dk_ndrive, r, c);
    590 	}
    591 	/* blank out if we lost any disks */
    592 	for (i = l; i < last_disks; i++) {
    593 		int j;
    594 		if (disk_horiz)
    595 			c += DISKCOLWIDTH;
    596 		else
    597 			r++;
    598 		for (j = 0; j < 5; j++) {
    599 			if (disk_horiz)
    600 				mvprintw(r+j, c, "%*s", DISKCOLWIDTH, "");
    601 			else
    602 				mvprintw(r, c+j*DISKCOLWIDTH, "%*s", DISKCOLWIDTH, "");
    603 		}
    604 	}
    605 	last_disks = l;
    606 
    607 	/* Interrupts */
    608 	failcnt = 0;
    609 	inttotal = 0;
    610 	for (i = 0; i < nintr; i++) {
    611 		if (s.intrcnt[i] == 0)
    612 			continue;
    613 		if (intrloc[i] == 0) {
    614 			if (nextintsrow == LINES)
    615 				continue;
    616 			intrloc[i] = nextintsrow++;
    617 			mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
    618 				INTSCOLEND - (INTSCOL + 9), intrname[i]);
    619 		}
    620 		X(s, s1, intrcnt);
    621 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    622 		inttotal += l;
    623 		putint(l, intrloc[i], INTSCOL, 8);
    624 	}
    625 
    626 	for (i = 0; i < nevcnt; i++) {
    627 		if (s.evcnt[i] == 0)
    628 			continue;
    629 		if (ie_head[i].ie_loc == 0) {
    630 			if (nextintsrow == LINES)
    631 				continue;
    632 			ie_head[i].ie_loc = nextintsrow++;
    633 			print_ie_title(i);
    634 		}
    635 		X(s, s1, evcnt);
    636 		l = (int)((float)s.evcnt[i]/etime + 0.5);
    637 		inttotal += l;
    638 		putint(l, ie_head[i].ie_loc, INTSCOL, 8);
    639 	}
    640 	putint(inttotal, INTSROW, INTSCOL, 8);
    641 
    642 	PUTRATE(s, s1, uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
    643 	PUTRATE(s, s1, uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
    644 	PUTRATE(s, s1, uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
    645 	PUTRATE(s, s1, uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
    646 	PUTRATE(s, s1, uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
    647 	PUTRATE(s, s1, uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
    648 	PUTRATE(s, s1, uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
    649 	PUTRATE(s, s1, uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
    650 	PUTRATE(s, s1, uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
    651 	PUTRATE(s, s1, uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
    652 	PUTRATE(s, s1, uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
    653 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
    654 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
    655 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
    656 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
    657 	PUTRATE(s, s1, uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
    658 	if (LINES - 1 > VMSTATROW + 16)
    659 		PUTRATE(s, s1, uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
    660 
    661 }
    662 
    663 void
    664 vmstat_boot(char *args)
    665 {
    666 	copyinfo(&z, &s1);
    667 	display_mode = BOOT;
    668 }
    669 
    670 void
    671 vmstat_run(char *args)
    672 {
    673 	copyinfo(&s1, &s2);
    674 	display_mode = RUN;
    675 }
    676 
    677 void
    678 vmstat_time(char *args)
    679 {
    680 	display_mode = TIME;
    681 }
    682 
    683 void
    684 vmstat_zero(char *args)
    685 {
    686 	if (display_mode == RUN)
    687 		getinfo(&s1);
    688 }
    689 
    690 /* calculate number of users on the system */
    691 static int
    692 ucount(void)
    693 {
    694 	static int onusers = -1;
    695 	static struct utmpentry *oehead = NULL;
    696 	int nusers = 0;
    697 	struct utmpentry *ehead;
    698 
    699 	nusers = getutentries(NULL, &ehead);
    700 	if (oehead != ehead) {
    701 		freeutentries(oehead);
    702 		oehead = ehead;
    703 	}
    704 
    705 	if (nusers != onusers) {
    706 		if (nusers == 1)
    707 			mvprintw(STATROW, STATCOL + 8, " ");
    708 		else
    709 			mvprintw(STATROW, STATCOL + 8, "s");
    710 	}
    711 	onusers = nusers;
    712 	return (nusers);
    713 }
    714 
    715 static float
    716 cputime(int indx)
    717 {
    718 	double t;
    719 	int i;
    720 
    721 	t = 0;
    722 	for (i = 0; i < CPUSTATES; i++)
    723 		t += cur.cp_time[i];
    724 	if (t == 0.0)
    725 		t = 1.0;
    726 	return (cur.cp_time[indx] * 100.0 / t);
    727 }
    728 
    729 void
    730 puthumanint(u_int64_t 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 	if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
    741 		hline('*', w);
    742 		return;
    743 	}
    744 	printw("%*s", w, b);
    745 }
    746 
    747 void
    748 putint(int n, int l, int c, int w)
    749 {
    750 	char b[128];
    751 
    752 	if (move(l, c) != OK)
    753 		return;
    754 	if (n == 0) {
    755 		hline(' ', w);
    756 		return;
    757 	}
    758 	(void)snprintf(b, sizeof b, "%*d", w, n);
    759 	if (strlen(b) > w) {
    760 		if (display_mode == TIME)
    761 			hline('*', w);
    762 		else
    763 			puthumanint(n, l, c, w);
    764 		return;
    765 	}
    766 	addstr(b);
    767 }
    768 
    769 void
    770 putfloat(double f, int l, int c, int w, int d, int nz)
    771 {
    772 	char b[128];
    773 
    774 	if (move(l, c) != OK)
    775 		return;
    776 	if (nz && f == 0.0) {
    777 		hline(' ', w);
    778 		return;
    779 	}
    780 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
    781 	if (strlen(b) > w) {
    782 		hline('*', w);
    783 		return;
    784 	}
    785 	addstr(b);
    786 }
    787 
    788 static void
    789 getinfo(struct Info *stats)
    790 {
    791 	int mib[2];
    792 	size_t size;
    793 	int i;
    794 
    795 	cpureadstats();
    796 	dkreadstats();
    797 	tpreadstats();
    798 	NREAD(X_NCHSTATS, &stats->nchstats, sizeof stats->nchstats);
    799 	if (nintr)
    800 		NREAD(X_INTRCNT, stats->intrcnt, nintr * LONG);
    801 	for (i = 0; i < nevcnt; i++)
    802 		KREAD(ie_head[i].ie_count, &stats->evcnt[i],
    803 		      sizeof stats->evcnt[i]);
    804 	size = sizeof(stats->uvmexp);
    805 	mib[0] = CTL_VM;
    806 	mib[1] = VM_UVMEXP2;
    807 	if (sysctl(mib, 2, &stats->uvmexp, &size, NULL, 0) < 0) {
    808 		error("can't get uvmexp: %s\n", strerror(errno));
    809 		memset(&stats->uvmexp, 0, sizeof(stats->uvmexp));
    810 	}
    811 	size = sizeof(stats->Total);
    812 	mib[0] = CTL_VM;
    813 	mib[1] = VM_METER;
    814 	if (sysctl(mib, 2, &stats->Total, &size, NULL, 0) < 0) {
    815 		error("Can't get kernel info: %s\n", strerror(errno));
    816 		memset(&stats->Total, 0, sizeof(stats->Total));
    817 	}
    818 }
    819 
    820 static void
    821 allocinfo(struct Info *stats)
    822 {
    823 
    824 	if (nintr &&
    825 	    (stats->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
    826 		error("calloc failed");
    827 		die(0);
    828 	}
    829 	if ((stats->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
    830 		error("calloc failed");
    831 		die(0);
    832 	}
    833 }
    834 
    835 static void
    836 copyinfo(struct Info *from, struct Info *to)
    837 {
    838 	long *intrcnt;
    839 	u_int64_t *evcnt;
    840 
    841 	intrcnt = to->intrcnt;
    842 	evcnt = to->evcnt;
    843 	*to = *from;
    844 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof *intrcnt);
    845 	memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
    846 }
    847 
    848 static void
    849 dinfo(int dn, int r, int c)
    850 {
    851 	double atime;
    852 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
    853 
    854 	mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
    855 	ADV;
    856 
    857 	putint((int)(cur.dk_seek[dn]/etime+0.5), r, c, DISKCOLWIDTH);
    858 	ADV;
    859 	putint((int)((cur.dk_rxfer[dn]+cur.dk_wxfer[dn])/etime+0.5),
    860 	    r, c, DISKCOLWIDTH);
    861 	ADV;
    862 	puthumanint((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / etime + 0.5,
    863 		    r, c, DISKCOLWIDTH);
    864 	ADV;
    865 
    866 	/* time busy in disk activity */
    867 	atime = cur.dk_time[dn].tv_sec + cur.dk_time[dn].tv_usec / 1000000.0;
    868 	atime = atime * 100.0 / etime;
    869 	if (atime >= 100)
    870 		putint(100, r, c, DISKCOLWIDTH);
    871 	else
    872 		putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
    873 }
    874 
    875 static void
    876 tinfo(int dn, int r, int c)
    877 {
    878 	double atime;
    879 
    880 	mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, tp_name[dn]);
    881 	ADV;
    882 	ADV; /* skip over the seeks column - not relevant for tape drives */
    883 
    884 	putint((int)((cur_tape.rxfer[dn]+cur_tape.wxfer[dn])/etime+0.5),
    885 	    r, c, DISKCOLWIDTH);
    886 	ADV;
    887 	puthumanint((cur_tape.rbytes[dn] + cur_tape.wbytes[dn]) / etime + 0.5,
    888 		    r, c, DISKCOLWIDTH);
    889 	ADV;
    890 
    891 	/* time busy in disk activity */
    892 	atime = cur_tape.time[dn].tv_sec + cur_tape.time[dn].tv_usec / 1000000.0;
    893 	atime = atime * 100.0 / etime;
    894 	if (atime >= 100)
    895 		putint(100, r, c, DISKCOLWIDTH);
    896 	else
    897 		putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
    898 #undef ADV
    899 }
    900 
    901 
    902