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