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 "$Id: vmstat.c,v 1.25 1998/09/15 08:16:43 gibbs Exp $"; 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 64 #include <ctype.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 #include <devstat.h> 78 79 struct nlist namelist[] = { 80 #define X_CPTIME 0 81 { "_cp_time" }, 82 #define X_SUM 1 83 { "_cnt" }, 84 #define X_BOOTTIME 2 85 { "_boottime" }, 86 #define X_HZ 3 87 { "_hz" }, 88 #define X_STATHZ 4 89 { "_stathz" }, 90 #define X_NCHSTATS 5 91 { "_nchstats" }, 92 #define X_INTRNAMES 6 93 { "_intrnames" }, 94 #define X_EINTRNAMES 7 95 { "_eintrnames" }, 96 #define X_INTRCNT 8 97 { "_intrcnt" }, 98 #define X_EINTRCNT 9 99 { "_eintrcnt" }, 100 #define X_KMEMSTATISTICS 10 101 { "_kmemstatistics" }, 102 #define X_KMEMBUCKETS 11 103 { "_bucket" }, 104 #ifdef notyet 105 #define X_DEFICIT 12 106 { "_deficit" }, 107 #define X_FORKSTAT 13 108 { "_forkstat" }, 109 #define X_REC 14 110 { "_rectime" }, 111 #define X_PGIN 15 112 { "_pgintime" }, 113 #define X_XSTATS 16 114 { "_xstats" }, 115 #define X_END 17 116 #else 117 #define X_END 14 118 #endif 119 #if defined(hp300) || defined(luna68k) 120 #define X_HPDINIT (X_END) 121 { "_hp_dinit" }, 122 #endif 123 #ifdef mips 124 #define X_SCSI_DINIT (X_END) 125 { "_scsi_dinit" }, 126 #endif 127 #ifdef tahoe 128 #define X_VBDINIT (X_END) 129 { "_vbdinit" }, 130 #define X_CKEYSTATS (X_END+1) 131 { "_ckeystats" }, 132 #define X_DKEYSTATS (X_END+2) 133 { "_dkeystats" }, 134 #endif 135 #ifdef vax 136 #define X_MBDINIT (X_END) 137 { "_mbdinit" }, 138 #define X_UBDINIT (X_END+1) 139 { "_ubdinit" }, 140 #endif 141 { "" }, 142 }; 143 144 struct statinfo cur, last; 145 int num_devices, maxshowdevs, generation; 146 struct device_selection *dev_select; 147 int num_selected; 148 struct devstat_match *matches; 149 int num_matches = 0; 150 int num_devices_specified, num_selections, select_generation; 151 char **specified_devices; 152 devstat_select_mode select_mode; 153 154 struct vmmeter sum, osum; 155 156 int winlines = 20; 157 int nflag = 0; 158 159 kvm_t *kd; 160 161 #define FORKSTAT 0x01 162 #define INTRSTAT 0x02 163 #define MEMSTAT 0x04 164 #define SUMSTAT 0x08 165 #define TIMESTAT 0x10 166 #define VMSTAT 0x20 167 168 void cpustats(), dointr(), domem(), dosum(); 169 void dovmstat(), kread(), usage(); 170 #ifdef notyet 171 void dotimes(), doforkst(); 172 #endif 173 void printhdr __P((void)); 174 static void devstats(); 175 176 int 177 main(argc, argv) 178 register int argc; 179 register char **argv; 180 { 181 register int c, todo; 182 u_int interval; 183 int reps; 184 char *memf, *nlistf; 185 char errbuf[_POSIX2_LINE_MAX]; 186 char *err_str; 187 188 memf = nlistf = NULL; 189 interval = reps = todo = 0; 190 maxshowdevs = 3; 191 while ((c = getopt(argc, argv, "c:fiM:mN:n:p:stw:")) != -1) { 192 switch (c) { 193 case 'c': 194 reps = atoi(optarg); 195 break; 196 case 'f': 197 #ifdef notyet 198 todo |= FORKSTAT; 199 #else 200 errx(EX_USAGE, "sorry, -f is not (re)implemented yet"); 201 #endif 202 break; 203 case 'i': 204 todo |= INTRSTAT; 205 break; 206 case 'M': 207 memf = optarg; 208 break; 209 case 'm': 210 todo |= MEMSTAT; 211 break; 212 case 'N': 213 nlistf = optarg; 214 break; 215 case 'n': 216 nflag = 1; 217 maxshowdevs = atoi(optarg); 218 if (maxshowdevs < 0) 219 errx(1, "number of devices %d is < 0", 220 maxshowdevs); 221 break; 222 case 'p': 223 if (buildmatch(optarg, &matches, &num_matches) != 0) 224 errx(1, "%s", devstat_errbuf); 225 break; 226 case 's': 227 todo |= SUMSTAT; 228 break; 229 case 't': 230 #ifdef notyet 231 todo |= TIMESTAT; 232 #else 233 errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); 234 #endif 235 break; 236 case 'w': 237 interval = atoi(optarg); 238 break; 239 case '?': 240 default: 241 usage(); 242 } 243 } 244 argc -= optind; 245 argv += optind; 246 247 if (todo == 0) 248 todo = VMSTAT; 249 250 /* 251 * Discard setgid privileges if not the running kernel so that bad 252 * guys can't print interesting stuff from kernel memory. 253 */ 254 if (nlistf != NULL || memf != NULL) 255 setgid(getgid()); 256 257 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 258 if (kd == 0) 259 errx(1, "kvm_openfiles: %s", errbuf); 260 261 if ((c = kvm_nlist(kd, namelist)) != 0) { 262 if (c > 0) { 263 warnx("undefined symbols:"); 264 for (c = 0; 265 c < sizeof(namelist)/sizeof(namelist[0]); c++) 266 if (namelist[c].n_type == 0) 267 fprintf(stderr, " %s", 268 namelist[c].n_name); 269 (void)fputc('\n', stderr); 270 } else 271 warnx("kvm_nlist: %s", kvm_geterr(kd)); 272 exit(1); 273 } 274 275 if (todo & VMSTAT) { 276 char **getdrivedata(); 277 struct winsize winsize; 278 279 argv = getdrivedata(argv); 280 winsize.ws_row = 0; 281 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 282 if (winsize.ws_row > 0) 283 winlines = winsize.ws_row; 284 285 } 286 287 #define BACKWARD_COMPATIBILITY 288 #ifdef BACKWARD_COMPATIBILITY 289 if (*argv) { 290 interval = atoi(*argv); 291 if (*++argv) 292 reps = atoi(*argv); 293 } 294 #endif 295 296 if (interval) { 297 if (!reps) 298 reps = -1; 299 } else if (reps) 300 interval = 1; 301 302 #ifdef notyet 303 if (todo & FORKSTAT) 304 doforkst(); 305 #endif 306 if (todo & MEMSTAT) 307 domem(); 308 if (todo & SUMSTAT) 309 dosum(); 310 #ifdef notyet 311 if (todo & TIMESTAT) 312 dotimes(); 313 #endif 314 if (todo & INTRSTAT) 315 dointr(); 316 if (todo & VMSTAT) 317 dovmstat(interval, reps); 318 exit(0); 319 } 320 321 char ** 322 getdrivedata(argv) 323 char **argv; 324 { 325 register int i; 326 char buf[30]; 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 void needhdr(); 416 int mib[2], 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 /* 428 * Make sure that the userland devstat version matches the kernel 429 * devstat version. If not, exit and print a message informing 430 * the user of his mistake. 431 */ 432 if (checkversion() < 0) 433 errx(1, "%s", devstat_errbuf); 434 435 for (hdrcnt = 1;;) { 436 if (!--hdrcnt) 437 printhdr(); 438 kread(X_CPTIME, cur.cp_time, sizeof(cur.cp_time)); 439 440 tmp_dinfo = last.dinfo; 441 last.dinfo = cur.dinfo; 442 cur.dinfo = tmp_dinfo; 443 last.busy_time = cur.busy_time; 444 445 /* 446 * Here what we want to do is refresh our device stats. 447 * getdevs() returns 1 when the device list has changed. 448 * If the device list has changed, we want to go through 449 * the selection process again, in case a device that we 450 * were previously displaying has gone away. 451 */ 452 switch (getdevs(&cur)) { 453 case -1: 454 errx(1, "%s", devstat_errbuf); 455 break; 456 case 1: { 457 int retval; 458 459 num_devices = cur.dinfo->numdevs; 460 generation = cur.dinfo->generation; 461 462 retval = selectdevs(&dev_select, &num_selected, 463 &num_selections, &select_generation, 464 generation, cur.dinfo->devices, 465 num_devices, matches, num_matches, 466 specified_devices, 467 num_devices_specified, select_mode, 468 maxshowdevs, 0); 469 switch (retval) { 470 case -1: 471 errx(1, "%s", devstat_errbuf); 472 break; 473 case 1: 474 printhdr(); 475 break; 476 default: 477 break; 478 } 479 } 480 default: 481 break; 482 } 483 484 kread(X_SUM, &sum, sizeof(sum)); 485 size = sizeof(total); 486 mib[0] = CTL_VM; 487 mib[1] = VM_METER; 488 if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) { 489 printf("Can't get kerninfo: %s\n", strerror(errno)); 490 bzero(&total, sizeof(total)); 491 } 492 (void)printf("%2d%2d%2d", 493 total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); 494 #define pgtok(a) ((a) * sum.v_page_size >> 10) 495 #define rate(x) (((x) + halfuptime) / uptime) /* round */ 496 (void)printf("%8ld%6ld ", 497 (long)pgtok(total.t_avm), (long)pgtok(total.t_free)); 498 (void)printf("%4lu ", 499 (u_long)rate(sum.v_vm_faults - osum.v_vm_faults)); 500 (void)printf("%3lu ", 501 (u_long)rate(sum.v_reactivated - osum.v_reactivated)); 502 (void)printf("%3lu ", 503 (u_long)rate(sum.v_swapin + sum.v_vnodein - 504 (osum.v_swapin + osum.v_vnodein))); 505 (void)printf("%3lu ", 506 (u_long)rate(sum.v_swapout + sum.v_vnodeout - 507 (osum.v_swapout + osum.v_vnodeout))); 508 (void)printf("%3lu ", 509 (u_long)rate(sum.v_tfree - osum.v_tfree)); 510 (void)printf("%3lu ", 511 (u_long)rate(sum.v_pdpages - osum.v_pdpages)); 512 devstats(); 513 (void)printf("%4lu %4lu %3lu ", 514 (u_long)rate(sum.v_intr - osum.v_intr), 515 (u_long)rate(sum.v_syscall - osum.v_syscall), 516 (u_long)rate(sum.v_swtch - osum.v_swtch)); 517 cpustats(); 518 (void)printf("\n"); 519 (void)fflush(stdout); 520 if (reps >= 0 && --reps <= 0) 521 break; 522 osum = sum; 523 uptime = interval; 524 /* 525 * We round upward to avoid losing low-frequency events 526 * (i.e., >= 1 per interval but < 1 per second). 527 */ 528 if (interval != 1) 529 halfuptime = (uptime + 1) / 2; 530 else 531 halfuptime = 0; 532 (void)sleep(interval); 533 } 534 } 535 536 void 537 printhdr() 538 { 539 register int i; 540 541 (void)printf(" procs memory page%*s", 19, ""); 542 if (num_selected > 1) 543 (void)printf("disks %*s faults cpu\n", 544 ((num_selected < maxshowdevs) ? num_selected : 545 maxshowdevs ) * 4 - 7, ""); 546 else if (num_selected == 1) 547 (void)printf("disk faults cpu\n"); 548 else 549 (void)printf("%*s faults cpu\n", num_selected * 4, ""); 550 551 (void)printf(" r b w avm fre flt re pi po fr sr "); 552 for (i = 0; i < num_devices; i++) 553 if ((dev_select[i].selected) 554 && (dev_select[i].selected <= maxshowdevs)) 555 (void)printf("%c%c%d ", dev_select[i].device_name[0], 556 dev_select[i].device_name[1], 557 dev_select[i].unit_number); 558 (void)printf(" in sy cs us sy id\n"); 559 hdrcnt = winlines - 2; 560 } 561 562 /* 563 * Force a header to be prepended to the next output. 564 */ 565 void 566 needhdr() 567 { 568 569 hdrcnt = 1; 570 } 571 572 #ifdef notyet 573 void 574 dotimes() 575 { 576 u_int pgintime, rectime; 577 578 kread(X_REC, &rectime, sizeof(rectime)); 579 kread(X_PGIN, &pgintime, sizeof(pgintime)); 580 kread(X_SUM, &sum, sizeof(sum)); 581 (void)printf("%u reclaims, %u total time (usec)\n", 582 sum.v_pgrec, rectime); 583 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); 584 (void)printf("\n"); 585 (void)printf("%u page ins, %u total time (msec)\n", 586 sum.v_pgin, pgintime / 10); 587 (void)printf("average: %8.1f msec / page in\n", 588 pgintime / (sum.v_pgin * 10.0)); 589 } 590 #endif 591 592 long 593 pct(top, bot) 594 long top, bot; 595 { 596 long ans; 597 598 if (bot == 0) 599 return(0); 600 ans = (quad_t)top * 100 / bot; 601 return (ans); 602 } 603 604 #define PCT(top, bot) pct((long)(top), (long)(bot)) 605 606 #if defined(tahoe) 607 #include <machine/cpu.h> 608 #endif 609 610 void 611 dosum() 612 { 613 struct nchstats nchstats; 614 long nchtotal; 615 #if defined(tahoe) 616 struct keystats keystats; 617 #endif 618 619 kread(X_SUM, &sum, sizeof(sum)); 620 (void)printf("%9u cpu context switches\n", sum.v_swtch); 621 (void)printf("%9u device interrupts\n", sum.v_intr); 622 (void)printf("%9u software interrupts\n", sum.v_soft); 623 #ifdef vax 624 (void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma); 625 #endif 626 (void)printf("%9u traps\n", sum.v_trap); 627 (void)printf("%9u system calls\n", sum.v_syscall); 628 (void)printf("%9u swap pager pageins\n", sum.v_swapin); 629 (void)printf("%9u swap pager pages paged in\n", sum.v_swappgsin); 630 (void)printf("%9u swap pager pageouts\n", sum.v_swapout); 631 (void)printf("%9u swap pager pages paged out\n", sum.v_swappgsout); 632 (void)printf("%9u vnode pager pageins\n", sum.v_vnodein); 633 (void)printf("%9u vnode pager pages paged in\n", sum.v_vnodepgsin); 634 (void)printf("%9u vnode pager pageouts\n", sum.v_vnodeout); 635 (void)printf("%9u vnode pager pages paged out\n", sum.v_vnodepgsout); 636 (void)printf("%9u page daemon wakeups\n", sum.v_pdwakeups); 637 (void)printf("%9u pages examined by the page daemon\n", sum.v_pdpages); 638 (void)printf("%9u pages reactivated\n", sum.v_reactivated); 639 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults); 640 (void)printf("%9u copy-on-write optimized faults\n", sum.v_cow_optim); 641 (void)printf("%9u zero fill pages zeroed\n", sum.v_zfod); 642 (void)printf("%9u zero fill pages prezeroed\n", sum.v_ozfod); 643 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); 644 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults); 645 (void)printf("%9u pages freed\n", sum.v_tfree); 646 (void)printf("%9u pages freed by daemon\n", sum.v_dfree); 647 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree); 648 (void)printf("%9u pages active\n", sum.v_active_count); 649 (void)printf("%9u pages inactive\n", sum.v_inactive_count); 650 (void)printf("%9u pages in VM cache\n", sum.v_cache_count); 651 (void)printf("%9u pages wired down\n", sum.v_wire_count); 652 (void)printf("%9u pages free\n", sum.v_free_count); 653 (void)printf("%9u bytes per page\n", sum.v_page_size); 654 kread(X_NCHSTATS, &nchstats, sizeof(nchstats)); 655 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 656 nchstats.ncs_badhits + nchstats.ncs_falsehits + 657 nchstats.ncs_miss + nchstats.ncs_long; 658 (void)printf("%9ld total name lookups\n", nchtotal); 659 (void)printf( 660 "%9s cache hits (%ld%% pos + %ld%% neg) system %ld%% per-directory\n", 661 "", PCT(nchstats.ncs_goodhits, nchtotal), 662 PCT(nchstats.ncs_neghits, nchtotal), 663 PCT(nchstats.ncs_pass2, nchtotal)); 664 (void)printf("%9s deletions %ld%%, falsehits %ld%%, toolong %ld%%\n", "", 665 PCT(nchstats.ncs_badhits, nchtotal), 666 PCT(nchstats.ncs_falsehits, nchtotal), 667 PCT(nchstats.ncs_long, nchtotal)); 668 #if defined(tahoe) 669 kread(X_CKEYSTATS, &keystats, sizeof(keystats)); 670 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", 671 keystats.ks_allocs, "code cache keys allocated", 672 PCT(keystats.ks_allocfree, keystats.ks_allocs), 673 PCT(keystats.ks_norefs, keystats.ks_allocs), 674 PCT(keystats.ks_taken, keystats.ks_allocs), 675 PCT(keystats.ks_shared, keystats.ks_allocs)); 676 kread(X_DKEYSTATS, &keystats, sizeof(keystats)); 677 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", 678 keystats.ks_allocs, "data cache keys allocated", 679 PCT(keystats.ks_allocfree, keystats.ks_allocs), 680 PCT(keystats.ks_norefs, keystats.ks_allocs), 681 PCT(keystats.ks_taken, keystats.ks_allocs), 682 PCT(keystats.ks_shared, keystats.ks_allocs)); 683 #endif 684 } 685 686 #ifdef notyet 687 void 688 doforkst() 689 { 690 struct forkstat fks; 691 692 kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); 693 (void)printf("%d forks, %d pages, average %.2f\n", 694 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); 695 (void)printf("%d vforks, %d pages, average %.2f\n", 696 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); 697 } 698 #endif 699 700 static void 701 devstats() 702 { 703 register int dn, state; 704 long double transfers_per_second; 705 long double busy_seconds; 706 long tmp; 707 708 for (state = 0; state < CPUSTATES; ++state) { 709 tmp = cur.cp_time[state]; 710 cur.cp_time[state] -= last.cp_time[state]; 711 last.cp_time[state] = tmp; 712 } 713 714 busy_seconds = compute_etime(cur.busy_time, last.busy_time); 715 716 for (dn = 0; dn < num_devices; dn++) { 717 int di; 718 719 if ((dev_select[dn].selected == 0) 720 || (dev_select[dn].selected > maxshowdevs)) 721 continue; 722 723 di = dev_select[dn].position; 724 725 if (compute_stats(&cur.dinfo->devices[di], 726 &last.dinfo->devices[di], busy_seconds, 727 NULL, NULL, NULL, 728 NULL, &transfers_per_second, NULL, 729 NULL, NULL) != 0) 730 errx(1, "%s", devstat_errbuf); 731 732 printf("%3.0Lf ", transfers_per_second); 733 } 734 } 735 736 void 737 cpustats() 738 { 739 register int state; 740 double pct, total; 741 742 total = 0; 743 for (state = 0; state < CPUSTATES; ++state) 744 total += cur.cp_time[state]; 745 if (total) 746 pct = 100 / total; 747 else 748 pct = 0; 749 (void)printf("%2.0f ", (cur.cp_time[CP_USER] + 750 cur.cp_time[CP_NICE]) * pct); 751 (void)printf("%2.0f ", (cur.cp_time[CP_SYS] + 752 cur.cp_time[CP_INTR]) * pct); 753 (void)printf("%2.0f", cur.cp_time[CP_IDLE] * pct); 754 } 755 756 void 757 dointr() 758 { 759 register long *intrcnt, inttotal, uptime; 760 register int nintr, inamlen; 761 register char *intrname; 762 763 uptime = getuptime(); 764 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value; 765 inamlen = 766 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value; 767 intrcnt = malloc((size_t)nintr); 768 intrname = malloc((size_t)inamlen); 769 if (intrcnt == NULL || intrname == NULL) 770 errx(1, "malloc"); 771 kread(X_INTRCNT, intrcnt, (size_t)nintr); 772 kread(X_INTRNAMES, intrname, (size_t)inamlen); 773 (void)printf("interrupt total rate\n"); 774 inttotal = 0; 775 nintr /= sizeof(long); 776 while (--nintr >= 0) { 777 if (*intrcnt) 778 (void)printf("%-12s %8ld %8ld\n", intrname, 779 *intrcnt, *intrcnt / uptime); 780 intrname += strlen(intrname) + 1; 781 inttotal += *intrcnt++; 782 } 783 (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime); 784 } 785 786 void 787 domem() 788 { 789 register struct kmembuckets *kp; 790 register struct malloc_type *ks; 791 register int i, j; 792 int len, size, first, nkms; 793 long totuse = 0, totfree = 0, totreq = 0; 794 const char *name; 795 struct malloc_type kmemstats[200], *kmsp; 796 char *kmemnames[200]; 797 char buf[1024]; 798 struct kmembuckets buckets[MINBUCKET + 16]; 799 800 kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); 801 kread(X_KMEMSTATISTICS, &kmsp, sizeof(kmsp)); 802 for (nkms = 0; nkms < 200 && kmsp != NULL; nkms++) { 803 if (sizeof(kmemstats[0]) != kvm_read(kd, (u_long)kmsp, 804 &kmemstats[nkms], sizeof(kmemstats[0]))) 805 err(1, "kvm_read(%p)", (void *)kmsp); 806 if (sizeof(buf) != kvm_read(kd, 807 (u_long)kmemstats[nkms].ks_shortdesc, buf, sizeof(buf))) 808 err(1, "kvm_read(%p)", 809 (void *)kmemstats[nkms].ks_shortdesc); 810 kmemstats[nkms].ks_shortdesc = strdup(buf); 811 kmsp = kmemstats[nkms].ks_next; 812 } 813 (void)printf("Memory statistics by bucket size\n"); 814 (void)printf( 815 "Size In Use Free Requests HighWater Couldfree\n"); 816 for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) { 817 if (kp->kb_calls == 0) 818 continue; 819 size = 1 << i; 820 if(size < 1024) 821 (void)printf("%4d",size); 822 else 823 (void)printf("%3dK",size>>10); 824 (void)printf(" %8ld %6ld %10ld %7ld %10ld\n", 825 kp->kb_total - kp->kb_totalfree, 826 kp->kb_totalfree, kp->kb_calls, 827 kp->kb_highwat, kp->kb_couldfree); 828 totfree += size * kp->kb_totalfree; 829 } 830 831 (void)printf("\nMemory usage type by bucket size\n"); 832 (void)printf("Size Type(s)\n"); 833 kp = &buckets[MINBUCKET]; 834 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) { 835 if (kp->kb_calls == 0) 836 continue; 837 first = 1; 838 len = 8; 839 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) { 840 if (ks->ks_calls == 0) 841 continue; 842 if ((ks->ks_size & j) == 0) 843 continue; 844 name = ks->ks_shortdesc; 845 len += 2 + strlen(name); 846 if (first && j < 1024) 847 printf("%4d %s", j, name); 848 else if (first) 849 printf("%3dK %s", j>>10, name); 850 else 851 printf(","); 852 if (len >= 79) { 853 printf("\n\t "); 854 len = 10 + strlen(name); 855 } 856 if (!first) 857 printf(" %s", name); 858 first = 0; 859 } 860 printf("\n"); 861 } 862 863 (void)printf( 864 "\nMemory statistics by type Type Kern\n"); 865 (void)printf( 866 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n"); 867 for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) { 868 if (ks->ks_calls == 0) 869 continue; 870 (void)printf("%13s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u", 871 ks->ks_shortdesc, 872 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024, 873 (ks->ks_maxused + 1023) / 1024, 874 (ks->ks_limit + 1023) / 1024, ks->ks_calls, 875 ks->ks_limblocks, ks->ks_mapblocks); 876 first = 1; 877 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 878 if ((ks->ks_size & j) == 0) 879 continue; 880 if (first) 881 printf(" "); 882 else 883 printf(","); 884 if(j<1024) 885 printf("%d",j); 886 else 887 printf("%dK",j>>10); 888 first = 0; 889 } 890 printf("\n"); 891 totuse += ks->ks_memuse; 892 totreq += ks->ks_calls; 893 } 894 (void)printf("\nMemory Totals: In Use Free Requests\n"); 895 (void)printf(" %7ldK %6ldK %8ld\n", 896 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); 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) != 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, 928 "usage: vmstat [-ims] [-c count] [-M core] [-N system] [-w wait] [disks]\n"); 929 exit(1); 930 } 931