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