]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wclient.c
Small fixes of wclient terminal output.
[frescor/fwp.git] / wme_test / wclient.c
1 #include <errno.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <netdb.h>
7 #include <signal.h>
8 #include <sys/wait.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <time.h>
13 #include <string.h>
14 #include <pthread.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdbool.h>
18 #include "common.h"
19 #include <semaphore.h>
20
21 #define MAX_SENDENDPOINTS  10 
22 #define MIN_GRANULARITY 100
23
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;
30
31 int ac_sockfd[AC_NUM];
32
33 struct receiver {
34         pthread_t thread;
35         unsigned received, last_received;
36 } receivers[AC_NUM];
37
38 FILE* logfd;
39 char* server_addr; 
40 char logfname[100];
41
42 struct msg_t {
43         unsigned int tos;
44         struct timespec send_timestamp;
45         unsigned long int seqn;
46 };
47
48 union msg_buff {
49         struct msg_t msg;
50         char nonsense[BUFFSIZE];
51 };
52
53 /* maximal traffic delay in ms - 10 s*/
54 #define MAX_DELAY_US 10000000
55
56 unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
57
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; 
63 };*/
64
65 struct send_endpoint {
66         int ac;
67         long period_usec;       /* all time units are in microseconds */
68         int bandwidth_bps;      /* bits per second */
69 };
70
71 /*
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 },
78 };
79 */
80
81 struct send_endpoint sendpoints[MAX_SENDENDPOINTS];
82
83 unsigned int nr_sepoints = 0;
84
85 sem_t sem_thread_finished;
86
87 bool exit_flag = false;
88
89 void stopper()
90 {
91         int i;
92         exit_flag = true;
93
94         /* Interrupt all receivers */
95         for (i=0; i < AC_NUM; i++) {
96                 pthread_kill(receivers[i].thread, SIGUSR1);
97         }
98 }
99
100 void save_results()
101 {
102         int ac, i, maxi;
103         const int mini = 3000/opt_granularity_usec;
104         bool allzeros;
105         unsigned sum[AC_NUM];
106         double val;
107
108         fprintf(stderr, "Writing data to %s... ", logfname);
109         fflush(stderr);
110
111         /* Find maximal delay */
112         allzeros = true;
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;
116                 }
117                 if (!allzeros) break;
118         }
119         maxi++;
120         if (maxi < mini) maxi = mini;
121
122         /* Calculate total number of received packets per AC */
123         for (ac = 0; ac < AC_NUM; ac++) { 
124                 sum[ac] = 0;
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); */
129         }
130
131         /* Write pdf */
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++) { 
135                         if (sum[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);
139                 }
140         }
141         
142         fprintf(logfd,"\n\n");
143
144         /* Write PDF */
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++) {
150                         if (sum[ac])
151                                 val = (double)integral[ac]*100.0 / sum[ac];
152                         else val = -1; /* Don't display this ac */
153                         fprintf(logfd," %lf", val);
154                         if (i>0)
155                                 integral[ac] += delay_stats[ac][i-1];
156                 }
157         }
158         
159         fprintf(stderr, "finished.\n");
160         fclose(logfd);
161
162         exit(0);
163 }
164
165 static inline 
166 void timespec_add (struct timespec *sum, const struct timespec *left,
167               const struct timespec *right)
168 {
169         sum->tv_sec = left->tv_sec + right->tv_sec;
170         sum->tv_nsec = left->tv_nsec + right->tv_nsec;
171
172         if (sum->tv_nsec >= 1000000000){
173                 ++sum->tv_sec;
174                 sum->tv_nsec -= 1000000000;
175         }
176 }
177
178 static inline 
179 void timespec_sub (struct timespec *diff, const struct timespec *left,
180               const struct timespec *right)
181 {
182         diff->tv_sec = left->tv_sec - right->tv_sec;
183         diff->tv_nsec = left->tv_nsec - right->tv_nsec;
184
185         if (diff->tv_nsec < 0){
186                   --diff->tv_sec;
187                   diff->tv_nsec += 1000000000;
188         }
189 }
190
191 int create_ac_socket(unsigned int ac) 
192 {
193         int sockfd;
194         unsigned int yes=1, tos;
195
196
197         if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
198         {
199                 perror("Unable to open socket");
200                 return -1;
201         }
202         
203         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
204                 perror("Unable to set socket");
205                 return -1;
206         }
207
208         
209         //tos = ((AC_NUM - ac) *2 - 1)*32;
210         tos = ac_to_tos[ac];
211         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
212                 perror("Unable to set TOS");
213                 close(sockfd);
214                 return -1;
215         }
216
217         return sockfd;
218 }
219
220 void empty_handler()
221 {
222 }
223
224 void* receiver(void* queue)
225 {
226         struct msg_t    msg;
227         struct  sockaddr_in rem_addr;
228         int     mlen;
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; 
233         
234         min_trans_time = ~0;
235         
236         block_signals();
237         set_rt_prio(99);
238
239         ac = (int)queue;
240         rem_addr_length = sizeof(rem_addr);
241         while (!exit_flag) {
242                 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,    \
243                                 (struct sockaddr*)&rem_addr, &rem_addr_length);
244                 if (mlen < 0) {
245                         if (errno == EINTR) continue;
246                         perror("Chyba pri prijimani pozadavku");
247                         goto out;
248                 }       
249                 clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
250                 send_timestamp = msg.send_timestamp;
251                 
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;
255           
256                 if (trans_time_usec < MAX_DELAY_US)
257                         delay_stats[ac][trans_time_usec/opt_granularity_usec]++;
258
259                 receivers[ac].received++;
260         
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); */
268         }
269 out:
270         sem_post(&sem_thread_finished);
271         return NULL;
272 }
273
274 void* sender(void* endpoint)
275 {
276         struct sockaddr_in rem_addr;
277         union msg_buff buff;
278         unsigned long int seqn;
279         struct timespec time_to_wait, current_time, period, interval;
280         struct hostent* ph;
281         struct send_endpoint* spoint = (struct send_endpoint*) endpoint;
282         char buf1[12], buf2[12], buf3[12];
283         int ac = spoint->ac;
284         int packet_size;
285         unsigned period_usec;
286         char stream_desc[100];
287
288         set_rt_prio(90-ac);
289
290         if (opt_packet_size) {
291                 packet_size = opt_packet_size;
292                 period_usec = SEC_TO_USEC*packet_size*8/spoint->bandwidth_bps;
293         } else {
294                 period_usec = spoint->period_usec;
295                 packet_size = (long long)spoint->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
296         }
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);
303
304         if (packet_size < sizeof(struct msg_t)) {
305                 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
306                 exit(1);
307         }
308
309         
310         memset(&rem_addr,0, sizeof(rem_addr));
311                 
312         rem_addr.sin_family = AF_INET;
313         ph = gethostbyname(server_addr);
314         if (ph) 
315                 rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
316         else {
317                 perror("Unknown server");
318                 exit(1);
319         }
320         rem_addr.sin_port = htons(BASE_PORT + ac);
321         seqn = 0;
322         
323         block_signals();
324
325         while (!exit_flag) {
326
327                 buff.msg.seqn = seqn;
328                 buff.msg.tos = ac_to_tos[ac];
329                 
330                 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
331                 
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");
336                                 goto out;
337                 }
338
339 #ifdef DEBUG
340                 printf("%d", ac);
341                 fflush(stdout);
342 #endif
343                 seqn++;
344
345                 /*           |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
346                 /* |-------------|     nominal period*/
347                 if (opt_jitter) {
348                         period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
349                                                  + rand() % (2*period_usec*opt_jitter/100));
350                 } else {
351                         period.tv_nsec = USEC_TO_NSEC*(period_usec);
352                 }
353                 period.tv_sec = 0;
354
355                 timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
356                 clock_gettime(CLOCK_MONOTONIC,&current_time);
357                 timespec_sub(&interval,&time_to_wait,&current_time);
358                 nanosleep(&interval,NULL);
359         }
360 out:
361         sem_post(&sem_thread_finished);
362         return NULL;
363 }
364
365 char* parse_bandwidths(char *params)
366 {
367         struct send_endpoint *sp = &sendpoints[nr_sepoints];
368
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" };
371                 int i;
372
373                 if (strlen(params) < 2)
374                         return params;
375                 for (i=0; i<AC_NUM; i++) {
376                         if (strncmp(params, ac_ids[i], 2) == 0) {
377                                 sp->ac = i;
378                                 params+=strlen(ac_ids[i]);
379                                 break;
380                         }
381                 }
382                 if (i==AC_NUM || *params=='\0' || *params!=':')
383                         return params;
384                 params++;
385
386                 long bw;
387                 char *next_char;
388                 bw = strtol(params, &next_char, 10);
389                 if (bw == 0)
390                         return params;
391                 sp->bandwidth_bps = bw*Kbit;
392                 params = next_char;
393
394                 long period;
395                 if (*params == '@') {
396                         params++;
397                         period = strtol(params, &next_char, 10);
398                         if (period == 0)
399                                 return params;
400                         params = next_char;
401                 }
402                 else
403                         period = 10;
404                 sp->period_usec = period*MSEC_TO_USEC;
405
406                 if (*params != '\0' && *params != ',')
407                         return params;
408                 nr_sepoints++;
409                 sp++;
410                 if (*params == ',')
411                         params++;
412         }
413         return NULL;
414 }
415
416 int main(int argc, char *argv[])
417 {
418         int ac, i, rc;
419         pthread_attr_t attr;
420         pthread_t thread;
421         char opt;
422
423
424         while ((opt = getopt(argc, argv, "b:c:g:j:o:s:")) != -1) {
425                 switch (opt) {
426                         case 'b': {
427                                 char *error;
428                                 error = parse_bandwidths(optarg);
429                                 if (error != NULL) {
430                                         if (*error == '\0')
431                                                 fprintf(stderr, "Bandwidth parse error - string to short\n");
432                                         else
433                                                 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
434                                         exit(1);
435                                 }
436                                 break;
437                         }
438                         case 'c':
439                                 opt_count_sec = atoi(optarg);
440                                 break;
441                         case 'g':
442                                 opt_granularity_usec = atoi(optarg);
443                                 if (opt_granularity_usec < MIN_GRANULARITY) {
444                                         fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
445                                         exit(1);
446                                 }
447                                 break;
448                         case 'j':
449                                 opt_jitter = atoi(optarg);
450                                 break;
451                         case 'o':
452                                 opt_output = optarg;
453                                 break;
454                         case 's':
455                                 opt_packet_size = atoi(optarg);
456                                 break;
457                         default:
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");
466                                 exit(1);
467                 }
468         }
469         if (optind < argc) {
470                 server_addr = argv[optind];
471         } else {
472                 fprintf(stderr, "Expected server address argument\n");
473                 exit(1);
474         }
475
476
477         if (nr_sepoints == 0)
478                 parse_bandwidths("BE:300@10");
479                 
480         memset(delay_stats,0, sizeof(delay_stats));     
481         pthread_attr_init(&attr);
482
483         snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
484
485         if ((logfd = fopen(logfname,"w+")) == NULL) {
486                 fprintf(stderr,"Can not open %s\n", logfname);
487                 exit(1);
488         }
489         fprintf(logfd, "# Invoked as: ");
490         for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
491         fprintf(logfd, "\n");
492                 
493         if (signal(SIGTERM, stopper) == SIG_ERR) {
494                 perror("Error in signal registration");
495                 exit(1);
496         }
497                 
498         if (signal(SIGINT, stopper) == SIG_ERR) {
499                 perror("Signal handler registration error");
500                 exit(1);
501         }
502
503         struct sigaction sa;
504         sa.sa_handler = empty_handler;
505         sa.sa_flags = 0;        /* don't restart syscalls */
506
507         if (sigaction(SIGUSR1, &sa, NULL) < 0) {
508                 perror("sigaction error");
509                 exit(1);
510         }
511
512         sem_init(&sem_thread_finished, 0, 0);
513
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);
518                 if (rc) {
519                         fprintf(stderr, "Error while creating receiver %d\n",rc);
520                         return 1;
521                 }
522         }               
523                         
524         /* create sendpoints */
525         for (i = 0; i < nr_sepoints; i++) {
526                 rc = pthread_create(&thread, &attr, sender, (void*) &sendpoints[i]);
527                 if (rc) {
528                         fprintf(stderr, "Error while creating sender %d\n",rc);
529                         return 1;
530                 }
531         }
532         
533         i = 0;
534         while (!exit_flag) {
535                 sleep(1);
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);
541                 }
542                 fflush(stderr);
543                 i++;
544                 if (i == opt_count_sec)
545                         stopper();
546         }
547
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);
552         }
553         save_results();
554
555         return 0;
556 }