xref: /freebsd/usr.bin/vmstat/vmstat.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/uio.h>
35 #include <sys/namei.h>
36 #include <sys/malloc.h>
37 #include <sys/signal.h>
38 #include <sys/fcntl.h>
39 #include <sys/ioctl.h>
40 #include <sys/resource.h>
41 #include <sys/sysctl.h>
42 #include <sys/time.h>
43 #include <sys/user.h>
44 #define	_WANT_VMMETER
45 #include <sys/vmmeter.h>
46 #include <sys/pcpu.h>
47 
48 #include <vm/vm_param.h>
49 
50 #include <ctype.h>
51 #include <devstat.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <inttypes.h>
55 #include <kvm.h>
56 #include <limits.h>
57 #include <memstat.h>
58 #include <nlist.h>
59 #include <paths.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sysexits.h>
64 #include <time.h>
65 #include <unistd.h>
66 #include <libutil.h>
67 #include <libxo/xo.h>
68 
69 #define VMSTAT_XO_VERSION "1"
70 
71 static char da[] = "da";
72 
73 enum x_stats { X_SUM, X_HZ, X_STATHZ, X_NCHSTATS, X_INTRNAMES, X_SINTRNAMES,
74     X_INTRCNT, X_SINTRCNT, X_NINTRCNT };
75 
76 static struct nlist namelist[] = {
77 	[X_SUM] = { .n_name = "_vm_cnt", },
78 	[X_HZ] = { .n_name = "_hz", },
79 	[X_STATHZ] = { .n_name = "_stathz", },
80 	[X_NCHSTATS] = { .n_name = "_nchstats", },
81 	[X_INTRNAMES] = { .n_name = "_intrnames", },
82 	[X_SINTRNAMES] = { .n_name = "_sintrnames", },
83 	[X_INTRCNT] = { .n_name = "_intrcnt", },
84 	[X_SINTRCNT] = { .n_name = "_sintrcnt", },
85 	[X_NINTRCNT] = { .n_name = "_nintrcnt", },
86 	{ .n_name = NULL, },
87 };
88 
89 static struct devstat_match *matches;
90 static struct device_selection *dev_select;
91 static struct statinfo cur, last;
92 static devstat_select_mode select_mode;
93 static size_t size_cp_times;
94 static long *cur_cp_times, *last_cp_times;
95 static long generation, select_generation;
96 static int hz, hdrcnt, maxshowdevs;
97 static int num_devices, num_devices_specified;
98 static int num_matches, num_selected, num_selections;
99 static char **specified_devices;
100 
101 static struct __vmmeter {
102 	uint64_t v_swtch;
103 	uint64_t v_trap;
104 	uint64_t v_syscall;
105 	uint64_t v_intr;
106 	uint64_t v_soft;
107 	uint64_t v_vm_faults;
108 	uint64_t v_io_faults;
109 	uint64_t v_cow_faults;
110 	uint64_t v_cow_optim;
111 	uint64_t v_zfod;
112 	uint64_t v_ozfod;
113 	uint64_t v_swapin;
114 	uint64_t v_swapout;
115 	uint64_t v_swappgsin;
116 	uint64_t v_swappgsout;
117 	uint64_t v_vnodein;
118 	uint64_t v_vnodeout;
119 	uint64_t v_vnodepgsin;
120 	uint64_t v_vnodepgsout;
121 	uint64_t v_intrans;
122 	uint64_t v_reactivated;
123 	uint64_t v_pdwakeups;
124 	uint64_t v_pdpages;
125 	uint64_t v_pdshortfalls;
126 	uint64_t v_dfree;
127 	uint64_t v_pfree;
128 	uint64_t v_tfree;
129 	uint64_t v_forks;
130 	uint64_t v_vforks;
131 	uint64_t v_rforks;
132 	uint64_t v_kthreads;
133 	uint64_t v_forkpages;
134 	uint64_t v_vforkpages;
135 	uint64_t v_rforkpages;
136 	uint64_t v_kthreadpages;
137 	u_int v_page_size;
138 	u_int v_page_count;
139 	u_int v_free_reserved;
140 	u_int v_free_target;
141 	u_int v_free_min;
142 	u_int v_free_count;
143 	u_int v_wire_count;
144 	u_long v_user_wire_count;
145 	u_int v_active_count;
146 	u_int v_inactive_target;
147 	u_int v_inactive_count;
148 	u_int v_laundry_count;
149 	u_int v_pageout_free_min;
150 	u_int v_interrupt_free_min;
151 	u_int v_free_severe;
152 } sum, osum;
153 
154 #define	VMSTAT_DEFAULT_LINES	20	/* Default number of `winlines'. */
155 static volatile sig_atomic_t wresized;		/* Tty resized when non-zero. */
156 static int winlines = VMSTAT_DEFAULT_LINES; /* Current number of tty rows. */
157 
158 static int	aflag;
159 static int	nflag;
160 static int	Pflag;
161 static int	hflag;
162 
163 static kvm_t	*kd;
164 
165 #define	FORKSTAT	0x01
166 #define	INTRSTAT	0x02
167 #define	MEMSTAT		0x04
168 #define	SUMSTAT		0x08
169 #define	TIMESTAT	0x10
170 #define	VMSTAT		0x20
171 #define	ZMEMSTAT	0x40
172 #define	OBJSTAT		0x80
173 
174 static void	cpustats(void);
175 static void	pcpustats(u_long, int);
176 static void	devstats(void);
177 static void	doforkst(void);
178 static void	dointr(unsigned int, int);
179 static void	doobjstat(void);
180 static void	dosum(void);
181 static void	dovmstat(unsigned int, int);
182 static void	domemstat_malloc(void);
183 static void	domemstat_zone(void);
184 static void	kread(int, void *, size_t);
185 static void	kreado(int, void *, size_t, size_t);
186 static void	kreadptr(uintptr_t, void *, size_t);
187 static void	needhdr(int);
188 static void	needresize(int);
189 static void	doresize(void);
190 static void	printhdr(int, u_long);
191 static void	usage(void);
192 
193 static long	pct(long, long);
194 static long long	getuptime(void);
195 
196 static char	**getdrivedata(char **);
197 
198 int
199 main(int argc, char *argv[])
200 {
201 	char *bp, *buf, *memf, *nlistf;
202 	float f;
203 	int bufsize, c, reps, todo;
204 	size_t len;
205 	unsigned int interval;
206 	char errbuf[_POSIX2_LINE_MAX];
207 
208 	memf = nlistf = NULL;
209 	interval = reps = todo = 0;
210 	maxshowdevs = 2;
211 
212 	argc = xo_parse_args(argc, argv);
213 	if (argc < 0)
214 		return (argc);
215 
216 	hflag = isatty(1);
217 
218 	while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:oPp:sw:z")) != -1) {
219 		switch (c) {
220 		case 'a':
221 			aflag++;
222 			break;
223 		case 'c':
224 			reps = atoi(optarg);
225 			break;
226 		case 'P':
227 			Pflag++;
228 			break;
229 		case 'f':
230 			todo |= FORKSTAT;
231 			break;
232 		case 'h':
233 			hflag = 1;
234 			break;
235 		case 'H':
236 			hflag = 0;
237 			break;
238 		case 'i':
239 			todo |= INTRSTAT;
240 			break;
241 		case 'M':
242 			memf = optarg;
243 			break;
244 		case 'm':
245 			todo |= MEMSTAT;
246 			break;
247 		case 'N':
248 			nlistf = optarg;
249 			break;
250 		case 'n':
251 			nflag = 1;
252 			maxshowdevs = atoi(optarg);
253 			if (maxshowdevs < 0)
254 				xo_errx(1, "number of devices %d is < 0",
255 				    maxshowdevs);
256 			break;
257 		case 'o':
258 			todo |= OBJSTAT;
259 			break;
260 		case 'p':
261 			if (devstat_buildmatch(optarg, &matches, &num_matches)
262 			    != 0)
263 				xo_errx(1, "%s", devstat_errbuf);
264 			break;
265 		case 's':
266 			todo |= SUMSTAT;
267 			break;
268 		case 'w':
269 			/* Convert to milliseconds. */
270 			f = atof(optarg);
271 			interval = f * 1000;
272 			break;
273 		case 'z':
274 			todo |= ZMEMSTAT;
275 			break;
276 		case '?':
277 		default:
278 			usage();
279 		}
280 	}
281 	argc -= optind;
282 	argv += optind;
283 
284 	xo_set_version(VMSTAT_XO_VERSION);
285 	if (!hflag)
286 		xo_set_options(NULL, "no-humanize");
287 	if (todo == 0)
288 		todo = VMSTAT;
289 
290 	if (memf != NULL) {
291 		kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
292 		if (kd == NULL)
293 			xo_errx(1, "kvm_openfiles: %s", errbuf);
294 	}
295 
296 retry_nlist:
297 	if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) {
298 		if (c > 0) {
299 			bufsize = 0;
300 			len = 0;
301 
302 			/*
303 			 * 'cnt' was renamed to 'vm_cnt'.  If 'vm_cnt' is not
304 			 * found try looking up older 'cnt' symbol.
305 			 * */
306 			if (namelist[X_SUM].n_type == 0 &&
307 			    strcmp(namelist[X_SUM].n_name, "_vm_cnt") == 0) {
308 				namelist[X_SUM].n_name = "_cnt";
309 				goto retry_nlist;
310 			}
311 
312 			/*
313 			 * 'nintrcnt' doesn't exist in older kernels, but
314 			 * that isn't fatal.
315 			 */
316 			if (namelist[X_NINTRCNT].n_type == 0 && c == 1)
317 				goto nlist_ok;
318 
319 			for (c = 0; c < (int)(nitems(namelist)); c++)
320 				if (namelist[c].n_type == 0)
321 					bufsize += strlen(namelist[c].n_name)
322 					    + 1;
323 			bufsize += len + 1;
324 			buf = bp = alloca(bufsize);
325 
326 			for (c = 0; c < (int)(nitems(namelist)); c++)
327 				if (namelist[c].n_type == 0) {
328 					xo_error(" %s",
329 					    namelist[c].n_name);
330 					len = strlen(namelist[c].n_name);
331 					*bp++ = ' ';
332 					memcpy(bp, namelist[c].n_name, len);
333 					bp += len;
334 				}
335 			*bp = '\0';
336 			xo_error("undefined symbols:\n", buf);
337 		} else
338 			xo_warnx("kvm_nlist: %s", kvm_geterr(kd));
339 		xo_finish();
340 		exit(1);
341 	}
342 nlist_ok:
343 	if (kd && Pflag)
344 		xo_errx(1, "Cannot use -P with crash dumps");
345 
346 	if (todo & VMSTAT) {
347 		/*
348 		 * Make sure that the userland devstat version matches the
349 		 * kernel devstat version.  If not, exit and print a
350 		 * message informing the user of his mistake.
351 		 */
352 		if (devstat_checkversion(NULL) < 0)
353 			xo_errx(1, "%s", devstat_errbuf);
354 
355 
356 		argv = getdrivedata(argv);
357 	}
358 
359 	if (*argv) {
360 		f = atof(*argv);
361 		interval = f * 1000;
362 		if (*++argv)
363 			reps = atoi(*argv);
364 	}
365 
366 	if (interval) {
367 		if (!reps)
368 			reps = -1;
369 	} else if (reps)
370 		interval = 1 * 1000;
371 
372 	if (todo & FORKSTAT)
373 		doforkst();
374 	if (todo & MEMSTAT)
375 		domemstat_malloc();
376 	if (todo & ZMEMSTAT)
377 		domemstat_zone();
378 	if (todo & SUMSTAT)
379 		dosum();
380 	if (todo & OBJSTAT)
381 		doobjstat();
382 	if (todo & INTRSTAT)
383 		dointr(interval, reps);
384 	if (todo & VMSTAT)
385 		dovmstat(interval, reps);
386 	xo_finish();
387 	exit(0);
388 }
389 
390 static int
391 mysysctl(const char *name, void *oldp, size_t *oldlenp)
392 {
393 	int error;
394 
395 	error = sysctlbyname(name, oldp, oldlenp, NULL, 0);
396 	if (error != 0 && errno != ENOMEM)
397 		xo_err(1, "sysctl(%s)", name);
398 	return (error);
399 }
400 
401 static char **
402 getdrivedata(char **argv)
403 {
404 
405 	if ((num_devices = devstat_getnumdevs(NULL)) < 0)
406 		xo_errx(1, "%s", devstat_errbuf);
407 
408 	cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
409 	last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
410 
411 	if (devstat_getdevs(NULL, &cur) == -1)
412 		xo_errx(1, "%s", devstat_errbuf);
413 
414 	num_devices = cur.dinfo->numdevs;
415 	generation = cur.dinfo->generation;
416 
417 	specified_devices = malloc(sizeof(char *));
418 	for (num_devices_specified = 0; *argv; ++argv) {
419 		if (isdigit(**argv))
420 			break;
421 		num_devices_specified++;
422 		specified_devices = reallocf(specified_devices,
423 		    sizeof(char *) * num_devices_specified);
424 		if (specified_devices == NULL) {
425 			xo_errx(1, "%s", "reallocf (specified_devices)");
426 		}
427 		specified_devices[num_devices_specified - 1] = *argv;
428 	}
429 	dev_select = NULL;
430 
431 	if (nflag == 0 && maxshowdevs < num_devices_specified)
432 		maxshowdevs = num_devices_specified;
433 
434 	/*
435 	 * People are generally only interested in disk statistics when
436 	 * they're running vmstat.  So, that's what we're going to give
437 	 * them if they don't specify anything by default.  We'll also give
438 	 * them any other random devices in the system so that we get to
439 	 * maxshowdevs devices, if that many devices exist.  If the user
440 	 * specifies devices on the command line, either through a pattern
441 	 * match or by naming them explicitly, we will give the user only
442 	 * those devices.
443 	 */
444 	if ((num_devices_specified == 0) && (num_matches == 0)) {
445 		if (devstat_buildmatch(da, &matches, &num_matches) != 0)
446 			xo_errx(1, "%s", devstat_errbuf);
447 		select_mode = DS_SELECT_ADD;
448 	} else
449 		select_mode = DS_SELECT_ONLY;
450 
451 	/*
452 	 * At this point, selectdevs will almost surely indicate that the
453 	 * device list has changed, so we don't look for return values of 0
454 	 * or 1.  If we get back -1, though, there is an error.
455 	 */
456 	if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
457 	    &select_generation, generation, cur.dinfo->devices,
458 	    num_devices, matches, num_matches, specified_devices,
459 	    num_devices_specified, select_mode,
460 	    maxshowdevs, 0) == -1)
461 		xo_errx(1, "%s", devstat_errbuf);
462 
463 	return(argv);
464 }
465 
466 /* Return system uptime in nanoseconds */
467 static long long
468 getuptime(void)
469 {
470 	struct timespec sp;
471 
472 	(void)clock_gettime(CLOCK_UPTIME, &sp);
473 	return((long long)sp.tv_sec * 1000000000LL + sp.tv_nsec);
474 }
475 
476 static void
477 fill_vmmeter(struct __vmmeter *vmmp)
478 {
479 	struct vmmeter vm_cnt;
480 	size_t size;
481 
482 	if (kd != NULL) {
483 		kread(X_SUM, &vm_cnt, sizeof(vm_cnt));
484 #define	GET_COUNTER(name) \
485 		vmmp->name = kvm_counter_u64_fetch(kd, (u_long)vm_cnt.name)
486 		GET_COUNTER(v_swtch);
487 		GET_COUNTER(v_trap);
488 		GET_COUNTER(v_syscall);
489 		GET_COUNTER(v_intr);
490 		GET_COUNTER(v_soft);
491 		GET_COUNTER(v_vm_faults);
492 		GET_COUNTER(v_io_faults);
493 		GET_COUNTER(v_cow_faults);
494 		GET_COUNTER(v_cow_optim);
495 		GET_COUNTER(v_zfod);
496 		GET_COUNTER(v_ozfod);
497 		GET_COUNTER(v_swapin);
498 		GET_COUNTER(v_swapout);
499 		GET_COUNTER(v_swappgsin);
500 		GET_COUNTER(v_swappgsout);
501 		GET_COUNTER(v_vnodein);
502 		GET_COUNTER(v_vnodeout);
503 		GET_COUNTER(v_vnodepgsin);
504 		GET_COUNTER(v_vnodepgsout);
505 		GET_COUNTER(v_intrans);
506 		GET_COUNTER(v_tfree);
507 		GET_COUNTER(v_forks);
508 		GET_COUNTER(v_vforks);
509 		GET_COUNTER(v_rforks);
510 		GET_COUNTER(v_kthreads);
511 		GET_COUNTER(v_forkpages);
512 		GET_COUNTER(v_vforkpages);
513 		GET_COUNTER(v_rforkpages);
514 		GET_COUNTER(v_kthreadpages);
515 #undef GET_COUNTER
516 	} else {
517 #define GET_VM_STATS(cat, name)	do {					\
518 	size = sizeof(vmmp->name);					\
519 	mysysctl("vm.stats." #cat "." #name, &vmmp->name, &size);	\
520 } while (0)
521 		/* sys */
522 		GET_VM_STATS(sys, v_swtch);
523 		GET_VM_STATS(sys, v_trap);
524 		GET_VM_STATS(sys, v_syscall);
525 		GET_VM_STATS(sys, v_intr);
526 		GET_VM_STATS(sys, v_soft);
527 
528 		/* vm */
529 		GET_VM_STATS(vm, v_vm_faults);
530 		GET_VM_STATS(vm, v_io_faults);
531 		GET_VM_STATS(vm, v_cow_faults);
532 		GET_VM_STATS(vm, v_cow_optim);
533 		GET_VM_STATS(vm, v_zfod);
534 		GET_VM_STATS(vm, v_ozfod);
535 		GET_VM_STATS(vm, v_swapin);
536 		GET_VM_STATS(vm, v_swapout);
537 		GET_VM_STATS(vm, v_swappgsin);
538 		GET_VM_STATS(vm, v_swappgsout);
539 		GET_VM_STATS(vm, v_vnodein);
540 		GET_VM_STATS(vm, v_vnodeout);
541 		GET_VM_STATS(vm, v_vnodepgsin);
542 		GET_VM_STATS(vm, v_vnodepgsout);
543 		GET_VM_STATS(vm, v_intrans);
544 		GET_VM_STATS(vm, v_reactivated);
545 		GET_VM_STATS(vm, v_pdwakeups);
546 		GET_VM_STATS(vm, v_pdpages);
547 		GET_VM_STATS(vm, v_pdshortfalls);
548 		GET_VM_STATS(vm, v_dfree);
549 		GET_VM_STATS(vm, v_pfree);
550 		GET_VM_STATS(vm, v_tfree);
551 		GET_VM_STATS(vm, v_page_size);
552 		GET_VM_STATS(vm, v_page_count);
553 		GET_VM_STATS(vm, v_free_reserved);
554 		GET_VM_STATS(vm, v_free_target);
555 		GET_VM_STATS(vm, v_free_min);
556 		GET_VM_STATS(vm, v_free_count);
557 		GET_VM_STATS(vm, v_wire_count);
558 		GET_VM_STATS(vm, v_user_wire_count);
559 		GET_VM_STATS(vm, v_active_count);
560 		GET_VM_STATS(vm, v_inactive_target);
561 		GET_VM_STATS(vm, v_inactive_count);
562 		GET_VM_STATS(vm, v_laundry_count);
563 		GET_VM_STATS(vm, v_pageout_free_min);
564 		GET_VM_STATS(vm, v_interrupt_free_min);
565 		/*GET_VM_STATS(vm, v_free_severe);*/
566 		GET_VM_STATS(vm, v_forks);
567 		GET_VM_STATS(vm, v_vforks);
568 		GET_VM_STATS(vm, v_rforks);
569 		GET_VM_STATS(vm, v_kthreads);
570 		GET_VM_STATS(vm, v_forkpages);
571 		GET_VM_STATS(vm, v_vforkpages);
572 		GET_VM_STATS(vm, v_rforkpages);
573 		GET_VM_STATS(vm, v_kthreadpages);
574 #undef GET_VM_STATS
575 	}
576 }
577 
578 static void
579 fill_vmtotal(struct vmtotal *vmtp)
580 {
581 	size_t size;
582 
583 	if (kd != NULL) {
584 		/* XXX fill vmtp */
585 		xo_errx(1, "not implemented");
586 	} else {
587 		size = sizeof(*vmtp);
588 		mysysctl("vm.vmtotal", vmtp, &size);
589 		if (size != sizeof(*vmtp))
590 			xo_errx(1, "vm.total size mismatch");
591 	}
592 }
593 
594 /* Determine how many cpu columns, and what index they are in kern.cp_times */
595 static void
596 getcpuinfo(u_long *maskp, int *maxidp)
597 {
598 	long *times;
599 	u_long mask;
600 	size_t size;
601 	int empty, i, j, maxcpu, maxid;
602 
603 	if (kd != NULL)
604 		xo_errx(1, "not implemented");
605 	mask = 0;
606 	size = sizeof(maxcpu);
607 	mysysctl("kern.smp.maxcpus", &maxcpu, &size);
608 	if (size != sizeof(maxcpu))
609 		xo_errx(1, "sysctl kern.smp.maxcpus");
610 	size = sizeof(long) * maxcpu * CPUSTATES;
611 	times = malloc(size);
612 	if (times == NULL)
613 		xo_err(1, "malloc %zd bytes", size);
614 	mysysctl("kern.cp_times", times, &size);
615 	maxid = (size / CPUSTATES / sizeof(long)) - 1;
616 	for (i = 0; i <= maxid; i++) {
617 		empty = 1;
618 		for (j = 0; empty && j < CPUSTATES; j++) {
619 			if (times[i * CPUSTATES + j] != 0)
620 				empty = 0;
621 		}
622 		if (!empty)
623 			mask |= (1ul << i);
624 	}
625 	if (maskp)
626 		*maskp = mask;
627 	if (maxidp)
628 		*maxidp = maxid;
629 }
630 
631 static void
632 dovmstat(unsigned int interval, int reps)
633 {
634 	struct clockinfo clockrate;
635 	struct vmtotal total;
636 	struct devinfo *tmp_dinfo;
637 	u_long cpumask;
638 	size_t size;
639 	time_t uptime, halfuptime;
640 	int maxid, rate_adj, retval;
641 
642 	uptime = getuptime() / 1000000000LL;
643 	halfuptime = uptime / 2;
644 	rate_adj = 1;
645 	maxid = 0;
646 	cpumask = 0;
647 
648 	/*
649 	 * If the user stops the program (control-Z) and then resumes it,
650 	 * print out the header again.
651 	 */
652 	(void)signal(SIGCONT, needhdr);
653 
654 	/*
655 	 * If our standard output is a tty, then install a SIGWINCH handler
656 	 * and set wresized so that our first iteration through the main
657 	 * vmstat loop will peek at the terminal's current rows to find out
658 	 * how many lines can fit in a screenful of output.
659 	 */
660 	if (isatty(fileno(stdout)) != 0) {
661 		wresized = 1;
662 		(void)signal(SIGWINCH, needresize);
663 	} else {
664 		wresized = 0;
665 		winlines = VMSTAT_DEFAULT_LINES;
666 	}
667 
668 	if (kd != NULL) {
669 		if (namelist[X_STATHZ].n_type != 0 &&
670 		    namelist[X_STATHZ].n_value != 0)
671 			kread(X_STATHZ, &hz, sizeof(hz));
672 		if (!hz)
673 			kread(X_HZ, &hz, sizeof(hz));
674 	} else {
675 		size = sizeof(clockrate);
676 		mysysctl("kern.clockrate", &clockrate, &size);
677 		if (size != sizeof(clockrate))
678 			xo_errx(1, "clockrate size mismatch");
679 		hz = clockrate.hz;
680 	}
681 
682 	if (Pflag) {
683 		getcpuinfo(&cpumask, &maxid);
684 		size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES;
685 		cur_cp_times = calloc(1, size_cp_times);
686 		last_cp_times = calloc(1, size_cp_times);
687 	}
688 	for (hdrcnt = 1;;) {
689 		if (!--hdrcnt)
690 			printhdr(maxid, cpumask);
691 		if (kd != NULL) {
692 			if (kvm_getcptime(kd, cur.cp_time) < 0)
693 				xo_errx(1, "kvm_getcptime: %s", kvm_geterr(kd));
694 		} else {
695 			size = sizeof(cur.cp_time);
696 			mysysctl("kern.cp_time", &cur.cp_time, &size);
697 			if (size != sizeof(cur.cp_time))
698 				xo_errx(1, "cp_time size mismatch");
699 		}
700 		if (Pflag) {
701 			size = size_cp_times;
702 			mysysctl("kern.cp_times", cur_cp_times, &size);
703 			if (size != size_cp_times)
704 				xo_errx(1, "cp_times mismatch");
705 		}
706 
707 		tmp_dinfo = last.dinfo;
708 		last.dinfo = cur.dinfo;
709 		cur.dinfo = tmp_dinfo;
710 		last.snap_time = cur.snap_time;
711 
712 		/*
713 		 * Here what we want to do is refresh our device stats.
714 		 * getdevs() returns 1 when the device list has changed.
715 		 * If the device list has changed, we want to go through
716 		 * the selection process again, in case a device that we
717 		 * were previously displaying has gone away.
718 		 */
719 		switch (devstat_getdevs(NULL, &cur)) {
720 		case -1:
721 			xo_errx(1, "%s", devstat_errbuf);
722 			break;
723 		case 1:
724 			num_devices = cur.dinfo->numdevs;
725 			generation = cur.dinfo->generation;
726 
727 			retval = devstat_selectdevs(&dev_select, &num_selected,
728 			    &num_selections, &select_generation,
729 			    generation, cur.dinfo->devices,
730 			    num_devices, matches, num_matches,
731 			    specified_devices,
732 			    num_devices_specified, select_mode,
733 			    maxshowdevs, 0);
734 			switch (retval) {
735 			case -1:
736 				xo_errx(1, "%s", devstat_errbuf);
737 				break;
738 			case 1:
739 				printhdr(maxid, cpumask);
740 				break;
741 			default:
742 				break;
743 			}
744 			break;
745 		default:
746 			break;
747 		}
748 
749 		fill_vmmeter(&sum);
750 		fill_vmtotal(&total);
751 		xo_open_container("processes");
752 		xo_emit("{:runnable/%2d} {:waiting/%2ld} "
753 		    "{:swapped-out/%2ld}", total.t_rq - 1, total.t_dw +
754 		    total.t_pw, total.t_sw);
755 		xo_close_container("processes");
756 		xo_open_container("memory");
757 #define	rate(x)	(unsigned long)(((x) * rate_adj + halfuptime) / uptime)
758 		xo_emit(" {[:4}{h,hn-decimal:available-memory/%ju}{]:}",
759 		    (uintmax_t)total.t_avm * sum.v_page_size);
760 		xo_emit(" {[:4}{h,hn-decimal:free-memory/%ju}{]:}",
761 		    (uintmax_t)total.t_free * sum.v_page_size);
762 		xo_emit(" {[:4}{h,hn-decimal,hn-1000:total-page-faults/%lu}{]:} ",
763 		    rate(sum.v_vm_faults - osum.v_vm_faults));
764 		xo_close_container("memory");
765 
766 		xo_open_container("paging-rates");
767 		xo_emit("{:page-reactivated/%3lu} ",
768 		    rate(sum.v_reactivated - osum.v_reactivated));
769 		xo_emit("{:paged-in/%3lu} ",
770 		    rate(sum.v_swapin + sum.v_vnodein -
771 		    (osum.v_swapin + osum.v_vnodein)));
772 		xo_emit("{:paged-out/%3lu}",
773 		    rate(sum.v_swapout + sum.v_vnodeout -
774 		    (osum.v_swapout + osum.v_vnodeout)));
775 		xo_emit(" {[:4}{h,hn-decimal,hn-1000:freed/%lu}{]:}",
776 		    rate(sum.v_tfree - osum.v_tfree));
777 		xo_emit(" {[:4}{h,hn-decimal,hn-1000:scanned/%lu}{]:}",
778 		    rate(sum.v_pdpages - osum.v_pdpages));
779 		xo_close_container("paging-rates");
780 
781 		devstats();
782 		xo_open_container("fault-rates");
783 		xo_emit(" {[:4}{h,hn-decimal,hn-1000:interrupts/%lu}{]:}"
784 		    " {[:4}{h,hn-decimal,hn-1000:system-calls/%lu}{]:}"
785 		    " {[:4}{h,hn-decimal,hn-1000:context-switches/%lu}{]:}",
786 		    rate(sum.v_intr - osum.v_intr),
787 		    rate(sum.v_syscall - osum.v_syscall),
788 		    rate(sum.v_swtch - osum.v_swtch));
789 		xo_close_container("fault-rates");
790 		if (Pflag)
791 			pcpustats(cpumask, maxid);
792 		else
793 			cpustats();
794 		xo_emit("\n");
795 		xo_flush();
796 		if (reps >= 0 && --reps <= 0)
797 			break;
798 		osum = sum;
799 		uptime = interval;
800 		rate_adj = 1000;
801 		/*
802 		 * We round upward to avoid losing low-frequency events
803 		 * (i.e., >= 1 per interval but < 1 per millisecond).
804 		 */
805 		if (interval != 1)
806 			halfuptime = (uptime + 1) / 2;
807 		else
808 			halfuptime = 0;
809 		(void)usleep(interval * 1000);
810 	}
811 }
812 
813 static void
814 printhdr(int maxid, u_long cpumask)
815 {
816 	int i, num_shown;
817 
818 	num_shown = MIN(num_selected, maxshowdevs);
819 	xo_emit(" {T:procs}    {T:memory}    {T:/page%*s}", 19, "");
820 	if (num_shown > 1)
821 		xo_emit("   {T:/disks %*s}  ", num_shown * 5 - 7, "");
822 	else if (num_shown == 1)
823 		xo_emit("   {T:disks} ");
824 	xo_emit(" {T:faults}      ");
825 	if (Pflag) {
826 		for (i = 0; i <= maxid; i++) {
827 			if (cpumask & (1ul << i))
828 				xo_emit("  {T:/cpu%d}   ", i);
829 		}
830 		xo_emit("\n");
831 	} else
832 		xo_emit(" {T:cpu}\n");
833 	xo_emit(" {T:r}  {T:b}  {T:w}  {T:avm}  {T:fre}  {T:flt}  {T:re}"
834 	    "  {T:pi}  {T:po}   {T:fr}   {T:sr} ");
835 	for (i = 0; i < num_devices; i++)
836 		if ((dev_select[i].selected) &&
837 		    (dev_select[i].selected <= maxshowdevs))
838 			xo_emit("{T:/%3.3s%d} ", dev_select[i].device_name,
839 			    dev_select[i].unit_number);
840 	xo_emit("  {T:in}   {T:sy}   {T:cs}");
841 	if (Pflag) {
842 		for (i = 0; i <= maxid; i++) {
843 			if (cpumask & (1ul << i))
844 				xo_emit(" {T:us} {T:sy} {T:id}");
845 		}
846 		xo_emit("\n");
847 	} else
848 		xo_emit(" {T:us} {T:sy} {T:id}\n");
849 	if (wresized != 0)
850 		doresize();
851 	hdrcnt = winlines;
852 }
853 
854 /*
855  * Force a header to be prepended to the next output.
856  */
857 static void
858 needhdr(int dummy __unused)
859 {
860 
861 	hdrcnt = 1;
862 }
863 
864 /*
865  * When the terminal is resized, force an update of the maximum number of rows
866  * printed between each header repetition.  Then force a new header to be
867  * prepended to the next output.
868  */
869 void
870 needresize(int signo __unused)
871 {
872 
873 	wresized = 1;
874 	hdrcnt = 1;
875 }
876 
877 /*
878  * Update the global `winlines' count of terminal rows.
879  */
880 void
881 doresize(void)
882 {
883 	struct winsize w;
884 	int status;
885 
886 	for (;;) {
887 		status = ioctl(fileno(stdout), TIOCGWINSZ, &w);
888 		if (status == -1 && errno == EINTR)
889 			continue;
890 		else if (status == -1)
891 			xo_err(1, "ioctl");
892 		if (w.ws_row > 3)
893 			winlines = w.ws_row - 3;
894 		else
895 			winlines = VMSTAT_DEFAULT_LINES;
896 		break;
897 	}
898 
899 	/*
900 	 * Inhibit doresize() calls until we are rescheduled by SIGWINCH.
901 	 */
902 	wresized = 0;
903 }
904 
905 static long
906 pct(long top, long bot)
907 {
908 	long ans;
909 
910 	if (bot == 0)
911 		return(0);
912 	ans = (quad_t)top * 100 / bot;
913 	return (ans);
914 }
915 
916 #define	PCT(top, bot) pct((long)(top), (long)(bot))
917 
918 static void
919 dosum(void)
920 {
921 	struct nchstats lnchstats;
922 	size_t size;
923 	long nchtotal;
924 
925 	fill_vmmeter(&sum);
926 	xo_open_container("summary-statistics");
927 	xo_emit("{:context-switches/%9u} {N:cpu context switches}\n",
928 	    sum.v_swtch);
929 	xo_emit("{:interrupts/%9u} {N:device interrupts}\n",
930 	    sum.v_intr);
931 	xo_emit("{:software-interrupts/%9u} {N:software interrupts}\n",
932 	    sum.v_soft);
933 	xo_emit("{:traps/%9u} {N:traps}\n", sum.v_trap);
934 	xo_emit("{:system-calls/%9u} {N:system calls}\n",
935 	    sum.v_syscall);
936 	xo_emit("{:kernel-threads/%9u} {N:kernel threads created}\n",
937 	    sum.v_kthreads);
938 	xo_emit("{:forks/%9u} {N: fork() calls}\n", sum.v_forks);
939 	xo_emit("{:vforks/%9u} {N:vfork() calls}\n",
940 	    sum.v_vforks);
941 	xo_emit("{:rforks/%9u} {N:rfork() calls}\n",
942 	    sum.v_rforks);
943 	xo_emit("{:swap-ins/%9u} {N:swap pager pageins}\n",
944 	    sum.v_swapin);
945 	xo_emit("{:swap-in-pages/%9u} {N:swap pager pages paged in}\n",
946 	    sum.v_swappgsin);
947 	xo_emit("{:swap-outs/%9u} {N:swap pager pageouts}\n",
948 	    sum.v_swapout);
949 	xo_emit("{:swap-out-pages/%9u} {N:swap pager pages paged out}\n",
950 	    sum.v_swappgsout);
951 	xo_emit("{:vnode-page-ins/%9u} {N:vnode pager pageins}\n",
952 	    sum.v_vnodein);
953 	xo_emit("{:vnode-page-in-pages/%9u} {N:vnode pager pages paged in}\n",
954 	    sum.v_vnodepgsin);
955 	xo_emit("{:vnode-page-outs/%9u} {N:vnode pager pageouts}\n",
956 	    sum.v_vnodeout);
957 	xo_emit("{:vnode-page-out-pages/%9u} {N:vnode pager pages paged out}\n",
958 	    sum.v_vnodepgsout);
959 	xo_emit("{:page-daemon-wakeups/%9u} {N:page daemon wakeups}\n",
960 	    sum.v_pdwakeups);
961 	xo_emit("{:page-daemon-pages/%9u} {N:pages examined by the page "
962 	    "daemon}\n", sum.v_pdpages);
963 	xo_emit("{:page-reclamation-shortfalls/%9u} {N:clean page reclamation "
964 	    "shortfalls}\n", sum.v_pdshortfalls);
965 	xo_emit("{:reactivated/%9u} {N:pages reactivated by the page daemon}\n",
966 	    sum.v_reactivated);
967 	xo_emit("{:copy-on-write-faults/%9u} {N:copy-on-write faults}\n",
968 	    sum.v_cow_faults);
969 	xo_emit("{:copy-on-write-optimized-faults/%9u} {N:copy-on-write "
970 	    "optimized faults}\n", sum.v_cow_optim);
971 	xo_emit("{:zero-fill-pages/%9u} {N:zero fill pages zeroed}\n",
972 	    sum.v_zfod);
973 	xo_emit("{:zero-fill-prezeroed/%9u} {N:zero fill pages prezeroed}\n",
974 	    sum.v_ozfod);
975 	xo_emit("{:intransit-blocking/%9u} {N:intransit blocking page faults}\n",
976 	    sum.v_intrans);
977 	xo_emit("{:total-faults/%9u} {N:total VM faults taken}\n",
978 	    sum.v_vm_faults);
979 	xo_emit("{:faults-requiring-io/%9u} {N:page faults requiring I\\/O}\n",
980 	    sum.v_io_faults);
981 	xo_emit("{:faults-from-thread-creation/%9u} {N:pages affected by "
982 	    "kernel thread creation}\n", sum.v_kthreadpages);
983 	xo_emit("{:faults-from-fork/%9u} {N:pages affected by  fork}()\n",
984 	    sum.v_forkpages);
985 	xo_emit("{:faults-from-vfork/%9u} {N:pages affected by vfork}()\n",
986 	    sum.v_vforkpages);
987 	xo_emit("{:pages-rfork/%9u} {N:pages affected by rfork}()\n",
988 	    sum.v_rforkpages);
989 	xo_emit("{:pages-freed/%9u} {N:pages freed}\n",
990 	    sum.v_tfree);
991 	xo_emit("{:pages-freed-by-daemon/%9u} {N:pages freed by daemon}\n",
992 	    sum.v_dfree);
993 	xo_emit("{:pages-freed-on-exit/%9u} {N:pages freed by exiting processes}\n",
994 	    sum.v_pfree);
995 	xo_emit("{:active-pages/%9u} {N:pages active}\n",
996 	    sum.v_active_count);
997 	xo_emit("{:inactive-pages/%9u} {N:pages inactive}\n",
998 	    sum.v_inactive_count);
999 	xo_emit("{:laundry-pages/%9u} {N:pages in the laundry queue}\n",
1000 	    sum.v_laundry_count);
1001 	xo_emit("{:wired-pages/%9u} {N:pages wired down}\n",
1002 	    sum.v_wire_count);
1003 	xo_emit("{:virtual-user-wired-pages/%9lu} {N:virtual user pages wired "
1004 	    "down}\n", sum.v_user_wire_count);
1005 	xo_emit("{:free-pages/%9u} {N:pages free}\n",
1006 	    sum.v_free_count);
1007 	xo_emit("{:bytes-per-page/%9u} {N:bytes per page}\n", sum.v_page_size);
1008 	if (kd != NULL) {
1009 		kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats));
1010 	} else {
1011 		size = sizeof(lnchstats);
1012 		mysysctl("vfs.cache.nchstats", &lnchstats, &size);
1013 		if (size != sizeof(lnchstats))
1014 			xo_errx(1, "vfs.cache.nchstats size mismatch");
1015 	}
1016 	nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits +
1017 	    lnchstats.ncs_badhits + lnchstats.ncs_falsehits +
1018 	    lnchstats.ncs_miss + lnchstats.ncs_long;
1019 	xo_emit("{:total-name-lookups/%9ld} {N:total name lookups}\n",
1020 	    nchtotal);
1021 	xo_emit("{P:/%9s} {N:cache hits} "
1022 	    "({:positive-cache-hits/%ld}% pos + "
1023 	    "{:negative-cache-hits/%ld}% {N:neg}) "
1024 	    "system {:cache-hit-percent/%ld}% per-directory\n",
1025 	    "", PCT(lnchstats.ncs_goodhits, nchtotal),
1026 	    PCT(lnchstats.ncs_neghits, nchtotal),
1027 	    PCT(lnchstats.ncs_pass2, nchtotal));
1028 	xo_emit("{P:/%9s} {L:deletions} {:deletions/%ld}%, "
1029 	    "{L:falsehits} {:false-hits/%ld}%, "
1030 	    "{L:toolong} {:too-long/%ld}%\n", "",
1031 	    PCT(lnchstats.ncs_badhits, nchtotal),
1032 	    PCT(lnchstats.ncs_falsehits, nchtotal),
1033 	    PCT(lnchstats.ncs_long, nchtotal));
1034 	xo_close_container("summary-statistics");
1035 }
1036 
1037 static void
1038 doforkst(void)
1039 {
1040 
1041 	fill_vmmeter(&sum);
1042 	xo_open_container("fork-statistics");
1043 	xo_emit("{:fork/%u} {N:forks}, {:fork-pages/%u} {N:pages}, "
1044 	    "{L:average} {:fork-average/%.2f}\n",
1045 	    sum.v_forks, sum.v_forkpages,
1046 	    sum.v_forks == 0 ? 0.0 :
1047 	    (double)sum.v_forkpages / sum.v_forks);
1048 	xo_emit("{:vfork/%u} {N:vforks}, {:vfork-pages/%u} {N:pages}, "
1049 	    "{L:average} {:vfork-average/%.2f}\n",
1050 	    sum.v_vforks, sum.v_vforkpages,
1051 	    sum.v_vforks == 0 ? 0.0 :
1052 	    (double)sum.v_vforkpages / sum.v_vforks);
1053 	xo_emit("{:rfork/%u} {N:rforks}, {:rfork-pages/%u} {N:pages}, "
1054 	    "{L:average} {:rfork-average/%.2f}\n",
1055 	    sum.v_rforks, sum.v_rforkpages,
1056 	    sum.v_rforks == 0 ? 0.0 :
1057 	    (double)sum.v_rforkpages / sum.v_rforks);
1058 	xo_close_container("fork-statistics");
1059 }
1060 
1061 static void
1062 devstats(void)
1063 {
1064 	long double busy_seconds, transfers_per_second;
1065 	long tmp;
1066 	int di, dn, state;
1067 
1068 	for (state = 0; state < CPUSTATES; ++state) {
1069 		tmp = cur.cp_time[state];
1070 		cur.cp_time[state] -= last.cp_time[state];
1071 		last.cp_time[state] = tmp;
1072 	}
1073 
1074 	busy_seconds = cur.snap_time - last.snap_time;
1075 
1076 	xo_open_list("device");
1077 	for (dn = 0; dn < num_devices; dn++) {
1078 		if (dev_select[dn].selected == 0 ||
1079 		    dev_select[dn].selected > maxshowdevs)
1080 			continue;
1081 
1082 		di = dev_select[dn].position;
1083 
1084 		if (devstat_compute_statistics(&cur.dinfo->devices[di],
1085 		    &last.dinfo->devices[di], busy_seconds,
1086 		    DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
1087 		    DSM_NONE) != 0)
1088 			xo_errx(1, "%s", devstat_errbuf);
1089 
1090 		xo_open_instance("device");
1091 		xo_emit("{ekq:name/%s%d}",
1092 		    dev_select[dn].device_name,
1093 		    dev_select[dn].unit_number);
1094 		xo_emit("{[:5}{h,hn-decimal,hn-1000:transfers/%ju}{]:}",
1095 		    (uintmax_t)transfers_per_second);
1096 		xo_close_instance("device");
1097 	}
1098 	xo_close_list("device");
1099 }
1100 
1101 static void
1102 percent(const char *name, long pctv, int *over)
1103 {
1104 	char fmt[64];
1105 
1106 	snprintf(fmt, sizeof(fmt), " {:%s/%%%ulld/%%lld}", name,
1107 	    (*over && pctv <= 9) ? 1 : 2);
1108 	xo_emit(fmt, pctv);
1109 	if (*over && pctv <= 9)
1110 		(*over)--;
1111 	else if (pctv >= 100)
1112 		(*over)++;
1113 }
1114 
1115 static void
1116 cpustats(void)
1117 {
1118 	long total;
1119 	int state, over;
1120 
1121 	total = 0;
1122 	for (state = 0; state < CPUSTATES; ++state)
1123 		total += cur.cp_time[state];
1124 	if (total == 0)
1125 		total = 1;
1126 	over = 0;
1127 	xo_open_container("cpu-statistics");
1128 	percent("user", 100LL * (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) /
1129 	    total, &over);
1130 	percent("system", 100LL * (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) /
1131 	    total, &over);
1132 	percent("idle", 100LL * cur.cp_time[CP_IDLE] / total, &over);
1133 	xo_close_container("cpu-statistics");
1134 }
1135 
1136 static void
1137 pcpustats(u_long cpumask, int maxid)
1138 {
1139 	long tmp, total;
1140 	int i, state, over;
1141 
1142 	/* devstats does this for cp_time */
1143 	for (i = 0; i <= maxid; i++) {
1144 		if ((cpumask & (1ul << i)) == 0)
1145 			continue;
1146 		for (state = 0; state < CPUSTATES; ++state) {
1147 			tmp = cur_cp_times[i * CPUSTATES + state];
1148 			cur_cp_times[i * CPUSTATES + state] -= last_cp_times[i *
1149 			    CPUSTATES + state];
1150 			last_cp_times[i * CPUSTATES + state] = tmp;
1151 		}
1152 	}
1153 
1154 	over = 0;
1155 	xo_open_list("cpu");
1156 	for (i = 0; i <= maxid; i++) {
1157 		if ((cpumask & (1ul << i)) == 0)
1158 			continue;
1159 		xo_open_instance("cpu");
1160 		xo_emit("{ke:name/%d}", i);
1161 		total = 0;
1162 		for (state = 0; state < CPUSTATES; ++state)
1163 			total += cur_cp_times[i * CPUSTATES + state];
1164 		if (total == 0)
1165 			total = 1;
1166 		percent("user",
1167 		    100LL * (cur_cp_times[i * CPUSTATES + CP_USER] +
1168 		    cur_cp_times[i * CPUSTATES + CP_NICE]) / total, &over);
1169 		percent("system",
1170 		    100LL * (cur_cp_times[i * CPUSTATES + CP_SYS] +
1171 		    cur_cp_times[i * CPUSTATES + CP_INTR]) / total, &over);
1172 		percent("idle",
1173 		    100LL * cur_cp_times[i * CPUSTATES + CP_IDLE] / total,
1174 		    &over);
1175 		xo_close_instance("cpu");
1176 	}
1177 	xo_close_list("cpu");
1178 }
1179 
1180 static unsigned int
1181 read_intrcnts(unsigned long **intrcnts)
1182 {
1183 	size_t intrcntlen;
1184 	uintptr_t kaddr;
1185 
1186 	if (kd != NULL) {
1187 		kread(X_SINTRCNT, &intrcntlen, sizeof(intrcntlen));
1188 		if ((*intrcnts = malloc(intrcntlen)) == NULL)
1189 			err(1, "malloc()");
1190 		if (namelist[X_NINTRCNT].n_type == 0)
1191 			kread(X_INTRCNT, *intrcnts, intrcntlen);
1192 		else {
1193 			kread(X_INTRCNT, &kaddr, sizeof(kaddr));
1194 			kreadptr(kaddr, *intrcnts, intrcntlen);
1195 		}
1196 	} else {
1197 		for (*intrcnts = NULL, intrcntlen = 1024; ; intrcntlen *= 2) {
1198 			*intrcnts = reallocf(*intrcnts, intrcntlen);
1199 			if (*intrcnts == NULL)
1200 				err(1, "reallocf()");
1201 			if (mysysctl("hw.intrcnt", *intrcnts, &intrcntlen) == 0)
1202 				break;
1203 		}
1204 	}
1205 
1206 	return (intrcntlen / sizeof(unsigned long));
1207 }
1208 
1209 static void
1210 print_intrcnts(unsigned long *intrcnts, unsigned long *old_intrcnts,
1211     char *intrnames, unsigned int nintr, size_t istrnamlen, long long period_ms)
1212 {
1213 	uint64_t inttotal, old_inttotal, total_count, total_rate;
1214 	unsigned long count, rate;
1215 	unsigned int i;
1216 
1217 	inttotal = 0;
1218 	old_inttotal = 0;
1219 	xo_open_list("interrupt");
1220 	for (i = 0; i < nintr; i++) {
1221 		if (intrnames[0] != '\0' && (*intrcnts != 0 || aflag)) {
1222 			count = *intrcnts - *old_intrcnts;
1223 			rate = ((uint64_t)count * 1000 + period_ms / 2) / period_ms;
1224 			xo_open_instance("interrupt");
1225 			xo_emit("{d:name/%-*s}{ket:name/%s} "
1226 			    "{:total/%20lu} {:rate/%10lu}\n",
1227 			    (int)istrnamlen, intrnames, intrnames, count, rate);
1228 			xo_close_instance("interrupt");
1229 		}
1230 		intrnames += strlen(intrnames) + 1;
1231 		inttotal += *intrcnts++;
1232 		old_inttotal += *old_intrcnts++;
1233 	}
1234 	total_count = inttotal - old_inttotal;
1235 	total_rate = (total_count * 1000 + period_ms / 2) / period_ms;
1236 	xo_close_list("interrupt");
1237 	xo_emit("{L:/%-*s} {:total-interrupts/%20ju} "
1238 	    "{:total-rate/%10ju}\n", (int)istrnamlen,
1239 	    "Total", (uintmax_t)total_count, (uintmax_t)total_rate);
1240 }
1241 
1242 static void
1243 dointr(unsigned int interval, int reps)
1244 {
1245 	unsigned long *intrcnts, *old_intrcnts;
1246 	char *intrname, *intrnames;
1247 	long long period_ms, old_uptime, uptime;
1248 	size_t clen, inamlen, istrnamlen;
1249 	uintptr_t kaddr;
1250 	unsigned int nintr;
1251 
1252 	old_intrcnts = NULL;
1253 	uptime = getuptime();
1254 
1255 	/* Get the names of each interrupt source */
1256 	if (kd != NULL) {
1257 		kread(X_SINTRNAMES, &inamlen, sizeof(inamlen));
1258 		if ((intrnames = malloc(inamlen)) == NULL)
1259 			xo_err(1, "malloc()");
1260 		if (namelist[X_NINTRCNT].n_type == 0)
1261 			kread(X_INTRNAMES, intrnames, inamlen);
1262 		else {
1263 			kread(X_INTRNAMES, &kaddr, sizeof(kaddr));
1264 			kreadptr(kaddr, intrnames, inamlen);
1265 		}
1266 	} else {
1267 		for (intrnames = NULL, inamlen = 1024; ; inamlen *= 2) {
1268 			if ((intrnames = reallocf(intrnames, inamlen)) == NULL)
1269 				xo_err(1, "reallocf()");
1270 			if (mysysctl("hw.intrnames", intrnames, &inamlen) == 0)
1271 				break;
1272 		}
1273 	}
1274 
1275 	/* Determine the length of the longest interrupt name */
1276 	intrname = intrnames;
1277 	istrnamlen = strlen("interrupt");
1278 	while (intrname < intrnames + inamlen) {
1279 		clen = strlen(intrname);
1280 		if (clen > istrnamlen)
1281 			istrnamlen = clen;
1282 		intrname += strlen(intrname) + 1;
1283 	}
1284 	xo_emit("{T:/%-*s} {T:/%20s} {T:/%10s}\n",
1285 	    (int)istrnamlen, "interrupt", "total", "rate");
1286 
1287 	/*
1288 	 * Loop reps times printing differential interrupt counts.  If reps is
1289 	 * zero, then run just once, printing total counts
1290 	 */
1291 	xo_open_container("interrupt-statistics");
1292 
1293 	period_ms = uptime / 1000000;
1294 	while(1) {
1295 		nintr = read_intrcnts(&intrcnts);
1296 		/*
1297 		 * Initialize old_intrcnts to 0 for the first pass, so
1298 		 * print_intrcnts will print total interrupts since boot
1299 		 */
1300 		if (old_intrcnts == NULL) {
1301 			old_intrcnts = calloc(nintr, sizeof(unsigned long));
1302 			if (old_intrcnts == NULL)
1303 				xo_err(1, "calloc()");
1304 		}
1305 
1306 		print_intrcnts(intrcnts, old_intrcnts, intrnames, nintr,
1307 		    istrnamlen, period_ms);
1308 		xo_flush();
1309 
1310 		free(old_intrcnts);
1311 		old_intrcnts = intrcnts;
1312 		if (reps >= 0 && --reps <= 0)
1313 			break;
1314 		usleep(interval * 1000);
1315 		old_uptime = uptime;
1316 		uptime = getuptime();
1317 		period_ms = (uptime - old_uptime) / 1000000;
1318 	}
1319 
1320 	xo_close_container("interrupt-statistics");
1321 }
1322 
1323 static void
1324 domemstat_malloc(void)
1325 {
1326 	struct memory_type_list *mtlp;
1327 	struct memory_type *mtp;
1328 	size_t i, zones;
1329 	int error, first;
1330 
1331 	mtlp = memstat_mtl_alloc();
1332 	if (mtlp == NULL) {
1333 		xo_warn("memstat_mtl_alloc");
1334 		return;
1335 	}
1336 	if (kd == NULL) {
1337 		if (memstat_sysctl_malloc(mtlp, 0) < 0) {
1338 			xo_warnx("memstat_sysctl_malloc: %s",
1339 			    memstat_strerror(memstat_mtl_geterror(mtlp)));
1340 			return;
1341 		}
1342 	} else {
1343 		if (memstat_kvm_malloc(mtlp, kd) < 0) {
1344 			error = memstat_mtl_geterror(mtlp);
1345 			if (error == MEMSTAT_ERROR_KVM)
1346 				xo_warnx("memstat_kvm_malloc: %s",
1347 				    kvm_geterr(kd));
1348 			else
1349 				xo_warnx("memstat_kvm_malloc: %s",
1350 				    memstat_strerror(error));
1351 		}
1352 	}
1353 	xo_open_container("malloc-statistics");
1354 	xo_emit("{T:/%16s} {T:/%4s} {T:/%5s} {T:/%3s} {T:Size(s)}\n",
1355 	    "Type", "Use", "Memory", "Req");
1356 	xo_open_list("memory");
1357 	zones = memstat_malloc_zone_get_count();
1358 	for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1359 	    mtp = memstat_mtl_next(mtp)) {
1360 		if (memstat_get_numallocs(mtp) == 0 &&
1361 		    memstat_get_count(mtp) == 0)
1362 			continue;
1363 		xo_open_instance("memory");
1364 		xo_emit("{k:type/%16s/%s} "
1365 		    "{[:4}{h,hn-decimal,hn-1000:in-use/%ju}{]:} "
1366 		    "{[:5}{h,hn-decimal:memory-use/%ju}{]:} "
1367 		    "{[:4}{h,hn-decimal,hn-1000:requests/%ju}{]:} ",
1368 		    memstat_get_name(mtp), (uintmax_t)memstat_get_count(mtp),
1369 		    (uintmax_t)memstat_get_bytes(mtp),
1370 		    (uintmax_t)memstat_get_numallocs(mtp));
1371 		first = 1;
1372 		xo_open_list("size");
1373 		for (i = 0; i < zones; i++) {
1374 			if (memstat_malloc_zone_used(mtp, i)) {
1375 				if (!first)
1376 					xo_emit(",");
1377 				xo_emit("{lh:size/%d}", memstat_malloc_zone_get_size(i));
1378 				first = 0;
1379 			}
1380 		}
1381 		xo_close_list("size");
1382 		xo_close_instance("memory");
1383 		xo_emit("\n");
1384 	}
1385 	xo_close_list("memory");
1386 	xo_close_container("malloc-statistics");
1387 	memstat_mtl_free(mtlp);
1388 }
1389 
1390 static void
1391 domemstat_zone(void)
1392 {
1393 	struct memory_type_list *mtlp;
1394 	struct memory_type *mtp;
1395 	int error, len;
1396 
1397 	mtlp = memstat_mtl_alloc();
1398 	if (mtlp == NULL) {
1399 		xo_warn("memstat_mtl_alloc");
1400 		return;
1401 	}
1402 	if (kd == NULL) {
1403 		if (memstat_sysctl_uma(mtlp, 0) < 0) {
1404 			xo_warnx("memstat_sysctl_uma: %s",
1405 			    memstat_strerror(memstat_mtl_geterror(mtlp)));
1406 			return;
1407 		}
1408 	} else {
1409 		if (memstat_kvm_uma(mtlp, kd) < 0) {
1410 			error = memstat_mtl_geterror(mtlp);
1411 			if (error == MEMSTAT_ERROR_KVM)
1412 				xo_warnx("memstat_kvm_uma: %s",
1413 				    kvm_geterr(kd));
1414 			else
1415 				xo_warnx("memstat_kvm_uma: %s",
1416 				    memstat_strerror(error));
1417 		}
1418 	}
1419 	xo_open_container("memory-zone-statistics");
1420 	xo_emit("{T:/%-19s} {T:/%7s} {T:/%7s} {T:/%8s} {T:/%8s} {T:/%8s} "
1421 	    "{T:/%4s} {T:/%4s} {T:/%4s}\n", "ITEM", "SIZE",
1422 	    "LIMIT", "USED", "FREE", "REQ", "FAIL", "SLEEP", "XDOM");
1423 	xo_open_list("zone");
1424 	for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
1425 	    mtp = memstat_mtl_next(mtp)) {
1426 		len = strlen(memstat_get_name(mtp));
1427 		xo_open_instance("zone");
1428 		xo_emit("{k:name/%s}:{d:size/%*ju}{e:size/%ju},"
1429 		    "{:limit/%7ju},{:used/%8ju},"
1430 		    "{:free/%8ju},{:requests/%8ju},"
1431 		    "{:fail/%4ju},{:sleep/%4ju},{:xdomain/%4ju}\n",
1432 		    memstat_get_name(mtp), MAX(1, 26 - len),
1433 		    (uintmax_t)memstat_get_size(mtp),
1434 		    (uintmax_t)memstat_get_size(mtp),
1435 		    (uintmax_t)memstat_get_countlimit(mtp),
1436 		    (uintmax_t)memstat_get_count(mtp),
1437 		    (uintmax_t)memstat_get_free(mtp),
1438 		    (uintmax_t)memstat_get_numallocs(mtp),
1439 		    (uintmax_t)memstat_get_failures(mtp),
1440 		    (uintmax_t)memstat_get_sleeps(mtp),
1441 		    (uintmax_t)memstat_get_xdomain(mtp));
1442 		xo_close_instance("zone");
1443 	}
1444 	memstat_mtl_free(mtlp);
1445 	xo_close_list("zone");
1446 	xo_close_container("memory-zone-statistics");
1447 }
1448 
1449 static void
1450 display_object(struct kinfo_vmobject *kvo)
1451 {
1452 	const char *str;
1453 
1454 	xo_open_instance("object");
1455 	xo_emit("{:resident/%5ju} ", (uintmax_t)kvo->kvo_resident);
1456 	xo_emit("{:active/%5ju} ", (uintmax_t)kvo->kvo_active);
1457 	xo_emit("{:inactive/%5ju} ", (uintmax_t)kvo->kvo_inactive);
1458 	xo_emit("{:refcount/%3d} ", kvo->kvo_ref_count);
1459 	xo_emit("{:shadowcount/%3d} ", kvo->kvo_shadow_count);
1460 
1461 #define	MEMATTR_STR(type, val)					\
1462 	if (kvo->kvo_memattr == (type)) {			\
1463 		str = (val);					\
1464 	} else
1465 #ifdef VM_MEMATTR_UNCACHEABLE
1466 	MEMATTR_STR(VM_MEMATTR_UNCACHEABLE, "UC")
1467 #endif
1468 #ifdef VM_MEMATTR_WRITE_COMBINING
1469 	MEMATTR_STR(VM_MEMATTR_WRITE_COMBINING, "WC")
1470 #endif
1471 #ifdef VM_MEMATTR_WRITE_THROUGH
1472 	MEMATTR_STR(VM_MEMATTR_WRITE_THROUGH, "WT")
1473 #endif
1474 #ifdef VM_MEMATTR_WRITE_PROTECTED
1475 	MEMATTR_STR(VM_MEMATTR_WRITE_PROTECTED, "WP")
1476 #endif
1477 #ifdef VM_MEMATTR_WRITE_BACK
1478 	MEMATTR_STR(VM_MEMATTR_WRITE_BACK, "WB")
1479 #endif
1480 #ifdef VM_MEMATTR_WEAK_UNCACHEABLE
1481 	MEMATTR_STR(VM_MEMATTR_WEAK_UNCACHEABLE, "UC-")
1482 #endif
1483 #ifdef VM_MEMATTR_WB_WA
1484 	MEMATTR_STR(VM_MEMATTR_WB_WA, "WB")
1485 #endif
1486 #ifdef VM_MEMATTR_NOCACHE
1487 	MEMATTR_STR(VM_MEMATTR_NOCACHE, "NC")
1488 #endif
1489 #ifdef VM_MEMATTR_DEVICE
1490 	MEMATTR_STR(VM_MEMATTR_DEVICE, "DEV")
1491 #endif
1492 #ifdef VM_MEMATTR_DEVICE_NP
1493 	MEMATTR_STR(VM_MEMATTR_DEVICE, "NP")
1494 #endif
1495 #ifdef VM_MEMATTR_CACHEABLE
1496 	MEMATTR_STR(VM_MEMATTR_CACHEABLE, "C")
1497 #endif
1498 #ifdef VM_MEMATTR_PREFETCHABLE
1499 	MEMATTR_STR(VM_MEMATTR_PREFETCHABLE, "PRE")
1500 #endif
1501 	{
1502 		str = "??";
1503 	}
1504 #undef MEMATTR_STR
1505 	xo_emit("{:attribute/%-3s} ", str);
1506 	switch (kvo->kvo_type) {
1507 	case KVME_TYPE_NONE:
1508 		str = "--";
1509 		break;
1510 	case KVME_TYPE_DEFAULT:
1511 		str = "df";
1512 		break;
1513 	case KVME_TYPE_VNODE:
1514 		str = "vn";
1515 		break;
1516 	case KVME_TYPE_SWAP:
1517 		str = "sw";
1518 		break;
1519 	case KVME_TYPE_DEVICE:
1520 		str = "dv";
1521 		break;
1522 	case KVME_TYPE_PHYS:
1523 		str = "ph";
1524 		break;
1525 	case KVME_TYPE_DEAD:
1526 		str = "dd";
1527 		break;
1528 	case KVME_TYPE_SG:
1529 		str = "sg";
1530 		break;
1531 	case KVME_TYPE_MGTDEVICE:
1532 		str = "md";
1533 		break;
1534 	case KVME_TYPE_UNKNOWN:
1535 	default:
1536 		str = "??";
1537 		break;
1538 	}
1539 	xo_emit("{:type/%-2s} ", str);
1540 	xo_emit("{:path/%-s}\n", kvo->kvo_path);
1541 	xo_close_instance("object");
1542 }
1543 
1544 static void
1545 doobjstat(void)
1546 {
1547 	struct kinfo_vmobject *kvo;
1548 	int cnt, i;
1549 
1550 	kvo = kinfo_getvmobject(&cnt);
1551 	if (kvo == NULL) {
1552 		xo_warn("Failed to fetch VM object list");
1553 		return;
1554 	}
1555 	xo_emit("{T:RES/%5s} {T:ACT/%5s} {T:INACT/%5s} {T:REF/%3s} {T:SHD/%3s} "
1556 	    "{T:CM/%3s} {T:TP/%2s} {T:PATH/%s}\n");
1557 	xo_open_list("object");
1558 	for (i = 0; i < cnt; i++)
1559 		display_object(&kvo[i]);
1560 	free(kvo);
1561 	xo_close_list("object");
1562 }
1563 
1564 /*
1565  * kread reads something from the kernel, given its nlist index.
1566  */
1567 static void
1568 kreado(int nlx, void *addr, size_t size, size_t offset)
1569 {
1570 	const char *sym;
1571 
1572 	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
1573 		sym = namelist[nlx].n_name;
1574 		if (*sym == '_')
1575 			++sym;
1576 		xo_errx(1, "symbol %s not defined", sym);
1577 	}
1578 	if ((size_t)kvm_read(kd, namelist[nlx].n_value + offset, addr,
1579 	    size) != size) {
1580 		sym = namelist[nlx].n_name;
1581 		if (*sym == '_')
1582 			++sym;
1583 		xo_errx(1, "%s: %s", sym, kvm_geterr(kd));
1584 	}
1585 }
1586 
1587 static void
1588 kread(int nlx, void *addr, size_t size)
1589 {
1590 
1591 	kreado(nlx, addr, size, 0);
1592 }
1593 
1594 static void
1595 kreadptr(uintptr_t addr, void *buf, size_t size)
1596 {
1597 
1598 	if ((size_t)kvm_read(kd, addr, buf, size) != size)
1599 		xo_errx(1, "%s", kvm_geterr(kd));
1600 }
1601 
1602 static void __dead2
1603 usage(void)
1604 {
1605 	xo_error("%s%s",
1606 	    "usage: vmstat [-afHhimoPsz] [-M core [-N system]] [-c count] [-n devs]\n",
1607 	    "              [-p type,if,pass] [-w wait] [disks] [wait [count]]\n");
1608 	xo_finish();
1609 	exit(1);
1610 }
1611