]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemuMonitorJSONGetAllBlockStatsInfo: Directly probe data from 'query-named-block...
authorPeter Krempa <pkrempa@redhat.com>
Wed, 1 Oct 2025 14:26:01 +0000 (16:26 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 21 Oct 2025 13:35:45 +0000 (15:35 +0200)
Currently the data which was probed for statistics from
'query-named-block-nodes' was updated in a separate call in
qemuMonitorJSONBlockStatsUpdateCapacityBlockdev.

This patch moves and adapts the code so that everything is probed in
qemuMonitorJSONGetAllBlockStatsInfo.

qemuMonitorJSONBlockStatsUpdateCapacityBlockdev is now an empty function
and will be removed later.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/qemu/qemu_monitor_json.c
tests/qemumonitorjsontest.c

index 7d061cbb97704a02b5c9ca0a7464c7d99730f93a..d5a8bd643a12bc04bebb6ce61f9e3094b64a44e6 100644 (file)
@@ -2524,6 +2524,42 @@ qemuMonitorJSONQueryBlockstats(qemuMonitor *mon,
 }
 
 
+static int
+qemuMonitorJSONGetOneBlockStatsNamedNodes(size_t pos G_GNUC_UNUSED,
+                                          virJSONValue *val,
+                                          void *opaque)
+{
+    GHashTable *stats = opaque;
+    virJSONValue *image;
+    const char *nodename;
+    qemuBlockStats *entry;
+
+    if (!(nodename = virJSONValueObjectGetString(val, "node-name")) ||
+        !(image = virJSONValueObjectGetObject(val, "image"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("query-named-block-nodes entry was not in expected format"));
+        return -1;
+    }
+
+    if (!(entry = virHashLookup(stats, nodename))) {
+        entry = qemuBlockStatsNew();
+        g_hash_table_insert(stats, g_strdup(nodename), entry);
+    }
+
+    /* updating actual size makes sense only when virtual size is present */
+    if (virJSONValueObjectGetNumberUlong(image, "virtual-size", &entry->capacity) == 0) {
+        /* if actual-size is missing, image is not thin provisioned */
+        if (virJSONValueObjectGetNumberUlong(image, "actual-size", &entry->physical) < 0)
+            entry->physical = entry->capacity;
+    }
+
+    ignore_value(virJSONValueObjectGetNumberUlong(val, "write_threshold",
+                                                  &entry->write_threshold));
+
+    return 1; /* we don't want to steal the value from the JSON array */
+}
+
+
 int
 qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitor *mon,
                                     GHashTable *hash)
@@ -2533,6 +2569,7 @@ qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitor *mon,
     size_t i;
     g_autoptr(virJSONValue) blockstatsDevices = NULL;
     g_autoptr(virJSONValue) blockstatsNodes = NULL;
+    g_autoptr(virJSONValue) nodes = NULL;
 
     if (!(blockstatsDevices = qemuMonitorJSONQueryBlockstats(mon, false)))
         return -1;
@@ -2580,59 +2617,22 @@ qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitor *mon,
             nstats = rc;
     }
 
-    return nstats;
-}
-
-
-static int
-qemuMonitorJSONBlockStatsUpdateCapacityBlockdevWorker(size_t pos G_GNUC_UNUSED,
-                                                      virJSONValue *val,
-                                                      void *opaque)
-{
-    GHashTable *stats = opaque;
-    virJSONValue *image;
-    const char *nodename;
-    qemuBlockStats *entry;
-
-    if (!(nodename = virJSONValueObjectGetString(val, "node-name")) ||
-        !(image = virJSONValueObjectGetObject(val, "image"))) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("query-named-block-nodes entry was not in expected format"));
+    if (!(nodes = qemuMonitorJSONQueryNamedBlockNodes(mon)))
         return -1;
-    }
-
-    if (!(entry = virHashLookup(stats, nodename))) {
-        entry = qemuBlockStatsNew();
-        g_hash_table_insert(stats, g_strdup(nodename), entry);
-    }
-
-    /* updating actual size makes sense only when virtual size is present */
-    if (virJSONValueObjectGetNumberUlong(image, "virtual-size", &entry->capacity) == 0) {
-        /* if actual-size is missing, image is not thin provisioned */
-        if (virJSONValueObjectGetNumberUlong(image, "actual-size", &entry->physical) < 0)
-            entry->physical = entry->capacity;
-    }
 
-    ignore_value(virJSONValueObjectGetNumberUlong(val, "write_threshold",
-                                                  &entry->write_threshold));
+    if (virJSONValueArrayForeachSteal(nodes,
+                                      qemuMonitorJSONGetOneBlockStatsNamedNodes,
+                                      hash) < 0)
+        return -1;
 
-    return 1; /* we don't want to steal the value from the JSON array */
+    return nstats;
 }
 
 
 int
-qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(qemuMonitor *mon,
-                                                GHashTable *stats)
+qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(qemuMonitor *mon G_GNUC_UNUSED,
+                                                GHashTable *stats G_GNUC_UNUSED)
 {
-    g_autoptr(virJSONValue) nodes = NULL;
-
-    if (!(nodes = qemuMonitorJSONQueryNamedBlockNodes(mon)))
-        return -1;
-
-    if (virJSONValueArrayForeachSteal(nodes,
-                                      qemuMonitorJSONBlockStatsUpdateCapacityBlockdevWorker,
-                                      stats) < 0)
-        return -1;
 
     return 0;
 }
index 7b56a507ea8a3ce0b8a35056a4ba5dbd218a16e4..a229a89860419f81e8fcb8e193b986a744799217 100644 (file)
@@ -1544,6 +1544,8 @@ testQemuMonitorJSONqemuMonitorJSONGetAllBlockStatsInfo(const void *opaque)
         return -1;
     if (qemuMonitorTestAddItem(test, "query-blockstats", reply) < 0)
         return -1;
+    if (qemuMonitorTestAddItem(test, "query-named-block-nodes", "{\"return\":[]}") < 0)
+        return -1;
 
 #define CHECK0FULL(var, value, varformat, valformat) \
     if (stats->var != value) { \