vmstat.c revision 184646
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 184646 2008-11-04 18:20:54Z keramida $"); 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 <memstat.h> 71#include <nlist.h> 72#include <paths.h> 73#include <stdio.h> 74#include <stdlib.h> 75#include <string.h> 76#include <sysexits.h> 77#include <time.h> 78#include <unistd.h> 79#include <libutil.h> 80 81static char da[] = "da"; 82 83static struct nlist namelist[] = { 84#define X_SUM 0 85 { "_cnt" }, 86#define X_BOOTTIME 1 87 { "_boottime" }, 88#define X_HZ 2 89 { "_hz" }, 90#define X_STATHZ 3 91 { "_stathz" }, 92#define X_NCHSTATS 4 93 { "_nchstats" }, 94#define X_INTRNAMES 5 95 { "_intrnames" }, 96#define X_EINTRNAMES 6 97 { "_eintrnames" }, 98#define X_INTRCNT 7 99 { "_intrcnt" }, 100#define X_EINTRCNT 8 101 { "_eintrcnt" }, 102#define X_KMEMSTATS 9 103 { "_kmemstatistics" }, 104#define X_KMEMZONES 10 105 { "_kmemzones" }, 106#ifdef notyet 107#define X_DEFICIT XXX 108 { "_deficit" }, 109#define X_REC XXX 110 { "_rectime" }, 111#define X_PGIN XXX 112 { "_pgintime" }, 113#define X_XSTATS XXX 114 { "_xstats" }, 115#define X_END XXX 116#else 117#define X_END 11 118#endif 119 { "" }, 120}; 121 122static struct statinfo cur, last; 123static int num_devices, maxshowdevs; 124static long generation; 125static struct device_selection *dev_select; 126static int num_selected; 127static struct devstat_match *matches; 128static int num_matches = 0; 129static int num_devices_specified, num_selections; 130static long select_generation; 131static char **specified_devices; 132static devstat_select_mode select_mode; 133 134static struct vmmeter sum, osum; 135 136#define VMSTAT_DEFAULT_LINES 20 /* Default number of `winlines'. */ 137volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */ 138static int winlines = VMSTAT_DEFAULT_LINES; /* Current number of tty rows. */ 139 140static int aflag; 141static int nflag; 142static int Pflag; 143static int hflag; 144 145static kvm_t *kd; 146 147#define FORKSTAT 0x01 148#define INTRSTAT 0x02 149#define MEMSTAT 0x04 150#define SUMSTAT 0x08 151#define TIMESTAT 0x10 152#define VMSTAT 0x20 153#define ZMEMSTAT 0x40 154 155static void cpustats(void); 156static void pcpustats(int, u_long, int); 157static void devstats(void); 158static void doforkst(void); 159static void dointr(void); 160static void dosum(void); 161static void dovmstat(unsigned int, int); 162static void domemstat_malloc(void); 163static void domemstat_zone(void); 164static void kread(int, void *, size_t); 165static void kreado(int, void *, size_t, size_t); 166static char *kgetstr(const char *); 167static void needhdr(int); 168static void needresize(int); 169static void doresize(void); 170static void printhdr(int, u_long); 171static void usage(void); 172 173static long pct(long, long); 174static long getuptime(void); 175 176static char **getdrivedata(char **); 177 178int 179main(int argc, char *argv[]) 180{ 181 int c, todo; 182 unsigned int interval; 183 int reps; 184 char *memf, *nlistf; 185 char errbuf[_POSIX2_LINE_MAX]; 186 187 memf = nlistf = NULL; 188 interval = reps = todo = 0; 189 maxshowdevs = 2; 190 hflag = isatty(1); 191 while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:Pp:stw:z")) != -1) { 192 switch (c) { 193 case 'a': 194 aflag++; 195 break; 196 case 'c': 197 reps = atoi(optarg); 198 break; 199 case 'P': 200 Pflag++; 201 break; 202 case 'f': 203 todo |= FORKSTAT; 204 break; 205 case 'h': 206 hflag = 1; 207 break; 208 case 'H': 209 hflag = 0; 210 break; 211 case 'i': 212 todo |= INTRSTAT; 213 break; 214 case 'M': 215 memf = optarg; 216 break; 217 case 'm': 218 todo |= MEMSTAT; 219 break; 220 case 'N': 221 nlistf = optarg; 222 break; 223 case 'n': 224 nflag = 1; 225 maxshowdevs = atoi(optarg); 226 if (maxshowdevs < 0) 227 errx(1, "number of devices %d is < 0", 228 maxshowdevs); 229 break; 230 case 'p': 231 if (devstat_buildmatch(optarg, &matches, &num_matches) != 0) 232 errx(1, "%s", devstat_errbuf); 233 break; 234 case 's': 235 todo |= SUMSTAT; 236 break; 237 case 't': 238#ifdef notyet 239 todo |= TIMESTAT; 240#else 241 errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); 242#endif 243 break; 244 case 'w': 245 interval = atoi(optarg); 246 break; 247 case 'z': 248 todo |= ZMEMSTAT; 249 break; 250 case '?': 251 default: 252 usage(); 253 } 254 } 255 argc -= optind; 256 argv += optind; 257 258 if (todo == 0) 259 todo = VMSTAT; 260 261 if (memf != NULL) { 262 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 263 if (kd == NULL) 264 errx(1, "kvm_openfiles: %s", errbuf); 265 } 266 267 if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) { 268 if (c > 0) { 269 warnx("undefined symbols:"); 270 for (c = 0; 271 c < (int)(sizeof(namelist)/sizeof(namelist[0])); 272 c++) 273 if (namelist[c].n_type == 0) 274 (void)fprintf(stderr, " %s", 275 namelist[c].n_name); 276 (void)fputc('\n', stderr); 277 } else 278 warnx("kvm_nlist: %s", kvm_geterr(kd)); 279 exit(1); 280 } 281 if (kd && Pflag) 282 errx(1, "Cannot use -P with crash dumps"); 283 284 if (todo & VMSTAT) { 285 /* 286 * Make sure that the userland devstat version matches the 287 * kernel devstat version. If not, exit and print a 288 * message informing the user of his mistake. 289 */ 290 if (devstat_checkversion(NULL) < 0) 291 errx(1, "%s", devstat_errbuf); 292 293 294 argv = getdrivedata(argv); 295 } 296 297#define BACKWARD_COMPATIBILITY 298#ifdef BACKWARD_COMPATIBILITY 299 if (*argv) { 300 interval = atoi(*argv); 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 = 1; 311 312 if (todo & FORKSTAT) 313 doforkst(); 314 if (todo & MEMSTAT) 315 domemstat_malloc(); 316 if (todo & ZMEMSTAT) 317 domemstat_zone(); 318 if (todo & SUMSTAT) 319 dosum(); 320#ifdef notyet 321 if (todo & TIMESTAT) 322 dotimes(); 323#endif 324 if (todo & INTRSTAT) 325 dointr(); 326 if (todo & VMSTAT) 327 dovmstat(interval, reps); 328 exit(0); 329} 330 331static int 332mysysctl(const char *name, void *oldp, size_t *oldlenp, 333 void *newp, size_t newlen) 334{ 335 int error; 336 337 error = sysctlbyname(name, oldp, oldlenp, newp, newlen); 338 if (error != 0 && errno != ENOMEM) 339 err(1, "sysctl(%s)", name); 340 return (error); 341} 342 343static char ** 344getdrivedata(char **argv) 345{ 346 if ((num_devices = devstat_getnumdevs(NULL)) < 0) 347 errx(1, "%s", devstat_errbuf); 348 349 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 350 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); 351 bzero(cur.dinfo, sizeof(struct devinfo)); 352 bzero(last.dinfo, sizeof(struct devinfo)); 353 354 if (devstat_getdevs(NULL, &cur) == -1) 355 errx(1, "%s", devstat_errbuf); 356 357 num_devices = cur.dinfo->numdevs; 358 generation = cur.dinfo->generation; 359 360 specified_devices = (char **)malloc(sizeof(char *)); 361 for (num_devices_specified = 0; *argv; ++argv) { 362 if (isdigit(**argv)) 363 break; 364 num_devices_specified++; 365 specified_devices = (char **)realloc(specified_devices, 366 sizeof(char *) * 367 num_devices_specified); 368 specified_devices[num_devices_specified - 1] = *argv; 369 } 370 dev_select = NULL; 371 372 if (nflag == 0 && maxshowdevs < num_devices_specified) 373 maxshowdevs = num_devices_specified; 374 375 /* 376 * People are generally only interested in disk statistics when 377 * they're running vmstat. So, that's what we're going to give 378 * them if they don't specify anything by default. We'll also give 379 * them any other random devices in the system so that we get to 380 * maxshowdevs devices, if that many devices exist. If the user 381 * specifies devices on the command line, either through a pattern 382 * match or by naming them explicitly, we will give the user only 383 * those devices. 384 */ 385 if ((num_devices_specified == 0) && (num_matches == 0)) { 386 if (devstat_buildmatch(da, &matches, &num_matches) != 0) 387 errx(1, "%s", devstat_errbuf); 388 389 select_mode = DS_SELECT_ADD; 390 } else 391 select_mode = DS_SELECT_ONLY; 392 393 /* 394 * At this point, selectdevs will almost surely indicate that the 395 * device list has changed, so we don't look for return values of 0 396 * or 1. If we get back -1, though, there is an error. 397 */ 398 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections, 399 &select_generation, generation, cur.dinfo->devices, 400 num_devices, matches, num_matches, specified_devices, 401 num_devices_specified, select_mode, 402 maxshowdevs, 0) == -1) 403 errx(1, "%s", devstat_errbuf); 404 405 return(argv); 406} 407 408static long 409getuptime(void) 410{ 411 struct timespec sp; 412 time_t uptime; 413 414 (void)clock_gettime(CLOCK_MONOTONIC, &sp); 415 uptime = sp.tv_sec; 416 if (uptime <= 0 || uptime > 60*60*24*365*10) 417 errx(1, "time makes no sense; namelist must be wrong"); 418 419 return(uptime); 420} 421 422static void 423fill_vmmeter(struct vmmeter *vmmp) 424{ 425 if (kd != NULL) { 426 kread(X_SUM, vmmp, sizeof(*vmmp)); 427 } else { 428 size_t size = sizeof(unsigned int); 429#define GET_VM_STATS(cat, name) \ 430 mysysctl("vm.stats." #cat "." #name, &vmmp->name, &size, NULL, 0) 431 /* sys */ 432 GET_VM_STATS(sys, v_swtch); 433 GET_VM_STATS(sys, v_trap); 434 GET_VM_STATS(sys, v_syscall); 435 GET_VM_STATS(sys, v_intr); 436 GET_VM_STATS(sys, v_soft); 437 438 /* vm */ 439 GET_VM_STATS(vm, v_vm_faults); 440 GET_VM_STATS(vm, v_cow_faults); 441 GET_VM_STATS(vm, v_cow_optim); 442 GET_VM_STATS(vm, v_zfod); 443 GET_VM_STATS(vm, v_ozfod); 444 GET_VM_STATS(vm, v_swapin); 445 GET_VM_STATS(vm, v_swapout); 446 GET_VM_STATS(vm, v_swappgsin); 447 GET_VM_STATS(vm, v_swappgsout); 448 GET_VM_STATS(vm, v_vnodein); 449 GET_VM_STATS(vm, v_vnodeout); 450 GET_VM_STATS(vm, v_vnodepgsin); 451 GET_VM_STATS(vm, v_vnodepgsout); 452 GET_VM_STATS(vm, v_intrans); 453 GET_VM_STATS(vm, v_reactivated); 454 GET_VM_STATS(vm, v_pdwakeups); 455 GET_VM_STATS(vm, v_pdpages); 456 GET_VM_STATS(vm, v_tcached); 457 GET_VM_STATS(vm, v_dfree); 458 GET_VM_STATS(vm, v_pfree); 459 GET_VM_STATS(vm, v_tfree); 460 GET_VM_STATS(vm, v_page_size); 461 GET_VM_STATS(vm, v_page_count); 462 GET_VM_STATS(vm, v_free_reserved); 463 GET_VM_STATS(vm, v_free_target); 464 GET_VM_STATS(vm, v_free_min); 465 GET_VM_STATS(vm, v_free_count); 466 GET_VM_STATS(vm, v_wire_count); 467 GET_VM_STATS(vm, v_active_count); 468 GET_VM_STATS(vm, v_inactive_target); 469 GET_VM_STATS(vm, v_inactive_count); 470 GET_VM_STATS(vm, v_cache_count); 471 GET_VM_STATS(vm, v_cache_min); 472 GET_VM_STATS(vm, v_cache_max); 473 GET_VM_STATS(vm, v_pageout_free_min); 474 GET_VM_STATS(vm, v_interrupt_free_min); 475 /*GET_VM_STATS(vm, v_free_severe);*/ 476 GET_VM_STATS(vm, v_forks); 477 GET_VM_STATS(vm, v_vforks); 478 GET_VM_STATS(vm, v_rforks); 479 GET_VM_STATS(vm, v_kthreads); 480 GET_VM_STATS(vm, v_forkpages); 481 GET_VM_STATS(vm, v_vforkpages); 482 GET_VM_STATS(vm, v_rforkpages); 483 GET_VM_STATS(vm, v_kthreadpages); 484#undef GET_VM_STATS 485 } 486} 487 488static void 489fill_vmtotal(struct vmtotal *vmtp) 490{ 491 if (kd != NULL) { 492 /* XXX fill vmtp */ 493 errx(1, "not implemented"); 494 } else { 495 size_t size = sizeof(*vmtp); 496 mysysctl("vm.vmtotal", vmtp, &size, NULL, 0); 497 if (size != sizeof(*vmtp)) 498 errx(1, "vm.total size mismatch"); 499 } 500} 501 502/* Determine how many cpu columns, and what index they are in kern.cp_times */ 503static int 504getcpuinfo(u_long *maskp, int *maxidp) 505{ 506 int maxcpu; 507 int maxid; 508 int ncpus; 509 int i, j; 510 int empty; 511 size_t size; 512 long *times; 513 u_long mask; 514 515 if (kd != NULL) 516 errx(1, "not implemented"); 517 mask = 0; 518 ncpus = 0; 519 size = sizeof(maxcpu); 520 mysysctl("kern.smp.maxcpus", &maxcpu, &size, NULL, 0); 521 if (size != sizeof(maxcpu)) 522 errx(1, "sysctl kern.smp.maxcpus"); 523 size = sizeof(long) * maxcpu * CPUSTATES; 524 times = malloc(size); 525 if (times == NULL) 526 err(1, "malloc %zd bytes", size); 527 mysysctl("kern.cp_times", times, &size, NULL, 0); 528 maxid = (size / CPUSTATES / sizeof(long)) - 1; 529 for (i = 0; i <= maxid; i++) { 530 empty = 1; 531 for (j = 0; empty && j < CPUSTATES; j++) { 532 if (times[i * CPUSTATES + j] != 0) 533 empty = 0; 534 } 535 if (!empty) { 536 mask |= (1ul << i); 537 ncpus++; 538 } 539 } 540 if (maskp) 541 *maskp = mask; 542 if (maxidp) 543 *maxidp = maxid; 544 return (ncpus); 545} 546 547 548static void 549prthuman(u_int64_t val, int size) 550{ 551 char buf[10]; 552 int flags; 553 554 if (size < 5 || size > 9) 555 errx(1, "doofus"); 556 flags = HN_B | HN_NOSPACE | HN_DECIMAL; 557 humanize_number(buf, size, val, "", HN_AUTOSCALE, flags); 558 printf("%*s", size, buf); 559} 560 561static int hz, hdrcnt; 562 563static long *cur_cp_times; 564static long *last_cp_times; 565static size_t size_cp_times; 566 567static void 568dovmstat(unsigned int interval, int reps) 569{ 570 struct vmtotal total; 571 time_t uptime, halfuptime; 572 struct devinfo *tmp_dinfo; 573 size_t size; 574 int ncpus, maxid; 575 u_long cpumask; 576 577 uptime = getuptime(); 578 halfuptime = uptime / 2; 579 580 /* 581 * If the user stops the program (control-Z) and then resumes it, 582 * print out the header again. 583 */ 584 (void)signal(SIGCONT, needhdr); 585 586 /* 587 * If our standard output is a tty, then install a SIGWINCH handler 588 * and set wresized so that our first iteration through the main 589 * vmstat loop will peek at the terminal's current rows to find out 590 * how many lines can fit in a screenful of output. 591 */ 592 if (isatty(fileno(stdout)) != 0) { 593 wresized = 1; 594 (void)signal(SIGWINCH, needresize); 595 } else { 596 wresized = 0; 597 winlines = VMSTAT_DEFAULT_LINES; 598 } 599 600 if (kd != NULL) { 601 if (namelist[X_STATHZ].n_type != 0 && 602 namelist[X_STATHZ].n_value != 0) 603 kread(X_STATHZ, &hz, sizeof(hz)); 604 if (!hz) 605 kread(X_HZ, &hz, sizeof(hz)); 606 } else { 607 struct clockinfo clockrate; 608 609 size = sizeof(clockrate); 610 mysysctl("kern.clockrate", &clockrate, &size, NULL, 0); 611 if (size != sizeof(clockrate)) 612 errx(1, "clockrate size mismatch"); 613 hz = clockrate.hz; 614 } 615 616 if (Pflag) { 617 ncpus = getcpuinfo(&cpumask, &maxid); 618 size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES; 619 cur_cp_times = malloc(size_cp_times); 620 last_cp_times = malloc(size_cp_times); 621 bzero(cur_cp_times, size_cp_times); 622 bzero(last_cp_times, size_cp_times); 623 } 624 for (hdrcnt = 1;;) { 625 if (!--hdrcnt) 626 printhdr(ncpus, cpumask); 627 if (kd != NULL) { 628 if (kvm_getcptime(kd, cur.cp_time) < 0) 629 errx(1, "kvm_getcptime: %s", kvm_geterr(kd)); 630 } else { 631 size = sizeof(cur.cp_time); 632 mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0); 633 if (size != sizeof(cur.cp_time)) 634 errx(1, "cp_time size mismatch"); 635 } 636 if (Pflag) { 637 size = size_cp_times; 638 mysysctl("kern.cp_times", cur_cp_times, &size, NULL, 0); 639 if (size != size_cp_times) 640 errx(1, "cp_times mismatch"); 641 } 642 643 tmp_dinfo = last.dinfo; 644 last.dinfo = cur.dinfo; 645 cur.dinfo = tmp_dinfo; 646 last.snap_time = cur.snap_time; 647 648 /* 649 * Here what we want to do is refresh our device stats. 650 * getdevs() returns 1 when the device list has changed. 651 * If the device list has changed, we want to go through 652 * the selection process again, in case a device that we 653 * were previously displaying has gone away. 654 */ 655 switch (devstat_getdevs(NULL, &cur)) { 656 case -1: 657 errx(1, "%s", devstat_errbuf); 658 break; 659 case 1: { 660 int retval; 661 662 num_devices = cur.dinfo->numdevs; 663 generation = cur.dinfo->generation; 664 665 retval = devstat_selectdevs(&dev_select, &num_selected, 666 &num_selections, &select_generation, 667 generation, cur.dinfo->devices, 668 num_devices, matches, num_matches, 669 specified_devices, 670 num_devices_specified, select_mode, 671 maxshowdevs, 0); 672 switch (retval) { 673 case -1: 674 errx(1, "%s", devstat_errbuf); 675 break; 676 case 1: 677 printhdr(ncpus, cpumask); 678 break; 679 default: 680 break; 681 } 682 } 683 default: 684 break; 685 } 686 687 fill_vmmeter(&sum); 688 fill_vmtotal(&total); 689 (void)printf("%2d %1d %1d", 690 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); 691#define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10)) 692#define rate(x) (((x) + halfuptime) / uptime) /* round */ 693 if (hflag) { 694 printf(" "); 695 prthuman(total.t_avm * (u_int64_t)sum.v_page_size, 7); 696 printf(" "); 697 prthuman(total.t_free * (u_int64_t)sum.v_page_size, 6); 698 printf(" "); 699 } else { 700 printf(" %7d ", vmstat_pgtok(total.t_avm)); 701 printf(" %6d ", vmstat_pgtok(total.t_free)); 702 } 703 (void)printf("%5lu ", 704 (unsigned long)rate(sum.v_vm_faults - osum.v_vm_faults)); 705 (void)printf("%3lu ", 706 (unsigned long)rate(sum.v_reactivated - osum.v_reactivated)); 707 (void)printf("%3lu ", 708 (unsigned long)rate(sum.v_swapin + sum.v_vnodein - 709 (osum.v_swapin + osum.v_vnodein))); 710 (void)printf("%3lu ", 711 (unsigned long)rate(sum.v_swapout + sum.v_vnodeout - 712 (osum.v_swapout + osum.v_vnodeout))); 713 (void)printf("%5lu ", 714 (unsigned long)rate(sum.v_tfree - osum.v_tfree)); 715 (void)printf("%3lu ", 716 (unsigned long)rate(sum.v_pdpages - osum.v_pdpages)); 717 devstats(); 718 (void)printf("%4lu %4lu %4lu", 719 (unsigned long)rate(sum.v_intr - osum.v_intr), 720 (unsigned long)rate(sum.v_syscall - osum.v_syscall), 721 (unsigned long)rate(sum.v_swtch - osum.v_swtch)); 722 if (Pflag) 723 pcpustats(ncpus, cpumask, maxid); 724 else 725 cpustats(); 726 (void)printf("\n"); 727 (void)fflush(stdout); 728 if (reps >= 0 && --reps <= 0) 729 break; 730 osum = sum; 731 uptime = interval; 732 /* 733 * We round upward to avoid losing low-frequency events 734 * (i.e., >= 1 per interval but < 1 per second). 735 */ 736 if (interval != 1) 737 halfuptime = (uptime + 1) / 2; 738 else 739 halfuptime = 0; 740 (void)sleep(interval); 741 } 742} 743 744static void 745printhdr(int ncpus, u_long cpumask) 746{ 747 int i, num_shown; 748 749 num_shown = (num_selected < maxshowdevs) ? num_selected : maxshowdevs; 750 (void)printf(" procs memory page%*s", 19, ""); 751 if (num_shown > 1) 752 (void)printf(" disks %*s", num_shown * 4 - 7, ""); 753 else if (num_shown == 1) 754 (void)printf("disk"); 755 (void)printf(" faults "); 756 if (Pflag) { 757 for (i = 0; i < ncpus; i++) { 758 if (cpumask & (1ul << i)) 759 printf("cpu%-2d ", i); 760 } 761 printf("\n"); 762 } else 763 printf("cpu\n"); 764 (void)printf(" r b w avm fre flt re pi po fr sr "); 765 for (i = 0; i < num_devices; i++) 766 if ((dev_select[i].selected) 767 && (dev_select[i].selected <= maxshowdevs)) 768 (void)printf("%c%c%d ", dev_select[i].device_name[0], 769 dev_select[i].device_name[1], 770 dev_select[i].unit_number); 771 (void)printf(" in sy cs"); 772 if (Pflag) { 773 for (i = 0; i < ncpus; i++) 774 printf(" us sy id"); 775 printf("\n"); 776 } else 777 printf(" us sy id\n"); 778 if (wresized != 0) 779 doresize(); 780 hdrcnt = winlines; 781} 782 783/* 784 * Force a header to be prepended to the next output. 785 */ 786static void 787needhdr(int dummy __unused) 788{ 789 790 hdrcnt = 1; 791} 792 793/* 794 * When the terminal is resized, force an update of the maximum number of rows 795 * printed between each header repetition. Then force a new header to be 796 * prepended to the next output. 797 */ 798void 799needresize(int signo) 800{ 801 802 wresized = 1; 803 hdrcnt = 1; 804} 805 806/* 807 * Update the global `winlines' count of terminal rows. 808 */ 809void 810doresize(void) 811{ 812 int status; 813 struct winsize w; 814 815 for (;;) { 816 status = ioctl(fileno(stdout), TIOCGWINSZ, &w); 817 if (status == -1 && errno == EINTR) 818 continue; 819 else if (status == -1) 820 err(1, "ioctl"); 821 if (w.ws_row > 3) 822 winlines = w.ws_row - 3; 823 else 824 winlines = VMSTAT_DEFAULT_LINES; 825 break; 826 } 827 828 /* 829 * Inhibit doresize() calls until we are rescheduled by SIGWINCH. 830 */ 831 wresized = 0; 832} 833 834#ifdef notyet 835static void 836dotimes(void) 837{ 838 unsigned int pgintime, rectime; 839 840 kread(X_REC, &rectime, sizeof(rectime)); 841 kread(X_PGIN, &pgintime, sizeof(pgintime)); 842 kread(X_SUM, &sum, sizeof(sum)); 843 (void)printf("%u reclaims, %u total time (usec)\n", 844 sum.v_pgrec, rectime); 845 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); 846 (void)printf("\n"); 847 (void)printf("%u page ins, %u total time (msec)\n", 848 sum.v_pgin, pgintime / 10); 849 (void)printf("average: %8.1f msec / page in\n", 850 pgintime / (sum.v_pgin * 10.0)); 851} 852#endif 853 854static long 855pct(long top, long bot) 856{ 857 long ans; 858 859 if (bot == 0) 860 return(0); 861 ans = (quad_t)top * 100 / bot; 862 return (ans); 863} 864 865#define PCT(top, bot) pct((long)(top), (long)(bot)) 866 867static void 868dosum(void) 869{ 870 struct nchstats lnchstats; 871 long nchtotal; 872 873 fill_vmmeter(&sum); 874 (void)printf("%9u cpu context switches\n", sum.v_swtch); 875 (void)printf("%9u device interrupts\n", sum.v_intr); 876 (void)printf("%9u software interrupts\n", sum.v_soft); 877 (void)printf("%9u traps\n", sum.v_trap); 878 (void)printf("%9u system calls\n", sum.v_syscall); 879 (void)printf("%9u kernel threads created\n", sum.v_kthreads); 880 (void)printf("%9u fork() calls\n", sum.v_forks); 881 (void)printf("%9u vfork() calls\n", sum.v_vforks); 882 (void)printf("%9u rfork() calls\n", sum.v_rforks); 883 (void)printf("%9u swap pager pageins\n", sum.v_swapin); 884 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin); 885 (void)printf("%9u swap pager pageouts\n", sum.v_swapout); 886 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout); 887 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein); 888 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin); 889 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout); 890 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout); 891 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups); 892 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages); 893 (void)printf("%9u pages reactivated\n", sum.v_reactivated); 894 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults); 895 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim); 896 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod); 897 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod); 898 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); 899 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults); 900 (void)printf("%9u pages affected by kernel thread creation\n", sum.v_kthreadpages); 901 (void)printf("%9u pages affected by fork()\n", sum.v_forkpages); 902 (void)printf("%9u pages affected by vfork()\n", sum.v_vforkpages); 903 (void)printf("%9u pages affected by rfork()\n", sum.v_rforkpages); 904 (void)printf("%9u pages cached\n", sum.v_tcached); 905 (void)printf("%9u pages freed\n", sum.v_tfree); 906 (void)printf("%9u pages freed by daemon\n", sum.v_dfree); 907 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree); 908 (void)printf("%9u pages active\n", sum.v_active_count); 909 (void)printf("%9u pages inactive\n", sum.v_inactive_count); 910 (void)printf("%9u pages in VM cache\n", sum.v_cache_count); 911 (void)printf("%9u pages wired down\n", sum.v_wire_count); 912 (void)printf("%9u pages free\n", sum.v_free_count); 913 (void)printf("%9u bytes per page\n", sum.v_page_size); 914 if (kd != NULL) { 915 kread(X_NCHSTATS, &lnchstats, sizeof(lnchstats)); 916 } else { 917 size_t size = sizeof(lnchstats); 918 mysysctl("vfs.cache.nchstats", &lnchstats, &size, NULL, 0); 919 if (size != sizeof(lnchstats)) 920 errx(1, "vfs.cache.nchstats size mismatch"); 921 } 922 nchtotal = lnchstats.ncs_goodhits + lnchstats.ncs_neghits + 923 lnchstats.ncs_badhits + lnchstats.ncs_falsehits + 924 lnchstats.ncs_miss + lnchstats.ncs_long; 925 (void)printf("%9ld total name lookups\n", nchtotal); 926 (void)printf( 927 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n", 928 "", PCT(lnchstats.ncs_goodhits, nchtotal), 929 PCT(lnchstats.ncs_neghits, nchtotal), 930 PCT(lnchstats.ncs_pass2, nchtotal)); 931 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "", 932 PCT(lnchstats.ncs_badhits, nchtotal), 933 PCT(lnchstats.ncs_falsehits, nchtotal), 934 PCT(lnchstats.ncs_long, nchtotal)); 935} 936 937static void 938doforkst(void) 939{ 940 fill_vmmeter(&sum); 941 (void)printf("%u forks, %u pages, average %.2f\n", 942 sum.v_forks, sum.v_forkpages, 943 sum.v_forks == 0 ? 0.0 : 944 (double)sum.v_forkpages / sum.v_forks); 945 (void)printf("%u vforks, %u pages, average %.2f\n", 946 sum.v_vforks, sum.v_vforkpages, 947 sum.v_vforks == 0 ? 0.0 : 948 (double)sum.v_vforkpages / sum.v_vforks); 949 (void)printf("%u rforks, %u pages, average %.2f\n", 950 sum.v_rforks, sum.v_rforkpages, 951 sum.v_rforks == 0 ? 0.0 : 952 (double)sum.v_rforkpages / sum.v_rforks); 953} 954 955static void 956devstats(void) 957{ 958 int dn, state; 959 long double transfers_per_second; 960 long double busy_seconds; 961 long tmp; 962 963 for (state = 0; state < CPUSTATES; ++state) { 964 tmp = cur.cp_time[state]; 965 cur.cp_time[state] -= last.cp_time[state]; 966 last.cp_time[state] = tmp; 967 } 968 969 busy_seconds = cur.snap_time - last.snap_time; 970 971 for (dn = 0; dn < num_devices; dn++) { 972 int di; 973 974 if ((dev_select[dn].selected == 0) 975 || (dev_select[dn].selected > maxshowdevs)) 976 continue; 977 978 di = dev_select[dn].position; 979 980 if (devstat_compute_statistics(&cur.dinfo->devices[di], 981 &last.dinfo->devices[di], busy_seconds, 982 DSM_TRANSFERS_PER_SECOND, &transfers_per_second, 983 DSM_NONE) != 0) 984 errx(1, "%s", devstat_errbuf); 985 986 (void)printf("%3.0Lf ", transfers_per_second); 987 } 988} 989 990static void 991percent(double pct, int *over) 992{ 993 char buf[10]; 994 int l; 995 996 l = snprintf(buf, sizeof(buf), "%.0f", pct); 997 if (l == 1 && *over) { 998 printf("%s", buf); 999 (*over)--; 1000 } else 1001 printf("%2s", buf); 1002 if (l > 2) 1003 (*over)++; 1004} 1005 1006static void 1007cpustats(void) 1008{ 1009 int state, over; 1010 double lpct, total; 1011 1012 total = 0; 1013 for (state = 0; state < CPUSTATES; ++state) 1014 total += cur.cp_time[state]; 1015 if (total) 1016 lpct = 100.0 / total; 1017 else 1018 lpct = 0.0; 1019 over = 0; 1020 printf(" "); 1021 percent((cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * lpct, &over); 1022 printf(" "); 1023 percent((cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * lpct, &over); 1024 printf(" "); 1025 percent(cur.cp_time[CP_IDLE] * lpct, &over); 1026} 1027 1028static void 1029pcpustats(int ncpus, u_long cpumask, int maxid) 1030{ 1031 int state, i; 1032 double lpct, total; 1033 long tmp; 1034 int over; 1035 1036 /* devstats does this for cp_time */ 1037 for (i = 0; i <= maxid; i++) { 1038 if ((cpumask & (1ul << i)) == 0) 1039 continue; 1040 for (state = 0; state < CPUSTATES; ++state) { 1041 tmp = cur_cp_times[i * CPUSTATES + state]; 1042 cur_cp_times[i * CPUSTATES + state] -= last_cp_times[i * CPUSTATES + state]; 1043 last_cp_times[i * CPUSTATES + state] = tmp; 1044 } 1045 } 1046 1047 over = 0; 1048 for (i = 0; i <= maxid; i++) { 1049 if ((cpumask & (1ul << i)) == 0) 1050 continue; 1051 total = 0; 1052 for (state = 0; state < CPUSTATES; ++state) 1053 total += cur_cp_times[i * CPUSTATES + state]; 1054 if (total) 1055 lpct = 100.0 / total; 1056 else 1057 lpct = 0.0; 1058 printf(" "); 1059 percent((cur_cp_times[i * CPUSTATES + CP_USER] + 1060 cur_cp_times[i * CPUSTATES + CP_NICE]) * lpct, &over); 1061 printf(" "); 1062 percent((cur_cp_times[i * CPUSTATES + CP_SYS] + 1063 cur_cp_times[i * CPUSTATES + CP_INTR]) * lpct, &over); 1064 printf(" "); 1065 percent(cur_cp_times[i * CPUSTATES + CP_IDLE] * lpct, &over); 1066 } 1067} 1068 1069static void 1070dointr(void) 1071{ 1072 unsigned long *intrcnt, uptime; 1073 uint64_t inttotal; 1074 size_t clen, inamlen, intrcntlen, istrnamlen; 1075 unsigned int i, nintr; 1076 char *intrname, *tintrname; 1077 1078 uptime = getuptime(); 1079 if (kd != NULL) { 1080 intrcntlen = namelist[X_EINTRCNT].n_value - 1081 namelist[X_INTRCNT].n_value; 1082 inamlen = namelist[X_EINTRNAMES].n_value - 1083 namelist[X_INTRNAMES].n_value; 1084 if ((intrcnt = malloc(intrcntlen)) == NULL || 1085 (intrname = malloc(inamlen)) == NULL) 1086 err(1, "malloc()"); 1087 kread(X_INTRCNT, intrcnt, intrcntlen); 1088 kread(X_INTRNAMES, intrname, inamlen); 1089 } else { 1090 for (intrcnt = NULL, intrcntlen = 1024; ; intrcntlen *= 2) { 1091 if ((intrcnt = reallocf(intrcnt, intrcntlen)) == NULL) 1092 err(1, "reallocf()"); 1093 if (mysysctl("hw.intrcnt", 1094 intrcnt, &intrcntlen, NULL, 0) == 0) 1095 break; 1096 } 1097 for (intrname = NULL, inamlen = 1024; ; inamlen *= 2) { 1098 if ((intrname = reallocf(intrname, inamlen)) == NULL) 1099 err(1, "reallocf()"); 1100 if (mysysctl("hw.intrnames", 1101 intrname, &inamlen, NULL, 0) == 0) 1102 break; 1103 } 1104 } 1105 nintr = intrcntlen / sizeof(unsigned long); 1106 tintrname = intrname; 1107 istrnamlen = strlen("interrupt"); 1108 for (i = 0; i < nintr; i++) { 1109 clen = strlen(tintrname); 1110 if (clen > istrnamlen) 1111 istrnamlen = clen; 1112 tintrname += clen + 1; 1113 } 1114 (void)printf("%-*s %20s %10s\n", istrnamlen, "interrupt", "total", 1115 "rate"); 1116 inttotal = 0; 1117 for (i = 0; i < nintr; i++) { 1118 if (intrname[0] != '\0' && (*intrcnt != 0 || aflag)) 1119 (void)printf("%-*s %20lu %10lu\n", istrnamlen, intrname, 1120 *intrcnt, *intrcnt / uptime); 1121 intrname += strlen(intrname) + 1; 1122 inttotal += *intrcnt++; 1123 } 1124 (void)printf("%-*s %20llu %10llu\n", istrnamlen, "Total", 1125 (long long)inttotal, (long long)(inttotal / uptime)); 1126} 1127 1128static void 1129domemstat_malloc(void) 1130{ 1131 struct memory_type_list *mtlp; 1132 struct memory_type *mtp; 1133 int error, first, i; 1134 1135 mtlp = memstat_mtl_alloc(); 1136 if (mtlp == NULL) { 1137 warn("memstat_mtl_alloc"); 1138 return; 1139 } 1140 if (kd == NULL) { 1141 if (memstat_sysctl_malloc(mtlp, 0) < 0) { 1142 warnx("memstat_sysctl_malloc: %s", 1143 memstat_strerror(memstat_mtl_geterror(mtlp))); 1144 return; 1145 } 1146 } else { 1147 if (memstat_kvm_malloc(mtlp, kd) < 0) { 1148 error = memstat_mtl_geterror(mtlp); 1149 if (error == MEMSTAT_ERROR_KVM) 1150 warnx("memstat_kvm_malloc: %s", 1151 kvm_geterr(kd)); 1152 else 1153 warnx("memstat_kvm_malloc: %s", 1154 memstat_strerror(error)); 1155 } 1156 } 1157 printf("%13s %5s %6s %7s %8s Size(s)\n", "Type", "InUse", "MemUse", 1158 "HighUse", "Requests"); 1159 for (mtp = memstat_mtl_first(mtlp); mtp != NULL; 1160 mtp = memstat_mtl_next(mtp)) { 1161 if (memstat_get_numallocs(mtp) == 0 && 1162 memstat_get_count(mtp) == 0) 1163 continue; 1164 printf("%13s %5lld %5lldK %7s %8lld ", 1165 memstat_get_name(mtp), memstat_get_count(mtp), 1166 ((int64_t)memstat_get_bytes(mtp) + 1023) / 1024, "-", 1167 memstat_get_numallocs(mtp)); 1168 first = 1; 1169 for (i = 0; i < 32; i++) { 1170 if (memstat_get_sizemask(mtp) & (1 << i)) { 1171 if (!first) 1172 printf(","); 1173 printf("%d", 1 << (i + 4)); 1174 first = 0; 1175 } 1176 } 1177 printf("\n"); 1178 } 1179 memstat_mtl_free(mtlp); 1180} 1181 1182static void 1183domemstat_zone(void) 1184{ 1185 struct memory_type_list *mtlp; 1186 struct memory_type *mtp; 1187 char name[MEMTYPE_MAXNAME + 1]; 1188 int error; 1189 1190 mtlp = memstat_mtl_alloc(); 1191 if (mtlp == NULL) { 1192 warn("memstat_mtl_alloc"); 1193 return; 1194 } 1195 if (kd == NULL) { 1196 if (memstat_sysctl_uma(mtlp, 0) < 0) { 1197 warnx("memstat_sysctl_uma: %s", 1198 memstat_strerror(memstat_mtl_geterror(mtlp))); 1199 return; 1200 } 1201 } else { 1202 if (memstat_kvm_uma(mtlp, kd) < 0) { 1203 error = memstat_mtl_geterror(mtlp); 1204 if (error == MEMSTAT_ERROR_KVM) 1205 warnx("memstat_kvm_uma: %s", 1206 kvm_geterr(kd)); 1207 else 1208 warnx("memstat_kvm_uma: %s", 1209 memstat_strerror(error)); 1210 } 1211 } 1212 printf("%-20s %8s %8s %8s %8s %8s %8s\n\n", "ITEM", "SIZE", 1213 "LIMIT", "USED", "FREE", "REQUESTS", "FAILURES"); 1214 for (mtp = memstat_mtl_first(mtlp); mtp != NULL; 1215 mtp = memstat_mtl_next(mtp)) { 1216 strlcpy(name, memstat_get_name(mtp), MEMTYPE_MAXNAME); 1217 strcat(name, ":"); 1218 printf("%-20s %8llu, %8llu, %8llu, %8llu, %8llu, %8llu\n", name, 1219 memstat_get_size(mtp), memstat_get_countlimit(mtp), 1220 memstat_get_count(mtp), memstat_get_free(mtp), 1221 memstat_get_numallocs(mtp), memstat_get_failures(mtp)); 1222 } 1223 memstat_mtl_free(mtlp); 1224 printf("\n"); 1225} 1226 1227/* 1228 * kread reads something from the kernel, given its nlist index. 1229 */ 1230static void 1231kreado(int nlx, void *addr, size_t size, size_t offset) 1232{ 1233 const char *sym; 1234 1235 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 1236 sym = namelist[nlx].n_name; 1237 if (*sym == '_') 1238 ++sym; 1239 errx(1, "symbol %s not defined", sym); 1240 } 1241 if ((size_t)kvm_read(kd, namelist[nlx].n_value + offset, addr, 1242 size) != size) { 1243 sym = namelist[nlx].n_name; 1244 if (*sym == '_') 1245 ++sym; 1246 errx(1, "%s: %s", sym, kvm_geterr(kd)); 1247 } 1248} 1249 1250static void 1251kread(int nlx, void *addr, size_t size) 1252{ 1253 kreado(nlx, addr, size, 0); 1254} 1255 1256static char * 1257kgetstr(const char *strp) 1258{ 1259 int n = 0, size = 1; 1260 char *ret = NULL; 1261 1262 do { 1263 if (size == n + 1) { 1264 ret = realloc(ret, size); 1265 if (ret == NULL) 1266 err(1, "%s: realloc", __func__); 1267 size *= 2; 1268 } 1269 if (kvm_read(kd, (u_long)strp + n, &ret[n], 1) != 1) 1270 errx(1, "%s: %s", __func__, kvm_geterr(kd)); 1271 } while (ret[n++] != '\0'); 1272 return (ret); 1273} 1274 1275static void 1276usage(void) 1277{ 1278 (void)fprintf(stderr, "%s%s", 1279 "usage: vmstat [-afHhimPsz] [-c count] [-M core [-N system]] [-w wait]\n", 1280 " [-n devs] [-p type,if,pass] [disks]\n"); 1281 exit(1); 1282} 1283