qemu: Implement qemuDomainGetStatsCpu fallback for qemu:///session

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 <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2022-08-09 16:16:09 +02:00
parent cdc22d9a21
commit 044b8744d6

View File

@ -17765,6 +17765,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
qemuDomainGetStatsCpuHaltPollTimeFromStats(virDomainObj *dom,
unsigned int privflags,
@ -17860,8 +17884,15 @@ qemuDomainGetStatsCpu(virQEMUDriver *driver,
virTypedParamList *params,
unsigned int privflags)
{
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;