]> rtime.felk.cvut.cz Git - frescor/fwp.git/blobdiff - wme_test/wclient.c
Fixed handling of send when it would block.
[frescor/fwp.git] / wme_test / wclient.c
index 2b05e4b61f27cc8bf24435a20fa82a42d7864570..266ef30c790f56917f4868f5cd5429d864540cdf 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include "common.h"
+#include <semaphore.h>
 
-/*#define AC_VO 0
-#define AC_VI 1
-#define AC_BE 2
-#define AC_BK 3
-*/
-
-#define PARAM_SERVERADDR 1
-#define BASE_PORT       5100
-#define AC_QUEUES       4
-#define MAX_SENDENDPOINTS  10
-#define MTU             800 
+#define MAX_STREAMS  10 
+#define MIN_GRANULARITY 100
 
+unsigned opt_packet_size = 800;
+unsigned opt_period_usec = 10*MSEC_TO_USEC;
+unsigned opt_jitter = 0;
+char    *opt_output = "delay_stats";
+unsigned opt_count_sec = 0;
+unsigned opt_def_bandwidth = 200;
+unsigned opt_def_period_msec = 10;
+int opt_granularity_usec = MIN_GRANULARITY;
 
-enum {  AC_VO = 0,
-       AC_VI = 1,
-       AC_BE = 2,
-       AC_BK = 3
-};
+int ac_sockfd[AC_NUM];
 
-const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
-const unsigned int ac_to_tos[4] = {224,160,96,64};
+struct receiver {
+       pthread_t thread;
+       unsigned received, last_received;
+} receivers[AC_NUM];
 
-int ac_sockfd[AC_QUEUES];
 FILE* logfd;
 char* server_addr; 
+char logfname[100];
 
-const char logfname[] = "delay_stats.dat";
-
-struct msg_t {
-       unsigned int tos;
-       struct timespec send_timestamp;
-       unsigned long int seqn;
-       unsigned char padding[MTU];
-};
-
-/* maximal traffic delay in ms */
+/* maximal traffic delay in ms - 10 s*/
 #define MAX_DELAY_US 10000000
-#define GRANULARITY 100
 
-unsigned delay_stats[AC_QUEUES][MAX_DELAY_US/GRANULARITY];
+unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
 
-/*struct ac_stats[AC_QUEUES] {
+/*struct ac_stats[AC_NUM] {
    unsigned long int min_trans_time;
    unsigned long int sum_trans_time;
    struct timespec   recv_timestamp;
    struct timespec   send_timestamp; 
 };*/
 
-struct send_endpoint {
-       int ac;
-       long period_nsec;
+struct stream {
+       /* Input parameters */
+       enum ac ac;             /*  */
        int bandwidth_bps;      /* bits per second */
-};
-
-#define MSEC (1000*1000)
-#define Mbit (1000*1000)
-#define Kbit 1000
+       int jitter;             /* percent */
+       /* Mulualy exclusive input parameters */
+       int packet_size;
+       long period_usec;       /* all time units are in microseconds */
 
+       /* Statistics */
+       unsigned long long sent, received;
+};
 
 /*
 struct send_endpoint sepoint[] = {
-       { .ac = AC_VO, .period_nsec=200*MSEC, .bandwidth_bps = 34*Kbit },
-       { .ac = AC_VI, .period_nsec=25*MSEC, .bandwidth_bps =  480*Kbit },
-       { .ac = AC_BE, .period_nsec=40*MSEC, .bandwidth_bps =  300*Kbit },
-       { .ac = AC_BK, .period_nsec=40*MSEC, .bandwidth_bps =  300*Kbit },
-//     { .ac = AC_VI, .period_nsec=17*MSEC, .bandwidth_bps =  675*Kbit },
+       { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
+       { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps =  480*Kbit },
+       { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
+       { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
+//     { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps =  675*Kbit },
 };
 */
 
