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