From e21fee604e3b53fd8c57ac3817e6d829f62d6ee3 Mon Sep 17 00:00:00 2001 From: rsc Date: Fri, 23 Apr 2004 05:14:58 +0000 Subject: move these here --- src/cmd/draw/stats.c | 884 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 884 insertions(+) create mode 100644 src/cmd/draw/stats.c (limited to 'src/cmd/draw/stats.c') diff --git a/src/cmd/draw/stats.c b/src/cmd/draw/stats.c new file mode 100644 index 00000000..07d20ad4 --- /dev/null +++ b/src/cmd/draw/stats.c @@ -0,0 +1,884 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct Graph Graph; +typedef struct Machine Machine; + +enum +{ + Ncolor = 6, + Ysqueeze = 2, /* vertical squeezing of label text */ + Labspace = 2, /* room around label */ + Dot = 2, /* height of dot */ + Opwid = 5, /* strlen("add ") or strlen("drop ") */ + Nlab = 3, /* max number of labels on y axis */ + Lablen = 16, /* max length of label */ + Lx = 4, /* label tick length */ + + STACK = 8192, + XSTACK = 32768, +}; + +enum +{ + Vbattery, + Vcontext, + Vcpu, + Vether, + Vethererr, + Vetherin, + Vetherout, + Vfault, + Vfork, + Vidle, + Vintr, + Vload, + Vmem, + Vswap, + Vsys, + Vsyscall, + Vuser, + Nvalue, +}; + +char* +labels[Nvalue] = +{ + "battery", + "context", + "cpu", + "ether", + "ethererr", + "etherin", + "etherout", + "fault", + "fork", + "idle", + "intr", + "load", + "mem", + "swap", + "sys", + "syscall", + "user", +}; + +struct Graph +{ + int colindex; + Rectangle r; + int *data; + int ndata; + char *label; + int value; + void (*update)(Graph*, ulong, ulong); + Machine *mach; + int overflow; + Image *overtmp; + ulong vmax; +}; + +struct Machine +{ + char *name; + int fd; + int pid; + int dead; + int absolute[Nvalue]; + ulong last[Nvalue]; + ulong val[Nvalue][2]; +}; + +char *menu2str[Nvalue+1]; +char xmenu2str[Nvalue+1][40]; + +Menu menu2 = {menu2str, nil}; +int present[Nvalue]; +Image *cols[Ncolor][3]; +Graph *graph; +Machine *mach; +Font *mediumfont; +char *mysysname; +char argchars[] = "bceEfiIlmnsw"; +int pids[1024]; +int parity; /* toggled to avoid patterns in textured background */ +int nmach; +int ngraph; /* totaly number is ngraph*nmach */ +double scale = 1.0; +int logscale = 0; +int ylabels = 0; +int oldsystem = 0; +int sleeptime = 1000; +int changedvmax; + +Mousectl *mc; +Keyboardctl *kc; + +void +killall(char *s) +{ + int i; + + for(i=0; iheight-Ysqueeze > maxy) + break; + w = chartorune(r, s); + s += w; + w = runestringwidth(mediumfont, r); + if(w > maxw) + maxw = w; + runestring(screen, p, display->black, ZP, mediumfont, r); + p.y += mediumfont->height-Ysqueeze; + } +} + +Point +paritypt(int x) +{ + return Pt(x+parity, 0); +} + +Point +datapoint(Graph *g, int x, ulong v, ulong vmax) +{ + Point p; + double y; + + p.x = x; + y = ((double)v)/(vmax*scale); + if(logscale){ + /* + * Arrange scale to cover a factor of 1000. + * vmax corresponds to the 100 mark. + * 10*vmax is the top of the scale. + */ + if(y <= 0.) + y = 0; + else{ + y = log10(y); + /* 1 now corresponds to the top; -2 to the bottom; rescale */ + y = (y+2.)/3.; + } + } + p.y = g->r.max.y - Dy(g->r)*y - Dot; + if(p.y < g->r.min.y) + p.y = g->r.min.y; + if(p.y > g->r.max.y-Dot) + p.y = g->r.max.y-Dot; + return p; +} + +void +drawdatum(Graph *g, int x, ulong prev, ulong v, ulong vmax) +{ + int c; + Point p, q; + + c = g->colindex; + p = datapoint(g, x, v, vmax); + q = datapoint(g, x, prev, vmax); + if(p.y < q.y){ + draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x)); + draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP); + draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP); + }else{ + draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x)); + draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP); + draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP); + } + +} + +void +redraw(Graph *g, int vmax) +{ + int i, c; + + if(vmax != g->vmax){ + g->vmax = vmax; + changedvmax = 1; + } + c = g->colindex; + draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x)); + for(i=1; ir); i++) + drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax); + drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax); + g->overflow = 0; +} + +void +update1(Graph *g, ulong v, ulong vmax) +{ + char buf[32]; + int overflow; + + if(vmax != g->vmax){ + g->vmax = vmax; + changedvmax = 1; + } + if(g->overflow && g->overtmp!=nil) + draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min); + draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y)); + drawdatum(g, g->r.max.x-1, g->data[0], v, vmax); + memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0])); + g->data[0] = v; + g->overflow = 0; + if(logscale) + overflow = (v>10*vmax*scale); + else + overflow = (v>vmax*scale); + if(overflow && g->overtmp!=nil){ + g->overflow = 1; + draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min); + sprint(buf, "%ld", v); + string(screen, g->overtmp->r.min, display->black, ZP, mediumfont, buf); + } +} + +void +usage(void) +{ + fprint(2, "usage: stats [-O] [-S scale] [-LY] [-%s] [machine...]\n", argchars); + exits("usage"); +} + +void +addgraph(int n) +{ + Graph *g, *ograph; + int i, j; + static int nadd; + + if(n > Nvalue) + abort(); + /* avoid two adjacent graphs of same color */ + if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor) + nadd++; + ograph = graph; + graph = emalloc(nmach*(ngraph+1)*sizeof(Graph)); + for(i=0; ivalue = n; + g->label = menu2str[n]+Opwid; + g->update = update1; /* no other update functions yet */ + g->mach = &mach[i]; + g->colindex = nadd%Ncolor; + } + present[n] = 1; + nadd++; +} + +void +dropgraph(int which) +{ + Graph *ograph; + int i, j, n; + + if(which > nelem(menu2str)) + abort(); + /* convert n to index in graph table */ + n = -1; + for(i=0; i 0){ + fprint(2, "stats: internal error: ngraph>0 in addmachine()\n"); + usage(); + } + if(mach == nil) + nmach = 0; /* a little dance to get us started with local machine by default */ + mach = erealloc(mach, (nmach+1)*sizeof(Machine)); + memset(mach+nmach, 0, sizeof(Machine)); + if (initmach(mach+nmach, name)){ + nmach++; + return 1; + } else + return 0; +} + +void +newvalue(Machine *m, int i, ulong *v, ulong *vmax) +{ + ulong now; + + if(m->absolute[i]){ + *v = m->val[i][0]; + *vmax = m->val[i][1]; + }else{ + now = m->val[i][0]; + *v = (vlong)((now - m->last[i])*sleeptime)/1000; + m->last[i] = now; + *vmax = m->val[i][1]; + } + if(*vmax == 0) + *vmax = 1; +} + +void +labelstrs(Graph *g, char strs[Nlab][Lablen], int *np) +{ + int j; + ulong vmax; + + vmax = g->vmax; + if(logscale){ + for(j=1; j<=2; j++) + sprint(strs[j-1], "%g", scale*pow(10., j)*(double)vmax/100.); + *np = 2; + }else{ + for(j=1; j<=3; j++) + sprint(strs[j-1], "%g", scale*(double)j*(double)vmax/4.0); + *np = 3; + } +} + +int +labelwidth(void) +{ + int i, j, n, w, maxw; + char strs[Nlab][Lablen]; + + maxw = 0; + for(i=0; i maxw) + maxw = w; + } + } + return maxw; +} + +void +resize(void) +{ + int i, j, k, n, startx, starty, x, y, dx, dy, ly, ondata, maxx, wid, nlab; + Graph *g; + Rectangle machr, r; + ulong v, vmax; + char buf[128], labs[Nlab][Lablen]; + + draw(screen, screen->r, display->white, nil, ZP); + + /* label left edge */ + x = screen->r.min.x; + y = screen->r.min.y + Labspace+mediumfont->height+Labspace; + dy = (screen->r.max.y - y)/ngraph; + dx = Labspace+stringwidth(mediumfont, "0")+Labspace; + startx = x+dx+1; + starty = y; + for(i=0; ir.max.x, y), display->black, nil, ZP); + draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x)); + label(Pt(x, y), dy, graph[i].label); + draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP); + } + + /* label top edge */ + dx = (screen->r.max.x - startx)/nmach; + for(x=startx, i=0; ir.max.y), display->black, nil, ZP); + j = dx/stringwidth(mediumfont, "0"); + // n = mach[i].nproc; + n = 1; + if(n>1 && j>=1+3+(n>10)+(n>100)){ /* first char of name + (n) */ + j -= 3+(n>10)+(n>100); + if(j <= 0) + j = 1; + snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].name, n); + }else + snprint(buf, sizeof buf, "%.*s", j, mach[i].name); + string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP, mediumfont, buf); + } + + maxx = screen->r.max.x; + + /* label right, if requested */ + if(ylabels && dy>Nlab*(mediumfont->height+1)){ + wid = labelwidth(); + if(wid < (maxx-startx)-30){ + /* else there's not enough room */ + maxx -= 1+Lx+wid; + draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), display->black, nil, ZP); + y = starty; + for(j=0; jr.max.x, y+dy-1); + if(j == ngraph-1) + r.max.y = screen->r.max.y; + draw(screen, r, cols[g->colindex][0], nil, paritypt(r.min.x)); + for(k=0; kblack, nil, ZP); + ly -= mediumfont->height/2; + string(screen, Pt(maxx+1+Lx, ly), display->black, ZP, mediumfont, labs[k]); + } + } + } + } + + /* create graphs */ + for(i=0; ir.max.y); + if(i < nmach-1) + machr.max.x = startx+(i+1)*dx - 1; + y = starty; + for(j=0; jndata; + g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */ + g->data = erealloc(g->data, g->ndata*sizeof(ulong)); + if(g->ndata > ondata) + memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(ulong)); + /* set geometry */ + g->r = machr; + g->r.min.y = y; + g->r.max.y = y+dy - 1; + if(j == ngraph-1) + g->r.max.y = screen->r.max.y; + draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x)); + g->overflow = 0; + r = g->r; + r.max.y = r.min.y+mediumfont->height; + r.max.x = r.min.x+stringwidth(mediumfont, "9999999"); + freeimage(g->overtmp); + g->overtmp = nil; + if(r.max.x <= g->r.max.x) + g->overtmp = allocimage(display, r, screen->chan, 0, -1); + newvalue(g->mach, g->value, &v, &vmax); + redraw(g, vmax); + } + } + + flushimage(display, 1); +} + +void +eresized(int new) +{ + lockdisplay(display); + if(new && getwindow(display, Refnone) < 0) { + fprint(2, "stats: can't reattach to window\n"); + killall("reattach"); + } + resize(); + unlockdisplay(display); +} + +void +mousethread(void *v) +{ + Mouse m; + int i; + + USED(v); + + while(readmouse(mc) == 0){ + m = mc->m; + if(m.buttons == 4){ + for(i=0; i= 0){ + if(!present[i]) + addgraph(i); + else if(ngraph > 1) + dropgraph(i); + resize(); + } + unlockdisplay(display); + } + } +} + +void +resizethread(void *v) +{ + USED(v); + + while(recv(mc->resizec, 0) == 1){ + lockdisplay(display); + if(getwindow(display, Refnone) < 0) + sysfatal("attach to window: %r"); + resize(); + unlockdisplay(display); + } +} + +void +keyboardthread(void *v) +{ + Rune r; + + while(recv(kc->c, &r) == 1) + if(r == 0x7F || r == 'q') + killall("quit"); +} + +void machthread(void*); + +void +threadmain(int argc, char *argv[]) +{ + int i, j; + char *s; + ulong v, vmax, nargs; + char args[100]; + + nmach = 1; + mysysname = sysname(); + if(mysysname == nil){ + fprint(2, "stats: can't find sysname: %r\n"); + exits("sysname"); + } + + nargs = 0; + ARGBEGIN{ + case 'T': + s = ARGF(); + if(s == nil) + usage(); + i = atoi(s); + if(i > 0) + sleeptime = 1000*i; + break; + case 'S': + s = ARGF(); + if(s == nil) + usage(); + scale = atof(s); + if(scale <= 0.) + usage(); + break; + case 'L': + logscale++; + break; + case 'Y': + ylabels++; + break; + case 'O': + oldsystem = 1; + break; + default: + if(nargs>=sizeof args || strchr(argchars, ARGC())==nil) + usage(); + args[nargs++] = ARGC(); + }ARGEND + + for(i=0; ilocking = 1; + threadcreate(keyboardthread, nil, XSTACK); + threadcreate(mousethread, nil, XSTACK); + threadcreate(resizethread, nil, XSTACK); + + resize(); + unlockdisplay(display); + + for(;;){ + parity = 1-parity; + lockdisplay(display); + for(i=0; ifd, buf+t, sizeof buf-t); + m->dead = 0; + if(n <= 0) + break; + t += n; + while((p = memchr(buf, '\n', t)) != nil){ + *p++ = 0; + n = tokenize(buf, f, nelem(f)); + if(n >= 3){ + for(i=0; ifd = p[0]; + m->pid = pid; + if((q = strchr(m->name, '.')) != nil) + *q = 0; + return 1; +} + -- cgit v1.2.3