qemu-block
[Top][All Lists]
Advanced

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

[Qemu-block] [PATCH v2 5/6] block: convert to use the qemu_szutostr func


From: Daniel P. Berrange
Subject: [Qemu-block] [PATCH v2 5/6] block: convert to use the qemu_szutostr functions
Date: Fri, 9 Sep 2016 18:42:00 +0100

Remove the get_human_readable_size() method in favour of using
the new common qemu_szutostr() method. There are two slight
differences in output format

 - the new code will also deal with petabyte and exabyte suffixes,
   where as old code stopped at the terrabyte level.
 - if the value has a trailing '.0' the decimal point will be
   omitted altogether. ie,  '5M' instead of '5.0M'

Signed-off-by: Daniel P. Berrange <address@hidden>
---
 block/qapi.c               | 63 +++++++++++++++-------------------------------
 tests/qemu-iotests/095.out |  2 +-
 tests/qemu-iotests/104.out |  2 +-
 3 files changed, 22 insertions(+), 45 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index 6f947e3..0f59d9c 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -535,43 +535,15 @@ BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
     return head;
 }
 
-#define NB_SUFFIXES 4
-
-static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
-{
-    static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
-    int64_t base;
-    int i;
-
-    if (size <= 999) {
-        snprintf(buf, buf_size, "%" PRId64, size);
-    } else {
-        base = 1024;
-        for (i = 0; i < NB_SUFFIXES; i++) {
-            if (size < (10 * base)) {
-                snprintf(buf, buf_size, "%0.1f%c",
-                         (double)size / base,
-                         suffixes[i]);
-                break;
-            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
-                snprintf(buf, buf_size, "%" PRId64 "%c",
-                         ((size + (base >> 1)) / base),
-                         suffixes[i]);
-                break;
-            }
-            base = base * 1024;
-        }
-    }
-    return buf;
-}
 
 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
                         QEMUSnapshotInfo *sn)
 {
-    char buf1[128], date_buf[128], clock_buf[128];
+    char date_buf[128], clock_buf[128];
     struct tm tm;
     time_t ti;
     int64_t secs;
+    char *szval;
 
     if (!sn) {
         func_fprintf(f,
@@ -589,13 +561,14 @@ void bdrv_snapshot_dump(fprintf_function func_fprintf, 
void *f,
                  (int)((secs / 60) % 60),
                  (int)(secs % 60),
                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
+        szval = qemu_szutostr_full(sn->vm_state_size,
+                                   QEMU_STRTOSZ_DEFSUFFIX_B,
+                                   false, "");
         func_fprintf(f,
                      "%-10s%-20s%7s%20s%15s",
-                     sn->id_str, sn->name,
-                     get_human_readable_size(buf1, sizeof(buf1),
-                                             sn->vm_state_size),
-                     date_buf,
-                     clock_buf);
+                     sn->id_str, sn->name, szval,
+                     date_buf, clock_buf);
+        g_free(szval);
     }
 }
 
@@ -704,22 +677,26 @@ void bdrv_image_info_specific_dump(fprintf_function 
func_fprintf, void *f,
 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
                           ImageInfo *info)
 {
-    char size_buf[128], dsize_buf[128];
+    char *szval, *dszval;
     if (!info->has_actual_size) {
-        snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
+        dszval = g_strdup("unavailable");
     } else {
-        get_human_readable_size(dsize_buf, sizeof(dsize_buf),
-                                info->actual_size);
+        dszval = qemu_szutostr_full(info->actual_size,
+                                    QEMU_STRTOSZ_DEFSUFFIX_B,
+                                    false, "");
     }
-    get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
+    szval = qemu_szutostr_full(info->virtual_size,
+                               QEMU_STRTOSZ_DEFSUFFIX_B,
+                               false, "");
     func_fprintf(f,
                  "image: %s\n"
                  "file format: %s\n"
                  "virtual size: %s (%" PRId64 " bytes)\n"
                  "disk size: %s\n",
-                 info->filename, info->format, size_buf,
-                 info->virtual_size,
-                 dsize_buf);
+                 info->filename, info->format, szval,
+                 info->virtual_size, dszval);
+    g_free(szval);
+    g_free(dszval);
 
     if (info->has_encrypted && info->encrypted) {
         func_fprintf(f, "encrypted: yes\n");
diff --git a/tests/qemu-iotests/095.out b/tests/qemu-iotests/095.out
index 73875ca..96c02f6 100644
--- a/tests/qemu-iotests/095.out
+++ b/tests/qemu-iotests/095.out
@@ -6,7 +6,7 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=104857600 
backing_file=TEST_DIR/
 === Base image info before commit and resize ===
 image: TEST_DIR/t.IMGFMT.base
 file format: IMGFMT
-virtual size: 5.0M (5242880 bytes)
+virtual size: 5M (5242880 bytes)
 
 === Running QEMU Live Commit Test ===
 
diff --git a/tests/qemu-iotests/104.out b/tests/qemu-iotests/104.out
index ab8d892..f40b629 100644
--- a/tests/qemu-iotests/104.out
+++ b/tests/qemu-iotests/104.out
@@ -4,7 +4,7 @@ QA output created by 104
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1024
 image: TEST_DIR/t.IMGFMT
 file format: IMGFMT
-virtual size: 1.0K (1024 bytes)
+virtual size: 1K (1024 bytes)
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1234
 image: TEST_DIR/t.IMGFMT
 file format: IMGFMT
-- 
2.7.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]