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