3 #include <sys/socket.h>
4 #include <netinet/in.h>
19 #include <semaphore.h>
22 #include <fwp_proto.h>
25 #define MAX_STREAMS 10
26 #define MIN_GRANULARITY 100
28 unsigned opt_packet_size = 800;
29 int opt_send_buf_size = -1;
30 unsigned opt_period_usec = 10*MSEC_TO_USEC;
31 unsigned opt_jitter = 0;
32 char *opt_output = "delay_stats";
33 unsigned opt_count_sec = 0;
34 unsigned opt_def_bandwidth = 200;
35 unsigned opt_def_period_msec = 0;
36 int opt_granularity_usec = MIN_GRANULARITY;
37 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
39 bool some_queue_is_full = false;
40 struct timespec reset_timestamp;
42 /* Locked when some queue is full to prevent multiple resets of
44 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
46 int ac_sockfd[AC_NUM];
50 unsigned received, last_received;
57 /* maximal traffic delay in ms - 10 s*/
58 #define MAX_DELAY_US 10000000
61 unsigned csc; /* Client-server-client delay divided by 2 */
62 unsigned cs; /* Client-server delay */
63 unsigned sc; /* Server-client delay */
66 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
67 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
69 /*struct ac_stats[AC_NUM] {
70 unsigned long int min_trans_time;
71 unsigned long int sum_trans_time;
72 struct timespec recv_timestamp;
73 struct timespec send_timestamp;
77 /* Input parameters */
79 int bandwidth_bps; /* bits per second */
80 int jitter; /* percent */
81 /* Mulualy exclusive input parameters */
83 long period_usec; /* all time units are in microseconds */
87 struct sockaddr_in rem_addr;
93 pthread_mutex_t mutex;
94 unsigned long long sent, really_sent, received;
98 struct send_endpoint sepoint[] = {
99 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
100 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
101 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
102 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
103 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
107 struct stream streams[MAX_STREAMS];
109 unsigned int nr_streams = 0;
111 sem_t sem_thread_finished;
113 bool exit_flag = false;
116 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
117 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
118 #define send_packet(s, b) send_packet_fwp(s, b)
120 #define negotiate_contract_for_stream(s) 0
121 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
122 #define send_packet(s, b) send_packet_native(s, b)
130 /* Interrupt all receivers */
131 for (i=0; i < AC_NUM; i++) {
132 pthread_kill(receivers[i].thread, SIGUSR1);
136 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
142 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
143 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
144 stream->sent, stream->sent*SEC_TO_USEC/useconds,
145 stream->received, stream->received*SEC_TO_USEC/useconds);
150 snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
151 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
152 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
153 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
154 (int)(SEC_TO_USEC/stream->period_usec), real);
157 void save_results(int argc, char *argv[], int useconds)
160 const int mini = 3000/opt_granularity_usec;
162 unsigned send_count[AC_NUM];
164 fprintf(stderr, "Writing data to %s... ", logfname);
167 fprintf(logfd, "# Invoked as: ");
168 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
169 fprintf(logfd, "\n");
171 if (useconds/SEC_TO_USEC != opt_count_sec) {
173 usec_to_text(buf, useconds);
174 fprintf(logfd, "# Data gathered for %s.\n", buf);
177 for (i = 0; i < nr_streams; i++) {
178 char stream_desc[200];
179 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
180 fprintf(logfd, "# Stream %s\n", stream_desc);
183 /* Find maximal delay */
185 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
186 for (ac = 0; ac < AC_NUM; ac++) {
187 if ((delay_stats[ac][maxi].csc != 0) ||
188 (delay_stats[ac][maxi].cs != 0) ||
189 (delay_stats[ac][maxi].sc != 0))
192 if (!allzeros) break;
195 if (maxi < mini) maxi = mini;
197 /* Calculate total number of sent packets per AC */
198 memset(send_count, 0, sizeof(send_count));
199 for (i = 0; i < nr_streams; i++) {
201 send_count[ac] += streams[i].sent;
206 for ( i = 0 ; i < maxi; i++) {
207 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
208 for (ac = 0; ac < AC_NUM; ac++) {
210 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
211 else val = -1; /* Don't display this ac */
212 fprintf(logfd," %lf", val);
216 fprintf(logfd,"\n\n");
219 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
222 for (ac = 0; ac < AC_NUM; ac++) {
223 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
225 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
227 if (send_count[ac] != 0) {
229 while ((delay_stats[ac][i].csc == 0) &&
230 (delay_stats[ac][i].cs == 0) &&
231 (delay_stats[ac][i].sc == 0)) i++;
233 for (i++; i < maxi+1; i++) {
234 if (memcmp(&last, &integral, sizeof(last))) {
235 fprintf(logfd,"%f %f %f %f\n", i*opt_granularity_usec/1000.0,
236 (double)integral.csc*100.0 / send_count[ac],
237 (double)integral.cs *100.0 / send_count[ac],
238 (double)integral.sc *100.0 / send_count[ac]
243 integral.csc += delay_stats[ac][i-1].csc;
244 integral.sc += delay_stats[ac][i-1].sc;
245 integral.cs += delay_stats[ac][i-1].cs;
249 fprintf(logfd,"\n\n");
252 fprintf(stderr, "finished.\n");
258 int create_ac_socket(unsigned int ac)
261 unsigned int yes=1, tos;
264 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
266 perror("Unable to open socket");
269 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
270 perror("set non-blocking socket");
273 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
274 perror("Unable to set socket");
278 if (opt_send_buf_size >= 0) {
279 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
280 perror("Unable to set socket buffer size");
286 //tos = ((AC_NUM - ac) *2 - 1)*32;
288 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
289 perror("Unable to set TOS");
301 void reset_statistics()
304 for (i = 0; i < nr_streams; i++) {
305 pthread_mutex_lock(&streams[i].mutex);
307 streams[i].really_sent = 0;
308 streams[i].received = 0;
309 pthread_mutex_unlock(&streams[i].mutex);
311 pthread_mutex_lock(&delay_stats_mutex);
312 clock_gettime(CLOCK_REALTIME, &reset_timestamp);
313 memset(delay_stats, 0, sizeof(delay_stats));
314 pthread_mutex_unlock(&delay_stats_mutex);
317 void* receiver(void* queue)
320 struct sockaddr_in rem_addr;
322 unsigned int ac, rem_addr_length;
323 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
324 long long int min_trans_time;
325 struct timespec send_timestamp, server_timestamp, recv_timestamp;
334 rem_addr_length = sizeof(rem_addr);
337 FD_SET(ac_sockfd[ac], &fdset);
338 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
340 if (errno == EINTR) continue;
341 perror("receiver select");
344 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,
345 (struct sockaddr*)&rem_addr, &rem_addr_length);
347 perror("Chyba pri prijimani pozadavku");
350 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
351 send_timestamp = msg.send_timestamp;
352 server_timestamp = msg.sendback_timestamp;
354 /* Check whether this message was sent after reset_statistics() */
356 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
357 continue; /* If so, don't count it */
360 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
361 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
362 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
364 pthread_mutex_lock(&delay_stats_mutex);
365 if (trans_time_usec < MAX_DELAY_US) {
366 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
368 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
369 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
371 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
372 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
374 pthread_mutex_unlock(&delay_stats_mutex);
376 receivers[ac].received++;
378 pthread_mutex_lock(&streams[msg.stream].mutex);
379 streams[msg.stream].received++;
380 pthread_mutex_unlock(&streams[msg.stream].mutex);
382 /*if (trans_time_nsec < min_trans_time)
383 min_trans_time = trans_time_nsec;*/
384 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
385 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
386 msg.seqn, msg.tos, send_timestamp.tv_sec,\
387 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
388 recv_timestamp.tv_nsec, trans_time_msec); */
391 sem_post(&sem_thread_finished);
398 * @return -1 in case of error, 1 in case of sucessfull send and 0
399 * when all buffers are full.
403 send_packet_native(struct stream* stream, union msg_buff* buff)
406 while (sendto(ac_sockfd[stream->ac], buff, stream->packet_size, 0,
407 (struct sockaddr*)&stream->rem_addr, sizeof(stream->rem_addr)) < 0) {
408 if (errno == EINTR) continue;
409 if (errno == EAGAIN) {
410 if (opt_wait_for_queue_is_full &&
411 !some_queue_is_full &&
412 /* We use mutex as atomic test and set */
413 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
414 some_queue_is_full = true;
420 perror("Error while sending");
429 send_packet_fwp(struct stream* stream, union msg_buff* buff)
432 /* while (sendto(ac_sockfd[stream->ac], buff, stream->packet_size, 0, */
433 /* (struct sockaddr*)&stream->rem_addr, sizeof(stream->rem_addr)) < 0) { */
434 /* if (errno == EINTR) continue; */
435 /* if (errno == EAGAIN) { */
436 /* if (opt_wait_for_queue_is_full && */
437 /* !some_queue_is_full && */
438 /* /\* We use mutex as atomic test and set *\/ */
439 /* (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) { */
440 /* some_queue_is_full = true; */
441 /* reset_statistics(); */
446 /* perror("Error while sending"); */
456 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
458 struct timespec time_to_wait, current_time, period, interval;
459 unsigned period_usec = stream->period_usec;
461 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
462 /* |-------------| nominal period*/
463 if (stream->jitter) {
464 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
465 + rand() % (2*period_usec*stream->jitter/100));
467 period.tv_nsec = USEC_TO_NSEC*(period_usec);
471 timespec_add(&time_to_wait, last_send_time, &period);
472 clock_gettime(CLOCK_REALTIME,¤t_time);
473 timespec_sub(&interval,&time_to_wait,¤t_time);
474 nanosleep(&interval,NULL);
478 void* sender(void* arg)
481 unsigned long int seqn;
482 struct stream* stream = (struct stream*) arg;
483 char stream_desc[100];
486 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
487 printf("%s\n", stream_desc);
489 if (stream->bandwidth_bps == 0)
495 set_rt_prio(90-stream->ac);
499 /* buff.msg.seqn = seqn++; */
500 /* buff.msg.tos = ac_to_tos[stream->ac]; */
501 buff.msg.stream = stream-streams;
503 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
505 ret = send_packet(stream, &buff);
510 pthread_mutex_lock(&stream->mutex);
513 stream->really_sent++;
514 pthread_mutex_unlock(&stream->mutex);
517 printf("%d", stream->ac);
521 wait_for_next_send(stream, &buff.msg.send_timestamp);
524 sem_post(&sem_thread_finished);
529 static int negotiate_contract_for_stream_fwp(struct stream *stream)
531 struct fwp_contract contract;
535 contract.budget = stream->packet_size;
536 contract.period_usec = stream->period_usec;
537 ret = fwp_contract_negotiate(&contract, &vres_id);
539 if (contract.status == FWP_CNT_NEGOTIATED) {
540 stream->vres_id = vres_id;
542 stream->vres_id = -1;
550 static void create_stream_endpoint_fwp(struct stream *stream)
552 /* TODO: How to create an enpoint? */
555 static void create_stream_endpoint_native(struct stream *stream)
559 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
561 stream->rem_addr.sin_family = AF_INET;
562 ph = gethostbyname(server_addr);
564 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
566 perror("Unknown server");
569 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
574 calc_stream_params(struct stream *stream)
577 unsigned period_usec;
581 /* If some parameters are not set explicitely, use default values. */
582 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
583 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
584 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
586 bandwidth = stream->bandwidth_bps;
588 /* Avoid arithmetic exception. Server thread will exit if
589 stream->bandwidth_bps == 0. */
590 if (bandwidth == 0) bandwidth = 1;
592 if (stream->packet_size) {
593 packet_size = stream->packet_size;
594 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
595 if (period_usec == 0) period_usec = 1;
596 } else if (stream->period_usec) {
597 period_usec = stream->period_usec;
598 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
601 stream_to_text(buf, sizeof(buf), stream, 0);
602 fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
606 if (packet_size < sizeof(struct msg_t)) {
607 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
611 stream->packet_size = packet_size;
612 stream->period_usec = period_usec;
613 stream->jitter = opt_jitter;
615 ret = negotiate_contract_for_stream(stream);
617 create_stream_endpoint(stream);
620 stream_to_text(buf, sizeof(buf), stream, 0);
621 fprintf(stderr, "Contract hasn't been accepted: %s\n", buf);
622 stream->bandwidth_bps = 0;
627 * Parse -b parameter.
629 * @param params String to parse
631 * @return NULL in case of success, pointer to a problematic character
634 char* parse_bandwidths(char *params)
636 struct stream *sp = &streams[nr_streams];
638 while (*params && nr_streams < MAX_STREAMS) {
639 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
643 if (strlen(params) < 2)
645 for (i=0; i<AC_NUM; i++) {
646 if (strncmp(params, ac_ids[i], 2) == 0) {
648 params+=strlen(ac_ids[i]);
656 if (*params == ':') {
659 bw = strtol(params, &next_char, 10);
660 if (next_char == params)
666 sp->bandwidth_bps = bw*Kbit;
669 long packet_size = 0;
670 if (*params == '@') {
672 period = strtol(params, &next_char, 10);
678 if (*params == '/') {
680 packet_size = strtol(params, &next_char, 10);
681 if (packet_size == 0)
689 sp->period_usec = period*MSEC_TO_USEC;
690 sp->packet_size = packet_size;
694 if (*params != '\0' && *params != ',')
704 int main(int argc, char *argv[])
706 int ac, i, rc, seconds;
712 while ((opt = getopt(argc, argv, "B:b:c:g:j:o:qQ:s:T:")) != -1) {
715 opt_def_bandwidth = atoi(optarg);
719 error = parse_bandwidths(optarg);
722 fprintf(stderr, "Bandwidth parse error - string to short\n");
724 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
730 opt_count_sec = atoi(optarg);
733 opt_granularity_usec = atoi(optarg);
734 if (opt_granularity_usec < MIN_GRANULARITY) {
735 fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
740 opt_jitter = atoi(optarg);
746 opt_send_buf_size = atoi(optarg);
749 opt_wait_for_queue_is_full = true;
752 opt_packet_size = atoi(optarg);
755 opt_def_period_msec = atoi(optarg);
758 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
759 fprintf(stderr, "Options:\n");
760 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
761 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
762 fprintf(stderr, " -c count (number of seconds to run)\n");
763 fprintf(stderr, " -g histogram granularity [usec]\n");
764 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
765 fprintf(stderr, " -o output filename (.dat will be appended)\n");
766 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
767 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
768 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
769 fprintf(stderr, " -T default period for -b option [msec]\n");
773 if (opt_packet_size && opt_def_period_msec) {
774 fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
779 server_addr = argv[optind];
781 fprintf(stderr, "Expected server address argument\n");
786 parse_bandwidths("BE");
788 pthread_attr_init(&attr);
790 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
792 if ((logfd = fopen(logfname,"w+")) == NULL) {
793 fprintf(stderr,"Can not open %s\n", logfname);
796 if (signal(SIGTERM, stopper) == SIG_ERR) {
797 perror("Error in signal registration");
801 if (signal(SIGINT, stopper) == SIG_ERR) {
802 perror("Signal handler registration error");
807 sa.sa_handler = empty_handler;
808 sa.sa_flags = 0; /* don't restart syscalls */
810 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
811 perror("sigaction error");
815 sem_init(&sem_thread_finished, 0, 0);
819 /* create four receivers each per AC */
820 for (ac = AC_NUM - 1; ac >= 0; ac--) {
821 ac_sockfd[ac] = create_ac_socket(ac);
822 if (ac_sockfd[ac] < 0) {
825 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
827 fprintf(stderr, "Error while creating receiver %d\n",rc);
832 /* create sendpoints */
833 for (i = 0; i < nr_streams; i++) {
834 struct stream *s = &streams[i];
835 pthread_mutex_init(&s->mutex, NULL);
836 calc_stream_params(s);
837 rc = pthread_create(&thread, &attr, sender, (void*) s);
839 fprintf(stderr, "Error while creating sender %d\n",rc);
848 fprintf(stderr, "\r%3ds", seconds);
849 for (ac = 0; ac < AC_NUM; ac++) {
850 int delta = receivers[ac].received - receivers[ac].last_received;
851 receivers[ac].last_received = receivers[ac].received;
852 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
855 if (seconds == opt_count_sec)
859 fprintf(stderr, "\nWaiting for threads to finish\n");
860 /* Wait for all threads to finish */
861 for (i=0; i < nr_streams + AC_NUM; i++) {
862 sem_wait(&sem_thread_finished);
864 struct timespec end_timestamp, measure_length;
865 clock_gettime(CLOCK_REALTIME,&end_timestamp);
866 timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
868 save_results(argc, argv, timespec2usec(&measure_length));