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