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;
120 /* Interrupt all receivers */
121 for (i=0; i < AC_NUM; i++) {
122 pthread_kill(receivers[i].thread, SIGUSR1);
126 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
132 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
133 bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
134 stream->sent, stream->sent*SEC_TO_USEC/useconds,
135 stream->received, stream->received*SEC_TO_USEC/useconds);
140 snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
141 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
142 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
143 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
144 (int)(SEC_TO_USEC/stream->period_usec), real);
147 void save_results(int argc, char *argv[], int useconds)
150 const int mini = 3000/opt_granularity_usec;
152 unsigned send_count[AC_NUM];
154 fprintf(stderr, "Writing data to %s... ", logfname);
157 fprintf(logfd, "# Invoked as: ");
158 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
159 fprintf(logfd, "\n");
161 if (useconds/SEC_TO_USEC != opt_count_sec) {
163 usec_to_text(buf, useconds);
164 fprintf(logfd, "# Data gathered for %s.\n", buf);
167 for (i = 0; i < nr_streams; i++) {
168 char stream_desc[200];
169 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
170 fprintf(logfd, "# Stream %s\n", stream_desc);
173 /* Find maximal delay */
175 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
176 for (ac = 0; ac < AC_NUM; ac++) {
177 if ((delay_stats[ac][maxi].csc != 0) ||
178 (delay_stats[ac][maxi].cs != 0) ||
179 (delay_stats[ac][maxi].sc != 0))
182 if (!allzeros) break;
185 if (maxi < mini) maxi = mini;
187 /* Calculate total number of sent packets per AC */
188 memset(send_count, 0, sizeof(send_count));
189 for (i = 0; i < nr_streams; i++) {
191 send_count[ac] += streams[i].sent;
196 for ( i = 0 ; i < maxi; i++) {
197 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
198 for (ac = 0; ac < AC_NUM; ac++) {
200 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
201 else val = -1; /* Don't display this ac */
202 fprintf(logfd," %lf", val);
206 fprintf(logfd,"\n\n");
209 fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
212 for (ac = 0; ac < AC_NUM; ac++) {
213 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
215 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
217 if (send_count[ac] != 0) {
219 while ((delay_stats[ac][i].csc == 0) &&
220 (delay_stats[ac][i].cs == 0) &&
221 (delay_stats[ac][i].sc == 0)) i++;
223 for (i++; i < maxi+1; i++) {
224 if (memcmp(&last, &integral, sizeof(last))) {
225 fprintf(logfd,"%f %f %f %f\n", i*opt_granularity_usec/1000.0,
226 (double)integral.csc*100.0 / send_count[ac],
227 (double)integral.cs *100.0 / send_count[ac],
228 (double)integral.sc *100.0 / send_count[ac]
233 integral.csc += delay_stats[ac][i-1].csc;
234 integral.sc += delay_stats[ac][i-1].sc;
235 integral.cs += delay_stats[ac][i-1].cs;
239 fprintf(logfd,"\n\n");
242 fprintf(stderr, "finished.\n");
249 void timespec_add (struct timespec *sum, const struct timespec *left,
250 const struct timespec *right)
252 sum->tv_sec = left->tv_sec + right->tv_sec;
253 sum->tv_nsec = left->tv_nsec + right->tv_nsec;
255 if (sum->tv_nsec >= 1000000000){
257 sum->tv_nsec -= 1000000000;
262 void timespec_sub (struct timespec *diff, const struct timespec *left,
263 const struct timespec *right)
265 diff->tv_sec = left->tv_sec - right->tv_sec;
266 diff->tv_nsec = left->tv_nsec - right->tv_nsec;
268 if (diff->tv_nsec < 0){
270 diff->tv_nsec += 1000000000;
274 static inline long long timespec_sub_usec(const struct timespec *left,
275 const struct timespec *right)
277 struct timespec result;
278 timespec_sub(&result, left, right);
279 return (long long)result.tv_sec * SEC_TO_USEC +
280 result.tv_nsec / USEC_TO_NSEC;
283 static inline long long timespec2usec(const struct timespec *ts)
285 return ts->tv_sec * SEC_TO_USEC + ts->tv_nsec / USEC_TO_NSEC;
289 int create_ac_socket(unsigned int ac)
292 unsigned int yes=1, tos;
295 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
297 perror("Unable to open socket");
300 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
301 perror("set non-blocking socket");
304 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
305 perror("Unable to set socket");
309 if (opt_send_buf_size >= 0) {
310 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
311 perror("Unable to set socket buffer size");
317 //tos = ((AC_NUM - ac) *2 - 1)*32;
319 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
320 perror("Unable to set TOS");
332 void reset_statistics()
335 for (i = 0; i < nr_streams; i++) {
336 pthread_mutex_lock(&streams[i].mutex);
338 streams[i].really_sent = 0;
339 streams[i].received = 0;
340 pthread_mutex_unlock(&streams[i].mutex);
342 pthread_mutex_lock(&delay_stats_mutex);
343 clock_gettime(CLOCK_REALTIME, &reset_timestamp);
344 memset(delay_stats, 0, sizeof(delay_stats));
345 pthread_mutex_unlock(&delay_stats_mutex);
348 void* receiver(void* queue)
351 struct sockaddr_in rem_addr;
353 unsigned int ac, rem_addr_length;
354 long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
355 long long int min_trans_time;
356 struct timespec send_timestamp, server_timestamp, recv_timestamp;
365 rem_addr_length = sizeof(rem_addr);
368 FD_SET(ac_sockfd[ac], &fdset);
369 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
371 if (errno == EINTR) continue;
372 perror("receiver select");
375 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,
376 (struct sockaddr*)&rem_addr, &rem_addr_length);
378 perror("Chyba pri prijimani pozadavku");
381 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
382 send_timestamp = msg.send_timestamp;
383 server_timestamp = msg.sendback_timestamp;
385 /* Check whether this message was sent after reset_statistics() */
387 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
388 continue; /* If so, don't count it */
391 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
392 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
393 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
395 pthread_mutex_lock(&delay_stats_mutex);
396 if (trans_time_usec < MAX_DELAY_US) {
397 delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
399 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
400 delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
402 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
403 delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
405 pthread_mutex_unlock(&delay_stats_mutex);
407 receivers[ac].received++;
409 pthread_mutex_lock(&streams[msg.stream].mutex);
410 streams[msg.stream].received++;
411 pthread_mutex_unlock(&streams[msg.stream].mutex);
413 /*if (trans_time_nsec < min_trans_time)
414 min_trans_time = trans_time_nsec;*/
415 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
416 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
417 msg.seqn, msg.tos, send_timestamp.tv_sec,\
418 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
419 recv_timestamp.tv_nsec, trans_time_msec); */
422 sem_post(&sem_thread_finished);
429 * @return -1 in case of error, 1 in case of sucessfull send and 0
430 * when all buffers are full.
433 send_packet(struct stream* stream, union msg_buff* buff)
436 while (sendto(ac_sockfd[stream->ac], buff, stream->packet_size, 0,
437 (struct sockaddr*)&stream->rem_addr, sizeof(stream->rem_addr)) < 0) {
438 if (errno == EINTR) continue;
439 if (errno == EAGAIN) {
440 if (opt_wait_for_queue_is_full &&
441 !some_queue_is_full &&
442 /* We use mutex as atomic test and set */
443 (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
444 some_queue_is_full = true;
450 perror("Error while sending");
459 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
461 struct timespec time_to_wait, current_time, period, interval;
462 unsigned period_usec = stream->period_usec;
464 /* |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
465 /* |-------------| nominal period*/
466 if (stream->jitter) {
467 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
468 + rand() % (2*period_usec*stream->jitter/100));
470 period.tv_nsec = USEC_TO_NSEC*(period_usec);
474 timespec_add(&time_to_wait, last_send_time, &period);
475 clock_gettime(CLOCK_REALTIME,¤t_time);
476 timespec_sub(&interval,&time_to_wait,¤t_time);
477 nanosleep(&interval,NULL);
481 void* sender(void* arg)
484 unsigned long int seqn;
485 struct stream* stream = (struct stream*) arg;
486 char stream_desc[100];
489 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
490 printf("%s\n", stream_desc);
492 if (stream->bandwidth_bps == 0)
498 set_rt_prio(90-stream->ac);
502 /* buff.msg.seqn = seqn++; */
503 /* buff.msg.tos = ac_to_tos[stream->ac]; */
504 buff.msg.stream = stream-streams;
506 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
508 ret = send_packet(stream, &buff);
513 pthread_mutex_lock(&stream->mutex);
516 stream->really_sent++;
517 pthread_mutex_unlock(&stream->mutex);
520 printf("%d", stream->ac);
524 wait_for_next_send(stream, &buff.msg.send_timestamp);
527 sem_post(&sem_thread_finished);
532 static int negotiate_contract_for_stream(struct stream *stream)
534 struct fwp_contract contr;
538 contr->budget = stream->packet_size;
539 contr->period_usec = stream->period_usec;
540 ret = fwp_contract_negotiate(&contr, &vres_id);
542 if (contract->status == FWP_CNT_NEGOTIATED) {
543 stream->vres_id = vres_id;
545 stream->vres_id = -1;
551 #define negotiate_contract_for_stream(s) 0
553 void create_stream_endpoint(struct stream *stream)
557 memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
559 stream->rem_addr.sin_family = AF_INET;
560 ph = gethostbyname(server_addr);
562 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
564 perror("Unknown server");
567 stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
572 calc_stream_params(struct stream *stream)
575 unsigned period_usec;
579 /* If some parameters are not set explicitely, use default values. */
580 if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
581 if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
582 if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
584 bandwidth = stream->bandwidth_bps;
586 /* Avoid arithmetic exception. Server thread will exit if
587 stream->bandwidth_bps == 0. */
588 if (bandwidth == 0) bandwidth = 1;
590 if (stream->packet_size) {
591 packet_size = stream->packet_size;
592 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
593 if (period_usec == 0) period_usec = 1;
594 } else if (stream->period_usec) {
595 period_usec = stream->period_usec;
596 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
599 stream_to_text(buf, sizeof(buf), stream, 0);
600 fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
604 if (packet_size < sizeof(struct msg_t)) {
605 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
609 stream->packet_size = packet_size;
610 stream->period_usec = period_usec;
611 stream->jitter = opt_jitter;
613 ret = negotiate_contract_for_stream(stream);
615 create_stream_endpoint(stream);
618 stream_to_text(buf, sizeof(buf), stream, 0);
619 fprintf(stderr, "Contract hasn't been accepted: %s\n", buf);
620 stream->bandwidth_bps = 0;
625 * Parse -b parameter.
627 * @param params String to parse
629 * @return NULL in case of success, pointer to a problematic character
632 char* parse_bandwidths(char *params)
634 struct stream *sp = &streams[nr_streams];
636 while (*params && nr_streams < MAX_STREAMS) {
637 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
641 if (strlen(params) < 2)
643 for (i=0; i<AC_NUM; i++) {
644 if (strncmp(params, ac_ids[i], 2) == 0) {
646 params+=strlen(ac_ids[i]);
654 if (*params == ':') {
657 bw = strtol(params, &next_char, 10);
658 if (next_char == params)
664 sp->bandwidth_bps = bw*Kbit;
667 long packet_size = 0;
668 if (*params == '@') {
670 period = strtol(params, &next_char, 10);
676 if (*params == '/') {
678 packet_size = strtol(params, &next_char, 10);
679 if (packet_size == 0)
687 sp->period_usec = period*MSEC_TO_USEC;
688 sp->packet_size = packet_size;
692 if (*params != '\0' && *params != ',')
702 int main(int argc, char *argv[])
704 int ac, i, rc, seconds;
710 while ((opt = getopt(argc, argv, "B:b:c:g:j:o:qQ:s:T:")) != -1) {
713 opt_def_bandwidth = atoi(optarg);
717 error = parse_bandwidths(optarg);
720 fprintf(stderr, "Bandwidth parse error - string to short\n");
722 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
728 opt_count_sec = atoi(optarg);
731 opt_granularity_usec = atoi(optarg);
732 if (opt_granularity_usec < MIN_GRANULARITY) {
733 fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
738 opt_jitter = atoi(optarg);
744 opt_send_buf_size = atoi(optarg);
747 opt_wait_for_queue_is_full = true;
750 opt_packet_size = atoi(optarg);
753 opt_def_period_msec = atoi(optarg);
756 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
757 fprintf(stderr, "Options:\n");
758 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
759 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
760 fprintf(stderr, " -c count (number of seconds to run)\n");
761 fprintf(stderr, " -g histogram granularity [usec]\n");
762 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
763 fprintf(stderr, " -o output filename (.dat will be appended)\n");
764 fprintf(stderr, " -q gather statistics only after some queue becomes full\n");
765 fprintf(stderr, " -Q <bytes> set size for socket send buffers\n");
766 fprintf(stderr, " -s size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
767 fprintf(stderr, " -T default period for -b option [msec]\n");
771 if (opt_packet_size && opt_def_period_msec) {
772 fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
777 server_addr = argv[optind];
779 fprintf(stderr, "Expected server address argument\n");
784 parse_bandwidths("BE");
786 pthread_attr_init(&attr);
788 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
790 if ((logfd = fopen(logfname,"w+")) == NULL) {
791 fprintf(stderr,"Can not open %s\n", logfname);
794 if (signal(SIGTERM, stopper) == SIG_ERR) {
795 perror("Error in signal registration");
799 if (signal(SIGINT, stopper) == SIG_ERR) {
800 perror("Signal handler registration error");
805 sa.sa_handler = empty_handler;
806 sa.sa_flags = 0; /* don't restart syscalls */
808 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
809 perror("sigaction error");
813 sem_init(&sem_thread_finished, 0, 0);
817 /* create four receivers each per AC */
818 for (ac = AC_NUM - 1; ac >= 0; ac--) {
819 ac_sockfd[ac] = create_ac_socket(ac);
820 if (ac_sockfd[ac] < 0) {
823 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
825 fprintf(stderr, "Error while creating receiver %d\n",rc);
830 /* create sendpoints */
831 for (i = 0; i < nr_streams; i++) {
832 struct stream *s = &streams[i];
833 pthread_mutex_init(&s->mutex, NULL);
834 calc_stream_params(s);
835 rc = pthread_create(&thread, &attr, sender, (void*) s);
837 fprintf(stderr, "Error while creating sender %d\n",rc);
846 fprintf(stderr, "\r%3ds", seconds);
847 for (ac = 0; ac < AC_NUM; ac++) {
848 int delta = receivers[ac].received - receivers[ac].last_received;
849 receivers[ac].last_received = receivers[ac].received;
850 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
853 if (seconds == opt_count_sec)
857 fprintf(stderr, "\nWaiting for threads to finish\n");
858 /* Wait for all threads to finish */
859 for (i=0; i < nr_streams + AC_NUM; i++) {
860 sem_wait(&sem_thread_finished);
862 struct timespec end_timestamp, measure_length;
863 clock_gettime(CLOCK_REALTIME,&end_timestamp);
864 timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
866 save_results(argc, argv, timespec2usec(&measure_length));