1 /* $NetBSD: vmstat.c,v 1.164 2008/11/28 05:58:22 dholland 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.164 2008/11/28 05:58:22 dholland Exp $"); 74 #endif 75 #endif /* not lint */ 76 77 #define __POOL_EXPOSE 78 79 #include <sys/param.h> 80 #include <sys/mount.h> 81 #include <sys/uio.h> 82 83 #include <sys/buf.h> 84 #include <sys/evcnt.h> 85 #include <sys/ioctl.h> 86 #include <sys/malloc.h> 87 #include <sys/mallocvar.h> 88 #include <sys/namei.h> 89 #include <sys/pool.h> 90 #include <sys/proc.h> 91 #include <sys/sched.h> 92 #include <sys/socket.h> 93 #include <sys/sysctl.h> 94 #include <sys/time.h> 95 #include <sys/user.h> 96 97 #include <uvm/uvm_extern.h> 98 #include <uvm/uvm_stat.h> 99 100 #include <net/if.h> 101 #include <netinet/in.h> 102 #include <netinet/in_var.h> 103 104 #include <ufs/ufs/inode.h> 105 106 #include <nfs/rpcv2.h> 107 #include <nfs/nfsproto.h> 108 #include <nfs/nfsnode.h> 109 110 #include <ctype.h> 111 #include <err.h> 112 #include <errno.h> 113 #include <fcntl.h> 114 #include <kvm.h> 115 #include <limits.h> 116 #include <nlist.h> 117 #undef n_hash 118 #include <paths.h> 119 #include <signal.h> 120 #include <stdio.h> 121 #include <stddef.h> 122 #include <stdlib.h> 123 #include <string.h> 124 #include <time.h> 125 #include <unistd.h> 126 #include <util.h> 127 128 #include "drvstats.h" 129 130 /* 131 * General namelist 132 */ 133 struct nlist namelist[] = 134 { 135 #define X_BOOTTIME 0 136 { .n_name = "_boottime" }, 137 #define X_HZ 1 138 { .n_name = "_hz" }, 139 #define X_STATHZ 2 140 { .n_name = "_stathz" }, 141 #define X_NCHSTATS 3 142 { .n_name = "_nchstats" }, 143 #define X_KMEMSTAT 4 144 { .n_name = "_kmemstatistics" }, 145 #define X_KMEMBUCKETS 5 146 { .n_name = "_kmembuckets" }, 147 #define X_ALLEVENTS 6 148 { .n_name = "_allevents" }, 149 #define X_POOLHEAD 7 150 { .n_name = "_pool_head" }, 151 #define X_UVMEXP 8 152 { .n_name = "_uvmexp" }, 153 #define X_TIME_SECOND 9 154 { .n_name = "_time_second" }, 155 #define X_TIME 10 156 { .n_name = "_time" }, 157 #define X_NL_SIZE 11 158 { .n_name = NULL }, 159 }; 160 161 /* 162 * Namelist for pre-evcnt interrupt counters. 163 */ 164 struct nlist intrnl[] = 165 { 166 #define X_INTRNAMES 0 167 { .n_name = "_intrnames" }, 168 #define X_EINTRNAMES 1 169 { .n_name = "_eintrnames" }, 170 #define X_INTRCNT 2 171 { .n_name = "_intrcnt" }, 172 #define X_EINTRCNT 3 173 { .n_name = "_eintrcnt" }, 174 #define X_INTRNL_SIZE 4 175 { .n_name = NULL }, 176 }; 177 178 179 /* 180 * Namelist for hash statistics 181 */ 182 struct nlist hashnl[] = 183 { 184 #define X_NFSNODE 0 185 { .n_name = "_nfsnodehash" }, 186 #define X_NFSNODETBL 1 187 { .n_name = "_nfsnodehashtbl" }, 188 #define X_IHASH 2 189 { .n_name = "_ihash" }, 190 #define X_IHASHTBL 3 191 { .n_name = "_ihashtbl" }, 192 #define X_BUFHASH 4 193 { .n_name = "_bufhash" }, 194 #define X_BUFHASHTBL 5 195 { .n_name = "_bufhashtbl" }, 196 #define X_UIHASH 6 197 { .n_name = "_uihash" }, 198 #define X_UIHASHTBL 7 199 { .n_name = "_uihashtbl" }, 200 #define X_IFADDRHASH 8 201 { .n_name = "_in_ifaddrhash" }, 202 #define X_IFADDRHASHTBL 9 203 { .n_name = "_in_ifaddrhashtbl" }, 204 #define X_NCHASH 10 205 { .n_name = "_nchash" }, 206 #define X_NCHASHTBL 11 207 { .n_name = "_nchashtbl" }, 208 #define X_NCVHASH 12 209 { .n_name = "_ncvhash" }, 210 #define X_NCVHASHTBL 13 211 { .n_name = "_ncvhashtbl" }, 212 #define X_HASHNL_SIZE 14 /* must be last */ 213 { .n_name = NULL }, 214 }; 215 216 /* 217 * Namelist for UVM histories 218 */ 219 struct nlist histnl[] = 220 { 221 { .n_name = "_uvm_histories" }, 222 #define X_UVM_HISTORIES 0 223 { .n_name = NULL }, 224 }; 225 226 227 #define KILO 1024 228 229 struct uvmexp uvmexp, ouvmexp; 230 int ndrives; 231 232 int winlines = 20; 233 234 kvm_t *kd; 235 236 #define FORKSTAT 1<<0 237 #define INTRSTAT 1<<1 238 #define MEMSTAT 1<<2 239 #define SUMSTAT 1<<3 240 #define EVCNTSTAT 1<<4 241 #define VMSTAT 1<<5 242 #define HISTLIST 1<<6 243 #define HISTDUMP 1<<7 244 #define HASHSTAT 1<<8 245 #define HASHLIST 1<<9 246 #define VMTOTAL 1<<10 247 #define POOLCACHESTAT 1<<11 248 249 /* 250 * Print single word. `ovflow' is number of characters didn't fit 251 * on the last word. `fmt' is a format string to print this word. 252 * It must contain asterisk for field width. `width' is a width 253 * occupied by this word. `fixed' is a number of constant chars in 254 * `fmt'. `val' is a value to be printed using format string `fmt'. 255 */ 256 #define PRWORD(ovflw, fmt, width, fixed, val) do { \ 257 (ovflw) += printf((fmt), \ 258 (width) - (fixed) - (ovflw) > 0 ? \ 259 (width) - (fixed) - (ovflw) : 0, \ 260 (val)) - (width); \ 261 if ((ovflw) < 0) \ 262 (ovflw) = 0; \ 263 } while (/* CONSTCOND */0) 264 265 void cpustats(int *); 266 void deref_kptr(const void *, void *, size_t, const char *); 267 void drvstats(int *); 268 void doevcnt(int verbose); 269 void dohashstat(int, int, const char *); 270 void dointr(int verbose); 271 void domem(void); 272 void dopool(int, int); 273 void dopoolcache(void); 274 void dosum(void); 275 void dovmstat(struct timespec *, int); 276 void print_total_hdr(void); 277 void dovmtotal(struct timespec *, int); 278 void kread(struct nlist *, int, void *, size_t); 279 int kreadc(struct nlist *, int, void *, size_t); 280 void needhdr(int); 281 long getuptime(void); 282 void printhdr(void); 283 long pct(long, long); 284 void usage(void); 285 void doforkst(void); 286 287 void hist_traverse(int, const char *); 288 void hist_dodump(struct uvm_history *); 289 290 int main(int, char **); 291 char **choosedrives(char **); 292 293 /* Namelist and memory file names. */ 294 char *nlistf, *memf; 295 296 /* allow old usage [vmstat 1] */ 297 #define BACKWARD_COMPATIBILITY 298 299 int 300 main(int argc, char *argv[]) 301 { 302 int c, todo, verbose, wide; 303 struct timespec interval; 304 int reps; 305 char errbuf[_POSIX2_LINE_MAX]; 306 gid_t egid = getegid(); 307 const char *histname, *hashname; 308 309 histname = hashname = NULL; 310 (void)setegid(getgid()); 311 memf = nlistf = NULL; 312 reps = todo = verbose = wide = 0; 313 interval.tv_sec = 0; 314 interval.tv_nsec = 0; 315 while ((c = getopt(argc, argv, "Cc:efh:HilLM:mN:stu:UvWw:")) != -1) { 316 switch (c) { 317 case 'c': 318 reps = atoi(optarg); 319 break; 320 case 'C': 321 todo |= POOLCACHESTAT; 322 break; 323 case 'e': 324 todo |= EVCNTSTAT; 325 break; 326 case 'f': 327 todo |= FORKSTAT; 328 break; 329 case 'h': 330 hashname = optarg; 331 /* FALLTHROUGH */ 332 case 'H': 333 todo |= HASHSTAT; 334 break; 335 case 'i': 336 todo |= INTRSTAT; 337 break; 338 case 'l': 339 todo |= HISTLIST; 340 break; 341 case 'L': 342 todo |= HASHLIST; 343 break; 344 case 'M': 345 memf = optarg; 346 break; 347 case 'm': 348 todo |= MEMSTAT; 349 break; 350 case 'N': 351 nlistf = optarg; 352 break; 353 case 's': 354 todo |= SUMSTAT; 355 break; 356 case 't': 357 todo |= VMTOTAL; 358 break; 359 case 'u': 360 histname = optarg; 361 /* FALLTHROUGH */ 362 case 'U': 363 todo |= HISTDUMP; 364 break; 365 case 'v': 366 verbose++; 367 break; 368 case 'W': 369 wide++; 370 break; 371 case 'w': 372 interval.tv_sec = atol(optarg); 373 break; 374 case '?': 375 default: 376 usage(); 377 } 378 } 379 argc -= optind; 380 argv += optind; 381 382 if (todo == 0) 383 todo = VMSTAT; 384 385 /* 386 * Discard setgid privileges. If not the running kernel, we toss 387 * them away totally so that bad guys can't print interesting stuff 388 * from kernel memory, otherwise switch back to kmem for the 389 * duration of the kvm_openfiles() call. 390 */ 391 if (nlistf != NULL || memf != NULL) 392 (void)setgid(getgid()); 393 else 394 (void)setegid(egid); 395 396 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 397 if (kd == NULL) 398 errx(1, "kvm_openfiles: %s", errbuf); 399 400 if (nlistf == NULL && memf == NULL) 401 (void)setgid(getgid()); 402 403 if ((c = kvm_nlist(kd, namelist)) != 0) { 404 int doexit = 0; 405 if (c == -1) 406 errx(1, "kvm_nlist: %s %s", "namelist", kvm_geterr(kd)); 407 for (c = 0; c < sizeof(namelist) / sizeof(namelist[0])-1; c++) 408 if (namelist[c].n_type == 0 && 409 c != X_TIME_SECOND && 410 c != X_TIME) { 411 if (doexit++ == 0) 412 (void)fprintf(stderr, "vmstat: undefined symbols:"); 413 (void)fprintf(stderr, " %s", 414 namelist[c].n_name); 415 } 416 if (doexit) { 417 (void)fputc('\n', stderr); 418 exit(1); 419 } 420 } 421 if (todo & INTRSTAT) 422 (void) kvm_nlist(kd, intrnl); 423 if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE) 424 errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd)); 425 if (kvm_nlist(kd, histnl) == -1) 426 errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd)); 427 428 if (todo & VMSTAT) { 429 struct winsize winsize; 430 431 (void)drvinit(0);/* Initialize disk stats, no disks selected. */ 432 433 (void)setgid(getgid()); /* don't need privs anymore */ 434 435 argv = choosedrives(argv); /* Select disks. */ 436 winsize.ws_row = 0; 437 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize); 438 if (winsize.ws_row > 0) 439 winlines = winsize.ws_row; 440 441 } 442 443 #ifdef BACKWARD_COMPATIBILITY 444 if (*argv) { 445 interval.tv_sec = atol(*argv); 446 if (*++argv) 447 reps = atoi(*argv); 448 } 449 #endif 450 451 if (interval.tv_sec) { 452 if (!reps) 453 reps = -1; 454 } else if (reps) 455 interval.tv_sec = 1; 456 457 458 /* 459 * Statistics dumping is incompatible with the default 460 * VMSTAT/dovmstat() output. So perform the interval/reps handling 461 * for it here. 462 */ 463 if ((todo & (VMSTAT|VMTOTAL)) == 0) { 464 for (;;) { 465 if (todo & (HISTLIST|HISTDUMP)) { 466 if ((todo & (HISTLIST|HISTDUMP)) == 467 (HISTLIST|HISTDUMP)) 468 errx(1, "you may list or dump," 469 " but not both!"); 470 hist_traverse(todo, histname); 471 (void)putchar('\n'); 472 } 473 if (todo & FORKSTAT) { 474 doforkst(); 475 (void)putchar('\n'); 476 } 477 if (todo & MEMSTAT) { 478 domem(); 479 dopool(verbose, wide); 480 (void)putchar('\n'); 481 } 482 if (todo & POOLCACHESTAT) { 483 dopoolcache(); 484 (void)putchar('\n'); 485 } 486 if (todo & SUMSTAT) { 487 dosum(); 488 (void)putchar('\n'); 489 } 490 if (todo & INTRSTAT) { 491 dointr(verbose); 492 (void)putchar('\n'); 493 } 494 if (todo & EVCNTSTAT) { 495 doevcnt(verbose); 496 (void)putchar('\n'); 497 } 498 if (todo & (HASHLIST|HASHSTAT)) { 499 if ((todo & (HASHLIST|HASHSTAT)) == 500 (HASHLIST|HASHSTAT)) 501 errx(1, "you may list or display," 502 " but not both!"); 503 dohashstat(verbose, todo, hashname); 504 (void)putchar('\n'); 505 } 506 507 fflush(stdout); 508 if (reps >= 0 && --reps <=0) 509 break; 510 (void)nanosleep(&interval, NULL); 511 } 512 } else { 513 if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) { 514 errx(1, "you may not both do vmstat and vmtotal"); 515 } 516 if (todo & VMSTAT) 517 dovmstat(&interval, reps); 518 if (todo & VMTOTAL) 519 dovmtotal(&interval, reps); 520 } 521 return 0; 522 } 523 524 char ** 525 choosedrives(char **argv) 526 { 527 int i; 528 529 /* 530 * Choose drives to be displayed. Priority goes to (in order) drives 531 * supplied as arguments, default drives. If everything isn't filled 532 * in and there are drives not taken care of, display the first few 533 * that fit. 534 */ 535 #define BACKWARD_COMPATIBILITY 536 for (ndrives = 0; *argv; ++argv) { 537 #ifdef BACKWARD_COMPATIBILITY 538 if (isdigit((unsigned char)**argv)) 539 break; 540 #endif 541 for (i = 0; i < ndrive; i++) { 542 if (strcmp(dr_name[i], *argv)) 543 continue; 544 drv_select[i] = 1; 545 ++ndrives; 546 break; 547 } 548 } 549 for (i = 0; i < ndrive && ndrives < 2; i++) { 550 if (drv_select[i]) 551 continue; 552 drv_select[i] = 1; 553 ++ndrives; 554 } 555 556 return (argv); 557 } 558 559 long 560 getuptime(void) 561 { 562 static struct timeval boottime; 563 struct timeval now; 564 time_t uptime, nowsec; 565 566 if (boottime.tv_sec == 0) 567 kread(namelist, X_BOOTTIME, &boottime, sizeof(boottime)); 568 if (kreadc(namelist, X_TIME_SECOND, &nowsec, sizeof(nowsec))) { 569 /* 570 * XXX this assignment dance can be removed once timeval tv_sec 571 * is SUS mandated time_t 572 */ 573 now.tv_sec = nowsec; 574 now.tv_usec = 0; 575 } else { 576 kread(namelist, X_TIME, &now, sizeof(now)); 577 } 578 uptime = now.tv_sec - boottime.tv_sec; 579 if (uptime <= 0 || uptime > 60*60*24*365*10) 580 errx(1, "time makes no sense; namelist must be wrong."); 581 return (uptime); 582 } 583 584 int hz, hdrcnt; 585 586 void 587 print_total_hdr() 588 { 589 590 (void)printf("procs memory\n"); 591 (void)printf("ru dw pw sl sw"); 592 (void)printf(" total-v active-v active-r"); 593 (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n"); 594 hdrcnt = winlines - 2; 595 } 596 597 void 598 dovmtotal(struct timespec *interval, int reps) 599 { 600 struct vmtotal total; 601 int mib[2]; 602 size_t size; 603 604 (void)signal(SIGCONT, needhdr); 605 606 for (hdrcnt = 1;;) { 607 if (!--hdrcnt) 608 print_total_hdr(); 609 if (memf != NULL) { 610 (void)printf( 611 "Unable to get vmtotals from crash dump.\n"); 612 (void)memset(&total, 0, sizeof(total)); 613 } else { 614 size = sizeof(total); 615 mib[0] = CTL_VM; 616 mib[1] = VM_METER; 617 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) { 618 (void)printf("Can't get vmtotals: %s\n", 619 strerror(errno)); 620 (void)memset(&total, 0, sizeof(total)); 621 } 622 } 623 (void)printf("%2d ", total.t_rq); 624 (void)printf("%2d ", total.t_dw); 625 (void)printf("%2d ", total.t_pw); 626 (void)printf("%2d ", total.t_sl); 627 (void)printf("%2d ", total.t_sw); 628 629 (void)printf("%9d ", total.t_vm); 630 (void)printf("%9d ", total.t_avm); 631 (void)printf("%9d ", total.t_arm); 632 (void)printf("%5d ", total.t_vmshr); 633 (void)printf("%6d ", total.t_avmshr); 634 (void)printf("%5d ", total.t_rmshr); 635 (void)printf("%6d ", total.t_armshr); 636 (void)printf("%5d", total.t_free); 637 638 (void)putchar('\n'); 639 640 (void)fflush(stdout); 641 if (reps >= 0 && --reps <= 0) 642 break; 643 644 (void)nanosleep(interval, NULL); 645 } 646 } 647 648 void 649 dovmstat(struct timespec *interval, int reps) 650 { 651 struct vmtotal total; 652 time_t uptime, halfuptime; 653 int mib[2]; 654 size_t size; 655 int pagesize = getpagesize(); 656 int ovflw; 657 658 uptime = getuptime(); 659 halfuptime = uptime / 2; 660 (void)signal(SIGCONT, needhdr); 661 662 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) 663 kread(namelist, X_STATHZ, &hz, sizeof(hz)); 664 if (!hz) 665 kread(namelist, X_HZ, &hz, sizeof(hz)); 666 667 for (hdrcnt = 1;;) { 668 if (!--hdrcnt) 669 printhdr(); 670 /* Read new disk statistics */ 671 cpureadstats(); 672 drvreadstats(); 673 tkreadstats(); 674 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp)); 675 if (memf != NULL) { 676 /* 677 * XXX Can't do this if we're reading a crash 678 * XXX dump because they're lazily-calculated. 679 */ 680 (void)printf( 681 "Unable to get vmtotals from crash dump.\n"); 682 (void)memset(&total, 0, sizeof(total)); 683 } else { 684 size = sizeof(total); 685 mib[0] = CTL_VM; 686 mib[1] = VM_METER; 687 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) { 688 (void)printf("Can't get vmtotals: %s\n", 689 strerror(errno)); 690 (void)memset(&total, 0, sizeof(total)); 691 } 692 } 693 ovflw = 0; 694 PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1); 695 PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw); 696 PRWORD(ovflw, " %*d", 2, 1, total.t_sw); 697 #define pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10)) 698 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */ 699 PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_avm)); 700 PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free)); 701 PRWORD(ovflw, " %*ld", 5, 1, 702 rate(uvmexp.faults - ouvmexp.faults)); 703 PRWORD(ovflw, " %*ld", 4, 1, 704 rate(uvmexp.pdreact - ouvmexp.pdreact)); 705 PRWORD(ovflw, " %*ld", 4, 1, 706 rate(uvmexp.pageins - ouvmexp.pageins)); 707 PRWORD(ovflw, " %*ld", 5, 1, 708 rate(uvmexp.pgswapout - ouvmexp.pgswapout)); 709 PRWORD(ovflw, " %*ld", 5, 1, 710 rate(uvmexp.pdfreed - ouvmexp.pdfreed)); 711 PRWORD(ovflw, " %*ld", 6, 2, 712 rate(uvmexp.pdscans - ouvmexp.pdscans)); 713 drvstats(&ovflw); 714 PRWORD(ovflw, " %*ld", 5, 1, 715 rate(uvmexp.intrs - ouvmexp.intrs)); 716 PRWORD(ovflw, " %*ld", 5, 1, 717 rate(uvmexp.syscalls - ouvmexp.syscalls)); 718 PRWORD(ovflw, " %*ld", 4, 1, 719 rate(uvmexp.swtch - ouvmexp.swtch)); 720 cpustats(&ovflw); 721 (void)putchar('\n'); 722 (void)fflush(stdout); 723 if (reps >= 0 && --reps <= 0) 724 break; 725 ouvmexp = uvmexp; 726 uptime = interval->tv_sec; 727 /* 728 * We round upward to avoid losing low-frequency events 729 * (i.e., >= 1 per interval but < 1 per second). 730 */ 731 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2; 732 (void)nanosleep(interval, NULL); 733 } 734 } 735 736 void 737 printhdr(void) 738 { 739 int i; 740 741 (void)printf(" procs memory page%*s", 23, ""); 742 if (ndrives > 0) 743 (void)printf("%s %*sfaults cpu\n", 744 ((ndrives > 1) ? "disks" : "disk"), 745 ((ndrives > 1) ? ndrives * 3 - 4 : 0), ""); 746 else 747 (void)printf("%*s faults cpu\n", 748 ndrives * 3, ""); 749 750 (void)printf(" r b w avm fre flt re pi po fr sr "); 751 for (i = 0; i < ndrive; i++) 752 if (drv_select[i]) 753 (void)printf("%c%c ", dr_name[i][0], 754 dr_name[i][strlen(dr_name[i]) - 1]); 755 (void)printf(" in sy cs us sy id\n"); 756 hdrcnt = winlines - 2; 757 } 758 759 /* 760 * Force a header to be prepended to the next output. 761 */ 762 void 763 /*ARGSUSED*/ 764 needhdr(int dummy) 765 { 766 767 hdrcnt = 1; 768 } 769 770 long 771 pct(long top, long bot) 772 { 773 long ans; 774 775 if (bot == 0) 776 return (0); 777 ans = (long)((quad_t)top * 100 / bot); 778 return (ans); 779 } 780 781 #define PCT(top, bot) (int)pct((long)(top), (long)(bot)) 782 783 void 784 dosum(void) 785 { 786 struct nchstats nchstats; 787 u_long nchtotal; 788 int mib[2]; 789 struct uvmexp_sysctl uvmexp2; 790 size_t ssize; 791 int active_kernel; 792 793 /* 794 * The "active" and "inactive" variables 795 * are now estimated by the kernel and sadly 796 * can not easily be dug out of a crash dump. 797 */ 798 ssize = sizeof(uvmexp2); 799 memset(&uvmexp2, 0, ssize); 800 active_kernel = (memf == NULL); 801 if (active_kernel) { 802 /* only on active kernel */ 803 mib[0] = CTL_VM; 804 mib[1] = VM_UVMEXP2; 805 if (sysctl(mib, 2, &uvmexp2, &ssize, NULL, 0) < 0) 806 fprintf(stderr, "%s: sysctl vm.uvmexp2 failed: %s", 807 getprogname(), strerror(errno)); 808 } 809 810 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp)); 811 812 (void)printf("%9u bytes per page\n", uvmexp.pagesize); 813 814 (void)printf("%9u page color%s\n", 815 uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s"); 816 817 (void)printf("%9u pages managed\n", uvmexp.npages); 818 (void)printf("%9u pages free\n", uvmexp.free); 819 if (active_kernel) { 820 (void)printf("%9" PRIu64 " pages active\n", uvmexp2.active); 821 (void)printf("%9" PRIu64 " pages inactive\n", uvmexp2.inactive); 822 } 823 (void)printf("%9u pages paging\n", uvmexp.paging); 824 (void)printf("%9u pages wired\n", uvmexp.wired); 825 (void)printf("%9u zero pages\n", uvmexp.zeropages); 826 (void)printf("%9u reserve pagedaemon pages\n", 827 uvmexp.reserve_pagedaemon); 828 (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel); 829 (void)printf("%9u anonymous pages\n", uvmexp.anonpages); 830 (void)printf("%9u cached file pages\n", uvmexp.filepages); 831 (void)printf("%9u cached executable pages\n", uvmexp.execpages); 832 833 (void)printf("%9u minimum free pages\n", uvmexp.freemin); 834 (void)printf("%9u target free pages\n", uvmexp.freetarg); 835 (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax); 836 837 (void)printf("%9u swap devices\n", uvmexp.nswapdev); 838 (void)printf("%9u swap pages\n", uvmexp.swpages); 839 (void)printf("%9u swap pages in use\n", uvmexp.swpginuse); 840 (void)printf("%9u swap allocations\n", uvmexp.nswget); 841 842 (void)printf("%9u total faults taken\n", uvmexp.faults); 843 (void)printf("%9u traps\n", uvmexp.traps); 844 (void)printf("%9u device interrupts\n", uvmexp.intrs); 845 (void)printf("%9u CPU context switches\n", uvmexp.swtch); 846 (void)printf("%9u software interrupts\n", uvmexp.softs); 847 (void)printf("%9u system calls\n", uvmexp.syscalls); 848 (void)printf("%9u pagein requests\n", uvmexp.pageins); 849 (void)printf("%9u pageout requests\n", uvmexp.pdpageouts); 850 (void)printf("%9u swap ins\n", uvmexp.swapins); 851 (void)printf("%9u swap outs\n", uvmexp.swapouts); 852 (void)printf("%9u pages swapped in\n", uvmexp.pgswapin); 853 (void)printf("%9u pages swapped out\n", uvmexp.pgswapout); 854 (void)printf("%9u forks total\n", uvmexp.forks); 855 (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait); 856 (void)printf("%9u forks shared address space with parent\n", 857 uvmexp.forks_sharevm); 858 (void)printf("%9u pagealloc zero wanted and avail\n", 859 uvmexp.pga_zerohit); 860 (void)printf("%9u pagealloc zero wanted and not avail\n", 861 uvmexp.pga_zeromiss); 862 (void)printf("%9u aborts of idle page zeroing\n", 863 uvmexp.zeroaborts); 864 (void)printf("%9u pagealloc desired color avail\n", 865 uvmexp.colorhit); 866 (void)printf("%9u pagealloc desired color not avail\n", 867 uvmexp.colormiss); 868 (void)printf("%9u pagealloc local cpu avail\n", 869 uvmexp.cpuhit); 870 (void)printf("%9u pagealloc local cpu not avail\n", 871 uvmexp.cpumiss); 872 873 (void)printf("%9u faults with no memory\n", uvmexp.fltnoram); 874 (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon); 875 (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait); 876 (void)printf("%9u faults found released page\n", uvmexp.fltpgrele); 877 (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck, 878 uvmexp.fltrelckok); 879 (void)printf("%9u anon page faults\n", uvmexp.fltanget); 880 (void)printf("%9u anon retry faults\n", uvmexp.fltanretry); 881 (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy); 882 (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap); 883 (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap); 884 (void)printf("%9u locked pager get faults\n", uvmexp.fltlget); 885 (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget); 886 (void)printf("%9u anon faults\n", uvmexp.flt_anon); 887 (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow); 888 (void)printf("%9u object faults\n", uvmexp.flt_obj); 889 (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy); 890 (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero); 891 892 (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke); 893 (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs); 894 (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout); 895 (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed); 896 (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans); 897 (void)printf("%9u anonymous pages scanned by daemon\n", 898 uvmexp.pdanscan); 899 (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan); 900 (void)printf("%9u pages reactivated\n", uvmexp.pdreact); 901 (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy); 902 (void)printf("%9u total pending pageouts\n", uvmexp.pdpending); 903 (void)printf("%9u pages deactivated\n", uvmexp.pddeact); 904 905 kread(namelist, X_NCHSTATS, &nchstats, sizeof(nchstats)); 906 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 907 nchstats.ncs_badhits + nchstats.ncs_falsehits + 908 nchstats.ncs_miss + nchstats.ncs_long; 909 (void)printf("%9lu total name lookups\n", nchtotal); 910 (void)printf("%9lu good hits\n", nchstats.ncs_goodhits); 911 (void)printf("%9lu negative hits\n", nchstats.ncs_neghits); 912 (void)printf("%9lu bad hits\n", nchstats.ncs_badhits); 913 (void)printf("%9lu false hits\n", nchstats.ncs_falsehits); 914 (void)printf("%9lu miss\n", nchstats.ncs_miss); 915 (void)printf("%9lu too long\n", nchstats.ncs_long); 916 (void)printf("%9lu pass2 hits\n", nchstats.ncs_pass2); 917 (void)printf("%9lu 2passes\n", nchstats.ncs_2passes); 918 (void)printf( 919 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", 920 "", PCT(nchstats.ncs_goodhits, nchtotal), 921 PCT(nchstats.ncs_neghits, nchtotal), 922 PCT(nchstats.ncs_pass2, nchtotal)); 923 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", 924 PCT(nchstats.ncs_badhits, nchtotal), 925 PCT(nchstats.ncs_falsehits, nchtotal), 926 PCT(nchstats.ncs_long, nchtotal)); 927 } 928 929 void 930 doforkst(void) 931 { 932 933 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp)); 934 935 (void)printf("%u forks total\n", uvmexp.forks); 936 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait); 937 (void)printf("%u forks shared address space with parent\n", 938 uvmexp.forks_sharevm); 939 } 940 941 void 942 drvstats(int *ovflwp) 943 { 944 int dn; 945 double etime; 946 int ovflw = *ovflwp; 947 948 /* Calculate disk stat deltas. */ 949 cpuswap(); 950 drvswap(); 951 tkswap(); 952 etime = cur.cp_etime; 953 954 for (dn = 0; dn < ndrive; ++dn) { 955 if (!drv_select[dn]) 956 continue; 957 PRWORD(ovflw, " %*.0f", 3, 1, 958 (cur.rxfer[dn] + cur.wxfer[dn]) / etime); 959 } 960 *ovflwp = ovflw; 961 } 962 963 void 964 cpustats(int *ovflwp) 965 { 966 int state; 967 double pcnt, total; 968 double stat_us, stat_sy, stat_id; 969 int ovflw = *ovflwp; 970 971 total = 0; 972 for (state = 0; state < CPUSTATES; ++state) 973 total += cur.cp_time[state]; 974 if (total) 975 pcnt = 100 / total; 976 else 977 pcnt = 0; 978 stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt; 979 stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt; 980 stat_id = cur.cp_time[CP_IDLE] * pcnt; 981 PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us); 982 PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1, 983 stat_sy); 984 PRWORD(ovflw, " %*.0f", 3, 1, stat_id); 985 *ovflwp = ovflw; 986 } 987 988 void 989 dointr(int verbose) 990 { 991 unsigned long *intrcnt, *ointrcnt; 992 unsigned long long inttotal, uptime; 993 int nintr, inamlen; 994 char *intrname, *ointrname; 995 struct evcntlist allevents; 996 struct evcnt evcnt, *evptr; 997 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX]; 998 999 inttotal = 0; 1000 uptime = getuptime(); 1001 (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate"); 1002 nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value; 1003 inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value; 1004 if (nintr != 0 && inamlen != 0) { 1005 ointrcnt = intrcnt = malloc((size_t)nintr); 1006 ointrname = intrname = malloc((size_t)inamlen); 1007 if (intrcnt == NULL || intrname == NULL) 1008 errx(1, "%s", ""); 1009 kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr); 1010 kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen); 1011 nintr /= sizeof(long); 1012 while (--nintr >= 0) { 1013 if (*intrcnt || verbose) 1014 (void)printf("%-34s %16llu %8llu\n", intrname, 1015 (unsigned long long)*intrcnt, 1016 (unsigned long long) 1017 (*intrcnt / uptime)); 1018 intrname += strlen(intrname) + 1; 1019 inttotal += *intrcnt++; 1020 } 1021 free(ointrcnt); 1022 free(ointrname); 1023 } 1024 1025 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents); 1026 evptr = TAILQ_FIRST(&allevents); 1027 while (evptr) { 1028 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed"); 1029 evptr = TAILQ_NEXT(&evcnt, ev_list); 1030 if (evcnt.ev_type != EVCNT_TYPE_INTR) 1031 continue; 1032 1033 if (evcnt.ev_count == 0 && !verbose) 1034 continue; 1035 1036 deref_kptr(evcnt.ev_group, evgroup, 1037 (size_t)evcnt.ev_grouplen + 1, "event chain trashed"); 1038 deref_kptr(evcnt.ev_name, evname, 1039 (size_t)evcnt.ev_namelen + 1, "event chain trashed"); 1040 1041 (void)printf("%s %s%*s %16llu %8llu\n", evgroup, evname, 1042 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "", 1043 (unsigned long long)evcnt.ev_count, 1044 (unsigned long long)(evcnt.ev_count / uptime)); 1045 1046 inttotal += evcnt.ev_count++; 1047 } 1048 (void)printf("%-34s %16llu %8llu\n", "Total", inttotal, 1049 (unsigned long long)(inttotal / uptime)); 1050 } 1051 1052 void 1053 doevcnt(int verbose) 1054 { 1055 static const char * evtypes [] = { "misc", "intr", "trap" }; 1056 unsigned long long uptime; 1057 struct evcntlist allevents; 1058 struct evcnt evcnt, *evptr; 1059 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX]; 1060 1061 /* XXX should print type! */ 1062 1063 uptime = getuptime(); 1064 (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate", "type"); 1065 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents); 1066 evptr = TAILQ_FIRST(&allevents); 1067 while (evptr) { 1068 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed"); 1069 1070 evptr = TAILQ_NEXT(&evcnt, ev_list); 1071 if (evcnt.ev_count == 0 && !verbose) 1072 continue; 1073 1074 deref_kptr(evcnt.ev_group, evgroup, 1075 (size_t)evcnt.ev_grouplen + 1, "event chain trashed"); 1076 deref_kptr(evcnt.ev_name, evname, 1077 (size_t)evcnt.ev_namelen + 1, "event chain trashed"); 1078 1079 (void)printf("%s %s%*s %16llu %8llu %s\n", evgroup, evname, 1080 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "", 1081 (unsigned long long)evcnt.ev_count, 1082 (unsigned long long)(evcnt.ev_count / uptime), 1083 (evcnt.ev_type < sizeof(evtypes)/sizeof(evtypes[0]) ? 1084 evtypes[evcnt.ev_type] : "?")); 1085 } 1086 } 1087 1088 static char memname[64]; 1089 1090 void 1091 domem(void) 1092 { 1093 struct kmembuckets *kp; 1094 struct malloc_type ks, *ksp; 1095 int i, j; 1096 int len, size, first; 1097 long totuse = 0, totfree = 0, totreq = 0; 1098 struct kmembuckets buckets[MINBUCKET + 16]; 1099 1100 kread(namelist, X_KMEMBUCKETS, buckets, sizeof(buckets)); 1101 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; 1102 i++, kp++) { 1103 if (kp->kb_calls == 0) 1104 continue; 1105 if (first) { 1106 (void)printf("Memory statistics by bucket size\n"); 1107 (void)printf( 1108 " Size In Use Free Requests HighWater Couldfree\n"); 1109 first = 0; 1110 } 1111 size = 1 << i; 1112 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size, 1113 kp->kb_total - kp->kb_totalfree, 1114 kp->kb_totalfree, kp->kb_calls, 1115 kp->kb_highwat, kp->kb_couldfree); 1116 totfree += size * kp->kb_totalfree; 1117 } 1118 1119 /* 1120 * If kmem statistics are not being gathered by the kernel, 1121 * first will still be 1. 1122 */ 1123 if (first) { 1124 warnx("Kmem statistics are not being gathered by the kernel."); 1125 return; 1126 } 1127 1128 (void)printf("\nMemory usage type by bucket size\n"); 1129 (void)printf(" Size Type(s)\n"); 1130 kp = &buckets[MINBUCKET]; 1131 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) { 1132 if (kp->kb_calls == 0) 1133 continue; 1134 first = 1; 1135 len = 8; 1136 for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp)); 1137 ksp != NULL; ksp = ks.ks_next) { 1138 deref_kptr(ksp, &ks, sizeof(ks), "malloc type"); 1139 if (ks.ks_calls == 0) 1140 continue; 1141 if ((ks.ks_size & j) == 0) 1142 continue; 1143 deref_kptr(ks.ks_shortdesc, memname, 1144 sizeof(memname), "malloc type name"); 1145 len += 2 + strlen(memname); 1146 if (first) 1147 (void)printf("%8d %s", j, memname); 1148 else 1149 (void)printf(","); 1150 if (len >= 80) { 1151 (void)printf("\n\t "); 1152 len = 10 + strlen(memname); 1153 } 1154 if (!first) 1155 (void)printf(" %s", memname); 1156 first = 0; 1157 } 1158 (void)putchar('\n'); 1159 } 1160 1161 (void)printf( 1162 "\nMemory statistics by type Type Kern\n"); 1163 (void)printf( 1164 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n"); 1165 for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp)); 1166 ksp != NULL; ksp = ks.ks_next) { 1167 deref_kptr(ksp, &ks, sizeof(ks), "malloc type"); 1168 if (ks.ks_calls == 0) 1169 continue; 1170 deref_kptr(ks.ks_shortdesc, memname, 1171 sizeof(memname), "malloc type name"); 1172 (void)printf("%15s %5ld %6ldK %6ldK %6ldK %10ld %5u %5u", 1173 memname, 1174 ks.ks_inuse, howmany(ks.ks_memuse, KILO), 1175 howmany(ks.ks_maxused, KILO), 1176 howmany(ks.ks_limit, KILO), ks.ks_calls, 1177 ks.ks_limblocks, ks.ks_mapblocks); 1178 first = 1; 1179 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 1180 if ((ks.ks_size & j) == 0) 1181 continue; 1182 if (first) 1183 (void)printf(" %d", j); 1184 else 1185 (void)printf(",%d", j); 1186 first = 0; 1187 } 1188 (void)printf("\n"); 1189 totuse += ks.ks_memuse; 1190 totreq += ks.ks_calls; 1191 } 1192 (void)printf("\nMemory totals: In Use Free Requests\n"); 1193 (void)printf(" %7ldK %6ldK %8ld\n\n", 1194 howmany(totuse, KILO), howmany(totfree, KILO), totreq); 1195 } 1196 1197 void 1198 dopool(int verbose, int wide) 1199 { 1200 int first, ovflw; 1201 void *addr; 1202 long total, inuse, this_total, this_inuse; 1203 TAILQ_HEAD(,pool) pool_head; 1204 struct pool pool, *pp = &pool; 1205 struct pool_allocator pa; 1206 char name[32], maxp[32]; 1207 1208 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head)); 1209 addr = TAILQ_FIRST(&pool_head); 1210 1211 total = inuse = 0; 1212 1213 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) { 1214 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed"); 1215 deref_kptr(pp->pr_alloc, &pa, sizeof(pa), 1216 "pool allocator trashed"); 1217 deref_kptr(pp->pr_wchan, name, sizeof(name), 1218 "pool wait channel trashed"); 1219 name[sizeof(name)-1] = '\0'; 1220 1221 if (first) { 1222 (void)printf("Memory resource pool statistics\n"); 1223 (void)printf( 1224 "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n", 1225 wide ? 16 : 11, "Name", 1226 wide ? 6 : 5, "Size", 1227 wide ? 12 : 9, "Requests", 1228 "Fail", 1229 wide ? 12 : 9, "Releases", 1230 wide ? " InUse" : "", 1231 wide ? " Avail" : "", 1232 wide ? 7 : 6, "Pgreq", 1233 wide ? 7 : 6, "Pgrel", 1234 "Npage", 1235 wide ? " PageSz" : "", 1236 "Hiwat", 1237 "Minpg", 1238 "Maxpg", 1239 "Idle", 1240 wide ? " Flags" : "", 1241 wide ? " Util" : ""); 1242 first = 0; 1243 } 1244 if (pp->pr_nget == 0 && !verbose) 1245 continue; 1246 if (pp->pr_maxpages == UINT_MAX) 1247 (void)snprintf(maxp, sizeof(maxp), "inf"); 1248 else 1249 (void)snprintf(maxp, sizeof(maxp), "%u", 1250 pp->pr_maxpages); 1251 ovflw = 0; 1252 PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name); 1253 PRWORD(ovflw, " %*u", wide ? 6 : 5, 1, pp->pr_size); 1254 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget); 1255 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail); 1256 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput); 1257 if (wide) 1258 PRWORD(ovflw, " %*u", 7, 1, pp->pr_nout); 1259 if (wide) 1260 PRWORD(ovflw, " %*u", 6, 1, pp->pr_nitems); 1261 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagealloc); 1262 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagefree); 1263 PRWORD(ovflw, " %*u", 6, 1, pp->pr_npages); 1264 if (wide) 1265 PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz); 1266 PRWORD(ovflw, " %*u", 6, 1, pp->pr_hiwat); 1267 PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages); 1268 PRWORD(ovflw, " %*s", 6, 1, maxp); 1269 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle); 1270 if (wide) 1271 PRWORD(ovflw, " 0x%0*x", 4, 1, 1272 pp->pr_flags | pp->pr_roflags); 1273 1274 this_inuse = pp->pr_nout * pp->pr_size; 1275 this_total = pp->pr_npages * pa.pa_pagesz; 1276 if (pp->pr_roflags & PR_RECURSIVE) { 1277 /* 1278 * Don't count in-use memory, since it's part 1279 * of another pool and will be accounted for 1280 * there. 1281 */ 1282 total += (this_total - this_inuse); 1283 } else { 1284 inuse += this_inuse; 1285 total += this_total; 1286 } 1287 if (wide) { 1288 if (this_total == 0) 1289 (void)printf(" ---"); 1290 else 1291 (void)printf(" %5.1f%%", 1292 (100.0 * this_inuse) / this_total); 1293 } 1294 (void)printf("\n"); 1295 } 1296 1297 inuse /= KILO; 1298 total /= KILO; 1299 (void)printf( 1300 "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n", 1301 inuse, total, (100.0 * inuse) / total); 1302 } 1303 1304 void 1305 dopoolcache(void) 1306 { 1307 struct pool_cache pool_cache, *pc = &pool_cache; 1308 pool_cache_cpu_t cache_cpu, *cc = &cache_cpu; 1309 TAILQ_HEAD(,pool) pool_head; 1310 struct pool pool, *pp = &pool; 1311 char name[32]; 1312 uint64_t cpuhit, cpumiss, tot; 1313 void *addr; 1314 int first, ovflw, i; 1315 double p; 1316 1317 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head)); 1318 addr = TAILQ_FIRST(&pool_head); 1319 1320 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) { 1321 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed"); 1322 if (pp->pr_cache == NULL) 1323 continue; 1324 deref_kptr(pp->pr_wchan, name, sizeof(name), 1325 "pool wait channel trashed"); 1326 deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed"); 1327 name[sizeof(name)-1] = '\0'; 1328 1329 cpuhit = 0; 1330 cpumiss = 0; 1331 for (i = 0; i < sizeof(pc->pc_cpus) / sizeof(pc->pc_cpus[0]); 1332 i++) { 1333 if ((addr = pc->pc_cpus[i]) == NULL) 1334 continue; 1335 deref_kptr(addr, cc, sizeof(*cc), 1336 "pool cache cpu trashed"); 1337 cpuhit += cc->cc_hits; 1338 cpumiss += cc->cc_misses; 1339 } 1340 1341 if (first) { 1342 (void)printf("Pool cache statistics.\n"); 1343 (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n", 1344 12, "Name", 1345 6, "Spin", 1346 6, "GrpSz", 1347 5, "Full", 1348 5, "Emty", 1349 10, "PoolLayer", 1350 11, "CacheLayer", 1351 6, "Hit%", 1352 12, "CpuLayer", 1353 6, "Hit%" 1354 ); 1355 first = 0; 1356 } 1357 1358 ovflw = 0; 1359 PRWORD(ovflw, "%-*s", 13, 1, name); 1360 PRWORD(ovflw, " %*llu", 6, 1, (long long)pc->pc_contended); 1361 PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize); 1362 PRWORD(ovflw, " %*u", 5, 1, pc->pc_nfull); 1363 PRWORD(ovflw, " %*u", 5, 1, pc->pc_nempty); 1364 PRWORD(ovflw, " %*llu", 10, 1, (long long)pc->pc_misses); 1365 1366 tot = pc->pc_hits + pc->pc_misses; 1367 p = pc->pc_hits * 100.0 / (tot); 1368 PRWORD(ovflw, " %*llu", 11, 1, (long long)tot); 1369 PRWORD(ovflw, " %*.1f", 6, 1, p); 1370 1371 tot = cpuhit + cpumiss; 1372 p = cpuhit * 100.0 / (tot); 1373 PRWORD(ovflw, " %*llu", 12, 1, (long long)tot); 1374 PRWORD(ovflw, " %*.1f", 6, 1, p); 1375 printf("\n"); 1376 } 1377 } 1378 1379 enum hashtype { /* from <sys/systm.h> */ 1380 HASH_LIST, 1381 HASH_TAILQ 1382 }; 1383 1384 struct uidinfo { /* XXX: no kernel header file */ 1385 LIST_ENTRY(uidinfo) ui_hash; 1386 uid_t ui_uid; 1387 long ui_proccnt; 1388 }; 1389 1390 struct kernel_hash { 1391 const char * description; /* description */ 1392 int hashsize; /* nlist index for hash size */ 1393 int hashtbl; /* nlist index for hash table */ 1394 enum hashtype type; /* type of hash table */ 1395 size_t offset; /* offset of {LIST,TAILQ}_NEXT */ 1396 } khashes[] = 1397 { 1398 { 1399 "buffer hash", 1400 X_BUFHASH, X_BUFHASHTBL, 1401 HASH_LIST, offsetof(struct buf, b_hash) 1402 }, { 1403 "inode cache (ihash)", 1404 X_IHASH, X_IHASHTBL, 1405 HASH_LIST, offsetof(struct inode, i_hash) 1406 }, { 1407 "ipv4 address -> interface hash", 1408 X_IFADDRHASH, X_IFADDRHASHTBL, 1409 HASH_LIST, offsetof(struct in_ifaddr, ia_hash), 1410 }, { 1411 "name cache hash", 1412 X_NCHASH, X_NCHASHTBL, 1413 HASH_LIST, offsetof(struct namecache, nc_hash), 1414 }, { 1415 "name cache directory hash", 1416 X_NCVHASH, X_NCVHASHTBL, 1417 HASH_LIST, offsetof(struct namecache, nc_vhash), 1418 }, { 1419 "user info (uid -> used processes) hash", 1420 X_UIHASH, X_UIHASHTBL, 1421 HASH_LIST, offsetof(struct uidinfo, ui_hash), 1422 }, { 1423 NULL, -1, -1, 0, 0, 1424 } 1425 }; 1426 1427 void 1428 dohashstat(int verbose, int todo, const char *hashname) 1429 { 1430 LIST_HEAD(, generic) *hashtbl_list; 1431 TAILQ_HEAD(, generic) *hashtbl_tailq; 1432 struct kernel_hash *curhash; 1433 void *hashaddr, *hashbuf, *nhashbuf, *nextaddr; 1434 size_t elemsize, hashbufsize, thissize; 1435 u_long hashsize; 1436 int i, used, items, chain, maxchain; 1437 1438 hashbuf = NULL; 1439 hashbufsize = 0; 1440 1441 if (todo & HASHLIST) { 1442 (void)printf("Supported hashes:\n"); 1443 for (curhash = khashes; curhash->description; curhash++) { 1444 if (hashnl[curhash->hashsize].n_value == 0 || 1445 hashnl[curhash->hashtbl].n_value == 0) 1446 continue; 1447 (void)printf("\t%-16s%s\n", 1448 hashnl[curhash->hashsize].n_name + 1, 1449 curhash->description); 1450 } 1451 return; 1452 } 1453 1454 if (hashname != NULL) { 1455 for (curhash = khashes; curhash->description; curhash++) { 1456 if (strcmp(hashnl[curhash->hashsize].n_name + 1, 1457 hashname) == 0 && 1458 hashnl[curhash->hashsize].n_value != 0 && 1459 hashnl[curhash->hashtbl].n_value != 0) 1460 break; 1461 } 1462 if (curhash->description == NULL) { 1463 warnx("%s: no such hash", hashname); 1464 return; 1465 } 1466 } 1467 1468 (void)printf( 1469 "%-16s %8s %8s %8s %8s %8s %8s\n" 1470 "%-16s %8s %8s %8s %8s %8s %8s\n", 1471 "", "total", "used", "util", "num", "average", "maximum", 1472 "hash table", "buckets", "buckets", "%", "items", "chain", 1473 "chain"); 1474 1475 for (curhash = khashes; curhash->description; curhash++) { 1476 if (hashnl[curhash->hashsize].n_value == 0 || 1477 hashnl[curhash->hashtbl].n_value == 0) 1478 continue; 1479 if (hashname != NULL && 1480 strcmp(hashnl[curhash->hashsize].n_name + 1, hashname)) 1481 continue; 1482 elemsize = curhash->type == HASH_LIST ? 1483 sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq); 1484 deref_kptr((void *)hashnl[curhash->hashsize].n_value, 1485 &hashsize, sizeof(hashsize), 1486 hashnl[curhash->hashsize].n_name); 1487 hashsize++; 1488 deref_kptr((void *)hashnl[curhash->hashtbl].n_value, 1489 &hashaddr, sizeof(hashaddr), 1490 hashnl[curhash->hashtbl].n_name); 1491 if (verbose) 1492 (void)printf( 1493 "%s %lu, %s %p, offset %ld, elemsize %llu\n", 1494 hashnl[curhash->hashsize].n_name + 1, hashsize, 1495 hashnl[curhash->hashtbl].n_name + 1, hashaddr, 1496 (long)curhash->offset, 1497 (unsigned long long)elemsize); 1498 thissize = hashsize * elemsize; 1499 if (hashbuf == NULL || thissize > hashbufsize) { 1500 if ((nhashbuf = realloc(hashbuf, thissize)) == NULL) 1501 errx(1, "malloc hashbuf %llu", 1502 (unsigned long long)hashbufsize); 1503 hashbuf = nhashbuf; 1504 hashbufsize = thissize; 1505 } 1506 deref_kptr(hashaddr, hashbuf, thissize, 1507 hashnl[curhash->hashtbl].n_name); 1508 used = 0; 1509 items = maxchain = 0; 1510 if (curhash->type == HASH_LIST) { 1511 hashtbl_list = hashbuf; 1512 hashtbl_tailq = NULL; 1513 } else { 1514 hashtbl_list = NULL; 1515 hashtbl_tailq = hashbuf; 1516 } 1517 for (i = 0; i < hashsize; i++) { 1518 if (curhash->type == HASH_LIST) 1519 nextaddr = LIST_FIRST(&hashtbl_list[i]); 1520 else 1521 nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]); 1522 if (nextaddr == NULL) 1523 continue; 1524 if (verbose) 1525 (void)printf("%5d: %p\n", i, nextaddr); 1526 used++; 1527 chain = 0; 1528 do { 1529 chain++; 1530 deref_kptr((char *)nextaddr + curhash->offset, 1531 &nextaddr, sizeof(void *), 1532 "hash chain corrupted"); 1533 if (verbose > 1) 1534 (void)printf("got nextaddr as %p\n", 1535 nextaddr); 1536 } while (nextaddr != NULL); 1537 items += chain; 1538 if (verbose && chain > 1) 1539 (void)printf("\tchain = %d\n", chain); 1540 if (chain > maxchain) 1541 maxchain = chain; 1542 } 1543 (void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n", 1544 hashnl[curhash->hashsize].n_name + 1, 1545 hashsize, used, used * 100.0 / hashsize, 1546 items, used ? (double)items / used : 0.0, maxchain); 1547 } 1548 } 1549 1550 /* 1551 * kreadc like kread but returns 1 if sucessful, 0 otherwise 1552 */ 1553 int 1554 kreadc(struct nlist *nl, int nlx, void *addr, size_t size) 1555 { 1556 const char *sym; 1557 1558 sym = nl[nlx].n_name; 1559 if (*sym == '_') 1560 ++sym; 1561 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) 1562 return 0; 1563 deref_kptr((void *)nl[nlx].n_value, addr, size, sym); 1564 return 1; 1565 } 1566 1567 /* 1568 * kread reads something from the kernel, given its nlist index in namelist[]. 1569 */ 1570 void 1571 kread(struct nlist *nl, int nlx, void *addr, size_t size) 1572 { 1573 const char *sym; 1574 1575 sym = nl[nlx].n_name; 1576 if (*sym == '_') 1577 ++sym; 1578 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) 1579 errx(1, "symbol %s not defined", sym); 1580 deref_kptr((void *)nl[nlx].n_value, addr, size, sym); 1581 } 1582 1583 /* 1584 * Dereference the kernel pointer `kptr' and fill in the local copy 1585 * pointed to by `ptr'. The storage space must be pre-allocated, 1586 * and the size of the copy passed in `len'. 1587 */ 1588 void 1589 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg) 1590 { 1591 1592 if (*msg == '_') 1593 msg++; 1594 if (kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len) 1595 errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd)); 1596 } 1597 1598 /* 1599 * Traverse the UVM history buffers, performing the requested action. 1600 * 1601 * Note, we assume that if we're not listing, we're dumping. 1602 */ 1603 void 1604 hist_traverse(int todo, const char *histname) 1605 { 1606 struct uvm_history_head histhead; 1607 struct uvm_history hist, *histkva; 1608 char *name = NULL; 1609 size_t namelen = 0; 1610 1611 if (histnl[0].n_value == 0) { 1612 warnx("UVM history is not compiled into the kernel."); 1613 return; 1614 } 1615 1616 deref_kptr((void *)histnl[X_UVM_HISTORIES].n_value, &histhead, 1617 sizeof(histhead), histnl[X_UVM_HISTORIES].n_name); 1618 1619 if (histhead.lh_first == NULL) { 1620 warnx("No active UVM history logs."); 1621 return; 1622 } 1623 1624 if (todo & HISTLIST) 1625 (void)printf("Active UVM histories:"); 1626 1627 for (histkva = LIST_FIRST(&histhead); histkva != NULL; 1628 histkva = LIST_NEXT(&hist, list)) { 1629 deref_kptr(histkva, &hist, sizeof(hist), "histkva"); 1630 if (name == NULL || hist.namelen > namelen) { 1631 if (name != NULL) 1632 free(name); 1633 namelen = hist.namelen; 1634 if ((name = malloc(namelen + 1)) == NULL) 1635 err(1, "malloc history name"); 1636 } 1637 1638 deref_kptr(hist.name, name, namelen, "history name"); 1639 name[namelen] = '\0'; 1640 if (todo & HISTLIST) 1641 (void)printf(" %s", name); 1642 else { 1643 /* 1644 * If we're dumping all histories, do it, else 1645 * check to see if this is the one we want. 1646 */ 1647 if (histname == NULL || strcmp(histname, name) == 0) { 1648 if (histname == NULL) 1649 (void)printf( 1650 "\nUVM history `%s':\n", name); 1651 hist_dodump(&hist); 1652 } 1653 } 1654 } 1655 1656 if (todo & HISTLIST) 1657 (void)putchar('\n'); 1658 1659 if (name != NULL) 1660 free(name); 1661 } 1662 1663 /* 1664 * Actually dump the history buffer at the specified KVA. 1665 */ 1666 void 1667 hist_dodump(struct uvm_history *histp) 1668 { 1669 struct uvm_history_ent *histents, *e; 1670 size_t histsize; 1671 char *fmt = NULL, *fn = NULL; 1672 size_t fmtlen = 0, fnlen = 0; 1673 int i; 1674 1675 histsize = sizeof(struct uvm_history_ent) * histp->n; 1676 1677 if ((histents = malloc(histsize)) == NULL) 1678 err(1, "malloc history entries"); 1679 1680 (void)memset(histents, 0, histsize); 1681 1682 deref_kptr(histp->e, histents, histsize, "history entries"); 1683 i = histp->f; 1684 do { 1685 e = &histents[i]; 1686 if (e->fmt != NULL) { 1687 if (fmt == NULL || e->fmtlen > fmtlen) { 1688 if (fmt != NULL) 1689 free(fmt); 1690 fmtlen = e->fmtlen; 1691 if ((fmt = malloc(fmtlen + 1)) == NULL) 1692 err(1, "malloc printf format"); 1693 } 1694 if (fn == NULL || e->fnlen > fnlen) { 1695 if (fn != NULL) 1696 free(fn); 1697 fnlen = e->fnlen; 1698 if ((fn = malloc(fnlen + 1)) == NULL) 1699 err(1, "malloc function name"); 1700 } 1701 1702 deref_kptr(e->fmt, fmt, fmtlen, "printf format"); 1703 fmt[fmtlen] = '\0'; 1704 1705 deref_kptr(e->fn, fn, fnlen, "function name"); 1706 fn[fnlen] = '\0'; 1707 1708 (void)printf("%06ld.%06ld ", (long int)e->tv.tv_sec, 1709 (long int)e->tv.tv_usec); 1710 (void)printf("%s#%ld: ", fn, e->call); 1711 (void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]); 1712 (void)putchar('\n'); 1713 } 1714 i = (i + 1) % histp->n; 1715 } while (i != histp->f); 1716 1717 free(histents); 1718 if (fmt != NULL) 1719 free(fmt); 1720 if (fn != NULL) 1721 free(fn); 1722 } 1723 1724 void 1725 usage(void) 1726 { 1727 1728 (void)fprintf(stderr, 1729 "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n" 1730 "\t\t[-u histname] [-w wait] [disks]\n", getprogname()); 1731 exit(1); 1732 } 1733