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 */
76 Ysqueeze = 2, /* vertical squeezing of label text */
77 Labspace = 2, /* room around label */
78 Dot = 2, /* height of dot */
79 Opwid = 5, /* strlen("add ") or strlen("drop ") */
91 char *menu2str[Nmenu2+1] = {
98 void rttval(Machine*, long*, long*, long*);
99 void lostval(Machine*, long*, long*, long*);
101 Menu menu2 = {menu2str, nil};
103 void (*newvaluefn[Nmenu2])(Machine*, long*, long*, long*) = {
108 Image *cols[Ncolor][3];
113 int parity; /* toggled to avoid patterns in textured background */
115 int ngraph; /* totaly number is ngraph*nmach */
121 void startproc(void (*)(void*), void*);
124 int which2index(int);
125 int index2which(int);
133 for(i=0; i<NPROC; i++)
134 if(pids[i] && pids[i]!=pid)
135 postnote(PNPROC, pids[i], "kill");
145 fprint(2, "%s: out of memory allocating %ld: %r\n", argv0, sz);
153 erealloc(void *v, ulong sz)
157 fprint(2, "%s: out of memory reallocating %ld: %r\n", argv0, sz);
167 if((t = strdup(s)) == nil) {
168 fprint(2, "%s: out of memory in strdup(%.10s): %r\n", argv0, s);
175 mkcol(int i, int c0, int c1, int c2)
177 cols[i][0] = allocimagemix(display, c0, DWhite);
178 cols[i][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c1);
179 cols[i][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c2);
186 mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
188 mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
190 mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
192 mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
194 mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
196 cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
197 cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
198 cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
202 label(Point p, int dy, char *text)
213 if(p.y+font->height-Ysqueeze > maxy)
215 w = chartorune(r, s);
217 w = runestringwidth(font, r);
220 runestring(screen, p, display->black, ZP, font, r);
221 p.y += font->height-Ysqueeze;
226 hashmark(Point p, int dy, long v, long vmax, char *label)
232 y = p.y + (dy*(vmax-v))/vmax;
233 draw(screen, Rect(p.x, y-1, p.x+Labspace, y+1), display->black, nil, ZP);
234 if(dy > 5*font->height)
235 string(screen, Pt(x, y-font->height/2),
236 display->black, ZP, font, label);
240 hashmarks(Point p, int dy, int which)
242 switch(index2which(which)){
244 hashmark(p, dy, rttscale(1000000), Rttmax, "1.");
245 hashmark(p, dy, rttscale(100000), Rttmax, "0.1");
246 hashmark(p, dy, rttscale(10000), Rttmax, "0.01");
247 hashmark(p, dy, rttscale(1000), Rttmax, "0.001");
250 hashmark(p, dy, 75, 100, " 75%");
251 hashmark(p, dy, 50, 100, " 50%");
252 hashmark(p, dy, 25, 100, " 25%");
260 return Pt(x+parity, 0);
264 datapoint(Graph *g, int x, long v, long vmax)
269 p.y = g->r.max.y - Dy(g->r)*v/vmax - Dot;
272 if(p.y > g->r.max.y-Dot)
273 p.y = g->r.max.y-Dot;
278 drawdatum(Graph *g, int x, long prev, long v, long vmax)
284 p = datapoint(g, x, v, vmax);
285 q = datapoint(g, x, prev, vmax);
287 draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
288 draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
289 draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
291 draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
292 draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
293 draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
299 drawmark(Graph *g, int x)
303 c = (g->colindex+1)&Ncolor;
304 draw(screen, Rect(x, g->r.min.y, x+1, g->r.max.y), cols[c][2], nil, ZP);
308 redraw(Graph *g, int vmax)
313 draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
314 for(i=1; i<Dx(g->r); i++)
315 drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
316 drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
322 if(g->overtmp != nil)
323 draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
328 drawmsg(Graph *g, char *msg)
330 if(g->overtmp == nil)
333 /* save previous contents of screen */
334 draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
337 if(strlen(msg) > g->overtmplen)
338 msg[g->overtmplen] = 0;
339 string(screen, g->overtmp->r.min, display->black, ZP, font, msg);
343 clearcursor(Graph *g)
348 if(g->overtmp == nil)
351 if(g->cursor > 0 && g->cursor < g->ndata){
352 x = g->r.max.x - g->cursor;
355 prev = g->data[g->cursor-1];
356 drawdatum(g, x, prev, g->data[g->cursor], g->vmax);
362 drawcursor(Graph *g, int x)
364 if(g->overtmp == nil)
367 draw(screen, Rect(x, g->r.min.y, x+1, g->r.max.y), cols[g->colindex][2], nil, ZP);
371 update1(Graph *g, long v, long vmax, long mark)
375 /* put back screen value sans message */
376 if(g->overflow || *g->msg){
381 draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
382 drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
384 drawmark(g, g->r.max.x-1);
385 memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
389 sprint(buf, "%ld", v);
396 if(g->cursor >= g->ndata){
408 pinglost(Machine *m, Req*)
414 pingreply(Machine *m, Req *r)
426 pingclean(Machine *m, ushort seq, vlong now, int)
431 y = 10LL*1000000000LL;
432 for(l = &m->list; *l; ){
435 if(x > y || r->seq == seq){
453 char buf[128], err[ERRMAX];
457 ip = (Icmphdr *)(buf + IPV4HDR_LEN);
458 memset(buf, 0, sizeof buf);
459 r = malloc(sizeof *r);
463 for(i = 32; i < MSGLEN; i++)
465 ip->type = EchoRequest;
468 ip->seq[1] = m->seq>>8;
472 pingclean(m, -1, r->time, 0);
476 if(write(m->pingfd, buf, MSGLEN) < MSGLEN){
477 errstr(err, sizeof err);
478 if(strstr(err, "unreach")||strstr(err, "exceed"))
496 ip = (Icmphdr *)(buf + IPV4HDR_LEN);
498 n = read(m->pingfd, buf, sizeof(buf));
503 for(i = 32; i < MSGLEN; i++)
504 if(buf[i] != (i&0xff))
508 x = (ip->seq[1]<<8) | ip->seq[0];
509 if(ip->type != EchoReply || ip->code != 0)
512 pingclean(m, x, nsec(), ip4->ttl);
518 initmach(Machine *m, char *name)
524 p = strchr(name, '!');
527 m->name = estrdup(p+1);
531 m->name = estrdup(p);
533 m->pingfd = dial(netmkaddr(m->name, "icmp", "1"), nil, nil, &cfd);
535 sysfatal("dialing %s: %r", m->name);
536 write(cfd, "ignoreadvice", 12);
538 startproc(pingrcv, m);
546 x = 10.0*log10(x) - 20.0;
559 return pow(10.0, dx/10.0);
563 rttval(Machine *m, long *v, long *vmax, long *mark)
570 x = m->rttsum/m->rttmsgs;
571 m->rttsum = m->rttmsgs = 0;
581 lostval(Machine *m, long *v, long *vmax, long *mark)
585 if(m->rcvdmsgs+m->lostmsgs > 0)
586 x = (m->lostavg>>1) + (((m->lostmsgs*100)/(m->lostmsgs + m->rcvdmsgs))>>1);
590 m->lostmsgs = m->rcvdmsgs = 0;
605 fprint(2, "usage: %s machine [machine...]\n", argv0);
616 if(n > nelem(menu2str))
618 /* avoid two adjacent graphs of same color */
619 if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
622 graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
623 for(i=0; i<nmach; i++)
624 for(j=0; j<ngraph; j++)
625 graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
628 for(i=0; i<nmach; i++){
629 g = &graph[i*ngraph+(ngraph-1)];
630 memset(g, 0, sizeof(Graph));
631 g->label = menu2str[n]+Opwid;
632 g->newvalue = newvaluefn[n];
633 g->update = update1; /* no other update functions yet */
635 g->colindex = nadd%Ncolor;
642 which2index(int which)
647 for(i=0; i<ngraph; i++){
648 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
654 fprint(2, "%s: internal error can't drop graph\n", argv0);
661 index2which(int index)
666 for(i=0; i<Nmenu2; i++){
667 if(strcmp(menu2str[i]+Opwid, graph[index].label) == 0){
673 fprint(2, "%s: internal error can't identify graph\n", argv0);
685 if(which > nelem(menu2str))
687 /* convert n to index in graph table */
688 n = which2index(which);
690 graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
691 for(i=0; i<nmach; i++){
693 graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
694 free(ograph[i*ngraph+j].data);
695 freeimage(ograph[i*ngraph+j].overtmp);
696 for(j++; j<ngraph; j++)
697 graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
705 addmachine(char *name)
708 fprint(2, "%s: internal error: ngraph>0 in addmachine()\n", argv0);
712 sysfatal("too many machines");
713 initmach(&mach[nmach++], name);
720 int i, j, n, startx, starty, x, y, dx, dy, hashdx, ondata;
726 draw(screen, screen->r, display->white, nil, ZP);
728 /* label left edge */
730 y = screen->r.min.y + Labspace+font->height+Labspace;
731 dy = (screen->r.max.y - y)/ngraph;
732 dx = Labspace+stringwidth(font, "0")+Labspace;
735 for(i=0; i<ngraph; i++,y+=dy){
736 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
737 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
738 label(Pt(x, y), dy, graph[i].label);
739 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
742 /* label right edge */
743 dx = Labspace+stringwidth(font, "0.001")+Labspace;
745 x = screen->r.max.x - dx;
746 y = screen->r.min.y + Labspace+font->height+Labspace;
747 for(i=0; i<ngraph; i++,y+=dy){
748 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
749 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
750 hashmarks(Pt(x, y), dy, i);
751 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
755 dx = (screen->r.max.x - dx - startx)/nmach;
756 for(x=startx, i=0; i<nmach; i++,x+=dx){
757 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
758 j = dx/stringwidth(font, "0");
760 if(n>1 && j>=1+3+(n>10)+(n>100)){ /* first char of name + (n) */
761 j -= 3+(n>10)+(n>100);
764 snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].name, n);
766 snprint(buf, sizeof buf, "%.*s", j, mach[i].name);
767 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP,
770 /* draw last vertical line */
772 Rect(screen->r.max.x-hashdx-1, starty-1, screen->r.max.x-hashdx, screen->r.max.y),
773 display->black, nil, ZP);
776 for(i=0; i<nmach; i++){
777 machr = Rect(startx+i*dx, starty, screen->r.max.x, screen->r.max.y);
779 machr.max.x = startx+(i+1)*dx - 1;
781 machr.max.x = screen->r.max.x - hashdx - 1;
783 for(j=0; j<ngraph; j++, y+=dy){
784 g = &graph[i*ngraph+j];
787 g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
788 g->data = erealloc(g->data, g->ndata*sizeof(long));
789 if(g->ndata > ondata)
790 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(long));
794 g->r.max.y = y+dy - 1;
796 g->r.max.y = screen->r.max.y;
797 draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
800 freeimage(g->overtmp);
804 r.max.y = r.min.y+font->height;
805 n = (g->r.max.x - r.min.x)/stringwidth(font, "9");
809 r.max.x = r.min.x+stringwidth(font, "9")*n;
811 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
813 g->newvalue(g->mach, &v, &vmax, &mark);
818 flushimage(display, 1);
824 lockdisplay(display);
825 if(new && getwindow(display, Refnone) < 0) {
826 fprint(2, "%s: can't reattach to window\n", argv0);
830 unlockdisplay(display);
838 for(i=0; i<Nmenu2; i++)
840 memmove(menu2str[i], "drop ", Opwid);
842 memmove(menu2str[i], "add ", Opwid);
843 i = emenuhit(3, m, &menu2);
861 for(i = 0; i < ngraph*nmach; i++){
862 if(ptinrect(m->xy, graph[i].r))
865 if(i == ngraph*nmach)
869 if(g->overtmp == nil)
872 /* clear any previous message and cursor */
873 if(g->overflow || *g->msg){
879 dx = g->r.max.x - m->xy.x;
881 dt = dx*pinginterval;
882 e = &g->msg[sizeof(g->msg)];
883 seprint(g->msg, e, "%s", ctime(starttime-dt/1000)+11);
887 switch(index2which(i)){
889 f = rttunscale(g->data[dx]);
890 seprint(g->msg+n, e, " %3.3g", f/1000000);
893 seprint(g->msg+n, e, " %ld%%", g->data[dx]);
898 drawcursor(g, m->xy.x);
908 if(mouse.buttons == 4){
909 lockdisplay(display);
911 unlockdisplay(display);
912 } else if(mouse.buttons == 1){
913 lockdisplay(display);
915 unlockdisplay(display);
921 startproc(void (*f)(void*), void *arg)
925 switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
927 fprint(2, "%s: fork failed: %r\n", argv0);
928 killall("fork failed");
931 killall("process died");
938 main(int argc, char *argv[])
942 char flags[10], *f, *p;
944 fmtinstall('V', eipfmt);
947 pinginterval = 5000; /* 5 seconds */
953 pinginterval = atoi(p);
956 if(f - flags >= sizeof(flags)-1)
963 pids[npid++] = getpid();
965 for(i=0; i<argc; i++)
968 for(f = flags; *f; f++)
984 for(i=0; i<nmach; i++)
985 for(j=0; j<ngraph; j++)
986 graph[i*ngraph+j].mach = &mach[i];
988 if(initdraw(nil, nil, argv0) < 0){
989 fprint(2, "%s: initdraw failed: %r\n", argv0);
992 display->locking = 1; /* tell library we're using the display lock */
995 startproc(mouseproc, 0);
1001 unlockdisplay(display); /* display is still locked from initdraw() */
1003 lockdisplay(display);
1007 for(i=0; i<nmach*ngraph; i++){
1008 graph[i].newvalue(graph[i].mach, &v, &vmax, &mark);
1009 graph[i].update(&graph[i], v, vmax, mark);
1011 starttime = time(0);
1013 flushimage(display, 1);
1014 unlockdisplay(display);
1015 pingsend(&mach[j%nmach]);
1016 sleep(pinginterval/nmach);