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