Blame SOURCES/libvirt-qemu-Implement-qemuDomainGetStatsCpu-fallback-for-qemu-session.patch

e9d682
From 5da85fb944db3dd8213a7302deaffa3b294acd64 Mon Sep 17 00:00:00 2001
e9d682
Message-Id: <5da85fb944db3dd8213a7302deaffa3b294acd64@dist-git>
e9d682
From: Michal Privoznik <mprivozn@redhat.com>
e9d682
Date: Tue, 9 Aug 2022 16:16:09 +0200
e9d682
Subject: [PATCH] qemu: Implement qemuDomainGetStatsCpu fallback for
e9d682
 qemu:///session
e9d682
MIME-Version: 1.0
e9d682
Content-Type: text/plain; charset=UTF-8
e9d682
Content-Transfer-Encoding: 8bit
e9d682
e9d682
For domains started under session URI, we don't set up CGroups
e9d682
(well, how could we since we're not running as root anyways).
e9d682
Nevertheless, fetching CPU statistics exits early because of
e9d682
lacking cpuacct controller. But with recent extension to
e9d682
virProcessGetStatInfo() we can get the values we need from the
e9d682
proc filesystem. Implement the fallback for the session URI as
e9d682
some of virt tools rely on cpu.* stats to be reported (virt-top,
e9d682
virt-manager).
e9d682
e9d682
Resolves: https://gitlab.com/libvirt/libvirt/-/issues/353
e9d682
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1693707
e9d682
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
e9d682
Reviewed-by: Ján Tomko <jtomko@redhat.com>
e9d682
(cherry picked from commit 044b8744d65f8571038f85685b3c4b241162977b)
e9d682
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2157094
e9d682
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
e9d682
---
e9d682
 src/qemu/qemu_driver.c | 35 +++++++++++++++++++++++++++++++++--
e9d682
 1 file changed, 33 insertions(+), 2 deletions(-)
e9d682
e9d682
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
e9d682
index 84cf2c6a4f..ac210d8069 100644
e9d682
--- a/src/qemu/qemu_driver.c
e9d682
+++ b/src/qemu/qemu_driver.c
e9d682
@@ -18042,6 +18042,30 @@ qemuDomainGetStatsCpuCgroup(virDomainObj *dom,
e9d682
     return 0;
e9d682
 }
e9d682
 
e9d682
+
e9d682
+static int
e9d682
+qemuDomainGetStatsCpuProc(virDomainObj *vm,
e9d682
+                          virTypedParamList *params)
e9d682
+{
e9d682
+    unsigned long long cpuTime = 0;
e9d682
+    unsigned long long sysTime = 0;
e9d682
+    unsigned long long userTime = 0;
e9d682
+
e9d682
+    if (virProcessGetStatInfo(&cpuTime, &sysTime, &userTime,
e9d682
+                              NULL, NULL, vm->pid, 0) < 0) {
e9d682
+        /* ignore error */
e9d682
+        return 0;
e9d682
+    }
e9d682
+
e9d682
+    if (virTypedParamListAddULLong(params, cpuTime, "cpu.time") < 0 ||
e9d682
+        virTypedParamListAddULLong(params, userTime, "cpu.user") < 0 ||
e9d682
+        virTypedParamListAddULLong(params, sysTime, "cpu.system") < 0)
e9d682
+        return -1;
e9d682
+
e9d682
+    return 0;
e9d682
+}
e9d682
+
e9d682
+
e9d682
 static int
e9d682
 qemuDomainGetStatsCpuHaltPollTime(virDomainObj *dom,
e9d682
                                   virTypedParamList *params)
e9d682
@@ -18066,8 +18090,15 @@ qemuDomainGetStatsCpu(virQEMUDriver *driver,
e9d682
                       virTypedParamList *params,
e9d682
                       unsigned int privflags G_GNUC_UNUSED)
e9d682
 {
e9d682
-    if (qemuDomainGetStatsCpuCgroup(dom, params) < 0)
e9d682
-        return -1;
e9d682
+    qemuDomainObjPrivate *priv = dom->privateData;
e9d682
+
e9d682
+    if (priv->cgroup) {
e9d682
+        if (qemuDomainGetStatsCpuCgroup(dom, params) < 0)
e9d682
+            return -1;
e9d682
+    } else {
e9d682
+        if (qemuDomainGetStatsCpuProc(dom, params) < 0)
e9d682
+            return -1;
e9d682
+    }
e9d682
 
e9d682
     if (qemuDomainGetStatsCpuCache(driver, dom, params) < 0)
e9d682
         return -1;
e9d682
-- 
e9d682
2.39.0
e9d682