1 /* 2 * Copyright (c) 1980, 1986, 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980, 1986, 1991 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)vmstat.c 5.37 (Berkeley) 09/02/92"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/time.h> 20 #include <sys/proc.h> 21 #include <sys/user.h> 22 #include <sys/dkstat.h> 23 #include <sys/buf.h> 24 #include <sys/namei.h> 25 #include <sys/malloc.h> 26 #include <sys/signal.h> 27 #include <sys/fcntl.h> 28 #include <sys/ioctl.h> 29 #include <sys/kinfo.h> 30 #include <vm/vm.h> 31 #include <time.h> 32 #include <nlist.h> 33 #include <kvm.h> 34 #include <errno.h> 35 #include <unistd.h> 36 #include <stdio.h> 37 #include <ctype.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <paths.h> 41 #include <limits.h> 42 43 #define NEWVM /* XXX till old has been updated or purged */ 44 struct nlist nl[] = { 45 #define X_CPTIME 0 46 { "_cp_time" }, 47 #define X_DK_NDRIVE 1 48 { "_dk_ndrive" }, 49 #define X_SUM 2 50 { "_cnt" }, 51 #define X_BOOTTIME 3 52 { "_boottime" }, 53 #define X_DKXFER 4 54 { "_dk_xfer" }, 55 #define X_HZ 5 56 { "_hz" }, 57 #define X_STATHZ 6 58 { "_stathz" }, 59 #define X_NCHSTATS 7 60 { "_nchstats" }, 61 #define X_INTRNAMES 8 62 { "_intrnames" }, 63 #define X_EINTRNAMES 9 64 { "_eintrnames" }, 65 #define X_INTRCNT 10 66 { "_intrcnt" }, 67 #define X_EINTRCNT 11 68 { "_eintrcnt" }, 69 #define X_KMEMSTAT 12 70 { "_kmemstats" }, 71 #define X_KMEMBUCKETS 13 72 { "_bucket" }, 73 #ifdef notdef 74 #define X_DEFICIT 14 75 { "_deficit" }, 76 #define X_FORKSTAT 15 77 { "_forkstat" }, 78 #define X_REC 16 79 { "_rectime" }, 80 #define X_PGIN 17 81 { "_pgintime" }, 82 #define X_XSTATS 18 83 { "_xstats" }, 84 #define X_END 18 85 #else 86 #define X_END 14 87 #endif 88 #if defined(hp300) || defined(luna68k) 89 #define X_HPDINIT (X_END) 90 { "_hp_dinit" }, 91 #endif 92 #ifdef tahoe 93 #define X_VBDINIT (X_END) 94 { "_vbdinit" }, 95 #define X_CKEYSTATS (X_END+1) 96 { "_ckeystats" }, 97 #define X_DKEYSTATS (X_END+2) 98 { "_dkeystats" }, 99 #endif 100 #ifdef vax 101 #define X_MBDINIT (X_END) 102 { "_mbdinit" }, 103 #define X_UBDINIT (X_END+1) 104 { "_ubdinit" }, 105 #endif 106 { "" }, 107 }; 108 109 struct _disk { 110 long time[CPUSTATES]; 111 long *xfer; 112 } cur, last; 113 114 struct vmmeter sum, osum; 115 char **dr_name; 116 int *dr_select, dk_ndrive, ndrives; 117 118 int winlines = 20; 119 120 kvm_t *kd; 121 122 #define FORKSTAT 0x01 123 #define INTRSTAT 0x02 124 #define MEMSTAT 0x04 125 #define SUMSTAT 0x08 126 #define TIMESTAT 0x10 127 #define VMSTAT 0x20 128 129 #include "names.c" /* disk names -- machine dependent */ 130 131 void cpustats(), dkstats(), dointr(), domem(), dosum(); 132 void dovmstat(), kread(), usage(); 133 #ifdef notdef 134 void dotimes(), doforkst(); 135 #endif 136 137 main(argc, argv) 138 register int argc; 139 register char **argv; 140 { 141 extern int optind; 142 extern char *optarg; 143 register int c, todo; 144 u_int interval; 145 int reps; 146 char *memf, *nlistf; 147 char errbuf[_POSIX2_LINE_MAX]; 148 149 memf = nlistf = NULL; 150 interval = reps = todo = 0; 151 while ((c = getopt(argc, argv, "c:fiM:mN:stw:")) != EOF) { 152 switch (c) { 153 case 'c': 154 reps = atoi(optarg); 155 break; 156 #ifndef notdef 157 case 'f': 158 todo |= FORKSTAT; 159 break; 160 #endif 161 case 'i': 162 todo |= INTRSTAT; 163 break; 164 case 'M': 165 memf = optarg; 166 break; 167 case 'm': 168 todo |= MEMSTAT; 169 break; 170 case 'N': 171 nlistf = optarg; 172 break; 173 case 's': 174 todo |= SUMSTAT; 175 break; 176 #ifndef notdef 177 case 't': 178 todo |= TIMESTAT; 179 break; 180 #endif 181 case 'w': 182 interval = atoi(optarg); 183 break; 184 case '?': 185 default: 186 usage(); 187 } 188 } 189 argc -= optind; 190 argv += optind; 191 192 if (todo == 0) 193 todo = VMSTAT; 194 195 /* 196 * Discard setgid privileges if not the running kernel so that bad 197 * guys can't print interesting stuff from kernel memory. 198 */ 199 if (nlistf != NULL || memf != NULL) 200 setgid(getgid()); 201 202 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 203 if (kd == 0) { 204 (void)fprintf(stderr, 205 "vmstat: kvm_openfiles: %s\n", errbuf); 206 exit(1); 207 } 208 209 if ((c = kvm_nlist(kd, nl)) != 0) { 210 if (c > 0) { 211 (void)fprintf(stderr, 212 "vmstat: undefined symbols: "); 213 for (c = 0; c < sizeof(nl)/sizeof(nl[0]); c++) 214 if (nl[c].n_type == 0) 215 printf(" %s", nl[c].n_name); 216 (void)fputc('\n', stderr); 217 } else 218 (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n", 219 kvm_geterr(kd)); 220 exit(1); 221 } 222 223 if (todo & VMSTAT) { 224 char **getdrivedata(); 225 struct winsize winsize; 226 227 argv = getdrivedata(argv); 228 winsize.ws_row = 0; 229 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 230 if (winsize.ws_row > 0) 231 winlines = winsize.ws_row; 232 233 } 234 235 #define BACKWARD_COMPATIBILITY 236 #ifdef BACKWARD_COMPATIBILITY 237 if (*argv) { 238 interval = atoi(*argv); 239 if (*++argv) 240 reps = atoi(*argv); 241 } 242 #endif 243 244 if (interval) { 245 if (!reps) 246 reps = -1; 247 } else if (reps) 248 interval = 1; 249 250 #ifdef notdef 251 if (todo & FORKSTAT) 252 doforkst(); 253 #endif 254 if (todo & MEMSTAT) 255 domem(); 256 if (todo & SUMSTAT) 257 dosum(); 258 #ifdef notdef 259 if (todo & TIMESTAT) 260 dotimes(); 261 #endif 262 if (todo & INTRSTAT) 263 dointr(); 264 if (todo & VMSTAT) 265 dovmstat(interval, reps); 266 exit(0); 267 } 268 269 char ** 270 getdrivedata(argv) 271 char **argv; 272 { 273 register int i; 274 register char **cp; 275 char buf[30]; 276 277 kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive)); 278 if (dk_ndrive <= 0) { 279 (void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive); 280 exit(1); 281 } 282 dr_select = calloc((size_t)dk_ndrive, sizeof(int)); 283 dr_name = calloc((size_t)dk_ndrive, sizeof(char *)); 284 for (i = 0; i < dk_ndrive; i++) 285 dr_name[i] = NULL; 286 cur.xfer = calloc((size_t)dk_ndrive, sizeof(long)); 287 last.xfer = calloc((size_t)dk_ndrive, sizeof(long)); 288 if (!read_names()) 289 exit (1); 290 for (i = 0; i < dk_ndrive; i++) 291 if (dr_name[i] == NULL) { 292 (void)sprintf(buf, "??%d", i); 293 dr_name[i] = strdup(buf); 294 } 295 296 /* 297 * Choose drives to be displayed. Priority goes to (in order) drives 298 * supplied as arguments, default drives. If everything isn't filled 299 * in and there are drives not taken care of, display the first few 300 * that fit. 301 */ 302 #define BACKWARD_COMPATIBILITY 303 for (ndrives = 0; *argv; ++argv) { 304 #ifdef BACKWARD_COMPATIBILITY 305 if (isdigit(**argv)) 306 break; 307 #endif 308 for (i = 0; i < dk_ndrive; i++) { 309 if (strcmp(dr_name[i], *argv)) 310 continue; 311 dr_select[i] = 1; 312 ++ndrives; 313 break; 314 } 315 } 316 for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 317 if (dr_select[i]) 318 continue; 319 for (cp = defdrives; *cp; cp++) 320 if (strcmp(dr_name[i], *cp) == 0) { 321 dr_select[i] = 1; 322 ++ndrives; 323 break; 324 } 325 } 326 for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 327 if (dr_select[i]) 328 continue; 329 dr_select[i] = 1; 330 ++ndrives; 331 } 332 return(argv); 333 } 334 335 long 336 getuptime() 337 { 338 static time_t now, boottime; 339 time_t uptime; 340 341 if (boottime == 0) 342 kread(X_BOOTTIME, &boottime, sizeof(boottime)); 343 (void)time(&now); 344 uptime = now - boottime; 345 if (uptime <= 0 || uptime > 60*60*24*365*10) { 346 (void)fprintf(stderr, 347 "vmstat: time makes no sense; namelist must be wrong.\n"); 348 exit(1); 349 } 350 return(uptime); 351 } 352 353 int hz, hdrcnt; 354 355 void 356 dovmstat(interval, reps) 357 u_int interval; 358 int reps; 359 { 360 struct vmtotal total; 361 time_t uptime, halfuptime; 362 void needhdr(); 363 int size; 364 365 uptime = getuptime(); 366 halfuptime = uptime / 2; 367 (void)signal(SIGCONT, needhdr); 368 369 if (nl[X_STATHZ].n_type != 0 && nl[X_STATHZ].n_value != 0) 370 kread(X_STATHZ, &hz, sizeof(hz)); 371 if (!hz) 372 kread(X_HZ, &hz, sizeof(hz)); 373 374 for (hdrcnt = 1;;) { 375 if (!--hdrcnt) 376 printhdr(); 377 kread(X_CPTIME, cur.time, sizeof(cur.time)); 378 kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer) * dk_ndrive); 379 kread(X_SUM, &sum, sizeof(sum)); 380 size = sizeof(total); 381 if (getkerninfo(KINFO_METER, &total, &size, 0) < 0) { 382 printf("Can't get kerninfo: %s\n", strerror(errno)); 383 bzero(&total, sizeof(total)); 384 } 385 (void)printf("%2d%2d%2d", 386 total.t_rq, total.t_dw + total.t_pw, total.t_sw); 387 #define pgtok(a) ((a) * sum.v_page_size >> 10) 388 #define rate(x) (((x) + halfuptime) / uptime) /* round */ 389 (void)printf("%6ld%6ld ", 390 pgtok(total.t_avm), pgtok(total.t_free)); 391 #ifdef NEWVM 392 (void)printf("%4lu ", rate(sum.v_faults - osum.v_faults)); 393 (void)printf("%3lu ", 394 rate(sum.v_reactivated - osum.v_reactivated)); 395 (void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins)); 396 (void)printf("%3lu %3lu ", 397 rate(sum.v_pageouts - osum.v_pageouts), 0); 398 #else 399 (void)printf("%3lu %2lu ", 400 rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) - 401 (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))), 402 rate(sum.v_xsfrec + sum.v_xifrec - 403 osum.v_xsfrec - osum.v_xifrec)); 404 (void)printf("%3lu ", 405 rate(pgtok(sum.v_pgpgin - osum.v_pgpgin))); 406 (void)printf("%3lu %3lu ", 407 rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)), 408 rate(pgtok(sum.v_dfree - osum.v_dfree))); 409 (void)printf("%3d ", pgtok(deficit)); 410 #endif 411 (void)printf("%3lu ", rate(sum.v_scan - osum.v_scan)); 412 dkstats(); 413 (void)printf("%4lu %4lu %3lu ", 414 rate(sum.v_intr - osum.v_intr), 415 rate(sum.v_syscall - osum.v_syscall), 416 rate(sum.v_swtch - osum.v_swtch)); 417 cpustats(); 418 (void)printf("\n"); 419 (void)fflush(stdout); 420 if (reps >= 0 && --reps <= 0) 421 break; 422 osum = sum; 423 uptime = interval; 424 /* 425 * We round upward to avoid losing low-frequency events 426 * (i.e., >= 1 per interval but < 1 per second). 427 */ 428 halfuptime = (uptime + 1) / 2; 429 (void)sleep(interval); 430 } 431 } 432 433 printhdr() 434 { 435 register int i; 436 437 (void)printf(" procs memory page%*s", 20, ""); 438 if (ndrives > 1) 439 (void)printf("disks %*s faults cpu\n", 440 ndrives * 3 - 6, ""); 441 else 442 (void)printf("%*s faults cpu\n", ndrives * 3, ""); 443 #ifndef NEWVM 444 (void)printf(" r b w avm fre re at pi po fr de sr "); 445 #else 446 (void)printf(" r b w avm fre flt re pi po fr sr "); 447 #endif 448 for (i = 0; i < dk_ndrive; i++) 449 if (dr_select[i]) 450 (void)printf("%c%c ", dr_name[i][0], 451 dr_name[i][strlen(dr_name[i]) - 1]); 452 (void)printf(" in sy cs us sy id\n"); 453 hdrcnt = winlines - 2; 454 } 455 456 /* 457 * Force a header to be prepended to the next output. 458 */ 459 void 460 needhdr() 461 { 462 463 hdrcnt = 1; 464 } 465 466 #ifdef notdef 467 void 468 dotimes() 469 { 470 u_int pgintime, rectime; 471 472 kread(X_REC, &rectime, sizeof(rectime)); 473 kread(X_PGIN, &pgintime, sizeof(pgintime)); 474 kread(X_SUM, &sum, sizeof(sum)); 475 (void)printf("%u reclaims, %u total time (usec)\n", 476 sum.v_pgrec, rectime); 477 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); 478 (void)printf("\n"); 479 (void)printf("%u page ins, %u total time (msec)\n", 480 sum.v_pgin, pgintime / 10); 481 (void)printf("average: %8.1f msec / page in\n", 482 pgintime / (sum.v_pgin * 10.0)); 483 } 484 #endif 485 486 pct(top, bot) 487 long top, bot; 488 { 489 if (bot == 0) 490 return(0); 491 return((top * 100) / bot); 492 } 493 494 #define PCT(top, bot) pct((long)(top), (long)(bot)) 495 496 #if defined(tahoe) 497 #include <machine/cpu.h> 498 #endif 499 500 void 501 dosum() 502 { 503 struct nchstats nchstats; 504 #ifndef NEWVM 505 struct xstats xstats; 506 #endif 507 long nchtotal; 508 #if defined(tahoe) 509 struct keystats keystats; 510 #endif 511 512 kread(X_SUM, &sum, sizeof(sum)); 513 (void)printf("%9u cpu context switches\n", sum.v_swtch); 514 (void)printf("%9u device interrupts\n", sum.v_intr); 515 (void)printf("%9u software interrupts\n", sum.v_soft); 516 #ifdef vax 517 (void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma); 518 #endif 519 (void)printf("%9u traps\n", sum.v_trap); 520 (void)printf("%9u system calls\n", sum.v_syscall); 521 (void)printf("%9u total faults taken\n", sum.v_faults); 522 (void)printf("%9u swap ins\n", sum.v_swpin); 523 (void)printf("%9u swap outs\n", sum.v_swpout); 524 (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE); 525 (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE); 526 (void)printf("%9u page ins\n", sum.v_pageins); 527 (void)printf("%9u page outs\n", sum.v_pageouts); 528 (void)printf("%9u pages paged in\n", sum.v_pgpgin); 529 (void)printf("%9u pages paged out\n", sum.v_pgpgout); 530 (void)printf("%9u pages reactivated\n", sum.v_reactivated); 531 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); 532 (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE); 533 (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE); 534 (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan); 535 (void)printf("%9u revolutions of the clock hand\n", sum.v_rev); 536 #ifdef NEWVM 537 (void)printf("%9u VM object cache lookups\n", sum.v_lookups); 538 (void)printf("%9u VM object hits\n", sum.v_hits); 539 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults); 540 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults); 541 (void)printf("%9u pages freed by daemon\n", sum.v_dfree); 542 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree); 543 (void)printf("%9u pages free\n", sum.v_free_count); 544 (void)printf("%9u pages wired down\n", sum.v_wire_count); 545 (void)printf("%9u pages active\n", sum.v_active_count); 546 (void)printf("%9u pages inactive\n", sum.v_inactive_count); 547 (void)printf("%9u bytes per page\n", sum.v_page_size); 548 #else 549 (void)printf("%9u sequential process pages freed\n", sum.v_seqfree); 550 (void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec, 551 PCT(sum.v_fastpgrec, sum.v_pgrec)); 552 (void)printf("%9u reclaims from free list\n", sum.v_pgfrec); 553 (void)printf("%9u executable fill pages created\n", 554 sum.v_nexfod / CLSIZE); 555 (void)printf("%9u executable fill page faults\n", 556 sum.v_exfod / CLSIZE); 557 (void)printf("%9u swap text pages found in free list\n", 558 sum.v_xsfrec); 559 (void)printf("%9u inode text pages found in free list\n", 560 sum.v_xifrec); 561 (void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE); 562 (void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE); 563 (void)printf("%9u pages freed by the clock daemon\n", 564 sum.v_dfree / CLSIZE); 565 #endif 566 kread(X_NCHSTATS, &nchstats, sizeof(nchstats)); 567 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 568 nchstats.ncs_badhits + nchstats.ncs_falsehits + 569 nchstats.ncs_miss + nchstats.ncs_long; 570 (void)printf("%9ld total name lookups\n", nchtotal); 571 (void)printf( 572 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", 573 "", PCT(nchstats.ncs_goodhits, nchtotal), 574 PCT(nchstats.ncs_neghits, nchtotal), 575 PCT(nchstats.ncs_pass2, nchtotal)); 576 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", 577 PCT(nchstats.ncs_badhits, nchtotal), 578 PCT(nchstats.ncs_falsehits, nchtotal), 579 PCT(nchstats.ncs_long, nchtotal)); 580 #ifndef NEWVM 581 kread(X_XSTATS, &xstats, sizeof(xstats)); 582 (void)printf("%9lu total calls to xalloc (cache hits %d%%)\n", 583 xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc)); 584 (void)printf("%9s sticky %lu flushed %lu unused %lu\n", "", 585 xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused); 586 (void)printf("%9lu total calls to xfree", xstats.free); 587 (void)printf(" (sticky %lu cached %lu swapped %lu)\n", 588 xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap); 589 #endif 590 #if defined(tahoe) 591 kread(X_CKEYSTATS, &keystats, sizeof(keystats)); 592 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", 593 keystats.ks_allocs, "code cache keys allocated", 594 PCT(keystats.ks_allocfree, keystats.ks_allocs), 595 PCT(keystats.ks_norefs, keystats.ks_allocs), 596 PCT(keystats.ks_taken, keystats.ks_allocs), 597 PCT(keystats.ks_shared, keystats.ks_allocs)); 598 kread(X_DKEYSTATS, &keystats, sizeof(keystats)); 599 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", 600 keystats.ks_allocs, "data cache keys allocated", 601 PCT(keystats.ks_allocfree, keystats.ks_allocs), 602 PCT(keystats.ks_norefs, keystats.ks_allocs), 603 PCT(keystats.ks_taken, keystats.ks_allocs), 604 PCT(keystats.ks_shared, keystats.ks_allocs)); 605 #endif 606 } 607 608 #ifdef notdef 609 void 610 doforkst() 611 { 612 struct forkstat fks; 613 614 kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); 615 (void)printf("%d forks, %d pages, average %.2f\n", 616 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); 617 (void)printf("%d vforks, %d pages, average %.2f\n", 618 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); 619 } 620 #endif 621 622 void 623 dkstats() 624 { 625 register int dn, state; 626 double etime; 627 long tmp; 628 629 for (dn = 0; dn < dk_ndrive; ++dn) { 630 tmp = cur.xfer[dn]; 631 cur.xfer[dn] -= last.xfer[dn]; 632 last.xfer[dn] = tmp; 633 } 634 etime = 0; 635 for (state = 0; state < CPUSTATES; ++state) { 636 tmp = cur.time[state]; 637 cur.time[state] -= last.time[state]; 638 last.time[state] = tmp; 639 etime += cur.time[state]; 640 } 641 if (etime == 0) 642 etime = 1; 643 etime /= hz; 644 for (dn = 0; dn < dk_ndrive; ++dn) { 645 if (!dr_select[dn]) 646 continue; 647 (void)printf("%2.0f ", cur.xfer[dn] / etime); 648 } 649 } 650 651 void 652 cpustats() 653 { 654 register int state; 655 double pct, total; 656 657 total = 0; 658 for (state = 0; state < CPUSTATES; ++state) 659 total += cur.time[state]; 660 if (total) 661 pct = 100 / total; 662 else 663 pct = 0; 664 (void)printf("%2.0f ", /* user + nice */ 665 (cur.time[0] + cur.time[1]) * pct); 666 (void)printf("%2.0f ", cur.time[2] * pct); /* system */ 667 (void)printf("%2.0f", cur.time[3] * pct); /* idle */ 668 } 669 670 void 671 dointr() 672 { 673 register long *intrcnt, inttotal, uptime; 674 register int nintr, inamlen; 675 register char *intrname; 676 677 uptime = getuptime(); 678 nintr = nl[X_EINTRCNT].n_value - nl[X_INTRCNT].n_value; 679 inamlen = nl[X_EINTRNAMES].n_value - nl[X_INTRNAMES].n_value; 680 intrcnt = malloc((size_t)nintr); 681 intrname = malloc((size_t)inamlen); 682 if (intrcnt == NULL || intrname == NULL) { 683 (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno)); 684 exit(1); 685 } 686 kread(X_INTRCNT, intrcnt, (size_t)nintr); 687 kread(X_INTRNAMES, intrname, (size_t)inamlen); 688 (void)printf("interrupt total rate\n"); 689 inttotal = 0; 690 nintr /= sizeof(long); 691 while (--nintr >= 0) { 692 if (*intrcnt) 693 (void)printf("%-12s %8ld %8ld\n", intrname, 694 *intrcnt, *intrcnt / uptime); 695 intrname += strlen(intrname) + 1; 696 inttotal += *intrcnt++; 697 } 698 (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime); 699 } 700 701 /* 702 * These names are defined in <sys/malloc.h>. 703 */ 704 char *kmemnames[] = INITKMEMNAMES; 705 706 void 707 domem() 708 { 709 register struct kmembuckets *kp; 710 register struct kmemstats *ks; 711 register int i; 712 int size; 713 long totuse = 0, totfree = 0, totreq = 0; 714 struct kmemstats kmemstats[M_LAST]; 715 struct kmembuckets buckets[MINBUCKET + 16]; 716 717 kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); 718 (void)printf("Memory statistics by bucket size\n"); 719 (void)printf( 720 " Size In Use Free Requests HighWater Couldfree\n"); 721 for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) { 722 if (kp->kb_calls == 0) 723 continue; 724 size = 1 << i; 725 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size, 726 kp->kb_total - kp->kb_totalfree, 727 kp->kb_totalfree, kp->kb_calls, 728 kp->kb_highwat, kp->kb_couldfree); 729 totfree += size * kp->kb_totalfree; 730 } 731 732 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats)); 733 (void)printf("\nMemory statistics by type\n"); 734 (void)printf( 735 " Type In Use MemUse HighUse Limit Requests TypeLimit KernLimit\n"); 736 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { 737 if (ks->ks_calls == 0) 738 continue; 739 (void)printf("%10s %6ld %7ldK %8ldK %5ldK %8ld %6u %9u\n", 740 kmemnames[i] ? kmemnames[i] : "undefined", 741 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024, 742 (ks->ks_maxused + 1023) / 1024, 743 (ks->ks_limit + 1023) / 1024, ks->ks_calls, 744 ks->ks_limblocks, ks->ks_mapblocks); 745 totuse += ks->ks_memuse; 746 totreq += ks->ks_calls; 747 } 748 (void)printf("\nMemory Totals: In Use Free Requests\n"); 749 (void)printf(" %7ldK %6ldK %8ld\n", 750 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); 751 } 752 753 /* 754 * kread reads something from the kernel, given its nlist index. 755 */ 756 void 757 kread(nlx, addr, size) 758 int nlx; 759 void *addr; 760 size_t size; 761 { 762 char *sym; 763 764 if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0) { 765 sym = nl[nlx].n_name; 766 if (*sym == '_') 767 ++sym; 768 (void)fprintf(stderr, 769 "vmstat: symbol %s not defined\n", sym); 770 exit(1); 771 } 772 if (kvm_read(kd, nl[nlx].n_value, addr, size) != size) { 773 sym = nl[nlx].n_name; 774 if (*sym == '_') 775 ++sym; 776 (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd)); 777 exit(1); 778 } 779 } 780 781 void 782 usage() 783 { 784 (void)fprintf(stderr, 785 #ifndef NEWVM 786 "usage: vmstat [-fimst] [-c count] [-M core] \ 787 [-N system] [-w wait] [disks]\n"); 788 #else 789 "usage: vmstat [-ims] [-c count] [-M core] \ 790 [-N system] [-w wait] [disks]\n"); 791 #endif 792 exit(1); 793 } 794