This patch adds a new worker qemuDomainGetStatsVm which reports the
stats returned by "query-stats" via qemuMonitorQueryStats for the VM
target.

Signed-off-by: Amneesh Singh <na...@weirdnatto.in>
---
 include/libvirt/libvirt-domain.h |  1 +
 src/libvirt-domain.c             |  3 +++
 src/qemu/qemu_driver.c           | 41 ++++++++++++++++++++++++++++++++
 tools/virsh-domain-monitor.c     |  7 ++++++
 4 files changed, 52 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 7430a08619..3b25c8dd77 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -2706,6 +2706,7 @@ typedef enum {
     VIR_DOMAIN_STATS_IOTHREAD = (1 << 7), /* return iothread poll info (Since: 
4.10.0) */
     VIR_DOMAIN_STATS_MEMORY = (1 << 8), /* return domain memory info (Since: 
6.0.0) */
     VIR_DOMAIN_STATS_DIRTYRATE = (1 << 9), /* return domain dirty rate info 
(Since: 7.2.0) */
+    VIR_DOMAIN_STATS_VM = (1 << 10), /* return vm info (Since: 8.6.0) */
 } virDomainStatsTypes;
 
 /**
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index 80a6f0f269..52fa136186 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -12481,6 +12481,9 @@ virConnectGetDomainCapabilities(virConnectPtr conn,
  *                                                   rate for a virtual cpu as
  *                                                   unsigned long long.
  *
+ * VIR_DOMAIN_STATS_VM:
+ *     Return fd-based KVM statistics for the target VM
+ *
  * Note that entire stats groups or individual stat fields may be missing from
  * the output in case they are not supported by the given hypervisor, are not
  * applicable for the current state of the guest domain, or their retrieval
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 79146b6bb8..ce4d4a0f03 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -18727,6 +18727,41 @@ qemuDomainGetStatsDirtyRate(virQEMUDriver *driver 
G_GNUC_UNUSED,
     return 0;
 }
 
+
+static int
+qemuDomainGetStatsVm(virQEMUDriver *driver G_GNUC_UNUSED,
+                     virDomainObj *dom,
+                     virTypedParamList *params,
+                     unsigned int privflags)
+{
+    qemuDomainObjPrivate *priv = dom->privateData;
+    g_autoptr(virJSONValue) queried_stats = NULL;
+    g_autoptr(GHashTable) stats = NULL;
+    virJSONValue *stats_obj = NULL;
+
+    if (!HAVE_JOB(privflags) || !virDomainObjIsActive(dom))
+        return 0;
+
+    if (qemuDomainRefreshStatsSchema(dom) < 0)
+        return 0;
+
+    qemuDomainObjEnterMonitor(dom);
+    queried_stats = qemuMonitorQueryStats(priv->mon,
+                                          QEMU_MONITOR_QUERY_STATS_TARGET_VM,
+                                          NULL, NULL);
+    qemuDomainObjExitMonitor(dom);
+
+    if (!queried_stats || virJSONValueArraySize(queried_stats) != 1)
+        return 0;
+
+    stats_obj = virJSONValueArrayGet(queried_stats, 0);
+    stats = qemuMonitorExtractQueryStats(stats_obj);
+
+    qemuDomainAddStatsFromHashTable(stats, priv->statsSchema, "vm", params);
+
+    return 0;
+}
+
 typedef int
 (*qemuDomainGetStatsFunc)(virQEMUDriver *driver,
                           virDomainObj *dom,
@@ -18751,6 +18786,11 @@ static virQEMUCapsFlags queryDirtyRateRequired[] = {
     QEMU_CAPS_LAST
 };
 
+static virQEMUCapsFlags queryVmRequired[] = {
+    QEMU_CAPS_QUERY_STATS,
+    QEMU_CAPS_LAST
+};
+
 static struct qemuDomainGetStatsWorker qemuDomainGetStatsWorkers[] = {
     { qemuDomainGetStatsState, VIR_DOMAIN_STATS_STATE, false, NULL },
     { qemuDomainGetStatsCpu, VIR_DOMAIN_STATS_CPU_TOTAL, true, NULL },
@@ -18762,6 +18802,7 @@ static struct qemuDomainGetStatsWorker 
qemuDomainGetStatsWorkers[] = {
     { qemuDomainGetStatsIOThread, VIR_DOMAIN_STATS_IOTHREAD, true, 
queryIOThreadRequired },
     { qemuDomainGetStatsMemory, VIR_DOMAIN_STATS_MEMORY, false, NULL },
     { qemuDomainGetStatsDirtyRate, VIR_DOMAIN_STATS_DIRTYRATE, true, 
queryDirtyRateRequired },
+    { qemuDomainGetStatsVm, VIR_DOMAIN_STATS_VM, true, queryVmRequired },
     { NULL, 0, false, NULL }
 };
 
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index dc5fe13e49..be8f827685 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -2065,6 +2065,10 @@ static const vshCmdOptDef opts_domstats[] = {
      .type = VSH_OT_BOOL,
      .help = N_("report domain dirty rate information"),
     },
+    {.name = "vm",
+     .type = VSH_OT_BOOL,
+     .help = N_("report fd-based VM statistics by KVM"),
+    },
     {.name = "list-active",
      .type = VSH_OT_BOOL,
      .help = N_("list only active domains"),
@@ -2186,6 +2190,9 @@ cmdDomstats(vshControl *ctl, const vshCmd *cmd)
     if (vshCommandOptBool(cmd, "dirtyrate"))
         stats |= VIR_DOMAIN_STATS_DIRTYRATE;
 
+    if (vshCommandOptBool(cmd, "vm"))
+        stats |= VIR_DOMAIN_STATS_VM;
+
     if (vshCommandOptBool(cmd, "list-active"))
         flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_ACTIVE;
 
-- 
2.37.1

Reply via email to