Home | History | Annotate | Line # | Download | only in systat
vmstat.c revision 1.53
      1 /*	$NetBSD: vmstat.c,v 1.53 2003/08/07 11:16:01 agc 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.53 2003/08/07 11:16:01 agc 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 
    173 	if (!NREAD(X_ALLEVENTS, &allevents, sizeof allevents))
    174 		return;
    175 	evptr = allevents.tqh_first;
    176 	for (; evptr != NULL; evptr = evcnt.ev_list.tqe_next) {
    177 		if (!KREAD(evptr, &evcnt, sizeof evcnt))
    178 			return;
    179 		if (evcnt.ev_type != EVCNT_TYPE_INTR)
    180 			continue;
    181 		ie_head = realloc(ie_head, sizeof *ie * (nevcnt + 1));
    182 		if (ie_head == NULL) {
    183 			error("realloc failed");
    184 			die(0);
    185 		}
    186 		ie = ie_head + nevcnt;
    187 		ie->ie_group = malloc(evcnt.ev_grouplen + 1);
    188 		ie->ie_name = malloc(evcnt.ev_namelen + 1);
    189 		if (ie->ie_group == NULL || ie->ie_name == NULL)
    190 			return;
    191 		if (!KREAD(evcnt.ev_group, ie->ie_group, evcnt.ev_grouplen + 1))
    192 			return;
    193 		if (!KREAD(evcnt.ev_name, ie->ie_name, evcnt.ev_namelen + 1))
    194 			return;
    195 		ie->ie_count = &evptr->ev_count;
    196 		ie->ie_loc = 0;
    197 		nevcnt++;
    198 	}
    199 }
    200 
    201 int
    202 initvmstat(void)
    203 {
    204 	char *intrnamebuf, *cp;
    205 	int i;
    206 
    207 	if (namelist[0].n_type == 0) {
    208 		if (kvm_nlist(kd, namelist)) {
    209 			nlisterr(namelist);
    210 			return(0);
    211 		}
    212 		if (namelist[0].n_type == 0) {
    213 			error("No namelist");
    214 			return(0);
    215 		}
    216 	}
    217 	hertz = stathz ? stathz : hz;
    218 	if (!dkinit(1))
    219 		return(0);
    220 
    221 	/* Old style interrupt counts - deprecated */
    222 	nintr = (namelist[X_EINTRCNT].n_value -
    223 		namelist[X_INTRCNT].n_value) / sizeof (long);
    224 	intrloc = calloc(nintr, sizeof (long));
    225 	intrname = calloc(nintr, sizeof (long));
    226 	intrnamebuf = malloc(namelist[X_EINTRNAMES].n_value -
    227 		namelist[X_INTRNAMES].n_value);
    228 	if (intrnamebuf == NULL || intrname == 0 || intrloc == 0) {
    229 		error("Out of memory\n");
    230 		if (intrnamebuf)
    231 			free(intrnamebuf);
    232 		if (intrname)
    233 			free(intrname);
    234 		if (intrloc)
    235 			free(intrloc);
    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 	/* event counter interrupt counts */
    247 	get_interrupt_events();
    248 
    249 	nextintsrow = INTSROW + 1;
    250 	allocinfo(&s);
    251 	allocinfo(&s1);
    252 	allocinfo(&s2);
    253 	allocinfo(&z);
    254 
    255 	getinfo(&s2, RUN);
    256 	copyinfo(&s2, &s1);
    257 	return(1);
    258 }
    259 
    260 void
    261 fetchvmstat(void)
    262 {
    263 	time_t now;
    264 
    265 	time(&now);
    266 	strlcpy(buf, ctime(&now), sizeof(buf));
    267 	buf[19] = '\0';
    268 	getinfo(&s, state);
    269 }
    270 
    271 static void
    272 print_ie_title(int i)
    273 {
    274 	int width, name_width, group_width;
    275 
    276 	width = INTSCOLEND - (INTSCOL + 9);
    277 	if (width <= 0)
    278 		return;
    279 
    280 	move(ie_head[i].ie_loc, INTSCOL + 9);
    281 	group_width = strlen(ie_head[i].ie_group);
    282 	name_width = strlen(ie_head[i].ie_name);
    283 	width -= group_width + 1 + name_width;
    284 	if (width < 0) {
    285 		/*
    286 		 * Screen to narrow for full strings
    287 		 * This is all rather horrid, in some cases there are a lot
    288 		 * of events in the same group, and in others the event
    289 		 * name is "intr".  There are also names which need 7 or 8
    290 		 * columns before they become meaningful.
    291 		 * This is a bad compromise.
    292 		 */
    293 		width = -width;
    294 		group_width -= (width + 1) / 2;
    295 		name_width -= width / 2;
    296 		/* some have the 'useful' name "intr", display their group */
    297 		if (strcasecmp(ie_head[i].ie_name, "intr") == 0) {
    298 			 group_width += name_width + 1;
    299 			 name_width = 0;
    300 		} else {
    301 			if (group_width <= 3 || name_width < 0) {
    302 				/* don't display group */
    303 				name_width += group_width + 1;
    304 				group_width = 0;
    305 			}
    306 		}
    307 	}
    308 
    309 	if (group_width != 0) {
    310 		printw("%-.*s", group_width, ie_head[i].ie_group);
    311 		if (name_width != 0)
    312 			printw(" ");
    313 	}
    314 	if (name_width != 0)
    315 		printw("%-.*s", name_width, ie_head[i].ie_name);
    316 }
    317 
    318 void
    319 labelvmstat(void)
    320 {
    321 	int i;
    322 
    323 	clear();
    324 	mvprintw(STATROW, STATCOL + 4, "users    Load");
    325 	mvprintw(MEMROW, MEMCOL,     "          memory totals (in kB)");
    326 	mvprintw(MEMROW + 1, MEMCOL, "         real   virtual    free");
    327 	mvprintw(MEMROW + 2, MEMCOL, "Active");
    328 	mvprintw(MEMROW + 3, MEMCOL, "All");
    329 
    330 	mvprintw(PAGEROW, PAGECOL, "        PAGING   SWAPPING ");
    331 	mvprintw(PAGEROW + 1, PAGECOL, "        in  out   in  out ");
    332 	mvprintw(PAGEROW + 2, PAGECOL, "ops");
    333 	mvprintw(PAGEROW + 3, PAGECOL, "pages");
    334 
    335 	mvprintw(INTSROW, INTSCOL + 9, "Interrupts");
    336 
    337 	mvprintw(VMSTATROW + 0, VMSTATCOL + 10, "forks");
    338 	mvprintw(VMSTATROW + 1, VMSTATCOL + 10, "fkppw");
    339 	mvprintw(VMSTATROW + 2, VMSTATCOL + 10, "fksvm");
    340 	mvprintw(VMSTATROW + 3, VMSTATCOL + 10, "pwait");
    341 	mvprintw(VMSTATROW + 4, VMSTATCOL + 10, "relck");
    342 	mvprintw(VMSTATROW + 5, VMSTATCOL + 10, "rlkok");
    343 	mvprintw(VMSTATROW + 6, VMSTATCOL + 10, "noram");
    344 	mvprintw(VMSTATROW + 7, VMSTATCOL + 10, "ndcpy");
    345 	mvprintw(VMSTATROW + 8, VMSTATCOL + 10, "fltcp");
    346 	mvprintw(VMSTATROW + 9, VMSTATCOL + 10, "zfod");
    347 	mvprintw(VMSTATROW + 10, VMSTATCOL + 10, "cow");
    348 	mvprintw(VMSTATROW + 11, VMSTATCOL + 10, "fmin");
    349 	mvprintw(VMSTATROW + 12, VMSTATCOL + 10, "ftarg");
    350 	mvprintw(VMSTATROW + 13, VMSTATCOL + 10, "itarg");
    351 	mvprintw(VMSTATROW + 14, VMSTATCOL + 10, "wired");
    352 	mvprintw(VMSTATROW + 15, VMSTATCOL + 10, "pdfre");
    353 	if (LINES - 1 > VMSTATROW + 16)
    354 		mvprintw(VMSTATROW + 16, VMSTATCOL + 10, "pdscn");
    355 
    356 	mvprintw(GENSTATROW, GENSTATCOL, " Csw   Trp   Sys  Int  Sof   Flt");
    357 
    358 	mvprintw(GRAPHROW, GRAPHCOL,
    359 		"    . %% Sy    . %% Us    . %% Ni    . %% In    . %% Id");
    360 	mvprintw(PROCSROW, PROCSCOL, "Proc:r  d  s  w");
    361 	mvprintw(GRAPHROW + 1, GRAPHCOL,
    362 		"|    |    |    |    |    |    |    |    |    |    |");
    363 
    364 	mvprintw(NAMEIROW, NAMEICOL, "Namei         Sys-cache     Proc-cache");
    365 	mvprintw(NAMEIROW + 1, NAMEICOL,
    366 		"    Calls     hits    %%     hits     %%");
    367 	mvprintw(DISKROW, DISKCOL, "Disks:");
    368 	if (disk_horiz) {
    369 		mvprintw(DISKROW + 1, DISKCOL + 1, "seeks");
    370 		mvprintw(DISKROW + 2, DISKCOL + 1, "xfers");
    371 		mvprintw(DISKROW + 3, DISKCOL + 1, "bytes");
    372 		mvprintw(DISKROW + 4, DISKCOL + 1, "%%busy");
    373 	} else {
    374 		mvprintw(DISKROW, DISKCOL + 1 + 1 * DISKCOLWIDTH, "seeks");
    375 		mvprintw(DISKROW, DISKCOL + 1 + 2 * DISKCOLWIDTH, "xfers");
    376 		mvprintw(DISKROW, DISKCOL + 1 + 3 * DISKCOLWIDTH, "bytes");
    377 		mvprintw(DISKROW, DISKCOL + 1 + 4 * DISKCOLWIDTH, "%%busy");
    378 	}
    379 	for (i = 0; i < nintr; i++) {
    380 		if (intrloc[i] == 0)
    381 			continue;
    382 		mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
    383 			INTSCOLEND - (INTSCOL + 9), intrname[i]);
    384 	}
    385 	for (i = 0; i < nevcnt; i++) {
    386 		if (ie_head[i].ie_loc == 0)
    387 			continue;
    388 		print_ie_title(i);
    389 	}
    390 }
    391 
    392 #define X(fld)	{t=s.fld[i]; s.fld[i]-=s1.fld[i]; if(state==TIME) s1.fld[i]=t;}
    393 #define Y(fld)	{t = s.fld; s.fld -= s1.fld; if(state == TIME) s1.fld = t;}
    394 #define Z(fld)	{t = s.nchstats.fld; s.nchstats.fld -= s1.nchstats.fld; \
    395 	if(state == TIME) s1.nchstats.fld = t;}
    396 #define PUTRATE(fld, l, c, w) {Y(fld); putint((int)((float)s.fld/etime + 0.5), l, c, w);}
    397 #define MAXFAIL 5
    398 
    399 static	char cpuchar[CPUSTATES] = { '=' , '>', '-', '%', ' ' };
    400 static	char cpuorder[CPUSTATES] = { CP_SYS, CP_USER, CP_NICE, CP_INTR, CP_IDLE };
    401 
    402 void
    403 showvmstat(void)
    404 {
    405 	float f1, f2;
    406 	int psiz, inttotal;
    407 	int i, l, r, c;
    408 	static int failcnt = 0;
    409 	static int relabel = 0;
    410 	static int last_disks = 0;
    411 
    412 	if (relabel) {
    413 		labelvmstat();
    414 		relabel = 0;
    415 	}
    416 
    417 	if (state == TIME) {
    418 		dkswap();
    419 		etime = cur.cp_etime;
    420 		/* < 5 ticks - ignore this trash */
    421 		if ((etime * hertz) < 1.0) {
    422 			if (failcnt++ > MAXFAIL)
    423 				return;
    424 			clear();
    425 			mvprintw(2, 10, "The alternate system clock has died!");
    426 			mvprintw(3, 10, "Reverting to ``pigs'' display.");
    427 			move(CMDLINE, 0);
    428 			refresh();
    429 			failcnt = 0;
    430 			sleep(5);
    431 			command("pigs");
    432 			return;
    433 		}
    434 	} else
    435 		etime = 1.0;
    436 
    437 	failcnt = 0;
    438 	inttotal = 0;
    439 	for (i = 0; i < nintr; i++) {
    440 		if (s.intrcnt[i] == 0)
    441 			continue;
    442 		if (intrloc[i] == 0) {
    443 			if (nextintsrow == LINES)
    444 				continue;
    445 			intrloc[i] = nextintsrow++;
    446 			mvprintw(intrloc[i], INTSCOL + 9, "%-.*s",
    447 				INTSCOLEND - (INTSCOL + 9), intrname[i]);
    448 		}
    449 		X(intrcnt);
    450 		l = (int)((float)s.intrcnt[i]/etime + 0.5);
    451 		inttotal += l;
    452 		putint(l, intrloc[i], INTSCOL, 8);
    453 	}
    454 	for (i = 0; i < nevcnt; i++) {
    455 		if (s.evcnt[i] == 0)
    456 			continue;
    457 		if (ie_head[i].ie_loc == 0) {
    458 			if (nextintsrow == LINES)
    459 				continue;
    460 			ie_head[i].ie_loc = nextintsrow++;
    461 			print_ie_title(i);
    462 		}
    463 		X(evcnt);
    464 		l = (int)((float)s.evcnt[i]/etime + 0.5);
    465 		inttotal += l;
    466 		putint(l, ie_head[i].ie_loc, INTSCOL, 8);
    467 	}
    468 	putint(inttotal, INTSROW, INTSCOL, 8);
    469 	Z(ncs_goodhits); Z(ncs_badhits); Z(ncs_miss);
    470 	Z(ncs_long); Z(ncs_pass2); Z(ncs_2passes);
    471 	s.nchcount = nchtotal.ncs_goodhits + nchtotal.ncs_badhits +
    472 	    nchtotal.ncs_miss + nchtotal.ncs_long;
    473 	if (state == TIME)
    474 		s1.nchcount = s.nchcount;
    475 
    476 	psiz = 0;
    477 	f2 = 0.0;
    478 
    479 	/*
    480 	 * Last CPU state not calculated yet.
    481 	 */
    482 	for (c = 0; c < CPUSTATES; c++) {
    483 		i = cpuorder[c];
    484 		f1 = cputime(i);
    485 		f2 += f1;
    486 		l = (int) ((f2 + 1.0) / 2.0) - psiz;
    487 		if (c == 0)
    488 			putfloat(f1, GRAPHROW, GRAPHCOL + 1, 5, 1, 0);
    489 		else
    490 			putfloat(f1, GRAPHROW, GRAPHCOL + 10 * c + 1, 5, 1, 0);
    491 		mvhline(GRAPHROW + 2, psiz, cpuchar[c], l);
    492 		psiz += l;
    493 	}
    494 
    495 	putint(ucount(), STATROW, STATCOL, 3);
    496 	putfloat(avenrun[0], STATROW, STATCOL + 17, 6, 2, 0);
    497 	putfloat(avenrun[1], STATROW, STATCOL + 23, 6, 2, 0);
    498 	putfloat(avenrun[2], STATROW, STATCOL + 29, 6, 2, 0);
    499 	mvaddstr(STATROW, STATCOL + 53, buf);
    500 #define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
    501 
    502 	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 6, 7);
    503 	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),	/* XXX */
    504 	    MEMROW + 2, MEMCOL + 16, 7);
    505 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 6, 7);
    506 	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
    507 	    MEMROW + 3, MEMCOL + 16, 7);
    508 	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 24, 7);
    509 	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
    510 	    MEMROW + 3, MEMCOL + 24, 7);
    511 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
    512 	putint(total.t_dw, PROCSROW + 1, PROCSCOL + 6, 3);
    513 	putint(total.t_sl, PROCSROW + 1, PROCSCOL + 9, 3);
    514 	putint(total.t_sw, PROCSROW + 1, PROCSCOL + 12, 3);
    515 	PUTRATE(uvmexp.forks, VMSTATROW + 0, VMSTATCOL + 3, 6);
    516 	PUTRATE(uvmexp.forks_ppwait, VMSTATROW + 1, VMSTATCOL + 3, 6);
    517 	PUTRATE(uvmexp.forks_sharevm, VMSTATROW + 2, VMSTATCOL + 3, 6);
    518 	PUTRATE(uvmexp.fltpgwait, VMSTATROW + 3, VMSTATCOL + 4, 5);
    519 	PUTRATE(uvmexp.fltrelck, VMSTATROW + 4, VMSTATCOL + 3, 6);
    520 	PUTRATE(uvmexp.fltrelckok, VMSTATROW + 5, VMSTATCOL + 3, 6);
    521 	PUTRATE(uvmexp.fltnoram, VMSTATROW + 6, VMSTATCOL + 3, 6);
    522 	PUTRATE(uvmexp.fltamcopy, VMSTATROW + 7, VMSTATCOL + 3, 6);
    523 	PUTRATE(uvmexp.flt_prcopy, VMSTATROW + 8, VMSTATCOL + 3, 6);
    524 	PUTRATE(uvmexp.flt_przero, VMSTATROW + 9, VMSTATCOL + 3, 6);
    525 	PUTRATE(uvmexp.flt_acow, VMSTATROW + 10, VMSTATCOL, 9);
    526 	putint(s.uvmexp.freemin, VMSTATROW + 11, VMSTATCOL, 9);
    527 	putint(s.uvmexp.freetarg, VMSTATROW + 12, VMSTATCOL, 9);
    528 	putint(s.uvmexp.inactarg, VMSTATROW + 13, VMSTATCOL, 9);
    529 	putint(s.uvmexp.wired, VMSTATROW + 14, VMSTATCOL, 9);
    530 	PUTRATE(uvmexp.pdfreed, VMSTATROW + 15, VMSTATCOL, 9);
    531 	if (LINES - 1 > VMSTATROW + 16)
    532 		PUTRATE(uvmexp.pdscans, VMSTATROW + 16, VMSTATCOL, 9);
    533 
    534 	PUTRATE(uvmexp.pageins, PAGEROW + 2, PAGECOL + 5, 5);
    535 	PUTRATE(uvmexp.pdpageouts, PAGEROW + 2, PAGECOL + 10, 5);
    536 	PUTRATE(uvmexp.swapins, PAGEROW + 2, PAGECOL + 15, 5);
    537 	PUTRATE(uvmexp.swapouts, PAGEROW + 2, PAGECOL + 20, 5);
    538 	PUTRATE(uvmexp.pgswapin, PAGEROW + 3, PAGECOL + 5, 5);
    539 	PUTRATE(uvmexp.pgswapout, PAGEROW + 3, PAGECOL + 10, 5);
    540 
    541 	PUTRATE(uvmexp.swtch, GENSTATROW + 1, GENSTATCOL, 4);
    542 	PUTRATE(uvmexp.traps, GENSTATROW + 1, GENSTATCOL + 4, 6);
    543 	PUTRATE(uvmexp.syscalls, GENSTATROW + 1, GENSTATCOL + 10, 6);
    544 	PUTRATE(uvmexp.intrs, GENSTATROW + 1, GENSTATCOL + 16, 5);
    545 	PUTRATE(uvmexp.softs, GENSTATROW + 1, GENSTATCOL + 21, 5);
    546 	PUTRATE(uvmexp.faults, GENSTATROW + 1, GENSTATCOL + 26, 6);
    547 	for (l = 0, i = 0, r = DISKROW, c = DISKCOL; i < dk_ndrive; i++) {
    548 		if (!dk_select[i])
    549 			continue;
    550 		if (disk_horiz)
    551 			c += DISKCOLWIDTH;
    552 		else
    553 			r++;
    554 		if (c + DISKCOLWIDTH > DISKCOLEND) {
    555 			if (disk_horiz && LINES - 1 - DISKROW >
    556 					(DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
    557 				disk_horiz = 0;
    558 				relabel = 1;
    559 			}
    560 			break;
    561 		}
    562 		if (r >= LINES - 1) {
    563 			if (!disk_horiz && LINES - 1 - DISKROW <
    564 					(DISKCOLEND - DISKCOL) / DISKCOLWIDTH) {
    565 				disk_horiz = 1;
    566 				relabel = 1;
    567 			}
    568 			break;
    569 		}
    570 		l++;
    571 
    572 		dinfo(i, r, c);
    573 	}
    574 	/* blank out if we lost any disks */
    575 	for (i = l; i < last_disks; i++) {
    576 		int j;
    577 		if (disk_horiz)
    578 			c += DISKCOLWIDTH;
    579 		else
    580 			r++;
    581 		for (j = 0; j < 5; j++) {
    582 			mvprintw(r, c, "%*s", DISKCOLWIDTH, "");
    583 			if (disk_horiz)
    584 				r++;
    585 			else
    586 				c += DISKCOLWIDTH;
    587 		}
    588 	}
    589 	last_disks = l;
    590 	putint(s.nchcount, NAMEIROW + 2, NAMEICOL, 9);
    591 	putint(nchtotal.ncs_goodhits, NAMEIROW + 2, NAMEICOL + 9, 9);
    592 #define nz(x)	((x) ? (x) : 1)
    593 	putfloat(nchtotal.ncs_goodhits * 100.0 / nz(s.nchcount),
    594 	   NAMEIROW + 2, NAMEICOL + 19, 4, 0, 1);
    595 	putint(nchtotal.ncs_pass2, NAMEIROW + 2, NAMEICOL + 23, 9);
    596 	putfloat(nchtotal.ncs_pass2 * 100.0 / nz(s.nchcount),
    597 	   NAMEIROW + 2, NAMEICOL + 34, 4, 0, 1);
    598 #undef nz
    599 }
    600 
    601 void
    602 vmstat_boot(char *args)
    603 {
    604 	copyinfo(&z, &s1);
    605 	state = BOOT;
    606 }
    607 
    608 void
    609 vmstat_run(char *args)
    610 {
    611 	copyinfo(&s1, &s2);
    612 	state = RUN;
    613 }
    614 
    615 void
    616 vmstat_time(char *args)
    617 {
    618 	state = TIME;
    619 }
    620 
    621 void
    622 vmstat_zero(char *args)
    623 {
    624 	if (state == RUN)
    625 		getinfo(&s1, RUN);
    626 }
    627 
    628 /* calculate number of users on the system */
    629 static int
    630 ucount(void)
    631 {
    632 	static int onusers = -1;
    633 	static struct utmpentry *oehead = NULL;
    634 	int nusers = 0;
    635 	struct utmpentry *ehead;
    636 
    637 	nusers = getutentries(NULL, &ehead);
    638 	if (oehead != ehead) {
    639 		freeutentries(oehead);
    640 		oehead = ehead;
    641 	}
    642 
    643 	if (nusers != onusers) {
    644 		if (nusers == 1)
    645 			mvprintw(STATROW, STATCOL + 8, " ");
    646 		else
    647 			mvprintw(STATROW, STATCOL + 8, "s");
    648 	}
    649 	onusers = nusers;
    650 	return (nusers);
    651 }
    652 
    653 static float
    654 cputime(int indx)
    655 {
    656 	double t;
    657 	int i;
    658 
    659 	t = 0;
    660 	for (i = 0; i < CPUSTATES; i++)
    661 		t += cur.cp_time[i];
    662 	if (t == 0.0)
    663 		t = 1.0;
    664 	return (cur.cp_time[indx] * 100.0 / t);
    665 }
    666 
    667 static void
    668 puthumanint(u_int64_t n, int l, int c, int w)
    669 {
    670 	char b[128];
    671 
    672 	if (move(l, c) != OK)
    673 		return;
    674 	if (n == 0) {
    675 		hline(' ', w);
    676 		return;
    677 	}
    678 	if (humanize_number(b, w, n, "", HN_AUTOSCALE, HN_NOSPACE) == -1 ) {
    679 		hline('*', w);
    680 		return;
    681 	}
    682 	hline(' ', w - strlen(b));
    683 	mvaddstr(l, c + w - strlen(b), b);
    684 }
    685 
    686 static void
    687 putint(int n, int l, int c, int w)
    688 {
    689 	char b[128];
    690 
    691 	if (move(l, c) != OK)
    692 		return;
    693 	if (n == 0) {
    694 		hline(' ', w);
    695 		return;
    696 	}
    697 	(void)snprintf(b, sizeof b, "%*d", w, n);
    698 	if (strlen(b) > w) {
    699 		hline('*', w);
    700 		return;
    701 	}
    702 	addstr(b);
    703 }
    704 
    705 static void
    706 putfloat(double f, int l, int c, int w, int d, int nz)
    707 {
    708 	char b[128];
    709 
    710 	if (move(l, c) != OK)
    711 		return;
    712 	if (nz && f == 0.0) {
    713 		hline(' ', w);
    714 		return;
    715 	}
    716 	(void)snprintf(b, sizeof b, "%*.*f", w, d, f);
    717 	if (strlen(b) > w) {
    718 		hline('*', w);
    719 		return;
    720 	}
    721 	addstr(b);
    722 }
    723 
    724 static void
    725 getinfo(struct Info *s, enum state st)
    726 {
    727 	int mib[2];
    728 	size_t size;
    729 	int i;
    730 
    731 	dkreadstats();
    732 	NREAD(X_NCHSTATS, &s->nchstats, sizeof s->nchstats);
    733 	NREAD(X_INTRCNT, s->intrcnt, nintr * LONG);
    734 	for (i = 0; i < nevcnt; i++)
    735 		KREAD(ie_head[i].ie_count, &s->evcnt[i], sizeof s->evcnt[i]);
    736 	size = sizeof(s->uvmexp);
    737 	mib[0] = CTL_VM;
    738 	mib[1] = VM_UVMEXP2;
    739 	if (sysctl(mib, 2, &s->uvmexp, &size, NULL, 0) < 0) {
    740 		error("can't get uvmexp: %s\n", strerror(errno));
    741 		memset(&s->uvmexp, 0, sizeof(s->uvmexp));
    742 	}
    743 	size = sizeof(s->Total);
    744 	mib[0] = CTL_VM;
    745 	mib[1] = VM_METER;
    746 	if (sysctl(mib, 2, &s->Total, &size, NULL, 0) < 0) {
    747 		error("Can't get kernel info: %s\n", strerror(errno));
    748 		memset(&s->Total, 0, sizeof(s->Total));
    749 	}
    750 }
    751 
    752 static void
    753 allocinfo(struct Info *s)
    754 {
    755 
    756 	if ((s->intrcnt = calloc(nintr, sizeof(long))) == NULL) {
    757 		error("calloc failed");
    758 		die(0);
    759 	}
    760 	if ((s->evcnt = calloc(nevcnt, sizeof(u_int64_t))) == NULL) {
    761 		error("calloc failed");
    762 		die(0);
    763 	}
    764 }
    765 
    766 static void
    767 copyinfo(struct Info *from, struct Info *to)
    768 {
    769 	long *intrcnt;
    770 	u_int64_t *evcnt;
    771 
    772 	intrcnt = to->intrcnt;
    773 	evcnt = to->evcnt;
    774 	*to = *from;
    775 	memmove(to->intrcnt = intrcnt, from->intrcnt, nintr * sizeof (int));
    776 	memmove(to->evcnt = evcnt, from->evcnt, nevcnt * sizeof *evcnt);
    777 }
    778 
    779 static void
    780 dinfo(int dn, int r, int c)
    781 {
    782 	double atime;
    783 #define ADV if (disk_horiz) r++; else c += DISKCOLWIDTH
    784 
    785 	mvprintw(r, c, "%*.*s", DISKCOLWIDTH, DISKCOLWIDTH, dr_name[dn]);
    786 	ADV;
    787 
    788 	putint((int)(cur.dk_seek[dn]/etime+0.5), r, c, DISKCOLWIDTH);
    789 	ADV;
    790 	putint((int)((cur.dk_rxfer[dn]+cur.dk_wxfer[dn])/etime+0.5),
    791 	    r, c, DISKCOLWIDTH);
    792 	ADV;
    793 	puthumanint((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / etime + 0.5,
    794 		    r, c, DISKCOLWIDTH);
    795 	ADV;
    796 
    797 	/* time busy in disk activity */
    798 	atime = cur.dk_time[dn].tv_sec + cur.dk_time[dn].tv_usec / 1000000.0;
    799 	atime = atime * 100.0 / etime;
    800 	if (atime >= 100)
    801 		putint(100, r, c, DISKCOLWIDTH);
    802 	else
    803 		putfloat(atime, r, c, DISKCOLWIDTH, 1, 1);
    804 #undef ADV
    805 }
    806