[libvirt] [PATCH v2 03/12] getstats: prepare monitor collection for recursion

Eric Blake eblake at redhat.com
Tue Dec 16 08:04:09 UTC 2014


A future patch will allow recursion into backing chains when
collecting block stats.  This patch should not change behavior,
but merely moves out the common code that will be reused once
recursion is enabled, and adds the parameter that will turn on
recursion.

* src/qemu/qemu_monitor.h (qemuMonitorGetAllBlockStatsInfo)
(qemuMonitorBlockStatsUpdateCapacity): Add recursion parameter,
although it is ignored for now.
* src/qemu/qemu_monitor.h (qemuMonitorGetAllBlockStatsInfo)
(qemuMonitorBlockStatsUpdateCapacity): Likewise.
* src/qemu/qemu_monitor_json.h
(qemuMonitorJSONGetAllBlockStatsInfo)
(qemuMonitorJSONBlockStatsUpdateCapacity): Likewise.
* src/qemu/qemu_monitor_json.c
(qemuMonitorJSONGetAllBlockStatsInfo)
(qemuMonitorJSONBlockStatsUpdateCapacity): Add parameter, and
split...
(qemuMonitorJSONGetOneBlockStatsInfo)
(qemuMonitorJSONBlockStatsUpdateCapacityOne): ...into helpers.
(qemuMonitorJSONGetBlockStatsInfo): Update caller.
* src/qemu/qemu_driver.c (qemuDomainGetStatsBlock): Update caller.
* src/qemu/qemu_migration.c (qemuMigrationCookieAddNBD): Likewise.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 src/qemu/qemu_driver.c       |   5 +-
 src/qemu/qemu_migration.c    |   3 +-
 src/qemu/qemu_monitor.c      |  24 ++--
 src/qemu/qemu_monitor.h      |   6 +-
 src/qemu/qemu_monitor_json.c | 257 +++++++++++++++++++++++++------------------
 src/qemu/qemu_monitor_json.h |   6 +-
 6 files changed, 175 insertions(+), 126 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index b13c5e1..7aaee96 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -18539,8 +18539,9 @@ qemuDomainGetStatsBlock(virQEMUDriverPtr driver,
         abbreviated = true; /* it's ok, just go ahead silently */
     } else {
         qemuDomainObjEnterMonitor(driver, dom);
-        rc = qemuMonitorGetAllBlockStatsInfo(priv->mon, &stats);
-        ignore_value(qemuMonitorBlockStatsUpdateCapacity(priv->mon, stats));
+        rc = qemuMonitorGetAllBlockStatsInfo(priv->mon, &stats, false);
+        ignore_value(qemuMonitorBlockStatsUpdateCapacity(priv->mon, stats,
+                                                         false));
         qemuDomainObjExitMonitor(driver, dom);

         if (rc < 0) {
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 0acbb57..834093d 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -571,7 +571,8 @@ qemuMigrationCookieAddNBD(qemuMigrationCookiePtr mig,
                 goto cleanup;

             qemuDomainObjEnterMonitor(driver, vm);
-            if (qemuMonitorBlockStatsUpdateCapacity(priv->mon, stats) < 0) {
+            if (qemuMonitorBlockStatsUpdateCapacity(priv->mon, stats,
+                                                    false) < 0) {
                 qemuDomainObjExitMonitor(driver, vm);
                 goto cleanup;
             }
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index c9c84f9..100bbd0 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -1783,16 +1783,16 @@ int qemuMonitorGetBlockStatsInfo(qemuMonitorPtr mon,
     return ret;
 }

-/* Fills the first 'nstats' block stats. 'stats' must be an array.
- * Returns <0 on error, otherwise the number of block stats retrieved.
- * if 'dev_name' is != NULL, look for this device only and skip
- * any other. In that case return value cannot be greater than 1.
+
+/* Creates a hash table in 'ret_stats' with all block stats.
+ * Returns <0 on error, 0 on success.
  */
 int
 qemuMonitorGetAllBlockStatsInfo(qemuMonitorPtr mon,
-                                virHashTablePtr *ret_stats)
+                                virHashTablePtr *ret_stats,
+                                bool backingChain)
 {
-    VIR_DEBUG("mon=%p ret_stats=%p", mon, ret_stats);
+    VIR_DEBUG("mon=%p ret_stats=%p, backing=%d", mon, ret_stats, backingChain);

     if (!mon->json) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -1800,15 +1800,17 @@ qemuMonitorGetAllBlockStatsInfo(qemuMonitorPtr mon,
         return -1;
     }

-    return qemuMonitorJSONGetAllBlockStatsInfo(mon, ret_stats);
+    return qemuMonitorJSONGetAllBlockStatsInfo(mon, ret_stats, backingChain);
 }


 /* Updates "stats" to fill virtual and physical size of the image */
-int qemuMonitorBlockStatsUpdateCapacity(qemuMonitorPtr mon,
-                                        virHashTablePtr stats)
+int
+qemuMonitorBlockStatsUpdateCapacity(qemuMonitorPtr mon,
+                                    virHashTablePtr stats,
+                                    bool backingChain)
 {
-    VIR_DEBUG("mon=%p, stats=%p", mon, stats);
+    VIR_DEBUG("mon=%p, stats=%p, backing=%d", mon, stats, backingChain);

     if (!mon->json) {
         virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
@@ -1816,7 +1818,7 @@ int qemuMonitorBlockStatsUpdateCapacity(qemuMonitorPtr mon,
         return -1;
     }

-    return qemuMonitorJSONBlockStatsUpdateCapacity(mon, stats);
+    return qemuMonitorJSONBlockStatsUpdateCapacity(mon, stats, backingChain);
 }


diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 21533a4..edab66f 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -382,11 +382,13 @@ struct _qemuBlockStats {
 };

 int qemuMonitorGetAllBlockStatsInfo(qemuMonitorPtr mon,
-                                    virHashTablePtr *ret_stats)
+                                    virHashTablePtr *ret_stats,
+                                    bool backingChain)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

 int qemuMonitorBlockStatsUpdateCapacity(qemuMonitorPtr mon,
-                                        virHashTablePtr stats)
+                                        virHashTablePtr stats,
+                                        bool backingChain)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

 int qemuMonitorGetBlockStatsParamsNumber(qemuMonitorPtr mon,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 162579b..3a13890 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -1627,7 +1627,7 @@ int qemuMonitorJSONGetBlockStatsInfo(qemuMonitorPtr mon,
     if (flush_total_times)
         *flush_total_times = -1;

-    if (qemuMonitorJSONGetAllBlockStatsInfo(mon, &blockstats) < 0)
+    if (qemuMonitorJSONGetAllBlockStatsInfo(mon, &blockstats, false) < 0)
         goto cleanup;

     if (!(stats = virHashLookup(blockstats, dev_name))) {
@@ -1693,16 +1693,112 @@ qemuMonitorJSONDevGetBlockExtent(virJSONValuePtr dev,
 }


-int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
-                                        virHashTablePtr *ret_stats)
+static int
+qemuMonitorJSONGetOneBlockStatsInfo(virJSONValuePtr dev,
+                                    const char *dev_name,
+                                    virHashTablePtr hash,
+                                    bool backingChain ATTRIBUTE_UNUSED)
 {
-    int ret = -1;
-    int rc;
-    size_t i;
-    virJSONValuePtr cmd;
-    virJSONValuePtr reply = NULL;
-    virJSONValuePtr devices;
     qemuBlockStatsPtr bstats = NULL;
+    virJSONValuePtr stats;
+    int ret = -1;
+
+    if (VIR_ALLOC(bstats) < 0)
+        goto cleanup;
+
+    if ((stats = virJSONValueObjectGet(dev, "stats")) == NULL ||
+        stats->type != VIR_JSON_TYPE_OBJECT) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("blockstats stats entry was not "
+                         "in expected format"));
+        goto cleanup;
+    }
+
+    if (virJSONValueObjectGetNumberLong(stats, "rd_bytes",
+                                        &bstats->rd_bytes) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot read %s statistic"),
+                       "rd_bytes");
+        goto cleanup;
+    }
+    if (virJSONValueObjectGetNumberLong(stats, "rd_operations",
+                                        &bstats->rd_req) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot read %s statistic"),
+                       "rd_operations");
+        goto cleanup;
+    }
+    if (virJSONValueObjectHasKey(stats, "rd_total_time_ns") &&
+        (virJSONValueObjectGetNumberLong(stats, "rd_total_time_ns",
+                                         &bstats->rd_total_times) < 0)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot read %s statistic"),
+                       "rd_total_time_ns");
+        goto cleanup;
+    }
+    if (virJSONValueObjectGetNumberLong(stats, "wr_bytes",
+                                        &bstats->wr_bytes) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot read %s statistic"),
+                       "wr_bytes");
+        goto cleanup;
+    }
+    if (virJSONValueObjectGetNumberLong(stats, "wr_operations",
+                                        &bstats->wr_req) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot read %s statistic"),
+                       "wr_operations");
+        goto cleanup;
+    }
+    if (virJSONValueObjectHasKey(stats, "wr_total_time_ns") &&
+        (virJSONValueObjectGetNumberLong(stats, "wr_total_time_ns",
+                                         &bstats->wr_total_times) < 0)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot read %s statistic"),
+                       "wr_total_time_ns");
+        goto cleanup;
+    }
+    if (virJSONValueObjectHasKey(stats, "flush_operations") &&
+        (virJSONValueObjectGetNumberLong(stats, "flush_operations",
+                                         &bstats->flush_req) < 0)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot read %s statistic"),
+                       "flush_operations");
+        goto cleanup;
+    }
+    if (virJSONValueObjectHasKey(stats, "flush_total_time_ns") &&
+        (virJSONValueObjectGetNumberLong(stats, "flush_total_time_ns",
+                                         &bstats->flush_total_times) < 0)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot read %s statistic"),
+                       "flush_total_time_ns");
+        goto cleanup;
+    }
+
+    /* it's ok to not have this information here. Just skip silently. */
+    qemuMonitorJSONDevGetBlockExtent(dev, &bstats->wr_highest_offset);
+
+    if (virHashAddEntry(hash, dev_name, bstats) < 0)
+        goto cleanup;
+    bstats = NULL;
+    ret = 0;
+ cleanup:
+    VIR_FREE(bstats);
+    return ret;
+}
+
+
+int
+qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
+                                    virHashTablePtr *ret_stats,
+                                    bool backingChain)
+{
+    int ret = -1;
+    int rc;
+    size_t i;
+    virJSONValuePtr cmd;
+    virJSONValuePtr reply = NULL;
+    virJSONValuePtr devices;
     virHashTablePtr hash = NULL;

     if (!(cmd = qemuMonitorJSONMakeCommand("query-blockstats", NULL)))
@@ -1726,12 +1822,8 @@ int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,

     for (i = 0; i < virJSONValueArraySize(devices); i++) {
         virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
-        virJSONValuePtr stats;
         const char *dev_name;

-        if (VIR_ALLOC(bstats) < 0)
-            goto cleanup;
-
         if (!dev || dev->type != VIR_JSON_TYPE_OBJECT) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("blockstats device entry was not "
@@ -1749,81 +1841,10 @@ int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
         if (STRPREFIX(dev_name, QEMU_DRIVE_HOST_PREFIX))
             dev_name += strlen(QEMU_DRIVE_HOST_PREFIX);

-        if ((stats = virJSONValueObjectGet(dev, "stats")) == NULL ||
-            stats->type != VIR_JSON_TYPE_OBJECT) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("blockstats stats entry was not "
-                             "in expected format"));
+        if (qemuMonitorJSONGetOneBlockStatsInfo(dev, dev_name, hash,
+                                                backingChain) < 0)
             goto cleanup;
-        }

-        if (virJSONValueObjectGetNumberLong(stats, "rd_bytes",
-                                            &bstats->rd_bytes) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                           "rd_bytes");
-            goto cleanup;
-        }
-        if (virJSONValueObjectGetNumberLong(stats, "rd_operations",
-                                            &bstats->rd_req) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                            "rd_operations");
-            goto cleanup;
-        }
-        if (virJSONValueObjectHasKey(stats, "rd_total_time_ns") &&
-            (virJSONValueObjectGetNumberLong(stats, "rd_total_time_ns",
-                                             &bstats->rd_total_times) < 0)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                           "rd_total_time_ns");
-            goto cleanup;
-        }
-        if (virJSONValueObjectGetNumberLong(stats, "wr_bytes",
-                                            &bstats->wr_bytes) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                           "wr_bytes");
-            goto cleanup;
-        }
-        if (virJSONValueObjectGetNumberLong(stats, "wr_operations",
-                                            &bstats->wr_req) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                           "wr_operations");
-            goto cleanup;
-        }
-        if (virJSONValueObjectHasKey(stats, "wr_total_time_ns") &&
-            (virJSONValueObjectGetNumberLong(stats, "wr_total_time_ns",
-                                             &bstats->wr_total_times) < 0)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                           "wr_total_time_ns");
-            goto cleanup;
-        }
-        if (virJSONValueObjectHasKey(stats, "flush_operations") &&
-            (virJSONValueObjectGetNumberLong(stats, "flush_operations",
-                                             &bstats->flush_req) < 0)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                           "flush_operations");
-            goto cleanup;
-        }
-        if (virJSONValueObjectHasKey(stats, "flush_total_time_ns") &&
-            (virJSONValueObjectGetNumberLong(stats, "flush_total_time_ns",
-                                             &bstats->flush_total_times) < 0)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                           "flush_total_time_ns");
-            goto cleanup;
-        }
-
-        /* it's ok to not have this information here. Just skip silently. */
-        qemuMonitorJSONDevGetBlockExtent(dev, &bstats->wr_highest_offset);
-
-        if (virHashAddEntry(hash, dev_name, bstats) < 0)
-            goto cleanup;
-        bstats = NULL;
     }

     *ret_stats = hash;
