#include "common.h"
#include <semaphore.h>
-#define MSEC (1000)
-
-#define PARAM_SERVERADDR 1
-#define MAX_SENDENDPOINTS 10
+#define MAX_STREAMS 10
+#define MIN_GRANULARITY 100
unsigned opt_packet_size = 800;
-unsigned opt_period_usec = 10*MSEC;
+unsigned opt_period_usec = 10*MSEC_TO_USEC;
unsigned opt_jitter = 0;
char *opt_output = "delay_stats";
unsigned opt_count_sec = 0;
+unsigned opt_def_bandwidth = 200;
+unsigned opt_def_period_msec = 10;
+int opt_granularity_usec = MIN_GRANULARITY;
-int ac_sockfd[AC_QUEUES];
+int ac_sockfd[AC_NUM];
struct receiver {
pthread_t thread;
unsigned received, last_received;
-} receivers[AC_QUEUES];
+} receivers[AC_NUM];
FILE* logfd;
char* server_addr;
char logfname[100];
-struct msg_t {
- unsigned int tos;
- struct timespec send_timestamp;
- unsigned long int seqn;
-};
-
-union msg_buff {
- struct msg_t msg;
- char nonsense[BUFFSIZE];
-};
-
/* maximal traffic delay in ms - 10 s*/
#define MAX_DELAY_US 10000000
-#define GRANULARITY 100
-unsigned delay_stats[AC_QUEUES][MAX_DELAY_US/GRANULARITY];
+unsigned delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
-/*struct ac_stats[AC_QUEUES] {
+/*struct ac_stats[AC_NUM] {
unsigned long int min_trans_time;
unsigned long int sum_trans_time;
struct timespec recv_timestamp;
struct timespec send_timestamp;
};*/
-struct send_endpoint {
- int ac;
- long period_usec; /* all time units are in microseconds */
+struct stream {
+ /* Input parameters */
+ enum ac ac; /* */
int bandwidth_bps; /* bits per second */
+ int jitter; /* percent */
+ /* Mulualy exclusive input parameters */
+ int packet_size;
+ long period_usec; /* all time units are in microseconds */
+
+ /* Statistics */
+ unsigned long long sent, received;
};
/*
struct send_endpoint sepoint[] = {
- { .ac = AC_VO, .period_usec=200*MSEC, .bandwidth_bps = 34*Kbit },
- { .ac = AC_VI, .period_usec=25*MSEC, .bandwidth_bps = 480*Kbit },
- { .ac = AC_BE, .period_usec=40*MSEC, .bandwidth_bps = 300*Kbit },
- { .ac = AC_BK, .period_usec=40*MSEC, .bandwidth_bps = 300*Kbit },
-// { .ac = AC_VI, .period_usec=17*MSEC, .bandwidth_bps = 675*Kbit },
+ { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
+ { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps = 480*Kbit },
+ { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
+ { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps = 300*Kbit },
+// { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps = 675*Kbit },
};
*/
-struct send_endpoint sepoint[] = {
- { .ac = AC_VO, .period_usec=10*MSEC, .bandwidth_bps = 300*Kbit },
- { .ac = AC_VI, .period_usec=10*MSEC, .bandwidth_bps = 300*Kbit },
- { .ac = AC_BE, .period_usec=10*MSEC, .bandwidth_bps = 300*Kbit },
- { .ac = AC_BK, .period_usec=10*MSEC, .bandwidth_bps = 300*Kbit },
-};
+struct stream streams[MAX_STREAMS];
-unsigned int nr_sepoints = sizeof(sepoint)/sizeof(*sepoint);
+unsigned int nr_streams = 0;
sem_t sem_thread_finished;
exit_flag = true;
/* Interrupt all receivers */
- for (i=0; i<AC_QUEUES; i++) {
+ for (i=0; i < AC_NUM; i++) {
pthread_kill(receivers[i].thread, SIGUSR1);
}
}
-void save_results()
+void stream_to_text(char *stream_desc, size_t n, struct stream *stream, int seconds)
+{
+ char buf[3][12];
+ char real[100];
+
+ if (seconds) {
+ snprintf(real, sizeof(real), "; real: sent %lld (%lld/s), received %lld (%lld/s)",
+ stream->sent, stream->sent/seconds,
+ stream->received, stream->received/seconds);
+ }
+
+ snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
+ stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
+ stream->packet_size, usec_to_text(buf[1], stream->period_usec),
+ usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
+ (int)(SEC_TO_USEC/stream->period_usec), real);
+}
+
+void save_results(int argc, char *argv[], int seconds)
{
int ac, i, maxi;
+ const int mini = 3000/opt_granularity_usec;
bool allzeros;
- unsigned sum[AC_QUEUES];
+ unsigned sum[AC_NUM];
+ double val;
+
+ fprintf(stderr, "Writing data to %s... ", logfname);
+ fflush(stderr);
- fprintf(stderr, "\nWriting data to %s...\n", logfname);
+ fprintf(logfd, "# Invoked as: ");
+ for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
+ fprintf(logfd, "\n");
+
+ for (i = 0; i < nr_streams; i++) {
+ char stream_desc[120];
+ stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], seconds);
+ fprintf(logfd, "# Stream %s\n", stream_desc);
+ }
+ /* Find maximal delay */
allzeros = true;
- for (maxi = MAX_DELAY_US/GRANULARITY - 1; maxi >= 0; maxi--) {
- for (ac = 0; ac < AC_QUEUES; ac++) {
+ for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
+ for (ac = 0; ac < AC_NUM; ac++) {
if (delay_stats[ac][maxi] != 0) allzeros = false;
}
if (!allzeros) break;
}
- if (maxi < 3000/GRANULARITY) maxi = 3000/GRANULARITY;
-
- for (ac = 0; ac < AC_QUEUES; ac++) {
- sum[ac] = 0;
- for ( i = 0 ; i < maxi; i++)
- sum[ac]+=delay_stats[ac][i];
- if (sum[ac] == 0)
- fprintf(stderr, "No response in AC %d\n", ac);
+ maxi++;
+ if (maxi < mini) maxi = mini;
+
+ /* Calculate total number of sent packets per AC */
+ for (ac = 0; ac < AC_NUM; ac++) sum[ac] = 0;
+ for (i = 0; i < nr_streams; i++) {
+ ac = streams[i].ac;
+ sum[ac] += streams[i].sent;
}
+#if 0
+ /* Write pdf */
for ( i = 0 ; i < maxi; i++) {
- fprintf(logfd,"\n%f", i*GRANULARITY/1000.0);
- for (ac = 0; ac < AC_QUEUES; ac++) {
- double val;
- val = (double)delay_stats[ac][i]*100.0 / sum[ac];
+ fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
+ for (ac = 0; ac < AC_NUM; ac++) {
+ if (sum[ac])
+ val = (double)delay_stats[ac][i]*100.0 / sum[ac];
+ else val = -1; /* Don't display this ac */
fprintf(logfd," %lf", val);
}
}
- fprintf(stderr, "Finished.\n");
+ fprintf(logfd,"\n\n");
+#endif
+
+ /* Write PDF */
+ for (ac = 0; ac < AC_NUM; ac++) {
+ unsigned long long integral = 0, last = -1;
+
+ fprintf(logfd,"%f %f\n", 0.0, 0.0);
+
+ if (sum[ac] != 0) {
+ i=0;
+ while (delay_stats[ac][i] == 0) i++;
+
+ for (i++; i < maxi+1; i++) {
+ if (last != integral) {
+ val = (double)integral*100.0 / sum[ac];
+ fprintf(logfd,"%f %f\n", i*opt_granularity_usec/1000.0, val);
+ last = integral;
+ }
+ if (i>0)
+ integral += delay_stats[ac][i-1];
+ }
+ }
+ fprintf(logfd,"\n\n");
+ }
+
+ fprintf(stderr, "finished.\n");
fclose(logfd);
exit(0);
perror("Unable to open socket");
return -1;
}
-
+ if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
+ perror("set non-blocking socket");
+ return -1;
+ }
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
perror("Unable to set socket");
return -1;
}
- //tos = ((AC_QUEUES - ac) *2 - 1)*32;
+ //tos = ((AC_NUM - ac) *2 - 1)*32;
tos = ac_to_tos[ac];
if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
perror("Unable to set TOS");
{
struct msg_t msg;
struct sockaddr_in rem_addr;
- int mlen;
+ int mlen, ret;
unsigned int ac, rem_addr_length;
unsigned long int trans_time_usec;
unsigned long int min_trans_time;
- struct timespec send_timestamp,recv_timestamp, trans_time;
+ struct timespec send_timestamp,recv_timestamp, trans_time;
+ fd_set fdset;
min_trans_time = ~0;
ac = (int)queue;
rem_addr_length = sizeof(rem_addr);
+ FD_ZERO(&fdset);
while (!exit_flag) {
+ FD_SET(ac_sockfd[ac], &fdset);
+ ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
+ if (ret < 0) {
+ if (errno == EINTR) continue;
+ perror("receiver select");
+ goto out;
+ }
mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0, \
(struct sockaddr*)&rem_addr, &rem_addr_length);
if (mlen < 0) {
- if (errno == EINTR) continue;
perror("Chyba pri prijimani pozadavku");
goto out;
}
send_timestamp = msg.send_timestamp;
timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
- trans_time_usec = (trans_time.tv_sec * 1000000 + \
- trans_time.tv_nsec / 1000) /2;
+ trans_time_usec = (trans_time.tv_sec * SEC_TO_USEC + \
+ trans_time.tv_nsec / USEC_TO_NSEC) /2;
if (trans_time_usec < MAX_DELAY_US)
- delay_stats[ac][trans_time_usec/GRANULARITY]++;
+ delay_stats[ac][trans_time_usec/opt_granularity_usec]++;
receivers[ac].received++;
+ streams[msg.stream].received++;
/*if (trans_time_nsec < min_trans_time)
min_trans_time = trans_time_nsec;*/
return NULL;
}
-void* sender(void* endpoint)
+void* sender(void* arg)
{
struct sockaddr_in rem_addr;
union msg_buff buff;
unsigned long int seqn;
struct timespec time_to_wait, current_time, period, interval;
struct hostent* ph;
- struct send_endpoint* spoint = (struct send_endpoint*) endpoint;
- char buf1[12], buf2[12], buf3[12];
- int ac = spoint->ac;
+ struct stream* stream = (struct stream*) arg;
int packet_size;
unsigned period_usec;
char stream_desc[100];
+ if (stream->bandwidth_bps == 0)
+ goto out;
+
+ set_rt_prio(90-stream->ac);
+
if (opt_packet_size) {
packet_size = opt_packet_size;
- period_usec = 1000000LL*packet_size*8/spoint->bandwidth_bps;
+ period_usec = SEC_TO_USEC*packet_size*8/stream->bandwidth_bps;
} else {
- period_usec = spoint->period_usec;
- packet_size = (long long)spoint->bandwidth_bps/8 * period_usec/1000000;
+ period_usec = stream->period_usec;
+ packet_size = (long long)stream->bandwidth_bps/8 * period_usec/SEC_TO_USEC;
}
- snprintf(stream_desc, sizeof(stream_desc), "%d: %s %s (%d bytes per %s +-%s, %d packets/s)\n",
- spoint-sepoint, ac_to_text[ac], bandwidth_to_text(buf1, spoint->bandwidth_bps),
- packet_size, usec_to_text(buf2, period_usec),
- usec_to_text(buf3, opt_jitter*period_usec/100), 1000000/period_usec);
- printf("%s", stream_desc);
- fprintf(logfd, "# Stream %s", stream_desc);
+ stream->packet_size = packet_size;
+ stream->period_usec = period_usec;
+ stream->jitter = opt_jitter;
+
+
+ stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
+
+ printf("%s\n", stream_desc);
if (packet_size < sizeof(struct msg_t)) {
- fprintf(stderr, "Pakcet size too small (min %d)\n", sizeof(struct msg_t));
+ fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
exit(1);
}
perror("Unknown server");
exit(1);
}
- rem_addr.sin_port = htons(BASE_PORT + ac);
+ rem_addr.sin_port = htons(BASE_PORT + stream->ac);
seqn = 0;
block_signals();
- set_rt_prio(90-ac);
while (!exit_flag) {
buff.msg.seqn = seqn;
- buff.msg.tos = ac_to_tos[ac];
+ buff.msg.tos = ac_to_tos[stream->ac];
+ buff.msg.stream = stream-streams;
clock_gettime(CLOCK_MONOTONIC,&buff.msg.send_timestamp);
-
- while (sendto(ac_sockfd[ac], &buff, packet_size, 0,\
+
+ while (sendto(ac_sockfd[stream->ac], &buff, packet_size, 0,\
(struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
if (errno == EINTR) continue;
- perror("Error while sending");
- goto out;
+ if (errno == EAGAIN) {
+ break;
+ } else {
+ perror("Error while sending");
+ goto out;
+ }
}
#ifdef DEBUG
fflush(stdout);
#endif
seqn++;
+ stream->sent++;
/* |~~~+~~~| jitter interval (width = 2*opt_jitter percentage from period)*/
/* |-------------| nominal period*/
if (opt_jitter) {
- period.tv_nsec = 1000LL*(period_usec*(100-opt_jitter)/100
+ period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-opt_jitter)/100
+ rand() % (2*period_usec*opt_jitter/100));
} else {
- period.tv_nsec = 1000LL*(period_usec);
+ period.tv_nsec = USEC_TO_NSEC*(period_usec);
}
period.tv_sec = 0;
return NULL;
}
+char* parse_bandwidths(char *params)
+{
+ struct stream *sp = &streams[nr_streams];
+
+ while (*params && nr_streams < MAX_STREAMS) {
+ char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
+ int i;
+ char *next_char;
+
+ if (strlen(params) < 2)
+ return params;
+ for (i=0; i<AC_NUM; i++) {
+ if (strncmp(params, ac_ids[i], 2) == 0) {
+ sp->ac = i;
+ params+=strlen(ac_ids[i]);
+ break;
+ }
+ }
+ if (i==AC_NUM)
+ return params;
+
+ long bw;
+ if (*params == ':') {
+ params++;
+
+ bw = strtol(params, &next_char, 10);
+ if (next_char == params)
+ return params;
+ params = next_char;
+ } else
+ bw = opt_def_bandwidth;
+
+ sp->bandwidth_bps = bw*Kbit;
+
+ long period;
+ if (*params == '@') {
+ params++;
+ period = strtol(params, &next_char, 10);
+ if (period == 0)
+ return params;
+ params = next_char;
+ }
+ else
+ period = opt_def_period_msec;
+ sp->period_usec = period*MSEC_TO_USEC;
+
+ if (*params != '\0' && *params != ',')
+ return params;
+ nr_streams++;
+ sp++;
+ if (*params == ',')
+ params++;
+ }
+ return NULL;
+}
+
int main(int argc, char *argv[])
{
- int ac, i, rc;
+ int ac, i, rc, seconds;
pthread_attr_t attr;
pthread_t thread;
char opt;
- while ((opt = getopt(argc, argv, "c:j:o:s:")) != -1) {
+ while ((opt = getopt(argc, argv, "B:b:c:g:j:o:s:T:")) != -1) {
switch (opt) {
+ case 'B':
+ opt_def_bandwidth = atoi(optarg);
+ break;
+ case 'b': {
+ char *error;
+ error = parse_bandwidths(optarg);
+ if (error != NULL) {
+ if (*error == '\0')
+ fprintf(stderr, "Bandwidth parse error - string to short\n");
+ else
+ fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
+ exit(1);
+ }
+ break;
+ }
case 'c':
opt_count_sec = atoi(optarg);
break;
+ case 'g':
+ opt_granularity_usec = atoi(optarg);
+ if (opt_granularity_usec < MIN_GRANULARITY) {
+ fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
+ exit(1);
+ }
+ break;
case 'j':
opt_jitter = atoi(optarg);
break;
case 's':
opt_packet_size = atoi(optarg);
break;
+ case 'T':
+ opt_def_period_msec = atoi(optarg);
+ break;
default:
fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
fprintf(stderr, "Options:\n");
- fprintf(stderr, " -c count (number of seconds to run)");
+ fprintf(stderr, " -B default bandwidth for -b option [kbit]\n");
+ fprintf(stderr, " -b bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec>][,...]\n");
+ fprintf(stderr, " -c count (number of seconds to run)\n");
+ fprintf(stderr, " -g histogram granularity [usec]\n");
fprintf(stderr, " -j send jitter (0-100) [%%]\n");
- fprintf(stderr, " -o output filename (.dat will be appended)");
+ fprintf(stderr, " -o output filename (.dat will be appended)\n");
fprintf(stderr, " -s size of data payload in packets [bytes]\n");
+ fprintf(stderr, " -T default period for -b option [msec]\n");
exit(1);
}
}
}
+ if (nr_streams == 0)
+ parse_bandwidths("BE");
memset(delay_stats,0, sizeof(delay_stats));
pthread_attr_init(&attr);
fprintf(stderr,"Can not open %s\n", logfname);
exit(1);
}
- fprintf(logfd, "# Invoked as: ");
- for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
- fprintf(logfd, "\n");
-
if (signal(SIGTERM, stopper) == SIG_ERR) {
perror("Error in signal registration");
exit(1);
sem_init(&sem_thread_finished, 0, 0);
/* create four receivers each per AC */
- for (ac = AC_QUEUES - 1; ac >= 0; ac--) {
+ for (ac = AC_NUM - 1; ac >= 0; ac--) {
ac_sockfd[ac] = create_ac_socket(ac);
rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
if (rc) {
}
/* create sendpoints */
- for (i = 0; i < nr_sepoints; i++) {
- rc = pthread_create(&thread, &attr, sender, (void*) &sepoint[i]);
+ for (i = 0; i < nr_streams; i++) {
+ rc = pthread_create(&thread, &attr, sender, (void*) &streams[i]);
if (rc) {
fprintf(stderr, "Error while creating sender %d\n",rc);
return 1;
}
}
- i = 0;
+ seconds = 0;
while (!exit_flag) {
- fprintf(stderr, "\r");
- for (ac = 0; ac<AC_QUEUES; ac++) {
+ sleep(1);
+ seconds++;
+ fprintf(stderr, "\r%3ds", seconds);
+ for (ac = 0; ac < AC_NUM; ac++) {
int delta = receivers[ac].received - receivers[ac].last_received;
receivers[ac].last_received = receivers[ac].received;
- fprintf(stderr, "%s: %5d (+%4d) ", ac_to_text[ac], receivers[ac].received, delta);
+ fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
}
fflush(stderr);
- sleep(1);
- i++;
- if (i == opt_count_sec) exit_flag = 1;
+ if (seconds == opt_count_sec)
+ stopper();
}
fprintf(stderr, "\nWaiting for threads to finish\n");
/* Wait for all threads to finish */
- for (i=0; i < nr_sepoints + AC_QUEUES; i++) {
+ for (i=0; i < nr_streams + AC_NUM; i++) {
sem_wait(&sem_thread_finished);
}
- printf("\n");
- save_results();
+ save_results(argc, argv, seconds);
return 0;
}