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 */
81 void (*update)(Graph*, long, ulong);
101 char *menu2str[Nvalue+1];
102 char xmenu2str[Nvalue+1][40];
104 Menu menu2 = {menu2str, 0};
106 Image *cols[Ncolor][3];
112 char argchars[] = "bcCeEfiIlmnsw";
114 int parity; /* toggled to avoid patterns in textured background */
116 int ngraph; /* totaly number is ngraph*nmach */
121 int sleeptime = 1000;
132 for(i=0; i<nmach; i++)
134 postnote(PNPROC, mach[i].pid, "kill");
144 fprint(2, "stats: out of memory allocating %ld: %r\n", sz);
152 erealloc(void *v, ulong sz)
156 fprint(2, "stats: out of memory reallocating %ld: %r\n", sz);
166 if((t = strdup(s)) == nil) {
167 fprint(2, "stats: out of memory in strdup(%.10s): %r\n", s);
174 mkcol(int i, int c0, int c1, int c2)
176 cols[i][0] = allocimagemix(display, c0, DWhite);
177 cols[i][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c1);
178 cols[i][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c2);
185 mediumfont = openfont(display, fontname);
186 if(mediumfont == nil)
187 mediumfont = openfont(display, "/lib/font/bit/pelm/latin1.8.font");
188 if(mediumfont == nil)
192 mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
194 mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
196 mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
198 mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
200 mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
202 cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
203 cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
204 cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
208 label(Point p, int dy, char *text)
219 if(p.y+mediumfont->height-Ysqueeze > maxy)
221 w = chartorune(r, s);
223 w = runestringwidth(mediumfont, r);
226 runestring(screen, p, display->black, ZP, mediumfont, r);
227 p.y += mediumfont->height-Ysqueeze;
234 return Pt(x+parity, 0);
238 datapoint(Graph *g, int x, ulong v, ulong vmax)
244 y = ((double)v)/(vmax*scale);
247 * Arrange scale to cover a factor of 1000.
248 * vmax corresponds to the 100 mark.
249 * 10*vmax is the top of the scale.
255 /* 1 now corresponds to the top; -2 to the bottom; rescale */
259 p.y = g->r.max.y - Dy(g->r)*y - Dot;
262 if(p.y > g->r.max.y-Dot)
263 p.y = g->r.max.y-Dot;
268 drawdatum(Graph *g, int x, ulong prev, ulong v, ulong vmax)
274 p = datapoint(g, x, v, vmax);
275 q = datapoint(g, x, prev, vmax);
277 draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
278 draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
279 draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
281 draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
282 draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
283 draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
289 redraw(Graph *g, int vmax)
298 draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
299 for(i=1; i<Dx(g->r); i++)
300 drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
301 drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
306 update1(Graph *g, long v, ulong vmax)
317 if(g->overflow && g->overtmp!=nil)
318 draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
319 draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
320 drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
321 memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
325 overflow = (v>10*vmax*scale);
327 overflow = (v>vmax*scale);
328 if(overflow && g->overtmp!=nil){
330 draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
331 sprint(buf, "%ld", v);
332 string(screen, g->overtmp->r.min, display->black, ZP, mediumfont, buf);
339 fprint(2, "usage: stats [-LY] [-F font] [-O] [-S scale] [-W winsize] [-%s] [machine...]\n", argchars);
340 threadexitsall("usage");
352 /* avoid two adjacent graphs of same color */
353 if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
356 graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
357 for(i=0; i<nmach; i++)
358 for(j=0; j<ngraph; j++)
359 graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
362 for(i=0; i<nmach; i++){
363 g = &graph[i*ngraph+(ngraph-1)];
364 memset(g, 0, sizeof(Graph));
366 g->label = menu2str[n]+Opwid;
367 g->update = update1; /* no other update functions yet */
369 g->colindex = nadd%Ncolor;
381 if(which > nelem(menu2str))
383 /* convert n to index in graph table */
385 for(i=0; i<ngraph; i++)
386 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
391 fprint(2, "stats: internal error can't drop graph\n");
395 graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
396 for(i=0; i<nmach; i++){
398 graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
399 free(ograph[i*ngraph+j].data);
400 freeimage(ograph[i*ngraph+j].overtmp);
401 for(j++; j<ngraph; j++)
402 graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
409 int initmach(Machine*, char*);
412 addmachine(char *name)
415 fprint(2, "stats: internal error: ngraph>0 in addmachine()\n");
419 nmach = 0; /* a little dance to get us started with local machine by default */
420 mach = erealloc(mach, (nmach+1)*sizeof(Machine));
421 memset(mach+nmach, 0, sizeof(Machine));
422 if (initmach(mach+nmach, name)){
430 newvalue(Machine *m, int i, ulong *v, ulong *vmax)
435 m->last[i] = m->val[i][0];
439 * Invert the ewma to obtain the 5s load statistics.
440 * Ewma is load' = (1884/2048)*load + (164/2048)*last5s, so we do
441 * last5s = (load' - (1884/2048)*load) / (164/2048).
443 if(++m->nload%5 == 0){
445 m->load = (now - (((vlong)m->last[i]*1884)/2048)) * 2048 / 164;
449 *vmax = m->val[i][1];
450 }else if(m->absolute[i]){
452 *vmax = m->val[i][1];
455 *v = (vlong)((now - m->last[i])*sleeptime)/1000;
457 *vmax = m->val[i][1];
464 labelstrs(Graph *g, char strs[Nlab][Lablen], int *np)
472 sprint(strs[j-1], "%g", scale*pow(10., j)*(double)vmax/100.);
476 sprint(strs[j-1], "%g", scale*(double)j*(double)vmax/4.0);
484 int i, j, n, w, maxw;
485 char strs[Nlab][Lablen];
488 for(i=0; i<ngraph; i++){
489 /* choose value for rightmost graph */
490 labelstrs(&graph[ngraph*(nmach-1)+i], strs, &n);
492 w = stringwidth(mediumfont, strs[j]);
503 int i, j, k, n, startx, starty, x, y, dx, dy, ly, ondata, maxx, wid, nlab;
507 char buf[128], labs[Nlab][Lablen];
509 draw(screen, screen->r, display->white, nil, ZP);
511 /* label left edge */
513 y = screen->r.min.y + Labspace+mediumfont->height+Labspace;
514 dy = (screen->r.max.y - y)/ngraph;
515 dx = Labspace+stringwidth(mediumfont, "0")+Labspace;
518 for(i=0; i<ngraph; i++,y+=dy){
519 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
520 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
521 label(Pt(x, y), dy, graph[i].label);
522 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
526 dx = (screen->r.max.x - startx)/nmach;
527 for(x=startx, i=0; i<nmach; i++,x+=dx){
528 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
529 j = dx/stringwidth(mediumfont, "0");
530 // n = mach[i].nproc;
532 if(n>1 && j>=1+3+(n>10)+(n>100)){ /* first char of name + (n) */
533 j -= 3+(n>10)+(n>100);
536 snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].name, n);
538 snprint(buf, sizeof buf, "%.*s", j, mach[i].name);
539 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP, mediumfont, buf);
542 maxx = screen->r.max.x;
544 /* label right, if requested */
545 if(ylabels && dy>Nlab*(mediumfont->height+1)){
547 if(wid < (maxx-startx)-30){
548 /* else there's not enough room */
550 draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), display->black, nil, ZP);
552 for(j=0; j<ngraph; j++, y+=dy){
553 /* choose value for rightmost graph */
554 g = &graph[ngraph*(nmach-1)+j];
555 labelstrs(g, labs, &nlab);
556 r = Rect(maxx+1, y, screen->r.max.x, y+dy-1);
558 r.max.y = screen->r.max.y;
559 draw(screen, r, cols[g->colindex][0], nil, paritypt(r.min.x));
560 for(k=0; k<nlab; k++){
561 ly = y + (dy*(nlab-k)/(nlab+1));
562 draw(screen, Rect(maxx+1, ly, maxx+1+Lx, ly+1), display->black, nil, ZP);
563 ly -= mediumfont->height/2;
564 string(screen, Pt(maxx+1+Lx, ly), display->black, ZP, mediumfont, labs[k]);
571 for(i=0; i<nmach; i++){
572 machr = Rect(startx+i*dx, starty, maxx, screen->r.max.y);
574 machr.max.x = startx+(i+1)*dx - 1;
576 for(j=0; j<ngraph; j++, y+=dy){
577 g = &graph[i*ngraph+j];
580 g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
581 g->data = erealloc(g->data, g->ndata*sizeof(ulong));
582 if(g->ndata > ondata)
583 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(ulong));
587 g->r.max.y = y+dy - 1;
589 g->r.max.y = screen->r.max.y;
590 draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
593 r.max.y = r.min.y+mediumfont->height;
594 r.max.x = r.min.x+stringwidth(mediumfont, "9999999");
595 freeimage(g->overtmp);
597 if(r.max.x <= g->r.max.x)
598 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
599 newvalue(g->mach, g->value, &v, &vmax);
604 flushimage(display, 1);
610 lockdisplay(display);
611 if(new && getwindow(display, Refnone) < 0) {
612 fprint(2, "stats: can't reattach to window\n");
616 unlockdisplay(display);
627 while(readmouse(mc) == 0){
630 for(i=0; i<Nvalue; i++)
632 memmove(menu2str[i], "drop ", Opwid);
634 memmove(menu2str[i], "add ", Opwid);
635 lockdisplay(display);
636 i = menuhit(3, mc, &menu2, nil);
644 unlockdisplay(display);
650 resizethread(void *v)
654 while(recv(mc->resizec, 0) == 1){
655 lockdisplay(display);
656 if(getwindow(display, Refnone) < 0)
657 sysfatal("attach to window: %r");
659 unlockdisplay(display);
664 keyboardthread(void *v)
668 while(recv(kc->c, &r) == 1)
669 if(r == 0x7F || r == 'q')
673 void machproc(void*);
674 void updateproc(void*);
677 threadmain(int argc, char *argv[])
685 mysysname = sysname();
686 if(mysysname == nil){
687 fprint(2, "stats: can't find sysname: %r\n");
688 threadexitsall("sysname");
713 fontname = EARGF(usage());
722 winsize = EARGF(usage());
725 if(nargs>=sizeof args || strchr(argchars, ARGC())==nil)
727 args[nargs++] = ARGC();
730 for(i=0; i<Nvalue; i++){
731 menu2str[i] = xmenu2str[i];
732 snprint(xmenu2str[i], sizeof xmenu2str[i], "add %s", labels[i]);
736 mach = emalloc(nmach*sizeof(Machine));
737 initmach(&mach[0], mysysname);
739 for(i=j=0; i<argc; i++)
743 for(i=0; i<nmach; i++)
744 proccreate(machproc, &mach[i], STACK);
746 for(i=0; i<nargs; i++)
749 fprint(2, "stats: internal error: unknown arg %c\n", args[i]);
799 for(i=0; i<nmach; i++)
800 for(j=0; j<ngraph; j++)
801 graph[i*ngraph+j].mach = &mach[i];
803 if(initdraw(0, nil, "stats") < 0)
804 sysfatal("initdraw: %r");
806 if((mc = initmouse(nil, screen)) == nil)
807 sysfatal("initmouse: %r");
808 if((kc = initkeyboard(nil)) == nil)
809 sysfatal("initkeyboard: %r");
811 display->locking = 1;
812 threadcreate(keyboardthread, nil, XSTACK);
813 threadcreate(mousethread, nil, XSTACK);
814 threadcreate(resizethread, nil, XSTACK);
815 proccreate(updateproc, nil, XSTACK);
817 unlockdisplay(display);
829 lockdisplay(display);
830 for(i=0; i<nmach*ngraph; i++){
831 newvalue(graph[i].mach, graph[i].value, &v, &vmax);
832 graph[i].update(&graph[i], v, vmax);
838 flushimage(display, 1);
839 unlockdisplay(display);
847 char buf[256], *f[4], *p;
854 n = read(m->fd, buf+t, sizeof buf-t);
859 while((p = memchr(buf, '\n', t)) != nil){
861 n = tokenize(buf, f, nelem(f));
863 for(i=0; i<Nvalue; i++){
864 if(strcmp(labels[i], f[0]) == 0){
869 m->val[i][0] = strtoul(f[1], 0, 0);
870 m->val[i][1] = strtoul(f[2], 0, 0);
883 postnote(PNPROC, m->pid, "kill");
889 initmach(Machine *m, char *name)
892 int p[2], kfd[3], pid;
895 if(strcmp(name, mysysname) == 0)
899 sysfatal("pipe: %r");
901 memset(args, 0, sizeof args);
902 args[0] = "auxstats";
905 if((q = strchr(name, ':')) != nil){
910 kfd[0] = open("/dev/null", OREAD);
913 if((pid = threadspawn(kfd, "auxstats", args)) < 0){
914 fprint(2, "spawn: %r\n");
922 if((q = strchr(m->name, '.')) != nil)