9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <sys/epoll.h>
12 #include <sys/ioctl.h>
15 unsigned short port = 2007;
16 unsigned update_frequency = 1048576;
18 struct in_addr *destinations = NULL;
19 unsigned num_destinations = 0;
20 unsigned room_destinations = 0;
22 struct in_addr *sources = NULL;
23 unsigned num_sources = 0;
24 unsigned room_sources = 0;
26 unsigned num_senders = 128;
27 unsigned num_sockets_per_sender = 16;
28 unsigned num_receivers = 128;
29 unsigned do_listen = 1;
31 // quite arbitrary, should probably be a bit higher than num_sockets_per_sender
32 unsigned epoll_room_in_receiver = 128;
34 unsigned long long total_bytes_received = 0;
35 pthread_mutex_t receive_mutex = PTHREAD_MUTEX_INITIALIZER;
37 unsigned long long total_bytes_sent = 0;
38 pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
40 const static struct option longopts[] = {
41 { "source-list", required_argument, NULL, 's' },
42 { "destination-list", required_argument, NULL, 'd' },
43 { "num-senders", required_argument, NULL, 'n' },
44 { "num-sockets-per-sender", required_argument, NULL, 'N' },
45 { "port", required_argument, NULL, 'p' },
46 { "sender-only", no_argument, NULL, 'o' },
50 // generates from [0,1>
51 double gen_uniform_random()
53 return rand() / (RAND_MAX+1.0);
56 double gen_pareto_random(double min, double k)
58 double u = gen_uniform_random();
59 return min * pow(u, -1.0 / k);
62 void read_ip_list(char *filename, struct in_addr **addr_list, unsigned *num, unsigned *room)
65 FILE *in = fopen(filename, "r");
76 if (fgets(buf, 256, in) == NULL)
79 ptr = strchr(buf, '\n');
83 ptr = strchr(buf, '\r');
87 ptr = buf + strspn(buf, " \t");
89 if (ptr[0] == '#' || ptr[0] == 0)
92 he = gethostbyname(ptr);
98 // just pick the first for now
99 memcpy(&addr.s_addr, he->h_addr_list[0], sizeof(addr.s_addr));
107 *addr_list = (struct in_addr *)realloc(*addr_list, *room * sizeof(struct in_addr));
110 (*addr_list)[*num] = addr;
117 void parse_options(int argc, char **argv)
119 int option_index = 0;
122 int c = getopt_long(argc, argv, "s:d:n:N:p:o", longopts, &option_index);
125 read_ip_list(optarg, &sources, &num_sources, &room_sources);
128 read_ip_list(optarg, &destinations, &num_destinations, &room_destinations);
131 num_senders = atoi(optarg);
134 num_sockets_per_sender = atoi(optarg);
143 return; // end of argument list
145 fprintf(stderr, "Invalid option\n");
153 unsigned long long bytes_left;
156 void generate_new_sender(int ep_fd, struct sender *s)
159 unsigned src_num, dst_num;
160 unsigned long long num_bytes;
161 struct sockaddr_in sin;
162 struct epoll_event ev;
164 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
170 // find the right parameters
171 src_num = (unsigned)(num_sources * gen_uniform_random());
172 dst_num = (unsigned)(num_destinations * gen_uniform_random());
173 num_bytes = (unsigned)gen_pareto_random(1048576.0, 1.0);
175 // FIXME: bind to the right source
177 if (ioctl(sock, FIONBIO, &one) == -1) {
182 sin.sin_family = AF_INET;
183 sin.sin_port = htons(port);
184 sin.sin_addr = destinations[dst_num];
186 // non-blocking connect (will be detected by epoll later)
187 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 && errno != EINPROGRESS) {
192 // stick it in the epoll set (FIXME: make edge-triggered?)
193 ev.events = EPOLLOUT | EPOLLHUP | EPOLLERR;
197 s->bytes_left = num_bytes;
199 if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
200 perror("EPOLL_CTL_ADD");
205 void *sender_worker(void *arg)
209 unsigned long long bytes_sent = 0;
211 struct epoll_event *events;
213 ep_fd = epoll_create(num_sockets_per_sender);
215 perror("epoll_create");
219 // malloc, since there might not be enough room on the stack
220 events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * num_sockets_per_sender);
221 if (events == NULL) {
226 // fill the buffer with random junk
227 for (i = 0; i < 65536; ++i)
228 buf[i] = rand() & 0xff;
230 // allocate all the senders
231 for (i = 0; i < num_sockets_per_sender; ++i) {
232 struct sender *s = (struct sender *)malloc(sizeof(struct sender));
238 generate_new_sender(ep_fd, s);
242 int num_active = epoll_wait(ep_fd, events, num_sockets_per_sender, -1);
243 if (num_active == -1) {
244 perror("epoll_wait");
248 for (i = 0; i < num_active; ++i) {
249 struct sender *s = (struct sender *)events[i].data.ptr;
250 unsigned long long bytes_to_send = s->bytes_left;
253 if (bytes_to_send > 65536) {
254 bytes_to_send = 65536;
257 ret = send(s->fd, buf, bytes_to_send, MSG_NOSIGNAL);
266 s->bytes_left -= ret;
269 // update the central counter after every 1MB (8ms
270 // at gigabit speeds, should be enough) of sent data
271 if (bytes_sent > update_frequency) {
272 pthread_mutex_lock(&send_mutex);
273 total_bytes_sent += bytes_sent;
274 pthread_mutex_unlock(&send_mutex);
279 if (s->bytes_left == 0) {
280 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, s->fd, NULL) == -1) {
281 perror("EPOLL_CTL_DEL");
285 generate_new_sender(ep_fd, s);
290 pthread_mutex_lock(&send_mutex);
291 total_bytes_sent += bytes_sent;
292 pthread_mutex_unlock(&send_mutex);
300 void *receiver_worker(void *arg)
302 int server_sock = (int)arg;
304 unsigned long long bytes_received = 0;
306 struct epoll_event ev, *events;
308 ep_fd = epoll_create(num_sockets_per_sender);
310 perror("epoll_create");
314 // malloc, since there might not be enough room on the stack
315 events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * epoll_room_in_receiver);
316 if (events == NULL) {
321 // stick the receiver socket in the epoll set
323 ev.data.fd = server_sock;
325 if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, server_sock, &ev) == -1) {
326 perror("EPOLL_CTL_ADD");
331 int num_active = epoll_wait(ep_fd, events, epoll_room_in_receiver, -1);
332 if (num_active == -1) {
333 perror("epoll_wait");
337 for (i = 0; i < num_active; ++i) {
338 int sock = events[i].data.fd;
340 if (sock == server_sock) {
341 struct sockaddr_in addr;
343 socklen_t addr_len = sizeof(addr);
345 sock = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
347 if (errno == EAGAIN) {
348 // another thread snatched it, ignore
356 // add it to the epoll set
357 ev.events = EPOLLIN | EPOLLHUP | EPOLLERR;
360 if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
361 perror("EPOLL_CTL_ADD");
367 ret = read(sock, buf, 65536);
369 if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, sock, NULL) == -1) {
370 perror("EPOLL_CTL_DEL");
377 bytes_received += ret;
379 // update the central counter after every 1MB (8ms
380 // at gigabit speeds, should be enough) of received data
381 if (bytes_received > update_frequency) {
382 pthread_mutex_lock(&receive_mutex);
383 total_bytes_received += bytes_received;
384 pthread_mutex_unlock(&receive_mutex);
391 pthread_mutex_lock(&receive_mutex);
392 total_bytes_received += bytes_received;
393 pthread_mutex_unlock(&receive_mutex);
398 // We're keeping this separate receiver dispatcher around because we might eventually
399 // want to move away from the model where each receiver worker does the accept(). Thus,
400 // it makes sense to keep this dispatcher around, even though all it does at the moment
401 // is spawn off a few new threads and then die itself.
402 void *receiver_dispatcher(void *arg)
404 int server_sock = (int)arg;
409 // FIXME: these do not really set errno
410 if (pthread_attr_init(&attr) != 0) {
411 perror("pthread_attr_init()");
415 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
416 perror("pthread_attr_setstacksize");
420 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
421 perror("pthread_attr_setdetachstate");
425 for (i = 0; i < num_receivers; ++i) {
426 if (pthread_create(&thread, &attr, receiver_worker, (void *)server_sock) != 0) {
427 perror("pthread_create()");
435 int get_server_socket(unsigned short port)
438 struct sockaddr_in sin;
441 server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
442 if (server_sock == -1) {
447 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
448 perror("setsocket(SO_REUSEADDR)");
452 sin.sin_family = AF_INET;
453 sin.sin_port = htons(port);
454 sin.sin_addr.s_addr = INADDR_ANY;
456 if (bind(server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
461 if (listen(server_sock, 255) == -1) {
469 int main(int argc, char **argv)
475 parse_options(argc, argv);
477 if (destinations == NULL || sources == NULL) {
478 fprintf(stderr, "Missing or empty source or destination host list. Aborting.\n");
483 server_sock = get_server_socket(port);
486 printf("Sending data on port %u from %u sources to %u destinations.\n\n",
487 port, num_sources, num_destinations);
489 // FIXME: these do not really set errno
490 if (pthread_attr_init(&attr) != 0) {
491 perror("pthread_attr_init()");
495 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 65536 + 0x4000) != 0) {
496 perror("pthread_attr_setstacksize");
500 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
501 perror("pthread_attr_setdetachstate");
505 // Fire off the master receiver.
508 if (pthread_create(&thread, &attr, receiver_dispatcher, (void *)server_sock) != 0) {
509 perror("pthread_create()");
514 printf("Waiting five seconds before starting senders...\n");
517 // Fire off sender workers.
518 for (i = 0; i < num_senders; ++i) {
521 if (pthread_create(&thread, &attr, sender_worker, NULL) != 0) {
522 perror("pthread_create()");
527 // Just stay around collecting statistics until we're done.
529 unsigned long long sent, received;
530 static unsigned long long last_sent = 0, last_received = 0;
531 double recv_rate, send_rate;
533 pthread_mutex_lock(&send_mutex);
534 sent = total_bytes_sent;
535 pthread_mutex_unlock(&send_mutex);
537 pthread_mutex_lock(&receive_mutex);
538 received = total_bytes_received;
539 pthread_mutex_unlock(&receive_mutex);
541 send_rate = (sent - last_sent) * 8.0 / 1048576.0;
542 recv_rate = (received - last_received) * 8.0 / 1048576.0;
544 printf("%12llu %12llu %5.0f Mbit/sec %5.0f Mbit/sec\n", sent, received,
545 send_rate, recv_rate);
548 last_received = received;