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