]> rtime.felk.cvut.cz Git - frescor/fwp.git/blobdiff - wme_test/wclient.c
wme_test: update status every second when compiled without FWP
[frescor/fwp.git] / wme_test / wclient.c
index 0011be58dc944c423ab7b281c8a346e95383b6dd..30fb5f162f684a41086a75e923ee0b7a5a36b40d 100644 (file)
@@ -1,4 +1,5 @@
 #include <errno.h>
+#include <error.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -24,6 +25,7 @@
 #include <fwp_confdefs.h>
 #include <fwp.h>
 #include <fwp_vres.h>
+#include <ncurses.h>
 #endif
 
 #define MAX_STREAMS  10 
@@ -36,7 +38,7 @@ char  *opt_interface;
 unsigned opt_jitter = 0;
 char    *opt_output = "delay_stats";
 unsigned opt_count_sec = 0;
-unsigned opt_def_bandwidth = 200;
+unsigned opt_def_bandwidth = 50;
 unsigned opt_def_period_msec = 0;
 int opt_granularity_usec = MIN_GRANULARITY;
 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
@@ -45,6 +47,8 @@ char *opt_comment = NULL;
 bool some_queue_is_full = false;
 struct timespec reset_timestamp;
 
+bool some_contract_not_accepted = false;
+
 /* Locked when some queue is full to prevent multiple resets of
    statstics. */
 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -52,6 +56,7 @@ pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
 int ac_sockfd[AC_NUM];
 
 struct receiver {
+       bool valid;
        pthread_t thread;
        unsigned received, last_received;
 };
@@ -94,11 +99,14 @@ struct stream {
 #ifndef WITH_FWP
        struct sockaddr_in rem_addr;
 #else
+       fwp_contract_d_t contract_send;
+       fwp_contract_d_t contract_resp;
        fwp_endpoint_d_t endpoint;
        fwp_endpoint_d_t resp_endpoint;
        fwp_vres_d_t vres;
        uint16_t resp_port;
        struct receiver receiver;
+       long wc_delay;          /* worst-case delay  */
 #endif
 
        /* Statistics */
@@ -153,8 +161,10 @@ void stopper()
        /* Interrupt all receivers */
 #ifdef WITH_FWP
        for (i=0; i < nr_streams; i++) {
-               pthread_kill(streams[i].receiver.thread, SIGUSR1);
-       }       
+               if (streams[i].receiver.valid) pthread_kill(streams[i].receiver.thread, SIGUSR1);
+               if (streams[i].contract_send) fwp_contract_cancel(streams[i].contract_send);
+               if (streams[i].contract_resp)fwp_contract_cancel(streams[i].contract_resp);
+       }
 #else
        for (i=0; i < AC_NUM; i++) {
                pthread_kill(receivers[i].thread, SIGUSR1);
@@ -300,21 +310,21 @@ int create_ac_socket(unsigned int ac)
 
        if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        {
-               perror("Unable to open socket");
+               error(0, errno, "Unable to open socket");
                return -1;
        }
        if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
-               perror("set non-blocking socket");
+               error(0, errno, "set non-blocking socket");
                return -1;
        }
        if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
-               perror("Unable to set socket");
+               error(0, errno, "Unable to set socket");
                return -1;
        }
 
        if (opt_send_buf_size >= 0) {
                if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
-                       perror("Unable to set socket buffer size");
+                       error(0, errno, "Unable to set socket buffer size");
                        return -1;
                }
        }
@@ -323,7 +333,7 @@ int create_ac_socket(unsigned int ac)
        //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");
+               error(0, errno, "Unable to set TOS");
                close(sockfd);
                return -1;
        }
@@ -367,7 +377,7 @@ int recv_packet_native(unsigned ac, struct msg_t *msg)
                ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
                if (ret < 0) {
                        if (errno == EINTR) continue;
-                       perror("receiver select");
+                       error(0, errno, "receiver select");
                        return -1;
                }
                mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
@@ -409,7 +419,8 @@ void* receiver(void* arg)
                mlen = recv_packet_native(ac, &msg);
 #endif
                if (mlen < 0) {
-                       perror("Chyba pri prijimani pozadavku");
+                       if (errno != EINTR)
+                               error(0, errno, "receive_packet error");
                        goto out;
                }       
                clock_gettime(CLOCK_REALTIME,&recv_timestamp);
