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