[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH nbdkit v3 2/5] filters: stats: Add humanrate() helper



We want to show both operation rate based on time spent in operation,
and global rate, based on elapsed time. Add a helper to make it easier.
---
 filters/stats/stats.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/filters/stats/stats.c b/filters/stats/stats.c
index 44f3379..abcbff2 100644
--- a/filters/stats/stats.c
+++ b/filters/stats/stats.c
@@ -94,6 +94,13 @@ humansize (uint64_t bytes)
   return ret;
 }
 
+static char *
+humanrate (uint64_t bytes, uint64_t usecs)
+{
+  double secs = usecs / 1000000.0;
+  return secs != 0.0 ? humansize (bytes / secs) : NULL;
+}
+
 static inline const char *
 maybe (char *s)
 {
@@ -105,9 +112,7 @@ print_stat (const stat *st, int64_t usecs)
 {
   if (st->ops > 0) {
     char *size = humansize (st->bytes);
-    char *rate =
-      usecs / 1000000.0 != 0 ?
-      humansize (st->bytes / (usecs / 1000000.0)) : NULL;
+    char *rate = humanrate (st->bytes, usecs);
 
     fprintf (fp, "%s: %" PRIu64 " ops, %s, %s/s\n",
              st->name, st->ops, maybe (size), maybe (rate));
-- 
2.21.0



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]