3 #include <sys/socket.h>
4 #include <netinet/in.h>
19 #include <semaphore.h>
20 #include <sys/ioctl.h>
24 #include <fwp_confdefs.h>
29 #define MAX_STREAMS 10
30 #define MIN_GRANULARITY 100
32 unsigned opt_packet_size = 800;
33 int opt_send_buf_size = -1;
34 unsigned opt_period_usec = 10*MSEC_TO_USEC;
36 unsigned opt_jitter = 0;
37 char *opt_output = "delay_stats";
38 unsigned opt_count_sec = 0;
39 unsigned opt_def_bandwidth = 200;
40 unsigned opt_def_period_msec = 0;
41 int opt_granularity_usec = MIN_GRANULARITY;
42 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
43 char *opt_comment = NULL;
45 bool some_queue_is_full = false;
46 struct timespec reset_timestamp;
48 /* Locked when some queue is full to prevent multiple resets of
50 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
52 int ac_sockfd[AC_NUM];
56 unsigned received, last_received;
59 struct receiver receivers[AC_NUM];
65 /* maximal traffic delay in ms - 10 s*/
66 #define MAX_DELAY_US 10000000
69 unsigned csc; /* Client-server-client delay divided by 2 */
70 unsigned cs; /* Client-server delay */
71 unsigned sc; /* Server-client delay */
74 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
75 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
77 /*struct ac_stats[AC_NUM] {
78 unsigned long int min_trans_time;
79 unsigned long int sum_trans_time;
80 struct timespec recv_timestamp;
81 struct timespec send_timestamp;
85 /* Input parameters */
87 int bandwidth_bps; /* bits per second */
88 int jitter; /* percent */
89 /* Mulualy exclusive input parameters */
91 long period_usec; /* all time units are in microseconds */
95 struct sockaddr_in rem_addr;
97 fwp_endpoint_d_t endpoint;
98 fwp_endpoint_d_t resp_endpoint;
101 struct receiver receiver;
105 pthread_mutex_t mutex;
106 unsigned long long sent, really_sent, received;
109 static struct cmsg_ipi {
111 struct in_pktinfo ipi;
112 } cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
117 struct send_endpoint sepoint[] = {
118 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
119 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
120 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
121 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
122 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
126 struct stream streams[MAX_STREAMS];
128 unsigned int nr_streams = 0;
130 sem_t sem_thread_finished;
132 bool exit_flag = false;
135 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
136 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
137 #define send_packet(s, b) send_packet_fwp(s, b)
138 #define recv_packet(s, b) recv_packet_fwp(s, b)
139 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
141 #define negotiate_contract_for_stream(s) 0
142 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
143 #define send_packet(s, b) send_packet_native(s, b)
144 #define recv_packet(s, b) recv_packet_native(s, b)
145 #define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
153 /* Interrupt all receivers */
155 for (i=0; i < nr_streams; i++) {
156 pthread_kill(streams[i].receiver.thread, SIGUSR1);
159 for (i=0; i < AC_NUM; i++) {
160 pthread_kill(receivers[i].thread, SIGUSR1);
165 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
171 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
172 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
173 stream->sent, stream->sent*SEC_TO_USEC/useconds,
174 stream->received, stream->received*SEC_TO_USEC/useconds);
179 snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
180 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
181 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
182 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
183 (int)(SEC_TO_USEC/stream->period_usec), real);
186 void save_results(int argc, char *argv[], int useconds)
189 const int mini = 3000/opt_granularity_usec;
191 unsigned send_count[AC_NUM];
193 fprintf(stderr, "Writing data to %s... ", logfname);
196 fprintf(logfd, "# Invoked as: ");
197 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
199 fprintf(logfd, "(%s)", opt_comment);
201 fprintf(logfd, "\n");
203 if (useconds/SEC_TO_USEC != opt_count_sec) {
205 usec_to_text(buf, useconds);
206 fprintf(logfd, "# Data gathered for %s.\n", buf);
209 for (i = 0; i < nr_streams; i++) {
210 char stream_desc[200];
211 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
212 fprintf(logfd, "# Stream %s\n", stream_desc);
215 /* Find maximal delay */
217 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
218 for (ac = 0; ac < AC_NUM; ac++) {
219 if ((delay_stats[ac][maxi].csc != 0) ||
220 (delay_stats[ac][maxi].cs != 0) ||
221 (delay_stats[ac][maxi].sc != 0))
224 if (!allzeros) break;
227 if (maxi < mini) maxi = mini;
229 /* Calculate total number of sent packets per AC */
230 memset(send_count, 0, sizeof(send_count));
231 for (i = 0; i < nr_streams; i++) {
233 send_count[ac] += streams[i].sent;
238 for ( i = 0 ; i < maxi; i++) {
239 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
240 for (ac = 0; ac < AC_NUM; ac++) {
242 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
243 else val = -1; /* Don't display this ac */
244 fprintf(logfd," %lf", val);
248 fprintf(logfd,"\n\n");
251 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
254 for (ac = 0; ac < AC_NUM; ac++) {
255 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
257 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
259 if (send_count[ac] != 0) {
261 while ((delay_stats[ac][i].csc == 0) &&
262 (delay_stats[ac][i].cs == 0) &&
263 (delay_stats[ac][i].sc == 0)) i++;
265 for (i++; i < maxi+1; i++) {
266 if (memcmp(&last, &integral, sizeof(last))) {
268 snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
269 snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
270 snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
272 fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
273 integral.csc != last.csc ? buf[0] : "-",
274 integral.cs != last.cs ? buf[1] : "-",
275 integral.sc != last.sc ? buf[2] : "-"
280 integral.csc += delay_stats[ac][i-1].csc;
281 integral.sc += delay_stats[ac][i-1].sc;
282 integral.cs += delay_stats[ac][i-1].cs;
286 fprintf(logfd,"\n\n");
289 fprintf(stderr, "finished.\n");
295 int create_ac_socket(unsigned int ac)
298 unsigned int yes=1, tos;
301 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
303 perror("Unable to open socket");
306 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
307 perror("set non-blocking socket");
310 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
311 perror("Unable to set socket");
315 if (opt_send_buf_size >= 0) {
316 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
317 perror("Unable to set socket buffer size");
323 //tos = ((AC_NUM - ac) *2 - 1)*32;
325 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
326 perror("Unable to set TOS");
338 void reset_statistics()
341 for (i = 0; i < nr_streams; i++) {
342 pthread_mutex_lock(&streams[i].mutex);
344 streams[i].really_sent = 0;
345 streams[i].received = 0;
346 pthread_mutex_unlock(&streams[i].mutex);
348 pthread_mutex_lock(&delay_stats_mutex);
349 clock_gettime(CLOCK_REALTIME, &reset_timestamp);
350 memset(delay_stats, 0, sizeof(delay_stats));
351 pthread_mutex_unlock(&delay_stats_mutex);
355 int recv_packet_native(unsigned ac, struct msg_t *msg)
359 struct sockaddr_in rem_addr;
360 unsigned int rem_addr_length;
363 FD_SET(ac_sockfd[ac], &fdset);
364 rem_addr_length = sizeof(rem_addr);
367 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
369 if (errno == EINTR) continue;
370 perror("receiver select");
373 mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
374 (struct sockaddr*)&rem_addr, &rem_addr_length);
380 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
383 mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
388 void* receiver(void* arg)
391 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
392 long long int min_trans_time;
393 struct timespec send_timestamp, server_timestamp, recv_timestamp;
404 struct stream *stream = arg;
406 mlen = recv_packet_fwp(stream, &msg);
409 mlen = recv_packet_native(ac, &msg);
412 perror("Chyba pri prijimani pozadavku");
415 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
416 send_timestamp = msg.send_timestamp;
417 server_timestamp = msg.sendback_timestamp;
419 /* Check whether this message was sent after reset_statistics() */
421 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
422 continue; /* If so, don't count it */
425 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
426 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
427 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
429 pthread_mutex_lock(&delay_stats_mutex);
430 if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
431 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
433 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
434 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
436 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
437 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
439 pthread_mutex_unlock(&delay_stats_mutex);
441 receivers[ac].received++;
443 pthread_mutex_lock(&streams[msg.stream].mutex);
444 streams[msg.stream].received++;
445 pthread_mutex_unlock(&streams[msg.stream].mutex);
447 /*if (trans_time_nsec < min_trans_time)
448 min_trans_time = trans_time_nsec;*/
449 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
450 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
451 msg.seqn, msg.tos, send_timestamp.tv_sec,\
452 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
453 recv_timestamp.tv_nsec, trans_time_msec); */
456 sem_post(&sem_thread_finished);
463 * @return -1 in case of error, 1 in case of sucessfull send and 0
464 * when all buffers are full.
468 send_packet_native(struct stream* stream, union msg_buff* buff)
474 iov.iov_len = stream->packet_size;
475 msg.msg_name = (void*)&stream->rem_addr;
476 msg.msg_namelen = sizeof(stream->rem_addr);
480 msg.msg_control = &cmsg;
481 msg.msg_controllen = cmsg_len;
485 while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
486 if (errno == EINTR) continue;
487 if (errno == EAGAIN) {
488 if (opt_wait_for_queue_is_full &&
489 !some_queue_is_full &&
490 /* We use mutex as atomic test and set */
491 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
492 some_queue_is_full = true;
498 perror("Error while sending");
507 send_packet_fwp(struct stream* stream, union msg_buff* buff)
511 buff->msg.resp_port = htons(stream->resp_port);
512 ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
519 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
521 struct timespec time_to_wait, current_time, period, interval;
522 unsigned period_usec = stream->period_usec;
524 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
525 /* |-------------| nominal period*/
526 if (stream->jitter) {
527 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
528 + rand() % (2*period_usec*stream->jitter/100));
530 period.tv_nsec = USEC_TO_NSEC*(period_usec);
534 timespec_add(&time_to_wait, last_send_time, &period);
535 clock_gettime(CLOCK_REALTIME,¤t_time);
536 timespec_sub(&interval,&time_to_wait,¤t_time);
537 nanosleep(&interval,NULL);
541 void* sender(void* arg)
544 unsigned long int seqn;
545 struct stream* stream = (struct stream*) arg;
546 char stream_desc[100];
549 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
550 printf("%s\n", stream_desc);
552 if (stream->bandwidth_bps == 0)
558 set_rt_prio(90-stream->ac);
562 /* buff.msg.seqn = seqn++; */
563 /* buff.msg.tos = ac_to_tos[stream->ac]; */
564 buff.msg.stream = stream-streams;
566 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
568 ret = send_packet(stream, &buff);
573 pthread_mutex_lock(&stream->mutex);
576 stream->really_sent++;
577 pthread_mutex_unlock(&stream->mutex);
580 printf("%d", stream->ac);
584 wait_for_next_send(stream, &buff.msg.send_timestamp);
587 sem_post(&sem_thread_finished);
592 static int negotiate_contract_for_stream_fwp(struct stream *stream)
594 fwp_contract_t contract;
595 fwp_contract_d_t contract_d;
599 /* Contract for client->server stream */
601 contract.budget = stream->packet_size;
602 contract.period_usec = stream->period_usec;
604 contract_d = fwp_contract_create(&contract);
605 ret = fwp_contract_negotiate(contract_d, &stream->vres);
607 fprintf(stderr, "Send contract was not accepted\n");
611 /* Contract for server->client stream */
612 contract.budget = stream->packet_size;
613 contract.period_usec = stream->period_usec;
615 contract_d = fwp_contract_create(&contract);
616 ret = fwp_contract_negotiate(contract_d, &vres2);
618 fprintf(stderr, "Receive contract was not accepted\n");
622 /* We don't use the vres at server, since the server doesn't
623 * know the parameters. Instread, server uses plain
631 static void create_stream_endpoint_fwp(struct stream *stream)
633 /* fwp_endpoint_attr_t attr; */
637 stream->ac = fwp_vres_get_ac(stream->vres);
639 /* fwp_endpoint_attr_init(&attr); */
640 /* fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
642 ph = gethostbyname(server_addr);
643 if (ph && ph->h_addr_list[0]) {
644 struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
645 ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
646 NULL, &stream->endpoint);
647 /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
649 perror("fwp_send_endpoint_create");
652 ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
654 perror("fwp_send_endpoint_bind");
657 ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
659 perror("fwp_receive_endpoint_create");
663 fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
664 stream->resp_port = port;
665 printf("resp_port = %d\n", port);
667 ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
669 perror("Error while creating receiver\n");
675 snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
682 static void create_stream_endpoint_native(struct stream *stream)
686 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
688 stream->rem_addr.sin_family = AF_INET;
689 ph = gethostbyname(server_addr);
691 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
694 snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
698 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
703 calc_stream_params(struct stream *stream)
706 unsigned period_usec;
710 /* If some parameters are not set explicitely, use default values. */
711 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
712 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
713 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
715 bandwidth = stream->bandwidth_bps;
717 /* Avoid arithmetic exception. Server thread will exit if
718 stream->bandwidth_bps == 0. */
719 if (bandwidth == 0) bandwidth = 1;
721 if (stream->packet_size) {
722 packet_size = stream->packet_size;
723 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
724 if (period_usec == 0) period_usec = 1;
725 } else if (stream->period_usec) {
726 period_usec = stream->period_usec;
727 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
730 stream_to_text(buf, sizeof(buf), stream, 0);
731 fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
735 if (packet_size < sizeof(struct msg_t)) {
736 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
740 stream->packet_size = packet_size;
741 stream->period_usec = period_usec;
742 stream->jitter = opt_jitter;
744 ret = negotiate_contract_for_stream(stream);
746 create_stream_endpoint(stream);
749 stream_to_text(buf, sizeof(buf), stream, 0);
750 fprintf(stderr, "Contract hasn't been accepted: %s\n", buf);
751 stream->bandwidth_bps = 0;
756 * Parse -b parameter.
758 * @param params String to parse
760 * @return NULL in case of success, pointer to a problematic character
763 char* parse_bandwidths(char *params)
765 struct stream *sp = &streams[nr_streams];
767 while (*params && nr_streams < MAX_STREAMS) {
768 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
772 if (strlen(params) < 2)
774 for (i=0; i<AC_NUM; i++) {
775 if (strncmp(params, ac_ids[i], 2) == 0) {
777 params+=strlen(ac_ids[i]);
785 if (*params == ':') {
788 bw = strtol(params, &next_char, 10);
789 if (next_char == params)
795 sp->bandwidth_bps = bw*Kbit;
798 long packet_size = 0;
799 if (*params == '@') {
801 period = strtol(params, &next_char, 10);
807 if (*params == '/') {
809 packet_size = strtol(params, &next_char, 10);
810 if (packet_size == 0)
818 sp->period_usec = period*MSEC_TO_USEC;
819 sp->packet_size = packet_size;
823 if (*params != '\0' && *params != ',')
834 void wait_for_all_threads_to_finish_fwp(void)
837 /* Wait for all threads to finish */
839 /* for (i=0; i < 2*nr_streams; i++) { */
840 /* sem_wait(&sem_thread_finished); */
844 void wait_for_all_threads_to_finish_native(void)
847 /* Wait for all threads to finish */
848 for (i=0; i < nr_streams + AC_NUM; i++) {
849 sem_wait(&sem_thread_finished);
854 int main(int argc, char *argv[])
856 int ac, i, rc, seconds;
862 while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
865 opt_def_bandwidth = atoi(optarg);
869 error = parse_bandwidths(optarg);
872 fprintf(stderr, "Bandwidth parse error - string to short\n");
874 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
880 opt_comment = optarg;
883 opt_count_sec = atoi(optarg);
886 opt_granularity_usec = atoi(optarg);
887 if (opt_granularity_usec < MIN_GRANULARITY) {
888 fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
893 opt_interface = optarg;
897 fprintf(stderr, "-j is not allowd when compiled with FWP\n");
900 opt_jitter = atoi(optarg);
907 opt_send_buf_size = atoi(optarg);
910 opt_wait_for_queue_is_full = true;
913 opt_packet_size = atoi(optarg);
916 opt_def_period_msec = atoi(optarg);
919 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
920 fprintf(stderr, "Options:\n");
921 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
922 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
923 fprintf(stderr, " -C comment (added to header)\n");
924 fprintf(stderr, " -c count (number of seconds to run)\n");
925 fprintf(stderr, " -g histogram granularity [usec]\n");
926 fprintf(stderr, " -I <interface> send packets from this interface");
927 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
928 fprintf(stderr, " -o output filename (.dat will be appended)\n");
929 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
930 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
931 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
932 fprintf(stderr, " -T default period for -b option [msec]\n");
936 if (opt_packet_size && opt_def_period_msec) {
937 fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
942 server_addr = argv[optind];
944 fprintf(stderr, "Expected server address argument\n");
949 parse_bandwidths("BE");
951 pthread_attr_init(&attr);
953 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
955 if ((logfd = fopen(logfname,"w+")) == NULL) {
956 fprintf(stderr,"Can not open %s\n", logfname);
959 if (signal(SIGTERM, stopper) == SIG_ERR) {
960 perror("Error in signal registration");
964 if (signal(SIGINT, stopper) == SIG_ERR) {
965 perror("Signal handler registration error");
970 sa.sa_handler = empty_handler;
971 sa.sa_flags = 0; /* don't restart syscalls */
973 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
974 perror("sigaction error");
978 sem_init(&sem_thread_finished, 0, 0);
985 fprintf(stderr, "FWP initialization failed\n");
989 /* create four receivers each per AC */
990 for (ac = AC_NUM - 1; ac >= 0; ac--) {
991 ac_sockfd[ac] = create_ac_socket(ac);
992 if (ac_sockfd[ac] < 0) {
995 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
997 fprintf(stderr, "Error while creating receiver %d\n",rc);
1004 if (opt_interface) {
1007 memset(&ifr, 0, sizeof(ifr));
1008 strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
1009 if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
1010 fprintf(stderr, "unknown iface %s\n", opt_interface);
1013 cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
1014 cmsg_len = sizeof(cmsg);
1016 /* create sendpoints */
1017 for (i = 0; i < nr_streams; i++) {
1018 struct stream *s = &streams[i];
1019 pthread_mutex_init(&s->mutex, NULL);
1020 calc_stream_params(s);
1021 rc = pthread_create(&thread, &attr, sender, (void*) s);
1023 fprintf(stderr, "Error while creating sender %d\n",rc);
1029 while (!exit_flag) {
1032 fprintf(stderr, "\r%3ds", seconds);
1033 for (ac = 0; ac < AC_NUM; ac++) {
1034 int delta = receivers[ac].received - receivers[ac].last_received;
1035 receivers[ac].last_received = receivers[ac].received;
1036 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
1039 if (seconds == opt_count_sec)
1043 fprintf(stderr, "\nWaiting for threads to finish\n");
1044 wait_for_all_threads_to_finish();
1046 struct timespec end_timestamp, measure_length;
1047 clock_gettime(CLOCK_REALTIME,&end_timestamp);
1048 timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
1050 save_results(argc, argv, timespec2usec(&measure_length));