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_async(stream->endpoint, buff, stream->packet_size);
536 frsh_strerror(ret, msg, sizeof(msg));
537 fprintf(stderr, "frsh_send error: %s\n", msg);
539 return (ret == 0) ? 0 : -1;
544 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
546 struct timespec time_to_wait, current_time, period, interval;
547 unsigned period_usec = stream->period_usec;
549 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
550 /* |-------------| nominal period*/
551 if (stream->jitter) {
552 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
553 + rand() % (2*period_usec*stream->jitter/100));
555 period.tv_nsec = USEC_TO_NSEC*(period_usec);
559 timespec_add(&time_to_wait, last_send_time, &period);
560 clock_gettime(CLOCK_REALTIME,¤t_time);
561 timespec_sub(&interval,&time_to_wait,¤t_time);
562 nanosleep(&interval,NULL);
566 void* sender(void* arg)
569 unsigned long int seqn;
570 struct stream* stream = (struct stream*) arg;
575 char stream_desc[100];
576 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
577 printf("%s\n", stream_desc);
579 if (stream->bandwidth_bps == 0)
585 set_rt_prio(90-stream->ac);
589 /* buff.msg.seqn = seqn++; */
590 /* buff.msg.tos = ac_to_tos[stream->ac]; */
591 buff.msg.stream = stream-streams;
593 clock_gettime(CLOCK_REALTIME,&ts);
594 buff.msg.send_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
596 ret = send_packet(stream, &buff);
602 pthread_mutex_lock(&stream->mutex);
605 stream->really_sent++;
606 pthread_mutex_unlock(&stream->mutex);
609 printf("%d", stream->ac);
613 wait_for_next_send(stream, &ts);
616 sem_post(&sem_thread_finished);
621 static int negotiate_contract_for_stream_fwp(struct stream *stream)
623 frsh_contract_t contract;
625 frsh_rel_time_t budget, period, deadline;
626 frsh_signal_info_t si;
628 /* Contract for client->server stream */
629 frsh_contract_init(&contract);
630 frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
631 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
632 period = frsh_usec_to_rel_time(stream->period_usec);
633 frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_REGULAR);
634 deadline = frsh_usec_to_rel_time(3*stream->period_usec);
635 frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
637 ret = frsh_contract_negotiate(&contract, &stream->vres);
638 frsh_contract_destroy(&contract);
641 fprintf(stderr, "Send contract was not accepted\n");
645 /* Contract for server->client stream */
646 /* TODO: Use group negotiation for these two contracts */
647 frsh_contract_init(&contract);
648 frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
649 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
650 period = frsh_usec_to_rel_time(stream->period_usec);
651 frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_DUMMY);
652 deadline = frsh_usec_to_rel_time(3*stream->period_usec);
653 frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
655 ret = frsh_contract_negotiate(&contract, &stream->vres_rcv);
656 frsh_contract_destroy(&contract);
658 fprintf(stderr, "Receive contract was not accepted\n");
662 /* We don't use the vres at server, since the server doesn't
663 * know the parameters. Instread, server uses plain
671 static void create_stream_endpoint_fwp(struct stream *stream)
673 /* fwp_endpoint_attr_t attr; */
677 fres_block_fwp_sched *fwp_sched;
679 frsh_vres_get_contract(stream->vres, &c);
680 fwp_sched = fres_contract_get_fwp_sched(c);
682 stream->ac = fwp_sched->ac_id;
684 /* fwp_endpoint_attr_init(&attr); */
685 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
687 ph = gethostbyname(server_addr);
688 if (ph && ph->h_addr_list[0]) {
689 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
690 frsh_send_endpoint_protocol_info_t spi = { NULL, 0 };
691 frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
692 frsh_endpoint_queueing_info_t qi = { .queue_size=0, .queue_policy=FRSH_QRP_OLDEST };
693 ret = frsh_send_endpoint_create(FRSH_NETPF_FWP, a->s_addr, BASE_PORT + stream->ac,
694 spi, &stream->endpoint);
695 if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
697 ret = frsh_send_endpoint_bind(stream->vres, stream->endpoint);
698 if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
700 ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
701 &stream->resp_endpoint);
702 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
705 frsh_receive_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL, NULL);
706 stream->resp_port = port;
708 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
709 if (ret) error(1, ret, "Error while creating receiver");
711 stream->receiver.valid = true;
714 error(1, errno, "gethostbyname(%s)", server_addr);
719 static void create_stream_endpoint_native(struct stream *stream)
723 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
725 stream->rem_addr.sin_family = AF_INET;
726 ph = gethostbyname(server_addr);
728 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
730 error(1, errno, "gethostbyname(%s)", server_addr);
732 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
737 calc_stream_params(struct stream *stream)
740 unsigned period_usec;
744 /* If some parameters are not set explicitely, use default values. */
745 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
746 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
747 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
749 bandwidth = stream->bandwidth_bps;
751 /* Avoid arithmetic exception. Server thread will exit if
752 stream->bandwidth_bps == 0. */
753 if (bandwidth == 0) bandwidth = 1;
755 if (stream->packet_size) {
756 packet_size = stream->packet_size;
757 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
758 if (period_usec == 0) period_usec = 1;
759 } else if (stream->period_usec) {
760 period_usec = stream->period_usec;
761 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
764 stream_to_text(buf, sizeof(buf), stream, 0);
765 error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
768 if (packet_size < sizeof(struct msg_t)) {
769 error(1, 0, "Packet size too small (min %zd)", sizeof(struct msg_t));
772 stream->packet_size = packet_size;
773 stream->period_usec = period_usec;
774 stream->jitter = opt_jitter;
776 ret = negotiate_contract_for_stream(stream);
778 create_stream_endpoint(stream);
781 stream_to_text(buf, sizeof(buf), stream, 0);
782 fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
783 stream->bandwidth_bps = 0;
784 some_contract_not_accepted = true;
789 * Parse -b parameter.
791 * @param params String to parse
793 * @return NULL in case of success, pointer to a problematic character
796 char* parse_bandwidths(char *params)
798 struct stream *sp = &streams[nr_streams];
800 while (*params && nr_streams < MAX_STREAMS) {
801 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
805 if (strlen(params) < 2)
807 for (i=0; i<AC_NUM; i++) {
808 if (strncmp(params, ac_ids[i], 2) == 0) {
810 params+=strlen(ac_ids[i]);
818 if (*params == ':') {
821 bw = strtol(params, &next_char, 10);
822 if (next_char == params)
828 sp->bandwidth_bps = bw*Kbit;
831 long packet_size = 0;
832 if (*params == '@') {
834 period = strtol(params, &next_char, 10);
840 if (*params == '/') {
842 packet_size = strtol(params, &next_char, 10);
843 if (packet_size == 0)
851 sp->period_usec = period*MSEC_TO_USEC;
852 sp->packet_size = packet_size;
856 if (*params != '\0' && *params != ',')
867 void wait_for_all_threads_to_finish_fwp(void)
870 /* wait for all threads to finish */
871 for (i=0; i < 2*nr_streams; i++) {
872 sem_wait(&sem_thread_finished);
876 void wait_for_all_threads_to_finish_native(void)
879 /* Wait for all threads to finish */
880 for (i=0; i < nr_streams + AC_NUM; i++) {
881 sem_wait(&sem_thread_finished);
890 ul_log_domain_t *domain;
897 int locked_log(WINDOW *logwin, void *arg)
899 struct log_params *p = arg;
900 if(!(p->level&UL_LOGL_CONT)) {
901 p->level&=UL_LOGL_MASK;
903 wprintw(logwin,"<%d>", p->level);
904 if(p->domain && p->domain->name)
905 wprintw(logwin,"%s: ",p->domain->name);
907 vwprintw(logwin, p->format, p->ap);
908 wnoutrefresh(logwin);
913 wclient_log_fnc(ul_log_domain_t *domain, int level,
914 const char *format, va_list ap)
916 struct log_params p = {
924 use_window(logwin, locked_log, (void*)&p);
936 /* intrflush(stdscr, FALSE); */
937 /* keypad(stdscr, TRUE); */
939 logwin = newwin(0, 0, LINES/2, 0);
941 scrollok(logwin, TRUE);
942 /* ul_log_redir(wclient_log_fnc, 0); */
952 /* ul_log_redir(NULL, 0); */
958 #define addfield(title, format, ...) \
960 x+=strlen(title)+1; \
961 if (i == 0) addstr(title); \
963 snprintf(str, sizeof(str), format, __VA_ARGS__); \
967 void print_status_gui(int seconds)
970 char str[200], s1[20];
972 struct stream *s = NULL;
974 for (i = 0; i <= nr_streams; i++) {
975 if (i>0) s = &streams[i-1];
978 addfield("Stream", "%d", i);
979 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
980 addfield("Packet size", "%d bytes", s->packet_size);
981 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
982 addfield("AC ", "%s", ac_to_text[s->ac]);
983 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
984 addfield("Received responses", "%lld", s->received);
986 wnoutrefresh(stdscr);
990 void print_status_nogui(int seconds)
993 fprintf(stderr, "\r%3ds", seconds);
994 for (ac = 0; ac < AC_NUM; ac++) {
995 int delta = receivers[ac].received - receivers[ac].last_received;
996 receivers[ac].last_received = receivers[ac].received;
997 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
1002 int main(int argc, char *argv[])
1004 int i, rc, frames, seconds;
1005 pthread_attr_t attr;
1010 while ((opt = getopt(argc, argv, "B:b:C:c:Gg:I:j:o:qQ:s:T:")) != -1) {
1013 opt_def_bandwidth = atoi(optarg);
1017 errpos = parse_bandwidths(optarg);
1018 if (errpos != NULL) {
1019 if (*errpos == '\0')
1020 error(1, 0, "Bandwidth parse error - string to short");
1022 error(1, 0, "Bandwidth parse error at '%s'", errpos);
1027 opt_comment = optarg;
1030 opt_count_sec = atoi(optarg);
1036 opt_granularity_usec = atoi(optarg);
1037 if (opt_granularity_usec < MIN_GRANULARITY) {
1038 error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
1042 opt_interface = optarg;
1046 error(1, 0, "-j is not allowd when compiled with FWP");
1048 opt_jitter = atoi(optarg);
1052 opt_output = optarg;
1055 opt_send_buf_size = atoi(optarg);
1058 opt_wait_for_queue_is_full = true;
1061 opt_packet_size = atoi(optarg);
1064 opt_def_period_msec = atoi(optarg);
1067 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
1068 fprintf(stderr, "Options:\n");
1069 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
1070 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
1071 fprintf(stderr, " -C comment (added to header)\n");
1072 fprintf(stderr, " -c count (number of seconds to run)\n");
1073 fprintf(stderr, " -g histogram granularity [usec]\n");
1074 fprintf(stderr, " -I <interface> send packets from this interface\n");
1075 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
1076 fprintf(stderr, " -o output filename (.dat will be appended)\n");
1077 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
1078 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
1079 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
1080 fprintf(stderr, " -T default period for -b option [msec]\n");
1084 if (opt_packet_size && opt_def_period_msec) {
1085 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
1088 if (optind < argc) {
1089 server_addr = argv[optind];
1091 error(1, 0, "Expected server address argument");
1094 if (nr_streams == 0)
1095 parse_bandwidths("BE");
1097 pthread_attr_init(&attr);
1099 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1101 if ((logfd = fopen(logfname,"w+")) == NULL) {
1102 error(1, errno ,"Can not open %s", logfname);
1104 if (signal(SIGTERM, stopper) == SIG_ERR) {
1105 error(1, errno, "Error in signal registration");
1108 if (signal(SIGINT, stopper) == SIG_ERR) {
1109 error(1, errno, "Signal handler registration error");
1112 struct sigaction sa;
1113 sa.sa_handler = empty_handler;
1114 sa.sa_flags = 0; /* don't restart syscalls */
1116 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1117 error(1, errno, "sigaction error");
1120 sem_init(&sem_thread_finished, 0, 0);
1125 //ul_log_domain_arg2levels("6");
1128 error(1, errno, "FWP initialization failed");
1132 /* create four receivers each per AC */
1133 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1134 ac_sockfd[ac] = create_ac_socket(ac);
1135 if (ac_sockfd[ac] < 0) {
1138 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1140 error(1, rc, "Error while creating receiver");
1142 receivers[ac].valid = true;
1147 if (opt_interface) {
1150 memset(&ifr, 0, sizeof(ifr));
1151 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1152 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1153 error(1, 0, "unknown iface %s", opt_interface);
1155 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1156 cmsg_len = sizeof(cmsg);
1158 /* create sendpoints */
1159 for (i = 0; i < nr_streams; i++) {
1160 struct stream *s = &streams[i];
1161 pthread_mutex_init(&s->mutex, NULL);
1162 calc_stream_params(s);
1163 rc = pthread_create(&thread, &attr, sender, (void*) s);
1164 if (rc) error(1, rc, "Error while creating sender");
1167 if (some_contract_not_accepted) {
1174 while (!exit_flag) {
1182 print_status_gui(seconds);
1186 print_status_nogui(seconds);
1189 if (seconds == opt_count_sec)
1196 for (i=0; i < nr_streams; i++) {
1197 if (streams[i].vres)
1198 frsh_contract_cancel(streams[i].vres);
1199 if (streams[i].vres_rcv)
1200 frsh_contract_cancel(streams[i].vres_rcv);
1204 fprintf(stderr, "\nWaiting for threads to finish\n");
1205 wait_for_all_threads_to_finish();
1208 uint64_t end_timestamp, measure_length;
1209 clock_gettime(CLOCK_REALTIME,&ts);
1210 end_timestamp = ts.tv_sec*1000000000LL+ts.tv_nsec;
1211 measure_length = end_timestamp - reset_timestamp;
1213 save_results(argc, argv, measure_length/1000);