4 #include <sys/socket.h>
5 #include <netinet/in.h>
20 #include <semaphore.h>
21 #include <sys/ioctl.h>
27 #include <ul_logreg.h>
34 /* static UL_LOG_CUST(ulogd); */
35 /* static ul_log_domain_t ulogd = {UL_LOGL_MSG, "wclient"}; */
36 /* UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_wclient, ulogd); */
40 #define MAX_STREAMS 10
41 #define MIN_GRANULARITY 100
43 unsigned opt_packet_size = 800;
44 int opt_send_buf_size = -1;
45 unsigned opt_period_usec = 10*MSEC_TO_USEC;
47 unsigned opt_jitter = 0;
48 char *opt_output = "delay_stats";
49 unsigned opt_count_sec = 0;
50 unsigned opt_def_bandwidth = 50;
51 unsigned opt_def_period_msec = 0;
52 int opt_granularity_usec = MIN_GRANULARITY;
53 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
54 char *opt_comment = NULL;
57 bool some_queue_is_full = false;
58 uint64_t reset_timestamp; /* [nsec] */
60 bool some_contract_not_accepted = false;
62 /* Locked when some queue is full to prevent multiple resets of
64 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
66 int ac_sockfd[AC_NUM];
71 unsigned received, last_received;
74 struct receiver receivers[AC_NUM];
80 /* maximal traffic delay in ms - 10 s*/
81 #define MAX_DELAY_US 10000000
84 unsigned csc; /* Client-server-client delay divided by 2 */
85 unsigned cs; /* Client-server delay */
86 unsigned sc; /* Server-client delay */
89 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
90 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
92 /*struct ac_stats[AC_NUM] {
93 unsigned long int min_trans_time;
94 unsigned long int sum_trans_time;
95 struct timespec recv_timestamp;
96 struct timespec send_timestamp;
100 /* Input parameters */
102 int bandwidth_bps; /* bits per second */
103 int jitter; /* percent */
104 /* Mulualy exclusive input parameters */
106 long period_usec; /* all time units are in microseconds */
108 /* Internal fields */
110 struct sockaddr_in rem_addr;
112 frsh_send_endpoint_t endpoint;
113 frsh_receive_endpoint_t resp_endpoint;
114 frsh_vres_id_t vres, vres_rcv;
116 struct receiver receiver;
120 pthread_mutex_t mutex;
121 unsigned long long sent, really_sent, received;
122 long wc_delay; /* worst-case delay */
125 static struct cmsg_ipi {
127 struct in_pktinfo ipi;
128 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
133 struct send_endpoint sepoint[] = {
134 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
135 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
136 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
137 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
138 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
142 struct stream streams[MAX_STREAMS];
144 unsigned int nr_streams = 0;
146 sem_t sem_thread_finished;
148 bool exit_flag = false;
151 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
152 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
153 #define send_packet(s, b) send_packet_fwp(s, b)
154 #define recv_packet(s, b) recv_packet_fwp(s, b)
155 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
157 #define negotiate_contract_for_stream(s) 0
158 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
159 #define send_packet(s, b) send_packet_native(s, b)
160 #define recv_packet(s, b) recv_packet_native(s, b)
161 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
169 /* Interrupt all receivers */
171 for (i=0; i < nr_streams; i++) {
172 if (streams[i].receiver.valid) pthread_kill(streams[i].receiver.thread, SIGUSR1);
175 for (i=0; i < AC_NUM; i++) {
176 pthread_kill(receivers[i].thread, SIGUSR1);
181 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
187 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
188 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
189 stream->sent, stream->sent*SEC_TO_USEC/useconds,
190 stream->received, stream->received*SEC_TO_USEC/useconds);
195 snprintf(stream_desc, n, "%"PRIdPTR": %s %s (%d bytes per %s +-%s, %d packets/s)%s",
196 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
197 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
198 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
199 (int)(SEC_TO_USEC/stream->period_usec), real);
202 void save_results(int argc, char *argv[], int useconds)
205 const int mini = 3000/opt_granularity_usec;
207 unsigned send_count[AC_NUM];
209 fprintf(stderr, "Writing data to %s... ", logfname);
212 fprintf(logfd, "# Invoked as: ");
213 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
215 fprintf(logfd, "(%s)", opt_comment);
217 fprintf(logfd, "\n");
219 if (useconds/SEC_TO_USEC != opt_count_sec) {
221 usec_to_text(buf, useconds);
222 fprintf(logfd, "# Data gathered for %s.\n", buf);
225 for (i = 0; i < nr_streams; i++) {
226 char stream_desc[200];
227 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
228 fprintf(logfd, "# Stream %s\n", stream_desc);
231 /* Find maximal delay */
233 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
234 for (ac = 0; ac < AC_NUM; ac++) {
235 if ((delay_stats[ac][maxi].csc != 0) ||
236 (delay_stats[ac][maxi].cs != 0) ||
237 (delay_stats[ac][maxi].sc != 0))
240 if (!allzeros) break;
243 if (maxi < mini) maxi = mini;
245 /* Calculate total number of sent packets per AC */
246 memset(send_count, 0, sizeof(send_count));
247 for (i = 0; i < nr_streams; i++) {
249 send_count[ac] += streams[i].sent;
254 for ( i = 0 ; i < maxi; i++) {
255 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
256 for (ac = 0; ac < AC_NUM; ac++) {
258 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
259 else val = -1; /* Don't display this ac */
260 fprintf(logfd," %lf", val);
264 fprintf(logfd,"\n\n");
267 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
270 for (ac = 0; ac < AC_NUM; ac++) {
271 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
273 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
275 if (send_count[ac] != 0) {
277 while ((delay_stats[ac][i].csc == 0) &&
278 (delay_stats[ac][i].cs == 0) &&
279 (delay_stats[ac][i].sc == 0)) i++;
281 for (i++; i < maxi+1; i++) {
282 if (memcmp(&last, &integral, sizeof(last))) {
284 snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
285 snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
286 snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
288 fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
289 integral.csc != last.csc ? buf[0] : "-",
290 integral.cs != last.cs ? buf[1] : "-",
291 integral.sc != last.sc ? buf[2] : "-"
296 integral.csc += delay_stats[ac][i-1].csc;
297 integral.sc += delay_stats[ac][i-1].sc;
298 integral.cs += delay_stats[ac][i-1].cs;
302 fprintf(logfd,"\n\n");
305 fprintf(stderr, "finished.\n");
311 int create_ac_socket(intptr_t ac)
314 unsigned int yes=1, tos;
317 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
319 error(0, errno, "Unable to open socket");
322 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
323 error(0, errno, "set non-blocking socket");
326 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
327 error(0, errno, "Unable to set socket");
331 if (opt_send_buf_size >= 0) {
332 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
333 error(0, errno, "Unable to set socket buffer size");
339 //tos = ((AC_NUM - ac) *2 - 1)*32;
341 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
342 error(0, errno, "Unable to set TOS");
354 void reset_statistics()
358 for (i = 0; i < nr_streams; i++) {
359 pthread_mutex_lock(&streams[i].mutex);
361 streams[i].really_sent = 0;
362 streams[i].received = 0;
363 pthread_mutex_unlock(&streams[i].mutex);
365 pthread_mutex_lock(&delay_stats_mutex);
366 clock_gettime(CLOCK_REALTIME, &ts);
367 reset_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
368 memset(delay_stats, 0, sizeof(delay_stats));
369 pthread_mutex_unlock(&delay_stats_mutex);
373 int recv_packet_native(intptr_t ac, struct msg_t *msg)
377 struct sockaddr_in rem_addr;
378 unsigned int rem_addr_length;
381 FD_SET(ac_sockfd[ac], &fdset);
382 rem_addr_length = sizeof(rem_addr);
385 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
387 if (errno == EINTR) continue;
388 error(0, errno, "receiver select");
391 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
392 (struct sockaddr*)&rem_addr, &rem_addr_length);
398 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
402 mlen = frsh_receive_sync(stream->resp_endpoint, msg, sizeof(*msg), &mlen, NULL);
407 void* receiver(void* arg)
410 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
411 long long int min_trans_time;
413 uint64_t send_timestamp, server_timestamp, recv_timestamp;
424 struct stream *stream = arg;
426 mlen = recv_packet_fwp(stream, &msg);
429 mlen = recv_packet_native(ac, &msg);
433 error(0, errno, "receive_packet error");
436 clock_gettime(CLOCK_REALTIME,&ts);
437 recv_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
438 send_timestamp = msg.send_timestamp;
439 server_timestamp = msg.sendback_timestamp;
441 /* Check whether this message was sent after reset_statistics() */
443 if (send_timestamp < reset_timestamp) {
444 continue; /* If so, don't count it */
447 trans_time_usec = (recv_timestamp - send_timestamp) / 2 / 1000;
448 client_to_server_usec = (server_timestamp - send_timestamp) / 1000;
449 server_to_client_usec = (recv_timestamp - server_timestamp) / 1000;
451 pthread_mutex_lock(&delay_stats_mutex);
452 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
453 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
455 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
456 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
458 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
459 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
461 pthread_mutex_unlock(&delay_stats_mutex);
463 if (trans_time_usec > streams[msg.stream].wc_delay) {
464 streams[msg.stream].wc_delay = trans_time_usec;
466 receivers[ac].received++;
468 pthread_mutex_lock(&streams[msg.stream].mutex);
469 streams[msg.stream].received++;
470 pthread_mutex_unlock(&streams[msg.stream].mutex);
472 /*if (trans_time_nsec < min_trans_time)
473 min_trans_time = trans_time_nsec;*/
474 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
475 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
476 msg.seqn, msg.tos, send_timestamp.tv_sec,\
477 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
478 recv_timestamp.tv_nsec, trans_time_msec); */
481 sem_post(&sem_thread_finished);
488 * @return -1 in case of error, 1 in case of sucessfull send and 0
489 * when all buffers are full.
493 send_packet_native(struct stream* stream, union msg_buff* buff)
499 iov.iov_len = stream->packet_size;
500 msg.msg_name = (void*)&stream->rem_addr;
501 msg.msg_namelen = sizeof(stream->rem_addr);
505 msg.msg_control = &cmsg;
506 msg.msg_controllen = cmsg_len;
510 while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
511 if (errno == EINTR) continue;
512 if (errno == EAGAIN) {
513 if (opt_wait_for_queue_is_full &&
514 !some_queue_is_full &&
515 /* We use mutex as atomic test and set */
516 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
517 some_queue_is_full = true;
523 error(0, errno, "Error while sending");
532 send_packet_fwp(struct stream* stream, union msg_buff* buff)
536 buff->msg.resp_port = htons(stream->resp_port);
537 ret = frsh_send_async(stream->endpoint, buff, stream->packet_size);
540 frsh_strerror(ret, msg, sizeof(msg));
541 fprintf(stderr, "frsh_send error: %s\n", msg);
543 return (ret == 0) ? 0 : -1;
548 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
550 struct timespec time_to_wait, current_time, period, interval;
551 unsigned period_usec = stream->period_usec;
553 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
554 /* |-------------| nominal period*/
555 if (stream->jitter) {
556 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
557 + rand() % (2*period_usec*stream->jitter/100));
559 period.tv_nsec = USEC_TO_NSEC*(period_usec);
563 timespec_add(&time_to_wait, last_send_time, &period);
564 clock_gettime(CLOCK_REALTIME,¤t_time);
565 timespec_sub(&interval,&time_to_wait,¤t_time);
566 nanosleep(&interval,NULL);
570 void* sender(void* arg)
573 unsigned long int seqn;
574 struct stream* stream = (struct stream*) arg;
579 char stream_desc[100];
580 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
581 printf("%s\n", stream_desc);
583 if (stream->bandwidth_bps == 0)
589 set_rt_prio(90-stream->ac);
593 /* buff.msg.seqn = seqn++; */
594 /* buff.msg.tos = ac_to_tos[stream->ac]; */
595 buff.msg.stream = stream-streams;
597 clock_gettime(CLOCK_REALTIME,&ts);
598 buff.msg.send_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
600 ret = send_packet(stream, &buff);
606 pthread_mutex_lock(&stream->mutex);
609 stream->really_sent++;
610 pthread_mutex_unlock(&stream->mutex);
613 printf("%d", stream->ac);
617 wait_for_next_send(stream, &ts);
620 sem_post(&sem_thread_finished);
625 static int negotiate_contract_for_stream_fwp(struct stream *stream)
627 frsh_contract_t contract;
629 frsh_rel_time_t budget, period, deadline;
630 frsh_signal_info_t si;
632 /* Contract for client->server stream */
633 frsh_contract_init(&contract);
634 frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
635 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
636 period = frsh_usec_to_rel_time(stream->period_usec);
637 frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_REGULAR);
638 deadline = frsh_usec_to_rel_time(3*stream->period_usec);
639 frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
641 ret = frsh_contract_negotiate(&contract, &stream->vres);
642 frsh_contract_destroy(&contract);
645 fprintf(stderr, "Send contract was not accepted\n");
649 /* Contract for server->client stream */
650 /* TODO: Use group negotiation for these two contracts */
651 frsh_contract_init(&contract);
652 frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
653 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
654 period = frsh_usec_to_rel_time(stream->period_usec);
655 frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_DUMMY);
656 deadline = frsh_usec_to_rel_time(3*stream->period_usec);
657 frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
659 ret = frsh_contract_negotiate(&contract, &stream->vres_rcv);
660 frsh_contract_destroy(&contract);
662 fprintf(stderr, "Receive contract was not accepted\n");
666 /* We don't use the vres at server, since the server doesn't
667 * know the parameters. Instread, server uses plain
675 static void create_stream_endpoint_fwp(struct stream *stream)
677 /* fwp_endpoint_attr_t attr; */
681 fres_block_fwp_sched *fwp_sched;
683 frsh_vres_get_contract(stream->vres, &c);
684 fwp_sched = fres_contract_get_fwp_sched(c);
686 stream->ac = fwp_sched->ac_id;
688 /* fwp_endpoint_attr_init(&attr); */
689 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
691 ph = gethostbyname(server_addr);
692 if (ph && ph->h_addr_list[0]) {
693 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
694 frsh_send_endpoint_protocol_info_t spi = { NULL, 0 };
695 frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
696 frsh_endpoint_queueing_info_t qi = { .queue_size=0, .queue_policy=FRSH_QRP_OLDEST };
697 ret = frsh_send_endpoint_create(FRSH_NETPF_FWP, a->s_addr, BASE_PORT + stream->ac,
698 spi, &stream->endpoint);
699 if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
701 ret = frsh_send_endpoint_bind(stream->vres, stream->endpoint);
702 if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
704 ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
705 &stream->resp_endpoint);
706 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
709 frsh_receive_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL, NULL);
710 stream->resp_port = port;
712 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
713 if (ret) error(1, ret, "Error while creating receiver");
715 stream->receiver.valid = true;
718 error(1, errno, "gethostbyname(%s)", server_addr);
723 static void create_stream_endpoint_native(struct stream *stream)
727 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
729 stream->rem_addr.sin_family = AF_INET;
730 ph = gethostbyname(server_addr);
732 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
734 error(1, errno, "gethostbyname(%s)", server_addr);
736 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
741 calc_stream_params(struct stream *stream)
744 unsigned period_usec;
748 /* If some parameters are not set explicitely, use default values. */
749 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
750 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
751 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
753 bandwidth = stream->bandwidth_bps;
755 /* Avoid arithmetic exception. Server thread will exit if
756 stream->bandwidth_bps == 0. */
757 if (bandwidth == 0) bandwidth = 1;
759 if (stream->packet_size) {
760 packet_size = stream->packet_size;
761 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
762 if (period_usec == 0) period_usec = 1;
763 } else if (stream->period_usec) {
764 period_usec = stream->period_usec;
765 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
768 stream_to_text(buf, sizeof(buf), stream, 0);
769 error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
772 if (packet_size < sizeof(struct msg_t)) {
773 error(1, 0, "Packet size too small (min %zd)", sizeof(struct msg_t));
776 stream->packet_size = packet_size;
777 stream->period_usec = period_usec;
778 stream->jitter = opt_jitter;
780 ret = negotiate_contract_for_stream(stream);
782 create_stream_endpoint(stream);
785 stream_to_text(buf, sizeof(buf), stream, 0);
786 fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
787 stream->bandwidth_bps = 0;
788 some_contract_not_accepted = true;
793 * Parse -b parameter.
795 * @param params String to parse
797 * @return NULL in case of success, pointer to a problematic character
800 char* parse_bandwidths(char *params)
802 struct stream *sp = &streams[nr_streams];
804 while (*params && nr_streams < MAX_STREAMS) {
805 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
809 if (strlen(params) < 2)
811 for (i=0; i<AC_NUM; i++) {
812 if (strncmp(params, ac_ids[i], 2) == 0) {
814 params+=strlen(ac_ids[i]);
822 if (*params == ':') {
825 bw = strtol(params, &next_char, 10);
826 if (next_char == params)
832 sp->bandwidth_bps = bw*Kbit;
835 long packet_size = 0;
836 if (*params == '@') {
838 period = strtol(params, &next_char, 10);
844 if (*params == '/') {
846 packet_size = strtol(params, &next_char, 10);
847 if (packet_size == 0)
855 sp->period_usec = period*MSEC_TO_USEC;
856 sp->packet_size = packet_size;
860 if (*params != '\0' && *params != ',')
871 void wait_for_all_threads_to_finish_fwp(void)
874 /* wait for all threads to finish */
875 for (i=0; i < 2*nr_streams; i++) {
876 sem_wait(&sem_thread_finished);
880 void wait_for_all_threads_to_finish_native(void)
883 /* Wait for all threads to finish */
884 for (i=0; i < nr_streams + AC_NUM; i++) {
885 sem_wait(&sem_thread_finished);
894 ul_log_domain_t *domain;
901 int locked_log(WINDOW *logwin, void *arg)
903 struct log_params *p = arg;
904 if(!(p->level&UL_LOGL_CONT)) {
905 p->level&=UL_LOGL_MASK;
907 wprintw(logwin,"<%d>", p->level);
908 if(p->domain && p->domain->name)
909 wprintw(logwin,"%s: ",p->domain->name);
911 vwprintw(logwin, p->format, p->ap);
912 wnoutrefresh(logwin);
917 wclient_log_fnc(ul_log_domain_t *domain, int level,
918 const char *format, va_list ap)
920 struct log_params p = {
928 use_window(logwin, locked_log, (void*)&p);
940 /* intrflush(stdscr, FALSE); */
941 /* keypad(stdscr, TRUE); */
943 logwin = newwin(0, 0, LINES/2, 0);
945 scrollok(logwin, TRUE);
946 /* ul_log_redir(wclient_log_fnc, 0); */
956 /* ul_log_redir(NULL, 0); */
962 #define addfield(title, format, ...) \
964 x+=strlen(title)+1; \
965 if (i == 0) addstr(title); \
967 snprintf(str, sizeof(str), format, __VA_ARGS__); \
971 void print_status_gui(int seconds)
974 char str[200], s1[20];
976 struct stream *s = NULL;
978 for (i = 0; i <= nr_streams; i++) {
979 if (i>0) s = &streams[i-1];
982 addfield("Stream", "%d", i);
983 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
984 addfield("Packet size", "%d bytes", s->packet_size);
985 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
986 addfield("AC ", "%s", ac_to_text[s->ac]);
987 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
988 addfield("Received responses", "%lld", s->received);
990 wnoutrefresh(stdscr);
994 void print_status_nogui(int seconds)
997 fprintf(stderr, "\r%3ds", seconds);
998 for (ac = 0; ac < AC_NUM; ac++) {
999 int delta = receivers[ac].received - receivers[ac].last_received;
1000 receivers[ac].last_received = receivers[ac].received;
1001 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
1007 int print_log_domain(ul_log_domain_t *domain, void *context)
1009 printf("%s = %d\n", domain->name, domain->level);
1013 int main(int argc, char *argv[])
1015 int i, rc, frames, seconds;
1016 pthread_attr_t attr;
1021 while ((opt = getopt(argc, argv, "B:b:C:c:Gg:I:j:l:o:qQ:s:T:")) != -1) {
1024 opt_def_bandwidth = atoi(optarg);
1028 errpos = parse_bandwidths(optarg);
1029 if (errpos != NULL) {
1030 if (*errpos == '\0')
1031 error(1, 0, "Bandwidth parse error - string to short");
1033 error(1, 0, "Bandwidth parse error at '%s'", errpos);
1038 opt_comment = optarg;
1041 opt_count_sec = atoi(optarg);
1047 opt_granularity_usec = atoi(optarg);
1048 if (opt_granularity_usec < MIN_GRANULARITY) {
1049 error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
1053 opt_interface = optarg;
1057 error(1, 0, "-j is not allowd when compiled with FWP");
1059 opt_jitter = atoi(optarg);
1064 if (*optarg == '?') {
1065 ul_logreg_for_each_domain(print_log_domain, NULL);
1070 ret = ul_log_domain_arg2levels(optarg);
1072 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1077 opt_output = optarg;
1080 opt_send_buf_size = atoi(optarg);
1083 opt_wait_for_queue_is_full = true;
1086 opt_packet_size = atoi(optarg);
1089 opt_def_period_msec = atoi(optarg);
1092 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
1093 fprintf(stderr, "Options:\n");
1094 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
1095 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
1096 fprintf(stderr, " -C comment (added to header)\n");
1097 fprintf(stderr, " -c count (number of seconds to run)\n");
1098 fprintf(stderr, " -g histogram granularity [usec]\n");
1099 fprintf(stderr, " -I <interface> send packets from this interface\n");
1101 fprintf(stderr, " -l <loglevel> uLUt logging levels\n");
1103 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
1104 fprintf(stderr, " -o output filename (.dat will be appended)\n");
1105 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
1106 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
1107 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
1108 fprintf(stderr, " -T default period for -b option [msec]\n");
1112 if (opt_packet_size && opt_def_period_msec) {
1113 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
1116 if (optind < argc) {
1117 server_addr = argv[optind];
1119 error(1, 0, "Expected server address argument");
1122 if (nr_streams == 0)
1123 parse_bandwidths("BE");
1125 pthread_attr_init(&attr);
1127 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1129 if ((logfd = fopen(logfname,"w+")) == NULL) {
1130 error(1, errno ,"Can not open %s", logfname);
1132 if (signal(SIGTERM, stopper) == SIG_ERR) {
1133 error(1, errno, "Error in signal registration");
1136 if (signal(SIGINT, stopper) == SIG_ERR) {
1137 error(1, errno, "Signal handler registration error");
1140 struct sigaction sa;
1141 sa.sa_handler = empty_handler;
1142 sa.sa_flags = 0; /* don't restart syscalls */
1144 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1145 error(1, errno, "sigaction error");
1148 sem_init(&sem_thread_finished, 0, 0);
1153 //ul_log_domain_arg2levels("6");
1156 error(1, errno, "FWP initialization failed");
1160 /* create four receivers each per AC */
1161 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1162 ac_sockfd[ac] = create_ac_socket(ac);
1163 if (ac_sockfd[ac] < 0) {
1166 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1168 error(1, rc, "Error while creating receiver");
1170 receivers[ac].valid = true;
1175 if (opt_interface) {
1178 memset(&ifr, 0, sizeof(ifr));
1179 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1180 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1181 error(1, 0, "unknown iface %s", opt_interface);
1183 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1184 cmsg_len = sizeof(cmsg);
1186 /* create sendpoints */
1187 for (i = 0; i < nr_streams; i++) {
1188 struct stream *s = &streams[i];
1189 pthread_mutex_init(&s->mutex, NULL);
1190 calc_stream_params(s);
1191 rc = pthread_create(&thread, &attr, sender, (void*) s);
1192 if (rc) error(1, rc, "Error while creating sender");
1195 if (some_contract_not_accepted) {
1202 while (!exit_flag) {
1210 print_status_gui(seconds);
1214 print_status_nogui(seconds);
1217 if (seconds == opt_count_sec)
1224 fprintf(stderr, "\nWaiting for threads to finish\n");
1225 wait_for_all_threads_to_finish();
1228 for (i=0; i < nr_streams; i++) {
1229 if (streams[i].vres)
1230 frsh_contract_cancel(streams[i].vres);
1231 if (streams[i].vres_rcv)
1232 frsh_contract_cancel(streams[i].vres_rcv);
1237 uint64_t end_timestamp, measure_length;
1238 clock_gettime(CLOCK_REALTIME,&ts);
1239 end_timestamp = ts.tv_sec*1000000000LL+ts.tv_nsec;
1240 measure_length = end_timestamp - reset_timestamp;
1242 save_results(argc, argv, measure_length/1000);