From 5f79244d8d83fffab21dc3d18697d938ef836c09 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Fri, 27 May 2016 14:46:46 +0200 Subject: tools: updated cbr with flood and sleep options The cbr client will now use busy waiting by default to control the sending rate. A --sleep option has been added to allow low CPU usage when sending at low data rates. A --flood option has been added that writes SDU's as fast as possible. --- src/tools/cbr/cbr_client.c | 68 +++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 19 deletions(-) (limited to 'src/tools/cbr/cbr_client.c') diff --git a/src/tools/cbr/cbr_client.c b/src/tools/cbr/cbr_client.c index ff7d4057..b0c04f39 100644 --- a/src/tools/cbr/cbr_client.c +++ b/src/tools/cbr/cbr_client.c @@ -24,18 +24,29 @@ #include #include -int client_main(int duration, int size, long rate) +static void busy_wait_until(const struct timespec * deadline) +{ + struct timespec now; + clock_gettime(CLOCK_REALTIME, &now); + while (now.tv_sec < deadline->tv_sec) + clock_gettime(CLOCK_REALTIME, &now); + while (now.tv_sec == deadline->tv_sec + && now.tv_nsec < deadline->tv_nsec) + clock_gettime(CLOCK_REALTIME, &now); +} + +int client_main(int duration, int size, long rate, bool flood, bool sleep) { int fd = 0; int result = 0; bool stop = false; char buf[size]; long seqnr = 0; - unsigned long gap = size * 8 * (BILLION / rate); /* ns */ + unsigned long gap = size * 8.0 * (BILLION / (double) rate); struct timespec start; struct timespec end; - struct timespec interval = {(gap / BILLION), gap % BILLION}; + struct timespec intv = {(gap / BILLION), gap % BILLION}; int ms; if (ap_init(CLIENT_AP_NAME)) { @@ -62,24 +73,43 @@ int client_main(int duration, int size, long rate) } clock_gettime(CLOCK_REALTIME, &start); - while (!stop) { - memcpy(buf, &seqnr, sizeof(seqnr)); - - if (flow_write(fd, buf, size) == -1) { - printf("Failed to write SDU.\n"); - stop = true; - continue; + if (!flood) { + while (!stop) { + clock_gettime(CLOCK_REALTIME, &end); + ts_add(&end, &intv, &end); + memcpy(buf, &seqnr, sizeof(seqnr)); + + if (flow_write(fd, buf, size) == -1) { + stop = true; + continue; + } + + if (sleep) + nanosleep(&intv, NULL); + else + busy_wait_until(&end); + + ++seqnr; + + if (ts_diff_us(&start, &end) / MILLION + >= (long) duration) + stop = true; + } + } else { /* flood */ + while (!stop) { + clock_gettime(CLOCK_REALTIME, &end); + if (flow_write(fd, buf, size) == -1) { + stop = true; + continue; + } + + ++seqnr; + + if (ts_diff_us(&start, &end) / MILLION + >= (long) duration) + stop = true; } - nanosleep(&interval, NULL); - - seqnr++; - - clock_gettime(CLOCK_REALTIME, &end); - - if (duration != 0 - && ts_diff_us(&start, &end) / MILLION >= (long) duration) - stop = true; } clock_gettime(CLOCK_REALTIME, &end); -- cgit v1.2.3