Skip to contentSkip to conversation

qapi and snapshot code clean up in block layer

PATCH Superseded Wenchao Xia 4 participants 14 messages May 23, 2013 – May 24, 2013 Block layer

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover qapi and snapshot code clean up in block layer

These patches are the common part of my hmp/qmp block query series and Pavel's qmp snapshot command converion series. It mainly does following things:
1 move snapshot related code to block/snapshot.c, qmp and info dumping code to block/qapi.c.
2 better info dumping function to get rid of buffer, avoid string truncation.

Stefan Hajnoczi (1):
1 block: drop bs_snapshots global variable

v2:
Squash code moving patches since they are reviewed in v1.
Drop bdrv_snapshot_find() function change patch, since it related to snapshot
logic which should be changed together with Pavel's serial.
Use a parameter in message_printf() to tell where to print, instead of use
global variable "cur_mon" inside.

Wenchao Xia (4):
2 block: move snapshot code in block.c to block/snapshot.c
3 block: move qmp and info dump related code to block/qapi.c
4 util: add new function message_printf()
5 block: dump to specified output for bdrv_snapshot_dump() and bdrv_image_info_dump()

 block.c                     |  318 -------------------------------------
 block/Makefile.objs         |    1 +
 block/qapi.c                |  365 +++++++++++++++++++++++++++++++++++++++++++
 block/snapshot.c            |  162 +++++++++++++++++++
 include/block/block.h       |   29 +----
 include/block/block_int.h   |    1 +
 include/block/qapi.h        |   42 +++++
 include/block/snapshot.h    |   54 +++++++
 include/qemu/error-report.h |   13 ++
 qemu-img.c                  |  169 ++-------------------
 savevm.c                    |   44 +++---
 util/qemu-error.c           |   28 +++-
 12 files changed, 697 insertions(+), 529 deletions(-)
 create mode 100644 block/qapi.c
 create mode 100644 block/snapshot.c
 create mode 100644 include/block/qapi.h
 create mode 100644 include/block/snapshot.h
#1 lore raw

PATCH v2 1/5 block: drop bs_snapshots global variable

From: Stefan Hajnoczi <[email protected]>

The bs_snapshots global variable points to the BlockDriverState which will be used to save vmstate. This is really a savevm.c concept but was moved into block.c:bdrv_snapshots() when it became clear that hotplug could result in a dangling pointer.

While auditing the block layer's global state I came upon bs_snapshots and realized that a variable is not necessary here. Simply find the first BlockDriverState capable of internal snapshots each time this is needed.

The behavior of bdrv_snapshots() is preserved across hotplug because new drives are always appended to the bdrv_states list. This means that calling the new find_vmstate_bs() function is idempotent - it returns the same BlockDriverState unless it was hot-unplugged.

---
 block.c               |   28 ----------------------------
 include/block/block.h |    1 -
 savevm.c              |   19 +++++++++++++++----
 3 files changed, 15 insertions(+), 33 deletions(-)
block.c +0 -28
index 3f87489..478a3b2 100644
@@ -99,9 +99,6 @@ static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
     QLIST_HEAD_INITIALIZER(bdrv_drivers);
 
-/* The device to use for VM snapshots */
-static BlockDriverState *bs_snapshots;
-
 /* If non-zero, use only whitelisted block drivers */
 static int use_bdrv_whitelist;
 
@@ -1357,9 +1354,6 @@ void bdrv_close(BlockDriverState *bs)
     notifier_list_notify(&bs->close_notifiers, bs);
 
     if (bs->drv) {
-        if (bs == bs_snapshots) {
-            bs_snapshots = NULL;
-        }
         if (bs->backing_hd) {
             bdrv_delete(bs->backing_hd);
             bs->backing_hd = NULL;
@@ -1591,7 +1585,6 @@ void bdrv_delete(BlockDriverState *bs)
 
     bdrv_close(bs);
 
-    assert(bs != bs_snapshots);
     g_free(bs);
 }
 
@@ -1635,9 +1628,6 @@ void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
 {
     bs->dev_ops = ops;
     bs->dev_opaque = opaque;
-    if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
-        bs_snapshots = NULL;
-    }
 }
 
 void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
@@ -3381,24 +3371,6 @@ int bdrv_is_snapshot(BlockDriverState *bs)
     return !!(bs->open_flags & BDRV_O_SNAPSHOT);
 }
 
-BlockDriverState *bdrv_snapshots(void)
-{
-    BlockDriverState *bs;
-
-    if (bs_snapshots) {
-        return bs_snapshots;
-    }
-
-    bs = NULL;
-    while ((bs = bdrv_next(bs))) {
-        if (bdrv_can_snapshot(bs)) {
-            bs_snapshots = bs;
-            return bs;
-        }
-    }
-    return NULL;
-}
-
 int bdrv_snapshot_create(BlockDriverState *bs,
                          QEMUSnapshotInfo *sn_info)
 {
include/block/block.h +0 -1
index 1251c5c..38263b9 100644
@@ -332,7 +332,6 @@ BlockInfo *bdrv_query_info(BlockDriverState *s);
 BlockStats *bdrv_query_stats(const BlockDriverState *bs);
 int bdrv_can_snapshot(BlockDriverState *bs);
 int bdrv_is_snapshot(BlockDriverState *bs);
-BlockDriverState *bdrv_snapshots(void);
 int bdrv_snapshot_create(BlockDriverState *bs,
                          QEMUSnapshotInfo *sn_info);
 int bdrv_snapshot_goto(BlockDriverState *bs,
savevm.c +15 -4
index 31dcce9..baa1a09 100644
@@ -2262,6 +2262,17 @@ out:
     return ret;
 }
 
+static BlockDriverState *find_vmstate_bs(void)
+{
+    BlockDriverState *bs = NULL;
+    while ((bs = bdrv_next(bs))) {
+        if (bdrv_can_snapshot(bs)) {
+            return bs;
+        }
+    }
+    return NULL;
+}
+
 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
                               const char *name)
 {
@@ -2338,7 +2349,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
         }
     }
 
