4 #include <sys/socket.h>
5 #include <netinet/in.h>
20 #include <semaphore.h>
21 #include <sys/ioctl.h>
31 #define MAX_STREAMS 10
32 #define MIN_GRANULARITY 100
34 unsigned opt_packet_size = 800;
35 int opt_send_buf_size = -1;
36 unsigned opt_period_usec = 10*MSEC_TO_USEC;
38 unsigned opt_jitter = 0;
39 char *opt_output = "delay_stats";
40 unsigned opt_count_sec = 0;
41 unsigned opt_def_bandwidth = 50;
42 unsigned opt_def_period_msec = 0;
43 int opt_granularity_usec = MIN_GRANULARITY;
44 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
45 char *opt_comment = NULL;
47 bool some_queue_is_full = false;
48 uint64_t reset_timestamp; /* [nsec] */
50 bool some_contract_not_accepted = false;
52 /* Locked when some queue is full to prevent multiple resets of
54 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
56 int ac_sockfd[AC_NUM];
61 unsigned received, last_received;
64 struct receiver receivers[AC_NUM];
70 /* maximal traffic delay in ms - 10 s*/
71 #define MAX_DELAY_US 10000000
74 unsigned csc; /* Client-server-client delay divided by 2 */
75 unsigned cs; /* Client-server delay */
76 unsigned sc; /* Server-client delay */
79 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
80 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
82 /*struct ac_stats[AC_NUM] {
83 unsigned long int min_trans_time;
84 unsigned long int sum_trans_time;
85 struct timespec recv_timestamp;
86 struct timespec send_timestamp;
90 /* Input parameters */
92 int bandwidth_bps; /* bits per second */
93 int jitter; /* percent */
94 /* Mulualy exclusive input parameters */
96 long period_usec; /* all time units are in microseconds */
100 struct sockaddr_in rem_addr;
102 frsh_send_endpoint_t endpoint;
103 frsh_receive_endpoint_t resp_endpoint;
104 frsh_vres_id_t vres, vres_rcv;
106 struct receiver receiver;
107 long wc_delay; /* worst-case delay */
111 pthread_mutex_t mutex;
112 unsigned long long sent, really_sent, received;
115 static struct cmsg_ipi {
117 struct in_pktinfo ipi;
118 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
123 struct send_endpoint sepoint[] = {
124 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
125 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
126 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
127 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
128 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
132 struct stream streams[MAX_STREAMS];
134 unsigned int nr_streams = 0;
136 sem_t sem_thread_finished;
138 bool exit_flag = false;
141 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
142 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
143 #define send_packet(s, b) send_packet_fwp(s, b)
144 #define recv_packet(s, b) recv_packet_fwp(s, b)
145 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
147 #define negotiate_contract_for_stream(s) 0
148 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
149 #define send_packet(s, b) send_packet_native(s, b)
150 #define recv_packet(s, b) recv_packet_native(s, b)
151 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
159 /* Interrupt all receivers */
161 for (i=0; i < nr_streams; i++) {
162 if (streams[i].receiver.valid) pthread_kill(streams[i].receiver.thread, SIGUSR1);
165 for (i=0; i < AC_NUM; i++) {
166 pthread_kill(receivers[i].thread, SIGUSR1);
171 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
177 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
178 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
179 stream->sent, stream->sent*SEC_TO_USEC/useconds,
180 stream->received, stream->received*SEC_TO_USEC/useconds);
185 snprintf(stream_desc, n, "%"PRIdPTR": %s %s (%d bytes per %s +-%s, %d packets/s)%s",
186 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
187 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
188 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
189 (int)(SEC_TO_USEC/stream->period_usec), real);
192 void save_results(int argc, char *argv[], int useconds)
195 const int mini = 3000/opt_granularity_usec;
197 unsigned send_count[AC_NUM];
199 fprintf(stderr, "Writing data to %s... ", logfname);
202 fprintf(logfd, "# Invoked as: ");
203 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
205 fprintf(logfd, "(%s)", opt_comment);
207 fprintf(logfd, "\n");
209 if (useconds/SEC_TO_USEC != opt_count_sec) {
211 usec_to_text(buf, useconds);
212 fprintf(logfd, "# Data gathered for %s.\n", buf);
215 for (i = 0; i < nr_streams; i++) {
216 char stream_desc[200];
217 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
218 fprintf(logfd, "# Stream %s\n", stream_desc);
221 /* Find maximal delay */
223 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
224 for (ac = 0; ac < AC_NUM; ac++) {
225 if ((delay_stats[ac][maxi].csc != 0) ||
226 (delay_stats[ac][maxi].cs != 0) ||
227 (delay_stats[ac][maxi].sc != 0))
230 if (!allzeros) break;
233 if (maxi < mini) maxi = mini;
235 /* Calculate total number of sent packets per AC */
236 memset(send_count, 0, sizeof(send_count));
237 for (i = 0; i < nr_streams; i++) {
239 send_count[ac] += streams[i].sent;
244 for ( i = 0 ; i < maxi; i++) {
245 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
246 for (ac = 0; ac < AC_NUM; ac++) {
248 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
249 else val = -1; /* Don't display this ac */
250 fprintf(logfd," %lf", val);
254 fprintf(logfd,"\n\n");
257 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
260 for (ac = 0; ac < AC_NUM; ac++) {
261 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
263 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
265 if (send_count[ac] != 0) {
267 while ((delay_stats[ac][i].csc == 0) &&
268 (delay_stats[ac][i].cs == 0) &&
269 (delay_stats[ac][i].sc == 0)) i++;
271 for (i++; i < maxi+1; i++) {
272 if (memcmp(&last, &integral, sizeof(last))) {
274 snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
275 snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
276 snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
278 fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
279 integral.csc != last.csc ? buf[0] : "-",
280 integral.cs != last.cs ? buf[1] : "-",
281 integral.sc != last.sc ? buf[2] : "-"
286 integral.csc += delay_stats[ac][i-1].csc;
287 integral.sc += delay_stats[ac][i-1].sc;
288 integral.cs += delay_stats[ac][i-1].cs;
292 fprintf(logfd,"\n\n");
295 fprintf(stderr, "finished.\n");
301 int create_ac_socket(intptr_t ac)
304 unsigned int yes=1, tos;
307 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
309 error(0, errno, "Unable to open socket");
312 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
313 error(0, errno, "set non-blocking socket");
316 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
317 error(0, errno, "Unable to set socket");
321 if (opt_send_buf_size >= 0) {
322 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
323 error(0, errno, "Unable to set socket buffer size");
329 //tos = ((AC_NUM - ac) *2 - 1)*32;
331 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
332 error(0, errno, "Unable to set TOS");
344 void reset_statistics()
348 for (i = 0; i < nr_streams; i++) {
349 pthread_mutex_lock(&streams[i].mutex);
351 streams[i].really_sent = 0;
352 streams[i].received = 0;
353 pthread_mutex_unlock(&streams[i].mutex);
355 pthread_mutex_lock(&delay_stats_mutex);
356 clock_gettime(CLOCK_REALTIME, &ts);
357 reset_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
358 memset(delay_stats, 0, sizeof(delay_stats));
359 pthread_mutex_unlock(&delay_stats_mutex);
363 int recv_packet_native(intptr_t ac, struct msg_t *msg)
367 struct sockaddr_in rem_addr;
368 unsigned int rem_addr_length;
371 FD_SET(ac_sockfd[ac], &fdset);
372 rem_addr_length = sizeof(rem_addr);
375 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
377 if (errno == EINTR) continue;
378 error(0, errno, "receiver select");
381 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
382 (struct sockaddr*)&rem_addr, &rem_addr_length);
388 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
392 mlen = frsh_receive_sync(stream->resp_endpoint, msg, sizeof(*msg), &mlen, NULL);
397 void* receiver(void* arg)
400 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
401 long long int min_trans_time;
403 uint64_t send_timestamp, server_timestamp, recv_timestamp;
414 struct stream *stream = arg;
416 mlen = recv_packet_fwp(stream, &msg);
419 mlen = recv_packet_native(ac, &msg);
423 error(0, errno, "receive_packet error");
426 clock_gettime(CLOCK_REALTIME,&ts);
427 recv_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
428 send_timestamp = msg.send_timestamp;
429 server_timestamp = msg.sendback_timestamp;
431 /* Check whether this message was sent after reset_statistics() */
433 if (send_timestamp < reset_timestamp) {
434 continue; /* If so, don't count it */
437 trans_time_usec = (recv_timestamp - send_timestamp) / 2 / 1000;
438 client_to_server_usec = (server_timestamp - send_timestamp) / 1000;
439 server_to_client_usec = (recv_timestamp - server_timestamp) / 1000;
441 pthread_mutex_lock(&delay_stats_mutex);
442 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
443 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
445 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
446 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
448 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
449 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
451 pthread_mutex_unlock(&delay_stats_mutex);
454 if (trans_time_usec > stream->wc_delay) {
455 stream->wc_delay = trans_time_usec;
458 receivers[ac].received++;
460 pthread_mutex_lock(&streams[msg.stream].mutex);
461 streams[msg.stream].received++;
462 pthread_mutex_unlock(&streams[msg.stream].mutex);
464 /*if (trans_time_nsec < min_trans_time)
465 min_trans_time = trans_time_nsec;*/
466 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
467 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
468 msg.seqn, msg.tos, send_timestamp.tv_sec,\
469 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
470 recv_timestamp.tv_nsec, trans_time_msec); */
473 sem_post(&sem_thread_finished);
480 * @return -1 in case of error, 1 in case of sucessfull send and 0
481 * when all buffers are full.
485 send_packet_native(struct stream* stream, union msg_buff* buff)
491 iov.iov_len = stream->packet_size;
492 msg.msg_name = (void*)&stream->rem_addr;
493 msg.msg_namelen = sizeof(stream->rem_addr);
497 msg.msg_control = &cmsg;
498 msg.msg_controllen = cmsg_len;
502 while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
503 if (errno == EINTR) continue;
504 if (errno == EAGAIN) {
505 if (opt_wait_for_queue_is_full &&
506 !some_queue_is_full &&
507 /* We use mutex as atomic test and set */
508 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
509 some_queue_is_full = true;
515 error(0, errno, "Error while sending");
524 send_packet_fwp(struct stream* stream, union msg_buff* buff)
528 buff->msg.resp_port = htons(stream->resp_port);
529 ret = frsh_send_sync(stream->endpoint, buff, stream->packet_size);
531 return (ret == 0) ? 0 : -1;
536 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
538 struct timespec time_to_wait, current_time, period, interval;
539 unsigned period_usec = stream->period_usec;
541 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
542 /* |-------------| nominal period*/
543 if (stream->jitter) {
544 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
545 + rand() % (2*period_usec*stream->jitter/100));
547 period.tv_nsec = USEC_TO_NSEC*(period_usec);
551 timespec_add(&time_to_wait, last_send_time, &period);
552 clock_gettime(CLOCK_REALTIME,¤t_time);
553 timespec_sub(&interval,&time_to_wait,¤t_time);
554 nanosleep(&interval,NULL);
558 void* sender(void* arg)
561 unsigned long int seqn;
562 struct stream* stream = (struct stream*) arg;
567 char stream_desc[100];
568 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
569 printf("%s\n", stream_desc);
571 if (stream->bandwidth_bps == 0)
577 set_rt_prio(90-stream->ac);
581 /* buff.msg.seqn = seqn++; */
582 /* buff.msg.tos = ac_to_tos[stream->ac]; */
583 buff.msg.stream = stream-streams;
585 clock_gettime(CLOCK_REALTIME,&ts);
586 buff.msg.send_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
588 ret = send_packet(stream, &buff);
593 pthread_mutex_lock(&stream->mutex);
596 stream->really_sent++;
597 pthread_mutex_unlock(&stream->mutex);
600 printf("%d", stream->ac);
604 wait_for_next_send(stream, &ts);
607 sem_post(&sem_thread_finished);
612 static int negotiate_contract_for_stream_fwp(struct stream *stream)
614 frsh_contract_t contract;
616 frsh_rel_time_t budget, period, deadline;
617 frsh_signal_info_t si;
619 /* Contract for client->server stream */
620 frsh_contract_init(&contract);
621 frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
622 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
623 period = frsh_usec_to_rel_time(stream->period_usec);
624 frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_REGULAR);
625 deadline = frsh_usec_to_rel_time(3*stream->period_usec);
626 frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
628 ret = frsh_contract_negotiate(&contract, &stream->vres);
629 frsh_contract_destroy(&contract);
632 fprintf(stderr, "Send contract was not accepted\n");
636 /* Contract for server->client stream */
637 /* TODO: Use group negotiation for these two contracts */
638 frsh_contract_init(&contract);
639 frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
640 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
641 period = frsh_usec_to_rel_time(stream->period_usec);
642 frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_DUMMY);
643 deadline = frsh_usec_to_rel_time(3*stream->period_usec);
644 frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
646 ret = frsh_contract_negotiate(&contract, &stream->vres_rcv);
647 frsh_contract_destroy(&contract);
649 fprintf(stderr, "Receive contract was not accepted\n");
653 /* We don't use the vres at server, since the server doesn't
654 * know the parameters. Instread, server uses plain
662 static void create_stream_endpoint_fwp(struct stream *stream)
664 /* fwp_endpoint_attr_t attr; */
668 fres_block_fwp_sched *fwp_sched;
670 frsh_vres_get_contract(stream->vres, &c);
671 fwp_sched = fres_contract_get_fwp_sched(c);
673 stream->ac = fwp_sched->ac_id;
675 /* fwp_endpoint_attr_init(&attr); */
676 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
678 ph = gethostbyname(server_addr);
679 if (ph && ph->h_addr_list[0]) {
680 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
681 frsh_send_endpoint_protocol_info_t spi = { NULL, 0 };
682 frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
683 frsh_endpoint_queueing_info_t qi = { .queue_size=0, .queue_policy=FRSH_QRP_OLDEST };
684 ret = frsh_send_endpoint_create(FRSH_NETPF_FWP, a->s_addr, BASE_PORT + stream->ac,
685 spi, &stream->endpoint);
686 if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
688 ret = frsh_send_endpoint_bind(stream->vres, stream->endpoint);
689 if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
691 ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
692 &stream->resp_endpoint);
693 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
696 frsh_receive_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL, NULL);
697 stream->resp_port = port;
699 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
700 if (ret) error(1, ret, "Error while creating receiver");
702 stream->receiver.valid = true;
705 error(1, errno, "gethostbyname(%s)", server_addr);
710 static void create_stream_endpoint_native(struct stream *stream)
714 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
716 stream->rem_addr.sin_family = AF_INET;
717 ph = gethostbyname(server_addr);
719 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
721 error(1, errno, "gethostbyname(%s)", server_addr);
723 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
728 calc_stream_params(struct stream *stream)
731 unsigned period_usec;
735 /* If some parameters are not set explicitely, use default values. */
736 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
737 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
738 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
740 bandwidth = stream->bandwidth_bps;
742 /* Avoid arithmetic exception. Server thread will exit if
743 stream->bandwidth_bps == 0. */
744 if (bandwidth == 0) bandwidth = 1;
746 if (stream->packet_size) {
747 packet_size = stream->packet_size;
748 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
749 if (period_usec == 0) period_usec = 1;
750 } else if (stream->period_usec) {
751 period_usec = stream->period_usec;
752 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
755 stream_to_text(buf, sizeof(buf), stream, 0);
756 error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
759 if (packet_size < sizeof(struct msg_t)) {
760 error(1, 0, "Packet size too small (min %zd)", sizeof(struct msg_t));
763 stream->packet_size = packet_size;
764 stream->period_usec = period_usec;
765 stream->jitter = opt_jitter;
767 ret = negotiate_contract_for_stream(stream);
769 create_stream_endpoint(stream);
772 stream_to_text(buf, sizeof(buf), stream, 0);
773 fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
774 stream->bandwidth_bps = 0;
775 some_contract_not_accepted = true;
780 * Parse -b parameter.
782 * @param params String to parse
784 * @return NULL in case of success, pointer to a problematic character
787 char* parse_bandwidths(char *params)
789 struct stream *sp = &streams[nr_streams];
791 while (*params && nr_streams < MAX_STREAMS) {
792 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
796 if (strlen(params) < 2)
798 for (i=0; i<AC_NUM; i++) {
799 if (strncmp(params, ac_ids[i], 2) == 0) {
801 params+=strlen(ac_ids[i]);
809 if (*params == ':') {
812 bw = strtol(params, &next_char, 10);
813 if (next_char == params)
819 sp->bandwidth_bps = bw*Kbit;
822 long packet_size = 0;
823 if (*params == '@') {
825 period = strtol(params, &next_char, 10);
831 if (*params == '/') {
833 packet_size = strtol(params, &next_char, 10);
834 if (packet_size == 0)
842 sp->period_usec = period*MSEC_TO_USEC;
843 sp->packet_size = packet_size;
847 if (*params != '\0' && *params != ',')
858 void wait_for_all_threads_to_finish_fwp(void)
861 /* Wait for all threads to finish */
863 /* for (i=0; i < 2*nr_streams; i++) { */
864 /* sem_wait(&sem_thread_finished); */
868 void wait_for_all_threads_to_finish_native(void)
871 /* Wait for all threads to finish */
872 for (i=0; i < nr_streams + AC_NUM; i++) {
873 sem_wait(&sem_thread_finished);
886 #define addfield(title, format, ...) \
888 x+=strlen(title)+1; \
889 if (i == 0) addstr(title); \
891 snprintf(str, sizeof(str), format, __VA_ARGS__); \
895 void print_status(int seconds)
898 char str[200], s1[20];
900 struct stream *s = NULL;
902 for (i = 0; i <= nr_streams; i++) {
903 if (i>0) s = &streams[i-1];
906 addfield("Stream", "%d", i);
907 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
908 addfield("Packet size", "%d bytes", s->packet_size);
909 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
910 addfield("AC ", "%s", ac_to_text[s->ac]);
911 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
912 addfield("Received responses", "%lld", s->received);
918 void print_status(int seconds)
921 fprintf(stderr, "\r%3ds", seconds);
922 for (ac = 0; ac < AC_NUM; ac++) {
923 int delta = receivers[ac].received - receivers[ac].last_received;
924 receivers[ac].last_received = receivers[ac].received;
925 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
931 int main(int argc, char *argv[])
933 int i, rc, frames, seconds;
939 while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
942 opt_def_bandwidth = atoi(optarg);
946 errpos = parse_bandwidths(optarg);
947 if (errpos != NULL) {
949 error(1, 0, "Bandwidth parse error - string to short");
951 error(1, 0, "Bandwidth parse error at '%s'", errpos);
956 opt_comment = optarg;
959 opt_count_sec = atoi(optarg);
962 opt_granularity_usec = atoi(optarg);
963 if (opt_granularity_usec < MIN_GRANULARITY) {
964 error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
968 opt_interface = optarg;
972 error(1, 0, "-j is not allowd when compiled with FWP");
974 opt_jitter = atoi(optarg);
981 opt_send_buf_size = atoi(optarg);
984 opt_wait_for_queue_is_full = true;
987 opt_packet_size = atoi(optarg);
990 opt_def_period_msec = atoi(optarg);
993 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
994 fprintf(stderr, "Options:\n");
995 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
996 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
997 fprintf(stderr, " -C comment (added to header)\n");
998 fprintf(stderr, " -c count (number of seconds to run)\n");
999 fprintf(stderr, " -g histogram granularity [usec]\n");
1000 fprintf(stderr, " -I <interface> send packets from this interface\n");
1001 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
1002 fprintf(stderr, " -o output filename (.dat will be appended)\n");
1003 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
1004 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
1005 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
1006 fprintf(stderr, " -T default period for -b option [msec]\n");
1010 if (opt_packet_size && opt_def_period_msec) {
1011 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
1014 if (optind < argc) {
1015 server_addr = argv[optind];
1017 error(1, 0, "Expected server address argument");
1020 if (nr_streams == 0)
1021 parse_bandwidths("BE");
1023 pthread_attr_init(&attr);
1025 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1027 if ((logfd = fopen(logfname,"w+")) == NULL) {
1028 error(1, errno ,"Can not open %s", logfname);
1030 if (signal(SIGTERM, stopper) == SIG_ERR) {
1031 error(1, errno, "Error in signal registration");
1034 if (signal(SIGINT, stopper) == SIG_ERR) {
1035 error(1, errno, "Signal handler registration error");
1038 struct sigaction sa;
1039 sa.sa_handler = empty_handler;
1040 sa.sa_flags = 0; /* don't restart syscalls */
1042 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1043 error(1, errno, "sigaction error");
1046 sem_init(&sem_thread_finished, 0, 0);
1053 error(1, errno, "FWP initialization failed");
1057 /* create four receivers each per AC */
1058 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1059 ac_sockfd[ac] = create_ac_socket(ac);
1060 if (ac_sockfd[ac] < 0) {
1063 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1065 error(1, rc, "Error while creating receiver");
1067 receivers[ac].valid = true;
1072 if (opt_interface) {
1075 memset(&ifr, 0, sizeof(ifr));
1076 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1077 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1078 error(1, 0, "unknown iface %s", opt_interface);
1080 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1081 cmsg_len = sizeof(cmsg);
1083 /* create sendpoints */
1084 for (i = 0; i < nr_streams; i++) {
1085 struct stream *s = &streams[i];
1086 pthread_mutex_init(&s->mutex, NULL);
1087 calc_stream_params(s);
1088 rc = pthread_create(&thread, &attr, sender, (void*) s);
1089 if (rc) error(1, rc, "Error while creating sender");
1092 if (some_contract_not_accepted) {
1099 while (!exit_flag) {
1110 print_status(seconds);
1111 if (seconds == opt_count_sec)
1118 for (i=0; i < nr_streams; i++) {
1119 if (streams[i].vres)
1120 frsh_contract_cancel(streams[i].vres);
1121 if (streams[i].vres_rcv)
1122 frsh_contract_cancel(streams[i].vres_rcv);
1125 fprintf(stderr, "\nWaiting for threads to finish\n");
1126 wait_for_all_threads_to_finish();
1129 uint64_t end_timestamp, measure_length;
1130 clock_gettime(CLOCK_REALTIME,&ts);
1131 end_timestamp = ts.tv_sec*1000000000LL+ts.tv_nsec;
1132 measure_length = end_timestamp - reset_timestamp;
1134 save_results(argc, argv, measure_length/1000);