1 | #include <stdio.h>
|
---|
2 | #include <unistd.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <errno.h>
|
---|
5 | #include <string.h>
|
---|
6 | #include <sys/socket.h>
|
---|
7 | #include <netinet/in.h>
|
---|
8 | #include <arpa/inet.h>
|
---|
9 | #include <netdb.h>
|
---|
10 | #include <sys/types.h>
|
---|
11 | #include <sys/stat.h>
|
---|
12 |
|
---|
13 | #define PORT 8899
|
---|
14 |
|
---|
15 | int plottype;
|
---|
16 | char* setupfile;
|
---|
17 | char* format;
|
---|
18 |
|
---|
19 | char* title[]={"w0", "r0", "w1", "r1"};
|
---|
20 |
|
---|
21 | struct speed {
|
---|
22 | struct speed *prev, *next;
|
---|
23 | int size, num;
|
---|
24 | float f[1];
|
---|
25 | };
|
---|
26 |
|
---|
27 | struct speed* speed;
|
---|
28 |
|
---|
29 | static int
|
---|
30 | xrecv(int s, int n, int* v)
|
---|
31 | {
|
---|
32 | int res, rest=n*4;
|
---|
33 | char* p=(char*)v;
|
---|
34 | while (rest) {
|
---|
35 | res=recv(s, p, rest, 0);
|
---|
36 | if (res<0) {
|
---|
37 | if (errno!=EINTR) {
|
---|
38 | perror("recv");
|
---|
39 | return -1;
|
---|
40 | } else
|
---|
41 | res=0;
|
---|
42 | } else if (res==0) {
|
---|
43 | fprintf(stderr, "no more data\n");
|
---|
44 | return -1;
|
---|
45 | }
|
---|
46 | rest-=res;
|
---|
47 | p+=res;
|
---|
48 | }
|
---|
49 | return 0;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static int
|
---|
53 | add_entry(struct speed* e)
|
---|
54 | {
|
---|
55 | struct speed *prev, *next;
|
---|
56 | /* original e->f[i] is measured in Bytes/s */
|
---|
57 | /* size is in terms of Words */
|
---|
58 | switch (plottype) {
|
---|
59 | case 0:
|
---|
60 | break;
|
---|
61 | case 1: {
|
---|
62 | int i;
|
---|
63 | for (i=e->num-1; i>=0; i--) {
|
---|
64 | e->f[i]=4./e->f[i];
|
---|
65 | }
|
---|
66 | }
|
---|
67 | break;
|
---|
68 | case 2: {
|
---|
69 | int i;
|
---|
70 | for (i=e->num-1; i>=0; i--) {
|
---|
71 | e->f[i]=(e->size*4.)/e->f[i];
|
---|
72 | }
|
---|
73 | }
|
---|
74 | break;
|
---|
75 | default:
|
---|
76 | fprintf(stderr, "Program error; plottype %d not implemented\n",
|
---|
77 | plottype);
|
---|
78 | }
|
---|
79 |
|
---|
80 | next=speed; prev=0;
|
---|
81 |
|
---|
82 | while (next && (next->size<=e->size)) {prev=next; next=next->next;}
|
---|
83 | fprintf(stderr, "size=%d; ", e->size);
|
---|
84 | if (prev)
|
---|
85 | fprintf(stderr, "prev.size=%d; ", prev->size);
|
---|
86 | else
|
---|
87 | fprintf(stderr, "prev=0; ");
|
---|
88 | if (next)
|
---|
89 | fprintf(stderr, "next.size=%d\n", next->size);
|
---|
90 | else
|
---|
91 | fprintf(stderr, "next=0\n");
|
---|
92 |
|
---|
93 | if (next) {
|
---|
94 | e->next=next;
|
---|
95 | next->prev=e;
|
---|
96 | } else
|
---|
97 | e->next=0;
|
---|
98 | if (prev) {
|
---|
99 | e->prev=prev;
|
---|
100 | prev->next=e;
|
---|
101 | } else {
|
---|
102 | e->prev=0;
|
---|
103 | speed=e;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static void
|
---|
110 | dump_speed(void)
|
---|
111 | {
|
---|
112 | struct speed *e=speed;
|
---|
113 | fprintf(stderr, "---------------\n");
|
---|
114 | while (e) {
|
---|
115 | int i;
|
---|
116 | fprintf(stderr, "%6d", e->size);
|
---|
117 | for (i=0; i<e->num; i++)
|
---|
118 | fprintf(stderr, " %12.2f\n", e->f[i]);
|
---|
119 | fprintf(stderr, "\n");
|
---|
120 | e=e->next;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int
|
---|
125 | setup_plot(char* name)
|
---|
126 | {
|
---|
127 | struct stat buf;
|
---|
128 | static time_t last_mtime=0;
|
---|
129 |
|
---|
130 | if (stat(name, &buf)<0) {
|
---|
131 | fprintf(stderr, "cannot stat \"%s\": %s\n", name, strerror(errno));
|
---|
132 | return -1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (buf.st_mtime!=last_mtime) {
|
---|
136 | FILE* f;
|
---|
137 | char s[1024];
|
---|
138 |
|
---|
139 | f=fopen(name, "r");
|
---|
140 | if (!f) {
|
---|
141 | fprintf(stderr, "cannot open \"%s\": %s\n", name, strerror(errno));
|
---|
142 | return -1;
|
---|
143 | }
|
---|
144 | while (fgets(s, 1024, f)) {
|
---|
145 | printf("%s\n", s);
|
---|
146 | }
|
---|
147 | fclose(f);
|
---|
148 | last_mtime=buf.st_mtime;
|
---|
149 | }
|
---|
150 | return 0;
|
---|
151 | }
|
---|
152 |
|
---|
153 | static void
|
---|
154 | plot_it(void)
|
---|
155 | {
|
---|
156 | struct speed *e;
|
---|
157 | int i;
|
---|
158 |
|
---|
159 | if (!speed) return;
|
---|
160 |
|
---|
161 | setup_plot(setupfile);
|
---|
162 |
|
---|
163 | printf("plot ");
|
---|
164 | for (i=0; i<speed->num; i++)
|
---|
165 | printf("\"-\" us 1:2 title '%s' w l%s",
|
---|
166 | title[i], (i<speed->num-1)?", ":"\n");
|
---|
167 |
|
---|
168 | for (i=0; i<speed->num; i++) {
|
---|
169 | e=speed;
|
---|
170 | while (e) {
|
---|
171 | printf(format, e->size, e->f[i]);
|
---|
172 | e=e->next;
|
---|
173 | }
|
---|
174 | printf("e\n\n");
|
---|
175 | }
|
---|
176 | fflush(stderr);
|
---|
177 | }
|
---|
178 |
|
---|
179 | static void
|
---|
180 | init_plot(void)
|
---|
181 | {
|
---|
182 | printf("set term x11\n");
|
---|
183 | switch (plottype) {
|
---|
184 | case 0:
|
---|
185 | printf("set title \"Throughput\"\n");
|
---|
186 | printf("set xlabel \"Size/words\"\n");
|
---|
187 | printf("set ylabel \"Byte/s\"\n");
|
---|
188 | printf("plot \"-\" with points\n");
|
---|
189 | printf("0 0\n1 1\ne\n\n");
|
---|
190 | break;
|
---|
191 | case 1:
|
---|
192 | printf("set title \"Seconds/Word\"\n");
|
---|
193 | printf("set xlabel \"Size/words\"\n");
|
---|
194 | printf("set ylabel \"s/word\"\n");
|
---|
195 | printf("plot \"-\" with points\n");
|
---|
196 | printf("0 0\n1 0\ne\n\n");
|
---|
197 | break;
|
---|
198 | case 2:
|
---|
199 | printf("set title \"Duration\"\n");
|
---|
200 | printf("set xlabel \"Size/words\"\n");
|
---|
201 | printf("set ylabel \"s\"\n");
|
---|
202 | printf("plot \"-\" with points\n");
|
---|
203 | printf("0 0\n1 1\ne\n\n");
|
---|
204 | break;
|
---|
205 | default:
|
---|
206 | fprintf(stderr, "Program error; plottype %d not implemented\n",
|
---|
207 | plottype);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | static void
|
---|
212 | printusage(int argc, char* argv[])
|
---|
213 | {
|
---|
214 | fprintf(stderr, "usage: %s [-s setupfile] [-t type] | gnuplot\n", argv[0]);
|
---|
215 | fprintf(stderr, " -t: 0: bytes/s (default)\n"
|
---|
216 | " 1: s/word\n"
|
---|
217 | " 2: s (absolute)\n");
|
---|
218 | fprintf(stderr, " -s: setupfile contains arbitrary gnuplot commands\n"
|
---|
219 | " it is reread before plotting each ntuple\n"
|
---|
220 | " default is 'gnuplot.ini'\n");
|
---|
221 | }
|
---|
222 |
|
---|
223 | static int
|
---|
224 | getoptions(int argc, char* argv[])
|
---|
225 | {
|
---|
226 | extern char *optarg;
|
---|
227 | extern int optind;
|
---|
228 | int errflag, c;
|
---|
229 | const char* args="t:s:";
|
---|
230 |
|
---|
231 | setupfile="gnuplot.ini";
|
---|
232 | plottype=0;
|
---|
233 |
|
---|
234 | optarg=0; errflag=0;
|
---|
235 |
|
---|
236 | while (!errflag && ((c=getopt(argc, argv, args))!=-1)) {
|
---|
237 | switch (c) {
|
---|
238 | case 't': plottype=atoi(optarg); break;
|
---|
239 | case 's': setupfile=optarg; break;
|
---|
240 | default: errflag=1;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (errflag || optind!=argc) {
|
---|
245 | printusage(argc, argv);
|
---|
246 | return -1;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if ((plottype<0) || (plottype>2)) {
|
---|
250 | fprintf(stderr, "plottype %d not known\n", plottype);
|
---|
251 | return -1;
|
---|
252 | }
|
---|
253 |
|
---|
254 | return 0;
|
---|
255 | }
|
---|
256 |
|
---|
257 | int main(int argc, char* argv[])
|
---|
258 | {
|
---|
259 | int s, ns;
|
---|
260 | struct sockaddr_in addr;
|
---|
261 | struct sockaddr caddr;
|
---|
262 | struct in_addr in_addr;
|
---|
263 | int tmp, res;
|
---|
264 |
|
---|
265 | if (getoptions(argc, argv)<0) return 1;
|
---|
266 | switch (plottype) {
|
---|
267 | case 0:
|
---|
268 | format="%6d %12.2f\n";
|
---|
269 | break;
|
---|
270 | case 1:
|
---|
271 | format="%6d %g\n";
|
---|
272 | break;
|
---|
273 | case 2:
|
---|
274 | format="%6d %g\n";
|
---|
275 | break;
|
---|
276 | default:
|
---|
277 | fprintf(stderr, "Program error; plottype %d not implemented\n",
|
---|
278 | plottype);
|
---|
279 | }
|
---|
280 |
|
---|
281 | res=setvbuf(stdout, 0, _IONBF, 0);
|
---|
282 | if (res<0) {perror("setvbuf"); return 1;}
|
---|
283 |
|
---|
284 | speed=0;
|
---|
285 |
|
---|
286 | init_plot();
|
---|
287 |
|
---|
288 | bzero(&addr, sizeof(struct sockaddr_in));
|
---|
289 | bzero(&caddr, sizeof(struct sockaddr));
|
---|
290 | addr.sin_family=AF_INET;
|
---|
291 | addr.sin_port=htons(PORT);
|
---|
292 | addr.sin_addr.s_addr=INADDR_ANY;
|
---|
293 | s=socket(addr.sin_family, SOCK_STREAM, 0);
|
---|
294 | if (s<0)
|
---|
295 | {perror("socket"); return 1;}
|
---|
296 | if (bind(s, (struct sockaddr*)&addr, sizeof(struct sockaddr_in))<0)
|
---|
297 | {perror("bind"); return 1;}
|
---|
298 | if (listen(s, 1)<0) {perror("listen"); return 1;}
|
---|
299 | tmp=sizeof(struct sockaddr_in);
|
---|
300 | ns=accept(s, (struct sockaddr*)&addr, &tmp);
|
---|
301 | if (ns<0) {perror("accept"); return 1;}
|
---|
302 | in_addr.s_addr=addr.sin_addr.s_addr;
|
---|
303 | fprintf(stderr, "%s accepted\n", inet_ntoa(in_addr));
|
---|
304 |
|
---|
305 | do {
|
---|
306 | int n, i;
|
---|
307 | struct speed *e;
|
---|
308 |
|
---|
309 | if (xrecv(ns, 1, &n)<0) return 1;
|
---|
310 |
|
---|
311 | e=malloc(sizeof(struct speed)+(n-1)*sizeof(float));
|
---|
312 | if (!e) {
|
---|
313 | perror("malloc");
|
---|
314 | return -1;
|
---|
315 | }
|
---|
316 | e->num=n;
|
---|
317 |
|
---|
318 | if (xrecv(ns, 1, &e->size)<0) return 1;
|
---|
319 | for (i=0; i<n; i++) {
|
---|
320 | if (xrecv(ns, 1, (int*)(e->f+i))<0) return 1;
|
---|
321 | }
|
---|
322 | add_entry(e);
|
---|
323 | plot_it();
|
---|
324 | } while (1);
|
---|
325 |
|
---|
326 | return 0;
|
---|
327 | }
|
---|