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