]> git.sesse.net Git - jam/blob - jam.c
It looks like send() can give us EAGAIN even if epoll() said the socket
[jam] / jam.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <getopt.h>
6 #include <netdb.h>
7 #include <unistd.h>
8 #include <pthread.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <sys/epoll.h>
12 #include <sys/ioctl.h>
13 #include <errno.h>
14
15 unsigned short port = 2007;
16
17 struct in_addr *destinations = NULL;
18 unsigned num_destinations = 0;
19 unsigned room_destinations = 0;
20
21 struct in_addr *sources = NULL;
22 unsigned num_sources = 0;
23 unsigned room_sources = 0;
24
25 unsigned num_senders = 128;
26 unsigned num_sockets_per_sender = 16;
27 unsigned do_listen = 1;
28
29 unsigned long long total_bytes_received = 0;
30 pthread_mutex_t receive_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32 unsigned long long total_bytes_sent = 0;
33 pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
34
35 const static struct option longopts[] = {
36         { "source-list", required_argument, NULL, 's' },
37         { "destination-list", required_argument, NULL, 'd' },
38         { "num-senders", required_argument, NULL, 'n' },
39         { "num-sockets-per-sender", required_argument, NULL, 'N' },
40         { "port", required_argument, NULL, 'p' },
41         { "sender-only", no_argument, NULL, 'o' },
42         { NULL, 0, NULL, 0 }
43 };
44
45 // generates from [0,1>
46 double gen_uniform_random()
47 {
48         return rand() / (RAND_MAX+1.0);
49 }
50
51 double gen_pareto_random(double min, double k)
52 {
53         double u = gen_uniform_random();
54         return min * pow(u, -1.0 / k);
55 }
56
57 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
58 {
59         char buf[256];
60         FILE *in = fopen(filename, "r");
61         if (in == NULL) {
62                 perror(filename);
63                 exit(1);
64         }
65
66         for ( ;; ) {
67                 char *ptr;
68                 struct in_addr addr;
69                 struct hostent *he;
70
71                 if (fgets(buf, 256, in) == NULL)
72                         break;
73
74                 ptr = strchr(buf, '\n');
75                 if (ptr != NULL)
76                         *ptr = 0;
77
78                 ptr = strchr(buf, '\r');
79                 if (ptr != NULL)
80                         *ptr = 0;
81                 
82                 ptr = buf + strspn(buf, " \t");
83
84                 if (ptr[0] == '#' || ptr[0] == 0)
85                         continue;
86
87                 he = gethostbyname(ptr);
88                 if (he == NULL) {
89                         perror(ptr);
90                         exit(1);
91                 }
92
93                 // just pick the first for now
94                 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
95
96                 if (*num >= *room) {
97                         if (*room == 0) {
98                                 *room = 16;
99                         } else {
100                                 *room <<= 1;
101                         }
102                         *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
103                 }
104
105                 (*addr_list)[*num] = addr;
106                 ++*num;
107         }
108
109         fclose(in);
110 }
111
112 void parse_options(int argc, char **argv)
113 {
114         int option_index = 0;
115
116         for ( ;; ) {
117                 int c = getopt_long(argc, argv, "s:d:n:N:p:o", longopts, &option_index); 
118                 switch (c) {
119                 case 's':
120                         read_ip_list(optarg, &sources, &num_sources, &room_sources);
121                         break;
122                 case 'd':
123                         read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
124                         break;
125                 case 'n':
126                         num_senders = atoi(optarg);
127                         break;
128                 case 'N':
129                         num_sockets_per_sender = atoi(optarg);
130                         break;
131                 case 'p':
132                         port = atoi(optarg);
133                         break;
134                 case 'o':
135                         do_listen = 0;
136                         break;
137                 case -1:
138                         return;       // end of argument list
139                 default:
140                         fprintf(stderr, "Invalid option\n");
141                         exit(1);
142                 }
143         }
144 }
145
146 struct sender {
147         int fd;
148         unsigned long long bytes_left;
149 };
150
151 void generate_new_sender(int ep_fd)
152 {
153         int sock, one = 1;
154         unsigned src_num, dst_num;
155         unsigned long long num_bytes;
156         struct sockaddr_in sin;
157         struct epoll_event ev;
158         struct sender *s;
159         
160         s = (struct sender *)malloc(sizeof(struct sender));
161         if (s == NULL) {
162                 perror("malloc()");
163                 exit(1);
164         }
165
166         sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
167         if (sock == -1) {
168                 perror("socket()");
169                 exit(1);
170         }
171
172         // find the right parameters
173         src_num = (unsigned)(num_sources * gen_uniform_random());
174         dst_num = (unsigned)(num_destinations * gen_uniform_random());
175         num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
176
177         // FIXME: bind to the right source
178
179         if (ioctl(sock, FIONBIO, &one) == -1) {
180                 perror("FIONBIO");
181                 exit(1);
182         }
183
184         sin.sin_family = AF_INET;
185         sin.sin_port = htons(port);
186         sin.sin_addr = destinations[dst_num];
187
188         // non-blocking connect (will be detected by epoll later)
189         if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 && errno != EINPROGRESS) {
190                 perror("connect()");
191                 exit(1);
192         }
193         
194         // stick it in the epoll set (FIXME: make edge-triggered?)
195         ev.events = EPOLLOUT | EPOLLHUP | EPOLLERR;
196         ev.data.ptr = s;
197
198         s->fd = sock;
199         s->bytes_left = num_bytes;
200
201         if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
202                 perror("EPOLL_CTL_ADD");
203                 exit(1);
204         }
205 }
206
207 void *sender_worker(void *arg)
208 {
209         unsigned i;
210         char buf[65536];
211         unsigned long long bytes_sent = 0;
212         int ep_fd;
213         struct epoll_event *events;
214
215         ep_fd = epoll_create(num_sockets_per_sender);
216         if (ep_fd == -1) {
217                 perror("epoll_create");
218                 exit(1);
219         }
220
221         // malloc, since there might not be enough room on the stack
222         events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * num_sockets_per_sender);
223         if (events == NULL) {
224                 perror("malloc");
225                 exit(1);
226         }
227
228         // fill the buffer with random junk
229         for (i = 0; i < 65536; ++i)
230                 buf[i] = rand() & 0xff;
231
232         // allocate all the senders
233         for (i = 0; i < num_sockets_per_sender; ++i)
234                 generate_new_sender(ep_fd);
235
236         for ( ;; ) {
237                 int num_active = epoll_wait(ep_fd, events, num_sockets_per_sender, -1);
238                 if (num_active == -1) {
239                         perror("epoll_wait");
240                         exit(1);
241                 }
242
243                 for (i = 0; i < num_active; ++i) {
244                         struct sender *s = (struct sender *)events[i].data.ptr;
245                         unsigned long long bytes_to_send = s->bytes_left;
246                         unsigned ret;
247
248                         if (bytes_to_send > 65536) {
249                                 bytes_to_send = 65536;  
250                         }
251
252                         ret = send(s->fd, buf, bytes_to_send, MSG_NOSIGNAL);
253                         if (ret == -1) {
254                                 if (errno == EAGAIN)
255                                         continue;
256
257                                 perror("send()");
258                                 exit(1);
259                         }
260
261                         s->bytes_left -= ret;
262                         bytes_sent += ret;
263
264                         // update the central counter after every 1MB (8ms
265                         // at gigabit speeds, should be enough) of sent data
266                         if (bytes_sent > 1048576) {
267                                 pthread_mutex_lock(&send_mutex);
268                                 total_bytes_sent += bytes_sent;
269                                 pthread_mutex_unlock(&send_mutex);
270
271                                 bytes_sent = 0;
272                         }
273                         
274                         if (s->bytes_left == 0) {
275                                 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, s->fd, NULL) == -1) {
276                                         perror("EPOLL_CTL_ADD");
277                                         exit(1);
278                                 }
279                                 close(s->fd);
280
281                                 free(s);
282                                 generate_new_sender(ep_fd);
283                         }
284                 }
285         }
286
287         pthread_mutex_lock(&send_mutex);
288         total_bytes_sent += bytes_sent;
289         pthread_mutex_unlock(&send_mutex);
290
291         free(events);
292         close(ep_fd);
293
294         pthread_exit(0);
295 }
296
297 void *receiver_worker(void *arg)
298 {
299         int sock = (int)arg;
300         char buf[65536];
301         unsigned long long bytes_received = 0;
302
303         for ( ;; ) {
304                 int ret = read(sock, buf, 65536);
305                 if (ret == 0)
306                         break;
307
308                 bytes_received += ret;
309
310                 // update the central counter after every 1MB (8ms
311                 // at gigabit speeds, should be enough) of received data
312                 if (bytes_received > 1048576) {
313                         pthread_mutex_lock(&receive_mutex);
314                         total_bytes_received += bytes_received;
315                         pthread_mutex_unlock(&receive_mutex);
316
317                         bytes_received = 0;
318                 }
319         }
320         
321         if (close(sock) == -1) {
322                 perror("close()");
323                 exit(1);
324         }
325                         
326         pthread_mutex_lock(&receive_mutex);
327         total_bytes_received += bytes_received;
328         pthread_mutex_unlock(&receive_mutex);
329
330         pthread_exit(0);
331 }
332         
333 void *receiver_dispatcher(void *arg)
334 {
335         int server_sock = (int)arg;
336         pthread_attr_t attr;
337         
338         // FIXME: these do not really set errno
339         if (pthread_attr_init(&attr) != 0) {
340                 perror("pthread_attr_init()");
341                 exit(1);
342         }
343
344         if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
345                 perror("pthread_attr_setstacksize");
346                 exit(1);
347         }
348
349         if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
350                 perror("pthread_attr_setdetachstate");
351                 exit(1);
352         }
353
354         /*
355          * Listen for incoming connections, spawning off one receiver
356          * thread for each (which will just gobble up the data until
357          * we're done).
358          */
359         for ( ;; ) {
360                 struct sockaddr_in addr;
361                 socklen_t addr_len = sizeof(addr);
362                 pthread_t thread;
363
364                 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
365                 if (sock == -1) {
366                         perror("accept()");
367                         exit(1);
368                 }
369
370                 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
371                         perror("pthread_create()");
372                         exit(1);
373                 }
374         }
375 }
376
377 int get_server_socket(unsigned short port)
378 {
379         int server_sock;
380         struct sockaddr_in sin;
381         unsigned one = 1;
382
383         server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
384         if (server_sock == -1) {
385                 perror("socket()");
386                 exit(1);
387         }
388
389         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
390                 perror("setsocket(SO_REUSEADDR)");
391                 exit(1);
392         }
393
394         sin.sin_family = AF_INET;
395         sin.sin_port = htons(port);
396         sin.sin_addr.s_addr = INADDR_ANY;
397
398         if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
399                 perror("bind()");
400                 exit(1);
401         }
402
403         if (listen(server_sock, 255) == -1) {
404                 perror("listen()");
405                 exit(1);
406         }
407
408         return server_sock;
409 }
410
411 int main(int argc, char **argv)
412 {
413         int server_sock;
414         unsigned i;
415         pthread_attr_t attr;
416
417         parse_options(argc, argv);
418
419         if (destinations == NULL || sources == NULL) {
420                 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
421                 exit(1);
422         }
423
424         if (do_listen) {
425                 server_sock = get_server_socket(port);
426         }
427         
428         printf("Sending data on port %u from %u sources to %u destinations.\n\n",
429                 port, num_sources, num_destinations);
430                 
431         // FIXME: these do not really set errno
432         if (pthread_attr_init(&attr) != 0) {
433                 perror("pthread_attr_init()");
434                 exit(1);
435         }
436
437         if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
438                 perror("pthread_attr_setstacksize");
439                 exit(1);
440         }
441
442         if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
443                 perror("pthread_attr_setdetachstate");
444                 exit(1);
445         }
446                 
447         // Fire off the master receiver.
448         if (do_listen) {
449                 pthread_t thread;
450                 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
451                         perror("pthread_create()");
452                         exit(1);
453                 }
454         }
455
456         printf("Waiting five seconds before starting senders...\n");
457         sleep(5);
458
459         // Fire off sender workers.
460         for (i = 0; i < num_senders; ++i) {
461                 pthread_t thread;
462                 
463                 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
464                         perror("pthread_create()");
465                         exit(1);
466                 }
467         }
468
469         // Just stay around collecting statistics until we're done.
470         for ( ;; ) {
471                 unsigned long long sent, received;
472                 static unsigned long long last_sent = 0, last_received = 0;
473                 double recv_rate, send_rate;
474
475                 pthread_mutex_lock(&send_mutex);
476                 sent = total_bytes_sent;
477                 pthread_mutex_unlock(&send_mutex);
478                 
479                 pthread_mutex_lock(&receive_mutex);
480                 received = total_bytes_received;
481                 pthread_mutex_unlock(&receive_mutex);
482
483                 send_rate = (sent - last_sent) * 8.0 / 1048576.0;       
484                 recv_rate = (received - last_received) * 8.0 / 1048576.0;       
485                 
486                 printf("%12llu %12llu  %5.0f Mbit/sec  %5.0f Mbit/sec\n", sent, received,
487                         send_rate, recv_rate);
488
489                 last_sent = sent;
490                 last_received = received;
491
492                 sleep(1);
493         }
494
495         exit(0);
496 }