4 #include <sys/socket.h>
5 #include <netinet/in.h>
20 #include <semaphore.h>
21 #include <sys/ioctl.h>
26 #include <fwp_confdefs.h>
32 #define MAX_STREAMS 10
33 #define MIN_GRANULARITY 100
35 unsigned opt_packet_size = 800;
36 int opt_send_buf_size = -1;
37 unsigned opt_period_usec = 10*MSEC_TO_USEC;
39 unsigned opt_jitter = 0;
40 char *opt_output = "delay_stats";
41 unsigned opt_count_sec = 0;
42 unsigned opt_def_bandwidth = 50;
43 unsigned opt_def_period_msec = 0;
44 int opt_granularity_usec = MIN_GRANULARITY;
45 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
46 char *opt_comment = NULL;
48 bool some_queue_is_full = false;
49 uint64_t reset_timestamp; /* [nsec] */
51 bool some_contract_not_accepted = false;
53 /* Locked when some queue is full to prevent multiple resets of
55 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
57 int ac_sockfd[AC_NUM];
62 unsigned received, last_received;
65 struct receiver receivers[AC_NUM];
71 /* maximal traffic delay in ms - 10 s*/
72 #define MAX_DELAY_US 10000000
75 unsigned csc; /* Client-server-client delay divided by 2 */
76 unsigned cs; /* Client-server delay */
77 unsigned sc; /* Server-client delay */
80 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
81 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
83 /*struct ac_stats[AC_NUM] {
84 unsigned long int min_trans_time;
85 unsigned long int sum_trans_time;
86 struct timespec recv_timestamp;
87 struct timespec send_timestamp;
91 /* Input parameters */
93 int bandwidth_bps; /* bits per second */
94 int jitter; /* percent */
95 /* Mulualy exclusive input parameters */
97 long period_usec; /* all time units are in microseconds */
101 struct sockaddr_in rem_addr;
103 fwp_contract_d_t contract_send;
104 fwp_contract_d_t contract_resp;
105 fwp_endpoint_d_t endpoint;
106 fwp_endpoint_d_t resp_endpoint;
109 struct receiver receiver;
110 long wc_delay; /* worst-case delay */
114 pthread_mutex_t mutex;
115 unsigned long long sent, really_sent, received;
118 static struct cmsg_ipi {
120 struct in_pktinfo ipi;
121 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
126 struct send_endpoint sepoint[] = {
127 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
128 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
129 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
130 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
131 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
135 struct stream streams[MAX_STREAMS];
137 unsigned int nr_streams = 0;
139 sem_t sem_thread_finished;
141 bool exit_flag = false;
144 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
145 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
146 #define send_packet(s, b) send_packet_fwp(s, b)
147 #define recv_packet(s, b) recv_packet_fwp(s, b)
148 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
150 #define negotiate_contract_for_stream(s) 0
151 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
152 #define send_packet(s, b) send_packet_native(s, b)
153 #define recv_packet(s, b) recv_packet_native(s, b)
154 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
162 /* Interrupt all receivers */
164 for (i=0; i < nr_streams; i++) {
165 if (streams[i].receiver.valid) pthread_kill(streams[i].receiver.thread, SIGUSR1);
166 if (streams[i].contract_send) fwp_contract_cancel(streams[i].contract_send);
167 if (streams[i].contract_resp)fwp_contract_cancel(streams[i].contract_resp);
170 for (i=0; i < AC_NUM; i++) {
171 pthread_kill(receivers[i].thread, SIGUSR1);
176 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
182 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
183 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
184 stream->sent, stream->sent*SEC_TO_USEC/useconds,
185 stream->received, stream->received*SEC_TO_USEC/useconds);
190 snprintf(stream_desc, n, "%"PRIdPTR": %s %s (%d bytes per %s +-%s, %d packets/s)%s",
191 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
192 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
193 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
194 (int)(SEC_TO_USEC/stream->period_usec), real);
197 void save_results(int argc, char *argv[], int useconds)
200 const int mini = 3000/opt_granularity_usec;
202 unsigned send_count[AC_NUM];
204 fprintf(stderr, "Writing data to %s... ", logfname);
207 fprintf(logfd, "# Invoked as: ");
208 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
210 fprintf(logfd, "(%s)", opt_comment);
212 fprintf(logfd, "\n");
214 if (useconds/SEC_TO_USEC != opt_count_sec) {
216 usec_to_text(buf, useconds);
217 fprintf(logfd, "# Data gathered for %s.\n", buf);
220 for (i = 0; i < nr_streams; i++) {
221 char stream_desc[200];
222 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
223 fprintf(logfd, "# Stream %s\n", stream_desc);
226 /* Find maximal delay */
228 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
229 for (ac = 0; ac < AC_NUM; ac++) {
230 if ((delay_stats[ac][maxi].csc != 0) ||
231 (delay_stats[ac][maxi].cs != 0) ||
232 (delay_stats[ac][maxi].sc != 0))
235 if (!allzeros) break;
238 if (maxi < mini) maxi = mini;
240 /* Calculate total number of sent packets per AC */
241 memset(send_count, 0, sizeof(send_count));
242 for (i = 0; i < nr_streams; i++) {
244 send_count[ac] += streams[i].sent;
249 for ( i = 0 ; i < maxi; i++) {
250 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
251 for (ac = 0; ac < AC_NUM; ac++) {
253 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
254 else val = -1; /* Don't display this ac */
255 fprintf(logfd," %lf", val);
259 fprintf(logfd,"\n\n");
262 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
265 for (ac = 0; ac < AC_NUM; ac++) {
266 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
268 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
270 if (send_count[ac] != 0) {
272 while ((delay_stats[ac][i].csc == 0) &&
273 (delay_stats[ac][i].cs == 0) &&
274 (delay_stats[ac][i].sc == 0)) i++;
276 for (i++; i < maxi+1; i++) {
277 if (memcmp(&last, &integral, sizeof(last))) {
279 snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
280 snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
281 snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
283 fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
284 integral.csc != last.csc ? buf[0] : "-",
285 integral.cs != last.cs ? buf[1] : "-",
286 integral.sc != last.sc ? buf[2] : "-"
291 integral.csc += delay_stats[ac][i-1].csc;
292 integral.sc += delay_stats[ac][i-1].sc;
293 integral.cs += delay_stats[ac][i-1].cs;
297 fprintf(logfd,"\n\n");
300 fprintf(stderr, "finished.\n");
306 int create_ac_socket(intptr_t ac)
309 unsigned int yes=1, tos;
312 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
314 error(0, errno, "Unable to open socket");
317 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
318 error(0, errno, "set non-blocking socket");
321 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
322 error(0, errno, "Unable to set socket");
326 if (opt_send_buf_size >= 0) {
327 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
328 error(0, errno, "Unable to set socket buffer size");
334 //tos = ((AC_NUM - ac) *2 - 1)*32;
336 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
337 error(0, errno, "Unable to set TOS");
349 void reset_statistics()
353 for (i = 0; i < nr_streams; i++) {
354 pthread_mutex_lock(&streams[i].mutex);
356 streams[i].really_sent = 0;
357 streams[i].received = 0;
358 pthread_mutex_unlock(&streams[i].mutex);
360 pthread_mutex_lock(&delay_stats_mutex);
361 clock_gettime(CLOCK_REALTIME, &ts);
362 reset_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
363 memset(delay_stats, 0, sizeof(delay_stats));
364 pthread_mutex_unlock(&delay_stats_mutex);
368 int recv_packet_native(intptr_t ac, struct msg_t *msg)
372 struct sockaddr_in rem_addr;
373 unsigned int rem_addr_length;
376 FD_SET(ac_sockfd[ac], &fdset);
377 rem_addr_length = sizeof(rem_addr);
380 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
382 if (errno == EINTR) continue;
383 error(0, errno, "receiver select");
386 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
387 (struct sockaddr*)&rem_addr, &rem_addr_length);
393 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
396 mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
401 void* receiver(void* arg)
404 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
405 long long int min_trans_time;
407 uint64_t send_timestamp, server_timestamp, recv_timestamp;
418 struct stream *stream = arg;
420 mlen = recv_packet_fwp(stream, &msg);
423 mlen = recv_packet_native(ac, &msg);
427 error(0, errno, "receive_packet error");
430 clock_gettime(CLOCK_REALTIME,&ts);
431 recv_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
432 send_timestamp = msg.send_timestamp;
433 server_timestamp = msg.sendback_timestamp;
435 /* Check whether this message was sent after reset_statistics() */
437 if (send_timestamp < reset_timestamp) {
438 continue; /* If so, don't count it */
441 trans_time_usec = (recv_timestamp - send_timestamp) / 2 / 1000;
442 client_to_server_usec = (server_timestamp - send_timestamp) / 1000;
443 server_to_client_usec = (recv_timestamp - server_timestamp) / 1000;
445 pthread_mutex_lock(&delay_stats_mutex);
446 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
447 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
449 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
450 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
452 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
453 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
455 pthread_mutex_unlock(&delay_stats_mutex);
458 if (trans_time_usec > stream->wc_delay) {
459 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 = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
540 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
542 struct timespec time_to_wait, current_time, period, interval;
543 unsigned period_usec = stream->period_usec;
545 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
546 /* |-------------| nominal period*/
547 if (stream->jitter) {
548 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
549 + rand() % (2*period_usec*stream->jitter/100));
551 period.tv_nsec = USEC_TO_NSEC*(period_usec);
555 timespec_add(&time_to_wait, last_send_time, &period);
556 clock_gettime(CLOCK_REALTIME,¤t_time);
557 timespec_sub(&interval,&time_to_wait,¤t_time);
558 nanosleep(&interval,NULL);
562 void* sender(void* arg)
565 unsigned long int seqn;
566 struct stream* stream = (struct stream*) arg;
571 char stream_desc[100];
572 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
573 printf("%s\n", stream_desc);
575 if (stream->bandwidth_bps == 0)
581 set_rt_prio(90-stream->ac);
585 /* buff.msg.seqn = seqn++; */
586 /* buff.msg.tos = ac_to_tos[stream->ac]; */
587 buff.msg.stream = stream-streams;
589 clock_gettime(CLOCK_REALTIME,&ts);
590 buff.msg.send_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
592 ret = send_packet(stream, &buff);
597 pthread_mutex_lock(&stream->mutex);
600 stream->really_sent++;
601 pthread_mutex_unlock(&stream->mutex);
604 printf("%d", stream->ac);
608 wait_for_next_send(stream, &ts);
611 sem_post(&sem_thread_finished);
616 static int negotiate_contract_for_stream_fwp(struct stream *stream)
618 fwp_contract_t contract;
622 /* Contract for client->server stream */
623 memset(&contract, 0, sizeof(contract));
624 contract.budget = stream->packet_size;
625 contract.period_usec = stream->period_usec;
626 contract.deadline_usec = 3*stream->period_usec;
628 stream->contract_send = fwp_contract_create(&contract);
629 ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
631 stream->contract_send = NULL;
632 /* fprintf(stderr, "Send contract was not accepted\n\n\n"); */
636 /* Contract for server->client stream */
637 memset(&contract, 0, sizeof(contract));
638 contract.budget = stream->packet_size;
639 contract.period_usec = stream->period_usec;
640 contract.deadline_usec = 3*stream->period_usec;
642 stream->contract_resp = fwp_contract_create(&contract);
643 ret = fwp_contract_negotiate(stream->contract_resp, &vres2);
645 stream->contract_resp = NULL;
646 /* fprintf(stderr, "Receive contract was not accepted\n\n\n"); */
650 /* We don't use the vres at server, since the server doesn't
651 * know the parameters. Instread, server uses plain
659 static void create_stream_endpoint_fwp(struct stream *stream)
661 /* fwp_endpoint_attr_t attr; */
665 stream->ac = fwp_vres_get_ac(stream->vres);
667 /* fwp_endpoint_attr_init(&attr); */
668 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
670 ph = gethostbyname(server_addr);
671 if (ph && ph->h_addr_list[0]) {
672 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
673 ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
674 NULL, &stream->endpoint);
675 /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
676 if (ret < 0) error(1, errno, "fwp_send_endpoint_create");
678 ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
679 if (ret != 0) error(1, errno, "fwp_send_endpoint_bind");
681 ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
682 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
685 fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
686 stream->resp_port = port;
688 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
689 if (ret) error(1, ret, "Error while creating receiver");
691 stream->receiver.valid = true;
694 error(1, errno, "gethostbyname(%s)", server_addr);
699 static void create_stream_endpoint_native(struct stream *stream)
703 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
705 stream->rem_addr.sin_family = AF_INET;
706 ph = gethostbyname(server_addr);
708 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
710 error(1, errno, "gethostbyname(%s)", server_addr);
712 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
717 calc_stream_params(struct stream *stream)
720 unsigned period_usec;
724 /* If some parameters are not set explicitely, use default values. */
725 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
726 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
727 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
729 bandwidth = stream->bandwidth_bps;
731 /* Avoid arithmetic exception. Server thread will exit if
732 stream->bandwidth_bps == 0. */
733 if (bandwidth == 0) bandwidth = 1;
735 if (stream->packet_size) {
736 packet_size = stream->packet_size;
737 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
738 if (period_usec == 0) period_usec = 1;
739 } else if (stream->period_usec) {
740 period_usec = stream->period_usec;
741 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
744 stream_to_text(buf, sizeof(buf), stream, 0);
745 error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
748 if (packet_size < sizeof(struct msg_t)) {
749 error(1, 0, "Packet size too small (min %zd)", sizeof(struct msg_t));
752 stream->packet_size = packet_size;
753 stream->period_usec = period_usec;
754 stream->jitter = opt_jitter;
756 ret = negotiate_contract_for_stream(stream);
758 create_stream_endpoint(stream);
761 stream_to_text(buf, sizeof(buf), stream, 0);
762 fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
763 stream->bandwidth_bps = 0;
764 some_contract_not_accepted = true;
769 * Parse -b parameter.
771 * @param params String to parse
773 * @return NULL in case of success, pointer to a problematic character
776 char* parse_bandwidths(char *params)
778 struct stream *sp = &streams[nr_streams];
780 while (*params && nr_streams < MAX_STREAMS) {
781 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
785 if (strlen(params) < 2)
787 for (i=0; i<AC_NUM; i++) {
788 if (strncmp(params, ac_ids[i], 2) == 0) {
790 params+=strlen(ac_ids[i]);
798 if (*params == ':') {
801 bw = strtol(params, &next_char, 10);
802 if (next_char == params)
808 sp->bandwidth_bps = bw*Kbit;
811 long packet_size = 0;
812 if (*params == '@') {
814 period = strtol(params, &next_char, 10);
820 if (*params == '/') {
822 packet_size = strtol(params, &next_char, 10);
823 if (packet_size == 0)
831 sp->period_usec = period*MSEC_TO_USEC;
832 sp->packet_size = packet_size;
836 if (*params != '\0' && *params != ',')
847 void wait_for_all_threads_to_finish_fwp(void)
850 /* Wait for all threads to finish */
852 /* for (i=0; i < 2*nr_streams; i++) { */
853 /* sem_wait(&sem_thread_finished); */
857 void wait_for_all_threads_to_finish_native(void)
860 /* Wait for all threads to finish */
861 for (i=0; i < nr_streams + AC_NUM; i++) {
862 sem_wait(&sem_thread_finished);
875 #define addfield(title, format, ...) \
877 x+=strlen(title)+1; \
878 if (i == 0) addstr(title); \
880 snprintf(str, sizeof(str), format, __VA_ARGS__); \
884 void print_status(int seconds)
887 char str[200], s1[20];
889 struct stream *s = NULL;
891 for (i = 0; i <= nr_streams; i++) {
892 if (i>0) s = &streams[i-1];
895 addfield("Stream", "%d", i);
896 addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
897 addfield("Packet size", "%d bytes", s->packet_size);
898 addfield("Period ", "%s", usec_to_text(s1, s->period_usec));
899 addfield("AC ", "%s", ac_to_text[s->ac]);
900 addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
901 addfield("Received responses", "%lld", s->received);
907 void print_status(int seconds)
910 fprintf(stderr, "\r%3ds", seconds);
911 for (ac = 0; ac < AC_NUM; ac++) {
912 int delta = receivers[ac].received - receivers[ac].last_received;
913 receivers[ac].last_received = receivers[ac].received;
914 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
920 int main(int argc, char *argv[])
922 int i, rc, frames, seconds;
928 while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
931 opt_def_bandwidth = atoi(optarg);
935 errpos = parse_bandwidths(optarg);
936 if (errpos != NULL) {
938 error(1, 0, "Bandwidth parse error - string to short");
940 error(1, 0, "Bandwidth parse error at '%s'", errpos);
945 opt_comment = optarg;
948 opt_count_sec = atoi(optarg);
951 opt_granularity_usec = atoi(optarg);
952 if (opt_granularity_usec < MIN_GRANULARITY) {
953 error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
957 opt_interface = optarg;
961 error(1, 0, "-j is not allowd when compiled with FWP");
963 opt_jitter = atoi(optarg);
970 opt_send_buf_size = atoi(optarg);
973 opt_wait_for_queue_is_full = true;
976 opt_packet_size = atoi(optarg);
979 opt_def_period_msec = atoi(optarg);
982 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
983 fprintf(stderr, "Options:\n");
984 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
985 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
986 fprintf(stderr, " -C comment (added to header)\n");
987 fprintf(stderr, " -c count (number of seconds to run)\n");
988 fprintf(stderr, " -g histogram granularity [usec]\n");
989 fprintf(stderr, " -I <interface> send packets from this interface\n");
990 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
991 fprintf(stderr, " -o output filename (.dat will be appended)\n");
992 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
993 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
994 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
995 fprintf(stderr, " -T default period for -b option [msec]\n");
999 if (opt_packet_size && opt_def_period_msec) {
1000 error(1, 0, "Error: Nonzero -T and -s can't be used together!");
1003 if (optind < argc) {
1004 server_addr = argv[optind];
1006 error(1, 0, "Expected server address argument");
1009 if (nr_streams == 0)
1010 parse_bandwidths("BE");
1012 pthread_attr_init(&attr);
1014 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
1016 if ((logfd = fopen(logfname,"w+")) == NULL) {
1017 error(1, errno ,"Can not open %s", logfname);
1019 if (signal(SIGTERM, stopper) == SIG_ERR) {
1020 error(1, errno, "Error in signal registration");
1023 if (signal(SIGINT, stopper) == SIG_ERR) {
1024 error(1, errno, "Signal handler registration error");
1027 struct sigaction sa;
1028 sa.sa_handler = empty_handler;
1029 sa.sa_flags = 0; /* don't restart syscalls */
1031 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1032 error(1, errno, "sigaction error");
1035 sem_init(&sem_thread_finished, 0, 0);
1042 error(1, errno, "FWP initialization failed");
1046 /* create four receivers each per AC */
1047 for (ac = AC_NUM - 1; ac >= 0; ac--) {
1048 ac_sockfd[ac] = create_ac_socket(ac);
1049 if (ac_sockfd[ac] < 0) {
1052 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
1054 error(1, rc, "Error while creating receiver");
1056 receivers[ac].valid = true;
1061 if (opt_interface) {
1064 memset(&ifr, 0, sizeof(ifr));
1065 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1066 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1067 error(1, 0, "unknown iface %s", opt_interface);
1069 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1070 cmsg_len = sizeof(cmsg);
1072 /* create sendpoints */
1073 for (i = 0; i < nr_streams; i++) {
1074 struct stream *s = &streams[i];
1075 pthread_mutex_init(&s->mutex, NULL);
1076 calc_stream_params(s);
1077 rc = pthread_create(&thread, &attr, sender, (void*) s);
1078 if (rc) error(1, rc, "Error while creating sender");
1081 if (some_contract_not_accepted) {
1088 while (!exit_flag) {
1099 print_status(seconds);
1100 if (seconds == opt_count_sec)
1109 fprintf(stderr, "\nWaiting for threads to finish\n");
1110 wait_for_all_threads_to_finish();
1113 uint64_t end_timestamp, measure_length;
1114 clock_gettime(CLOCK_REALTIME,&ts);
1115 end_timestamp = ts.tv_sec*1000000000LL+ts.tv_nsec;
1116 measure_length = end_timestamp - reset_timestamp;
1118 save_results(argc, argv, measure_length/1000);