-struct send_endpoint sepoint[] = {
-       { .ac = AC_VO, .period_nsec=40*MSEC, .bandwidth_bps = 300*Kbit },
-       { .ac = AC_VI, .period_nsec=40*MSEC, .bandwidth_bps =  300*Kbit },
-       { .ac = AC_BE, .period_nsec=60*MSEC, .bandwidth_bps =  200*Kbit },
-       { .ac = AC_BK, .period_nsec=60*MSEC, .bandwidth_bps =  200*Kbit },
-};
+struct stream streams[MAX_STREAMS];
 
-unsigned int nr_sepoints = sizeof(sepoint)/sizeof(*sepoint);
+unsigned int nr_streams = 0;
+
+sem_t sem_thread_finished;
+
+bool exit_flag = false;
 
 void stopper()
+{
+       int i;
+       exit_flag = true;
+
+       /* Interrupt all receivers */
+       for (i=0; i < AC_NUM; i++) {
+               pthread_kill(receivers[i].thread, SIGUSR1);
+       }
+}
+
+void stream_to_text(char *stream_desc, size_t n, struct stream *stream, int seconds)
+{
+       char buf[3][12];
+       char real[100];
+
+       if (seconds) {
+               snprintf(real, sizeof(real), "; real: sent %lld (%lld/s), received %lld (%lld/s)",
+                        stream->sent, stream->sent/seconds,
+                        stream->received, stream->received/seconds);
+       }
+       
+       snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
+                stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
+                stream->packet_size, usec_to_text(buf[1], stream->period_usec),
+                usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
+                (int)(SEC_TO_USEC/stream->period_usec), real);
+}
+
+void save_results(int argc, char *argv[], int seconds)
 {
        int ac, i, maxi;
+       const int mini = 3000/opt_granularity_usec;
        bool allzeros;
-       unsigned sum[AC_QUEUES];
+       unsigned sum[AC_NUM];
+       double val;
+
+       fprintf(stderr, "Writing data to %s... ", logfname);
+       fflush(stderr);
 
-       printf("Writing data to log file...\n");
+       fprintf(logfd, "# Invoked as: ");
+       for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
+       fprintf(logfd, "\n");
+               
+       for (i = 0; i < nr_streams; i++) {
+               char stream_desc[120];
+               stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], seconds);
+               fprintf(logfd, "# Stream %s\n", stream_desc);
+       }
 
+       /* Find maximal delay */
        allzeros = true;
