3 #include <sys/socket.h>
4 #include <netinet/in.h>
19 #include <semaphore.h>
23 #define MAX_SENDENDPOINTS 10
25 unsigned opt_packet_size = 800;
26 unsigned opt_period_usec = 10*MSEC;
27 unsigned opt_jitter = 0;
28 char *opt_output = "delay_stats";
29 unsigned opt_count_sec = 0;
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
55 #define GRANULARITY 100
57 unsigned delay_stats[AC_NUM][MAX_DELAY_US/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;
66 struct send_endpoint {
68 long period_usec; /* all time units are in microseconds */
69 int bandwidth_bps; /* bits per second */
73 struct send_endpoint sepoint[] = {
74 { .ac = AC_VO, .period_usec=200*MSEC, .bandwidth_bps = 34*Kbit },
75 { .ac = AC_VI, .period_usec=25*MSEC, .bandwidth_bps = 480*Kbit },
76 { .ac = AC_BE, .period_usec=40*MSEC, .bandwidth_bps = 300*Kbit },
77 { .ac = AC_BK, .period_usec=40*MSEC, .bandwidth_bps = 300*Kbit },
78 // { .ac = AC_VI, .period_usec=17*MSEC, .bandwidth_bps = 675*Kbit },
82 struct send_endpoint sepoint[] = {
83 { .ac = AC_VO, .period_usec=10*MSEC, .bandwidth_bps = 300*Kbit },
84 { .ac = AC_VI, .period_usec=10*MSEC, .bandwidth_bps = 300*Kbit },
85 { .ac = AC_BE, .period_usec=10*MSEC, .bandwidth_bps = 300*Kbit },
86 { .ac = AC_BK, .period_usec=10*MSEC, .bandwidth_bps = 300*Kbit },
89 unsigned int nr_sepoints = sizeof(sepoint)/sizeof(*sepoint);
91 sem_t sem_thread_finished;
93 bool exit_flag = false;
100 /* Interrupt all receivers */
101 for (i=0; i < AC_NUM; i++) {
102 pthread_kill(receivers[i].thread, SIGUSR1);
110 unsigned sum[AC_NUM];
112 fprintf(stderr, "\nWriting data to %s...\n", logfname);
115 for (maxi = MAX_DELAY_US/GRANULARITY - 1; maxi >= 0; maxi--) {
116 for (ac = 0; ac < AC_NUM; ac++) {
117 if (delay_stats[ac][maxi] != 0) allzeros = false;
119 if (!allzeros) break;
121 if (maxi < 3000/GRANULARITY) maxi = 3000/GRANULARITY;
123 for (ac = 0; ac < AC_NUM; ac++) {
125 for ( i = 0 ; i < maxi; i++)
126 sum[ac]+=delay_stats[ac][i];
128 fprintf(stderr, "No response in AC %d\n", ac);
131 for ( i = 0 ; i < maxi; i++) {
132 fprintf(logfd,"\n%f", i*GRANULARITY/1000.0);
133 for (ac = 0; ac < AC_NUM; ac++) {
135 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
136 fprintf(logfd," %lf", val);
140 fprintf(stderr, "Finished.\n");
147 void timespec_add (struct timespec *sum, const struct timespec *left,
148 const struct timespec *right)
150 sum->tv_sec = left->tv_sec + right->tv_sec;
151 sum->tv_nsec = left->tv_nsec + right->tv_nsec;
153 if (sum->tv_nsec >= 1000000000){
155 sum->tv_nsec -= 1000000000;
160 void timespec_sub (struct timespec *diff, const struct timespec *left,
161 const struct timespec *right)
163 diff->tv_sec = left->tv_sec - right->tv_sec;
164 diff->tv_nsec = left->tv_nsec - right->tv_nsec;
166 if (diff->tv_nsec < 0){
168 diff->tv_nsec += 1000000000;
172 int create_ac_socket(unsigned int ac)
175 unsigned int yes=1, tos;
178 if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
180 perror("Unable to open socket");
184 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
185 perror("Unable to set socket");
190 //tos = ((AC_NUM - ac) *2 - 1)*32;
192 if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
193 perror("Unable to set TOS");
205 void* receiver(void* queue)
208 struct sockaddr_in rem_addr;
210 unsigned int ac, rem_addr_length;
211 unsigned long int trans_time_usec;
212 unsigned long int min_trans_time;
213 struct timespec send_timestamp,recv_timestamp, trans_time;
221 rem_addr_length = sizeof(rem_addr);
223 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0, \
224 (struct sockaddr*)&rem_addr, &rem_addr_length);
226 if (errno == EINTR) continue;
227 perror("Chyba pri prijimani pozadavku");
230 clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
231 send_timestamp = msg.send_timestamp;
233 timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
234 trans_time_usec = (trans_time.tv_sec * SEC_TO_USEC + \
235 trans_time.tv_nsec / USEC_TO_NSEC) /2;
237 if (trans_time_usec < MAX_DELAY_US)
238 delay_stats[ac][trans_time_usec/GRANULARITY]++;
240 receivers[ac].received++;
242 /*if (trans_time_nsec < min_trans_time)
243 min_trans_time = trans_time_nsec;*/
244 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
245 "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
246 msg.seqn, msg.tos, send_timestamp.tv_sec,\
247 send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
248 recv_timestamp.tv_nsec, trans_time_msec); */
251 sem_post(&sem_thread_finished);
255 void* sender(void* endpoint)
257 struct sockaddr_in rem_addr;
259 unsigned long int seqn;
260 struct timespec time_to_wait, current_time, period, interval;
262 struct send_endpoint* spoint = (struct send_endpoint*) endpoint;
263 char buf1[12], buf2[12], buf3[12];
266 unsigned period_usec;
267 char stream_desc[100];
269 if (opt_packet_size) {
270 packet_size = opt_packet_size;
271 period_usec = SEC_TO_USEC*packet_size*8/spoint->bandwidth_bps;
273 period_usec = spoint->period_usec;
274 packet_size = (long long)spoint->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
276 snprintf(stream_desc, sizeof(stream_desc), "%d: %s %s (%d bytes per %s +-%s, %d packets/s)\n",
277 spoint-sepoint, ac_to_text[ac], bandwidth_to_text(buf1, spoint->bandwidth_bps),
278 packet_size, usec_to_text(buf2, period_usec),
279 usec_to_text(buf3, opt_jitter*period_usec/100), (int) SEC_TO_USEC/period_usec);
280 printf("%s", stream_desc);
281 fprintf(logfd, "# Stream %s", stream_desc);
283 if (packet_size < sizeof(struct msg_t)) {
284 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
289 memset(&rem_addr,0, sizeof(rem_addr));
291 rem_addr.sin_family = AF_INET;
292 ph = gethostbyname(server_addr);
294 rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
296 perror("Unknown server");
299 rem_addr.sin_port = htons(BASE_PORT + ac);
307 buff.msg.seqn = seqn;
308 buff.msg.tos = ac_to_tos[ac];
310 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
312 while (sendto(ac_sockfd[ac], &buff, packet_size, 0,\
313 (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
314 if (errno == EINTR) continue;
315 perror("Error while sending");
325 /* |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
326 /* |-------------| nominal period*/
328 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
329 + rand() % (2*period_usec*opt_jitter/100));
331 period.tv_nsec = USEC_TO_NSEC*(period_usec);
335 timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
336 clock_gettime(CLOCK_MONOTONIC,¤t_time);
337 timespec_sub(&interval,&time_to_wait,¤t_time);
338 nanosleep(&interval,NULL);
341 sem_post(&sem_thread_finished);
345 int main(int argc, char *argv[])
353 while ((opt = getopt(argc, argv, "c:j:o:s:")) != -1) {
356 opt_count_sec = atoi(optarg);
359 opt_jitter = atoi(optarg);
365 opt_packet_size = atoi(optarg);
368 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
369 fprintf(stderr, "Options:\n");
370 fprintf(stderr, " -c count (number of seconds to run)");
371 fprintf(stderr, " -j send jitter (0-100) [%%]\n");
372 fprintf(stderr, " -o output filename (.dat will be appended)");
373 fprintf(stderr, " -s size of data payload in packets [bytes]\n");
378 server_addr = argv[optind];
380 fprintf(stderr, "Expected server address argument\n");
386 memset(delay_stats,0, sizeof(delay_stats));
387 pthread_attr_init(&attr);
389 snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
391 if ((logfd = fopen(logfname,"w+")) == NULL) {
392 fprintf(stderr,"Can not open %s\n", logfname);
395 fprintf(logfd, "# Invoked as: ");
396 for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
397 fprintf(logfd, "\n");
399 if (signal(SIGTERM, stopper) == SIG_ERR) {
400 perror("Error in signal registration");
404 if (signal(SIGINT, stopper) == SIG_ERR) {
405 perror("Signal handler registration error");
410 sa.sa_handler = empty_handler;
411 sa.sa_flags = 0; /* don't restart syscalls */
413 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
414 perror("sigaction error");
418 sem_init(&sem_thread_finished, 0, 0);
420 /* create four receivers each per AC */
421 for (ac = AC_NUM - 1; ac >= 0; ac--) {
422 ac_sockfd[ac] = create_ac_socket(ac);
423 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
425 fprintf(stderr, "Error while creating receiver %d\n",rc);
430 /* create sendpoints */
431 for (i = 0; i < nr_sepoints; i++) {
432 rc = pthread_create(&thread, &attr, sender, (void*) &sepoint[i]);
434 fprintf(stderr, "Error while creating sender %d\n",rc);
441 fprintf(stderr, "\r");
442 for (ac = 0; ac < AC_NUM; ac++) {
443 int delta = receivers[ac].received - receivers[ac].last_received;
444 receivers[ac].last_received = receivers[ac].received;
445 fprintf(stderr, "%s: %5d (+%4d) ", ac_to_text[ac], receivers[ac].received, delta);
450 if (i == opt_count_sec) exit_flag = 1;
453 fprintf(stderr, "\nWaiting for threads to finish\n");
454 /* Wait for all threads to finish */
455 for (i=0; i < nr_sepoints + AC_NUM; i++) {
456 sem_wait(&sem_thread_finished);