xref: /dragonfly/usr.bin/vmstat/vmstat.c (revision 0db87cb7)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1980, 1986, 1991, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)vmstat.c	8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/vmstat/vmstat.c,v 1.38.2.4 2001/07/31 19:52:41 tmm Exp $
32  */
33 
34 #include <sys/user.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/uio.h>
38 #include <sys/namei.h>
39 #include <sys/malloc.h>
40 #include <sys/signal.h>
41 #include <sys/fcntl.h>
42 #include <sys/ioctl.h>
43 #include <sys/sysctl.h>
44 #include <sys/vmmeter.h>
45 #include <sys/interrupt.h>
46 
47 #include <vm/vm_param.h>
48 
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <kinfo.h>
53 #include <kvm.h>
54 #include <limits.h>
55 #include <nlist.h>
56 #include <paths.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <sysexits.h>
61 #include <time.h>
62 #include <unistd.h>
63 #include <devstat.h>
64 
65 static struct nlist namelist[] = {
66 #define	X_BOOTTIME	0
67 	{ "_boottime",	0, 0, 0, 0 },
68 #define X_NCHSTATS	1
69 	{ "_nchstats",	0, 0, 0, 0 },
70 #define	X_KMEMSTATISTICS	2
71 	{ "_kmemstatistics",	0, 0, 0, 0 },
72 #define	X_ZLIST		3
73 	{ "_zlist",	0, 0, 0, 0 },
74 #ifdef notyet
75 #define	X_DEFICIT	4
76 	{ "_deficit",	0, 0, 0, 0 },
77 #define	X_FORKSTAT	5
78 	{ "_forkstat",	0, 0, 0, 0 },
79 #define X_REC		6
80 	{ "_rectime",	0, 0, 0, 0 },
81 #define X_PGIN		7
82 	{ "_pgintime",	0, 0, 0, 0 },
83 #define	X_XSTATS	8
84 	{ "_xstats",	0, 0, 0, 0 },
85 #define X_END		9
86 #else
87 #define X_END		4
88 #endif
89 	{ "", 0, 0, 0, 0 },
90 };
91 
92 struct statinfo cur, last;
93 int num_devices, maxshowdevs;
94 long generation;
95 struct device_selection *dev_select;
96 int num_selected;
97 struct devstat_match *matches;
98 int num_matches = 0;
99 int num_devices_specified, num_selections;
100 long select_generation;
101 char **specified_devices;
102 devstat_select_mode select_mode;
103 
104 struct	vmmeter vmm, ovmm;
105 struct	vmstats vms, ovms;
106 
107 int	winlines = 20;
108 int	nflag = 0;
109 int	verbose = 0;
110 
111 kvm_t *kd;
112 
113 struct kinfo_cputime cp_time, old_cp_time, diff_cp_time;
114 
115 #define	FORKSTAT	0x01
116 #define	INTRSTAT	0x02
117 #define	MEMSTAT		0x04
118 #define	SUMSTAT		0x08
119 #define	TIMESTAT	0x10
120 #define	VMSTAT		0x20
121 #define ZMEMSTAT	0x40
122 
123 static void cpustats(void);
124 static void dointr(void);
125 static void domem(void);
126 static void dosum(void);
127 static void dozmem(void);
128 static void dovmstat(u_int, int);
129 static void kread(int, void *, size_t);
130 static void usage(void);
131 static char **getdrivedata(char **);
132 static long getuptime(void);
133 static void needhdr(int);
134 static long pct(long, long);
135 
136 #ifdef notyet
137 static void dotimes(void); /* Not implemented */
138 static void doforkst(void);
139 #endif
140 static void printhdr(void);
141 static void devstats(void);
142 
143 int
144 main(int argc, char **argv)
145 {
146 	int c, todo;
147 	u_int interval;		/* milliseconds */
148 	int reps;
149 	char *memf, *nlistf;
150 	char errbuf[_POSIX2_LINE_MAX];
151 
152 	memf = nlistf = NULL;
153 	interval = reps = todo = 0;
154 	maxshowdevs = 2;
155 	while ((c = getopt(argc, argv, "c:fiM:mN:n:p:stvw:z")) != -1) {
156 		switch (c) {
157 		case 'c':
158 			reps = atoi(optarg);
159 			break;
160 		case 'f':
161 #ifdef notyet
162 			todo |= FORKSTAT;
163 #else
164 			errx(EX_USAGE, "sorry, -f is not (re)implemented yet");
165 #endif
166 			break;
167 		case 'i':
168 			todo |= INTRSTAT;
169 			break;
170 		case 'M':
171 			memf = optarg;
172 			break;
173 		case 'm':
174 			todo |= MEMSTAT;
175 			break;
176 		case 'N':
177 			nlistf = optarg;
178 			break;
179 		case 'n':
180 			nflag = 1;
181 			maxshowdevs = atoi(optarg);
182 			if (maxshowdevs < 0)
183 				errx(1, "number of devices %d is < 0",
184 				     maxshowdevs);
185 			break;
186 		case 'p':
187 			if (buildmatch(optarg, &matches, &num_matches) != 0)
188 				errx(1, "%s", devstat_errbuf);
189 			break;
190 		case 's':
191 			todo |= SUMSTAT;
192 			break;
193 		case 't':
194 #ifdef notyet
195 			todo |= TIMESTAT;
196 #else
197 			errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
198 #endif
199 			break;
200 		case 'v':
201 			++verbose;
202 			break;
203 		case 'w':
204 			interval = (u_int)(strtod(optarg, NULL) * 1000.0);
205 			break;
206 		case 'z':
207 			todo |= ZMEMSTAT;
208 			break;
209 		default:
210 			usage();
211 		}
212 	}
213 	argc -= optind;
214 	argv += optind;
215 
216 	if (todo == 0)
217 		todo = VMSTAT;
218 
219 	/*
220 	 * Discard setgid privileges if not the running kernel so that bad
221 	 * guys can't print interesting stuff from kernel memory.
222 	 */
223 	if (nlistf != NULL || memf != NULL)
224 		setgid(getgid());
225 
226 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
227 	if (kd == NULL)
228 		errx(1, "kvm_openfiles: %s", errbuf);
229 
230 	if ((c = kvm_nlist(kd, namelist)) != 0) {
231 		if (c > 0) {
232 			warnx("undefined symbols:");
233 			for (c = 0; c < (int)__arysize(namelist); c++)
234 				if (namelist[c].n_type == 0)
235 					fprintf(stderr, " %s",
236 					    namelist[c].n_name);
237 			fputc('\n', stderr);
238 		} else
239 			warnx("kvm_nlist: %s", kvm_geterr(kd));
240 		exit(1);
241 	}
242 
243 	if (todo & VMSTAT) {
244 		struct winsize winsize;
245 
246 		/*
247 		 * Make sure that the userland devstat version matches the
248 		 * kernel devstat version.  If not, exit and print a
249 		 * message informing the user of his mistake.
250 		 */
251 		if (checkversion() < 0)
252 			errx(1, "%s", devstat_errbuf);
253 
254 
255 		argv = getdrivedata(argv);
256 		winsize.ws_row = 0;
257 		ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
258 		if (winsize.ws_row > 0)
259 			winlines = winsize.ws_row;
260 
261 	}
262 
263 #define	BACKWARD_COMPATIBILITY
264 #ifdef	BACKWARD_COMPATIBILITY
265 	if (*argv) {
266 		interval = (u_int)(strtod(*argv, NULL) * 1000.0);
267 		if (*++argv)
268 			reps = atoi(*argv);
269 	}
270 #endif
271 
272 	if (interval) {
273 		if (!reps)
274 			reps = -1;
275 	} else if (reps) {
276 		interval = 1000;
277 	}
278 
279 #ifdef notyet
280 	if (todo & FORKSTAT)
281 		doforkst();
282 #endif
283 	if (todo & MEMSTAT)
284 		domem();
285 	if (todo & ZMEMSTAT)
286 		dozmem();
287 	if (todo & SUMSTAT)
288 		dosum();
289 #ifdef notyet
290 	if (todo & TIMESTAT)
291 		dotimes();
292 #endif
293 	if (todo & INTRSTAT)
294 		dointr();
295 	if (todo & VMSTAT)
296 		dovmstat(interval, reps);
297 	exit(0);
298 }
299 
300 static char **
301 getdrivedata(char **argv)
302 {
303 	if ((num_devices = getnumdevs()) < 0)
304 		errx(1, "%s", devstat_errbuf);
305 
306 	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
307 	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
308 	bzero(cur.dinfo, sizeof(struct devinfo));
309 	bzero(last.dinfo, sizeof(struct devinfo));
310 
311 	if (getdevs(&cur) == -1)
312 		errx(1, "%s", devstat_errbuf);
313 
314 	num_devices = cur.dinfo->numdevs;
315 	generation = cur.dinfo->generation;
316 
317 	specified_devices = (char **)malloc(sizeof(char *));
318 	for (num_devices_specified = 0; *argv; ++argv) {
319 		if (isdigit(**argv))
320 			break;
321 		num_devices_specified++;
322 		specified_devices = (char **)realloc(specified_devices,
323 						     sizeof(char *) *
324 						     num_devices_specified);
325 		specified_devices[num_devices_specified - 1] = *argv;
326 	}
327 	dev_select = NULL;
328 
329 	if (nflag == 0 && maxshowdevs < num_devices_specified)
330 			maxshowdevs = num_devices_specified;
331 
332 	/*
333 	 * People are generally only interested in disk statistics when
334 	 * they're running vmstat.  So, that's what we're going to give
335 	 * them if they don't specify anything by default.  We'll also give
336 	 * them any other random devices in the system so that we get to
337 	 * maxshowdevs devices, if that many devices exist.  If the user
338 	 * specifies devices on the command line, either through a pattern
339 	 * match or by naming them explicitly, we will give the user only
340 	 * those devices.
341 	 */
342 	if ((num_devices_specified == 0) && (num_matches == 0)) {
343 		if (buildmatch("da", &matches, &num_matches) != 0)
344 			errx(1, "%s", devstat_errbuf);
345 
346 		select_mode = DS_SELECT_ADD;
347 	} else
348 		select_mode = DS_SELECT_ONLY;
349 
350 	/*
351 	 * At this point, selectdevs will almost surely indicate that the
352 	 * device list has changed, so we don't look for return values of 0
353 	 * or 1.  If we get back -1, though, there is an error.
354 	 */
355 	if (selectdevs(&dev_select, &num_selected, &num_selections,
356 		       &select_generation, generation, cur.dinfo->devices,
357 		       num_devices, matches, num_matches, specified_devices,
358 		       num_devices_specified, select_mode,
359 		       maxshowdevs, 0) == -1)
360 		errx(1, "%s", devstat_errbuf);
361 
362 	return(argv);
363 }
364 
365 static long
366 getuptime(void)
367 {
368 	static time_t now, boottime;
369 	time_t uptime;
370 
371 	if (boottime == 0)
372 		kread(X_BOOTTIME, &boottime, sizeof(boottime));
373 	time(&now);
374 	uptime = now - boottime;
375 	if (uptime <= 0 || uptime > 60*60*24*365*10)
376 		errx(1, "time makes no sense; namelist must be wrong");
377 	return(uptime);
378 }
379 
380 int	hdrcnt;
381 
382 static void
383 dovmstat(u_int interval, int reps)
384 {
385 	struct vmtotal total;
386 	struct devinfo *tmp_dinfo;
387 	size_t vmm_size = sizeof(vmm);
388 	size_t vms_size = sizeof(vms);
389 	size_t vmt_size = sizeof(total);
390 	int initial = 1;
391 
392 	signal(SIGCONT, needhdr);
393 
394 	for (hdrcnt = 1;;) {
395 		if (!--hdrcnt)
396 			printhdr();
397 		if (kinfo_get_sched_cputime(&cp_time))
398 			err(1, "kinfo_get_sched_cputime");
399 
400 		tmp_dinfo = last.dinfo;
401 		last.dinfo = cur.dinfo;
402 		cur.dinfo = tmp_dinfo;
403 		last.busy_time = cur.busy_time;
404 
405 		/*
406 		 * Here what we want to do is refresh our device stats.
407 		 * getdevs() returns 1 when the device list has changed.
408 		 * If the device list has changed, we want to go through
409 		 * the selection process again, in case a device that we
410 		 * were previously displaying has gone away.
411 		 */
412 		switch (getdevs(&cur)) {
413 		case -1:
414 			errx(1, "%s", devstat_errbuf);
415 			break;
416 		case 1: {
417 			int retval;
418 
419 			num_devices = cur.dinfo->numdevs;
420 			generation = cur.dinfo->generation;
421 
422 			retval = selectdevs(&dev_select, &num_selected,
423 					    &num_selections, &select_generation,
424 					    generation, cur.dinfo->devices,
425 					    num_devices, matches, num_matches,
426 					    specified_devices,
427 					    num_devices_specified, select_mode,
428 					    maxshowdevs, 0);
429 			switch (retval) {
430 			case -1:
431 				errx(1, "%s", devstat_errbuf);
432 				break;
433 			case 1:
434 				printhdr();
435 				break;
436 			default:
437 				break;
438 			}
439 		}
440 		default:
441 			break;
442 		}
443 
444 		if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) {
445 			perror("sysctlbyname: vm.vmstats");
446 			exit(1);
447 		}
448 		if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
449 			perror("sysctlbyname: vm.vmmeter");
450 			exit(1);
451 		}
452 		if (sysctlbyname("vm.vmtotal", &total, &vmt_size, NULL, 0)) {
453 			perror("sysctlbyname: vm.vmtotal");
454 			exit(1);
455 		}
456 		printf("%2ld %1ld %1ld",
457 		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
458 
459 #define vmstat_pgtok(a)	\
460 	(intmax_t)(((intmax_t)(a) * vms.v_page_size) >> 10)
461 #define rate(x)		\
462 	(intmax_t)(initial ? (x) : ((intmax_t)(x) * 1000 + interval / 2) \
463 				   / interval)
464 
465 		printf(" %7jd %6jd ",
466 		       vmstat_pgtok(total.t_avm),
467 		       vmstat_pgtok(total.t_free));
468 		printf("%4ju ",
469 		       rate(vmm.v_vm_faults - ovmm.v_vm_faults));
470 		printf("%3ju ",
471 		       rate(vmm.v_reactivated - ovmm.v_reactivated));
472 		printf("%3ju ",
473 		       rate(vmm.v_swapin + vmm.v_vnodein -
474 			    (ovmm.v_swapin + ovmm.v_vnodein)));
475 		printf("%3ju ",
476 		       rate(vmm.v_swapout + vmm.v_vnodeout -
477 			    (ovmm.v_swapout + ovmm.v_vnodeout)));
478 		printf("%3ju ",
479 		       rate(vmm.v_tfree - ovmm.v_tfree));
480 		printf("%3ju ",
481 		       rate(vmm.v_pdpages - ovmm.v_pdpages));
482 		devstats();
483 		printf("%4ju %4ju %3ju ",
484 		       rate(vmm.v_intr - ovmm.v_intr),
485 		       rate(vmm.v_syscall - ovmm.v_syscall),
486 		       rate(vmm.v_swtch - ovmm.v_swtch));
487 		cpustats();
488 		printf("\n");
489 		fflush(stdout);
490 		if (reps >= 0 && --reps <= 0)
491 			break;
492 		ovmm = vmm;
493 		usleep(interval * 1000);
494 		initial = 0;
495 	}
496 }
497 
498 static void
499 printhdr(void)
500 {
501 	int i, num_shown;
502 
503 	num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
504 	printf(" procs      memory      page%*s", 19, "");
505 	if (num_shown > 1)
506 		printf(" disks %*s", num_shown * 4 - 7, "");
507 	else if (num_shown == 1)
508 		printf("disk");
509 	printf("   faults      cpu\n");
510 	printf(" r b w     avm    fre  flt  re  pi  po  fr  sr ");
511 	for (i = 0; i < num_devices; i++)
512 		if ((dev_select[i].selected)
513 		 && (dev_select[i].selected <= maxshowdevs))
514 			printf("%c%c%d ", dev_select[i].device_name[0],
515 				     dev_select[i].device_name[1],
516 				     dev_select[i].unit_number);
517 	printf("  in   sy  cs us sy id\n");
518 	hdrcnt = winlines - 2;
519 }
520 
521 /*
522  * Force a header to be prepended to the next output.
523  */
524 static void
525 needhdr(__unused int signo)
526 {
527 
528 	hdrcnt = 1;
529 }
530 
531 static long
532 pct(long top, long bot)
533 {
534 	long ans;
535 
536 	if (bot == 0)
537 		return(0);
538 	ans = (quad_t)top * 100 / bot;
539 	return (ans);
540 }
541 
542 #define	PCT(top, bot) pct((long)(top), (long)(bot))
543 
544 static void
545 dosum(void)
546 {
547 	struct nchstats *nch_tmp, nchstats;
548 	size_t vms_size = sizeof(vms);
549 	size_t vmm_size = sizeof(vmm);
550 	int cpucnt;
551 	u_long nchtotal;
552 	u_long nchpathtotal;
553 	size_t nch_size = sizeof(struct nchstats) * SMP_MAXCPU;
554 
555 	if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) {
556 		perror("sysctlbyname: vm.vmstats");
557 		exit(1);
558 	}
559 	if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
560 		perror("sysctlbyname: vm.vmstats");
561 		exit(1);
562 	}
563 	printf("%9u cpu context switches\n", vmm.v_swtch);
564 	printf("%9u device interrupts\n", vmm.v_intr);
565 	printf("%9u software interrupts\n", vmm.v_soft);
566 	printf("%9u traps\n", vmm.v_trap);
567 	printf("%9u system calls\n", vmm.v_syscall);
568 	printf("%9u kernel threads created\n", vmm.v_kthreads);
569 	printf("%9u  fork() calls\n", vmm.v_forks);
570 	printf("%9u vfork() calls\n", vmm.v_vforks);
571 	printf("%9u rfork() calls\n", vmm.v_rforks);
572 	printf("%9u exec() calls\n", vmm.v_exec);
573 	printf("%9u swap pager pageins\n", vmm.v_swapin);
574 	printf("%9u swap pager pages paged in\n", vmm.v_swappgsin);
575 	printf("%9u swap pager pageouts\n", vmm.v_swapout);
576 	printf("%9u swap pager pages paged out\n", vmm.v_swappgsout);
577 	printf("%9u vnode pager pageins\n", vmm.v_vnodein);
578 	printf("%9u vnode pager pages paged in\n", vmm.v_vnodepgsin);
579 	printf("%9u vnode pager pageouts\n", vmm.v_vnodeout);
580 	printf("%9u vnode pager pages paged out\n", vmm.v_vnodepgsout);
581 	printf("%9u page daemon wakeups\n", vmm.v_pdwakeups);
582 	printf("%9u pages examined by the page daemon\n", vmm.v_pdpages);
583 	printf("%9u pages reactivated\n", vmm.v_reactivated);
584 	printf("%9u copy-on-write faults\n", vmm.v_cow_faults);
585 	printf("%9u copy-on-write optimized faults\n", vmm.v_cow_optim);
586 	printf("%9u zero fill pages zeroed\n", vmm.v_zfod);
587 	printf("%9u zero fill pages prezeroed\n", vmm.v_ozfod);
588 	printf("%9u intransit blocking page faults\n", vmm.v_intrans);
589 	printf("%9u total VM faults taken\n", vmm.v_vm_faults);
590 	printf("%9u pages affected by kernel thread creation\n", vmm.v_kthreadpages);
591 	printf("%9u pages affected by  fork()\n", vmm.v_forkpages);
592 	printf("%9u pages affected by vfork()\n", vmm.v_vforkpages);
593 	printf("%9u pages affected by rfork()\n", vmm.v_rforkpages);
594 	printf("%9u pages freed\n", vmm.v_tfree);
595 	printf("%9u pages freed by daemon\n", vmm.v_dfree);
596 	printf("%9u pages freed by exiting processes\n", vmm.v_pfree);
597 	printf("%9u pages active\n", vms.v_active_count);
598 	printf("%9u pages inactive\n", vms.v_inactive_count);
599 	printf("%9u pages in VM cache\n", vms.v_cache_count);
600 	printf("%9u pages wired down\n", vms.v_wire_count);
601 	printf("%9u pages free\n", vms.v_free_count);
602 	printf("%9u bytes per page\n", vms.v_page_size);
603 	printf("%9u global smp invltlbs\n", vmm.v_smpinvltlb);
604 
605 	if ((nch_tmp = malloc(nch_size)) == NULL) {
606 		perror("malloc");
607 		exit(1);
608 	} else {
609 		if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) {
610 			perror("sysctlbyname vfs.cache.nchstats");
611 			free(nch_tmp);
612 			exit(1);
613 		} else {
614 			if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) {
615 				perror("realloc");
616 				exit(1);
617 			}
618 		}
619 	}
620 
621 	cpucnt = nch_size / sizeof(struct nchstats);
622 	kvm_nch_cpuagg(nch_tmp, &nchstats, cpucnt);
623 
624 	nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
625 	    nchstats.ncs_badhits + nchstats.ncs_falsehits +
626 	    nchstats.ncs_miss;
627 	nchpathtotal = nchstats.ncs_longhits + nchstats.ncs_longmiss;
628 	printf("%9ld total path lookups\n", nchpathtotal);
629 	printf("%9ld total component lookups\n", nchtotal);
630 	printf(
631 	    "%9s cache hits (%ld%% pos + %ld%% neg)\n",
632 	    "", PCT(nchstats.ncs_goodhits, nchtotal),
633 	    PCT(nchstats.ncs_neghits, nchtotal));
634 	printf("%9s deletions %ld%%, falsehits %ld%%\n", "",
635 	    PCT(nchstats.ncs_badhits, nchtotal),
636 	    PCT(nchstats.ncs_falsehits, nchtotal));
637 	free(nch_tmp);
638 }
639 
640 #ifdef notyet
641 void
642 doforkst(void)
643 {
644 	struct forkstat fks;
645 
646 	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
647 	printf("%d forks, %d pages, average %.2f\n",
648 	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
649 	printf("%d vforks, %d pages, average %.2f\n",
650 	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
651 }
652 #endif
653 
654 static void
655 devstats(void)
656 {
657 	int dn;
658 	long double transfers_per_second;
659 	long double busy_seconds;
660 
661 	diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
662 	diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
663 	diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
664 	diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
665 	diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
666 	old_cp_time = cp_time;
667 
668 	busy_seconds = compute_etime(cur.busy_time, last.busy_time);
669 
670 	for (dn = 0; dn < num_devices; dn++) {
671 		int di;
672 
673 		if ((dev_select[dn].selected == 0)
674 		 || (dev_select[dn].selected > maxshowdevs))
675 			continue;
676 
677 		di = dev_select[dn].position;
678 
679 		if (compute_stats(&cur.dinfo->devices[di],
680 				  &last.dinfo->devices[di], busy_seconds,
681 				  NULL, NULL, NULL,
682 				  NULL, &transfers_per_second, NULL,
683 				  NULL, NULL) != 0)
684 			errx(1, "%s", devstat_errbuf);
685 
686 		printf("%3.0Lf ", transfers_per_second);
687 	}
688 }
689 
690 static void
691 cpustats(void)
692 {
693 	uint64_t total;
694 	double totusage;
695 
696 	total = diff_cp_time.cp_user + diff_cp_time.cp_nice +
697 	    diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle;
698 
699 	if (total)
700 		totusage = 100.0 / total;
701 	else
702 		totusage = 0;
703 	printf("%2.0f ",
704 	       (diff_cp_time.cp_user + diff_cp_time.cp_nice) * totusage);
705 	printf("%2.0f ",
706 	       (diff_cp_time.cp_sys + diff_cp_time.cp_intr) * totusage);
707 	printf("%2.0f",
708 	       diff_cp_time.cp_idle * totusage);
709 }
710 
711 static void
712 dointr(void)
713 {
714 	u_long *intrcnt, uptime;
715 	u_int64_t inttotal;
716 	size_t nintr, inamlen, i, size;
717 	int nwidth;
718 	char *intrstr;
719 	char **intrname;
720 
721 	uptime = getuptime();
722 	if (sysctlbyname("hw.intrnames", NULL, &inamlen, NULL, 0) != 0)
723 		errx(1, "sysctlbyname");
724 	intrstr = malloc(inamlen);
725 	if (intrstr == NULL)
726 		err(1, "malloc");
727 	sysctlbyname("hw.intrnames", intrstr, &inamlen, NULL, 0);
728 	for (nintr = 0, i = 0; i < inamlen; ++i) {
729 		if (intrstr[i] == 0)
730 			nintr++;
731 	}
732 	intrname = malloc(nintr * sizeof(char *));
733 	for (i = 0; i < nintr; ++i) {
734 		intrname[i] = intrstr;
735 		intrstr += strlen(intrstr) + 1;
736 	}
737 
738 	size = nintr * sizeof(*intrcnt);
739 	intrcnt = calloc(nintr, sizeof(*intrcnt));
740 	if (intrcnt == NULL)
741 		err(1, "malloc");
742 	sysctlbyname("hw.intrcnt", intrcnt, &size, NULL, 0);
743 
744 	nwidth = 21;
745 	for (i = 0; i < nintr; ++i) {
746 		if (nwidth < (int)strlen(intrname[i]))
747 			nwidth = (int)strlen(intrname[i]);
748 	}
749 	if (verbose) nwidth += 12;
750 
751 	printf("%-*.*s %11s %10s\n",
752 		nwidth, nwidth, "interrupt", "total", "rate");
753 	inttotal = 0;
754 	for (i = 0; i < nintr; ++i) {
755 		int named;
756 		char *infop, irqinfo[72];
757 
758 		if ((named = strncmp(intrname[i], "irq", 3)) != 0 ||
759 		    intrcnt[i] > 0) {
760 			infop = intrname[i];
761 			if (verbose) {
762 				ssize_t irq, cpu;
763 
764 				irq = i % MAX_INTS;
765 				cpu = i / MAX_INTS;
766 				if (named) {
767 					snprintf(irqinfo, sizeof(irqinfo),
768 						 "irq%-3zd %3zd: %s",
769 						 irq, cpu, intrname[i]);
770 				} else {
771 					snprintf(irqinfo, sizeof(irqinfo),
772 						 "irq%-3zd %3zd: ", irq, cpu);
773 				}
774 				infop = irqinfo;
775 			}
776 			printf("%-*.*s %11lu %10lu\n",
777 				nwidth, nwidth, infop,
778 				intrcnt[i], intrcnt[i] / uptime);
779 		}
780 		inttotal += intrcnt[i];
781 	}
782 	printf("%-*.*s %11llu %10llu\n",
783 		nwidth, nwidth, "Total",
784 		(long long)inttotal, (long long)(inttotal / uptime));
785 }
786 
787 #define	MAX_KMSTATS	1024
788 
789 static long
790 cpuagg(size_t *ary)
791 {
792     int i;
793     long ttl;
794 
795     for (i = ttl = 0; i < SMP_MAXCPU; ++i)
796 	ttl += ary[i];
797     return(ttl);
798 }
799 
800 static void
801 domem(void)
802 {
803 	struct malloc_type *ks;
804 	int i, j;
805 	int first, nkms;
806 	long totuse = 0, totfree = 0, totreq = 0;
807 	struct malloc_type kmemstats[MAX_KMSTATS], *kmsp;
808 	char buf[1024];
809 
810 	kread(X_KMEMSTATISTICS, &kmsp, sizeof(kmsp));
811 	for (nkms = 0; nkms < MAX_KMSTATS && kmsp != NULL; nkms++) {
812 		if (sizeof(kmemstats[0]) != kvm_read(kd, (u_long)kmsp,
813 		    &kmemstats[nkms], sizeof(kmemstats[0])))
814 			err(1, "kvm_read(%p)", (void *)kmsp);
815 		if (sizeof(buf) !=  kvm_read(kd,
816 	            (u_long)kmemstats[nkms].ks_shortdesc, buf, sizeof(buf)))
817 			err(1, "kvm_read(%p)",
818 			    kmemstats[nkms].ks_shortdesc);
819 		buf[sizeof(buf) - 1] = '\0';
820 		kmemstats[nkms].ks_shortdesc = strdup(buf);
821 		kmsp = kmemstats[nkms].ks_next;
822 	}
823 	if (kmsp != NULL)
824 		warnx("truncated to the first %d memory types", nkms);
825 
826 	printf(
827 	    "\nMemory statistics by type                          Type  Kern\n");
828 	printf(
829 "              Type   InUse  MemUse HighUse       Limit  Requests  Limit Limit\n");
830 	for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) {
831 		if (ks->ks_calls == 0)
832 			continue;
833 		printf("%19s%7ld%7ldK%7ldK%11zuK%10jd%5u%6u",
834 		    ks->ks_shortdesc,
835 		    cpuagg(ks->ks_inuse), (cpuagg(ks->ks_memuse) + 1023) / 1024,
836 		    (ks->ks_maxused + 1023) / 1024,
837 		    (ks->ks_limit + 1023) / 1024, (intmax_t)ks->ks_calls,
838 		    ks->ks_limblocks, ks->ks_mapblocks);
839 		first = 1;
840 		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
841 			if ((ks->ks_size & j) == 0)
842 				continue;
843 			if (first)
844 				printf("  ");
845 			else
846 				printf(",");
847 			if(j<1024)
848 				printf("%d",j);
849 			else
850 				printf("%dK",j>>10);
851 			first = 0;
852 		}
853 		printf("\n");
854 		totuse += cpuagg(ks->ks_memuse);
855 		totreq += ks->ks_calls;
856 	}
857 	printf("\nMemory Totals:  In Use    Free    Requests\n");
858 	printf("              %7ldK %6ldK    %8ld\n",
859 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
860 }
861 
862 static void
863 dozmem(void)
864 {
865 	char *buf;
866 	size_t bufsize;
867 
868 	buf = NULL;
869 	bufsize = 1024;
870 	for (;;) {
871 		if ((buf = realloc(buf, bufsize)) == NULL)
872 			err(1, "realloc()");
873 		if (sysctlbyname("vm.zone", buf, &bufsize, NULL, 0) == 0)
874 			break;
875 		if (errno != ENOMEM)
876 			err(1, "sysctl()");
877 		bufsize *= 2;
878 	}
879 	buf[bufsize] = '\0'; /* play it safe */
880 	printf("%s\n\n", buf);
881 	free(buf);
882 }
883 
884 /*
885  * kread reads something from the kernel, given its nlist index.
886  */
887 static void
888 kread(int nlx, void *addr, size_t size)
889 {
890 	const char *sym;
891 
892 	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
893 		sym = namelist[nlx].n_name;
894 		if (*sym == '_')
895 			++sym;
896 		errx(1, "symbol %s not defined", sym);
897 	}
898 	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != (ssize_t)size) {
899 		sym = namelist[nlx].n_name;
900 		if (*sym == '_')
901 			++sym;
902 		errx(1, "%s: %s", sym, kvm_geterr(kd));
903 	}
904 }
905 
906 static void
907 usage(void)
908 {
909 	fprintf(stderr, "%s%s",
910 		"usage: vmstat [-imsvz] [-c count] [-M core] [-N system] [-w wait]\n",
911 		"              [-n devs] [disks]\n");
912 	exit(1);
913 }
914