vmstat.c revision 128573
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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1980, 1986, 1991, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#if 0 41#ifndef lint 42static char sccsid[] = "@(#)vmstat.c 8.1 (Berkeley) 6/6/93"; 43#endif /* not lint */ 44#endif 45 46#include <sys/cdefs.h> 47__FBSDID("$FreeBSD: head/usr.bin/vmstat/vmstat.c 128573 2004-04-23 13:10:29Z tjr $"); 48 49#include <sys/param.h> 50#include <sys/time.h> 51#include <sys/proc.h> 52#include <sys/uio.h> 53#include <sys/namei.h> 54#include <sys/malloc.h> 55#include <sys/signal.h> 56#include <sys/fcntl.h> 57#include <sys/ioctl.h> 58#include <sys/resource.h> 59#include <sys/sysctl.h> 60#include <sys/vmmeter.h> 61 62#include <vm/vm_param.h> 63 64#include <ctype.h> 65#include <devstat.h> 66#include <err.h> 67#include <errno.h> 68#include <kvm.h> 69#include <limits.h> 70#include <nlist.h> 71#include <paths.h> 72#include <stdio.h> 73#include <stdlib.h> 74#include <string.h> 75#include <sysexits.h> 76#include <time.h> 77#include <unistd.h> 78 79static char da[] = "da"; 80 81static struct nlist namelist[] = { 82#define X_CPTIME 0 83 { "_cp_time" }, 84#define X_SUM 1 85 { "_cnt" }, 86#define X_BOOTTIME 2 87 { "_boottime" }, 88#define X_HZ 3 89 { "_hz" }, 90#define X_STATHZ 4 91 { "_stathz" }, 92#define X_NCHSTATS 5 93 { "_nchstats" }, 94#define X_INTRNAMES 6 95 { "_intrnames" }, 96#define X_EINTRNAMES 7 97 { "_eintrnames" }, 98#define X_INTRCNT 8 99 { "_intrcnt" }, 100#define X_EINTRCNT 9 101 { "_eintrcnt" }, 102#ifdef notyet 103#define X_DEFICIT 10 104 { "_deficit" }, 105#define X_REC 11 106 { "_rectime" }, 107#define X_PGIN 12 108 { "_pgintime" }, 109#define X_XSTATS 13 110 { "_xstats" }, 111#define X_END 14 112#else 113#define X_END 10 114#endif 115 { "" }, 116}; 117 118static struct statinfo cur, last; 119static int num_devices, maxshowdevs; 120static long generation; 121static struct device_selection *dev_select; 122static int num_selected; 123static struct devstat_match *matches; 124static int num_matches = 0; 125static int num_devices_specified, num_selections; 126static long select_generation; 127static char **specified_devices; 128static devstat_select_mode select_mode; 129 130static struct vmmeter sum, osum; 131 132static int winlines = 20; 133static int aflag; 134static int nflag; 135 136static kvm_t *kd; 137 138#define FORKSTAT 0x01 139#define INTRSTAT 0x02 140#define MEMSTAT 0x04 141#define SUMSTAT 0x08 142#define TIMESTAT 0x10 143#define VMSTAT 0x20 144#define ZMEMSTAT 0x40 145 146static void cpustats(void); 147static void devstats(void); 148static void doforkst(void); 149static void domem(void); 150static void dointr(void); 151static void dosum(void); 152static void dosysctl(const char *); 153static void dovmstat(unsigned int, int); 154static void dozmem(void); 155static void kread(int, void *, size_t); 156static void needhdr(int); 157static void printhdr(void); 158static void usage(void); 159 160static long pct(long, long); 161static long getuptime(void); 162 163static char **getdrivedata(char **); 164 165int 166main(int argc, char *argv[]) 167{ 168 int c, todo; 169 unsigned int interval; 170 int reps; 171 char *memf, *nlistf; 172 char errbuf[_POSIX2_LINE_MAX]; 173 174 memf = nlistf = NULL; 175 interval = reps = todo = 0; 176 maxshowdevs = 2; 177 while ((c = getopt(argc, argv, "ac:fiM:mN:n:p:stw:z")) != -1) { 178 switch (c) { 179 case 'a': 180 aflag++; 181 break; 182 case 'c': 183 reps = atoi(optarg); 184 break; 185 case 'f': 186 todo |= FORKSTAT; 187 break; 188 case 'i': 189 todo |= INTRSTAT; 190 break; 191 case 'M': 192 memf = optarg; 193 break; 194 case 'm': 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 'p': 208 if (devstat_buildmatch(optarg, &matches, &num_matches) != 0) 209 errx(1, "%s", devstat_errbuf); 210 break; 211 case 's': 212 todo |= SUMSTAT; 213 break; 214 case 't': 215#ifdef notyet 216 todo |= TIMESTAT; 217#else 218 errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); 219#endif 220 break; 221 case 'w': 222 interval = atoi(optarg); 223 break; 224 case 'z': 225 todo |= ZMEMSTAT; 226 break; 227 case '?': 228 default: 229 usage(); 230 } 231 } 232 argc -= optind; 233 argv += optind; 234 235 if (todo == 0) 236 todo = VMSTAT; 237 238 if (memf != NULL) { 239 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 240 if (kd == NULL) 241 errx(1, "kvm_openfiles: %s", errbuf); 242 } 243 244 if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) { 245 if (c > 0) { 246 warnx("undefined symbols:"); 247 for (c = 0; 248 c < (int)(sizeof(namelist)/sizeof(namelist[0])); 249 c++) 250 if (namelist[c].n_type == 0) 251 (void)fprintf(stderr, " %s", 252 namelist[c].n_name); 253 (void)fputc('\n', stderr); 254 } else 255 warnx("kvm_nlist: %s", kvm_geterr(kd)); 256 exit(1); 257 } 258 259 if (todo & VMSTAT) { 260 struct winsize winsize; 261 262 /* 263 * Make sure that the userland devstat version matches the 264 * kernel devstat version. If not, exit and print a 265 * message informing the user of his mistake. 266 */ 267 if (devstat_checkversion(NULL) < 0) 268 errx(1, "%s", devstat_errbuf); 269 270 271 argv = getdrivedata(argv); 272 winsize.ws_row = 0; 273 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 274 if (winsize.ws_row > 0) 275 winlines = winsize.ws_row; 276 277 } 278 279#define BACKWARD_COMPATIBILITY 280#ifdef BACKWARD_COMPATIBILITY 281 if (*argv) { 282 interval = atoi(*argv); 283 if (*++argv) 284 reps = atoi(*argv); 285 } 286#endif 287 288 if (interval) { 289 if (!reps) 290 reps = -1; 291 } else if (reps) 292 interval = 1; 293 294 if (todo & FORKSTAT) 295 doforkst(); 296 if (todo & MEMSTAT) 297 domem(); 298 if (todo & ZMEMSTAT) 299 dozmem(); 300 if (todo & SUMSTAT) 301 dosum(); 302#ifdef notyet 303 if (todo & TIMESTAT) 304 dotimes(); 305#endif 306 if (todo & INTRSTAT) 307 dointr(); 308 if (todo & VMSTAT) 309 dovmstat(interval, reps); 310 exit(0); 311} 312 313static int 314mysysctl(const char *name, void *oldp, size_t *oldlenp, 315 void *newp, size_t newlen) 316{ 317 int error; 318 319 error = sysctlbyname(name, oldp, oldlenp, newp, newlen); 320 if (error != 0 && errno != ENOMEM) 321 err(1, "sysctl(%s)", name); 322 return (error); 323} 324 325static char ** 326getdrivedata(char **argv) 327{ 328 if ((num_devices = devstat_getnumdevs(NULL)) < 0) 329 errx(1, "%s", devstat_errbuf); 330 331 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 332 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 333 bzero(cur.dinfo, sizeof(struct devinfo)); 334 bzero(last.dinfo, sizeof(struct devinfo)); 335 336 if (devstat_getdevs(NULL, &cur) == -1) 337 errx(1, "%s", devstat_errbuf); 338 339 num_devices = cur.dinfo->numdevs; 340 generation = cur.dinfo->generation; 341 342 specified_devices = (char **)malloc(sizeof(char *)); 343 for (num_devices_specified = 0; *argv; ++argv) { 344 if (isdigit(**argv)) 345 break; 346 num_devices_specified++; 347 specified_devices = (char **)realloc(specified_devices, 348 sizeof(char *) * 349 num_devices_specified); 350 specified_devices[num_devices_specified - 1] = *argv; 351 } 352 dev_select = NULL; 353 354 if (nflag == 0 && maxshowdevs < num_devices_specified) 355 maxshowdevs = num_devices_specified; 356 357 /* 358 * People are generally only interested in disk statistics when 359 * they're running vmstat. So, that's what we're going to give 360 * them if they don't specify anything by default. We'll also give 361 * them any other random devices in the system so that we get to 362 * maxshowdevs devices, if that many devices exist. If the user 363 * specifies devices on the command line, either through a pattern 364 * match or by naming them explicitly, we will give the user only 365 * those devices. 366 */ 367 if ((num_devices_specified == 0) && (num_matches == 0)) { 368 if (devstat_buildmatch(da, &matches, &num_matches) != 0) 369 errx(1, "%s", devstat_errbuf); 370 371 select_mode = DS_SELECT_ADD; 372 } else 373 select_mode = DS_SELECT_ONLY; 374 375 /* 376 * At this point, selectdevs will almost surely indicate that the 377 * device list has changed, so we don't look for return values of 0 378 * or 1. If we get back -1, though, there is an error. 379 */ 380 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections, 381 &select_generation, generation, cur.dinfo->devices, 382 num_devices, matches, num_matches, specified_devices, 383 num_devices_specified, select_mode, 384 maxshowdevs, 0) == -1) 385 errx(1, "%s", devstat_errbuf); 386 387 return(argv); 388} 389 390static long 391getuptime(void) 392{ 393 static struct timeval boottime; 394 static time_t now; 395 time_t uptime; 396 397 if (boottime.tv_sec == 0) { 398 if (kd != NULL) { 399 kread(X_BOOTTIME, &boottime, sizeof(boottime)); 400 } else { 401 size_t size; 402 403 size = sizeof(boottime); 404 mysysctl("kern.boottime", &boottime, &size, NULL, 0); 405 if (size != sizeof(boottime)) 406 errx(1, "kern.boottime size mismatch"); 407 } 408 } 409 (void)time(&now); 410 uptime = now - boottime.tv_sec; 411 if (uptime <= 0 || uptime > 60*60*24*365*10) 412 errx(1, "time makes no sense; namelist must be wrong"); 413 return(uptime); 414} 415 416static void 417fill_vmmeter(struct vmmeter *vmmp) 418{ 419 if (kd != NULL) { 420 kread(X_SUM, vmmp, sizeof(*vmmp)); 421 } else { 422 size_t size = sizeof(unsigned int); 423#define GET_VM_STATS(cat, name) \ 424 mysysctl("vm.stats." #cat "." #name, &vmmp->name, &size, NULL, 0) 425 /* sys */ 426 GET_VM_STATS(sys, v_swtch); 427 GET_VM_STATS(sys, v_trap); 428 GET_VM_STATS(sys, v_syscall); 429 GET_VM_STATS(sys, v_intr); 430 GET_VM_STATS(sys, v_soft); 431 432 /* vm */ 433 GET_VM_STATS(vm, v_vm_faults); 434 GET_VM_STATS(vm, v_cow_faults); 435 GET_VM_STATS(vm, v_cow_optim); 436 GET_VM_STATS(vm, v_zfod); 437 GET_VM_STATS(vm, v_ozfod); 438 GET_VM_STATS(vm, v_swapin); 439 GET_VM_STATS(vm, v_swapout); 440 GET_VM_STATS(vm, v_swappgsin); 441 GET_VM_STATS(vm, v_swappgsout); 442 GET_VM_STATS(vm, v_vnodein); 443 GET_VM_STATS(vm, v_vnodeout); 444 GET_VM_STATS(vm, v_vnodepgsin); 445 GET_VM_STATS(vm, v_vnodepgsout); 446 GET_VM_STATS(vm, v_intrans); 447 GET_VM_STATS(vm, v_reactivated); 448 GET_VM_STATS(vm, v_pdwakeups); 449 GET_VM_STATS(vm, v_pdpages); 450 GET_VM_STATS(vm, v_dfree); 451 GET_VM_STATS(vm, v_pfree); 452 GET_VM_STATS(vm, v_tfree); 453 GET_VM_STATS(vm, v_page_size); 454 GET_VM_STATS(vm, v_page_count); 455 GET_VM_STATS(vm, v_free_reserved); 456 GET_VM_STATS(vm, v_free_target); 457 GET_VM_STATS(vm, v_free_min); 458 GET_VM_STATS(vm, v_free_count); 459 GET_VM_STATS(vm, v_wire_count); 460 GET_VM_STATS(vm, v_active_count); 461 GET_VM_STATS(vm, v_inactive_target); 462 GET_VM_STATS(vm, v_inactive_count); 463 GET_VM_STATS(vm, v_cache_count); 464 GET_VM_STATS(vm, v_cache_min); 465 GET_VM_STATS(vm, v_cache_max); 466 GET_VM_STATS(vm, v_pageout_free_min); 467 GET_VM_STATS(vm, v_interrupt_free_min); 468 /*GET_VM_STATS(vm, v_free_severe);*/ 469 GET_VM_STATS(vm, v_forks); 470 GET_VM_STATS(vm, v_vforks); 471 GET_VM_STATS(vm, v_rforks); 472 GET_VM_STATS(vm, v_kthreads); 473 GET_VM_STATS(vm, v_forkpages); 474 GET_VM_STATS(vm, v_vforkpages); 475 GET_VM_STATS(vm, v_rforkpages); 476 GET_VM_STATS(vm, v_kthreadpages); 477#undef GET_VM_STATS 478 } 479} 480 481static void 482fill_vmtotal(struct vmtotal *vmtp) 483{ 484 if (kd != NULL) { 485 /* XXX fill vmtp */ 486 errx(1, "not implemented"); 487 } else { 488 size_t size = sizeof(*vmtp); 489 mysysctl("vm.vmtotal", vmtp, &size, NULL, 0); 490 if (size != sizeof(*vmtp)) 491 errx(1, "vm.total size mismatch"); 492 } 493} 494 495static int hz, hdrcnt; 496 497static void 498dovmstat(unsigned int interval, int reps) 499{ 500 struct vmtotal total; 501 time_t uptime, halfuptime; 502 struct devinfo *tmp_dinfo; 503 size_t size; 504 505 uptime = getuptime(); 506 halfuptime = uptime / 2; 507 (void)signal(SIGCONT, needhdr); 508 509 if (kd != NULL) { 510 if (namelist[X_STATHZ].n_type != 0 && 511 namelist[X_STATHZ].n_value != 0) 512 kread(X_STATHZ, &hz, sizeof(hz)); 513 if (!hz) 514 kread(X_HZ, &hz, sizeof(hz)); 515 } else { 516 struct clockinfo clockrate; 517 518 size = sizeof(clockrate); 519 mysysctl("kern.clockrate", &clockrate, &size, NULL, 0); 520 if (size != sizeof(clockrate)) 521 errx(1, "clockrate size mismatch"); 522 hz = clockrate.hz; 523 } 524 525 for (hdrcnt = 1;;) { 526 if (!--hdrcnt) 527 printhdr(); 528 if (kd != NULL) { 529 kread(X_CPTIME, cur.cp_time, sizeof(cur.cp_time)); 530 } else { 531 size = sizeof(cur.cp_time); 532 mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0); 533 if (size != sizeof(cur.cp_time)) 534 errx(1, "cp_time size mismatch"); 535 } 536 537 tmp_dinfo = last.dinfo; 538 last.dinfo = cur.dinfo; 539 cur.dinfo = tmp_dinfo; 540 last.snap_time = cur.snap_time; 541 542 /* 543 * Here what we want to do is refresh our device stats. 544 * getdevs() returns 1 when the device list has changed. 545 * If the device list has changed, we want to go through 546 * the selection process again, in case a device that we 547 * were previously displaying has gone away. 548 */ 549 switch (devstat_getdevs(NULL, &cur)) { 550 case -1: 551 errx(1, "%s", devstat_errbuf); 552 break; 553 case 1: { 554 int retval; 555 556 num_devices = cur.dinfo->numdevs; 557 generation = cur.dinfo->generation; 558 559 retval = devstat_selectdevs(&dev_select, &num_selected, 560 &num_selections, &select_generation, 561 generation, cur.dinfo->devices, 562 num_devices, matches, num_matches, 563 specified_devices, 564 num_devices_specified, select_mode, 565 maxshowdevs, 0); 566 switch (retval) { 567 case -1: 568 errx(1, "%s", devstat_errbuf); 569 break; 570 case 1: 571 printhdr(); 572 break; 573 default: 574 break; 575 } 576 } 577 default: 578 break; 579 } 580 581 fill_vmmeter(&sum); 582 fill_vmtotal(&total); 583 (void)printf("%2d %1d %1d", 584 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); 585#define vmstat_pgtok(a) ((a) * sum.v_page_size >> 10) 586#define rate(x) (((x) + halfuptime) / uptime) /* round */ 587 (void)printf(" %7ld %6ld ", (long)vmstat_pgtok(total.t_avm), 588 (long)vmstat_pgtok(total.t_free)); 589 (void)printf("%4lu ", 590 (unsigned long)rate(sum.v_vm_faults - osum.v_vm_faults)); 591 (void)printf("%3lu ", 592 (unsigned long)rate(sum.v_reactivated - osum.v_reactivated)); 593 (void)printf("%3lu ", 594 (unsigned long)rate(sum.v_swapin + sum.v_vnodein - 595 (osum.v_swapin + osum.v_vnodein))); 596 (void)printf("%3lu ", 597 (unsigned long)rate(sum.v_swapout + sum.v_vnodeout - 598 (osum.v_swapout + osum.v_vnodeout))); 599 (void)printf("%3lu ", 600 (unsigned long)rate(sum.v_tfree - osum.v_tfree)); 601 (void)printf("%3lu ", 602 (unsigned long)rate(sum.v_pdpages - osum.v_pdpages)); 603 devstats(); 604 (void)printf("%4lu %4lu %3lu ", 605 (unsigned long)rate(sum.v_intr - osum.v_intr), 606 (unsigned long)rate(sum.v_syscall - osum.v_syscall), 607 (unsigned long)rate(sum.v_swtch - osum.v_swtch)); 608 cpustats(); 609 (void)printf("\n"); 610 (void)fflush(stdout); 611 if (reps >= 0 && --reps <= 0) 612 break; 613 osum = sum; 614 uptime = interval; 615 /* 616 * We round upward to avoid losing low-frequency events 617 * (i.e., >= 1 per interval but < 1 per second). 618 */ 619 if (interval != 1) 620 halfuptime = (uptime + 1) / 2; 621 else 622 halfuptime = 0; 623 (void)sleep(interval); 624 } 625} 626 627static void 628printhdr(void) 629{ 630 int i, num_shown; 631 632 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs; 633 (void)printf(" procs memory page%*s", 19, ""); 634 if (num_shown > 1) 635 (void)printf(" disks %*s", num_shown * 4 - 7, ""); 636 else if (num_shown == 1) 637 (void)printf("disk"); 638 (void)printf(" faults cpu\n"); 639 (void)printf(" r b w avm fre flt re pi po fr sr "); 640 for (i = 0; i < num_devices; i++) 641 if ((dev_select[i].selected) 642 && (dev_select[i].selected <= maxshowdevs)) 643 (void)printf("%c%c%d ", dev_select[i].device_name[0], 644 dev_select[i].device_name[1], 645 dev_select[i].unit_number); 646 (void)printf(" in sy cs us sy id\n"); 647 hdrcnt = winlines - 2; 648} 649 650/* 651 * Force a header to be prepended to the next output. 652 */ 653static void 654needhdr(int dummy __unused) 655{ 656 657 hdrcnt = 1; 658} 659 660#ifdef notyet 661static void 662dotimes(void) 663{ 664 unsigned int pgintime, rectime; 665 666 kread(X_REC, &rectime, sizeof(rectime)); 667 kread(X_PGIN, &pgintime, sizeof(pgintime)); 668 kread(X_SUM, &sum, sizeof(sum)); 669 (void)printf("%u reclaims, %u total time (usec)\n", 670 sum.v_pgrec, rectime); 671 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); 672 (void)printf("\n"); 673 (void)printf("%u page ins, %u total time (msec)\n", 674 sum.v_pgin, pgintime / 10); 675 (void)printf("average: %8.1f msec / page in\n", 676 pgintime / (sum.v_pgin * 10.0)); 677} 678#endif 679 680static long 681pct(long top, long bot) 682{ 683 long ans; 684 685 if (bot == 0) 686 return(0); 687 ans = (quad_t)top * 100 / bot; 688 return (ans); 689} 690 691#define PCT(top, bot) pct((long)(top), (long)(bot)) 692 693static void 694dosum(void) 695{ 696 struct nchstats lnchstats; 697 long nchtotal; 698 699 fill_vmmeter(&sum); 700 (void)printf("%9u cpu context switches\n", sum.v_swtch); 701 (void)printf("%9u device interrupts\n", sum.v_intr); 702 (void)printf("%9u software interrupts\n", sum.v_soft); 703 (void)printf("%9u traps\n", sum.v_trap); 704 (void)printf("%9u system calls\n", sum.v_syscall); 705 (void)printf("%9u kernel threads created\n", sum.v_kthreads); 706 (void)printf("%9u fork() calls\n", sum.v_forks); 707 (void)printf("%9u vfork() calls\n", sum.v_vforks); 708 (void)printf("%9u rfork() calls\n", sum.v_rforks); 709 (void)printf("%9u swap pager pageins\n", sum.v_swapin); 710 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin); 711 (void)printf("%9u swap pager pageouts\n", sum.v_swapout); 712 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout); 713 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein); 714 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin); 715 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout); 716 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout); 717 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups); 718 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages); 719 (void)printf("%9u pages reactivated\n", sum.v_reactivated); 720 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults); 721 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim); 722 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod); 723 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod); 724 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); 725 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults); 726 (void)printf("%9u pages affected by kernel thread creation\n", sum.v_kthreadpages); 727 (void)printf("%9u pages affected by fork()\n", sum.v_forkpages); 728 (void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages); 729 (void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages); 730 (void)printf("%9u pages freed\n", sum.v_tfree); 731 (void)printf("%9u pages freed by daemon\n", sum.v_dfree); 732 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree); 733 (void)printf("%9u pages active\n", sum.v_active_count); 734 (void)printf("%9u pages inactive\n", sum.v_inactive_count); 735 (void)printf("%9u pages in VM cache\n", sum.v_cache_count); 736 (void)printf("%9u pages wired down\n", sum.v_wire_count); 737 (void)printf("%9u pages free\n", sum.v_free_count); 738 (void)printf("%9u bytes per page\n", sum.v_page_size); 739 if (kd != NULL) { 740 kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats)); 741 } else { 742 size_t size = sizeof(lnchstats); 743 mysysctl("vfs.cache.nchstats", &lnchstats, &size, NULL, 0); 744 if (size != sizeof(lnchstats)) 745 errx(1, "vfs.cache.nchstats size mismatch"); 746 } 747 nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits + 748 lnchstats.ncs_badhits + lnchstats.ncs_falsehits + 749 lnchstats.ncs_miss + lnchstats.ncs_long; 750 (void)printf("%9ld total name lookups\n", nchtotal); 751 (void)printf( 752 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n", 753 "", PCT(lnchstats.ncs_goodhits, nchtotal), 754 PCT(lnchstats.ncs_neghits, nchtotal), 755 PCT(lnchstats.ncs_pass2, nchtotal)); 756 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "", 757 PCT(lnchstats.ncs_badhits, nchtotal), 758 PCT(lnchstats.ncs_falsehits, nchtotal), 759 PCT(lnchstats.ncs_long, nchtotal)); 760} 761 762static void 763doforkst(void) 764{ 765 fill_vmmeter(&sum); 766 (void)printf("%u forks, %u pages, average %.2f\n", 767 sum.v_forks, sum.v_forkpages, 768 sum.v_forks == 0 ? 0.0 : 769 (double)sum.v_forkpages / sum.v_forks); 770 (void)printf("%u vforks, %u pages, average %.2f\n", 771 sum.v_vforks, sum.v_vforkpages, 772 sum.v_vforks == 0 ? 0.0 : 773 (double)sum.v_vforkpages / sum.v_vforks); 774 (void)printf("%u rforks, %u pages, average %.2f\n", 775 sum.v_rforks, sum.v_rforkpages, 776 sum.v_rforks == 0 ? 0.0 : 777 (double)sum.v_rforkpages / sum.v_rforks); 778} 779 780static void 781devstats(void) 782{ 783 int dn, state; 784 long double transfers_per_second; 785 long double busy_seconds; 786 long tmp; 787 788 for (state = 0; state < CPUSTATES; ++state) { 789 tmp = cur.cp_time[state]; 790 cur.cp_time[state] -= last.cp_time[state]; 791 last.cp_time[state] = tmp; 792 } 793 794 busy_seconds = cur.snap_time - last.snap_time; 795 796 for (dn = 0; dn < num_devices; dn++) { 797 int di; 798 799 if ((dev_select[dn].selected == 0) 800 || (dev_select[dn].selected > maxshowdevs)) 801 continue; 802 803 di = dev_select[dn].position; 804 805 if (devstat_compute_statistics(&cur.dinfo->devices[di], 806 &last.dinfo->devices[di], busy_seconds, 807 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 808 DSM_NONE) != 0) 809 errx(1, "%s", devstat_errbuf); 810 811 (void)printf("%3.0Lf ", transfers_per_second); 812 } 813} 814 815static void 816cpustats(void) 817{ 818 int state; 819 double lpct, total; 820 821 total = 0; 822 for (state = 0; state < CPUSTATES; ++state) 823 total += cur.cp_time[state]; 824 if (total) 825 lpct = 100.0 / total; 826 else 827 lpct = 0.0; 828 (void)printf("%2.0f ", (cur.cp_time[CP_USER] + 829 cur.cp_time[CP_NICE]) * lpct); 830 (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + 831 cur.cp_time[CP_INTR]) * lpct); 832 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * lpct); 833} 834 835static void 836dointr(void) 837{ 838 unsigned long *intrcnt, uptime; 839 uint64_t inttotal; 840 size_t clen, inamlen, intrcntlen, istrnamlen; 841 unsigned int i, nintr; 842 char *intrname, *tintrname; 843 844 uptime = getuptime(); 845 if (kd != NULL) { 846 intrcntlen = namelist[X_EINTRCNT].n_value - 847 namelist[X_INTRCNT].n_value; 848 inamlen = namelist[X_EINTRNAMES].n_value - 849 namelist[X_INTRNAMES].n_value; 850 if ((intrcnt = malloc(intrcntlen)) == NULL || 851 (intrname = malloc(inamlen)) == NULL) 852 err(1, "malloc()"); 853 kread(X_INTRCNT, intrcnt, intrcntlen); 854 kread(X_INTRNAMES, intrname, inamlen); 855 } else { 856 for (intrcnt = NULL, intrcntlen = 1024; ; intrcntlen *= 2) { 857 if ((intrcnt = reallocf(intrcnt, intrcntlen)) == NULL) 858 err(1, "reallocf()"); 859 if (mysysctl("hw.intrcnt", 860 intrcnt, &intrcntlen, NULL, 0) == 0) 861 break; 862 } 863 for (intrname = NULL, inamlen = 1024; ; inamlen *= 2) { 864 if ((intrname = reallocf(intrname, inamlen)) == NULL) 865 err(1, "reallocf()"); 866 if (mysysctl("hw.intrnames", 867 intrname, &inamlen, NULL, 0) == 0) 868 break; 869 } 870 } 871 nintr = intrcntlen / sizeof(unsigned long); 872 tintrname = intrname; 873 istrnamlen = strlen("interrupt"); 874 for (i = 0; i < nintr; i++) { 875 clen = strlen(tintrname); 876 if (clen > istrnamlen) 877 istrnamlen = clen; 878 tintrname += clen + 1; 879 } 880 (void)printf("%-*s %20s %10s\n", istrnamlen, "interrupt", "total", 881 "rate"); 882 inttotal = 0; 883 for (i = 0; i < nintr; i++) { 884 if (intrname[0] != '\0' && (*intrcnt != 0 || aflag)) 885 (void)printf("%-*s %20lu %10lu\n", istrnamlen, intrname, 886 *intrcnt, *intrcnt / uptime); 887 intrname += strlen(intrname) + 1; 888 inttotal += *intrcnt++; 889 } 890 (void)printf("%-*s %20llu %10llu\n", istrnamlen, "Total", 891 (long long)inttotal, (long long)(inttotal / uptime)); 892} 893 894static void 895domem(void) 896{ 897 if (kd != NULL) 898 errx(1, "not implemented"); 899 dosysctl("kern.malloc"); 900} 901 902static void 903dozmem(void) 904{ 905 if (kd != NULL) 906 errx(1, "not implemented"); 907 dosysctl("vm.zone"); 908} 909 910static void 911dosysctl(const char *name) 912{ 913 char *buf; 914 size_t bufsize; 915 916 for (buf = NULL, bufsize = 1024; ; bufsize *= 2) { 917 if ((buf = realloc(buf, bufsize)) == NULL) 918 err(1, "realloc()"); 919 bufsize--; /* Leave space for the kern.malloc fixup. */ 920 if (mysysctl(name, buf, &bufsize, NULL, 0) == 0) 921 break; 922 } 923 buf[bufsize] = '\0'; /* Fix up kern.malloc not returning a string. */ 924 (void)printf("%s", buf); 925 free(buf); 926} 927 928/* 929 * kread reads something from the kernel, given its nlist index. 930 */ 931static void 932kread(int nlx, void *addr, size_t size) 933{ 934 const char *sym; 935 936 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 937 sym = namelist[nlx].n_name; 938 if (*sym == '_') 939 ++sym; 940 errx(1, "symbol %s not defined", sym); 941 } 942 if ((size_t)kvm_read(kd, namelist[nlx].n_value, addr, size) != size) { 943 sym = namelist[nlx].n_name; 944 if (*sym == '_') 945 ++sym; 946 errx(1, "%s: %s", sym, kvm_geterr(kd)); 947 } 948} 949 950static void 951usage(void) 952{ 953 (void)fprintf(stderr, "%s%s", 954 "usage: vmstat [-aimsz] [-c count] [-M core [-N system]] [-w wait]\n", 955 " [-n devs] [disks]\n"); 956 exit(1); 957} 958