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];
111 char argchars[] = "bceEfiIlmnsw";
113 int parity; /* toggled to avoid patterns in textured background */
115 int ngraph; /* totaly number is ngraph*nmach */
120 int sleeptime = 1000;
131 for(i=0; i<nmach; i++)
133 postnote(PNPROC, mach[i].pid, "kill");
143 fprint(2, "stats: out of memory allocating %ld: %r\n", sz);
151 erealloc(void *v, ulong sz)
155 fprint(2, "stats: out of memory reallocating %ld: %r\n", sz);
165 if((t = strdup(s)) == nil) {
166 fprint(2, "stats: out of memory in strdup(%.10s): %r\n", s);
173 mkcol(int i, int c0, int c1, int c2)
175 cols[i][0] = allocimagemix(display, c0, DWhite);
176 cols[i][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c1);
177 cols[i][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c2);
183 mediumfont = openfont(display, "/lib/font/bit/pelm/latin1.8.font");
184 if(mediumfont == nil)
188 mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
190 mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
192 mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
194 mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
196 mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
198 cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
199 cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
200 cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
204 label(Point p, int dy, char *text)
215 if(p.y+mediumfont->height-Ysqueeze > maxy)
217 w = chartorune(r, s);
219 w = runestringwidth(mediumfont, r);
222 runestring(screen, p, display->black, ZP, mediumfont, r);
223 p.y += mediumfont->height-Ysqueeze;
230 return Pt(x+parity, 0);
234 datapoint(Graph *g, int x, ulong v, ulong vmax)
240 y = ((double)v)/(vmax*scale);
243 * Arrange scale to cover a factor of 1000.
244 * vmax corresponds to the 100 mark.
245 * 10*vmax is the top of the scale.
251 /* 1 now corresponds to the top; -2 to the bottom; rescale */
255 p.y = g->r.max.y - Dy(g->r)*y - Dot;
258 if(p.y > g->r.max.y-Dot)
259 p.y = g->r.max.y-Dot;
264 drawdatum(Graph *g, int x, ulong prev, ulong v, ulong vmax)
270 p = datapoint(g, x, v, vmax);
271 q = datapoint(g, x, prev, vmax);
273 draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
274 draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
275 draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
277 draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
278 draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
279 draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
285 redraw(Graph *g, int vmax)
294 draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
295 for(i=1; i<Dx(g->r); i++)
296 drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
297 drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
302 update1(Graph *g, long v, ulong vmax)
313 if(g->overflow && g->overtmp!=nil)
314 draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
315 draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
316 drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
317 memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
321 overflow = (v>10*vmax*scale);
323 overflow = (v>vmax*scale);
324 if(overflow && g->overtmp!=nil){
326 draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
327 sprint(buf, "%ld", v);
328 string(screen, g->overtmp->r.min, display->black, ZP, mediumfont, buf);
335 fprint(2, "usage: stats [-O] [-S scale] [-LY] [-W winsize] [-%s] [machine...]\n", argchars);
336 threadexitsall("usage");
348 /* avoid two adjacent graphs of same color */
349 if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
352 graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
353 for(i=0; i<nmach; i++)
354 for(j=0; j<ngraph; j++)
355 graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
358 for(i=0; i<nmach; i++){
359 g = &graph[i*ngraph+(ngraph-1)];
360 memset(g, 0, sizeof(Graph));
362 g->label = menu2str[n]+Opwid;
363 g->update = update1; /* no other update functions yet */
365 g->colindex = nadd%Ncolor;
377 if(which > nelem(menu2str))
379 /* convert n to index in graph table */
381 for(i=0; i<ngraph; i++)
382 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
387 fprint(2, "stats: internal error can't drop graph\n");
391 graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
392 for(i=0; i<nmach; i++){
394 graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
395 free(ograph[i*ngraph+j].data);
396 freeimage(ograph[i*ngraph+j].overtmp);
397 for(j++; j<ngraph; j++)
398 graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
405 int initmach(Machine*, char*);
408 addmachine(char *name)
411 fprint(2, "stats: internal error: ngraph>0 in addmachine()\n");
415 nmach = 0; /* a little dance to get us started with local machine by default */
416 mach = erealloc(mach, (nmach+1)*sizeof(Machine));
417 memset(mach+nmach, 0, sizeof(Machine));
418 if (initmach(mach+nmach, name)){
426 newvalue(Machine *m, int i, ulong *v, ulong *vmax)
431 m->last[i] = m->val[i][0];
435 * Invert the ewma to obtain the 5s load statistics.
436 * Ewma is load' = (1884/2048)*load + (164/2048)*last5s, so we do
437 * last5s = (load' - (1884/2048)*load) / (164/2048).
439 if(++m->nload%5 == 0){
441 m->load = (now - (((vlong)m->last[i]*1884)/2048)) * 2048 / 164;
445 *vmax = m->val[i][1];
446 }else if(m->absolute[i]){
448 *vmax = m->val[i][1];
451 *v = (vlong)((now - m->last[i])*sleeptime)/1000;
453 *vmax = m->val[i][1];
460 labelstrs(Graph *g, char strs[Nlab][Lablen], int *np)
468 sprint(strs[j-1], "%g", scale*pow(10., j)*(double)vmax/100.);
472 sprint(strs[j-1], "%g", scale*(double)j*(double)vmax/4.0);
480 int i, j, n, w, maxw;
481 char strs[Nlab][Lablen];
484 for(i=0; i<ngraph; i++){
485 /* choose value for rightmost graph */
486 labelstrs(&graph[ngraph*(nmach-1)+i], strs, &n);
488 w = stringwidth(mediumfont, strs[j]);
499 int i, j, k, n, startx, starty, x, y, dx, dy, ly, ondata, maxx, wid, nlab;
503 char buf[128], labs[Nlab][Lablen];
505 draw(screen, screen->r, display->white, nil, ZP);
507 /* label left edge */
509 y = screen->r.min.y + Labspace+mediumfont->height+Labspace;
510 dy = (screen->r.max.y - y)/ngraph;
511 dx = Labspace+stringwidth(mediumfont, "0")+Labspace;
514 for(i=0; i<ngraph; i++,y+=dy){
515 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
516 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
517 label(Pt(x, y), dy, graph[i].label);
518 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
522 dx = (screen->r.max.x - startx)/nmach;
523 for(x=startx, i=0; i<nmach; i++,x+=dx){
524 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
525 j = dx/stringwidth(mediumfont, "0");
526 // n = mach[i].nproc;
528 if(n>1 && j>=1+3+(n>10)+(n>100)){ /* first char of name + (n) */
529 j -= 3+(n>10)+(n>100);
532 snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].name, n);
534 snprint(buf, sizeof buf, "%.*s", j, mach[i].name);
535 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP, mediumfont, buf);
538 maxx = screen->r.max.x;
540 /* label right, if requested */
541 if(ylabels && dy>Nlab*(mediumfont->height+1)){
543 if(wid < (maxx-startx)-30){
544 /* else there's not enough room */
546 draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), display->black, nil, ZP);
548 for(j=0; j<ngraph; j++, y+=dy){
549 /* choose value for rightmost graph */
550 g = &graph[ngraph*(nmach-1)+j];
551 labelstrs(g, labs, &nlab);
552 r = Rect(maxx+1, y, screen->r.max.x, y+dy-1);
554 r.max.y = screen->r.max.y;
555 draw(screen, r, cols[g->colindex][0], nil, paritypt(r.min.x));
556 for(k=0; k<nlab; k++){
557 ly = y + (dy*(nlab-k)/(nlab+1));
558 draw(screen, Rect(maxx+1, ly, maxx+1+Lx, ly+1), display->black, nil, ZP);
559 ly -= mediumfont->height/2;
560 string(screen, Pt(maxx+1+Lx, ly), display->black, ZP, mediumfont, labs[k]);
567 for(i=0; i<nmach; i++){
568 machr = Rect(startx+i*dx, starty, maxx, screen->r.max.y);
570 machr.max.x = startx+(i+1)*dx - 1;
572 for(j=0; j<ngraph; j++, y+=dy){
573 g = &graph[i*ngraph+j];
576 g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
577 g->data = erealloc(g->data, g->ndata*sizeof(ulong));
578 if(g->ndata > ondata)
579 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(ulong));
583 g->r.max.y = y+dy - 1;
585 g->r.max.y = screen->r.max.y;
586 draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
589 r.max.y = r.min.y+mediumfont->height;
590 r.max.x = r.min.x+stringwidth(mediumfont, "9999999");
591 freeimage(g->overtmp);
593 if(r.max.x <= g->r.max.x)
594 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
595 newvalue(g->mach, g->value, &v, &vmax);
600 flushimage(display, 1);
606 lockdisplay(display);
607 if(new && getwindow(display, Refnone) < 0) {
608 fprint(2, "stats: can't reattach to window\n");
612 unlockdisplay(display);
623 while(readmouse(mc) == 0){
626 for(i=0; i<Nvalue; i++)
628 memmove(menu2str[i], "drop ", Opwid);
630 memmove(menu2str[i], "add ", Opwid);
631 lockdisplay(display);
632 i = menuhit(3, mc, &menu2, nil);
640 unlockdisplay(display);
646 resizethread(void *v)
650 while(recv(mc->resizec, 0) == 1){
651 lockdisplay(display);
652 if(getwindow(display, Refnone) < 0)
653 sysfatal("attach to window: %r");
655 unlockdisplay(display);
660 keyboardthread(void *v)
664 while(recv(kc->c, &r) == 1)
665 if(r == 0x7F || r == 'q')
669 void machproc(void*);
670 void updateproc(void*);
673 threadmain(int argc, char *argv[])
681 mysysname = sysname();
682 if(mysysname == nil){
683 fprint(2, "stats: can't find sysname: %r\n");
684 threadexitsall("sysname");
715 winsize = EARGF(usage());
718 if(nargs>=sizeof args || strchr(argchars, ARGC())==nil)
720 args[nargs++] = ARGC();
723 for(i=0; i<Nvalue; i++){
724 menu2str[i] = xmenu2str[i];
725 snprint(xmenu2str[i], sizeof xmenu2str[i], "add %s", labels[i]);
729 mach = emalloc(nmach*sizeof(Machine));
730 initmach(&mach[0], mysysname);
732 for(i=j=0; i<argc; i++)
736 for(i=0; i<nmach; i++)
737 proccreate(machproc, &mach[i], STACK);
739 for(i=0; i<nargs; i++)
742 fprint(2, "stats: internal error: unknown arg %c\n", args[i]);
792 for(i=0; i<nmach; i++)
793 for(j=0; j<ngraph; j++)
794 graph[i*ngraph+j].mach = &mach[i];
796 if(initdraw(0, nil, "stats") < 0)
797 sysfatal("initdraw: %r");
799 if((mc = initmouse(nil, screen)) == nil)
800 sysfatal("initmouse: %r");
801 if((kc = initkeyboard(nil)) == nil)
802 sysfatal("initkeyboard: %r");
804 display->locking = 1;
805 threadcreate(keyboardthread, nil, XSTACK);
806 threadcreate(mousethread, nil, XSTACK);
807 threadcreate(resizethread, nil, XSTACK);
808 proccreate(updateproc, nil, XSTACK);
810 unlockdisplay(display);
822 lockdisplay(display);
823 for(i=0; i<nmach*ngraph; i++){
824 newvalue(graph[i].mach, graph[i].value, &v, &vmax);
825 graph[i].update(&graph[i], v, vmax);
831 flushimage(display, 1);
832 unlockdisplay(display);
840 char buf[256], *f[4], *p;
847 n = read(m->fd, buf+t, sizeof buf-t);
852 while((p = memchr(buf, '\n', t)) != nil){
854 n = tokenize(buf, f, nelem(f));
856 for(i=0; i<Nvalue; i++){
857 if(strcmp(labels[i], f[0]) == 0){
862 m->val[i][0] = strtoul(f[1], 0, 0);
863 m->val[i][1] = strtoul(f[2], 0, 0);
876 postnote(PNPROC, m->pid, "kill");
882 initmach(Machine *m, char *name)
885 int p[2], kfd[3], pid;
888 if(strcmp(name, mysysname) == 0)
892 sysfatal("pipe: %r");
894 memset(args, 0, sizeof args);
895 args[0] = "auxstats";
898 if((q = strchr(name, ':')) != nil){
903 kfd[0] = open("/dev/null", OREAD);
906 if((pid = threadspawn(kfd, "auxstats", args)) < 0){
907 fprint(2, "spawn: %r\n");
915 if((q = strchr(m->name, '.')) != nil)