Home | History | Annotate | Line # | Download | only in vmstat
vmstat.c revision 1.30
      1 /*	$NetBSD: vmstat.c,v 1.30 1996/06/05 00:18:58 cgd 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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) 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.1 (Berkeley) 6/6/93";
     45 #else
     46 static char rcsid[] = "$NetBSD: vmstat.c,v 1.30 1996/06/05 00:18:58 cgd 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 #define NEWVM			/* XXX till old has been updated or purged */
     78 struct nlist namelist[] = {
     79 #define	X_CPTIME	0
     80 	{ "_cp_time" },
     81 #define	X_DK_NDRIVE	1
     82 	{ "_dk_ndrive" },
     83 #define X_SUM		2
     84 	{ "_cnt" },
     85 #define	X_BOOTTIME	3
     86 	{ "_boottime" },
     87 #define	X_DKXFER	4
     88 	{ "_dk_xfer" },
     89 #define X_HZ		5
     90 	{ "_hz" },
     91 #define X_STATHZ	6
     92 	{ "_stathz" },
     93 #define X_NCHSTATS	7
     94 	{ "_nchstats" },
     95 #define	X_INTRNAMES	8
     96 	{ "_intrnames" },
     97 #define	X_EINTRNAMES	9
     98 	{ "_eintrnames" },
     99 #define	X_INTRCNT	10
    100 	{ "_intrcnt" },
    101 #define	X_EINTRCNT	11
    102 	{ "_eintrcnt" },
    103 #define	X_KMEMSTAT	12
    104 	{ "_kmemstats" },
    105 #define	X_KMEMBUCKETS	13
    106 	{ "_bucket" },
    107 #define X_ALLEVENTS	14
    108 	{ "_allevents" },
    109 #ifdef notdef
    110 #define	X_DEFICIT	15
    111 	{ "_deficit" },
    112 #define	X_FORKSTAT	16
    113 	{ "_forkstat" },
    114 #define X_REC		17
    115 	{ "_rectime" },
    116 #define X_PGIN		18
    117 	{ "_pgintime" },
    118 #define	X_XSTATS	19
    119 	{ "_xstats" },
    120 #define X_END		20
    121 #else
    122 #define X_END		15
    123 #endif
    124 #ifdef tahoe
    125 #define	X_VBDINIT	(X_END)
    126 	{ "_vbdinit" },
    127 #define	X_CKEYSTATS	(X_END+1)
    128 	{ "_ckeystats" },
    129 #define	X_DKEYSTATS	(X_END+2)
    130 	{ "_dkeystats" },
    131 #endif
    132 #if defined(pc532)
    133 #define	X_IVT		(X_END)
    134 	{ "_ivt" },
    135 #endif
    136 	{ "" },
    137 };
    138 
    139 /* Objects defined in dkstats.c */
    140 extern struct _disk	cur;
    141 extern char	**dr_name;
    142 extern int	*dk_select, dk_ndrive;
    143 
    144 struct	vmmeter sum, osum;
    145 int		ndrives;
    146 
    147 int	winlines = 20;
    148 
    149 kvm_t *kd;
    150 
    151 #define	FORKSTAT	0x01
    152 #define	INTRSTAT	0x02
    153 #define	MEMSTAT		0x04
    154 #define	SUMSTAT		0x08
    155 #define	TIMESTAT	0x10
    156 #define	VMSTAT		0x20
    157 
    158 void	cpustats __P((void));
    159 void	dkstats __P((void));
    160 void	dointr __P((void));
    161 void	domem __P((void));
    162 void	dosum __P((void));
    163 void	dovmstat __P((u_int, int));
    164 void	kread __P((int, void *, size_t));
    165 void	usage __P((void));
    166 #ifdef notdef
    167 void	dotimes __P((void));
    168 void	doforkst __P((void));
    169 #endif
    170 
    171 char	**choosedrives __P((char **));
    172 
    173 /* Namelist and memory file names. */
    174 char	*nlistf, *memf;
    175 
    176 main(argc, argv)
    177 	register int argc;
    178 	register char **argv;
    179 {
    180 	extern int optind;
    181 	extern char *optarg;
    182 	register int c, todo;
    183 	u_int interval;
    184 	int reps;
    185         char errbuf[_POSIX2_LINE_MAX];
    186 
    187 	memf = nlistf = NULL;
    188 	interval = reps = todo = 0;
    189 	while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) {
    190 		switch (c) {
    191 		case 'c':
    192 			reps = atoi(optarg);
    193 			break;
    194 #ifndef notdef
    195 		case 'f':
    196 			todo |= FORKSTAT;
    197 			break;
    198 #endif
    199 		case 'i':
    200 			todo |= INTRSTAT;
    201 			break;
    202 		case 'M':
    203 			memf = optarg;
    204 			break;
    205 		case 'm':
    206 			todo |= MEMSTAT;
    207 			break;
    208 		case 'N':
    209 			nlistf = optarg;
    210 			break;
    211 		case 's':
    212 			todo |= SUMSTAT;
    213 			break;
    214 #ifndef notdef
    215 		case 't':
    216 			todo |= TIMESTAT;
    217 			break;
    218 #endif
    219 		case 'w':
    220 			interval = atoi(optarg);
    221 			break;
    222 		case '?':
    223 		default:
    224 			usage();
    225 		}
    226 	}
    227 	argc -= optind;
    228 	argv += optind;
    229 
    230 	if (todo == 0)
    231 		todo = VMSTAT;
    232 
    233 	/*
    234 	 * Discard setgid privileges if not the running kernel so that bad
    235 	 * guys can't print interesting stuff from kernel memory.
    236 	 */
    237 	if (nlistf != NULL || memf != NULL)
    238 		setgid(getgid());
    239 
    240         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
    241 	if (kd == 0) {
    242 		(void)fprintf(stderr,
    243 		    "vmstat: kvm_openfiles: %s\n", errbuf);
    244 		exit(1);
    245 	}
    246 
    247 	if ((c = kvm_nlist(kd, namelist)) != 0) {
    248 		if (c > 0) {
    249 			(void)fprintf(stderr,
    250 			    "vmstat: undefined symbols:");
    251 			for (c = 0;
    252 			    c < sizeof(namelist)/sizeof(namelist[0]); c++)
    253 				if (namelist[c].n_type == 0)
    254 					fprintf(stderr, " %s",
    255 					    namelist[c].n_name);
    256 			(void)fputc('\n', stderr);
    257 		} else
    258 			(void)fprintf(stderr, "vmstat: kvm_nlist: %s\n",
    259 			    kvm_geterr(kd));
    260 		exit(1);
    261 	}
    262 
    263 	if (todo & VMSTAT) {
    264 		struct winsize winsize;
    265 
    266 		dkinit(0);	/* Initialize disk stats, no disks selected. */
    267 		argv = choosedrives(argv);	/* Select disks. */
    268 		winsize.ws_row = 0;
    269 		(void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
    270 		if (winsize.ws_row > 0)
    271 			winlines = winsize.ws_row;
    272 
    273 	}
    274 
    275 #define	BACKWARD_COMPATIBILITY
    276 #ifdef	BACKWARD_COMPATIBILITY
    277 	if (*argv) {
    278 		interval = atoi(*argv);
    279 		if (*++argv)
    280 			reps = atoi(*argv);
    281 	}
    282 #endif
    283 
    284 	if (interval) {
    285 		if (!reps)
    286 			reps = -1;
    287 	} else if (reps)
    288 		interval = 1;
    289 
    290 #ifdef notdef
    291 	if (todo & FORKSTAT)
    292 		doforkst();
    293 #endif
    294 	if (todo & MEMSTAT)
    295 		domem();
    296 	if (todo & SUMSTAT)
    297 		dosum();
    298 #ifdef notdef
    299 	if (todo & TIMESTAT)
    300 		dotimes();
    301 #endif
    302 	if (todo & INTRSTAT)
    303 		dointr();
    304 	if (todo & VMSTAT)
    305 		dovmstat(interval, reps);
    306 	exit(0);
    307 }
    308 
    309 char **
    310 choosedrives(argv)
    311 	char **argv;
    312 {
    313 	register int i;
    314 	register char **cp;
    315 	char buf[30];
    316 
    317 	/*
    318 	 * Choose drives to be displayed.  Priority goes to (in order) drives
    319 	 * supplied as arguments, default drives.  If everything isn't filled
    320 	 * in and there are drives not taken care of, display the first few
    321 	 * that fit.
    322 	 */
    323 #define BACKWARD_COMPATIBILITY
    324 	for (ndrives = 0; *argv; ++argv) {
    325 #ifdef	BACKWARD_COMPATIBILITY
    326 		if (isdigit(**argv))
    327 			break;
    328 #endif
    329 		for (i = 0; i < dk_ndrive; i++) {
    330 			if (strcmp(dr_name[i], *argv))
    331 				continue;
    332 			dk_select[i] = 1;
    333 			++ndrives;
    334 			break;
    335 		}
    336 	}
    337 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
    338 		if (dk_select[i])
    339 			continue;
    340 		dk_select[i] = 1;
    341 		++ndrives;
    342 	}
    343 	return(argv);
    344 }
    345 
    346 long
    347 getuptime()
    348 {
    349 	static time_t now;
    350 	static struct timeval boottime;
    351 	time_t uptime;
    352 
    353 	if (boottime.tv_sec == 0)
    354 		kread(X_BOOTTIME, &boottime, sizeof(boottime));
    355 	(void)time(&now);
    356 	uptime = now - boottime.tv_sec;
    357 	if (uptime <= 0 || uptime > 60*60*24*365*10) {
    358 		(void)fprintf(stderr,
    359 		    "vmstat: time makes no sense; namelist must be wrong.\n");
    360 		exit(1);
    361 	}
    362 	return(uptime);
    363 }
    364 
    365 int	hz, hdrcnt;
    366 
    367 void
    368 dovmstat(interval, reps)
    369 	u_int interval;
    370 	int reps;
    371 {
    372 	struct vmtotal total;
    373 	time_t uptime, halfuptime;
    374 	void needhdr();
    375 	int mib[2];
    376 	size_t size;
    377 
    378 	uptime = getuptime();
    379 	halfuptime = uptime / 2;
    380 	(void)signal(SIGCONT, needhdr);
    381 
    382 	if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
    383 		kread(X_STATHZ, &hz, sizeof(hz));
    384 	if (!hz)
    385 		kread(X_HZ, &hz, sizeof(hz));
    386 
    387 	for (hdrcnt = 1;;) {
    388 		if (!--hdrcnt)
    389 			printhdr();
    390 		/* Read new disk statistics */
    391 		dkreadstats();
    392 		kread(X_SUM, &sum, sizeof(sum));
    393 		size = sizeof(total);
    394 		mib[0] = CTL_VM;
    395 		mib[1] = VM_METER;
    396 		if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
    397 			printf("Can't get kerninfo: %s\n", strerror(errno));
    398 			bzero(&total, sizeof(total));
    399 		}
    400 		(void)printf("%2d%2d%2d",
    401 		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
    402 #define pgtok(a) ((a) * (sum.v_page_size >> 10))
    403 #define	rate(x)	(((x) + halfuptime) / uptime)	/* round */
    404 		(void)printf("%6ld%6ld ",
    405 		    pgtok(total.t_avm), pgtok(total.t_free));
    406 #ifdef NEWVM
    407 		(void)printf("%4lu ", rate(sum.v_faults - osum.v_faults));
    408 		(void)printf("%3lu ",
    409 		    rate(sum.v_reactivated - osum.v_reactivated));
    410 		(void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins));
    411 		(void)printf("%3lu %3lu ",
    412 		    rate(sum.v_pageouts - osum.v_pageouts), 0);
    413 #else
    414 		(void)printf("%3lu %2lu ",
    415 		    rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) -
    416 		    (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))),
    417 		    rate(sum.v_xsfrec + sum.v_xifrec -
    418 		    osum.v_xsfrec - osum.v_xifrec));
    419 		(void)printf("%3lu ",
    420 		    rate(pgtok(sum.v_pgpgin - osum.v_pgpgin)));
    421 		(void)printf("%3lu %3lu ",
    422 		    rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)),
    423 		    rate(pgtok(sum.v_dfree - osum.v_dfree)));
    424 		(void)printf("%3d ", pgtok(deficit));
    425 #endif
    426 		(void)printf("%3lu ", rate(sum.v_scan - osum.v_scan));
    427 		dkstats();
    428 		(void)printf("%4lu %4lu %3lu ",
    429 		    rate(sum.v_intr - osum.v_intr),
    430 		    rate(sum.v_syscall - osum.v_syscall),
    431 		    rate(sum.v_swtch - osum.v_swtch));
    432 		cpustats();
    433 		(void)printf("\n");
    434 		(void)fflush(stdout);
    435 		if (reps >= 0 && --reps <= 0)
    436 			break;
    437 		osum = sum;
    438 		uptime = interval;
    439 		/*
    440 		 * We round upward to avoid losing low-frequency events
    441 		 * (i.e., >= 1 per interval but < 1 per second).
    442 		 */
    443 		halfuptime = (uptime + 1) / 2;
    444 		(void)sleep(interval);
    445 	}
    446 }
    447 
    448 printhdr()
    449 {
    450 	register int i;
    451 
    452 	(void)printf(" procs   memory     page%*s", 20, "");
    453 	if (ndrives > 0)
    454 #ifdef NEWVM
    455 		(void)printf("%s %*sfaults   cpu\n",
    456 		   ((ndrives > 1) ? "disks" : "disk"),
    457 #else
    458 		(void)printf("disks %*sfaults      cpu\n",
    459 #endif
    460 		   ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
    461 	else
    462 #ifdef NEWVM
    463 		(void)printf("%*s  faults   cpu\n",
    464 #else
    465 		(void)printf("%*s  faults      cpu\n",
    466 #endif
    467 		   ndrives * 3, "");
    468 
    469 #ifdef NEWVM
    470 	(void)printf(" r b w   avm   fre  flt  re  pi  po  fr  sr ");
    471 #else
    472 	(void)printf(" r b w   avm   fre  re at  pi  po  fr  de  sr ");
    473 #endif
    474 	for (i = 0; i < dk_ndrive; i++)
    475 		if (dk_select[i])
    476 			(void)printf("%c%c ", dr_name[i][0],
    477 			    dr_name[i][strlen(dr_name[i]) - 1]);
    478 	(void)printf("  in   sy  cs us sy id\n");
    479 	hdrcnt = winlines - 2;
    480 }
    481 
    482 /*
    483  * Force a header to be prepended to the next output.
    484  */
    485 void
    486 needhdr()
    487 {
    488 
    489 	hdrcnt = 1;
    490 }
    491 
    492 #ifdef notdef
    493 void
    494 dotimes()
    495 {
    496 	u_int pgintime, rectime;
    497 
    498 	kread(X_REC, &rectime, sizeof(rectime));
    499 	kread(X_PGIN, &pgintime, sizeof(pgintime));
    500 	kread(X_SUM, &sum, sizeof(sum));
    501 	(void)printf("%u reclaims, %u total time (usec)\n",
    502 	    sum.v_pgrec, rectime);
    503 	(void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
    504 	(void)printf("\n");
    505 	(void)printf("%u page ins, %u total time (msec)\n",
    506 	    sum.v_pgin, pgintime / 10);
    507 	(void)printf("average: %8.1f msec / page in\n",
    508 	    pgintime / (sum.v_pgin * 10.0));
    509 }
    510 #endif
    511 
    512 pct(top, bot)
    513 	long top, bot;
    514 {
    515 	long ans;
    516 
    517 	if (bot == 0)
    518 		return(0);
    519 	ans = (quad_t)top * 100 / bot;
    520 	return (ans);
    521 }
    522 
    523 #define	PCT(top, bot) pct((long)(top), (long)(bot))
    524 
    525 #if defined(tahoe)
    526 #include <machine/cpu.h>
    527 #endif
    528 
    529 void
    530 dosum()
    531 {
    532 	struct nchstats nchstats;
    533 #ifndef NEWVM
    534 	struct xstats xstats;
    535 #endif
    536 	long nchtotal;
    537 #if defined(tahoe)
    538 	struct keystats keystats;
    539 #endif
    540 
    541 	kread(X_SUM, &sum, sizeof(sum));
    542 	(void)printf("%9u cpu context switches\n", sum.v_swtch);
    543 	(void)printf("%9u device interrupts\n", sum.v_intr);
    544 	(void)printf("%9u software interrupts\n", sum.v_soft);
    545 	(void)printf("%9u traps\n", sum.v_trap);
    546 	(void)printf("%9u system calls\n", sum.v_syscall);
    547 	(void)printf("%9u total faults taken\n", sum.v_faults);
    548 	(void)printf("%9u swap ins\n", sum.v_swpin);
    549 	(void)printf("%9u swap outs\n", sum.v_swpout);
    550 	(void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE);
    551 	(void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE);
    552 	(void)printf("%9u page ins\n", sum.v_pageins);
    553 	(void)printf("%9u page outs\n", sum.v_pageouts);
    554 	(void)printf("%9u pages paged in\n", sum.v_pgpgin);
    555 	(void)printf("%9u pages paged out\n", sum.v_pgpgout);
    556 	(void)printf("%9u pages reactivated\n", sum.v_reactivated);
    557 	(void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
    558 	(void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE);
    559 	(void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE);
    560 	(void)printf("%9u pages examined by the clock daemon\n", sum.v_scan);
    561 	(void)printf("%9u revolutions of the clock hand\n", sum.v_rev);
    562 #ifdef NEWVM
    563 	(void)printf("%9u VM object cache lookups\n", sum.v_lookups);
    564 	(void)printf("%9u VM object hits\n", sum.v_hits);
    565 	(void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
    566 	(void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
    567 	(void)printf("%9u pages freed by daemon\n", sum.v_dfree);
    568 	(void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
    569 	(void)printf("%9u pages free\n", sum.v_free_count);
    570 	(void)printf("%9u pages wired down\n", sum.v_wire_count);
    571 	(void)printf("%9u pages active\n", sum.v_active_count);
    572 	(void)printf("%9u pages inactive\n", sum.v_inactive_count);
    573 	(void)printf("%9u bytes per page\n", sum.v_page_size);
    574 #else
    575 	(void)printf("%9u sequential process pages freed\n", sum.v_seqfree);
    576 	(void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec,
    577 	    PCT(sum.v_fastpgrec, sum.v_pgrec));
    578 	(void)printf("%9u reclaims from free list\n", sum.v_pgfrec);
    579 	(void)printf("%9u executable fill pages created\n",
    580 	    sum.v_nexfod / CLSIZE);
    581 	(void)printf("%9u executable fill page faults\n",
    582 	    sum.v_exfod / CLSIZE);
    583 	(void)printf("%9u swap text pages found in free list\n",
    584 	    sum.v_xsfrec);
    585 	(void)printf("%9u inode text pages found in free list\n",
    586 	    sum.v_xifrec);
    587 	(void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE);
    588 	(void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE);
    589 	(void)printf("%9u pages freed by the clock daemon\n",
    590 	    sum.v_dfree / CLSIZE);
    591 #endif
    592 	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
    593 	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
    594 	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
    595 	    nchstats.ncs_miss + nchstats.ncs_long;
    596 	(void)printf("%9ld total name lookups\n", nchtotal);
    597 	(void)printf(
    598 	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
    599 	    "", PCT(nchstats.ncs_goodhits, nchtotal),
    600 	    PCT(nchstats.ncs_neghits, nchtotal),
    601 	    PCT(nchstats.ncs_pass2, nchtotal));
    602 	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
    603 	    PCT(nchstats.ncs_badhits, nchtotal),
    604 	    PCT(nchstats.ncs_falsehits, nchtotal),
    605 	    PCT(nchstats.ncs_long, nchtotal));
    606 #ifndef NEWVM
    607 	kread(X_XSTATS, &xstats, sizeof(xstats));
    608 	(void)printf("%9lu total calls to xalloc (cache hits %d%%)\n",
    609 	    xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc));
    610 	(void)printf("%9s sticky %lu flushed %lu unused %lu\n", "",
    611 	    xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused);
    612 	(void)printf("%9lu total calls to xfree", xstats.free);
    613 	(void)printf(" (sticky %lu cached %lu swapped %lu)\n",
    614 	    xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap);
    615 #endif
    616 #if defined(tahoe)
    617 	kread(X_CKEYSTATS, &keystats, sizeof(keystats));
    618 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
    619 	    keystats.ks_allocs, "code cache keys allocated",
    620 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
    621 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
    622 	    PCT(keystats.ks_taken, keystats.ks_allocs),
    623 	    PCT(keystats.ks_shared, keystats.ks_allocs));
    624 	kread(X_DKEYSTATS, &keystats, sizeof(keystats));
    625 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
    626 	    keystats.ks_allocs, "data cache keys allocated",
    627 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
    628 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
    629 	    PCT(keystats.ks_taken, keystats.ks_allocs),
    630 	    PCT(keystats.ks_shared, keystats.ks_allocs));
    631 #endif
    632 }
    633 
    634 #ifdef notdef
    635 void
    636 doforkst()
    637 {
    638 	struct forkstat fks;
    639 
    640 	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
    641 	(void)printf("%d forks, %d pages, average %.2f\n",
    642 	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
    643 	(void)printf("%d vforks, %d pages, average %.2f\n",
    644 	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
    645 }
    646 #endif
    647 
    648 void
    649 dkstats()
    650 {
    651 	register int dn, state;
    652 	double etime;
    653 	long tmp;
    654 
    655 	/* Calculate disk stat deltas. */
    656 	dkswap();
    657 	etime = 0;
    658 	for (state = 0; state < CPUSTATES; ++state) {
    659 		etime += cur.cp_time[state];
    660 	}
    661 	if (etime == 0)
    662 		etime = 1;
    663 	etime /= hz;
    664 	for (dn = 0; dn < dk_ndrive; ++dn) {
    665 		if (!dk_select[dn])
    666 			continue;
    667 		(void)printf("%2.0f ", cur.dk_xfer[dn] / etime);
    668 	}
    669 }
    670 
    671 void
    672 cpustats()
    673 {
    674 	register int state;
    675 	double pct, total;
    676 
    677 	total = 0;
    678 	for (state = 0; state < CPUSTATES; ++state)
    679 		total += cur.cp_time[state];
    680 	if (total)
    681 		pct = 100 / total;
    682 	else
    683 		pct = 0;
    684 	(void)printf("%2.0f ", (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct);
    685 	(void)printf("%2.0f ", (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct);
    686 	(void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct);
    687 }
    688 
    689 #if defined(pc532)
    690 /* To get struct iv ...*/
    691 #define _KERNEL
    692 #include <machine/psl.h>
    693 #undef _KERNEL
    694 void
    695 dointr()
    696 {
    697 	register long i, j, inttotal, uptime;
    698 	static char iname[64];
    699 	struct iv ivt[32], *ivp = ivt;
    700 
    701 	iname[63] = '\0';
    702 	uptime = getuptime();
    703 	kread(X_IVT, ivp, sizeof(ivt));
    704 
    705 	for (i = 0; i < 2; i++) {
    706 		(void)printf("%sware interrupts:\n", i ? "\nsoft" : "hard");
    707 		(void)printf("interrupt       total     rate\n");
    708 		inttotal = 0;
    709 		for (j = 0; j < 16; j++, ivp++) {
    710 			if (ivp->iv_vec && ivp->iv_use && ivp->iv_cnt) {
    711 				if (kvm_read(kd, (u_long)ivp->iv_use, iname, 63) != 63) {
    712 					(void)fprintf(stderr, "vmstat: iv_use: %s\n",
    713 					    kvm_geterr(kd));
    714 					exit(1);
    715 				}
    716 				(void)printf("%-12s %8ld %8ld\n", iname,
    717 				    ivp->iv_cnt, ivp->iv_cnt / uptime);
    718 				inttotal += ivp->iv_cnt;
    719 			}
    720 		}
    721 		(void)printf("Total        %8ld %8ld\n",
    722 		    inttotal, inttotal / uptime);
    723 	}
    724 }
    725 #else
    726 void
    727 dointr()
    728 {
    729 	register long *intrcnt, inttotal, uptime;
    730 	register int nintr, inamlen;
    731 	register char *intrname;
    732 	struct evcntlist allevents;
    733 	struct evcnt evcnt, *evptr;
    734 	struct device dev;
    735 
    736 	uptime = getuptime();
    737 	nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
    738 	inamlen =
    739 	    namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
    740 	intrcnt = malloc((size_t)nintr);
    741 	intrname = malloc((size_t)inamlen);
    742 	if (intrcnt == NULL || intrname == NULL) {
    743 		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
    744 		exit(1);
    745 	}
    746 	kread(X_INTRCNT, intrcnt, (size_t)nintr);
    747 	kread(X_INTRNAMES, intrname, (size_t)inamlen);
    748 	(void)printf("interrupt         total     rate\n");
    749 	inttotal = 0;
    750 	nintr /= sizeof(long);
    751 	while (--nintr >= 0) {
    752 		if (*intrcnt)
    753 			(void)printf("%-14s %8ld %8ld\n", intrname,
    754 			    *intrcnt, *intrcnt / uptime);
    755 		intrname += strlen(intrname) + 1;
    756 		inttotal += *intrcnt++;
    757 	}
    758 	kread(X_ALLEVENTS, &allevents, sizeof allevents);
    759 	evptr = allevents.tqh_first;
    760 	while (evptr) {
    761 		if (kvm_read(kd, (long)evptr, (void *)&evcnt,
    762 		    sizeof evcnt) != sizeof evcnt) {
    763 			(void)fprintf(stderr, "vmstat: event chain trashed\n",
    764 			    kvm_geterr(kd));
    765 			exit(1);
    766 		}
    767 		if (strcmp(evcnt.ev_name, "intr") == 0) {
    768 			if (kvm_read(kd, (long)evcnt.ev_dev, (void *)&dev,
    769 			    sizeof dev) != sizeof dev) {
    770 				(void)fprintf(stderr, "vmstat: event chain trashed\n",
    771 				    kvm_geterr(kd));
    772 				exit(1);
    773 			}
    774 			if (evcnt.ev_count)
    775 				(void)printf("%-14s %8ld %8ld\n", dev.dv_xname,
    776 				    evcnt.ev_count, evcnt.ev_count / uptime);
    777 			inttotal += evcnt.ev_count++;
    778 		}
    779 		evptr = evcnt.ev_list.tqe_next;
    780 	}
    781 	(void)printf("Total          %8ld %8ld\n", inttotal, inttotal / uptime);
    782 }
    783 #endif
    784 
    785 /*
    786  * These names are defined in <sys/malloc.h>.
    787  */
    788 char *kmemnames[] = INITKMEMNAMES;
    789 
    790 void
    791 domem()
    792 {
    793 	register struct kmembuckets *kp;
    794 	register struct kmemstats *ks;
    795 	register int i, j;
    796 	int len, size, first;
    797 	long totuse = 0, totfree = 0, totreq = 0;
    798 	char *name;
    799 	struct kmemstats kmemstats[M_LAST];
    800 	struct kmembuckets buckets[MINBUCKET + 16];
    801 
    802 	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
    803 	(void)printf("Memory statistics by bucket size\n");
    804 	(void)printf(
    805 	    "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
    806 	for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
    807 		if (kp->kb_calls == 0)
    808 			continue;
    809 		size = 1 << i;
    810 		(void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
    811 			kp->kb_total - kp->kb_totalfree,
    812 			kp->kb_totalfree, kp->kb_calls,
    813 			kp->kb_highwat, kp->kb_couldfree);
    814 		totfree += size * kp->kb_totalfree;
    815 	}
    816 
    817 	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
    818 	(void)printf("\nMemory usage type by bucket size\n");
    819 	(void)printf("    Size  Type(s)\n");
    820 	kp = &buckets[MINBUCKET];
    821 	for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
    822 		if (kp->kb_calls == 0)
    823 			continue;
    824 		first = 1;
    825 		len = 8;
    826 		for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    827 			if (ks->ks_calls == 0)
    828 				continue;
    829 			if ((ks->ks_size & j) == 0)
    830 				continue;
    831 			name = kmemnames[i] ? kmemnames[i] : "undefined";
    832 			len += 2 + strlen(name);
    833 			if (first)
    834 				printf("%8d  %s", j, name);
    835 			else
    836 				printf(",");
    837 			if (len >= 80) {
    838 				printf("\n\t ");
    839 				len = 10 + strlen(name);
    840 			}
    841 			if (!first)
    842 				printf(" %s", name);
    843 			first = 0;
    844 		}
    845 		printf("\n");
    846 	}
    847 
    848 	(void)printf(
    849 	    "\nMemory statistics by type                        Type  Kern\n");
    850 	(void)printf(
    851 "       Type  InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
    852 	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    853 		if (ks->ks_calls == 0)
    854 			continue;
    855 		(void)printf("%12s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
    856 		    kmemnames[i] ? kmemnames[i] : "undefined",
    857 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
    858 		    (ks->ks_maxused + 1023) / 1024,
    859 		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
    860 		    ks->ks_limblocks, ks->ks_mapblocks);
    861 		first = 1;
    862 		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
    863 			if ((ks->ks_size & j) == 0)
    864 				continue;
    865 			if (first)
    866 				printf("  %d", j);
    867 			else
    868 				printf(",%d", j);
    869 			first = 0;
    870 		}
    871 		printf("\n");
    872 		totuse += ks->ks_memuse;
    873 		totreq += ks->ks_calls;
    874 	}
    875 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
    876 	(void)printf("              %7ldK %6ldK    %8ld\n",
    877 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
    878 }
    879 
    880 /*
    881  * kread reads something from the kernel, given its nlist index.
    882  */
    883 void
    884 kread(nlx, addr, size)
    885 	int nlx;
    886 	void *addr;
    887 	size_t size;
    888 {
    889 	char *sym;
    890 
    891 	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
    892 		sym = namelist[nlx].n_name;
    893 		if (*sym == '_')
    894 			++sym;
    895 		(void)fprintf(stderr,
    896 		    "vmstat: symbol %s not defined\n", sym);
    897 		exit(1);
    898 	}
    899 	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
    900 		sym = namelist[nlx].n_name;
    901 		if (*sym == '_')
    902 			++sym;
    903 		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
    904 		exit(1);
    905 	}
    906 }
    907 
    908 void
    909 usage()
    910 {
    911 	(void)fprintf(stderr,
    912 #ifndef NEWVM
    913 	    "usage: vmstat [-fimst] [-c count] [-M core] \
    914 [-N system] [-w wait] [disks]\n");
    915 #else
    916 	    "usage: vmstat [-ims] [-c count] [-M core] \
    917 [-N system] [-w wait] [disks]\n");
    918 #endif
    919 	exit(1);
    920 }
    921 
    922