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