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