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