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