]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wclient.c
Added bounds for y axis in plots.
[frescor/fwp.git] / wme_test / wclient.c
1 #include <errno.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <netdb.h>
7 #include <signal.h>
8 #include <sys/wait.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <time.h>
13 #include <string.h>
14 #include <pthread.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdbool.h>
18 #include "common.h"
19 #include <semaphore.h>
20
21 #define MAX_STREAMS  10 
22 #define MIN_GRANULARITY 100
23
24 unsigned opt_packet_size = 800;
25 int opt_send_buf_size = -1;
26 unsigned opt_period_usec = 10*MSEC_TO_USEC;
27 unsigned opt_jitter = 0;
28 char    *opt_output = "delay_stats";
29 unsigned opt_count_sec = 0;
30 unsigned opt_def_bandwidth = 200;
31 unsigned opt_def_period_msec = 0;
32 int opt_granularity_usec = MIN_GRANULARITY;
33 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
34
35 bool some_queue_is_full = false;
36 struct timespec reset_timestamp;
37
38 /* Locked when some queue is full to prevent multiple resets of
39    statstics. */
40 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42 int ac_sockfd[AC_NUM];
43
44 struct receiver {
45         pthread_t thread;
46         unsigned received, last_received;
47 } receivers[AC_NUM];
48
49 FILE* logfd;
50 char* server_addr; 
51 char logfname[100];
52
53 /* maximal traffic delay in ms - 10 s*/
54 #define MAX_DELAY_US 10000000
55
56 unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
57 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
58
59 /*struct ac_stats[AC_NUM] {
60    unsigned long int min_trans_time;
61    unsigned long int sum_trans_time;
62    struct timespec   recv_timestamp;
63    struct timespec   send_timestamp; 
64 };*/
65
66 struct stream {
67         /* Input parameters */
68         enum ac ac;             /*  */
69         int bandwidth_bps;      /* bits per second */
70         int jitter;             /* percent */
71         /* Mulualy exclusive input parameters */
72         int packet_size;
73         long period_usec;       /* all time units are in microseconds */
74         struct sockaddr_in rem_addr;
75
76         /* Statistics */
77         pthread_mutex_t mutex;
78         unsigned long long sent, really_sent, received;
79 };
80
81 /*
82 struct send_endpoint sepoint[] = {
83         { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
84         { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps =  480*Kbit },
85         { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
86         { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
87 //      { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps =  675*Kbit },
88 };
89 */
90
91 struct stream streams[MAX_STREAMS];
92
93 unsigned int nr_streams = 0;
94
95 sem_t sem_thread_finished;
96
97 bool exit_flag = false;
98
99 void stopper()
100 {
101         int i;
102         exit_flag = true;
103
104         /* Interrupt all receivers */
105         for (i=0; i < AC_NUM; i++) {
106                 pthread_kill(receivers[i].thread, SIGUSR1);
107         }
108 }
109
110 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
111 {
112         char buf[3][12];
113         char real[100];
114
115         if (useconds) {
116                 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
117                          bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
118                          stream->sent, stream->sent*SEC_TO_USEC/useconds,
119                          stream->received, stream->received*SEC_TO_USEC/useconds);
120         } else {
121                 real[0]=0;
122         }
123         
124         snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
125                  stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
126                  stream->packet_size, usec_to_text(buf[1], stream->period_usec),
127                  usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
128                  (int)(SEC_TO_USEC/stream->period_usec), real);
129 }
130
131 void save_results(int argc, char *argv[], int useconds)
132 {
133         int ac, i, maxi;
134         const int mini = 3000/opt_granularity_usec;
135         bool allzeros;
136         unsigned sum[AC_NUM];
137         double val;
138
139         fprintf(stderr, "Writing data to %s... ", logfname);
140         fflush(stderr);
141
142         fprintf(logfd, "# Invoked as: ");
143         for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
144         fprintf(logfd, "\n");
145
146         if (useconds/SEC_TO_USEC != opt_count_sec) {
147                 char buf[20];
148                 usec_to_text(buf, useconds);
149                 fprintf(logfd, "# Data gathered for %s.\n", buf);
150         }
151                 
152         for (i = 0; i < nr_streams; i++) {
153                 char stream_desc[200];
154                 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
155                 fprintf(logfd, "# Stream %s\n", stream_desc);
156         }
157
158         /* Find maximal delay */
159         allzeros = true;
160         for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
161                 for (ac = 0; ac < AC_NUM; ac++) {
162                         if (delay_stats[ac][maxi] != 0) allzeros = false;
163                 }
164                 if (!allzeros) break;
165         }
166         maxi++;
167         if (maxi < mini) maxi = mini;
168
169         /* Calculate total number of sent packets per AC */
170         for (ac = 0; ac < AC_NUM; ac++) sum[ac] = 0;
171         for (i = 0; i < nr_streams; i++) {
172                 ac = streams[i].ac;
173                 sum[ac] += streams[i].sent;
174         }
175
176 #if 0
177         /* Write pdf */
178         for ( i = 0 ; i < maxi; i++) {
179                 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
180                 for (ac = 0; ac < AC_NUM; ac++) { 
181                         if (sum[ac])
182                                 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
183                         else val = -1; /* Don't display this ac */
184                         fprintf(logfd," %lf", val);
185                 }
186         }
187         
188         fprintf(logfd,"\n\n");
189 #endif
190         
191         /* Write PDF */
192         for (ac = 0; ac < AC_NUM; ac++) {
193                 unsigned long long integral = 0, last = -1;
194
195                 fprintf(logfd,"%f %f\n", 0.0, 0.0);
196
197                 if (sum[ac] != 0) {
198                         i=0;
199                         while (delay_stats[ac][i] == 0) i++;
200                 
201                         for (i++; i < maxi+1; i++) {
202                                 if (last != integral) {
203                                         val = (double)integral*100.0 / sum[ac];
204                                         fprintf(logfd,"%f %f\n", i*opt_granularity_usec/1000.0, val);
205                                         last = integral;
206                                 }
207                                 if (i>0)
208                                         integral += delay_stats[ac][i-1];
209                         }
210                 }
211                 fprintf(logfd,"\n\n");
212         }
213         
214         fprintf(stderr, "finished.\n");
215         fclose(logfd);
216
217         exit(0);
218 }
219
220 static inline 
221 void timespec_add (struct timespec *sum, const struct timespec *left,
222               const struct timespec *right)
223 {
224         sum->tv_sec = left->tv_sec + right->tv_sec;
225         sum->tv_nsec = left->tv_nsec + right->tv_nsec;
226
227         if (sum->tv_nsec >= 1000000000){
228                 ++sum->tv_sec;
229                 sum->tv_nsec -= 1000000000;
230         }
231 }
232
233 static inline 
234 void timespec_sub (struct timespec *diff, const struct timespec *left,
235               const struct timespec *right)
236 {
237         diff->tv_sec = left->tv_sec - right->tv_sec;
238         diff->tv_nsec = left->tv_nsec - right->tv_nsec;
239
240         if (diff->tv_nsec < 0){
241                   --diff->tv_sec;
242                   diff->tv_nsec += 1000000000;
243         }
244 }
245
246 static inline long long timespec_sub_usec(const struct timespec *left,
247                                           const struct timespec *right)
248 {
249         struct timespec result;
250         timespec_sub(&result, left, right);
251         return (long long)result.tv_sec * SEC_TO_USEC +
252                 result.tv_nsec / USEC_TO_NSEC;
253 }
254
255 static inline long long timespec2usec(const struct timespec *ts)
256 {
257         return ts->tv_sec * SEC_TO_USEC + ts->tv_nsec / USEC_TO_NSEC;
258 }
259
260
261 int create_ac_socket(unsigned int ac) 
262 {
263         int sockfd;
264         unsigned int yes=1, tos;
265
266
267         if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
268         {
269                 perror("Unable to open socket");
270                 return -1;
271         }
272         if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
273                 perror("set non-blocking socket");
274                 return -1;
275         }
276         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
277                 perror("Unable to set socket");
278                 return -1;
279         }
280
281         if (opt_send_buf_size >= 0) {
282                 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
283                         perror("Unable to set socket buffer size");
284                         return -1;
285                 }
286         }
287
288         
289         //tos = ((AC_NUM - ac) *2 - 1)*32;
290         tos = ac_to_tos[ac];
291         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
292                 perror("Unable to set TOS");
293                 close(sockfd);
294                 return -1;
295         }
296
297         return sockfd;
298 }
299
300 void empty_handler()
301 {
302 }
303
304 void reset_statistics()
305 {
306         int i;
307         for (i = 0; i < nr_streams; i++) {
308                 pthread_mutex_lock(&streams[i].mutex);
309                 streams[i].sent = 0;
310                 streams[i].really_sent = 0;
311                 streams[i].received = 0;
312                 pthread_mutex_unlock(&streams[i].mutex);
313         }
314         pthread_mutex_lock(&delay_stats_mutex);
315         clock_gettime(CLOCK_MONOTONIC, &reset_timestamp);
316         memset(delay_stats, 0, sizeof(delay_stats));
317         pthread_mutex_unlock(&delay_stats_mutex);
318 }
319
320 void* receiver(void* queue)
321 {
322         struct msg_t    msg;
323         struct  sockaddr_in rem_addr;
324         int     mlen, ret;
325         unsigned int ac, rem_addr_length; 
326         long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
327         long long int min_trans_time;
328         struct timespec send_timestamp, server_timestamp, recv_timestamp;
329         fd_set fdset;
330         
331         min_trans_time = ~0;
332         
333         block_signals();
334         set_rt_prio(99);
335
336         ac = (int)queue;
337         rem_addr_length = sizeof(rem_addr);
338         FD_ZERO(&fdset);
339         while (!exit_flag) {
340                 FD_SET(ac_sockfd[ac], &fdset);
341                 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
342                 if (ret < 0) {
343                         if (errno == EINTR) continue;
344                         perror("receiver select");
345                         goto out;
346                 }
347                 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,
348                                 (struct sockaddr*)&rem_addr, &rem_addr_length);
349                 if (mlen < 0) {
350                         perror("Chyba pri prijimani pozadavku");
351                         goto out;
352                 }       
353                 clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
354                 send_timestamp = msg.send_timestamp;
355 /*              server_timestamp = msg.sendback_timestamp; */
356
357                 /* Check whether this message was sent after reset_statistics() */
358                 
359                 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
360                         continue; /* If so, don't count it */
361                 }
362
363                 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp);
364                 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
365                 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
366
367                 trans_time_usec /= 2;
368
369                 if (trans_time_usec < MAX_DELAY_US) {
370                         pthread_mutex_lock(&delay_stats_mutex);
371                         delay_stats[ac][trans_time_usec/opt_granularity_usec]++;
372                         pthread_mutex_unlock(&delay_stats_mutex);
373                 }
374                 receivers[ac].received++;
375                 pthread_mutex_lock(&streams[msg.stream].mutex);
376                 streams[msg.stream].received++;
377                 pthread_mutex_unlock(&streams[msg.stream].mutex);
378         
379                 /*if (trans_time_nsec < min_trans_time) 
380                         min_trans_time = trans_time_nsec;*/
381                 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
382                          "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
383                          msg.seqn, msg.tos, send_timestamp.tv_sec,\
384                          send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
385                          recv_timestamp.tv_nsec, trans_time_msec); */
386         }
387 out:
388         sem_post(&sem_thread_finished);
389         return NULL;
390 }
391
392 /** 
393  * Send a packet.
394  * 
395  * @return -1 in case of error, 1 in case of sucessfull send and 0
396  *         when all buffers are full.
397  */
398 static inline int 
399 send_packet(struct stream* stream, union msg_buff* buff)
400 {
401         int ret = 1;
402         while (sendto(ac_sockfd[stream->ac], buff, stream->packet_size, 0,
403                       (struct sockaddr*)&stream->rem_addr, sizeof(stream->rem_addr)) < 0) {
404                 if (errno == EINTR) continue;
405                 if (errno == EAGAIN) {
406                         if (opt_wait_for_queue_is_full && 
407                             !some_queue_is_full && 
408                             /* We use mutex as atomic test and set */
409                             (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
410                                 some_queue_is_full = true;
411                                 reset_statistics();
412                         }
413                         ret = 0;
414                         break;
415                 } else {
416                         perror("Error while sending");
417                         ret = -1;
418                         break;
419                 }
420         }
421         return ret;
422 }
423
424 static inline void
425 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
426 {
427         struct timespec time_to_wait, current_time, period, interval;
428         unsigned period_usec = stream->period_usec;
429
430         /*           |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
431         /* |-------------|     nominal period*/
432         if (stream->jitter) {
433                 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
434                                                + rand() % (2*period_usec*stream->jitter/100));
435         } else {
436                 period.tv_nsec = USEC_TO_NSEC*(period_usec);
437         }
438         period.tv_sec = 0;
439         
440         timespec_add(&time_to_wait, last_send_time, &period);
441         clock_gettime(CLOCK_MONOTONIC,&current_time);
442         timespec_sub(&interval,&time_to_wait,&current_time);
443         nanosleep(&interval,NULL);
444 }
445
446
447 void* sender(void* arg)
448 {
449         union msg_buff buff;
450         unsigned long int seqn;
451         struct stream* stream = (struct stream*) arg;
452         char stream_desc[100];
453         int ret;
454
455         stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
456         printf("%s\n", stream_desc);
457
458         if (stream->bandwidth_bps == 0)
459                 goto out;
460
461         seqn = 0;
462         
463         block_signals();
464         set_rt_prio(90-stream->ac);
465
466         while (!exit_flag) {
467
468 /*              buff.msg.seqn = seqn++; */
469 /*              buff.msg.tos = ac_to_tos[stream->ac]; */
470                 buff.msg.stream = stream-streams;
471                 
472                 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
473
474                 ret = send_packet(stream, &buff);
475                 if (ret < 0) {
476                         goto out;
477                 }
478
479                 pthread_mutex_lock(&stream->mutex);
480                 stream->sent++;
481                 if (ret > 0)
482                         stream->really_sent++;
483                 pthread_mutex_unlock(&stream->mutex);
484
485 #ifdef DEBUG
486                 printf("%d", stream->ac);
487                 fflush(stdout);
488 #endif
489
490                 wait_for_next_send(stream, &buff.msg.send_timestamp);
491         }
492 out:
493         sem_post(&sem_thread_finished);
494         return NULL;
495 }
496
497 static inline void
498 calc_stream_params(struct stream *stream)
499 {
500         int packet_size;
501         unsigned period_usec;
502         int bandwidth;
503         struct hostent* ph;
504
505         bandwidth = stream->bandwidth_bps;
506
507         /* Avoid arithmetic exception. Server thread will exit if
508            stream->bandwidth_bps == 0. */
509         if (bandwidth == 0) bandwidth = 1;
510
511         if (stream->packet_size) {
512                 packet_size = stream->packet_size;
513                 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
514                 if (period_usec == 0) period_usec = 1;
515         } else if (stream->period_usec) {
516                 period_usec = stream->period_usec;
517                 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
518         } else {
519                 char buf[200];
520                 stream_to_text(buf, sizeof(buf), stream, 0);
521                 fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
522                 exit(1);
523         }
524
525         if (packet_size < sizeof(struct msg_t)) {
526                 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
527                 exit(1);
528         }
529
530         stream->packet_size = packet_size;
531         stream->period_usec = period_usec;
532         stream->jitter = opt_jitter;
533
534         memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
535                 
536         stream->rem_addr.sin_family = AF_INET;
537         ph = gethostbyname(server_addr);
538         if (ph) 
539                 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
540         else {
541                 perror("Unknown server");
542                 exit(1);
543         }
544         stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
545
546 }
547
548 /** 
549  * Parse -b parameter.
550  * 
551  * @param params String to parse
552  * 
553  * @return NULL in case of success, pointer to a problematic character
554  *         on error.
555  */
556 char* parse_bandwidths(char *params)
557 {
558         struct stream *sp = &streams[nr_streams];
559
560         while (*params && nr_streams < MAX_STREAMS) {
561                 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
562                 int i;
563                 char *next_char;
564
565                 if (strlen(params) < 2)
566                         return params;
567                 for (i=0; i<AC_NUM; i++) {
568                         if (strncmp(params, ac_ids[i], 2) == 0) {
569                                 sp->ac = i;
570                                 params+=strlen(ac_ids[i]);
571                                 break;
572                         }
573                 }
574                 if (i==AC_NUM)
575                         return params;
576
577                 long bw;
578                 if (*params == ':') {
579                         params++;
580
581                         bw = strtol(params, &next_char, 10);
582                         if (next_char == params)
583                                 return params;
584                         params = next_char;
585                 } else
586                         bw = opt_def_bandwidth;
587                 
588                 sp->bandwidth_bps = bw*Kbit;
589
590                 long period = 0;
591                 long packet_size = 0;
592                 if (*params == '@') {
593                         params++;
594                         period = strtol(params, &next_char, 10);
595                         if (period == 0)
596                                 return params;
597                         params = next_char;
598                 }
599                 else {
600                         if (*params == '/') {
601                                 params++;
602                                 packet_size = strtol(params, &next_char, 10);
603                                 if (packet_size == 0)
604                                         return params;
605                                 params = next_char;
606                         } else {
607                                 packet_size = opt_packet_size;
608                                 period = opt_def_period_msec;
609                         }
610                 }
611                 sp->period_usec = period*MSEC_TO_USEC;
612                 sp->packet_size = packet_size;
613
614                 
615
616                 if (*params != '\0' && *params != ',')
617                         return params;
618                 nr_streams++;
619                 sp++;
620                 if (*params == ',')
621                         params++;
622         }
623         return NULL;
624 }
625
626 int main(int argc, char *argv[])
627 {
628         int ac, i, rc, seconds;
629         pthread_attr_t attr;
630         pthread_t thread;
631         char opt;
632
633
634         while ((opt = getopt(argc, argv, "B:b:c:g:j:o:qQ:s:T:")) != -1) {
635                 switch (opt) {
636                         case 'B':
637                                 opt_def_bandwidth = atoi(optarg);
638                                 break;
639                         case 'b': {
640                                 char *error;
641                                 error = parse_bandwidths(optarg);
642                                 if (error != NULL) {
643                                         if (*error == '\0')
644                                                 fprintf(stderr, "Bandwidth parse error - string to short\n");
645                                         else
646                                                 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
647                                         exit(1);
648                                 }
649                                 break;
650                         }
651                         case 'c':
652                                 opt_count_sec = atoi(optarg);
653                                 break;
654                         case 'g':
655                                 opt_granularity_usec = atoi(optarg);
656                                 if (opt_granularity_usec < MIN_GRANULARITY) {
657                                         fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
658                                         exit(1);
659                                 }
660                                 break;
661                         case 'j':
662                                 opt_jitter = atoi(optarg);
663                                 break;
664                         case 'o':
665                                 opt_output = optarg;
666                                 break;
667                         case 'Q':
668                                 opt_send_buf_size = atoi(optarg);
669                                 break;
670                         case 'q':
671                                 opt_wait_for_queue_is_full = true;
672                                 break;
673                         case 's':
674                                 opt_packet_size = atoi(optarg);
675                                 break;
676                         case 'T':
677                                 opt_def_period_msec = atoi(optarg);
678                                 break;
679                         default:
680                                 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
681                                 fprintf(stderr, "Options:\n");
682                                 fprintf(stderr, "    -B  default bandwidth for -b option [kbit]\n");
683                                 fprintf(stderr, "    -b  bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
684                                 fprintf(stderr, "    -c  count (number of seconds to run)\n");
685                                 fprintf(stderr, "    -g  histogram granularity [usec]\n");
686                                 fprintf(stderr, "    -j  send jitter (0-100) [%%]\n");
687                                 fprintf(stderr, "    -o  output filename (.dat will be appended)\n");
688                                 fprintf(stderr, "    -q  gather statistics only after some queue becomes full\n");
689                                 fprintf(stderr, "    -Q  <bytes> set size for socket send buffers\n");
690                                 fprintf(stderr, "    -s  size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
691                                 fprintf(stderr, "    -T  default period for -b option [msec]\n");
692                                 exit(1);
693                 }
694         }
695         if (opt_packet_size && opt_def_period_msec) {
696                 fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
697                 exit(1);
698         }
699
700         if (optind < argc) {
701                 server_addr = argv[optind];
702         } else {
703                 fprintf(stderr, "Expected server address argument\n");
704                 exit(1);
705         }
706
707         if (nr_streams == 0)
708                 parse_bandwidths("BE");
709                 
710         pthread_attr_init(&attr);
711
712         snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
713
714         if ((logfd = fopen(logfname,"w+")) == NULL) {
715                 fprintf(stderr,"Can not open %s\n", logfname);
716                 exit(1);
717         }
718         if (signal(SIGTERM, stopper) == SIG_ERR) {
719                 perror("Error in signal registration");
720                 exit(1);
721         }
722                 
723         if (signal(SIGINT, stopper) == SIG_ERR) {
724                 perror("Signal handler registration error");
725                 exit(1);
726         }
727
728         struct sigaction sa;
729         sa.sa_handler = empty_handler;
730         sa.sa_flags = 0;        /* don't restart syscalls */
731
732         if (sigaction(SIGUSR1, &sa, NULL) < 0) {
733                 perror("sigaction error");
734                 exit(1);
735         }
736
737         sem_init(&sem_thread_finished, 0, 0);
738
739         reset_statistics();
740
741         /* create four receivers each per AC */
742         for (ac = AC_NUM - 1; ac >= 0; ac--) {
743                 ac_sockfd[ac] = create_ac_socket(ac);
744                 if (ac_sockfd[ac] < 0) {
745                         return 1;
746                 }
747                 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
748                 if (rc) {
749                         fprintf(stderr, "Error while creating receiver %d\n",rc);
750                         return 1;
751                 }
752         }               
753                         
754         /* create sendpoints */
755         for (i = 0; i < nr_streams; i++) {
756                 struct stream *s = &streams[i];
757                 pthread_mutex_init(&s->mutex, NULL);
758                 calc_stream_params(s);
759                 rc = pthread_create(&thread, &attr, sender, (void*) s);
760                 if (rc) {
761                         fprintf(stderr, "Error while creating sender %d\n",rc);
762                         return 1;
763                 }
764         }
765         
766         seconds = 0;
767         while (!exit_flag) {
768                 sleep(1);
769                 seconds++;
770                 fprintf(stderr, "\r%3ds", seconds);
771                 for (ac = 0; ac < AC_NUM; ac++) {
772                         int delta = receivers[ac].received - receivers[ac].last_received;
773                         receivers[ac].last_received = receivers[ac].received;
774                         fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
775                 }
776                 fflush(stderr);
777                 if (seconds == opt_count_sec)
778                         stopper();
779         }
780
781         fprintf(stderr, "\nWaiting for threads to finish\n");
782         /* Wait for all threads to finish */
783         for (i=0; i < nr_streams + AC_NUM; i++) {
784                 sem_wait(&sem_thread_finished);
785         }
786         struct timespec end_timestamp, measure_length;
787         clock_gettime(CLOCK_MONOTONIC,&end_timestamp);
788         timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
789
790         save_results(argc, argv, timespec2usec(&measure_length));
791
792         return 0;
793 }