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 struct timespec ts; 401 402 clock_gettime(CLOCK_UPTIME, &ts); 403 404 return ts.tv_sec; 405 } 406 407 int hdrcnt; 408 409 static void 410 dovmstat(u_int interval, int reps) 411 { 412 struct vmtotal total; 413 struct devinfo *tmp_dinfo; 414 size_t vmm_size = sizeof(vmm); 415 size_t vms_size = sizeof(vms); 416 size_t vmt_size = sizeof(total); 417 int initial = 1; 418 int dooutput = 1; 419 420 signal(SIGCONT, needhdr); 421 if (reps != 0) 422 dooutput = 0; 423 424 for (hdrcnt = 1;;) { 425 if (!--hdrcnt) 426 printhdr(); 427 if (kinfo_get_sched_cputime(&cp_time)) 428 err(1, "kinfo_get_sched_cputime"); 429 430 tmp_dinfo = last.dinfo; 431 last.dinfo = cur.dinfo; 432 cur.dinfo = tmp_dinfo; 433 last.busy_time = cur.busy_time; 434 435 /* 436 * Here what we want to do is refresh our device stats. 437 * getdevs() returns 1 when the device list has changed. 438 * If the device list has changed, we want to go through 439 * the selection process again, in case a device that we 440 * were previously displaying has gone away. 441 */ 442 switch (getdevs(&cur)) { 443 case -1: 444 errx(1, "%s", devstat_errbuf); 445 break; 446 case 1: { 447 int retval; 448 449 num_devices = cur.dinfo->numdevs; 450 generation = cur.dinfo->generation; 451 452 retval = selectdevs(&dev_select, &num_selected, 453 &num_selections, &select_generation, 454 generation, cur.dinfo->devices, 455 num_devices, matches, num_matches, 456 specified_devices, 457 num_devices_specified, select_mode, 458 maxshowdevs, 0); 459 switch (retval) { 460 case -1: 461 errx(1, "%s", devstat_errbuf); 462 break; 463 case 1: 464 printhdr(); 465 break; 466 default: 467 break; 468 } 469 } 470 default: 471 break; 472 } 473 474 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) { 475 perror("sysctlbyname: vm.vmstats"); 476 exit(1); 477 } 478 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) { 479 perror("sysctlbyname: vm.vmmeter"); 480 exit(1); 481 } 482 if (sysctlbyname("vm.vmtotal", &total, &vmt_size, NULL, 0)) { 483 perror("sysctlbyname: vm.vmtotal"); 484 exit(1); 485 } 486 487 /* 488 * Be a little inventive so we can squeeze everything into 489 * 80 columns. These days the run queue can trivially be 490 * into the three digits and under heavy paging loads the 491 * blocked (d+p) count can as well. 492 */ 493 if (dooutput) { 494 char b1[4]; 495 char b2[4]; 496 char b3[2]; 497 498 strcpy(b1, "***"); 499 strcpy(b2, "***"); 500 strcpy(b3, "*"); 501 if (total.t_rq - 1 < 1000) { 502 snprintf(b1, sizeof(b1), 503 "%3ld", total.t_rq - 1); 504 } 505 if (total.t_dw + total.t_pw < 1000) { 506 snprintf(b2, sizeof(b2), 507 "%3ld", total.t_dw + total.t_pw); 508 } 509 if (total.t_sw < 10) { 510 snprintf(b3, sizeof(b3), "%ld", total.t_sw); 511 } 512 printf("%s %s %s", b1, b2, b3); 513 } 514 515 #define rate(x) \ 516 (intmax_t)(initial ? (x) : ((intmax_t)(x) * 1000 + interval / 2) \ 517 / interval) 518 519 if (dooutput) { 520 printf(" %s ", 521 formatnum((int64_t)total.t_free * 522 vms.v_page_size, 523 5, 1)); 524 printf("%s ", 525 formatnum(rate(vmm.v_vm_faults - 526 ovmm.v_vm_faults), 527 5, 1)); 528 printf("%s ", 529 formatnum(rate((vmm.v_reactivated - 530 ovmm.v_reactivated) * 531 vms.v_page_size), 532 4, 1)); 533 printf("%s ", 534 formatnum(rate((vmm.v_swappgsin + 535 vmm.v_vnodepgsin - 536 ovmm.v_swappgsin - 537 ovmm.v_vnodepgsin) * 538 vms.v_page_size), 539 4, 1)); 540 printf("%s ", 541 formatnum(rate((vmm.v_swappgsout + 542 vmm.v_vnodepgsout - 543 ovmm.v_swappgsout - 544 ovmm.v_vnodepgsout) * 545 vms.v_page_size), 546 4, 1)); 547 printf("%s ", 548 formatnum(rate((vmm.v_tfree - ovmm.v_tfree) * 549 vms.v_page_size), 4, 1)); 550 } 551 devstats(dooutput); 552 if (dooutput) { 553 printf("%s ", 554 formatnum(rate(vmm.v_intr - ovmm.v_intr), 555 5, 1)); 556 printf("%s ", 557 formatnum(rate(vmm.v_syscall - 558 ovmm.v_syscall), 559 5, 1)); 560 printf("%s ", 561 formatnum(rate(vmm.v_swtch - 562 ovmm.v_swtch), 563 5, 1)); 564 cpustats(); 565 printf("\n"); 566 fflush(stdout); 567 } 568 if (reps >= 0 && --reps <= 0) 569 break; 570 ovmm = vmm; 571 usleep(interval * 1000); 572 initial = 0; 573 dooutput = 1; 574 } 575 } 576 577 static const char * 578 formatnum(intmax_t value, int width, int do10s) 579 { 580 static char buf[16][64]; 581 static int bi; 582 const char *fmt; 583 double d; 584 585 if (brief_opt) 586 do10s = 0; 587 588 bi = (bi + 1) % 16; 589 590 if (unformatted_opt) { 591 switch(width) { 592 case 4: 593 snprintf(buf[bi], sizeof(buf[bi]), "%4jd", value); 594 break; 595 case 5: 596 snprintf(buf[bi], sizeof(buf[bi]), "%5jd", value); 597 break; 598 default: 599 snprintf(buf[bi], sizeof(buf[bi]), "%jd", value); 600 break; 601 } 602 return buf[bi]; 603 } 604 605 d = (double)value; 606 fmt = "n/a"; 607 608 switch(width) { 609 case 4: 610 if (value < 1024) { 611 fmt = "%4.0f"; 612 } else if (value < 10*1024) { 613 fmt = "%3.1fK"; 614 d = d / 1024; 615 } else if (value < 1000*1024) { 616 fmt = "%3.0fK"; 617 d = d / 1024; 618 } else if (value < 10*1024*1024) { 619 fmt = "%3.1fM"; 620 d = d / (1024 * 1024); 621 } else if (value < 1000*1024*1024) { 622 fmt = "%3.0fM"; 623 d = d / (1024 * 1024); 624 } else { 625 fmt = "%3.1fG"; 626 d = d / (1024.0 * 1024.0 * 1024.0); 627 } 628 break; 629 case 5: 630 if (value < 1024) { 631 fmt = "%5.0f"; 632 } else if (value < 10*1024) { 633 fmt = "%4.2fK"; 634 d = d / 1024; 635 } else if (value < 100*1024 && do10s) { 636 fmt = "%4.1fK"; 637 d = d / 1024; 638 } else if (value < 1000*1024) { 639 fmt = "%4.0fK"; 640 d = d / 1024; 641 } else if (value < 10*1024*1024) { 642 fmt = "%4.2fM"; 643 d = d / (1024 * 1024); 644 } else if (value < 100*1024*1024 && do10s) { 645 fmt = "%4.1fM"; 646 d = d / (1024 * 1024); 647 } else if (value < 1000*1024*1024) { 648 fmt = "%4.0fM"; 649 d = d / (1024 * 1024); 650 } else if (value < 10LL*1024*1024*1024) { 651 fmt = "%4.2fG"; 652 d = d / (1024.0 * 1024.0 * 1024.0); 653 } else if (value < 100LL*1024*1024*1024 && do10s) { 654 fmt = "%4.1fG"; 655 d = d / (1024.0 * 1024.0 * 1024.0); 656 } else if (value < 1000LL*1024*1024*1024) { 657 fmt = "%4.0fG"; 658 d = d / (1024.0 * 1024.0 * 1024.0); 659 } else { 660 fmt = "%4.2fT"; 661 d = d / (1024.0 * 1024.0 * 1024.0 * 1024.0); 662 } 663 break; 664 default: 665 fprintf(stderr, "formatnum: unsupported width %d\n", width); 666 exit(1); 667 break; 668 } 669 snprintf(buf[bi], sizeof(buf[bi]), fmt, d); 670 return buf[bi]; 671 } 672 673 static void 674 printhdr(void) 675 { 676 int i, num_shown; 677 678 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs; 679 printf("--procs-- ---memory-- -------paging------ "); 680 if (num_shown > 1) 681 printf("--disks%.*s", 682 num_shown * 4 - 6, 683 "---------------------------------"); 684 else if (num_shown == 1) 685 printf("disk"); 686 printf(" -----faults------ ---cpu---\n"); 687 printf(" r b w fre flt re pi po fr "); 688 for (i = 0; i < num_devices; i++) 689 if ((dev_select[i].selected) 690 && (dev_select[i].selected <= maxshowdevs)) 691 printf(" %c%c%d ", dev_select[i].device_name[0], 692 dev_select[i].device_name[1], 693 dev_select[i].unit_number); 694 printf(" int sys ctx us sy id\n"); 695 hdrcnt = winlines - 2; 696 } 697 698 /* 699 * Force a header to be prepended to the next output. 700 */ 701 static void 702 needhdr(__unused int signo) 703 { 704 705 hdrcnt = 1; 706 } 707 708 static long 709 pct(long top, long bot) 710 { 711 long ans; 712 713 if (bot == 0) 714 return(0); 715 ans = (quad_t)top * 100 / bot; 716 return (ans); 717 } 718 719 #define PCT(top, bot) pct((long)(top), (long)(bot)) 720 721 static void 722 dosum(void) 723 { 724 struct nchstats *nch_tmp, nchstats; 725 size_t vms_size = sizeof(vms); 726 size_t vmm_size = sizeof(vmm); 727 int cpucnt; 728 u_long nchtotal; 729 u_long nchpathtotal; 730 size_t nch_size = sizeof(struct nchstats) * SMP_MAXCPU; 731 732 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) { 733 perror("sysctlbyname: vm.vmstats"); 734 exit(1); 735 } 736 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) { 737 perror("sysctlbyname: vm.vmstats"); 738 exit(1); 739 } 740 printf("%9u cpu context switches\n", vmm.v_swtch); 741 printf("%9u device interrupts\n", vmm.v_intr); 742 printf("%9u software interrupts\n", vmm.v_soft); 743 printf("%9u traps\n", vmm.v_trap); 744 printf("%9u system calls\n", vmm.v_syscall); 745 printf("%9u kernel threads created\n", vmm.v_kthreads); 746 printf("%9u fork() calls\n", vmm.v_forks); 747 printf("%9u vfork() calls\n", vmm.v_vforks); 748 printf("%9u rfork() calls\n", vmm.v_rforks); 749 printf("%9u exec() calls\n", vmm.v_exec); 750 printf("%9u swap pager pageins\n", vmm.v_swapin); 751 printf("%9u swap pager pages paged in\n", vmm.v_swappgsin); 752 printf("%9u swap pager pageouts\n", vmm.v_swapout); 753 printf("%9u swap pager pages paged out\n", vmm.v_swappgsout); 754 printf("%9u vnode pager pageins\n", vmm.v_vnodein); 755 printf("%9u vnode pager pages paged in\n", vmm.v_vnodepgsin); 756 printf("%9u vnode pager pageouts\n", vmm.v_vnodeout); 757 printf("%9u vnode pager pages paged out\n", vmm.v_vnodepgsout); 758 printf("%9u page daemon wakeups\n", vmm.v_pdwakeups); 759 printf("%9u pages examined by the page daemon\n", vmm.v_pdpages); 760 printf("%9u pages reactivated\n", vmm.v_reactivated); 761 printf("%9u copy-on-write faults\n", vmm.v_cow_faults); 762 printf("%9u copy-on-write optimized faults\n", vmm.v_cow_optim); 763 printf("%9u zero fill pages zeroed\n", vmm.v_zfod); 764 printf("%9u zero fill pages prezeroed\n", vmm.v_ozfod); 765 printf("%9u intransit blocking page faults\n", vmm.v_intrans); 766 printf("%9u total VM faults taken\n", vmm.v_vm_faults); 767 printf("%9u pages affected by kernel thread creation\n", vmm.v_kthreadpages); 768 printf("%9u pages affected by fork()\n", vmm.v_forkpages); 769 printf("%9u pages affected by vfork()\n", vmm.v_vforkpages); 770 printf("%9u pages affected by rfork()\n", vmm.v_rforkpages); 771 printf("%9u pages freed\n", vmm.v_tfree); 772 printf("%9u pages freed by daemon\n", vmm.v_dfree); 773 printf("%9u pages freed by exiting processes\n", vmm.v_pfree); 774 printf("%9lu pages active\n", vms.v_active_count); 775 printf("%9lu pages inactive\n", vms.v_inactive_count); 776 printf("%9lu pages in VM cache\n", vms.v_cache_count); 777 printf("%9lu pages wired down\n", vms.v_wire_count); 778 printf("%9lu pages free\n", vms.v_free_count); 779 printf("%9u bytes per page\n", vms.v_page_size); 780 printf("%9u global smp invltlbs\n", vmm.v_smpinvltlb); 781 782 if ((nch_tmp = malloc(nch_size)) == NULL) { 783 perror("malloc"); 784 exit(1); 785 } else { 786 if (sysctlbyname("vfs.cache.nchstats", nch_tmp, &nch_size, NULL, 0)) { 787 perror("sysctlbyname vfs.cache.nchstats"); 788 free(nch_tmp); 789 exit(1); 790 } else { 791 if ((nch_tmp = realloc(nch_tmp, nch_size)) == NULL) { 792 perror("realloc"); 793 exit(1); 794 } 795 } 796 } 797 798 cpucnt = nch_size / sizeof(struct nchstats); 799 kvm_nch_cpuagg(nch_tmp, &nchstats, cpucnt); 800 801 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 802 nchstats.ncs_badhits + nchstats.ncs_falsehits + 803 nchstats.ncs_miss; 804 nchpathtotal = nchstats.ncs_longhits + nchstats.ncs_longmiss; 805 printf("%9ld total path lookups\n", nchpathtotal); 806 printf("%9ld total component lookups\n", nchtotal); 807 printf( 808 "%9s cache hits (%ld%% pos + %ld%% neg)\n", 809 "", PCT(nchstats.ncs_goodhits, nchtotal), 810 PCT(nchstats.ncs_neghits, nchtotal)); 811 printf("%9s deletions %ld%%, falsehits %ld%%\n", "", 812 PCT(nchstats.ncs_badhits, nchtotal), 813 PCT(nchstats.ncs_falsehits, nchtotal)); 814 free(nch_tmp); 815 } 816 817 #ifdef notyet 818 void 819 doforkst(void) 820 { 821 struct forkstat fks; 822 823 kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); 824 printf("%d forks, %d pages, average %.2f\n", 825 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); 826 printf("%d vforks, %d pages, average %.2f\n", 827 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); 828 } 829 #endif 830 831 static void 832 devstats(int dooutput) 833 { 834 int dn; 835 long double transfers_per_second; 836 long double busy_seconds; 837 838 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user; 839 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice; 840 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys; 841 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr; 842 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle; 843 old_cp_time = cp_time; 844 845 busy_seconds = compute_etime(cur.busy_time, last.busy_time); 846 847 for (dn = 0; dn < num_devices; dn++) { 848 int di; 849 850 if ((dev_select[dn].selected == 0) 851 || (dev_select[dn].selected > maxshowdevs)) 852 continue; 853 854 di = dev_select[dn].position; 855 856 if (compute_stats(&cur.dinfo->devices[di], 857 &last.dinfo->devices[di], busy_seconds, 858 NULL, NULL, NULL, 859 NULL, &transfers_per_second, NULL, 860 NULL, NULL) != 0) 861 errx(1, "%s", devstat_errbuf); 862 863 if (dooutput) 864 printf("%s ", formatnum(transfers_per_second, 4, 0)); 865 } 866 } 867 868 static void 869 cpustats(void) 870 { 871 uint64_t total; 872 double totusage; 873 874 total = diff_cp_time.cp_user + diff_cp_time.cp_nice + 875 diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle; 876 877 if (total) 878 totusage = 100.0 / total; 879 else 880 totusage = 0; 881 printf("%2.0f ", 882 (diff_cp_time.cp_user + diff_cp_time.cp_nice) * totusage); 883 printf("%2.0f ", 884 (diff_cp_time.cp_sys + diff_cp_time.cp_intr) * totusage); 885 printf("%2.0f", 886 diff_cp_time.cp_idle * totusage); 887 } 888 889 static void 890 dointr(void) 891 { 892 u_long *intrcnt, uptime; 893 u_int64_t inttotal; 894 size_t nintr, inamlen, i, size; 895 int nwidth; 896 char *intrstr; 897 char **intrname; 898 899 uptime = getuptime(); 900 if (sysctlbyname("hw.intrnames", NULL, &inamlen, NULL, 0) != 0) 901 errx(1, "sysctlbyname"); 902 intrstr = malloc(inamlen); 903 if (intrstr == NULL) 904 err(1, "malloc"); 905 sysctlbyname("hw.intrnames", intrstr, &inamlen, NULL, 0); 906 for (nintr = 0, i = 0; i < inamlen; ++i) { 907 if (intrstr[i] == 0) 908 nintr++; 909 } 910 intrname = malloc(nintr * sizeof(char *)); 911 for (i = 0; i < nintr; ++i) { 912 intrname[i] = intrstr; 913 intrstr += strlen(intrstr) + 1; 914 } 915 916 size = nintr * sizeof(*intrcnt); 917 intrcnt = calloc(nintr, sizeof(*intrcnt)); 918 if (intrcnt == NULL) 919 err(1, "malloc"); 920 sysctlbyname("hw.intrcnt", intrcnt, &size, NULL, 0); 921 922 nwidth = 21; 923 for (i = 0; i < nintr; ++i) { 924 if (nwidth < (int)strlen(intrname[i])) 925 nwidth = (int)strlen(intrname[i]); 926 } 927 if (verbose) nwidth += 12; 928 929 printf("%-*.*s %11s %10s\n", 930 nwidth, nwidth, "interrupt", "total", "rate"); 931 inttotal = 0; 932 for (i = 0; i < nintr; ++i) { 933 int named; 934 char *infop, irqinfo[72]; 935 936 if ((named = strncmp(intrname[i], "irq", 3)) != 0 || 937 intrcnt[i] > 0) { 938 infop = intrname[i]; 939 if (verbose) { 940 ssize_t irq, cpu; 941 942 irq = i % MAX_INTS; 943 cpu = i / MAX_INTS; 944 if (named) { 945 snprintf(irqinfo, sizeof(irqinfo), 946 "irq%-3zd %3zd: %s", 947 irq, cpu, intrname[i]); 948 } else { 949 snprintf(irqinfo, sizeof(irqinfo), 950 "irq%-3zd %3zd: ", irq, cpu); 951 } 952 infop = irqinfo; 953 } 954 printf("%-*.*s %11lu %10lu\n", 955 nwidth, nwidth, infop, 956 intrcnt[i], intrcnt[i] / uptime); 957 } 958 inttotal += intrcnt[i]; 959 } 960 printf("%-*.*s %11llu %10llu\n", 961 nwidth, nwidth, "Total", 962 (long long)inttotal, (long long)(inttotal / uptime)); 963 } 964 965 #define MAX_KMSTATS 16384 966 967 enum ksuse { KSINUSE, KSMEMUSE, KSCALLS }; 968 969 static long 970 cpuagg(struct malloc_type *ks, enum ksuse use) 971 { 972 int i; 973 long ttl; 974 975 ttl = 0; 976 977 switch(use) { 978 case KSINUSE: 979 for (i = 0; i < ncpus; ++i) 980 ttl += ks->ks_use[i].inuse; 981 break; 982 case KSMEMUSE: 983 for (i = 0; i < ncpus; ++i) 984 ttl += ks->ks_use[i].memuse; 985 break; 986 case KSCALLS: 987 for (i = 0; i < ncpus; ++i) 988 ttl += ks->ks_use[i].calls; 989 break; 990 } 991 return(ttl); 992 } 993 994 static void 995 domem(void) 996 { 997 struct malloc_type *ks; 998 int i; 999 int nkms; 1000 long totuse = 0, totreq = 0; 1001 struct malloc_type kmemstats[MAX_KMSTATS], *kmsp; 1002 char buf[1024]; 1003 1004 kread(X_KMEMSTATISTICS, &kmsp, sizeof(kmsp)); 1005 for (nkms = 0; nkms < MAX_KMSTATS && kmsp != NULL; nkms++) { 1006 if (sizeof(kmemstats[0]) != kvm_read(kd, (u_long)kmsp, 1007 &kmemstats[nkms], sizeof(kmemstats[0]))) 1008 err(1, "kvm_read(%p)", (void *)kmsp); 1009 if (sizeof(buf) != kvm_read(kd, 1010 (u_long)kmemstats[nkms].ks_shortdesc, buf, sizeof(buf))) 1011 err(1, "kvm_read(%p)", 1012 kmemstats[nkms].ks_shortdesc); 1013 buf[sizeof(buf) - 1] = '\0'; 1014 kmemstats[nkms].ks_shortdesc = strdup(buf); 1015 if (kmemstats[nkms].ks_use) { 1016 size_t usebytes; 1017 void *use; 1018 1019 usebytes = ncpus * sizeof(kmemstats[nkms].ks_use[0]); 1020 use = malloc(usebytes); 1021 if (kvm_read(kd, (u_long)kmemstats[nkms].ks_use, 1022 use, usebytes) != (ssize_t)usebytes) { 1023 err(1, "kvm_read(%p)", kmemstats[nkms].ks_use); 1024 } 1025 kmemstats[nkms].ks_use = use; 1026 } 1027 kmsp = kmemstats[nkms].ks_next; 1028 } 1029 if (kmsp != NULL) 1030 warnx("truncated to the first %d memory types", nkms); 1031 1032 printf( 1033 "\nMemory statistics by type\n"); 1034 printf(" Type Count MemUse Limit Requests\n"); 1035 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) { 1036 long ks_inuse; 1037 long ks_memuse; 1038 long ks_calls; 1039 1040 ks_calls = cpuagg(ks, KSCALLS); 1041 if (ks_calls == 0 && verbose == 0) 1042 continue; 1043 1044 ks_inuse = cpuagg(ks, KSINUSE); 1045 ks_memuse = cpuagg(ks, KSMEMUSE); 1046 1047 printf("%19s %s %s %s %s\n", 1048 ks->ks_shortdesc, 1049 formatnum(ks_inuse, 5, 1), 1050 formatnum(ks_memuse, 5, 1), 1051 formatnum(ks->ks_limit, 5, 1), 1052 formatnum(ks_calls, 5, 1)); 1053 1054 totuse += ks_memuse; 1055 totreq += ks_calls; 1056 } 1057 printf("\nMemory Totals: In Use Requests\n"); 1058 printf(" %s %s\n", 1059 formatnum(totuse, 5, 1), 1060 formatnum(totreq, 5, 1)); 1061 } 1062 1063 static void 1064 dooc(void) 1065 { 1066 struct objcache_stats *stat, *s; 1067 size_t len, count; 1068 1069 if (sysctlbyname("kern.objcache.stats", NULL, &len, NULL, 0) < 0) 1070 errx(1, "objcache stats sysctl failed\n"); 1071 1072 /* Add some extra space. */ 1073 stat = malloc(len + (8 * sizeof(*stat))); 1074 if (sysctlbyname("kern.objcache.stats", stat, &len, NULL, 0) < 0) 1075 errx(1, "objcache stats sysctl failed\n"); 1076 1077 printf( 1078 "\nObjcache statistics by name\n"); 1079 printf(" Name Used Cached Limit Requests Allocs Fails Exhausts\n"); 1080 for (s = stat, count = 0; count < len; ++s) { 1081 printf("%21s %s %s %s %s %s %s %s\n", 1082 s->oc_name, 1083 formatnum(s->oc_used, 5, 1), 1084 formatnum(s->oc_cached, 5, 1), 1085 s->oc_limit < OBJCACHE_UNLIMITED ? 1086 formatnum(s->oc_limit, 5, 1) : "unlim", 1087 formatnum(s->oc_requested, 5, 1), 1088 formatnum(s->oc_allocated, 5, 1), 1089 formatnum(s->oc_failed, 4, 1), 1090 formatnum(s->oc_exhausted, 4, 1)); 1091 1092 count += sizeof(*s); 1093 } 1094 free(stat); 1095 } 1096 1097 #define MAXSAVE 16 1098 1099 static void 1100 dozmem(u_int interval, int reps) 1101 { 1102 struct zlist zlist; 1103 struct vm_zone *kz; 1104 struct vm_zone zone; 1105 struct vm_zone save[MAXSAVE]; 1106 long zfreecnt_prev; 1107 long znalloc_prev; 1108 long zfreecnt_next; 1109 long znalloc_next; 1110 char name[64]; 1111 size_t namesz; 1112 int first = 1; 1113 int i; 1114 int n; 1115 1116 bzero(save, sizeof(save)); 1117 1118 again: 1119 kread(X_ZLIST, &zlist, sizeof(zlist)); 1120 kz = LIST_FIRST(&zlist); 1121 i = 0; 1122 1123 while (kz) { 1124 if (kvm_read(kd, (intptr_t)kz, &zone, sizeof(zone)) != 1125 (ssize_t)sizeof(zone)) { 1126 perror("kvm_read"); 1127 break; 1128 } 1129 zfreecnt_prev = save[i].zfreecnt; 1130 znalloc_prev = save[i].znalloc; 1131 for (n = 0; n < ncpus; ++n) { 1132 zfreecnt_prev += save[i].zpcpu[n].zfreecnt; 1133 znalloc_prev += save[i].zpcpu[n].znalloc; 1134 } 1135 1136 zfreecnt_next = zone.zfreecnt; 1137 znalloc_next = zone.znalloc; 1138 for (n = 0; n < ncpus; ++n) { 1139 zfreecnt_next += zone.zpcpu[n].zfreecnt; 1140 znalloc_next += zone.zpcpu[n].znalloc; 1141 } 1142 save[i] = zone; 1143 1144 namesz = sizeof(name); 1145 if (kvm_readstr(kd, (intptr_t)zone.zname, name, &namesz) == NULL) { 1146 perror("kvm_read"); 1147 break; 1148 } 1149 if (first && interval) { 1150 /* do nothing */ 1151 } else if (zone.zmax) { 1152 printf("%-10s %9ld / %-9ld %5ldM used" 1153 " %6.2f%% ", 1154 name, 1155 (long)(zone.ztotal - zfreecnt_next), 1156 (long)zone.zmax, 1157 (long)zone.zpagecount * 4096 / (1024 * 1024), 1158 (double)(zone.ztotal - zfreecnt_next) * 1159 100.0 / (double)zone.zmax); 1160 } else { 1161 printf("%-10s %9ld %5ldM used" 1162 " ", 1163 name, 1164 (long)(zone.ztotal - zfreecnt_next), 1165 (long)(zone.ztotal - zfreecnt_next) * 1166 zone.zsize / (1024 * 1024)); 1167 } 1168 if (first == 0) { 1169 printf("use=%ld\n", znalloc_next - znalloc_prev); 1170 } else if (interval == 0) 1171 printf("\n"); 1172 1173 kz = LIST_NEXT(&zone, zlink); 1174 ++i; 1175 } 1176 if (reps) { 1177 first = 0; 1178 fflush(stdout); 1179 usleep(interval * 1000); 1180 --reps; 1181 printf("\n"); 1182 goto again; 1183 } 1184 } 1185 1186 /* 1187 * kread reads something from the kernel, given its nlist index. 1188 */ 1189 static void 1190 kread(int nlx, void *addr, size_t size) 1191 { 1192 const char *sym; 1193 1194 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 1195 sym = namelist[nlx].n_name; 1196 if (*sym == '_') 1197 ++sym; 1198 errx(1, "symbol %s not defined", sym); 1199 } 1200 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != (ssize_t)size) { 1201 sym = namelist[nlx].n_name; 1202 if (*sym == '_') 1203 ++sym; 1204 errx(1, "%s: %s", sym, kvm_geterr(kd)); 1205 } 1206 } 1207 1208 static void 1209 usage(void) 1210 { 1211 fprintf(stderr, "%s%s", 1212 "usage: vmstat [-imsuvz] [-c count] [-M core] " 1213 "[-N system] [-w wait]\n", 1214 " [-n devs] [disks]\n"); 1215 exit(1); 1216 } 1217