]> git.ipfire.org Git - fireperf.git/commitdiff
main: Output everything in Bit/s
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Sep 2024 16:20:50 +0000 (16:20 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Sep 2024 16:20:50 +0000 (16:20 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/tui.c
src/util.c
src/util.h

index edde08e43e2e071f41f73fce44c44141dc138dd6..21cbcf163f0784f1f41dcf9c064be99721943d6f 100644 (file)
--- 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;
 
index de4715e1ee0119a194301e2c1b180ae58a626fb3..4d7779e7603a422de512e92c67bfe80fb1934fd7 100644 (file)
 
 #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;
 
index 32c3f2ad08c4af261054a36ea8cd231b929c2c38..351164d1282ec220bf7e6715fa47224202b05600 100644 (file)
 
 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);