]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/ip/gping.c
ip/torrent: remove unneeded assignment
[plan9front.git] / sys / src / cmd / ip / gping.c
1 #include <u.h>
2 #include <libc.h>
3 #include <ctype.h>
4 #include <auth.h>
5 #include <fcall.h>
6 #include <draw.h>
7 #include <event.h>
8 #include <ip.h>
9 #include "icmp.h"
10
11 #define MAXNUM  8       /* maximum number of numbers on data line */
12
13 typedef struct Graph    Graph;
14 typedef struct Machine  Machine;
15 typedef struct Req      Req;
16
17 enum {
18         Gmsglen = 16,
19 };
20
21 struct Graph
22 {
23         int             colindex;
24         Rectangle       r;
25         long            *data;
26         int             ndata;
27         char            *label;
28         void            (*newvalue)(Machine*, long*, long*, long*);
29         void            (*update)(Graph*, long, long, long);
30         Machine         *mach;
31         int             overflow;
32         Image           *overtmp;
33         int             overtmplen;
34         char            msg[Gmsglen];
35         int             cursor;
36         int             vmax;
37 };
38
39 enum
40 {
41         MSGLEN          = 64,
42
43         Rttmax          = 50,
44 };
45
46 struct Req
47 {
48         int     seq;    /* sequence number */
49         vlong   time;   /* time sent */
50 //      int     rtt;
51         Req     *next;
52 };
53
54 struct Machine
55 {
56         Lock;
57         char    *name;
58         int     pingfd;
59         int     nproc;
60
61         int     rttmsgs;
62         ulong   rttsum;
63         ulong   lastrtt;
64
65         int     lostmsgs;
66         int     rcvdmsgs;
67         ulong   lostavg;
68         int     unreachable;
69
70         ushort  seq;
71         Req     *first;
72         Req     *last;
73         Req     *rcvd;
74
75         char    buf[1024];
76         char    *bufp;
77         char    *ebufp;
78 };
79
80 enum
81 {
82         Ncolor          = 6,
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 ") */
87         NPROC           = 128,
88         NMACH           = 32,
89 };
90
91 enum Menu2
92 {
93         Mrtt,
94         Mlost,
95         Nmenu2,
96 };
97
98 char    *menu2str[Nmenu2+1] = {
99         "add  sec rtt",
100         "add  % lost ",
101         nil,
102 };
103
104
105 void    rttval(Machine*, long*, long*, long*);
106 void    lostval(Machine*, long*, long*, long*);
107
108 Menu    menu2 = {menu2str, nil};
109 int             present[Nmenu2];
110 void            (*newvaluefn[Nmenu2])(Machine*, long*, long*, long*) = {
111         rttval,
112         lostval,
113 };
114
115 Image           *cols[Ncolor][3];
116 Graph           *graph;
117 Machine         mach[NMACH];
118 int             pids[NPROC];
119 int             npid;
120 int             parity; /* toggled to avoid patterns in textured background */
121 int             nmach;
122 int             ngraph; /* totaly number is ngraph*nmach */
123 long            starttime;
124 int             pinginterval;
125
126 void    dropgraph(int);
127 void    addgraph(int);
128 void    startproc(void (*)(void*), void*);
129 void    resize(void);
130 long    rttscale(long);
131 int     which2index(int);
132 int     index2which(int);
133
134 void
135 killall(char *s)
136 {
137         int i, pid;
138
139         pid = getpid();
140         for(i=0; i<NPROC; i++)
141                 if(pids[i] && pids[i]!=pid)
142                         postnote(PNPROC, pids[i], "kill");
143         exits(s);
144 }
145
146 void*
147 emalloc(ulong sz)
148 {
149         void *v;
150         v = malloc(sz);
151         if(v == nil) {
152                 fprint(2, "%s: out of memory allocating %ld: %r\n", argv0, sz);
153                 killall("mem");
154         }
155         memset(v, 0, sz);
156         return v;
157 }
158
159 void*
160 erealloc(void *v, ulong sz)
161 {
162         v = realloc(v, sz);
163         if(v == nil) {
164                 fprint(2, "%s: out of memory reallocating %ld: %r\n", argv0, sz);
165                 killall("mem");
166         }
167         return v;
168 }
169
170 char*
171 estrdup(char *s)
172 {
173         char *t;
174         if((t = strdup(s)) == nil) {
175                 fprint(2, "%s: out of memory in strdup(%.10s): %r\n", argv0, s);
176                 killall("mem");
177         }
178         return t;
179 }
180
181 void
182 mkcol(int i, int c0, int c1, int c2)
183 {
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);
187 }
188
189 void
190 colinit(void)
191 {
192         /* Peach */
193         mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
194         /* Aqua */
195         mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
196         /* Yellow */
197         mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
198         /* Green */
199         mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
200         /* Blue */
201         mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
202         /* Grey */
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);
206 }
207
208 int
209 loadbuf(Machine *m, int *fd)
210 {
211         int n;
212
213
214         if(*fd < 0)
215                 return 0;
216         seek(*fd, 0, 0);
217         n = read(*fd, m->buf, sizeof m->buf);
218         if(n <= 0){
219                 close(*fd);
220                 *fd = -1;
221                 return 0;
222         }
223         m->bufp = m->buf;
224         m->ebufp = m->buf+n;
225         return 1;
226 }
227
228 void
229 label(Point p, int dy, char *text)
230 {
231         char *s;
232         Rune r[2];
233         int w, maxw, maxy;
234
235         p.x += Labspace;
236         maxy = p.y+dy;
237         maxw = 0;
238         r[1] = '\0';
239         for(s=text; *s; ){
240                 if(p.y+font->height-Ysqueeze > maxy)
241                         break;
242                 w = chartorune(r, s);
243                 s += w;
244                 w = runestringwidth(font, r);
245                 if(w > maxw)
246                         maxw = w;
247                 runestring(screen, p, display->black, ZP, font, r);
248                 p.y += font->height-Ysqueeze;
249         }
250 }
251
252 void
253 hashmark(Point p, int dy, long v, long vmax, char *label)
254 {
255         int y;
256         int x;
257
258         x = p.x + Labspace;
259         y = p.y + (dy*(vmax-v))/vmax;
260         draw(screen, Rect(p.x, y-1, p.x+Labspace, y+1), display->black, nil, ZP);
261         if(dy > 5*font->height)
262                 string(screen, Pt(x, y-font->height/2),
263                         display->black, ZP, font, label);
264 }
265
266 void
267 hashmarks(Point p, int dy, int which)
268 {
269         switch(index2which(which)){
270         case Mrtt:
271                 hashmark(p, dy, rttscale(1000000), Rttmax, "1.");
272                 hashmark(p, dy, rttscale(100000), Rttmax, "0.1");
273                 hashmark(p, dy, rttscale(10000), Rttmax, "0.01");
274                 hashmark(p, dy, rttscale(1000), Rttmax, "0.001");
275                 break;
276         case Mlost:
277                 hashmark(p, dy, 75, 100, " 75%");
278                 hashmark(p, dy, 50, 100, " 50%");
279                 hashmark(p, dy, 25, 100, " 25%");
280                 break;
281         }
282 }
283
284 Point
285 paritypt(int x)
286 {
287         return Pt(x+parity, 0);
288 }
289
290 Point
291 datapoint(Graph *g, int x, long v, long vmax)
292 {
293         Point p;
294
295         p.x = x;
296         p.y = g->r.max.y - Dy(g->r)*v/vmax - Dot;
297         if(p.y < g->r.min.y)
298                 p.y = g->r.min.y;
299         if(p.y > g->r.max.y-Dot)
300                 p.y = g->r.max.y-Dot;
301         return p;
302 }
303
304 void
305 drawdatum(Graph *g, int x, long prev, long v, long vmax)
306 {
307         int c;
308         Point p, q;
309
310         c = g->colindex;
311         p = datapoint(g, x, v, vmax);
312         q = datapoint(g, x, prev, vmax);
313         if(p.y < q.y){
314                 draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
315                 draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
316                 draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
317         }else{
318                 draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
319                 draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
320                 draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
321         }
322         g->vmax = vmax;
323 }
324
325 void
326 drawmark(Graph *g, int x)
327 {
328         int c;
329
330         c = (g->colindex+1)&Ncolor;
331         draw(screen, Rect(x, g->r.min.y, x+1, g->r.max.y), cols[c][2], nil, ZP);
332 }
333
334 void
335 redraw(Graph *g, int vmax)
336 {
337         int i, c;
338
339         c = g->colindex;
340         draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
341         for(i=1; i<Dx(g->r); i++)
342                 drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
343         drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
344 }
345
346 void
347 clearmsg(Graph *g)
348 {
349         if(g->overtmp != nil)
350                 draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
351         g->overflow = 0;
352 }
353
354 void
355 drawmsg(Graph *g, char *msg)
356 {
357         if(g->overtmp == nil)
358                 return;
359
360         /* save previous contents of screen */
361         draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
362
363         /* draw message */
364         if(strlen(msg) > g->overtmplen)
365                 msg[g->overtmplen] = 0;
366         string(screen, g->overtmp->r.min, display->black, ZP, font, msg);
367 }
368
369 void
370 clearcursor(Graph *g)
371 {
372         int x;
373         long prev;
374
375         if(g->overtmp == nil)
376                 return;
377
378         if(g->cursor > 0 && g->cursor < g->ndata){
379                 x = g->r.max.x - g->cursor;
380                 prev = 0;
381                 if(g->cursor > 0)
382                         prev = g->data[g->cursor-1];
383                 drawdatum(g, x, prev, g->data[g->cursor], g->vmax);
384                 g->cursor = -1;
385         }
386 }
387
388 void
389 drawcursor(Graph *g, int x)
390 {
391         if(g->overtmp == nil)
392                 return;
393
394         draw(screen, Rect(x, g->r.min.y, x+1, g->r.max.y), cols[g->colindex][2], nil, ZP);
395 }
396
397 void
398 update1(Graph *g, long v, long vmax, long mark)
399 {
400         char buf[Gmsglen];
401
402         /* put back screen value sans message */
403         if(g->overflow || *g->msg){
404                 clearmsg(g);
405                 g->overflow = 0;
406         }
407
408         draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
409         drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
410         if(mark)
411                 drawmark(g, g->r.max.x-1);
412         memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
413         g->data[0] = v;
414         if(v>vmax){
415                 g->overflow = 1;
416                 sprint(buf, "%ld", v);
417                 drawmsg(g, buf);
418         } else if(*g->msg)
419                 drawmsg(g, g->msg);
420
421         if(g->cursor >= 0){
422                 g->cursor++;
423                 if(g->cursor >= g->ndata){
424                         g->cursor = -1;
425                         if(*g->msg){
426                                 clearmsg(g);
427                                 *g->msg = 0;
428                         }
429                 }
430         }
431
432 }
433
434 void
435 pinglost(Machine *m, Req*)
436 {
437         m->lostmsgs++;
438 }
439
440 void
441 pingreply(Machine *m, Req *r)
442 {
443         ulong x;
444
445         x = r->time/1000LL;
446         m->rttsum += x;
447         m->rcvdmsgs++;
448         m->rttmsgs++;
449 }
450
451
452 void
453 pingclean(Machine *m, ushort seq, vlong now, int)
454 {
455         Req **l, *r;
456         vlong x, y;
457
458         y = 10LL*1000000000LL;
459         for(l = &m->first; *l; ){
460                 r = *l;
461                 x = now - r->time;
462                 if(x > y || r->seq == seq){
463                         *l = r->next;
464                         r->time = x;
465                         if(r->seq != seq)
466                                 pinglost(m, r);
467                         else
468                                 pingreply(m, r);
469                         free(r);
470                 } else
471                         l = &(r->next);
472         }
473 }
474
475 /* IPv4 only */
476 void
477 pingsend(Machine *m)
478 {
479         int i;
480         char buf[128], err[ERRMAX];
481         Icmphdr *ip;
482         Req *r;
483
484         ip = (Icmphdr *)(buf + IPV4HDR_LEN);
485         memset(buf, 0, sizeof buf);
486         r = malloc(sizeof *r);
487         if(r == nil)
488                 return;
489
490         for(i = 32; i < MSGLEN; i++)
491                 buf[i] = i;
492         ip->type = EchoRequest;
493         ip->code = 0;
494         ip->seq[0] = m->seq;
495         ip->seq[1] = m->seq>>8;
496         r->seq = m->seq;
497         r->next = nil;
498         lock(m);
499         pingclean(m, -1, nsec(), 0);
500         if(m->first == nil)
501                 m->first = r;
502         else
503                 m->last->next = r;
504         m->last = r;
505         r->time = nsec();
506         unlock(m);
507         if(write(m->pingfd, buf, MSGLEN) < MSGLEN){
508                 errstr(err, sizeof err);
509                 if(strstr(err, "unreach")||strstr(err, "exceed"))
510                         m->unreachable++;
511         }
512         m->seq++;
513 }
514
515 /* IPv4 only */
516 void
517 pingrcv(void *arg)
518 {
519         int i, n, fd;
520         uchar buf[512];
521         ushort x;
522         vlong now;
523         Icmphdr *ip;
524         Ip4hdr *ip4;
525         Machine *m = arg;
526
527         ip4 = (Ip4hdr *)buf;
528         ip = (Icmphdr *)(buf + IPV4HDR_LEN);
529         fd = dup(m->pingfd, -1);
530         for(;;){
531                 n = read(fd, buf, sizeof(buf));
532                 now = nsec();
533                 if(n <= 0)
534                         continue;
535                 if(n < MSGLEN){
536                         print("bad len %d/%d\n", n, MSGLEN);
537                         continue;
538                 }
539                 for(i = 32; i < MSGLEN; i++)
540                         if(buf[i] != (i&0xff))
541                                 continue;
542                 x = (ip->seq[1]<<8) | ip->seq[0];
543                 if(ip->type != EchoReply || ip->code != 0)
544                         continue;
545                 lock(m);
546                 pingclean(m, x, now, ip4->ttl);
547                 unlock(m);
548         }
549 }
550
551 void
552 initmach(Machine *m, char *name)
553 {
554         char *p;
555
556         srand(time(0));
557         p = strchr(name, '!');
558         if(p){
559                 p++;
560                 m->name = estrdup(p+1);
561         }else
562                 p = name;
563
564         m->name = estrdup(p);
565         m->nproc = 1;
566         m->pingfd = dial(netmkaddr(m->name, "icmp", "1"), 0, 0, 0);
567         if(m->pingfd < 0)
568                 sysfatal("dialing %s: %r", m->name);
569         startproc(pingrcv, m);
570 }
571
572 long
573 rttscale(long x)
574 {
575         if(x == 0)
576                 return 0;
577         x = 10.0*log10(x) - 20.0;
578         if(x < 0)
579                 x = 0;
580         return x;
581 }
582
583 double
584 rttunscale(long x)
585 {
586         double dx;
587
588         x += 20;
589         dx = x;
590         return pow(10.0, dx/10.0);
591 }
592
593 void
594 rttval(Machine *m, long *v, long *vmax, long *mark)
595 {
596         ulong x;
597
598         if(m->rttmsgs == 0){
599                 x = m->lastrtt;
600         } else {
601                 x = m->rttsum/m->rttmsgs;
602                 m->rttsum = m->rttmsgs = 0;
603                 m->lastrtt = x;
604         }
605
606         *v = rttscale(x);
607         *vmax = Rttmax;
608         *mark = 0;
609 }
610
611 void
612 lostval(Machine *m, long *v, long *vmax, long *mark)
613 {
614         ulong x;
615
616         if(m->rcvdmsgs+m->lostmsgs > 0)
617                 x = (m->lostavg>>1) + (((m->lostmsgs*100)/(m->lostmsgs + m->rcvdmsgs))>>1);
618         else
619                 x = m->lostavg;
620         m->lostavg = x;
621         m->lostmsgs = m->rcvdmsgs = 0;
622
623         if(m->unreachable){
624                 m->unreachable = 0;
625                 *mark = 100;
626         } else
627                 *mark = 0;
628
629         *v = x;
630         *vmax = 100;
631 }
632
633 void
634 usage(void)
635 {
636         fprint(2, "usage: %s machine [machine...]\n", argv0);
637         exits("usage");
638 }
639
640 void
641 addgraph(int n)
642 {
643         Graph *g, *ograph;
644         int i, j;
645         static int nadd;
646
647         if(n > nelem(menu2str))
648                 abort();
649         /* avoid two adjacent graphs of same color */
650         if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
651                 nadd++;
652         ograph = graph;
653         graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
654         for(i=0; i<nmach; i++)
655                 for(j=0; j<ngraph; j++)
656                         graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
657         free(ograph);
658         ngraph++;
659         for(i=0; i<nmach; i++){
660                 g = &graph[i*ngraph+(ngraph-1)];
661                 memset(g, 0, sizeof(Graph));
662                 g->label = menu2str[n]+Opwid;
663                 g->newvalue = newvaluefn[n];
664                 g->update = update1;    /* no other update functions yet */
665                 g->mach = &mach[i];
666                 g->colindex = nadd%Ncolor;
667         }
668         present[n] = 1;
669         nadd++;
670 }
671
672 int
673 which2index(int which)
674 {
675         int i, n;
676
677         n = -1;
678         for(i=0; i<ngraph; i++){
679                 if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
680                         n = i;
681                         break;
682                 }
683         }
684         if(n < 0){
685                 fprint(2, "%s: internal error can't drop graph\n", argv0);
686                 killall("error");
687         }
688         return n;
689 }
690
691 int
692 index2which(int index)
693 {
694         int i, n;
695
696         n = -1;
697         for(i=0; i<Nmenu2; i++){
698                 if(strcmp(menu2str[i]+Opwid, graph[index].label) == 0){
699                         n = i;
700                         break;
701                 }
702         }
703         if(n < 0){
704                 fprint(2, "%s: internal error can't identify graph\n", argv0);
705                 killall("error");
706         }
707         return n;
708 }
709
710 void
711 dropgraph(int which)
712 {
713         Graph *ograph;
714         int i, j, n;
715
716         if(which > nelem(menu2str))
717                 abort();
718         /* convert n to index in graph table */
719         n = which2index(which);
720         ograph = graph;
721         graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
722         for(i=0; i<nmach; i++){
723                 for(j=0; j<n; j++)
724                         graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
725                 free(ograph[i*ngraph+j].data);
726                 freeimage(ograph[i*ngraph+j].overtmp);
727                 for(j++; j<ngraph; j++)
728                         graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
729         }
730         free(ograph);
731         ngraph--;
732         present[which] = 0;
733 }
734
735 void
736 addmachine(char *name)
737 {
738         if(ngraph > 0){
739                 fprint(2, "%s: internal error: ngraph>0 in addmachine()\n", argv0);
740                 usage();
741         }
742         if(nmach == NMACH)
743                 sysfatal("too many machines");
744         initmach(&mach[nmach++], name);
745 }
746
747
748 void
749 resize(void)
750 {
751         int i, j, n, startx, starty, x, y, dx, dy, hashdx, ondata;
752         Graph *g;
753         Rectangle machr, r;
754         long v, vmax, mark;
755         char buf[128];
756
757         draw(screen, screen->r, display->white, nil, ZP);
758
759         /* label left edge */
760         x = screen->r.min.x;
761         y = screen->r.min.y + Labspace+font->height+Labspace;
762         dy = (screen->r.max.y - y)/ngraph;
763         dx = Labspace+stringwidth(font, "0")+Labspace;
764         startx = x+dx+1;
765         starty = y;
766         for(i=0; i<ngraph; i++,y+=dy){
767                 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
768                 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
769                 label(Pt(x, y), dy, graph[i].label);
770                 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
771         }
772
773         /* label right edge */
774         dx = Labspace+stringwidth(font, "0.001")+Labspace;
775         hashdx = dx;
776         x = screen->r.max.x - dx;
777         y = screen->r.min.y + Labspace+font->height+Labspace;
778         for(i=0; i<ngraph; i++,y+=dy){
779                 draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
780                 draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
781                 hashmarks(Pt(x, y), dy, i);
782                 draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
783         }
784
785         /* label top edge */
786         dx = (screen->r.max.x - dx - startx)/nmach;
787         for(x=startx, i=0; i<nmach; i++,x+=dx){
788                 draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
789                 j = dx/stringwidth(font, "0");
790                 n = mach[i].nproc;
791                 if(n>1 && j>=1+3+(n>10)+(n>100)){       /* first char of name + (n) */
792                         j -= 3+(n>10)+(n>100);
793                         if(j <= 0)
794                                 j = 1;
795                         snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].name, n);
796                 }else
797                         snprint(buf, sizeof buf, "%.*s", j, mach[i].name);
798                 string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP,
799                         font, buf);
800         }
801         /* draw last vertical line */
802         draw(screen,
803                 Rect(screen->r.max.x-hashdx-1, starty-1, screen->r.max.x-hashdx, screen->r.max.y),
804                 display->black, nil, ZP);
805
806         /* create graphs */
807         for(i=0; i<nmach; i++){
808                 machr = Rect(startx+i*dx, starty, screen->r.max.x, screen->r.max.y);
809                 if(i < nmach-1)
810                         machr.max.x = startx+(i+1)*dx - 1;
811                 else
812                         machr.max.x = screen->r.max.x - hashdx - 1;
813                 y = starty;
814                 for(j=0; j<ngraph; j++, y+=dy){
815                         g = &graph[i*ngraph+j];
816                         /* allocate data */
817                         ondata = g->ndata;
818                         g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
819                         g->data = erealloc(g->data, g->ndata*sizeof(long));
820                         if(g->ndata > ondata)
821                                 memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(long));
822                         /* set geometry */
823                         g->r = machr;
824                         g->r.min.y = y;
825                         g->r.max.y = y+dy - 1;
826                         if(j == ngraph-1)
827                                 g->r.max.y = screen->r.max.y;
828                         draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
829                         g->overflow = 0;
830                         *g->msg = 0;
831                         freeimage(g->overtmp);
832                         g->overtmp = nil;
833                         g->overtmplen = 0;
834                         r = g->r;
835                         r.max.y = r.min.y+font->height;
836                         n = (g->r.max.x - r.min.x)/stringwidth(font, "9");
837                         if(n > 4){
838                                 if(n > Gmsglen)
839                                         n = Gmsglen;
840                                 r.max.x = r.min.x+stringwidth(font, "9")*n;
841                                 g->overtmplen = n;
842                                 g->overtmp = allocimage(display, r, screen->chan, 0, -1);
843                         }
844                         g->newvalue(g->mach, &v, &vmax, &mark);
845                         redraw(g, vmax);
846                 }
847         }
848
849         flushimage(display, 1);
850 }
851
852 void
853 eresized(int new)
854 {
855         lockdisplay(display);
856         if(new && getwindow(display, Refnone) < 0) {
857                 fprint(2, "%s: can't reattach to window\n", argv0);
858                 killall("reattach");
859         }
860         resize();
861         unlockdisplay(display);
862 }
863
864 void
865 dobutton2(Mouse *m)
866 {
867         int i;
868
869         for(i=0; i<Nmenu2; i++)
870                 if(present[i])
871                         memmove(menu2str[i], "drop ", Opwid);
872                 else
873                         memmove(menu2str[i], "add  ", Opwid);
874         i = emenuhit(3, m, &menu2);
875         if(i >= 0){
876                 if(!present[i])
877                         addgraph(i);
878                 else if(ngraph > 1)
879                         dropgraph(i);
880                 resize();
881         }
882 }
883
884 void
885 dobutton1(Mouse *m)
886 {
887         int i, n, dx, dt;
888         Graph *g;
889         char *e;
890         double f;
891
892         for(i = 0; i < ngraph*nmach; i++){
893                 if(ptinrect(m->xy, graph[i].r))
894                         break;
895         }
896         if(i == ngraph*nmach)
897                 return;
898
899         g = &graph[i];
900         if(g->overtmp == nil)
901                 return;
902
903         /* clear any previous message and cursor */
904         if(g->overflow || *g->msg){
905                 clearmsg(g);
906                 *g->msg = 0;
907                 clearcursor(g);
908         }
909
910         dx = g->r.max.x - m->xy.x;
911         g->cursor = dx;
912         dt = dx*pinginterval;
913         e = &g->msg[sizeof(g->msg)];
914         seprint(g->msg, e, "%s", ctime(starttime-dt/1000)+11);
915         g->msg[8] = 0;
916         n = 8;
917
918         switch(index2which(i)){
919         case Mrtt:
920                 f = rttunscale(g->data[dx]);
921                 seprint(g->msg+n, e, " %3.3g", f/1000000);
922                 break;
923         case Mlost:
924                 seprint(g->msg+n, e, " %ld%%", g->data[dx]);
925                 break;
926         }
927
928         drawmsg(g, g->msg);
929         drawcursor(g, m->xy.x);
930 }
931
932 void
933 mouseproc(void*)
934 {
935         Mouse mouse;
936
937         for(;;){
938                 mouse = emouse();
939                 if(mouse.buttons == 4){
940                         lockdisplay(display);
941                         dobutton2(&mouse);
942                         unlockdisplay(display);
943                 } else if(mouse.buttons == 1){
944                         lockdisplay(display);
945                         dobutton1(&mouse);
946                         unlockdisplay(display);
947                 }
948         }
949 }
950
951 void
952 startproc(void (*f)(void*), void *arg)
953 {
954         int pid;
955
956         switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
957         case -1:
958                 fprint(2, "%s: fork failed: %r\n", argv0);
959                 killall("fork failed");
960         case 0:
961                 f(arg);
962                 killall("process died");
963                 exits(nil);
964         }
965         pids[npid++] = pid;
966 }
967
968 void
969 main(int argc, char *argv[])
970 {
971         int i, j;
972         long v, vmax, mark;
973         char flags[10], *f, *p;
974
975         fmtinstall('V', eipfmt);
976
977         f = flags;
978         pinginterval = 5000;            /* 5 seconds */
979         ARGBEGIN{
980         case 'i':
981                 p = ARGF();
982                 if(p == nil)
983                         usage();
984                 pinginterval = atoi(p);
985                 break;
986         default:
987                 if(f - flags >= sizeof(flags)-1)
988                         usage();
989                 *f++ = ARGC();
990                 break;
991         }ARGEND
992         *f = 0;
993
994         for(i=0; i<argc; i++)
995                 addmachine(argv[i]);
996
997         for(f = flags; *f; f++)
998                 switch(*f){
999                 case 'l':
1000                         addgraph(Mlost);
1001                         break;
1002                 case 'r':
1003                         addgraph(Mrtt);
1004                         break;
1005                 }
1006
1007         if(nmach == 0)
1008                 usage();
1009
1010         if(ngraph == 0)
1011                 addgraph(Mrtt);
1012
1013         for(i=0; i<nmach; i++)
1014                 for(j=0; j<ngraph; j++)
1015                         graph[i*ngraph+j].mach = &mach[i];
1016
1017         if(initdraw(nil, nil, argv0) < 0){
1018                 fprint(2, "%s: initdraw failed: %r\n", argv0);
1019                 exits("initdraw");
1020         }
1021         colinit();
1022         einit(Emouse);
1023         startproc(mouseproc, 0);
1024         display->locking = 1;   /* tell library we're using the display lock */
1025
1026         resize();
1027
1028         starttime = time(0);
1029
1030         unlockdisplay(display); /* display is still locked from initdraw() */
1031         for(j = 0; ; j++){
1032                 lockdisplay(display);
1033                 if(j == nmach){
1034                         parity = 1-parity;
1035                         j = 0;
1036                         for(i=0; i<nmach*ngraph; i++){
1037                                 graph[i].newvalue(graph[i].mach, &v, &vmax, &mark);
1038                                 graph[i].update(&graph[i], v, vmax, mark);
1039                         }
1040                         starttime = time(0);
1041                 }
1042                 flushimage(display, 1);
1043                 unlockdisplay(display);
1044                 pingsend(&mach[j%nmach]);
1045                 sleep(pinginterval/nmach);
1046         }
1047 }