]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/wclient.c
Added test for measuring of CPU scheduler latency.
[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 #ifdef WITH_FWP
22 #include <fwp_proto.h>
23 #endif
24
25 #define MAX_STREAMS  10 
26 #define MIN_GRANULARITY 100
27
28 unsigned opt_packet_size = 800;
29 int opt_send_buf_size = -1;
30 unsigned opt_period_usec = 10*MSEC_TO_USEC;
31 unsigned opt_jitter = 0;
32 char    *opt_output = "delay_stats";
33 unsigned opt_count_sec = 0;
34 unsigned opt_def_bandwidth = 200;
35 unsigned opt_def_period_msec = 0;
36 int opt_granularity_usec = MIN_GRANULARITY;
37 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
38
39 bool some_queue_is_full = false;
40 struct timespec reset_timestamp;
41
42 /* Locked when some queue is full to prevent multiple resets of
43    statstics. */
44 pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
45
46 int ac_sockfd[AC_NUM];
47
48 struct receiver {
49         pthread_t thread;
50         unsigned received, last_received;
51 } receivers[AC_NUM];
52
53 FILE* logfd;
54 char* server_addr; 
55 char logfname[100];
56
57 /* maximal traffic delay in ms - 10 s*/
58 #define MAX_DELAY_US 10000000
59
60 struct delay_stat {
61         unsigned csc;           /* Client-server-client delay divided by 2 */
62         unsigned cs;            /* Client-server delay */
63         unsigned sc;            /* Server-client delay */
64 };
65
66 struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
67 pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
68
69 /*struct ac_stats[AC_NUM] {
70    unsigned long int min_trans_time;
71    unsigned long int sum_trans_time;
72    struct timespec   recv_timestamp;
73    struct timespec   send_timestamp; 
74 };*/
75
76 struct stream {
77         /* Input parameters */
78         enum ac ac;             /*  */
79         int bandwidth_bps;      /* bits per second */
80         int jitter;             /* percent */
81         /* Mulualy exclusive input parameters */
82         int packet_size;
83         long period_usec;       /* all time units are in microseconds */
84
85         /* Internal fields */
86 #ifndef WITH_FWP
87         struct sockaddr_in rem_addr;
88 #else
89         int vres_id;
90 #endif
91
92         /* Statistics */
93         pthread_mutex_t mutex;
94         unsigned long long sent, really_sent, received;
95 };
96
97 /*
98 struct send_endpoint sepoint[] = {
99         { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
100         { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps =  480*Kbit },
101         { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
102         { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
103 //      { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps =  675*Kbit },
104 };
105 */
106
107 struct stream streams[MAX_STREAMS];
108
109 unsigned int nr_streams = 0;
110
111 sem_t sem_thread_finished;
112
113 bool exit_flag = false;
114
115 #ifdef WITH_FWP
116 #define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
117 #define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
118 #define send_packet(s, b) send_packet_fwp(s, b)
119 #else
120 #define negotiate_contract_for_stream(s) 0
121 #define create_stream_endpoint(s) create_stream_endpoint_native(s)
122 #define send_packet(s, b) send_packet_native(s, b)
123 #endif
124
125 void stopper()
126 {
127         int i;
128         exit_flag = true;
129
130         /* Interrupt all receivers */
131         for (i=0; i < AC_NUM; i++) {
132                 pthread_kill(receivers[i].thread, SIGUSR1);
133         }
134 }
135
136 void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
137 {
138         char buf[3][12];
139         char real[100];
140
141         if (useconds) {
142                 snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
143                          bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
144                          stream->sent, stream->sent*SEC_TO_USEC/useconds,
145                          stream->received, stream->received*SEC_TO_USEC/useconds);
146         } else {
147                 real[0]=0;
148         }
149         
150         snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
151                  stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
152                  stream->packet_size, usec_to_text(buf[1], stream->period_usec),
153                  usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
154                  (int)(SEC_TO_USEC/stream->period_usec), real);
155 }
156
157 void save_results(int argc, char *argv[], int useconds)
158 {
159         int ac, i, maxi;
160         const int mini = 3000/opt_granularity_usec;
161         bool allzeros;
162         unsigned send_count[AC_NUM];
163
164         fprintf(stderr, "Writing data to %s... ", logfname);
165         fflush(stderr);
166
167         fprintf(logfd, "# Invoked as: ");
168         for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
169         fprintf(logfd, "\n");
170
171         if (useconds/SEC_TO_USEC != opt_count_sec) {
172                 char buf[20];
173                 usec_to_text(buf, useconds);
174                 fprintf(logfd, "# Data gathered for %s.\n", buf);
175         }
176                 
177         for (i = 0; i < nr_streams; i++) {
178                 char stream_desc[200];
179                 stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
180                 fprintf(logfd, "# Stream %s\n", stream_desc);
181         }
182
183         /* Find maximal delay */
184         allzeros = true;
185         for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
186                 for (ac = 0; ac < AC_NUM; ac++) {
187                         if ((delay_stats[ac][maxi].csc != 0) ||
188                             (delay_stats[ac][maxi].cs != 0) ||
189                             (delay_stats[ac][maxi].sc != 0))
190                                 allzeros = false;
191                 }
192                 if (!allzeros) break;
193         }
194         maxi++;
195         if (maxi < mini) maxi = mini;
196
197         /* Calculate total number of sent packets per AC */
198         memset(send_count, 0, sizeof(send_count));
199         for (i = 0; i < nr_streams; i++) {
200                 ac = streams[i].ac;
201                 send_count[ac] += streams[i].sent;
202         }
203
204 #if 0
205         /* Write pdf */
206         for ( i = 0 ; i < maxi; i++) {
207                 fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
208                 for (ac = 0; ac < AC_NUM; ac++) { 
209                         if (sum[ac])
210                                 val = (double)delay_stats[ac][i]*100.0 / sum[ac];
211                         else val = -1; /* Don't display this ac */
212                         fprintf(logfd," %lf", val);
213                 }
214         }
215         
216         fprintf(logfd,"\n\n");
217 #endif
218         
219         fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
220
221         /* Write PDF */
222         for (ac = 0; ac < AC_NUM; ac++) {
223                 struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
224
225                 fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
226
227                 if (send_count[ac] != 0) {
228                         i=0;
229                         while ((delay_stats[ac][i].csc == 0) &&
230                                (delay_stats[ac][i].cs == 0) &&
231                                (delay_stats[ac][i].sc == 0)) i++;
232                 
233                         for (i++; i < maxi+1; i++) {
234                                 if (memcmp(&last, &integral, sizeof(last))) {
235                                         fprintf(logfd,"%f %f %f %f\n", i*opt_granularity_usec/1000.0,
236                                                 (double)integral.csc*100.0 / send_count[ac],
237                                                 (double)integral.cs *100.0 / send_count[ac],
238                                                 (double)integral.sc *100.0 / send_count[ac]
239                                                 );
240                                         last = integral;
241                                 }
242                                 if (i>0) {
243                                         integral.csc += delay_stats[ac][i-1].csc;
244                                         integral.sc  += delay_stats[ac][i-1].sc;
245                                         integral.cs  += delay_stats[ac][i-1].cs;
246                                 }
247                         }
248                 }
249                 fprintf(logfd,"\n\n");
250         }
251         
252         fprintf(stderr, "finished.\n");
253         fclose(logfd);
254
255         exit(0);
256 }
257
258 int create_ac_socket(unsigned int ac) 
259 {
260         int sockfd;
261         unsigned int yes=1, tos;
262
263
264         if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
265         {
266                 perror("Unable to open socket");
267                 return -1;
268         }
269         if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
270                 perror("set non-blocking socket");
271                 return -1;
272         }
273         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
274                 perror("Unable to set socket");
275                 return -1;
276         }
277
278         if (opt_send_buf_size >= 0) {
279                 if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
280                         perror("Unable to set socket buffer size");
281                         return -1;
282                 }
283         }
284
285         
286         //tos = ((AC_NUM - ac) *2 - 1)*32;
287         tos = ac_to_tos[ac];
288         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
289                 perror("Unable to set TOS");
290                 close(sockfd);
291                 return -1;
292         }
293
294         return sockfd;
295 }
296
297 void empty_handler()
298 {
299 }
300
301 void reset_statistics()
302 {
303         int i;
304         for (i = 0; i < nr_streams; i++) {
305                 pthread_mutex_lock(&streams[i].mutex);
306                 streams[i].sent = 0;
307                 streams[i].really_sent = 0;
308                 streams[i].received = 0;
309                 pthread_mutex_unlock(&streams[i].mutex);
310         }
311         pthread_mutex_lock(&delay_stats_mutex);
312         clock_gettime(CLOCK_REALTIME, &reset_timestamp);
313         memset(delay_stats, 0, sizeof(delay_stats));
314         pthread_mutex_unlock(&delay_stats_mutex);
315 }
316
317 void* receiver(void* queue)
318 {
319         struct msg_t    msg;
320         struct  sockaddr_in rem_addr;
321         int     mlen, ret;
322         unsigned int ac, rem_addr_length; 
323         long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
324         long long int min_trans_time;
325         struct timespec send_timestamp, server_timestamp, recv_timestamp;
326         fd_set fdset;
327         
328         min_trans_time = ~0;
329         
330         block_signals();
331         set_rt_prio(99);
332
333         ac = (int)queue;
334         rem_addr_length = sizeof(rem_addr);
335         FD_ZERO(&fdset);
336         while (!exit_flag) {
337                 FD_SET(ac_sockfd[ac], &fdset);
338                 ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
339                 if (ret < 0) {
340                         if (errno == EINTR) continue;
341                         perror("receiver select");
342                         goto out;
343                 }
344                 mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,
345                                 (struct sockaddr*)&rem_addr, &rem_addr_length);
346                 if (mlen < 0) {
347                         perror("Chyba pri prijimani pozadavku");
348                         goto out;
349                 }       
350                 clock_gettime(CLOCK_REALTIME,&recv_timestamp);
351                 send_timestamp = msg.send_timestamp;
352                 server_timestamp = msg.sendback_timestamp;
353
354                 /* Check whether this message was sent after reset_statistics() */
355                 
356                 if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
357                         continue; /* If so, don't count it */
358                 }
359
360                 trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
361                 client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
362                 server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
363
364                 pthread_mutex_lock(&delay_stats_mutex);
365                 if (trans_time_usec < MAX_DELAY_US) {
366                         delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
367                 }
368                 if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
369                         delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
370                 }
371                 if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
372                         delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
373                 }
374                 pthread_mutex_unlock(&delay_stats_mutex);
375                 
376                 receivers[ac].received++;
377                 
378                 pthread_mutex_lock(&streams[msg.stream].mutex);
379                 streams[msg.stream].received++;
380                 pthread_mutex_unlock(&streams[msg.stream].mutex);
381         
382                 /*if (trans_time_nsec < min_trans_time) 
383                         min_trans_time = trans_time_nsec;*/
384                 /*printf("seqn= %lu tos= %d start= %lu(s).%lu(ns)"\
385                          "stop= %lu(s).%lu(ns)\n trans_time = %lums\n",\
386                          msg.seqn, msg.tos, send_timestamp.tv_sec,\
387                          send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
388                          recv_timestamp.tv_nsec, trans_time_msec); */
389         }
390 out:
391         sem_post(&sem_thread_finished);
392         return NULL;
393 }
394
395 /** 
396  * Send a packet.
397  * 
398  * @return -1 in case of error, 1 in case of sucessfull send and 0
399  *         when all buffers are full.
400  */
401 #ifndef WITH_FWP
402 static inline int 
403 send_packet_native(struct stream* stream, union msg_buff* buff)
404 {
405         int ret = 1;
406         while (sendto(ac_sockfd[stream->ac], buff, stream->packet_size, 0,
407                       (struct sockaddr*)&stream->rem_addr, sizeof(stream->rem_addr)) < 0) {
408                 if (errno == EINTR) continue;
409                 if (errno == EAGAIN) {
410                         if (opt_wait_for_queue_is_full && 
411                             !some_queue_is_full && 
412                             /* We use mutex as atomic test and set */
413                             (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
414                                 some_queue_is_full = true;
415                                 reset_statistics();
416                         }
417                         ret = 0;
418                         break;
419                 } else {
420                         perror("Error while sending");
421                         ret = -1;
422                         break;
423                 }
424         }
425         return ret;
426 }
427 #else
428 static inline int 
429 send_packet_fwp(struct stream* stream, union msg_buff* buff)
430 {
431         int ret = 1;
432 /*      while (sendto(ac_sockfd[stream->ac], buff, stream->packet_size, 0, */
433 /*                    (struct sockaddr*)&stream->rem_addr, sizeof(stream->rem_addr)) < 0) { */
434 /*              if (errno == EINTR) continue; */
435 /*              if (errno == EAGAIN) { */
436 /*                      if (opt_wait_for_queue_is_full &&  */
437 /*                          !some_queue_is_full &&  */
438 /*                          /\* We use mutex as atomic test and set *\/ */
439 /*                          (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) { */
440 /*                              some_queue_is_full = true; */
441 /*                              reset_statistics(); */
442 /*                      } */
443 /*                      ret = 0; */
444 /*                      break; */
445 /*              } else { */
446 /*                      perror("Error while sending"); */
447 /*                      ret = -1; */
448 /*                      break; */
449 /*              } */
450 /*      } */
451         return ret;
452 }
453 #endif
454
455 static inline void
456 wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
457 {
458         struct timespec time_to_wait, current_time, period, interval;
459         unsigned period_usec = stream->period_usec;
460
461         /*           |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
462         /* |-------------|     nominal period*/
463         if (stream->jitter) {
464                 period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
465                                                + rand() % (2*period_usec*stream->jitter/100));
466         } else {
467                 period.tv_nsec = USEC_TO_NSEC*(period_usec);
468         }
469         period.tv_sec = 0;
470         
471         timespec_add(&time_to_wait, last_send_time, &period);
472         clock_gettime(CLOCK_REALTIME,&current_time);
473         timespec_sub(&interval,&time_to_wait,&current_time);
474         nanosleep(&interval,NULL);
475 }
476
477
478 void* sender(void* arg)
479 {
480         union msg_buff buff;
481         unsigned long int seqn;
482         struct stream* stream = (struct stream*) arg;
483         char stream_desc[100];
484         int ret;
485
486         stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
487         printf("%s\n", stream_desc);
488
489         if (stream->bandwidth_bps == 0)
490                 goto out;
491
492         seqn = 0;
493         
494         block_signals();
495         set_rt_prio(90-stream->ac);
496
497         while (!exit_flag) {
498
499 /*              buff.msg.seqn = seqn++; */
500 /*              buff.msg.tos = ac_to_tos[stream->ac]; */
501                 buff.msg.stream = stream-streams;
502                 
503                 clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
504
505                 ret = send_packet(stream, &buff);
506                 if (ret < 0) {
507                         goto out;
508                 }
509
510                 pthread_mutex_lock(&stream->mutex);
511                 stream->sent++;
512                 if (ret > 0)
513                         stream->really_sent++;
514                 pthread_mutex_unlock(&stream->mutex);
515
516 #ifdef DEBUG
517                 printf("%d", stream->ac);
518                 fflush(stdout);
519 #endif
520
521                 wait_for_next_send(stream, &buff.msg.send_timestamp);
522         }
523 out:
524         sem_post(&sem_thread_finished);
525         return NULL;
526 }
527
528 #ifdef WITH_FWP
529 static int negotiate_contract_for_stream_fwp(struct stream *stream)
530 {
531         struct fwp_contract contract;
532         int vres_id;
533         int ret;
534
535         contract.budget = stream->packet_size;
536         contract.period_usec = stream->period_usec;
537         ret = fwp_contract_negotiate(&contract, &vres_id);
538         if (ret == 0) {
539                 if (contract.status == FWP_CNT_NEGOTIATED) {
540                         stream->vres_id = vres_id;
541                 } else {
542                         stream->vres_id = -1;
543                 }
544         }
545         return ret;
546 }
547 #endif
548
549 #ifdef WITH_FWP
550 static void create_stream_endpoint_fwp(struct stream *stream)
551 {
552         /* TODO: How to create an enpoint? */
553 }
554 #else
555 static void create_stream_endpoint_native(struct stream *stream)
556 {
557         struct hostent* ph;
558
559         memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
560
561         stream->rem_addr.sin_family = AF_INET;
562         ph = gethostbyname(server_addr);
563         if (ph)
564                 stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
565         else {
566                 perror("Unknown server");
567                 exit(1);
568         }
569         stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
570 }
571 #endif
572
573 static inline void
574 calc_stream_params(struct stream *stream)
575 {
576         int packet_size;
577         unsigned period_usec;
578         int bandwidth;
579         int ret;
580
581         /* If some parameters are not set explicitely, use default values. */
582         if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
583         if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
584         if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
585
586         bandwidth = stream->bandwidth_bps;
587
588         /* Avoid arithmetic exception. Server thread will exit if
589            stream->bandwidth_bps == 0. */
590         if (bandwidth == 0) bandwidth = 1;
591
592         if (stream->packet_size) {
593                 packet_size = stream->packet_size;
594                 period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
595                 if (period_usec == 0) period_usec = 1;
596         } else if (stream->period_usec) {
597                 period_usec = stream->period_usec;
598                 packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
599         } else {
600                 char buf[200];
601                 stream_to_text(buf, sizeof(buf), stream, 0);
602                 fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
603                 exit(1);
604         }
605
606         if (packet_size < sizeof(struct msg_t)) {
607                 fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
608                 exit(1);
609         }
610
611         stream->packet_size = packet_size;
612         stream->period_usec = period_usec;
613         stream->jitter = opt_jitter;
614
615         ret = negotiate_contract_for_stream(stream);
616         if (ret == 0) {
617                 create_stream_endpoint(stream);
618         } else {
619                 char buf[200];
620                 stream_to_text(buf, sizeof(buf), stream, 0);
621                 fprintf(stderr, "Contract hasn't been accepted: %s\n", buf);
622                 stream->bandwidth_bps = 0;
623         }
624 }
625
626 /** 
627  * Parse -b parameter.
628  * 
629  * @param params String to parse
630  * 
631  * @return NULL in case of success, pointer to a problematic character
632  *         on error.
633  */
634 char* parse_bandwidths(char *params)
635 {
636         struct stream *sp = &streams[nr_streams];
637
638         while (*params && nr_streams < MAX_STREAMS) {
639                 char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
640                 int i;
641                 char *next_char;
642
643                 if (strlen(params) < 2)
644                         return params;
645                 for (i=0; i<AC_NUM; i++) {
646                         if (strncmp(params, ac_ids[i], 2) == 0) {
647                                 sp->ac = i;
648                                 params+=strlen(ac_ids[i]);
649                                 break;
650                         }
651                 }
652                 if (i==AC_NUM)
653                         return params;
654
655                 long bw;
656                 if (*params == ':') {
657                         params++;
658
659                         bw = strtol(params, &next_char, 10);
660                         if (next_char == params)
661                                 return params;
662                         params = next_char;
663                 } else
664                         bw = -1;
665                 
666                 sp->bandwidth_bps = bw*Kbit;
667
668                 long period = 0;
669                 long packet_size = 0;
670                 if (*params == '@') {
671                         params++;
672                         period = strtol(params, &next_char, 10);
673                         if (period == 0)
674                                 return params;
675                         params = next_char;
676                 }
677                 else {
678                         if (*params == '/') {
679                                 params++;
680                                 packet_size = strtol(params, &next_char, 10);
681                                 if (packet_size == 0)
682                                         return params;
683                                 params = next_char;
684                         } else {
685                                 packet_size = -1; 
686                                 period = -1;
687                         }
688                 }
689                 sp->period_usec = period*MSEC_TO_USEC;
690                 sp->packet_size = packet_size;
691
692                 
693
694                 if (*params != '\0' && *params != ',')
695                         return params;
696                 nr_streams++;
697                 sp++;
698                 if (*params == ',')
699                         params++;
700         }
701         return NULL;
702 }
703
704 int main(int argc, char *argv[])
705 {
706         int ac, i, rc, seconds;
707         pthread_attr_t attr;
708         pthread_t thread;
709         char opt;
710
711
712         while ((opt = getopt(argc, argv, "B:b:c:g:j:o:qQ:s:T:")) != -1) {
713                 switch (opt) {
714                         case 'B':
715                                 opt_def_bandwidth = atoi(optarg);
716                                 break;
717                         case 'b': {
718                                 char *error;
719                                 error = parse_bandwidths(optarg);
720                                 if (error != NULL) {
721                                         if (*error == '\0')
722                                                 fprintf(stderr, "Bandwidth parse error - string to short\n");
723                                         else
724                                                 fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
725                                         exit(1);
726                                 }
727                                 break;
728                         }
729                         case 'c':
730                                 opt_count_sec = atoi(optarg);
731                                 break;
732                         case 'g':
733                                 opt_granularity_usec = atoi(optarg);
734                                 if (opt_granularity_usec < MIN_GRANULARITY) {
735                                         fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
736                                         exit(1);
737                                 }
738                                 break;
739                         case 'j':
740                                 opt_jitter = atoi(optarg);
741                                 break;
742                         case 'o':
743                                 opt_output = optarg;
744                                 break;
745                         case 'Q':
746                                 opt_send_buf_size = atoi(optarg);
747                                 break;
748                         case 'q':
749                                 opt_wait_for_queue_is_full = true;
750                                 break;
751                         case 's':
752                                 opt_packet_size = atoi(optarg);
753                                 break;
754                         case 'T':
755                                 opt_def_period_msec = atoi(optarg);
756                                 break;
757                         default:
758                                 fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
759                                 fprintf(stderr, "Options:\n");
760                                 fprintf(stderr, "    -B  default bandwidth for -b option [kbit]\n");
761                                 fprintf(stderr, "    -b  bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
762                                 fprintf(stderr, "    -c  count (number of seconds to run)\n");
763                                 fprintf(stderr, "    -g  histogram granularity [usec]\n");
764                                 fprintf(stderr, "    -j  send jitter (0-100) [%%]\n");
765                                 fprintf(stderr, "    -o  output filename (.dat will be appended)\n");
766                                 fprintf(stderr, "    -q  gather statistics only after some queue becomes full\n");
767                                 fprintf(stderr, "    -Q  <bytes> set size for socket send buffers\n");
768                                 fprintf(stderr, "    -s  size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
769                                 fprintf(stderr, "    -T  default period for -b option [msec]\n");
770                                 exit(1);
771                 }
772         }
773         if (opt_packet_size && opt_def_period_msec) {
774                 fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
775                 exit(1);
776         }
777
778         if (optind < argc) {
779                 server_addr = argv[optind];
780         } else {
781                 fprintf(stderr, "Expected server address argument\n");
782                 exit(1);
783         }
784
785         if (nr_streams == 0)
786                 parse_bandwidths("BE");
787                 
788         pthread_attr_init(&attr);
789
790         snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
791
792         if ((logfd = fopen(logfname,"w+")) == NULL) {
793                 fprintf(stderr,"Can not open %s\n", logfname);
794                 exit(1);
795         }
796         if (signal(SIGTERM, stopper) == SIG_ERR) {
797                 perror("Error in signal registration");
798                 exit(1);
799         }
800                 
801         if (signal(SIGINT, stopper) == SIG_ERR) {
802                 perror("Signal handler registration error");
803                 exit(1);
804         }
805
806         struct sigaction sa;
807         sa.sa_handler = empty_handler;
808         sa.sa_flags = 0;        /* don't restart syscalls */
809
810         if (sigaction(SIGUSR1, &sa, NULL) < 0) {
811                 perror("sigaction error");
812                 exit(1);
813         }
814
815         sem_init(&sem_thread_finished, 0, 0);
816
817         reset_statistics();
818
819         /* create four receivers each per AC */
820         for (ac = AC_NUM - 1; ac >= 0; ac--) {
821                 ac_sockfd[ac] = create_ac_socket(ac);
822                 if (ac_sockfd[ac] < 0) {
823                         return 1;
824                 }
825                 rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
826                 if (rc) {
827                         fprintf(stderr, "Error while creating receiver %d\n",rc);
828                         return 1;
829                 }
830         }               
831                         
832         /* create sendpoints */
833         for (i = 0; i < nr_streams; i++) {
834                 struct stream *s = &streams[i];
835                 pthread_mutex_init(&s->mutex, NULL);
836                 calc_stream_params(s);
837                 rc = pthread_create(&thread, &attr, sender, (void*) s);
838                 if (rc) {
839                         fprintf(stderr, "Error while creating sender %d\n",rc);
840                         return 1;
841                 }
842         }
843         
844         seconds = 0;
845         while (!exit_flag) {
846                 sleep(1);
847                 seconds++;
848                 fprintf(stderr, "\r%3ds", seconds);
849                 for (ac = 0; ac < AC_NUM; ac++) {
850                         int delta = receivers[ac].received - receivers[ac].last_received;
851                         receivers[ac].last_received = receivers[ac].received;
852                         fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
853                 }
854                 fflush(stderr);
855                 if (seconds == opt_count_sec)
856                         stopper();
857         }
858
859         fprintf(stderr, "\nWaiting for threads to finish\n");
860         /* Wait for all threads to finish */
861         for (i=0; i < nr_streams + AC_NUM; i++) {
862                 sem_wait(&sem_thread_finished);
863         }
864         struct timespec end_timestamp, measure_length;
865         clock_gettime(CLOCK_REALTIME,&end_timestamp);
866         timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
867
868         save_results(argc, argv, timespec2usec(&measure_length));
869
870         return 0;
871 }