9 #define MAXNUM 10 /* maximum number of numbers on data line */
11 typedef struct Graph Graph;
12 typedef struct Machine Machine;
21 void (*newvalue)(Machine*, uvlong*, uvlong*, int);
22 void (*update)(Graph*, uvlong, uvlong);
47 /* /net/ether0/stats */
69 uvlong devsysstat[10];
70 uvlong prevsysstat[10];
73 uvlong netetherstats[8];
74 uvlong prevetherstats[8];
75 uvlong batterystats[2];
76 uvlong netetherifstats[2];
79 /* big enough to hold /dev/sysstat even with many processors */
95 Ysqueeze = 2, /* vertical squeezing of label text */
96 Labspace = 2, /* room around label */
97 Dot = 2, /* height of dot */
98 Opwid = 5, /* strlen("add ") or strlen("drop ") */
99 Nlab = 3, /* max number of labels on y axis */
100 Lablen = 16, /* max length of label */
101 Lx = 4, /* label tick length */
127 char *menu2str[Nmenu2+1] = {
150 void contextval(Machine*, uvlong*, uvlong*, int),
151 etherval(Machine*, uvlong*, uvlong*, int),
152 ethererrval(Machine*, uvlong*, uvlong*, int),
153 etherinval(Machine*, uvlong*, uvlong*, int),
154 etheroutval(Machine*, uvlong*, uvlong*, int),
155 faultval(Machine*, uvlong*, uvlong*, int),
156 intrval(Machine*, uvlong*, uvlong*, int),
157 inintrval(Machine*, uvlong*, uvlong*, int),
158 loadval(Machine*, uvlong*, uvlong*, int),
159 idleval(Machine*, uvlong*, uvlong*, int),
160 memval(Machine*, uvlong*, uvlong*, int),
161 swapval(Machine*, uvlong*, uvlong*, int),
162 syscallval(Machine*, uvlong*, uvlong*, int),
163 tlbmissval(Machine*, uvlong*, uvlong*, int),
164 tlbpurgeval(Machine*, uvlong*, uvlong*, int),
165 batteryval(Machine*, uvlong*, uvlong*, int),
166 signalval(Machine*, uvlong*, uvlong*, int),
167 tempval(Machine*, uvlong*, uvlong*, int);
169 Menu menu2 = {menu2str, nil};
171 void (*newvaluefn[Nmenu2])(Machine*, uvlong*, uvlong*, int init) = {
192 Image *cols[Ncolor][3];
196 char argchars[] = "8bceEfiImlnpstwz";
198 int parity; /* toggled to avoid patterns in textured background */
200 int ngraph; /* totaly number is ngraph*nmach */
205 int sleeptime = 1000;
207 char *procnames[NPROC] = {"main", "mouse"};
215 for(i=0; i<NPROC; i++)
216 if(pids[i] && pids[i]!=pid)
217 postnote(PNPROC, pids[i], "kill");
227 fprint(2, "stats: out of memory allocating %ld: %r\n", sz);
235 erealloc(void *v, ulong sz)
239 fprint(2, "stats: out of memory reallocating %ld: %r\n", sz);
249 if((t = strdup(s)) == nil) {
250 fprint(2, "stats: out of memory in strdup(%.10s): %r\n", s);
257 mkcol(int i, int c0, int c1, int c2)
259 cols[i][0] = allocimagemix(display, c0, DWhite);
260 cols[i][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c1);
261 cols[i][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c2);
268 mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
270 mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
272 mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
274 mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
276 mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
278 cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
279 cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
280 cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
284 loadbuf(Machine *m, int *fd)
292 n = read(*fd, m->buf, sizeof m->buf-1);
305 label(Point p, int dy, char *text)
316 if(p.y+font->height-Ysqueeze > maxy)
318 w = chartorune(r, s);
320 w = runestringwidth(font, r);
323 runestring(screen, p, display->black, ZP, font, r);
324 p.y += font->height-Ysqueeze;
331 return Pt(x+parity, 0);
335 datapoint(Graph *g, int x, uvlong v, uvlong vmax)
341 y = ((double)v)/(vmax*scale);
344 * Arrange scale to cover a factor of 1000.
345 * vmax corresponds to the 100 mark.
346 * 10*vmax is the top of the scale.
352 /* 1 now corresponds to the top; -2 to the bottom; rescale */
356 p.y = g->r.max.y - Dy(g->r)*y - Dot;
359 if(p.y > g->r.max.y-Dot)
360 p.y = g->r.max.y-Dot;
365 drawdatum(Graph *g, int x, uvlong prev, uvlong v, uvlong vmax)
371 p = datapoint(g, x, v, vmax);
372 q = datapoint(g, x, prev, vmax);
374 draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
375 draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
376 draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
378 draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
379 draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
380 draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
386 redraw(Graph *g, uvlong vmax)
391 draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
392 for(i=1; i<Dx(g->r); i++)
393 drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
394 drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
399 update1(Graph *g, uvlong v, uvlong vmax)
404 if(g->overflow && g->overtmp!=nil)
405 draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
406 draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
407 drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
408 memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
412 overflow = (v>10*vmax*scale);
414 overflow = (v>vmax*scale);
415 if(overflow && g->overtmp!=nil){
417 draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
418 sprint(buf, "%llud", v);
419 string(screen, g->overtmp->r.min, display->black, ZP, font, buf);
423 /* read one line of text from buffer and process integers */
425 readnums(Machine *m, int n, uvlong *a, int spanlines)
433 for(ep=m->bufp; ep<m->ebufp; ep++)
437 for(i=0; i<n && p<ep; i++){
438 while(p<ep && (!isascii(*p) || !isdigit(*p)) && *p!='-')
442 a[i] = strtoull(p, &p, 10);
450 /* Network on fd1, mount driver on fd0 */
457 fprint(2, "stats: can't pipe: %r\n");
461 switch(rfork(RFNOWAIT|RFPROC|RFFDG)) {
463 sysfatal("rfork record module");
470 execl("/bin/aux/fcall", "fcall", nil);
471 fprint(2, "stats: can't exec fcall: %r\n");
484 connect9fs(char *addr)
489 fprint(2, "connect9fs...");
490 na = netmkaddr(addr, 0, "9fs");
492 fprint(2, "dial %s...", na);
493 if((fd = dial(na, 0, dir, 0)) < 0)
496 fprint(2, "dir %s...", dir);
497 // if(strstr(dir, "tcp"))
510 switch(rfork(RFPROC|RFFDG|RFNAMEG)) {
524 fd = open("/sys/log/cpu", OWRITE);
529 execl("/bin/srvold9p", "srvold9p", "-ds", nil);
531 execl("/bin/srvold9p", "srvold9p", "-s", nil);
545 connectexportfs(char *addr)
547 char buf[ERRMAX], dir[256], *na;
553 na = netmkaddr(addr, 0, "exportfs");
554 if((fd = dial(na, 0, dir, 0)) < 0)
557 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client");
562 n = write(fd, tree, strlen(tree));
568 strcpy(buf, "can't read tree");
569 n = read(fd, buf, sizeof buf - 1);
570 if(n!=2 || buf[0]!='O' || buf[1]!='K'){
571 buf[sizeof buf - 1] = '\0';
572 werrstr("bad remote tree: %s\n", buf);
577 // if(strstr(dir, "tcp"))
587 readswap(Machine *m, uvlong *a)
589 if(strstr(m->buf, "memory\n")){
590 /* new /dev/swap - skip first 3 numbers */
591 if(!readnums(m, 7, a, 1))
599 return readnums(m, nelem(m->devswap), a, 0);
619 for(i = 0; j >= 10; i++)
625 initmach(Machine *m, char *name)
629 char *p, mpt[256], buf[256];
631 p = strchr(name, '!');
636 m->name = estrdup(p);
637 m->shortname = shortname(p);
638 m->remote = (strcmp(p, mysysname) != 0);
642 snprint(mpt, sizeof mpt, "/n/%s", p);
643 fd = connectexportfs(name);
645 fprint(2, "can't connect to %s: %r\n", name);
648 /* BUG? need to use amount() now? */
649 if(mount(fd, -1, mpt, MREPL, "") < 0){
650 fprint(2, "stats: mount %s on %s failed (%r); trying /n/sid\n", name, mpt);
651 strcpy(mpt, "/n/sid");
652 if(mount(fd, -1, mpt, MREPL, "") < 0){
653 fprint(2, "stats: mount %s on %s failed: %r\n", name, mpt);
659 snprint(buf, sizeof buf, "%s/dev/swap", mpt);
660 m->swapfd = open(buf, OREAD);
661 if(loadbuf(m, &m->swapfd) && readswap(m, a))
662 memmove(m->devswap, a, sizeof m->devswap);
664 m->devswap[Maxswap] = 100;
665 m->devswap[Maxmem] = 100;
668 snprint(buf, sizeof buf, "%s/dev/sysstat", mpt);
669 m->statsfd = open(buf, OREAD);
670 if(loadbuf(m, &m->statsfd)){
671 for(n=0; readnums(m, nelem(m->devsysstat), a, 0); n++)
676 m->lgproc = ilog10(m->nproc);
678 snprint(buf, sizeof buf, "%s/net/ether0/stats", mpt);
679 m->etherfd = open(buf, OREAD);
680 if(loadbuf(m, &m->etherfd) && readnums(m, nelem(m->netetherstats), a, 1))
681 memmove(m->netetherstats, a, sizeof m->netetherstats);
683 snprint(buf, sizeof buf, "%s/net/ether0/ifstats", mpt);
684 m->ifstatsfd = open(buf, OREAD);
685 if(loadbuf(m, &m->ifstatsfd)){
686 /* need to check that this is a wavelan interface */
687 if(strncmp(m->buf, "Signal: ", 8) == 0 && readnums(m, nelem(m->netetherifstats), a, 1))
688 memmove(m->netetherifstats, a, sizeof m->netetherifstats);
691 snprint(buf, sizeof buf, "%s/mnt/apm/battery", mpt);
692 m->batteryfd = open(buf, OREAD);
694 if(m->batteryfd >= 0){
695 if(loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
696 memmove(m->batterystats, a, sizeof(m->batterystats));
698 snprint(buf, sizeof buf, "%s/dev/battery", mpt);
699 m->bitsybatfd = open(buf, OREAD);
700 if(loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
701 memmove(m->batterystats, a, sizeof(m->batterystats));
703 snprint(buf, sizeof buf, "%s/dev/cputemp", mpt);
704 m->tempfd = open(buf, OREAD);
705 if(loadbuf(m, &m->tempfd))
706 for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
714 alarmed(void *a, char *s)
716 if(strcmp(s, "alarm") == 0)
717 notejmp(a, catchalarm, 1);
724 return init | present[Mmem] | present[Mswap];
731 return init | present[Mcontext] | present[Mfault] | present[Mintr] | present[Mload] | present[Midle] |
732 present[Minintr] | present[Msyscall] | present[Mtlbmiss] | present[Mtlbpurge];
739 return init | present[Mether] | present[Metherin] | present[Metherout] | present[Methererr];
743 needbattery(int init)
745 return init | present[Mbattery];
751 return init | present[Msignal];
757 return init | present[Mtemp];
761 readmach(Machine *m, int init)
764 uvlong a[nelem(m->devsysstat)];
767 if(m->remote && (m->disable || setjmp(catchalarm))){
768 if (m->disable++ >= 5)
769 m->disable = 0; /* give it another chance */
770 memmove(m->devsysstat, m->prevsysstat, sizeof m->devsysstat);
771 memmove(m->netetherstats, m->prevetherstats, sizeof m->netetherstats);
774 snprint(buf, sizeof buf, "%s", m->name);
775 if (strcmp(m->name, buf) != 0){
777 m->name = estrdup(buf);
779 m->shortname = shortname(buf);
780 if(display != nil) /* else we're still initializing */
787 if(needswap(init) && loadbuf(m, &m->swapfd) && readswap(m, a))
788 memmove(m->devswap, a, sizeof m->devswap);
789 if(needstat(init) && loadbuf(m, &m->statsfd)){
790 memmove(m->prevsysstat, m->devsysstat, sizeof m->devsysstat);
791 memset(m->devsysstat, 0, sizeof m->devsysstat);
792 for(n=0; n<m->nproc && readnums(m, nelem(m->devsysstat), a, 0); n++)
793 for(i=0; i<nelem(m->devsysstat); i++)
794 m->devsysstat[i] += a[i];
796 if(needether(init) && loadbuf(m, &m->etherfd) && readnums(m, nelem(m->netetherstats), a, 1)){
797 memmove(m->prevetherstats, m->netetherstats, sizeof m->netetherstats);
798 memmove(m->netetherstats, a, sizeof m->netetherstats);
800 if(needsignal(init) && loadbuf(m, &m->ifstatsfd) && strncmp(m->buf, "Signal: ", 8)==0 && readnums(m, nelem(m->netetherifstats), a, 1)){
801 memmove(m->netetherifstats, a, sizeof m->netetherifstats);
803 if(needbattery(init) && loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
804 memmove(m->batterystats, a, sizeof(m->batterystats));
805 if(needbattery(init) && loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
806 memmove(m->batterystats, a, sizeof(m->batterystats));
807 if(needtemp(init) && loadbuf(m, &m->tempfd))
808 for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
817 memval(Machine *m, uvlong *v, uvlong *vmax, int)
819 *v = m->devswap[Mem];
820 *vmax = m->devswap[Maxmem];
824 swapval(Machine *m, uvlong *v, uvlong *vmax, int)
826 *v = m->devswap[Swap];
827 *vmax = m->devswap[Maxswap];
831 contextval(Machine *m, uvlong *v, uvlong *vmax, int init)
833 *v = m->devsysstat[Context]-m->prevsysstat[Context];
834 *vmax = sleeptime*m->nproc;
840 * bug: need to factor in HZ
843 intrval(Machine *m, uvlong *v, uvlong *vmax, int init)
845 *v = m->devsysstat[Interrupt]-m->prevsysstat[Interrupt];
846 *vmax = sleeptime*m->nproc*10;
848 *vmax = sleeptime*10;
852 syscallval(Machine *m, uvlong *v, uvlong *vmax, int init)
854 *v = m->devsysstat[Syscall]-m->prevsysstat[Syscall];
855 *vmax = sleeptime*m->nproc;
861 faultval(Machine *m, uvlong *v, uvlong *vmax, int init)
863 *v = m->devsysstat[Fault]-m->prevsysstat[Fault];
864 *vmax = sleeptime*m->nproc;
870 tlbmissval(Machine *m, uvlong *v, uvlong *vmax, int init)
872 *v = m->devsysstat[TLBfault]-m->prevsysstat[TLBfault];
873 *vmax = (sleeptime/1000)*10*m->nproc;
875 *vmax = (sleeptime/1000)*10;
879 tlbpurgeval(Machine *m, uvlong *v, uvlong *vmax, int init)
881 *v = m->devsysstat[TLBpurge]-m->prevsysstat[TLBpurge];
882 *vmax = (sleeptime/1000)*10*m->nproc;
884 *vmax = (sleeptime/1000)*10;
888 loadval(Machine *m, uvlong *v, uvlong *vmax, int init)
890 *v = m->devsysstat[Load];
891 *vmax = 1000*m->nproc;
897 idleval(Machine *m, uvlong *v, uvlong *vmax, int)
899 *v = m->devsysstat[Idle]/m->nproc;
904 inintrval(Machine *m, uvlong *v, uvlong *vmax, int)
906 *v = m->devsysstat[InIntr]/m->nproc;
911 etherval(Machine *m, uvlong *v, uvlong *vmax, int init)
913 *v = m->netetherstats[In]-m->prevetherstats[In] + m->netetherstats[Out]-m->prevetherstats[Out];
914 *vmax = sleeptime*m->nproc;
920 etherinval(Machine *m, uvlong *v, uvlong *vmax, int init)
922 *v = m->netetherstats[In]-m->prevetherstats[In];
923 *vmax = sleeptime*m->nproc;
929 etheroutval(Machine *m, uvlong *v, uvlong *vmax, int init)
931 *v = m->netetherstats[Out]-m->prevetherstats[Out];
932 *vmax = sleeptime*m->nproc;
938 ethererrval(Machine *m, uvlong *v, uvlong *vmax, int init)
943 for(i=Err0; i<nelem(m->netetherstats); i++)
944 *v += m->netetherstats[i];
945 *vmax = (sleeptime/1000)*10*m->nproc;
947 *vmax = (sleeptime/1000)*10;
951 batteryval(Machine *m, uvlong *v, uvlong *vmax, int)
953 *v = m->batterystats[0];
954 if(m->bitsybatfd >= 0)
955 *vmax = 184; // at least on my bitsy...
961 signalval(Machine *m, uvlong *v, uvlong *vmax, int)
966 l = m->netetherifstats[0];
968 * Range is seen to be from about -45 (strong) to -95 (weak); rescale
970 if(l == 0){ /* probably not present */
978 tempval(Machine *m, uvlong *v, uvlong *vmax, int)
984 if(l == ~0 || l == 0)
993 fprint(2, "usage: stats [-O] [-S scale] [-LY] [-%s] [machine...]\n", argchars);
1004 if(n > nelem(menu2str))
1006 /* avoid two adjacent graphs of same color */
1007 if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
1010 graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
1011 for(i=0; i<nmach; i++)
1012 for(j=0; j<ngraph; j++)
1013 graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
1016 for(i=0; i<nmach; i++){
1017 g = &graph[i*ngraph+(ngraph-1)];
1018 memset(g, 0, sizeof(Graph));
1019 g->label = menu2str[n]+Opwid;
1020 g->newvalue = newvaluefn[n];
1021 g->update = update1; /* no other update functions yet */
1023 g->colindex = nadd%Ncolor;
1030 dropgraph(int which)
1035 if(which > nelem(menu2str))
1037 /* convert n to index in graph table */
1039 for(i=0; i<ngraph; i++)
1040 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
1045 fprint(2, "stats: internal error can't drop graph\n");
1049 graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
1050 for(i=0; i<nmach; i++){
1052 graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
1053 free(ograph[i*ngraph+j].data);
1054 freeimage(ograph[i*ngraph+j].overtmp);
1055 for(j++; j<ngraph; j++)
1056 graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
1064 addmachine(char *name)
1067 fprint(2, "stats: internal error: ngraph>0 in addmachine()\n");
1071 nmach = 0; /* a little dance to get us started with local machine by default */
1072 mach = erealloc(mach, (nmach+1)*sizeof(Machine));
1073 memset(mach+nmach, 0, sizeof(Machine));
1074 if (initmach(mach+nmach, name)){
1082 labelstrs(Graph *g, char strs[Nlab][Lablen], int *np)
1087 g->newvalue(g->mach, &v, &vmax, 1);
1090 sprint(strs[j-1], "%g", scale*pow(10., j)*(double)vmax/100.);
1094 sprint(strs[j-1], "%g", scale*(double)j*(double)vmax/4.0);
1102 int i, j, n, w, maxw;
1103 char strs[Nlab][Lablen];
1106 for(i=0; i<ngraph; i++){
1107 /* choose value for rightmost graph */
1108 labelstrs(&graph[ngraph*(nmach-1)+i], strs, &n);
1110 w = stringwidth(font, strs[j]);
1121 int i, j, k, n, startx, starty, x, y, dx, dy, ly, ondata, maxx, wid, nlab;
1125 char buf[128], labs[Nlab][Lablen];
1127 draw(screen, screen->r, display->white, nil, ZP);
1129 /* label left edge */
1130 x = screen->r.min.x;
1131 y = screen->r.min.y + Labspace+font->height+Labspace;
1132 dy = (screen->r.max.y - y)/ngraph;
1133 dx = Labspace+stringwidth(font, "0")+Labspace;
1136 for(i=0; i<ngraph; i++,y+=dy){
1137 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
1138 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
1139 label(Pt(x, y), dy, graph[i].label);
1140 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
1143 /* label top edge */
1144 dx = (screen->r.max.x - startx)/nmach;
1145 for(x=startx, i=0; i<nmach; i++,x+=dx){
1146 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
1147 j = dx/stringwidth(font, "0");
1149 if(n>1 && j>=1+3+mach[i].lgproc){ /* first char of name + (n) */
1150 j -= 3+mach[i].lgproc;
1153 snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].shortname, n);
1155 snprint(buf, sizeof buf, "%.*s", j, mach[i].shortname);
1156 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP, font, buf);
1159 maxx = screen->r.max.x;
1161 /* label right, if requested */
1162 if(ylabels && dy>Nlab*(font->height+1)){
1164 if(wid < (maxx-startx)-30){
1165 /* else there's not enough room */
1167 draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), display->black, nil, ZP);
1169 for(j=0; j<ngraph; j++, y+=dy){
1170 /* choose value for rightmost graph */
1171 g = &graph[ngraph*(nmach-1)+j];
1172 labelstrs(g, labs, &nlab);
1173 r = Rect(maxx+1, y, screen->r.max.x, y+dy-1);
1175 r.max.y = screen->r.max.y;
1176 draw(screen, r, cols[g->colindex][0], nil, paritypt(r.min.x));
1177 for(k=0; k<nlab; k++){
1178 ly = y + (dy*(nlab-k)/(nlab+1));
1179 draw(screen, Rect(maxx+1, ly, maxx+1+Lx, ly+1), display->black, nil, ZP);
1180 ly -= font->height/2;
1181 string(screen, Pt(maxx+1+Lx, ly), display->black, ZP, font, labs[k]);
1188 for(i=0; i<nmach; i++){
1189 machr = Rect(startx+i*dx, starty, maxx, screen->r.max.y);
1191 machr.max.x = startx+(i+1)*dx - 1;
1193 for(j=0; j<ngraph; j++, y+=dy){
1194 g = &graph[i*ngraph+j];
1197 g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
1198 g->data = erealloc(g->data, g->ndata*sizeof(ulong));
1199 if(g->ndata > ondata)
1200 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(ulong));
1204 g->r.max.y = y+dy - 1;
1206 g->r.max.y = screen->r.max.y;
1207 draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
1210 r.max.y = r.min.y+font->height;
1211 r.max.x = r.min.x+stringwidth(font, "999999999999");
1212 freeimage(g->overtmp);
1214 if(r.max.x <= g->r.max.x)
1215 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
1216 g->newvalue(g->mach, &v, &vmax, 0);
1221 flushimage(display, 1);
1227 lockdisplay(display);
1228 if(new && getwindow(display, Refnone) < 0) {
1229 fprint(2, "stats: can't reattach to window\n");
1230 killall("reattach");
1233 unlockdisplay(display);
1244 if(mouse.buttons == 4){
1245 lockdisplay(display);
1246 for(i=0; i<Nmenu2; i++)
1248 memmove(menu2str[i], "drop ", Opwid);
1250 memmove(menu2str[i], "add ", Opwid);
1251 i = emenuhit(3, &mouse, &menu2);
1259 unlockdisplay(display);
1265 startproc(void (*f)(void), int index)
1269 switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
1271 fprint(2, "stats: fork failed: %r\n");
1272 killall("fork failed");
1275 fprint(2, "stats: %s process exits\n", procnames[index]);
1277 killall("process died");
1285 main(int argc, char *argv[])
1289 uvlong v, vmax, nargs;
1293 mysysname = getenv("sysname");
1294 if(mysysname == nil){
1295 fprint(2, "stats: can't find $sysname: %r\n");
1302 secs = atof(EARGF(usage()));
1304 sleeptime = 1000*secs;
1307 scale = atof(EARGF(usage()));
1321 if(nargs>=sizeof args || strchr(argchars, ARGC())==nil)
1323 args[nargs++] = ARGC();
1327 mach = emalloc(nmach*sizeof(Machine));
1328 initmach(&mach[0], mysysname);
1329 readmach(&mach[0], 1);
1331 for(i=j=0; i<argc; i++){
1332 if (addmachine(argv[i]))
1333 readmach(&mach[j++], 1);
1339 for(i=0; i<nargs; i++)
1342 fprint(2, "stats: internal error: unknown arg %c\n", args[i]);
1355 addgraph(Metherout);
1376 addgraph(Metherout);
1377 addgraph(Methererr);
1380 addgraph(Mtlbpurge);
1387 addgraph(Mtlbpurge);
1403 for(i=0; i<nmach; i++)
1404 for(j=0; j<ngraph; j++)
1405 graph[i*ngraph+j].mach = &mach[i];
1407 if(initdraw(nil, nil, "stats") < 0){
1408 fprint(2, "stats: initdraw failed: %r\n");
1414 startproc(mouseproc, Mouseproc);
1415 pids[Mainproc] = getpid();
1416 display->locking = 1; /* tell library we're using the display lock */
1420 unlockdisplay(display); /* display is still locked from initdraw() */
1422 for(i=0; i<nmach; i++)
1423 readmach(&mach[i], 0);
1424 lockdisplay(display);
1426 for(i=0; i<nmach*ngraph; i++){
1427 graph[i].newvalue(graph[i].mach, &v, &vmax, 0);
1428 graph[i].update(&graph[i], v, vmax);
1430 flushimage(display, 1);
1431 unlockdisplay(display);