size_t total_bytes_received;
};
-static char* format_size(ssize_t size) {
- const char* suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL };
+enum {
+ FIREPERF_FORMAT_BYTES,
+ FIREPERF_FORMAT_BITS,
+};
+
+const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL };
+const char* suffixes_bits[] = { "b", "Kb", "Mb", "Gb", "Tb", NULL };
+
+static char* format_size(ssize_t size, int unit) {
+ const char** suffixes;
+ unsigned int divisor;
+
+ switch (unit) {
+ case FIREPERF_FORMAT_BYTES:
+ suffixes = suffixes_bytes;
+ divisor = 1024;
+ break;
+
+ case FIREPERF_FORMAT_BITS:
+ suffixes = suffixes_bits;
+ divisor = 1000;
+ break;
+
+ // Invalid input
+ default:
+ return NULL;
+ }
const char** suffix;
char* retval = NULL;
double s = size;
for (suffix = suffixes; *suffix; suffix++) {
- if (abs(s) < 1024)
+ if (abs(s) < divisor)
break;
- s /= 1024;
+ s /= divisor;
}
if (!*suffix)
const char* timestamp = format_timespec(&now);
// Format total bytes received
- char* total_bytes_received = format_size(stats->total_bytes_received);
+ char* total_bytes_received = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES);
// Calculate bandwidth
- char* bps = format_size(stats->bytes_received / delta);
+ char* bps = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS);
INFO( conf, "--- %s --------------------\n", timestamp);
DEBUG(conf, " %-20s: %19.4fs\n", "Delta", delta);