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