From 4068dd1166b63313427221cd1c98893fea664baa Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 28 Sep 2024 16:20:50 +0000 Subject: [PATCH] main: Output everything in Bit/s Signed-off-by: Michael Tremer --- src/tui.c | 10 +++++----- src/util.c | 36 +++++++++++++++++++++++++----------- src/util.h | 4 +++- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/tui.c b/src/tui.c index edde08e..21cbcf1 100644 --- a/src/tui.c +++ b/src/tui.c @@ -234,7 +234,7 @@ static int fireperf_tui_update_status(struct fireperf_tui* tui) { size_t peak_bps = fireperf_tui_get_peak_bps(tui, max_x); // Format peak bandwidth - r = format_size(buffer, peak_bps, FIREPERF_FORMAT_BITS); + r = format_size(buffer, peak_bps * 8, FIREPERF_FORMAT_BITS_PER_SECOND); if (r < 0) goto ERROR; @@ -500,7 +500,7 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) { size_t avg_sent_bytes = fireperf_tui_get_avg_bytes(tui, FIREPERF_SENT, max_x); // Fetch peak bandwidth - size_t peak_bps = fireperf_tui_get_peak_bps(tui, max_x) * 1.2; + size_t peak_bps = fireperf_tui_get_peak_bps(tui, max_x); // Figure out how many bps a step represents double step = peak_bps / (max_y - 4); @@ -510,7 +510,7 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) { // Draw the grid for (double line = 0; line <= 1; line += 0.25) { - r = format_size(label, line * peak_bps, FIREPERF_FORMAT_BYTES); + r = format_size(label, line * peak_bps * 8, FIREPERF_FORMAT_BITS_PER_SECOND); if (r < 0) continue; @@ -527,7 +527,7 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) { // Show the average if (avg_sent_bytes) { - r = format_size(label, avg_sent_bytes, FIREPERF_FORMAT_BYTES); + r = format_size(label, avg_sent_bytes * 8, FIREPERF_FORMAT_BITS_PER_SECOND); if (r < 0) return r; @@ -553,7 +553,7 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) { // Show the average if (avg_rcvd_bytes) { - r = format_size(label, avg_rcvd_bytes, FIREPERF_FORMAT_BYTES); + r = format_size(label, avg_rcvd_bytes * 8, FIREPERF_FORMAT_BITS_PER_SECOND); if (r < 0) return r; diff --git a/src/util.c b/src/util.c index de4715e..4d7779e 100644 --- a/src/util.c +++ b/src/util.c @@ -26,22 +26,36 @@ #include "util.h" -static const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL }; -static const char* suffixes_bits[] = { "b", "Kb", "Mb", "Gb", "Tb", NULL }; +static const char* units_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL }; +static const char* units_bits[] = { "b", "Kb", "Mb", "Gb", "Tb", NULL }; -int __format_size(char* buffer, const size_t length, ssize_t size, const int unit) { - const char** suffixes = NULL; +int __format_size(char* buffer, const size_t length, ssize_t size, int format) { + const char** units = NULL; unsigned int divisor; int r; - switch (unit) { + const char* suffix = ""; + + switch (format) { + case FIREPERF_FORMAT_BYTES_PER_SECOND: + format = FIREPERF_FORMAT_BYTES; + suffix = "/s"; + break; + + case FIREPERF_FORMAT_BITS_PER_SECOND: + format = FIREPERF_FORMAT_BITS; + suffix = "/s"; + break; + } + + switch (format) { case FIREPERF_FORMAT_BYTES: - suffixes = suffixes_bytes; + units = units_bytes; divisor = 1024; break; case FIREPERF_FORMAT_BITS: - suffixes = suffixes_bits; + units = units_bits; divisor = 1000; break; @@ -50,23 +64,23 @@ int __format_size(char* buffer, const size_t length, ssize_t size, const int uni return -EINVAL; } - const char** suffix = NULL; + const char** unit = NULL; // Convert into double double s = size; - for (suffix = suffixes; *suffix; suffix++) { + for (unit = units; *unit; unit++) { if (abs(s) < divisor) break; s /= divisor; } - if (!*suffix) + if (!*unit) return -EINVAL; // Format the output string - r = snprintf(buffer, length, "%.02f %s", s, *suffix); + r = snprintf(buffer, length, "%.02f %s%s", s, *unit, suffix); if (r < 0) return r; diff --git a/src/util.h b/src/util.h index 32c3f2a..351164d 100644 --- a/src/util.h +++ b/src/util.h @@ -23,13 +23,15 @@ enum { FIREPERF_FORMAT_BYTES, + FIREPERF_FORMAT_BYTES_PER_SECOND, FIREPERF_FORMAT_BITS, + FIREPERF_FORMAT_BITS_PER_SECOND, }; #define format_size(buffer, size, unit) \ __format_size(buffer, sizeof(buffer), size, unit) -int __format_size(char* buffer, const size_t length, ssize_t size, const int unit); +int __format_size(char* buffer, const size_t length, ssize_t size, int format); const char* format_timespec(const struct timespec* t); double timespec_delta(struct timespec* t1, struct timespec* t2); -- 2.47.3