[libvirt] [PATCH] virsh: additional scaled output units

Eric Blake eblake at redhat.com
Tue Sep 9 04:11:43 UTC 2014


The parser accepts P and E, so the formatter should too.

* tools/virsh.c (vshPrettyCapacity): Handle larger units.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 tools/virsh.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 9706acc..64195a4 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -151,22 +151,40 @@ vshNameSorter(const void *a, const void *b)
 double
 vshPrettyCapacity(unsigned long long val, const char **unit)
 {
-    if (val < 1024) {
+    double limit = 1024;
+
+    if (val < limit) {
         *unit = "B";
-        return (double)val;
-    } else if (val < (1024.0l * 1024.0l)) {
+        return val;
+    }
+    limit *= 1024;
+    if (val < limit) {
         *unit = "KiB";
-        return (((double)val / 1024.0l));
-    } else if (val < (1024.0l * 1024.0l * 1024.0l)) {
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    if (val < limit) {
         *unit = "MiB";
-        return (double)val / (1024.0l * 1024.0l);
-    } else if (val < (1024.0l * 1024.0l * 1024.0l * 1024.0l)) {
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    if (val < limit) {
         *unit = "GiB";
-        return (double)val / (1024.0l * 1024.0l * 1024.0l);
-    } else {
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    if (val < limit) {
         *unit = "TiB";
-        return (double)val / (1024.0l * 1024.0l * 1024.0l * 1024.0l);
+        return val / (limit / 1024);
     }
+    limit *= 1024;
+    if (val < limit) {
+        *unit = "PiB";
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    *unit = "EiB";
+    return val / (limit / 1024);
 }

 /*
-- 
1.9.3




More information about the libvir-list mailing list