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