1 /* $NetBSD: vmstat.c,v 1.265 2026/03/22 11:25:12 yamt Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000, 2001, 2007, 2019, 2020 5 * The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation by: 9 * - Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * - Simon Burge and Luke Mewburn of Wasabi Systems, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1980, 1986, 1991, 1993 37 * The Regents of the University of California. All rights reserved. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 */ 63 64 #include <sys/cdefs.h> 65 #ifndef lint 66 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\ 67 The Regents of the University of California. All rights reserved."); 68 #endif /* not lint */ 69 70 #ifndef lint 71 #if 0 72 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95"; 73 #else 74 __RCSID("$NetBSD: vmstat.c,v 1.265 2026/03/22 11:25:12 yamt Exp $"); 75 #endif 76 #endif /* not lint */ 77 78 #define __POOL_EXPOSE 79 #define __NAMECACHE_PRIVATE 80 81 #include <sys/param.h> 82 #include <sys/types.h> 83 #include <sys/mount.h> 84 #include <sys/uio.h> 85 86 #include <sys/buf.h> 87 #include <sys/evcnt.h> 88 #include <sys/ioctl.h> 89 #include <sys/malloc.h> 90 #include <sys/mallocvar.h> 91 #include <sys/namei.h> 92 #include <sys/pool.h> 93 #include <sys/proc.h> 94 #include <sys/sched.h> 95 #include <sys/socket.h> 96 #include <sys/sysctl.h> 97 #include <sys/time.h> 98 #include <sys/queue.h> 99 #include <sys/kernhist.h> 100 #include <sys/vnode.h> 101 #include <sys/vnode_impl.h> 102 #include <sys/uidinfo.h> 103 104 #include <uvm/uvm_extern.h> 105 #include <uvm/uvm_stat.h> 106 107 #include <net/if.h> 108 #include <netinet/in.h> 109 #include <netinet/in_var.h> 110 111 #include <ufs/ufs/inode.h> 112 113 #include <nfs/rpcv2.h> 114 #include <nfs/nfsproto.h> 115 #include <nfs/nfsnode.h> 116 117 #include <assert.h> 118 #include <ctype.h> 119 #include <err.h> 120 #include <errno.h> 121 #include <fcntl.h> 122 #include <kvm.h> 123 #include <limits.h> 124 #include <nlist.h> 125 #undef n_hash 126 #include <paths.h> 127 #include <signal.h> 128 #include <stdio.h> 129 #include <stddef.h> 130 #include <stdlib.h> 131 #include <string.h> 132 #include <time.h> 133 #include <unistd.h> 134 #include <util.h> 135 136 #include "drvstats.h" 137 138 /* 139 * All this mess will go away once everything is converted. 140 */ 141 #ifdef __HAVE_CPU_DATA_FIRST 142 143 # include <sys/cpu_data.h> 144 struct cpu_info { 145 struct cpu_data ci_data; 146 }; 147 #else 148 # include <sys/cpu.h> 149 #endif 150 151 /* 152 * General namelist 153 */ 154 struct nlist namelist[] = 155 { 156 #define X_HZ 0 157 { .n_name = "_hz" }, 158 #define X_STATHZ 1 159 { .n_name = "_stathz" }, 160 #define X_NCHSTATS 2 161 { .n_name = "_nchstats" }, 162 #define X_ALLEVENTS 3 163 { .n_name = "_allevents" }, 164 #define X_POOLHEAD 4 165 { .n_name = "_pool_head" }, 166 #define X_UVMEXP 5 167 { .n_name = "_uvmexp" }, 168 #define X_CPU_INFOS 6 169 { .n_name = "_cpu_infos" }, 170 #define X_NL_SIZE 7 171 { .n_name = NULL }, 172 }; 173 174 /* 175 * Namelist for time data. 176 */ 177 struct nlist timenl[] = 178 { 179 #define X_TIMEBASEBIN 0 180 { .n_name = "_timebasebin" }, 181 #define X_TIME_SECOND 1 182 { .n_name = "_time_second" }, 183 #define X_TIME 2 184 { .n_name = "_time" }, 185 #define X_TIMENL_SIZE 3 186 { .n_name = NULL }, 187 }; 188 189 /* 190 * Namelist for pre-evcnt interrupt counters. 191 */ 192 struct nlist intrnl[] = 193 { 194 #define X_INTRNAMES 0 195 { .n_name = "_intrnames" }, 196 #define X_EINTRNAMES 1 197 { .n_name = "_eintrnames" }, 198 #define X_INTRCNT 2 199 { .n_name = "_intrcnt" }, 200 #define X_EINTRCNT 3 201 { .n_name = "_eintrcnt" }, 202 #define X_INTRNL_SIZE 4 203 { .n_name = NULL }, 204 }; 205 206 207 /* 208 * Namelist for hash statistics 209 */ 210 struct nlist hashnl[] = 211 { 212 #define X_BUFHASH 0 213 { .n_name = "_bufhash" }, 214 #define X_BUFHASHTBL 1 215 { .n_name = "_bufhashtbl" }, 216 #define X_UIHASH 2 217 { .n_name = "_uihash" }, 218 #define X_UIHASHTBL 3 219 { .n_name = "_uihashtbl" }, 220 #define X_IFADDRHASH 4 221 { .n_name = "_in_ifaddrhash" }, 222 #define X_IFADDRHASHTBL 5 223 { .n_name = "_in_ifaddrhashtbl" }, 224 #define X_VCACHEHASH 6 225 { .n_name = "_vcache_hashmask" }, 226 #define X_VCACHETBL 7 227 { .n_name = "_vcache_hashtab" }, 228 #define X_HASHNL_SIZE 8 /* must be last */ 229 { .n_name = NULL }, 230 }; 231 232 /* 233 * Namelist for kernel histories 234 */ 235 struct nlist histnl[] = 236 { 237 { .n_name = "_kern_histories" }, 238 #define X_KERN_HISTORIES 0 239 { .n_name = NULL }, 240 }; 241 242 243 #define KILO 1024 244 245 struct cpu_counter { 246 uint64_t nintr; 247 uint64_t nsyscall; 248 uint64_t nswtch; 249 uint64_t nfault; 250 uint64_t ntrap; 251 uint64_t nsoft; 252 } cpucounter, ocpucounter; 253 254 struct uvmexp_sysctl uvmexp, ouvmexp; 255 int ndrives; 256 257 int winlines = 20; 258 259 kvm_t *kd; 260 261 262 #define FORKSTAT 0x001 263 #define INTRSTAT 0x002 264 #define MEMSTAT 0x004 265 #define SUMSTAT 0x008 266 #define EVCNTSTAT 0x010 267 #define VMSTAT 0x020 268 #define HISTLIST 0x040 269 #define HISTDUMP 0x080 270 #define HASHSTAT 0x100 271 #define HASHLIST 0x200 272 #define VMTOTAL 0x400 273 #define POOLCACHESTAT 0x800 274 275 /* 276 * Print single word. `ovflow' is number of characters didn't fit 277 * on the last word. `fmt' is a format string to print this word. 278 * It must contain asterisk for field width. `width' is a width 279 * occupied by this word. `fixed' is a number of constant chars in 280 * `fmt'. `val' is a value to be printed using format string `fmt'. 281 */ 282 #define PRWORD(ovflw, fmt, width, fixed, val) do { \ 283 (ovflw) += printf((fmt), \ 284 (width) - (fixed) - (ovflw) > 0 ? \ 285 (width) - (fixed) - (ovflw) : 0, \ 286 (val)) - (width); \ 287 if ((ovflw) < 0) \ 288 (ovflw) = 0; \ 289 } while (0) 290 291 void cpustats(int *); 292 void cpucounters(struct cpu_counter *); 293 void deref_kptr(const void *, void *, size_t, const char *); 294 void drvstats(int *); 295 void doevcnt(int verbose, int type); 296 void dohashstat(int, int, const char *); 297 void dohashstat_sysctl(int, int, const char *); 298 void dointr(int verbose); 299 void dopool(int, int); 300 void dopoolcache(int); 301 void dosum(void); 302 void dovmstat(struct timespec *, int); 303 void print_total_hdr(void); 304 void dovmtotal(struct timespec *, int); 305 void kread(struct nlist *, int, void *, size_t); 306 int kreadc(struct nlist *, int, void *, size_t); 307 void needhdr(int); 308 void getnlist(int); 309 long getuptime(void); 310 void printhdr(void); 311 long pct(u_long, u_long); 312 __dead static void usage(void); 313 void doforkst(void); 314 315 void hist_traverse(int, const char *); 316 void hist_dodump(struct kern_history *); 317 void hist_traverse_sysctl(int, const char *); 318 void hist_dodump_sysctl(int[], unsigned int); 319 320 char **choosedrives(char **); 321 322 /* Namelist and memory file names. */ 323 char *nlistf, *memf; 324 325 /* allow old usage [vmstat 1] */ 326 #define BACKWARD_COMPATIBILITY 327 328 static const int clockrate_mib[] = { CTL_KERN, KERN_CLOCKRATE }; 329 static const int vmmeter_mib[] = { CTL_VM, VM_METER }; 330 static const int uvmexp2_mib[] = { CTL_VM, VM_UVMEXP2 }; 331 static const int boottime_mib[] = { CTL_KERN, KERN_BOOTTIME }; 332 333 static int numdisks = 2; 334 335 int 336 main(int argc, char *argv[]) 337 { 338 int c, todo, verbose, wide; 339 struct timespec interval; 340 int reps; 341 const char *histname, *hashname; 342 char errbuf[_POSIX2_LINE_MAX]; 343 344 histname = hashname = NULL; 345 memf = nlistf = NULL; 346 reps = todo = verbose = wide = 0; 347 interval.tv_sec = 0; 348 interval.tv_nsec = 0; 349 while ((c = getopt(argc, argv, "Cc:efh:HilLM:mN:n:stu:UvWw:")) != -1) { 350 switch (c) { 351 case 'c': 352 reps = atoi(optarg); 353 break; 354 case 'C': 355 todo |= POOLCACHESTAT; 356 break; 357 case 'e': 358 todo |= EVCNTSTAT; 359 break; 360 case 'f': 361 todo |= FORKSTAT; 362 break; 363 case 'h': 364 hashname = optarg; 365 /* FALLTHROUGH */ 366 case 'H': 367 todo |= HASHSTAT; 368 break; 369 case 'i': 370 todo |= INTRSTAT; 371 break; 372 case 'l': 373 todo |= HISTLIST; 374 break; 375 case 'L': 376 todo |= HASHLIST; 377 break; 378 case 'M': 379 memf = optarg; 380 break; 381 case 'm': 382 todo |= MEMSTAT; 383 break; 384 case 'N': 385 nlistf = optarg; 386 break; 387 case 'n': 388 numdisks = atoi(optarg); 389 break; 390 case 's': 391 todo |= SUMSTAT; 392 break; 393 case 't': 394 todo |= VMTOTAL; 395 break; 396 case 'u': 397 histname = optarg; 398 /* FALLTHROUGH */ 399 case 'U': 400 todo |= HISTDUMP; 401 break; 402 case 'v': 403 verbose++; 404 break; 405 case 'W': 406 wide++; 407 break; 408 case 'w': 409 interval.tv_sec = atol(optarg); 410 break; 411 case '?': 412 default: 413 usage(); 414 } 415 } 416 argc -= optind; 417 argv += optind; 418 419 if (todo == 0) 420 todo = VMSTAT; 421 422 if (memf == NULL) { 423 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); 424 } else { 425 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 426 } 427 428 if (kd == NULL) 429 errx(EXIT_FAILURE, "%s", errbuf); 430 431 if (memf != NULL) 432 getnlist(todo); /* Only need this if a core is specified. */ 433 434 if (todo & VMSTAT) { 435 struct winsize winsize; 436 437 (void)drvinit(0);/* Initialize disk stats, no disks selected. */ 438 439 argv = choosedrives(argv); /* Select disks. */ 440 winsize.ws_row = 0; 441 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize); 442 if (winsize.ws_row > 0) 443 winlines = winsize.ws_row; 444 445 } 446 447 #ifdef BACKWARD_COMPATIBILITY 448 if (*argv) { 449 interval.tv_sec = atol(*argv); 450 if (*++argv) 451 reps = atoi(*argv); 452 } 453 #endif 454 455 if (interval.tv_sec) { 456 if (!reps) 457 reps = -1; 458 } else if (reps) 459 interval.tv_sec = 1; 460 461 /* 462 * Statistics dumping is incompatible with the default 463 * VMSTAT/dovmstat() output. So perform the interval/reps handling 464 * for it here. 465 */ 466 if ((todo & (VMSTAT|VMTOTAL)) == 0) { 467 for (;;) { 468 if (todo & (HISTLIST|HISTDUMP)) { 469 if ((todo & (HISTLIST|HISTDUMP)) == 470 (HISTLIST|HISTDUMP)) 471 errx(1, "you may list or dump," 472 " but not both!"); 473 if (memf != NULL) 474 hist_traverse(todo, histname); 475 else 476 hist_traverse_sysctl(todo, histname); 477 (void)putchar('\n'); 478 } 479 if (todo & FORKSTAT) { 480 doforkst(); 481 (void)putchar('\n'); 482 } 483 if (todo & MEMSTAT) { 484 dopool(verbose, wide); 485 (void)putchar('\n'); 486 } 487 if (todo & POOLCACHESTAT) { 488 dopoolcache(verbose); 489 (void)putchar('\n'); 490 } 491 if (todo & SUMSTAT) { 492 dosum(); 493 (void)putchar('\n'); 494 } 495 if (todo & INTRSTAT) { 496 dointr(verbose); 497 (void)putchar('\n'); 498 } 499 if (todo & EVCNTSTAT) { 500 doevcnt(verbose, EVCNT_TYPE_ANY); 501 (void)putchar('\n'); 502 } 503 if (todo & (HASHLIST|HASHSTAT)) { 504 if ((todo & (HASHLIST|HASHSTAT)) == 505 (HASHLIST|HASHSTAT)) 506 errx(1, "you may list or display," 507 " but not both!"); 508 dohashstat(verbose, todo, hashname); 509 (void)putchar('\n'); 510 } 511 512 fflush(stdout); 513 if (reps >= 0 && --reps <=0) 514 break; 515 (void)nanosleep(&interval, NULL); 516 } 517 } else { 518 if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) { 519 errx(1, "you may not both do vmstat and vmtotal"); 520 } 521 if (todo & VMSTAT) 522 dovmstat(&interval, reps); 523 if (todo & VMTOTAL) 524 dovmtotal(&interval, reps); 525 } 526 return 0; 527 } 528 529 void 530 getnlist(int todo) 531 { 532 static int done = 0; 533 int c; 534 size_t i; 535 536 if ((c = kvm_nlist(kd, namelist)) != 0) { 537 int doexit = 0; 538 if (c == -1) 539 errx(1, "kvm_nlist: %s %s", 540 "namelist", kvm_geterr(kd)); 541 for (i = 0; i < __arraycount(namelist)-1; i++) 542 if (namelist[i].n_type == 0) { 543 if (doexit++ == 0) 544 (void)fprintf(stderr, 545 "%s: undefined symbols:", 546 getprogname()); 547 (void)fprintf(stderr, " %s", 548 namelist[i].n_name); 549 } 550 if (doexit) { 551 (void)fputc('\n', stderr); 552 exit(1); 553 } 554 } 555 556 if ((todo & (VMSTAT|INTRSTAT)) && !(done & (VMSTAT))) { 557 done |= VMSTAT; 558 if ((c = kvm_nlist(kd, timenl)) == -1 || c == X_TIMENL_SIZE) 559 errx(1, "kvm_nlist: %s %s", "timenl", kvm_geterr(kd)); 560 } 561 if ((todo & (SUMSTAT|INTRSTAT)) && !(done & (SUMSTAT|INTRSTAT))) { 562 done |= SUMSTAT|INTRSTAT; 563 (void) kvm_nlist(kd, intrnl); 564 } 565 if ((todo & (HASHLIST|HASHSTAT)) && !(done & (HASHLIST|HASHSTAT))) { 566 done |= HASHLIST|HASHSTAT; 567 if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE) 568 errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd)); 569 } 570 if ((todo & (HISTLIST|HISTDUMP)) && !(done & (HISTLIST|HISTDUMP))) { 571 done |= HISTLIST|HISTDUMP; 572 if (kvm_nlist(kd, histnl) == -1) 573 errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd)); 574 } 575 } 576 577 char ** 578 choosedrives(char **argv) 579 { 580 size_t i, j, k; 581 582 /* 583 * Choose drives to be displayed. Priority goes to (in order) drives 584 * supplied as arguments, default drives. If everything isn't filled 585 * in and there are drives not taken care of, display the first few 586 * that fit. 587 */ 588 #define BACKWARD_COMPATIBILITY 589 for (ndrives = 0; *argv; ++argv) { 590 #ifdef BACKWARD_COMPATIBILITY 591 if (isdigit((unsigned char)**argv)) 592 break; 593 #endif 594 for (i = 0; i < ndrive; i++) { 595 if (strcmp(dr_name[i], *argv)) 596 continue; 597 drv_select[i] = 1; 598 ++ndrives; 599 break; 600 } 601 } 602 603 /* 604 * Pick the most active drives. Must read the stats once before 605 * sorting so that there is current IO data, before selecting 606 * just the first 'numdisks' (default 2) drives. 607 */ 608 drvreadstats(); 609 for (i = 0; i < ndrive && ndrives < numdisks; i++) { 610 uint64_t high_bytes = 0, bytes; 611 612 k = ndrive; 613 for (j = 0; j < ndrive; j++) { 614 if (drv_select[j]) 615 continue; 616 bytes = cur.rbytes[j] + cur.wbytes[j]; 617 if (bytes > high_bytes) { 618 high_bytes = bytes; 619 k = j; 620 } 621 } 622 if (k != ndrive) { 623 drv_select[k] = 1; 624 ++ndrives; 625 } 626 } 627 628 return (argv); 629 } 630 631 long 632 getuptime(void) 633 { 634 static struct timespec boottime; 635 struct timespec now; 636 time_t uptime, nowsec; 637 638 if (memf == NULL) { 639 if (boottime.tv_sec == 0) { 640 size_t buflen = sizeof(boottime); 641 if (sysctl(boottime_mib, __arraycount(boottime_mib), 642 &boottime, &buflen, NULL, 0) == -1) 643 warn("Can't get boottime"); 644 } 645 clock_gettime(CLOCK_REALTIME, &now); 646 } else { 647 if (boottime.tv_sec == 0) { 648 struct bintime bt; 649 650 kread(timenl, X_TIMEBASEBIN, &bt, sizeof(bt)); 651 bintime2timespec(&bt, &boottime); 652 } 653 if (kreadc(timenl, X_TIME_SECOND, &nowsec, sizeof(nowsec))) { 654 /* 655 * XXX this assignment dance can be removed once 656 * timeval tv_sec is SUS mandated time_t 657 */ 658 now.tv_sec = nowsec; 659 now.tv_nsec = 0; 660 } else { 661 kread(timenl, X_TIME, &now, sizeof(now)); 662 } 663 } 664 uptime = now.tv_sec - boottime.tv_sec; 665 if (uptime <= 0 || uptime > 60*60*24*365*10) 666 errx(1, "time makes no sense; namelist must be wrong."); 667 return (uptime); 668 } 669 670 int hz, hdrcnt; 671 672 void 673 print_total_hdr(void) 674 { 675 676 (void)printf("procs memory\n"); 677 (void)printf("ru dw pw sl"); 678 (void)printf(" total-v active-v active-r"); 679 (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n"); 680 hdrcnt = winlines - 2; 681 } 682 683 void 684 dovmtotal(struct timespec *interval, int reps) 685 { 686 struct vmtotal total; 687 size_t size; 688 689 (void)signal(SIGCONT, needhdr); 690 691 for (hdrcnt = 1;;) { 692 if (!--hdrcnt) 693 print_total_hdr(); 694 if (memf != NULL) { 695 warnx("Unable to get vmtotals from crash dump."); 696 (void)memset(&total, 0, sizeof(total)); 697 } else { 698 size = sizeof(total); 699 if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib), 700 &total, &size, NULL, 0) == -1) { 701 warn("Can't get vmtotals"); 702 (void)memset(&total, 0, sizeof(total)); 703 } 704 } 705 (void)printf("%2d ", total.t_rq); 706 (void)printf("%2d ", total.t_dw); 707 (void)printf("%2d ", total.t_pw); 708 (void)printf("%2d ", total.t_sl); 709 710 (void)printf("%9d ", total.t_vm); 711 (void)printf("%9d ", total.t_avm); 712 (void)printf("%9d ", total.t_arm); 713 (void)printf("%5d ", total.t_vmshr); 714 (void)printf("%6d ", total.t_avmshr); 715 (void)printf("%5d ", total.t_rmshr); 716 (void)printf("%6d ", total.t_armshr); 717 (void)printf("%5d", total.t_free); 718 719 (void)putchar('\n'); 720 721 (void)fflush(stdout); 722 if (reps >= 0 && --reps <= 0) 723 break; 724 725 (void)nanosleep(interval, NULL); 726 } 727 } 728 729 void 730 dovmstat(struct timespec *interval, int reps) 731 { 732 struct vmtotal total; 733 time_t uptime, halfuptime; 734 size_t size; 735 int pagesize = getpagesize(); 736 int ovflw; 737 738 uptime = getuptime(); 739 halfuptime = uptime / 2; 740 (void)signal(SIGCONT, needhdr); 741 742 if (memf != NULL) { 743 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) 744 kread(namelist, X_STATHZ, &hz, sizeof(hz)); 745 if (!hz) 746 kread(namelist, X_HZ, &hz, sizeof(hz)); 747 } else { 748 struct clockinfo clockinfo; 749 size = sizeof(clockinfo); 750 if (sysctl(clockrate_mib, 2, &clockinfo, &size, NULL, 0) == -1) 751 err(1, "sysctl kern.clockrate failed"); 752 hz = clockinfo.stathz; 753 if (!hz) 754 hz = clockinfo.hz; 755 } 756 757 for (hdrcnt = 1;;) { 758 if (!--hdrcnt) 759 printhdr(); 760 /* Read new disk statistics */ 761 cpureadstats(); 762 drvreadstats(); 763 tkreadstats(); 764 if (memf != NULL) { 765 struct uvmexp uvmexp_kernel; 766 /* 767 * XXX Can't do this if we're reading a crash 768 * XXX dump because they're lazily-calculated. 769 */ 770 warnx("Unable to get vmtotals from crash dump."); 771 (void)memset(&total, 0, sizeof(total)); 772 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel)); 773 #define COPY(field) uvmexp.field = uvmexp_kernel.field 774 COPY(pdreact); 775 COPY(pageins); 776 COPY(pgswapout); 777 COPY(pdfreed); 778 COPY(pdscans); 779 #undef COPY 780 } else { 781 size = sizeof(total); 782 if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib), 783 &total, &size, NULL, 0) == -1) { 784 warn("Can't get vmtotals"); 785 (void)memset(&total, 0, sizeof(total)); 786 } 787 size = sizeof(uvmexp); 788 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp, 789 &size, NULL, 0) == -1) 790 warn("sysctl vm.uvmexp2 failed"); 791 } 792 cpucounters(&cpucounter); 793 ovflw = 0; 794 PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1); 795 PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw); 796 #define pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10)) 797 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */ 798 PRWORD(ovflw, " %*ld", 9, 1, pgtok(total.t_avm)); 799 PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free)); 800 PRWORD(ovflw, " %*ld", 5, 1, 801 rate(cpucounter.nfault - ocpucounter.nfault)); 802 PRWORD(ovflw, " %*ld", 4, 1, 803 rate(uvmexp.pdreact - ouvmexp.pdreact)); 804 PRWORD(ovflw, " %*ld", 4, 1, 805 rate(uvmexp.pageins - ouvmexp.pageins)); 806 PRWORD(ovflw, " %*ld", 5, 1, 807 rate(uvmexp.pgswapout - ouvmexp.pgswapout)); 808 PRWORD(ovflw, " %*ld", 5, 1, 809 rate(uvmexp.pdfreed - ouvmexp.pdfreed)); 810 PRWORD(ovflw, " %*ld", 6, 2, 811 rate(uvmexp.pdscans - ouvmexp.pdscans)); 812 drvstats(&ovflw); 813 PRWORD(ovflw, " %*ld", 5, 1, 814 rate(cpucounter.nintr - ocpucounter.nintr)); 815 PRWORD(ovflw, " %*ld", 5, 1, 816 rate(cpucounter.nsyscall - ocpucounter.nsyscall)); 817 PRWORD(ovflw, " %*ld", 4, 1, 818 rate(cpucounter.nswtch - ocpucounter.nswtch)); 819 cpustats(&ovflw); 820 (void)putchar('\n'); 821 (void)fflush(stdout); 822 if (reps >= 0 && --reps <= 0) 823 break; 824 ouvmexp = uvmexp; 825 ocpucounter = cpucounter; 826 uptime = interval->tv_sec; 827 /* 828 * We round upward to avoid losing low-frequency events 829 * (i.e., >= 1 per interval but < 1 per second). 830 */ 831 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2; 832 (void)nanosleep(interval, NULL); 833 } 834 } 835 836 void 837 printhdr(void) 838 { 839 size_t i; 840 841 (void)printf(" procs memory page%*s", 23, ""); 842 if (ndrives > 0) 843 (void)printf("%s %*sfaults cpu\n", 844 ((ndrives > 1) ? "disks" : "disk"), 845 ((ndrives > 1) ? ndrives * 3 - 4 : 0), ""); 846 else 847 (void)printf("%*s faults cpu\n", 848 ndrives * 3, ""); 849 850 (void)printf(" r b avm fre flt re pi po fr sr "); 851 for (i = 0; i < ndrive; i++) 852 if (drv_select[i]) 853 (void)printf("%c%c ", dr_name[i][0], 854 dr_name[i][strlen(dr_name[i]) - 1]); 855 (void)printf(" in sy cs us sy id\n"); 856 hdrcnt = winlines - 2; 857 } 858 859 /* 860 * Force a header to be prepended to the next output. 861 */ 862 void 863 /*ARGSUSED*/ 864 needhdr(int dummy) 865 { 866 867 hdrcnt = 1; 868 } 869 870 long 871 pct(u_long top, u_long bot) 872 { 873 long ans; 874 875 if (bot == 0) 876 return (0); 877 ans = (long)((quad_t)top * 100 / bot); 878 return (ans); 879 } 880 881 #define PCT(top, bot) (int)pct((u_long)(top), (u_long)(bot)) 882 883 void 884 dosum(void) 885 { 886 struct nchstats nch_stats; 887 uint64_t nchtotal; 888 size_t ssize; 889 int active_kernel; 890 struct cpu_counter cc; 891 892 /* 893 * The "active" and "inactive" variables 894 * are now estimated by the kernel and sadly 895 * can not easily be dug out of a crash dump. 896 */ 897 ssize = sizeof(uvmexp); 898 memset(&uvmexp, 0, ssize); 899 active_kernel = (memf == NULL); 900 if (active_kernel) { 901 /* only on active kernel */ 902 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp, 903 &ssize, NULL, 0) == -1) 904 warn("sysctl vm.uvmexp2 failed"); 905 } else { 906 struct uvmexp uvmexp_kernel; 907 struct pool pool, *pp = &pool; 908 struct pool_allocator pa; 909 TAILQ_HEAD(,pool) pool_head; 910 void *addr; 911 uint64_t bytes; 912 913 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel)); 914 #define COPY(field) uvmexp.field = uvmexp_kernel.field 915 COPY(pagesize); 916 COPY(ncolors); 917 COPY(npages); 918 COPY(free); 919 COPY(paging); 920 COPY(wired); 921 COPY(reserve_pagedaemon); 922 COPY(reserve_kernel); 923 COPY(anonpages); 924 COPY(filepages); 925 COPY(execpages); 926 COPY(freemin); 927 COPY(freetarg); 928 COPY(wiredmax); 929 COPY(nswapdev); 930 COPY(swpages); 931 COPY(swpginuse); 932 COPY(nswget); 933 COPY(pageins); 934 COPY(pdpageouts); 935 COPY(pgswapin); 936 COPY(pgswapout); 937 COPY(forks); 938 COPY(forks_ppwait); 939 COPY(forks_sharevm); 940 COPY(colorhit); 941 COPY(colormiss); 942 COPY(cpuhit); 943 COPY(cpumiss); 944 COPY(fltnoram); 945 COPY(fltnoanon); 946 COPY(fltpgwait); 947 COPY(fltpgrele); 948 COPY(fltrelck); 949 COPY(fltrelckok); 950 COPY(fltanget); 951 COPY(fltanretry); 952 COPY(fltamcopy); 953 COPY(fltamcopy); 954 COPY(fltnomap); 955 COPY(fltlget); 956 COPY(fltget); 957 COPY(flt_anon); 958 COPY(flt_acow); 959 COPY(flt_obj); 960 COPY(flt_prcopy); 961 COPY(flt_przero); 962 COPY(pdwoke); 963 COPY(pdrevs); 964 COPY(pdfreed); 965 COPY(pdscans); 966 COPY(pdanscan); 967 COPY(pdobscan); 968 COPY(pdreact); 969 COPY(pdbusy); 970 COPY(pddeact); 971 COPY(bootpages); 972 #undef COPY 973 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head)); 974 addr = TAILQ_FIRST(&pool_head); 975 uvmexp.poolpages = 0; 976 for (; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist)) { 977 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed"); 978 deref_kptr(pp->pr_alloc, &pa, sizeof(pa), 979 "pool allocator trashed"); 980 bytes = pp->pr_npages * pa.pa_pagesz; 981 if ((pp->pr_roflags & PR_RECURSIVE) != 0) 982 bytes -= (pp->pr_nout * pp->pr_size); 983 uvmexp.poolpages += bytes / uvmexp.pagesize; 984 } 985 } 986 987 988 (void)printf("%9" PRIu64 " bytes per page\n", uvmexp.pagesize); 989 990 (void)printf("%9" PRIu64 " page color%s\n", 991 uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s"); 992 993 (void)printf("%9" PRIu64 " pages managed\n", uvmexp.npages); 994 (void)printf("%9" PRIu64 " pages free\n", uvmexp.free); 995 if (active_kernel) { 996 (void)printf("%9" PRIu64 " pages active\n", uvmexp.active); 997 (void)printf("%9" PRIu64 " pages inactive\n", uvmexp.inactive); 998 } 999 (void)printf("%9" PRIu64 " pages paging\n", uvmexp.paging); 1000 (void)printf("%9" PRIu64 " pages wired\n", uvmexp.wired); 1001 (void)printf("%9" PRIu64 " reserve pagedaemon pages\n", 1002 uvmexp.reserve_pagedaemon); 1003 (void)printf("%9" PRIu64 " reserve kernel pages\n", uvmexp.reserve_kernel); 1004 (void)printf("%9" PRIu64 " boot kernel pages\n", uvmexp.bootpages); 1005 (void)printf("%9" PRIu64 " kernel pool pages\n", uvmexp.poolpages); 1006 (void)printf("%9" PRIu64 " anonymous pages\n", uvmexp.anonpages); 1007 (void)printf("%9" PRIu64 " cached file pages\n", uvmexp.filepages); 1008 (void)printf("%9" PRIu64 " cached executable pages\n", uvmexp.execpages); 1009 1010 (void)printf("%9" PRIu64 " minimum free pages\n", uvmexp.freemin); 1011 (void)printf("%9" PRIu64 " target free pages\n", uvmexp.freetarg); 1012 (void)printf("%9" PRIu64 " maximum wired pages\n", uvmexp.wiredmax); 1013 1014 (void)printf("%9" PRIu64 " swap devices\n", uvmexp.nswapdev); 1015 (void)printf("%9" PRIu64 " swap pages\n", uvmexp.swpages); 1016 (void)printf("%9" PRIu64 " swap pages in use\n", uvmexp.swpginuse); 1017 (void)printf("%9" PRIu64 " swap allocations\n", uvmexp.nswget); 1018 1019 cpucounters(&cc); 1020 1021 (void)printf("%9" PRIu64 " total faults taken\n", cc.nfault); 1022 (void)printf("%9" PRIu64 " traps\n", cc.ntrap); 1023 (void)printf("%9" PRIu64 " device interrupts\n", cc.nintr); 1024 (void)printf("%9" PRIu64 " CPU context switches\n", cc.nswtch); 1025 (void)printf("%9" PRIu64 " software interrupts\n", cc.nsoft); 1026 (void)printf("%9" PRIu64 " system calls\n", cc.nsyscall); 1027 (void)printf("%9" PRIu64 " pagein requests\n", uvmexp.pageins); 1028 (void)printf("%9" PRIu64 " pageout requests\n", uvmexp.pdpageouts); 1029 (void)printf("%9" PRIu64 " pages swapped in\n", uvmexp.pgswapin); 1030 (void)printf("%9" PRIu64 " pages swapped out\n", uvmexp.pgswapout); 1031 (void)printf("%9" PRIu64 " forks total\n", uvmexp.forks); 1032 (void)printf("%9" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait); 1033 (void)printf("%9" PRIu64 " forks shared address space with parent\n", 1034 uvmexp.forks_sharevm); 1035 (void)printf("%9" PRIu64 " pagealloc desired color avail\n", 1036 uvmexp.colorhit); 1037 (void)printf("%9" PRIu64 " pagealloc desired color not avail\n", 1038 uvmexp.colormiss); 1039 (void)printf("%9" PRIu64 " pagealloc local cpu avail\n", 1040 uvmexp.cpuhit); 1041 (void)printf("%9" PRIu64 " pagealloc local cpu not avail\n", 1042 uvmexp.cpumiss); 1043 1044 (void)printf("%9" PRIu64 " faults with no memory\n", uvmexp.fltnoram); 1045 (void)printf("%9" PRIu64 " faults with no anons\n", uvmexp.fltnoanon); 1046 (void)printf("%9" PRIu64 " faults had to wait on pages\n", uvmexp.fltpgwait); 1047 (void)printf("%9" PRIu64 " faults found released page\n", uvmexp.fltpgrele); 1048 (void)printf("%9" PRIu64 " faults relock (%" PRIu64 " ok)\n", uvmexp.fltrelck, 1049 uvmexp.fltrelckok); 1050 (void)printf("%9" PRIu64 " anon page faults\n", uvmexp.fltanget); 1051 (void)printf("%9" PRIu64 " anon retry faults\n", uvmexp.fltanretry); 1052 (void)printf("%9" PRIu64 " amap copy faults\n", uvmexp.fltamcopy); 1053 (void)printf("%9" PRIu64 " neighbour anon page faults\n", uvmexp.fltnamap); 1054 (void)printf("%9" PRIu64 " neighbour object page faults\n", uvmexp.fltnomap); 1055 (void)printf("%9" PRIu64 " locked pager get faults\n", uvmexp.fltlget); 1056 (void)printf("%9" PRIu64 " unlocked pager get faults\n", uvmexp.fltget); 1057 (void)printf("%9" PRIu64 " anon faults\n", uvmexp.flt_anon); 1058 (void)printf("%9" PRIu64 " anon copy on write faults\n", uvmexp.flt_acow); 1059 (void)printf("%9" PRIu64 " object faults\n", uvmexp.flt_obj); 1060 (void)printf("%9" PRIu64 " promote copy faults\n", uvmexp.flt_prcopy); 1061 (void)printf("%9" PRIu64 " promote zero fill faults\n", uvmexp.flt_przero); 1062 (void)printf("%9" PRIu64 " faults upgraded lock\n", 1063 uvmexp.fltup); 1064 (void)printf("%9" PRIu64 " faults couldn't upgrade lock\n", 1065 uvmexp.fltnoup); 1066 1067 (void)printf("%9" PRIu64 " times daemon wokeup\n",uvmexp.pdwoke); 1068 (void)printf("%9" PRIu64 " revolutions of the clock hand\n", uvmexp.pdrevs); 1069 (void)printf("%9" PRIu64 " pages freed by daemon\n", uvmexp.pdfreed); 1070 (void)printf("%9" PRIu64 " pages scanned by daemon\n", uvmexp.pdscans); 1071 (void)printf("%9" PRIu64 " anonymous pages scanned by daemon\n", 1072 uvmexp.pdanscan); 1073 (void)printf("%9" PRIu64 " object pages scanned by daemon\n", uvmexp.pdobscan); 1074 (void)printf("%9" PRIu64 " pages reactivated\n", uvmexp.pdreact); 1075 (void)printf("%9" PRIu64 " pages found busy by daemon\n", uvmexp.pdbusy); 1076 (void)printf("%9" PRIu64 " pages deactivated\n", uvmexp.pddeact); 1077 (void)printf("%9" PRIu64 " per-cpu stats synced\n", uvmexp.countsyncall); 1078 (void)printf("%9" PRIu64 " anon pages possibly dirty\n", uvmexp.anonunknown); 1079 (void)printf("%9" PRIu64 " anon pages dirty\n", uvmexp.anondirty); 1080 (void)printf("%9" PRIu64 " anon pages clean\n", uvmexp.anonclean); 1081 (void)printf("%9" PRIu64 " file pages possibly dirty\n", uvmexp.fileunknown); 1082 (void)printf("%9" PRIu64 " file pages dirty\n", uvmexp.filedirty); 1083 (void)printf("%9" PRIu64 " file pages clean\n", uvmexp.fileclean); 1084 1085 if (active_kernel) { 1086 ssize = sizeof(nch_stats); 1087 if (sysctlbyname("vfs.namecache_stats", &nch_stats, &ssize, 1088 NULL, 0)) { 1089 warn("vfs.namecache_stats failed"); 1090 memset(&nch_stats, 0, sizeof(nch_stats)); 1091 } 1092 } else { 1093 kread(namelist, X_NCHSTATS, &nch_stats, sizeof(nch_stats)); 1094 } 1095 1096 nchtotal = nch_stats.ncs_goodhits + nch_stats.ncs_neghits + 1097 nch_stats.ncs_badhits + nch_stats.ncs_falsehits + 1098 nch_stats.ncs_miss + nch_stats.ncs_long; 1099 (void)printf("%9" PRIu64 " total name lookups\n", nchtotal); 1100 (void)printf("%9" PRIu64 " good hits\n", nch_stats.ncs_goodhits); 1101 (void)printf("%9" PRIu64 " negative hits\n", nch_stats.ncs_neghits); 1102 (void)printf("%9" PRIu64 " bad hits\n", nch_stats.ncs_badhits); 1103 (void)printf("%9" PRIu64 " false hits\n", nch_stats.ncs_falsehits); 1104 (void)printf("%9" PRIu64 " miss\n", nch_stats.ncs_miss); 1105 (void)printf("%9" PRIu64 " too long\n", nch_stats.ncs_long); 1106 (void)printf("%9" PRIu64 " pass2 hits\n", nch_stats.ncs_pass2); 1107 (void)printf("%9" PRIu64 " 2passes\n", nch_stats.ncs_2passes); 1108 (void)printf("%9" PRIu64 " reverse hits\n", nch_stats.ncs_revhits); 1109 (void)printf("%9" PRIu64 " reverse miss\n", nch_stats.ncs_revmiss); 1110 (void)printf("%9" PRIu64 " access denied\n", nch_stats.ncs_denied); 1111 (void)printf( 1112 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", 1113 "", PCT(nch_stats.ncs_goodhits, nchtotal), 1114 PCT(nch_stats.ncs_neghits, nchtotal), 1115 PCT(nch_stats.ncs_pass2, nchtotal)); 1116 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", 1117 PCT(nch_stats.ncs_badhits, nchtotal), 1118 PCT(nch_stats.ncs_falsehits, nchtotal), 1119 PCT(nch_stats.ncs_long, nchtotal)); 1120 } 1121 1122 void 1123 doforkst(void) 1124 { 1125 if (memf != NULL) { 1126 struct uvmexp uvmexp_kernel; 1127 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel)); 1128 #define COPY(field) uvmexp.field = uvmexp_kernel.field 1129 COPY(forks); 1130 COPY(forks_ppwait); 1131 COPY(forks_sharevm); 1132 #undef COPY 1133 } else { 1134 size_t size = sizeof(uvmexp); 1135 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp, 1136 &size, NULL, 0) == -1) 1137 warn("sysctl vm.uvmexp2 failed"); 1138 } 1139 1140 (void)printf("%" PRIu64 " forks total\n", uvmexp.forks); 1141 (void)printf("%" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait); 1142 (void)printf("%" PRIu64 " forks shared address space with parent\n", 1143 uvmexp.forks_sharevm); 1144 } 1145 1146 void 1147 drvstats(int *ovflwp) 1148 { 1149 size_t dn; 1150 double dtime; 1151 int ovflw = *ovflwp; 1152 1153 /* Calculate disk stat deltas. */ 1154 cpuswap(); 1155 drvswap(); 1156 tkswap(); 1157 1158 for (dn = 0; dn < ndrive; ++dn) { 1159 /* elapsed time for disk stats */ 1160 dtime = cur.cp_etime; 1161 if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) { 1162 dtime = (double)cur.timestamp[dn].tv_sec + 1163 ((double)cur.timestamp[dn].tv_usec / (double)1000000); 1164 } 1165 1166 if (!drv_select[dn]) 1167 continue; 1168 PRWORD(ovflw, " %*.0f", 3, 1, 1169 (cur.rxfer[dn] + cur.wxfer[dn]) / dtime); 1170 } 1171 *ovflwp = ovflw; 1172 } 1173 1174 void 1175 cpucounters(struct cpu_counter *cc) 1176 { 1177 static struct cpu_info **cpu_infos; 1178 static int initialised; 1179 struct cpu_info **slot; 1180 1181 if (memf == NULL) { 1182 cc->nintr = uvmexp.intrs; 1183 cc->nsyscall = uvmexp.syscalls; 1184 cc->nswtch = uvmexp.swtch; 1185 cc->nfault = uvmexp.faults; 1186 cc->ntrap = uvmexp.traps; 1187 cc->nsoft = uvmexp.softs; 1188 return; 1189 } 1190 1191 if (!initialised) { 1192 kread(namelist, X_CPU_INFOS, &cpu_infos, sizeof(cpu_infos)); 1193 initialised = 1; 1194 } 1195 1196 slot = cpu_infos; 1197 1198 memset(cc, 0, sizeof(*cc)); 1199 1200 for (;;) { 1201 struct cpu_info tci, *ci = NULL; 1202 1203 deref_kptr(slot++, &ci, sizeof(ci), "CPU array trashed"); 1204 if (!ci) { 1205 break; 1206 } 1207 1208 if ((size_t)kvm_read(kd, (u_long)ci, &tci, sizeof(tci)) 1209 != sizeof(tci)) { 1210 warnx("Can't read cpu info from %p (%s)", 1211 ci, kvm_geterr(kd)); 1212 memset(cc, 0, sizeof(*cc)); 1213 return; 1214 } 1215 cc->nintr += tci.ci_data.cpu_nintr; 1216 cc->nsyscall += tci.ci_data.cpu_nsyscall; 1217 cc->nswtch = tci.ci_data.cpu_nswtch; 1218 cc->nfault = tci.ci_data.cpu_nfault; 1219 cc->ntrap = tci.ci_data.cpu_ntrap; 1220 cc->nsoft = tci.ci_data.cpu_nsoft; 1221 } 1222 } 1223 1224 void 1225 cpustats(int *ovflwp) 1226 { 1227 int state; 1228 double pcnt, total; 1229 double stat_us, stat_sy, stat_id; 1230 int ovflw = *ovflwp; 1231 1232 total = 0; 1233 for (state = 0; state < CPUSTATES; ++state) 1234 total += cur.cp_time[state]; 1235 if (total) 1236 pcnt = 100 / total; 1237 else 1238 pcnt = 0; 1239 stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt; 1240 stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt; 1241 stat_id = cur.cp_time[CP_IDLE] * pcnt; 1242 PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us); 1243 PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1, 1244 stat_sy); 1245 PRWORD(ovflw, " %*.0f", 3, 1, stat_id); 1246 *ovflwp = ovflw; 1247 } 1248 1249 void 1250 dointr(int verbose) 1251 { 1252 unsigned long *intrcnt, *ointrcnt; 1253 unsigned long long inttotal, uptime; 1254 int nintr, inamlen; 1255 char *intrname, *ointrname; 1256 1257 if (memf == NULL) { 1258 doevcnt(verbose, EVCNT_TYPE_INTR); 1259 return; 1260 } 1261 1262 inttotal = 0; 1263 uptime = getuptime(); 1264 nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value; 1265 inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value; 1266 if (nintr != 0 && inamlen != 0) { 1267 (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate"); 1268 1269 ointrcnt = intrcnt = malloc((size_t)nintr); 1270 ointrname = intrname = malloc((size_t)inamlen); 1271 if (intrcnt == NULL || intrname == NULL) 1272 errx(1, "%s", ""); 1273 kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr); 1274 kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen); 1275 nintr /= sizeof(long); 1276 while (--nintr >= 0) { 1277 if (*intrcnt || verbose) 1278 (void)printf("%-34s %16llu %8llu\n", intrname, 1279 (unsigned long long)*intrcnt, 1280 (unsigned long long) 1281 (*intrcnt / uptime)); 1282 intrname += strlen(intrname) + 1; 1283 inttotal += *intrcnt++; 1284 } 1285 free(ointrcnt); 1286 free(ointrname); 1287 } 1288 1289 doevcnt(verbose, EVCNT_TYPE_INTR); 1290 } 1291 1292 void 1293 doevcnt(int verbose, int type) 1294 { 1295 static const char * const evtypes [] = { "misc", "intr", "trap" }; 1296 uint64_t counttotal, uptime; 1297 struct evcntlist allevents; 1298 struct evcnt evcnt, *evptr; 1299 size_t evlen_max, total_max, rate_max; 1300 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX]; 1301 1302 counttotal = 0; 1303 uptime = getuptime(); 1304 1305 if (memf == NULL) { 1306 const int mib[4] = { CTL_KERN, KERN_EVCNT, type, 1307 verbose ? KERN_EVCNT_COUNT_ANY : KERN_EVCNT_COUNT_NONZERO }; 1308 size_t buflen0, buflen = 0; 1309 void *buf0, *buf = NULL; 1310 const struct evcnt_sysctl *evs, *last_evs; 1311 for (;;) { 1312 size_t newlen; 1313 int error; 1314 if (buflen) 1315 buf = malloc(buflen); 1316 error = sysctl(mib, __arraycount(mib), 1317 buf, &newlen, NULL, 0); 1318 if (error) { 1319 err(1, "kern.evcnt"); 1320 if (buf) 1321 free(buf); 1322 return; 1323 } 1324 if (newlen <= buflen) { 1325 buflen = newlen; 1326 break; 1327 } 1328 if (buf) 1329 free(buf); 1330 buflen = newlen; 1331 } 1332 buflen0 = buflen; 1333 evs = buf0 = buf; 1334 last_evs = (void *)((char *)buf + buflen); 1335 buflen /= sizeof(uint64_t); 1336 /* calc columns */ 1337 evlen_max = 0; 1338 total_max = sizeof("total") - 1; 1339 rate_max = sizeof("rate") - 1; 1340 while (evs < last_evs 1341 && buflen >= sizeof(*evs)/sizeof(uint64_t) 1342 && buflen >= evs->ev_len) { 1343 char cbuf[64]; 1344 size_t len; 1345 len = strlen(evs->ev_strings + evs->ev_grouplen + 1); 1346 len += evs->ev_grouplen + 1; 1347 if (evlen_max < len) 1348 evlen_max= len; 1349 len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64, 1350 evs->ev_count); 1351 if (total_max < len) 1352 total_max = len; 1353 len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64, 1354 evs->ev_count / uptime); 1355 if (rate_max < len) 1356 rate_max = len; 1357 buflen -= evs->ev_len; 1358 counttotal += evs->ev_count; 1359 evs = (const void *) 1360 ((const uint64_t *)evs + evs->ev_len); 1361 } 1362 if (type != EVCNT_TYPE_ANY) { 1363 char cbuf[64]; 1364 size_t len; 1365 1366 len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64, 1367 counttotal); 1368 if (total_max < len) 1369 total_max = len; 1370 1371 len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64, 1372 counttotal / uptime); 1373 if (rate_max < len) 1374 rate_max = len; 1375 } 1376 const char *evdesc = type == EVCNT_TYPE_ANY ? 1377 "event" : "interrupt"; 1378 const char *typedesc = type == EVCNT_TYPE_ANY ? 1379 " type" : ""; 1380 1381 (void)printf("%-*s %*s %*s%s\n", 1382 (int)evlen_max, evdesc, 1383 (int)total_max, "total", 1384 (int)rate_max, "rate", 1385 typedesc); 1386 1387 buflen = buflen0; 1388 evs = buf0; 1389 last_evs = (void *)((char *)buf + buflen); 1390 buflen /= sizeof(uint64_t); 1391 while (evs < last_evs 1392 && buflen >= sizeof(*evs)/sizeof(uint64_t) 1393 && buflen >= evs->ev_len) { 1394 (void)printf(type == EVCNT_TYPE_ANY ? 1395 "%s %s%*s %*"PRIu64" %*"PRIu64" %s\n" : 1396 "%s %s%*s %*"PRIu64" %*"PRIu64"\n", 1397 evs->ev_strings, 1398 evs->ev_strings + evs->ev_grouplen + 1, 1399 (int)evlen_max - (evs->ev_grouplen + 1 1400 + evs->ev_namelen), "", 1401 (int)total_max, evs->ev_count, 1402 (int)rate_max, evs->ev_count / uptime, 1403 (evs->ev_type < __arraycount(evtypes) ? 1404 evtypes[evs->ev_type] : "?")); 1405 buflen -= evs->ev_len; 1406 evs = (const void *) 1407 ((const uint64_t *)evs + evs->ev_len); 1408 } 1409 free(buf); 1410 if (type != EVCNT_TYPE_ANY) 1411 (void)printf("%-*s %*"PRIu64" %*"PRIu64"\n", 1412 (int)evlen_max, "Total", 1413 (int)total_max, counttotal, 1414 (int)rate_max, counttotal / uptime); 1415 return; 1416 } 1417 1418 if (type == EVCNT_TYPE_ANY) 1419 (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate", 1420 "type"); 1421 1422 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents); 1423 evptr = TAILQ_FIRST(&allevents); 1424 while (evptr) { 1425 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed"); 1426 1427 evptr = TAILQ_NEXT(&evcnt, ev_list); 1428 if (evcnt.ev_count == 0 && !verbose) 1429 continue; 1430 if (type != EVCNT_TYPE_ANY && evcnt.ev_type != type) 1431 continue; 1432 1433 deref_kptr(evcnt.ev_group, evgroup, 1434 (size_t)evcnt.ev_grouplen + 1, "event chain trashed"); 1435 deref_kptr(evcnt.ev_name, evname, 1436 (size_t)evcnt.ev_namelen + 1, "event chain trashed"); 1437 1438 (void)printf(type == EVCNT_TYPE_ANY ? 1439 "%s %s%*s %16"PRIu64" %8"PRIu64" %s\n" : 1440 "%s %s%*s %16"PRIu64" %8"PRIu64"\n", 1441 evgroup, evname, 1442 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "", 1443 evcnt.ev_count, 1444 (evcnt.ev_count / uptime), 1445 (evcnt.ev_type < __arraycount(evtypes) ? 1446 evtypes[evcnt.ev_type] : "?")); 1447 1448 counttotal += evcnt.ev_count; 1449 } 1450 if (type != EVCNT_TYPE_ANY) 1451 (void)printf("%-34s %16"PRIu64" %8"PRIu64"\n", 1452 "Total", counttotal, counttotal / uptime); 1453 } 1454 1455 static void 1456 dopool_sysctl(int verbose, int wide) 1457 { 1458 uint64_t total, inuse, this_total, this_inuse; 1459 struct { 1460 uint64_t pt_nget; 1461 uint64_t pt_nfail; 1462 uint64_t pt_nput; 1463 uint64_t pt_nout; 1464 uint64_t pt_nitems; 1465 uint64_t pt_npagealloc; 1466 uint64_t pt_npagefree; 1467 uint64_t pt_npages; 1468 } pool_totals; 1469 size_t i, len; 1470 int name_len, ovflw; 1471 struct pool_sysctl *pp, *data; 1472 char maxp[32]; 1473 1474 data = asysctlbyname("kern.pool", &len); 1475 if (data == NULL) 1476 err(1, "failed to read kern.pool"); 1477 1478 memset(&pool_totals, 0, sizeof pool_totals); 1479 total = inuse = 0; 1480 len /= sizeof(*data); 1481 1482 (void)printf("Memory resource pool statistics\n"); 1483 (void)printf( 1484 "%-*s%*s%*s%*s%*s%s%s%*s%*s%*s%s%*s%6s%*s%*s%s%s%s\n", 1485 wide ? 16 : 11, "Name", 1486 wide ? 9 : 5, "Size", 1487 wide ? 13 : 9, "Requests", 1488 wide ? 8 : 5, "Fail", 1489 wide ? 13 : 9, "Releases", 1490 wide ? " InUse" : "", 1491 wide ? " Avail" : "", 1492 wide ? 11 : 6, "Pgreq", 1493 wide ? 11 : 6, "Pgrel", 1494 wide ? 9 : 6, "Npage", 1495 wide ? " PageSz" : "", 1496 wide ? 8 : 6, "Hiwat", 1497 "Minpg", 1498 wide ? 9 : 6, "Maxpg", 1499 wide ? 8 : 5, "Idle", 1500 wide ? " Flags" : "", 1501 wide ? " Util" : "", 1502 wide ? " TotalKB" : ""); 1503 1504 name_len = MIN((int)sizeof(pp->pr_wchan), wide ? 16 : 11); 1505 for (i = 0; i < len; ++i) { 1506 pp = &data[i]; 1507 if (pp->pr_nget == 0 && !verbose) 1508 continue; 1509 if (pp->pr_maxpages == UINT_MAX) 1510 (void)snprintf(maxp, sizeof(maxp), "inf"); 1511 else 1512 (void)snprintf(maxp, sizeof(maxp), "%" PRIu64, 1513 pp->pr_maxpages); 1514 ovflw = 0; 1515 PRWORD(ovflw, "%-*s", name_len, 0, pp->pr_wchan); 1516 PRWORD(ovflw, " %*" PRIu64, wide ? 9 : 5, 1, pp->pr_size); 1517 PRWORD(ovflw, " %*" PRIu64, wide ? 13 : 9, 1, pp->pr_nget); 1518 pool_totals.pt_nget += pp->pr_nget; 1519 PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pp->pr_nfail); 1520 pool_totals.pt_nfail += pp->pr_nfail; 1521 PRWORD(ovflw, " %*" PRIu64, wide ? 13 : 9, 1, pp->pr_nput); 1522 pool_totals.pt_nput += pp->pr_nput; 1523 if (wide) { 1524 PRWORD(ovflw, " %*" PRIu64, 9, 1, pp->pr_nout); 1525 pool_totals.pt_nout += pp->pr_nout; 1526 PRWORD(ovflw, " %*" PRIu64, 9, 1, pp->pr_nitems); 1527 pool_totals.pt_nitems += pp->pr_nitems; 1528 } 1529 PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pp->pr_npagealloc); 1530 pool_totals.pt_npagealloc += pp->pr_npagealloc; 1531 PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pp->pr_npagefree); 1532 pool_totals.pt_npagefree += pp->pr_npagefree; 1533 PRWORD(ovflw, " %*" PRIu64, wide ? 9 : 6, 1, pp->pr_npages); 1534 pool_totals.pt_npages += pp->pr_npages; 1535 if (wide) 1536 PRWORD(ovflw, " %*" PRIu64, 9, 1, pp->pr_pagesize); 1537 PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 6, 1, pp->pr_hiwat); 1538 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_minpages); 1539 PRWORD(ovflw, " %*s", wide ? 9 : 6, 1, maxp); 1540 PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pp->pr_nidle); 1541 if (wide) 1542 PRWORD(ovflw, " 0x%0*" PRIx64, 6, 1, 1543 pp->pr_flags); 1544 1545 this_inuse = pp->pr_nout * pp->pr_size; 1546 this_total = pp->pr_npages * pp->pr_pagesize; 1547 if (pp->pr_flags & PR_RECURSIVE) { 1548 /* 1549 * Don't count in-use memory, since it's part 1550 * of another pool and will be accounted for 1551 * there. 1552 */ 1553 total += (this_total - this_inuse); 1554 } else { 1555 inuse += this_inuse; 1556 total += this_total; 1557 } 1558 if (wide) { 1559 if (this_total == 0) { 1560 (void)printf(" ---"); 1561 } else { 1562 (void)printf(" %5.1f%% %10" PRIu64, 1563 (100.0 * this_inuse) / this_total, 1564 this_total / KILO); 1565 } 1566 } 1567 (void)printf("\n"); 1568 } 1569 ovflw = 0; 1570 PRWORD(ovflw, "%-*s", name_len, 0, "Totals"); 1571 PRWORD(ovflw, " %*s", wide ? 9 : 5, 1, ""); 1572 PRWORD(ovflw, " %*" PRIu64, wide ? 13 : 9, 1, pool_totals.pt_nget); 1573 PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pool_totals.pt_nfail); 1574 PRWORD(ovflw, " %*" PRIu64, wide ? 13 : 9, 1, pool_totals.pt_nput); 1575 if (wide) { 1576 PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nout); 1577 PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nitems); 1578 } 1579 PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagealloc); 1580 PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagefree); 1581 PRWORD(ovflw, " %*" PRIu64, wide ? 9 : 6, 1, pool_totals.pt_npages); 1582 (void)printf("\n"); 1583 1584 inuse /= KILO; 1585 total /= KILO; 1586 (void)printf( 1587 "\nIn use %" PRIu64 "K, " 1588 "total allocated %" PRIu64 "K; utilization %.1f%%\n", 1589 inuse, total, (100.0 * inuse) / total); 1590 1591 free(data); 1592 } 1593 1594 void 1595 dopool(int verbose, int wide) 1596 { 1597 int first, ovflw; 1598 void *addr; 1599 uint64_t total, inuse, this_total, this_inuse; 1600 struct { 1601 uint64_t pt_nget; 1602 uint64_t pt_nfail; 1603 uint64_t pt_nput; 1604 uint64_t pt_nout; 1605 uint64_t pt_nitems; 1606 uint64_t pt_npagealloc; 1607 uint64_t pt_npagefree; 1608 uint64_t pt_npages; 1609 } pool_totals; 1610 TAILQ_HEAD(,pool) pool_head; 1611 struct pool pool, *pp = &pool; 1612 struct pool_allocator pa; 1613 char maxp[32], name[32]; 1614 1615 if (memf == NULL) { 1616 dopool_sysctl(verbose, wide); 1617 return; 1618 } 1619 1620 memset(&pool_totals, 0, sizeof pool_totals); 1621 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head)); 1622 addr = TAILQ_FIRST(&pool_head); 1623 1624 total = inuse = 0; 1625 1626 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) { 1627 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed"); 1628 deref_kptr(pp->pr_alloc, &pa, sizeof(pa), 1629 "pool allocator trashed"); 1630 deref_kptr(pp->pr_wchan, name, sizeof(name), 1631 "pool wait channel trashed"); 1632 name[sizeof(name)-1] = '\0'; 1633 1634 if (first) { 1635 (void)printf("Memory resource pool statistics\n"); 1636 (void)printf( 1637 "%-*s%*s%*s%*s%*s%s%s%*s%*s%*s%s%*s%6s%*s%*s%s%s%s\n", 1638 wide ? 16 : 11, "Name", 1639 wide ? 9 : 5, "Size", 1640 wide ? 13 : 9, "Requests", 1641 wide ? 8 : 5, "Fail", 1642 wide ? 13 : 9, "Releases", 1643 wide ? " InUse" : "", 1644 wide ? " Avail" : "", 1645 wide ? 11 : 6, "Pgreq", 1646 wide ? 11 : 6, "Pgrel", 1647 wide ? 9 : 6, "Npage", 1648 wide ? " PageSz" : "", 1649 wide ? 8 : 6, "Hiwat", 1650 "Minpg", 1651 wide ? 9 : 6, "Maxpg", 1652 wide ? 8 : 5, "Idle", 1653 wide ? " Flags" : "", 1654 wide ? " Util" : "", 1655 wide ? " TotalKB" : ""); 1656 first = 0; 1657 } 1658 if (pp->pr_nget == 0 && !verbose) 1659 continue; 1660 if (pp->pr_maxpages == UINT_MAX) 1661 (void)snprintf(maxp, sizeof(maxp), "inf"); 1662 else 1663 (void)snprintf(maxp, sizeof(maxp), "%u", 1664 pp->pr_maxpages); 1665 ovflw = 0; 1666 PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name); 1667 PRWORD(ovflw, " %*u", wide ? 9 : 5, 1, pp->pr_size); 1668 PRWORD(ovflw, " %*lu", wide ? 13 : 9, 1, pp->pr_nget); 1669 pool_totals.pt_nget += pp->pr_nget; 1670 PRWORD(ovflw, " %*lu", wide ? 8 : 5, 1, pp->pr_nfail); 1671 pool_totals.pt_nfail += pp->pr_nfail; 1672 PRWORD(ovflw, " %*lu", wide ? 13 : 9, 1, pp->pr_nput); 1673 pool_totals.pt_nput += pp->pr_nput; 1674 if (wide) { 1675 PRWORD(ovflw, " %*u", 9, 1, pp->pr_nout); 1676 pool_totals.pt_nout += pp->pr_nout; 1677 PRWORD(ovflw, " %*u", 9, 1, pp->pr_nitems); 1678 pool_totals.pt_nitems += pp->pr_nitems; 1679 } 1680 PRWORD(ovflw, " %*lu", wide ? 11 : 6, 1, pp->pr_npagealloc); 1681 pool_totals.pt_npagealloc += pp->pr_npagealloc; 1682 PRWORD(ovflw, " %*lu", wide ? 11 : 6, 1, pp->pr_npagefree); 1683 pool_totals.pt_npagefree += pp->pr_npagefree; 1684 PRWORD(ovflw, " %*u", wide ? 9 : 6, 1, pp->pr_npages); 1685 pool_totals.pt_npages += pp->pr_npages; 1686 if (wide) 1687 PRWORD(ovflw, " %*u", 9, 1, pa.pa_pagesz); 1688 PRWORD(ovflw, " %*u", wide ? 8 : 6, 1, pp->pr_hiwat); 1689 PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages); 1690 PRWORD(ovflw, " %*s", wide ? 9 : 6, 1, maxp); 1691 PRWORD(ovflw, " %*lu", wide ? 8 : 5, 1, pp->pr_nidle); 1692 if (wide) 1693 PRWORD(ovflw, " 0x%0*x", 6, 1, 1694 pp->pr_flags | pp->pr_roflags); 1695 1696 this_inuse = (uint64_t)pp->pr_nout * pp->pr_size; 1697 this_total = (uint64_t)pp->pr_npages * pa.pa_pagesz; 1698 if (pp->pr_roflags & PR_RECURSIVE) { 1699 /* 1700 * Don't count in-use memory, since it's part 1701 * of another pool and will be accounted for 1702 * there. 1703 */ 1704 total += (this_total - this_inuse); 1705 } else { 1706 inuse += this_inuse; 1707 total += this_total; 1708 } 1709 if (wide) { 1710 if (this_total == 0) { 1711 (void)printf(" ---"); 1712 } else { 1713 (void)printf(" %5.1f%% %10" PRIu64, 1714 (100.0 * this_inuse) / this_total, 1715 this_total / KILO); 1716 } 1717 } 1718 (void)printf("\n"); 1719 } 1720 ovflw = 0; 1721 PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, "Totals"); 1722 PRWORD(ovflw, " %*s", wide ? 9 : 5, 1, ""); 1723 PRWORD(ovflw, " %*" PRIu64, wide ? 13 : 9, 1, pool_totals.pt_nget); 1724 PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pool_totals.pt_nfail); 1725 PRWORD(ovflw, " %*" PRIu64, wide ? 13 : 9, 1, pool_totals.pt_nput); 1726 if (wide) { 1727 PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nout); 1728 PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nitems); 1729 } 1730 PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagealloc); 1731 PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagefree); 1732 PRWORD(ovflw, " %*" PRIu64, wide ? 9 : 6, 1, pool_totals.pt_npages); 1733 (void)printf("\n"); 1734 1735 inuse /= KILO; 1736 total /= KILO; 1737 (void)printf( 1738 "\nIn use %" PRIu64 "K, " 1739 "total allocated %" PRIu64 "K; utilization %.1f%%\n", 1740 inuse, total, (100.0 * inuse) / total); 1741 } 1742 1743 static void 1744 dopoolcache_sysctl(int verbose) 1745 { 1746 struct pool_sysctl *data, *pp; 1747 size_t i, len; 1748 bool first = true; 1749 int ovflw; 1750 uint64_t tot; 1751 double p; 1752 1753 data = asysctlbyname("kern.pool", &len); 1754 if (data == NULL) 1755 err(1, "failed to read kern.pool"); 1756 len /= sizeof(*data); 1757 1758 for (i = 0; i < len; ++i) { 1759 pp = &data[i]; 1760 if (pp->pr_cache_meta_size == 0) 1761 continue; 1762 1763 if (pp->pr_cache_nmiss_global == 0 && !verbose) 1764 continue; 1765 1766 if (first) { 1767 (void)printf("Pool cache statistics.\n"); 1768 (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n", 1769 12, "Name", 1770 6, "Spin", 1771 6, "GrpSz", 1772 5, "Full", 1773 5, "Emty", 1774 10, "PoolLayer", 1775 11, "CacheLayer", 1776 6, "Hit%", 1777 12, "CpuLayer", 1778 6, "Hit%" 1779 ); 1780 first = false; 1781 } 1782 1783 ovflw = 0; 1784 PRWORD(ovflw, "%-*s", MIN((int)sizeof(pp->pr_wchan), 13), 1, 1785 pp->pr_wchan); 1786 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_ncontended); 1787 PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_meta_size); 1788 PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nfull); 1789 PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nempty); 1790 PRWORD(ovflw, " %*" PRIu64, 10, 1, pp->pr_cache_nmiss_global); 1791 1792 tot = pp->pr_cache_nhit_global + pp->pr_cache_nmiss_global; 1793 p = pp->pr_cache_nhit_global * 100.0 / tot; 1794 PRWORD(ovflw, " %*" PRIu64, 11, 1, tot); 1795 PRWORD(ovflw, " %*.1f", 6, 1, p); 1796 1797 tot = pp->pr_cache_nhit_pcpu + pp->pr_cache_nmiss_pcpu; 1798 p = pp->pr_cache_nhit_pcpu * 100.0 / tot; 1799 PRWORD(ovflw, " %*" PRIu64, 12, 1, tot); 1800 PRWORD(ovflw, " %*.1f", 6, 1, p); 1801 printf("\n"); 1802 } 1803 } 1804 1805 void 1806 dopoolcache(int verbose) 1807 { 1808 struct pool_cache pool_cache, *pc = &pool_cache; 1809 pool_cache_cpu_t cache_cpu, *cc = &cache_cpu; 1810 TAILQ_HEAD(,pool) pool_head; 1811 struct pool pool, *pp = &pool; 1812 char name[32]; 1813 uint64_t cpuhit, cpumiss, pchit, pcmiss, contended, tot; 1814 uint32_t nfull; 1815 void *addr; 1816 int first, ovflw; 1817 size_t i; 1818 double p; 1819 1820 if (memf == NULL) { 1821 dopoolcache_sysctl(verbose); 1822 return; 1823 } 1824 1825 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head)); 1826 addr = TAILQ_FIRST(&pool_head); 1827 1828 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) { 1829 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed"); 1830 if (pp->pr_cache == NULL) 1831 continue; 1832 deref_kptr(pp->pr_wchan, name, sizeof(name), 1833 "pool wait channel trashed"); 1834 deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed"); 1835 name[sizeof(name)-1] = '\0'; 1836 1837 cpuhit = 0; 1838 cpumiss = 0; 1839 pcmiss = 0; 1840 contended = 0; 1841 nfull = 0; 1842 for (i = 0; i < __arraycount(pc->pc_cpus); i++) { 1843 if ((addr = pc->pc_cpus[i]) == NULL) 1844 continue; 1845 deref_kptr(addr, cc, sizeof(*cc), 1846 "pool cache cpu trashed"); 1847 cpuhit += cc->cc_hits; 1848 cpumiss += cc->cc_misses; 1849 pcmiss += cc->cc_pcmisses; 1850 nfull += cc->cc_nfull; 1851 contended += cc->cc_contended; 1852 } 1853 pchit = cpumiss - pcmiss; 1854 1855 if (pcmiss == 0 && !verbose) 1856 continue; 1857 1858 if (first) { 1859 (void)printf("Pool cache statistics.\n"); 1860 (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n", 1861 12, "Name", 1862 6, "Spin", 1863 6, "GrpSz", 1864 5, "Full", 1865 5, "Emty", 1866 10, "PoolLayer", 1867 11, "CacheLayer", 1868 6, "Hit%", 1869 12, "CpuLayer", 1870 6, "Hit%" 1871 ); 1872 first = 0; 1873 } 1874 1875 ovflw = 0; 1876 PRWORD(ovflw, "%-*s", 13, 1, name); 1877 PRWORD(ovflw, " %*llu", 6, 1, (long long)contended); 1878 PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize); 1879 PRWORD(ovflw, " %*u", 5, 1, nfull); 1880 PRWORD(ovflw, " %*u", 5, 1, 0); 1881 PRWORD(ovflw, " %*llu", 10, 1, (long long)pcmiss); 1882 1883 tot = pchit + pcmiss; 1884 p = pchit * 100.0 / (tot); 1885 PRWORD(ovflw, " %*llu", 11, 1, (long long)tot); 1886 PRWORD(ovflw, " %*.1f", 6, 1, p); 1887 1888 tot = cpuhit + cpumiss; 1889 p = cpuhit * 100.0 / (tot); 1890 PRWORD(ovflw, " %*llu", 12, 1, (long long)tot); 1891 PRWORD(ovflw, " %*.1f", 6, 1, p); 1892 printf("\n"); 1893 } 1894 } 1895 1896 enum hashtype { /* from <sys/systm.h> */ 1897 HASH_LIST, 1898 HASH_SLIST, 1899 HASH_TAILQ, 1900 HASH_PSLIST 1901 }; 1902 1903 struct kernel_hash { 1904 const char * description; /* description */ 1905 int hashsize; /* nlist index for hash size */ 1906 int hashtbl; /* nlist index for hash table */ 1907 enum hashtype type; /* type of hash table */ 1908 size_t offset; /* offset of {LIST,TAILQ}_NEXT */ 1909 } khashes[] = 1910 { 1911 { 1912 "buffer hash", 1913 X_BUFHASH, X_BUFHASHTBL, 1914 HASH_LIST, offsetof(struct buf, b_hash) 1915 }, { 1916 "ipv4 address -> interface hash", 1917 X_IFADDRHASH, X_IFADDRHASHTBL, 1918 HASH_LIST, offsetof(struct in_ifaddr, ia_hash), 1919 }, { 1920 "user info (uid -> used processes) hash", 1921 X_UIHASH, X_UIHASHTBL, 1922 HASH_SLIST, offsetof(struct uidinfo, ui_hash), 1923 }, { 1924 "vnode cache hash", 1925 X_VCACHEHASH, X_VCACHETBL, 1926 HASH_SLIST, offsetof(struct vnode_impl, vi_hash), 1927 }, { 1928 NULL, -1, -1, 0, 0, 1929 } 1930 }; 1931 1932 void 1933 dohashstat(int verbose, int todo, const char *hashname) 1934 { 1935 LIST_HEAD(, generic) *hashtbl_list; 1936 SLIST_HEAD(, generic) *hashtbl_slist; 1937 TAILQ_HEAD(, generic) *hashtbl_tailq; 1938 struct kernel_hash *curhash; 1939 void *hashaddr, *hashbuf, *nhashbuf, *nextaddr; 1940 size_t elemsize, hashbufsize, thissize; 1941 u_long hashsize, i; 1942 int used, items, chain, maxchain; 1943 1944 if (memf == NULL) { 1945 dohashstat_sysctl(verbose, todo, hashname); 1946 return; 1947 } 1948 1949 hashbuf = NULL; 1950 hashbufsize = 0; 1951 1952 if (todo & HASHLIST) { 1953 (void)printf("Supported hashes:\n"); 1954 for (curhash = khashes; curhash->description; curhash++) { 1955 if (hashnl[curhash->hashsize].n_value == 0 || 1956 hashnl[curhash->hashtbl].n_value == 0) 1957 continue; 1958 (void)printf("\t%-16s%s\n", 1959 hashnl[curhash->hashsize].n_name + 1, 1960 curhash->description); 1961 } 1962 return; 1963 } 1964 1965 if (hashname != NULL) { 1966 for (curhash = khashes; curhash->description; curhash++) { 1967 if (strcmp(hashnl[curhash->hashsize].n_name + 1, 1968 hashname) == 0 && 1969 hashnl[curhash->hashsize].n_value != 0 && 1970 hashnl[curhash->hashtbl].n_value != 0) 1971 break; 1972 } 1973 if (curhash->description == NULL) { 1974 warnx("%s: no such hash", hashname); 1975 return; 1976 } 1977 } 1978 1979 (void)printf( 1980 "%-16s %8s %8s %8s %8s %8s %8s\n" 1981 "%-16s %8s %8s %8s %8s %8s %8s\n", 1982 "", "total", "used", "util", "num", "average", "maximum", 1983 "hash table", "buckets", "buckets", "%", "items", "chain", 1984 "chain"); 1985 1986 for (curhash = khashes; curhash->description; curhash++) { 1987 if (hashnl[curhash->hashsize].n_value == 0 || 1988 hashnl[curhash->hashtbl].n_value == 0) 1989 continue; 1990 if (hashname != NULL && 1991 strcmp(hashnl[curhash->hashsize].n_name + 1, hashname)) 1992 continue; 1993 switch (curhash->type) { 1994 case HASH_LIST: 1995 elemsize = sizeof(*hashtbl_list); 1996 break; 1997 case HASH_SLIST: 1998 elemsize = sizeof(*hashtbl_slist); 1999 break; 2000 case HASH_TAILQ: 2001 elemsize = sizeof(*hashtbl_tailq); 2002 break; 2003 default: 2004 /* shouldn't get here */ 2005 continue; 2006 } 2007 deref_kptr((void *)hashnl[curhash->hashsize].n_value, 2008 &hashsize, sizeof(hashsize), 2009 hashnl[curhash->hashsize].n_name); 2010 hashsize++; 2011 deref_kptr((void *)hashnl[curhash->hashtbl].n_value, 2012 &hashaddr, sizeof(hashaddr), 2013 hashnl[curhash->hashtbl].n_name); 2014 if (verbose) 2015 (void)printf( 2016 "%s %lu, %s %p, offset %ld, elemsize %llu\n", 2017 hashnl[curhash->hashsize].n_name + 1, hashsize, 2018 hashnl[curhash->hashtbl].n_name + 1, hashaddr, 2019 (long)curhash->offset, 2020 (unsigned long long)elemsize); 2021 thissize = hashsize * elemsize; 2022 if (hashbuf == NULL || thissize > hashbufsize) { 2023 if ((nhashbuf = realloc(hashbuf, thissize)) == NULL) 2024 errx(1, "malloc hashbuf %llu", 2025 (unsigned long long)hashbufsize); 2026 hashbuf = nhashbuf; 2027 hashbufsize = thissize; 2028 } 2029 deref_kptr(hashaddr, hashbuf, thissize, 2030 hashnl[curhash->hashtbl].n_name); 2031 used = 0; 2032 items = maxchain = 0; 2033 if (curhash->type == HASH_LIST) { 2034 hashtbl_list = hashbuf; 2035 hashtbl_slist = NULL; 2036 hashtbl_tailq = NULL; 2037 } else if (curhash->type == HASH_SLIST) { 2038 hashtbl_list = NULL; 2039 hashtbl_slist = hashbuf; 2040 hashtbl_tailq = NULL; 2041 } else { 2042 hashtbl_list = NULL; 2043 hashtbl_slist = NULL; 2044 hashtbl_tailq = hashbuf; 2045 } 2046 for (i = 0; i < hashsize; i++) { 2047 if (curhash->type == HASH_LIST) 2048 nextaddr = LIST_FIRST(&hashtbl_list[i]); 2049 else if (curhash->type == HASH_SLIST) 2050 nextaddr = SLIST_FIRST(&hashtbl_slist[i]); 2051 else 2052 nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]); 2053 if (nextaddr == NULL) 2054 continue; 2055 if (verbose) 2056 (void)printf("%5lu: %p\n", i, nextaddr); 2057 used++; 2058 chain = 0; 2059 do { 2060 chain++; 2061 deref_kptr((char *)nextaddr + curhash->offset, 2062 &nextaddr, sizeof(void *), 2063 "hash chain corrupted"); 2064 if (verbose > 1) 2065 (void)printf("got nextaddr as %p\n", 2066 nextaddr); 2067 } while (nextaddr != NULL); 2068 items += chain; 2069 if (verbose && chain > 1) 2070 (void)printf("\tchain = %d\n", chain); 2071 if (chain > maxchain) 2072 maxchain = chain; 2073 } 2074 (void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n", 2075 hashnl[curhash->hashsize].n_name + 1, 2076 hashsize, used, used * 100.0 / hashsize, 2077 items, used ? (double)items / used : 0.0, maxchain); 2078 } 2079 } 2080 2081 void 2082 dohashstat_sysctl(int verbose, int todo, const char *hashname) 2083 { 2084 struct hashstat_sysctl hash, *data, *hs; 2085 int mib[3]; 2086 int error; 2087 size_t i, len, miblen; 2088 2089 2090 miblen = __arraycount(mib); 2091 error = sysctlnametomib("kern.hashstat", mib, &miblen); 2092 if (error) 2093 err(EXIT_FAILURE, "nametomib kern.hashstat failed"); 2094 assert(miblen < 3); 2095 2096 if (todo & HASHLIST) { 2097 mib[miblen] = CTL_DESCRIBE; 2098 miblen++; 2099 }; 2100 2101 if (hashname) { 2102 mib[miblen] = CTL_QUERY; 2103 miblen++; 2104 memset(&hash, 0, sizeof(hash)); 2105 strlcpy(hash.hash_name, hashname, sizeof(hash.hash_name)); 2106 len = sizeof(hash); 2107 error = sysctl(mib, miblen, &hash, &len, &hash, len); 2108 if (error == ENOENT) { 2109 err(1, "hash '%s' not found", hashname); 2110 return; 2111 } else if (error) { 2112 err(1, "sysctl kern.hashstat query failed"); 2113 return; 2114 } 2115 2116 data = &hash; 2117 len = 1; 2118 } else { 2119 data = asysctl(mib, miblen, &len); 2120 if (data == NULL) 2121 err(1, "failed to read kern.hashstat"); 2122 len /= sizeof(*data); 2123 } 2124 2125 if (todo & HASHLIST) { 2126 printf("Supported hashes:\n"); 2127 for (i = 0, hs = data; i < len; i++, hs++) { 2128 printf("\t%-16s%s\n", hs->hash_name, hs->hash_desc); 2129 } 2130 } else { 2131 printf("%-16s %8s %8s %8s %8s %8s %8s\n" 2132 "%-16s %8s %8s %8s %8s %8s %8s\n", 2133 "", "total", "used", "util", "num", "average", "maximum", 2134 "hash table", "buckets", "buckets", "%", "items", "chain", 2135 "chain"); 2136 for (i = 0, hs = data; i < len; i++, hs++) { 2137 printf("%-16s %8"PRId64" %8"PRId64" %8.2f %8"PRId64 2138 " %8.2f %8"PRId64"\n", 2139 hs->hash_name, hs->hash_size, hs->hash_used, 2140 hs->hash_used * 100.0 / hs->hash_size, hs->hash_items, 2141 hs->hash_used ? (double)hs->hash_items / hs->hash_used : 0.0, 2142 hs->hash_maxchain); 2143 } 2144 } 2145 2146 if (!hashname && (data != NULL)) 2147 free(data); 2148 } 2149 2150 /* 2151 * kreadc like kread but returns 1 if successful, 0 otherwise 2152 */ 2153 int 2154 kreadc(struct nlist *nl, int nlx, void *addr, size_t size) 2155 { 2156 const char *sym; 2157 2158 sym = nl[nlx].n_name; 2159 if (*sym == '_') 2160 ++sym; 2161 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) 2162 return 0; 2163 deref_kptr((void *)nl[nlx].n_value, addr, size, sym); 2164 return 1; 2165 } 2166 2167 /* 2168 * kread reads something from the kernel, given its nlist index in namelist[]. 2169 */ 2170 void 2171 kread(struct nlist *nl, int nlx, void *addr, size_t size) 2172 { 2173 const char *sym; 2174 2175 sym = nl[nlx].n_name; 2176 if (*sym == '_') 2177 ++sym; 2178 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) 2179 errx(1, "symbol %s not defined", sym); 2180 deref_kptr((void *)nl[nlx].n_value, addr, size, sym); 2181 } 2182 2183 /* 2184 * Dereference the kernel pointer `kptr' and fill in the local copy 2185 * pointed to by `ptr'. The storage space must be pre-allocated, 2186 * and the size of the copy passed in `len'. 2187 */ 2188 void 2189 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg) 2190 { 2191 2192 if (*msg == '_') 2193 msg++; 2194 if ((size_t)kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len) 2195 errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd)); 2196 } 2197 2198 /* 2199 * Traverse the kernel history buffers, performing the requested action. 2200 * 2201 * Note, we assume that if we're not listing, we're dumping. 2202 */ 2203 void 2204 hist_traverse(int todo, const char *histname) 2205 { 2206 struct kern_history_head histhead; 2207 struct kern_history hist, *histkva; 2208 char *name = NULL; 2209 size_t namelen = 0; 2210 2211 if (histnl[0].n_value == 0) { 2212 warnx("kernel history is not compiled into the kernel."); 2213 return; 2214 } 2215 2216 deref_kptr((void *)histnl[X_KERN_HISTORIES].n_value, &histhead, 2217 sizeof(histhead), histnl[X_KERN_HISTORIES].n_name); 2218 2219 if (histhead.lh_first == NULL) { 2220 warnx("No active kernel history logs."); 2221 return; 2222 } 2223 2224 if (todo & HISTLIST) 2225 (void)printf("Active kernel histories:"); 2226 2227 for (histkva = LIST_FIRST(&histhead); histkva != NULL; 2228 histkva = LIST_NEXT(&hist, list)) { 2229 deref_kptr(histkva, &hist, sizeof(hist), "histkva"); 2230 if (name == NULL || hist.namelen > namelen) { 2231 if (name != NULL) 2232 free(name); 2233 namelen = hist.namelen; 2234 if ((name = malloc(namelen + 1)) == NULL) 2235 err(1, "malloc history name"); 2236 } 2237 2238 deref_kptr(hist.name, name, namelen, "history name"); 2239 name[namelen] = '\0'; 2240 if (todo & HISTLIST) 2241 (void)printf(" %s", name); 2242 else { 2243 /* 2244 * If we're dumping all histories, do it, else 2245 * check to see if this is the one we want. 2246 */ 2247 if (histname == NULL || strcmp(histname, name) == 0) { 2248 if (histname == NULL) 2249 (void)printf( 2250 "\nkernel history `%s':\n", name); 2251 hist_dodump(&hist); 2252 } 2253 } 2254 } 2255 2256 if (todo & HISTLIST) 2257 (void)putchar('\n'); 2258 2259 if (name != NULL) 2260 free(name); 2261 } 2262 2263 /* 2264 * Actually dump the history buffer at the specified KVA. 2265 */ 2266 void 2267 hist_dodump(struct kern_history *histp) 2268 { 2269 struct kern_history_ent *histents, *e; 2270 struct timeval tv; 2271 size_t histsize; 2272 char *fmt = NULL, *fn = NULL; 2273 size_t fmtlen = 0, fnlen = 0; 2274 unsigned i; 2275 2276 histsize = sizeof(struct kern_history_ent) * histp->n; 2277 2278 if ((histents = malloc(histsize)) == NULL) 2279 err(1, "malloc history entries"); 2280 2281 (void)memset(histents, 0, histsize); 2282 2283 (void)printf("%"PRIu32" entries, next is %"PRIu32"\n", 2284 histp->n, histp->f); 2285 2286 deref_kptr(histp->e, histents, histsize, "history entries"); 2287 i = histp->f; 2288 do { 2289 e = &histents[i]; 2290 if (e->fmt != NULL) { 2291 if (fmt == NULL || e->fmtlen > fmtlen) { 2292 free(fmt); 2293 fmtlen = e->fmtlen; 2294 if ((fmt = malloc(fmtlen + 1)) == NULL) 2295 err(1, "malloc printf format"); 2296 } 2297 if (fn == NULL || e->fnlen > fnlen) { 2298 free(fn); 2299 fnlen = e->fnlen; 2300 if ((fn = malloc(fnlen + 1)) == NULL) 2301 err(1, "malloc function name"); 2302 } 2303 2304 deref_kptr(e->fmt, fmt, fmtlen, "printf format"); 2305 fmt[fmtlen] = '\0'; 2306 for (unsigned z = 0; z < fmtlen - 1; z++) { 2307 if (fmt[z] == '%' && fmt[z+1] == 's') 2308 fmt[z+1] = 'p'; 2309 } 2310 2311 deref_kptr(e->fn, fn, fnlen, "function name"); 2312 fn[fnlen] = '\0'; 2313 2314 bintime2timeval(&e->bt, &tv); 2315 (void)printf("%06ld.%06ld ", (long int)tv.tv_sec, 2316 (long int)tv.tv_usec); 2317 (void)printf("%s#%" PRId32 "@%" PRId32 ": ", 2318 fn, e->call, e->cpunum); 2319 (void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]); 2320 (void)putchar('\n'); 2321 } 2322 i = (i + 1) % histp->n; 2323 } while (i != histp->f); 2324 2325 free(histents); 2326 free(fmt); 2327 free(fn); 2328 } 2329 2330 void 2331 hist_traverse_sysctl(int todo, const char *histname) 2332 { 2333 int error; 2334 int mib[4]; 2335 unsigned int i; 2336 size_t len, miblen; 2337 struct sysctlnode query, histnode[32]; 2338 2339 /* retrieve names of available histories */ 2340 miblen = __arraycount(mib); 2341 error = sysctlnametomib("kern.hist", mib, &miblen); 2342 if (error != 0) { 2343 if (errno == ENOENT) { 2344 warnx("kernel history is not compiled into the kernel."); 2345 return; 2346 } else 2347 err(EXIT_FAILURE, "nametomib kern.hist failed"); 2348 } 2349 2350 /* get the list of nodenames below kern.hist */ 2351 mib[2] = CTL_QUERY; 2352 memset(&query, 0, sizeof(query)); 2353 query.sysctl_flags = SYSCTL_VERSION; 2354 len = sizeof(histnode); 2355 error = sysctl(mib, 3, &histnode[0], &len, &query, sizeof(query)); 2356 if (error != 0) { 2357 err(1, "query failed"); 2358 return; 2359 } 2360 if (len == 0) { 2361 warnx("No active kernel history logs."); 2362 return; 2363 } 2364 2365 len = len / sizeof(histnode[0]); /* get # of entries returned */ 2366 2367 if (todo & HISTLIST) 2368 (void)printf("Active kernel histories:"); 2369 2370 for (i = 0; i < len; i++) { 2371 if (todo & HISTLIST) 2372 (void)printf(" %s", histnode[i].sysctl_name); 2373 else { 2374 /* 2375 * If we're dumping all histories, do it, else 2376 * check to see if this is the one we want. 2377 */ 2378 if (histname == NULL || 2379 strcmp(histname, histnode[i].sysctl_name) == 0) { 2380 if (histname == NULL) 2381 (void)printf( 2382 "\nkernel history `%s':\n", 2383 histnode[i].sysctl_name); 2384 mib[2] = histnode[i].sysctl_num; 2385 mib[3] = CTL_EOL; 2386 hist_dodump_sysctl(mib, 4); 2387 } 2388 } 2389 } 2390 2391 if (todo & HISTLIST) 2392 (void)putchar('\n'); 2393 else if (mib[2] == CTL_QUERY) 2394 warnx("history %s not found", histname); 2395 } 2396 2397 /* 2398 * Actually dump the history buffer at the specified KVA. 2399 */ 2400 void 2401 hist_dodump_sysctl(int mib[], unsigned int miblen) 2402 { 2403 struct sysctl_history *hist; 2404 struct timeval tv; 2405 struct sysctl_history_event *e; 2406 size_t histsize; 2407 char *strp; 2408 unsigned i; 2409 char *fmt = NULL, *fn = NULL; 2410 2411 hist = NULL; 2412 histsize = 0; 2413 do { 2414 errno = 0; 2415 if (sysctl(mib, miblen, hist, &histsize, NULL, 0) == 0) 2416 break; 2417 if (errno != ENOMEM) 2418 break; 2419 if ((hist = realloc(hist, histsize)) == NULL) 2420 errx(1, "realloc history buffer"); 2421 } while (errno == ENOMEM); 2422 if (errno != 0) 2423 err(1, "sysctl failed"); 2424 2425 strp = (char *)(&hist->sh_events[hist->sh_numentries]); 2426 2427 (void)printf("%"PRIu32" entries, next is %"PRIu32"\n", 2428 hist->sh_numentries, 2429 hist->sh_nextfree); 2430 2431 i = hist->sh_nextfree; 2432 2433 do { 2434 e = &hist->sh_events[i]; 2435 if (e->she_fmtoffset != 0) { 2436 fmt = &strp[e->she_fmtoffset]; 2437 size_t fmtlen = strlen(fmt); 2438 for (unsigned z = 0; z < fmtlen - 1; z++) { 2439 if (fmt[z] == '%' && fmt[z+1] == 's') 2440 fmt[z+1] = 'p'; 2441 } 2442 fn = &strp[e->she_funcoffset]; 2443 bintime2timeval(&e->she_bintime, &tv); 2444 (void)printf("%06ld.%06ld %s#%"PRIu32"@%"PRIu32": ", 2445 (long int)tv.tv_sec, (long int)tv.tv_usec, 2446 fn, e->she_callnumber, e->she_cpunum); 2447 (void)printf(fmt, e->she_values[0], e->she_values[1], 2448 e->she_values[2], e->she_values[3]); 2449 (void)putchar('\n'); 2450 } 2451 i = (i + 1) % hist->sh_numentries; 2452 } while (i != hist->sh_nextfree); 2453 2454 free(hist); 2455 } 2456 2457 static void 2458 usage(void) 2459 { 2460 2461 (void)fprintf(stderr, 2462 "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname]\n" 2463 "\t\t[-M core] [-N system] [-n diskcount] [-u histname]\n" 2464 "[-w wait] [disks]\n", 2465 getprogname()); 2466 exit(1); 2467 } 2468