Home | History | Annotate | Line # | Download | only in vmstat
vmstat.c revision 1.13
      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.1 (Berkeley) 6/6/93";
     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 #else
    586 	(void)printf("%9u sequential process pages freed\n", sum.v_seqfree);
    587 	(void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec,
    588 	    PCT(sum.v_fastpgrec, sum.v_pgrec));
    589 	(void)printf("%9u reclaims from free list\n", sum.v_pgfrec);
    590 	(void)printf("%9u executable fill pages created\n",
    591 	    sum.v_nexfod / CLSIZE);
    592 	(void)printf("%9u executable fill page faults\n",
    593 	    sum.v_exfod / CLSIZE);
    594 	(void)printf("%9u swap text pages found in free list\n",
    595 	    sum.v_xsfrec);
    596 	(void)printf("%9u inode text pages found in free list\n",
    597 	    sum.v_xifrec);
    598 	(void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE);
    599 	(void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE);
    600 	(void)printf("%9u pages freed by the clock daemon\n",
    601 	    sum.v_dfree / CLSIZE);
    602 #endif
    603 	kread(X_NCHSTATS, &nchstats, sizeof(nchstats));
    604 	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
    605 	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
    606 	    nchstats.ncs_miss + nchstats.ncs_long;
    607 	(void)printf("%9ld total name lookups\n", nchtotal);
    608 	(void)printf(
    609 	    "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
    610 	    "", PCT(nchstats.ncs_goodhits, nchtotal),
    611 	    PCT(nchstats.ncs_neghits, nchtotal),
    612 	    PCT(nchstats.ncs_pass2, nchtotal));
    613 	(void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
    614 	    PCT(nchstats.ncs_badhits, nchtotal),
    615 	    PCT(nchstats.ncs_falsehits, nchtotal),
    616 	    PCT(nchstats.ncs_long, nchtotal));
    617 #ifndef NEWVM
    618 	kread(X_XSTATS, &xstats, sizeof(xstats));
    619 	(void)printf("%9lu total calls to xalloc (cache hits %d%%)\n",
    620 	    xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc));
    621 	(void)printf("%9s sticky %lu flushed %lu unused %lu\n", "",
    622 	    xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused);
    623 	(void)printf("%9lu total calls to xfree", xstats.free);
    624 	(void)printf(" (sticky %lu cached %lu swapped %lu)\n",
    625 	    xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap);
    626 #endif
    627 #if defined(tahoe)
    628 	kread(X_CKEYSTATS, &keystats, sizeof(keystats));
    629 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
    630 	    keystats.ks_allocs, "code cache keys allocated",
    631 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
    632 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
    633 	    PCT(keystats.ks_taken, keystats.ks_allocs),
    634 	    PCT(keystats.ks_shared, keystats.ks_allocs));
    635 	kread(X_DKEYSTATS, &keystats, sizeof(keystats));
    636 	(void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n",
    637 	    keystats.ks_allocs, "data cache keys allocated",
    638 	    PCT(keystats.ks_allocfree, keystats.ks_allocs),
    639 	    PCT(keystats.ks_norefs, keystats.ks_allocs),
    640 	    PCT(keystats.ks_taken, keystats.ks_allocs),
    641 	    PCT(keystats.ks_shared, keystats.ks_allocs));
    642 #endif
    643 }
    644 
    645 #ifdef notdef
    646 void
    647 doforkst()
    648 {
    649 	struct forkstat fks;
    650 
    651 	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
    652 	(void)printf("%d forks, %d pages, average %.2f\n",
    653 	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
    654 	(void)printf("%d vforks, %d pages, average %.2f\n",
    655 	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
    656 }
    657 #endif
    658 
    659 void
    660 dkstats()
    661 {
    662 	register int dn, state;
    663 	double etime;
    664 	long tmp;
    665 
    666 	for (dn = 0; dn < dk_ndrive; ++dn) {
    667 		tmp = cur.xfer[dn];
    668 		cur.xfer[dn] -= last.xfer[dn];
    669 		last.xfer[dn] = tmp;
    670 	}
    671 	etime = 0;
    672 	for (state = 0; state < CPUSTATES; ++state) {
    673 		tmp = cur.time[state];
    674 		cur.time[state] -= last.time[state];
    675 		last.time[state] = tmp;
    676 		etime += cur.time[state];
    677 	}
    678 	if (etime == 0)
    679 		etime = 1;
    680 	etime /= hz;
    681 	for (dn = 0; dn < dk_ndrive; ++dn) {
    682 		if (!dr_select[dn])
    683 			continue;
    684 		(void)printf("%2.0f ", cur.xfer[dn] / etime);
    685 	}
    686 }
    687 
    688 void
    689 cpustats()
    690 {
    691 	register int state;
    692 	double pct, total;
    693 
    694 	total = 0;
    695 	for (state = 0; state < CPUSTATES; ++state)
    696 		total += cur.time[state];
    697 	if (total)
    698 		pct = 100 / total;
    699 	else
    700 		pct = 0;
    701 	(void)printf("%2.0f ", (cur.time[CP_USER] + cur.time[CP_NICE]) * pct);
    702 	(void)printf("%2.0f ", (cur.time[CP_SYS] + cur.time[CP_INTR]) * pct);
    703 	(void)printf("%2.0f", cur.time[CP_IDLE] * pct);
    704 }
    705 
    706 void
    707 dointr()
    708 {
    709 	register long *intrcnt, inttotal, uptime;
    710 	register int nintr, inamlen;
    711 	register char *intrname;
    712 
    713 	uptime = getuptime();
    714 	nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
    715 	inamlen =
    716 	    namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
    717 	intrcnt = malloc((size_t)nintr);
    718 	intrname = malloc((size_t)inamlen);
    719 	if (intrcnt == NULL || intrname == NULL) {
    720 		(void)fprintf(stderr, "vmstat: %s.\n", strerror(errno));
    721 		exit(1);
    722 	}
    723 	kread(X_INTRCNT, intrcnt, (size_t)nintr);
    724 	kread(X_INTRNAMES, intrname, (size_t)inamlen);
    725 	(void)printf("interrupt      total      rate\n");
    726 	inttotal = 0;
    727 	nintr /= sizeof(long);
    728 	while (--nintr >= 0) {
    729 		if (*intrcnt)
    730 			(void)printf("%-12s %8ld %8ld\n", intrname,
    731 			    *intrcnt, *intrcnt / uptime);
    732 		intrname += strlen(intrname) + 1;
    733 		inttotal += *intrcnt++;
    734 	}
    735 	(void)printf("Total        %8ld %8ld\n", inttotal, inttotal / uptime);
    736 }
    737 
    738 /*
    739  * These names are defined in <sys/malloc.h>.
    740  */
    741 char *kmemnames[] = INITKMEMNAMES;
    742 
    743 void
    744 domem()
    745 {
    746 	register struct kmembuckets *kp;
    747 	register struct kmemstats *ks;
    748 	register int i, j;
    749 	int len, size, first;
    750 	long totuse = 0, totfree = 0, totreq = 0;
    751 	char *name;
    752 	struct kmemstats kmemstats[M_LAST];
    753 	struct kmembuckets buckets[MINBUCKET + 16];
    754 
    755 	kread(X_KMEMBUCKETS, buckets, sizeof(buckets));
    756 	(void)printf("Memory statistics by bucket size\n");
    757 	(void)printf(
    758 	    "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
    759 	for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
    760 		if (kp->kb_calls == 0)
    761 			continue;
    762 		size = 1 << i;
    763 		(void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
    764 			kp->kb_total - kp->kb_totalfree,
    765 			kp->kb_totalfree, kp->kb_calls,
    766 			kp->kb_highwat, kp->kb_couldfree);
    767 		totfree += size * kp->kb_totalfree;
    768 	}
    769 
    770 	kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats));
    771 	(void)printf("\nMemory usage type by bucket size\n");
    772 	(void)printf("    Size  Type(s)\n");
    773 	kp = &buckets[MINBUCKET];
    774 	for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
    775 		if (kp->kb_calls == 0)
    776 			continue;
    777 		first = 1;
    778 		len = 8;
    779 		for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    780 			if (ks->ks_calls == 0)
    781 				continue;
    782 #ifdef notdef
    783 			if ((ks->ks_size & j) == 0)
    784 				continue;
    785 #endif
    786 			name = kmemnames[i] ? kmemnames[i] : "undefined";
    787 			len += 2 + strlen(name);
    788 			if (first)
    789 				printf("%8d  %s", j, name);
    790 			else
    791 				printf(",");
    792 			if (len >= 80) {
    793 				printf("\n\t ");
    794 				len = 10 + strlen(name);
    795 			}
    796 			if (!first)
    797 				printf(" %s", name);
    798 			first = 0;
    799 		}
    800 		printf("\n");
    801 	}
    802 
    803 	(void)printf(
    804 	    "\nMemory statistics by type                        Type  Kern\n");
    805 	(void)printf(
    806 "      Type  InUse MemUse HighUse  Limit Requests Limit Limit Size(s)\n");
    807 	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
    808 #ifdef notdef
    809 		if (ks->ks_calls == 0)
    810 			continue;
    811 #endif
    812 		(void)printf("%11s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
    813 		    kmemnames[i] ? kmemnames[i] : "undefined",
    814 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
    815 		    (ks->ks_maxused + 1023) / 1024,
    816 		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
    817 		    ks->ks_limblocks, ks->ks_mapblocks);
    818 		first = 1;
    819 		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
    820 #ifdef notdef
    821 			if ((ks->ks_size & j) == 0)
    822 				continue;
    823 			if (first)
    824 				printf("  %d", j);
    825 			else
    826 				printf(",%d", j);
    827 			first = 0;
    828 #endif
    829 		}
    830 		printf("\n");
    831 		totuse += ks->ks_memuse;
    832 		totreq += ks->ks_calls;
    833 	}
    834 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
    835 	(void)printf("              %7ldK %6ldK    %8ld\n",
    836 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
    837 }
    838 
    839 /*
    840  * kread reads something from the kernel, given its nlist index.
    841  */
    842 void
    843 kread(nlx, addr, size)
    844 	int nlx;
    845 	void *addr;
    846 	size_t size;
    847 {
    848 	char *sym;
    849 
    850 	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
    851 		sym = namelist[nlx].n_name;
    852 		if (*sym == '_')
    853 			++sym;
    854 		(void)fprintf(stderr,
    855 		    "vmstat: symbol %s not defined\n", sym);
    856 		exit(1);
    857 	}
    858 	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) {
    859 		sym = namelist[nlx].n_name;
    860 		if (*sym == '_')
    861 			++sym;
    862 		(void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd));
    863 		exit(1);
    864 	}
    865 }
    866 
    867 void
    868 usage()
    869 {
    870 	(void)fprintf(stderr,
    871 #ifndef NEWVM
    872 	    "usage: vmstat [-fimst] [-c count] [-M core] \
    873 [-N system] [-w wait] [disks]\n");
    874 #else
    875 	    "usage: vmstat [-ims] [-c count] [-M core] \
    876 [-N system] [-w wait] [disks]\n");
    877 #endif
    878 	exit(1);
    879 }
    880