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