]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wclient.c
Merge branch 'master' of molnam1@rtime.felk.cvut.cz:/var/git/frescor
[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_STREAMS  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 unsigned opt_def_bandwidth = 200;
30 unsigned opt_def_period_msec = 10;
31 int opt_granularity_usec = MIN_GRANULARITY;
32
33 int ac_sockfd[AC_NUM];
34
35 struct receiver {
36         pthread_t thread;
37         unsigned received, last_received;
38 } receivers[AC_NUM];
39
40 FILE* logfd;
41 char* server_addr; 
42 char logfname[100];
43
44 /* maximal traffic delay in ms - 10 s*/
45 #define MAX_DELAY_US 10000000
46
47 unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
48
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; 
54 };*/
55
56 struct stream {
57         /* Input parameters */
58         enum ac ac;             /*  */
59         int bandwidth_bps;      /* bits per second */
60         int jitter;             /* percent */
61         /* Mulualy exclusive input parameters */
62         int packet_size;
63         long period_usec;       /* all time units are in microseconds */
64
65         /* Statistics */
66         unsigned long long sent, received;
67 };
68
69 /*
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 },
76 };
77 */
78
79 struct stream streams[MAX_STREAMS];
80
81 unsigned int nr_streams = 0;
82
83 sem_t sem_thread_finished;
84
85 bool exit_flag = false;
86
87 void stopper()
88 {
89         int i;
90         exit_flag = true;
91
92         /* Interrupt all receivers */
93         for (i=0; i < AC_NUM; i++) {
94                 pthread_kill(receivers[i].thread, SIGUSR1);
95         }
96 }
97
98 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, int seconds)
99 {
100         char buf[3][12];
101         char real[100];
102
103         if (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);
107         }
108         
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);
114 }
115
116 void save_results(int argc, char *argv[], int seconds)
117 {
118         int ac, i, maxi;
119         const int mini = 3000/opt_granularity_usec;
120         bool allzeros;
121         unsigned sum[AC_NUM];
122         double val;
123
124         fprintf(stderr, "Writing data to %s... ", logfname);
125         fflush(stderr);
126
127         fprintf(logfd, "# Invoked as: ");
128         for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
129         fprintf(logfd, "\n");
130                 
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);
135         }
136
137         /* Find maximal delay */
138         allzeros = true;
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;
142                 }
143                 if (!allzeros) break;
144         }
145         maxi++;
146         if (maxi < mini) maxi = mini;
147
148         /* Calculate total number of sent packets per AC */
149         for (ac = 0; ac < AC_NUM; ac++) sum[ac] = 0;
150         for (i = 0; i < nr_streams; i++) {
151                 ac = streams[i].ac;
152                 sum[ac] += streams[i].sent;
153         }
154
155 #if 0
156         /* Write pdf */
157         for ( i = 0 ; i < maxi; i++) {
158                 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
159                 for (ac = 0; ac < AC_NUM; ac++) { 
160                         if (sum[ac])
161                                 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
162                         else val = -1; /* Don't display this ac */
163                         fprintf(logfd," %lf", val);
164                 }
165         }
166         
167         fprintf(logfd,"\n\n");
168 #endif
169         
170         /* Write PDF */
171         for (ac = 0; ac < AC_NUM; ac++) {
172                 unsigned long long integral = 0, last = -1;
173
174                 fprintf(logfd,"%f %f\n", 0.0, 0.0);
175
176                 if (sum[ac] != 0) {
177                         i=0;
178                         while (delay_stats[ac][i] == 0) i++;
179                 
180                         for (i++; i < maxi+1; i++) {
181                                 if (last != integral) {
182                                         val = (double)integral*100.0 / sum[ac];
183                                         fprintf(logfd,"%f %f\n", i*opt_granularity_usec/1000.0, val);
184                                         last = integral;
185                                 }
186                                 if (i>0)
187                                         integral += delay_stats[ac][i-1];
188                         }
189                 }
190                 fprintf(logfd,"\n\n");
191         }
192         
193         fprintf(stderr, "finished.\n");
194         fclose(logfd);
195
196         exit(0);
197 }
198
199 static inline 
200 void timespec_add (struct timespec *sum, const struct timespec *left,
201               const struct timespec *right)
202 {
203         sum->tv_sec = left->tv_sec + right->tv_sec;
204         sum->tv_nsec = left->tv_nsec + right->tv_nsec;
205
206         if (sum->tv_nsec >= 1000000000){
207                 ++sum->tv_sec;
208                 sum->tv_nsec -= 1000000000;
209         }
210 }
211
212 static inline 
213 void timespec_sub (struct timespec *diff, const struct timespec *left,
214               const struct timespec *right)
215 {
216         diff->tv_sec = left->tv_sec - right->tv_sec;
217         diff->tv_nsec = left->tv_nsec - right->tv_nsec;
218
219         if (diff->tv_nsec < 0){
220                   --diff->tv_sec;
221                   diff->tv_nsec += 1000000000;
222         }
223 }
224
225 int create_ac_socket(unsigned int ac) 
226 {
227         int sockfd;
228         unsigned int yes=1, tos;
229
230
231         if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
232         {
233                 perror("Unable to open socket");
234                 return -1;
235         }
236         if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
237                 perror("set non-blocking socket");
238                 return -1;
239         }
240         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
241                 perror("Unable to set socket");
242                 return -1;
243         }
244
245         
246         //tos = ((AC_NUM - ac) *2 - 1)*32;
247         tos = ac_to_tos[ac];
248         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
249                 perror("Unable to set TOS");
250                 close(sockfd);
251                 return -1;
252         }
253
254         return sockfd;
255 }
256
257 void empty_handler()
258 {
259 }
260
261 void* receiver(void* queue)
262 {
263         struct msg_t    msg;
264         struct  sockaddr_in rem_addr;
265         int     mlen, ret;
266         unsigned int ac, rem_addr_length; 
267         unsigned long int trans_time_usec;
268         unsigned long int min_trans_time;
269         struct timespec   send_timestamp,recv_timestamp, trans_time;
270         fd_set fdset;
271         
272         min_trans_time = ~0;
273         
274         block_signals();
275         set_rt_prio(99);
276
277         ac = (int)queue;
278         rem_addr_length = sizeof(rem_addr);
279         FD_ZERO(&fdset);
280         while (!exit_flag) {
281                 FD_SET(ac_sockfd[ac], &fdset);
282                 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
283                 if (ret < 0) {
284                         if (errno == EINTR) continue;
285                         perror("receiver select");
286                         goto out;
287                 }
288                 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,    \
289                                 (struct sockaddr*)&rem_addr, &rem_addr_length);
290                 if (mlen < 0) {
291                         perror("Chyba pri prijimani pozadavku");
292                         goto out;
293                 }       
294                 clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
295                 send_timestamp = msg.send_timestamp;
296                 
297                 timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
298                 trans_time_usec = (trans_time.tv_sec * SEC_TO_USEC + \
299                                          trans_time.tv_nsec / USEC_TO_NSEC) /2;
300           
301                 if (trans_time_usec < MAX_DELAY_US)
302                         delay_stats[ac][trans_time_usec/opt_granularity_usec]++;
303
304                 receivers[ac].received++;
305                 streams[msg.stream].received++;
306         
307                 /*if (trans_time_nsec < min_trans_time) 
308                         min_trans_time = trans_time_nsec;*/
309                 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
310                          "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
311                          msg.seqn, msg.tos, send_timestamp.tv_sec,\
312                          send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
313                          recv_timestamp.tv_nsec, trans_time_msec); */
314         }
315 out:
316         sem_post(&sem_thread_finished);
317         return NULL;
318 }
319
320 void* sender(void* arg)
321 {
322         struct sockaddr_in rem_addr;
323         union msg_buff buff;
324         unsigned long int seqn;
325         struct timespec time_to_wait, current_time, period, interval;
326         struct hostent* ph;
327         struct stream* stream = (struct stream*) arg;
328         int packet_size;
329         unsigned period_usec;
330         char stream_desc[100];
331
332         if (stream->bandwidth_bps == 0)
333                 goto out;
334
335         set_rt_prio(90-stream->ac);
336
337         if (opt_packet_size) {
338                 packet_size = opt_packet_size;
339                 period_usec = SEC_TO_USEC*packet_size*8/stream->bandwidth_bps;
340         } else {
341                 period_usec = stream->period_usec;
342                 packet_size = (long long)stream->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
343         }
344         stream->packet_size = packet_size;
345         stream->period_usec = period_usec;
346         stream->jitter = opt_jitter;
347
348
349         stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
350
351         printf("%s\n", stream_desc);
352
353         if (packet_size < sizeof(struct msg_t)) {
354                 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
355                 exit(1);
356         }
357
358         
359         memset(&rem_addr,0, sizeof(rem_addr));
360                 
361         rem_addr.sin_family = AF_INET;
362         ph = gethostbyname(server_addr);
363         if (ph) 
364                 rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
365         else {
366                 perror("Unknown server");
367                 exit(1);
368         }
369         rem_addr.sin_port = htons(BASE_PORT + stream->ac);
370         seqn = 0;
371         
372         block_signals();
373
374         while (!exit_flag) {
375
376                 buff.msg.seqn = seqn;
377                 buff.msg.tos = ac_to_tos[stream->ac];
378                 buff.msg.stream = stream-streams;
379                 
380                 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
381
382                 while (sendto(ac_sockfd[stream->ac], &buff, packet_size, 0,\
383                                 (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
384                                 if (errno == EINTR) continue;
385                                 if (errno != EAGAIN) {
386                                         perror("Error while sending");
387                                         goto out;
388                                 }
389                 }
390
391 #ifdef DEBUG
392                 printf("%d", ac);
393                 fflush(stdout);
394 #endif
395                 seqn++;
396                 stream->sent++;
397
398                 /*           |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
399                 /* |-------------|     nominal period*/
400                 if (opt_jitter) {
401                         period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
402                                                  + rand() % (2*period_usec*opt_jitter/100));
403                 } else {
404                         period.tv_nsec = USEC_TO_NSEC*(period_usec);
405                 }
406                 period.tv_sec = 0;
407
408                 timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
409                 clock_gettime(CLOCK_MONOTONIC,&current_time);
410                 timespec_sub(&interval,&time_to_wait,&current_time);
411                 nanosleep(&interval,NULL);
412         }
413 out:
414         sem_post(&sem_thread_finished);
415         return NULL;
416 }
417
418 char* parse_bandwidths(char *params)
419 {
420         struct stream *sp = &streams[nr_streams];
421
422         while (*params && nr_streams < MAX_STREAMS) {
423                 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
424                 int i;
425                 char *next_char;
426
427                 if (strlen(params) < 2)
428                         return params;
429                 for (i=0; i<AC_NUM; i++) {
430                         if (strncmp(params, ac_ids[i], 2) == 0) {
431                                 sp->ac = i;
432                                 params+=strlen(ac_ids[i]);
433                                 break;
434                         }
435                 }
436                 if (i==AC_NUM)
437                         return params;
438
439                 long bw;
440                 if (*params == ':') {
441                         params++;
442
443                         bw = strtol(params, &next_char, 10);
444                         if (next_char == params)
445                                 return params;
446                         params = next_char;
447                 } else
448                         bw = opt_def_bandwidth;
449                 
450                 sp->bandwidth_bps = bw*Kbit;
451
452                 long period;
453                 if (*params == '@') {
454                         params++;
455                         period = strtol(params, &next_char, 10);
456                         if (period == 0)
457                                 return params;
458                         params = next_char;
459                 }
460                 else
461                         period = opt_def_period_msec;
462                 sp->period_usec = period*MSEC_TO_USEC;
463
464                 if (*params != '\0' && *params != ',')
465                         return params;
466                 nr_streams++;
467                 sp++;
468                 if (*params == ',')
469                         params++;
470         }
471         return NULL;
472 }
473
474 int main(int argc, char *argv[])
475 {
476         int ac, i, rc, seconds;
477         pthread_attr_t attr;
478         pthread_t thread;
479         char opt;
480
481
482         while ((opt = getopt(argc, argv, "B:b:c:g:j:o:s:T:")) != -1) {
483                 switch (opt) {
484                         case 'B':
485                                 opt_def_bandwidth = atoi(optarg);
486                                 break;
487                         case 'b': {
488                                 char *error;
489                                 error = parse_bandwidths(optarg);
490                                 if (error != NULL) {
491                                         if (*error == '\0')
492                                                 fprintf(stderr, "Bandwidth parse error - string to short\n");
493                                         else
494                                                 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
495                                         exit(1);
496                                 }
497                                 break;
498                         }
499                         case 'c':
500                                 opt_count_sec = atoi(optarg);
501                                 break;
502                         case 'g':
503                                 opt_granularity_usec = atoi(optarg);
504                                 if (opt_granularity_usec < MIN_GRANULARITY) {
505                                         fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
506                                         exit(1);
507                                 }
508                                 break;
509                         case 'j':
510                                 opt_jitter = atoi(optarg);
511                                 break;
512                         case 'o':
513                                 opt_output = optarg;
514                                 break;
515                         case 's':
516                                 opt_packet_size = atoi(optarg);
517                                 break;
518                         case 'T':
519                                 opt_def_period_msec = atoi(optarg);
520                                 break;
521                         default:
522                                 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
523                                 fprintf(stderr, "Options:\n");
524                                 fprintf(stderr, "    -B  default bandwidth for -b option [kbit]\n");
525                                 fprintf(stderr, "    -b  bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec>][,...]\n");
526                                 fprintf(stderr, "    -c  count (number of seconds to run)\n");
527                                 fprintf(stderr, "    -g  histogram granularity [usec]\n");
528                                 fprintf(stderr, "    -j  send jitter (0-100) [%%]\n");
529                                 fprintf(stderr, "    -o  output filename (.dat will be appended)\n");
530                                 fprintf(stderr, "    -s  size of data payload in packets [bytes]\n");
531                                 fprintf(stderr, "    -T  default period for -b option [msec]\n");
532                                 exit(1);
533                 }
534         }
535         if (optind < argc) {
536                 server_addr = argv[optind];
537         } else {
538                 fprintf(stderr, "Expected server address argument\n");
539                 exit(1);
540         }
541
542
543         if (nr_streams == 0)
544                 parse_bandwidths("BE");
545                 
546         memset(delay_stats,0, sizeof(delay_stats));     
547         pthread_attr_init(&attr);
548
549         snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
550
551         if ((logfd = fopen(logfname,"w+")) == NULL) {
552                 fprintf(stderr,"Can not open %s\n", logfname);
553                 exit(1);
554         }
555         if (signal(SIGTERM, stopper) == SIG_ERR) {
556                 perror("Error in signal registration");
557                 exit(1);
558         }
559                 
560         if (signal(SIGINT, stopper) == SIG_ERR) {
561                 perror("Signal handler registration error");
562                 exit(1);
563         }
564
565         struct sigaction sa;
566         sa.sa_handler = empty_handler;
567         sa.sa_flags = 0;        /* don't restart syscalls */
568
569         if (sigaction(SIGUSR1, &sa, NULL) < 0) {
570                 perror("sigaction error");
571                 exit(1);
572         }
573
574         sem_init(&sem_thread_finished, 0, 0);
575
576         /* create four receivers each per AC */
577         for (ac = AC_NUM - 1; ac >= 0; ac--) {
578                 ac_sockfd[ac] = create_ac_socket(ac);
579                 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
580                 if (rc) {
581                         fprintf(stderr, "Error while creating receiver %d\n",rc);
582                         return 1;
583                 }
584         }               
585                         
586         /* create sendpoints */
587         for (i = 0; i < nr_streams; i++) {
588                 rc = pthread_create(&thread, &attr, sender, (void*) &streams[i]);
589                 if (rc) {
590                         fprintf(stderr, "Error while creating sender %d\n",rc);
591                         return 1;
592                 }
593         }
594         
595         seconds = 0;
596         while (!exit_flag) {
597                 sleep(1);
598                 seconds++;
599                 fprintf(stderr, "\r%3ds", seconds);
600                 for (ac = 0; ac < AC_NUM; ac++) {
601                         int delta = receivers[ac].received - receivers[ac].last_received;
602                         receivers[ac].last_received = receivers[ac].received;
603                         fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
604                 }
605                 fflush(stderr);
606                 if (seconds == opt_count_sec)
607                         stopper();
608         }
609
610         fprintf(stderr, "\nWaiting for threads to finish\n");
611         /* Wait for all threads to finish */
612         for (i=0; i < nr_streams + AC_NUM; i++) {
613                 sem_wait(&sem_thread_finished);
614         }
615
616         save_results(argc, argv, seconds);
617
618         return 0;
619 }