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