|
|
1.1 ! root 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[] = "@(#)main.c 5.7 (Berkeley) 3/12/90"; ! 15: #endif not lint ! 16: ! 17: #include "systat.h" ! 18: #include <paths.h> ! 19: ! 20: static struct nlist nlst[] = { ! 21: #define X_CCPU 0 ! 22: { "_ccpu" }, ! 23: #define X_FSCALE 1 ! 24: { "_fscale" }, ! 25: #define X_HZ 2 ! 26: { "_hz" }, ! 27: #define X_PHZ 3 ! 28: { "_phz" }, ! 29: { "" } ! 30: }; ! 31: ! 32: int kmem = -1; ! 33: int mem = -1; ! 34: int swap = -1; ! 35: int naptime = 5; ! 36: ! 37: int die(); ! 38: int display(); ! 39: int suspend(); ! 40: sig_t sigtstpdfl; ! 41: ! 42: int dellave; ! 43: ! 44: static WINDOW *wload; /* one line window for load average */ ! 45: ! 46: main(argc, argv) ! 47: int argc; ! 48: char **argv; ! 49: { ! 50: fixpt_t ccpu; ! 51: ! 52: argc--, argv++; ! 53: while (argc > 0) { ! 54: if (argv[0][0] == '-') { ! 55: struct cmdtab *p; ! 56: ! 57: p = lookup(&argv[0][1]); ! 58: if (p == (struct cmdtab *)-1) { ! 59: fprintf(stderr, "%s: unknown request\n", ! 60: &argv[0][1]); ! 61: exit(1); ! 62: } ! 63: curcmd = p; ! 64: } else { ! 65: naptime = atoi(argv[0]); ! 66: if (naptime <= 0) ! 67: naptime = 5; ! 68: } ! 69: argc--, argv++; ! 70: } ! 71: nlist(_PATH_UNIX, nlst); ! 72: if (nlst[X_CCPU].n_type == 0) { ! 73: fprintf(stderr, "Couldn't namelist %s.\n", _PATH_UNIX); ! 74: exit(1); ! 75: } ! 76: kmemf = _PATH_KMEM; ! 77: kmem = open(kmemf, O_RDONLY); ! 78: if (kmem < 0) { ! 79: perror(kmemf); ! 80: exit(1); ! 81: } ! 82: memf = _PATH_MEM; ! 83: mem = open(memf, O_RDONLY); ! 84: if (mem < 0) { ! 85: perror(memf); ! 86: exit(1); ! 87: } ! 88: swapf = _PATH_DRUM; ! 89: swap = open(swapf, O_RDONLY); ! 90: if (swap < 0) { ! 91: perror(swapf); ! 92: exit(1); ! 93: } ! 94: signal(SIGINT, die); ! 95: signal(SIGQUIT, die); ! 96: signal(SIGTERM, die); ! 97: ! 98: /* ! 99: * Initialize display. Load average appears in a one line ! 100: * window of its own. Current command's display appears in ! 101: * an overlapping sub-window of stdscr configured by the display ! 102: * routines to minimize update work by curses. ! 103: */ ! 104: initscr(); ! 105: CMDLINE = LINES - 1; ! 106: wnd = (*curcmd->c_open)(); ! 107: if (wnd == NULL) { ! 108: fprintf(stderr, "Couldn't initialize display.\n"); ! 109: die(); ! 110: } ! 111: wload = newwin(1, 0, 3, 20); ! 112: if (wload == NULL) { ! 113: fprintf(stderr, "Couldn't set up load average window.\n"); ! 114: die(); ! 115: } ! 116: ! 117: gethostname(hostname, sizeof (hostname)); ! 118: ccpu = getw(nlst[X_CCPU].n_value); ! 119: fscale = getw(nlst[X_FSCALE].n_value); ! 120: lccpu = log((double) ccpu / fscale); ! 121: hz = getw(nlst[X_HZ].n_value); ! 122: phz = getw(nlst[X_PHZ].n_value); ! 123: (*curcmd->c_init)(); ! 124: curcmd->c_flags |= CF_INIT; ! 125: labels(); ! 126: ! 127: known[0].k_uid = -1; ! 128: known[0].k_name[0] = '\0'; ! 129: numknown = 1; ! 130: procs[0].pid = -1; ! 131: strcpy(procs[0].cmd, "<idle>"); ! 132: numprocs = 1; ! 133: dellave = 0.0; ! 134: ! 135: signal(SIGALRM, display); ! 136: sigtstpdfl = signal(SIGTSTP, suspend); ! 137: display(); ! 138: noecho(); ! 139: crmode(); ! 140: keyboard(); ! 141: /*NOTREACHED*/ ! 142: } ! 143: ! 144: labels() ! 145: { ! 146: if (curcmd->c_flags & CF_LOADAV) { ! 147: mvaddstr(2, 20, ! 148: "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); ! 149: mvaddstr(3, 5, "Load Average"); ! 150: } ! 151: (*curcmd->c_label)(); ! 152: #ifdef notdef ! 153: mvprintw(21, 25, "CPU usage on %s", hostname); ! 154: #endif ! 155: refresh(); ! 156: } ! 157: ! 158: display() ! 159: { ! 160: register int i, j; ! 161: ! 162: /* Get the load average over the last minute. */ ! 163: (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); ! 164: (*curcmd->c_fetch)(); ! 165: if (curcmd->c_flags & CF_LOADAV) { ! 166: j = 5.0*avenrun[0] + 0.5; ! 167: dellave -= avenrun[0]; ! 168: if (dellave >= 0.0) ! 169: c = '<'; ! 170: else { ! 171: c = '>'; ! 172: dellave = -dellave; ! 173: } ! 174: if (dellave < 0.1) ! 175: c = '|'; ! 176: dellave = avenrun[0]; ! 177: wmove(wload, 0, 0); wclrtoeol(wload); ! 178: for (i = (j > 50) ? 50 : j; i > 0; i--) ! 179: waddch(wload, c); ! 180: if (j > 50) ! 181: wprintw(wload, " %4.1f", avenrun[0]); ! 182: } ! 183: (*curcmd->c_refresh)(); ! 184: if (curcmd->c_flags & CF_LOADAV) ! 185: wrefresh(wload); ! 186: wrefresh(wnd); ! 187: move(CMDLINE, col); ! 188: refresh(); ! 189: alarm(naptime); ! 190: } ! 191: ! 192: load() ! 193: { ! 194: double avenrun[3]; ! 195: ! 196: (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); ! 197: mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", ! 198: avenrun[0], avenrun[1], avenrun[2]); ! 199: clrtoeol(); ! 200: } ! 201: ! 202: die() ! 203: { ! 204: move(CMDLINE, 0); ! 205: clrtoeol(); ! 206: refresh(); ! 207: endwin(); ! 208: exit(0); ! 209: } ! 210: ! 211: error(fmt, a1, a2, a3) ! 212: { ! 213: mvprintw(CMDLINE, 0, fmt, a1, a2, a3); ! 214: clrtoeol(); ! 215: refresh(); ! 216: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.