@@ -438,6 +449,11 @@ void* receiver(void* arg)
                }
                pthread_mutex_unlock(&delay_stats_mutex);
 
+#ifdef WITH_FWP
+               if (trans_time_usec > stream->wc_delay) {
+                       stream->wc_delay = trans_time_usec;
+               }
+#endif
                receivers[ac].received++;
                
                pthread_mutex_lock(&streams[msg.stream].mutex);
@@ -495,7 +511,7 @@ send_packet_native(struct stream* stream, union msg_buff* buff)
                        ret = 0;
                        break;
                } else {
-                       perror("Error while sending");
+                       error(0, errno, "Error while sending");
                        ret = -1;
                        break;
                }
@@ -543,12 +559,13 @@ void* sender(void* arg)
        union msg_buff buff;
        unsigned long int seqn;
        struct stream* stream = (struct stream*) arg;
-       char stream_desc[100];
        int ret;
 
+#ifndef WITH_FWP
+       char stream_desc[100];
        stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
        printf("%s\n", stream_desc);
-
+#endif
        if (stream->bandwidth_bps == 0)
                goto out;
 
@@ -592,31 +609,35 @@ out:
 static int negotiate_contract_for_stream_fwp(struct stream *stream)
 {
        fwp_contract_t contract;
-       fwp_contract_d_t contract_d;
        fwp_vres_d_t vres2;
        int ret;
 
        /* Contract for client->server stream */
-
+       memset(&contract, 0, sizeof(contract));
        contract.budget = stream->packet_size;
        contract.period_usec = stream->period_usec;
+       contract.deadline_usec = 3*stream->period_usec;
        
-       contract_d = fwp_contract_create(&contract);
-       ret = fwp_contract_negotiate(contract_d, &stream->vres);
+       stream->contract_send = fwp_contract_create(&contract);
+       ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
        if (ret != 0) {
-               fprintf(stderr, "Send contract was not accepted\n");
-               exit(1);
+               stream->contract_send = NULL;
+/*             fprintf(stderr, "Send contract was not accepted\n\n\n"); */
+               return ret;
        }
 
        /* Contract for server->client stream */
+       memset(&contract, 0, sizeof(contract));
        contract.budget = stream->packet_size;
        contract.period_usec = stream->period_usec;
+       contract.deadline_usec = 3*stream->period_usec;
 
-       contract_d = fwp_contract_create(&contract);
-       ret = fwp_contract_negotiate(contract_d, &vres2);
+       stream->contract_resp = fwp_contract_create(&contract);
+       ret = fwp_contract_negotiate(stream->contract_resp, &vres2);
        if (ret != 0) {
-               fprintf(stderr, "Receive contract was not accepted\n");
-               exit(1);
+               stream->contract_resp = NULL;
+/*             fprintf(stderr, "Receive contract was not accepted\n\n\n"); */
+               return ret;
        }
 
        /* We don't use the vres at server, since the server doesn't
@@ -645,36 +666,25 @@ static void create_stream_endpoint_fwp(struct stream *stream)
                ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
                                               NULL, &stream->endpoint);
                /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
-               if (ret < 0) {
-                       perror("fwp_send_endpoint_create");
-                       exit(1);
-               }
+               if (ret < 0) error(1, errno, "fwp_send_endpoint_create");
+               
                ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
-               if (ret != 0) {
-                       perror("fwp_send_endpoint_bind");
-                       exit(1);
-               }
+               if (ret != 0) error(1, errno, "fwp_send_endpoint_bind");
+               
                ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
-               if (ret != 0) {
-                       perror("fwp_receive_endpoint_create");
-                       exit(1);
-               }
+               if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
+               
                unsigned int port;
                fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
                stream->resp_port = port;
-               printf("resp_port = %d\n", port);
 
                ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
-               if (ret) {
-                       perror("Error while creating receiver\n");
-                       exit(1);
-               }
+               if (ret) error(1, ret, "Error while creating receiver");
+               
+               stream->receiver.valid = true;
        }
        else {
-               char str[100];
-               snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
-               perror(str);
-               exit(1);
+               error(1, errno, "gethostbyname(%s)", server_addr);
        }
 
 }
@@ -690,10 +700,7 @@ static void create_stream_endpoint_native(struct stream *stream)
        if (ph)
                stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
        else {
-               char str[100];
-               snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
-               perror(str);
-               exit(1);
+               error(1, errno, "gethostbyname(%s)", server_addr);
        }
        stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
 }
@@ -728,13 +735,11 @@ calc_stream_params(struct stream *stream)
        } else {
                char buf[200];
                stream_to_text(buf, sizeof(buf), stream, 0);
-               fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
-               exit(1);
+               error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
        }
 
        if (packet_size < sizeof(struct msg_t)) {
-               fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
-               exit(1);
+               error(1, 0, "Packet size too small (min %d)", sizeof(struct msg_t));
        }
 
        stream->packet_size = packet_size;
@@ -747,8 +752,9 @@ calc_stream_params(struct stream *stream)
        } else {
                char buf[200];
                stream_to_text(buf, sizeof(buf), stream, 0);
-               fprintf(stderr, "Contract hasn't been accepted: %s\n", buf);
+               fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
                stream->bandwidth_bps = 0;
+               some_contract_not_accepted = true;
        }
 }
 
@@ -851,9 +857,62 @@ void wait_for_all_threads_to_finish_native(void)
 }
 #endif
 
+#ifdef WITH_FWP
+void init_gui()
+{
+       initscr();
+       cbreak();
+       noecho();
+}
+
+#define addfield(title, format, ...)                                   \
+       move(y, x);                                                     \
+       x+=strlen(title)+1;                                             \
+       if (i == 0) addstr(title);                                      \
+       else {                                                          \
+               snprintf(str, sizeof(str), format, __VA_ARGS__);        \
+               addstr(str);                                            \
+       }
+
+void print_status(int seconds)
+{
+       int i;
+       char str[200], s1[20];
+       int x = 0, y;
+       struct stream *s = NULL;
+       
+       for (i = 0; i <= nr_streams; i++) {
+               if (i>0) s = &streams[i-1];
+               y=i;
+               x=0;
+               addfield("Stream", "%d", i);
+               addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
+               addfield("Packet size", "%d bytes", s->packet_size);
+               addfield("Period   ", "%s", usec_to_text(s1, s->period_usec));
+               addfield("AC   ", "%s", ac_to_text[s->ac]);
+               addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
+               addfield("Received responses", "%lld", s->received);
+       }
+       refresh();
+}
+#else
+void init_gui() {}
+void print_status(int seconds)
+{
+       int ac;
+       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);
+}
+#endif
+
 int main(int argc, char *argv[])
 {
-       int ac, i, rc, seconds;
+       int i, rc, frames, seconds;
        pthread_attr_t attr;
        pthread_t thread;
        char opt;
@@ -865,14 +924,13 @@ int main(int argc, char *argv[])
                                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");
+                               char *errpos;
+                               errpos = parse_bandwidths(optarg);
+                               if (errpos != NULL) {
+                                       if (*errpos == '\0')
+                                               error(1, 0, "Bandwidth parse error - string to short");
                                        else
-                                               fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
-                                       exit(1);
+                                               error(1, 0, "Bandwidth parse error at '%s'", errpos);
                                }
                                break;
                        }
@@ -885,8 +943,7 @@ int main(int argc, char *argv[])
                        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);
+                                       error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
                                }
                                break;
                        case 'I':
@@ -894,8 +951,7 @@ int main(int argc, char *argv[])
                                break;
                        case 'j':
                                #ifdef WITH_FWP
-                               fprintf(stderr, "-j is not allowd when compiled with FWP\n");
-                               exit(1);
+                               error(1, 0, "-j is not allowd when compiled with FWP");
                                #else
                                opt_jitter = atoi(optarg);
                                #endif
@@ -934,15 +990,13 @@ int main(int argc, char *argv[])
                }
        }
        if (opt_packet_size && opt_def_period_msec) {
-               fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
-               exit(1);
+               error(1, 0, "Error: Nonzero -T and -s can't be used together!");
        }
 
        if (optind < argc) {
                server_addr = argv[optind];
        } else {
-               fprintf(stderr, "Expected server address argument\n");
-               exit(1);
+               error(1, 0, "Expected server address argument");
        }
 
        if (nr_streams == 0)
@@ -953,17 +1007,14 @@ int main(int argc, char *argv[])
        snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
 
        if ((logfd = fopen(logfname,"w+")) == NULL) {
-               fprintf(stderr,"Can not open %s\n", logfname);
-               exit(1);
+               error(1, errno ,"Can not open %s", logfname);
        }
        if (signal(SIGTERM, stopper) == SIG_ERR) {
-               perror("Error in signal registration");
-               exit(1);
+               error(1, errno, "Error in signal registration");
        }
                
        if (signal(SIGINT, stopper) == SIG_ERR) {
-               perror("Signal handler registration error");
-               exit(1);
+               error(1, errno, "Signal handler registration error");
        }
 
        struct sigaction sa;
@@ -971,8 +1022,7 @@ int main(int argc, char *argv[])
        sa.sa_flags = 0;        /* don't restart syscalls */
 
        if (sigaction(SIGUSR1, &sa, NULL) < 0) {
-               perror("sigaction error");
-               exit(1);
+               error(1, errno, "sigaction error");
        }
 
        sem_init(&sem_thread_finished, 0, 0);
@@ -982,10 +1032,10 @@ int main(int argc, char *argv[])
 #ifdef WITH_FWP
        rc = fwp_init();
        if (rc != 0) {
-               fprintf(stderr, "FWP initialization failed\n");
-               exit(1);
+               error(1, errno, "FWP initialization failed");
        }
 #else
+       int ac;
        /* create four receivers each per AC */
        for (ac = AC_NUM - 1; ac >= 0; ac--) {
                ac_sockfd[ac] = create_ac_socket(ac);
@@ -994,9 +1044,9 @@ int main(int argc, char *argv[])
                }
                rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
                if (rc) {
-                       fprintf(stderr, "Error while creating receiver %d\n",rc);
-                       return 1;
+                       error(1, rc, "Error while creating receiver");
                }
+               receivers[ac].valid = true;
        }               
 #endif
 
