xref: /freebsd/usr.bin/vmstat/vmstat.c (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
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 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1986, 1991, 1993\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif
43 
44 #ifndef lint
45 static const char sccsid[] = "@(#)vmstat.c	8.1 (Berkeley) 6/6/93";
46 #endif
47 
48 #include <sys/param.h>
49 #include <sys/time.h>
50 #include <sys/proc.h>
51 #include <sys/dkstat.h>
52 #include <sys/uio.h>
53 #include <sys/namei.h>
54 #include <sys/malloc.h>
55 #include <sys/signal.h>
56 #include <sys/fcntl.h>
57 #include <sys/ioctl.h>
58 #include <sys/sysctl.h>
59 #include <sys/vmmeter.h>
60 
61 #include <vm/vm_param.h>
62 
63 #include <ctype.h>
64 #include <devstat.h>
65 #include <err.h>
66 #include <errno.h>
67 #include <kvm.h>
68 #include <limits.h>
69 #include <nlist.h>
70 #include <paths.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <sysexits.h>
75 #include <time.h>
76 #include <unistd.h>
77 
78 static char da[] = "da";
79 
80 static struct nlist namelist[] = {
81 #define	X_CPTIME	0
82 	{ "_cp_time" },
83 #define X_SUM		1
84 	{ "_cnt" },
85 #define	X_BOOTTIME	2
86 	{ "_boottime" },
87 #define X_HZ		3
88 	{ "_hz" },
89 #define X_STATHZ	4
90 	{ "_stathz" },
91 #define X_NCHSTATS	5
92 	{ "_nchstats" },
93 #define	X_INTRNAMES	6
94 	{ "_intrnames" },
95 #define	X_EINTRNAMES	7
96 	{ "_eintrnames" },
97 #define	X_INTRCNT	8
98 	{ "_intrcnt" },
99 #define	X_EINTRCNT	9
100 	{ "_eintrcnt" },
101 #ifdef notyet
102 #define	X_DEFICIT	10
103 	{ "_deficit" },
104 #define	X_FORKSTAT	11
105 	{ "_forkstat" },
106 #define X_REC		12
107 	{ "_rectime" },
108 #define X_PGIN		13
109 	{ "_pgintime" },
110 #define	X_XSTATS	14
111 	{ "_xstats" },
112 #define X_END		15
113 #else
114 #define X_END		10
115 #endif
116 	{ "" },
117 };
118 
119 struct statinfo cur, last;
120 int num_devices, maxshowdevs;
121 long generation;
122 struct device_selection *dev_select;
123 int num_selected;
124 struct devstat_match *matches;
125 int num_matches = 0;
126 int num_devices_specified, num_selections;
127 long select_generation;
128 char **specified_devices;
129 devstat_select_mode select_mode;
130 
131 struct	vmmeter sum, osum;
132 
133 int	winlines = 20;
134 int	nflag = 0;
135 
136 kvm_t *kd;
137 
138 #define	FORKSTAT	0x01
139 #define	INTRSTAT	0x02
140 #define	MEMSTAT		0x04
141 #define	SUMSTAT		0x08
142 #define	TIMESTAT	0x10
143 #define	VMSTAT		0x20
144 #define ZMEMSTAT	0x40
145 
146 static void	cpustats(void);
147 static void	devstats(void);
148 static void	dosysctl(char *);
149 static void	domem(void);
150 static void	dointr(void);
151 static void	dosum(void);
152 static void	dovmstat(u_int, int);
153 static void	dozmem(void);
154 static void	kread(int, void *, size_t);
155 static void	needhdr(int);
156 static void	printhdr(void);
157 static void	usage(void);
158 
159 static long	pct(long, long);
160 static long	getuptime(void);
161 
162 char **getdrivedata(char **);
163 
164 int
165 main(argc, argv)
166 	int argc;
167 	char **argv;
168 {
169 	int c, todo;
170 	u_int interval;
171 	int reps;
172 	char *memf, *nlistf;
173 	char errbuf[_POSIX2_LINE_MAX];
174 
175 	memf = nlistf = NULL;
176 	interval = reps = todo = 0;
177 	maxshowdevs = 2;
178 	while ((c = getopt(argc, argv, "c:fiM:mN:n:p:stw:z")) != -1) {
179 		switch (c) {
180 		case 'c':
181 			reps = atoi(optarg);
182 			break;
183 		case 'f':
184 			errx(EX_USAGE, "sorry, -f is not (re)implemented yet");
185 			break;
186 		case 'i':
187 			todo |= INTRSTAT;
188 			break;
189 		case 'M':
190 			memf = optarg;
191 			break;
192 		case 'm':
193 			todo |= MEMSTAT;
194 			break;
195 		case 'N':
196 			nlistf = optarg;
197 			break;
198 		case 'n':
199 			nflag = 1;
200 			maxshowdevs = atoi(optarg);
201 			if (maxshowdevs < 0)
202 				errx(1, "number of devices %d is < 0",
203 				     maxshowdevs);
204 			break;
205 		case 'p':
206 			if (buildmatch(optarg, &matches, &num_matches) != 0)
207 				errx(1, "%s", devstat_errbuf);
208 			break;
209 		case 's':
210 			todo |= SUMSTAT;
211 			break;
212 		case 't':
213 #ifdef notyet
214 			todo |= TIMESTAT;
215 #else
216 			errx(EX_USAGE, "sorry, -t is not (re)implemented yet");
217 #endif
218 			break;
219 		case 'w':
220 			interval = atoi(optarg);
221 			break;
222 		case 'z':
223 			todo |= ZMEMSTAT;
224 			break;
225 		case '?':
226 		default:
227 			usage();
228 		}
229 	}
230 	argc -= optind;
231 	argv += optind;
232 
233 	if (todo == 0)
234 		todo = VMSTAT;
235 
236 	/*
237 	 * Discard setgid privileges if not the running kernel so that bad
238 	 * guys can't print interesting stuff from kernel memory.
239 	 */
240 	if (nlistf != NULL || memf != NULL)
241 		setgid(getgid());
242 
243 	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
244 	if (kd == 0)
245 		errx(1, "kvm_openfiles: %s", errbuf);
246 	setgid(getgid());
247 
248 	if ((c = kvm_nlist(kd, namelist)) != 0) {
249 		if (c > 0) {
250 			warnx("undefined symbols:");
251 			for (c = 0;
252 			    c < (int)(sizeof(namelist)/sizeof(namelist[0]));
253 			    c++)
254 				if (namelist[c].n_type == 0)
255 					(void)fprintf(stderr, " %s",
256 					    namelist[c].n_name);
257 			(void)fputc('\n', stderr);
258 		} else
259 			warnx("kvm_nlist: %s", kvm_geterr(kd));
260 		exit(1);
261 	}
262 
263 	if (todo & VMSTAT) {
264 		struct winsize winsize;
265 
266 		/*
267 		 * Make sure that the userland devstat version matches the
268 		 * kernel devstat version.  If not, exit and print a
269 		 * message informing the user of his mistake.
270 		 */
271 		if (checkversion() < 0)
272 			errx(1, "%s", devstat_errbuf);
273 
274 
275 		argv = getdrivedata(argv);
276 		winsize.ws_row = 0;
277 		(void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize);
278 		if (winsize.ws_row > 0)
279 			winlines = winsize.ws_row;
280 
281 	}
282 
283 #define	BACKWARD_COMPATIBILITY
284 #ifdef	BACKWARD_COMPATIBILITY
285 	if (*argv) {
286 		interval = atoi(*argv);
287 		if (*++argv)
288 			reps = atoi(*argv);
289 	}
290 #endif
291 
292 	if (interval) {
293 		if (!reps)
294 			reps = -1;
295 	} else if (reps)
296 		interval = 1;
297 
298 #ifdef notyet
299 	if (todo & FORKSTAT)
300 		doforkst();
301 #endif
302 	if (todo & MEMSTAT)
303 		domem();
304 	if (todo & ZMEMSTAT)
305 		dozmem();
306 	if (todo & SUMSTAT)
307 		dosum();
308 #ifdef notyet
309 	if (todo & TIMESTAT)
310 		dotimes();
311 #endif
312 	if (todo & INTRSTAT)
313 		dointr();
314 	if (todo & VMSTAT)
315 		dovmstat(interval, reps);
316 	exit(0);
317 }
318 
319 char **
320 getdrivedata(argv)
321 	char **argv;
322 {
323 	if ((num_devices = getnumdevs()) < 0)
324 		errx(1, "%s", devstat_errbuf);
325 
326 	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
327 	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
328 	bzero(cur.dinfo, sizeof(struct devinfo));
329 	bzero(last.dinfo, sizeof(struct devinfo));
330 
331 	if (getdevs(&cur) == -1)
332 		errx(1, "%s", devstat_errbuf);
333 
334 	num_devices = cur.dinfo->numdevs;
335 	generation = cur.dinfo->generation;
336 
337 	specified_devices = (char **)malloc(sizeof(char *));
338 	for (num_devices_specified = 0; *argv; ++argv) {
339 		if (isdigit(**argv))
340 			break;
341 		num_devices_specified++;
342 		specified_devices = (char **)realloc(specified_devices,
343 						     sizeof(char *) *
344 						     num_devices_specified);
345 		specified_devices[num_devices_specified - 1] = *argv;
346 	}
347 	dev_select = NULL;
348 
349 	if (nflag == 0 && maxshowdevs < num_devices_specified)
350 			maxshowdevs = num_devices_specified;
351 
352 	/*
353 	 * People are generally only interested in disk statistics when
354 	 * they're running vmstat.  So, that's what we're going to give
355 	 * them if they don't specify anything by default.  We'll also give
356 	 * them any other random devices in the system so that we get to
357 	 * maxshowdevs devices, if that many devices exist.  If the user
358 	 * specifies devices on the command line, either through a pattern
359 	 * match or by naming them explicitly, we will give the user only
360 	 * those devices.
361 	 */
362 	if ((num_devices_specified == 0) && (num_matches == 0)) {
363 		if (buildmatch(da, &matches, &num_matches) != 0)
364 			errx(1, "%s", devstat_errbuf);
365 
366 		select_mode = DS_SELECT_ADD;
367 	} else
368 		select_mode = DS_SELECT_ONLY;
369 
370 	/*
371 	 * At this point, selectdevs will almost surely indicate that the
372 	 * device list has changed, so we don't look for return values of 0
373 	 * or 1.  If we get back -1, though, there is an error.
374 	 */
375 	if (selectdevs(&dev_select, &num_selected, &num_selections,
376 		       &select_generation, generation, cur.dinfo->devices,
377 		       num_devices, matches, num_matches, specified_devices,
378 		       num_devices_specified, select_mode,
379 		       maxshowdevs, 0) == -1)
380 		errx(1, "%s", devstat_errbuf);
381 
382 	return(argv);
383 }
384 
385 long
386 getuptime()
387 {
388 	static time_t now, boottime;
389 	time_t uptime;
390 
391 	if (boottime == 0)
392 		kread(X_BOOTTIME, &boottime, sizeof(boottime));
393 	(void)time(&now);
394 	uptime = now - boottime;
395 	if (uptime <= 0 || uptime > 60*60*24*365*10)
396 		errx(1, "time makes no sense; namelist must be wrong");
397 	return(uptime);
398 }
399 
400 int	hz, hdrcnt;
401 
402 void
403 dovmstat(interval, reps)
404 	u_int interval;
405 	int reps;
406 {
407 	struct vmtotal total;
408 	time_t uptime, halfuptime;
409 	struct devinfo *tmp_dinfo;
410 	int mib[2];
411 	size_t size;
412 
413 	uptime = getuptime();
414 	halfuptime = uptime / 2;
415 	(void)signal(SIGCONT, needhdr);
416 
417 	if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
418 		kread(X_STATHZ, &hz, sizeof(hz));
419 	if (!hz)
420 		kread(X_HZ, &hz, sizeof(hz));
421 
422 	for (hdrcnt = 1;;) {
423 		if (!--hdrcnt)
424 			printhdr();
425 		kread(X_CPTIME, cur.cp_time, sizeof(cur.cp_time));
426 
427 		tmp_dinfo = last.dinfo;
428 		last.dinfo = cur.dinfo;
429 		cur.dinfo = tmp_dinfo;
430 		last.busy_time = cur.busy_time;
431 
432 		/*
433 		 * Here what we want to do is refresh our device stats.
434 		 * getdevs() returns 1 when the device list has changed.
435 		 * If the device list has changed, we want to go through
436 		 * the selection process again, in case a device that we
437 		 * were previously displaying has gone away.
438 		 */
439 		switch (getdevs(&cur)) {
440 		case -1:
441 			errx(1, "%s", devstat_errbuf);
442 			break;
443 		case 1: {
444 			int retval;
445 
446 			num_devices = cur.dinfo->numdevs;
447 			generation = cur.dinfo->generation;
448 
449 			retval = selectdevs(&dev_select, &num_selected,
450 					    &num_selections, &select_generation,
451 					    generation, cur.dinfo->devices,
452 					    num_devices, matches, num_matches,
453 					    specified_devices,
454 					    num_devices_specified, select_mode,
455 					    maxshowdevs, 0);
456 			switch (retval) {
457 			case -1:
458 				errx(1, "%s", devstat_errbuf);
459 				break;
460 			case 1:
461 				printhdr();
462 				break;
463 			default:
464 				break;
465 			}
466 		}
467 		default:
468 			break;
469 		}
470 
471 		kread(X_SUM, &sum, sizeof(sum));
472 		size = sizeof(total);
473 		mib[0] = CTL_VM;
474 		mib[1] = VM_METER;
475 		if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) {
476 			(void)printf("Can't get kerninfo: %s\n",
477 				     strerror(errno));
478 			bzero(&total, sizeof(total));
479 		}
480 		(void)printf("%2d %1d %1d",
481 		    total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw);
482 #define vmstat_pgtok(a) ((a) * sum.v_page_size >> 10)
483 #define	rate(x)	(((x) + halfuptime) / uptime)	/* round */
484 		(void)printf(" %7ld %6ld ", (long)vmstat_pgtok(total.t_avm),
485 			     (long)vmstat_pgtok(total.t_free));
486 		(void)printf("%4lu ",
487 		    (u_long)rate(sum.v_vm_faults - osum.v_vm_faults));
488 		(void)printf("%3lu ",
489 		    (u_long)rate(sum.v_reactivated - osum.v_reactivated));
490 		(void)printf("%3lu ",
491 		    (u_long)rate(sum.v_swapin + sum.v_vnodein -
492 		    (osum.v_swapin + osum.v_vnodein)));
493 		(void)printf("%3lu ",
494 		    (u_long)rate(sum.v_swapout + sum.v_vnodeout -
495 		    (osum.v_swapout + osum.v_vnodeout)));
496 		(void)printf("%3lu ",
497 		    (u_long)rate(sum.v_tfree - osum.v_tfree));
498 		(void)printf("%3lu ",
499 		    (u_long)rate(sum.v_pdpages - osum.v_pdpages));
500 		devstats();
501 		(void)printf("%4lu %4lu %3lu ",
502 		    (u_long)rate(sum.v_intr - osum.v_intr),
503 		    (u_long)rate(sum.v_syscall - osum.v_syscall),
504 		    (u_long)rate(sum.v_swtch - osum.v_swtch));
505 		cpustats();
506 		(void)printf("\n");
507 		(void)fflush(stdout);
508 		if (reps >= 0 && --reps <= 0)
509 			break;
510 		osum = sum;
511 		uptime = interval;
512 		/*
513 		 * We round upward to avoid losing low-frequency events
514 		 * (i.e., >= 1 per interval but < 1 per second).
515 		 */
516 		if (interval != 1)
517 			halfuptime = (uptime + 1) / 2;
518 		else
519 			halfuptime = 0;
520 		(void)sleep(interval);
521 	}
522 }
523 
524 void
525 printhdr()
526 {
527 	int i, num_shown;
528 
529 	num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs;
530 	(void)printf(" procs      memory      page%*s", 19, "");
531 	if (num_shown > 1)
532 		(void)printf(" disks %*s", num_shown * 4 - 7, "");
533 	else if (num_shown == 1)
534 		(void)printf("disk");
535 	(void)printf("   faults      cpu\n");
536 	(void)printf(" r b w     avm    fre  flt  re  pi  po  fr  sr ");
537 	for (i = 0; i < num_devices; i++)
538 		if ((dev_select[i].selected)
539 		 && (dev_select[i].selected <= maxshowdevs))
540 			(void)printf("%c%c%d ", dev_select[i].device_name[0],
541 				     dev_select[i].device_name[1],
542 				     dev_select[i].unit_number);
543 	(void)printf("  in   sy  cs us sy id\n");
544 	hdrcnt = winlines - 2;
545 }
546 
547 /*
548  * Force a header to be prepended to the next output.
549  */
550 void
551 needhdr(dummy)
552 	int dummy __unused;
553 {
554 
555 	hdrcnt = 1;
556 }
557 
558 #ifdef notyet
559 void
560 dotimes()
561 {
562 	u_int pgintime, rectime;
563 
564 	kread(X_REC, &rectime, sizeof(rectime));
565 	kread(X_PGIN, &pgintime, sizeof(pgintime));
566 	kread(X_SUM, &sum, sizeof(sum));
567 	(void)printf("%u reclaims, %u total time (usec)\n",
568 	    sum.v_pgrec, rectime);
569 	(void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec);
570 	(void)printf("\n");
571 	(void)printf("%u page ins, %u total time (msec)\n",
572 	    sum.v_pgin, pgintime / 10);
573 	(void)printf("average: %8.1f msec / page in\n",
574 	    pgintime / (sum.v_pgin * 10.0));
575 }
576 #endif
577 
578 long
579 pct(top, bot)
580 	long top, bot;
581 {
582 	long ans;
583 
584 	if (bot == 0)
585 		return(0);
586 	ans = (quad_t)top * 100 / bot;
587 	return (ans);
588 }
589 
590 #define	PCT(top, bot) pct((long)(top), (long)(bot))
591 
592 void
593 dosum()
594 {
595 	struct nchstats lnchstats;
596 	long nchtotal;
597 
598 	kread(X_SUM, &sum, sizeof(sum));
599 	(void)printf("%9u cpu context switches\n", sum.v_swtch);
600 	(void)printf("%9u device interrupts\n", sum.v_intr);
601 	(void)printf("%9u software interrupts\n", sum.v_soft);
602 	(void)printf("%9u traps\n", sum.v_trap);
603 	(void)printf("%9u system calls\n", sum.v_syscall);
604 	(void)printf("%9u kernel threads created\n", sum.v_kthreads);
605 	(void)printf("%9u  fork() calls\n", sum.v_forks);
606 	(void)printf("%9u vfork() calls\n", sum.v_vforks);
607 	(void)printf("%9u rfork() calls\n", sum.v_rforks);
608 	(void)printf("%9u swap pager pageins\n", sum.v_swapin);
609 	(void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin);
610 	(void)printf("%9u swap pager pageouts\n", sum.v_swapout);
611 	(void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout);
612 	(void)printf("%9u vnode pager pageins\n", sum.v_vnodein);
613 	(void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin);
614 	(void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout);
615 	(void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout);
616 	(void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups);
617 	(void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages);
618 	(void)printf("%9u pages reactivated\n", sum.v_reactivated);
619 	(void)printf("%9u copy-on-write faults\n", sum.v_cow_faults);
620 	(void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim);
621 	(void)printf("%9u zero fill pages zeroed\n", sum.v_zfod);
622 	(void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod);
623 	(void)printf("%9u intransit blocking page faults\n", sum.v_intrans);
624 	(void)printf("%9u total VM faults taken\n", sum.v_vm_faults);
625 	(void)printf("%9u pages affected by kernel thread creation\n", sum.v_kthreadpages);
626 	(void)printf("%9u pages affected by  fork()\n", sum.v_forkpages);
627 	(void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages);
628 	(void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages);
629 	(void)printf("%9u pages freed\n", sum.v_tfree);
630 	(void)printf("%9u pages freed by daemon\n", sum.v_dfree);
631 	(void)printf("%9u pages freed by exiting processes\n", sum.v_pfree);
632 	(void)printf("%9u pages active\n", sum.v_active_count);
633 	(void)printf("%9u pages inactive\n", sum.v_inactive_count);
634 	(void)printf("%9u pages in VM cache\n", sum.v_cache_count);
635 	(void)printf("%9u pages wired down\n", sum.v_wire_count);
636 	(void)printf("%9u pages free\n", sum.v_free_count);
637 	(void)printf("%9u bytes per page\n", sum.v_page_size);
638 	kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats));
639 	nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits +
640 	    lnchstats.ncs_badhits + lnchstats.ncs_falsehits +
641 	    lnchstats.ncs_miss + lnchstats.ncs_long;
642 	(void)printf("%9ld total name lookups\n", nchtotal);
643 	(void)printf(
644 	    "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n",
645 	    "", PCT(lnchstats.ncs_goodhits, nchtotal),
646 	    PCT(lnchstats.ncs_neghits, nchtotal),
647 	    PCT(lnchstats.ncs_pass2, nchtotal));
648 	(void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "",
649 	    PCT(lnchstats.ncs_badhits, nchtotal),
650 	    PCT(lnchstats.ncs_falsehits, nchtotal),
651 	    PCT(lnchstats.ncs_long, nchtotal));
652 }
653 
654 #ifdef notyet
655 void
656 doforkst()
657 {
658 	struct forkstat fks;
659 
660 	kread(X_FORKSTAT, &fks, sizeof(struct forkstat));
661 	(void)printf("%d forks, %d pages, average %.2f\n",
662 	    fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork);
663 	(void)printf("%d vforks, %d pages, average %.2f\n",
664 	    fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork);
665 }
666 #endif
667 
668 static void
669 devstats()
670 {
671 	int dn, state;
672 	long double transfers_per_second;
673 	long double busy_seconds;
674 	long tmp;
675 
676 	for (state = 0; state < CPUSTATES; ++state) {
677 		tmp = cur.cp_time[state];
678 		cur.cp_time[state] -= last.cp_time[state];
679 		last.cp_time[state] = tmp;
680 	}
681 
682 	busy_seconds = compute_etime(cur.busy_time, last.busy_time);
683 
684 	for (dn = 0; dn < num_devices; dn++) {
685 		int di;
686 
687 		if ((dev_select[dn].selected == 0)
688 		 || (dev_select[dn].selected > maxshowdevs))
689 			continue;
690 
691 		di = dev_select[dn].position;
692 
693 		if (devstat_compute_statistics(&cur.dinfo->devices[di],
694 		    &last.dinfo->devices[di], busy_seconds,
695 		    DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
696 		    DSM_NONE) != 0)
697 			errx(1, "%s", devstat_errbuf);
698 
699 		(void)printf("%3.0Lf ", transfers_per_second);
700 	}
701 }
702 
703 void
704 cpustats()
705 {
706 	int state;
707 	double lpct, total;
708 
709 	total = 0;
710 	for (state = 0; state < CPUSTATES; ++state)
711 		total += cur.cp_time[state];
712 	if (total)
713 		lpct = 100.0 / total;
714 	else
715 		lpct = 0.0;
716 	(void)printf("%2.0f ", (cur.cp_time[CP_USER] +
717 				cur.cp_time[CP_NICE]) * lpct);
718 	(void)printf("%2.0f ", (cur.cp_time[CP_SYS] +
719 				cur.cp_time[CP_INTR]) * lpct);
720 	(void)printf("%2.0f", cur.cp_time[CP_IDLE] * lpct);
721 }
722 
723 void
724 dointr()
725 {
726 	u_long *intrcnt, uptime;
727 	u_int64_t inttotal;
728 	int nintr, inamlen;
729 	char *intrname;
730 
731 	uptime = getuptime();
732 	nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value;
733 	inamlen =
734 	    namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value;
735 	intrcnt = malloc((size_t)nintr);
736 	intrname = malloc((size_t)inamlen);
737 	if (intrcnt == NULL || intrname == NULL)
738 		errx(1, "malloc");
739 	kread(X_INTRCNT, intrcnt, (size_t)nintr);
740 	kread(X_INTRNAMES, intrname, (size_t)inamlen);
741 	(void)printf("interrupt                   total       rate\n");
742 	inttotal = 0;
743 	nintr /= sizeof(long);
744 	while (--nintr >= 0) {
745 		if (*intrcnt)
746 			(void)printf("%-12s %20lu %10lu\n", intrname,
747 			    *intrcnt, *intrcnt / uptime);
748 		intrname += strlen(intrname) + 1;
749 		inttotal += *intrcnt++;
750 	}
751 	(void)printf("Total        %20llu %10llu\n", inttotal,
752 			inttotal / (u_int64_t) uptime);
753 }
754 
755 void
756 domem(void)
757 {
758 	dosysctl("kern.malloc");
759 }
760 
761 void
762 dozmem(void)
763 {
764 	dosysctl("vm.zone");
765 }
766 
767 void
768 dosysctl(char *name)
769 {
770 	char *buf;
771 	size_t bufsize;
772 
773 	buf = NULL;
774 	bufsize = 1024;
775 	for (;;) {
776 		if ((buf = realloc(buf, bufsize)) == NULL)
777 			err(1, "realloc()");
778 		if (sysctlbyname(name, buf, &bufsize, 0, NULL) == 0)
779 			break;
780 		if (errno != ENOMEM)
781 			err(1, "sysctl()");
782 		bufsize *= 2;
783 	}
784 	buf[bufsize] = '\0'; /* play it safe */
785 	(void)printf("%s\n\n", buf);
786 	free(buf);
787 }
788 
789 /*
790  * kread reads something from the kernel, given its nlist index.
791  */
792 void
793 kread(nlx, addr, size)
794 	int nlx;
795 	void *addr;
796 	size_t size;
797 {
798 	char *sym;
799 
800 	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
801 		sym = namelist[nlx].n_name;
802 		if (*sym == '_')
803 			++sym;
804 		errx(1, "symbol %s not defined", sym);
805 	}
806 	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != (int)size) {
807 		sym = namelist[nlx].n_name;
808 		if (*sym == '_')
809 			++sym;
810 		errx(1, "%s: %s", sym, kvm_geterr(kd));
811 	}
812 }
813 
814 void
815 usage()
816 {
817 	(void)fprintf(stderr, "%s%s",
818 		"usage: vmstat [-imsz] [-c count] [-M core] [-N system] [-w wait]\n",
819 		"              [-n devs] [disks]\n");
820 	exit(1);
821 }
822