11 #define MAXNUM 8 /* maximum number of numbers on data line */
13 typedef struct Graph Graph;
14 typedef struct Machine Machine;
15 typedef struct Req Req;
28 void (*newvalue)(Machine*, long*, long*, long*);
29 void (*update)(Graph*, long, long, long);
48 int seq; /* sequence number */
49 vlong time; /* time sent */
83 Ysqueeze = 2, /* vertical squeezing of label text */
84 Labspace = 2, /* room around label */
85 Dot = 2, /* height of dot */
86 Opwid = 5, /* strlen("add ") or strlen("drop ") */
98 char *menu2str[Nmenu2+1] = {
105 void rttval(Machine*, long*, long*, long*);
106 void lostval(Machine*, long*, long*, long*);
108 Menu menu2 = {menu2str, nil};
110 void (*newvaluefn[Nmenu2])(Machine*, long*, long*, long*) = {
115 Image *cols[Ncolor][3];
120 int parity; /* toggled to avoid patterns in textured background */
122 int ngraph; /* totaly number is ngraph*nmach */
128 void startproc(void (*)(void*), void*);
131 int which2index(int);
132 int index2which(int);
140 for(i=0; i<NPROC; i++)
141 if(pids[i] && pids[i]!=pid)
142 postnote(PNPROC, pids[i], "kill");
152 fprint(2, "%s: out of memory allocating %ld: %r\n", argv0, sz);
160 erealloc(void *v, ulong sz)
164 fprint(2, "%s: out of memory reallocating %ld: %r\n", argv0, sz);
174 if((t = strdup(s)) == nil) {
175 fprint(2, "%s: out of memory in strdup(%.10s): %r\n", argv0, s);
182 mkcol(int i, int c0, int c1, int c2)
184 cols[i][0] = allocimagemix(display, c0, DWhite);
185 cols[i][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c1);
186 cols[i][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c2);
193 mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
195 mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
197 mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
199 mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
201 mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
203 cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
204 cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
205 cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
209 label(Point p, int dy, char *text)
220 if(p.y+font->height-Ysqueeze > maxy)
222 w = chartorune(r, s);
224 w = runestringwidth(font, r);
227 runestring(screen, p, display->black, ZP, font, r);
228 p.y += font->height-Ysqueeze;
233 hashmark(Point p, int dy, long v, long vmax, char *label)
239 y = p.y + (dy*(vmax-v))/vmax;
240 draw(screen, Rect(p.x, y-1, p.x+Labspace, y+1), display->black, nil, ZP);
241 if(dy > 5*font->height)
242 string(screen, Pt(x, y-font->height/2),
243 display->black, ZP, font, label);
247 hashmarks(Point p, int dy, int which)
249 switch(index2which(which)){
251 hashmark(p, dy, rttscale(1000000), Rttmax, "1.");
252 hashmark(p, dy, rttscale(100000), Rttmax, "0.1");
253 hashmark(p, dy, rttscale(10000), Rttmax, "0.01");
254 hashmark(p, dy, rttscale(1000), Rttmax, "0.001");
257 hashmark(p, dy, 75, 100, " 75%");
258 hashmark(p, dy, 50, 100, " 50%");
259 hashmark(p, dy, 25, 100, " 25%");
267 return Pt(x+parity, 0);
271 datapoint(Graph *g, int x, long v, long vmax)
276 p.y = g->r.max.y - Dy(g->r)*v/vmax - Dot;
279 if(p.y > g->r.max.y-Dot)
280 p.y = g->r.max.y-Dot;
285 drawdatum(Graph *g, int x, long prev, long v, long vmax)
291 p = datapoint(g, x, v, vmax);
292 q = datapoint(g, x, prev, vmax);
294 draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
295 draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
296 draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
298 draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
299 draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
300 draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
306 drawmark(Graph *g, int x)
310 c = (g->colindex+1)&Ncolor;
311 draw(screen, Rect(x, g->r.min.y, x+1, g->r.max.y), cols[c][2], nil, ZP);
315 redraw(Graph *g, int vmax)
320 draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
321 for(i=1; i<Dx(g->r); i++)
322 drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
323 drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
329 if(g->overtmp != nil)
330 draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
335 drawmsg(Graph *g, char *msg)
337 if(g->overtmp == nil)
340 /* save previous contents of screen */
341 draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
344 if(strlen(msg) > g->overtmplen)
345 msg[g->overtmplen] = 0;
346 string(screen, g->overtmp->r.min, display->black, ZP, font, msg);
350 clearcursor(Graph *g)
355 if(g->overtmp == nil)
358 if(g->cursor > 0 && g->cursor < g->ndata){
359 x = g->r.max.x - g->cursor;
362 prev = g->data[g->cursor-1];
363 drawdatum(g, x, prev, g->data[g->cursor], g->vmax);
369 drawcursor(Graph *g, int x)
371 if(g->overtmp == nil)
374 draw(screen, Rect(x, g->r.min.y, x+1, g->r.max.y), cols[g->colindex][2], nil, ZP);
378 update1(Graph *g, long v, long vmax, long mark)
382 /* put back screen value sans message */
383 if(g->overflow || *g->msg){
388 draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
389 drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
391 drawmark(g, g->r.max.x-1);
392 memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
396 sprint(buf, "%ld", v);
403 if(g->cursor >= g->ndata){
415 pinglost(Machine *m, Req*)
421 pingreply(Machine *m, Req *r)
433 pingclean(Machine *m, ushort seq, vlong now, int)
438 y = 10LL*1000000000LL;
439 for(l = &m->first; *l; ){
442 if(x > y || r->seq == seq){
460 char buf[128], err[ERRMAX];
464 ip = (Icmphdr *)(buf + IPV4HDR_LEN);
465 memset(buf, 0, sizeof buf);
466 r = malloc(sizeof *r);
470 for(i = 32; i < MSGLEN; i++)
472 ip->type = EchoRequest;
475 ip->seq[1] = m->seq>>8;
479 pingclean(m, -1, nsec(), 0);
487 if(write(m->pingfd, buf, MSGLEN) < MSGLEN){
488 errstr(err, sizeof err);
489 if(strstr(err, "unreach")||strstr(err, "exceed"))
507 ip = (Icmphdr *)(buf + IPV4HDR_LEN);
509 n = read(m->pingfd, buf, sizeof(buf));
514 for(i = 32; i < MSGLEN; i++)
515 if(buf[i] != (i&0xff))
519 x = (ip->seq[1]<<8) | ip->seq[0];
520 if(ip->type != EchoReply || ip->code != 0)
523 pingclean(m, x, nsec(), ip4->ttl);
529 initmach(Machine *m, char *name)
535 p = strchr(name, '!');
538 m->name = estrdup(p+1);
542 m->name = estrdup(p);
544 m->pingfd = dial(netmkaddr(m->name, "icmp", "1"), nil, nil, &cfd);
546 sysfatal("dialing %s: %r", m->name);
547 write(cfd, "ignoreadvice", 12);
549 startproc(pingrcv, m);
557 x = 10.0*log10(x) - 20.0;
570 return pow(10.0, dx/10.0);
574 rttval(Machine *m, long *v, long *vmax, long *mark)
581 x = m->rttsum/m->rttmsgs;
582 m->rttsum = m->rttmsgs = 0;
592 lostval(Machine *m, long *v, long *vmax, long *mark)
596 if(m->rcvdmsgs+m->lostmsgs > 0)
597 x = (m->lostavg>>1) + (((m->lostmsgs*100)/(m->lostmsgs + m->rcvdmsgs))>>1);
601 m->lostmsgs = m->rcvdmsgs = 0;
616 fprint(2, "usage: %s machine [machine...]\n", argv0);
627 if(n > nelem(menu2str))
629 /* avoid two adjacent graphs of same color */
630 if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
633 graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
634 for(i=0; i<nmach; i++)
635 for(j=0; j<ngraph; j++)
636 graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
639 for(i=0; i<nmach; i++){
640 g = &graph[i*ngraph+(ngraph-1)];
641 memset(g, 0, sizeof(Graph));
642 g->label = menu2str[n]+Opwid;
643 g->newvalue = newvaluefn[n];
644 g->update = update1; /* no other update functions yet */
646 g->colindex = nadd%Ncolor;
653 which2index(int which)
658 for(i=0; i<ngraph; i++){
659 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
665 fprint(2, "%s: internal error can't drop graph\n", argv0);
672 index2which(int index)
677 for(i=0; i<Nmenu2; i++){
678 if(strcmp(menu2str[i]+Opwid, graph[index].label) == 0){
684 fprint(2, "%s: internal error can't identify graph\n", argv0);
696 if(which > nelem(menu2str))
698 /* convert n to index in graph table */
699 n = which2index(which);
701 graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
702 for(i=0; i<nmach; i++){
704 graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
705 free(ograph[i*ngraph+j].data);
706 freeimage(ograph[i*ngraph+j].overtmp);
707 for(j++; j<ngraph; j++)
708 graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
716 addmachine(char *name)
719 fprint(2, "%s: internal error: ngraph>0 in addmachine()\n", argv0);
723 sysfatal("too many machines");
724 initmach(&mach[nmach++], name);
731 int i, j, n, startx, starty, x, y, dx, dy, hashdx, ondata;
737 draw(screen, screen->r, display->white, nil, ZP);
739 /* label left edge */
741 y = screen->r.min.y + Labspace+font->height+Labspace;
742 dy = (screen->r.max.y - y)/ngraph;
743 dx = Labspace+stringwidth(font, "0")+Labspace;
746 for(i=0; i<ngraph; i++,y+=dy){
747 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
748 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
749 label(Pt(x, y), dy, graph[i].label);
750 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
753 /* label right edge */
754 dx = Labspace+stringwidth(font, "0.001")+Labspace;
756 x = screen->r.max.x - dx;
757 y = screen->r.min.y + Labspace+font->height+Labspace;
758 for(i=0; i<ngraph; i++,y+=dy){
759 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
760 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
761 hashmarks(Pt(x, y), dy, i);
762 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
766 dx = (screen->r.max.x - dx - startx)/nmach;
767 for(x=startx, i=0; i<nmach; i++,x+=dx){
768 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
769 j = dx/stringwidth(font, "0");
771 if(n>1 && j>=1+3+(n>10)+(n>100)){ /* first char of name + (n) */
772 j -= 3+(n>10)+(n>100);
775 snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].name, n);
777 snprint(buf, sizeof buf, "%.*s", j, mach[i].name);
778 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP,
781 /* draw last vertical line */
783 Rect(screen->r.max.x-hashdx-1, starty-1, screen->r.max.x-hashdx, screen->r.max.y),
784 display->black, nil, ZP);
787 for(i=0; i<nmach; i++){
788 machr = Rect(startx+i*dx, starty, screen->r.max.x, screen->r.max.y);
790 machr.max.x = startx+(i+1)*dx - 1;
792 machr.max.x = screen->r.max.x - hashdx - 1;
794 for(j=0; j<ngraph; j++, y+=dy){
795 g = &graph[i*ngraph+j];
798 g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
799 g->data = erealloc(g->data, g->ndata*sizeof(long));
800 if(g->ndata > ondata)
801 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(long));
805 g->r.max.y = y+dy - 1;
807 g->r.max.y = screen->r.max.y;
808 draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
811 freeimage(g->overtmp);
815 r.max.y = r.min.y+font->height;
816 n = (g->r.max.x - r.min.x)/stringwidth(font, "9");
820 r.max.x = r.min.x+stringwidth(font, "9")*n;
822 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
824 g->newvalue(g->mach, &v, &vmax, &mark);
829 flushimage(display, 1);
835 lockdisplay(display);
836 if(new && getwindow(display, Refnone) < 0) {
837 fprint(2, "%s: can't reattach to window\n", argv0);
841 unlockdisplay(display);
849 for(i=0; i<Nmenu2; i++)
851 memmove(menu2str[i], "drop ", Opwid);
853 memmove(menu2str[i], "add ", Opwid);
854 i = emenuhit(3, m, &menu2);
872 for(i = 0; i < ngraph*nmach; i++){
873 if(ptinrect(m->xy, graph[i].r))
876 if(i == ngraph*nmach)
880 if(g->overtmp == nil)
883 /* clear any previous message and cursor */
884 if(g->overflow || *g->msg){
890 dx = g->r.max.x - m->xy.x;
892 dt = dx*pinginterval;
893 e = &g->msg[sizeof(g->msg)];
894 seprint(g->msg, e, "%s", ctime(starttime-dt/1000)+11);
898 switch(index2which(i)){
900 f = rttunscale(g->data[dx]);
901 seprint(g->msg+n, e, " %3.3g", f/1000000);
904 seprint(g->msg+n, e, " %ld%%", g->data[dx]);
909 drawcursor(g, m->xy.x);
919 if(mouse.buttons == 4){
920 lockdisplay(display);
922 unlockdisplay(display);
923 } else if(mouse.buttons == 1){
924 lockdisplay(display);
926 unlockdisplay(display);
932 startproc(void (*f)(void*), void *arg)
936 switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
938 fprint(2, "%s: fork failed: %r\n", argv0);
939 killall("fork failed");
942 killall("process died");
949 main(int argc, char *argv[])
953 char flags[10], *f, *p;
955 fmtinstall('V', eipfmt);
958 pinginterval = 5000; /* 5 seconds */
964 pinginterval = atoi(p);
967 if(f - flags >= sizeof(flags)-1)
974 for(i=0; i<argc; i++)
977 for(f = flags; *f; f++)
993 for(i=0; i<nmach; i++)
994 for(j=0; j<ngraph; j++)
995 graph[i*ngraph+j].mach = &mach[i];
997 if(initdraw(nil, nil, argv0) < 0){
998 fprint(2, "%s: initdraw failed: %r\n", argv0);
1001 display->locking = 1; /* tell library we're using the display lock */
1004 startproc(mouseproc, 0);
1008 starttime = time(0);
1010 unlockdisplay(display); /* display is still locked from initdraw() */
1012 lockdisplay(display);
1016 for(i=0; i<nmach*ngraph; i++){
1017 graph[i].newvalue(graph[i].mach, &v, &vmax, &mark);
1018 graph[i].update(&graph[i], v, vmax, mark);
1020 starttime = time(0);
1022 flushimage(display, 1);
1023 unlockdisplay(display);
1024 pingsend(&mach[j%nmach]);
1025 sleep(pinginterval/nmach);