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