-       for (maxi = MAX_DELAY_US/GRANULARITY - 1; maxi >= 0; maxi--) {
-               for (ac = 0; ac < AC_QUEUES; ac++) {
+       for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
+               for (ac = 0; ac < AC_NUM; ac++) {
                        if (delay_stats[ac][maxi] != 0) allzeros = false;
                }
                if (!allzeros) break;
        }
-       if (maxi < 10000/GRANULARITY) maxi = 10000/GRANULARITY;
-
-       for (ac = 0; ac < AC_QUEUES; ac++) { 
-               sum[ac] = 0;
-               for ( i = 0 ; i < maxi; i++) 
-                       sum[ac]+=delay_stats[ac][i];
-               if (sum[ac] == 0)
-                       fprintf(stderr, "No response in AC %d\n", ac);
+       maxi++;
+       if (maxi < mini) maxi = mini;
+
+       /* Calculate total number of sent packets per AC */
+       for (ac = 0; ac < AC_NUM; ac++) sum[ac] = 0;
+       for (i = 0; i < nr_streams; i++) {
+               ac = streams[i].ac;
+               sum[ac] += streams[i].sent;
        }
 
-       for (ac = 0; ac < AC_QUEUES; ac++) { 
-               for ( i = 0 ; i < maxi; i++) {
-                       double val;
-                       val = (double)delay_stats[ac][i]*100.0 / sum[ac];
-                       fprintf(logfd,"%f %lf\n", i*GRANULARITY/1000.0, val);
+#if 0
+       /* Write pdf */
+       for ( i = 0 ; i < maxi; i++) {
+               fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
+               for (ac = 0; ac < AC_NUM; ac++) { 
+                       if (sum[ac])
+                               val = (double)delay_stats[ac][i]*100.0 / sum[ac];
+                       else val = -1; /* Don't display this ac */
+                       fprintf(logfd," %lf", val);
                }
+       }
+       
+       fprintf(logfd,"\n\n");
+#endif
+       
+       /* Write PDF */
+       for (ac = 0; ac < AC_NUM; ac++) {
+               unsigned long long integral = 0, last = -1;
+
+               fprintf(logfd,"%f %f\n", 0.0, 0.0);
+
+               if (sum[ac] != 0) {
+                       i=0;
+                       while (delay_stats[ac][i] == 0) i++;
                
-               fprintf(logfd, "\n\n");
+                       for (i++; i < maxi+1; i++) {
+                               if (last != integral) {
+                                       val = (double)integral*100.0 / sum[ac];
+                                       fprintf(logfd,"%f %f\n", i*opt_granularity_usec/1000.0, val);
+                                       last = integral;
+                               }
+                               if (i>0)
+                                       integral += delay_stats[ac][i-1];
+                       }
+               }
+               fprintf(logfd,"\n\n");
        }
        
-       printf("Finished.\n");
+       fprintf(stderr, "finished.\n");
        fclose(logfd);
 
        exit(0);
@@ -172,14 +233,17 @@ int create_ac_socket(unsigned int ac)
                perror("Unable to open socket");
                return -1;
        }
-       
+       if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
+               perror("set non-blocking socket");
+               return -1;
+       }
        if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
                perror("Unable to set socket");
                return -1;
        }
 
        
-       //tos = ((AC_QUEUES - ac) *2 - 1)*32;
+       //tos = ((AC_NUM - ac) *2 - 1)*32;
        tos = ac_to_tos[ac];
        if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
                perror("Unable to set TOS");
@@ -190,37 +254,55 @@ int create_ac_socket(unsigned int ac)
        return sockfd;
 }
 
+void empty_handler()
+{
+}
 
 void* receiver(void* queue)
 {
        struct msg_t    msg;
        struct  sockaddr_in rem_addr;
-       int     mlen;
+       int     mlen, ret;
        unsigned int ac, rem_addr_length; 
        unsigned long int trans_time_usec;
        unsigned long int min_trans_time;
-       struct timespec   send_timestamp,recv_timestamp, trans_time; 
+       struct timespec   send_timestamp,recv_timestamp, trans_time;
+       fd_set fdset;
        
        min_trans_time = ~0;
        
+       block_signals();
+       set_rt_prio(99);
+
        ac = (int)queue;
         rem_addr_length = sizeof(rem_addr);
-       while (1) {
-               while ((mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,\
-                       (struct sockaddr*)&rem_addr, &rem_addr_length)) < 0) {
-                           if (errno == EINTR) continue;
-                           perror("Chyba pri prijimani pozadavku");
-                           return NULL;
+       FD_ZERO(&fdset);
+       while (!exit_flag) {
+               FD_SET(ac_sockfd[ac], &fdset);
+               ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
+               if (ret < 0) {
+                       if (errno == EINTR) continue;
+                       perror("receiver select");
+                       goto out;
+               }
+               mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,    \
+                               (struct sockaddr*)&rem_addr, &rem_addr_length);
+               if (mlen < 0) {
+                       perror("Chyba pri prijimani pozadavku");
+                       goto out;
                }       
                clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
                send_timestamp = msg.send_timestamp;
                
                timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
-               trans_time_usec = (trans_time.tv_sec * 1000000 + \
-                                        trans_time.tv_nsec / 1000) /2;
+               trans_time_usec = (trans_time.tv_sec * SEC_TO_USEC + \
+                                        trans_time.tv_nsec / USEC_TO_NSEC) /2;
          
                if (trans_time_usec < MAX_DELAY_US)
-                       delay_stats[ac][trans_time_usec/GRANULARITY]++;
+                       delay_stats[ac][trans_time_usec/opt_granularity_usec]++;
+
+               receivers[ac].received++;
+               streams[msg.stream].received++;
        
                /*if (trans_time_nsec < min_trans_time) 
                        min_trans_time = trans_time_nsec;*/
@@ -230,83 +312,225 @@ void* receiver(void* queue)
                         send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
                         recv_timestamp.tv_nsec, trans_time_msec); */
        }
