]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: virHostCPUGetStatsLinux: add guest_nice to VIR_NODE_CPU_STATS_GUEST
authorClaudio Fontana <cfontana@suse.de>
Wed, 17 Jun 2026 16:14:37 +0000 (18:14 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 22 Jun 2026 14:42:36 +0000 (16:42 +0200)
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>
src/util/virhostcpu.c
tests/virhostcpudata/linux-cpustat-24cpu.out
tests/virhostcpudata/linux-cpustat-24cpu.stat

index 1db096ba24dfc6e361ce515011be76da6427cd59..f2b25e940bd97e82a0257f8fca4e8de04f92d4f2 100644 (file)
@@ -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;
         }
index f3183f5c41378bf51bb26360f701f3c5fa0da183..a975702a069359cd631e665a39a485ca9e16fa28 100644 (file)
@@ -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
index bc9d449e1c40aaf352b72b3c75de3013490d3370..e13035b5f063386d5c3357a1b14c3801278f59ab 100644 (file)
@@ -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