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