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