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 <[email protected]>
---
 src/util/virhostcpu.c                         | 2 +-
 tests/virhostcpudata/linux-cpustat-24cpu.out  | 8 ++++----
 tests/virhostcpudata/linux-cpustat-24cpu.stat | 4 ++--
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
index 1db096ba24..f2b25e940b 100644
--- a/src/util/virhostcpu.c
+++ b/src/util/virhostcpu.c
@@ -882,7 +882,7 @@ virHostCPUGetStatsLinux(FILE *procstat,
                 return -1;
 
             if (virHostCPUStatsAssign(&params[4], VIR_NODE_CPU_STATS_GUEST,
-                                      guest * TICK_TO_NSEC) < 0)
+                                      (guest + guest_nice) * TICK_TO_NSEC) < 0)
                 return -1;
             return 0;
         }
diff --git a/tests/virhostcpudata/linux-cpustat-24cpu.out 
b/tests/virhostcpudata/linux-cpustat-24cpu.out
index f3183f5c41..a975702a06 100644
--- a/tests/virhostcpudata/linux-cpustat-24cpu.out
+++ b/tests/virhostcpudata/linux-cpustat-24cpu.out
@@ -1,16 +1,16 @@
 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
diff --git a/tests/virhostcpudata/linux-cpustat-24cpu.stat 
b/tests/virhostcpudata/linux-cpustat-24cpu.stat
index bc9d449e1c..e13035b5f0 100644
--- a/tests/virhostcpudata/linux-cpustat-24cpu.stat
+++ b/tests/virhostcpudata/linux-cpustat-24cpu.stat
@@ -1,5 +1,5 @@
-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
-- 
2.35.3

Reply via email to