3 #include <sys/socket.h>
4 #include <netinet/in.h>
19 #include <semaphore.h>
21 #define MAX_SENDENDPOINTS 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;
50 char nonsense[BUFFSIZE];
53 /* maximal traffic delay in ms - 10 s*/
54 #define MAX_DELAY_US 10000000
56 unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
58 /*struct ac_stats[AC_NUM] {
59 unsigned long int min_trans_time;
60 unsigned long int sum_trans_time;
61 struct timespec recv_timestamp;
62 struct timespec send_timestamp;
65 struct send_endpoint {
67 long period_usec; /* all time units are in microseconds */
68 int bandwidth_bps; /* bits per second */
72 struct send_endpoint sepoint[] = {
73 { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
74 { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
75 { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
76 { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
77 // { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
81 struct send_endpoint sendpoints[MAX_SENDENDPOINTS];
83 unsigned int nr_sepoints = 0;
85 sem_t sem_thread_finished;
87 bool exit_flag = false;
94 /* Interrupt all receivers */
95 for (i=0; i < AC_NUM; i++) {
96 pthread_kill(receivers[i].thread, SIGUSR1);
103 const int mini = 3000/opt_granularity_usec;
105 unsigned sum[AC_NUM];
108 fprintf(stderr, "Writing data to %s... ", logfname);
111 /* Find maximal delay */
113 for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
114 for (ac = 0; ac < AC_NUM; ac++) {
115 if (delay_stats[ac][maxi] != 0) allzeros = false;
117 if (!allzeros) break;
120 if (maxi < mini) maxi = mini;
122 /* Calculate total number of received packets per AC */
123 for (ac = 0; ac < AC_NUM; ac++) {
125 for ( i = 0 ; i < maxi; i++)
126 sum[ac]+=delay_stats[ac][i];
127 /* if (sum[ac] == 0) */
128 /* fprintf(stderr, "No response in AC %d\n", ac); */
132 for ( i = 0 ; i < maxi; i++) {
133 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
134 for (ac = 0; ac < AC_NUM; ac++) {
136 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
137 else val = -1; /* Don't display this ac */
138 fprintf(logfd," %lf", val);
142 fprintf(logfd,"\n\n");
145 unsigned long long integral[AC_NUM];
146 for (ac = 0; ac < AC_NUM; ac++) integral[ac] = 0;
147 for ( i = 0 ; i < maxi+1; i++) {
148 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
149 for (ac = 0; ac < AC_NUM; ac++) {
151 val = (double)integral[ac]*100.0 / sum[ac];
152 else val = -1; /* Don't display this ac */
153 fprintf(logfd," %lf", val);
155 integral[ac] += delay_stats[ac][i-1];
159 fprintf(stderr, "finished.\n");
166 void timespec_add (struct timespec *sum, const struct timespec *left,
167 const struct timespec *right)
169 sum->tv_sec = left->tv_sec + right->tv_sec;
170 sum->tv_nsec = left->tv_nsec + right->tv_nsec;
172 if (sum->tv_nsec >= 1000000000){
174 sum->tv_nsec -= 1000000000;
179 void timespec_sub (struct timespec *diff, const struct timespec *left,
180 const struct timespec *right)
182 diff->tv_sec = left->tv_sec - right->tv_sec;
183 diff->tv_nsec = left->tv_nsec - right->tv_nsec;
185 if (diff->tv_nsec < 0){
187 diff->tv_nsec += 1000000000;
191 int create_ac_socket(unsigned int ac)
194 unsigned int yes=1, tos;
197 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
199 perror("Unable to open socket");
203 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
204 perror("Unable to set socket");
209 //tos = ((AC_NUM - ac) *2 - 1)*32;
211 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
212 perror("Unable to set TOS");
224 void* receiver(void* queue)
227 struct sockaddr_in rem_addr;
229 unsigned int ac, rem_addr_length;
230 unsigned long int trans_time_usec;
231 unsigned long int min_trans_time;
232 struct timespec send_timestamp,recv_timestamp, trans_time;
240 rem_addr_length = sizeof(rem_addr);
242 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0, \
243 (struct sockaddr*)&rem_addr, &rem_addr_length);
245 if (errno == EINTR) continue;
246 perror("Chyba pri prijimani pozadavku");
249 clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
250 send_timestamp = msg.send_timestamp;
252 timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
253 trans_time_usec = (trans_time.tv_sec * SEC_TO_USEC + \
254 trans_time.tv_nsec / USEC_TO_NSEC) /2;
256 if (trans_time_usec < MAX_DELAY_US)
257 delay_stats[ac][trans_time_usec/opt_granularity_usec]++;
259 receivers[ac].received++;
261 /*if (trans_time_nsec < min_trans_time)
262 min_trans_time = trans_time_nsec;*/
263 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
264 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
265 msg.seqn, msg.tos, send_timestamp.tv_sec,\
266 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
267 recv_timestamp.tv_nsec, trans_time_msec); */
270 sem_post(&sem_thread_finished);
274 void* sender(void* endpoint)
276 struct sockaddr_in rem_addr;
278 unsigned long int seqn;
279 struct timespec time_to_wait, current_time, period, interval;
281 struct send_endpoint* spoint = (struct send_endpoint*) endpoint;
282 char buf1[12], buf2[12], buf3[12];
285 unsigned period_usec;
286 char stream_desc[100];
290 if (opt_packet_size) {
291 packet_size = opt_packet_size;
292 period_usec = SEC_TO_USEC*packet_size*8/spoint->bandwidth_bps;
294 period_usec = spoint->period_usec;
295 packet_size = (long long)spoint->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
297 snprintf(stream_desc, sizeof(stream_desc), "%d: %s %s (%d bytes per %s +-%s, %d packets/s)\n",
298 spoint-sendpoints, ac_to_text[ac], bandwidth_to_text(buf1, spoint->bandwidth_bps),
299 packet_size, usec_to_text(buf2, period_usec),
300 usec_to_text(buf3, opt_jitter*period_usec/100), (int) SEC_TO_USEC/period_usec);
301 printf("%s", stream_desc);
302 fprintf(logfd, "# Stream %s", stream_desc);
304 if (packet_size < sizeof(struct msg_t)) {
305 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
310 memset(&rem_addr,0, sizeof(rem_addr));
312 rem_addr.sin_family = AF_INET;
313 ph = gethostbyname(server_addr);
315 rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
317 perror("Unknown server");
320 rem_addr.sin_port = htons(BASE_PORT + ac);
327 buff.msg.seqn = seqn;
328 buff.msg.tos = ac_to_tos[ac];
330 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
332 while (sendto(ac_sockfd[ac], &buff, packet_size, 0,\
333 (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
334 if (errno == EINTR) continue;
335 perror("Error while sending");
345 /* |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
346 /* |-------------| nominal period*/
348 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
349 + rand() % (2*period_usec*opt_jitter/100));
351 period.tv_nsec = USEC_TO_NSEC*(period_usec);
355 timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
356 clock_gettime(CLOCK_MONOTONIC,¤t_time);
357 timespec_sub(&interval,&time_to_wait,¤t_time);
358 nanosleep(&interval,NULL);
361 sem_post(&sem_thread_finished);
365 char* parse_bandwidths(char *params)
367 struct send_endpoint *sp = &sendpoints[nr_sepoints];
369 while (*params && nr_sepoints < MAX_SENDENDPOINTS) {
370 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
373 if (strlen(params) < 2)
375 for (i=0; i<AC_NUM; i++) {
376 if (strncmp(params, ac_ids[i], 2) == 0) {
378 params+=strlen(ac_ids[i]);
382 if (i==AC_NUM || *params=='\0' || *params!=':')
388 bw = strtol(params, &next_char, 10);
391 sp->bandwidth_bps = bw*Kbit;
395 if (*params == '@') {
397 period = strtol(params, &next_char, 10);
404 sp->period_usec = period*MSEC_TO_USEC;
406 if (*params != '\0' && *params != ',')
416 int main(int argc, char *argv[])
424 while ((opt = getopt(argc, argv, "b:c:g:j:o:s:")) != -1) {
428 error = parse_bandwidths(optarg);
431 fprintf(stderr, "Bandwidth parse error - string to short\n");
433 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
439 opt_count_sec = atoi(optarg);
442 opt_granularity_usec = atoi(optarg);
443 if (opt_granularity_usec < MIN_GRANULARITY) {
444 fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
449 opt_jitter = atoi(optarg);
455 opt_packet_size = atoi(optarg);
458 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
459 fprintf(stderr, "Options:\n");
460 fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK):<kbit>[@<msec>][,...]\n");
461 fprintf(stderr, " -c count (number of seconds to run)\n");
462 fprintf(stderr, " -g histogram granularity [usec]\n");
463 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
464 fprintf(stderr, " -o output filename (.dat will be appended)\n");
465 fprintf(stderr, " -s size of data payload in packets [bytes]\n");
470 server_addr = argv[optind];
472 fprintf(stderr, "Expected server address argument\n");
477 if (nr_sepoints == 0)
478 parse_bandwidths("BE:300@10");
480 memset(delay_stats,0, sizeof(delay_stats));
481 pthread_attr_init(&attr);
483 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
485 if ((logfd = fopen(logfname,"w+")) == NULL) {
486 fprintf(stderr,"Can not open %s\n", logfname);
489 fprintf(logfd, "# Invoked as: ");
490 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
491 fprintf(logfd, "\n");
493 if (signal(SIGTERM, stopper) == SIG_ERR) {
494 perror("Error in signal registration");
498 if (signal(SIGINT, stopper) == SIG_ERR) {
499 perror("Signal handler registration error");
504 sa.sa_handler = empty_handler;
505 sa.sa_flags = 0; /* don't restart syscalls */
507 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
508 perror("sigaction error");
512 sem_init(&sem_thread_finished, 0, 0);
514 /* create four receivers each per AC */
515 for (ac = AC_NUM - 1; ac >= 0; ac--) {
516 ac_sockfd[ac] = create_ac_socket(ac);
517 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
519 fprintf(stderr, "Error while creating receiver %d\n",rc);
524 /* create sendpoints */
525 for (i = 0; i < nr_sepoints; i++) {
526 rc = pthread_create(&thread, &attr, sender, (void*) &sendpoints[i]);
528 fprintf(stderr, "Error while creating sender %d\n",rc);
536 fprintf(stderr, "\r");
537 for (ac = 0; ac < AC_NUM; ac++) {
538 int delta = receivers[ac].received - receivers[ac].last_received;
539 receivers[ac].last_received = receivers[ac].received;
540 fprintf(stderr, "%s: %5d (+%4d) ", ac_to_text[ac], receivers[ac].received, delta);
544 if (i == opt_count_sec)
548 fprintf(stderr, "\nWaiting for threads to finish\n");
549 /* Wait for all threads to finish */
550 for (i=0; i < nr_sepoints + AC_NUM; i++) {
551 sem_wait(&sem_thread_finished);