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