1 /* 2 * Copyright (c) 1980, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1980, 1986, 1991, 1993 The Regents of the University of California. All rights reserved. 30 * @(#)vmstat.c 8.1 (Berkeley) 6/6/93 31 * $FreeBSD: src/usr.bin/vmstat/vmstat.c,v 1.38.2.4 2001/07/31 19:52:41 tmm Exp $ 32 */ 33 34 #include <sys/user.h> 35 #include <sys/param.h> 36 #include <sys/time.h> 37 #include <sys/uio.h> 38 #include <sys/namei.h> 39 #include <sys/malloc.h> 40 #include <sys/signal.h> 41 #include <sys/fcntl.h> 42 #include <sys/ioctl.h> 43 #include <sys/sysctl.h> 44 #include <sys/vmmeter.h> 45 #include <sys/interrupt.h> 46 47 #include <vm/vm_param.h> 48 #include <vm/vm_zone.h> 49 50 #include <ctype.h> 51 #include <err.h> 52 #include <errno.h> 53 #include <kinfo.h> 54 #include <kvm.h> 55 #include <limits.h> 56 #include <nlist.h> 57 #include <paths.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <sysexits.h> 62 #include <time.h> 63 #include <unistd.h> 64 #include <devstat.h> 65 66 static struct nlist namelist[] = { 67 #define X_BOOTTIME 0 68 { "_boottime", 0, 0, 0, 0 }, 69 #define X_NCHSTATS 1 70 { "_nchstats", 0, 0, 0, 0 }, 71 #define X_KMEMSTATISTICS 2 72 { "_kmemstatistics", 0, 0, 0, 0 }, 73 #define X_ZLIST 3 74 { "_zlist", 0, 0, 0, 0 }, 75 #ifdef notyet 76 #define X_DEFICIT 4 77 { "_deficit", 0, 0, 0, 0 }, 78 #define X_FORKSTAT 5 79 { "_forkstat", 0, 0, 0, 0 }, 80 #define X_REC 6 81 { "_rectime", 0, 0, 0, 0 }, 82 #define X_PGIN 7 83 { "_pgintime", 0, 0, 0, 0 }, 84 #define X_XSTATS 8 85 { "_xstats", 0, 0, 0, 0 }, 86 #define X_END 9 87 #else 88 #define X_END 4 89 #endif 90 { "", 0, 0, 0, 0 }, 91 }; 92 93 #define ONEMB (1024L * 1024L) 94 #define ONEKB (1024L) 95 96 LIST_HEAD(zlist, vm_zone); 97 98 struct statinfo cur, last; 99 int num_devices, maxshowdevs; 100 long generation; 101 struct device_selection *dev_select; 102 int num_selected; 103 struct devstat_match *matches; 104 int num_matches = 0; 105 int num_devices_specified, num_selections; 106 long select_generation; 107 char **specified_devices; 108 devstat_select_mode select_mode; 109 110 struct vmmeter vmm, ovmm; 111 struct vmstats vms, ovms; 112 113 int winlines = 20; 114 int nflag = 0; 115 int verbose = 0; 116 int unformatted_opt = 0; 117 int brief_opt = 0; 118 119 kvm_t *kd; 120 121 struct kinfo_cputime cp_time, old_cp_time, diff_cp_time; 122 123 #define FORKSTAT 0x01 124 #define INTRSTAT 0x02 125 #define MEMSTAT 0x04 126 #define SUMSTAT 0x08 127 #define TIMESTAT 0x10 128 #define VMSTAT 0x20 129 #define ZMEMSTAT 0x40 130 131 static void cpustats(void); 132 static void dointr(void); 133 static void domem(void); 134 static void dosum(void); 135 static void dozmem(u_int interval, int reps); 136 static void dovmstat(u_int, int); 137 static void kread(int, void *, size_t); 138 static void usage(void); 139 static char **getdrivedata(char **); 140 static long getuptime(void); 141 static void needhdr(int); 142 static long pct(long, long); 143 144 #ifdef notyet 145 static void dotimes(void); /* Not implemented */ 146 static void doforkst(void); 147 #endif 148 static void printhdr(void); 149 static const char *formatnum(intmax_t value, int width, int do10s); 150 static void devstats(int dooutput); 151 152 int 153 main(int argc, char **argv) 154 { 155 int c, todo; 156 u_int interval; /* milliseconds */ 157 int reps; 158 char *memf, *nlistf; 159 char errbuf[_POSIX2_LINE_MAX]; 160 161 memf = nlistf = NULL; 162 interval = reps = todo = 0; 163 maxshowdevs = 2; 164 while ((c = getopt(argc, argv, "bc:fiM:mN:n:p:stuvw:z")) != -1) { 165 switch (c) { 166 case 'b': 167 brief_opt = 1; 168 break; 169 case 'c': 170 reps = atoi(optarg); 171 break; 172 case 'f': 173 #ifdef notyet 174 todo |= FORKSTAT; 175 #else 176 errx(EX_USAGE, "sorry, -f is not (re)implemented yet"); 177 #endif 178 break; 179 case 'i': 180 todo |= INTRSTAT; 181 break; 182 case 'M': 183 memf = optarg; 184 break; 185 case 'm': 186 todo |= MEMSTAT; 187 break; 188 case 'N': 189 nlistf = optarg; 190 break; 191 case 'n': 192 nflag = 1; 193 maxshowdevs = atoi(optarg); 194 if (maxshowdevs < 0) 195 errx(1, "number of devices %d is < 0", 196 maxshowdevs); 197 break; 198 case 'p': 199 if (buildmatch(optarg, &matches, &num_matches) != 0) 200 errx(1, "%s", devstat_errbuf); 201 break; 202 case 's': 203 todo |= SUMSTAT; 204 break; 205 case 't': 206 #ifdef notyet 207 todo |= TIMESTAT; 208 #else 209 errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); 210 #endif 211 break; 212 case 'u': 213 unformatted_opt = 1; 214 break; 215 case 'v': 216 ++verbose; 217 break; 218 case 'w': 219 interval = (u_int)(strtod(optarg, NULL) * 1000.0); 220 break; 221 case 'z': 222 todo |= ZMEMSTAT; 223 break; 224 default: 225 usage(); 226 } 227 } 228 argc -= optind; 229 argv += optind; 230 231 if (todo == 0) 232 todo = VMSTAT; 233 234 /* 235 * Discard setgid privileges if not the running kernel so that bad 236 * guys can't print interesting stuff from kernel memory. 237 */ 238 if (nlistf != NULL || memf != NULL) 239 setgid(getgid()); 240 241 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 242 if (kd == NULL) 243 errx(1, "kvm_openfiles: %s", errbuf); 244 245 if ((c = kvm_nlist(kd, namelist)) != 0) { 246 if (c > 0) { 247 warnx("undefined symbols:"); 248 for (c = 0; c < (int)__arysize(namelist); c++) 249 if (namelist[c].n_type == 0) 250 fprintf(stderr, " %s", 251 namelist[c].n_name); 252 fputc('\n', stderr); 253 } else 254 warnx("kvm_nlist: %s", kvm_geterr(kd)); 255 exit(1); 256 } 257 258 if (todo & VMSTAT) { 259 struct winsize winsize; 260 261 /* 262 * Make sure that the userland devstat version matches the 263 * kernel devstat version. If not, exit and print a 264 * message informing the user of his mistake. 265 */ 266 if (checkversion() < 0) 267 errx(1, "%s", devstat_errbuf); 268 269 270 argv = getdrivedata(argv); 271 winsize.ws_row = 0; 272 ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 273 if (winsize.ws_row > 0) 274 winlines = winsize.ws_row; 275 276 } 277 278 #define BACKWARD_COMPATIBILITY 279 #ifdef BACKWARD_COMPATIBILITY 280 if (*argv) { 281 interval = (u_int)(strtod(*argv, NULL) * 1000.0); 282 if (*++argv) 283 reps = atoi(*argv); 284 } 285 #endif 286 287 if (interval) { 288 if (!reps) 289 reps = -1; 290 } else if (reps) { 291 interval = 1000; 292 } 293 294 #ifdef notyet 295 if (todo & FORKSTAT) 296 doforkst(); 297 #endif 298 if (todo & MEMSTAT) 299 domem(); 300 if (todo & ZMEMSTAT) 301 dozmem(interval, reps); 302 if (todo & SUMSTAT) 303 dosum(); 304 #ifdef notyet 305 if (todo & TIMESTAT) 306 dotimes(); 307 #endif 308 if (todo & INTRSTAT) 309 dointr(); 310 if (todo & VMSTAT) 311 dovmstat(interval, reps); 312 exit(0); 313 } 314 315 static char ** 316 getdrivedata(char **argv) 317 { 318 if ((num_devices = getnumdevs()) < 0) 319 errx(1, "%s", devstat_errbuf); 320 321 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 322 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 323 bzero(cur.dinfo, sizeof(struct devinfo)); 324 bzero(last.dinfo, sizeof(struct devinfo)); 325 326 if (getdevs(&cur) == -1) 327 errx(1, "%s", devstat_errbuf); 328 329 num_devices = cur.dinfo->numdevs; 330 generation = cur.dinfo->generation; 331 332 specified_devices = (char **)malloc(sizeof(char *)); 333 for (num_devices_specified = 0; *argv; ++argv) { 334 if (isdigit(**argv)) 335 break; 336 num_devices_specified++; 337 specified_devices = (char **)realloc(specified_devices, 338 sizeof(char *) * 339 num_devices_specified); 340 specified_devices[num_devices_specified - 1] = *argv; 341 } 342 dev_select = NULL; 343 344 if (nflag == 0 && maxshowdevs < num_devices_specified) 345 maxshowdevs = num_devices_specified; 346 347 /* 348 * People are generally only interested in disk statistics when 349 * they're running vmstat. So, that's what we're going to give 350 * them if they don't specify anything by default. We'll also give 351 * them any other random devices in the system so that we get to 352 * maxshowdevs devices, if that many devices exist. If the user 353 * specifies devices on the command line, either through a pattern 354 * match or by naming them explicitly, we will give the user only 355 * those devices. 356 */ 357 if ((num_devices_specified == 0) && (num_matches == 0)) { 358 if (buildmatch("da", &matches, &num_matches) != 0) 359 errx(1, "%s", devstat_errbuf); 360 361 select_mode = DS_SELECT_ADD; 362 } else 363 select_mode = DS_SELECT_ONLY; 364 365 /* 366 * At this point, selectdevs will almost surely indicate that the 367 * device list has changed, so we don't look for return values of 0 368 * or 1. If we get back -1, though, there is an error. 369 */ 370 if (selectdevs(&dev_select, &num_selected, &num_selections, 371 &select_generation, generation, cur.dinfo->devices, 372 num_devices, matches, num_matches, specified_devices, 373 num_devices_specified, select_mode, 374 maxshowdevs, 0) == -1) 375 errx(1, "%s", devstat_errbuf); 376 377 return(argv); 378 } 379 380 static long 381 getuptime(void) 382 { 383 static time_t now, boottime; 384 time_t uptime; 385 386 if (boottime == 0) 387 kread(X_BOOTTIME, &boottime, sizeof(boottime)); 388 time(&now); 389 uptime = now - boottime; 390 if (uptime <= 0 || uptime > 60*60*24*365*10) 391 errx(1, "time makes no sense; namelist must be wrong"); 392 return(uptime); 393 } 394 395 int hdrcnt; 396 397 static void 398 dovmstat(u_int interval, int reps) 399 { 400 struct vmtotal total; 401 struct devinfo *tmp_dinfo; 402 size_t vmm_size = sizeof(vmm); 403 size_t vms_size = sizeof(vms); 404 size_t vmt_size = sizeof(total); 405 int initial = 1; 406 int dooutput = 1; 407 408 signal(SIGCONT, needhdr); 409 if (reps != 0) 410 dooutput = 0; 411 412 for (hdrcnt = 1;;) { 413 if (!--hdrcnt) 414 printhdr(); 415 if (kinfo_get_sched_cputime(&cp_time)) 416 err(1, "kinfo_get_sched_cputime"); 417 418 tmp_dinfo = last.dinfo; 419 last.dinfo = cur.dinfo; 420 cur.dinfo = tmp_dinfo; 421 last.busy_time = cur.busy_time; 422 423 /* 424 * Here what we want to do is refresh our device stats. 425 * getdevs() returns 1 when the device list has changed. 426 * If the device list has changed, we want to go through 427 * the selection process again, in case a device that we 428 * were previously displaying has gone away. 429 */ 430 switch (getdevs(&cur)) { 431 case -1: 432 errx(1, "%s", devstat_errbuf); 433 break; 434 case 1: { 435 int retval; 436 437 num_devices = cur.dinfo->numdevs; 438 generation = cur.dinfo->generation; 439 440 retval = selectdevs(&dev_select, &num_selected, 441 &num_selections, &select_generation, 442 generation, cur.dinfo->devices, 443 num_devices, matches, num_matches, 444 specified_devices, 445 num_devices_specified, select_mode, 446 maxshowdevs, 0); 447 switch (retval) { 448 case -1: 449 errx(1, "%s", devstat_errbuf); 450 break; 451 case 1: 452 printhdr(); 453 break; 454 default: 455 break; 456 } 457 } 458 default: 459 break; 460 } 461 462 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) { 463 perror("sysctlbyname: vm.vmstats"); 464 exit(1); 465 } 466 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) { 467 perror("sysctlbyname: vm.vmmeter"); 468 exit(1); 469 } 470 if (sysctlbyname("vm.vmtotal", &total, &vmt_size, NULL, 0)) { 471 perror("sysctlbyname: vm.vmtotal"); 472 exit(1); 473 } 474 if (dooutput) { 475 printf("%3ld %2ld %2ld", 476 total.t_rq - 1, 477 total.t_dw + total.t_pw, 478 total.t_sw); 479 } 480 481 #define rate(x) \ 482 (intmax_t)(initial ? (x) : ((intmax_t)(x) * 1000 + interval / 2) \ 483 / interval) 484 485 if (dooutput) { 486 printf(" %s ", 487 formatnum((int64_t)total.t_free * 488 vms.v_page_size, 489 5, 1)); 490 printf("%s ", 491 formatnum(rate(vmm.v_vm_faults - 492 ovmm.v_vm_faults), 493 5, 1)); 494 printf("%s ", 495 formatnum(rate(vmm.v_reactivated - 496 ovmm.v_reactivated), 497 4, 0)); 498 printf("%s ", 499 formatnum(rate(vmm.v_swapin + vmm.v_vnodein - 500 (ovmm.v_swapin + 501 ovmm.v_vnodein)), 502 4, 0)); 503 printf("%s ", 504 formatnum(rate(vmm.v_swapout + vmm.v_vnodeout - 505 (ovmm.v_swapout + 506 ovmm.v_vnodeout)), 507 4, 0)); 508 printf("%s ", 509 formatnum(rate(vmm.v_tfree - ovmm.v_tfree), 510 4, 0)); 511 } 512 devstats(dooutput); 513 if (dooutput) { 514 printf("%s ", 515 formatnum(rate(vmm.v_intr - ovmm.v_intr), 516 5, 1)); 517 printf("%s ", 518 formatnum(rate(vmm.v_syscall - 519 ovmm.v_syscall), 520 5, 1)); 521 printf("%s ", 522 formatnum(rate(vmm.v_swtch - 523 ovmm.v_swtch), 524 5, 1)); 525 cpustats(); 526 printf("\n"); 527 fflush(stdout); 528 } 529 if (reps >= 0 && --reps <= 0) 530 break; 531 ovmm = vmm; 532 usleep(interval * 1000); 533 initial = 0; 534 dooutput = 1; 535 } 536 } 537 538 static const char * 539 formatnum(intmax_t value, int width, int do10s) 540 { 541 static char buf[16][64]; 542 static int bi; 543 const char *fmt; 544 double d; 545 546 if (brief_opt) 547 do10s = 0; 548 549 bi = (bi + 1) % 16; 550 551 if (unformatted_opt) { 552 switch(width) { 553 case 4: 554 snprintf(buf[bi], sizeof(buf[bi]), "%4jd", value); 555 break; 556 case 5: 557 snprintf(buf[bi], sizeof(buf[bi]), "%5jd", value); 558 break; 559 default: 560 snprintf(buf[bi], sizeof(buf[bi]), "%jd", value); 561 break; 562 } 563 return buf[bi]; 564 } 565 566 d = (double)value; 567 fmt = "n/a"; 568 569 switch(width) { 570 case 4: 571 if (value < 1024) { 572 fmt = "%4.0f"; 573 } else if (value < 10*1024) { 574 fmt = "%3.1fK"; 575 d = d / 1024; 576 } else if (value < 1000*1024) { 577 fmt = "%3.0fK"; 578 d = d / 1024; 579 } else if (value < 10*1024*1024) { 580 fmt = "%3.1fM"; 581 d = d / (1024 * 1024); 582 } else if (value < 1000*1024*1024) { 583 fmt = "%3.0fM"; 584 d = d / (1024 * 1024); 585 } else { 586 fmt = "%3.1fG"; 587 d = d / (1024.0 * 1024.0 * 1024.0); 588 } 589 break; 590 case 5: 591 if (value < 1024) { 592 fmt = "%5.0f"; 593 } else if (value < 10*1024) { 594 fmt = "%4.2fK"; 595 d = d / 1024; 596 } else if (value < 100*1024 && do10s) { 597 fmt = "%4.1fK"; 598 d = d / 1024; 599 } else if (value < 1000*1024) { 600 fmt = "%4.0fK"; 601 d = d / 1024; 602 } else if (value < 10*1024*1024) { 603 fmt = "%4.2fM"; 604 d = d / (1024 * 1024); 605 } else if (value < 100*1024*1024 && do10s) { 606 fmt = "%4.1fM"; 607 d = d / (1024 * 1024); 608 } else if (value < 1000*1024*1024) { 609 fmt = "%4.0fM"; 610 d = d / (1024 * 1024); 611 } else if (value < 10LL*1024*1024*1024) { 612 fmt = "%4.2fG"; 613 d = d / (1024.0 * 1024.0 * 1024.0); 614 } else if (value < 100LL*1024*1024*1024 && do10s) { 615 fmt = "%4.1fG"; 616 d = d / (1024.0 * 1024.0 * 1024.0); 617 } else if (value < 1000LL*1024*1024*1024) { 618 fmt = "%4.0fG"; 619 d = d / (1024.0 * 1024.0 * 1024.0); 620 } else { 621 fmt = "%4.2fT"; 622 d = d / (1024.0 * 1024.0 * 1024.0 * 1024.0); 623 } 624 break; 625 default: 626 fprintf(stderr, "formatnum: unsupported width %d\n", width); 627 exit(1); 628 break; 629 } 630 snprintf(buf[bi], sizeof(buf[bi]), fmt, d); 631 return buf[bi]; 632 } 633 634 static void 635 printhdr(void) 636 { 637 int i, num_shown; 638 639 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs; 640 printf("--procs-- ---memory-- -------paging------ "); 641 if (num_shown > 1) 642 printf("--disks%.*s", 643 num_shown * 4 - 6, 644 "---------------------------------"); 645 else if (num_shown == 1) 646 printf("disk"); 647 printf(" -----faults------ ---cpu---\n"); 648 printf(" r b w fre flt re pi po fr "); 649 for (i = 0; i < num_devices; i++) 650 if ((dev_select[i].selected) 651 && (dev_select[i].selected <= maxshowdevs)) 652 printf(" %c%c%d ", dev_select[i].device_name[0], 653 dev_select[i].device_name[1], 654 dev_select[i].unit_number); 655 printf(" int sys ctx us sy id\n"); 656 hdrcnt = winlines - 2; 657 } 658 659 /* 660 * Force a header to be prepended to the next output. 661 */ 662 static void 663 needhdr(__unused int signo) 664 { 665 666 hdrcnt = 1; 667 } 668 669 static long 670 pct(long top, long bot) 671 { 672 long ans; 673 674 if (bot == 0) 675 return(0); 676 ans = (quad_t)top * 100 / bot; 677 return (ans); 678 } 679 680 #define PCT(top, bot) pct((long)(top), (long)(bot)) 681 682 static void 683 dosum(void) 684 { 685 struct nchstats *nch_tmp, nchstats; 686 size_t vms_size = sizeof(vms); 687 size_t vmm_size = sizeof(vmm); 688 int cpucnt; 689 u_long nchtotal; 690 u_long nchpathtotal; 691 size_t nch_size = sizeof(struct nchstats) * SMP_MAXCPU; 692 693 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) { 694 perror("sysctlbyname: vm.vmstats"); 695 exit(1); 696 } 697 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) { 698 perror("sysctlbyname: vm.vmstats"); 699 exit(1); 700 } 701 printf("%9u cpu context switches\n", vmm.v_swtch); 702 printf("%9u device interrupts\n", vmm.v_intr); 703 printf("%9u software interrupts\n", vmm.v_soft); 704 printf("%9u traps\n", vmm.v_trap); 705 printf("%9u system calls\n", vmm.v_syscall); 706 printf("%9u kernel threads created\n", vmm.v_kthreads); 707 printf("%9u fork() calls\n", vmm.v_forks); 708 printf("%9u vfork() calls\n", vmm.v_vforks); 709 printf("%9u rfork() calls\n", vmm.v_rforks); 710 printf("%9u exec() calls\n", vmm.v_exec); 711 printf("%9u swap pager pageins\n", vmm.v_swapin); 712 printf("%9u swap pager pages paged in\n", vmm.v_swappgsin); 713 printf("%9u swap pager pageouts\n", vmm.v_swapout); 714 printf("%9u swap pager pages paged out\n", vmm.v_swappgsout); 715 printf("%9u vnode pager pageins\n", vmm.v_vnodein); 716 printf("%9u vnode pager pages paged in\n", vmm.v_vnodepgsin); 717 printf("%9u vnode pager pageouts\n", vmm.v_vnodeout); 718 printf("%9u vnode pager pages paged out\n", vmm.v_vnodepgsout); 719 printf("%9u page daemon wakeups\n", vmm.v_pdwakeups); 720 printf("%9u pages examined by the page daemon\n", vmm.v_pdpages); 721 printf("%9u pages reactivated\n", vmm.v_reactivated); 722 printf("%9u copy-on-write faults\n", vmm.v_cow_faults); 723 printf("%9u copy-on-write optimized faults\n", vmm.v_cow_optim); 724 printf("%9u zero fill pages zeroed\n", vmm.v_zfod); 725 printf("%9u zero fill pages prezeroed\n", vmm.v_ozfod); 726 printf("%9u intransit blocking page faults\n", vmm.v_intrans); 727 printf("%9u total VM faults taken\n", vmm.v_vm_faults); 728 printf("%9u pages affected by kernel thread creation\n", vmm.v_kthreadpages); 729 printf("%9u pages affected by fork()\n", vmm.v_forkpages); 730 printf("%9u pages affected by vfork()\n", vmm.v_vforkpages); 731 printf("%9u pages affected by rfork()\n", vmm.v_rforkpages); 732 printf("%9u pages freed\n", vmm.v_tfree); 733 printf("%9u pages freed by daemon\n", vmm.v_dfree); 734 printf("%9u pages freed by exiting processes\n", vmm.v_pfree); 735 printf("%9lu pages active\n", vms.v_active_count); 736 printf("%9lu pages inactive\n", vms.v_inactive_count); 737 printf("%9lu pages in VM cache\n", vms.v_cache_count); 738 printf("%9lu pages wired down\n", vms.v_wire_count); 739 printf("%9lu pages free\n", vms.v_free_count); 740 printf("%9u bytes per page\n", vms.v_page_size); 741 printf("%9u global smp invltlbs\n", vmm.v_smpinvltlb); 742 743 if ((nch_tmp = malloc(nch_size)) == NULL) { 744 perror("malloc"); 745 exit(1); 746 } else { 747 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) { 748 perror("sysctlbyname vfs.cache.nchstats"); 749 free(nch_tmp); 750 exit(1); 751 } else { 752 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) { 753 perror("realloc"); 754 exit(1); 755 } 756 } 757 } 758 759 cpucnt = nch_size / sizeof(struct nchstats); 760 kvm_nch_cpuagg(nch_tmp, &nchstats, cpucnt); 761 762 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 763 nchstats.ncs_badhits + nchstats.ncs_falsehits + 764 nchstats.ncs_miss; 765 nchpathtotal = nchstats.ncs_longhits + nchstats.ncs_longmiss; 766 printf("%9ld total path lookups\n", nchpathtotal); 767 printf("%9ld total component lookups\n", nchtotal); 768 printf( 769 "%9s cache hits (%ld%% pos + %ld%% neg)\n", 770 "", PCT(nchstats.ncs_goodhits, nchtotal), 771 PCT(nchstats.ncs_neghits, nchtotal)); 772 printf("%9s deletions %ld%%, falsehits %ld%%\n", "", 773 PCT(nchstats.ncs_badhits, nchtotal), 774 PCT(nchstats.ncs_falsehits, nchtotal)); 775 free(nch_tmp); 776 } 777 778 #ifdef notyet 779 void 780 doforkst(void) 781 { 782 struct forkstat fks; 783 784 kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); 785 printf("%d forks, %d pages, average %.2f\n", 786 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); 787 printf("%d vforks, %d pages, average %.2f\n", 788 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); 789 } 790 #endif 791 792 static void 793 devstats(int dooutput) 794 { 795 int dn; 796 long double transfers_per_second; 797 long double busy_seconds; 798 799 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user; 800 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice; 801 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys; 802 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr; 803 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle; 804 old_cp_time = cp_time; 805 806 busy_seconds = compute_etime(cur.busy_time, last.busy_time); 807 808 for (dn = 0; dn < num_devices; dn++) { 809 int di; 810 811 if ((dev_select[dn].selected == 0) 812 || (dev_select[dn].selected > maxshowdevs)) 813 continue; 814 815 di = dev_select[dn].position; 816 817 if (compute_stats(&cur.dinfo->devices[di], 818 &last.dinfo->devices[di], busy_seconds, 819 NULL, NULL, NULL, 820 NULL, &transfers_per_second, NULL, 821 NULL, NULL) != 0) 822 errx(1, "%s", devstat_errbuf); 823 824 if (dooutput) 825 printf("%s ", formatnum(transfers_per_second, 4, 0)); 826 } 827 } 828 829 static void 830 cpustats(void) 831 { 832 uint64_t total; 833 double totusage; 834 835 total = diff_cp_time.cp_user + diff_cp_time.cp_nice + 836 diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle; 837 838 if (total) 839 totusage = 100.0 / total; 840 else 841 totusage = 0; 842 printf("%2.0f ", 843 (diff_cp_time.cp_user + diff_cp_time.cp_nice) * totusage); 844 printf("%2.0f ", 845 (diff_cp_time.cp_sys + diff_cp_time.cp_intr) * totusage); 846 printf("%2.0f", 847 diff_cp_time.cp_idle * totusage); 848 } 849 850 static void 851 dointr(void) 852 { 853 u_long *intrcnt, uptime; 854 u_int64_t inttotal; 855 size_t nintr, inamlen, i, size; 856 int nwidth; 857 char *intrstr; 858 char **intrname; 859 860 uptime = getuptime(); 861 if (sysctlbyname("hw.intrnames", NULL, &inamlen, NULL, 0) != 0) 862 errx(1, "sysctlbyname"); 863 intrstr = malloc(inamlen); 864 if (intrstr == NULL) 865 err(1, "malloc"); 866 sysctlbyname("hw.intrnames", intrstr, &inamlen, NULL, 0); 867 for (nintr = 0, i = 0; i < inamlen; ++i) { 868 if (intrstr[i] == 0) 869 nintr++; 870 } 871 intrname = malloc(nintr * sizeof(char *)); 872 for (i = 0; i < nintr; ++i) { 873 intrname[i] = intrstr; 874 intrstr += strlen(intrstr) + 1; 875 } 876 877 size = nintr * sizeof(*intrcnt); 878 intrcnt = calloc(nintr, sizeof(*intrcnt)); 879 if (intrcnt == NULL) 880 err(1, "malloc"); 881 sysctlbyname("hw.intrcnt", intrcnt, &size, NULL, 0); 882 883 nwidth = 21; 884 for (i = 0; i < nintr; ++i) { 885 if (nwidth < (int)strlen(intrname[i])) 886 nwidth = (int)strlen(intrname[i]); 887 } 888 if (verbose) nwidth += 12; 889 890 printf("%-*.*s %11s %10s\n", 891 nwidth, nwidth, "interrupt", "total", "rate"); 892 inttotal = 0; 893 for (i = 0; i < nintr; ++i) { 894 int named; 895 char *infop, irqinfo[72]; 896 897 if ((named = strncmp(intrname[i], "irq", 3)) != 0 || 898 intrcnt[i] > 0) { 899 infop = intrname[i]; 900 if (verbose) { 901 ssize_t irq, cpu; 902 903 irq = i % MAX_INTS; 904 cpu = i / MAX_INTS; 905 if (named) { 906 snprintf(irqinfo, sizeof(irqinfo), 907 "irq%-3zd %3zd: %s", 908 irq, cpu, intrname[i]); 909 } else { 910 snprintf(irqinfo, sizeof(irqinfo), 911 "irq%-3zd %3zd: ", irq, cpu); 912 } 913 infop = irqinfo; 914 } 915 printf("%-*.*s %11lu %10lu\n", 916 nwidth, nwidth, infop, 917 intrcnt[i], intrcnt[i] / uptime); 918 } 919 inttotal += intrcnt[i]; 920 } 921 printf("%-*.*s %11llu %10llu\n", 922 nwidth, nwidth, "Total", 923 (long long)inttotal, (long long)(inttotal / uptime)); 924 } 925 926 #define MAX_KMSTATS 1024 927 928 enum ksuse { KSINUSE, KSMEMUSE, KSCALLS }; 929 930 static long 931 cpuagg(struct malloc_type *ks, enum ksuse use) 932 { 933 int i; 934 long ttl; 935 936 ttl = 0; 937 938 switch(use) { 939 case KSINUSE: 940 for (i = 0; i < SMP_MAXCPU; ++i) 941 ttl += ks->ks_use[i].inuse; 942 break; 943 case KSMEMUSE: 944 for (i = 0; i < SMP_MAXCPU; ++i) 945 ttl += ks->ks_use[i].memuse; 946 break; 947 case KSCALLS: 948 for (i = 0; i < SMP_MAXCPU; ++i) 949 ttl += ks->ks_use[i].calls; 950 break; 951 } 952 return(ttl); 953 } 954 955 static void 956 domem(void) 957 { 958 struct malloc_type *ks; 959 int i; 960 int nkms; 961 long totuse = 0, totreq = 0; 962 struct malloc_type kmemstats[MAX_KMSTATS], *kmsp; 963 char buf[1024]; 964 965 kread(X_KMEMSTATISTICS, &kmsp, sizeof(kmsp)); 966 for (nkms = 0; nkms < MAX_KMSTATS && kmsp != NULL; nkms++) { 967 if (sizeof(kmemstats[0]) != kvm_read(kd, (u_long)kmsp, 968 &kmemstats[nkms], sizeof(kmemstats[0]))) 969 err(1, "kvm_read(%p)", (void *)kmsp); 970 if (sizeof(buf) != kvm_read(kd, 971 (u_long)kmemstats[nkms].ks_shortdesc, buf, sizeof(buf))) 972 err(1, "kvm_read(%p)", 973 kmemstats[nkms].ks_shortdesc); 974 buf[sizeof(buf) - 1] = '\0'; 975 kmemstats[nkms].ks_shortdesc = strdup(buf); 976 kmsp = kmemstats[nkms].ks_next; 977 } 978 if (kmsp != NULL) 979 warnx("truncated to the first %d memory types", nkms); 980 981 printf( 982 "\nMemory statistics by type\n"); 983 printf(" Type Count MemUse Limit Requests\n"); 984 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) { 985 long ks_inuse; 986 long ks_memuse; 987 long ks_calls; 988 989 ks_calls = cpuagg(ks, KSCALLS); 990 if (ks_calls == 0) 991 continue; 992 993 ks_inuse = cpuagg(ks, KSINUSE); 994 ks_memuse = cpuagg(ks, KSMEMUSE); 995 996 printf("%19s %s %s %s %s\n", 997 ks->ks_shortdesc, 998 formatnum(ks_inuse, 5, 1), 999 formatnum(ks_memuse, 5, 1), 1000 formatnum(ks->ks_limit, 5, 1), 1001 formatnum(ks_calls, 5, 1)); 1002 1003 totuse += ks_memuse; 1004 totreq += ks_calls; 1005 } 1006 printf("\nMemory Totals: In Use Requests\n"); 1007 printf(" %s %s\n", 1008 formatnum(totuse, 5, 1), 1009 formatnum(totreq, 5, 1)); 1010 } 1011 1012 #define MAXSAVE 16 1013 1014 static void 1015 dozmem(u_int interval, int reps) 1016 { 1017 struct zlist zlist; 1018 struct vm_zone *kz; 1019 struct vm_zone zone; 1020 struct vm_zone copy; 1021 struct vm_zone save[MAXSAVE]; 1022 char name[64]; 1023 size_t namesz; 1024 int i; 1025 int first = 1; 1026 1027 bzero(save, sizeof(save)); 1028 1029 again: 1030 kread(X_ZLIST, &zlist, sizeof(zlist)); 1031 kz = LIST_FIRST(&zlist); 1032 i = 0; 1033 1034 while (kz) { 1035 if (kvm_read(kd, (intptr_t)kz, &zone, sizeof(zone)) != 1036 (ssize_t)sizeof(zone)) { 1037 perror("kvm_read"); 1038 break; 1039 } 1040 copy = zone; 1041 zone.znalloc -= save[i].znalloc; 1042 save[i] = copy; 1043 namesz = sizeof(name); 1044 if (kvm_readstr(kd, (intptr_t)zone.zname, name, &namesz) == NULL) { 1045 perror("kvm_read"); 1046 break; 1047 } 1048 if (first && interval) { 1049 /* do nothing */ 1050 } else if (zone.zmax) { 1051 printf("%-10s %9ld/%9ld %5ldM used" 1052 " use=%-9lu %6.2f%%\n", 1053 name, 1054 (long)(zone.ztotal - zone.zfreecnt), 1055 (long)zone.zmax, 1056 (long)(zone.ztotal - zone.zfreecnt) * 1057 zone.zsize / (1024 * 1024), 1058 (unsigned long)zone.znalloc, 1059 (double)(zone.ztotal - zone.zfreecnt) * 1060 100.0 / (double)zone.zmax); 1061 } else { 1062 printf("%-10s %9ld %5ldM used" 1063 " use=%-9lu\n", 1064 name, 1065 (long)(zone.ztotal - zone.zfreecnt), 1066 (long)(zone.ztotal - zone.zfreecnt) * 1067 zone.zsize / (1024 * 1024), 1068 (unsigned long)zone.znalloc); 1069 } 1070 kz = LIST_NEXT(&zone, zlink); 1071 ++i; 1072 } 1073 if (reps) { 1074 first = 0; 1075 fflush(stdout); 1076 usleep(interval * 1000); 1077 --reps; 1078 printf("\n"); 1079 goto again; 1080 } 1081 } 1082 1083 /* 1084 * kread reads something from the kernel, given its nlist index. 1085 */ 1086 static void 1087 kread(int nlx, void *addr, size_t size) 1088 { 1089 const char *sym; 1090 1091 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 1092 sym = namelist[nlx].n_name; 1093 if (*sym == '_') 1094 ++sym; 1095 errx(1, "symbol %s not defined", sym); 1096 } 1097 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != (ssize_t)size) { 1098 sym = namelist[nlx].n_name; 1099 if (*sym == '_') 1100 ++sym; 1101 errx(1, "%s: %s", sym, kvm_geterr(kd)); 1102 } 1103 } 1104 1105 static void 1106 usage(void) 1107 { 1108 fprintf(stderr, "%s%s", 1109 "usage: vmstat [-imsuvz] [-c count] [-M core] " 1110 "[-N system] [-w wait]\n", 1111 " [-n devs] [disks]\n"); 1112 exit(1); 1113 } 1114