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