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