4 #include <sys/socket.h>
5 #include <netinet/in.h>
20 #include <semaphore.h>
21 #include <sys/ioctl.h>
30 /* static UL_LOG_CUST(ulogd); */
31 /* static ul_log_domain_t ulogd = {UL_LOGL_MSG, "wclient"}; */
32 /* UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_wclient, ulogd); */
36 #define MAX_STREAMS 10
37 #define MIN_GRANULARITY 100
39 unsigned opt_packet_size = 800;
40 int opt_send_buf_size = -1;
41 unsigned opt_period_usec = 10*MSEC_TO_USEC;
43 unsigned opt_jitter = 0;
44 char *opt_output = "delay_stats";
45 unsigned opt_count_sec = 0;
46 unsigned opt_def_bandwidth = 50;
47 unsigned opt_def_period_msec = 0;
48 int opt_granularity_usec = MIN_GRANULARITY;
49 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
50 char *opt_comment = NULL;
53 bool some_queue_is_full = false;
54 uint64_t reset_timestamp; /* [nsec] */
56 bool some_contract_not_accepted = false;
58 /* Locked when some queue is full to prevent multiple resets of
60 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
62 int ac_sockfd[AC_NUM];
67 unsigned received, last_received;
70 struct receiver receivers[AC_NUM];
76 /* maximal traffic delay in ms - 10 s*/
77 #define MAX_DELAY_US 10000000
80 unsigned csc; /* Client-server-client delay divided by 2 */
81 unsigned cs; /* Client-server delay */
82 unsigned sc; /* Server-client delay */
85 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
86 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
88 /*struct ac_stats[AC_NUM] {
89 unsigned long int min_trans_time;
90 unsigned long int sum_trans_time;
91 struct timespec recv_timestamp;
92 struct timespec send_timestamp;
96 /* Input parameters */
98 int bandwidth_bps; /* bits per second */
99 int jitter; /* percent */
100 /* Mulualy exclusive input parameters */
102 long period_usec; /* all time units are in microseconds */
104 /* Internal fields */
106 struct sockaddr_in rem_addr;
108 frsh_send_endpoint_t endpoint;
109 frsh_receive_endpoint_t resp_endpoint;
110 frsh_vres_id_t vres, vres_rcv;
112 struct receiver receiver;
116 pthread_mutex_t mutex;
117 unsigned long long sent, really_sent, received;
118 long wc_delay; /* worst-case delay */
121 static struct cmsg_ipi {
123 struct in_pktinfo ipi;
124 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
129 struct send_endpoint sepoint[] = {
130 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
131 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
132 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
133 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
134 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
138 struct stream streams[MAX_STREAMS];
140 unsigned int nr_streams = 0;
142 sem_t sem_thread_finished;
144 bool exit_flag = false;
147 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
148 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
149 #define send_packet(s, b) send_packet_fwp(s, b)
150 #define recv_packet(s, b) recv_packet_fwp(s, b)
151 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
153 #define negotiate_contract_for_stream(s) 0
154 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
155 #define send_packet(s, b) send_packet_native(s, b)
156 #define recv_packet(s, b) recv_packet_native(s, b)
157 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
165 /* Interrupt all receivers */
167 for (i=0; i < nr_streams; i++) {
168 if (streams[i].receiver.valid) pthread_kill(streams[i].receiver.thread, SIGUSR1);
171 for (i=0; i < AC_NUM; i++) {
172 pthread_kill(receivers[i].thread, SIGUSR1);
177 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
183 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
184 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
185 stream->sent, stream->sent*SEC_TO_USEC/useconds,
186 stream->received, stream->received*SEC_TO_USEC/useconds);
191 snprintf(stream_desc, n, "%"PRIdPTR": %s %s (%d bytes per %s +-%s, %d packets/s)%s",
192 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
193 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
194 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
195 (int)(SEC_TO_USEC/stream->period_usec), real);
198 void save_results(int argc, char *argv[], int useconds)
201 const int mini = 3000/opt_granularity_usec;
203 unsigned send_count[AC_NUM];
205 fprintf(stderr, "Writing data to %s... ", logfname);
208 fprintf(logfd, "# Invoked as: ");
209 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
211 fprintf(logfd, "(%s)", opt_comment);
213 fprintf(logfd, "\n");
215 if (useconds/SEC_TO_USEC != opt_count_sec) {
217 usec_to_text(buf, useconds);
218 fprintf(logfd, "# Data gathered for %s.\n", buf);
221 for (i = 0; i < nr_streams; i++) {
222 char stream_desc[200];
223 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
224 fprintf(logfd, "# Stream %s\n", stream_desc);
227 /* Find maximal delay */
229 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
230 for (ac = 0; ac < AC_NUM; ac++) {
231 if ((delay_stats[ac][maxi].csc != 0) ||
232 (delay_stats[ac][maxi].cs != 0) ||
233 (delay_stats[ac][maxi].sc != 0))
236 if (!allzeros) break;
239 if (maxi < mini) maxi = mini;
241 /* Calculate total number of sent packets per AC */
242 memset(send_count, 0, sizeof(send_count));
243 for (i = 0; i < nr_streams; i++) {
245 send_count[ac] += streams[i].sent;
250 for ( i = 0 ; i < maxi; i++) {
251 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
252 for (ac = 0; ac < AC_NUM; ac++) {
254 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
255 else val = -1; /* Don't display this ac */
256 fprintf(logfd," %lf", val);
260 fprintf(logfd,"\n\n");
263 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
266 for (ac = 0; ac < AC_NUM; ac++) {
267 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
269 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
271 if (send_count[ac] != 0) {
273 while ((delay_stats[ac][i].csc == 0) &&
274 (delay_stats[ac][i].cs == 0) &&
275 (delay_stats[ac][i].sc == 0)) i++;
277 for (i++; i < maxi+1; i++) {
278 if (memcmp(&last, &integral, sizeof(last))) {
280 snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
281 snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
282 snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
284 fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
285 integral.csc != last.csc ? buf[0] : "-",
286 integral.cs != last.cs ? buf[1] : "-",
287 integral.sc != last.sc ? buf[2] : "-"
292 integral.csc += delay_stats[ac][i-1].csc;
293 integral.sc += delay_stats[ac][i-1].sc;
294 integral.cs += delay_stats[ac][i-1].cs;
298 fprintf(logfd,"\n\n");
301 fprintf(stderr, "finished.\n");
307 int create_ac_socket(intptr_t ac)
310 unsigned int yes=1, tos;
313 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
315 error(0, errno, "Unable to open socket");
318 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
319 error(0, errno, "set non-blocking socket");
322 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
323 error(0, errno, "Unable to set socket");
327 if (opt_send_buf_size >= 0) {
328 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
329 error(0, errno, "Unable to set socket buffer size");
335 //tos = ((AC_NUM - ac) *2 - 1)*32;
337 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
338 error(0, errno, "Unable to set TOS");
350 void reset_statistics()
354 for (i = 0; i < nr_streams; i++) {
355 pthread_mutex_lock(&streams[i].mutex);
357 streams[i].really_sent = 0;
358 streams[i].received = 0;
359 pthread_mutex_unlock(&streams[i].mutex);
361 pthread_mutex_lock(&delay_stats_mutex);
362 clock_gettime(CLOCK_REALTIME, &ts);
363 reset_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
364 memset(delay_stats, 0, sizeof(delay_stats));
365 pthread_mutex_unlock(&delay_stats_mutex);
369 int recv_packet_native(intptr_t ac, struct msg_t *msg)
373 struct sockaddr_in rem_addr;
374 unsigned int rem_addr_length;
377 FD_SET(ac_sockfd[ac], &fdset);
378 rem_addr_length = sizeof(rem_addr);
381 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
383 if (errno == EINTR) continue;
384 error(0, errno, "receiver select");
387 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
388 (struct sockaddr*)&rem_addr, &rem_addr_length);
394 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
398 mlen = frsh_receive_sync(stream->resp_endpoint, msg, sizeof(*msg), &mlen, NULL);
403 void* receiver(void* arg)
406 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
407 long long int min_trans_time;
409 uint64_t send_timestamp, server_timestamp, recv_timestamp;
420 struct stream *stream = arg;
422 mlen = recv_packet_fwp(stream, &msg);
425 mlen = recv_packet_native(ac, &msg);
429 error(0, errno, "receive_packet error");
432 clock_gettime(CLOCK_REALTIME,&ts);
433 recv_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
434 send_timestamp = msg.send_timestamp;
435 server_timestamp = msg.sendback_timestamp;
437 /* Check whether this message was sent after reset_statistics() */
439 if (send_timestamp < reset_timestamp) {
440 continue; /* If so, don't count it */
443 trans_time_usec = (recv_timestamp - send_timestamp) / 2 / 1000;
444 client_to_server_usec = (server_timestamp - send_timestamp) / 1000;
445 server_to_client_usec = (recv_timestamp - server_timestamp) / 1000;
447 pthread_mutex_lock(&delay_stats_mutex);
448 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
449 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
451 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
452 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
454 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
455 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
457 pthread_mutex_unlock(&delay_stats_mutex);
459 if (trans_time_usec > streams[msg.stream].wc_delay) {
460 streams[msg.stream].wc_delay = trans_time_usec;
462 receivers[ac].received++;
464 pthread_mutex_lock(&streams[msg.stream].mutex);
465 streams[msg.stream].received++;
466 pthread_mutex_unlock(&streams[msg.stream].mutex);
468 /*if (trans_time_nsec < min_trans_time)
469 min_trans_time = trans_time_nsec;*/
470 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
471 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
472 msg.seqn, msg.tos, send_timestamp.tv_sec,\
473 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
474 recv_timestamp.tv_nsec, trans_time_msec); */
477 sem_post(&sem_thread_finished);
484 * @return -1 in case of error, 1 in case of sucessfull send and 0
485 * when all buffers are full.
489 send_packet_native(struct stream* stream, union msg_buff* buff)
495 iov.iov_len = stream->packet_size;
496 msg.msg_name = (void*)&stream->rem_addr;
497 msg.msg_namelen = sizeof(stream->rem_addr);
501 msg.msg_control = &cmsg;
502 msg.msg_controllen = cmsg_len;
506 while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
507 if (errno == EINTR) continue;
508 if (errno == EAGAIN) {
509 if (opt_wait_for_queue_is_full &&
510 !some_queue_is_full &&
511 /* We use mutex as atomic test and set */
512 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
513 some_queue_is_full = true;
519 error(0, errno, "Error while sending");
528 send_packet_fwp(struct stream* stream, union msg_buff* buff)
532 buff->msg.resp_port = htons(stream->resp_port);
533 ret = frsh_send_sync(stream->endpoint, buff, stream->packet_size);
534 return (ret == 0) ? 0 : -1;
539 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
541 struct timespec time_to_wait, current_time, period, interval;
542 unsigned period_usec = stream->period_usec;
544 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
545 /* |-------------| nominal period*/
546 if (stream->jitter) {
547 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
548 + rand() % (2*period_usec*stream->jitter/100));
550 period.tv_nsec = USEC_TO_NSEC*(period_usec);
554 timespec_add(&time_to_wait, last_send_time, &period);
555 clock_gettime(CLOCK_REALTIME,¤t_time);
556 timespec_sub(&interval,&time_to_wait,¤t_time);
557 nanosleep(&interval,NULL);
561 void* sender(void* arg)
564 unsigned long int seqn;
565 struct stream* stream = (struct stream*) arg;
570 char stream_desc[100];
571 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
572 printf("%s\n", stream_desc);
574 if (stream->bandwidth_bps == 0)
580 set_rt_prio(90-stream->ac);
584 /* buff.msg.seqn = seqn++; */
585 /* buff.msg.tos = ac_to_tos[stream->ac]; */
586 buff.msg.stream = stream-streams;
588 clock_gettime(CLOCK_REALTIME,&ts);
589 buff.msg.send_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
591 ret = send_packet(stream, &buff);
596 pthread_mutex_lock(&stream->mutex);
599 stream->really_sent++;
600 pthread_mutex_unlock(&stream->mutex);
603 printf("%d", stream->ac);
607 wait_for_next_send(stream, &ts);
610 sem_post(&sem_thread_finished);
615 static int negotiate_contract_for_stream_fwp(struct stream *stream)
617 frsh_contract_t contract;
619 frsh_rel_time_t budget, period, deadline;
620 frsh_signal_info_t si;
622 /* Contract for client->server stream */
623 frsh_contract_init(&contract);
624 frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
625 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
626 period = frsh_usec_to_rel_time(stream->period_usec);
627 frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_REGULAR);
628 deadline = frsh_usec_to_rel_time(3*stream->period_usec);
629 frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
631 ret = frsh_contract_negotiate(&contract, &stream->vres);
632 frsh_contract_destroy(&contract);
635 fprintf(stderr, "Send contract was not accepted\n");
639 /* Contract for server->client stream */
640 /* TODO: Use group negotiation for these two contracts */
641 frsh_contract_init(&contract);
642 frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
643 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
644 period = frsh_usec_to_rel_time(stream->period_usec);
645 frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_DUMMY);
646 deadline = frsh_usec_to_rel_time(3*stream->period_usec);
647 frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
649 ret = frsh_contract_negotiate(&contract, &stream->vres_rcv);
650 frsh_contract_destroy(&contract);
652 fprintf(stderr, "Receive contract was not accepted\n");
656 /* We don't use the vres at server, since the server doesn't
657 * know the parameters. Instread, server uses plain
665 static void create_stream_endpoint_fwp(struct stream *stream)
667 /* fwp_endpoint_attr_t attr; */
671 fres_block_fwp_sched *fwp_sched;
673 frsh_vres_get_contract(stream->vres, &c);
674 fwp_sched = fres_contract_get_fwp_sched(c);
676 stream->ac = fwp_sched->ac_id;
678 /* fwp_endpoint_attr_init(&attr); */
679 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
681 ph = gethostbyname(server_addr);
682 if (ph && ph->h_addr_list[0]) {
683 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
684 frsh_send_endpoint_protocol_info_t spi = { NULL, 0 };
685 frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
686 frsh_endpoint_queueing_info_t qi = { .queue_size=0, .queue_policy=FRSH_QRP_OLDEST };
687 ret = frsh_send_endpoint_create(FRSH_NETPF_FWP, a->s_addr, BASE_PORT + stream->ac,
688 spi, &stream->endpoint);
689 if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
691 ret = frsh_send_endpoint_bind(stream->vres, stream->endpoint);
692 if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
694 ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
695 &stream->resp_endpoint);
696 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
699 frsh_receive_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL, NULL);
700 stream->resp_port = port;
702 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
703 if (ret) error(1, ret, "Error while creating receiver");
705 stream->receiver.valid = true;
708 error(1, errno, "gethostbyname(%s)", server_addr);
713 static void create_stream_endpoint_native(struct stream *stream)
717 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
719 stream->rem_addr.sin_family = AF_INET;
720 ph = gethostbyname(server_addr);
722 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
724 error(1, errno, "gethostbyname(%s)", server_addr);
726 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
731 calc_stream_params(struct stream *stream)
734 unsigned period_usec;
738 /* If some parameters are not set explicitely, use default values. */
739 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
740 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
741 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
743 bandwidth = stream->bandwidth_bps;
745 /* Avoid arithmetic exception. Server thread will exit if
746 stream->bandwidth_bps == 0. */
747 if (bandwidth == 0) bandwidth = 1;
749 if (stream->packet_size) {
750 packet_size = stream->packet_size;
751 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
752 if (period_usec == 0) period_usec = 1;
753 } else if (stream->period_usec) {
754 period_usec = stream->period_usec;
755 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
758 stream_to_text(buf, sizeof(buf), stream, 0);
759 error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
762 if (packet_size < sizeof(struct msg_t)) {
763 error(1, 0, "Packet size too small (min %zd)", sizeof(struct msg_t));
766 stream->packet_size = packet_size;
767 stream->period_usec = period_usec;
768 stream->jitter = opt_jitter;
770 ret = negotiate_contract_for_stream(stream);
772 create_stream_endpoint(stream);
775 stream_to_text(buf, sizeof(buf), stream, 0);
776 fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
777 stream->bandwidth_bps = 0;
778 some_contract_not_accepted = true;
783 * Parse -b parameter.
785 * @param params String to parse
787 * @return NULL in case of success, pointer to a problematic character
790 char* parse_bandwidths(char *params)
792 struct stream *sp = &streams[nr_streams];
794 while (*params && nr_streams < MAX_STREAMS) {
795 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
799 if (strlen(params) < 2)
801 for (i=0; i<AC_NUM; i++) {
802 if (strncmp(params, ac_ids[i], 2) == 0) {
804 params+=strlen(ac_ids[i]);
812 if (*params == ':') {
815 bw = strtol(params, &next_char, 10);
816 if (next_char == params)
822 sp->bandwidth_bps = bw*Kbit;
825 long packet_size = 0;
826 if (*params == '@') {
828 period = strtol(params, &next_char, 10);
834 if (*params == '/') {
836 packet_size = strtol(params, &next_char, 10);
837 if (packet_size == 0)
845 sp->period_usec = period*MSEC_TO_USEC;
846 sp->packet_size = packet_size;
850 if (*params != '\0' && *params != ',')
861 void wait_for_all_threads_to_finish_fwp(void)
864 /* wait for all threads to finish */
865 for (i=0; i < 2*nr_streams; i++) {
866 sem_wait(&sem_thread_finished);
870 void wait_for_all_threads_to_finish_native(void)
873 /* Wait for all threads to finish */
874 for (i=0; i < nr_streams + AC_NUM; i++) {
875 sem_wait(&sem_thread_finished);
884 ul_log_domain_t *domain;
891 int locked_log(WINDOW *logwin, void *arg)
893 struct log_params *p = arg;
894 if(!(p->level&UL_LOGL_CONT)) {
895 p->level&=UL_LOGL_MASK;
897 wprintw(logwin,"<%d>", p->level);
898 if(p->domain && p->domain->name)
899 wprintw(logwin,"%s: ",p->domain->name);
901 vwprintw(logwin, p->format, p->ap);
902 wnoutrefresh(logwin);
907 wclient_log_fnc(ul_log_domain_t *domain, int level,
908 const char *format, va_list ap)
910 struct log_params p = {
918 use_window(logwin, locked_log, (void*)&p);
930 /* intrflush(stdscr, FALSE); */
931 /* keypad(stdscr, TRUE); */
933 logwin = newwin(0, 0, LINES/2, 0);
935 scrollok(logwin, TRUE);
936 /* ul_log_redir(wclient_log_fnc, 0); */
946 /* ul_log_redir(NULL, 0); */
952 #define addfield(title, format, ...) \
954 x+=strlen(title)+1; \
955 if (i == 0) addstr(title); \
957 snprintf(str, sizeof(str), format, __VA_ARGS__); \
961 void print_status_gui(int seconds)
964 char str[200], s1[20];
966 struct stream *s = NULL;
968 for (i = 0; i <= nr_streams; i++) {
969 if (i>0) s = &streams[i-1];
972 addfield("Stream", "%d", i);
973 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
974 addfield("Packet size", "%d bytes", s->packet_size);
975 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
976 addfield("AC ", "%s", ac_to_text[s->ac]);
977 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
978 addfield("Received responses", "%lld", s->received);
980 wnoutrefresh(stdscr);
984 void print_status_nogui(int seconds)
987 fprintf(stderr, "\r%3ds", seconds);
988 for (ac = 0; ac < AC_NUM; ac++) {
989 int delta = receivers[ac].received - receivers[ac].last_received;
990 receivers[ac].last_received = receivers[ac].received;
991 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
996 int main(int argc, char *argv[])
998 int i, rc, frames, seconds;
1004 while ((opt = getopt(argc, argv, "B:b:C:c:Gg:I:j:o:qQ:s:T:")) != -1) {
1007 opt_def_bandwidth = atoi(optarg);
1011 errpos = parse_bandwidths(optarg);
1012 if (errpos != NULL) {
1013 if (*errpos == '\0')
1014 error(1, 0, "Bandwidth parse error - string to short");
1016 error(1, 0, "Bandwidth parse error at '%s'", errpos);
1021 opt_comment = optarg;
1024 opt_count_sec = atoi(optarg);
1030 opt_granularity_usec = atoi(optarg);
1031 if (opt_granularity_usec < MIN_GRANULARITY) {
1032 error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
1036 opt_interface = optarg;
1040 error(1, 0, "-j is not allowd when compiled with FWP");
1042 opt_jitter = atoi(optarg);
1046 opt_output = optarg;
1049 opt_send_buf_size = atoi(optarg);
1052 opt_wait_for_queue_is_full = true;
1055 opt_packet_size = atoi(optarg);
1058 opt_def_period_msec = atoi(optarg);
1061 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
1062 fprintf(stderr, "Options:\n");
1063 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
1064 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
1065 fprintf(stderr, " -C comment (added to header)\n");
1066 fprintf(stderr, " -c count (number of seconds to run)\n");
1067 fprintf(stderr, " -g histogram granularity [usec]\n");
1068 fprintf(stderr, " -I <interface> send packets from this interface\n");
1069 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
1070 fprintf(stderr, " -o output filename (.dat will be appended)\n");
1071 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
1072 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
1073 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
1074 fprintf(stderr, " -T default period for -b option [msec]\n");
1078 if (opt_packet_size && opt_def_period_msec) {
1079 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
1082 if (optind < argc) {
1083 server_addr = argv[optind];
1085 error(1, 0, "Expected server address argument");
1088 if (nr_streams == 0)
1089 parse_bandwidths("BE");
1091 pthread_attr_init(&attr);
1093 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1095 if ((logfd = fopen(logfname,"w+")) == NULL) {
1096 error(1, errno ,"Can not open %s", logfname);
1098 if (signal(SIGTERM, stopper) == SIG_ERR) {
1099 error(1, errno, "Error in signal registration");
1102 if (signal(SIGINT, stopper) == SIG_ERR) {
1103 error(1, errno, "Signal handler registration error");
1106 struct sigaction sa;
1107 sa.sa_handler = empty_handler;
1108 sa.sa_flags = 0; /* don't restart syscalls */
1110 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1111 error(1, errno, "sigaction error");
1114 sem_init(&sem_thread_finished, 0, 0);
1119 //ul_log_domain_arg2levels("6");
1122 error(1, errno, "FWP initialization failed");
1126 /* create four receivers each per AC */
1127 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1128 ac_sockfd[ac] = create_ac_socket(ac);
1129 if (ac_sockfd[ac] < 0) {
1132 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1134 error(1, rc, "Error while creating receiver");
1136 receivers[ac].valid = true;
1141 if (opt_interface) {
1144 memset(&ifr, 0, sizeof(ifr));
1145 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1146 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1147 error(1, 0, "unknown iface %s", opt_interface);
1149 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1150 cmsg_len = sizeof(cmsg);
1152 /* create sendpoints */
1153 for (i = 0; i < nr_streams; i++) {
1154 struct stream *s = &streams[i];
1155 pthread_mutex_init(&s->mutex, NULL);
1156 calc_stream_params(s);
1157 rc = pthread_create(&thread, &attr, sender, (void*) s);
1158 if (rc) error(1, rc, "Error while creating sender");
1161 if (some_contract_not_accepted) {
1168 while (!exit_flag) {
1176 print_status_gui(seconds);
1180 print_status_nogui(seconds);
1183 if (seconds == opt_count_sec)
1190 for (i=0; i < nr_streams; i++) {
1191 if (streams[i].vres)
1192 frsh_contract_cancel(streams[i].vres);
1193 if (streams[i].vres_rcv)
1194 frsh_contract_cancel(streams[i].vres_rcv);
1198 fprintf(stderr, "\nWaiting for threads to finish\n");
1199 wait_for_all_threads_to_finish();
1202 uint64_t end_timestamp, measure_length;
1203 clock_gettime(CLOCK_REALTIME,&ts);
1204 end_timestamp = ts.tv_sec*1000000000LL+ts.tv_nsec;
1205 measure_length = end_timestamp - reset_timestamp;
1207 save_results(argc, argv, measure_length/1000);