Annotation of researchv9/jtools/src/cmd/sysmon.c, revision 1.1.1.1

1.1       root        1: #include <jerq.h>
                      2: #include <nlist.h>
                      3: #include <ctype.h>
                      4: 
                      5: typedef int bool;
                      6: 
                      7: #undef TRUE
                      8: #define TRUE -1
                      9: #define FALSE 0
                     10: 
                     11: struct ld {
                     12:     float l_runq;
                     13:     long l_cp[5];
                     14: };
                     15: 
                     16: #define TIMELEN 6
                     17: #define LOADLEN 13
                     18: 
                     19: struct nodedef {
                     20:        Rectangle bar;          /* the bar graph of system use */
                     21:        char load[LOADLEN];             /* load numbers */
                     22:        int vec[5];                     /* current use vectors */
                     23:        int oldvec[5];          /* old use vectors */
                     24:        struct ld       m_old;  /* previous set of poal numbers */
                     25:        struct ld       m_new;  /* current set of load numbers */
                     26: } node0;
                     27: 
                     28: /* user nice sys queue idle */
                     29: short black_bits[]={
                     30:        0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 
                     31:        0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 
                     32: };
                     33: Texture black;
                     34: short white_bits[]={
                     35:        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
                     36:        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
                     37: };
                     38: Texture white;
                     39: short darkgrey_bits[] = {
                     40:        0xDDDD, 0x7777, 0xDDDD, 0x7777, 0xDDDD, 0x7777, 0xDDDD, 0x7777,
                     41:        0xDDDD, 0x7777, 0xDDDD, 0x7777, 0xDDDD, 0x7777, 0xDDDD, 0x7777,
                     42: };
                     43: Texture darkgrey;
                     44: short lightgrey_bits[] = {
                     45:        0x2222, 0x8888, 0x2222, 0x8888, 0x2222, 0x8888, 0x2222, 0x8888,
                     46:        0x2222, 0x8888, 0x2222, 0x8888, 0x2222, 0x8888, 0x2222, 0x8888,
                     47: };
                     48: Texture lightgrey;
                     49: short grey_bits[] = {
                     50:        0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555,
                     51:        0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555,
                     52: };
                     53: Texture grey;
                     54: 
                     55: #ifdef BSD
                     56: #include <sys/param.h>
                     57: #define NUMLOADBAR     4
                     58: Texture *txt[]={
                     59:        &black, &lightgrey, &darkgrey, &white
                     60: };
                     61: #else
                     62: #define NUMLOADBAR     5
                     63: Texture *txt[]={
                     64:        &black, &lightgrey, &darkgrey, &grey, &white
                     65: };
                     66: #endif BSD
                     67: 
                     68: /* size contraints */
                     69: #define BARHEIGHT 18
                     70: #define MINBARHEIGHT 4
                     71: #define MAXBARHEIGHT 24
                     72: #define TIMEORIGIN 2
                     73: #define NAMELEN 9
                     74: int CHARHEIGHT;
                     75: int CHARWIDTH;
                     76: int TIMECORNER;
                     77: int LOADORIGIN;
                     78: int LOADCORNER;
                     79: 
                     80: int size;
                     81: int barheight;
                     82: 
                     83: main(ac, av)
                     84: int ac;
                     85: char *av[];
                     86: {
                     87:        register i, state;
                     88:        int ticks = 5;
                     89:        int permin = 60;
                     90:        char *p;
                     91: 
                     92:        /* initialize load gathering */
                     93:        initload();
                     94:        gettime();
                     95:        getload();
                     96:        docpu();
                     97: 
                     98:        request(ALARM|KBD|MOUSE);
                     99:        initdisplay(ac, av);
                    100:        inittextures();
                    101: 
                    102:        CHARHEIGHT = fontheight(&defont) - 2;
                    103:        CHARWIDTH = strwidth(&defont, "m");
                    104:        TIMECORNER = NAMELEN*CHARWIDTH;
                    105:        LOADORIGIN = TIMECORNER;
                    106:        LOADCORNER = TIMECORNER + LOADLEN*CHARWIDTH;
                    107:        barheight = MAXBARHEIGHT;
                    108: 
                    109:        /* read the tick value and arguments */
                    110:        if (ac > 1 && av[1][0] == '-') {
                    111:                if (isdigit(av[1][1])) {
                    112:                        ticks = -atoi(av[1]);
                    113:                        if (ticks < 2)
                    114:                                ticks = 2;
                    115:                }
                    116:                av++, ac--;
                    117:        }
                    118: 
                    119:        reshape();
                    120:        alarm(ticks * 60);
                    121:        while (TRUE) {
                    122:                state = wait(ALARM|KBD|MOUSE);
                    123:                if(P->state&RESHAPED)
                    124:                        reshape();
                    125:                if(state&KBD)
                    126:                        while(kbdchar() != -1)  /* dump any keyboard input */
                    127:                                ;
                    128:                if(state&ALARM) {
                    129:                        getinfo();
                    130:                        docpu();
                    131:                        drawload();
                    132:                        getload();
                    133:                        drawload();
                    134:                        drawbar();
                    135:                        if(permin++ >= 60/ticks){
                    136:                                drawtime();
                    137:                                gettime();
                    138:                                drawtime();
                    139:                                permin = 0;
                    140:                        }
                    141:                        alarm(ticks * 60);
                    142:                }
                    143:        }
                    144: }
                    145: 
                    146: inittextures()
                    147: {
                    148:        black = ToTexture(black_bits);
                    149:        white = ToTexture(white_bits);
                    150:        darkgrey = ToTexture(darkgrey_bits);
                    151:        lightgrey = ToTexture(lightgrey_bits);
                    152:        grey = ToTexture(grey_bits);
                    153: }
                    154: 
                    155: reshape()
                    156: {
                    157:        register int i;
                    158: 
                    159:        P->state&=~RESHAPED;
                    160:        rectf(&display, Drect, F_CLR);
                    161: 
                    162:        /* resize objects */
                    163:        size = Drect.corner.x-Drect.origin.x;
                    164:        barheight = (Drect.corner.y-Drect.origin.y-2) - CHARHEIGHT;
                    165:        if (barheight < MINBARHEIGHT)
                    166:                barheight = MINBARHEIGHT;
                    167: 
                    168:        /* redraw */
                    169:        nodeinit();
                    170:        drawload();
                    171:        drawtime();
                    172: }
                    173: 
                    174: 
                    175: /* clear out the bar and write in the node name */
                    176: nodeinit()
                    177: {
                    178:        register struct nodedef *n = &node0;
                    179:        register int i;
                    180: 
                    181:        /* make the bar */
                    182:        n->bar.origin.x = Drect.origin.x;
                    183:        n->bar.origin.y = Drect.origin.y + 0*(barheight+CHARHEIGHT);
                    184:        n->bar.corner.x = Drect.origin.x + size;
                    185:        n->bar.corner.y = n->bar.origin.y + barheight;
                    186:        n->bar = inset(n->bar, 4);
                    187:        rectf(&display, inset(n->bar, -2), F_OR);
                    188:        rectf(&display, n->bar, F_CLR);
                    189: 
                    190:        /* init the node */
                    191:        for(i = 0; i<NUMLOADBAR - 1; i++)
                    192:                n->vec[i] = n->oldvec[i] = 0;
                    193:        n->vec[i]=n->oldvec[i]=size;
                    194: };
                    195: 
                    196: /* draw the bar graph for a system's CPU time */
                    197: drawbar ()
                    198: {
                    199:        struct nodedef *n = &node0;
                    200:        Point pt[2];
                    201:        register i;
                    202: 
                    203:        rectf(&display, n->bar, F_CLR);
                    204:        pt[0].y = n->bar.origin.y;
                    205:        pt[0].x = n->bar.origin.x;
                    206:        pt[1].y =  n->bar.corner.y;
                    207:        for(i=0; i< NUMLOADBAR - 1; i++){
                    208:                if (!n->vec[i])
                    209:                        continue;
                    210:                pt[1].x = pt[0].x + n->vec[i];
                    211:                if (pt[1].x > n->bar.corner.x)
                    212:                        pt[1].x = n->bar.corner.x;
                    213:                texture(&display, Rpt(pt[0], pt[1]), txt[i], F_XOR);
                    214:                pt[0].x = pt[1].x;
                    215:        }
                    216: }
                    217: 
                    218: static char *timestr;
                    219: 
                    220: drawtime()
                    221: {
                    222:        if (size < TIMECORNER)
                    223:                return;
                    224:        string(&defont, timestr, &display,
                    225:                Pt (Drect.origin.x+TIMEORIGIN, Drect.origin.y+barheight-2), F_XOR);
                    226: }
                    227: 
                    228: gettime()
                    229: {
                    230:        char *p, *ctime();
                    231:        long l;
                    232: 
                    233:        l = time ((long *)0);
                    234:        p = ctime(&l);
                    235:        timestr = p+11;
                    236:        *(p+11+TIMELEN-1) = 0;
                    237: }
                    238: 
                    239: drawload()
                    240: {
                    241:        struct nodedef *n = &node0;
                    242: 
                    243:        if (size < LOADCORNER)
                    244:                return;
                    245:        string(&defont, n->load, &display, 
                    246:                Pt (Drect.origin.x+LOADORIGIN, n->bar.origin.y+barheight-6), F_XOR);
                    247: }
                    248: 
                    249: getload()
                    250: {
                    251:        double fabs();
                    252: 
                    253:        sprintf(node0.load, " %.2f %c%.2f", node0.m_new.l_runq,
                    254:                "-+"[node0.m_new.l_runq>node0.m_old.l_runq],
                    255:                                fabs(node0.m_new.l_runq-node0.m_old.l_runq));
                    256:        node0.m_old.l_runq = node0.m_new.l_runq;
                    257: }
                    258: 
                    259: /* CPU percentages */
                    260: docpu()
                    261: {
                    262:        register long *ln, *lo;
                    263:        register int sum;
                    264:        long diff[5];
                    265:        register long i;
                    266: 
                    267: #ifndef BSD
                    268:        i = node0.m_new.l_cp[3];
                    269:        node0.m_new.l_cp[3] = node0.m_new.l_cp[4];
                    270:        node0.m_new.l_cp[4] = i;
                    271: #endif BSD
                    272:        ln = node0.m_new.l_cp;
                    273:        lo = node0.m_old.l_cp;
                    274:        for(sum=i=0; i < NUMLOADBAR; i++) {
                    275:                diff[i] = *ln - *lo;
                    276:                sum += diff[i];
                    277:                *lo++ = *ln++;
                    278:        }
                    279:        sum = sum ? sum : 1;
                    280:        ln = node0.vec;
                    281:        lo = diff;
                    282:        for (i=0; i<NUMLOADBAR; i++) {
                    283:                *ln = (*lo * size) / sum;
                    284:                *ln++; *lo++;
                    285:        }
                    286: }
                    287: 
                    288: /* globals */
                    289: struct nlist nl[] ={
                    290:     {"_intrtime",0},
                    291:     {"_cp_time",0},
                    292:     {"_avenrun",0},
                    293:     { 0,0 },
                    294: };
                    295: 
                    296: #ifdef BSD
                    297: char *sys = "/vmunix";
                    298: #else
                    299: char *sys = "/unix";
                    300: #endif BSD
                    301: char *core = "/dev/kmem";
                    302: int mem;
                    303: 
                    304: /* imported */
                    305: extern long lseek();
                    306: 
                    307: /* initialize */
                    308: initload()
                    309: {
                    310:        nlist(sys, nl);
                    311: 
                    312:        mem = open(core, 0);
                    313:        if (mem<0) {
                    314:                printf("can't open %s\n", core);
                    315:                exit(1);
                    316:        }
                    317:        getinfo();
                    318:        node0.m_old = node0.m_new;
                    319: }
                    320: 
                    321: getinfo()
                    322: {
                    323:        long avenrun;
                    324: 
                    325:        lseek(mem, (long)nl[1].n_value, 0);
                    326:        read(mem, (char *)node0.m_new.l_cp, sizeof(node0.m_new.l_cp));
                    327:        lseek(mem, (long)nl[2].n_value, 0);
                    328: #ifdef BSD
                    329:        read(mem, (char *)&avenrun, sizeof(avenrun));
                    330:        node0.m_new.l_runq = (float)avenrun/FSCALE;
                    331: #else
                    332:        read(mem, (char *)&(node0.m_new.l_runq), sizeof(node0.m_new.l_runq));
                    333: #endif
                    334: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.