Introduce a new %pB format specifier to print sizes (in bytes) in a
human-readable form.

Signed-off-by: Roger Pau Monné <roger....@citrix.com>
---
Cc: Andrew Cooper <andrew.coop...@citrix.com>
Cc: George Dunlap <george.dun...@eu.citrix.com>
Cc: Ian Jackson <ian.jack...@eu.citrix.com>
Cc: Jan Beulich <jbeul...@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.w...@oracle.com>
Cc: Stefano Stabellini <sstabell...@kernel.org>
Cc: Tim Deegan <t...@xen.org>
Cc: Wei Liu <wei.l...@citrix.com>
---
 docs/misc/printk-formats.txt |  5 +++++
 xen/common/vsprintf.c        | 15 +++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/docs/misc/printk-formats.txt b/docs/misc/printk-formats.txt
index 525108f..0ee3504 100644
--- a/docs/misc/printk-formats.txt
+++ b/docs/misc/printk-formats.txt
@@ -30,3 +30,8 @@ Domain and vCPU information:
 
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
+
+Size in human readable form:
+
+       %pZ     Size in human-readable form (input size must be in bytes).
+                 e.g.  24MB
diff --git a/xen/common/vsprintf.c b/xen/common/vsprintf.c
index f92fb67..2dadaec 100644
--- a/xen/common/vsprintf.c
+++ b/xen/common/vsprintf.c
@@ -386,6 +386,21 @@ static char *pointer(char *str, char *end, const char 
**fmt_ptr,
             *str = 'v';
         return number(str + 1, end, v->vcpu_id, 10, -1, -1, 0);
     }
+    case 'Z':
+    {
+        static const char units[][3] = {"B", "KB", "MB", "GB", "TB"};
+        size_t size = (size_t)arg;
+        int i = 0;
+
+        /* Advance parents fmt string, as we have consumed 'B' */
+        ++*fmt_ptr;
+
+        while ( ++i < sizeof(units) && size >= 1024 )
+            size >>= 10; /* size /= 1024 */
+
+        str = number(str, end, size, 10, -1, -1, 0);
+        return string(str, end, units[i-1], -1, -1, 0);
+    }
     }
 
     if ( field_width == -1 )
-- 
2.7.4 (Apple Git-66)


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

Reply via email to