diff options
author | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-05-27 15:18:27 +0200 |
---|---|---|
committer | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-05-27 15:18:27 +0200 |
commit | 7d9e6dc52ef4fca6edf8199a3146f09308b57860 (patch) | |
tree | 56b88ae328a58f839d40838a92d49c5defde1c09 /src/tools/cbr/cbr_client.c | |
parent | 17e9c86c2fa45b39b88b73f9fd34c2d9b95ae7d4 (diff) | |
parent | 5f79244d8d83fffab21dc3d18697d938ef836c09 (diff) | |
download | ouroboros-7d9e6dc52ef4fca6edf8199a3146f09308b57860.tar.gz ouroboros-7d9e6dc52ef4fca6edf8199a3146f09308b57860.zip |
Merged in dstaesse/ouroboros/be-cbr-flood (pull request #116)
tools: updated cbr with flood and sleep options
Diffstat (limited to 'src/tools/cbr/cbr_client.c')
-rw-r--r-- | src/tools/cbr/cbr_client.c | 68 |
1 files changed, 49 insertions, 19 deletions
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 <ouroboros/dev.h> #include <ouroboros/time_utils.h> -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); |