+out:
+       sem_post(&sem_thread_finished);
+       return NULL;
 }
 
-void* sender(void* endpoint)
+void* sender(void* arg)
 {
        struct sockaddr_in rem_addr;
-       struct msg_t msg;
+       union msg_buff buff;
        unsigned long int seqn;
        struct timespec time_to_wait, current_time, period, interval;
        struct hostent* ph;
-       struct send_endpoint* spoint = (struct send_endpoint*) endpoint;
-       int ac = spoint->ac;
+       struct stream* stream = (struct stream*) arg;
+       int packet_size;
+       unsigned period_usec;
+       char stream_desc[100];
+
+       if (stream->bandwidth_bps == 0)
+               goto out;
+
+       set_rt_prio(90-stream->ac);
+
+       if (opt_packet_size) {
+               packet_size = opt_packet_size;
+               period_usec = SEC_TO_USEC*packet_size*8/stream->bandwidth_bps;
+       } else {
+               period_usec = stream->period_usec;
+               packet_size = (long long)stream->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
+       }
+       stream->packet_size = packet_size;
+       stream->period_usec = period_usec;
+       stream->jitter = opt_jitter;
+
+
+       stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
+
+       printf("%s\n", stream_desc);
+
+       if (packet_size < sizeof(struct msg_t)) {
+               fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
+               exit(1);
+       }
+
        
        memset(&rem_addr,0, sizeof(rem_addr));
-       //-------------------------------------------------------------------
-       // TODO: not functioning - check it
                
-       if ((rem_addr.sin_addr.s_addr = inet_addr(server_addr)) == INADDR_NONE){
-          ph = gethostbyname(server_addr);
-          if (ph) 
-                  rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
-          else {
-                  perror("Unknown server"); 
-          }
-       }
-       
-       //------------------------------------------------------------------
        rem_addr.sin_family = AF_INET;
-       rem_addr.sin_addr.s_addr =  inet_addr(server_addr);
-       rem_addr.sin_port = htons(BASE_PORT + ac);
+       ph = gethostbyname(server_addr);
+       if (ph) 
+               rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
+       else {
+               perror("Unknown server");
+               exit(1);
+       }
+       rem_addr.sin_port = htons(BASE_PORT + stream->ac);
        seqn = 0;
        
-       period.tv_nsec = spoint->period_nsec;
-       period.tv_sec = 0;
+       block_signals();
 
-       while (1) {
+       while (!exit_flag) {
 
-               msg.seqn = seqn;
-               msg.tos = ac_to_tos[ac];
+               buff.msg.seqn = seqn;
+               buff.msg.tos = ac_to_tos[stream->ac];
+               buff.msg.stream = stream-streams;
                
-               clock_gettime(CLOCK_MONOTONIC,&msg.send_timestamp);
-               
-               while (sendto(ac_sockfd[ac], &msg, sizeof(msg), 0,\
+               clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
+
+               while (sendto(ac_sockfd[stream->ac], &buff, packet_size, 0,\
                                (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
                                if (errno == EINTR) continue;
-                               perror("Error while sending.");
-                               return NULL;
+                               if (errno == EAGAIN) {
+                                       break;
+                               } else {
+                                       perror("Error while sending");
+                                       goto out;
+                               }
                }
+
+#ifdef DEBUG
                printf("%d", ac);
                fflush(stdout);
-
+#endif
                seqn++;
-               
-               timespec_add(&time_to_wait,&msg.send_timestamp,&period);
+               stream->sent++;
+
+               /*           |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
+               /* |-------------|     nominal period*/
+               if (opt_jitter) {
+                       period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
+                                                + rand() % (2*period_usec*opt_jitter/100));
+               } else {
+                       period.tv_nsec = USEC_TO_NSEC*(period_usec);
+               }
+               period.tv_sec = 0;
+
+               timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
                clock_gettime(CLOCK_MONOTONIC,&current_time);
                timespec_sub(&interval,&time_to_wait,&current_time);
                nanosleep(&interval,NULL);
-       }       
+       }
+out:
+       sem_post(&sem_thread_finished);
+       return NULL;
+}
+
+char* parse_bandwidths(char *params)
+{
+       struct stream *sp = &streams[nr_streams];
+
+       while (*params && nr_streams < MAX_STREAMS) {
+               char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
+               int i;
+               char *next_char;
+
+               if (strlen(params) < 2)
+                       return params;
+               for (i=0; i<AC_NUM; i++) {
+                       if (strncmp(params, ac_ids[i], 2) == 0) {
+                               sp->ac = i;
+                               params+=strlen(ac_ids[i]);
+                               break;
+                       }
+               }
+               if (i==AC_NUM)
+                       return params;
+
+               long bw;
+               if (*params == ':') {
+                       params++;
+
+                       bw = strtol(params, &next_char, 10);
+                       if (next_char == params)
+                               return params;
+                       params = next_char;
+               } else
+                       bw = opt_def_bandwidth;
+               
+               sp->bandwidth_bps = bw*Kbit;
+
+               long period;
+               if (*params == '@') {
+                       params++;
+                       period = strtol(params, &next_char, 10);
+                       if (period == 0)
+                               return params;
+                       params = next_char;
+               }
+               else
+                       period = opt_def_period_msec;
+               sp->period_usec = period*MSEC_TO_USEC;
+
+               if (*params != '\0' && *params != ',')
+                       return params;
+               nr_streams++;
+               sp++;
+               if (*params == ',')
+                       params++;
+       }
+       return NULL;
 }
 
 int main(int argc, char *argv[])
 {
-       int ac, i, rc;
+       int ac, i, rc, seconds;
        pthread_attr_t attr;
        pthread_t thread;
-       int use_stdin = 0;
        char opt;
 
 
-       while ((opt = getopt(argc, argv, "hs")) != -1) {
+       while ((opt = getopt(argc, argv, "B:b:c:g:j:o:s:T:")) != -1) {
                switch (opt) {
+                       case 'B':
+                               opt_def_bandwidth = atoi(optarg);
+                               break;
+                       case 'b': {
+                               char *error;
+                               error = parse_bandwidths(optarg);
+                               if (error != NULL) {
+                                       if (*error == '\0')
+                                               fprintf(stderr, "Bandwidth parse error - string to short\n");
+                                       else
+                                               fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
+                                       exit(1);
+                               }
+                               break;
+                       }
+                       case 'c':
+                               opt_count_sec = atoi(optarg);
+                               break;
+                       case 'g':
+                               opt_granularity_usec = atoi(optarg);
+                               if (opt_granularity_usec < MIN_GRANULARITY) {
+                                       fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
+                                       exit(1);
+                               }
+                               break;
+                       case 'j':
+                               opt_jitter = atoi(optarg);
+                               break;
+                       case 'o':
+                               opt_output = optarg;
+                               break;
                        case 's':
-                               use_stdin = 1;
+                               opt_packet_size = atoi(optarg);
+                               break;
+                       case 'T':
+                               opt_def_period_msec = atoi(optarg);
                                break;
                        default:
-                               fprintf(stderr, "Usage: %s [ options ] server_addr\n", argv[0]);
-                               fprintf(stderr, "options:  -s  read streams from stdin\n");
+                               fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
+                               fprintf(stderr, "Options:\n");
+                               fprintf(stderr, "    -B  default bandwidth for -b option [kbit]\n");
+                               fprintf(stderr, "    -b  bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec>][,...]\n");
+                               fprintf(stderr, "    -c  count (number of seconds to run)\n");
+                               fprintf(stderr, "    -g  histogram granularity [usec]\n");
+                               fprintf(stderr, "    -j  send jitter (0-100) [%%]\n");
+                               fprintf(stderr, "    -o  output filename (.dat will be appended)\n");
+                               fprintf(stderr, "    -s  size of data payload in packets [bytes]\n");
+                               fprintf(stderr, "    -T  default period for -b option [msec]\n");
                                exit(1);
                }
        }
@@ -318,16 +542,18 @@ int main(int argc, char *argv[])
        }
 
 
+       if (nr_streams == 0)
+               parse_bandwidths("BE");
                
        memset(delay_stats,0, sizeof(delay_stats));     
        pthread_attr_init(&attr);
 
+       snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
+
        if ((logfd = fopen(logfname,"w+")) == NULL) {
                fprintf(stderr,"Can not open %s\n", logfname);
                exit(1);
        }
-               
-
        if (signal(SIGTERM, stopper) == SIG_ERR) {
                perror("Error in signal registration");
                exit(1);
@@ -337,29 +563,59 @@ int main(int argc, char *argv[])
                perror("Signal handler registration error");
                exit(1);
        }
-       
+
+       struct sigaction sa;
+       sa.sa_handler = empty_handler;
+       sa.sa_flags = 0;        /* don't restart syscalls */
+
+       if (sigaction(SIGUSR1, &sa, NULL) < 0) {
+               perror("sigaction error");
+               exit(1);
+       }
+
+       sem_init(&sem_thread_finished, 0, 0);
+
        /* create four receivers each per AC */
-       for (ac = AC_QUEUES - 1; ac >= 0; ac--) {
+       for (ac = AC_NUM - 1; ac >= 0; ac--) {
                ac_sockfd[ac] = create_ac_socket(ac);
-               rc = pthread_create(&thread, &attr, receiver, (void*) ac);
+               rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
                if (rc) {
-                       printf("Error while creating receiver %d\n",rc);
+                       fprintf(stderr, "Error while creating receiver %d\n",rc);
                        return 1;
                }
        }               
                        
        /* create sendpoints */
-       for (i = 0; i < nr_sepoints; i++) {
-               rc = pthread_create(&thread, &attr, sender, (void*) &sepoint[i]);
+       for (i = 0; i < nr_streams; i++) {
+               rc = pthread_create(&thread, &attr, sender, (void*) &streams[i]);
                if (rc) {
-                       printf("Error while creating sender %d\n",rc);
+                       fprintf(stderr, "Error while creating sender %d\n",rc);
                        return 1;
                }
        }
        
-       while (1) {
-               sleep(100000);
+       seconds = 0;
+       while (!exit_flag) {
+               sleep(1);
+               seconds++;
+               fprintf(stderr, "\r%3ds", seconds);
+               for (ac = 0; ac < AC_NUM; ac++) {
+                       int delta = receivers[ac].received - receivers[ac].last_received;
+                       receivers[ac].last_received = receivers[ac].received;
+                       fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
+               }
+               fflush(stderr);
+               if (seconds == opt_count_sec)
+                       stopper();
        }
-       
+
+       fprintf(stderr, "\nWaiting for threads to finish\n");
+       /* Wait for all threads to finish */
+       for (i=0; i < nr_streams + AC_NUM; i++) {
+               sem_wait(&sem_thread_finished);
+       }
+
+       save_results(argc, argv, seconds);
+
        return 0;
 }