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