render / rpms / libvirt

Forked from rpms/libvirt 11 months ago
Clone
9119d9
From b2e3b9d00f8e0210132f412ef5cee8367930d440 Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <b2e3b9d00f8e0210132f412ef5cee8367930d440@dist-git>
9119d9
From: Peter Krempa <pkrempa@redhat.com>
9119d9
Date: Wed, 1 Oct 2014 11:20:23 +0200
9119d9
Subject: [PATCH] qemu: monitor: Add helper function to fill physical/virtual
9119d9
 image size
9119d9
9119d9
https://bugzilla.redhat.com/show_bug.cgi?id=1113116
9119d9
9119d9
While our code gathers block stats via "query-blockstats" some
9119d9
information need to be gathered via "query-block". Add a helper function
9119d9
that will update the blockstats structure if requested.
9119d9
9119d9
(cherry picked from commit 8caded6b8ec567eadf5a339ba221d5de3b03a509)
9119d9
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 src/qemu/qemu_monitor.c      | 17 ++++++++++
9119d9
 src/qemu/qemu_monitor.h      |  6 ++++
9119d9
 src/qemu/qemu_monitor_json.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
9119d9
 src/qemu/qemu_monitor_json.h |  2 ++
9119d9
 4 files changed, 103 insertions(+)
9119d9
9119d9
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
9119d9
index 93e8f22..8e14366 100644
9119d9
--- a/src/qemu/qemu_monitor.c
9119d9
+++ b/src/qemu/qemu_monitor.c
9119d9
@@ -1780,6 +1780,23 @@ qemuMonitorGetAllBlockStatsInfo(qemuMonitorPtr mon,
9119d9
     return qemuMonitorJSONGetAllBlockStatsInfo(mon, ret_stats);
9119d9
 }
9119d9
 
9119d9
+
9119d9
+/* Updates "stats" to fill virtual and physical size of the image */
9119d9
+int qemuMonitorBlockStatsUpdateCapacity(qemuMonitorPtr mon,
9119d9
+                                        virHashTablePtr stats)
9119d9
+{
9119d9
+    VIR_DEBUG("mon=%p, stats=%p", mon, stats);
9119d9
+
9119d9
+    if (!mon->json) {
9119d9
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
9119d9
+                       _("block capacity/size info requires JSON monitor"));
9119d9
+        return -1;
9119d9
+    }
9119d9
+
9119d9
+    return qemuMonitorJSONBlockStatsUpdateCapacity(mon, stats);
9119d9
+}
9119d9
+
9119d9
+
9119d9
 /* Return 0 and update @nparams with the number of block stats
9119d9
  * QEMU supports if success. Return -1 if failure.
9119d9
  */
9119d9
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
9119d9
index 2551272..9c798cf 100644
9119d9
--- a/src/qemu/qemu_monitor.h
9119d9
+++ b/src/qemu/qemu_monitor.h
9119d9
@@ -358,12 +358,18 @@ struct _qemuBlockStats {
9119d9
     long long wr_total_times;
9119d9
     long long flush_req;
9119d9
     long long flush_total_times;
9119d9
+    unsigned long long capacity;
9119d9
+    unsigned long long physical;
9119d9
 };
9119d9
 
9119d9
 int qemuMonitorGetAllBlockStatsInfo(qemuMonitorPtr mon,
9119d9
                                     virHashTablePtr *ret_stats)
9119d9
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
9119d9
 
9119d9
+int qemuMonitorBlockStatsUpdateCapacity(qemuMonitorPtr mon,
9119d9
+                                        virHashTablePtr stats)
9119d9
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
9119d9
+
9119d9
 int qemuMonitorGetBlockStatsParamsNumber(qemuMonitorPtr mon,
9119d9
                                          int *nparams);
9119d9
 
9119d9
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
9119d9
index 2b23347..b0a9c99 100644
9119d9
--- a/src/qemu/qemu_monitor_json.c
9119d9
+++ b/src/qemu/qemu_monitor_json.c
9119d9
@@ -1899,6 +1899,84 @@ int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
9119d9
 }
9119d9
 
9119d9
 
