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