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