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