[Libguestfs] [PATCH nbdkit] filters: stats: Show size and rate in human size

Nir Soffer nirsof at gmail.com
Tue Dec 3 22:28:37 UTC 2019


Make stats output easier to parse and more useful for humans.

Here is an example output with this change:

elapsed time: 1.33377 s
read: 248 ops, 4.75 MiB, 3.56 MiB/s
write: 78 ops, 32.64 MiB, 24.48 MiB/s
trim: 33 ops, 1.00 GiB, 767.75 MiB/s
flush: 9 ops, 0 bytes, 0 bytes/s
---
 filters/stats/nbdkit-stats-filter.pod | 11 ++++----
 filters/stats/stats.c                 | 36 +++++++++++++++++++++------
 2 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/filters/stats/nbdkit-stats-filter.pod b/filters/stats/nbdkit-stats-filter.pod
index e10399b..28693d4 100644
--- a/filters/stats/nbdkit-stats-filter.pod
+++ b/filters/stats/nbdkit-stats-filter.pod
@@ -25,12 +25,11 @@ number of read, write and trim operations involved:
                  run : \
                  mkfs ext4 /dev/sda
  '
- elapsed time: 1.13974 s
- read: 207 ops, 4002304 bytes, 2.80928e+07 bits/s
- write: 12 ops, 614400 bytes, 4.31257e+06 bits/s
- trim: 33 ops, 1073741824 bytes, 7.53676e+09 bits/s
- zero: 4 ops, 33628160 bytes, 2.36041e+08 bits/s
- flush: 9 ops, 0 bytes, 0 bits/s
+ elapsed time: 1.33377 s
+ read: 248 ops, 4.75 MiB, 3.56 MiB/s
+ write: 78 ops, 32.64 MiB, 24.48 MiB/s
+ trim: 33 ops, 1.00 GiB, 767.75 MiB/s
+ flush: 9 ops, 0 bytes, 0 bytes/s
 
 =head1 PARAMETERS
 
diff --git a/filters/stats/stats.c b/filters/stats/stats.c
index 919dc16..e10307c 100644
--- a/filters/stats/stats.c
+++ b/filters/stats/stats.c
@@ -71,18 +71,40 @@ static stat extents_st = { "extents" };
 static stat cache_st   = { "cache" };
 static stat flush_st   = { "flush" };
 
-static inline double
-calc_bps (uint64_t bytes, int64_t usecs)
+#define KiB 1024
+#define MiB 1048576
+#define GiB 1073741824
+
+static char *
+humansize(uint64_t bytes)
 {
-  return 8.0 * bytes / usecs * 1000000.;
+    char buf[32];
+
+    if (bytes < KiB)
+        snprintf (buf, sizeof(buf), "%" PRIu64 " bytes", bytes);
+    else if (bytes < MiB)
+        snprintf (buf, sizeof(buf), "%.2f KiB", bytes / (double)KiB);
+    else if (bytes < GiB)
+        snprintf (buf, sizeof(buf), "%.2f MiB", bytes / (double)MiB);
+    else
+        snprintf (buf, sizeof(buf), "%.2f GiB", bytes / (double)GiB);
+
+    return strdup(buf);
 }
 
-static inline void
+static void
 print_stat (const stat *st, int64_t usecs)
 {
-  if (st->ops > 0)
-    fprintf (fp, "%s: %" PRIu64 " ops, %" PRIu64 " bytes, %g bits/s\n",
-             st->name, st->ops, st->bytes, calc_bps (st->bytes, usecs));
+  if (st->ops > 0) {
+    char *size = humansize (st->bytes);
+    char *rate = humansize (st->bytes / (usecs / 1000000.0));
+
+    fprintf (fp, "%s: %" PRIu64 " ops, %s, %s/s\n",
+             st->name, st->ops, size, rate);
+
+    free(size);
+    free(rate);
+  }
 }
 
 static inline void
-- 
2.21.0





More information about the Libguestfs mailing list