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