9119d9
+int qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
9119d9
+                                            virHashTablePtr stats)
9119d9
+{
9119d9
+    int ret = -1;
9119d9
+    int rc;
9119d9
+    size_t i;
9119d9
+    virJSONValuePtr cmd;
9119d9
+    virJSONValuePtr reply = NULL;
9119d9
+    virJSONValuePtr devices;
9119d9
+
9119d9
+    if (!(cmd = qemuMonitorJSONMakeCommand("query-block", NULL)))
9119d9
+        return -1;
9119d9
+
9119d9
+    if ((rc = qemuMonitorJSONCommand(mon, cmd, &reply)) < 0)
9119d9
+        goto cleanup;
9119d9
+
9119d9
+    if (qemuMonitorJSONCheckError(cmd, reply) < 0)
9119d9
+        goto cleanup;
9119d9
+
9119d9
+    devices = virJSONValueObjectGet(reply, "return");
9119d9
+    if (!devices || devices->type != VIR_JSON_TYPE_ARRAY) {
9119d9
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
+                       _("query-block reply was missing device list"));
9119d9
+        goto cleanup;
9119d9
+    }
9119d9
+
9119d9
+    for (i = 0; i < virJSONValueArraySize(devices); i++) {
9119d9
+        virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
9119d9
+        virJSONValuePtr inserted;
9119d9
+        virJSONValuePtr image;
9119d9
+        qemuBlockStatsPtr bstats;
9119d9
+        const char *devname;
9119d9
+
9119d9
+        if (!dev || dev->type != VIR_JSON_TYPE_OBJECT) {
9119d9
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
+                           _("query-block device entry was not "
9119d9
+                             "in expected format"));
9119d9
+            goto cleanup;
9119d9
+        }
9119d9
+
9119d9
+        if (!(devname = virJSONValueObjectGetString(dev, "device"))) {
9119d9
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
+                           _("query-block device entry was not "
9119d9
+                             "in expected format"));
9119d9
+            goto cleanup;
9119d9
+        }
9119d9
+
9119d9
+        if (STRPREFIX(devname, QEMU_DRIVE_HOST_PREFIX))
9119d9
+            devname += strlen(QEMU_DRIVE_HOST_PREFIX);
9119d9
+
9119d9
+        /* ignore missing info */
9119d9
+        if (!(bstats = virHashLookup(stats, devname)))
9119d9
+            continue;
9119d9
+
9119d9
+        /* drive may be empty */
9119d9
+        if (!(inserted = virJSONValueObjectGet(dev, "inserted")) ||
9119d9
+            !(image = virJSONValueObjectGet(inserted, "image")))
9119d9
+            continue;
9119d9
+
9119d9
+        if (virJSONValueObjectGetNumberUlong(image, "virtual-size",
9119d9
+                                             &bstats->capacity) < 0)
9119d9
+            continue;
9119d9
+
9119d9
+        /* if actual-size is missing, image is not thin provisioned */
9119d9
+        if (virJSONValueObjectGetNumberUlong(image, "actual-size",
9119d9
+                                             &bstats->physical) < 0)
9119d9
+            bstats->physical = bstats->capacity;
9119d9
+    }
9119d9
+
9119d9
+    ret = 0;
9119d9
+
9119d9
+ cleanup:
9119d9
+    virJSONValueFree(cmd);
9119d9
+    virJSONValueFree(reply);
9119d9
+    return ret;
9119d9
+}
9119d9
+
9119d9
+
9119d9
 int qemuMonitorJSONGetBlockStatsParamsNumber(qemuMonitorPtr mon,
9119d9
                                              int *nparams)
9119d9
 {
9119d9
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
9119d9
index 8e65c4c..289bd11 100644
9119d9
--- a/src/qemu/qemu_monitor_json.h
9119d9
+++ b/src/qemu/qemu_monitor_json.h
9119d9
@@ -81,6 +81,8 @@ int qemuMonitorJSONGetBlockStatsInfo(qemuMonitorPtr mon,
9119d9
                                      long long *errs);
9119d9
 int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
9119d9
                                         virHashTablePtr *ret_stats);
9119d9
+int qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
9119d9
+                                            virHashTablePtr stats);
9119d9
 int qemuMonitorJSONGetBlockStatsParamsNumber(qemuMonitorPtr mon,
9119d9
                                              int *nparams);
9119d9
 int qemuMonitorJSONGetBlockExtent(qemuMonitorPtr mon,
9119d9
-- 
9119d9
2.1.2
9119d9