Home | History | Annotate | Line # | Download | only in vmstat
vmstat.c revision 1.44
      1 /*	$NetBSD: vmstat.c,v 1.44 1998/02/09 13:11:26 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 3/1/95";
     45 #else
     46 __RCSID("$NetBSD: vmstat.c,v 1.44 1998/02/09 13:11:26 mrg Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 #include <sys/proc.h>
     53 #include <sys/user.h>
     54 #include <sys/dkstat.h>
     55 #include <sys/buf.h>
     56 #include <sys/namei.h>
     57 #include <sys/malloc.h>
     58 #include <sys/fcntl.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/sysctl.h>
     61 #include <sys/device.h>
     62 #include <vm/vm.h>
     63 #include <time.h>
     64 #include <nlist.h>
     65 #include <kvm.h>
     66 #include <errno.h>
     67 #include <unistd.h>
     68 #include <signal.h>
     69 #include <stdio.h>
     70 #include <ctype.h>
     71 #include <stdlib.h>
     72 #include <string.h>
     73 #include <paths.h>
     74 #include <limits.h>
     75 #include "dkstats.h"
     76 
     77 struct nlist namelist[] = {
     78 #define	X_CPTIME	0
     79 	{ "_cp_time" },
     80 #define	X_BOOTTIME	1
     81 	{ "_boottime" },
     82 #define X_HZ		2
     83 	{ "_hz" },
     84 #define X_STATHZ	3
     85 	{ "_stathz" },
     86 #define X_NCHSTATS	4
     87 	{ "_nchstats" },
     88 #define	X_INTRNAMES	5
     89 	{ "_intrnames" },
     90 #define	X_EINTRNAMES	6
     91 	{ "_eintrnames" },
     92 #define	X_INTRCNT	7
     93 	{ "_intrcnt" },
     94 #define	X_EINTRCNT	8
     95 	{ "_eintrcnt" },
     96 #define	X_KMEMSTAT	9
     97 	{ "_kmemstats" },
     98 #define	X_KMEMBUCKETS	10
     99 	{ "_bucket" },
    100 #define X_ALLEVENTS	11
    101 	{ "_allevents" },
    102 #if defined(UVM)
    103 #define X_END		12
    104 #else
    105 #define X_SUM		12
    106 	{ "_cnt" },
    107 #define X_END		13
    108 #endif
    109 #if defined(pc532)
    110 #define	X_IVT		(X_END)
    111 	{ "_ivt" },
    112 #endif
    113 	{ "" },
    114 };
    115 
    116 /* Objects defined in dkstats.c */
    117 extern struct _disk	cur;
    118 extern char	**dr_name;
    119 extern int	*dk_select, dk_ndrive;
    120 
    121 #if defined(UVM)
    122 struct	uvmexp uvmexp, ouvmexp;
    123 #else
    124 struct	vmmeter sum, osum;
    125 #endif
    126 int		ndrives;
    127 
    128 int	winlines = 20;
    129 
    130 kvm_t *kd;
    131 
    132 #define	FORKSTAT	0x01
    133 #define	INTRSTAT	0x02
    134 #define	MEMSTAT		0x04
    135 #define	SUMSTAT		0x08
    136 #define	VMSTAT		0x20
    137 
    138 void	cpustats __P((void));
    139 void	dkstats __P((void));
    140 void	dointr __P((void));
    141 void	domem __P((void));
    142 void	dosum __P((void));
    143 void	dovmstat __P((u_int, int));
    144 void	kread __P((int, void *, size_t));
    145 void	needhdr __P((int));
    146 long	getuptime __P((void));
    147 void	printhdr __P((void));
    148 long	pct __P((long, long));
    149 void	usage __P((void));
    150 void	doforkst __P((void));
    151 
    152 int	main __P((int, char **));
    153 char	**choosedrives __P((char **));
    154 
    155 extern int dkinit __P((int));
    156 extern void dkreadstats __P((void));
    157 extern void dkswap __P((void));
    158 
    159 /* Namelist and memory file names. */
    160 char	*nlistf, *memf;
    161 
    162 int
    163 main(argc, argv)
    164 	int argc;
    165 	char **argv;
    166 {
    167 	extern int optind;
    168 	extern char *optarg;
    169 	int c, todo;
    170 	u_int interval;
    171 	int reps;
    172         char errbuf[_POSIX2_LINE_MAX];
    173 
    174 	memf = nlistf = NULL;
    175 	interval = reps = todo = 0;
    176 	while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != -1) {
    177 		switch (c) {
    178 		case 'c':
    179 			reps = atoi(optarg);
    180 			break;
    181 		case 'f':
    182 			todo |= FORKSTAT;
    183 			break;
    184 		case 'i':
    185 			todo |= INTRSTAT;
    186 			break;
    187 		case 'M':
    188 			memf = optarg;
    189 			break;
    190 		case 'm':
    191 			todo |= MEMSTAT;
    192 			break;
    193 		case 'N':
    194 			nlistf = optarg;
    195 			break;
    196 		case 's':
    197 			todo |= SUMSTAT;
    198 			break;
    199 		case 'w':
    200 			interval = atoi(optarg);
    201 			break;
    202 		case '?':
    203 		default:
    204 			usage();
    205 		}
    206 	}
    207 	argc -= optind;
    208 	argv += optind;
    209 
    210 	if (todo == 0)
    211 		todo = VMSTAT;
    212 
    213 	/*
    214 	 * Discard setgid privileges if not the running kernel so that bad
    215 	 * guys can't print interesting stuff from kernel memory.
    216 	 */
    217 	if (nlistf != NULL || memf != NULL)
    218 		setgid(getgid());
    219 
    220         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
    221 	if (kd == 0) {
    222 		(void)fprintf(stderr,
    223 		    "vmstat: kvm_openfiles: %s\n", errbuf);
    224 		exit(1);
    225 	}
    226 
    227 	if ((c = kvm_nlist(kd, namelist)) != 0) {
    228 		if (c > 0) {
    229 			(void)fprintf(stderr,
    230 			    "vmstat: undefined symbols:");
    231 			for (c = 0;
    232 			    c < sizeof(namelist)/sizeof(namelist[0]); c++)
    233 				if (namelist[c].n_type == 0)
    234 					fprintf(stderr, " %s",
    235 					    namelist[c].n_name);
    236 			(void)fputc('\n', stderr);
    237 		} else
    238 			(void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
    239 			    kvm_geterr(kd));
    240 		exit(1);
    241 	}
    242 
    243 	if (todo & VMSTAT) {
    244 		struct winsize winsize;
    245 
    246 		dkinit(0);	/* Initialize disk stats, no disks selected. */
    247 		argv = choosedrives(argv);	/* Select disks. */
    248 		winsize.ws_row = 0;
    249 		(void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
    250 		if (winsize.ws_row > 0)
    251 			winlines = winsize.ws_row;
    252 
    253 	}
    254 
    255 #define	BACKWARD_COMPATIBILITY
    256 #ifdef	BACKWARD_COMPATIBILITY
    257 	if (*argv) {
    258 		interval = atoi(*argv);
    259 		if (*++argv)
    260 			reps = atoi(*argv);
    261 	}
    262 #endif
    263 
    264 	if (interval) {
    265 		if (!reps)
    266 			reps = -1;
    267 	} else if (reps)
    268 		interval = 1;
    269 
    270 	if (todo & FORKSTAT)
    271 		doforkst();
    272 	if (todo & MEMSTAT)
    273 		domem();
    274 	if (todo & SUMSTAT)
    275 		dosum();
    276 	if (todo & INTRSTAT)
    277 		dointr();
    278 	if (todo & VMSTAT)
    279 		dovmstat(interval, reps);
    280 	exit(0);
    281 }
    282 
    283 char **
    284 choosedrives(argv)
    285 	char **argv;
    286 {
    287 	int i;
    288 
    289 	/*
    290 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    291 	 * supplied as arguments, default drives.  If everything isn't filled
    292 	 * in and there are drives not taken care of, display the first few
    293 	 * that fit.
    294 	 */
    295 #define BACKWARD_COMPATIBILITY
    296 	for (ndrives = 0; *argv; ++argv) {
    297 #ifdef	BACKWARD_COMPATIBILITY
    298 		if (isdigit(**argv))
    299 			break;
    300 #endif
    301 		for (i = 0; i < dk_ndrive; i++) {
    302 			if (strcmp(dr_name[i], *argv))
    303 				continue;
    304 			dk_select[i] = 1;
    305 			++ndrives;
    306 			break;
    307 		}
    308 	}
    309 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
    310 		if (dk_select[i])
    311 			continue;
    312 		dk_select[i] = 1;
    313 		++ndrives;
    314 	}
    315 	return(argv);
    316 }
    317 
    318 long
    319 getuptime()
    320 {
    321 	static time_t now;
    322 	static struct timeval boottime;
    323 	time_t uptime;
    324 
    325 	if (boottime.tv_sec == 0)
    326 		kread(X_BOOTTIME, &boottime, sizeof(boottime));
    327 	(void)time(&now);
    328 	uptime = now - boottime.tv_sec;
    329 	if (uptime <= 0 || uptime > 60*60*24*365*10) {
    330 		(void)fprintf(stderr,
    331 		    "vmstat: time makes no sense; namelist must be wrong.\n");
    332 		exit(1);
    333 	}
    334 	return(uptime);
    335 }
    336 
    337 int	hz, hdrcnt;
    338 
    339 void
    340 dovmstat(interval, reps)
    341 	u_int interval;
    342 	int reps;
    343 {
    344 	struct vmtotal total;
    345 	time_t uptime, halfuptime;
    346 	int mib[2];
    347 	size_t size;
    348 	int pagesize = getpagesize();
    349 
    350 	uptime = getuptime();
    351 	halfuptime = uptime / 2;
    352 	(void)signal(SIGCONT, needhdr);
    353 
    354 	if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
    355 		kread(X_STATHZ, &hz, sizeof(hz));
    356 	if (!hz)
    357 		kread(X_HZ, &hz, sizeof(hz));
    358 
    359 	for (hdrcnt = 1;;) {
    360 		if (!--hdrcnt)
    361 			printhdr();
    362 		/* Read new disk statistics */
    363 		dkreadstats();
    364 #if defined(UVM)
    365 		size = sizeof(uvmexp);
    366 		mib[0] = CTL_VM;
    367 		mib[1] = VM_UVMEXP;
    368 		if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
    369 			printf("can't get uvmexp: %s\n", strerror(errno));
    370 			bzero(&uvmexp, sizeof(uvmexp));
    371 		}
    372 #else
    373 		kread(X_SUM, &sum, sizeof(sum));
    374 #endif
    375 		size = sizeof(total);
    376 		mib[0] = CTL_VM;
    377 		mib[1] = VM_METER;
    378 		if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
    379 			printf("Can't get kerninfo: %s\n", strerror(errno));
    380 			bzero(&total, sizeof(total));
    381 		}
    382 		(void)printf("%2d%2d%2d",
    383 		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
    384 #define pgtok(a) (long)((a) * (pagesize >> 10))
    385 #define	rate(x)	(u_long)(((x) + halfuptime) / uptime)	/* round */
    386 		(void)printf("%6ld%6ld ",
    387 		    pgtok(total.t_avm), pgtok(total.t_free));
    388 #if defined(UVM)
    389 		(void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults));
    390 		(void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact));
    391 		(void)printf("%3lu ", rate(uvmexp.pgswapin - ouvmexp.pgswapin));
    392 		(void)printf("%4lu ",
    393 		    rate(uvmexp.pgswapout - ouvmexp.pgswapout));
    394 		(void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed));
    395 		(void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans));
    396 		dkstats();
    397 		(void)printf("%4lu %4lu %3lu ",
    398 		    rate(uvmexp.intrs - ouvmexp.intrs),
    399 		    rate(uvmexp.syscalls - ouvmexp.syscalls),
    400 		    rate(uvmexp.swtch - ouvmexp.swtch));
    401 		cpustats();
    402 		(void)printf("\n");
    403 		(void)fflush(stdout);
    404 		if (reps >= 0 && --reps <= 0)
    405 			break;
    406 		ouvmexp = uvmexp;
    407 #else
    408 		(void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
    409 		(void)printf("%3lu ",
    410 		    rate(sum.v_reactivated - osum.v_reactivated));
    411 		(void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
    412 		(void)printf("%3lu %3lu ",
    413 		    rate(sum.v_pageouts - osum.v_pageouts), (u_long)0);
    414 		(void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
    415 		dkstats();
    416 		(void)printf("%4lu %4lu %3lu ",
    417 		    rate(sum.v_intr - osum.v_intr),
    418 		    rate(sum.v_syscall - osum.v_syscall),
    419 		    rate(sum.v_swtch - osum.v_swtch));
    420 		cpustats();
    421 		(void)printf("\n");
    422 		(void)fflush(stdout);
    423 		if (reps >= 0 && --reps <= 0)
    424 			break;
    425 		osum = sum;
    426 #endif
    427 		uptime = interval;
    428 		/*
    429 		 * We round upward to avoid losing low-frequency events
    430 		 * (i.e., >= 1 per interval but < 1 per second).
    431 		 */
    432 		halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
    433 		(void)sleep(interval);
    434 	}
    435 }
    436 
    437 void
    438 printhdr()
    439 {
    440 	int i;
    441 
    442 #if defined(UVM)
    443 	(void)printf(" procs   memory     page%*s", 23, "");
    444 #else
    445 	(void)printf(" procs   memory     page%*s", 20, "");
    446 #endif
    447 	if (ndrives > 0)
    448 		(void)printf("%s %*sfaults   cpu\n",
    449 		   ((ndrives > 1) ? "disks" : "disk"),
    450 		   ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
    451 	else
    452 		(void)printf("%*s  faults   cpu\n",
    453 		   ndrives * 3, "");
    454 
    455 #if defined(UVM)
    456 	(void)printf(" r b w   avm   fre  flt  re  pi   po   fr   sr ");
    457 #else
    458 	(void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
    459 #endif
    460 	for (i = 0; i < dk_ndrive; i++)
    461 		if (dk_select[i])
    462 			(void)printf("%c%c ", dr_name[i][0],
    463 			    dr_name[i][strlen(dr_name[i]) - 1]);
    464 	(void)printf("  in   sy  cs us sy id\n");
    465 	hdrcnt = winlines - 2;
    466 }
    467 
    468 /*
    469  * Force a header to be prepended to the next output.
    470  */
    471 void
    472 needhdr(dummy)
    473 	int dummy;
    474 {
    475 
    476 	hdrcnt = 1;
    477 }
    478 
    479 long
    480 pct(top, bot)
    481 	long top, bot;
    482 {
    483 	long ans;
    484 
    485 	if (bot == 0)
    486 		return(0);
    487 	ans = (quad_t)top * 100 / bot;
    488 	return (ans);
    489 }
    490 
    491 #define	PCT(top, bot) (int)pct((long)(top), (long)(bot))
    492 
    493 void
    494 dosum()
    495 {
    496 	struct nchstats nchstats;
    497 	long nchtotal;
    498 
    499 #if defined(UVM)
    500 	int	mib[2];
    501 	size_t	size;
    502 
    503 	size = sizeof(uvmexp);
    504 	mib[0] = CTL_VM;
    505 	mib[1] = VM_UVMEXP;
    506 	if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
    507 		printf("can't get uvmexp: %s\n", strerror(errno));
    508 		bzero(&uvmexp, sizeof(uvmexp));
    509 	}
    510 
    511 	(void)printf("%9u bytes per page\n", uvmexp.pagesize);
    512 
    513 	(void)printf("%9u pages managed\n", uvmexp.npages);
    514 	(void)printf("%9u pages free\n", uvmexp.free);
    515 	(void)printf("%9u pages active\n", uvmexp.active);
    516 	(void)printf("%9u pages inactive\n", uvmexp.inactive);
    517 	(void)printf("%9u pages paging\n", uvmexp.paging);
    518 	(void)printf("%9u pages wired\n", uvmexp.wired);
    519 	(void)printf("%9u reserve pagedaemon pages\n",
    520 	    uvmexp.reserve_pagedaemon);
    521 	(void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
    522 
    523 	(void)printf("%9u minimum free pages\n", uvmexp.freemin);
    524 	(void)printf("%9u target free pages\n", uvmexp.freetarg);
    525 	(void)printf("%9u target inactive pages\n", uvmexp.inactarg);
    526 	(void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
    527 
    528 	(void)printf("%9u swap devices\n", uvmexp.nswapdev);
    529 	(void)printf("%9u swap pages\n", uvmexp.swpages);
    530 	(void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
    531 	(void)printf("%9u swap allocations\n", uvmexp.nswget);
    532 	(void)printf("%9u anons\n", uvmexp.nanon);
    533 	(void)printf("%9u free anons\n", uvmexp.nfreeanon);
    534 
    535 	(void)printf("%9u total faults taken\n", uvmexp.faults);
    536 	(void)printf("%9u traps\n", uvmexp.traps);
    537 	(void)printf("%9u device interrupts\n", uvmexp.intrs);
    538 	(void)printf("%9u cpu context switches\n", uvmexp.swtch);
    539 	(void)printf("%9u software interrupts\n", uvmexp.softs);
    540 	(void)printf("%9u system calls\n", uvmexp.syscalls);
    541 	(void)printf("%9u pagein requests\n", uvmexp.pageins / CLSIZE);
    542 	(void)printf("%9u pageout requests\n", uvmexp.pdpageouts / CLSIZE);
    543 	(void)printf("%9u swap ins\n", uvmexp.swapins);
    544 	(void)printf("%9u swap outs\n", uvmexp.swapouts);
    545 	(void)printf("%9u pages swapped in\n", uvmexp.pgswapin / CLSIZE);
    546 	(void)printf("%9u pages swapped out\n", uvmexp.pgswapout / CLSIZE);
    547 	(void)printf("%9u forks total\n", uvmexp.forks);
    548 	(void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
    549 	(void)printf("%9u forks shared address space with parent\n",
    550 	    uvmexp.forks_sharevm);
    551 
    552 	(void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
    553 	(void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
    554 	(void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
    555 	(void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
    556 	(void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
    557 	    uvmexp.fltrelckok);
    558 	(void)printf("%9u anon page faults\n", uvmexp.fltanget);
    559 	(void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
    560 	(void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
    561 	(void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
    562 	(void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
    563 	(void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
    564 	(void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
    565 	(void)printf("%9u anon faults\n", uvmexp.flt_anon);
    566 	(void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
    567 	(void)printf("%9u object faults\n", uvmexp.flt_obj);
    568 	(void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
    569 	(void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
    570 
    571 	(void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
    572 	(void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
    573 	(void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout);
    574 	(void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
    575 	(void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
    576 	(void)printf("%9u anonymous pages scanned by daemon\n", uvmexp.pdanscan);
    577 	(void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
    578 	(void)printf("%9u pages reactivated\n", uvmexp.pdreact);
    579 	(void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
    580 	(void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
    581 	(void)printf("%9u pages deactivated\n", uvmexp.pddeact);
    582 #else
    583 	kread(X_SUM, &sum, sizeof(sum));
    584 	(void)printf("%9u cpu context switches\n", sum.v_swtch);
    585 	(void)printf("%9u device interrupts\n", sum.v_intr);
    586 	(void)printf("%9u software interrupts\n", sum.v_soft);
    587 	(void)printf("%9u traps\n", sum.v_trap);
    588 	(void)printf("%9u system calls\n", sum.v_syscall);
    589 	(void)printf("%9u total faults taken\n", sum.v_faults);
    590 	(void)printf("%9u swap ins\n", sum.v_swpin);
    591 	(void)printf("%9u swap outs\n", sum.v_swpout);
    592 	(void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
    593 	(void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
    594 	(void)printf("%9u page ins\n", sum.v_pageins);
    595 	(void)printf("%9u page outs\n", sum.v_pageouts);
    596 	(void)printf("%9u pages paged in\n", sum.v_pgpgin);
    597 	(void)printf("%9u pages paged out\n", sum.v_pgpgout);
    598 	(void)printf("%9u pages reactivated\n", sum.v_reactivated);
    599 	(void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
    600 	(void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
    601 	(void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
    602 	(void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
    603 	(void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
    604 	(void)printf("%9u VM object cache lookups\n", sum.v_lookups);
    605 	(void)printf("%9u VM object hits\n", sum.v_hits);
    606 	(void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
    607 	(void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
    608 	(void)printf("%9u pages freed by daemon\n", sum.v_dfree);
    609 	(void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
    610 	(void)printf("%9u pages free\n", sum.v_free_count);
    611 	(void)printf("%9u pages wired down\n", sum.v_wire_count);
    612 	(void)printf("%9u pages active\n", sum.v_active_count);
    613 	(void)printf("%9u pages inactive\n", sum.v_inactive_count);
    614 	(void)printf("%9u bytes per page\n", sum.v_page_size);
    615 	(void)printf("%9u target inactive pages\n", sum.v_inactive_target);
    616 	(void)printf("%9u target free pages\n", sum.v_free_target);
    617 	(void)printf("%9u minimum free pages\n", sum.v_free_min);
    618 #endif
    619 	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
    620 	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
    621 	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
    622 	    nchstats.ncs_miss + nchstats.ncs_long;
    623 	(void)printf("%9ld total name lookups\n", nchtotal);
    624 	(void)printf(
    625 	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
    626 	    "", PCT(nchstats.ncs_goodhits, nchtotal),
    627 	    PCT(nchstats.ncs_neghits, nchtotal),
    628 	    PCT(nchstats.ncs_pass2, nchtotal));
    629 	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
    630 	    PCT(nchstats.ncs_badhits, nchtotal),
    631 	    PCT(nchstats.ncs_falsehits, nchtotal),
    632 	    PCT(nchstats.ncs_long, nchtotal));
    633 }
    634 
    635 void
    636 doforkst()
    637 {
    638 #if defined(UVM)
    639 	int	mib[2];
    640 	size_t	size;
    641 
    642 	size = sizeof(uvmexp);
    643 	mib[0] = CTL_VM;
    644 	mib[1] = VM_UVMEXP;
    645 	if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
    646 		printf("can't get uvmexp: %s\n", strerror(errno));
    647 		bzero(&uvmexp, sizeof(uvmexp));
    648 	}
    649 	(void)printf("%u forks total\n", uvmexp.forks);
    650 	(void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
    651 	(void)printf("%u forks shared address space with parent\n",
    652 	    uvmexp.forks_sharevm);
    653 #else
    654 
    655 	kread(X_SUM, &sum, sizeof(sum));
    656 	(void)printf("%u forks total\n", sum.v_forks);
    657 	(void)printf("%u forks blocked parent\n", sum.v_forks_ppwait);
    658 	(void)printf("%u forks shared address space with parent\n",
    659 	    sum.v_forks_sharevm);
    660 #endif
    661 }
    662 
    663 void
    664 dkstats()
    665 {
    666 	int dn, state;
    667 	double etime;
    668 
    669 	/* Calculate disk stat deltas. */
    670 	dkswap();
    671 	etime = 0;
    672 	for (state = 0; state < CPUSTATES; ++state) {
    673 		etime += cur.cp_time[state];
    674 	}
    675 	if (etime == 0)
    676 		etime = 1;
    677 	etime /= hz;
    678 	for (dn = 0; dn < dk_ndrive; ++dn) {
    679 		if (!dk_select[dn])
    680 			continue;
    681 		(void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
    682 	}
    683 }
    684 
    685 void
    686 cpustats()
    687 {
    688 	int state;
    689 	double pct, total;
    690 
    691 	total = 0;
    692 	for (state = 0; state < CPUSTATES; ++state)
    693 		total += cur.cp_time[state];
    694 	if (total)
    695 		pct = 100 / total;
    696 	else
    697 		pct = 0;
    698 	(void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
    699 	(void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
    700 	(void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
    701 }
    702 
    703 #if defined(pc532)
    704 /* To get struct iv ...*/
    705 #define _KERNEL
    706 #include <machine/psl.h>
    707 #undef _KERNEL
    708 void
    709 dointr()
    710 {
    711 	long i, j, inttotal, uptime;
    712 	static char iname[64];
    713 	struct iv ivt[32], *ivp = ivt;
    714 
    715 	iname[63] = '\0';
    716 	uptime = getuptime();
    717 	kread(X_IVT, ivp, sizeof(ivt));
    718 
    719 	for (i = 0; i < 2; i++) {
    720 		(void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
    721 		(void)printf("interrupt       total     rate\n");
    722 		inttotal = 0;
    723 		for (j = 0; j < 16; j++, ivp++) {
    724 			if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) {
    725 				if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
    726 					(void)fprintf(stderr, "vmstat: iv_use: %s\n",
    727 					    kvm_geterr(kd));
    728 					exit(1);
    729 				}
    730 				(void)printf("%-12s %8ld %8ld\n", iname,
    731 				    ivp->iv_cnt, ivp->iv_cnt / uptime);
    732 				inttotal += ivp->iv_cnt;
    733 			}
    734 		}
    735 		(void)printf("Total        %8ld %8ld\n",
    736 		    inttotal, inttotal / uptime);
    737 	}
    738 }
    739 #else
    740 void
    741 dointr()
    742 {
    743 	long *intrcnt, inttotal, uptime;
    744 	int nintr, inamlen;
    745 	char *intrname;
    746 	struct evcntlist allevents;
    747 	struct evcnt evcnt, *evptr;
    748 	struct device dev;
    749 
    750 	uptime = getuptime();
    751 	nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
    752 	inamlen =
    753 	    namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
    754 	intrcnt = malloc((size_t)nintr);
    755 	intrname = malloc((size_t)inamlen);
    756 	if (intrcnt == NULL || intrname == NULL) {
    757 		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
    758 		exit(1);
    759 	}
    760 	kread(X_INTRCNT, intrcnt, (size_t)nintr);
    761 	kread(X_INTRNAMES, intrname, (size_t)inamlen);
    762 	(void)printf("interrupt         total     rate\n");
    763 	inttotal = 0;
    764 	nintr /= sizeof(long);
    765 	while (--nintr >= 0) {
    766 		if (*intrcnt)
    767 			(void)printf("%-14s %8ld %8ld\n", intrname,
    768 			    *intrcnt, *intrcnt / uptime);
    769 		intrname += strlen(intrname) + 1;
    770 		inttotal += *intrcnt++;
    771 	}
    772 	kread(X_ALLEVENTS, &allevents, sizeof allevents);
    773 	evptr = allevents.tqh_first;
    774 	while (evptr) {
    775 		if (kvm_read(kd, (long)evptr, (void *)&evcnt,
    776 		    sizeof evcnt) != sizeof evcnt) {
    777 			(void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
    778 			    kvm_geterr(kd));
    779 			exit(1);
    780 		}
    781 		if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
    782 		    sizeof dev) != sizeof dev) {
    783 			(void)fprintf(stderr, "vmstat: event chain trashed: %s\n",
    784 			    kvm_geterr(kd));
    785 			exit(1);
    786 		}
    787 		if (evcnt.ev_count)
    788 			(void)printf("%-14s %8ld %8ld\n", dev.dv_xname,
    789 			    (long)evcnt.ev_count, evcnt.ev_count / uptime);
    790 		inttotal += evcnt.ev_count++;
    791 
    792 		evptr = evcnt.ev_list.tqe_next;
    793 	}
    794 	(void)printf("Total          %8ld %8ld\n", inttotal, inttotal / uptime);
    795 }
    796 #endif
    797 
    798 /*
    799  * These names are defined in <sys/malloc.h>.
    800  */
    801 char *kmemnames[] = INITKMEMNAMES;
    802 
    803 void
    804 domem()
    805 {
    806 	struct kmembuckets *kp;
    807 	struct kmemstats *ks;
    808 	int i, j;
    809 	int len, size, first;
    810 	long totuse = 0, totfree = 0, totreq = 0;
    811 	char *name;
    812 	struct kmemstats kmemstats[M_LAST];
    813 	struct kmembuckets buckets[MINBUCKET + 16];
    814 
    815 	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
    816 	for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
    817 	    i++, kp++) {
    818 		if (kp->kb_calls == 0)
    819 			continue;
    820 		if (first) {
    821 			(void)printf("Memory statistics by bucket size\n");
    822 			(void)printf(
    823 		 "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
    824 			first = 0;
    825 		}
    826 		size = 1 << i;
    827 		(void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
    828 			kp->kb_total - kp->kb_totalfree,
    829 			kp->kb_totalfree, kp->kb_calls,
    830 			kp->kb_highwat, kp->kb_couldfree);
    831 		totfree += size * kp->kb_totalfree;
    832 	}
    833 
    834 	/*
    835 	 * If kmem statistics are not being gathered by the kernel,
    836 	 * first will still be 1.
    837 	 */
    838 	if (first) {
    839 		printf(
    840 		    "Kmem statistics are not being gathered by the kernel.\n");
    841 		return;
    842 	}
    843 
    844 	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
    845 	(void)printf("\nMemory usage type by bucket size\n");
    846 	(void)printf("    Size  Type(s)\n");
    847 	kp = &buckets[MINBUCKET];
    848 	for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
    849 		if (kp->kb_calls == 0)
    850 			continue;
    851 		first = 1;
    852 		len = 8;
    853 		for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    854 			if (ks->ks_calls == 0)
    855 				continue;
    856 			if ((ks->ks_size & j) == 0)
    857 				continue;
    858 			if (kmemnames[i] == 0) {
    859 				kmemnames[i] = malloc(10);
    860 						/* strlen("undef/")+3+1);*/
    861 				snprintf(kmemnames[i], 10, "undef/%d", i);
    862 						/* same 10 as above!!! */
    863 			}
    864 			name = kmemnames[i];
    865 			len += 2 + strlen(name);
    866 			if (first)
    867 				printf("%8d  %s", j, name);
    868 			else
    869 				printf(",");
    870 			if (len >= 80) {
    871 				printf("\n\t ");
    872 				len = 10 + strlen(name);
    873 			}
    874 			if (!first)
    875 				printf(" %s", name);
    876 			first = 0;
    877 		}
    878 		printf("\n");
    879 	}
    880 
    881 	(void)printf(
    882 	    "\nMemory statistics by type                        Type  Kern\n");
    883 	(void)printf(
    884 "         Type  InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
    885 	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    886 		if (ks->ks_calls == 0)
    887 			continue;
    888 		(void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
    889 		    kmemnames[i] ? kmemnames[i] : "undefined",
    890 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
    891 		    (ks->ks_maxused + 1023) / 1024,
    892 		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
    893 		    ks->ks_limblocks, ks->ks_mapblocks);
    894 		first = 1;
    895 		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
    896 			if ((ks->ks_size & j) == 0)
    897 				continue;
    898 			if (first)
    899 				printf("  %d", j);
    900 			else
    901 				printf(",%d", j);
    902 			first = 0;
    903 		}
    904 		printf("\n");
    905 		totuse += ks->ks_memuse;
    906 		totreq += ks->ks_calls;
    907 	}
    908 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
    909 	(void)printf("              %7ldK %6ldK    %8ld\n",
    910 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
    911 }
    912 
    913 /*
    914  * kread reads something from the kernel, given its nlist index.
    915  */
    916 void
    917 kread(nlx, addr, size)
    918 	int nlx;
    919 	void *addr;
    920 	size_t size;
    921 {
    922 	char *sym;
    923 
    924 	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
    925 		sym = namelist[nlx].n_name;
    926 		if (*sym == '_')
    927 			++sym;
    928 		(void)fprintf(stderr,
    929 		    "vmstat: symbol %s not defined\n", sym);
    930 		exit(1);
    931 	}
    932 	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
    933 		sym = namelist[nlx].n_name;
    934 		if (*sym == '_')
    935 			++sym;
    936 		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
    937 		exit(1);
    938 	}
    939 }
    940 
    941 void
    942 usage()
    943 {
    944 	(void)fprintf(stderr,
    945 	    "usage: vmstat [-ims] [-c count] [-M core] \
    946 [-N system] [-w wait] [disks]\n");
    947 	exit(1);
    948 }
    949 
    950