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 received packets per AC */
149 for (ac = 0; ac < AC_NUM; ac++) {
151 for ( i = 0 ; i < maxi; i++)
152 sum[ac]+=delay_stats[ac][i];
153 /* if (sum[ac] == 0) */
154 /* fprintf(stderr, "No response in AC %d\n", ac); */
159 for ( i = 0 ; i < maxi; i++) {
160 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
161 for (ac = 0; ac < AC_NUM; ac++) {
163 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
164 else val = -1; /* Don't display this ac */
165 fprintf(logfd," %lf", val);
169 fprintf(logfd,"\n\n");
173 for (ac = 0; ac < AC_NUM; ac++) {
174 unsigned long long integral = 0, last = -1;
176 fprintf(logfd,"%f %f\n", 0.0, 0.0);
180 while (delay_stats[ac][i] == 0) i++;
182 for (i++; i < maxi+1; i++) {
183 if (last != integral) {
184 val = (double)integral*100.0 / sum[ac];
185 fprintf(logfd,"%f %f\n", i*opt_granularity_usec/1000.0, val);
189 integral += delay_stats[ac][i-1];
192 fprintf(logfd,"\n\n");
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 if (stream->bandwidth_bps == 0)
326 set_rt_prio(90-stream->ac);
328 if (opt_packet_size) {
329 packet_size = opt_packet_size;
330 period_usec = SEC_TO_USEC*packet_size*8/stream->bandwidth_bps;
332 period_usec = stream->period_usec;
333 packet_size = (long long)stream->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
335 stream->packet_size = packet_size;
336 stream->period_usec = period_usec;
337 stream->jitter = opt_jitter;
340 stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
342 printf("%s\n", stream_desc);
344 if (packet_size < sizeof(struct msg_t)) {
345 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
350 memset(&rem_addr,0, sizeof(rem_addr));
352 rem_addr.sin_family = AF_INET;
353 ph = gethostbyname(server_addr);
355 rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
357 perror("Unknown server");
360 rem_addr.sin_port = htons(BASE_PORT + stream->ac);
367 buff.msg.seqn = seqn;
368 buff.msg.tos = ac_to_tos[stream->ac];
369 buff.msg.stream = stream-streams;
371 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
373 while (sendto(ac_sockfd[stream->ac], &buff, packet_size, 0,\
374 (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
375 if (errno == EINTR) continue;
376 perror("Error while sending");
387 /* |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
388 /* |-------------| nominal period*/
390 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
391 + rand() % (2*period_usec*opt_jitter/100));
393 period.tv_nsec = USEC_TO_NSEC*(period_usec);
397 timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
398 clock_gettime(CLOCK_MONOTONIC,¤t_time);
399 timespec_sub(&interval,&time_to_wait,¤t_time);
400 nanosleep(&interval,NULL);
403 sem_post(&sem_thread_finished);
407 char* parse_bandwidths(char *params)
409 struct stream *sp = &streams[nr_streams];
411 while (*params && nr_streams < MAX_STREAMS) {
412 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
416 if (strlen(params) < 2)
418 for (i=0; i<AC_NUM; i++) {
419 if (strncmp(params, ac_ids[i], 2) == 0) {
421 params+=strlen(ac_ids[i]);
429 if (*params == ':') {
432 bw = strtol(params, &next_char, 10);
433 if (next_char == params)
437 bw = opt_def_bandwidth;
439 sp->bandwidth_bps = bw*Kbit;
442 if (*params == '@') {
444 period = strtol(params, &next_char, 10);
450 period = opt_def_period_msec;
451 sp->period_usec = period*MSEC_TO_USEC;
453 if (*params != '\0' && *params != ',')
463 int main(int argc, char *argv[])
465 int ac, i, rc, seconds;
471 while ((opt = getopt(argc, argv, "B:b:c:g:j:o:s:T:")) != -1) {
474 opt_def_bandwidth = atoi(optarg);
478 error = parse_bandwidths(optarg);
481 fprintf(stderr, "Bandwidth parse error - string to short\n");
483 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
489 opt_count_sec = atoi(optarg);
492 opt_granularity_usec = atoi(optarg);
493 if (opt_granularity_usec < MIN_GRANULARITY) {
494 fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
499 opt_jitter = atoi(optarg);
505 opt_packet_size = atoi(optarg);
508 opt_def_period_msec = atoi(optarg);
511 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
512 fprintf(stderr, "Options:\n");
513 fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
514 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec>][,...]\n");
515 fprintf(stderr, " -c count (number of seconds to run)\n");
516 fprintf(stderr, " -g histogram granularity [usec]\n");
517 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
518 fprintf(stderr, " -o output filename (.dat will be appended)\n");
519 fprintf(stderr, " -s size of data payload in packets [bytes]\n");
520 fprintf(stderr, " -T default period for -b option [msec]\n");
525 server_addr = argv[optind];
527 fprintf(stderr, "Expected server address argument\n");
533 parse_bandwidths("BE");
535 memset(delay_stats,0, sizeof(delay_stats));
536 pthread_attr_init(&attr);
538 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
540 if ((logfd = fopen(logfname,"w+")) == NULL) {
541 fprintf(stderr,"Can not open %s\n", logfname);
544 if (signal(SIGTERM, stopper) == SIG_ERR) {
545 perror("Error in signal registration");
549 if (signal(SIGINT, stopper) == SIG_ERR) {
550 perror("Signal handler registration error");
555 sa.sa_handler = empty_handler;
556 sa.sa_flags = 0; /* don't restart syscalls */
558 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
559 perror("sigaction error");
563 sem_init(&sem_thread_finished, 0, 0);
565 /* create four receivers each per AC */
566 for (ac = AC_NUM - 1; ac >= 0; ac--) {
567 ac_sockfd[ac] = create_ac_socket(ac);
568 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
570 fprintf(stderr, "Error while creating receiver %d\n",rc);
575 /* create sendpoints */
576 for (i = 0; i < nr_streams; i++) {
577 rc = pthread_create(&thread, &attr, sender, (void*) &streams[i]);
579 fprintf(stderr, "Error while creating sender %d\n",rc);
588 fprintf(stderr, "\r%3ds", seconds);
589 for (ac = 0; ac < AC_NUM; ac++) {
590 int delta = receivers[ac].received - receivers[ac].last_received;
591 receivers[ac].last_received = receivers[ac].received;
592 fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
595 if (seconds == opt_count_sec)
599 fprintf(stderr, "\nWaiting for threads to finish\n");
600 /* Wait for all threads to finish */
601 for (i=0; i < nr_streams + AC_NUM; i++) {
602 sem_wait(&sem_thread_finished);
605 save_results(argc, argv, seconds);