[PATCH] util: virHostCPUGetStatsLinux: support VIR_NODE_CPU_STATS_GUEST

Claudio Fontana posted 1 patch 1 week, 2 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20250828082941.1977-1-cfontana@suse.de
include/libvirt/libvirt-host.h               | 10 ++++++++
src/test/test_driver.c                       |  9 +++++--
src/util/virhostcpu.c                        |  5 +++-
tests/virhostcpudata/linux-cpustat-24cpu.out | 25 ++++++++++++++++++++
tools/virsh-host.c                           |  3 +++
5 files changed, 49 insertions(+), 3 deletions(-)
[PATCH] util: virHostCPUGetStatsLinux: support VIR_NODE_CPU_STATS_GUEST
Posted by Claudio Fontana 1 week, 2 days ago
it is very useful for platforms to know how much time a node is
spending running guests.

Signed-off-by: Claudio Fontana <cfontana@suse.de>
---
 include/libvirt/libvirt-host.h               | 10 ++++++++
 src/test/test_driver.c                       |  9 +++++--
 src/util/virhostcpu.c                        |  5 +++-
 tests/virhostcpudata/linux-cpustat-24cpu.out | 25 ++++++++++++++++++++
 tools/virsh-host.c                           |  3 +++
 5 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h
index 8922d00e0c..2b899144d3 100644
--- a/include/libvirt/libvirt-host.h
+++ b/include/libvirt/libvirt-host.h
@@ -267,6 +267,16 @@ typedef enum {
  */
 # define VIR_NODE_CPU_STATS_INTR "intr"
 
+/**
+ * VIR_NODE_CPU_STATS_GUEST:
+ *
+ * The cumulative CPU time spent running guests,
+ * since the node booting up (in nanoseconds).
+ *
+ * Since: 11.8.0
+ */
+# define VIR_NODE_CPU_STATS_GUEST "guest"
+
 /**
  * VIR_NODE_CPU_STATS_UTILIZATION:
  *
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index 25335d9002..1165689de7 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -4435,7 +4435,7 @@ static int testNodeGetCellsFreeMemory(virConnectPtr conn,
     return ret;
 }
 
-#define TEST_NB_CPU_STATS 4
+#define TEST_NB_CPU_STATS 5
 
 static int
 testNodeGetCPUStats(virConnectPtr conn G_GNUC_UNUSED,
@@ -4453,7 +4453,7 @@ testNodeGetCPUStats(virConnectPtr conn G_GNUC_UNUSED,
         return 0;
     }
 
-    for (i = 0; i < *nparams && i < 4; i++) {
+    for (i = 0; i < *nparams && i < 5; i++) {
         switch (i) {
         case 0:
             if (virHostCPUStatsAssign(&params[i],
@@ -4475,6 +4475,11 @@ testNodeGetCPUStats(virConnectPtr conn G_GNUC_UNUSED,
                                       VIR_NODE_CPU_STATS_IOWAIT, 763600000) < 0)
                 return -1;
             break;
+        case 4:
+            if (virHostCPUStatsAssign(&params[i],
+                                      VIR_NODE_CPU_STATS_GUEST, 1797400000) < 0)
+                return -1;
+            break;
         }
     }
 
diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
index 5dbcc8987c..c3b4f87de1 100644
--- a/src/util/virhostcpu.c
+++ b/src/util/virhostcpu.c
@@ -182,7 +182,7 @@ virHostCPUGetStatsFreeBSD(int cpuNum,
 # define CPUINFO_PATH "/proc/cpuinfo"
 # define PROCSTAT_PATH "/proc/stat"
 
-# define LINUX_NB_CPU_STATS 4
+# define LINUX_NB_CPU_STATS 5
 
 int
 virHostCPUGetSocket(unsigned int cpu, unsigned int *socket)
@@ -855,6 +855,9 @@ virHostCPUGetStatsLinux(FILE *procstat,
                                       iowait * TICK_TO_NSEC) < 0)
                 return -1;
 
+            if (virHostCPUStatsAssign(&params[4], VIR_NODE_CPU_STATS_GUEST,
+                                      guest * 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 0a1a5bcd9a..f3183f5c41 100644
--- a/tests/virhostcpudata/linux-cpustat-24cpu.out
+++ b/tests/virhostcpudata/linux-cpustat-24cpu.out
@@ -3,148 +3,173 @@ kernel: 8751170
 user: 14128079
 idle: 1816344522
 iowait: 81323
+guest: 5880634
 
 cpu0:
 kernel: 447603
 user: 749021
 idle: 75399242
 iowait: 5295
+guest: 331814
 
 cpu1:
 kernel: 167215
 user: 337326
 idle: 76178612
 iowait: 1121
+guest: 166726
 
 cpu2:
 kernel: 308930
 user: 666889
 idle: 75649696
 iowait: 4298
+guest: 272094
 
 cpu3:
 kernel: 227674
 user: 328464
 idle: 76131634
 iowait: 1219
+guest: 115551
 
 cpu4:
 kernel: 299514
 user: 583915
 idle: 75746383
 iowait: 3997
+guest: 253387
 
 cpu5:
 kernel: 112287
 user: 231867
 idle: 76336319
 iowait: 798
+guest: 118465
 
 cpu6:
 kernel: 546590
 user: 896252
 idle: 75132665
 iowait: 7210
+guest: 410328
 
 cpu7:
 kernel: 177715
 user: 342337
 idle: 76154889
 iowait: 1933
+guest: 204523
 
 cpu8:
 kernel: 452773
 user: 772479
 idle: 75359327
 iowait: 5845
+guest: 347169
 
 cpu9:
 kernel: 1050230
 user: 1079258
 idle: 74532776
 iowait: 3340
+guest: 150374
 
 cpu10:
 kernel: 535495
 user: 847295
 idle: 75202362
 iowait: 4038
+guest: 370309
 
 cpu11:
 kernel: 171635
 user: 323891
 idle: 76181622
 iowait: 993
+guest: 199566
 
 cpu12:
 kernel: 331031
 user: 683257
 idle: 75587176
 iowait: 5174
+guest: 293663
 
 cpu13:
 kernel: 112686
 user: 230633
 idle: 76345295
 iowait: 1367
+guest: 103907
 
 cpu14:
 kernel: 251393
 user: 547599
 idle: 75824554
 iowait: 5195
+guest: 207464
 
 cpu15:
 kernel: 199044
 user: 260673
 idle: 76230586
 iowait: 1379
+guest: 76655
 
 cpu16:
 kernel: 244158
 user: 463357
 idle: 75923993
 iowait: 6211
+guest: 184943
 
 cpu17:
 kernel: 88571
 user: 189253
 idle: 76411610
 iowait: 1388
+guest: 85456
 
 cpu18:
 kernel: 546539
 user: 875655
 idle: 75096896
 iowait: 5756
+guest: 408446
 
 cpu19:
 kernel: 186366
 user: 348768
 idle: 76137323
 iowait: 1299
+guest: 208604
 
 cpu20:
 kernel: 449460
 user: 765202
 idle: 75348938
 iowait: 4389
+guest: 353323
 
 cpu21:
 kernel: 1045076
 user: 1116075
 idle: 74500557
 iowait: 2411
+guest: 162678
 
 cpu22:
 kernel: 534125
 user: 847779
 idle: 75178185
 iowait: 5632
+guest: 376150
 
 cpu23:
 kernel: 265029
 user: 640815
 idle: 75753872
 iowait: 1026
+guest: 479032
 
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index 51b71b512c..e918cfa4ca 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -743,6 +743,7 @@ typedef enum {
     VIRSH_CPU_IDLE,
     VIRSH_CPU_IOWAIT,
     VIRSH_CPU_INTR,
+    VIRSH_CPU_GUEST,
     VIRSH_CPU_USAGE,
     VIRSH_CPU_LAST
 } virshCPUStats;
@@ -755,6 +756,7 @@ VIR_ENUM_IMPL(virshCPUStats,
               VIR_NODE_CPU_STATS_IDLE,
               VIR_NODE_CPU_STATS_IOWAIT,
               VIR_NODE_CPU_STATS_INTR,
+              VIR_NODE_CPU_STATS_GUEST,
               VIR_NODE_CPU_STATS_UTILIZATION);
 
 const char *virshCPUOutput[] = {
@@ -763,6 +765,7 @@ const char *virshCPUOutput[] = {
     N_("idle:"),
     N_("iowait:"),
     N_("intr:"),
+    N_("guest:"),
     N_("usage:")
 };
 
-- 
2.35.3
Re: [PATCH] util: virHostCPUGetStatsLinux: support VIR_NODE_CPU_STATS_GUEST
Posted by Daniel P. Berrangé via Devel 5 days, 12 hours ago
On Thu, Aug 28, 2025 at 10:29:41AM +0200, Claudio Fontana wrote:
> it is very useful for platforms to know how much time a node is
> spending running guests.
> 
> Signed-off-by: Claudio Fontana <cfontana@suse.de>
> ---
>  include/libvirt/libvirt-host.h               | 10 ++++++++
>  src/test/test_driver.c                       |  9 +++++--
>  src/util/virhostcpu.c                        |  5 +++-
>  tests/virhostcpudata/linux-cpustat-24cpu.out | 25 ++++++++++++++++++++
>  tools/virsh-host.c                           |  3 +++
>  5 files changed, 49 insertions(+), 3 deletions(-)
> 
> diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h
> index 8922d00e0c..2b899144d3 100644
> --- a/include/libvirt/libvirt-host.h
> +++ b/include/libvirt/libvirt-host.h
> @@ -267,6 +267,16 @@ typedef enum {
>   */
>  # define VIR_NODE_CPU_STATS_INTR "intr"
>  
> +/**
> + * VIR_NODE_CPU_STATS_GUEST:
> + *
> + * The cumulative CPU time spent running guests,
> + * since the node booting up (in nanoseconds).

FYI, I think this should be qualified slight to say

  'The cumulative CPU time spent running guest virtual CPUs,'

because it does NOT include the overhead from host side
emulation outside of vCPUs, which is often considered to
be part of the time spent running guests.


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Re: [PATCH] util: virHostCPUGetStatsLinux: support VIR_NODE_CPU_STATS_GUEST
Posted by Claudio Fontana 1 day, 17 hours ago
On 9/1/25 16:24, Daniel P. Berrangé via Devel wrote:
> On Thu, Aug 28, 2025 at 10:29:41AM +0200, Claudio Fontana wrote:
>> it is very useful for platforms to know how much time a node is
>> spending running guests.
>>
>> Signed-off-by: Claudio Fontana <cfontana@suse.de>
>> ---
>>  include/libvirt/libvirt-host.h               | 10 ++++++++
>>  src/test/test_driver.c                       |  9 +++++--
>>  src/util/virhostcpu.c                        |  5 +++-
>>  tests/virhostcpudata/linux-cpustat-24cpu.out | 25 ++++++++++++++++++++
>>  tools/virsh-host.c                           |  3 +++
>>  5 files changed, 49 insertions(+), 3 deletions(-)
>>
>> diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h
>> index 8922d00e0c..2b899144d3 100644
>> --- a/include/libvirt/libvirt-host.h
>> +++ b/include/libvirt/libvirt-host.h
>> @@ -267,6 +267,16 @@ typedef enum {
>>   */
>>  # define VIR_NODE_CPU_STATS_INTR "intr"
>>  
>> +/**
>> + * VIR_NODE_CPU_STATS_GUEST:
>> + *
>> + * The cumulative CPU time spent running guests,
>> + * since the node booting up (in nanoseconds).
> 
> FYI, I think this should be qualified slight to say
> 
>   'The cumulative CPU time spent running guest virtual CPUs,'
> 
> because it does NOT include the overhead from host side
> emulation outside of vCPUs, which is often considered to
> be part of the time spent running guests.
> 
> 
> With regards,
> Daniel

Hello, I just sent a follow-up patch to clarify this:

[PATCH] libvirt-host: VIR_NODE_CPU_STATS_GUEST: clarify "guest" time

Thanks,

Claudio
Re: [PATCH] util: virHostCPUGetStatsLinux: support VIR_NODE_CPU_STATS_GUEST
Posted by Michal Prívozník via Devel 5 days, 12 hours ago
On 8/28/25 10:29, Claudio Fontana wrote:
> it is very useful for platforms to know how much time a node is
> spending running guests.
> 
> Signed-off-by: Claudio Fontana <cfontana@suse.de>
> ---
>  include/libvirt/libvirt-host.h               | 10 ++++++++
>  src/test/test_driver.c                       |  9 +++++--
>  src/util/virhostcpu.c                        |  5 +++-
>  tests/virhostcpudata/linux-cpustat-24cpu.out | 25 ++++++++++++++++++++
>  tools/virsh-host.c                           |  3 +++
>  5 files changed, 49 insertions(+), 3 deletions(-)

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

and merged.

Michal