1: /* 2: * Copyright (c) 1980 Regents of the University of California. 3: * All rights reserved. The Berkeley software License Agreement 4: * specifies the terms and conditions for redistribution. 5: */ 6: 7: #ifndef lint 8: char copyright[] = 9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10: All rights reserved.\n"; 11: #endif not lint 12: 13: #ifndef lint 14: static char sccsid[] = "@(#)vmstat.c 5.4 (Berkeley) 5/17/86"; 15: #endif not lint 16: 17: #include <stdio.h> 18: #include <ctype.h> 19: #include <nlist.h> 20: 21: #include <sys/param.h> 22: #include <sys/file.h> 23: #include <sys/vm.h> 24: #include <sys/dk.h> 25: #include <sys/buf.h> 26: #include <sys/dir.h> 27: #include <sys/inode.h> 28: #include <sys/namei.h> 29: 30: struct nlist nl[] = { 31: #define X_CPTIME 0 32: { "_cp_time" }, 33: #define X_RATE 1 34: { "_rate" }, 35: #define X_TOTAL 2 36: { "_total" }, 37: #define X_DEFICIT 3 38: { "_deficit" }, 39: #define X_FORKSTAT 4 40: { "_forkstat" }, 41: #define X_SUM 5 42: { "_sum" }, 43: #define X_FIRSTFREE 6 44: { "_firstfree" }, 45: #define X_MAXFREE 7 46: { "_maxfree" }, 47: #define X_BOOTTIME 8 48: { "_boottime" }, 49: #define X_DKXFER 9 50: { "_dk_xfer" }, 51: #define X_REC 10 52: { "_rectime" }, 53: #define X_PGIN 11 54: { "_pgintime" }, 55: #define X_HZ 12 56: { "_hz" }, 57: #define X_PHZ 13 58: { "_phz" }, 59: #define X_NCHSTATS 14 60: { "_nchstats" }, 61: #define X_INTRNAMES 15 62: { "_intrnames" }, 63: #define X_EINTRNAMES 16 64: { "_eintrnames" }, 65: #define X_INTRCNT 17 66: { "_intrcnt" }, 67: #define X_EINTRCNT 18 68: { "_eintrcnt" }, 69: #define X_DK_NDRIVE 19 70: { "_dk_ndrive" }, 71: #define X_XSTATS 20 72: { "_xstats" }, 73: #ifdef vax 74: #define X_MBDINIT 21 75: { "_mbdinit" }, 76: #define X_UBDINIT 22 77: { "_ubdinit" }, 78: #endif 79: { "" }, 80: }; 81: 82: char **dr_name; 83: int *dr_select; 84: int dk_ndrive; 85: int ndrives = 0; 86: #ifdef vax 87: char *defdrives[] = { "hp0", "hp1", "hp2", 0 }; 88: #else 89: char *defdrives[] = { 0 }; 90: #endif 91: double stat1(); 92: int firstfree, maxfree; 93: int hz; 94: int phz; 95: int HZ; 96: 97: #ifdef vax 98: #define INTS(x) ((x) - (hz + phz)) 99: #endif 100: 101: struct { 102: int busy; 103: long time[CPUSTATES]; 104: long *xfer; 105: struct vmmeter Rate; 106: struct vmtotal Total; 107: struct vmmeter Sum; 108: struct forkstat Forkstat; 109: unsigned rectime; 110: unsigned pgintime; 111: } s, s1, z; 112: #define rate s.Rate 113: #define total s.Total 114: #define sum s.Sum 115: #define forkstat s.Forkstat 116: 117: struct vmmeter osum; 118: int deficit; 119: double etime; 120: int mf; 121: time_t now, boottime; 122: int printhdr(); 123: int lines = 1; 124: extern char *calloc(); 125: 126: main(argc, argv) 127: int argc; 128: char **argv; 129: { 130: extern char *ctime(); 131: register i; 132: int iter, nintv, iflag = 0; 133: long t; 134: char *arg, **cp, buf[BUFSIZ]; 135: 136: nlist("/vmunix", nl); 137: if(nl[0].n_type == 0) { 138: fprintf(stderr, "no /vmunix namelist\n"); 139: exit(1); 140: } 141: mf = open("/dev/kmem", 0); 142: if(mf < 0) { 143: fprintf(stderr, "cannot open /dev/kmem\n"); 144: exit(1); 145: } 146: iter = 0; 147: argc--, argv++; 148: while (argc>0 && argv[0][0]=='-') { 149: char *cp = *argv++; 150: argc--; 151: while (*++cp) switch (*cp) { 152: 153: case 't': 154: dotimes(); 155: exit(0); 156: 157: case 'z': 158: close(mf); 159: mf = open("/dev/kmem", 2); 160: lseek(mf, (long)nl[X_SUM].n_value, L_SET); 161: write(mf, &z.Sum, sizeof z.Sum); 162: exit(0); 163: 164: case 'f': 165: doforkst(); 166: exit(0); 167: 168: case 's': 169: dosum(); 170: exit(0); 171: 172: case 'i': 173: iflag++; 174: break; 175: 176: default: 177: fprintf(stderr, 178: "usage: vmstat [ -fsi ] [ interval ] [ count]\n"); 179: exit(1); 180: } 181: } 182: lseek(mf, (long)nl[X_FIRSTFREE].n_value, L_SET); 183: read(mf, &firstfree, sizeof firstfree); 184: lseek(mf, (long)nl[X_MAXFREE].n_value, L_SET); 185: read(mf, &maxfree, sizeof maxfree); 186: lseek(mf, (long)nl[X_BOOTTIME].n_value, L_SET); 187: read(mf, &boottime, sizeof boottime); 188: lseek(mf, (long)nl[X_HZ].n_value, L_SET); 189: read(mf, &hz, sizeof hz); 190: if (nl[X_PHZ].n_value != 0) { 191: lseek(mf, (long)nl[X_PHZ].n_value, L_SET); 192: read(mf, &phz, sizeof phz); 193: } 194: HZ = phz ? phz : hz; 195: if (nl[DK_NDRIVE].n_value == 0) { 196: fprintf(stderr, "dk_ndrive undefined in system\n"); 197: exit(1); 198: } 199: lseek(mf, nl[X_DK_NDRIVE].n_value, L_SET); 200: read(mf, &dk_ndrive, sizeof (dk_ndrive)); 201: if (dk_ndrive <= 0) { 202: fprintf(stderr, "dk_ndrive %d\n", dk_ndrive); 203: exit(1); 204: } 205: dr_select = (int *)calloc(dk_ndrive, sizeof (int)); 206: dr_name = (char **)calloc(dk_ndrive, sizeof (char *)); 207: #define allocate(e, t) \ 208: s./**/e = (t *)calloc(dk_ndrive, sizeof (t)); \ 209: s1./**/e = (t *)calloc(dk_ndrive, sizeof (t)); 210: allocate(xfer, long); 211: for (arg = buf, i = 0; i < dk_ndrive; i++) { 212: dr_name[i] = arg; 213: sprintf(dr_name[i], "dk%d", i); 214: arg += strlen(dr_name[i]) + 1; 215: } 216: read_names(); 217: time(&now); 218: nintv = now - boottime; 219: if (nintv <= 0 || nintv > 60*60*24*365*10) { 220: fprintf(stderr, 221: "Time makes no sense... namelist must be wrong.\n"); 222: exit(1); 223: } 224: if (iflag) { 225: dointr(nintv); 226: exit(0); 227: } 228: /* 229: * Choose drives to be displayed. Priority 230: * goes to (in order) drives supplied as arguments, 231: * default drives. If everything isn't filled 232: * in and there are drives not taken care of, 233: * display the first few that fit. 234: */ 235: ndrives = 0; 236: while (argc > 0 && !isdigit(argv[0][0])) { 237: for (i = 0; i < dk_ndrive; i++) { 238: if (strcmp(dr_name[i], argv[0])) 239: continue; 240: dr_select[i] = 1; 241: ndrives++; 242: } 243: argc--, argv++; 244: } 245: for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 246: if (dr_select[i]) 247: continue; 248: for (cp = defdrives; *cp; cp++) 249: if (strcmp(dr_name[i], *cp) == 0) { 250: dr_select[i] = 1; 251: ndrives++; 252: break; 253: } 254: } 255: for (i = 0; i < dk_ndrive && ndrives < 4; i++) { 256: if (dr_select[i]) 257: continue; 258: dr_select[i] = 1; 259: ndrives++; 260: } 261: if (argc > 1) 262: iter = atoi(argv[1]); 263: signal(SIGCONT, printhdr); 264: loop: 265: if (--lines == 0) 266: printhdr(); 267: lseek(mf, (long)nl[X_CPTIME].n_value, L_SET); 268: read(mf, s.time, sizeof s.time); 269: lseek(mf, (long)nl[X_DKXFER].n_value, L_SET); 270: read(mf, s.xfer, dk_ndrive * sizeof (long)); 271: if (nintv != 1) 272: lseek(mf, (long)nl[X_SUM].n_value, L_SET); 273: else 274: lseek(mf, (long)nl[X_RATE].n_value, L_SET); 275: read(mf, &rate, sizeof rate); 276: lseek(mf, (long)nl[X_TOTAL].n_value, L_SET); 277: read(mf, &total, sizeof total); 278: osum = sum; 279: lseek(mf, (long)nl[X_SUM].n_value, L_SET); 280: read(mf, &sum, sizeof sum); 281: lseek(mf, (long)nl[X_DEFICIT].n_value, L_SET); 282: read(mf, &deficit, sizeof deficit); 283: etime = 0; 284: for (i=0; i < dk_ndrive; i++) { 285: t = s.xfer[i]; 286: s.xfer[i] -= s1.xfer[i]; 287: s1.xfer[i] = t; 288: } 289: for (i=0; i < CPUSTATES; i++) { 290: t = s.time[i]; 291: s.time[i] -= s1.time[i]; 292: s1.time[i] = t; 293: etime += s.time[i]; 294: } 295: if(etime == 0.) 296: etime = 1.; 297: printf("%2d%2d%2d", total.t_rq, total.t_dw+total.t_pw, total.t_sw); 298: #define pgtok(a) ((a)*NBPG/1024) 299: printf("%6d%6d", pgtok(total.t_avm), pgtok(total.t_free)); 300: printf("%4d%3d", (rate.v_pgrec - (rate.v_xsfrec+rate.v_xifrec))/nintv, 301: (rate.v_xsfrec+rate.v_xifrec)/nintv); 302: printf("%4d", pgtok(rate.v_pgpgin)/nintv); 303: printf("%4d%4d%4d%4d", pgtok(rate.v_pgpgout)/nintv, 304: pgtok(rate.v_dfree)/nintv, pgtok(deficit), rate.v_scan/nintv); 305: etime /= (float)HZ; 306: for (i = 0; i < dk_ndrive; i++) 307: if (dr_select[i]) 308: stats(i); 309: printf("%4d%4d%4d", INTS(rate.v_intr/nintv), rate.v_syscall/nintv, 310: rate.v_swtch/nintv); 311: for(i=0; i<CPUSTATES; i++) { 312: float f = stat1(i); 313: if (i == 0) { /* US+NI */ 314: i++; 315: f += stat1(i); 316: } 317: printf("%3.0f", f); 318: } 319: printf("\n"); 320: fflush(stdout); 321: nintv = 1; 322: if (--iter &&argc > 0) { 323: sleep(atoi(argv[0])); 324: goto loop; 325: } 326: } 327: 328: printhdr() 329: { 330: register int i, j; 331: 332: printf(" procs memory page "); 333: i = (ndrives * 3 - 6) / 2; 334: if (i < 0) 335: i = 0; 336: for (j = 0; j < i; j++) 337: putchar(' '); 338: printf("faults"); 339: i = ndrives * 3 - 6 - i; 340: for (j = 0; j < i; j++) 341: putchar(' '); 342: printf(" cpu\n"); 343: printf(" r b w avm fre re at pi po fr de sr "); 344: for (i = 0; i < dk_ndrive; i++) 345: if (dr_select[i]) 346: printf("%c%c ", dr_name[i][0], dr_name[i][2]); 347: printf(" in sy cs us sy id\n"); 348: lines = 19; 349: } 350: 351: dotimes() 352: { 353: 354: lseek(mf, (long)nl[X_REC].n_value, L_SET); 355: read(mf, &s.rectime, sizeof s.rectime); 356: lseek(mf, (long)nl[X_PGIN].n_value, L_SET); 357: read(mf, &s.pgintime, sizeof s.pgintime); 358: lseek(mf, (long)nl[X_SUM].n_value, L_SET); 359: read(mf, &sum, sizeof sum); 360: printf("%d reclaims, %d total time (usec)\n", sum.v_pgrec, s.rectime); 361: printf("average: %d usec / reclaim\n", s.rectime/sum.v_pgrec); 362: printf("\n"); 363: printf("%d page ins, %d total time (msec)\n",sum.v_pgin, s.pgintime/10); 364: printf("average: %8.1f msec / page in\n", s.pgintime/(sum.v_pgin*10.0)); 365: } 366: 367: /* SHOULD BE AVAILABLE IN <sys/text.h> */ 368: /* 369: * Statistics 370: */ 371: struct xstats { 372: u_long alloc; /* calls to xalloc */ 373: u_long alloc_inuse; /* found in use/sticky */ 374: u_long alloc_cachehit; /* found in cache */ 375: u_long alloc_cacheflush; /* flushed cached text */ 376: u_long alloc_unused; /* flushed unused cached text */ 377: u_long free; /* calls to xfree */ 378: u_long free_inuse; /* still in use/sticky */ 379: u_long free_cache; /* placed in cache */ 380: u_long free_cacheswap; /* swapped out to place in cache */ 381: }; 382: /* END SHOULD BE AVAILABLE... */ 383: 384: dosum() 385: { 386: struct nchstats nchstats; 387: struct xstats xstats; 388: long nchtotal; 389: 390: lseek(mf, (long)nl[X_SUM].n_value, L_SET); 391: read(mf, &sum, sizeof sum); 392: printf("%9d swap ins\n", sum.v_swpin); 393: printf("%9d swap outs\n", sum.v_swpout); 394: printf("%9d pages swapped in\n", sum.v_pswpin / CLSIZE); 395: printf("%9d pages swapped out\n", sum.v_pswpout / CLSIZE); 396: printf("%9d total address trans. faults taken\n", sum.v_faults); 397: printf("%9d page ins\n", sum.v_pgin); 398: printf("%9d page outs\n", sum.v_pgout); 399: printf("%9d pages paged in\n", sum.v_pgpgin); 400: printf("%9d pages paged out\n", sum.v_pgpgout); 401: printf("%9d sequential process pages freed\n", sum.v_seqfree); 402: printf("%9d total reclaims (%d%% fast)\n", sum.v_pgrec, 403: (sum.v_fastpgrec * 100) / (sum.v_pgrec == 0 ? 1 : sum.v_pgrec)); 404: printf("%9d reclaims from free list\n", sum.v_pgfrec); 405: printf("%9d intransit blocking page faults\n", sum.v_intrans); 406: printf("%9d zero fill pages created\n", sum.v_nzfod / CLSIZE); 407: printf("%9d zero fill page faults\n", sum.v_zfod / CLSIZE); 408: printf("%9d executable fill pages created\n", sum.v_nexfod / CLSIZE); 409: printf("%9d executable fill page faults\n", sum.v_exfod / CLSIZE); 410: printf("%9d swap text pages found in free list\n", sum.v_xsfrec); 411: printf("%9d inode text pages found in free list\n", sum.v_xifrec); 412: printf("%9d file fill pages created\n", sum.v_nvrfod / CLSIZE); 413: printf("%9d file fill page faults\n", sum.v_vrfod / CLSIZE); 414: printf("%9d pages examined by the clock daemon\n", sum.v_scan); 415: printf("%9d revolutions of the clock hand\n", sum.v_rev); 416: printf("%9d pages freed by the clock daemon\n", sum.v_dfree / CLSIZE); 417: printf("%9d cpu context switches\n", sum.v_swtch); 418: printf("%9d device interrupts\n", sum.v_intr); 419: printf("%9d software interrupts\n", sum.v_soft); 420: #ifdef vax 421: printf("%9d pseudo-dma dz interrupts\n", sum.v_pdma); 422: #endif 423: printf("%9d traps\n", sum.v_trap); 424: printf("%9d system calls\n", sum.v_syscall); 425: lseek(mf, (long)nl[X_NCHSTATS].n_value, 0); 426: read(mf, &nchstats, sizeof nchstats); 427: nchtotal = nchstats.ncs_goodhits + nchstats.ncs_badhits + 428: nchstats.ncs_falsehits + nchstats.ncs_miss + nchstats.ncs_long; 429: printf("%9d total name lookups", nchtotal); 430: #define nz(x) ((x) ? (x) : 1) 431: printf(" (cache hits %d%% system %d%% per-process)\n", 432: nchstats.ncs_goodhits * 100 / nz(nchtotal), 433: nchstats.ncs_pass2 * 100 / nz(nchtotal)); 434: printf("%9s badhits %d, falsehits %d, toolong %d\n", "", 435: nchstats.ncs_badhits, nchstats.ncs_falsehits, nchstats.ncs_long); 436: lseek(mf, (long)nl[X_XSTATS].n_value, 0); 437: read(mf, &xstats, sizeof xstats); 438: printf("%9d total calls to xalloc (cache hits %d%%)\n", 439: xstats.alloc, xstats.alloc_cachehit * 100 / nz(xstats.alloc)); 440: printf("%9s sticky %d flushed %d unused %d\n", "", 441: xstats.alloc_inuse, xstats.alloc_cacheflush, xstats.alloc_unused); 442: printf("%9d total calls to xfree", xstats.free); 443: printf(" (sticky %d cached %d swapped %d)\n", 444: xstats.free_inuse, xstats.free_cache, xstats.free_cacheswap); 445: } 446: 447: doforkst() 448: { 449: 450: lseek(mf, (long)nl[X_FORKSTAT].n_value, L_SET); 451: read(mf, &forkstat, sizeof forkstat); 452: printf("%d forks, %d pages, average=%.2f\n", 453: forkstat.cntfork, forkstat.sizfork, 454: (float) forkstat.sizfork / forkstat.cntfork); 455: printf("%d vforks, %d pages, average=%.2f\n", 456: forkstat.cntvfork, forkstat.sizvfork, 457: (float)forkstat.sizvfork / forkstat.cntvfork); 458: } 459: 460: stats(dn) 461: { 462: 463: if (dn >= dk_ndrive) { 464: printf(" 0"); 465: return; 466: } 467: printf("%3.0f", s.xfer[dn]/etime); 468: } 469: 470: double 471: stat1(row) 472: { 473: double t; 474: register i; 475: 476: t = 0; 477: for(i=0; i<CPUSTATES; i++) 478: t += s.time[i]; 479: if(t == 0.) 480: t = 1.; 481: return(s.time[row]*100./t); 482: } 483: 484: dointr(nintv) 485: { 486: int nintr, inttotal; 487: long *intrcnt; 488: char *intrname, *malloc(); 489: 490: nintr = (nl[X_EINTRCNT].n_value - nl[X_INTRCNT].n_value) / sizeof(long); 491: intrcnt = (long *) malloc(nl[X_EINTRCNT].n_value - 492: nl[X_INTRCNT].n_value); 493: intrname = malloc(nl[X_EINTRNAMES].n_value - nl[X_INTRNAMES].n_value); 494: if (intrcnt == NULL || intrname == NULL) { 495: fprintf(stderr, "vmstat: out of memory\n"); 496: exit(9); 497: } 498: lseek(mf, (long)nl[X_INTRCNT].n_value, L_SET); 499: read(mf, intrcnt, nintr * sizeof (long)); 500: lseek(mf, (long)nl[X_INTRNAMES].n_value, L_SET); 501: read(mf, intrname, nl[X_EINTRNAMES].n_value - nl[X_INTRNAMES].n_value); 502: printf("interrupt total rate\n"); 503: inttotal = 0; 504: while (nintr--) { 505: if (*intrcnt) 506: printf("%-12s %8ld %8ld\n", intrname, 507: *intrcnt, *intrcnt / nintv); 508: intrname += strlen(intrname) + 1; 509: inttotal += *intrcnt++; 510: } 511: printf("Total %8ld %8ld\n", inttotal, inttotal / nintv); 512: } 513: 514: #define steal(where, var) \ 515: lseek(mf, where, L_SET); read(mf, &var, sizeof var); 516: /* 517: * Read the drive names out of kmem. 518: */ 519: #ifdef vax 520: #include <vaxuba/ubavar.h> 521: #include <vaxmba/mbavar.h> 522: 523: read_names() 524: { 525: struct mba_device mdev; 526: register struct mba_device *mp; 527: struct mba_driver mdrv; 528: short two_char; 529: char *cp = (char *) &two_char; 530: struct uba_device udev, *up; 531: struct uba_driver udrv; 532: 533: mp = (struct mba_device *) nl[X_MBDINIT].n_value; 534: up = (struct uba_device *) nl[X_UBDINIT].n_value; 535: if (up == 0) { 536: fprintf(stderr, "vmstat: Disk init info not in namelist\n"); 537: exit(1); 538: } 539: if (mp) for (;;) { 540: steal(mp++, mdev); 541: if (mdev.mi_driver == 0) 542: break; 543: if (mdev.mi_dk < 0 || mdev.mi_alive == 0) 544: continue; 545: steal(mdev.mi_driver, mdrv); 546: steal(mdrv.md_dname, two_char); 547: sprintf(dr_name[mdev.mi_dk], "%c%c%d", 548: cp[0], cp[1], mdev.mi_unit); 549: } 550: for (;;) { 551: steal(up++, udev); 552: if (udev.ui_driver == 0) 553: break; 554: if (udev.ui_dk < 0 || udev.ui_alive == 0) 555: continue; 556: steal(udev.ui_driver, udrv); 557: steal(udrv.ud_dname, two_char); 558: sprintf(dr_name[udev.ui_dk], "%c%c%d", 559: cp[0], cp[1], udev.ui_unit); 560: } 561: } 562: #endif