On Linux the accounting stats come from /proc/stat entries and the mapping is:
https://www.kernel.org/doc/html/latest/filesystems/proc.html
VIR_NODE_CPU_STATS_KERNEL = (system + irq + softirq)
VIR_NODE_CPU_STATS_USER = (user + nice)
VIR_NODE_CPU_STATS_IDLE = idle
<VIR_NODE_CPU_STATS_INTR> = N/A, irq is accounted by libvirt as KERNEL
VIR_NODE_CPU_STATS_IOWAIT = iowait
VIR_NODE_CPU_STATS_GUEST = guest
VIR_NODE_CPU_STATS_GUEST was introduced in:
b5878727c714
("util: virHostCPUGetStatsLinux: support VIR_NODE_CPU_STATS_GUEST")
with the goal of specifically accounting the CPU time spent running guest VCPUs.
Unfortunately at the time it was not considered that to get a measure of this
time, "guest" is not sufficient, because any guest that is "niced" is accounted
in a separate category, "guest_nice".
This patch rectifies the situation by adding the guest_nice counter, so that
VIR_NODE_CPU_STATS_GUEST = (guest + guest_nice)
which makes the statistic useful to the caller, as the totals sum up correctly.
In order to find the total USER time spent running host tasks, for example:
cpu_user_host = VIR_NODE_CPU_STATS_USER - VIR_NODE_CPU_STATS_GUEST
this follows from the fact that "guest" is included in "user" accounting,
and "guest_nice" is included in "nice" accounting.
Extend the existing tests artificially to capture this specific case.
Fixes: b5878727c714d813c820ad4a1b695fbbb5ffc84e
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
return -1;
if (virHostCPUStatsAssign(¶ms[4], VIR_NODE_CPU_STATS_GUEST,
- guest * TICK_TO_NSEC) < 0)
+ (guest + guest_nice) * TICK_TO_NSEC) < 0)
return -1;
return 0;
}
cpu:
kernel: 8751170
user: 14128079
-idle: 1816344522
+idle: 1816344521
iowait: 81323
-guest: 5880634
+guest: 5880635
cpu0:
kernel: 447603
user: 749021
-idle: 75399242
+idle: 75399241
iowait: 5295
-guest: 331814
+guest: 331815
cpu1:
kernel: 167215
-cpu 14126233 1846 7764352 1816344522 81323 395581 591237 0 5880634 0
-cpu0 748997 24 320851 75399242 5295 22050 104702 0 331814 0
+cpu 14126233 1846 7764352 1816344521 81323 395581 591237 0 5880634 1
+cpu0 748997 24 320851 75399241 5295 22050 104702 0 331814 1
cpu1 337325 1 140909 76178612 1121 8962 17344 0 166726 0
cpu2 666860 29 269302 75649696 4298 18473 21155 0 272094 0
cpu3 328387 77 211400 76131634 1219 9701 6573 0 115551 0