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