vmstat.c revision 1.196
1/* $NetBSD: vmstat.c,v 1.196 2014/06/03 21:41:56 joerg Exp $ */ 2 3/*- 4 * Copyright (c) 1998, 2000, 2001, 2007 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 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34/* 35 * Copyright (c) 1980, 1986, 1991, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63#include <sys/cdefs.h> 64#ifndef lint 65__COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\ 66 The Regents of the University of California. All rights reserved."); 67#endif /* not lint */ 68 69#ifndef lint 70#if 0 71static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95"; 72#else 73__RCSID("$NetBSD: vmstat.c,v 1.196 2014/06/03 21:41:56 joerg Exp $"); 74#endif 75#endif /* not lint */ 76 77#define __POOL_EXPOSE 78 79#include <sys/param.h> 80#include <sys/types.h> 81#include <sys/mount.h> 82#include <sys/uio.h> 83 84#include <sys/buf.h> 85#include <sys/evcnt.h> 86#include <sys/ioctl.h> 87#include <sys/malloc.h> 88#include <sys/mallocvar.h> 89#include <sys/namei.h> 90#include <sys/pool.h> 91#include <sys/proc.h> 92#include <sys/sched.h> 93#include <sys/socket.h> 94#include <sys/sysctl.h> 95#include <sys/time.h> 96#include <sys/queue.h> 97#include <sys/kernhist.h> 98 99#include <uvm/uvm_extern.h> 100#include <uvm/uvm_stat.h> 101 102#include <net/if.h> 103#include <netinet/in.h> 104#include <netinet/in_var.h> 105 106#include <ufs/ufs/inode.h> 107 108#include <nfs/rpcv2.h> 109#include <nfs/nfsproto.h> 110#include <nfs/nfsnode.h> 111 112#include <ctype.h> 113#include <err.h> 114#include <errno.h> 115#include <fcntl.h> 116#include <kvm.h> 117#include <limits.h> 118#include <nlist.h> 119#undef n_hash 120#include <paths.h> 121#include <signal.h> 122#include <stdio.h> 123#include <stddef.h> 124#include <stdlib.h> 125#include <string.h> 126#include <time.h> 127#include <unistd.h> 128#include <util.h> 129 130#include "drvstats.h" 131 132/* 133 * All this mess will go away once everything is converted. 134 */ 135#ifdef __HAVE_CPU_DATA_FIRST 136 137# include <sys/cpu_data.h> 138struct cpu_info { 139 struct cpu_data ci_data; 140}; 141#else 142# include <sys/cpu.h> 143#endif 144 145struct cpu_info **cpu_infos; 146 147/* 148 * General namelist 149 */ 150struct nlist namelist[] = 151{ 152#define X_BOOTTIME 0 153 { .n_name = "_boottime" }, 154#define X_HZ 1 155 { .n_name = "_hz" }, 156#define X_STATHZ 2 157 { .n_name = "_stathz" }, 158#define X_NCHSTATS 3 159 { .n_name = "_nchstats" }, 160#define X_ALLEVENTS 4 161 { .n_name = "_allevents" }, 162#define X_POOLHEAD 5 163 { .n_name = "_pool_head" }, 164#define X_UVMEXP 6 165 { .n_name = "_uvmexp" }, 166#define X_TIME_SECOND 7 167 { .n_name = "_time_second" }, 168#define X_TIME 8 169 { .n_name = "_time" }, 170#define X_CPU_INFOS 9 171 { .n_name = "_cpu_infos" }, 172#define X_NL_SIZE 10 173 { .n_name = NULL }, 174}; 175 176/* 177 * Namelist for pre-evcnt interrupt counters. 178 */ 179struct nlist intrnl[] = 180{ 181#define X_INTRNAMES 0 182 { .n_name = "_intrnames" }, 183#define X_EINTRNAMES 1 184 { .n_name = "_eintrnames" }, 185#define X_INTRCNT 2 186 { .n_name = "_intrcnt" }, 187#define X_EINTRCNT 3 188 { .n_name = "_eintrcnt" }, 189#define X_INTRNL_SIZE 4 190 { .n_name = NULL }, 191}; 192 193 194/* 195 * Namelist for hash statistics 196 */ 197struct nlist hashnl[] = 198{ 199#define X_NFSNODE 0 200 { .n_name = "_nfsnodehash" }, 201#define X_NFSNODETBL 1 202 { .n_name = "_nfsnodehashtbl" }, 203#define X_IHASH 2 204 { .n_name = "_ihash" }, 205#define X_IHASHTBL 3 206 { .n_name = "_ihashtbl" }, 207#define X_BUFHASH 4 208 { .n_name = "_bufhash" }, 209#define X_BUFHASHTBL 5 210 { .n_name = "_bufhashtbl" }, 211#define X_UIHASH 6 212 { .n_name = "_uihash" }, 213#define X_UIHASHTBL 7 214 { .n_name = "_uihashtbl" }, 215#define X_IFADDRHASH 8 216 { .n_name = "_in_ifaddrhash" }, 217#define X_IFADDRHASHTBL 9 218 { .n_name = "_in_ifaddrhashtbl" }, 219#define X_NCHASH 10 220 { .n_name = "_nchash" }, 221#define X_NCHASHTBL 11 222 { .n_name = "_nchashtbl" }, 223#define X_NCVHASH 12 224 { .n_name = "_ncvhash" }, 225#define X_NCVHASHTBL 13 226 { .n_name = "_ncvhashtbl" }, 227#define X_HASHNL_SIZE 14 /* must be last */ 228 { .n_name = NULL }, 229}; 230 231/* 232 * Namelist for kernel histories 233 */ 234struct nlist histnl[] = 235{ 236 { .n_name = "_kern_histories" }, 237#define X_KERN_HISTORIES 0 238 { .n_name = NULL }, 239}; 240 241 242#define KILO 1024 243 244struct cpu_counter { 245 uint64_t nintr; 246 uint64_t nsyscall; 247 uint64_t nswtch; 248 uint64_t nfault; 249 uint64_t ntrap; 250 uint64_t nsoft; 251} cpucounter, ocpucounter; 252 253struct uvmexp_sysctl uvmexp, ouvmexp; 254int ndrives; 255 256int winlines = 20; 257 258kvm_t *kd; 259 260 261#define FORKSTAT 0x001 262#define INTRSTAT 0x002 263#define MEMSTAT 0x004 264#define SUMSTAT 0x008 265#define EVCNTSTAT 0x010 266#define VMSTAT 0x020 267#define HISTLIST 0x040 268#define HISTDUMP 0x080 269#define HASHSTAT 0x100 270#define HASHLIST 0x200 271#define VMTOTAL 0x400 272#define POOLCACHESTAT 0x800 273 274/* 275 * Print single word. `ovflow' is number of characters didn't fit 276 * on the last word. `fmt' is a format string to print this word. 277 * It must contain asterisk for field width. `width' is a width 278 * occupied by this word. `fixed' is a number of constant chars in 279 * `fmt'. `val' is a value to be printed using format string `fmt'. 280 */ 281#define PRWORD(ovflw, fmt, width, fixed, val) do { \ 282 (ovflw) += printf((fmt), \ 283 (width) - (fixed) - (ovflw) > 0 ? \ 284 (width) - (fixed) - (ovflw) : 0, \ 285 (val)) - (width); \ 286 if ((ovflw) < 0) \ 287 (ovflw) = 0; \ 288} while (/* CONSTCOND */0) 289 290void cpustats(int *); 291void cpucounters(struct cpu_counter *); 292void deref_kptr(const void *, void *, size_t, const char *); 293void drvstats(int *); 294void doevcnt(int verbose, int type); 295void dohashstat(int, int, const char *); 296void dointr(int verbose); 297void dopool(int, int); 298void dopoolcache(int); 299void dosum(void); 300void dovmstat(struct timespec *, int); 301void print_total_hdr(void); 302void dovmtotal(struct timespec *, int); 303void kread(struct nlist *, int, void *, size_t); 304int kreadc(struct nlist *, int, void *, size_t); 305void needhdr(int); 306void getnlist(int); 307long getuptime(void); 308void printhdr(void); 309long pct(long, long); 310__dead static void usage(void); 311void doforkst(void); 312 313void hist_traverse(int, const char *); 314void hist_dodump(struct kern_history *); 315 316int main(int, char **); 317char **choosedrives(char **); 318 319/* Namelist and memory file names. */ 320char *nlistf, *memf; 321 322/* allow old usage [vmstat 1] */ 323#define BACKWARD_COMPATIBILITY 324 325static const int vmmeter_mib[] = { CTL_VM, VM_METER }; 326static const int uvmexp2_mib[] = { CTL_VM, VM_UVMEXP2 }; 327static const int boottime_mib[] = { CTL_KERN, KERN_BOOTTIME }; 328static char kvm_errbuf[_POSIX2_LINE_MAX]; 329 330int 331main(int argc, char *argv[]) 332{ 333 int c, todo, verbose, wide; 334 struct timespec interval; 335 int reps; 336 gid_t egid = getegid(); 337 const char *histname, *hashname; 338 339 histname = hashname = NULL; 340 (void)setegid(getgid()); 341 memf = nlistf = NULL; 342 reps = todo = verbose = wide = 0; 343 interval.tv_sec = 0; 344 interval.tv_nsec = 0; 345 while ((c = getopt(argc, argv, "Cc:efh:HilLM:mN:stu:UvWw:")) != -1) { 346 switch (c) { 347 case 'c': 348 reps = atoi(optarg); 349 break; 350 case 'C': 351 todo |= POOLCACHESTAT; 352 break; 353 case 'e': 354 todo |= EVCNTSTAT; 355 break; 356 case 'f': 357 todo |= FORKSTAT; 358 break; 359 case 'h': 360 hashname = optarg; 361 /* FALLTHROUGH */ 362 case 'H': 363 todo |= HASHSTAT; 364 break; 365 case 'i': 366 todo |= INTRSTAT; 367 break; 368 case 'l': 369 todo |= HISTLIST; 370 break; 371 case 'L': 372 todo |= HASHLIST; 373 break; 374 case 'M': 375 memf = optarg; 376 break; 377 case 'm': 378 todo |= MEMSTAT; 379 break; 380 case 'N': 381 nlistf = optarg; 382 break; 383 case 's': 384 todo |= SUMSTAT; 385 break; 386 case 't': 387 todo |= VMTOTAL; 388 break; 389 case 'u': 390 histname = optarg; 391 /* FALLTHROUGH */ 392 case 'U': 393 todo |= HISTDUMP; 394 break; 395 case 'v': 396 verbose++; 397 break; 398 case 'W': 399 wide++; 400 break; 401 case 'w': 402 interval.tv_sec = atol(optarg); 403 break; 404 case '?': 405 default: 406 usage(); 407 } 408 } 409 argc -= optind; 410 argv += optind; 411 412 if (todo == 0) 413 todo = VMSTAT; 414 415 /* 416 * Discard setgid privileges. If not the running kernel, we toss 417 * them away totally so that bad guys can't print interesting stuff 418 * from kernel memory, otherwise switch back to kmem for the 419 * duration of the kvm_openfiles() call. 420 */ 421 if (nlistf != NULL || memf != NULL) 422 (void)setgid(getgid()); 423 else 424 (void)setegid(egid); 425 426 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, kvm_errbuf); 427 if (kd == NULL) { 428 if (nlistf != NULL || memf != NULL) { 429 errx(1, "kvm_openfiles: %s", kvm_errbuf); 430 } 431 } 432 433 if (nlistf == NULL && memf == NULL) 434 (void)setgid(getgid()); 435 436 437 if (todo & VMSTAT) { 438 struct winsize winsize; 439 440 (void)drvinit(0);/* Initialize disk stats, no disks selected. */ 441 442 (void)setgid(getgid()); /* don't need privs anymore */ 443 444 argv = choosedrives(argv); /* Select disks. */ 445 winsize.ws_row = 0; 446 (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize); 447 if (winsize.ws_row > 0) 448 winlines = winsize.ws_row; 449 450 } 451 452#ifdef BACKWARD_COMPATIBILITY 453 if (*argv) { 454 interval.tv_sec = atol(*argv); 455 if (*++argv) 456 reps = atoi(*argv); 457 } 458#endif 459 460 if (interval.tv_sec) { 461 if (!reps) 462 reps = -1; 463 } else if (reps) 464 interval.tv_sec = 1; 465 466 467 getnlist(todo); 468 /* 469 * Statistics dumping is incompatible with the default 470 * VMSTAT/dovmstat() output. So perform the interval/reps handling 471 * for it here. 472 */ 473 if ((todo & (VMSTAT|VMTOTAL)) == 0) { 474 for (;;) { 475 if (todo & (HISTLIST|HISTDUMP)) { 476 if ((todo & (HISTLIST|HISTDUMP)) == 477 (HISTLIST|HISTDUMP)) 478 errx(1, "you may list or dump," 479 " but not both!"); 480 hist_traverse(todo, histname); 481 (void)putchar('\n'); 482 } 483 if (todo & FORKSTAT) { 484 doforkst(); 485 (void)putchar('\n'); 486 } 487 if (todo & MEMSTAT) { 488 dopool(verbose, wide); 489 (void)putchar('\n'); 490 } 491 if (todo & POOLCACHESTAT) { 492 dopoolcache(verbose); 493 (void)putchar('\n'); 494 } 495 if (todo & SUMSTAT) { 496 dosum(); 497 (void)putchar('\n'); 498 } 499 if (todo & INTRSTAT) { 500 dointr(verbose); 501 (void)putchar('\n'); 502 } 503 if (todo & EVCNTSTAT) { 504 doevcnt(verbose, EVCNT_TYPE_ANY); 505 (void)putchar('\n'); 506 } 507 if (todo & (HASHLIST|HASHSTAT)) { 508 if ((todo & (HASHLIST|HASHSTAT)) == 509 (HASHLIST|HASHSTAT)) 510 errx(1, "you may list or display," 511 " but not both!"); 512 dohashstat(verbose, todo, hashname); 513 (void)putchar('\n'); 514 } 515 516 fflush(stdout); 517 if (reps >= 0 && --reps <=0) 518 break; 519 (void)nanosleep(&interval, NULL); 520 } 521 } else { 522 if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) { 523 errx(1, "you may not both do vmstat and vmtotal"); 524 } 525 if (todo & VMSTAT) 526 dovmstat(&interval, reps); 527 if (todo & VMTOTAL) 528 dovmtotal(&interval, reps); 529 } 530 return 0; 531} 532 533void 534getnlist(int todo) 535{ 536 static int namelist_done = 0; 537 static int done = 0; 538 int c; 539 size_t i; 540 541 if (kd == NULL) 542 errx(1, "kvm_openfiles: %s", kvm_errbuf); 543 544 if (!namelist_done) { 545 namelist_done = 1; 546 if ((c = kvm_nlist(kd, namelist)) != 0) { 547 int doexit = 0; 548 if (c == -1) 549 errx(1, "kvm_nlist: %s %s", 550 "namelist", kvm_geterr(kd)); 551 for (i = 0; i < __arraycount(namelist)-1; i++) 552 if (namelist[i].n_type == 0 && 553 i != X_TIME_SECOND && 554 i != X_TIME) { 555 if (doexit++ == 0) 556 (void)fprintf(stderr, 557 "%s: undefined symbols:", 558 getprogname()); 559 (void)fprintf(stderr, " %s", 560 namelist[i].n_name); 561 } 562 if (doexit) { 563 (void)fputc('\n', stderr); 564 exit(1); 565 } 566 } 567 } 568 if ((todo & (SUMSTAT|INTRSTAT)) && !(done & (SUMSTAT|INTRSTAT))) { 569 done |= SUMSTAT|INTRSTAT; 570 (void) kvm_nlist(kd, intrnl); 571 } 572 if ((todo & (HASHLIST|HASHSTAT)) && !(done & (HASHLIST|HASHSTAT))) { 573 done |= HASHLIST|HASHSTAT; 574 if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE) 575 errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd)); 576 } 577 if ((todo & (HISTLIST|HISTDUMP)) && !(done & (HISTLIST|HISTDUMP))) { 578 done |= HISTLIST|HISTDUMP; 579 if (kvm_nlist(kd, histnl) == -1) 580 errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd)); 581 } 582} 583 584char ** 585choosedrives(char **argv) 586{ 587 size_t i; 588 589 /* 590 * Choose drives to be displayed. Priority goes to (in order) drives 591 * supplied as arguments, default drives. If everything isn't filled 592 * in and there are drives not taken care of, display the first few 593 * that fit. 594 */ 595#define BACKWARD_COMPATIBILITY 596 for (ndrives = 0; *argv; ++argv) { 597#ifdef BACKWARD_COMPATIBILITY 598 if (isdigit((unsigned char)**argv)) 599 break; 600#endif 601 for (i = 0; i < ndrive; i++) { 602 if (strcmp(dr_name[i], *argv)) 603 continue; 604 drv_select[i] = 1; 605 ++ndrives; 606 break; 607 } 608 } 609 for (i = 0; i < ndrive && ndrives < 2; i++) { 610 if (drv_select[i]) 611 continue; 612 drv_select[i] = 1; 613 ++ndrives; 614 } 615 616 return (argv); 617} 618 619long 620getuptime(void) 621{ 622 static struct timespec boottime; 623 struct timespec now; 624 time_t uptime, nowsec; 625 626 if (memf == NULL) { 627 if (boottime.tv_sec == 0) { 628 size_t buflen = sizeof(boottime); 629 if (sysctl(boottime_mib, __arraycount(boottime_mib), 630 &boottime, &buflen, NULL, 0) == -1) 631 warn("Can't get boottime"); 632 } 633 clock_gettime(CLOCK_REALTIME, &now); 634 } else { 635 if (boottime.tv_sec == 0) 636 kread(namelist, X_BOOTTIME, &boottime, 637 sizeof(boottime)); 638 if (kreadc(namelist, X_TIME_SECOND, &nowsec, sizeof(nowsec))) { 639 /* 640 * XXX this assignment dance can be removed once 641 * timeval tv_sec is SUS mandated time_t 642 */ 643 now.tv_sec = nowsec; 644 now.tv_nsec = 0; 645 } else { 646 kread(namelist, X_TIME, &now, sizeof(now)); 647 } 648 } 649 uptime = now.tv_sec - boottime.tv_sec; 650 if (uptime <= 0 || uptime > 60*60*24*365*10) 651 errx(1, "time makes no sense; namelist must be wrong."); 652 return (uptime); 653} 654 655int hz, hdrcnt; 656 657void 658print_total_hdr(void) 659{ 660 661 (void)printf("procs memory\n"); 662 (void)printf("ru dw pw sl"); 663 (void)printf(" total-v active-v active-r"); 664 (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n"); 665 hdrcnt = winlines - 2; 666} 667 668void 669dovmtotal(struct timespec *interval, int reps) 670{ 671 struct vmtotal total; 672 size_t size; 673 674 (void)signal(SIGCONT, needhdr); 675 676 for (hdrcnt = 1;;) { 677 if (!--hdrcnt) 678 print_total_hdr(); 679 if (memf != NULL) { 680 warnx("Unable to get vmtotals from crash dump."); 681 (void)memset(&total, 0, sizeof(total)); 682 } else { 683 size = sizeof(total); 684 if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib), 685 &total, &size, NULL, 0) == -1) { 686 warn("Can't get vmtotals"); 687 (void)memset(&total, 0, sizeof(total)); 688 } 689 } 690 (void)printf("%2d ", total.t_rq); 691 (void)printf("%2d ", total.t_dw); 692 (void)printf("%2d ", total.t_pw); 693 (void)printf("%2d ", total.t_sl); 694 695 (void)printf("%9d ", total.t_vm); 696 (void)printf("%9d ", total.t_avm); 697 (void)printf("%9d ", total.t_arm); 698 (void)printf("%5d ", total.t_vmshr); 699 (void)printf("%6d ", total.t_avmshr); 700 (void)printf("%5d ", total.t_rmshr); 701 (void)printf("%6d ", total.t_armshr); 702 (void)printf("%5d", total.t_free); 703 704 (void)putchar('\n'); 705 706 (void)fflush(stdout); 707 if (reps >= 0 && --reps <= 0) 708 break; 709 710 (void)nanosleep(interval, NULL); 711 } 712} 713 714void 715dovmstat(struct timespec *interval, int reps) 716{ 717 struct vmtotal total; 718 time_t uptime, halfuptime; 719 size_t size; 720 int pagesize = getpagesize(); 721 int ovflw; 722 723 uptime = getuptime(); 724 halfuptime = uptime / 2; 725 (void)signal(SIGCONT, needhdr); 726 727 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) 728 kread(namelist, X_STATHZ, &hz, sizeof(hz)); 729 if (!hz) 730 kread(namelist, X_HZ, &hz, sizeof(hz)); 731 732 kread(namelist, X_CPU_INFOS, &cpu_infos, sizeof(cpu_infos)); 733 734 for (hdrcnt = 1;;) { 735 if (!--hdrcnt) 736 printhdr(); 737 /* Read new disk statistics */ 738 cpureadstats(); 739 drvreadstats(); 740 tkreadstats(); 741 if (memf != NULL) { 742 struct uvmexp uvmexp_kernel; 743 /* 744 * XXX Can't do this if we're reading a crash 745 * XXX dump because they're lazily-calculated. 746 */ 747 warnx("Unable to get vmtotals from crash dump."); 748 (void)memset(&total, 0, sizeof(total)); 749 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel)); 750#define COPY(field) uvmexp.field = uvmexp_kernel.field 751 COPY(pdreact); 752 COPY(pageins); 753 COPY(pgswapout); 754 COPY(pdfreed); 755 COPY(pdscans); 756#undef COPY 757 } else { 758 size = sizeof(total); 759 if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib), 760 &total, &size, NULL, 0) == -1) { 761 warn("Can't get vmtotals"); 762 (void)memset(&total, 0, sizeof(total)); 763 } 764 size = sizeof(uvmexp); 765 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp, 766 &size, NULL, 0) == -1) 767 warn("sysctl vm.uvmexp2 failed"); 768 } 769 cpucounters(&cpucounter); 770 ovflw = 0; 771 PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1); 772 PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw); 773#define pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10)) 774#define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */ 775 PRWORD(ovflw, " %*ld", 9, 1, pgtok(total.t_avm)); 776 PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free)); 777 PRWORD(ovflw, " %*ld", 5, 1, 778 rate(cpucounter.nfault - ocpucounter.nfault)); 779 PRWORD(ovflw, " %*ld", 4, 1, 780 rate(uvmexp.pdreact - ouvmexp.pdreact)); 781 PRWORD(ovflw, " %*ld", 4, 1, 782 rate(uvmexp.pageins - ouvmexp.pageins)); 783 PRWORD(ovflw, " %*ld", 5, 1, 784 rate(uvmexp.pgswapout - ouvmexp.pgswapout)); 785 PRWORD(ovflw, " %*ld", 5, 1, 786 rate(uvmexp.pdfreed - ouvmexp.pdfreed)); 787 PRWORD(ovflw, " %*ld", 6, 2, 788 rate(uvmexp.pdscans - ouvmexp.pdscans)); 789 drvstats(&ovflw); 790 PRWORD(ovflw, " %*ld", 5, 1, 791 rate(cpucounter.nintr - ocpucounter.nintr)); 792 PRWORD(ovflw, " %*ld", 5, 1, 793 rate(cpucounter.nsyscall - ocpucounter.nsyscall)); 794 PRWORD(ovflw, " %*ld", 4, 1, 795 rate(cpucounter.nswtch - ocpucounter.nswtch)); 796 cpustats(&ovflw); 797 (void)putchar('\n'); 798 (void)fflush(stdout); 799 if (reps >= 0 && --reps <= 0) 800 break; 801 ouvmexp = uvmexp; 802 ocpucounter = cpucounter; 803 uptime = interval->tv_sec; 804 /* 805 * We round upward to avoid losing low-frequency events 806 * (i.e., >= 1 per interval but < 1 per second). 807 */ 808 halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2; 809 (void)nanosleep(interval, NULL); 810 } 811} 812 813void 814printhdr(void) 815{ 816 size_t i; 817 818 (void)printf(" procs memory page%*s", 23, ""); 819 if (ndrives > 0) 820 (void)printf("%s %*sfaults cpu\n", 821 ((ndrives > 1) ? "disks" : "disk"), 822 ((ndrives > 1) ? ndrives * 3 - 4 : 0), ""); 823 else 824 (void)printf("%*s faults cpu\n", 825 ndrives * 3, ""); 826 827 (void)printf(" r b avm fre flt re pi po fr sr "); 828 for (i = 0; i < ndrive; i++) 829 if (drv_select[i]) 830 (void)printf("%c%c ", dr_name[i][0], 831 dr_name[i][strlen(dr_name[i]) - 1]); 832 (void)printf(" in sy cs us sy id\n"); 833 hdrcnt = winlines - 2; 834} 835 836/* 837 * Force a header to be prepended to the next output. 838 */ 839void 840/*ARGSUSED*/ 841needhdr(int dummy) 842{ 843 844 hdrcnt = 1; 845} 846 847long 848pct(long top, long bot) 849{ 850 long ans; 851 852 if (bot == 0) 853 return (0); 854 ans = (long)((quad_t)top * 100 / bot); 855 return (ans); 856} 857 858#define PCT(top, bot) (int)pct((long)(top), (long)(bot)) 859 860void 861dosum(void) 862{ 863 struct nchstats_sysctl nch_stats; 864 uint64_t nchtotal; 865 size_t ssize; 866 int active_kernel; 867 struct cpu_counter cc; 868 869 /* 870 * The "active" and "inactive" variables 871 * are now estimated by the kernel and sadly 872 * can not easily be dug out of a crash dump. 873 */ 874 ssize = sizeof(uvmexp); 875 memset(&uvmexp, 0, ssize); 876 active_kernel = (memf == NULL); 877 if (active_kernel) { 878 /* only on active kernel */ 879 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp, 880 &ssize, NULL, 0) == -1) 881 warn("sysctl vm.uvmexp2 failed"); 882 } else { 883 struct uvmexp uvmexp_kernel; 884 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel)); 885#define COPY(field) uvmexp.field = uvmexp_kernel.field 886 COPY(pagesize); 887 COPY(ncolors); 888 COPY(npages); 889 COPY(free); 890 COPY(paging); 891 COPY(wired); 892 COPY(zeropages); 893 COPY(reserve_pagedaemon); 894 COPY(reserve_kernel); 895 COPY(anonpages); 896 COPY(filepages); 897 COPY(execpages); 898 COPY(freemin); 899 COPY(freetarg); 900 COPY(wiredmax); 901 COPY(nswapdev); 902 COPY(swpages); 903 COPY(swpginuse); 904 COPY(nswget); 905 COPY(pageins); 906 COPY(pdpageouts); 907 COPY(pgswapin); 908 COPY(pgswapout); 909 COPY(forks); 910 COPY(forks_ppwait); 911 COPY(forks_sharevm); 912 COPY(pga_zerohit); 913 COPY(pga_zeromiss); 914 COPY(zeroaborts); 915 COPY(colorhit); 916 COPY(colormiss); 917 COPY(cpuhit); 918 COPY(cpumiss); 919 COPY(fltnoram); 920 COPY(fltnoanon); 921 COPY(fltpgwait); 922 COPY(fltpgrele); 923 COPY(fltrelck); 924 COPY(fltrelckok); 925 COPY(fltanget); 926 COPY(fltanretry); 927 COPY(fltamcopy); 928 COPY(fltamcopy); 929 COPY(fltnomap); 930 COPY(fltlget); 931 COPY(fltget); 932 COPY(flt_anon); 933 COPY(flt_acow); 934 COPY(flt_obj); 935 COPY(flt_prcopy); 936 COPY(flt_przero); 937 COPY(pdwoke); 938 COPY(pdrevs); 939 COPY(pdfreed); 940 COPY(pdscans); 941 COPY(pdanscan); 942 COPY(pdobscan); 943 COPY(pdreact); 944 COPY(pdbusy); 945 COPY(pdpending); 946 COPY(pddeact); 947#undef COPY 948 } 949 950 951 (void)printf("%9" PRIu64 " bytes per page\n", uvmexp.pagesize); 952 953 (void)printf("%9" PRIu64 " page color%s\n", 954 uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s"); 955 956 (void)printf("%9" PRIu64 " pages managed\n", uvmexp.npages); 957 (void)printf("%9" PRIu64 " pages free\n", uvmexp.free); 958 if (active_kernel) { 959 (void)printf("%9" PRIu64 " pages active\n", uvmexp.active); 960 (void)printf("%9" PRIu64 " pages inactive\n", uvmexp.inactive); 961 } 962 (void)printf("%9" PRIu64 " pages paging\n", uvmexp.paging); 963 (void)printf("%9" PRIu64 " pages wired\n", uvmexp.wired); 964 (void)printf("%9" PRIu64 " zero pages\n", uvmexp.zeropages); 965 (void)printf("%9" PRIu64 " reserve pagedaemon pages\n", 966 uvmexp.reserve_pagedaemon); 967 (void)printf("%9" PRIu64 " reserve kernel pages\n", uvmexp.reserve_kernel); 968 (void)printf("%9" PRIu64 " anonymous pages\n", uvmexp.anonpages); 969 (void)printf("%9" PRIu64 " cached file pages\n", uvmexp.filepages); 970 (void)printf("%9" PRIu64 " cached executable pages\n", uvmexp.execpages); 971 972 (void)printf("%9" PRIu64 " minimum free pages\n", uvmexp.freemin); 973 (void)printf("%9" PRIu64 " target free pages\n", uvmexp.freetarg); 974 (void)printf("%9" PRIu64 " maximum wired pages\n", uvmexp.wiredmax); 975 976 (void)printf("%9" PRIu64 " swap devices\n", uvmexp.nswapdev); 977 (void)printf("%9" PRIu64 " swap pages\n", uvmexp.swpages); 978 (void)printf("%9" PRIu64 " swap pages in use\n", uvmexp.swpginuse); 979 (void)printf("%9" PRIu64 " swap allocations\n", uvmexp.nswget); 980 981 kread(namelist, X_CPU_INFOS, &cpu_infos, sizeof(cpu_infos)); 982 cpucounters(&cc); 983 984 (void)printf("%9" PRIu64 " total faults taken\n", cc.nfault); 985 (void)printf("%9" PRIu64 " traps\n", cc.ntrap); 986 (void)printf("%9" PRIu64 " device interrupts\n", cc.nintr); 987 (void)printf("%9" PRIu64 " CPU context switches\n", cc.nswtch); 988 (void)printf("%9" PRIu64 " software interrupts\n", cc.nsoft); 989 (void)printf("%9" PRIu64 " system calls\n", cc.nsyscall); 990 (void)printf("%9" PRIu64 " pagein requests\n", uvmexp.pageins); 991 (void)printf("%9" PRIu64 " pageout requests\n", uvmexp.pdpageouts); 992 (void)printf("%9" PRIu64 " pages swapped in\n", uvmexp.pgswapin); 993 (void)printf("%9" PRIu64 " pages swapped out\n", uvmexp.pgswapout); 994 (void)printf("%9" PRIu64 " forks total\n", uvmexp.forks); 995 (void)printf("%9" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait); 996 (void)printf("%9" PRIu64 " forks shared address space with parent\n", 997 uvmexp.forks_sharevm); 998 (void)printf("%9" PRIu64 " pagealloc zero wanted and avail\n", 999 uvmexp.pga_zerohit); 1000 (void)printf("%9" PRIu64 " pagealloc zero wanted and not avail\n", 1001 uvmexp.pga_zeromiss); 1002 (void)printf("%9" PRIu64 " aborts of idle page zeroing\n", 1003 uvmexp.zeroaborts); 1004 (void)printf("%9" PRIu64 " pagealloc desired color avail\n", 1005 uvmexp.colorhit); 1006 (void)printf("%9" PRIu64 " pagealloc desired color not avail\n", 1007 uvmexp.colormiss); 1008 (void)printf("%9" PRIu64 " pagealloc local cpu avail\n", 1009 uvmexp.cpuhit); 1010 (void)printf("%9" PRIu64 " pagealloc local cpu not avail\n", 1011 uvmexp.cpumiss); 1012 1013 (void)printf("%9" PRIu64 " faults with no memory\n", uvmexp.fltnoram); 1014 (void)printf("%9" PRIu64 " faults with no anons\n", uvmexp.fltnoanon); 1015 (void)printf("%9" PRIu64 " faults had to wait on pages\n", uvmexp.fltpgwait); 1016 (void)printf("%9" PRIu64 " faults found released page\n", uvmexp.fltpgrele); 1017 (void)printf("%9" PRIu64 " faults relock (%" PRIu64 " ok)\n", uvmexp.fltrelck, 1018 uvmexp.fltrelckok); 1019 (void)printf("%9" PRIu64 " anon page faults\n", uvmexp.fltanget); 1020 (void)printf("%9" PRIu64 " anon retry faults\n", uvmexp.fltanretry); 1021 (void)printf("%9" PRIu64 " amap copy faults\n", uvmexp.fltamcopy); 1022 (void)printf("%9" PRIu64 " neighbour anon page faults\n", uvmexp.fltnamap); 1023 (void)printf("%9" PRIu64 " neighbour object page faults\n", uvmexp.fltnomap); 1024 (void)printf("%9" PRIu64 " locked pager get faults\n", uvmexp.fltlget); 1025 (void)printf("%9" PRIu64 " unlocked pager get faults\n", uvmexp.fltget); 1026 (void)printf("%9" PRIu64 " anon faults\n", uvmexp.flt_anon); 1027 (void)printf("%9" PRIu64 " anon copy on write faults\n", uvmexp.flt_acow); 1028 (void)printf("%9" PRIu64 " object faults\n", uvmexp.flt_obj); 1029 (void)printf("%9" PRIu64 " promote copy faults\n", uvmexp.flt_prcopy); 1030 (void)printf("%9" PRIu64 " promote zero fill faults\n", uvmexp.flt_przero); 1031 1032 (void)printf("%9" PRIu64 " times daemon wokeup\n",uvmexp.pdwoke); 1033 (void)printf("%9" PRIu64 " revolutions of the clock hand\n", uvmexp.pdrevs); 1034 (void)printf("%9" PRIu64 " pages freed by daemon\n", uvmexp.pdfreed); 1035 (void)printf("%9" PRIu64 " pages scanned by daemon\n", uvmexp.pdscans); 1036 (void)printf("%9" PRIu64 " anonymous pages scanned by daemon\n", 1037 uvmexp.pdanscan); 1038 (void)printf("%9" PRIu64 " object pages scanned by daemon\n", uvmexp.pdobscan); 1039 (void)printf("%9" PRIu64 " pages reactivated\n", uvmexp.pdreact); 1040 (void)printf("%9" PRIu64 " pages found busy by daemon\n", uvmexp.pdbusy); 1041 (void)printf("%9" PRIu64 " total pending pageouts\n", uvmexp.pdpending); 1042 (void)printf("%9" PRIu64 " pages deactivated\n", uvmexp.pddeact); 1043 1044 if (active_kernel) { 1045 ssize = sizeof(nch_stats); 1046 if (sysctlbyname("vfs.namecache_stats", &nch_stats, &ssize, 1047 NULL, 0)) { 1048 warn("vfs.namecache_stats failed"); 1049 memset(&nch_stats, 0, sizeof(nch_stats)); 1050 } 1051 } else { 1052 struct nchstats nch_stats_kvm; 1053 1054 kread(namelist, X_NCHSTATS, &nch_stats_kvm, 1055 sizeof(nch_stats_kvm)); 1056 nch_stats.ncs_goodhits = nch_stats_kvm.ncs_goodhits; 1057 nch_stats.ncs_neghits = nch_stats_kvm.ncs_neghits; 1058 nch_stats.ncs_badhits = nch_stats_kvm.ncs_badhits; 1059 nch_stats.ncs_falsehits = nch_stats_kvm.ncs_falsehits; 1060 nch_stats.ncs_miss = nch_stats_kvm.ncs_miss; 1061 nch_stats.ncs_long = nch_stats_kvm.ncs_long; 1062 nch_stats.ncs_pass2 = nch_stats_kvm.ncs_pass2; 1063 nch_stats.ncs_2passes = nch_stats_kvm.ncs_2passes; 1064 nch_stats.ncs_revhits = nch_stats_kvm.ncs_revhits; 1065 nch_stats.ncs_revmiss = nch_stats_kvm.ncs_revmiss; 1066 } 1067 1068 nchtotal = nch_stats.ncs_goodhits + nch_stats.ncs_neghits + 1069 nch_stats.ncs_badhits + nch_stats.ncs_falsehits + 1070 nch_stats.ncs_miss + nch_stats.ncs_long; 1071 (void)printf("%9" PRIu64 " total name lookups\n", nchtotal); 1072 (void)printf("%9" PRIu64 " good hits\n", nch_stats.ncs_goodhits); 1073 (void)printf("%9" PRIu64 " negative hits\n", nch_stats.ncs_neghits); 1074 (void)printf("%9" PRIu64 " bad hits\n", nch_stats.ncs_badhits); 1075 (void)printf("%9" PRIu64 " false hits\n", nch_stats.ncs_falsehits); 1076 (void)printf("%9" PRIu64 " miss\n", nch_stats.ncs_miss); 1077 (void)printf("%9" PRIu64 " too long\n", nch_stats.ncs_long); 1078 (void)printf("%9" PRIu64 " pass2 hits\n", nch_stats.ncs_pass2); 1079 (void)printf("%9" PRIu64 " 2passes\n", nch_stats.ncs_2passes); 1080 (void)printf( 1081 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", 1082 "", PCT(nch_stats.ncs_goodhits, nchtotal), 1083 PCT(nch_stats.ncs_neghits, nchtotal), 1084 PCT(nch_stats.ncs_pass2, nchtotal)); 1085 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", 1086 PCT(nch_stats.ncs_badhits, nchtotal), 1087 PCT(nch_stats.ncs_falsehits, nchtotal), 1088 PCT(nch_stats.ncs_long, nchtotal)); 1089} 1090 1091void 1092doforkst(void) 1093{ 1094 kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp)); 1095 if (memf != NULL) { 1096 struct uvmexp uvmexp_kernel; 1097 kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel)); 1098#define COPY(field) uvmexp.field = uvmexp_kernel.field 1099 COPY(forks); 1100 COPY(forks_ppwait); 1101 COPY(forks_sharevm); 1102#undef COPY 1103 } else { 1104 size_t size = sizeof(uvmexp); 1105 if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp, 1106 &size, NULL, 0) == -1) 1107 warn("sysctl vm.uvmexp2 failed"); 1108 } 1109 1110 (void)printf("%" PRIu64 " forks total\n", uvmexp.forks); 1111 (void)printf("%" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait); 1112 (void)printf("%" PRIu64 " forks shared address space with parent\n", 1113 uvmexp.forks_sharevm); 1114} 1115 1116void 1117drvstats(int *ovflwp) 1118{ 1119 size_t dn; 1120 double etime; 1121 int ovflw = *ovflwp; 1122 1123 /* Calculate disk stat deltas. */ 1124 cpuswap(); 1125 drvswap(); 1126 tkswap(); 1127 etime = cur.cp_etime; 1128 1129 for (dn = 0; dn < ndrive; ++dn) { 1130 if (!drv_select[dn]) 1131 continue; 1132 PRWORD(ovflw, " %*.0f", 3, 1, 1133 (cur.rxfer[dn] + cur.wxfer[dn]) / etime); 1134 } 1135 *ovflwp = ovflw; 1136} 1137 1138void 1139cpucounters(struct cpu_counter *cc) 1140{ 1141 struct cpu_info **slot = cpu_infos; 1142 1143 memset(cc, 0, sizeof(*cc)); 1144 1145 for (;;) { 1146 struct cpu_info tci, *ci = NULL; 1147 1148 deref_kptr(slot++, &ci, sizeof(ci), "CPU array trashed"); 1149 if (!ci) { 1150 break; 1151 } 1152 1153 if ((size_t)kvm_read(kd, (u_long)ci, &tci, sizeof(tci)) 1154 != sizeof(tci)) { 1155 warnx("Can't read cpu info from %p (%s)", 1156 ci, kvm_geterr(kd)); 1157 memset(cc, 0, sizeof(*cc)); 1158 return; 1159 } 1160 cc->nintr += tci.ci_data.cpu_nintr; 1161 cc->nsyscall += tci.ci_data.cpu_nsyscall; 1162 cc->nswtch = tci.ci_data.cpu_nswtch; 1163 cc->nfault = tci.ci_data.cpu_nfault; 1164 cc->ntrap = tci.ci_data.cpu_ntrap; 1165 cc->nsoft = tci.ci_data.cpu_nsoft; 1166 } 1167} 1168 1169void 1170cpustats(int *ovflwp) 1171{ 1172 int state; 1173 double pcnt, total; 1174 double stat_us, stat_sy, stat_id; 1175 int ovflw = *ovflwp; 1176 1177 total = 0; 1178 for (state = 0; state < CPUSTATES; ++state) 1179 total += cur.cp_time[state]; 1180 if (total) 1181 pcnt = 100 / total; 1182 else 1183 pcnt = 0; 1184 stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt; 1185 stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt; 1186 stat_id = cur.cp_time[CP_IDLE] * pcnt; 1187 PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us); 1188 PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1, 1189 stat_sy); 1190 PRWORD(ovflw, " %*.0f", 3, 1, stat_id); 1191 *ovflwp = ovflw; 1192} 1193 1194void 1195dointr(int verbose) 1196{ 1197 unsigned long *intrcnt, *ointrcnt; 1198 unsigned long long inttotal, uptime; 1199 int nintr, inamlen; 1200 char *intrname, *ointrname; 1201 1202 inttotal = 0; 1203 uptime = getuptime(); 1204 (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate"); 1205 nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value; 1206 inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value; 1207 if (nintr != 0 && inamlen != 0) { 1208 ointrcnt = intrcnt = malloc((size_t)nintr); 1209 ointrname = intrname = malloc((size_t)inamlen); 1210 if (intrcnt == NULL || intrname == NULL) 1211 errx(1, "%s", ""); 1212 kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr); 1213 kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen); 1214 nintr /= sizeof(long); 1215 while (--nintr >= 0) { 1216 if (*intrcnt || verbose) 1217 (void)printf("%-34s %16llu %8llu\n", intrname, 1218 (unsigned long long)*intrcnt, 1219 (unsigned long long) 1220 (*intrcnt / uptime)); 1221 intrname += strlen(intrname) + 1; 1222 inttotal += *intrcnt++; 1223 } 1224 free(ointrcnt); 1225 free(ointrname); 1226 } 1227 1228 doevcnt(verbose, EVCNT_TYPE_INTR); 1229} 1230 1231void 1232doevcnt(int verbose, int type) 1233{ 1234 static const char * const evtypes [] = { "misc", "intr", "trap" }; 1235 uint64_t counttotal, uptime; 1236 struct evcntlist allevents; 1237 struct evcnt evcnt, *evptr; 1238 char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX]; 1239 1240 counttotal = 0; 1241 uptime = getuptime(); 1242 if (type == EVCNT_TYPE_ANY) 1243 (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate", 1244 "type"); 1245 1246 if (memf == NULL) do { 1247 const int mib[4] = { CTL_KERN, KERN_EVCNT, type, 1248 verbose ? KERN_EVCNT_COUNT_ANY : KERN_EVCNT_COUNT_NONZERO }; 1249 size_t buflen = 0; 1250 void *buf = NULL; 1251 const struct evcnt_sysctl *evs, *last_evs; 1252 for (;;) { 1253 size_t newlen; 1254 int error; 1255 if (buflen) 1256 buf = malloc(buflen); 1257 error = sysctl(mib, __arraycount(mib), 1258 buf, &newlen, NULL, 0); 1259 if (error) { 1260 err(1, "kern.evcnt"); 1261 if (buf) 1262 free(buf); 1263 return; 1264 } 1265 if (newlen <= buflen) { 1266 buflen = newlen; 1267 break; 1268 } 1269 if (buf) 1270 free(buf); 1271 buflen = newlen; 1272 } 1273 evs = buf; 1274 last_evs = (void *)((char *)buf + buflen); 1275 buflen /= sizeof(uint64_t); 1276 while (evs < last_evs 1277 && buflen >= sizeof(*evs)/sizeof(uint64_t) 1278 && buflen >= evs->ev_len) { 1279 (void)printf(type == EVCNT_TYPE_ANY ? 1280 "%s %s%*s %16"PRIu64" %8"PRIu64" %s\n" : 1281 "%s %s%*s %16"PRIu64" %8"PRIu64"\n", 1282 evs->ev_strings, 1283 evs->ev_strings + evs->ev_grouplen + 1, 1284 34 - (evs->ev_grouplen + 1 + evs->ev_namelen), "", 1285 evs->ev_count, 1286 evs->ev_count / uptime, 1287 (evs->ev_type < __arraycount(evtypes) ? 1288 evtypes[evs->ev_type] : "?")); 1289 buflen -= evs->ev_len; 1290 counttotal += evs->ev_count; 1291 evs = (const void *)((const uint64_t *)evs + evs->ev_len); 1292 } 1293 free(buf); 1294 if (type != EVCNT_TYPE_ANY) 1295 (void)printf("%-34s %16"PRIu64" %8"PRIu64"\n", 1296 "Total", counttotal, counttotal / uptime); 1297 return; 1298 } while (/*CONSTCOND*/ 0); 1299 1300 kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents); 1301 evptr = TAILQ_FIRST(&allevents); 1302 while (evptr) { 1303 deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed"); 1304 1305 evptr = TAILQ_NEXT(&evcnt, ev_list); 1306 if (evcnt.ev_count == 0 && !verbose) 1307 continue; 1308 if (type != EVCNT_TYPE_ANY && evcnt.ev_type != type) 1309 continue; 1310 1311 deref_kptr(evcnt.ev_group, evgroup, 1312 (size_t)evcnt.ev_grouplen + 1, "event chain trashed"); 1313 deref_kptr(evcnt.ev_name, evname, 1314 (size_t)evcnt.ev_namelen + 1, "event chain trashed"); 1315 1316 (void)printf(type == EVCNT_TYPE_ANY ? 1317 "%s %s%*s %16"PRIu64" %8"PRIu64" %s\n" : 1318 "%s %s%*s %16"PRIu64" %8"PRIu64"\n", 1319 evgroup, evname, 1320 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "", 1321 evcnt.ev_count, 1322 (evcnt.ev_count / uptime), 1323 (evcnt.ev_type < __arraycount(evtypes) ? 1324 evtypes[evcnt.ev_type] : "?")); 1325 1326 counttotal += evcnt.ev_count; 1327 } 1328 if (type != EVCNT_TYPE_ANY) 1329 (void)printf("%-34s %16"PRIu64" %8"PRIu64"\n", 1330 "Total", counttotal, counttotal / uptime); 1331} 1332 1333void 1334dopool(int verbose, int wide) 1335{ 1336 int first, ovflw; 1337 void *addr; 1338 long total, inuse, this_total, this_inuse; 1339 struct { 1340 uint64_t pt_nget; 1341 uint64_t pt_nfail; 1342 uint64_t pt_nput; 1343 uint64_t pt_nout; 1344 uint64_t pt_nitems; 1345 uint64_t pt_npagealloc; 1346 uint64_t pt_npagefree; 1347 uint64_t pt_npages; 1348 } pool_totals; 1349 char in_use[8]; 1350 char avail[8]; 1351 TAILQ_HEAD(,pool) pool_head; 1352 struct pool pool, *pp = &pool; 1353 struct pool_allocator pa; 1354 char name[32], maxp[32]; 1355 1356 memset(&pool_totals, 0, sizeof pool_totals); 1357 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head)); 1358 addr = TAILQ_FIRST(&pool_head); 1359 1360 total = inuse = 0; 1361 1362 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) { 1363 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed"); 1364 deref_kptr(pp->pr_alloc, &pa, sizeof(pa), 1365 "pool allocator trashed"); 1366 deref_kptr(pp->pr_wchan, name, sizeof(name), 1367 "pool wait channel trashed"); 1368 name[sizeof(name)-1] = '\0'; 1369 1370 if (first) { 1371 (void)printf("Memory resource pool statistics\n"); 1372 (void)printf( 1373 "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n", 1374 wide ? 16 : 11, "Name", 1375 wide ? 6 : 5, "Size", 1376 wide ? 12 : 9, "Requests", 1377 "Fail", 1378 wide ? 12 : 9, "Releases", 1379 wide ? " InUse" : "", 1380 wide ? " Avail" : "", 1381 wide ? 7 : 6, "Pgreq", 1382 wide ? 7 : 6, "Pgrel", 1383 "Npage", 1384 wide ? " PageSz" : "", 1385 "Hiwat", 1386 "Minpg", 1387 "Maxpg", 1388 "Idle", 1389 wide ? " Flags" : "", 1390 wide ? " Util" : ""); 1391 first = 0; 1392 } 1393 if (pp->pr_nget == 0 && !verbose) 1394 continue; 1395 if (pp->pr_maxpages == UINT_MAX) 1396 (void)snprintf(maxp, sizeof(maxp), "inf"); 1397 else 1398 (void)snprintf(maxp, sizeof(maxp), "%u", 1399 pp->pr_maxpages); 1400 ovflw = 0; 1401 PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name); 1402 PRWORD(ovflw, " %*u", wide ? 6 : 5, 1, pp->pr_size); 1403 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget); 1404 pool_totals.pt_nget += pp->pr_nget; 1405 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail); 1406 pool_totals.pt_nfail += pp->pr_nfail; 1407 PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput); 1408 pool_totals.pt_nput += pp->pr_nput; 1409 if (wide) { 1410 PRWORD(ovflw, " %*u", 7, 1, pp->pr_nout); 1411 pool_totals.pt_nout += pp->pr_nout; 1412 } 1413 if (wide) { 1414 PRWORD(ovflw, " %*u", 6, 1, pp->pr_nitems); 1415 pool_totals.pt_nitems += pp->pr_nitems; 1416 } 1417 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagealloc); 1418 pool_totals.pt_npagealloc += pp->pr_npagealloc; 1419 PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagefree); 1420 pool_totals.pt_npagefree += pp->pr_npagefree; 1421 PRWORD(ovflw, " %*u", 6, 1, pp->pr_npages); 1422 pool_totals.pt_npages += pp->pr_npages; 1423 if (wide) 1424 PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz); 1425 PRWORD(ovflw, " %*u", 6, 1, pp->pr_hiwat); 1426 PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages); 1427 PRWORD(ovflw, " %*s", 6, 1, maxp); 1428 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle); 1429 if (wide) 1430 PRWORD(ovflw, " 0x%0*x", 4, 1, 1431 pp->pr_flags | pp->pr_roflags); 1432 1433 this_inuse = pp->pr_nout * pp->pr_size; 1434 this_total = pp->pr_npages * pa.pa_pagesz; 1435 if (pp->pr_roflags & PR_RECURSIVE) { 1436 /* 1437 * Don't count in-use memory, since it's part 1438 * of another pool and will be accounted for 1439 * there. 1440 */ 1441 total += (this_total - this_inuse); 1442 } else { 1443 inuse += this_inuse; 1444 total += this_total; 1445 } 1446 if (wide) { 1447 if (this_total == 0) 1448 (void)printf(" ---"); 1449 else 1450 (void)printf(" %5.1f%%", 1451 (100.0 * this_inuse) / this_total); 1452 } 1453 (void)printf("\n"); 1454 } 1455 if (wide) { 1456 snprintf(in_use, sizeof in_use, "%7"PRId64, pool_totals.pt_nout); 1457 snprintf(avail, sizeof avail, "%6"PRId64, pool_totals.pt_nitems); 1458 } else { 1459 in_use[0] = '\0'; 1460 avail[0] = '\0'; 1461 } 1462 (void)printf( 1463 "%-*s%*s%*"PRId64"%5"PRId64"%*"PRId64"%s%s%*"PRId64"%*"PRId64"%6"PRId64"\n", 1464 wide ? 16 : 11, "Totals", 1465 wide ? 6 : 5, "", 1466 wide ? 12 : 9, pool_totals.pt_nget, 1467 pool_totals.pt_nfail, 1468 wide ? 12 : 9, pool_totals.pt_nput, 1469 in_use, 1470 avail, 1471 wide ? 7 : 6, pool_totals.pt_npagealloc, 1472 wide ? 7 : 6, pool_totals.pt_npagefree, 1473 pool_totals.pt_npages); 1474 1475 inuse /= KILO; 1476 total /= KILO; 1477 (void)printf( 1478 "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n", 1479 inuse, total, (100.0 * inuse) / total); 1480} 1481 1482void 1483dopoolcache(int verbose) 1484{ 1485 struct pool_cache pool_cache, *pc = &pool_cache; 1486 pool_cache_cpu_t cache_cpu, *cc = &cache_cpu; 1487 TAILQ_HEAD(,pool) pool_head; 1488 struct pool pool, *pp = &pool; 1489 char name[32]; 1490 uint64_t cpuhit, cpumiss, tot; 1491 void *addr; 1492 int first, ovflw; 1493 size_t i; 1494 double p; 1495 1496 kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head)); 1497 addr = TAILQ_FIRST(&pool_head); 1498 1499 for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) { 1500 deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed"); 1501 if (pp->pr_cache == NULL) 1502 continue; 1503 deref_kptr(pp->pr_wchan, name, sizeof(name), 1504 "pool wait channel trashed"); 1505 deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed"); 1506 if (pc->pc_misses == 0 && !verbose) 1507 continue; 1508 name[sizeof(name)-1] = '\0'; 1509 1510 cpuhit = 0; 1511 cpumiss = 0; 1512 for (i = 0; i < __arraycount(pc->pc_cpus); i++) { 1513 if ((addr = pc->pc_cpus[i]) == NULL) 1514 continue; 1515 deref_kptr(addr, cc, sizeof(*cc), 1516 "pool cache cpu trashed"); 1517 cpuhit += cc->cc_hits; 1518 cpumiss += cc->cc_misses; 1519 } 1520 1521 if (first) { 1522 (void)printf("Pool cache statistics.\n"); 1523 (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n", 1524 12, "Name", 1525 6, "Spin", 1526 6, "GrpSz", 1527 5, "Full", 1528 5, "Emty", 1529 10, "PoolLayer", 1530 11, "CacheLayer", 1531 6, "Hit%", 1532 12, "CpuLayer", 1533 6, "Hit%" 1534 ); 1535 first = 0; 1536 } 1537 1538 ovflw = 0; 1539 PRWORD(ovflw, "%-*s", 13, 1, name); 1540 PRWORD(ovflw, " %*llu", 6, 1, (long long)pc->pc_contended); 1541 PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize); 1542 PRWORD(ovflw, " %*u", 5, 1, pc->pc_nfull); 1543 PRWORD(ovflw, " %*u", 5, 1, pc->pc_nempty); 1544 PRWORD(ovflw, " %*llu", 10, 1, (long long)pc->pc_misses); 1545 1546 tot = pc->pc_hits + pc->pc_misses; 1547 p = pc->pc_hits * 100.0 / (tot); 1548 PRWORD(ovflw, " %*llu", 11, 1, (long long)tot); 1549 PRWORD(ovflw, " %*.1f", 6, 1, p); 1550 1551 tot = cpuhit + cpumiss; 1552 p = cpuhit * 100.0 / (tot); 1553 PRWORD(ovflw, " %*llu", 12, 1, (long long)tot); 1554 PRWORD(ovflw, " %*.1f", 6, 1, p); 1555 printf("\n"); 1556 } 1557} 1558 1559enum hashtype { /* from <sys/systm.h> */ 1560 HASH_LIST, 1561 HASH_TAILQ 1562}; 1563 1564struct uidinfo { /* XXX: no kernel header file */ 1565 LIST_ENTRY(uidinfo) ui_hash; 1566 uid_t ui_uid; 1567 long ui_proccnt; 1568}; 1569 1570struct kernel_hash { 1571 const char * description; /* description */ 1572 int hashsize; /* nlist index for hash size */ 1573 int hashtbl; /* nlist index for hash table */ 1574 enum hashtype type; /* type of hash table */ 1575 size_t offset; /* offset of {LIST,TAILQ}_NEXT */ 1576} khashes[] = 1577{ 1578 { 1579 "buffer hash", 1580 X_BUFHASH, X_BUFHASHTBL, 1581 HASH_LIST, offsetof(struct buf, b_hash) 1582 }, { 1583 "ipv4 address -> interface hash", 1584 X_IFADDRHASH, X_IFADDRHASHTBL, 1585 HASH_LIST, offsetof(struct in_ifaddr, ia_hash), 1586 }, { 1587 "name cache hash", 1588 X_NCHASH, X_NCHASHTBL, 1589 HASH_LIST, offsetof(struct namecache, nc_hash), 1590 }, { 1591 "name cache directory hash", 1592 X_NCVHASH, X_NCVHASHTBL, 1593 HASH_LIST, offsetof(struct namecache, nc_vhash), 1594 }, { 1595 "user info (uid -> used processes) hash", 1596 X_UIHASH, X_UIHASHTBL, 1597 HASH_LIST, offsetof(struct uidinfo, ui_hash), 1598 }, { 1599 NULL, -1, -1, 0, 0, 1600 } 1601}; 1602 1603void 1604dohashstat(int verbose, int todo, const char *hashname) 1605{ 1606 LIST_HEAD(, generic) *hashtbl_list; 1607 TAILQ_HEAD(, generic) *hashtbl_tailq; 1608 struct kernel_hash *curhash; 1609 void *hashaddr, *hashbuf, *nhashbuf, *nextaddr; 1610 size_t elemsize, hashbufsize, thissize; 1611 u_long hashsize, i; 1612 int used, items, chain, maxchain; 1613 1614 hashbuf = NULL; 1615 hashbufsize = 0; 1616 1617 if (todo & HASHLIST) { 1618 (void)printf("Supported hashes:\n"); 1619 for (curhash = khashes; curhash->description; curhash++) { 1620 if (hashnl[curhash->hashsize].n_value == 0 || 1621 hashnl[curhash->hashtbl].n_value == 0) 1622 continue; 1623 (void)printf("\t%-16s%s\n", 1624 hashnl[curhash->hashsize].n_name + 1, 1625 curhash->description); 1626 } 1627 return; 1628 } 1629 1630 if (hashname != NULL) { 1631 for (curhash = khashes; curhash->description; curhash++) { 1632 if (strcmp(hashnl[curhash->hashsize].n_name + 1, 1633 hashname) == 0 && 1634 hashnl[curhash->hashsize].n_value != 0 && 1635 hashnl[curhash->hashtbl].n_value != 0) 1636 break; 1637 } 1638 if (curhash->description == NULL) { 1639 warnx("%s: no such hash", hashname); 1640 return; 1641 } 1642 } 1643 1644 (void)printf( 1645 "%-16s %8s %8s %8s %8s %8s %8s\n" 1646 "%-16s %8s %8s %8s %8s %8s %8s\n", 1647 "", "total", "used", "util", "num", "average", "maximum", 1648 "hash table", "buckets", "buckets", "%", "items", "chain", 1649 "chain"); 1650 1651 for (curhash = khashes; curhash->description; curhash++) { 1652 if (hashnl[curhash->hashsize].n_value == 0 || 1653 hashnl[curhash->hashtbl].n_value == 0) 1654 continue; 1655 if (hashname != NULL && 1656 strcmp(hashnl[curhash->hashsize].n_name + 1, hashname)) 1657 continue; 1658 elemsize = curhash->type == HASH_LIST ? 1659 sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq); 1660 deref_kptr((void *)hashnl[curhash->hashsize].n_value, 1661 &hashsize, sizeof(hashsize), 1662 hashnl[curhash->hashsize].n_name); 1663 hashsize++; 1664 deref_kptr((void *)hashnl[curhash->hashtbl].n_value, 1665 &hashaddr, sizeof(hashaddr), 1666 hashnl[curhash->hashtbl].n_name); 1667 if (verbose) 1668 (void)printf( 1669 "%s %lu, %s %p, offset %ld, elemsize %llu\n", 1670 hashnl[curhash->hashsize].n_name + 1, hashsize, 1671 hashnl[curhash->hashtbl].n_name + 1, hashaddr, 1672 (long)curhash->offset, 1673 (unsigned long long)elemsize); 1674 thissize = hashsize * elemsize; 1675 if (hashbuf == NULL || thissize > hashbufsize) { 1676 if ((nhashbuf = realloc(hashbuf, thissize)) == NULL) 1677 errx(1, "malloc hashbuf %llu", 1678 (unsigned long long)hashbufsize); 1679 hashbuf = nhashbuf; 1680 hashbufsize = thissize; 1681 } 1682 deref_kptr(hashaddr, hashbuf, thissize, 1683 hashnl[curhash->hashtbl].n_name); 1684 used = 0; 1685 items = maxchain = 0; 1686 if (curhash->type == HASH_LIST) { 1687 hashtbl_list = hashbuf; 1688 hashtbl_tailq = NULL; 1689 } else { 1690 hashtbl_list = NULL; 1691 hashtbl_tailq = hashbuf; 1692 } 1693 for (i = 0; i < hashsize; i++) { 1694 if (curhash->type == HASH_LIST) 1695 nextaddr = LIST_FIRST(&hashtbl_list[i]); 1696 else 1697 nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]); 1698 if (nextaddr == NULL) 1699 continue; 1700 if (verbose) 1701 (void)printf("%5lu: %p\n", i, nextaddr); 1702 used++; 1703 chain = 0; 1704 do { 1705 chain++; 1706 deref_kptr((char *)nextaddr + curhash->offset, 1707 &nextaddr, sizeof(void *), 1708 "hash chain corrupted"); 1709 if (verbose > 1) 1710 (void)printf("got nextaddr as %p\n", 1711 nextaddr); 1712 } while (nextaddr != NULL); 1713 items += chain; 1714 if (verbose && chain > 1) 1715 (void)printf("\tchain = %d\n", chain); 1716 if (chain > maxchain) 1717 maxchain = chain; 1718 } 1719 (void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n", 1720 hashnl[curhash->hashsize].n_name + 1, 1721 hashsize, used, used * 100.0 / hashsize, 1722 items, used ? (double)items / used : 0.0, maxchain); 1723 } 1724} 1725 1726/* 1727 * kreadc like kread but returns 1 if sucessful, 0 otherwise 1728 */ 1729int 1730kreadc(struct nlist *nl, int nlx, void *addr, size_t size) 1731{ 1732 const char *sym; 1733 1734 sym = nl[nlx].n_name; 1735 if (*sym == '_') 1736 ++sym; 1737 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) 1738 return 0; 1739 deref_kptr((void *)nl[nlx].n_value, addr, size, sym); 1740 return 1; 1741} 1742 1743/* 1744 * kread reads something from the kernel, given its nlist index in namelist[]. 1745 */ 1746void 1747kread(struct nlist *nl, int nlx, void *addr, size_t size) 1748{ 1749 const char *sym; 1750 1751 sym = nl[nlx].n_name; 1752 if (*sym == '_') 1753 ++sym; 1754 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) 1755 errx(1, "symbol %s not defined", sym); 1756 deref_kptr((void *)nl[nlx].n_value, addr, size, sym); 1757} 1758 1759/* 1760 * Dereference the kernel pointer `kptr' and fill in the local copy 1761 * pointed to by `ptr'. The storage space must be pre-allocated, 1762 * and the size of the copy passed in `len'. 1763 */ 1764void 1765deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg) 1766{ 1767 1768 if (*msg == '_') 1769 msg++; 1770 if ((size_t)kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len) 1771 errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd)); 1772} 1773 1774/* 1775 * Traverse the kernel history buffers, performing the requested action. 1776 * 1777 * Note, we assume that if we're not listing, we're dumping. 1778 */ 1779void 1780hist_traverse(int todo, const char *histname) 1781{ 1782 struct kern_history_head histhead; 1783 struct kern_history hist, *histkva; 1784 char *name = NULL; 1785 size_t namelen = 0; 1786 1787 if (histnl[0].n_value == 0) { 1788 warnx("kernel history is not compiled into the kernel."); 1789 return; 1790 } 1791 1792 deref_kptr((void *)histnl[X_KERN_HISTORIES].n_value, &histhead, 1793 sizeof(histhead), histnl[X_KERN_HISTORIES].n_name); 1794 1795 if (histhead.lh_first == NULL) { 1796 warnx("No active kernel history logs."); 1797 return; 1798 } 1799 1800 if (todo & HISTLIST) 1801 (void)printf("Active kernel histories:"); 1802 1803 for (histkva = LIST_FIRST(&histhead); histkva != NULL; 1804 histkva = LIST_NEXT(&hist, list)) { 1805 deref_kptr(histkva, &hist, sizeof(hist), "histkva"); 1806 if (name == NULL || hist.namelen > namelen) { 1807 if (name != NULL) 1808 free(name); 1809 namelen = hist.namelen; 1810 if ((name = malloc(namelen + 1)) == NULL) 1811 err(1, "malloc history name"); 1812 } 1813 1814 deref_kptr(hist.name, name, namelen, "history name"); 1815 name[namelen] = '\0'; 1816 if (todo & HISTLIST) 1817 (void)printf(" %s", name); 1818 else { 1819 /* 1820 * If we're dumping all histories, do it, else 1821 * check to see if this is the one we want. 1822 */ 1823 if (histname == NULL || strcmp(histname, name) == 0) { 1824 if (histname == NULL) 1825 (void)printf( 1826 "\nkernel history `%s':\n", name); 1827 hist_dodump(&hist); 1828 } 1829 } 1830 } 1831 1832 if (todo & HISTLIST) 1833 (void)putchar('\n'); 1834 1835 if (name != NULL) 1836 free(name); 1837} 1838 1839/* 1840 * Actually dump the history buffer at the specified KVA. 1841 */ 1842void 1843hist_dodump(struct kern_history *histp) 1844{ 1845 struct kern_history_ent *histents, *e; 1846 size_t histsize; 1847 char *fmt = NULL, *fn = NULL; 1848 size_t fmtlen = 0, fnlen = 0; 1849 unsigned i; 1850 1851 histsize = sizeof(struct kern_history_ent) * histp->n; 1852 1853 if ((histents = malloc(histsize)) == NULL) 1854 err(1, "malloc history entries"); 1855 1856 (void)memset(histents, 0, histsize); 1857 1858 deref_kptr(histp->e, histents, histsize, "history entries"); 1859 i = histp->f; 1860 do { 1861 e = &histents[i]; 1862 if (e->fmt != NULL) { 1863 if (fmt == NULL || e->fmtlen > fmtlen) { 1864 if (fmt != NULL) 1865 free(fmt); 1866 fmtlen = e->fmtlen; 1867 if ((fmt = malloc(fmtlen + 1)) == NULL) 1868 err(1, "malloc printf format"); 1869 } 1870 if (fn == NULL || e->fnlen > fnlen) { 1871 if (fn != NULL) 1872 free(fn); 1873 fnlen = e->fnlen; 1874 if ((fn = malloc(fnlen + 1)) == NULL) 1875 err(1, "malloc function name"); 1876 } 1877 1878 deref_kptr(e->fmt, fmt, fmtlen, "printf format"); 1879 fmt[fmtlen] = '\0'; 1880 1881 deref_kptr(e->fn, fn, fnlen, "function name"); 1882 fn[fnlen] = '\0'; 1883 1884 (void)printf("%06ld.%06ld ", (long int)e->tv.tv_sec, 1885 (long int)e->tv.tv_usec); 1886 (void)printf("%s#%ld: ", fn, e->call); 1887 (void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]); 1888 (void)putchar('\n'); 1889 } 1890 i = (i + 1) % histp->n; 1891 } while (i != histp->f); 1892 1893 free(histents); 1894 if (fmt != NULL) 1895 free(fmt); 1896 if (fn != NULL) 1897 free(fn); 1898} 1899 1900static void 1901usage(void) 1902{ 1903 1904 (void)fprintf(stderr, 1905 "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n" 1906 "\t\t[-u histname] [-w wait] [disks]\n", getprogname()); 1907 exit(1); 1908} 1909