]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wclient.c
Fixed compitation of sender period
[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 MSEC (1000)
22
23 #define PARAM_SERVERADDR 1
24 #define MAX_SENDENDPOINTS  10
25
26 unsigned opt_packet_size = 800;
27 unsigned opt_period_usec = 10*MSEC;
28 unsigned opt_jitter = 0;
29 char    *opt_output = "delay_stats";
30 unsigned opt_count_sec = 0;
31
32 int ac_sockfd[AC_QUEUES];
33
34 struct receiver {
35         pthread_t thread;
36         unsigned received, last_received;
37 } receivers[AC_QUEUES];
38
39 FILE* logfd;
40 char* server_addr; 
41 char logfname[100];
42
43 struct msg_t {
44         unsigned int tos;
45         struct timespec send_timestamp;
46         unsigned long int seqn;
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 #define GRANULARITY 100
57
58 unsigned delay_stats[AC_QUEUES][MAX_DELAY_US/GRANULARITY];
59
60 /*struct ac_stats[AC_QUEUES] {
61    unsigned long int min_trans_time;
62    unsigned long int sum_trans_time;
63    struct timespec   recv_timestamp;
64    struct timespec   send_timestamp; 
65 };*/
66
67 struct send_endpoint {
68         int ac;
69         long period_usec;       /* all time units are in microseconds */
70         int bandwidth_bps;      /* bits per second */
71 };
72
73 /*
74 struct send_endpoint sepoint[] = {
75         { .ac = AC_VO, .period_usec=200*MSEC, .bandwidth_bps = 34*Kbit },
76         { .ac = AC_VI, .period_usec=25*MSEC, .bandwidth_bps =  480*Kbit },
77         { .ac = AC_BE, .period_usec=40*MSEC, .bandwidth_bps =  300*Kbit },
78         { .ac = AC_BK, .period_usec=40*MSEC, .bandwidth_bps =  300*Kbit },
79 //      { .ac = AC_VI, .period_usec=17*MSEC, .bandwidth_bps =  675*Kbit },
80 };
81 */
82
83 struct send_endpoint sepoint[] = {
84         { .ac = AC_VO, .period_usec=10*MSEC, .bandwidth_bps =  300*Kbit },
85         { .ac = AC_VI, .period_usec=10*MSEC, .bandwidth_bps =  300*Kbit },
86         { .ac = AC_BE, .period_usec=10*MSEC, .bandwidth_bps =  300*Kbit },
87         { .ac = AC_BK, .period_usec=10*MSEC, .bandwidth_bps =  300*Kbit },
88 };
89
90 unsigned int nr_sepoints = sizeof(sepoint)/sizeof(*sepoint);
91
92 sem_t sem_thread_finished;
93
94 bool exit_flag = false;
95
96 void stopper()
97 {
98         int i;
99         exit_flag = true;
100
101         /* Interrupt all receivers */
102         for (i=0; i<AC_QUEUES; i++) {
103                 pthread_kill(receivers[i].thread, SIGUSR1);
104         }
105 }
106
107 void save_results()
108 {
109         int ac, i, maxi;
110         bool allzeros;
111         unsigned sum[AC_QUEUES];
112
113         fprintf(stderr, "\nWriting data to %s...\n", logfname);
114
115         allzeros = true;
116         for (maxi = MAX_DELAY_US/GRANULARITY - 1; maxi >= 0; maxi--) {
117                 for (ac = 0; ac < AC_QUEUES; ac++) {
118                         if (delay_stats[ac][maxi] != 0) allzeros = false;
119                 }
120                 if (!allzeros) break;
121         }
122         if (maxi < 3000/GRANULARITY) maxi = 3000/GRANULARITY;
123
124         for (ac = 0; ac < AC_QUEUES; ac++) { 
125                 sum[ac] = 0;
126                 for ( i = 0 ; i < maxi; i++) 
127                         sum[ac]+=delay_stats[ac][i];
128                 if (sum[ac] == 0)
129                         fprintf(stderr, "No response in AC %d\n", ac);
130         }
131
132         for ( i = 0 ; i < maxi; i++) {
133                 fprintf(logfd,"\n%f", i*GRANULARITY/1000.0);
134                 for (ac = 0; ac < AC_QUEUES; ac++) { 
135                         double val;
136                         val = (double)delay_stats[ac][i]*100.0 / sum[ac];
137                         fprintf(logfd," %lf", val);
138                 }
139         }
140         
141         fprintf(stderr, "Finished.\n");
142         fclose(logfd);
143
144         exit(0);
145 }
146
147 static inline 
148 void timespec_add (struct timespec *sum, const struct timespec *left,
149               const struct timespec *right)
150 {
151         sum->tv_sec = left->tv_sec + right->tv_sec;
152         sum->tv_nsec = left->tv_nsec + right->tv_nsec;
153
154         if (sum->tv_nsec >= 1000000000){
155                 ++sum->tv_sec;
156                 sum->tv_nsec -= 1000000000;
157         }
158 }
159
160 static inline 
161 void timespec_sub (struct timespec *diff, const struct timespec *left,
162               const struct timespec *right)
163 {
164         diff->tv_sec = left->tv_sec - right->tv_sec;
165         diff->tv_nsec = left->tv_nsec - right->tv_nsec;
166
167         if (diff->tv_nsec < 0){
168                   --diff->tv_sec;
169                   diff->tv_nsec += 1000000000;
170         }
171 }
172
173 int create_ac_socket(unsigned int ac) 
174 {
175         int sockfd;
176         unsigned int yes=1, tos;
177
178
179         if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
180         {
181                 perror("Unable to open socket");
182                 return -1;
183         }
184         
185         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
186                 perror("Unable to set socket");
187                 return -1;
188         }
189
190         
191         //tos = ((AC_QUEUES - ac) *2 - 1)*32;
192         tos = ac_to_tos[ac];
193         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
194                 perror("Unable to set TOS");
195                 close(sockfd);
196                 return -1;
197         }
198
199         return sockfd;
200 }
201
202 void empty_handler()
203 {
204 }
205
206 void* receiver(void* queue)
207 {
208         struct msg_t    msg;
209         struct  sockaddr_in rem_addr;
210         int     mlen;
211         unsigned int ac, rem_addr_length; 
212         unsigned long int trans_time_usec;
213         unsigned long int min_trans_time;
214         struct timespec   send_timestamp,recv_timestamp, trans_time; 
215         
216         min_trans_time = ~0;
217         
218         block_signals();
219         set_rt_prio(99);
220
221         ac = (int)queue;
222         rem_addr_length = sizeof(rem_addr);
223         while (!exit_flag) {
224                 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,    \
225                                 (struct sockaddr*)&rem_addr, &rem_addr_length);
226                 if (mlen < 0) {
227                         if (errno == EINTR) continue;
228                         perror("Chyba pri prijimani pozadavku");
229                         goto out;
230                 }       
231                 clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
232                 send_timestamp = msg.send_timestamp;
233                 
234                 timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
235                 trans_time_usec = (trans_time.tv_sec * 1000000 + \
236                                          trans_time.tv_nsec / 1000) /2;
237           
238                 if (trans_time_usec < MAX_DELAY_US)
239                         delay_stats[ac][trans_time_usec/GRANULARITY]++;
240
241                 receivers[ac].received++;
242         
243                 /*if (trans_time_nsec < min_trans_time) 
244                         min_trans_time = trans_time_nsec;*/
245                 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
246                          "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
247                          msg.seqn, msg.tos, send_timestamp.tv_sec,\
248                          send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
249                          recv_timestamp.tv_nsec, trans_time_msec); */
250         }
251 out:
252         sem_post(&sem_thread_finished);
253         return NULL;
254 }
255
256 void* sender(void* endpoint)
257 {
258         struct sockaddr_in rem_addr;
259         union msg_buff buff;
260         unsigned long int seqn;
261         struct timespec time_to_wait, current_time, period, interval;
262         struct hostent* ph;
263         struct send_endpoint* spoint = (struct send_endpoint*) endpoint;
264         char buf1[12], buf2[12], buf3[12];
265         int ac = spoint->ac;
266         int packet_size;
267         unsigned period_usec;
268         char stream_desc[100];
269
270         if (opt_packet_size) {
271                 packet_size = opt_packet_size;
272                 period_usec = 1000000LL*packet_size*8/spoint->bandwidth_bps;
273         } else {
274                 period_usec = spoint->period_usec;
275                 packet_size = (long long)spoint->bandwidth_bps/8 * period_usec/1000000;
276         }
277         snprintf(stream_desc, sizeof(stream_desc), "%d: %s %s (%d bytes per %s +-%s, %d packets/s)\n",
278                  spoint-sepoint, ac_to_text[ac], bandwidth_to_text(buf1, spoint->bandwidth_bps),
279                  packet_size, usec_to_text(buf2, period_usec),
280                  usec_to_text(buf3, opt_jitter*period_usec/100), 1000000/period_usec);
281         printf("%s", stream_desc);
282         fprintf(logfd, "# Stream %s", stream_desc);
283
284         if (packet_size < sizeof(struct msg_t)) {
285                 fprintf(stderr, "Pakcet size too small (min %d)\n", sizeof(struct msg_t));
286                 exit(1);
287         }
288
289         
290         memset(&rem_addr,0, sizeof(rem_addr));
291                 
292         rem_addr.sin_family = AF_INET;
293         ph = gethostbyname(server_addr);
294         if (ph) 
295                 rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
296         else {
297                 perror("Unknown server");
298                 exit(1);
299         }
300         rem_addr.sin_port = htons(BASE_PORT + ac);
301         seqn = 0;
302         
303         block_signals();
304         set_rt_prio(90-ac);
305
306         while (!exit_flag) {
307
308                 buff.msg.seqn = seqn;
309                 buff.msg.tos = ac_to_tos[ac];
310                 
311                 clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
312                 
313                 while (sendto(ac_sockfd[ac], &buff, packet_size, 0,\
314                                 (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
315                                 if (errno == EINTR) continue;
316                                 perror("Error while sending");
317                                 goto out;
318                 }
319
320 #ifdef DEBUG
321                 printf("%d", ac);
322                 fflush(stdout);
323 #endif
324                 seqn++;
325
326                 /*           |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
327                 /* |-------------|     nominal period*/
328                 if (opt_jitter) {
329                         period.tv_nsec = 1000LL*(period_usec*(100-opt_jitter)/100
330                                                  + rand() % (2*period_usec*opt_jitter/100));
331                 } else {
332                         period.tv_nsec = 1000LL*(period_usec);
333                 }
334                 period.tv_sec = 0;
335
336                 timespec_add(&time_to_wait,&buff.msg.send_timestamp,&period);
337                 clock_gettime(CLOCK_MONOTONIC,&current_time);
338                 timespec_sub(&interval,&time_to_wait,&current_time);
339                 nanosleep(&interval,NULL);
340         }
341 out:
342         sem_post(&sem_thread_finished);
343         return NULL;
344 }
345
346 int main(int argc, char *argv[])
347 {
348         int ac, i, rc;
349         pthread_attr_t attr;
350         pthread_t thread;
351         char opt;
352
353
354         while ((opt = getopt(argc, argv, "c:j:o:s:")) != -1) {
355                 switch (opt) {
356                         case 'c':
357                                 opt_count_sec = atoi(optarg);
358                                 break;
359                         case 'j':
360                                 opt_jitter = atoi(optarg);
361                                 break;
362                         case 'o':
363                                 opt_output = optarg;
364                                 break;
365                         case 's':
366                                 opt_packet_size = atoi(optarg);
367                                 break;
368                         default:
369                                 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
370                                 fprintf(stderr, "Options:\n");
371                                 fprintf(stderr, "    -c  count (number of seconds to run)");
372                                 fprintf(stderr, "    -j  send jitter (0-100) [%%]\n");
373                                 fprintf(stderr, "    -o  output filename (.dat will be appended)");
374                                 fprintf(stderr, "    -s  size of data payload in packets [bytes]\n");
375                                 exit(1);
376                 }
377         }
378         if (optind < argc) {
379                 server_addr = argv[optind];
380         } else {
381                 fprintf(stderr, "Expected server address argument\n");
382                 exit(1);
383         }
384
385
386                 
387         memset(delay_stats,0, sizeof(delay_stats));     
388         pthread_attr_init(&attr);
389
390         snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
391
392         if ((logfd = fopen(logfname,"w+")) == NULL) {
393                 fprintf(stderr,"Can not open %s\n", logfname);
394                 exit(1);
395         }
396         fprintf(logfd, "# Invoked as: ");
397         for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
398         fprintf(logfd, "\n");
399                 
400         if (signal(SIGTERM, stopper) == SIG_ERR) {
401                 perror("Error in signal registration");
402                 exit(1);
403         }
404                 
405         if (signal(SIGINT, stopper) == SIG_ERR) {
406                 perror("Signal handler registration error");
407                 exit(1);
408         }
409
410         struct sigaction sa;
411         sa.sa_handler = empty_handler;
412         sa.sa_flags = 0;        /* don't restart syscalls */
413
414         if (sigaction(SIGUSR1, &sa, NULL) < 0) {
415                 perror("sigaction error");
416                 exit(1);
417         }
418
419         sem_init(&sem_thread_finished, 0, 0);
420
421         /* create four receivers each per AC */
422         for (ac = AC_QUEUES - 1; ac >= 0; ac--) {
423                 ac_sockfd[ac] = create_ac_socket(ac);
424                 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
425                 if (rc) {
426                         fprintf(stderr, "Error while creating receiver %d\n",rc);
427                         return 1;
428                 }
429         }               
430                         
431         /* create sendpoints */
432         for (i = 0; i < nr_sepoints; i++) {
433                 rc = pthread_create(&thread, &attr, sender, (void*) &sepoint[i]);
434                 if (rc) {
435                         fprintf(stderr, "Error while creating sender %d\n",rc);
436                         return 1;
437                 }
438         }
439         
440         i = 0;
441         while (!exit_flag) {
442                 fprintf(stderr, "\r");
443                 for (ac = 0; ac<AC_QUEUES; ac++) {
444                         int delta = receivers[ac].received - receivers[ac].last_received;
445                         receivers[ac].last_received = receivers[ac].received;
446                         fprintf(stderr, "%s: %5d (+%4d)  ", ac_to_text[ac], receivers[ac].received, delta);
447                 }
448                 fflush(stderr);
449                 sleep(1);
450                 i++;
451                 if (i == opt_count_sec) exit_flag = 1;
452         }
453
454         fprintf(stderr, "\nWaiting for threads to finish\n");
455         /* Wait for all threads to finish */
456         for (i=0; i < nr_sepoints + AC_QUEUES; i++) {
457                 sem_wait(&sem_thread_finished);
458         }
459         printf("\n");
460
461         save_results();
462
463         return 0;
464 }