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.39 (Berkeley) 10/22/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 namelist[] = { 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, namelist)) != 0) { 210 if (c > 0) { 211 (void)fprintf(stderr, 212 "vmstat: undefined symbols: "); 213 for (c = 0; 214 c < sizeof(namelist)/sizeof(namelist[0]); c++) 215 if (namelist[c].n_type == 0) 216 printf(" %s", namelist[c].n_name); 217 (void)fputc('\n', stderr); 218 } else 219 (void)fprintf(stderr, "vmstat: kvm_nlist: %s\n", 220 kvm_geterr(kd)); 221 exit(1); 222 } 223 224 if (todo & VMSTAT) { 225 char **getdrivedata(); 226 struct winsize winsize; 227 228 argv = getdrivedata(argv); 229 winsize.ws_row = 0; 230 (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); 231 if (winsize.ws_row > 0) 232 winlines = winsize.ws_row; 233 234 } 235 236 #define BACKWARD_COMPATIBILITY 237 #ifdef BACKWARD_COMPATIBILITY 238 if (*argv) { 239 interval = atoi(*argv); 240 if (*++argv) 241 reps = atoi(*argv); 242 } 243 #endif 244 245 if (interval) { 246 if (!reps) 247 reps = -1; 248 } else if (reps) 249 interval = 1; 250 251 #ifdef notdef 252 if (todo & FORKSTAT) 253 doforkst(); 254 #endif 255 if (todo & MEMSTAT) 256 domem(); 257 if (todo & SUMSTAT) 258 dosum(); 259 #ifdef notdef 260 if (todo & TIMESTAT) 261 dotimes(); 262 #endif 263 if (todo & INTRSTAT) 264 dointr(); 265 if (todo & VMSTAT) 266 dovmstat(interval, reps); 267 exit(0); 268 } 269 270 char ** 271 getdrivedata(argv) 272 char **argv; 273 { 274 register int i; 275 register char **cp; 276 char buf[30]; 277 278 kread(X_DK_NDRIVE, &dk_ndrive, sizeof(dk_ndrive)); 279 if (dk_ndrive <= 0) { 280 (void)fprintf(stderr, "vmstat: dk_ndrive %d\n", dk_ndrive); 281 exit(1); 282 } 283 dr_select = calloc((size_t)dk_ndrive, sizeof(int)); 284 dr_name = calloc((size_t)dk_ndrive, sizeof(char *)); 285 for (i = 0; i < dk_ndrive; i++) 286 dr_name[i] = NULL; 287 cur.xfer = calloc((size_t)dk_ndrive, sizeof(long)); 288 last.xfer = calloc((size_t)dk_ndrive, sizeof(long)); 289 if (!read_names()) 290 exit (1); 291 for (i = 0; i < dk_ndrive; i++) 292 if (dr_name[i] == NULL) { 293 (void)sprintf(buf, "??%d", i); 294 dr_name[i] = strdup(buf); 295 } 296 297 /* 298 * Choose drives to be displayed. Priority goes to (in order) drives 299 * supplied as arguments, default drives. If everything isn't filled 300 * in and there are drives not taken care of, display the first few 301 * that fit. 302 */ 303 #define BACKWARD_COMPATIBILITY 304 for (ndrives = 0; *argv; ++argv) { 305 #ifdef BACKWARD_COMPATIBILITY 306 if (isdigit(**argv)) 307 break; 308 #endif 309 for (i = 0; i < dk_ndrive; i++) { 310 if (strcmp(dr_name[i], *argv)) 311 continue; 312 dr_select[i] = 1; 313 ++ndrives; 314 break; 315 } 316 } 317 for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 318 if (dr_select[i]) 319 continue; 320 for (cp = defdrives; *cp; cp++) 321 if (strcmp(dr_name[i], *cp) == 0) { 322 dr_select[i] = 1; 323 ++ndrives; 324 break; 325 } 326 } 327 for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 328 if (dr_select[i]) 329 continue; 330 dr_select[i] = 1; 331 ++ndrives; 332 } 333 return(argv); 334 } 335 336 long 337 getuptime() 338 { 339 static time_t now, boottime; 340 time_t uptime; 341 342 if (boottime == 0) 343 kread(X_BOOTTIME, &boottime, sizeof(boottime)); 344 (void)time(&now); 345 uptime = now - boottime; 346 if (uptime <= 0 || uptime > 60*60*24*365*10) { 347 (void)fprintf(stderr, 348 "vmstat: time makes no sense; namelist must be wrong.\n"); 349 exit(1); 350 } 351 return(uptime); 352 } 353 354 int hz, hdrcnt; 355 356 void 357 dovmstat(interval, reps) 358 u_int interval; 359 int reps; 360 { 361 struct vmtotal total; 362 time_t uptime, halfuptime; 363 void needhdr(); 364 int size; 365 366 uptime = getuptime(); 367 halfuptime = uptime / 2; 368 (void)signal(SIGCONT, needhdr); 369 370 if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) 371 kread(X_STATHZ, &hz, sizeof(hz)); 372 if (!hz) 373 kread(X_HZ, &hz, sizeof(hz)); 374 375 for (hdrcnt = 1;;) { 376 if (!--hdrcnt) 377 printhdr(); 378 kread(X_CPTIME, cur.time, sizeof(cur.time)); 379 kread(X_DKXFER, cur.xfer, sizeof(*cur.xfer) * dk_ndrive); 380 kread(X_SUM, &sum, sizeof(sum)); 381 size = sizeof(total); 382 if (getkerninfo(KINFO_METER, &total, &size, 0) < 0) { 383 printf("Can't get kerninfo: %s\n", strerror(errno)); 384 bzero(&total, sizeof(total)); 385 } 386 (void)printf("%2d%2d%2d", 387 total.t_rq, total.t_dw + total.t_pw, total.t_sw); 388 #define pgtok(a) ((a) * sum.v_page_size >> 10) 389 #define rate(x) (((x) + halfuptime) / uptime) /* round */ 390 (void)printf("%6ld%6ld ", 391 pgtok(total.t_avm), pgtok(total.t_free)); 392 #ifdef NEWVM 393 (void)printf("%4lu ", rate(sum.v_faults - osum.v_faults)); 394 (void)printf("%3lu ", 395 rate(sum.v_reactivated - osum.v_reactivated)); 396 (void)printf("%3lu ", rate(sum.v_pageins - osum.v_pageins)); 397 (void)printf("%3lu %3lu ", 398 rate(sum.v_pageouts - osum.v_pageouts), 0); 399 #else 400 (void)printf("%3lu %2lu ", 401 rate(sum.v_pgrec - (sum.v_xsfrec+sum.v_xifrec) - 402 (osum.v_pgrec - (osum.v_xsfrec+osum.v_xifrec))), 403 rate(sum.v_xsfrec + sum.v_xifrec - 404 osum.v_xsfrec - osum.v_xifrec)); 405 (void)printf("%3lu ", 406 rate(pgtok(sum.v_pgpgin - osum.v_pgpgin))); 407 (void)printf("%3lu %3lu ", 408 rate(pgtok(sum.v_pgpgout - osum.v_pgpgout)), 409 rate(pgtok(sum.v_dfree - osum.v_dfree))); 410 (void)printf("%3d ", pgtok(deficit)); 411 #endif 412 (void)printf("%3lu ", rate(sum.v_scan - osum.v_scan)); 413 dkstats(); 414 (void)printf("%4lu %4lu %3lu ", 415 rate(sum.v_intr - osum.v_intr), 416 rate(sum.v_syscall - osum.v_syscall), 417 rate(sum.v_swtch - osum.v_swtch)); 418 cpustats(); 419 (void)printf("\n"); 420 (void)fflush(stdout); 421 if (reps >= 0 && --reps <= 0) 422 break; 423 osum = sum; 424 uptime = interval; 425 /* 426 * We round upward to avoid losing low-frequency events 427 * (i.e., >= 1 per interval but < 1 per second). 428 */ 429 halfuptime = (uptime + 1) / 2; 430 (void)sleep(interval); 431 } 432 } 433 434 printhdr() 435 { 436 register int i; 437 438 (void)printf(" procs memory page%*s", 20, ""); 439 if (ndrives > 1) 440 (void)printf("disks %*s faults cpu\n", 441 ndrives * 3 - 6, ""); 442 else 443 (void)printf("%*s faults cpu\n", ndrives * 3, ""); 444 #ifndef NEWVM 445 (void)printf(" r b w avm fre re at pi po fr de sr "); 446 #else 447 (void)printf(" r b w avm fre flt re pi po fr sr "); 448 #endif 449 for (i = 0; i < dk_ndrive; i++) 450 if (dr_select[i]) 451 (void)printf("%c%c ", dr_name[i][0], 452 dr_name[i][strlen(dr_name[i]) - 1]); 453 (void)printf(" in sy cs us sy id\n"); 454 hdrcnt = winlines - 2; 455 } 456 457 /* 458 * Force a header to be prepended to the next output. 459 */ 460 void 461 needhdr() 462 { 463 464 hdrcnt = 1; 465 } 466 467 #ifdef notdef 468 void 469 dotimes() 470 { 471 u_int pgintime, rectime; 472 473 kread(X_REC, &rectime, sizeof(rectime)); 474 kread(X_PGIN, &pgintime, sizeof(pgintime)); 475 kread(X_SUM, &sum, sizeof(sum)); 476 (void)printf("%u reclaims, %u total time (usec)\n", 477 sum.v_pgrec, rectime); 478 (void)printf("average: %u usec / reclaim\n", rectime / sum.v_pgrec); 479 (void)printf("\n"); 480 (void)printf("%u page ins, %u total time (msec)\n", 481 sum.v_pgin, pgintime / 10); 482 (void)printf("average: %8.1f msec / page in\n", 483 pgintime / (sum.v_pgin * 10.0)); 484 } 485 #endif 486 487 pct(top, bot) 488 long top, bot; 489 { 490 if (bot == 0) 491 return(0); 492 return((top * 100) / bot); 493 } 494 495 #define PCT(top, bot) pct((long)(top), (long)(bot)) 496 497 #if defined(tahoe) 498 #include <machine/cpu.h> 499 #endif 500 501 void 502 dosum() 503 { 504 struct nchstats nchstats; 505 #ifndef NEWVM 506 struct xstats xstats; 507 #endif 508 long nchtotal; 509 #if defined(tahoe) 510 struct keystats keystats; 511 #endif 512 513 kread(X_SUM, &sum, sizeof(sum)); 514 (void)printf("%9u cpu context switches\n", sum.v_swtch); 515 (void)printf("%9u device interrupts\n", sum.v_intr); 516 (void)printf("%9u software interrupts\n", sum.v_soft); 517 #ifdef vax 518 (void)printf("%9u pseudo-dma dz interrupts\n", sum.v_pdma); 519 #endif 520 (void)printf("%9u traps\n", sum.v_trap); 521 (void)printf("%9u system calls\n", sum.v_syscall); 522 (void)printf("%9u total faults taken\n", sum.v_faults); 523 (void)printf("%9u swap ins\n", sum.v_swpin); 524 (void)printf("%9u swap outs\n", sum.v_swpout); 525 (void)printf("%9u pages swapped in\n", sum.v_pswpin / CLSIZE); 526 (void)printf("%9u pages swapped out\n", sum.v_pswpout / CLSIZE); 527 (void)printf("%9u page ins\n", sum.v_pageins); 528 (void)printf("%9u page outs\n", sum.v_pageouts); 529 (void)printf("%9u pages paged in\n", sum.v_pgpgin); 530 (void)printf("%9u pages paged out\n", sum.v_pgpgout); 531 (void)printf("%9u pages reactivated\n", sum.v_reactivated); 532 (void)printf("%9u intransit blocking page faults\n", sum.v_intrans); 533 (void)printf("%9u zero fill pages created\n", sum.v_nzfod / CLSIZE); 534 (void)printf("%9u zero fill page faults\n", sum.v_zfod / CLSIZE); 535 (void)printf("%9u pages examined by the clock daemon\n", sum.v_scan); 536 (void)printf("%9u revolutions of the clock hand\n", sum.v_rev); 537 #ifdef NEWVM 538 (void)printf("%9u VM object cache lookups\n", sum.v_lookups); 539 (void)printf("%9u VM object hits\n", sum.v_hits); 540 (void)printf("%9u total VM faults taken\n", sum.v_vm_faults); 541 (void)printf("%9u copy-on-write faults\n", sum.v_cow_faults); 542 (void)printf("%9u pages freed by daemon\n", sum.v_dfree); 543 (void)printf("%9u pages freed by exiting processes\n", sum.v_pfree); 544 (void)printf("%9u pages free\n", sum.v_free_count); 545 (void)printf("%9u pages wired down\n", sum.v_wire_count); 546 (void)printf("%9u pages active\n", sum.v_active_count); 547 (void)printf("%9u pages inactive\n", sum.v_inactive_count); 548 (void)printf("%9u bytes per page\n", sum.v_page_size); 549 #else 550 (void)printf("%9u sequential process pages freed\n", sum.v_seqfree); 551 (void)printf("%9u total reclaims (%d%% fast)\n", sum.v_pgrec, 552 PCT(sum.v_fastpgrec, sum.v_pgrec)); 553 (void)printf("%9u reclaims from free list\n", sum.v_pgfrec); 554 (void)printf("%9u executable fill pages created\n", 555 sum.v_nexfod / CLSIZE); 556 (void)printf("%9u executable fill page faults\n", 557 sum.v_exfod / CLSIZE); 558 (void)printf("%9u swap text pages found in free list\n", 559 sum.v_xsfrec); 560 (void)printf("%9u inode text pages found in free list\n", 561 sum.v_xifrec); 562 (void)printf("%9u file fill pages created\n", sum.v_nvrfod / CLSIZE); 563 (void)printf("%9u file fill page faults\n", sum.v_vrfod / CLSIZE); 564 (void)printf("%9u pages freed by the clock daemon\n", 565 sum.v_dfree / CLSIZE); 566 #endif 567 kread(X_NCHSTATS, &nchstats, sizeof(nchstats)); 568 nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits + 569 nchstats.ncs_badhits + nchstats.ncs_falsehits + 570 nchstats.ncs_miss + nchstats.ncs_long; 571 (void)printf("%9ld total name lookups\n", nchtotal); 572 (void)printf( 573 "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n", 574 "", PCT(nchstats.ncs_goodhits, nchtotal), 575 PCT(nchstats.ncs_neghits, nchtotal), 576 PCT(nchstats.ncs_pass2, nchtotal)); 577 (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "", 578 PCT(nchstats.ncs_badhits, nchtotal), 579 PCT(nchstats.ncs_falsehits, nchtotal), 580 PCT(nchstats.ncs_long, nchtotal)); 581 #ifndef NEWVM 582 kread(X_XSTATS, &xstats, sizeof(xstats)); 583 (void)printf("%9lu total calls to xalloc (cache hits %d%%)\n", 584 xstats.alloc, PCT(xstats.alloc_cachehit, xstats.alloc)); 585 (void)printf("%9s sticky %lu flushed %lu unused %lu\n", "", 586 xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused); 587 (void)printf("%9lu total calls to xfree", xstats.free); 588 (void)printf(" (sticky %lu cached %lu swapped %lu)\n", 589 xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap); 590 #endif 591 #if defined(tahoe) 592 kread(X_CKEYSTATS, &keystats, sizeof(keystats)); 593 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", 594 keystats.ks_allocs, "code cache keys allocated", 595 PCT(keystats.ks_allocfree, keystats.ks_allocs), 596 PCT(keystats.ks_norefs, keystats.ks_allocs), 597 PCT(keystats.ks_taken, keystats.ks_allocs), 598 PCT(keystats.ks_shared, keystats.ks_allocs)); 599 kread(X_DKEYSTATS, &keystats, sizeof(keystats)); 600 (void)printf("%9d %s (free %d%% norefs %d%% taken %d%% shared %d%%)\n", 601 keystats.ks_allocs, "data cache keys allocated", 602 PCT(keystats.ks_allocfree, keystats.ks_allocs), 603 PCT(keystats.ks_norefs, keystats.ks_allocs), 604 PCT(keystats.ks_taken, keystats.ks_allocs), 605 PCT(keystats.ks_shared, keystats.ks_allocs)); 606 #endif 607 } 608 609 #ifdef notdef 610 void 611 doforkst() 612 { 613 struct forkstat fks; 614 615 kread(X_FORKSTAT, &fks, sizeof(struct forkstat)); 616 (void)printf("%d forks, %d pages, average %.2f\n", 617 fks.cntfork, fks.sizfork, (double)fks.sizfork / fks.cntfork); 618 (void)printf("%d vforks, %d pages, average %.2f\n", 619 fks.cntvfork, fks.sizvfork, (double)fks.sizvfork / fks.cntvfork); 620 } 621 #endif 622 623 void 624 dkstats() 625 { 626 register int dn, state; 627 double etime; 628 long tmp; 629 630 for (dn = 0; dn < dk_ndrive; ++dn) { 631 tmp = cur.xfer[dn]; 632 cur.xfer[dn] -= last.xfer[dn]; 633 last.xfer[dn] = tmp; 634 } 635 etime = 0; 636 for (state = 0; state < CPUSTATES; ++state) { 637 tmp = cur.time[state]; 638 cur.time[state] -= last.time[state]; 639 last.time[state] = tmp; 640 etime += cur.time[state]; 641 } 642 if (etime == 0) 643 etime = 1; 644 etime /= hz; 645 for (dn = 0; dn < dk_ndrive; ++dn) { 646 if (!dr_select[dn]) 647 continue; 648 (void)printf("%2.0f ", cur.xfer[dn] / etime); 649 } 650 } 651 652 void 653 cpustats() 654 { 655 register int state; 656 double pct, total; 657 658 total = 0; 659 for (state = 0; state < CPUSTATES; ++state) 660 total += cur.time[state]; 661 if (total) 662 pct = 100 / total; 663 else 664 pct = 0; 665 (void)printf("%2.0f ", /* user + nice */ 666 (cur.time[0] + cur.time[1]) * pct); 667 (void)printf("%2.0f ", cur.time[2] * pct); /* system */ 668 (void)printf("%2.0f", cur.time[3] * pct); /* idle */ 669 } 670 671 void 672 dointr() 673 { 674 register long *intrcnt, inttotal, uptime; 675 register int nintr, inamlen; 676 register char *intrname; 677 678 uptime = getuptime(); 679 nintr = namelist[X_EINTRCNT].n_value - namelist[X_INTRCNT].n_value; 680 inamlen = 681 namelist[X_EINTRNAMES].n_value - namelist[X_INTRNAMES].n_value; 682 intrcnt = malloc((size_t)nintr); 683 intrname = malloc((size_t)inamlen); 684 if (intrcnt == NULL || intrname == NULL) { 685 (void)fprintf(stderr, "vmstat: %s.\n", strerror(errno)); 686 exit(1); 687 } 688 kread(X_INTRCNT, intrcnt, (size_t)nintr); 689 kread(X_INTRNAMES, intrname, (size_t)inamlen); 690 (void)printf("interrupt total rate\n"); 691 inttotal = 0; 692 nintr /= sizeof(long); 693 while (--nintr >= 0) { 694 if (*intrcnt) 695 (void)printf("%-12s %8ld %8ld\n", intrname, 696 *intrcnt, *intrcnt / uptime); 697 intrname += strlen(intrname) + 1; 698 inttotal += *intrcnt++; 699 } 700 (void)printf("Total %8ld %8ld\n", inttotal, inttotal / uptime); 701 } 702 703 /* 704 * These names are defined in <sys/malloc.h>. 705 */ 706 char *kmemnames[] = INITKMEMNAMES; 707 708 void 709 domem() 710 { 711 register struct kmembuckets *kp; 712 register struct kmemstats *ks; 713 register int i, j; 714 int len, size, first; 715 long totuse = 0, totfree = 0, totreq = 0; 716 char *name; 717 struct kmemstats kmemstats[M_LAST]; 718 struct kmembuckets buckets[MINBUCKET + 16]; 719 720 kread(X_KMEMBUCKETS, buckets, sizeof(buckets)); 721 (void)printf("Memory statistics by bucket size\n"); 722 (void)printf( 723 " Size In Use Free Requests HighWater Couldfree\n"); 724 for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) { 725 if (kp->kb_calls == 0) 726 continue; 727 size = 1 << i; 728 (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size, 729 kp->kb_total - kp->kb_totalfree, 730 kp->kb_totalfree, kp->kb_calls, 731 kp->kb_highwat, kp->kb_couldfree); 732 totfree += size * kp->kb_totalfree; 733 } 734 735 kread(X_KMEMSTAT, kmemstats, sizeof(kmemstats)); 736 (void)printf("\nMemory usage type by bucket size\n"); 737 (void)printf(" Size Type(s)\n"); 738 kp = &buckets[MINBUCKET]; 739 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) { 740 if (kp->kb_calls == 0) 741 continue; 742 first = 1; 743 len = 8; 744 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { 745 if (ks->ks_calls == 0) 746 continue; 747 if ((ks->ks_size & j) == 0) 748 continue; 749 name = kmemnames[i] ? kmemnames[i] : "undefined"; 750 len += 2 + strlen(name); 751 if (first) 752 printf("%8d %s", j, name); 753 else 754 printf(","); 755 if (len >= 80) { 756 printf("\n\t "); 757 len = 10 + strlen(name); 758 } 759 if (!first) 760 printf(" %s", name); 761 first = 0; 762 } 763 printf("\n"); 764 } 765 766 (void)printf( 767 "\nMemory statistics by type Type Kern\n"); 768 (void)printf( 769 " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n"); 770 for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) { 771 if (ks->ks_calls == 0) 772 continue; 773 (void)printf("%11s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u", 774 kmemnames[i] ? kmemnames[i] : "undefined", 775 ks->ks_inuse, (ks->ks_memuse + 1023) / 1024, 776 (ks->ks_maxused + 1023) / 1024, 777 (ks->ks_limit + 1023) / 1024, ks->ks_calls, 778 ks->ks_limblocks, ks->ks_mapblocks); 779 first = 1; 780 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 781 if ((ks->ks_size & j) == 0) 782 continue; 783 if (first) 784 printf(" %d", j); 785 else 786 printf(",%d", j); 787 first = 0; 788 } 789 printf("\n"); 790 totuse += ks->ks_memuse; 791 totreq += ks->ks_calls; 792 } 793 (void)printf("\nMemory Totals: In Use Free Requests\n"); 794 (void)printf(" %7ldK %6ldK %8ld\n", 795 (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq); 796 } 797 798 /* 799 * kread reads something from the kernel, given its nlist index. 800 */ 801 void 802 kread(nlx, addr, size) 803 int nlx; 804 void *addr; 805 size_t size; 806 { 807 char *sym; 808 809 if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) { 810 sym = namelist[nlx].n_name; 811 if (*sym == '_') 812 ++sym; 813 (void)fprintf(stderr, 814 "vmstat: symbol %s not defined\n", sym); 815 exit(1); 816 } 817 if (kvm_read(kd, namelist[nlx].n_value, addr, size) != size) { 818 sym = namelist[nlx].n_name; 819 if (*sym == '_') 820 ++sym; 821 (void)fprintf(stderr, "vmstat: %s: %s\n", sym, kvm_geterr(kd)); 822 exit(1); 823 } 824 } 825 826 void 827 usage() 828 { 829 (void)fprintf(stderr, 830 #ifndef NEWVM 831 "usage: vmstat [-fimst] [-c count] [-M core] \ 832 [-N system] [-w wait] [disks]\n"); 833 #else 834 "usage: vmstat [-ims] [-c count] [-M core] \ 835 [-N system] [-w wait] [disks]\n"); 836 #endif 837 exit(1); 838 } 839