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