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