3 #include <sys/socket.h>
4 #include <netinet/in.h>
19 #include <semaphore.h>
21 #define MAX_STREAMS 10
22 #define MIN_GRANULARITY 100
24 unsigned opt_packet_size = 800;
25 unsigned opt_period_usec = 10*MSEC_TO_USEC;
26 unsigned opt_jitter = 0;
27 char *opt_output = "delay_stats";
28 unsigned opt_count_sec = 0;
29 unsigned opt_def_bandwidth = 200;
30 unsigned opt_def_period_msec = 10;
31 int opt_granularity_usec = MIN_GRANULARITY;
33 int ac_sockfd[AC_NUM];
37 unsigned received, last_received;
44 /* maximal traffic delay in ms - 10 s*/
45 #define MAX_DELAY_US 10000000
47 unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
49 /*struct ac_stats[AC_NUM] {
50 unsigned long int min_trans_time;
51 unsigned long int sum_trans_time;
52 struct timespec recv_timestamp;
53 struct timespec send_timestamp;
57 /* Input parameters */
59 int bandwidth_bps; /* bits per second */
60 int jitter; /* percent */
61 /* Mulualy exclusive input parameters */
63 long period_usec; /* all time units are in microseconds */
66 unsigned long long sent, received;
70 struct send_endpoint sepoint[] = {
71 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
72 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
73 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
74 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
75 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
79 struct stream streams[MAX_STREAMS];
81 unsigned int nr_streams = 0;
83 sem_t sem_thread_finished;
85 bool exit_flag = false;
92 /* Interrupt all receivers */
93 for (i=0; i < AC_NUM; i++) {
94 pthread_kill(receivers[i].thread, SIGUSR1);
98 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, int seconds)
104 snprintf(real, sizeof(real), "; real: sent %lld (%lld/s), received %lld (%lld/s)",
105 stream->sent, stream->sent/seconds,
106 stream->received, stream->received/seconds);
109 snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
110 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
111 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
112 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
113 (int)(SEC_TO_USEC/stream->period_usec), real);
116 void save_results(int argc, char *argv[], int seconds)
119 const int mini = 3000/opt_granularity_usec;
121 unsigned sum[AC_NUM];
124 fprintf(stderr, "Writing data to %s... ", logfname);
127 fprintf(logfd, "# Invoked as: ");
128 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
129 fprintf(logfd, "\n");
131 for (i = 0; i < nr_streams; i++) {
132 char stream_desc[120];
133 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], seconds);
134 fprintf(logfd, "# Stream %s\n", stream_desc);
137 /* Find maximal delay */
139 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
140 for (ac = 0; ac < AC_NUM; ac++) {
141 if (delay_stats[ac][maxi] != 0) allzeros = false;
143 if (!allzeros) break;
146 if (maxi < mini) maxi = mini;
148 /* Calculate total number of sent packets per AC */
149 for (ac = 0; ac < AC_NUM; ac++) sum[ac] = 0;
150 for (i = 0; i < nr_streams; i++) {
152 sum[ac] += streams[i].sent;
157 for ( i = 0 ; i < maxi; i++) {
158 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
159 for (ac = 0; ac < AC_NUM; ac++) {
161 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
162 else val = -1; /* Don't display this ac */
163 fprintf(logfd," %lf", val);
167 fprintf(logfd,"\n\n");
171 for (ac = 0; ac < AC_NUM; ac++) {
172 unsigned long long integral = 0, last = -1;
174 fprintf(logfd,"%f %f\n", 0.0, 0.0);
178 while (delay_stats[ac][i] == 0) i++;
180 for (i++; i < maxi+1; i++) {
181 if (last != integral) {
182 val = (double)integral*100.0 / sum[ac];
183 fprintf(logfd,"%f %f\n", i*opt_granularity_usec/1000.0, val);
187 integral += delay_stats[ac][i-1];
190 fprintf(logfd,"\n\n");
193 fprintf(stderr, "finished.\n");
200 void timespec_add (struct timespec *sum, const struct timespec *left,
201 const struct timespec *right)
203 sum->tv_sec = left->tv_sec + right->tv_sec;
204 sum->tv_nsec = left->tv_nsec + right->tv_nsec;
206 if (sum->tv_nsec >= 1000000000){
208 sum->tv_nsec -= 1000000000;
213 void timespec_sub (struct timespec *diff, const struct timespec *left,
214 const struct timespec *right)
216 diff->tv_sec = left->tv_sec - right->tv_sec;
217 diff->tv_nsec = left->tv_nsec - right->tv_nsec;
219 if (diff->tv_nsec < 0){
221 diff->tv_nsec += 1000000000;
225 int create_ac_socket(unsigned int ac)
228 unsigned int yes=1, tos;
231 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
233 perror("Unable to open socket");
236 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
237 perror("set non-blocking socket");
240 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
241 perror("Unable to set socket");
246 //tos = ((AC_NUM - ac) *2 - 1)*32;
248 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
249 perror("Unable to set TOS");
261 void* receiver(void* queue)
264 struct sockaddr_in rem_addr;
266 unsigned int ac, rem_addr_length;
267 unsigned long int trans_time_usec;
268 unsigned long int min_trans_time;
269 struct timespec send_timestamp,recv_timestamp, trans_time;
278 rem_addr_length = sizeof(rem_addr);
281 FD_SET(ac_sockfd[ac], &fdset);
282 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
284 if (errno == EINTR) continue;
285 perror("receiver select");
288 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0, \
289 (struct sockaddr*)&rem_addr, &rem_addr_length);
291 perror("Chyba pri prijimani pozadavku");
294 clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
295 send_timestamp = msg.send_timestamp;
297 timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
298 trans_time_usec = (trans_time.tv_sec * SEC_TO_USEC + \
299 trans_time.tv_nsec / USEC_TO_NSEC) /2;
301 if (trans_time_usec < MAX_DELAY_US)
302 delay_stats[ac][trans_time_usec/opt_granularity_usec]++;
304 receivers[ac].received++;
305 streams[msg.stream].received++;
307 /*if (trans_time_nsec < min_trans_time)
308 min_trans_time = trans_time_nsec;*/
309 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
310 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
311 msg.seqn, msg.tos, send_timestamp.tv_sec,\
312 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
313 recv_timestamp.tv_nsec, trans_time_msec); */
316 sem_post(&sem_thread_finished);
320 void* sender(void* arg)
322 struct sockaddr_in rem_addr;
324 unsigned long int seqn;
325 struct timespec time_to_wait, current_time, period, interval;
327 struct stream* stream = (struct stream*) arg;
329 unsigned period_usec;
330 char stream_desc[100];
332 if (stream->bandwidth_bps == 0)
335 set_rt_prio(90-stream->ac);
337 if (opt_packet_size) {
338 packet_size = opt_packet_size;
339 period_usec = SEC_TO_USEC*packet_size*8/stream->bandwidth_bps;
341 period_usec = stream->period_usec;
342 packet_size = (long long)stream->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
344 stream->packet_size = packet_size;
345 stream->period_usec = period_usec;
346 stream->jitter = opt_jitter;
349 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
351 printf("%s\n", stream_desc);
353 if (packet_size < sizeof(struct msg_t)) {
354 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
359 memset(&rem_addr,0, sizeof(rem_addr));
361 rem_addr.sin_family = AF_INET;
362 ph = gethostbyname(server_addr);
364 rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
366 perror("Unknown server");
369 rem_addr.sin_port = htons(BASE_PORT + stream->ac);
376 buff.msg.seqn = seqn;
377 buff.msg.tos = ac_to_tos[stream->ac];
378 buff.msg.stream = stream-streams;
380 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
382 while (sendto(ac_sockfd[stream->ac], &buff, packet_size, 0,\
383 (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
384 if (errno == EINTR) continue;
385 if (errno == EAGAIN) {
388 perror("Error while sending");
400 /* |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
401 /* |-------------| nominal period*/
403 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
404 + rand() % (2*period_usec*opt_jitter/100));
406 period.tv_nsec = USEC_TO_NSEC*(period_usec);
410 timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
411 clock_gettime(CLOCK_MONOTONIC,¤t_time);
412 timespec_sub(&interval,&time_to_wait,¤t_time);
413 nanosleep(&interval,NULL);
416 sem_post(&sem_thread_finished);
420 char* parse_bandwidths(char *params)
422 struct stream *sp = &streams[nr_streams];
424 while (*params && nr_streams < MAX_STREAMS) {
425 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
429 if (strlen(params) < 2)
431 for (i=0; i<AC_NUM; i++) {
432 if (strncmp(params, ac_ids[i], 2) == 0) {
434 params+=strlen(ac_ids[i]);
442 if (*params == ':') {
445 bw = strtol(params, &next_char, 10);
446 if (next_char == params)
450 bw = opt_def_bandwidth;
452 sp->bandwidth_bps = bw*Kbit;
455 if (*params == '@') {
457 period = strtol(params, &next_char, 10);
463 period = opt_def_period_msec;
464 sp->period_usec = period*MSEC_TO_USEC;
466 if (*params != '\0' && *params != ',')
476 int main(int argc, char *argv[])
478 int ac, i, rc, seconds;
484 while ((opt = getopt(argc, argv, "B:b:c:g:j:o:s:T:")) != -1) {
487 opt_def_bandwidth = atoi(optarg);
491 error = parse_bandwidths(optarg);
494 fprintf(stderr, "Bandwidth parse error - string to short\n");
496 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
502 opt_count_sec = atoi(optarg);
505 opt_granularity_usec = atoi(optarg);
506 if (opt_granularity_usec < MIN_GRANULARITY) {
507 fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
512 opt_jitter = atoi(optarg);
518 opt_packet_size = atoi(optarg);
521 opt_def_period_msec = atoi(optarg);
524 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
525 fprintf(stderr, "Options:\n");
526 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
527 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec>][,...]\n");
528 fprintf(stderr, " -c count (number of seconds to run)\n");
529 fprintf(stderr, " -g histogram granularity [usec]\n");
530 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
531 fprintf(stderr, " -o output filename (.dat will be appended)\n");
532 fprintf(stderr, " -s size of data payload in packets [bytes]\n");
533 fprintf(stderr, " -T default period for -b option [msec]\n");
538 server_addr = argv[optind];
540 fprintf(stderr, "Expected server address argument\n");
546 parse_bandwidths("BE");
548 memset(delay_stats,0, sizeof(delay_stats));
549 pthread_attr_init(&attr);
551 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
553 if ((logfd = fopen(logfname,"w+")) == NULL) {
554 fprintf(stderr,"Can not open %s\n", logfname);
557 if (signal(SIGTERM, stopper) == SIG_ERR) {
558 perror("Error in signal registration");
562 if (signal(SIGINT, stopper) == SIG_ERR) {
563 perror("Signal handler registration error");
568 sa.sa_handler = empty_handler;
569 sa.sa_flags = 0; /* don't restart syscalls */
571 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
572 perror("sigaction error");
576 sem_init(&sem_thread_finished, 0, 0);
578 /* create four receivers each per AC */
579 for (ac = AC_NUM - 1; ac >= 0; ac--) {
580 ac_sockfd[ac] = create_ac_socket(ac);
581 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
583 fprintf(stderr, "Error while creating receiver %d\n",rc);
588 /* create sendpoints */
589 for (i = 0; i < nr_streams; i++) {
590 rc = pthread_create(&thread, &attr, sender, (void*) &streams[i]);
592 fprintf(stderr, "Error while creating sender %d\n",rc);
601 fprintf(stderr, "\r%3ds", seconds);
602 for (ac = 0; ac < AC_NUM; ac++) {
603 int delta = receivers[ac].received - receivers[ac].last_received;
604 receivers[ac].last_received = receivers[ac].received;
605 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
608 if (seconds == opt_count_sec)
612 fprintf(stderr, "\nWaiting for threads to finish\n");
613 /* Wait for all threads to finish */
614 for (i=0; i < nr_streams + AC_NUM; i++) {
615 sem_wait(&sem_thread_finished);
618 save_results(argc, argv, seconds);