11 typedef struct Graph Graph;
12 typedef struct Machine Machine;
17 Ysqueeze = 2, /* vertical squeezing of label text */
18 Labspace = 2, /* room around label */
19 Dot = 2, /* height of dot */
20 Opwid = 5, /* strlen("add ") or strlen("drop ") */
21 Nlab = 3, /* max number of labels on y axis */
22 Lablen = 16, /* max length of label */
23 Lx = 4, /* label tick length */
83 void (*update)(Graph*, long, ulong);
103 char *menu2str[Nvalue+1];
104 char xmenu2str[Nvalue+1][40];
106 Menu menu2 = {menu2str, 0};
108 Image *cols[Ncolor][3];
114 char argchars[] = "8bcCeEfiIlmnsw";
116 int parity; /* toggled to avoid patterns in textured background */
118 int ngraph; /* totaly number is ngraph*nmach */
123 int sleeptime = 1000;
134 for(i=0; i<nmach; i++)
136 postnote(PNPROC, mach[i].pid, "kill");
146 fprint(2, "stats: out of memory allocating %ld: %r\n", sz);
154 erealloc(void *v, ulong sz)
158 fprint(2, "stats: out of memory reallocating %ld: %r\n", sz);
168 if((t = strdup(s)) == nil) {
169 fprint(2, "stats: out of memory in strdup(%.10s): %r\n", s);
176 mkcol(int i, int c0, int c1, int c2)
178 cols[i][0] = allocimagemix(display, c0, DWhite);
179 cols[i][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c1);
180 cols[i][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c2);
187 mediumfont = openfont(display, fontname);
188 if(mediumfont == nil)
189 mediumfont = openfont(display, "/lib/font/bit/pelm/latin1.8.font");
190 if(mediumfont == nil)
194 mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
196 mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
198 mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
200 mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
202 mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
204 cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
205 cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
206 cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
210 label(Point p, int dy, char *text)
221 if(p.y+mediumfont->height-Ysqueeze > maxy)
223 w = chartorune(r, s);
225 w = runestringwidth(mediumfont, r);
228 runestring(screen, p, display->black, ZP, mediumfont, r);
229 p.y += mediumfont->height-Ysqueeze;
236 return Pt(x+parity, 0);
240 datapoint(Graph *g, int x, ulong v, ulong vmax)
246 y = ((double)v)/(vmax*scale);
249 * Arrange scale to cover a factor of 1000.
250 * vmax corresponds to the 100 mark.
251 * 10*vmax is the top of the scale.
257 /* 1 now corresponds to the top; -2 to the bottom; rescale */
261 p.y = g->r.max.y - Dy(g->r)*y - Dot;
264 if(p.y > g->r.max.y-Dot)
265 p.y = g->r.max.y-Dot;
270 drawdatum(Graph *g, int x, ulong prev, ulong v, ulong vmax)
276 p = datapoint(g, x, v, vmax);
277 q = datapoint(g, x, prev, vmax);
279 draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
280 draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
281 draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
283 draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
284 draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
285 draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
291 redraw(Graph *g, int vmax)
300 draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
301 for(i=1; i<Dx(g->r); i++)
302 drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
303 drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
308 update1(Graph *g, long v, ulong vmax)
319 if(g->overflow && g->overtmp!=nil)
320 draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
321 draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
322 drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
323 memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
327 overflow = (v>10*vmax*scale);
329 overflow = (v>vmax*scale);
330 if(overflow && g->overtmp!=nil){
332 draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
333 sprint(buf, "%ld", v);
334 string(screen, g->overtmp->r.min, display->black, ZP, mediumfont, buf);
341 fprint(2, "usage: stats [-LY] [-F font] [-O] [-S scale] [-W winsize] [-%s] [machine...]\n", argchars);
342 threadexitsall("usage");
354 /* avoid two adjacent graphs of same color */
355 if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
358 graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
359 for(i=0; i<nmach; i++)
360 for(j=0; j<ngraph; j++)
361 graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
364 for(i=0; i<nmach; i++){
365 g = &graph[i*ngraph+(ngraph-1)];
366 memset(g, 0, sizeof(Graph));
368 g->label = menu2str[n]+Opwid;
369 g->update = update1; /* no other update functions yet */
371 g->colindex = nadd%Ncolor;
383 if(which > nelem(menu2str))
385 /* convert n to index in graph table */
387 for(i=0; i<ngraph; i++)
388 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
393 fprint(2, "stats: internal error can't drop graph\n");
397 graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
398 for(i=0; i<nmach; i++){
400 graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
401 free(ograph[i*ngraph+j].data);
402 freeimage(ograph[i*ngraph+j].overtmp);
403 for(j++; j<ngraph; j++)
404 graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
411 int initmach(Machine*, char*);
414 addmachine(char *name)
417 fprint(2, "stats: internal error: ngraph>0 in addmachine()\n");
421 nmach = 0; /* a little dance to get us started with local machine by default */
422 mach = erealloc(mach, (nmach+1)*sizeof(Machine));
423 memset(mach+nmach, 0, sizeof(Machine));
424 if (initmach(mach+nmach, name)){
432 newvalue(Machine *m, int i, ulong *v, ulong *vmax)
437 m->last[i] = m->val[i][0];
441 * Invert the ewma to obtain the 5s load statistics.
442 * Ewma is load' = (1884/2048)*load + (164/2048)*last5s, so we do
443 * last5s = (load' - (1884/2048)*load) / (164/2048).
445 if(++m->nload%5 == 0){
447 m->load = (now - (((vlong)m->last[i]*1884)/2048)) * 2048 / 164;
451 *vmax = m->val[i][1];
452 }else if(m->absolute[i]){
454 *vmax = m->val[i][1];
457 *v = (vlong)((now - m->last[i])*sleeptime)/1000;
459 *vmax = m->val[i][1];
466 labelstrs(Graph *g, char strs[Nlab][Lablen], int *np)
474 sprint(strs[j-1], "%g", scale*pow(10., j)*(double)vmax/100.);
478 sprint(strs[j-1], "%g", scale*(double)j*(double)vmax/4.0);
486 int i, j, n, w, maxw;
487 char strs[Nlab][Lablen];
490 for(i=0; i<ngraph; i++){
491 /* choose value for rightmost graph */
492 labelstrs(&graph[ngraph*(nmach-1)+i], strs, &n);
494 w = stringwidth(mediumfont, strs[j]);
505 int i, j, k, n, startx, starty, x, y, dx, dy, ly, ondata, maxx, wid, nlab;
509 char buf[128], labs[Nlab][Lablen];
511 draw(screen, screen->r, display->white, nil, ZP);
513 /* label left edge */
515 y = screen->r.min.y + Labspace+mediumfont->height+Labspace;
516 dy = (screen->r.max.y - y)/ngraph;
517 dx = Labspace+stringwidth(mediumfont, "0")+Labspace;
520 for(i=0; i<ngraph; i++,y+=dy){
521 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
522 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
523 label(Pt(x, y), dy, graph[i].label);
524 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
528 dx = (screen->r.max.x - startx)/nmach;
529 for(x=startx, i=0; i<nmach; i++,x+=dx){
530 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
531 j = dx/stringwidth(mediumfont, "0");
532 /* n = mach[i].nproc; */
534 if(n>1 && j>=1+3+(n>10)+(n>100)){ /* first char of name + (n) */
535 j -= 3+(n>10)+(n>100);
538 snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].name, n);
540 snprint(buf, sizeof buf, "%.*s", j, mach[i].name);
541 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP, mediumfont, buf);
544 maxx = screen->r.max.x;
546 /* label right, if requested */
547 if(ylabels && dy>Nlab*(mediumfont->height+1)){
549 if(wid < (maxx-startx)-30){
550 /* else there's not enough room */
552 draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), display->black, nil, ZP);
554 for(j=0; j<ngraph; j++, y+=dy){
555 /* choose value for rightmost graph */
556 g = &graph[ngraph*(nmach-1)+j];
557 labelstrs(g, labs, &nlab);
558 r = Rect(maxx+1, y, screen->r.max.x, y+dy-1);
560 r.max.y = screen->r.max.y;
561 draw(screen, r, cols[g->colindex][0], nil, paritypt(r.min.x));
562 for(k=0; k<nlab; k++){
563 ly = y + (dy*(nlab-k)/(nlab+1));
564 draw(screen, Rect(maxx+1, ly, maxx+1+Lx, ly+1), display->black, nil, ZP);
565 ly -= mediumfont->height/2;
566 string(screen, Pt(maxx+1+Lx, ly), display->black, ZP, mediumfont, labs[k]);
573 for(i=0; i<nmach; i++){
574 machr = Rect(startx+i*dx, starty, maxx, screen->r.max.y);
576 machr.max.x = startx+(i+1)*dx - 1;
578 for(j=0; j<ngraph; j++, y+=dy){
579 g = &graph[i*ngraph+j];
582 g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
583 g->data = erealloc(g->data, g->ndata*sizeof(ulong));
584 if(g->ndata > ondata)
585 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(ulong));
589 g->r.max.y = y+dy - 1;
591 g->r.max.y = screen->r.max.y;
592 draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
595 r.max.y = r.min.y+mediumfont->height;
596 r.max.x = r.min.x+stringwidth(mediumfont, "9999999");
597 freeimage(g->overtmp);
599 if(r.max.x <= g->r.max.x)
600 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
601 newvalue(g->mach, g->value, &v, &vmax);
606 flushimage(display, 1);
612 lockdisplay(display);
613 if(new && getwindow(display, Refnone) < 0) {
614 fprint(2, "stats: can't reattach to window\n");
618 unlockdisplay(display);
629 while(readmouse(mc) == 0){
632 for(i=0; i<Nvalue; i++)
634 memmove(menu2str[i], "drop ", Opwid);
636 memmove(menu2str[i], "add ", Opwid);
637 lockdisplay(display);
638 i = menuhit(3, mc, &menu2, nil);
646 unlockdisplay(display);
652 resizethread(void *v)
656 while(recv(mc->resizec, 0) == 1){
657 lockdisplay(display);
658 if(getwindow(display, Refnone) < 0)
659 sysfatal("attach to window: %r");
661 unlockdisplay(display);
666 keyboardthread(void *v)
670 while(recv(kc->c, &r) == 1)
671 if(r == 0x7F || r == 'q')
675 void machproc(void*);
676 void updateproc(void*);
679 threadmain(int argc, char *argv[])
687 mysysname = sysname();
688 if(mysysname == nil){
689 fprint(2, "stats: can't find sysname: %r\n");
690 threadexitsall("sysname");
715 fontname = EARGF(usage());
724 winsize = EARGF(usage());
727 if(nargs>=sizeof args || strchr(argchars, ARGC())==nil)
729 args[nargs++] = ARGC();
732 for(i=0; i<Nvalue; i++){
733 menu2str[i] = xmenu2str[i];
734 snprint(xmenu2str[i], sizeof xmenu2str[i], "add %s", labels[i]);
738 mach = emalloc(nmach*sizeof(Machine));
739 initmach(&mach[0], mysysname);
741 for(i=j=0; i<argc; i++)
745 for(i=0; i<nmach; i++)
746 proccreate(machproc, &mach[i], STACK);
748 for(i=0; i<nargs; i++)
751 fprint(2, "stats: internal error: unknown arg %c\n", args[i]);
804 for(i=0; i<nmach; i++)
805 for(j=0; j<ngraph; j++)
806 graph[i*ngraph+j].mach = &mach[i];
808 if(initdraw(0, nil, "stats") < 0)
809 sysfatal("initdraw: %r");
811 if((mc = initmouse(nil, screen)) == nil)
812 sysfatal("initmouse: %r");
813 if((kc = initkeyboard(nil)) == nil)
814 sysfatal("initkeyboard: %r");
816 display->locking = 1;
817 threadcreate(keyboardthread, nil, XSTACK);
818 threadcreate(mousethread, nil, XSTACK);
819 threadcreate(resizethread, nil, XSTACK);
820 proccreate(updateproc, nil, XSTACK);
822 unlockdisplay(display);
834 lockdisplay(display);
835 for(i=0; i<nmach*ngraph; i++){
836 newvalue(graph[i].mach, graph[i].value, &v, &vmax);
837 graph[i].update(&graph[i], v, vmax);
843 flushimage(display, 1);
844 unlockdisplay(display);
852 char buf[256], *f[4], *p;
859 n = read(m->fd, buf+t, sizeof buf-t);
864 while((p = memchr(buf, '\n', t)) != nil){
866 n = tokenize(buf, f, nelem(f));
868 for(i=0; i<Nvalue; i++){
869 if(strcmp(labels[i], f[0]) == 0){
874 m->val[i][0] = strtoul(f[1], 0, 0);
875 m->val[i][1] = strtoul(f[2], 0, 0);
888 postnote(PNPROC, m->pid, "kill");
894 initmach(Machine *m, char *name)
897 int p[2], kfd[3], pid;
900 if(strcmp(name, mysysname) == 0)
904 sysfatal("pipe: %r");
906 memset(args, 0, sizeof args);
907 args[0] = "auxstats";
910 if((q = strchr(name, ':')) != nil){
915 kfd[0] = open("/dev/null", OREAD);
918 if((pid = threadspawn(kfd, "auxstats", args)) < 0){
919 fprint(2, "spawn: %r\n");
927 if((q = strchr(m->name, '.')) != nil)