@@ -1007,8 +1057,7 @@ int main(int argc, char *argv[])
                memset(&ifr, 0, sizeof(ifr));
                strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
                if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
-                       fprintf(stderr, "unknown iface %s\n", opt_interface);
-                       exit(1);
+                       error(1, 0, "unknown iface %s", opt_interface);
                }
                cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
                cmsg_len = sizeof(cmsg);
@@ -1019,27 +1068,37 @@ int main(int argc, char *argv[])
                pthread_mutex_init(&s->mutex, NULL);
                calc_stream_params(s);
                rc = pthread_create(&thread, &attr, sender, (void*) s);
-               if (rc) {
-                       fprintf(stderr, "Error while creating sender %d\n",rc);
-                       return 1;
-               }
+               if (rc) error(1, rc, "Error while creating sender");
        }
-       
-       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);
+
+       if (some_contract_not_accepted) {
+               stopper();
+       } else {
+               //init_gui();
+
+               seconds = 1;
+               frames=0;
+               while (!exit_flag) {
+#ifdef WITH_FWP
+                       usleep(40000);
+#else
+                       sleep(1);
+#endif
+                       frames++;
+                       if (frames>=25) {
+                               seconds++;
+                               frames = 0;
+                       }
+                       print_status(seconds);
+                       if (seconds == opt_count_sec)
+                               stopper();
                }
-               fflush(stderr);
-               if (seconds == opt_count_sec)
-                       stopper();
        }
 
+#ifdef WITH_FWP
+       //endwin();
+       fwp_done();
+#else
        fprintf(stderr, "\nWaiting for threads to finish\n");
        wait_for_all_threads_to_finish();
 
@@ -1048,6 +1107,7 @@ int main(int argc, char *argv[])
        timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
 
        save_results(argc, argv, timespec2usec(&measure_length));
+#endif
 
        return 0;
 }