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