-    bs = bdrv_snapshots();
+    bs = find_vmstate_bs();
     if (!bs) {
         monitor_printf(mon, "No block device can accept snapshots\n");
         return;
@@ -2440,7 +2451,7 @@ int load_vmstate(const char *name)
     QEMUFile *f;
     int ret;
 
-    bs_vm_state = bdrv_snapshots();
+    bs_vm_state = find_vmstate_bs();
     if (!bs_vm_state) {
         error_report("No block device supports snapshots");
         return -ENOTSUP;
@@ -2519,7 +2530,7 @@ void do_delvm(Monitor *mon, const QDict *qdict)
     int ret;
     const char *name = qdict_get_str(qdict, "name");
 
-    bs = bdrv_snapshots();
+    bs = find_vmstate_bs();
     if (!bs) {
         monitor_printf(mon, "No block device supports snapshots\n");
         return;
@@ -2551,7 +2562,7 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict)
     int *available_snapshots;
     char buf[256];
 
-    bs = bdrv_snapshots();
+    bs = find_vmstate_bs();
     if (!bs) {
         monitor_printf(mon, "No available block device supports snapshots\n");
         return;
Signature · 1 line
1.7.1
#2 lore raw

PATCH v2 2/5 block: move snapshot code in block.c to block/snapshot.c

In reply to Wenchao Xia

All snapshot related code, except bdrv_snapshot_dump(), is moved to block/snapshot.c. bdrv_snapshot_dump() will be moved to another file later. It also fixes small code style errors reported by check script.

---
 block.c                  |  105 ------------------------------
 block/Makefile.objs      |    1 +
 block/snapshot.c         |  162 ++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h    |   28 ++------
 include/block/snapshot.h |   54 +++++++++++++++
 savevm.c                 |   23 +------
 6 files changed, 224 insertions(+), 149 deletions(-)
 create mode 100644 block/snapshot.c
 create mode 100644 include/block/snapshot.h
block.c +0 -105
index 478a3b2..2561800 100644
@@ -3346,111 +3346,6 @@ bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
     return false;
 }
 
-/**************************************************************/
-/* handling of snapshots */
-
-int bdrv_can_snapshot(BlockDriverState *bs)
-{
-    BlockDriver *drv = bs->drv;
-    if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
-        return 0;
-    }
-
-    if (!drv->bdrv_snapshot_create) {
-        if (bs->file != NULL) {
-            return bdrv_can_snapshot(bs->file);
-        }
-        return 0;
-    }
-
-    return 1;
-}
-
-int bdrv_is_snapshot(BlockDriverState *bs)
-{
-    return !!(bs->open_flags & BDRV_O_SNAPSHOT);
-}
-
-int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info)
-{
-    BlockDriver *drv = bs->drv;
-    if (!drv)
-        return -ENOMEDIUM;
-    if (drv->bdrv_snapshot_create)
-        return drv->bdrv_snapshot_create(bs, sn_info);
-    if (bs->file)
-        return bdrv_snapshot_create(bs->file, sn_info);
-    return -ENOTSUP;
-}
-
-int bdrv_snapshot_goto(BlockDriverState *bs,
-                       const char *snapshot_id)
-{
-    BlockDriver *drv = bs->drv;
-    int ret, open_ret;
-
-    if (!drv)
-        return -ENOMEDIUM;
-    if (drv->bdrv_snapshot_goto)
-        return drv->bdrv_snapshot_goto(bs, snapshot_id);
-
-    if (bs->file) {
-        drv->bdrv_close(bs);
-        ret = bdrv_snapshot_goto(bs->file, snapshot_id);
-        open_ret = drv->bdrv_open(bs, NULL, bs->open_flags);
-        if (open_ret < 0) {
-            bdrv_delete(bs->file);
-            bs->drv = NULL;
-            return open_ret;
-        }
-        return ret;
-    }
-
-    return -ENOTSUP;
-}
-
-int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
-{
-    BlockDriver *drv = bs->drv;
-    if (!drv)
-        return -ENOMEDIUM;
-    if (drv->bdrv_snapshot_delete)
-        return drv->bdrv_snapshot_delete(bs, snapshot_id);
-    if (bs->file)
-        return bdrv_snapshot_delete(bs->file, snapshot_id);
-    return -ENOTSUP;
-}
-
-int bdrv_snapshot_list(BlockDriverState *bs,
-                       QEMUSnapshotInfo **psn_info)
-{
-    BlockDriver *drv = bs->drv;
-    if (!drv)
-        return -ENOMEDIUM;
-    if (drv->bdrv_snapshot_list)
-        return drv->bdrv_snapshot_list(bs, psn_info);
-    if (bs->file)
-        return bdrv_snapshot_list(bs->file, psn_info);
-    return -ENOTSUP;
-}
-
-int bdrv_snapshot_load_tmp(BlockDriverState *bs,
-        const char *snapshot_name)
-{
-    BlockDriver *drv = bs->drv;
-    if (!drv) {
-        return -ENOMEDIUM;
-    }
-    if (!bs->read_only) {
-        return -EINVAL;
-    }
-    if (drv->bdrv_snapshot_load_tmp) {
-        return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
-    }
-    return -ENOTSUP;
-}
-
 /* backing_file can either be relative, or absolute, or a protocol.  If it is
  * relative, it must be relative to the chain.  So, passing in bs->filename
  * from a BDS as backing_file should not be done, as that may be relative to
block/Makefile.objs +1 -0
index 5f0358a..8670999 100644
@@ -4,6 +4,7 @@ block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-obj-y += qed-check.o
 block-obj-y += vhdx.o
 block-obj-y += parallels.o blkdebug.o blkverify.o
+block-obj-y += snapshot.o
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += raw-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
block/snapshot.c new file +162 -0

162 lines not on this page · raw message

include/block/block.h +6 -22
index 38263b9..fde6fb8 100644
@@ -7,6 +7,11 @@
 #include "block/coroutine.h"
 #include "qapi/qmp/qobject.h"
 #include "qapi-types.h"
+/*
+ * snapshot.h is needed since bdrv_snapshot_dump(), it can be removed when the
+ * function is moved to other file.
+ */
+#include "block/snapshot.h"
 
 /* block.c */
 typedef struct BlockDriver BlockDriver;
@@ -27,17 +32,6 @@ typedef struct BlockFragInfo {
     uint64_t compressed_clusters;
 } BlockFragInfo;
 
