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