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