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