]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Introduce virFormatIntPretty
authorMartin Kletzander <mkletzan@redhat.com>
Thu, 9 Nov 2017 15:19:25 +0000 (16:19 +0100)
committerMartin Kletzander <mkletzan@redhat.com>
Sat, 18 Nov 2017 09:45:10 +0000 (10:45 +0100)
We can't output better memory sizes if we want to be compatible with libvirt
older than the one which introduced /memory/unit, but for new things we can just
output nicer capacity to the user if available.  And this function enables that.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
src/libvirt_private.syms
src/util/virutil.c
src/util/virutil.h

index a91b87d09ade2ed645a72ffa7e47816c0776e9b4..99452a263410bbc1eeda1bad003584e52d18523f 100644 (file)
@@ -2927,6 +2927,7 @@ virDoubleToStr;
 virEnumFromString;
 virEnumToString;
 virFormatIntDecimal;
+virFormatIntPretty;
 virGetDeviceID;
 virGetDeviceUnprivSGIO;
 virGetEnvAllowSUID;
index 8bdcb02fd5d049e2ee98606e6d07a84d6f8b3115..e9dbaf3d7ade938cd13c6aa16e3fd087d438d54e 100644 (file)
@@ -485,6 +485,57 @@ virFormatIntDecimal(char *buf, size_t buflen, int val)
 }
 
 
+/**
+ * virFormatIntPretty
+ *
+ * @val: Value in bytes to be shortened
+ * @unit: unit to be used
+ *
+ * Similar to vshPrettyCapacity, but operates on integers and not doubles
+ *
+ * Returns shortened value that can be used with @unit.
+ */
+unsigned long long
+virFormatIntPretty(unsigned long long val,
+                   const char **unit)
+{
+    unsigned long long limit = 1024;
+
+    if (val % limit || val == 0) {
+        *unit = "B";
+        return val;
+    }
+    limit *= 1024;
+    if (val % limit) {
+        *unit = "KiB";
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    if (val % limit) {
+        *unit = "MiB";
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    if (val % limit) {
+        *unit = "GiB";
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    if (val % limit) {
+        *unit = "TiB";
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    if (val % limit) {
+        *unit = "PiB";
+        return val / (limit / 1024);
+    }
+    limit *= 1024;
+    *unit = "EiB";
+    return val / (limit / 1024);
+}
+
+
 const char *virEnumToString(const char *const*types,
                             unsigned int ntypes,
                             int type)
index ff89d1aaaa5f65a09c75f4a6f5ec6d6a53fd6b3a..9381ad568200772c23975069bdaaf826bfd12127 100644 (file)
@@ -66,6 +66,10 @@ int virParseVersionString(const char *str, unsigned long *version,
 char *virFormatIntDecimal(char *buf, size_t buflen, int val)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 
+unsigned long long
+virFormatIntPretty(unsigned long long val,
+                   const char **unit);
+
 int virDiskNameParse(const char *name, int *disk, int *partition);
 int virDiskNameToIndex(const char* str);
 char *virIndexToDiskName(int idx, const char *prefix);