]> 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 592ea8eee52ab01aef887a7dae03182956c9e9e2..266ef30c790f56917f4868f5cd5429d864540cdf 100644 (file)
@@ -18,7 +18,7 @@
 #include "common.h"
 #include <semaphore.h>
 
-#define MAX_SENDENDPOINTS  10 
+#define MAX_STREAMS  10 
 #define MIN_GRANULARITY 100
 
 unsigned opt_packet_size = 800;
@@ -26,6 +26,8 @@ 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;
 
 int ac_sockfd[AC_NUM];
@@ -39,17 +41,6 @@ FILE* logfd;
 char* server_addr; 
 char logfname[100];
 
-struct msg_t {
-       unsigned int tos;
-       struct timespec send_timestamp;
-       unsigned long int seqn;
-};
-
-union msg_buff {
-       struct msg_t msg;
-       char nonsense[BUFFSIZE];
-};
-
 /* maximal traffic delay in ms - 10 s*/
 #define MAX_DELAY_US 10000000
 
@@ -62,10 +53,17 @@ unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
    struct timespec   send_timestamp; 
 };*/
 
-struct send_endpoint {
-       int ac;
-       long period_usec;       /* all time units are in microseconds */
+struct stream {
+       /* Input parameters */
+       enum ac ac;             /*  */
        int bandwidth_bps;      /* bits per second */
+       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;
 };
 
 /*
@@ -78,9 +76,9 @@ struct send_endpoint sepoint[] = {
 };
 */
 
-struct send_endpoint sendpoints[MAX_SENDENDPOINTS];
+struct stream streams[MAX_STREAMS];
 
-unsigned int nr_sepoints = 0;
+unsigned int nr_streams = 0;
 
 sem_t sem_thread_finished;
 
@@ -97,7 +95,25 @@ void stopper()
        }
 }
 
-void save_results()
+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;
@@ -105,7 +121,18 @@ void save_results()
        unsigned sum[AC_NUM];
        double val;
 
-       fprintf(stderr, "\nWriting data to %s...\n", logfname);
+       fprintf(stderr, "Writing data to %s... ", logfname);
+       fflush(stderr);
+
+       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;
@@ -118,44 +145,52 @@ void save_results()
        maxi++;
        if (maxi < mini) maxi = mini;
 
-       /* Calculate total number of received packets per AC */
-       for (ac = 0; ac < AC_NUM; 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);
+       /* 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;
        }
 
+#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 = 0;
+                       else val = -1; /* Don't display this ac */
                        fprintf(logfd," %lf", val);
                }
        }
        
        fprintf(logfd,"\n\n");
-
+#endif
+       
        /* Write PDF */
-       unsigned long long integral[AC_NUM];
-       for (ac = 0; ac < AC_NUM; ac++) integral[ac] = 0;
-       for ( i = 0 ; i < maxi+1; i++) {
-               fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
-               for (ac = 0; ac < AC_NUM; ac++) {
-                       if (sum[ac])
-                               val = (double)integral[ac]*100.0 / sum[ac];
-                       else val = 0;
-                       fprintf(logfd," %lf", val);
-                       if (i>0)
-                               integral[ac] += delay_stats[ac][i-1];
+       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++;
+               
+                       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");
        }
        
-       fprintf(stderr, "Finished.\n");
+       fprintf(stderr, "finished.\n");
        fclose(logfd);
 
        exit(0);
@@ -198,7 +233,10 @@ 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;
@@ -224,11 +262,12 @@ 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;
        
@@ -237,11 +276,18 @@ void* receiver(void* queue)
 
        ac = (int)queue;
         rem_addr_length = sizeof(rem_addr);
+       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) {
-                       if (errno == EINTR) continue;
                        perror("Chyba pri prijimani pozadavku");
                        goto out;
                }       
@@ -256,6 +302,7 @@ void* receiver(void* queue)
                        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;*/
@@ -270,35 +317,38 @@ out:
        return NULL;
 }
 
-void* sender(void* endpoint)
+void* sender(void* arg)
 {
        struct sockaddr_in rem_addr;
        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;
-       char buf1[12], buf2[12], buf3[12];
-       int ac = spoint->ac;
+       struct stream* stream = (struct stream*) arg;
        int packet_size;
        unsigned period_usec;
        char stream_desc[100];
 
-       set_rt_prio(90-ac);
+       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/spoint->bandwidth_bps;
+               period_usec = SEC_TO_USEC*packet_size*8/stream->bandwidth_bps;
        } else {
-               period_usec = spoint->period_usec;
-               packet_size = (long long)spoint->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
+               period_usec = stream->period_usec;
+               packet_size = (long long)stream->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
        }
-       snprintf(stream_desc, sizeof(stream_desc), "%d: %s %s (%d bytes per %s +-%s, %d packets/s)\n",
-                spoint-sendpoints, ac_to_text[ac], bandwidth_to_text(buf1, spoint->bandwidth_bps),
-                packet_size, usec_to_text(buf2, period_usec),
-                usec_to_text(buf3, opt_jitter*period_usec/100), (int) SEC_TO_USEC/period_usec);
-       printf("%s", stream_desc);
-       fprintf(logfd, "# Stream %s", stream_desc);
+       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));
@@ -316,7 +366,7 @@ void* sender(void* endpoint)
                perror("Unknown server");
                exit(1);
        }
