]> git.sesse.net Git - jam/blob - jam.c
epoll_data is a union, not a struct. Doh, no wonder the .fd element was
[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                                 perror("send()");
255                                 exit(1);
256                         }
257
258                         s->bytes_left -= ret;
259                         bytes_sent += ret;
260
261                         // update the central counter after every 1MB (8ms
262                         // at gigabit speeds, should be enough) of sent data
263                         if (bytes_sent > 1048576) {
264                                 pthread_mutex_lock(&send_mutex);
265                                 total_bytes_sent += bytes_sent;
266                                 pthread_mutex_unlock(&send_mutex);
267
268                                 bytes_sent = 0;
269                         }
270                         
271                         if (s->bytes_left == 0) {
272                                 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, s->fd, NULL) == -1) {
273                                         perror("EPOLL_CTL_ADD");
274                                         exit(1);
275                                 }
276                                 close(s->fd);
277
278                                 free(s);
279                                 generate_new_sender(ep_fd);
280                         }
281                 }
282         }
283
284         pthread_mutex_lock(&send_mutex);
285         total_bytes_sent += bytes_sent;
286         pthread_mutex_unlock(&send_mutex);
287
288         free(events);
289         close(ep_fd);
290
291         pthread_exit(0);
292 }
293
294 void *receiver_worker(void *arg)
295 {
296         int sock = (int)arg;
297         char buf[65536];
298         unsigned long long bytes_received = 0;
299
300         for ( ;; ) {
301                 int ret = read(sock, buf, 65536);
302                 if (ret == 0)
303                         break;
304
305                 bytes_received += ret;
306
307                 // update the central counter after every 1MB (8ms
308                 // at gigabit speeds, should be enough) of received data
309                 if (bytes_received > 1048576) {
310                         pthread_mutex_lock(&receive_mutex);
311                         total_bytes_received += bytes_received;
312                         pthread_mutex_unlock(&receive_mutex);
313
314                         bytes_received = 0;
315                 }
316         }
317         
318         if (close(sock) == -1) {
319                 perror("close()");
320                 exit(1);
321         }
322                         
323         pthread_mutex_lock(&receive_mutex);
324         total_bytes_received += bytes_received;
325         pthread_mutex_unlock(&receive_mutex);
326
327         pthread_exit(0);
328 }
329         
330 void *receiver_dispatcher(void *arg)
331 {
332         int server_sock = (int)arg;
333         pthread_attr_t attr;
334         
335         // FIXME: these do not really set errno
336         if (pthread_attr_init(&attr) != 0) {
337                 perror("pthread_attr_init()");
338                 exit(1);
339         }
340
341         if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
342                 perror("pthread_attr_setstacksize");
343                 exit(1);
344         }
345
346         if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
347                 perror("pthread_attr_setdetachstate");
348                 exit(1);
349         }
350
351         /*
352          * Listen for incoming connections, spawning off one receiver
353          * thread for each (which will just gobble up the data until
354          * we're done).
355          */
356         for ( ;; ) {
357                 struct sockaddr_in addr;
358                 socklen_t addr_len = sizeof(addr);
359                 pthread_t thread;
360
361                 int sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
362                 if (sock == -1) {
363                         perror("accept()");
364                         exit(1);
365                 }
366
367                 if (pthread_create(&thread, &attr, receiver_worker, (void *)sock) != 0) {
368                         perror("pthread_create()");
369                         exit(1);
370                 }
371         }
372 }
373
374 int get_server_socket(unsigned short port)
375 {
376         int server_sock;
377         struct sockaddr_in sin;
378         unsigned one = 1;
379
380         server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
381         if (server_sock == -1) {
382                 perror("socket()");
383                 exit(1);
384         }
385
386         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
387                 perror("setsocket(SO_REUSEADDR)");
388                 exit(1);
389         }
390
391         sin.sin_family = AF_INET;
392         sin.sin_port = htons(port);
393         sin.sin_addr.s_addr = INADDR_ANY;
394
395         if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
396                 perror("bind()");
397                 exit(1);
398         }
399
400         if (listen(server_sock, 255) == -1) {
401                 perror("listen()");
402                 exit(1);
403         }
404
405         return server_sock;
406 }
407
408 int main(int argc, char **argv)
409 {
410         int server_sock;
411         unsigned i;
412         pthread_attr_t attr;
413
414         parse_options(argc, argv);
415
416         if (destinations == NULL || sources == NULL) {
417                 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
418                 exit(1);
419         }
420
421         if (do_listen) {
422                 server_sock = get_server_socket(port);
423         }
424         
425         printf("Sending data on port %u from %u sources to %u destinations.\n\n",
426                 port, num_sources, num_destinations);
427                 
428         // FIXME: these do not really set errno
429         if (pthread_attr_init(&attr) != 0) {
430                 perror("pthread_attr_init()");
431                 exit(1);
432         }
433
434         if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
435                 perror("pthread_attr_setstacksize");
436                 exit(1);
437         }
438
439         if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
440                 perror("pthread_attr_setdetachstate");
441                 exit(1);
442         }
443                 
444         // Fire off the master receiver.
445         if (do_listen) {
446                 pthread_t thread;
447                 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
448                         perror("pthread_create()");
449                         exit(1);
450                 }
451         }
452
453         printf("Waiting five seconds before starting senders...\n");
454         sleep(5);
455
456         // Fire off sender workers.
457         for (i = 0; i < num_senders; ++i) {
458                 pthread_t thread;
459                 
460                 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
461                         perror("pthread_create()");
462                         exit(1);
463                 }
464         }
465
466         // Just stay around collecting statistics until we're done.
467         for ( ;; ) {
468                 unsigned long long sent, received;
469                 static unsigned long long last_sent = 0, last_received = 0;
470                 double recv_rate, send_rate;
471
472                 pthread_mutex_lock(&send_mutex);
473                 sent = total_bytes_sent;
474                 pthread_mutex_unlock(&send_mutex);
475                 
476                 pthread_mutex_lock(&receive_mutex);
477                 received = total_bytes_received;
478                 pthread_mutex_unlock(&receive_mutex);
479
480                 send_rate = (sent - last_sent) * 8.0 / 1048576.0;       
481                 recv_rate = (received - last_received) * 8.0 / 1048576.0;       
482                 
483                 printf("%12llu %12llu  %5.0f Mbit/sec  %5.0f Mbit/sec\n", sent, received,
484                         send_rate, recv_rate);
485
486                 last_sent = sent;
487                 last_received = received;
488
489                 sleep(1);
490         }
491
492         exit(0);
493 }