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