>From 758bbd8a0a82e1c353d5a919a6589bb27dc7e6e7 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Wed, 4 Dec 2019 00:28:37 +0200 Subject: [PATCH] filters: stats: Show size and rate in human size 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 | 41 ++++++++++++++++++++++----- 2 files changed, 39 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..6e48017 100644 --- a/filters/stats/stats.c +++ b/filters/stats/stats.c @@ -71,18 +71,45 @@ 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.; + int r; + char *ret; + + if (bytes < KiB) + r = asprintf (&ret, "%" PRIu64 " bytes", bytes); + else if (bytes < MiB) + r = asprintf (&ret, "%.2f KiB", bytes / (double)KiB); + else if (bytes < GiB) + r = asprintf (&ret, "%.2f MiB", bytes / (double)MiB); + else + r = asprintf (&ret, "%.2f GiB", bytes / (double)GiB); + if (r == -1) + ret = NULL; + return ret; } -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 = + usecs / 1000000.0 != 0 ? + humansize (st->bytes / (usecs / 1000000.0)) : NULL; + + fprintf (fp, "%s: %" PRIu64 " ops, %s, %s/s\n", + st->name, st->ops, + size ? size : "(n/a)", rate ? rate : "(n/a)"); + + free (size); + free (rate); + } } static inline void -- 2.23.0