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