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