-typedef struct QEMUSnapshotInfo {
-    char id_str[128]; /* unique snapshot id */
-    /* the following fields are informative. They are not needed for
-       the consistency of the snapshot */
-    char name[256]; /* user chosen name */
-    uint64_t vm_state_size; /* VM state info size */
-    uint32_t date_sec; /* UTC date of the snapshot */
-    uint32_t date_nsec;
-    uint64_t vm_clock_nsec; /* VM clock relative to boot */
-} QEMUSnapshotInfo;
-
 /* Callbacks for block device models */
 typedef struct BlockDevOps {
     /*
@@ -330,17 +324,7 @@ void bdrv_get_full_backing_filename(BlockDriverState *bs,
                                     char *dest, size_t sz);
 BlockInfo *bdrv_query_info(BlockDriverState *s);
 BlockStats *bdrv_query_stats(const BlockDriverState *bs);
-int bdrv_can_snapshot(BlockDriverState *bs);
-int bdrv_is_snapshot(BlockDriverState *bs);
-int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info);
-int bdrv_snapshot_goto(BlockDriverState *bs,
-                       const char *snapshot_id);
-int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
-int bdrv_snapshot_list(BlockDriverState *bs,
-                       QEMUSnapshotInfo **psn_info);
-int bdrv_snapshot_load_tmp(BlockDriverState *bs,
-                           const char *snapshot_name);
+
 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size);
include/block/snapshot.h new file +54 -0

54 lines not on this page · raw message

savevm.c +1 -22
index baa1a09..ab53a02 100644
@@ -40,6 +40,7 @@
 #include "trace.h"
 #include "qemu/bitops.h"
 #include "qemu/iov.h"
+#include "block/snapshot.h"
 
 #define SELF_ANNOUNCE_ROUNDS 5
 
@@ -2273,28 +2274,6 @@ static BlockDriverState *find_vmstate_bs(void)
     return NULL;
 }
 
-static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
-                              const char *name)
-{
-    QEMUSnapshotInfo *sn_tab, *sn;
-    int nb_sns, i, ret;
-
-    ret = -ENOENT;
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
-    if (nb_sns < 0)
-        return ret;
-    for(i = 0; i < nb_sns; i++) {
-        sn = &sn_tab[i];
-        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
-            *sn_info = *sn;
-            ret = 0;
-            break;
-        }
-    }
-    g_free(sn_tab);
-    return ret;
-}
-
 /*
  * Deletes snapshots of a given name in all opened images.
  */
Signature · 1 line
1.7.1
#3 lore raw

Re: [Qemu-devel] [PATCH V2 2/5] block: move snapshot code in block.c to block/snapshot.c

block.c bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) · line 3372

Quoted 4 lines · Wenchao Xia
block.c +0 -4
-int bdrv_is_snapshot(BlockDriverState *bs)
-{
-    return !!(bs->open_flags & BDRV_O_SNAPSHOT);
-}

No need to respin, but this function has nothing to do with the other functions. This function is about -drive snapshot=on, the others are about internal snapshots.

It should not go into block/snapshot.c. At least it should be documented as having nothing to do with internal snapshots.

Stefan

#4 lore raw

PATCH v2 3/5 block: move qmp and info dump related code to block/qapi.c

In reply to Wenchao Xia

This patch is a pure code move patch, except following modification:
1 get_human_readable_size() is changed to static function. 2 dump_human_image_info() is renamed to bdrv_image_info_dump(). 3 in qmp_query_block() and qmp_query_blockstats, use bdrv_next(bs) instead of direct traverse of global array 'bdrv_states'. 4 code style fix.

To avoid conflict and tip better, macro in header file is BLOCK_QAPI_H instead of QAPI_H. Now block.h and snapshot.h are at the same level in include path, block_int.h and qapi.h will both include them.

---
 block.c                   |  185 -----------------------
 block/Makefile.objs       |    2 +-
 block/qapi.c              |  360 +++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h     |   10 --
 include/block/block_int.h |    1 +
 include/block/qapi.h      |   41 +++++
 qemu-img.c                |  156 +-------------------
 savevm.c                  |    1 +
 8 files changed, 408 insertions(+), 348 deletions(-)
 create mode 100644 block/qapi.c
 create mode 100644 include/block/qapi.h
block.c +0 -185
index 2561800..ca28c0d 100644
@@ -3089,128 +3089,6 @@ int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
     return data.ret;
 }
 
-BlockInfo *bdrv_query_info(BlockDriverState *bs)
-{
-    BlockInfo *info = g_malloc0(sizeof(*info));
-    info->device = g_strdup(bs->device_name);
-    info->type = g_strdup("unknown");
-    info->locked = bdrv_dev_is_medium_locked(bs);
-    info->removable = bdrv_dev_has_removable_media(bs);
-
-    if (bdrv_dev_has_removable_media(bs)) {
-        info->has_tray_open = true;
-        info->tray_open = bdrv_dev_is_tray_open(bs);
-    }
-
-    if (bdrv_iostatus_is_enabled(bs)) {
-        info->has_io_status = true;
-        info->io_status = bs->iostatus;
-    }
-
-    if (bs->dirty_bitmap) {
-        info->has_dirty = true;
-        info->dirty = g_malloc0(sizeof(*info->dirty));
-        info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
-        info->dirty->granularity =
-            ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
-    }
-
-    if (bs->drv) {
-        info->has_inserted = true;
-        info->inserted = g_malloc0(sizeof(*info->inserted));
-        info->inserted->file = g_strdup(bs->filename);
-        info->inserted->ro = bs->read_only;
-        info->inserted->drv = g_strdup(bs->drv->format_name);
-        info->inserted->encrypted = bs->encrypted;
-        info->inserted->encryption_key_missing = bdrv_key_required(bs);
-
-        if (bs->backing_file[0]) {
-            info->inserted->has_backing_file = true;
-            info->inserted->backing_file = g_strdup(bs->backing_file);
-        }
-
-        info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
-
-        if (bs->io_limits_enabled) {
-            info->inserted->bps =
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
-            info->inserted->bps_rd =
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
-            info->inserted->bps_wr =
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
-            info->inserted->iops =
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
-            info->inserted->iops_rd =
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
-            info->inserted->iops_wr =
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
-        }
-    }
-    return info;
-}
-
-BlockInfoList *qmp_query_block(Error **errp)
-{
-    BlockInfoList *head = NULL, **p_next = &head;
-    BlockDriverState *bs;
-
-    QTAILQ_FOREACH(bs, &bdrv_states, list) {
-        BlockInfoList *info = g_malloc0(sizeof(*info));
-        info->value = bdrv_query_info(bs);
-
-        *p_next = info;
-        p_next = &info->next;
-    }
-
-    return head;
-}
-
-BlockStats *bdrv_query_stats(const BlockDriverState *bs)
-{
-    BlockStats *s;
-
-    s = g_malloc0(sizeof(*s));
-
-    if (bs->device_name[0]) {
-        s->has_device = true;
-        s->device = g_strdup(bs->device_name);
-    }
-
-    s->stats = g_malloc0(sizeof(*s->stats));
-    s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
-    s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
-    s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
-    s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
-    s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
-    s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
-    s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
-    s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
-    s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
-
-    if (bs->file) {
-        s->has_parent = true;
-        s->parent = bdrv_query_stats(bs->file);
-    }
-
-    return s;
-}
-
-BlockStatsList *qmp_query_blockstats(Error **errp)
-{
-    BlockStatsList *head = NULL, **p_next = &head;
-    BlockDriverState *bs;
-
-    QTAILQ_FOREACH(bs, &bdrv_states, list) {
-        BlockStatsList *info = g_malloc0(sizeof(*info));
-        info->value = bdrv_query_stats(bs);
-
-        *p_next = info;
-        p_next = &info->next;
-    }
-
-    return head;
-}
-
 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
 {
     if (bs->backing_hd && bs->backing_hd->encrypted)
@@ -3441,69 +3319,6 @@ BlockDriverState *bdrv_find_base(BlockDriverState *bs)
     return curr_bs;
 }
 
