vmstat.c revision 164556
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 164556 2006-11-23 11:47:34Z ru $"); 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 <inttypes.h> 69#include <kvm.h> 70#include <limits.h> 71#include <memstat.h> 72#include <nlist.h> 73#include <paths.h> 74#include <stdio.h> 75#include <stdlib.h> 76#include <string.h> 77#include <sysexits.h> 78#include <time.h> 79#include <unistd.h> 80 81static char da[] = "da"; 82 83static struct nlist namelist[] = { 84#define X_CPTIME 0 85 { "_cp_time" }, 86#define X_SUM 1 87 { "_cnt" }, 88#define X_BOOTTIME 2 89 { "_boottime" }, 90#define X_HZ 3 91 { "_hz" }, 92#define X_STATHZ 4 93 { "_stathz" }, 94#define X_NCHSTATS 5 95 { "_nchstats" }, 96#define X_INTRNAMES 6 97 { "_intrnames" }, 98#define X_EINTRNAMES 7 99 { "_eintrnames" }, 100#define X_INTRCNT 8 101 { "_intrcnt" }, 102#define X_EINTRCNT 9 103 { "_eintrcnt" }, 104#define X_KMEMSTATS 10 105 { "_kmemstatistics" }, 106#define X_KMEMZONES 11 107 { "_kmemzones" }, 108#ifdef notyet 109#define X_DEFICIT XXX 110 { "_deficit" }, 111#define X_REC XXX 112 { "_rectime" }, 113#define X_PGIN XXX 114 { "_pgintime" }, 115#define X_XSTATS XXX 116 { "_xstats" }, 117#define X_END XXX 118#else 119#define X_END 12 120#endif 121 { "" }, 122}; 123 124static struct statinfo cur, last; 125static int num_devices, maxshowdevs; 126static long generation; 127static struct device_selection *dev_select; 128static int num_selected; 129static struct devstat_match *matches; 130static int num_matches = 0; 131static int num_devices_specified, num_selections; 132static long select_generation; 133static char **specified_devices; 134static devstat_select_mode select_mode; 135 136static struct vmmeter sum, osum; 137 138static int winlines = 20; 139static int aflag; 140static int nflag; 141 142static kvm_t *kd; 143 144#define FORKSTAT 0x01 145#define INTRSTAT 0x02 146#define MEMSTAT 0x04 147#define SUMSTAT 0x08 148#define TIMESTAT 0x10 149#define VMSTAT 0x20 150#define ZMEMSTAT 0x40 151 152static void cpustats(void); 153static void devstats(void); 154static void doforkst(void); 155static void dointr(void); 156static void dosum(void); 157static void dovmstat(unsigned int, int); 158static void domemstat_malloc(void); 159static void domemstat_zone(void); 160static void kread(int, void *, size_t); 161static void kreado(int, void *, size_t, size_t); 162static char *kgetstr(const char *); 163static void needhdr(int); 164static void printhdr(void); 165static void usage(void); 166 167static long pct(long, long); 168static long getuptime(void); 169 170static char **getdrivedata(char **); 171 172int 173main(int argc, char *argv[]) 174{ 175 int c, todo; 176 unsigned int interval; 177 int reps; 178 char *memf, *nlistf; 179 char errbuf[_POSIX2_LINE_MAX]; 180 181 memf = nlistf = NULL; 182 interval = reps = todo = 0; 183 maxshowdevs = 2; 184 while ((c = getopt(argc, argv, "ac:fiM:mN:n:p:stw:z")) != -1) { 185 switch (c) { 186 case 'a': 187 aflag++; 188 break; 189 case 'c': 190 reps = atoi(optarg); 191 break; 192 case 'f': 193 todo |= FORKSTAT; 194 break; 195 case 'i': 196 todo |= INTRSTAT; 197 break; 198 case 'M': 199 memf = optarg; 200 break; 201 case 'm': 202 todo |= MEMSTAT; 203 break; 204 case 'N': 205 nlistf = optarg; 206 break; 207 case 'n': 208 nflag = 1; 209 maxshowdevs = atoi(optarg); 210 if (maxshowdevs < 0) 211 errx(1, "number of devices %d is < 0", 212 maxshowdevs); 213 break; 214 case 'p': 215 if (devstat_buildmatch(optarg, &matches, &num_matches) != 0) 216 errx(1, "%s", devstat_errbuf); 217 break; 218 case 's': 219 todo |= SUMSTAT; 220 break; 221 case 't': 222#ifdef notyet 223 todo |= TIMESTAT; 224#else 225 errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); 226#endif 227 break; 228 case 'w': 229 interval = atoi(optarg); 230 break; 231 case 'z': 232 todo |= ZMEMSTAT; 233 break; 234 case '?': 235 default: 236 usage(); 237 } 238 } 239 argc -= optind; 240 argv += optind; 241 242 if (todo == 0) 243 todo = VMSTAT; 244 245 if (memf != NULL) { 246 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 247 if (kd == NULL) 248 errx(1, "kvm_openfiles: %s", errbuf); 249 } 250 251 if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) { 252 if (c > 0) { 253 warnx("undefined symbols:"); 254 for (c = 0; 255 c < (int)(sizeof(namelist)/sizeof(namelist[0])); 256 c++) 257 if (namelist[c].n_type == 0) 258 (void)fprintf(stderr, " %s", 259 namelist[c].n_name); 260 (void)fputc('\n', stderr); 261 } else 262 warnx("kvm_nlist: %s", kvm_geterr(kd)); 263 exit(1); 264 } 265 266 if (todo & VMSTAT) { 267 struct winsize winsize; 268 269 /* 270 * Make sure that the userland devstat version matches the 271 * kernel devstat version. If not, exit and print a 272 * message informing the user of his mistake. 273 */ 274 if (devstat_checkversion(NULL) < 0) 275 errx(1, "%s", devstat_errbuf); 276 277 278 argv = getdrivedata(argv); 279 winsize.ws_row = 0; 280 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 281 if (winsize.ws_row > 0) 282 winlines = winsize.ws_row; 283 284 } 285 286#define BACKWARD_COMPATIBILITY 287#ifdef BACKWARD_COMPATIBILITY 288 if (*argv) { 289 interval = atoi(*argv); 290 if (*++argv) 291 reps = atoi(*argv); 292 } 293#endif 294 295 if (interval) { 296 if (!reps) 297 reps = -1; 298 } else if (reps) 299 interval = 1; 300 301 if (todo & FORKSTAT) 302 doforkst(); 303 if (todo & MEMSTAT) 304 domemstat_malloc(); 305 if (todo & ZMEMSTAT) 306 domemstat_zone(); 307 if (todo & SUMSTAT) 308 dosum(); 309#ifdef notyet 310 if (todo & TIMESTAT) 311 dotimes(); 312#endif 313 if (todo & INTRSTAT) 314 dointr(); 315 if (todo & VMSTAT) 316 dovmstat(interval, reps); 317 exit(0); 318} 319 320static int 321mysysctl(const char *name, void *oldp, size_t *oldlenp, 322 void *newp, size_t newlen) 323{ 324 int error; 325 326 error = sysctlbyname(name, oldp, oldlenp, newp, newlen); 327 if (error != 0 && errno != ENOMEM) 328 err(1, "sysctl(%s)", name); 329 return (error); 330} 331 332static char ** 333getdrivedata(char **argv) 334{ 335 if ((num_devices = devstat_getnumdevs(NULL)) < 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 (devstat_getdevs(NULL, &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 (devstat_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 (devstat_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 397static long 398getuptime(void) 399{ 400 struct timespec sp; 401 time_t uptime; 402 403 (void)clock_gettime(CLOCK_MONOTONIC, &sp); 404 uptime = sp.tv_sec; 405 if (uptime <= 0 || uptime > 60*60*24*365*10) 406 errx(1, "time makes no sense; namelist must be wrong"); 407 408 return(uptime); 409} 410 411static void 412fill_vmmeter(struct vmmeter *vmmp) 413{ 414 if (kd != NULL) { 415 kread(X_SUM, vmmp, sizeof(*vmmp)); 416 } else { 417 size_t size = sizeof(unsigned int); 418#define GET_VM_STATS(cat, name) \ 419 mysysctl("vm.stats." #cat "." #name, &vmmp->name, &size, NULL, 0) 420 /* sys */ 421 GET_VM_STATS(sys, v_swtch); 422 GET_VM_STATS(sys, v_trap); 423 GET_VM_STATS(sys, v_syscall); 424 GET_VM_STATS(sys, v_intr); 425 GET_VM_STATS(sys, v_soft); 426 427 /* vm */ 428 GET_VM_STATS(vm, v_vm_faults); 429 GET_VM_STATS(vm, v_cow_faults); 430 GET_VM_STATS(vm, v_cow_optim); 431 GET_VM_STATS(vm, v_zfod); 432 GET_VM_STATS(vm, v_ozfod); 433 GET_VM_STATS(vm, v_swapin); 434 GET_VM_STATS(vm, v_swapout); 435 GET_VM_STATS(vm, v_swappgsin); 436 GET_VM_STATS(vm, v_swappgsout); 437 GET_VM_STATS(vm, v_vnodein); 438 GET_VM_STATS(vm, v_vnodeout); 439 GET_VM_STATS(vm, v_vnodepgsin); 440 GET_VM_STATS(vm, v_vnodepgsout); 441 GET_VM_STATS(vm, v_intrans); 442 GET_VM_STATS(vm, v_reactivated); 443 GET_VM_STATS(vm, v_pdwakeups); 444 GET_VM_STATS(vm, v_pdpages); 445 GET_VM_STATS(vm, v_dfree); 446 GET_VM_STATS(vm, v_pfree); 447 GET_VM_STATS(vm, v_tfree); 448 GET_VM_STATS(vm, v_page_size); 449 GET_VM_STATS(vm, v_page_count); 450 GET_VM_STATS(vm, v_free_reserved); 451 GET_VM_STATS(vm, v_free_target); 452 GET_VM_STATS(vm, v_free_min); 453 GET_VM_STATS(vm, v_free_count); 454 GET_VM_STATS(vm, v_wire_count); 455 GET_VM_STATS(vm, v_active_count); 456 GET_VM_STATS(vm, v_inactive_target); 457 GET_VM_STATS(vm, v_inactive_count); 458 GET_VM_STATS(vm, v_cache_count); 459 GET_VM_STATS(vm, v_cache_min); 460 GET_VM_STATS(vm, v_cache_max); 461 GET_VM_STATS(vm, v_pageout_free_min); 462 GET_VM_STATS(vm, v_interrupt_free_min); 463 /*GET_VM_STATS(vm, v_free_severe);*/ 464 GET_VM_STATS(vm, v_forks); 465 GET_VM_STATS(vm, v_vforks); 466 GET_VM_STATS(vm, v_rforks); 467 GET_VM_STATS(vm, v_kthreads); 468 GET_VM_STATS(vm, v_forkpages); 469 GET_VM_STATS(vm, v_vforkpages); 470 GET_VM_STATS(vm, v_rforkpages); 471 GET_VM_STATS(vm, v_kthreadpages); 472#undef GET_VM_STATS 473 } 474} 475 476static void 477fill_vmtotal(struct vmtotal *vmtp) 478{ 479 if (kd != NULL) { 480 /* XXX fill vmtp */ 481 errx(1, "not implemented"); 482 } else { 483 size_t size = sizeof(*vmtp); 484 mysysctl("vm.vmtotal", vmtp, &size, NULL, 0); 485 if (size != sizeof(*vmtp)) 486 errx(1, "vm.total size mismatch"); 487 } 488} 489 490static int hz, hdrcnt; 491 492static void 493dovmstat(unsigned int interval, int reps) 494{ 495 struct vmtotal total; 496 time_t uptime, halfuptime; 497 struct devinfo *tmp_dinfo; 498 size_t size; 499 500 uptime = getuptime(); 501 halfuptime = uptime / 2; 502 (void)signal(SIGCONT, needhdr); 503 504 if (kd != NULL) { 505 if (namelist[X_STATHZ].n_type != 0 && 506 namelist[X_STATHZ].n_value != 0) 507 kread(X_STATHZ, &hz, sizeof(hz)); 508 if (!hz) 509 kread(X_HZ, &hz, sizeof(hz)); 510 } else { 511 struct clockinfo clockrate; 512 513 size = sizeof(clockrate); 514 mysysctl("kern.clockrate", &clockrate, &size, NULL, 0); 515 if (size != sizeof(clockrate)) 516 errx(1, "clockrate size mismatch"); 517 hz = clockrate.hz; 518 } 519 520 for (hdrcnt = 1;;) { 521 if (!--hdrcnt) 522 printhdr(); 523 if (kd != NULL) { 524 kread(X_CPTIME, cur.cp_time, sizeof(cur.cp_time)); 525 } else { 526 size = sizeof(cur.cp_time); 527 mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0); 528 if (size != sizeof(cur.cp_time)) 529 errx(1, "cp_time size mismatch"); 530 } 531 532 tmp_dinfo = last.dinfo; 533 last.dinfo = cur.dinfo; 534 cur.dinfo = tmp_dinfo; 535 last.snap_time = cur.snap_time; 536 537 /* 538 * Here what we want to do is refresh our device stats. 539 * getdevs() returns 1 when the device list has changed. 540 * If the device list has changed, we want to go through 541 * the selection process again, in case a device that we 542 * were previously displaying has gone away. 543 */ 544 switch (devstat_getdevs(NULL, &cur)) { 545 case -1: 546 errx(1, "%s", devstat_errbuf); 547 break; 548 case 1: { 549 int retval; 550 551 num_devices = cur.dinfo->numdevs; 552 generation = cur.dinfo->generation; 553 554 retval = devstat_selectdevs(&dev_select, &num_selected, 555 &num_selections, &select_generation, 556 generation, cur.dinfo->devices, 557 num_devices, matches, num_matches, 558 specified_devices, 559 num_devices_specified, select_mode, 560 maxshowdevs, 0); 561 switch (retval) { 562 case -1: 563 errx(1, "%s", devstat_errbuf); 564 break; 565 case 1: 566 printhdr(); 567 break; 568 default: 569 break; 570 } 571 } 572 default: 573 break; 574 } 575 576 fill_vmmeter(&sum); 577 fill_vmtotal(&total); 578 (void)printf("%2u %1u %1u", 579 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); 580#define vmstat_pgtok(a) ((uintmax_t)(a) * (sum.v_page_size >> 10)) 581#define rate(x) (((x) + halfuptime) / uptime) /* round */ 582 (void)printf(" %7ju %6ju ", vmstat_pgtok(total.t_avm), 583 vmstat_pgtok(total.t_free)); 584 (void)printf("%5lu ", 585 (unsigned long)rate(sum.v_vm_faults - osum.v_vm_faults)); 586 (void)printf("%3lu ", 587 (unsigned long)rate(sum.v_reactivated - osum.v_reactivated)); 588 (void)printf("%3lu ", 589 (unsigned long)rate(sum.v_swapin + sum.v_vnodein - 590 (osum.v_swapin + osum.v_vnodein))); 591 (void)printf("%3lu ", 592 (unsigned long)rate(sum.v_swapout + sum.v_vnodeout - 593 (osum.v_swapout + osum.v_vnodeout))); 594 (void)printf("%5lu ", 595 (unsigned long)rate(sum.v_tfree - osum.v_tfree)); 596 (void)printf("%3lu ", 597 (unsigned long)rate(sum.v_pdpages - osum.v_pdpages)); 598 devstats(); 599 (void)printf("%4lu %4lu %4lu ", 600 (unsigned long)rate(sum.v_intr - osum.v_intr), 601 (unsigned long)rate(sum.v_syscall - osum.v_syscall), 602 (unsigned long)rate(sum.v_swtch - osum.v_swtch)); 603 cpustats(); 604 (void)printf("\n"); 605 (void)fflush(stdout); 606 if (reps >= 0 && --reps <= 0) 607 break; 608 osum = sum; 609 uptime = interval; 610 /* 611 * We round upward to avoid losing low-frequency events 612 * (i.e., >= 1 per interval but < 1 per second). 613 */ 614 if (interval != 1) 615 halfuptime = (uptime + 1) / 2; 616 else 617 halfuptime = 0; 618 (void)sleep(interval); 619 } 620} 621 622static void 623printhdr(void) 624{ 625 int i, num_shown; 626 627 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs; 628 (void)printf(" procs memory page%*s", 19, ""); 629 if (num_shown > 1) 630 (void)printf(" disks %*s", num_shown * 4 - 7, ""); 631 else if (num_shown == 1) 632 (void)printf("disk"); 633 (void)printf(" faults cpu\n"); 634 (void)printf(" r b w avm fre flt re pi po fr sr "); 635 for (i = 0; i < num_devices; i++) 636 if ((dev_select[i].selected) 637 && (dev_select[i].selected <= maxshowdevs)) 638 (void)printf("%c%c%d ", dev_select[i].device_name[0], 639 dev_select[i].device_name[1], 640 dev_select[i].unit_number); 641 (void)printf(" in sy cs us sy id\n"); 642 hdrcnt = winlines - 2; 643} 644 645/* 646 * Force a header to be prepended to the next output. 647 */ 648static void 649needhdr(int dummy __unused) 650{ 651 652 hdrcnt = 1; 653} 654 655#ifdef notyet 656static void 657dotimes(void) 658{ 659 unsigned int pgintime, rectime; 660 661 kread(X_REC, &rectime, sizeof(rectime)); 662 kread(X_PGIN, &pgintime, sizeof(pgintime)); 663 kread(X_SUM, &sum, sizeof(sum)); 664 (void)printf("%u reclaims, %u total time (usec)\n", 665 sum.v_pgrec, rectime); 666 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); 667 (void)printf("\n"); 668 (void)printf("%u page ins, %u total time (msec)\n", 669 sum.v_pgin, pgintime / 10); 670 (void)printf("average: %8.1f msec / page in\n", 671 pgintime / (sum.v_pgin * 10.0)); 672} 673#endif 674 675static long 676pct(long top, long bot) 677{ 678 long ans; 679 680 if (bot == 0) 681 return(0); 682 ans = (quad_t)top * 100 / bot; 683 return (ans); 684} 685 686#define PCT(top, bot) pct((long)(top), (long)(bot)) 687 688static void 689dosum(void) 690{ 691 struct nchstats lnchstats; 692 long nchtotal; 693 694 fill_vmmeter(&sum); 695 (void)printf("%9u cpu context switches\n", sum.v_swtch); 696 (void)printf("%9u device interrupts\n", sum.v_intr); 697 (void)printf("%9u software interrupts\n", sum.v_soft); 698 (void)printf("%9u traps\n", sum.v_trap); 699 (void)printf("%9u system calls\n", sum.v_syscall); 700 (void)printf("%9u kernel threads created\n", sum.v_kthreads); 701 (void)printf("%9u fork() calls\n", sum.v_forks); 702 (void)printf("%9u vfork() calls\n", sum.v_vforks); 703 (void)printf("%9u rfork() calls\n", sum.v_rforks); 704 (void)printf("%9u swap pager pageins\n", sum.v_swapin); 705 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin); 706 (void)printf("%9u swap pager pageouts\n", sum.v_swapout); 707 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout); 708 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein); 709 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin); 710 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout); 711 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout); 712 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups); 713 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages); 714 (void)printf("%9u pages reactivated\n", sum.v_reactivated); 715 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults); 716 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim); 717 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod); 718 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod); 719 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); 720 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults); 721 (void)printf("%9u pages affected by kernel thread creation\n", sum.v_kthreadpages); 722 (void)printf("%9u pages affected by fork()\n", sum.v_forkpages); 723 (void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages); 724 (void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages); 725 (void)printf("%9u pages freed\n", sum.v_tfree); 726 (void)printf("%9u pages freed by daemon\n", sum.v_dfree); 727 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree); 728 (void)printf("%9u pages active\n", sum.v_active_count); 729 (void)printf("%9u pages inactive\n", sum.v_inactive_count); 730 (void)printf("%9u pages in VM cache\n", sum.v_cache_count); 731 (void)printf("%9u pages wired down\n", sum.v_wire_count); 732 (void)printf("%9u pages free\n", sum.v_free_count); 733 (void)printf("%9u bytes per page\n", sum.v_page_size); 734 if (kd != NULL) { 735 kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats)); 736 } else { 737 size_t size = sizeof(lnchstats); 738 mysysctl("vfs.cache.nchstats", &lnchstats, &size, NULL, 0); 739 if (size != sizeof(lnchstats)) 740 errx(1, "vfs.cache.nchstats size mismatch"); 741 } 742 nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits + 743 lnchstats.ncs_badhits + lnchstats.ncs_falsehits + 744 lnchstats.ncs_miss + lnchstats.ncs_long; 745 (void)printf("%9ld total name lookups\n", nchtotal); 746 (void)printf( 747 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n", 748 "", PCT(lnchstats.ncs_goodhits, nchtotal), 749 PCT(lnchstats.ncs_neghits, nchtotal), 750 PCT(lnchstats.ncs_pass2, nchtotal)); 751 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "", 752 PCT(lnchstats.ncs_badhits, nchtotal), 753 PCT(lnchstats.ncs_falsehits, nchtotal), 754 PCT(lnchstats.ncs_long, nchtotal)); 755} 756 757static void 758doforkst(void) 759{ 760 fill_vmmeter(&sum); 761 (void)printf("%u forks, %u pages, average %.2f\n", 762 sum.v_forks, sum.v_forkpages, 763 sum.v_forks == 0 ? 0.0 : 764 (double)sum.v_forkpages / sum.v_forks); 765 (void)printf("%u vforks, %u pages, average %.2f\n", 766 sum.v_vforks, sum.v_vforkpages, 767 sum.v_vforks == 0 ? 0.0 : 768 (double)sum.v_vforkpages / sum.v_vforks); 769 (void)printf("%u rforks, %u pages, average %.2f\n", 770 sum.v_rforks, sum.v_rforkpages, 771 sum.v_rforks == 0 ? 0.0 : 772 (double)sum.v_rforkpages / sum.v_rforks); 773} 774 775static void 776devstats(void) 777{ 778 int dn, state; 779 long double transfers_per_second; 780 long double busy_seconds; 781 long tmp; 782 783 for (state = 0; state < CPUSTATES; ++state) { 784 tmp = cur.cp_time[state]; 785 cur.cp_time[state] -= last.cp_time[state]; 786 last.cp_time[state] = tmp; 787 } 788 789 busy_seconds = cur.snap_time - last.snap_time; 790 791 for (dn = 0; dn < num_devices; dn++) { 792 int di; 793 794 if ((dev_select[dn].selected == 0) 795 || (dev_select[dn].selected > maxshowdevs)) 796 continue; 797 798 di = dev_select[dn].position; 799 800 if (devstat_compute_statistics(&cur.dinfo->devices[di], 801 &last.dinfo->devices[di], busy_seconds, 802 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 803 DSM_NONE) != 0) 804 errx(1, "%s", devstat_errbuf); 805 806 (void)printf("%3.0Lf ", transfers_per_second); 807 } 808} 809 810static void 811cpustats(void) 812{ 813 int state; 814 double lpct, total; 815 816 total = 0; 817 for (state = 0; state < CPUSTATES; ++state) 818 total += cur.cp_time[state]; 819 if (total) 820 lpct = 100.0 / total; 821 else 822 lpct = 0.0; 823 (void)printf("%2.0f ", (cur.cp_time[CP_USER] + 824 cur.cp_time[CP_NICE]) * lpct); 825 (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + 826 cur.cp_time[CP_INTR]) * lpct); 827 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * lpct); 828} 829 830static void 831dointr(void) 832{ 833 unsigned long *intrcnt, uptime; 834 uint64_t inttotal; 835 size_t clen, inamlen, intrcntlen, istrnamlen; 836 unsigned int i, nintr; 837 char *intrname, *tintrname; 838 839 uptime = getuptime(); 840 if (kd != NULL) { 841 intrcntlen = namelist[X_EINTRCNT].n_value - 842 namelist[X_INTRCNT].n_value; 843 inamlen = namelist[X_EINTRNAMES].n_value - 844 namelist[X_INTRNAMES].n_value; 845 if ((intrcnt = malloc(intrcntlen)) == NULL || 846 (intrname = malloc(inamlen)) == NULL) 847 err(1, "malloc()"); 848 kread(X_INTRCNT, intrcnt, intrcntlen); 849 kread(X_INTRNAMES, intrname, inamlen); 850 } else { 851 for (intrcnt = NULL, intrcntlen = 1024; ; intrcntlen *= 2) { 852 if ((intrcnt = reallocf(intrcnt, intrcntlen)) == NULL) 853 err(1, "reallocf()"); 854 if (mysysctl("hw.intrcnt", 855 intrcnt, &intrcntlen, NULL, 0) == 0) 856 break; 857 } 858 for (intrname = NULL, inamlen = 1024; ; inamlen *= 2) { 859 if ((intrname = reallocf(intrname, inamlen)) == NULL) 860 err(1, "reallocf()"); 861 if (mysysctl("hw.intrnames", 862 intrname, &inamlen, NULL, 0) == 0) 863 break; 864 } 865 } 866 nintr = intrcntlen / sizeof(unsigned long); 867 tintrname = intrname; 868 istrnamlen = strlen("interrupt"); 869 for (i = 0; i < nintr; i++) { 870 clen = strlen(tintrname); 871 if (clen > istrnamlen) 872 istrnamlen = clen; 873 tintrname += clen + 1; 874 } 875 (void)printf("%-*s %20s %10s\n", istrnamlen, "interrupt", "total", 876 "rate"); 877 inttotal = 0; 878 for (i = 0; i < nintr; i++) { 879 if (intrname[0] != '\0' && (*intrcnt != 0 || aflag)) 880 (void)printf("%-*s %20lu %10lu\n", istrnamlen, intrname, 881 *intrcnt, *intrcnt / uptime); 882 intrname += strlen(intrname) + 1; 883 inttotal += *intrcnt++; 884 } 885 (void)printf("%-*s %20llu %10llu\n", istrnamlen, "Total", 886 (long long)inttotal, (long long)(inttotal / uptime)); 887} 888 889static void 890domemstat_malloc(void) 891{ 892 struct memory_type_list *mtlp; 893 struct memory_type *mtp; 894 int error, first, i; 895 896 mtlp = memstat_mtl_alloc(); 897 if (mtlp == NULL) { 898 warn("memstat_mtl_alloc"); 899 return; 900 } 901 if (kd == NULL) { 902 if (memstat_sysctl_malloc(mtlp, 0) < 0) { 903 warnx("memstat_sysctl_malloc: %s", 904 memstat_strerror(memstat_mtl_geterror(mtlp))); 905 return; 906 } 907 } else { 908 if (memstat_kvm_malloc(mtlp, kd) < 0) { 909 error = memstat_mtl_geterror(mtlp); 910 if (error == MEMSTAT_ERROR_KVM) 911 warnx("memstat_kvm_malloc: %s", 912 kvm_geterr(kd)); 913 else 914 warnx("memstat_kvm_malloc: %s", 915 memstat_strerror(error)); 916 } 917 } 918 printf("%13s %5s %6s %7s %8s Size(s)\n", "Type", "InUse", "MemUse", 919 "HighUse", "Requests"); 920 for (mtp = memstat_mtl_first(mtlp); mtp != NULL; 921 mtp = memstat_mtl_next(mtp)) { 922 if (memstat_get_numallocs(mtp) == 0 && 923 memstat_get_count(mtp) == 0) 924 continue; 925 printf("%13s %5lld %5lldK %7s %8lld ", 926 memstat_get_name(mtp), memstat_get_count(mtp), 927 ((int64_t)memstat_get_bytes(mtp) + 1023) / 1024, "-", 928 memstat_get_numallocs(mtp)); 929 first = 1; 930 for (i = 0; i < 32; i++) { 931 if (memstat_get_sizemask(mtp) & (1 << i)) { 932 if (!first) 933 printf(","); 934 printf("%d", 1 << (i + 4)); 935 first = 0; 936 } 937 } 938 printf("\n"); 939 } 940 memstat_mtl_free(mtlp); 941} 942 943static void 944domemstat_zone(void) 945{ 946 struct memory_type_list *mtlp; 947 struct memory_type *mtp; 948 char name[MEMTYPE_MAXNAME + 1]; 949 int error; 950 951 mtlp = memstat_mtl_alloc(); 952 if (mtlp == NULL) { 953 warn("memstat_mtl_alloc"); 954 return; 955 } 956 if (kd == NULL) { 957 if (memstat_sysctl_uma(mtlp, 0) < 0) { 958 warnx("memstat_sysctl_uma: %s", 959 memstat_strerror(memstat_mtl_geterror(mtlp))); 960 return; 961 } 962 } else { 963 if (memstat_kvm_uma(mtlp, kd) < 0) { 964 error = memstat_mtl_geterror(mtlp); 965 if (error == MEMSTAT_ERROR_KVM) 966 warnx("memstat_kvm_uma: %s", 967 kvm_geterr(kd)); 968 else 969 warnx("memstat_kvm_uma: %s", 970 memstat_strerror(error)); 971 } 972 } 973 printf("%-20s %8s %8s %8s %8s %8s %8s\n\n", "ITEM", "SIZE", 974 "LIMIT", "USED", "FREE", "REQUESTS", "FAILURES"); 975 for (mtp = memstat_mtl_first(mtlp); mtp != NULL; 976 mtp = memstat_mtl_next(mtp)) { 977 strlcpy(name, memstat_get_name(mtp), MEMTYPE_MAXNAME); 978 strcat(name, ":"); 979 printf("%-20s %8llu, %8llu, %8llu, %8llu, %8llu, %8llu\n", name, 980 memstat_get_size(mtp), memstat_get_countlimit(mtp), 981 memstat_get_count(mtp), memstat_get_free(mtp), 982 memstat_get_numallocs(mtp), memstat_get_failures(mtp)); 983 } 984 memstat_mtl_free(mtlp); 985 printf("\n"); 986} 987 988/* 989 * kread reads something from the kernel, given its nlist index. 990 */ 991static void 992kreado(int nlx, void *addr, size_t size, size_t offset) 993{ 994 const char *sym; 995 996 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 997 sym = namelist[nlx].n_name; 998 if (*sym == '_') 999 ++sym; 1000 errx(1, "symbol %s not defined", sym); 1001 } 1002 if ((size_t)kvm_read(kd, namelist[nlx].n_value + offset, addr, 1003 size) != size) { 1004 sym = namelist[nlx].n_name; 1005 if (*sym == '_') 1006 ++sym; 1007 errx(1, "%s: %s", sym, kvm_geterr(kd)); 1008 } 1009} 1010 1011static void 1012kread(int nlx, void *addr, size_t size) 1013{ 1014 kreado(nlx, addr, size, 0); 1015} 1016 1017static char * 1018kgetstr(const char *strp) 1019{ 1020 int n = 0, size = 1; 1021 char *ret = NULL; 1022 1023 do { 1024 if (size == n + 1) { 1025 ret = realloc(ret, size); 1026 if (ret == NULL) 1027 err(1, "%s: realloc", __func__); 1028 size *= 2; 1029 } 1030 if (kvm_read(kd, (u_long)strp + n, &ret[n], 1) != 1) 1031 errx(1, "%s: %s", __func__, kvm_geterr(kd)); 1032 } while (ret[n++] != '\0'); 1033 return (ret); 1034} 1035 1036static void 1037usage(void) 1038{ 1039 (void)fprintf(stderr, "%s%s", 1040 "usage: vmstat [-afimsz] [-c count] [-M core [-N system]] [-w wait]\n", 1041 " [-n devs] [-p type,if,pass] [disks]\n"); 1042 exit(1); 1043} 1044