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 */
77 Ysqueeze = 2, /* vertical squeezing of label text */
78 Labspace = 2, /* room around label */
79 Dot = 2, /* height of dot */
80 Opwid = 5, /* strlen("add ") or strlen("drop ") */
92 char *menu2str[Nmenu2+1] = {
99 void rttval(Machine*, long*, long*, long*);
100 void lostval(Machine*, long*, long*, long*);
102 Menu menu2 = {menu2str, nil};
104 void (*newvaluefn[Nmenu2])(Machine*, long*, long*, long*) = {
109 Image *cols[Ncolor][3];
114 int parity; /* toggled to avoid patterns in textured background */
116 int ngraph; /* totaly number is ngraph*nmach */
122 void startproc(void (*)(void*), void*);
125 int which2index(int);
126 int index2which(int);
134 for(i=0; i<NPROC; i++)
135 if(pids[i] && pids[i]!=pid)
136 postnote(PNPROC, pids[i], "kill");
146 fprint(2, "%s: out of memory allocating %ld: %r\n", argv0, sz);
154 erealloc(void *v, ulong sz)
158 fprint(2, "%s: out of memory reallocating %ld: %r\n", argv0, sz);
168 if((t = strdup(s)) == nil) {
169 fprint(2, "%s: out of memory in strdup(%.10s): %r\n", argv0, s);
176 mkcol(int i, int c0, int c1, int c2)
178 cols[i][0] = allocimagemix(display, c0, DWhite);
179 cols[i][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c1);
180 cols[i][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c2);
187 mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
189 mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
191 mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
193 mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
195 mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
197 cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
198 cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
199 cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
203 label(Point p, int dy, char *text)
214 if(p.y+font->height-Ysqueeze > maxy)
216 w = chartorune(r, s);
218 w = runestringwidth(font, r);
221 runestring(screen, p, display->black, ZP, font, r);
222 p.y += font->height-Ysqueeze;
227 hashmark(Point p, int dy, long v, long vmax, char *label)
233 y = p.y + (dy*(vmax-v))/vmax;
234 draw(screen, Rect(p.x, y-1, p.x+Labspace, y+1), display->black, nil, ZP);
235 if(dy > 5*font->height)
236 string(screen, Pt(x, y-font->height/2),
237 display->black, ZP, font, label);
241 hashmarks(Point p, int dy, int which)
243 switch(index2which(which)){
245 hashmark(p, dy, rttscale(1000000), Rttmax, "1.");
246 hashmark(p, dy, rttscale(100000), Rttmax, "0.1");
247 hashmark(p, dy, rttscale(10000), Rttmax, "0.01");
248 hashmark(p, dy, rttscale(1000), Rttmax, "0.001");
251 hashmark(p, dy, 75, 100, " 75%");
252 hashmark(p, dy, 50, 100, " 50%");
253 hashmark(p, dy, 25, 100, " 25%");
261 return Pt(x+parity, 0);
265 datapoint(Graph *g, int x, long v, long vmax)
270 p.y = g->r.max.y - Dy(g->r)*v/vmax - Dot;
273 if(p.y > g->r.max.y-Dot)
274 p.y = g->r.max.y-Dot;
279 drawdatum(Graph *g, int x, long prev, long v, long vmax)
285 p = datapoint(g, x, v, vmax);
286 q = datapoint(g, x, prev, vmax);
288 draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
289 draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
290 draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
292 draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
293 draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
294 draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
300 drawmark(Graph *g, int x)
304 c = (g->colindex+1)&Ncolor;
305 draw(screen, Rect(x, g->r.min.y, x+1, g->r.max.y), cols[c][2], nil, ZP);
309 redraw(Graph *g, int vmax)
314 draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
315 for(i=1; i<Dx(g->r); i++)
316 drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
317 drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
323 if(g->overtmp != nil)
324 draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
329 drawmsg(Graph *g, char *msg)
331 if(g->overtmp == nil)
334 /* save previous contents of screen */
335 draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
338 if(strlen(msg) > g->overtmplen)
339 msg[g->overtmplen] = 0;
340 string(screen, g->overtmp->r.min, display->black, ZP, font, msg);
344 clearcursor(Graph *g)
349 if(g->overtmp == nil)
352 if(g->cursor > 0 && g->cursor < g->ndata){
353 x = g->r.max.x - g->cursor;
356 prev = g->data[g->cursor-1];
357 drawdatum(g, x, prev, g->data[g->cursor], g->vmax);
363 drawcursor(Graph *g, int x)
365 if(g->overtmp == nil)
368 draw(screen, Rect(x, g->r.min.y, x+1, g->r.max.y), cols[g->colindex][2], nil, ZP);
372 update1(Graph *g, long v, long vmax, long mark)
376 /* put back screen value sans message */
377 if(g->overflow || *g->msg){
382 draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
383 drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
385 drawmark(g, g->r.max.x-1);
386 memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
390 sprint(buf, "%ld", v);
397 if(g->cursor >= g->ndata){
409 pinglost(Machine *m, Req*)
415 pingreply(Machine *m, Req *r)
427 pingclean(Machine *m, ushort seq, vlong now)
432 y = 10LL*1000000000LL;
433 for(l = &m->list; *l; ){
436 if(x > y || r->seq == seq){
458 ip = (Icmphdr *)(buf + (m->version==4? IPV4HDR_LEN: IPV6HDR_LEN));
459 memset(buf, 0, sizeof buf);
460 r = malloc(sizeof *r);
464 for(i = ip->data-buf; i < MSGLEN; i++)
466 ip->type = m->version==4? EchoRequest: EchoRequestV6;
469 ip->seq[1] = m->seq>>8;
473 pingclean(m, -1, r->time);
477 if(write(m->pingfd, buf, MSGLEN) < MSGLEN){
478 errstr(err, sizeof err);
479 if(strstr(err, "unreach")||strstr(err, "exceed"))
494 ip = (Icmphdr *)(buf + (m->version==4? IPV4HDR_LEN: IPV6HDR_LEN));
496 n = read(m->pingfd, buf, sizeof(buf));
501 for(i = ip->data-buf; i < MSGLEN; i++)
502 if(buf[i] != (i&0xff))
506 x = (ip->seq[1]<<8) | ip->seq[0];
507 if(ip->type != (m->version==4? EchoReply: EchoReplyV6) || ip->code != 0)
510 pingclean(m, x, nsec());
516 initmach(Machine *m, char *name)
522 p = strchr(name, '!');
525 m->name = estrdup(p+1);
528 m->name = estrdup(p);
532 if(strstr(name, "icmpv6!") != nil)
535 m->pingfd = dial(netmkaddr(m->name, m->version==4? "icmp": "icmpv6", "1"), nil, nil, &cfd);
541 sysfatal("dialing %s: %r", m->name);
543 write(cfd, "ignoreadvice", 12);
545 startproc(pingrcv, m);
553 x = 10.0*log10(x) - 20.0;
566 return pow(10.0, dx/10.0);
570 rttval(Machine *m, long *v, long *vmax, long *mark)
577 x = m->rttsum/m->rttmsgs;
578 m->rttsum = m->rttmsgs = 0;
588 lostval(Machine *m, long *v, long *vmax, long *mark)
592 if(m->rcvdmsgs+m->lostmsgs > 0)
593 x = (m->lostavg>>1) + (((m->lostmsgs*100)/(m->lostmsgs + m->rcvdmsgs))>>1);
597 m->lostmsgs = m->rcvdmsgs = 0;
612 fprint(2, "usage: %s machine [machine...]\n", argv0);
623 if(n > nelem(menu2str))
625 /* avoid two adjacent graphs of same color */
626 if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
629 graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
630 for(i=0; i<nmach; i++)
631 for(j=0; j<ngraph; j++)
632 graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
635 for(i=0; i<nmach; i++){
636 g = &graph[i*ngraph+(ngraph-1)];
637 memset(g, 0, sizeof(Graph));
638 g->label = menu2str[n]+Opwid;
639 g->newvalue = newvaluefn[n];
640 g->update = update1; /* no other update functions yet */
642 g->colindex = nadd%Ncolor;
649 which2index(int which)
654 for(i=0; i<ngraph; i++){
655 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
661 fprint(2, "%s: internal error can't drop graph\n", argv0);
668 index2which(int index)
673 for(i=0; i<Nmenu2; i++){
674 if(strcmp(menu2str[i]+Opwid, graph[index].label) == 0){
680 fprint(2, "%s: internal error can't identify graph\n", argv0);
692 if(which > nelem(menu2str))
694 /* convert n to index in graph table */
695 n = which2index(which);
697 graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
698 for(i=0; i<nmach; i++){
700 graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
701 free(ograph[i*ngraph+j].data);
702 freeimage(ograph[i*ngraph+j].overtmp);
703 for(j++; j<ngraph; j++)
704 graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
712 addmachine(char *name)
715 fprint(2, "%s: internal error: ngraph>0 in addmachine()\n", argv0);
719 sysfatal("too many machines");
720 initmach(&mach[nmach++], name);
727 int i, j, n, startx, starty, x, y, dx, dy, hashdx, ondata;
733 draw(screen, screen->r, display->white, nil, ZP);
735 /* label left edge */
737 y = screen->r.min.y + Labspace+font->height+Labspace;
738 dy = (screen->r.max.y - y)/ngraph;
739 dx = Labspace+stringwidth(font, "0")+Labspace;
742 for(i=0; i<ngraph; i++,y+=dy){
743 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
744 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
745 label(Pt(x, y), dy, graph[i].label);
746 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
749 /* label right edge */
750 dx = Labspace+stringwidth(font, "0.001")+Labspace;
752 x = screen->r.max.x - dx;
753 y = screen->r.min.y + Labspace+font->height+Labspace;
754 for(i=0; i<ngraph; i++,y+=dy){
755 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
756 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
757 hashmarks(Pt(x, y), dy, i);
758 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
762 dx = (screen->r.max.x - dx - startx)/nmach;
763 for(x=startx, i=0; i<nmach; i++,x+=dx){
764 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
765 j = dx/stringwidth(font, "0");
767 if(n>1 && j>=1+3+(n>10)+(n>100)){ /* first char of name + (n) */
768 j -= 3+(n>10)+(n>100);
771 snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].name, n);
773 snprint(buf, sizeof buf, "%.*s", j, mach[i].name);
774 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP,
777 /* draw last vertical line */
779 Rect(screen->r.max.x-hashdx-1, starty-1, screen->r.max.x-hashdx, screen->r.max.y),
780 display->black, nil, ZP);
783 for(i=0; i<nmach; i++){
784 machr = Rect(startx+i*dx, starty, screen->r.max.x, screen->r.max.y);
786 machr.max.x = startx+(i+1)*dx - 1;
788 machr.max.x = screen->r.max.x - hashdx - 1;
790 for(j=0; j<ngraph; j++, y+=dy){
791 g = &graph[i*ngraph+j];
794 g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
795 g->data = erealloc(g->data, g->ndata*sizeof(long));
796 if(g->ndata > ondata)
797 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(long));
801 g->r.max.y = y+dy - 1;
803 g->r.max.y = screen->r.max.y;
804 draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
807 freeimage(g->overtmp);
811 r.max.y = r.min.y+font->height;
812 n = (g->r.max.x - r.min.x)/stringwidth(font, "9");
816 r.max.x = r.min.x+stringwidth(font, "9")*n;
818 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
820 g->newvalue(g->mach, &v, &vmax, &mark);
825 flushimage(display, 1);
831 lockdisplay(display);
832 if(new && getwindow(display, Refnone) < 0) {
833 fprint(2, "%s: can't reattach to window\n", argv0);
837 unlockdisplay(display);
845 for(i=0; i<Nmenu2; i++)
847 memmove(menu2str[i], "drop ", Opwid);
849 memmove(menu2str[i], "add ", Opwid);
850 i = emenuhit(3, m, &menu2);
868 for(i = 0; i < ngraph*nmach; i++){
869 if(ptinrect(m->xy, graph[i].r))
872 if(i == ngraph*nmach)
876 if(g->overtmp == nil)
879 /* clear any previous message and cursor */
880 if(g->overflow || *g->msg){
886 dx = g->r.max.x - m->xy.x;
888 dt = dx*pinginterval;
889 e = &g->msg[sizeof(g->msg)];
890 seprint(g->msg, e, "%s", ctime(starttime-dt/1000)+11);
894 switch(index2which(i)){
896 f = rttunscale(g->data[dx]);
897 seprint(g->msg+n, e, " %3.3g", f/1000000);
900 seprint(g->msg+n, e, " %ld%%", g->data[dx]);
905 drawcursor(g, m->xy.x);
915 if(mouse.buttons == 4){
916 lockdisplay(display);
918 unlockdisplay(display);
919 } else if(mouse.buttons == 1){
920 lockdisplay(display);
922 unlockdisplay(display);
928 startproc(void (*f)(void*), void *arg)
932 switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
934 fprint(2, "%s: fork failed: %r\n", argv0);
935 killall("fork failed");
938 killall("process died");
945 main(int argc, char *argv[])
949 char flags[10], *f, *p;
951 fmtinstall('V', eipfmt);
954 pinginterval = 5000; /* 5 seconds */
960 pinginterval = atoi(p);
963 if(f - flags >= sizeof(flags)-1)
970 pids[npid++] = getpid();
972 for(i=0; i<argc; i++)
975 for(f = flags; *f; f++)
991 for(i=0; i<nmach; i++)
992 for(j=0; j<ngraph; j++)
993 graph[i*ngraph+j].mach = &mach[i];
995 if(initdraw(nil, nil, argv0) < 0){
996 fprint(2, "%s: initdraw failed: %r\n", argv0);
999 display->locking = 1; /* tell library we're using the display lock */
1002 startproc(mouseproc, 0);
1006 starttime = time(0);
1008 unlockdisplay(display); /* display is still locked from initdraw() */
1010 lockdisplay(display);
1014 for(i=0; i<nmach*ngraph; i++){
1015 graph[i].newvalue(graph[i].mach, &v, &vmax, &mark);
1016 graph[i].update(&graph[i], v, vmax, mark);
1018 starttime = time(0);
1020 flushimage(display, 1);
1021 unlockdisplay(display);
1022 pingsend(&mach[j%nmach]);
1023 sleep(pinginterval/nmach);