-#define NB_SUFFIXES 4
-
-char *get_human_readable_size(char *buf, int buf_size, int64_t size)
-{
-    static const char suffixes[NB_SUFFIXES] = "KMGT";
-    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;
-}
-
-char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
-{
-    char buf1[128], date_buf[128], clock_buf[128];
-    struct tm tm;
-    time_t ti;
-    int64_t secs;
-
-    if (!sn) {
-        snprintf(buf, buf_size,
-                 "%-10s%-20s%7s%20s%15s",
-                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
-    } else {
-        ti = sn->date_sec;
-        localtime_r(&ti, &tm);
-        strftime(date_buf, sizeof(date_buf),
-                 "%Y-%m-%d %H:%M:%S", &tm);
-        secs = sn->vm_clock_nsec / 1000000000;
-        snprintf(clock_buf, sizeof(clock_buf),
-                 "%02d:%02d:%02d.%03d",
-                 (int)(secs / 3600),
-                 (int)((secs / 60) % 60),
-                 (int)(secs % 60),
-                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
-        snprintf(buf, buf_size,
-                 "%-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);
-    }
-    return buf;
-}
-
 /**************************************************************/
 /* async I/Os */
 
block/Makefile.objs +1 -1
index 8670999..2981654 100644
@@ -4,7 +4,7 @@ block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-obj-y += qed-check.o
 block-obj-y += vhdx.o
 block-obj-y += parallels.o blkdebug.o blkverify.o
-block-obj-y += snapshot.o
+block-obj-y += snapshot.o qapi.o
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += raw-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
block/qapi.c new file +360 -0

360 lines not on this page · raw message

include/block/block.h +0 -10
index fde6fb8..35f7958 100644
@@ -7,11 +7,6 @@
 #include "block/coroutine.h"
 #include "qapi/qmp/qobject.h"
 #include "qapi-types.h"
-/*
- * snapshot.h is needed since bdrv_snapshot_dump(), it can be removed when the
- * function is moved to other file.
- */
-#include "block/snapshot.h"
 
 /* block.c */
 typedef struct BlockDriver BlockDriver;
@@ -322,12 +317,7 @@ void bdrv_get_backing_filename(BlockDriverState *bs,
                                char *filename, int filename_size);
 void bdrv_get_full_backing_filename(BlockDriverState *bs,
                                     char *dest, size_t sz);
-BlockInfo *bdrv_query_info(BlockDriverState *s);
-BlockStats *bdrv_query_stats(const BlockDriverState *bs);
-
-char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
 
-char *get_human_readable_size(char *buf, int buf_size, int64_t size);
 int path_is_absolute(const char *path);
 void path_combine(char *dest, int dest_size,
                   const char *base_path,
include/block/block_int.h +1 -0
index 6078dd3..ba52247 100644
@@ -33,6 +33,7 @@
 #include "qapi/qmp/qerror.h"
 #include "monitor/monitor.h"
 #include "qemu/hbitmap.h"
+#include "block/snapshot.h"
 
 #define BLOCK_FLAG_ENCRYPT          1
 #define BLOCK_FLAG_COMPAT6          4
include/block/qapi.h new file +41 -0

41 lines not on this page · raw message

qemu-img.c +4 -152

186 lines not on this page · raw message

savevm.c +1 -0
index ab53a02..f988e89 100644
@@ -41,6 +41,7 @@
 #include "qemu/bitops.h"
 #include "qemu/iov.h"
 #include "block/snapshot.h"
+#include "block/qapi.h"
 
 #define SELF_ANNOUNCE_ROUNDS 5
 
Signature · 1 line
1.7.1
#5 lore raw

PATCH v2 4/5 util: add new function message_printf()

In reply to Wenchao Xia

This function takes an input parameter *output, which can be specified by caller as stderr, stdout or a monitor. error_vprintf() now calls message_vprintf(), which is a static function added in this patch.

---
 include/qemu/error-report.h |   13 +++++++++++++
 util/qemu-error.c           |   28 ++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 2 deletions(-)
include/qemu/error-report.h +13 -0
index c902cc1..cdde78b 100644
@@ -14,6 +14,8 @@
 #define QEMU_ERROR_H
 
 #include <stdarg.h>
+#include <stdio.h>
+#include "qemu/typedefs.h"
 
 typedef struct Location {
     /* all members are private to qemu-error.c */
@@ -32,6 +34,17 @@ void loc_set_none(void);
 void loc_set_cmdline(char **argv, int idx, int cnt);
 void loc_set_file(const char *fname, int lno);
 
+typedef struct QemuOutput {
+    enum { OUTPUT_STREAM, OUTPUT_MONITOR } kind;
+    union {
+        FILE *stream;
+        Monitor *monitor;
+    };
+} QemuOutput;
+
+void message_printf(const QemuOutput *output, const char *fmt, ...)
+                    GCC_FMT_ATTR(2, 3);
+
 void error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
 void error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 void error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
util/qemu-error.c +26 -2
index 08a36f4..c7ff0a8 100644
@@ -13,6 +13,25 @@
 #include <stdio.h>
 #include "monitor/monitor.h"
 
+static GCC_FMT_ATTR(2, 0)
+void message_vprintf(const QemuOutput *output, const char *fmt, va_list ap)
+{
+    if (output->kind == OUTPUT_STREAM) {
+        vfprintf(output->stream, fmt, ap);
+    } else if (output->kind == OUTPUT_MONITOR) {
+        monitor_vprintf(output->monitor, fmt, ap);
+    }
+}
+
+void message_printf(const QemuOutput *output, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    message_vprintf(output, fmt, ap);
+    va_end(ap);
+}
+
 /*
  * Print to current monitor if we have one, else to stderr.
  * TODO should return int, so callers can calculate width, but that
@@ -20,11 +39,16 @@
  */
 void error_vprintf(const char *fmt, va_list ap)
 {
+    QemuOutput output;
+
     if (cur_mon) {
-        monitor_vprintf(cur_mon, fmt, ap);
+        output.kind = OUTPUT_MONITOR;
+        output.monitor = cur_mon;
     } else {
-        vfprintf(stderr, fmt, ap);
+        output.kind = OUTPUT_STREAM;
+        output.stream = stderr;
     }
+    message_vprintf(&output, fmt, ap);
 }
 
 /*
Signature · 1 line
1.7.1
Reviewed-by on v2 4/5
#6 lore raw

Re: [Qemu-devel] [PATCH V2 4/5] util: add new function message_printf()

Quoted 13 lines · Wenchao Xia

This function takes an input parameter *output, which can be specified by caller as stderr, stdout or a monitor. error_vprintf() now calls message_vprintf(), which is a static function added in this patch.

---
 include/qemu/error-report.h |   13 +++++++++++++
 util/qemu-error.c           |   28 ++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 2 deletions(-)

+++ b/util/qemu-error.c

util/qemu-error.c +0 -0
@@ -13,6 +13,25 @@
 #include <stdio.h>

util/qemu-error.c line 23

Quoted 10 lines
util/qemu-error.c +8 -0
 #include "monitor/monitor.h"
 
+static GCC_FMT_ATTR(2, 0)
+void message_vprintf(const QemuOutput *output, const char *fmt, va_list ap)
+{
+    if (output->kind == OUTPUT_STREAM) {
+        vfprintf(output->stream, fmt, ap);
+    } else if (output->kind == OUTPUT_MONITOR) {
+        monitor_vprintf(output->monitor, fmt, ap);
+    }

Should you use a switch statement here, instead of open coding all possible enum values? But that's cosmetic.

More importantly, I think this function should return an int, whose value is the value of vfprintf. On the monitor_vfprintf arm, it could return 0 for now (or, you could unravel THAT problem and fix monitor_vfprintf to return an output count, but that sounds like a bigger task).

util/qemu-error.c line 32

Quoted 9 lines
util/qemu-error.c +9 -0
+}
+
+void message_printf(const QemuOutput *output, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    message_vprintf(output, fmt, ap);
+    va_end(ap);

This function should also return int.

util/qemu-error.c line 37

Quoted 5 lines
util/qemu-error.c +2 -0
+}
+
 /*
  * Print to current monitor if we have one, else to stderr.
  * TODO should return int, so callers can calculate width, but that

And by fixing the underlying function to return int, you could finally get rid of this TODO.

Given that the int return problem is pre-existing, and probably deserves its own series, I'm fine with taking this patch as-is.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw

Re: [Qemu-devel] [PATCH V2 4/5] util: add new function message_printf()

Quoted 55 lines · Eric Blake
Quoted 13 lines · Wenchao Xia

This function takes an input parameter *output, which can be specified by caller as stderr, stdout or a monitor. error_vprintf() now calls message_vprintf(), which is a static function added in this patch.

---
  include/qemu/error-report.h |   13 +++++++++++++
  util/qemu-error.c           |   28 ++++++++++++++++++++++++++--
  2 files changed, 39 insertions(+), 2 deletions(-)

+++ b/util/qemu-error.c

util/qemu-error.c +0 -0
@@ -13,6 +13,25 @@
  #include <stdio.h>

util/qemu-error.c line 23

Quoted 10 lines
util/qemu-error.c +8 -0
  #include "monitor/monitor.h"
 
+static GCC_FMT_ATTR(2, 0)
+void message_vprintf(const QemuOutput *output, const char *fmt, va_list ap)
+{
+    if (output->kind == OUTPUT_STREAM) {
+        vfprintf(output->stream, fmt, ap);
+    } else if (output->kind == OUTPUT_MONITOR) {
+        monitor_vprintf(output->monitor, fmt, ap);
+    }

Should you use a switch statement here, instead of open coding all possible enum values? But that's cosmetic.

More importantly, I think this function should return an int, whose value is the value of vfprintf. On the monitor_vfprintf arm, it could return 0 for now (or, you could unravel THAT problem and fix monitor_vfprintf to return an output count, but that sounds like a bigger task).

util/qemu-error.c line 32

Quoted 9 lines
util/qemu-error.c +9 -0
+}
+
+void message_printf(const QemuOutput *output, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    message_vprintf(output, fmt, ap);
+    va_end(ap);

This function should also return int.

util/qemu-error.c line 37

Quoted 5 lines
util/qemu-error.c +2 -0
+}
+
  /*
   * Print to current monitor if we have one, else to stderr.
   * TODO should return int, so callers can calculate width, but that

And by fixing the underlying function to return int, you could finally get rid of this TODO.

Quoted 3 lines

Given that the int return problem is pre-existing, and probably deserves its own series, I'm fine with taking this patch as-is.

It may not have much meaning of returning int now, since underlining
function do not support it so no caller can benefit from it. I think a series later for that is better, thanks for your reviewing.

Quoted 2 lines
Signature · 3 lines
Best Regards

Wenchao Xia
Reviewed-by on v2 4/5
#8 lore raw

Re: [Qemu-devel] [PATCH V2 4/5] util: add new function message_printf()

In reply to Wenchao Xia
Quoted 5 lines · On Thu, 23 May 2013 16:47:15 +0800 Wenchao Xia <[email protected]>…

This function takes an input parameter *output, which can be specified by caller as stderr, stdout or a monitor. error_vprintf() now calls message_vprintf(), which is a static function added in this patch.

This solution looks good to me:

Quoted 85 lines
---
 include/qemu/error-report.h |   13 +++++++++++++
 util/qemu-error.c           |   28 ++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 2 deletions(-)
include/qemu/error-report.h +13 -0
index c902cc1..cdde78b 100644
@@ -14,6 +14,8 @@
 #define QEMU_ERROR_H
 
 #include <stdarg.h>
+#include <stdio.h>
+#include "qemu/typedefs.h"
 
 typedef struct Location {
     /* all members are private to qemu-error.c */
@@ -32,6 +34,17 @@ void loc_set_none(void);
 void loc_set_cmdline(char **argv, int idx, int cnt);
 void loc_set_file(const char *fname, int lno);
 
+typedef struct QemuOutput {
+    enum { OUTPUT_STREAM, OUTPUT_MONITOR } kind;
+    union {
+        FILE *stream;
+        Monitor *monitor;
+    };
+} QemuOutput;
+
+void message_printf(const QemuOutput *output, const char *fmt, ...)
+                    GCC_FMT_ATTR(2, 3);
+
 void error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
 void error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 void error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
util/qemu-error.c +26 -2
index 08a36f4..c7ff0a8 100644
@@ -13,6 +13,25 @@
 #include <stdio.h>
 #include "monitor/monitor.h"
 
+static GCC_FMT_ATTR(2, 0)
+void message_vprintf(const QemuOutput *output, const char *fmt, va_list ap)
+{
+    if (output->kind == OUTPUT_STREAM) {
+        vfprintf(output->stream, fmt, ap);
+    } else if (output->kind == OUTPUT_MONITOR) {
+        monitor_vprintf(output->monitor, fmt, ap);
+    }
+}
+
+void message_printf(const QemuOutput *output, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    message_vprintf(output, fmt, ap);
+    va_end(ap);
+}
+
 /*
  * Print to current monitor if we have one, else to stderr.
  * TODO should return int, so callers can calculate width, but that
@@ -20,11 +39,16 @@
  */
 void error_vprintf(const char *fmt, va_list ap)
 {
+    QemuOutput output;
+
     if (cur_mon) {
-        monitor_vprintf(cur_mon, fmt, ap);
+        output.kind = OUTPUT_MONITOR;
+        output.monitor = cur_mon;
     } else {
-        vfprintf(stderr, fmt, ap);
+        output.kind = OUTPUT_STREAM;
+        output.stream = stderr;
     }
+    message_vprintf(&output, fmt, ap);
 }
 
 /*
#9 lore raw

Re: [Qemu-devel] [PATCH V2 4/5] util: add new function message_printf()

In reply to Wenchao Xia
Quoted 27 lines · Wenchao Xia

This function takes an input parameter *output, which can be specified by caller as stderr, stdout or a monitor. error_vprintf() now calls message_vprintf(), which is a static function added in this patch.

---
 include/qemu/error-report.h |   13 +++++++++++++
 util/qemu-error.c           |   28 ++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 2 deletions(-)
include/qemu/error-report.h +2 -0
index c902cc1..cdde78b 100644
@@ -14,6 +14,8 @@
 #define QEMU_ERROR_H
 
 #include <stdarg.h>
+#include <stdio.h>
+#include "qemu/typedefs.h"
 
 typedef struct Location {
     /* all members are private to qemu-error.c */
@@ -32,6 +34,17 @@ void loc_set_none(void);
 void loc_set_cmdline(char **argv, int idx, int cnt);
 void loc_set_file(const char *fname, int lno);
 

include/qemu/error-report.h void loc_set_none(void); · line 46

Quoted 10 lines
include/qemu/error-report.h +10 -0
+typedef struct QemuOutput {
+    enum { OUTPUT_STREAM, OUTPUT_MONITOR } kind;
+    union {
+        FILE *stream;
+        Monitor *monitor;
+    };
+} QemuOutput;
+
+void message_printf(const QemuOutput *output, const char *fmt, ...)
+                    GCC_FMT_ATTR(2, 3);

This is introducing a slightly different solution for fprintf_function, which is already widely used:

  $ git grep fprintf_function | wc -l
  101

Please reuse fprintf_function.

Stefan

#10 lore raw

PATCH v2 5/5 block: dump to specified output for bdrv_snapshot_dump() and bdrv_image_info_dump()

In reply to Wenchao Xia

Buffer is not used now so the string would not be truncated any more. They can be used by both qemu and qemu-img with correct parameter specified.

---
 block/qapi.c         |   65 +++++++++++++++++++++++++++-----------------------
 include/block/qapi.h |    5 ++-
 qemu-img.c           |   15 +++++++----
 savevm.c             |   11 ++++++--
 4 files changed, 55 insertions(+), 41 deletions(-)
block/qapi.c +35 -30
index 155e77e..2e083f8 100644
@@ -259,7 +259,7 @@ static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
     return buf;
 }
 
-char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
+void bdrv_snapshot_dump(const QemuOutput *output, QEMUSnapshotInfo *sn)
 {
     char buf1[128], date_buf[128], clock_buf[128];
     struct tm tm;
@@ -267,9 +267,9 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
     int64_t secs;
 
     if (!sn) {
-        snprintf(buf, buf_size,
-                 "%-10s%-20s%7s%20s%15s",
-                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
+        message_printf(output,
+                       "%-10s%-20s%7s%20s%15s",
+                       "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
     } else {
         ti = sn->date_sec;
         localtime_r(&ti, &tm);
@@ -282,17 +282,17 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
                  (int)((secs / 60) % 60),
                  (int)(secs % 60),
                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
-        snprintf(buf, buf_size,
-                 "%-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);
+        message_printf(output,
+                       "%-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);
     }
-    return buf;
 }
 
-void bdrv_image_info_dump(ImageInfo *info)
+void bdrv_image_info_dump(const QemuOutput *output, ImageInfo *info)
 {
     char size_buf[128], dsize_buf[128];
     if (!info->has_actual_size) {
@@ -302,43 +302,47 @@ void bdrv_image_info_dump(ImageInfo *info)
                                 info->actual_size);
     }
     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
-    printf("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);
+    message_printf(output,
+                   "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);
 
     if (info->has_encrypted && info->encrypted) {
-        printf("encrypted: yes\n");
+        message_printf(output, "encrypted: yes\n");
     }
 
     if (info->has_cluster_size) {
-        printf("cluster_size: %" PRId64 "\n", info->cluster_size);
+        message_printf(output, "cluster_size: %" PRId64 "\n",
+                       info->cluster_size);
     }
 
     if (info->has_dirty_flag && info->dirty_flag) {
-        printf("cleanly shut down: no\n");
+        message_printf(output, "cleanly shut down: no\n");
     }
 
     if (info->has_backing_filename) {
-        printf("backing file: %s", info->backing_filename);
+        message_printf(output, "backing file: %s", info->backing_filename);
         if (info->has_full_backing_filename) {
-            printf(" (actual path: %s)", info->full_backing_filename);
+            message_printf(output, " (actual path: %s)",
+                           info->full_backing_filename);
         }
-        putchar('\n');
+        message_printf(output, "\n");
         if (info->has_backing_filename_format) {
-            printf("backing file format: %s\n", info->backing_filename_format);
+            message_printf(output, "backing file format: %s\n",
+                           info->backing_filename_format);
         }
     }
 
     if (info->has_snapshots) {
         SnapshotInfoList *elem;
-        char buf[256];
 
-        printf("Snapshot list:\n");
-        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+        message_printf(output, "Snapshot list:\n");
+        bdrv_snapshot_dump(output, NULL);
+        message_printf(output, "\n");
 
         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
          * we convert to the block layer's native QEMUSnapshotInfo for now.
@@ -354,7 +358,8 @@ void bdrv_image_info_dump(ImageInfo *info)
 
             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
-            printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
+            bdrv_snapshot_dump(output, &sn);
+            message_printf(output, "\n");
         }
     }
 }
include/block/qapi.h +3 -2
index 55d1848..9c80118 100644
@@ -28,6 +28,7 @@
 #include "qapi-types.h"
 #include "block/block.h"
 #include "block/snapshot.h"
+#include "qemu/error-report.h"
 
 void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info);
 void bdrv_collect_image_info(BlockDriverState *bs,
@@ -36,6 +37,6 @@ void bdrv_collect_image_info(BlockDriverState *bs,
 BlockInfo *bdrv_query_info(BlockDriverState *s);
 BlockStats *bdrv_query_stats(const BlockDriverState *bs);
 
-char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
-void bdrv_image_info_dump(ImageInfo *info);
+void bdrv_snapshot_dump(const QemuOutput *output, QEMUSnapshotInfo *sn);
+void bdrv_image_info_dump(const QemuOutput *output, ImageInfo *info);
 #endif
qemu-img.c +9 -6
index 5d1e480..14901cf 100644
@@ -1554,16 +1554,18 @@ static void dump_snapshots(BlockDriverState *bs)
 {
     QEMUSnapshotInfo *sn_tab, *sn;
     int nb_sns, i;
-    char buf[256];
+    QemuOutput output = {OUTPUT_STREAM, {stdout,} };
 
     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
     if (nb_sns <= 0)
         return;
-    printf("Snapshot list:\n");
-    printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+    message_printf(&output, "Snapshot list:\n");
+    bdrv_snapshot_dump(&output, NULL);
+    message_printf(&output, "\n");
     for(i = 0; i < nb_sns; i++) {
         sn = &sn_tab[i];
-        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
+        bdrv_snapshot_dump(&output, sn);
+        message_printf(&output, "\n");
     }
     g_free(sn_tab);
 }
@@ -1606,14 +1608,15 @@ static void dump_human_image_info_list(ImageInfoList *list)
 {
     ImageInfoList *elem;
     bool delim = false;
+    QemuOutput output = {OUTPUT_STREAM, {stdout,} };
 
     for (elem = list; elem; elem = elem->next) {
         if (delim) {
-            printf("\n");
+            message_printf(&output, "\n");
         }
         delim = true;
 
-        bdrv_image_info_dump(elem->value);
+        bdrv_image_info_dump(&output, elem->value);
     }
 }
 
savevm.c +8 -3
index f988e89..93414e1 100644
@@ -2540,7 +2540,10 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict)
     int nb_sns, i, ret, available;
     int total;
     int *available_snapshots;
-    char buf[256];
+    QemuOutput output;
+
+    output.kind = OUTPUT_MONITOR;
+    output.monitor = mon;
 
     bs = find_vmstate_bs();
     if (!bs) {
@@ -2583,10 +2586,12 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict)
     }
 
     if (total > 0) {
-        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+        bdrv_snapshot_dump(&output, NULL);
+        monitor_printf(mon, "\n");
         for (i = 0; i < total; i++) {
             sn = &sn_tab[available_snapshots[i]];
-            monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
+            bdrv_snapshot_dump(&output, sn);
+            monitor_printf(mon, "\n");
         }
     } else {
         monitor_printf(mon, "There is no suitable snapshot available\n");
Signature · 1 line
1.7.1
#11 lore raw

Re: [Qemu-devel] [PATCH V2 5/5] block: dump to specified output for bdrv_snapshot_dump() and bdrv_image_info_dump()

Quoted 13 lines · Wenchao Xia

Buffer is not used now so the string would not be truncated any more. They can be used by both qemu and qemu-img with correct parameter specified.

---
 block/qapi.c         |   65 +++++++++++++++++++++++++++-----------------------
 include/block/qapi.h |    5 ++-
 qemu-img.c           |   15 +++++++----
 savevm.c             |   11 ++++++--
 4 files changed, 55 insertions(+), 41 deletions(-)

block/qapi.c char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) · line 285

Quoted 10 lines
block/qapi.c +1 -6
@@ -282,17 +282,17 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
                  (int)((secs / 60) % 60),
                  (int)(secs % 60),
                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
-        snprintf(buf, buf_size,
-                 "%-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);
+        message_printf(output,

You got rid of ONE buffer...

block/qapi.c char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) · line 288

Quoted 3 lines
block/qapi.c +3 -0
+                       "%-10s%-20s%7s%20s%15s",
+                       sn->id_str, sn->name,
+                       get_human_readable_size(buf1, sizeof(buf1),

...but what is this other buffer still doing? get_human_readable_size needs to be converted to use QemuOutput.

block/qapi.c char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) · line 297

Quoted 3 lines
block/qapi.c +1 -0
+void bdrv_image_info_dump(const QemuOutput *output, ImageInfo *info)
 {
     char size_buf[128], dsize_buf[128];

Why do we still need size_buf and dsize_buf?

block/qapi.c void bdrv_image_info_dump(ImageInfo *info) · line 304

Quoted 5 lines
block/qapi.c +0 -0
     if (!info->has_actual_size) {
@@ -302,43 +302,47 @@ void bdrv_image_info_dump(ImageInfo *info)
                                 info->actual_size);
     }
     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);

Again, get_human_readable_size should be converted to use QemuOutput.

qemu-img.c static void dump_snapshots(BlockDriverState *bs) · line 1557

Quoted 7 lines
qemu-img.c +2 -1
+++ b/qemu-img.c
@@ -1554,16 +1554,18 @@ static void dump_snapshots(BlockDriverState *bs)
 {
     QEMUSnapshotInfo *sn_tab, *sn;
     int nb_sns, i;
-    char buf[256];
+    QemuOutput output = {OUTPUT_STREAM, {stdout,} };

This is relying on C99's rule that a union is initialized by its first named member. But I think it might be more readable as:

output = { .kind = OUTPUT_STREAM, .stream = stdout };

not to mention that you will HAVE to use a designator to ever initialize the monitor element of the union in any parallel code that favors the monitor.

Hmm, does C99 even allow anonymous unions, or is that a gcc extension?

Overall, I like the direction this is headed. The conversion looks reasonable, although it didn't quite go far enough for getting rid of buffers.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#12 lore raw

Re: [Qemu-devel] [PATCH V2 5/5] block: dump to specified output for bdrv_snapshot_dump() and bdrv_image_info_dump()

Quoted 40 lines · Eric Blake
Quoted 13 lines · Wenchao Xia

Buffer is not used now so the string would not be truncated any more. They can be used by both qemu and qemu-img with correct parameter specified.

---
  block/qapi.c         |   65 +++++++++++++++++++++++++++-----------------------
  include/block/qapi.h |    5 ++-
  qemu-img.c           |   15 +++++++----
  savevm.c             |   11 ++++++--
  4 files changed, 55 insertions(+), 41 deletions(-)

block/qapi.c char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) · line 285

Quoted 10 lines
block/qapi.c +1 -6
@@ -282,17 +282,17 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
                   (int)((secs / 60) % 60),
                   (int)(secs % 60),
                   (int)((sn->vm_clock_nsec / 1000000) % 1000));
-        snprintf(buf, buf_size,
-                 "%-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);
+        message_printf(output,

You got rid of ONE buffer...

block/qapi.c char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) · line 288

Quoted 3 lines
block/qapi.c +3 -0
+                       "%-10s%-20s%7s%20s%15s",
+                       sn->id_str, sn->name,
+                       get_human_readable_size(buf1, sizeof(buf1),

...but what is this other buffer still doing? get_human_readable_size needs to be converted to use QemuOutput.

block/qapi.c char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) · line 297

Quoted 3 lines
block/qapi.c +1 -0
+void bdrv_image_info_dump(const QemuOutput *output, ImageInfo *info)
  {
      char size_buf[128], dsize_buf[128];

Why do we still need size_buf and dsize_buf?

Quoted 8 lines

block/qapi.c void bdrv_image_info_dump(ImageInfo *info) · line 304

Quoted 5 lines
block/qapi.c +0 -0
      if (!info->has_actual_size) {
@@ -302,43 +302,47 @@ void bdrv_image_info_dump(ImageInfo *info)
                                  info->actual_size);
      }
      get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);

Again, get_human_readable_size should be converted to use QemuOutput.

They do not likely have a chance to be truncated, so have not change
them. I will convert them also in next version.

Quoted 11 lines

qemu-img.c static void dump_snapshots(BlockDriverState *bs) · line 1557

Quoted 7 lines
qemu-img.c +2 -1
+++ b/qemu-img.c
@@ -1554,16 +1554,18 @@ static void dump_snapshots(BlockDriverState *bs)
  {
      QEMUSnapshotInfo *sn_tab, *sn;
      int nb_sns, i;
-    char buf[256];
+    QemuOutput output = {OUTPUT_STREAM, {stdout,} };

This is relying on C99's rule that a union is initialized by its first named member. But I think it might be more readable as:

Quoted 5 lines

output = { .kind = OUTPUT_STREAM, .stream = stdout };

not to mention that you will HAVE to use a designator to ever initialize the monitor element of the union in any parallel code that favors the monitor.

This solve the initialization issue, will use it, thank u for the tip.

Quoted 7 lines

Hmm, does C99 even allow anonymous unions, or is that a gcc extension?

Overall, I like the direction this is headed. The conversion looks reasonable, although it didn't quite go far enough for getting rid of buffers.

Signature · 3 lines
Best Regards

Wenchao Xia
#13 lore raw

Re: [Qemu-devel] [PATCH V2 5/5] block: dump to specified output for bdrv_snapshot_dump() and bdrv_image_info_dump()

Quoted 54 lines · Wenchao Xia
Quoted 44 lines · Eric Blake
Quoted 17 lines · Wenchao Xia

Buffer is not used now so the string would not be truncated any more. They can be used
by both qemu and qemu-img with correct parameter specified.

---
  block/qapi.c         |   65
+++++++++++++++++++++++++++-----------------------
  include/block/qapi.h |    5 ++-
  qemu-img.c           |   15 +++++++----
  savevm.c             |   11 ++++++--
  4 files changed, 55 insertions(+), 41 deletions(-)
block/qapi.c +0 -0
@@ -282,17 +282,17 @@ char *bdrv_snapshot_dump(char *buf, int
 buf_size, QEMUSnapshotInfo *sn)
                   (int)((secs / 60) % 60),

block/qapi.c char *bdrv_snapshot_dump(char *buf, int · line 287

Quoted 10 lines
block/qapi.c +1 -6
                   (int)(secs % 60),
                   (int)((sn->vm_clock_nsec / 1000000) % 1000));
-        snprintf(buf, buf_size,
-                 "%-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);
+        message_printf(output,

You got rid of ONE buffer...

block/qapi.c char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) · line 288

Quoted 3 lines
block/qapi.c +3 -0
+                       "%-10s%-20s%7s%20s%15s",
+                       sn->id_str, sn->name,
+                       get_human_readable_size(buf1, sizeof(buf1),

...but what is this other buffer still doing? get_human_readable_size needs to be converted to use QemuOutput.

block/qapi.c char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) · line 297

Quoted 3 lines
block/qapi.c +1 -0
+void bdrv_image_info_dump(const QemuOutput *output, ImageInfo *info)
  {
      char size_buf[128], dsize_buf[128];

Why do we still need size_buf and dsize_buf?

Quoted 9 lines

block/qapi.c void bdrv_image_info_dump(ImageInfo *info) · line 305

Quoted 6 lines
block/qapi.c +0 -0
      if (!info->has_actual_size) {
@@ -302,43 +302,47 @@ void bdrv_image_info_dump(ImageInfo *info)
                                  info->actual_size);
      }
      get_human_readable_size(size_buf, sizeof(size_buf),
 info->virtual_size);

Again, get_human_readable_size should be converted to use QemuOutput.

Quoted 3 lines

They do not likely have a chance to be truncated, so have not change
them. I will convert them also in next version.

Just found this buffer is used in format control:

        message_printf(output,
                       "%-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);

if get_human_readable_size() is converted, a manual control is
needed. Same thing happens to clock_buf. It seems better to keep what it is.

Quoted 27 lines
Quoted 11 lines

qemu-img.c static void dump_snapshots(BlockDriverState *bs) · line 1557

Quoted 7 lines
qemu-img.c +2 -1
+++ b/qemu-img.c
@@ -1554,16 +1554,18 @@ static void dump_snapshots(BlockDriverState *bs)
  {
      QEMUSnapshotInfo *sn_tab, *sn;
      int nb_sns, i;
-    char buf[256];
+    QemuOutput output = {OUTPUT_STREAM, {stdout,} };

This is relying on C99's rule that a union is initialized by its first named member. But I think it might be more readable as:

Quoted 5 lines

output = { .kind = OUTPUT_STREAM, .stream = stdout };

not to mention that you will HAVE to use a designator to ever initialize the monitor element of the union in any parallel code that favors the monitor.

This solve the initialization issue, will use it, thank u for the tip.

Quoted 7 lines

Hmm, does C99 even allow anonymous unions, or is that a gcc extension?

Overall, I like the direction this is headed. The conversion looks reasonable, although it didn't quite go far enough for getting rid of buffers.

Signature · 3 lines
Best Regards

Wenchao Xia