@@ -1831,7 +1852,6 @@ int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
     ret = 0;

  cleanup:
-    VIR_FREE(bstats);
     virHashFree(hash);
     virJSONValueFree(cmd);
     virJSONValueFree(reply);
@@ -1839,8 +1859,45 @@ int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
 }


-int qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
-                                            virHashTablePtr stats)
+static int
+qemuMonitorJSONBlockStatsUpdateCapacityOne(virJSONValuePtr image,
+                                           const char *dev_name,
+                                           virHashTablePtr stats,
+                                           bool backingChain ATTRIBUTE_UNUSED)
+{
+    qemuBlockStatsPtr bstats;
+    int ret = -1;
+
+    if (!(bstats = virHashLookup(stats, dev_name))) {
+        if (VIR_ALLOC(bstats) < 0)
+            goto cleanup;
+
+        if (virHashAddEntry(stats, dev_name, bstats) < 0) {
+            VIR_FREE(bstats);
+            goto cleanup;
+        }
+    }
+
+    /* After this point, we ignore failures; the stats were
+     * zero-initialized when created which is a sane fallback.  */
+    ret = 0;
+    if (virJSONValueObjectGetNumberUlong(image, "virtual-size",
+                                         &bstats->capacity) < 0)
+        goto cleanup;
+
+    /* if actual-size is missing, image is not thin provisioned */
+    if (virJSONValueObjectGetNumberUlong(image, "actual-size",
+                                         &bstats->physical) < 0)
+        bstats->physical = bstats->capacity;
+ cleanup:
+    return ret;
+}
+
+
+int
+qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
+                                        virHashTablePtr stats,
+                                        bool backingChain)
 {
     int ret = -1;
     int rc;
@@ -1869,7 +1926,6 @@ int qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
         virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
         virJSONValuePtr inserted;
         virJSONValuePtr image;
-        qemuBlockStatsPtr bstats;
         const char *dev_name;

         if (!dev || dev->type != VIR_JSON_TYPE_OBJECT) {
@@ -1894,24 +1950,9 @@ int qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
             !(image = virJSONValueObjectGet(inserted, "image")))
             continue;

-        if (!(bstats = virHashLookup(stats, dev_name))) {
-            if (VIR_ALLOC(bstats) < 0)
-                goto cleanup;
-
-            if (virHashAddEntry(stats, dev_name, bstats) < 0) {
-                VIR_FREE(bstats);
-                goto cleanup;
-            }
-        }
-
-        if (virJSONValueObjectGetNumberUlong(image, "virtual-size",
-                                             &bstats->capacity) < 0)
-            continue;
-
-        /* if actual-size is missing, image is not thin provisioned */
-        if (virJSONValueObjectGetNumberUlong(image, "actual-size",
-                                             &bstats->physical) < 0)
-            bstats->physical = bstats->capacity;
+        if (qemuMonitorJSONBlockStatsUpdateCapacityOne(image, dev_name, stats,
+                                                       backingChain) < 0)
+            goto cleanup;
     }

     ret = 0;
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index ae20fb1..222f11e 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -80,9 +80,11 @@ int qemuMonitorJSONGetBlockStatsInfo(qemuMonitorPtr mon,
                                      long long *flush_total_times,
                                      long long *errs);
 int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
-                                        virHashTablePtr *ret_stats);
+                                        virHashTablePtr *ret_stats,
+                                        bool backingChain);
 int qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
-                                            virHashTablePtr stats);
+                                            virHashTablePtr stats,
+                                            bool backingChain);
 int qemuMonitorJSONGetBlockStatsParamsNumber(qemuMonitorPtr mon,
                                              int *nparams);
 int qemuMonitorJSONGetBlockExtent(qemuMonitorPtr mon,
-- 
1.9.3




More information about the libvir-list mailing list