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 int opt_granularity_usec = MIN_GRANULARITY;
31 int ac_sockfd[AC_NUM];
35 unsigned received, last_received;
44 struct timespec send_timestamp;
45 unsigned long int seqn;
51 char nonsense[BUFFSIZE];
54 /* maximal traffic delay in ms - 10 s*/
55 #define MAX_DELAY_US 10000000
57 unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
59 /*struct ac_stats[AC_NUM] {
60 unsigned long int min_trans_time;
61 unsigned long int sum_trans_time;
62 struct timespec recv_timestamp;
63 struct timespec send_timestamp;
67 /* Input parameters */
69 int bandwidth_bps; /* bits per second */
70 int jitter; /* percent */
71 /* Mulualy exclusive input parameters */
73 long period_usec; /* all time units are in microseconds */
76 unsigned long long sent, received;
80 struct send_endpoint sepoint[] = {
81 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
82 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
83 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
84 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
85 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
89 struct stream streams[MAX_STREAMS];
91 unsigned int nr_streams = 0;
93 sem_t sem_thread_finished;
95 bool exit_flag = false;
102 /* Interrupt all receivers */
103 for (i=0; i < AC_NUM; i++) {
104 pthread_kill(receivers[i].thread, SIGUSR1);
108 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, int seconds)
114 snprintf(real, sizeof(real), "; real: sent %lld (%lld/s), received %lld (%lld/s)",
115 stream->sent, stream->sent/seconds,
116 stream->received, stream->received/seconds);
119 snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
120 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
121 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
122 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
123 (int)(SEC_TO_USEC/stream->period_usec), real);
126 void save_results(int argc, char *argv[], int seconds)
129 const int mini = 3000/opt_granularity_usec;
131 unsigned sum[AC_NUM];
134 fprintf(stderr, "Writing data to %s... ", logfname);
137 fprintf(logfd, "# Invoked as: ");
138 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
139 fprintf(logfd, "\n");
141 for (i = 0; i < nr_streams; i++) {
142 char stream_desc[120];
143 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], seconds);
144 fprintf(logfd, "# Stream %s\n", stream_desc);
147 /* Find maximal delay */
149 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
150 for (ac = 0; ac < AC_NUM; ac++) {
151 if (delay_stats[ac][maxi] != 0) allzeros = false;
153 if (!allzeros) break;
156 if (maxi < mini) maxi = mini;
158 /* Calculate total number of received packets per AC */
159 for (ac = 0; ac < AC_NUM; ac++) {
161 for ( i = 0 ; i < maxi; i++)
162 sum[ac]+=delay_stats[ac][i];
163 /* if (sum[ac] == 0) */
164 /* fprintf(stderr, "No response in AC %d\n", ac); */
168 for ( i = 0 ; i < maxi; i++) {
169 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
170 for (ac = 0; ac < AC_NUM; ac++) {
172 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
173 else val = -1; /* Don't display this ac */
174 fprintf(logfd," %lf", val);
178 fprintf(logfd,"\n\n");
181 unsigned long long integral[AC_NUM];
182 for (ac = 0; ac < AC_NUM; ac++) integral[ac] = 0;
183 for ( i = 0 ; i < maxi+1; i++) {
184 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
185 for (ac = 0; ac < AC_NUM; ac++) {
187 val = (double)integral[ac]*100.0 / sum[ac];
188 else val = -1; /* Don't display this ac */
189 fprintf(logfd," %lf", val);
191 integral[ac] += delay_stats[ac][i-1];
195 fprintf(stderr, "finished.\n");
202 void timespec_add (struct timespec *sum, const struct timespec *left,
203 const struct timespec *right)
205 sum->tv_sec = left->tv_sec + right->tv_sec;
206 sum->tv_nsec = left->tv_nsec + right->tv_nsec;
208 if (sum->tv_nsec >= 1000000000){
210 sum->tv_nsec -= 1000000000;
215 void timespec_sub (struct timespec *diff, const struct timespec *left,
216 const struct timespec *right)
218 diff->tv_sec = left->tv_sec - right->tv_sec;
219 diff->tv_nsec = left->tv_nsec - right->tv_nsec;
221 if (diff->tv_nsec < 0){
223 diff->tv_nsec += 1000000000;
227 int create_ac_socket(unsigned int ac)
230 unsigned int yes=1, tos;
233 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
235 perror("Unable to open socket");
239 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
240 perror("Unable to set socket");
245 //tos = ((AC_NUM - ac) *2 - 1)*32;
247 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
248 perror("Unable to set TOS");
260 void* receiver(void* queue)
263 struct sockaddr_in rem_addr;
265 unsigned int ac, rem_addr_length;
266 unsigned long int trans_time_usec;
267 unsigned long int min_trans_time;
268 struct timespec send_timestamp,recv_timestamp, trans_time;
276 rem_addr_length = sizeof(rem_addr);
278 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0, \
279 (struct sockaddr*)&rem_addr, &rem_addr_length);
281 if (errno == EINTR) continue;
282 perror("Chyba pri prijimani pozadavku");
285 clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
286 send_timestamp = msg.send_timestamp;
288 timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
289 trans_time_usec = (trans_time.tv_sec * SEC_TO_USEC + \
290 trans_time.tv_nsec / USEC_TO_NSEC) /2;
292 if (trans_time_usec < MAX_DELAY_US)
293 delay_stats[ac][trans_time_usec/opt_granularity_usec]++;
295 receivers[ac].received++;
296 streams[msg.stream].received++;
298 /*if (trans_time_nsec < min_trans_time)
299 min_trans_time = trans_time_nsec;*/
300 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
301 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
302 msg.seqn, msg.tos, send_timestamp.tv_sec,\
303 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
304 recv_timestamp.tv_nsec, trans_time_msec); */
307 sem_post(&sem_thread_finished);
311 void* sender(void* arg)
313 struct sockaddr_in rem_addr;
315 unsigned long int seqn;
316 struct timespec time_to_wait, current_time, period, interval;
318 struct stream* stream = (struct stream*) arg;
320 unsigned period_usec;
321 char stream_desc[100];
323 set_rt_prio(90-stream->ac);
325 if (opt_packet_size) {
326 packet_size = opt_packet_size;
327 period_usec = SEC_TO_USEC*packet_size*8/stream->bandwidth_bps;
329 period_usec = stream->period_usec;
330 packet_size = (long long)stream->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
332 stream->packet_size = packet_size;
333 stream->period_usec = period_usec;
334 stream->jitter = opt_jitter;
337 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
339 printf("%s\n", stream_desc);
341 if (packet_size < sizeof(struct msg_t)) {
342 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
347 memset(&rem_addr,0, sizeof(rem_addr));
349 rem_addr.sin_family = AF_INET;
350 ph = gethostbyname(server_addr);
352 rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
354 perror("Unknown server");
357 rem_addr.sin_port = htons(BASE_PORT + stream->ac);
364 buff.msg.seqn = seqn;
365 buff.msg.tos = ac_to_tos[stream->ac];
367 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
369 while (sendto(ac_sockfd[stream->ac], &buff, packet_size, 0,\
370 (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
371 if (errno == EINTR) continue;
372 perror("Error while sending");
383 /* |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
384 /* |-------------| nominal period*/
386 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
387 + rand() % (2*period_usec*opt_jitter/100));
389 period.tv_nsec = USEC_TO_NSEC*(period_usec);
393 timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
394 clock_gettime(CLOCK_MONOTONIC,¤t_time);
395 timespec_sub(&interval,&time_to_wait,¤t_time);
396 nanosleep(&interval,NULL);
399 sem_post(&sem_thread_finished);
403 char* parse_bandwidths(char *params)
405 struct stream *sp = &streams[nr_streams];
407 while (*params && nr_streams < MAX_STREAMS) {
408 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
411 if (strlen(params) < 2)
413 for (i=0; i<AC_NUM; i++) {
414 if (strncmp(params, ac_ids[i], 2) == 0) {
416 params+=strlen(ac_ids[i]);
420 if (i==AC_NUM || *params=='\0' || *params!=':')
426 bw = strtol(params, &next_char, 10);
429 sp->bandwidth_bps = bw*Kbit;
433 if (*params == '@') {
435 period = strtol(params, &next_char, 10);
442 sp->period_usec = period*MSEC_TO_USEC;
444 if (*params != '\0' && *params != ',')
454 int main(int argc, char *argv[])
456 int ac, i, rc, seconds;
462 while ((opt = getopt(argc, argv, "b:c:g:j:o:s:")) != -1) {
466 error = parse_bandwidths(optarg);
469 fprintf(stderr, "Bandwidth parse error - string to short\n");
471 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
477 opt_count_sec = atoi(optarg);
480 opt_granularity_usec = atoi(optarg);
481 if (opt_granularity_usec < MIN_GRANULARITY) {
482 fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
487 opt_jitter = atoi(optarg);
493 opt_packet_size = atoi(optarg);
496 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
497 fprintf(stderr, "Options:\n");
498 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK):<kbit>[@<msec>][,...]\n");
499 fprintf(stderr, " -c count (number of seconds to run)\n");
500 fprintf(stderr, " -g histogram granularity [usec]\n");
501 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
502 fprintf(stderr, " -o output filename (.dat will be appended)\n");
503 fprintf(stderr, " -s size of data payload in packets [bytes]\n");
508 server_addr = argv[optind];
510 fprintf(stderr, "Expected server address argument\n");
516 parse_bandwidths("BE:300@10");
518 memset(delay_stats,0, sizeof(delay_stats));
519 pthread_attr_init(&attr);
521 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
523 if ((logfd = fopen(logfname,"w+")) == NULL) {
524 fprintf(stderr,"Can not open %s\n", logfname);
527 if (signal(SIGTERM, stopper) == SIG_ERR) {
528 perror("Error in signal registration");
532 if (signal(SIGINT, stopper) == SIG_ERR) {
533 perror("Signal handler registration error");
538 sa.sa_handler = empty_handler;
539 sa.sa_flags = 0; /* don't restart syscalls */
541 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
542 perror("sigaction error");
546 sem_init(&sem_thread_finished, 0, 0);
548 /* create four receivers each per AC */
549 for (ac = AC_NUM - 1; ac >= 0; ac--) {
550 ac_sockfd[ac] = create_ac_socket(ac);
551 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
553 fprintf(stderr, "Error while creating receiver %d\n",rc);
558 /* create sendpoints */
559 for (i = 0; i < nr_streams; i++) {
560 rc = pthread_create(&thread, &attr, sender, (void*) &streams[i]);
562 fprintf(stderr, "Error while creating sender %d\n",rc);
570 fprintf(stderr, "\r");
571 for (ac = 0; ac < AC_NUM; ac++) {
572 int delta = receivers[ac].received - receivers[ac].last_received;
573 receivers[ac].last_received = receivers[ac].received;
574 fprintf(stderr, "%s: %5d (+%4d) ", ac_to_text[ac], receivers[ac].received, delta);
578 if (seconds == opt_count_sec)
582 fprintf(stderr, "\nWaiting for threads to finish\n");
583 /* Wait for all threads to finish */
584 for (i=0; i < nr_streams + AC_NUM; i++) {
585 sem_wait(&sem_thread_finished);
588 save_results(argc, argv, seconds);