-       rem_addr.sin_port = htons(BASE_PORT + ac);
+       rem_addr.sin_port = htons(BASE_PORT + stream->ac);
        seqn = 0;
        
        block_signals();
@@ -324,15 +374,20 @@ void* sender(void* endpoint)
        while (!exit_flag) {
 
                buff.msg.seqn = seqn;
-               buff.msg.tos = ac_to_tos[ac];
+               buff.msg.tos = ac_to_tos[stream->ac];
+               buff.msg.stream = stream-streams;
                
                clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
-               
-               while (sendto(ac_sockfd[ac], &buff, packet_size, 0,\
+
+               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");
-                               goto out;
+                               if (errno == EAGAIN) {
+                                       break;
+                               } else {
+                                       perror("Error while sending");
+                                       goto out;
+                               }
                }
 
 #ifdef DEBUG
@@ -340,6 +395,7 @@ void* sender(void* endpoint)
                fflush(stdout);
 #endif
                seqn++;
+               stream->sent++;
 
                /*           |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
                /* |-------------|     nominal period*/
@@ -363,11 +419,12 @@ out:
 
 char* parse_bandwidths(char *params)
 {
-       struct send_endpoint *sp = &sendpoints[nr_sepoints];
+       struct stream *sp = &streams[nr_streams];
 
-       while (*params && nr_sepoints < MAX_SENDENDPOINTS) {
+       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;
@@ -378,17 +435,21 @@ char* parse_bandwidths(char *params)
                                break;
                        }
                }
-               if (i==AC_NUM || *params=='\0' || *params!=':')
+               if (i==AC_NUM)
                        return params;
-               params++;
 
                long bw;
-               char *next_char;
-               bw = strtol(params, &next_char, 10);
-               if (bw == 0)
-                       return params;
+               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;
-               params = next_char;
 
                long period;
                if (*params == '@') {
@@ -399,12 +460,12 @@ char* parse_bandwidths(char *params)
                        params = next_char;
                }
                else
-                       period = 10;
+                       period = opt_def_period_msec;
                sp->period_usec = period*MSEC_TO_USEC;
 
                if (*params != '\0' && *params != ',')
                        return params;
-               nr_sepoints++;
+               nr_streams++;
                sp++;
                if (*params == ',')
                        params++;
@@ -414,14 +475,17 @@ char* parse_bandwidths(char *params)
 
 int main(int argc, char *argv[])
 {
-       int ac, i, rc;
+       int ac, i, rc, seconds;
        pthread_attr_t attr;
        pthread_t thread;
        char opt;
 
 
-       while ((opt = getopt(argc, argv, "b:c:g:j:o:s:")) != -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);
@@ -453,15 +517,20 @@ int main(int argc, char *argv[])
                        case 's':
                                opt_packet_size = atoi(optarg);
                                break;
+                       case 'T':
+                               opt_def_period_msec = atoi(optarg);
+                               break;
                        default:
                                fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
                                fprintf(stderr, "Options:\n");
-                               fprintf(stderr, "    -b  bandwidth of streams (VO|VI|BE|BK):<kbit>[@<msec>][,...]\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);
                }
        }
@@ -473,8 +542,8 @@ int main(int argc, char *argv[])
        }
 
 
-       if (nr_sepoints == 0)
-               parse_bandwidths("BE:300@10");
+       if (nr_streams == 0)
+               parse_bandwidths("BE");
                
        memset(delay_stats,0, sizeof(delay_stats));     
        pthread_attr_init(&attr);
@@ -485,10 +554,6 @@ int main(int argc, char *argv[])
                fprintf(stderr,"Can not open %s\n", logfname);
                exit(1);
        }
-       fprintf(logfd, "# Invoked as: ");
-       for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
-       fprintf(logfd, "\n");
-               
        if (signal(SIGTERM, stopper) == SIG_ERR) {
                perror("Error in signal registration");
                exit(1);
@@ -521,36 +586,36 @@ int main(int argc, char *argv[])
        }               
                        
        /* create sendpoints */
-       for (i = 0; i < nr_sepoints; i++) {
-               rc = pthread_create(&thread, &attr, sender, (void*) &sendpoints[i]);
+       for (i = 0; i < nr_streams; i++) {
+               rc = pthread_create(&thread, &attr, sender, (void*) &streams[i]);
                if (rc) {
                        fprintf(stderr, "Error while creating sender %d\n",rc);
                        return 1;
                }
        }
        
-       i = 0;
+       seconds = 0;
        while (!exit_flag) {
                sleep(1);
-               fprintf(stderr, "\r");
+               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)  ", ac_to_text[ac], receivers[ac].received, delta);
+                       fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
                }
                fflush(stderr);
-               i++;
-               if (i == opt_count_sec) exit_flag = 1;
+               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_sepoints + AC_NUM; i++) {
+       for (i=0; i < nr_streams + AC_NUM; i++) {
                sem_wait(&sem_thread_finished);
        }
-       printf("\n");
 
-       save_results();
+       save_results(argc, argv, seconds);
 
        return 0;
 }