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