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;
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);
// 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;
// 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;
// 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;
#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;
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;
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);