1 /* $NetBSD: vmstat.c,v 1.107 2002/11/22 13:30:34 simonb Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000, 2001 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 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 /* 42 * Copyright (c) 1980, 1986, 1991, 1993 43 * The Regents of the University of California. All rights reserved. 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. All advertising materials mentioning features or use of this software 54 * must display the following acknowledgement: 55 * This product includes software developed by the University of 56 * California, Berkeley and its contributors. 57 * 4. Neither the name of the University nor the names of its contributors 58 * may be used to endorse or promote products derived from this software 59 * without specific prior written permission. 60 * 61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 71 * SUCH DAMAGE. 72 */ 73 74 #include <sys/cdefs.h> 75 #ifndef lint 76 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\n\ 77 The Regents of the University of California. All rights reserved.\n"); 78 #endif /* not lint */ 79 80 #ifndef lint 81 #if 0 82 static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95"; 83 #else 84 __RCSID("$NetBSD: vmstat.c,v 1.107 2002/11/22 13:30:34 simonb Exp $"); 85 #endif 86 #endif /* not lint */ 87 88 #define __POOL_EXPOSE 89 90 #include <sys/param.h> 91 #include <sys/mount.h> 92 #include <sys/uio.h> 93 94 #include <sys/buf.h> 95 #include <sys/device.h> 96 #include <sys/dkstat.h> 97 #include <sys/ioctl.h> 98 #include <sys/malloc.h> 99 #include <sys/namei.h> 100 #include <sys/pool.h> 101 #include <sys/proc.h> 102 #include <sys/sched.h> 103 #include <sys/socket.h> 104 #include <sys/sysctl.h> 105 #include <sys/time.h> 106 #include <sys/user.h> 107 108 #include <uvm/uvm_extern.h> 109 #include <uvm/uvm_stat.h> 110 111 #include <net/if.h> 112 #include <netinet/in.h> 113 #include <netinet/in_var.h> 114 115 #include <ufs/ufs/inode.h> 116 117 #include <nfs/rpcv2.h> 118 #include <nfs/nfsproto.h> 119 #include <nfs/nfsnode.h> 120 121 #include <ctype.h> 122 #include <err.h> 123 #include <errno.h> 124 #include <fcntl.h> 125 #include <kvm.h> 126 #include <limits.h> 127 #include <nlist.h> 128 #undef n_hash 129 #include <paths.h> 130 #include <signal.h> 131 #include <stdio.h> 132 #include <stddef.h> 133 #include <stdlib.h> 134 #include <string.h> 135 #include <time.h> 136 #include <unistd.h> 137 #include <util.h> 138 139 #include "dkstats.h" 140 141 /* 142 * General namelist 143 */ 144 struct nlist namelist[] = 145 { 146 #define X_BOOTTIME 0 147 { "_boottime" }, 148 #define X_HZ 1 149 { "_hz" }, 150 #define X_STATHZ 2 151 { "_stathz" }, 152 #define X_NCHSTATS 3 153 { "_nchstats" }, 154 #define X_INTRNAMES 4 155 { "_intrnames" }, 156 #define X_EINTRNAMES 5 157 { "_eintrnames" }, 158 #define X_INTRCNT 6 159 { "_intrcnt" }, 160 #define X_EINTRCNT 7 161 { "_eintrcnt" }, 162 #define X_KMEMSTAT 8 163 { "_kmemstats" }, 164 #define X_KMEMBUCKETS 9 165 { "_bucket" }, 166 #define X_ALLEVENTS 10 167 { "_allevents" }, 168 #define X_POOLHEAD 11 169 { "_pool_head" }, 170 #define X_UVMEXP 12 171 { "_uvmexp" }, 172 #define X_TIME 13 173 { "_time" }, 174 #define X_END 14 175 { NULL }, 176 }; 177 178 /* 179 * Namelist for hash statistics 180 */ 181 struct nlist hashnl[] = 182 { 183 #define X_NFSNODE 0 184 { "_nfsnodehash" }, 185 #define X_NFSNODETBL 1 186 { "_nfsnodehashtbl" }, 187 #define X_IHASH 2 188 { "_ihash" }, 189 #define X_IHASHTBL 3 190 { "_ihashtbl" }, 191 #define X_BUFHASH 4 192 { "_bufhash" }, 193 #define X_BUFHASHTBL 5 194 { "_bufhashtbl" }, 195 #define X_PIDHASH 6 196 { "_pidhash" }, 197 #define X_PIDHASHTBL 7 198 { "_pidhashtbl" }, 199 #define X_PGRPHASH 8 200 { "_pgrphash" }, 201 #define X_PGRPHASHTBL 9 202 { "_pgrphashtbl" }, 203 #define X_UIHASH 10 204 { "_uihash" }, 205 #define X_UIHASHTBL 11 206 { "_uihashtbl" }, 207 #define X_IFADDRHASH 12 208 { "_in_ifaddrhash" }, 209 #define X_IFADDRHASHTBL 13 210 { "_in_ifaddrhashtbl" }, 211 #define X_NCHASH 14 212 { "_nchash" }, 213 #define X_NCHASHTBL 15 214 { "_nchashtbl" }, 215 #define X_NCVHASH 16 216 { "_ncvhash" }, 217 #define X_NCVHASHTBL 17 218 { "_ncvhashtbl" }, 219 #define X_HASHNL_SIZE 18 /* must be last */ 220 { NULL }, 221 222 }; 223 224 /* 225 * Namelist for UVM histories 226 */ 227 struct nlist histnl[] = 228 { 229 { "_uvm_histories" }, 230 #define X_UVM_HISTORIES 0 231 { NULL }, 232 }; 233 234 235 236 struct uvmexp uvmexp, ouvmexp; 237 int ndrives; 238 239 int winlines = 20; 240 241 kvm_t *kd; 242 243 #define FORKSTAT 1<<0 244 #define INTRSTAT 1<<1 245 #define MEMSTAT 1<<2 246 #define SUMSTAT 1<<3 247 #define EVCNTSTAT 1<<4 248 #define VMSTAT 1<<5 249 #define HISTLIST 1<<6 250 #define HISTDUMP 1<<7 251 #define HASHSTAT 1<<8 252 #define HASHLIST 1<<9 253 254 void cpustats(void); 255 void deref_kptr(const void *, void *, size_t, const char *); 256 void dkstats(void); 257 void doevcnt(int verbose); 258 void dohashstat(int, int, const char *); 259 void dointr(int verbose); 260 void domem(void); 261 void dopool(int); 262 void dopoolcache(struct pool *, int); 263 void dosum(void); 264 void dovmstat(struct timespec *, int); 265 void kread(int, void *, size_t); 266 void needhdr(int); 267 long getuptime(void); 268 void printhdr(void); 269 long pct(long, long); 270 void usage(void); 271 void doforkst(void); 272 273 void hist_traverse(int, const char *); 274 void hist_dodump(struct uvm_history *); 275 276 int main(int, char **); 277 char **choosedrives(char **); 278 279 /* Namelist and memory file names. */ 280 char *nlistf, *memf; 281 282 /* allow old usage [vmstat 1] */ 283 #define BACKWARD_COMPATIBILITY 284 285 int 286 main(int argc, char *argv[]) 287 { 288 int c, todo, verbose; 289 struct timespec interval; 290 int reps; 291 char errbuf[_POSIX2_LINE_MAX]; 292 gid_t egid = getegid(); 293 const char *histname, *hashname; 294 295 histname = hashname = NULL; 296 (void)setegid(getgid()); 297 memf = nlistf = NULL; 298 reps = todo = verbose = 0; 299 interval.tv_sec = 0; 300 interval.tv_nsec = 0; 301 while ((c = getopt(argc, argv, "c:efh:HilLM:mN:suUvw:")) != -1) { 302 switch (c) { 303 case 'c': 304 reps = atoi(optarg); 305 break; 306 case 'e': 307 todo |= EVCNTSTAT; 308 break; 309 case 'f': 310 todo |= FORKSTAT; 311 break; 312 case 'h': 313 hashname = optarg; 314 /* FALLTHROUGH */ 315 case 'H': 316 todo |= HASHSTAT; 317 break; 318 case 'i': 319 todo |= INTRSTAT; 320 break; 321 case 'l': 322 todo |= HISTLIST; 323 break; 324 case 'L': 325 todo |= HASHLIST; 326 break; 327 case 'M': 328 memf = optarg; 329 break; 330 case 'm': 331 todo |= MEMSTAT; 332 break; 333 case 'N': 334 nlistf = optarg; 335 break; 336 case 's': 337 todo |= SUMSTAT; 338 break; 339 case 'u': 340 histname = optarg; 341 /* FALLTHROUGH */ 342 case 'U': 343 todo |= HISTDUMP; 344 break; 345 case 'v': 346 verbose++; 347 break; 348 case 'w': 349 interval.tv_sec = atol(optarg); 350 break; 351 case '?': 352 default: 353 usage(); 354 } 355 } 356 argc -= optind; 357 argv += optind; 358 359 if (todo == 0) 360 todo = VMSTAT; 361 362 /* 363 * Discard setgid privileges. If not the running kernel, we toss 364 * them away totally so that bad guys can't print interesting stuff 365 * from kernel memory, otherwise switch back to kmem for the 366 * duration of the kvm_openfiles() call. 367 */ 368 if (nlistf != NULL || memf != NULL) 369 (void)setgid(getgid()); 370 else 371 (void)setegid(egid); 372 373 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 374 if (kd == NULL) 375 errx(1, "kvm_openfiles: %s", errbuf); 376 377 if (nlistf == NULL && memf == NULL) 378 (void)setgid(getgid()); 379 380 if ((c = kvm_nlist(kd, namelist)) != 0) { 381 if (c == -1) 382 errx(1, "kvm_nlist: %s %s", "namelist", kvm_geterr(kd)); 383 (void)fprintf(stderr, "vmstat: undefined symbols:"); 384 for (c = 0; c < sizeof(namelist) / sizeof(namelist[0]); c++) 385 if (namelist[c].n_type == 0) 386 fprintf(stderr, " %s", namelist[c].n_name); 387 (void)fputc('\n', stderr); 388 exit(1); 389 } 390 if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE) 391 errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd)); 392 if (kvm_nlist(kd, histnl) == -1) 393 errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd)); 394 395 if (todo & VMSTAT) { 396 struct winsize winsize; 397 398 dkinit(0); /* Initialize disk stats, no disks selected. */ 399 400 (void)setgid(getgid()); /* don't need privs anymore */ 401 402 argv = choosedrives(argv); /* Select disks. */ 403 winsize.ws_row = 0; 404 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 405 if (winsize.ws_row > 0) 406 winlines = winsize.ws_row; 407 408 } 409 410 #ifdef BACKWARD_COMPATIBILITY 411 if (*argv) { 412 interval.tv_sec = atol(*argv); 413 if (*++argv) 414 reps = atoi(*argv); 415 } 416 #endif 417 418 if (interval.tv_sec) { 419 if (!reps) 420 reps = -1; 421 } else if (reps) 422 interval.tv_sec = 1; 423 424 425 /* 426 * Statistics dumping is incompatible with the default 427 * VMSTAT/dovmstat() output. So perform the interval/reps handling 428 * for it here. 429 */ 430 if ((todo & VMSTAT) == 0) { 431 for (;;) { 432 if (todo & (HISTLIST|HISTDUMP)) { 433 if ((todo & (HISTLIST|HISTDUMP)) == 434 (HISTLIST|HISTDUMP)) 435 errx(1, "you may list or dump," 436 " but not both!"); 437 hist_traverse(todo, histname); 438 putchar('\n'); 439 } 440 if (todo & FORKSTAT) { 441 doforkst(); 442 putchar('\n'); 443 } 444 if (todo & MEMSTAT) { 445 domem(); 446 dopool(verbose); 447 putchar('\n'); 448 } 449 if (todo & SUMSTAT) { 450 dosum(); 451 putchar('\n'); 452 } 453 if (todo & INTRSTAT) { 454 dointr(verbose); 455 putchar('\n'); 456 } 457 if (todo & EVCNTSTAT) { 458 doevcnt(verbose); 459 putchar('\n'); 460 } 461 if (todo & (HASHLIST|HASHSTAT)) { 462 if ((todo & (HASHLIST|HASHSTAT)) == 463 (HASHLIST|HASHSTAT)) 464 errx(1, "you may list or display," 465 " but not both!"); 466 dohashstat(verbose, todo, hashname); 467 putchar('\n'); 468 } 469 470 if (reps >= 0 && --reps <=0) 471 break; 472 nanosleep(&interval, NULL); 473 } 474 } else 475 dovmstat(&interval, reps); 476 exit(0); 477 } 478 479 char ** 480 choosedrives(char **argv) 481 { 482 int i; 483 484 /* 485 * Choose drives to be displayed. Priority goes to (in order) drives 486 * supplied as arguments, default drives. If everything isn't filled 487 * in and there are drives not taken care of, display the first few 488 * that fit. 489 */ 490 #define BACKWARD_COMPATIBILITY 491 for (ndrives = 0; *argv; ++argv) { 492 #ifdef BACKWARD_COMPATIBILITY 493 if (isdigit(**argv)) 494 break; 495 #endif 496 for (i = 0; i < dk_ndrive; i++) { 497 if (strcmp(dr_name[i], *argv)) 498 continue; 499 dk_select[i] = 1; 500 ++ndrives; 501 break; 502 } 503 } 504 for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 505 if (dk_select[i]) 506 continue; 507 dk_select[i] = 1; 508 ++ndrives; 509 } 510 return (argv); 511 } 512 513 long 514 getuptime(void) 515 { 516 static struct timeval boottime; 517 struct timeval now, diff; 518 time_t uptime; 519 520 if (boottime.tv_sec == 0) 521 kread(X_BOOTTIME, &boottime, sizeof(boottime)); 522 kread(X_TIME, &now, sizeof(now)); 523 timersub(&now, &boottime, &diff); 524 uptime = diff.tv_sec; 525 if (uptime <= 0 || uptime > 60*60*24*365*10) 526 errx(1, "time makes no sense; namelist must be wrong."); 527 return (uptime); 528 } 529 530 int hz, hdrcnt; 531 532 void 533 dovmstat(struct timespec *interval, int reps) 534 { 535 struct vmtotal total; 536 time_t uptime, halfuptime; 537 int mib[2]; 538 size_t size; 539 int pagesize = getpagesize(); 540 541 uptime = getuptime(); 542 halfuptime = uptime / 2; 543 (void)signal(SIGCONT, needhdr); 544 545 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) 546 kread(X_STATHZ, &hz, sizeof(hz)); 547 if (!hz) 548 kread(X_HZ, &hz, sizeof(hz)); 549 550 for (hdrcnt = 1;;) { 551 if (!--hdrcnt) 552 printhdr(); 553 /* Read new disk statistics */ 554 dkreadstats(); 555 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp)); 556 if (memf != NULL) { 557 /* 558 * XXX Can't do this if we're reading a crash 559 * XXX dump because they're lazily-calculated. 560 */ 561 printf("Unable to get vmtotals from crash dump.\n"); 562 memset(&total, 0, sizeof(total)); 563 } else { 564 size = sizeof(total); 565 mib[0] = CTL_VM; 566 mib[1] = VM_METER; 567 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) { 568 printf("Can't get vmtotals: %s\n", 569 strerror(errno)); 570 memset(&total, 0, sizeof(total)); 571 } 572 } 573 (void)printf("%2d%2d%2d", 574 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); 575 #define pgtok(a) (long)((a) * (pagesize >> 10)) 576 #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */ 577 (void)printf(" %6ld %6ld ", 578 pgtok(total.t_avm), pgtok(total.t_free)); 579 (void)printf("%4lu ", rate(uvmexp.faults - ouvmexp.faults)); 580 (void)printf("%3lu ", rate(uvmexp.pdreact - ouvmexp.pdreact)); 581 (void)printf("%3lu ", rate(uvmexp.pageins - ouvmexp.pageins)); 582 (void)printf("%4lu ", 583 rate(uvmexp.pgswapout - ouvmexp.pgswapout)); 584 (void)printf("%4lu ", rate(uvmexp.pdfreed - ouvmexp.pdfreed)); 585 (void)printf("%4lu ", rate(uvmexp.pdscans - ouvmexp.pdscans)); 586 dkstats(); 587 (void)printf("%4lu %4lu %3lu ", 588 rate(uvmexp.intrs - ouvmexp.intrs), 589 rate(uvmexp.syscalls - ouvmexp.syscalls), 590 rate(uvmexp.swtch - ouvmexp.swtch)); 591 cpustats(); 592 putchar('\n'); 593 (void)fflush(stdout); 594 if (reps >= 0 && --reps <= 0) 595 break; 596 ouvmexp = uvmexp; 597 uptime = interval->tv_sec; 598 /* 599 * We round upward to avoid losing low-frequency events 600 * (i.e., >= 1 per interval but < 1 per second). 601 */ 602 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2; 603 nanosleep(interval, NULL); 604 } 605 } 606 607 void 608 printhdr(void) 609 { 610 int i; 611 612 (void)printf(" procs memory page%*s", 23, ""); 613 if (ndrives > 0) 614 (void)printf("%s %*sfaults cpu\n", 615 ((ndrives > 1) ? "disks" : "disk"), 616 ((ndrives > 1) ? ndrives * 3 - 4 : 0), ""); 617 else 618 (void)printf("%*s faults cpu\n", 619 ndrives * 3, ""); 620 621 (void)printf(" r b w avm fre flt re pi po fr sr "); 622 for (i = 0; i < dk_ndrive; i++) 623 if (dk_select[i]) 624 (void)printf("%c%c ", dr_name[i][0], 625 dr_name[i][strlen(dr_name[i]) - 1]); 626 (void)printf(" in sy cs us sy id\n"); 627 hdrcnt = winlines - 2; 628 } 629 630 /* 631 * Force a header to be prepended to the next output. 632 */ 633 void 634 needhdr(int dummy) 635 { 636 637 hdrcnt = 1; 638 } 639 640 long 641 pct(long top, long bot) 642 { 643 long ans; 644 645 if (bot == 0) 646 return (0); 647 ans = (quad_t)top * 100 / bot; 648 return (ans); 649 } 650 651 #define PCT(top, bot) (int)pct((long)(top), (long)(bot)) 652 653 void 654 dosum(void) 655 { 656 struct nchstats nchstats; 657 long nchtotal; 658 659 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp)); 660 661 (void)printf("%9u bytes per page\n", uvmexp.pagesize); 662 663 (void)printf("%9u page color%s\n", 664 uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s"); 665 666 (void)printf("%9u pages managed\n", uvmexp.npages); 667 (void)printf("%9u pages free\n", uvmexp.free); 668 (void)printf("%9u pages active\n", uvmexp.active); 669 (void)printf("%9u pages inactive\n", uvmexp.inactive); 670 (void)printf("%9u pages paging\n", uvmexp.paging); 671 (void)printf("%9u pages wired\n", uvmexp.wired); 672 (void)printf("%9u zero pages\n", uvmexp.zeropages); 673 (void)printf("%9u reserve pagedaemon pages\n", 674 uvmexp.reserve_pagedaemon); 675 (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel); 676 (void)printf("%9u anonymous pages\n", uvmexp.anonpages); 677 (void)printf("%9u cached file pages\n", uvmexp.filepages); 678 (void)printf("%9u cached executable pages\n", uvmexp.execpages); 679 680 (void)printf("%9u minimum free pages\n", uvmexp.freemin); 681 (void)printf("%9u target free pages\n", uvmexp.freetarg); 682 (void)printf("%9u target inactive pages\n", uvmexp.inactarg); 683 (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax); 684 685 (void)printf("%9u swap devices\n", uvmexp.nswapdev); 686 (void)printf("%9u swap pages\n", uvmexp.swpages); 687 (void)printf("%9u swap pages in use\n", uvmexp.swpginuse); 688 (void)printf("%9u swap allocations\n", uvmexp.nswget); 689 (void)printf("%9u anons\n", uvmexp.nanon); 690 (void)printf("%9u free anons\n", uvmexp.nfreeanon); 691 692 (void)printf("%9u total faults taken\n", uvmexp.faults); 693 (void)printf("%9u traps\n", uvmexp.traps); 694 (void)printf("%9u device interrupts\n", uvmexp.intrs); 695 (void)printf("%9u cpu context switches\n", uvmexp.swtch); 696 (void)printf("%9u software interrupts\n", uvmexp.softs); 697 (void)printf("%9u system calls\n", uvmexp.syscalls); 698 (void)printf("%9u pagein requests\n", uvmexp.pageins); 699 (void)printf("%9u pageout requests\n", uvmexp.pdpageouts); 700 (void)printf("%9u swap ins\n", uvmexp.swapins); 701 (void)printf("%9u swap outs\n", uvmexp.swapouts); 702 (void)printf("%9u pages swapped in\n", uvmexp.pgswapin); 703 (void)printf("%9u pages swapped out\n", uvmexp.pgswapout); 704 (void)printf("%9u forks total\n", uvmexp.forks); 705 (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait); 706 (void)printf("%9u forks shared address space with parent\n", 707 uvmexp.forks_sharevm); 708 (void)printf("%9u pagealloc zero wanted and avail\n", 709 uvmexp.pga_zerohit); 710 (void)printf("%9u pagealloc zero wanted and not avail\n", 711 uvmexp.pga_zeromiss); 712 (void)printf("%9u aborts of idle page zeroing\n", 713 uvmexp.zeroaborts); 714 (void)printf("%9u pagealloc desired color avail\n", 715 uvmexp.colorhit); 716 (void)printf("%9u pagealloc desired color not avail\n", 717 uvmexp.colormiss); 718 719 (void)printf("%9u faults with no memory\n", uvmexp.fltnoram); 720 (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon); 721 (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait); 722 (void)printf("%9u faults found released page\n", uvmexp.fltpgrele); 723 (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck, 724 uvmexp.fltrelckok); 725 (void)printf("%9u anon page faults\n", uvmexp.fltanget); 726 (void)printf("%9u anon retry faults\n", uvmexp.fltanretry); 727 (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy); 728 (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap); 729 (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap); 730 (void)printf("%9u locked pager get faults\n", uvmexp.fltlget); 731 (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget); 732 (void)printf("%9u anon faults\n", uvmexp.flt_anon); 733 (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow); 734 (void)printf("%9u object faults\n", uvmexp.flt_obj); 735 (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy); 736 (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero); 737 738 (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke); 739 (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs); 740 (void)printf("%9u times daemon attempted swapout\n", uvmexp.pdswout); 741 (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed); 742 (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans); 743 (void)printf("%9u anonymous pages scanned by daemon\n", 744 uvmexp.pdanscan); 745 (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan); 746 (void)printf("%9u pages reactivated\n", uvmexp.pdreact); 747 (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy); 748 (void)printf("%9u total pending pageouts\n", uvmexp.pdpending); 749 (void)printf("%9u pages deactivated\n", uvmexp.pddeact); 750 kread(X_NCHSTATS, &nchstats, sizeof(nchstats)); 751 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 752 nchstats.ncs_badhits + nchstats.ncs_falsehits + 753 nchstats.ncs_miss + nchstats.ncs_long; 754 (void)printf("%9ld total name lookups\n", nchtotal); 755 (void)printf( 756 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", 757 "", PCT(nchstats.ncs_goodhits, nchtotal), 758 PCT(nchstats.ncs_neghits, nchtotal), 759 PCT(nchstats.ncs_pass2, nchtotal)); 760 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", 761 PCT(nchstats.ncs_badhits, nchtotal), 762 PCT(nchstats.ncs_falsehits, nchtotal), 763 PCT(nchstats.ncs_long, nchtotal)); 764 } 765 766 void 767 doforkst(void) 768 { 769 770 kread(X_UVMEXP, &uvmexp, sizeof(uvmexp)); 771 772 (void)printf("%u forks total\n", uvmexp.forks); 773 (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait); 774 (void)printf("%u forks shared address space with parent\n", 775 uvmexp.forks_sharevm); 776 } 777 778 void 779 dkstats(void) 780 { 781 int dn; 782 double etime; 783 784 /* Calculate disk stat deltas. */ 785 dkswap(); 786 etime = cur.cp_etime; 787 788 for (dn = 0; dn < dk_ndrive; ++dn) { 789 if (!dk_select[dn]) 790 continue; 791 (void)printf("%2.0f ", 792 (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime); 793 } 794 } 795 796 void 797 cpustats(void) 798 { 799 int state; 800 double pct, total; 801 802 total = 0; 803 for (state = 0; state < CPUSTATES; ++state) 804 total += cur.cp_time[state]; 805 if (total) 806 pct = 100 / total; 807 else 808 pct = 0; 809 (void)printf("%2.0f ", 810 (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pct); 811 (void)printf("%2.0f ", 812 (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pct); 813 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct); 814 } 815 816 void 817 dointr(int verbose) 818 { 819 unsigned long *intrcnt; 820 unsigned long long inttotal, uptime; 821 int nintr, inamlen; 822 char *intrname; 823 struct evcntlist allevents; 824 struct evcnt evcnt, *evptr; 825 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX]; 826 827 uptime = getuptime(); 828 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value; 829 inamlen = 830 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value; 831 intrcnt = malloc((size_t)nintr); 832 intrname = malloc((size_t)inamlen); 833 if (intrcnt == NULL || intrname == NULL) 834 errx(1, "%s", ""); 835 kread(X_INTRCNT, intrcnt, (size_t)nintr); 836 kread(X_INTRNAMES, intrname, (size_t)inamlen); 837 (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate"); 838 inttotal = 0; 839 nintr /= sizeof(long); 840 while (--nintr >= 0) { 841 if (*intrcnt || verbose) 842 (void)printf("%-34s %16llu %8llu\n", intrname, 843 (unsigned long long)*intrcnt, 844 (unsigned long long)(*intrcnt / uptime)); 845 intrname += strlen(intrname) + 1; 846 inttotal += *intrcnt++; 847 } 848 kread(X_ALLEVENTS, &allevents, sizeof allevents); 849 evptr = allevents.tqh_first; 850 while (evptr) { 851 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed"); 852 evptr = evcnt.ev_list.tqe_next; 853 if (evcnt.ev_type != EVCNT_TYPE_INTR) 854 continue; 855 856 if (evcnt.ev_count == 0 && !verbose) 857 continue; 858 859 deref_kptr(evcnt.ev_group, evgroup, evcnt.ev_grouplen + 1, 860 "event chain trashed"); 861 deref_kptr(evcnt.ev_name, evname, evcnt.ev_namelen + 1, 862 "event chain trashed"); 863 864 (void)printf("%s %s%*s %16llu %8llu\n", evgroup, evname, 865 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "", 866 (unsigned long long)evcnt.ev_count, 867 (unsigned long long)(evcnt.ev_count / uptime)); 868 869 inttotal += evcnt.ev_count++; 870 } 871 (void)printf("%-34s %16llu %8llu\n", "Total", inttotal, 872 (unsigned long long)(inttotal / uptime)); 873 } 874 875 void 876 doevcnt(int verbose) 877 { 878 static const char * evtypes [] = { "misc", "intr", "trap" }; 879 unsigned long long uptime; 880 struct evcntlist allevents; 881 struct evcnt evcnt, *evptr; 882 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX]; 883 884 /* XXX should print type! */ 885 886 uptime = getuptime(); 887 (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate", "type"); 888 kread(X_ALLEVENTS, &allevents, sizeof allevents); 889 evptr = allevents.tqh_first; 890 while (evptr) { 891 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed"); 892 893 evptr = evcnt.ev_list.tqe_next; 894 if (evcnt.ev_count == 0 && !verbose) 895 continue; 896 897 deref_kptr(evcnt.ev_group, evgroup, evcnt.ev_grouplen + 1, 898 "event chain trashed"); 899 deref_kptr(evcnt.ev_name, evname, evcnt.ev_namelen + 1, 900 "event chain trashed"); 901 902 (void)printf("%s %s%*s %16llu %8llu %s\n", evgroup, evname, 903 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "", 904 (unsigned long long)evcnt.ev_count, 905 (unsigned long long)(evcnt.ev_count / uptime), 906 (evcnt.ev_type < sizeof(evtypes)/sizeof(evtypes[0]) ? 907 evtypes[evcnt.ev_type] : "?")); 908 } 909 } 910 911 /* 912 * These names are defined in <sys/malloc.h>. 913 */ 914 char *kmemnames[] = INITKMEMNAMES; 915 916 void 917 domem(void) 918 { 919 struct kmembuckets *kp; 920 struct kmemstats *ks; 921 int i, j; 922 int len, size, first; 923 long totuse = 0, totfree = 0, totreq = 0; 924 char *name; 925 struct kmemstats kmemstats[M_LAST]; 926 struct kmembuckets buckets[MINBUCKET + 16]; 927 928 kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); 929 for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; 930 i++, kp++) { 931 if (kp->kb_calls == 0) 932 continue; 933 if (first) { 934 (void)printf("Memory statistics by bucket size\n"); 935 (void)printf( 936 " Size In Use Free Requests HighWater Couldfree\n"); 937 first = 0; 938 } 939 size = 1 << i; 940 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size, 941 kp->kb_total - kp->kb_totalfree, 942 kp->kb_totalfree, kp->kb_calls, 943 kp->kb_highwat, kp->kb_couldfree); 944 totfree += size * kp->kb_totalfree; 945 } 946 947 /* 948 * If kmem statistics are not being gathered by the kernel, 949 * first will still be 1. 950 */ 951 if (first) { 952 warnx("Kmem statistics are not being gathered by the kernel."); 953 return; 954 } 955 956 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats)); 957 (void)printf("\nMemory usage type by bucket size\n"); 958 (void)printf(" Size Type(s)\n"); 959 kp = &buckets[MINBUCKET]; 960 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) { 961 if (kp->kb_calls == 0) 962 continue; 963 first = 1; 964 len = 8; 965 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { 966 if (ks->ks_calls == 0) 967 continue; 968 if ((ks->ks_size & j) == 0) 969 continue; 970 if (kmemnames[i] == 0) { 971 kmemnames[i] = malloc(10); 972 /* strlen("undef/")+3+1);*/ 973 snprintf(kmemnames[i], 10, "undef/%d", i); 974 /* same 10 as above!!! */ 975 } 976 name = kmemnames[i]; 977 len += 2 + strlen(name); 978 if (first) 979 printf("%8d %s", j, name); 980 else 981 printf(","); 982 if (len >= 80) { 983 printf("\n\t "); 984 len = 10 + strlen(name); 985 } 986 if (!first) 987 printf(" %s", name); 988 first = 0; 989 } 990 putchar('\n'); 991 } 992 993 (void)printf( 994 "\nMemory statistics by type Type Kern\n"); 995 (void)printf( 996 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n"); 997 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { 998 if (ks->ks_calls == 0) 999 continue; 1000 (void)printf("%14s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u", 1001 kmemnames[i] ? kmemnames[i] : "undefined", 1002 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024, 1003 (ks->ks_maxused + 1023) / 1024, 1004 (ks->ks_limit + 1023) / 1024, ks->ks_calls, 1005 ks->ks_limblocks, ks->ks_mapblocks); 1006 first = 1; 1007 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 1008 if ((ks->ks_size & j) == 0) 1009 continue; 1010 if (first) 1011 printf(" %d", j); 1012 else 1013 printf(",%d", j); 1014 first = 0; 1015 } 1016 printf("\n"); 1017 totuse += ks->ks_memuse; 1018 totreq += ks->ks_calls; 1019 } 1020 (void)printf("\nMemory totals: In Use Free Requests\n"); 1021 (void)printf(" %7ldK %6ldK %8ld\n\n", 1022 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); 1023 } 1024 1025 void 1026 dopool(int verbose) 1027 { 1028 int first, ovflw; 1029 void *addr; 1030 long total = 0, inuse = 0; 1031 TAILQ_HEAD(,pool) pool_head; 1032 struct pool pool, *pp = &pool; 1033 struct pool_allocator pa; 1034 char name[32], maxp[32]; 1035 1036 kread(X_POOLHEAD, &pool_head, sizeof(pool_head)); 1037 addr = TAILQ_FIRST(&pool_head); 1038 1039 for (first = 1; addr != NULL; ) { 1040 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed"); 1041 deref_kptr(pp->pr_alloc, &pa, sizeof(pa), 1042 "pool allocatior trashed"); 1043 deref_kptr(pp->pr_wchan, name, sizeof(name), 1044 "pool wait channel trashed"); 1045 name[sizeof(name)-1] = '\0'; 1046 1047 if (first) { 1048 (void)printf("Memory resource pool statistics\n"); 1049 (void)printf( 1050 "%-11s%5s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n", 1051 "Name", 1052 "Size", 1053 "Requests", 1054 "Fail", 1055 "Releases", 1056 "Pgreq", 1057 "Pgrel", 1058 "Npage", 1059 "Hiwat", 1060 "Minpg", 1061 "Maxpg", 1062 "Idle"); 1063 first = 0; 1064 } 1065 if (pp->pr_maxpages == UINT_MAX) 1066 sprintf(maxp, "inf"); 1067 else 1068 sprintf(maxp, "%u", pp->pr_maxpages); 1069 /* 1070 * Print single word. `ovflow' is number of characters didn't fit 1071 * on the last word. `fmt' is a format string to print this word. 1072 * It must contain asterisk for field width. `width' is a width 1073 * occupied by this word. `fixed' is a number of constant chars in 1074 * `fmt'. `val' is a value to be printed using format string `fmt'. 1075 */ 1076 #define PRWORD(ovflw, fmt, width, fixed, val) do { \ 1077 (ovflw) += printf((fmt), \ 1078 (width) - (fixed) - (ovflw) > 0 ? \ 1079 (width) - (fixed) - (ovflw) : 0, \ 1080 (val)) - (width); \ 1081 if ((ovflw) < 0) \ 1082 (ovflw) = 0; \ 1083 } while (/* CONSTCOND */0) 1084 ovflw = 0; 1085 PRWORD(ovflw, "%-*s", 11, 0, name); 1086 PRWORD(ovflw, " %*u", 5, 1, pp->pr_size); 1087 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget); 1088 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail); 1089 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput); 1090 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc); 1091 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree); 1092 PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages); 1093 PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat); 1094 PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages); 1095 PRWORD(ovflw, " %*s", 6, 1, maxp); 1096 PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle); 1097 1098 if (pp->pr_roflags & PR_RECURSIVE) { 1099 /* 1100 * Don't count in-use memory, since it's part 1101 * of another pool and will be accounted for 1102 * there. 1103 */ 1104 total += pp->pr_npages * pa.pa_pagesz - 1105 (pp->pr_nget - pp->pr_nput) * pp->pr_size; 1106 } else { 1107 inuse += (pp->pr_nget - pp->pr_nput) * pp->pr_size; 1108 total += pp->pr_npages * pa.pa_pagesz; 1109 } 1110 dopoolcache(pp, verbose); 1111 addr = TAILQ_NEXT(pp, pr_poollist); 1112 } 1113 1114 inuse /= 1024; 1115 total /= 1024; 1116 printf("\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n", 1117 inuse, total, (double)(100 * inuse) / total); 1118 } 1119 1120 void 1121 dopoolcache(struct pool *pp, int verbose) 1122 { 1123 struct pool_cache pool_cache, *pc = &pool_cache; 1124 struct pool_cache_group pool_cache_group, *pcg = &pool_cache_group; 1125 void *addr, *pcg_addr; 1126 int i; 1127 1128 if (verbose < 1) 1129 return; 1130 1131 for (addr = TAILQ_FIRST(&pp->pr_cachelist); addr != NULL; 1132 addr = TAILQ_NEXT(pc, pc_poollist)) { 1133 deref_kptr(addr, pc, sizeof(*pc), "pool cache trashed"); 1134 printf("\tcache %p: allocfrom %p freeto %p\n", addr, 1135 pc->pc_allocfrom, pc->pc_freeto); 1136 printf("\t hits %lu misses %lu ngroups %lu nitems %lu\n", 1137 pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems); 1138 if (verbose < 2) 1139 continue; 1140 for (pcg_addr = TAILQ_FIRST(&pc->pc_grouplist); 1141 pcg_addr != NULL; pcg_addr = TAILQ_NEXT(pcg, pcg_list)) { 1142 deref_kptr(pcg_addr, pcg, sizeof(*pcg), 1143 "pool cache group trashed"); 1144 printf("\t\tgroup %p: avail %d\n", pcg_addr, 1145 pcg->pcg_avail); 1146 for (i = 0; i < PCG_NOBJECTS; i++) 1147 printf("\t\t\t%p\n", pcg->pcg_objects[i]); 1148 } 1149 } 1150 1151 } 1152 1153 enum hashtype { /* from <sys/systm.h> */ 1154 HASH_LIST, 1155 HASH_TAILQ 1156 }; 1157 1158 struct uidinfo { /* XXX: no kernel header file */ 1159 LIST_ENTRY(uidinfo) ui_hash; 1160 uid_t ui_uid; 1161 long ui_proccnt; 1162 }; 1163 1164 struct kernel_hash { 1165 const char * description; /* description */ 1166 int hashsize; /* nlist index for hash size */ 1167 int hashtbl; /* nlist index for hash table */ 1168 enum hashtype type; /* type of hash table */ 1169 size_t offset; /* offset of {LIST,TAILQ}_NEXT */ 1170 } khashes[] = 1171 { 1172 { 1173 "buffer hash", 1174 X_BUFHASH, X_BUFHASHTBL, 1175 HASH_LIST, offsetof(struct buf, b_hash) 1176 }, { 1177 "inode cache (ihash)", 1178 X_IHASH, X_IHASHTBL, 1179 HASH_LIST, offsetof(struct inode, i_hash) 1180 }, { 1181 "ipv4 address -> interface hash", 1182 X_IFADDRHASH, X_IFADDRHASHTBL, 1183 HASH_LIST, offsetof(struct in_ifaddr, ia_hash), 1184 }, { 1185 "name cache hash", 1186 X_NCHASH, X_NCHASHTBL, 1187 HASH_LIST, offsetof(struct namecache, nc_hash), 1188 }, { 1189 "name cache directory hash", 1190 X_NCVHASH, X_NCVHASHTBL, 1191 HASH_LIST, offsetof(struct namecache, nc_vhash), 1192 }, { 1193 "nfs client node cache", 1194 X_NFSNODE, X_NFSNODETBL, 1195 HASH_LIST, offsetof(struct nfsnode, n_hash) 1196 }, { 1197 "process group (pgrp) hash", 1198 X_PGRPHASH, X_PGRPHASHTBL, 1199 HASH_LIST, offsetof(struct pgrp, pg_hash), 1200 }, { 1201 "process id (pid) hash", 1202 X_PIDHASH, X_PIDHASHTBL, 1203 HASH_LIST, offsetof(struct proc, p_hash) 1204 }, { 1205 "user info (uid -> used processes) hash", 1206 X_UIHASH, X_UIHASHTBL, 1207 HASH_LIST, offsetof(struct uidinfo, ui_hash), 1208 }, { 1209 NULL, -1, -1, 0, 0, 1210 } 1211 }; 1212 1213 void 1214 dohashstat(int verbose, int todo, const char *hashname) 1215 { 1216 LIST_HEAD(, generic) *hashtbl_list; 1217 TAILQ_HEAD(, generic) *hashtbl_tailq; 1218 struct kernel_hash *curhash; 1219 void *hashaddr, *hashbuf, *nextaddr; 1220 size_t elemsize, hashbufsize, thissize; 1221 u_long hashsize; 1222 int i, used, items, chain, maxchain; 1223 1224 hashbuf = NULL; 1225 hashbufsize = 0; 1226 1227 if (todo & HASHLIST) { 1228 printf("Supported hashes:\n"); 1229 for (curhash = khashes; curhash->description; curhash++) { 1230 if (hashnl[curhash->hashsize].n_value == 0 || 1231 hashnl[curhash->hashtbl].n_value == 0) 1232 continue; 1233 printf("\t%-16s%s\n", 1234 hashnl[curhash->hashsize].n_name + 1, 1235 curhash->description); 1236 } 1237 return; 1238 } 1239 1240 if (hashname != NULL) { 1241 for (curhash = khashes; curhash->description; curhash++) { 1242 if (strcmp(hashnl[curhash->hashsize].n_name + 1, 1243 hashname) == 0 && 1244 hashnl[curhash->hashsize].n_value != 0 && 1245 hashnl[curhash->hashtbl].n_value != 0) 1246 break; 1247 } 1248 if (curhash->description == NULL) { 1249 warnx("%s: no such hash", hashname); 1250 return; 1251 } 1252 } 1253 1254 printf( 1255 "%-16s %8s %8s %8s %8s %8s %8s\n" 1256 "%-16s %8s %8s %8s %8s %8s %8s\n", 1257 "", "total", "used", "util", "num", "average", "maximum", 1258 "hash table", "buckets", "buckets", "%", "items", "chain", 1259 "chain"); 1260 1261 for (curhash = khashes; curhash->description; curhash++) { 1262 if (hashnl[curhash->hashsize].n_value == 0 || 1263 hashnl[curhash->hashtbl].n_value == 0) 1264 continue; 1265 if (hashname != NULL && 1266 strcmp(hashnl[curhash->hashsize].n_name + 1, hashname)) 1267 continue; 1268 elemsize = curhash->type == HASH_LIST ? 1269 sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq); 1270 deref_kptr((void *)hashnl[curhash->hashsize].n_value, 1271 &hashsize, sizeof(hashsize), 1272 hashnl[curhash->hashsize].n_name); 1273 hashsize++; 1274 deref_kptr((void *)hashnl[curhash->hashtbl].n_value, 1275 &hashaddr, sizeof(hashaddr), 1276 hashnl[curhash->hashtbl].n_name); 1277 if (verbose) 1278 printf("%s %lu, %s %p, offset %ld, elemsize %llu\n", 1279 hashnl[curhash->hashsize].n_name + 1, hashsize, 1280 hashnl[curhash->hashtbl].n_name + 1, hashaddr, 1281 (long)curhash->offset, 1282 (unsigned long long)elemsize); 1283 thissize = hashsize * elemsize; 1284 if (thissize > hashbufsize) { 1285 hashbufsize = thissize; 1286 if ((hashbuf = realloc(hashbuf, hashbufsize)) == NULL) 1287 errx(1, "malloc hashbuf %llu", 1288 (unsigned long long)hashbufsize); 1289 } 1290 deref_kptr(hashaddr, hashbuf, thissize, 1291 hashnl[curhash->hashtbl].n_name); 1292 used = 0; 1293 items = maxchain = 0; 1294 if (curhash->type == HASH_LIST) 1295 hashtbl_list = hashbuf; 1296 else 1297 hashtbl_tailq = hashbuf; 1298 for (i = 0; i < hashsize; i++) { 1299 if (curhash->type == HASH_LIST) 1300 nextaddr = LIST_FIRST(&hashtbl_list[i]); 1301 else 1302 nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]); 1303 if (nextaddr == NULL) 1304 continue; 1305 if (verbose) 1306 printf("%5d: %p\n", i, nextaddr); 1307 used++; 1308 chain = 0; 1309 do { 1310 chain++; 1311 deref_kptr((char *)nextaddr + curhash->offset, 1312 &nextaddr, sizeof(void *), 1313 "hash chain corrupted"); 1314 if (verbose > 1) 1315 printf("got nextaddr as %p\n", 1316 nextaddr); 1317 } while (nextaddr != NULL); 1318 items += chain; 1319 if (verbose && chain > 1) 1320 printf("\tchain = %d\n", chain); 1321 if (chain > maxchain) 1322 maxchain = chain; 1323 } 1324 printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n", 1325 hashnl[curhash->hashsize].n_name + 1, 1326 hashsize, used, used * 100.0 / hashsize, 1327 items, used ? (double)items / used : 0.0, maxchain); 1328 } 1329 } 1330 1331 /* 1332 * kread reads something from the kernel, given its nlist index in namelist[]. 1333 */ 1334 void 1335 kread(int nlx, void *addr, size_t size) 1336 { 1337 const char *sym; 1338 1339 sym = namelist[nlx].n_name; 1340 if (*sym == '_') 1341 ++sym; 1342 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) 1343 errx(1, "symbol %s not defined", sym); 1344 deref_kptr((void *)namelist[nlx].n_value, addr, size, sym); 1345 } 1346 1347 /* 1348 * Dereference the kernel pointer `kptr' and fill in the local copy 1349 * pointed to by `ptr'. The storage space must be pre-allocated, 1350 * and the size of the copy passed in `len'. 1351 */ 1352 void 1353 deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg) 1354 { 1355 1356 if (*msg == '_') 1357 msg++; 1358 if (kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len) 1359 errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd)); 1360 } 1361 1362 /* 1363 * Traverse the UVM history buffers, performing the requested action. 1364 * 1365 * Note, we assume that if we're not listing, we're dumping. 1366 */ 1367 void 1368 hist_traverse(int todo, const char *histname) 1369 { 1370 struct uvm_history_head histhead; 1371 struct uvm_history hist, *histkva; 1372 char *name = NULL; 1373 size_t namelen = 0; 1374 1375 if (histnl[0].n_value == 0) { 1376 warnx("UVM history is not compiled into the kernel."); 1377 return; 1378 } 1379 1380 deref_kptr((void *)histnl[X_UVM_HISTORIES].n_value, &histhead, 1381 sizeof(histhead), histnl[X_UVM_HISTORIES].n_name); 1382 1383 if (histhead.lh_first == NULL) { 1384 warnx("No active UVM history logs."); 1385 return; 1386 } 1387 1388 if (todo & HISTLIST) 1389 printf("Active UVM histories:"); 1390 1391 for (histkva = LIST_FIRST(&histhead); histkva != NULL; 1392 histkva = LIST_NEXT(&hist, list)) { 1393 deref_kptr(histkva, &hist, sizeof(hist), "histkva"); 1394 if (hist.namelen > namelen) { 1395 if (name != NULL) 1396 free(name); 1397 namelen = hist.namelen; 1398 if ((name = malloc(namelen + 1)) == NULL) 1399 err(1, "malloc history name"); 1400 } 1401 1402 deref_kptr(hist.name, name, namelen, "history name"); 1403 name[namelen] = '\0'; 1404 if (todo & HISTLIST) 1405 printf(" %s", name); 1406 else { 1407 /* 1408 * If we're dumping all histories, do it, else 1409 * check to see if this is the one we want. 1410 */ 1411 if (histname == NULL || strcmp(histname, name) == 0) { 1412 if (histname == NULL) 1413 printf("\nUVM history `%s':\n", name); 1414 hist_dodump(&hist); 1415 } 1416 } 1417 } 1418 1419 if (todo & HISTLIST) 1420 putchar('\n'); 1421 1422 if (name != NULL) 1423 free(name); 1424 } 1425 1426 /* 1427 * Actually dump the history buffer at the specified KVA. 1428 */ 1429 void 1430 hist_dodump(struct uvm_history *histp) 1431 { 1432 struct uvm_history_ent *histents, *e; 1433 size_t histsize; 1434 char *fmt = NULL, *fn = NULL; 1435 size_t fmtlen = 0, fnlen = 0; 1436 int i; 1437 1438 histsize = sizeof(struct uvm_history_ent) * histp->n; 1439 1440 if ((histents = malloc(histsize)) == NULL) 1441 err(1, "malloc history entries"); 1442 1443 memset(histents, 0, histsize); 1444 1445 deref_kptr(histp->e, histents, histsize, "history entries"); 1446 i = histp->f; 1447 do { 1448 e = &histents[i]; 1449 if (e->fmt != NULL) { 1450 if (e->fmtlen > fmtlen) { 1451 if (fmt != NULL) 1452 free(fmt); 1453 fmtlen = e->fmtlen; 1454 if ((fmt = malloc(fmtlen + 1)) == NULL) 1455 err(1, "malloc printf format"); 1456 } 1457 if (e->fnlen > fnlen) { 1458 if (fn != NULL) 1459 free(fn); 1460 fnlen = e->fnlen; 1461 if ((fn = malloc(fnlen + 1)) == NULL) 1462 err(1, "malloc function name"); 1463 } 1464 1465 deref_kptr(e->fmt, fmt, fmtlen, "printf format"); 1466 fmt[fmtlen] = '\0'; 1467 1468 deref_kptr(e->fn, fn, fnlen, "function name"); 1469 fn[fnlen] = '\0'; 1470 1471 printf("%06ld.%06ld ", (long int)e->tv.tv_sec, 1472 (long int)e->tv.tv_usec); 1473 printf("%s#%ld: ", fn, e->call); 1474 printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]); 1475 putchar('\n'); 1476 } 1477 i = (i + 1) % histp->n; 1478 } while (i != histp->f); 1479 1480 free(histents); 1481 if (fmt != NULL) 1482 free(fmt); 1483 if (fn != NULL) 1484 free(fn); 1485 } 1486 1487 void 1488 usage(void) 1489 { 1490 1491 (void)fprintf(stderr, 1492 "usage: %s [-efHilmsUv] [-h hashname] [-u histname] [-c count]\n" 1493 "\t\t[-M core] [-N system] [-w wait] [disks]\n", getprogname()); 1494 exit(1); 1495 } 1496