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