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");
561 n = write(fd, tree, strlen(tree));
567 strcpy(buf, "can't read tree");
568 n = read(fd, buf, sizeof buf - 1);
569 if(n!=2 || buf[0]!='O' || buf[1]!='K'){
570 buf[sizeof buf - 1] = '\0';
571 werrstr("bad remote tree: %s\n", buf);
576 // if(strstr(dir, "tcp"))
586 readswap(Machine *m, uvlong *a)
588 if(strstr(m->buf, "memory\n")){
589 /* new /dev/swap - skip first 3 numbers */
590 if(!readnums(m, 7, a, 1))
598 return readnums(m, nelem(m->devswap), a, 0);
618 for(i = 0; j >= 10; i++)
624 initmach(Machine *m, char *name)
628 char *p, mpt[256], buf[256];
630 p = strchr(name, '!');
635 m->name = estrdup(p);
636 m->shortname = shortname(p);
637 m->remote = (strcmp(p, mysysname) != 0);
641 snprint(mpt, sizeof mpt, "/n/%s", p);
642 fd = connectexportfs(name);
644 fprint(2, "can't connect to %s: %r\n", name);
647 /* BUG? need to use amount() now? */
648 if(mount(fd, -1, mpt, MREPL, "") < 0){
649 fprint(2, "stats: mount %s on %s failed (%r); trying /n/sid\n", name, mpt);
650 strcpy(mpt, "/n/sid");
651 if(mount(fd, -1, mpt, MREPL, "") < 0){
652 fprint(2, "stats: mount %s on %s failed: %r\n", name, mpt);
658 snprint(buf, sizeof buf, "%s/dev/swap", mpt);
659 m->swapfd = open(buf, OREAD);
660 if(loadbuf(m, &m->swapfd) && readswap(m, a))
661 memmove(m->devswap, a, sizeof m->devswap);
663 m->devswap[Maxswap] = 100;
664 m->devswap[Maxmem] = 100;
667 snprint(buf, sizeof buf, "%s/dev/sysstat", mpt);
668 m->statsfd = open(buf, OREAD);
669 if(loadbuf(m, &m->statsfd)){
670 for(n=0; readnums(m, nelem(m->devsysstat), a, 0); n++)
675 m->lgproc = ilog10(m->nproc);
677 snprint(buf, sizeof buf, "%s/net/ether0/stats", mpt);
678 m->etherfd = open(buf, OREAD);
679 if(loadbuf(m, &m->etherfd) && readnums(m, nelem(m->netetherstats), a, 1))
680 memmove(m->netetherstats, a, sizeof m->netetherstats);
682 snprint(buf, sizeof buf, "%s/net/ether0/ifstats", mpt);
683 m->ifstatsfd = open(buf, OREAD);
684 if(loadbuf(m, &m->ifstatsfd)){
685 /* need to check that this is a wavelan interface */
686 if(strncmp(m->buf, "Signal: ", 8) == 0 && readnums(m, nelem(m->netetherifstats), a, 1))
687 memmove(m->netetherifstats, a, sizeof m->netetherifstats);
690 snprint(buf, sizeof buf, "%s/mnt/apm/battery", mpt);
691 m->batteryfd = open(buf, OREAD);
693 if(m->batteryfd >= 0){
694 if(loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
695 memmove(m->batterystats, a, sizeof(m->batterystats));
697 snprint(buf, sizeof buf, "%s/dev/battery", mpt);
698 m->bitsybatfd = open(buf, OREAD);
699 if(loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
700 memmove(m->batterystats, a, sizeof(m->batterystats));
702 snprint(buf, sizeof buf, "%s/dev/cputemp", mpt);
703 m->tempfd = open(buf, OREAD);
704 if(loadbuf(m, &m->tempfd))
705 for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
713 alarmed(void *a, char *s)
715 if(strcmp(s, "alarm") == 0)
716 notejmp(a, catchalarm, 1);
723 return init | present[Mmem] | present[Mswap];
730 return init | present[Mcontext] | present[Mfault] | present[Mintr] | present[Mload] | present[Midle] |
731 present[Minintr] | present[Msyscall] | present[Mtlbmiss] | present[Mtlbpurge];
738 return init | present[Mether] | present[Metherin] | present[Metherout] | present[Methererr];
742 needbattery(int init)
744 return init | present[Mbattery];
750 return init | present[Msignal];
756 return init | present[Mtemp];
760 readmach(Machine *m, int init)
763 uvlong a[nelem(m->devsysstat)];
766 if(m->remote && (m->disable || setjmp(catchalarm))){
767 if (m->disable++ >= 5)
768 m->disable = 0; /* give it another chance */
769 memmove(m->devsysstat, m->prevsysstat, sizeof m->devsysstat);
770 memmove(m->netetherstats, m->prevetherstats, sizeof m->netetherstats);
773 snprint(buf, sizeof buf, "%s", m->name);
774 if (strcmp(m->name, buf) != 0){
776 m->name = estrdup(buf);
778 m->shortname = shortname(buf);
779 if(display != nil) /* else we're still initializing */
786 if(needswap(init) && loadbuf(m, &m->swapfd) && readswap(m, a))
787 memmove(m->devswap, a, sizeof m->devswap);
788 if(needstat(init) && loadbuf(m, &m->statsfd)){
789 memmove(m->prevsysstat, m->devsysstat, sizeof m->devsysstat);
790 memset(m->devsysstat, 0, sizeof m->devsysstat);
791 for(n=0; n<m->nproc && readnums(m, nelem(m->devsysstat), a, 0); n++)
792 for(i=0; i<nelem(m->devsysstat); i++)
793 m->devsysstat[i] += a[i];
795 if(needether(init) && loadbuf(m, &m->etherfd) && readnums(m, nelem(m->netetherstats), a, 1)){
796 memmove(m->prevetherstats, m->netetherstats, sizeof m->netetherstats);
797 memmove(m->netetherstats, a, sizeof m->netetherstats);
799 if(needsignal(init) && loadbuf(m, &m->ifstatsfd) && strncmp(m->buf, "Signal: ", 8)==0 && readnums(m, nelem(m->netetherifstats), a, 1)){
800 memmove(m->netetherifstats, a, sizeof m->netetherifstats);
802 if(needbattery(init) && loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
803 memmove(m->batterystats, a, sizeof(m->batterystats));
804 if(needbattery(init) && loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
805 memmove(m->batterystats, a, sizeof(m->batterystats));
806 if(needtemp(init) && loadbuf(m, &m->tempfd))
807 for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
816 memval(Machine *m, uvlong *v, uvlong *vmax, int)
818 *v = m->devswap[Mem];
819 *vmax = m->devswap[Maxmem];
823 swapval(Machine *m, uvlong *v, uvlong *vmax, int)
825 *v = m->devswap[Swap];
826 *vmax = m->devswap[Maxswap];
830 contextval(Machine *m, uvlong *v, uvlong *vmax, int init)
832 *v = m->devsysstat[Context]-m->prevsysstat[Context];
833 *vmax = sleeptime*m->nproc;
839 * bug: need to factor in HZ
842 intrval(Machine *m, uvlong *v, uvlong *vmax, int init)
844 *v = m->devsysstat[Interrupt]-m->prevsysstat[Interrupt];
845 *vmax = sleeptime*m->nproc*10;
847 *vmax = sleeptime*10;
851 syscallval(Machine *m, uvlong *v, uvlong *vmax, int init)
853 *v = m->devsysstat[Syscall]-m->prevsysstat[Syscall];
854 *vmax = sleeptime*m->nproc;
860 faultval(Machine *m, uvlong *v, uvlong *vmax, int init)
862 *v = m->devsysstat[Fault]-m->prevsysstat[Fault];
863 *vmax = sleeptime*m->nproc;
869 tlbmissval(Machine *m, uvlong *v, uvlong *vmax, int init)
871 *v = m->devsysstat[TLBfault]-m->prevsysstat[TLBfault];
872 *vmax = (sleeptime/1000)*10*m->nproc;
874 *vmax = (sleeptime/1000)*10;
878 tlbpurgeval(Machine *m, uvlong *v, uvlong *vmax, int init)
880 *v = m->devsysstat[TLBpurge]-m->prevsysstat[TLBpurge];
881 *vmax = (sleeptime/1000)*10*m->nproc;
883 *vmax = (sleeptime/1000)*10;
887 loadval(Machine *m, uvlong *v, uvlong *vmax, int init)
889 *v = m->devsysstat[Load];
890 *vmax = 1000*m->nproc;
896 idleval(Machine *m, uvlong *v, uvlong *vmax, int)
898 *v = m->devsysstat[Idle]/m->nproc;
903 inintrval(Machine *m, uvlong *v, uvlong *vmax, int)
905 *v = m->devsysstat[InIntr]/m->nproc;
910 etherval(Machine *m, uvlong *v, uvlong *vmax, int init)
912 *v = m->netetherstats[In]-m->prevetherstats[In] + m->netetherstats[Out]-m->prevetherstats[Out];
913 *vmax = sleeptime*m->nproc;
919 etherinval(Machine *m, uvlong *v, uvlong *vmax, int init)
921 *v = m->netetherstats[In]-m->prevetherstats[In];
922 *vmax = sleeptime*m->nproc;
928 etheroutval(Machine *m, uvlong *v, uvlong *vmax, int init)
930 *v = m->netetherstats[Out]-m->prevetherstats[Out];
931 *vmax = sleeptime*m->nproc;
937 ethererrval(Machine *m, uvlong *v, uvlong *vmax, int init)
942 for(i=Err0; i<nelem(m->netetherstats); i++)
943 *v += m->netetherstats[i];
944 *vmax = (sleeptime/1000)*10*m->nproc;
946 *vmax = (sleeptime/1000)*10;
950 batteryval(Machine *m, uvlong *v, uvlong *vmax, int)
952 *v = m->batterystats[0];
953 if(m->bitsybatfd >= 0)
954 *vmax = 184; // at least on my bitsy...
960 signalval(Machine *m, uvlong *v, uvlong *vmax, int)
965 l = m->netetherifstats[0];
967 * Range is seen to be from about -45 (strong) to -95 (weak); rescale
969 if(l == 0){ /* probably not present */
977 tempval(Machine *m, uvlong *v, uvlong *vmax, int)
983 if(l == ~0 || l == 0)
992 fprint(2, "usage: stats [-O] [-S scale] [-LY] [-%s] [machine...]\n", argchars);
1003 if(n > nelem(menu2str))
1005 /* avoid two adjacent graphs of same color */
1006 if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
1009 graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
1010 for(i=0; i<nmach; i++)
1011 for(j=0; j<ngraph; j++)
1012 graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
1015 for(i=0; i<nmach; i++){
1016 g = &graph[i*ngraph+(ngraph-1)];
1017 memset(g, 0, sizeof(Graph));
1018 g->label = menu2str[n]+Opwid;
1019 g->newvalue = newvaluefn[n];
1020 g->update = update1; /* no other update functions yet */
1022 g->colindex = nadd%Ncolor;
1029 dropgraph(int which)
1034 if(which > nelem(menu2str))
1036 /* convert n to index in graph table */
1038 for(i=0; i<ngraph; i++)
1039 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
1044 fprint(2, "stats: internal error can't drop graph\n");
1048 graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
1049 for(i=0; i<nmach; i++){
1051 graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
1052 free(ograph[i*ngraph+j].data);
1053 freeimage(ograph[i*ngraph+j].overtmp);
1054 for(j++; j<ngraph; j++)
1055 graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
1063 addmachine(char *name)
1066 fprint(2, "stats: internal error: ngraph>0 in addmachine()\n");
1070 nmach = 0; /* a little dance to get us started with local machine by default */
1071 mach = erealloc(mach, (nmach+1)*sizeof(Machine));
1072 memset(mach+nmach, 0, sizeof(Machine));
1073 if (initmach(mach+nmach, name)){
1081 labelstrs(Graph *g, char strs[Nlab][Lablen], int *np)
1086 g->newvalue(g->mach, &v, &vmax, 1);
1089 sprint(strs[j-1], "%g", scale*pow(10., j)*(double)vmax/100.);
1093 sprint(strs[j-1], "%g", scale*(double)j*(double)vmax/4.0);
1101 int i, j, n, w, maxw;
1102 char strs[Nlab][Lablen];
1105 for(i=0; i<ngraph; i++){
1106 /* choose value for rightmost graph */
1107 labelstrs(&graph[ngraph*(nmach-1)+i], strs, &n);
1109 w = stringwidth(font, strs[j]);
1120 int i, j, k, n, startx, starty, x, y, dx, dy, ly, ondata, maxx, wid, nlab;
1124 char buf[128], labs[Nlab][Lablen];
1126 draw(screen, screen->r, display->white, nil, ZP);
1128 /* label left edge */
1129 x = screen->r.min.x;
1130 y = screen->r.min.y + Labspace+font->height+Labspace;
1131 dy = (screen->r.max.y - y)/ngraph;
1132 dx = Labspace+stringwidth(font, "0")+Labspace;
1135 for(i=0; i<ngraph; i++,y+=dy){
1136 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
1137 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
1138 label(Pt(x, y), dy, graph[i].label);
1139 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
1142 /* label top edge */
1143 dx = (screen->r.max.x - startx)/nmach;
1144 for(x=startx, i=0; i<nmach; i++,x+=dx){
1145 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
1146 j = dx/stringwidth(font, "0");
1148 if(n>1 && j>=1+3+mach[i].lgproc){ /* first char of name + (n) */
1149 j -= 3+mach[i].lgproc;
1152 snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].shortname, n);
1154 snprint(buf, sizeof buf, "%.*s", j, mach[i].shortname);
1155 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP, font, buf);
1158 maxx = screen->r.max.x;
1160 /* label right, if requested */
1161 if(ylabels && dy>Nlab*(font->height+1)){
1163 if(wid < (maxx-startx)-30){
1164 /* else there's not enough room */
1166 draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), display->black, nil, ZP);
1168 for(j=0; j<ngraph; j++, y+=dy){
1169 /* choose value for rightmost graph */
1170 g = &graph[ngraph*(nmach-1)+j];
1171 labelstrs(g, labs, &nlab);
1172 r = Rect(maxx+1, y, screen->r.max.x, y+dy-1);
1174 r.max.y = screen->r.max.y;
1175 draw(screen, r, cols[g->colindex][0], nil, paritypt(r.min.x));
1176 for(k=0; k<nlab; k++){
1177 ly = y + (dy*(nlab-k)/(nlab+1));
1178 draw(screen, Rect(maxx+1, ly, maxx+1+Lx, ly+1), display->black, nil, ZP);
1179 ly -= font->height/2;
1180 string(screen, Pt(maxx+1+Lx, ly), display->black, ZP, font, labs[k]);
1187 for(i=0; i<nmach; i++){
1188 machr = Rect(startx+i*dx, starty, maxx, screen->r.max.y);
1190 machr.max.x = startx+(i+1)*dx - 1;
1192 for(j=0; j<ngraph; j++, y+=dy){
1193 g = &graph[i*ngraph+j];
1196 g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
1197 g->data = erealloc(g->data, g->ndata*sizeof(ulong));
1198 if(g->ndata > ondata)
1199 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(ulong));
1203 g->r.max.y = y+dy - 1;
1205 g->r.max.y = screen->r.max.y;
1206 draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
1209 r.max.y = r.min.y+font->height;
1210 r.max.x = r.min.x+stringwidth(font, "999999999999");
1211 freeimage(g->overtmp);
1213 if(r.max.x <= g->r.max.x)
1214 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
1215 g->newvalue(g->mach, &v, &vmax, 0);
1220 flushimage(display, 1);
1226 lockdisplay(display);
1227 if(new && getwindow(display, Refnone) < 0) {
1228 fprint(2, "stats: can't reattach to window\n");
1229 killall("reattach");
1232 unlockdisplay(display);
1243 if(mouse.buttons == 4){
1244 lockdisplay(display);
1245 for(i=0; i<Nmenu2; i++)
1247 memmove(menu2str[i], "drop ", Opwid);
1249 memmove(menu2str[i], "add ", Opwid);
1250 i = emenuhit(3, &mouse, &menu2);
1258 unlockdisplay(display);
1264 startproc(void (*f)(void), int index)
1268 switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
1270 fprint(2, "stats: fork failed: %r\n");
1271 killall("fork failed");
1274 fprint(2, "stats: %s process exits\n", procnames[index]);
1276 killall("process died");
1284 main(int argc, char *argv[])
1288 uvlong v, vmax, nargs;
1292 mysysname = getenv("sysname");
1293 if(mysysname == nil){
1294 fprint(2, "stats: can't find $sysname: %r\n");
1297 mysysname = estrdup(mysysname);
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);