Home | History | Annotate | Line # | Download | only in vmstat
vmstat.c revision 1.72
      1 /* $NetBSD: vmstat.c,v 1.72 2000/11/30 12:02:19 simonb Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1980, 1986, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 #ifndef lint
     75 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
     76 	The Regents of the University of California.  All rights reserved.\n");
     77 #endif /* not lint */
     78 
     79 #ifndef lint
     80 #if 0
     81 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 3/1/95";
     82 #else
     83 __RCSID("$NetBSD: vmstat.c,v 1.72 2000/11/30 12:02:19 simonb Exp $");
     84 #endif
     85 #endif /* not lint */
     86 
     87 #define	__POOL_EXPOSE
     88 
     89 #include <sys/param.h>
     90 #include <sys/time.h>
     91 #include <sys/proc.h>
     92 #include <sys/user.h>
     93 #include <sys/dkstat.h>
     94 #include <sys/buf.h>
     95 #include <sys/namei.h>
     96 #include <sys/malloc.h>
     97 #include <sys/ioctl.h>
     98 #include <sys/sched.h>
     99 #include <sys/sysctl.h>
    100 #include <sys/device.h>
    101 #include <sys/pool.h>
    102 
    103 #include <uvm/uvm_extern.h>
    104 #include <uvm/uvm_stat.h>
    105 
    106 #include <err.h>
    107 #include <fcntl.h>
    108 #include <time.h>
    109 #include <nlist.h>
    110 #include <kvm.h>
    111 #include <errno.h>
    112 #include <unistd.h>
    113 #include <signal.h>
    114 #include <stdio.h>
    115 #include <ctype.h>
    116 #include <stdlib.h>
    117 #include <string.h>
    118 #include <paths.h>
    119 #include <limits.h>
    120 #include "dkstats.h"
    121 
    122 struct nlist namelist[] = {
    123 #define	X_BOOTTIME	0
    124 	{ "_boottime" },
    125 #define X_HZ		1
    126 	{ "_hz" },
    127 #define X_STATHZ	2
    128 	{ "_stathz" },
    129 #define X_NCHSTATS	3
    130 	{ "_nchstats" },
    131 #define	X_INTRNAMES	4
    132 	{ "_intrnames" },
    133 #define	X_EINTRNAMES	5
    134 	{ "_eintrnames" },
    135 #define	X_INTRCNT	6
    136 	{ "_intrcnt" },
    137 #define	X_EINTRCNT	7
    138 	{ "_eintrcnt" },
    139 #define	X_KMEMSTAT	8
    140 	{ "_kmemstats" },
    141 #define	X_KMEMBUCKETS	9
    142 	{ "_bucket" },
    143 #define X_ALLEVENTS	10
    144 	{ "_allevents" },
    145 #define X_POOLHEAD	11
    146 	{ "_pool_head" },
    147 #define	X_UVMEXP	12
    148 	{ "_uvmexp" },
    149 #define X_END		13
    150 #if defined(pc532)
    151 #define	X_IVT		(X_END)
    152 	{ "_ivt" },
    153 #endif
    154 	{ "" },
    155 };
    156 
    157 /* Objects defined in dkstats.c */
    158 extern struct _disk	cur;
    159 extern char	**dr_name;
    160 extern int	*dk_select, dk_ndrive;
    161 
    162 struct	uvmexp uvmexp, ouvmexp;
    163 int		ndrives;
    164 
    165 int	winlines = 20;
    166 
    167 kvm_t *kd;
    168 
    169 #define	FORKSTAT	0x01
    170 #define	INTRSTAT	0x02
    171 #define	MEMSTAT		0x04
    172 #define	SUMSTAT		0x08
    173 #define	EVCNTSTAT	0x10
    174 #define	VMSTAT		0x20
    175 #define	HISTLIST	0x40
    176 #define	HISTDUMP	0x80
    177 
    178 void	cpustats __P((void));
    179 void	dkstats __P((void));
    180 void	doevcnt __P((int verbose));
    181 void	dointr __P((int verbose));
    182 void	domem __P((void));
    183 void	dopool __P((void));
    184 void	dosum __P((void));
    185 void	dovmstat __P((u_int, int));
    186 void	kread __P((int, void *, size_t));
    187 void	needhdr __P((int));
    188 long	getuptime __P((void));
    189 void	printhdr __P((void));
    190 long	pct __P((long, long));
    191 void	usage __P((void));
    192 void	doforkst __P((void));
    193 
    194 void	hist_traverse __P((int, const char *));
    195 void	hist_dodump __P((struct uvm_history *));
    196 
    197 int	main __P((int, char **));
    198 char	**choosedrives __P((char **));
    199 
    200 extern int dkinit __P((int, gid_t));
    201 extern void dkreadstats __P((void));
    202 extern void dkswap __P((void));
    203 
    204 /* Namelist and memory file names. */
    205 char	*nlistf, *memf;
    206 
    207 /* allow old usage [vmstat 1] */
    208 #define	BACKWARD_COMPATIBILITY
    209 
    210 int
    211 main(argc, argv)
    212 	int argc;
    213 	char **argv;
    214 {
    215 	int c, todo, verbose;
    216 	u_int interval;
    217 	int reps;
    218         char errbuf[_POSIX2_LINE_MAX];
    219 	gid_t	egid = getegid();
    220 	const char *histname = NULL;
    221 
    222 	(void)setegid(getgid());
    223 	memf = nlistf = NULL;
    224 	interval = reps = todo = verbose = 0;
    225 	while ((c = getopt(argc, argv, "c:efh:HilM:mN:svw:")) != -1) {
    226 		switch (c) {
    227 		case 'c':
    228 			reps = atoi(optarg);
    229 			break;
    230 		case 'e':
    231 			todo |= EVCNTSTAT;
    232 			break;
    233 		case 'f':
    234 			todo |= FORKSTAT;
    235 			break;
    236 		case 'h':
    237 			histname = optarg;
    238 			/* FALLTHROUGH */
    239 		case 'H':
    240 			todo |= HISTDUMP;
    241 			break;
    242 		case 'i':
    243 			todo |= INTRSTAT;
    244 			break;
    245 		case 'l':
    246 			todo |= HISTLIST;
    247 			break;
    248 		case 'M':
    249 			memf = optarg;
    250 			break;
    251 		case 'm':
    252 			todo |= MEMSTAT;
    253 			break;
    254 		case 'N':
    255 			nlistf = optarg;
    256 			break;
    257 		case 's':
    258 			todo |= SUMSTAT;
    259 			break;
    260 		case 'v':
    261 			verbose = 1;
    262 			break;
    263 		case 'w':
    264 			interval = atoi(optarg);
    265 			break;
    266 		case '?':
    267 		default:
    268 			usage();
    269 		}
    270 	}
    271 	argc -= optind;
    272 	argv += optind;
    273 
    274 	if (todo == 0)
    275 		todo = VMSTAT;
    276 
    277 	/*
    278 	 * Discard setgid privileges.  If not the running kernel, we toss
    279 	 * them away totally so that bad guys can't print interesting stuff
    280 	 * from kernel memory, otherwise switch back to kmem for the
    281 	 * duration of the kvm_openfiles() call.
    282 	 */
    283 	if (nlistf != NULL || memf != NULL)
    284 		(void)setgid(getgid());
    285 	else
    286 		(void)setegid(egid);
    287 
    288         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
    289 	if (kd == 0)
    290 		errx(1, "kvm_openfiles: %s\n", errbuf);
    291 
    292 	if (nlistf == NULL && memf == NULL) {
    293 		if (todo & VMSTAT)
    294 			(void)setegid(getgid());	/* XXX: dkinit */
    295 		else
    296 			(void)setgid(getgid());
    297 	}
    298 
    299 	if ((c = kvm_nlist(kd, namelist)) != 0) {
    300 		if (c > 0) {
    301 			(void)fprintf(stderr,
    302 			    "vmstat: undefined symbols:");
    303 			for (c = 0;
    304 			    c < sizeof(namelist)/sizeof(namelist[0]); c++)
    305 				if (namelist[c].n_type == 0)
    306 					fprintf(stderr, " %s",
    307 					    namelist[c].n_name);
    308 			(void)fputc('\n', stderr);
    309 		} else
    310 			(void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
    311 			    kvm_geterr(kd));
    312 		exit(1);
    313 	}
    314 
    315 	if (todo & VMSTAT) {
    316 		struct winsize winsize;
    317 
    318 		dkinit(0, egid); /* Initialize disk stats, no disks selected. */
    319 
    320 		(void)setgid(getgid()); /* don't need privs anymore */
    321 
    322 		argv = choosedrives(argv);	/* Select disks. */
    323 		winsize.ws_row = 0;
    324 		(void)ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
    325 		if (winsize.ws_row > 0)
    326 			winlines = winsize.ws_row;
    327 
    328 	}
    329 
    330 #ifdef	BACKWARD_COMPATIBILITY
    331 	if (*argv) {
    332 		interval = atoi(*argv);
    333 		if (*++argv)
    334 			reps = atoi(*argv);
    335 	}
    336 #endif
    337 
    338 	if (interval) {
    339 		if (!reps)
    340 			reps = -1;
    341 	} else if (reps)
    342 		interval = 1;
    343 
    344 	if (todo & (HISTLIST|HISTDUMP)) {
    345 		if ((todo & (HISTLIST|HISTDUMP)) == (HISTLIST|HISTDUMP))
    346 			errx(1, "you may list or dump, but not both!");
    347 		hist_traverse(todo, histname);
    348 	}
    349 	if (todo & FORKSTAT)
    350 		doforkst();
    351 	if (todo & MEMSTAT) {
    352 		domem();
    353 		dopool();
    354 	}
    355 	if (todo & SUMSTAT)
    356 		dosum();
    357 	if (todo & INTRSTAT)
    358 		dointr(verbose);
    359 	if (todo & EVCNTSTAT)
    360 		doevcnt(verbose);
    361 	if (todo & VMSTAT)
    362 		dovmstat(interval, reps);
    363 	exit(0);
    364 }
    365 
    366 char **
    367 choosedrives(argv)
    368 	char **argv;
    369 {
    370 	int i;
    371 
    372 	/*
    373 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    374 	 * supplied as arguments, default drives.  If everything isn't filled
    375 	 * in and there are drives not taken care of, display the first few
    376 	 * that fit.
    377 	 */
    378 #define BACKWARD_COMPATIBILITY
    379 	for (ndrives = 0; *argv; ++argv) {
    380 #ifdef	BACKWARD_COMPATIBILITY
    381 		if (isdigit(**argv))
    382 			break;
    383 #endif
    384 		for (i = 0; i < dk_ndrive; i++) {
    385 			if (strcmp(dr_name[i], *argv))
    386 				continue;
    387 			dk_select[i] = 1;
    388 			++ndrives;
    389 			break;
    390 		}
    391 	}
    392 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
    393 		if (dk_select[i])
    394 			continue;
    395 		dk_select[i] = 1;
    396 		++ndrives;
    397 	}
    398 	return(argv);
    399 }
    400 
    401 long
    402 getuptime()
    403 {
    404 	static time_t now;
    405 	static struct timeval boottime;
    406 	time_t uptime;
    407 
    408 	if (boottime.tv_sec == 0)
    409 		kread(X_BOOTTIME, &boottime, sizeof(boottime));
    410 	(void)time(&now);
    411 	uptime = now - boottime.tv_sec;
    412 	if (uptime <= 0 || uptime > 60*60*24*365*10) {
    413 		(void)fprintf(stderr,
    414 		    "vmstat: time makes no sense; namelist must be wrong.\n");
    415 		exit(1);
    416 	}
    417 	return(uptime);
    418 }
    419 
    420 int	hz, hdrcnt;
    421 
    422 void
    423 dovmstat(interval, reps)
    424 	u_int interval;
    425 	int reps;
    426 {
    427 	struct vmtotal total;
    428 	time_t uptime, halfuptime;
    429 	int mib[2];
    430 	size_t size;
    431 	int pagesize = getpagesize();
    432 
    433 	uptime = getuptime();
    434 	halfuptime = uptime / 2;
    435 	(void)signal(SIGCONT, needhdr);
    436 
    437 	if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
    438 		kread(X_STATHZ, &hz, sizeof(hz));
    439 	if (!hz)
    440 		kread(X_HZ, &hz, sizeof(hz));
    441 
    442 	for (hdrcnt = 1;;) {
    443 		if (!--hdrcnt)
    444 			printhdr();
    445 		/* Read new disk statistics */
    446 		dkreadstats();
    447 		kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
    448 		if (memf != NULL) {
    449 			/*
    450 			 * XXX Can't do this if we're reading a crash
    451 			 * XXX dump because they're lazily-calculated.
    452 			 */
    453 			printf("Unable to get vmtotals from crash dump.\n");
    454 			memset(&total, 0, sizeof(total));
    455 		} else {
    456 			size = sizeof(total);
    457 			mib[0] = CTL_VM;
    458 			mib[1] = VM_METER;
    459 			if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
    460 				printf("Can't get vmtotals: %s\n",
    461 				    strerror(errno));
    462 				memset(&total, 0, sizeof(total));
    463 			}
    464 		}
    465 		(void)printf("%2d%2d%2d",
    466 		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
    467 #define pgtok(a) (long)((a) * (pagesize >> 10))
    468 #define	rate(x)	(u_long)(((x) + halfuptime) / uptime)	/* round */
    469 		(void)printf("%6ld%6ld ",
    470 		    pgtok(total.t_avm), pgtok(total.t_free));
    471 		(void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults));
    472 		(void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact));
    473 		(void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins));
    474 		(void)printf("%4lu ",
    475 		    rate(uvmexp.pgswapout - ouvmexp.pgswapout));
    476 		(void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed));
    477 		(void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans));
    478 		dkstats();
    479 		(void)printf("%4lu %4lu %3lu ",
    480 		    rate(uvmexp.intrs - ouvmexp.intrs),
    481 		    rate(uvmexp.syscalls - ouvmexp.syscalls),
    482 		    rate(uvmexp.swtch - ouvmexp.swtch));
    483 		cpustats();
    484 		(void)printf("\n");
    485 		(void)fflush(stdout);
    486 		if (reps >= 0 && --reps <= 0)
    487 			break;
    488 		ouvmexp = uvmexp;
    489 		uptime = interval;
    490 		/*
    491 		 * We round upward to avoid losing low-frequency events
    492 		 * (i.e., >= 1 per interval but < 1 per second).
    493 		 */
    494 		halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
    495 		(void)sleep(interval);
    496 	}
    497 }
    498 
    499 void
    500 printhdr()
    501 {
    502 	int i;
    503 
    504 	(void)printf(" procs   memory     page%*s", 23, "");
    505 	if (ndrives > 0)
    506 		(void)printf("%s %*sfaults      cpu\n",
    507 		   ((ndrives > 1) ? "disks" : "disk"),
    508 		   ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
    509 	else
    510 		(void)printf("%*s  faults   cpu\n",
    511 		   ndrives * 3, "");
    512 
    513 	(void)printf(" r b w   avm   fre  flt  re  pi   po   fr   sr ");
    514 	for (i = 0; i < dk_ndrive; i++)
    515 		if (dk_select[i])
    516 			(void)printf("%c%c ", dr_name[i][0],
    517 			    dr_name[i][strlen(dr_name[i]) - 1]);
    518 	(void)printf("  in   sy  cs us sy id\n");
    519 	hdrcnt = winlines - 2;
    520 }
    521 
    522 /*
    523  * Force a header to be prepended to the next output.
    524  */
    525 void
    526 needhdr(dummy)
    527 	int dummy;
    528 {
    529 
    530 	hdrcnt = 1;
    531 }
    532 
    533 long
    534 pct(top, bot)
    535 	long top, bot;
    536 {
    537 	long ans;
    538 
    539 	if (bot == 0)
    540 		return(0);
    541 	ans = (quad_t)top * 100 / bot;
    542 	return (ans);
    543 }
    544 
    545 #define	PCT(top, bot) (int)pct((long)(top), (long)(bot))
    546 
    547 void
    548 dosum()
    549 {
    550 	struct nchstats nchstats;
    551 	long nchtotal;
    552 
    553 	kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
    554 
    555 	(void)printf("%9u bytes per page\n", uvmexp.pagesize);
    556 
    557 	(void)printf("%9u pages managed\n", uvmexp.npages);
    558 	(void)printf("%9u pages free\n", uvmexp.free);
    559 	(void)printf("%9u pages active\n", uvmexp.active);
    560 	(void)printf("%9u pages inactive\n", uvmexp.inactive);
    561 	(void)printf("%9u pages paging\n", uvmexp.paging);
    562 	(void)printf("%9u pages wired\n", uvmexp.wired);
    563 	(void)printf("%9u zero pages\n", uvmexp.zeropages);
    564 	(void)printf("%9u reserve pagedaemon pages\n",
    565 	    uvmexp.reserve_pagedaemon);
    566 	(void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
    567 	(void)printf("%9u anon pager pages\n", uvmexp.anonpages);
    568 	(void)printf("%9u vnode page cache pages\n", uvmexp.vnodepages);
    569 
    570 	(void)printf("%9u minimum free pages\n", uvmexp.freemin);
    571 	(void)printf("%9u target free pages\n", uvmexp.freetarg);
    572 	(void)printf("%9u target inactive pages\n", uvmexp.inactarg);
    573 	(void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
    574 
    575 	(void)printf("%9u swap devices\n", uvmexp.nswapdev);
    576 	(void)printf("%9u swap pages\n", uvmexp.swpages);
    577 	(void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
    578 	(void)printf("%9u swap allocations\n", uvmexp.nswget);
    579 	(void)printf("%9u anons\n", uvmexp.nanon);
    580 	(void)printf("%9u free anons\n", uvmexp.nfreeanon);
    581 
    582 	(void)printf("%9u total faults taken\n", uvmexp.faults);
    583 	(void)printf("%9u traps\n", uvmexp.traps);
    584 	(void)printf("%9u device interrupts\n", uvmexp.intrs);
    585 	(void)printf("%9u cpu context switches\n", uvmexp.swtch);
    586 	(void)printf("%9u software interrupts\n", uvmexp.softs);
    587 	(void)printf("%9u system calls\n", uvmexp.syscalls);
    588 	(void)printf("%9u pagein requests\n", uvmexp.pageins);
    589 	(void)printf("%9u pageout requests\n", uvmexp.pdpageouts);
    590 	(void)printf("%9u swap ins\n", uvmexp.swapins);
    591 	(void)printf("%9u swap outs\n", uvmexp.swapouts);
    592 	(void)printf("%9u pages swapped in\n", uvmexp.pgswapin);
    593 	(void)printf("%9u pages swapped out\n", uvmexp.pgswapout);
    594 	(void)printf("%9u forks total\n", uvmexp.forks);
    595 	(void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
    596 	(void)printf("%9u forks shared address space with parent\n",
    597 	    uvmexp.forks_sharevm);
    598 	(void)printf("%9u pagealloc zero wanted and avail\n",
    599 	    uvmexp.pga_zerohit);
    600 	(void)printf("%9u pagealloc zero wanted and not avail\n",
    601 	    uvmexp.pga_zeromiss);
    602 	(void)printf("%9u aborts of idle page zeroing\n",
    603 	    uvmexp.zeroaborts);
    604 
    605 	(void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
    606 	(void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
    607 	(void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
    608 	(void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
    609 	(void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
    610 	    uvmexp.fltrelckok);
    611 	(void)printf("%9u anon page faults\n", uvmexp.fltanget);
    612 	(void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
    613 	(void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
    614 	(void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
    615 	(void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
    616 	(void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
    617 	(void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
    618 	(void)printf("%9u anon faults\n", uvmexp.flt_anon);
    619 	(void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
    620 	(void)printf("%9u object faults\n", uvmexp.flt_obj);
    621 	(void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
    622 	(void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
    623 
    624 	(void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
    625 	(void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
    626 	(void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout);
    627 	(void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
    628 	(void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
    629 	(void)printf("%9u anonymous pages scanned by daemon\n", uvmexp.pdanscan);
    630 	(void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
    631 	(void)printf("%9u pages reactivated\n", uvmexp.pdreact);
    632 	(void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
    633 	(void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
    634 	(void)printf("%9u pages deactivated\n", uvmexp.pddeact);
    635 	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
    636 	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
    637 	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
    638 	    nchstats.ncs_miss + nchstats.ncs_long;
    639 	(void)printf("%9ld total name lookups\n", nchtotal);
    640 	(void)printf(
    641 	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
    642 	    "", PCT(nchstats.ncs_goodhits, nchtotal),
    643 	    PCT(nchstats.ncs_neghits, nchtotal),
    644 	    PCT(nchstats.ncs_pass2, nchtotal));
    645 	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
    646 	    PCT(nchstats.ncs_badhits, nchtotal),
    647 	    PCT(nchstats.ncs_falsehits, nchtotal),
    648 	    PCT(nchstats.ncs_long, nchtotal));
    649 }
    650 
    651 void
    652 doforkst()
    653 {
    654 
    655 	kread(X_UVMEXP, &uvmexp, sizeof(uvmexp));
    656 
    657 	(void)printf("%u forks total\n", uvmexp.forks);
    658 	(void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
    659 	(void)printf("%u forks shared address space with parent\n",
    660 	    uvmexp.forks_sharevm);
    661 }
    662 
    663 void
    664 dkstats()
    665 {
    666 	int dn, state;
    667 	double etime;
    668 
    669 	/* Calculate disk stat deltas. */
    670 	dkswap();
    671 	etime = 0;
    672 	for (state = 0; state < CPUSTATES; ++state) {
    673 		etime += cur.cp_time[state];
    674 	}
    675 	if (etime == 0)
    676 		etime = 1;
    677 	etime /= hz;
    678 	for (dn = 0; dn < dk_ndrive; ++dn) {
    679 		if (!dk_select[dn])
    680 			continue;
    681 		(void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
    682 	}
    683 }
    684 
    685 void
    686 cpustats()
    687 {
    688 	int state;
    689 	double pct, total;
    690 
    691 	total = 0;
    692 	for (state = 0; state < CPUSTATES; ++state)
    693 		total += cur.cp_time[state];
    694 	if (total)
    695 		pct = 100 / total;
    696 	else
    697 		pct = 0;
    698 	(void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
    699 	(void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
    700 	(void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
    701 }
    702 
    703 #if defined(pc532)
    704 /* To get struct iv ...*/
    705 #define _KERNEL
    706 #include <machine/psl.h>
    707 #undef _KERNEL
    708 void
    709 dointr(int verbose)
    710 {
    711 	long i, j, inttotal, uptime;
    712 	static char iname[64];
    713 	struct iv ivt[32], *ivp = ivt;
    714 
    715 	iname[63] = '\0';
    716 	uptime = getuptime();
    717 	kread(X_IVT, ivp, sizeof(ivt));
    718 
    719 	for (i = 0; i < 2; i++) {
    720 		(void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
    721 		(void)printf("interrupt       total     rate\n");
    722 		inttotal = 0;
    723 		for (j = 0; j < 16; j++, ivp++) {
    724 			if (ivp->iv_vec && ivp->iv_use &&
    725 			    (ivp->iv_cnt || verbose)) {
    726 				if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
    727 					(void)fprintf(stderr, "vmstat: iv_use: %s\n",
    728 					    kvm_geterr(kd));
    729 					exit(1);
    730 				}
    731 				(void)printf("%-12s %8ld %8ld\n", iname,
    732 				    ivp->iv_cnt, ivp->iv_cnt / uptime);
    733 				inttotal += ivp->iv_cnt;
    734 			}
    735 		}
    736 		(void)printf("Total        %8ld %8ld\n",
    737 		    inttotal, inttotal / uptime);
    738 	}
    739 }
    740 #else
    741 void
    742 dointr(int verbose)
    743 {
    744 	long *intrcnt;
    745 	long long inttotal, uptime;
    746 	int nintr, inamlen;
    747 	char *intrname;
    748 	struct evcntlist allevents;
    749 	struct evcnt evcnt, *evptr;
    750 	char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
    751 
    752 	uptime = getuptime();
    753 	nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
    754 	inamlen =
    755 	    namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
    756 	intrcnt = malloc((size_t)nintr);
    757 	intrname = malloc((size_t)inamlen);
    758 	if (intrcnt == NULL || intrname == NULL) {
    759 		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
    760 		exit(1);
    761 	}
    762 	kread(X_INTRCNT, intrcnt, (size_t)nintr);
    763 	kread(X_INTRNAMES, intrname, (size_t)inamlen);
    764 	(void)printf("%-24s %16s %8s\n", "interrupt", "total", "rate");
    765 	inttotal = 0;
    766 	nintr /= sizeof(long);
    767 	while (--nintr >= 0) {
    768 		if (*intrcnt || verbose)
    769 			(void)printf("%-24s %16lld %8lld\n", intrname,
    770 			    (long long)*intrcnt,
    771 			    (long long)(*intrcnt / uptime));
    772 		intrname += strlen(intrname) + 1;
    773 		inttotal += *intrcnt++;
    774 	}
    775 	kread(X_ALLEVENTS, &allevents, sizeof allevents);
    776 	evptr = allevents.tqh_first;
    777 	while (evptr) {
    778 		if (kvm_read(kd, (long)evptr, &evcnt, sizeof evcnt) != sizeof evcnt) {
    779 event_chain_trashed:
    780 			(void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
    781 			    kvm_geterr(kd));
    782 			exit(1);
    783 		}
    784 
    785 		evptr = evcnt.ev_list.tqe_next;
    786 		if (evcnt.ev_type != EVCNT_TYPE_INTR)
    787 			continue;
    788 
    789 		if (evcnt.ev_count == 0 && !verbose)
    790 			continue;
    791 
    792 		if (kvm_read(kd, (long)evcnt.ev_group, evgroup,
    793 		    evcnt.ev_grouplen + 1) != evcnt.ev_grouplen + 1)
    794 			goto event_chain_trashed;
    795 		if (kvm_read(kd, (long)evcnt.ev_name, evname,
    796 		    evcnt.ev_namelen + 1) != evcnt.ev_namelen + 1)
    797 			goto event_chain_trashed;
    798 
    799 		(void)printf("%s %s%*s %16lld %8lld\n", evgroup, evname,
    800 		    24 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
    801 		    (long long)evcnt.ev_count,
    802 		    (long long)(evcnt.ev_count / uptime));
    803 
    804 		inttotal += evcnt.ev_count++;
    805 	}
    806 	(void)printf("%-24s %16lld %8lld\n", "Total", inttotal,
    807 	    (long long)(inttotal / uptime));
    808 }
    809 #endif
    810 
    811 void
    812 doevcnt(int verbose)
    813 {
    814 	long long uptime;
    815 	struct evcntlist allevents;
    816 	struct evcnt evcnt, *evptr;
    817 	char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
    818 
    819 	/* XXX should print type! */
    820 
    821 	uptime = getuptime();
    822 	(void)printf("%-24s %16s %8s %s\n", "event", "total", "rate", "type");
    823 	kread(X_ALLEVENTS, &allevents, sizeof allevents);
    824 	evptr = allevents.tqh_first;
    825 	while (evptr) {
    826 		if (kvm_read(kd, (long)evptr, &evcnt, sizeof evcnt) != sizeof evcnt) {
    827 event_chain_trashed:
    828 			(void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
    829 			    kvm_geterr(kd));
    830 			exit(1);
    831 		}
    832 
    833 		evptr = evcnt.ev_list.tqe_next;
    834 		if (evcnt.ev_count == 0 && !verbose)
    835 			continue;
    836 
    837 		if (kvm_read(kd, (long)evcnt.ev_group, evgroup,
    838 		    evcnt.ev_grouplen + 1) != evcnt.ev_grouplen + 1)
    839 			goto event_chain_trashed;
    840 		if (kvm_read(kd, (long)evcnt.ev_name, evname,
    841 		    evcnt.ev_namelen + 1) != evcnt.ev_namelen + 1)
    842 			goto event_chain_trashed;
    843 
    844 		(void)printf("%s %s%*s %16lld %8lld %s\n", evgroup, evname,
    845 		    24 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
    846 		    (long long)evcnt.ev_count,
    847 		    (long long)(evcnt.ev_count / uptime),
    848 		    /* XXX do the following with an array lookup XXX */
    849 		    (evcnt.ev_type == EVCNT_TYPE_MISC) ? "misc" :
    850 		     ((evcnt.ev_type == EVCNT_TYPE_INTR) ? "intr" : "?"));
    851 	}
    852 }
    853 
    854 /*
    855  * These names are defined in <sys/malloc.h>.
    856  */
    857 char *kmemnames[] = INITKMEMNAMES;
    858 
    859 void
    860 domem()
    861 {
    862 	struct kmembuckets *kp;
    863 	struct kmemstats *ks;
    864 	int i, j;
    865 	int len, size, first;
    866 	long totuse = 0, totfree = 0, totreq = 0;
    867 	char *name;
    868 	struct kmemstats kmemstats[M_LAST];
    869 	struct kmembuckets buckets[MINBUCKET + 16];
    870 
    871 	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
    872 	for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
    873 	    i++, kp++) {
    874 		if (kp->kb_calls == 0)
    875 			continue;
    876 		if (first) {
    877 			(void)printf("Memory statistics by bucket size\n");
    878 			(void)printf(
    879 		 "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
    880 			first = 0;
    881 		}
    882 		size = 1 << i;
    883 		(void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
    884 			kp->kb_total - kp->kb_totalfree,
    885 			kp->kb_totalfree, kp->kb_calls,
    886 			kp->kb_highwat, kp->kb_couldfree);
    887 		totfree += size * kp->kb_totalfree;
    888 	}
    889 
    890 	/*
    891 	 * If kmem statistics are not being gathered by the kernel,
    892 	 * first will still be 1.
    893 	 */
    894 	if (first) {
    895 		printf(
    896 		    "Kmem statistics are not being gathered by the kernel.\n");
    897 		return;
    898 	}
    899 
    900 	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
    901 	(void)printf("\nMemory usage type by bucket size\n");
    902 	(void)printf("    Size  Type(s)\n");
    903 	kp = &buckets[MINBUCKET];
    904 	for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
    905 		if (kp->kb_calls == 0)
    906 			continue;
    907 		first = 1;
    908 		len = 8;
    909 		for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    910 			if (ks->ks_calls == 0)
    911 				continue;
    912 			if ((ks->ks_size & j) == 0)
    913 				continue;
    914 			if (kmemnames[i] == 0) {
    915 				kmemnames[i] = malloc(10);
    916 						/* strlen("undef/")+3+1);*/
    917 				snprintf(kmemnames[i], 10, "undef/%d", i);
    918 						/* same 10 as above!!! */
    919 			}
    920 			name = kmemnames[i];
    921 			len += 2 + strlen(name);
    922 			if (first)
    923 				printf("%8d  %s", j, name);
    924 			else
    925 				printf(",");
    926 			if (len >= 80) {
    927 				printf("\n\t ");
    928 				len = 10 + strlen(name);
    929 			}
    930 			if (!first)
    931 				printf(" %s", name);
    932 			first = 0;
    933 		}
    934 		printf("\n");
    935 	}
    936 
    937 	(void)printf(
    938 	    "\nMemory statistics by type                        Type  Kern\n");
    939 	(void)printf(
    940 "         Type  InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
    941 	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    942 		if (ks->ks_calls == 0)
    943 			continue;
    944 		(void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
    945 		    kmemnames[i] ? kmemnames[i] : "undefined",
    946 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
    947 		    (ks->ks_maxused + 1023) / 1024,
    948 		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
    949 		    ks->ks_limblocks, ks->ks_mapblocks);
    950 		first = 1;
    951 		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
    952 			if ((ks->ks_size & j) == 0)
    953 				continue;
    954 			if (first)
    955 				printf("  %d", j);
    956 			else
    957 				printf(",%d", j);
    958 			first = 0;
    959 		}
    960 		printf("\n");
    961 		totuse += ks->ks_memuse;
    962 		totreq += ks->ks_calls;
    963 	}
    964 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
    965 	(void)printf("              %7ldK %6ldK    %8ld\n",
    966 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
    967 }
    968 
    969 void
    970 dopool()
    971 {
    972 	int first, ovflw;
    973 	long addr;
    974 	long total = 0, inuse = 0;
    975 	TAILQ_HEAD(,pool) pool_head;
    976 	struct pool pool, *pp = &pool;
    977 
    978 	kread(X_POOLHEAD, &pool_head, sizeof(pool_head));
    979 	addr = (long)TAILQ_FIRST(&pool_head);
    980 
    981 	for (first = 1; addr != 0; ) {
    982 		char name[32], maxp[32];
    983 		if (kvm_read(kd, addr, (void *)pp, sizeof *pp) != sizeof *pp) {
    984 			(void)fprintf(stderr, "vmstat: pool chain trashed: %s\n",
    985 			    kvm_geterr(kd));
    986 			exit(1);
    987 		}
    988 		if (kvm_read(kd, (long)pp->pr_wchan, name, sizeof name) < 0) {
    989 			(void)fprintf(stderr, "vmstat: pool name trashed: %s\n",
    990 			    kvm_geterr(kd));
    991 			exit(1);
    992 		}
    993 		name[31]='\0';
    994 
    995 		if (first) {
    996 			(void)printf("Memory resource pool statistics\n");
    997 			(void)printf(
    998 			"%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n",
    999 		 		"Name",
   1000 				"Size",
   1001 				"Requests",
   1002 				"Fail",
   1003 				"Releases",
   1004 				"Pgreq",
   1005 				"Pgrel",
   1006 				"Npage",
   1007 				"Hiwat",
   1008 				"Minpg",
   1009 				"Maxpg",
   1010 				"Idle");
   1011 			first = 0;
   1012 		}
   1013 		if (pp->pr_maxpages == UINT_MAX)
   1014 			sprintf(maxp, "inf");
   1015 		else
   1016 			sprintf(maxp, "%u", pp->pr_maxpages);
   1017 /*
   1018  * Print single word.  `ovflow' is number of characters didn't fit
   1019  * on the last word.  `fmt' is a format string to print this word.
   1020  * It must contain asterisk for field width.  `width' is a width
   1021  * occupied by this word.  `fixed' is a number of constant chars in
   1022  * `fmt'.  `val' is a value to be printed using format string `fmt'.
   1023  */
   1024 #define	PRWORD(ovflw, fmt, width, fixed, val) do {	\
   1025 	(ovflw) += printf((fmt),			\
   1026 	    (width) - (fixed) - (ovflw) > 0 ?		\
   1027 	    (width) - (fixed) - (ovflw) : 0,		\
   1028 	    (val)) - (width);				\
   1029 	if ((ovflw) < 0)				\
   1030 		(ovflw) = 0;				\
   1031 } while (/* CONSTCOND */0)
   1032 		ovflw = 0;
   1033 		PRWORD(ovflw, "%-*s", 11, 0, name);
   1034 		PRWORD(ovflw, " %*u", 5, 1, pp->pr_size);
   1035 		PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget);
   1036 		PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
   1037 		PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput);
   1038 		PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc);
   1039 		PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree);
   1040 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages);
   1041 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat);
   1042 		PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages);
   1043 		PRWORD(ovflw, " %*s", 6, 1, maxp);
   1044 		PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle);
   1045 
   1046 		inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size;
   1047 		total += pp->pr_npages * pp->pr_pagesz;
   1048 		addr = (long)TAILQ_NEXT(pp, pr_poollist);
   1049 	}
   1050 
   1051 	printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
   1052 		inuse/1024, total/1024, (double)(100 * inuse) / total);
   1053 
   1054 }
   1055 
   1056 /*
   1057  * kread reads something from the kernel, given its nlist index.
   1058  */
   1059 void
   1060 kread(nlx, addr, size)
   1061 	int nlx;
   1062 	void *addr;
   1063 	size_t size;
   1064 {
   1065 	const char *sym;
   1066 
   1067 	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
   1068 		sym = namelist[nlx].n_name;
   1069 		if (*sym == '_')
   1070 			++sym;
   1071 		(void)fprintf(stderr,
   1072 		    "vmstat: symbol %s not defined\n", sym);
   1073 		exit(1);
   1074 	}
   1075 	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
   1076 		sym = namelist[nlx].n_name;
   1077 		if (*sym == '_')
   1078 			++sym;
   1079 		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
   1080 		exit(1);
   1081 	}
   1082 }
   1083 
   1084 struct nlist histnl[] = {
   1085 	{ "_uvm_histories" },
   1086 #define	X_UVM_HISTORIES		0
   1087 	{ NULL },
   1088 };
   1089 
   1090 /*
   1091  * Traverse the UVM history buffers, performing the requested action.
   1092  *
   1093  * Note, we assume that if we're not listing, we're dumping.
   1094  */
   1095 void
   1096 hist_traverse(todo, histname)
   1097 	int todo;
   1098 	const char *histname;
   1099 {
   1100 	struct uvm_history_head histhead;
   1101 	struct uvm_history hist, *histkva;
   1102 	char *name = NULL;
   1103 	size_t namelen = 0;
   1104 
   1105 	if (kvm_nlist(kd, histnl) != 0) {
   1106 		printf("UVM history is not compiled into the kernel.\n");
   1107 		return;
   1108 	}
   1109 
   1110 	if (kvm_read(kd, histnl[X_UVM_HISTORIES].n_value, &histhead,
   1111 	    sizeof(histhead)) != sizeof(histhead)) {
   1112 		warnx("unable to read %s: %s",
   1113 		    histnl[X_UVM_HISTORIES].n_name, kvm_geterr(kd));
   1114 		return;
   1115 	}
   1116 
   1117 	if (histhead.lh_first == NULL) {
   1118 		printf("No active UVM history logs.\n");
   1119 		return;
   1120 	}
   1121 
   1122 	if (todo & HISTLIST)
   1123 		printf("Active UVM histories:");
   1124 
   1125 	for (histkva = histhead.lh_first; histkva != NULL;
   1126 	    histkva = hist.list.le_next) {
   1127 		if (kvm_read(kd, (u_long)histkva, &hist, sizeof(hist)) !=
   1128 		    sizeof(hist)) {
   1129 			warnx("unable to read history at %p: %s",
   1130 			    histkva, kvm_geterr(kd));
   1131 			goto out;
   1132 		}
   1133 
   1134 		if (hist.namelen > namelen) {
   1135 			if (name != NULL)
   1136 				free(name);
   1137 			namelen = hist.namelen;
   1138 			if ((name = malloc(namelen + 1)) == NULL)
   1139 				err(1, "malloc history name");
   1140 		}
   1141 
   1142 		if (kvm_read(kd, (u_long)hist.name, name, namelen) !=
   1143 		    namelen) {
   1144 			warnx("unable to read history name at %p: %s",
   1145 			    hist.name, kvm_geterr(kd));
   1146 			goto out;
   1147 		}
   1148 		name[namelen] = '\0';
   1149 		if (todo & HISTLIST)
   1150 			printf(" %s", name);
   1151 		else {
   1152 			/*
   1153 			 * If we're dumping all histories, do it, else
   1154 			 * check to see if this is the one we want.
   1155 			 */
   1156 			if (histname == NULL || strcmp(histname, name) == 0) {
   1157 				if (histname == NULL)
   1158 					printf("\nUVM history `%s':\n", name);
   1159 				hist_dodump(&hist);
   1160 			}
   1161 		}
   1162 	}
   1163 
   1164 	if (todo & HISTLIST)
   1165 		printf("\n");
   1166 
   1167  out:
   1168 	if (name != NULL)
   1169 		free(name);
   1170 }
   1171 
   1172 /*
   1173  * Actually dump the history buffer at the specified KVA.
   1174  */
   1175 void
   1176 hist_dodump(histp)
   1177 	struct uvm_history *histp;
   1178 {
   1179 	struct uvm_history_ent *histents, *e;
   1180 	size_t histsize;
   1181 	char *fmt = NULL, *fn = NULL;
   1182 	size_t fmtlen = 0, fnlen = 0;
   1183 	int i;
   1184 
   1185 	histsize = sizeof(struct uvm_history_ent) * histp->n;
   1186 
   1187 	if ((histents = malloc(histsize)) == NULL)
   1188 		err(1, "malloc history entries");
   1189 
   1190 	memset(histents, 0, histsize);
   1191 
   1192 	if (kvm_read(kd, (u_long)histp->e, histents, histsize) != histsize) {
   1193 		warnx("unable to read history entries at %p: %s",
   1194 		    histp->e, kvm_geterr(kd));
   1195 		goto out;
   1196 	}
   1197 
   1198 	i = histp->f;
   1199 	do {
   1200 		e = &histents[i];
   1201 		if (e->fmt != NULL) {
   1202 			if (e->fmtlen > fmtlen) {
   1203 				if (fmt != NULL)
   1204 					free(fmt);
   1205 				fmtlen = e->fmtlen;
   1206 				if ((fmt = malloc(fmtlen + 1)) == NULL)
   1207 					err(1, "malloc printf format");
   1208 			}
   1209 			if (e->fnlen > fnlen) {
   1210 				if (fn != NULL)
   1211 					free(fn);
   1212 				fnlen = e->fnlen;
   1213 				if ((fn = malloc(fnlen + 1)) == NULL)
   1214 					err(1, "malloc function name");
   1215 			}
   1216 
   1217 			if (kvm_read(kd, (u_long)e->fmt, fmt, fmtlen)
   1218 			    != fmtlen) {
   1219 				warnx("unable to read printf format "
   1220 				    "at %p: %s", e->fmt, kvm_geterr(kd));
   1221 				goto out;
   1222 			}
   1223 			fmt[fmtlen] = '\0';
   1224 
   1225 			if (kvm_read(kd, (u_long)e->fn, fn, fnlen) != fnlen) {
   1226 				warnx("unable to read function name "
   1227 				    "at %p: %s", e->fn, kvm_geterr(kd));
   1228 				goto out;
   1229 			}
   1230 			fn[fnlen] = '\0';
   1231 
   1232 			printf("%06ld.%06ld ", (long int)e->tv.tv_sec,
   1233 			    (long int)e->tv.tv_usec);
   1234 			printf("%s#%ld: ", fn, e->call);
   1235 			printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
   1236 			printf("\n");
   1237 		}
   1238 		i = (i + 1) % histp->n;
   1239 	} while (i != histp->f);
   1240 
   1241  out:
   1242 	free(histents);
   1243 	if (fmt != NULL)
   1244 		free(fmt);
   1245 	if (fn != NULL)
   1246 		free(fn);
   1247 }
   1248 
   1249 void
   1250 usage()
   1251 {
   1252 
   1253 	(void)fprintf(stderr,
   1254 	    "usage: vmstat [-efHilmsv] [-h histname] [-c count] [-M core] \
   1255 [-N system] [-w wait] [disks]\n");
   1256 	exit(1);
   1257 }
   1258