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