[libvirt] [PATCHv2 1/2] Add a new flag VIR_DOMAIN_CPU_STATS_VCPU to virDomainGetCPUStats

Hu Tao hutao at cn.fujitsu.com
Tue Apr 24 09:20:21 UTC 2012


Currently virDomainGetCPUStats gets total cpu usage, which consists
of:

  1. vcpu usage: the physical cpu time consumed by virtual cpu(s) of
     domain
  2. hypervisor: `total cpu usage' - `vcpu usage'

The flag VIR_DOMAIN_CPU_STATS_VCPU is for getting vcpu usages.
---
 include/libvirt/libvirt.h.in |    8 ++++++++
 src/libvirt.c                |    9 +++++++--
 tools/virsh.c                |   16 +++++++++++-----
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index ac5df95..2ce8876 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -1355,6 +1355,14 @@ int                     virDomainGetState       (virDomainPtr domain,
  */
 #define VIR_DOMAIN_CPU_STATS_SYSTEMTIME "system_time"
 
+typedef enum {
+    /* virTypedParameterFlags goes here.  */
+
+    /* Additionally, these flags may be bitwise-OR'd in. These
+       flags should not override those of virTypedParameterFlags */
+    VIR_DOMAIN_CPU_STATS_VCPU = 1 << 3, /* get vcpu stats */
+} virDomainGetCPUStatsFlags;
+
 int virDomainGetCPUStats(virDomainPtr domain,
                          virTypedParameterPtr params,
                          unsigned int nparams,
diff --git a/src/libvirt.c b/src/libvirt.c
index af42d3b..3702bd4 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -18670,7 +18670,7 @@ error:
  * @nparams: number of parameters per cpu
  * @start_cpu: which cpu to start with, or -1 for summary
  * @ncpus: how many cpus to query
- * @flags: bitwise-OR of virTypedParameterFlags
+ * @flags: bitwise-OR of virDomainGetCPUStatsFlags
  *
  * Get statistics relating to CPU usage attributable to a single
  * domain (in contrast to the statistics returned by
@@ -18704,7 +18704,12 @@ error:
  * host perspective, this would typically match the cpus member
  * of virNodeGetInfo(), but might be less due to host cpu hotplug.
  *
- * For now, @flags is unused, and the statistics all relate to the
+ * If no flags specified, then statistics of total physical cpu time
+ * consumed by a domain are returned. If flag VIR_DOMAIN_CPU_STATS_VCPU
+ * is set, then statistics of physical cpu time consumed by virtual cpus
+ * of domain are returned. The difference is:
+ *   total cpu usage = vcpu usage + hypervisor usage
+ * For now, the statistics all relate to the
  * usage from the host perspective.  It is possible that a future
  * version will support a flag that queries the cpu usage from the
  * guest's perspective, where the maximum cpu to query would be
diff --git a/tools/virsh.c b/tools/virsh.c
index e177684..194dae7 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -5562,6 +5562,7 @@ static const vshCmdOptDef opts_cpu_stats[] = {
     {"total", VSH_OT_BOOL, 0, N_("Show total statistics only")},
     {"start", VSH_OT_INT, 0, N_("Show statistics from this CPU")},
     {"count", VSH_OT_INT, 0, N_("Number of shown CPUs at most")},
+    {"vcpu", VSH_OT_BOOL, 0, N_("Show vcpu statistics")},
     {NULL, 0, 0, NULL},
 };
 
@@ -5572,6 +5573,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
     virTypedParameterPtr params = NULL;
     int i, j, pos, max_id, cpu = -1, show_count = -1, nparams;
     bool show_total = false, show_per_cpu = false;
+    unsigned int flags = 0;
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         return false;
@@ -5579,6 +5581,10 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
     if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
         return false;
 
+    if (vshCommandOptBool(cmd, "vcpu")) {
+        flags |= VIR_DOMAIN_CPU_STATS_VCPU;
+    }
+
     show_total = vshCommandOptBool(cmd, "total");
     if (vshCommandOptInt(cmd, "start", &cpu) > 0)
         show_per_cpu = true;
@@ -5599,13 +5605,13 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
         cpu = 0;
 
     /* get number of cpus on the node */
-    if ((max_id = virDomainGetCPUStats(dom, NULL, 0, 0, 0, 0)) < 0)
+    if ((max_id = virDomainGetCPUStats(dom, NULL, 0, 0, 0, flags)) < 0)
         goto failed_stats;
     if (show_count < 0 || show_count > max_id)
         show_count = max_id;
 
     /* get percpu information */
-    if ((nparams = virDomainGetCPUStats(dom, NULL, 0, 0, 1, 0)) < 0)
+    if ((nparams = virDomainGetCPUStats(dom, NULL, 0, 0, 1, flags)) < 0)
         goto failed_stats;
 
     if (!nparams) {
@@ -5619,7 +5625,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
     while (show_count) {
         int ncpus = MIN(show_count, 128);
 
-        if (virDomainGetCPUStats(dom, params, nparams, cpu, ncpus, 0) < 0)
+        if (virDomainGetCPUStats(dom, params, nparams, cpu, ncpus, flags) < 0)
             goto failed_stats;
 
         for (i = 0; i < ncpus; i++) {
@@ -5653,7 +5659,7 @@ do_show_total:
         goto cleanup;
 
     /* get supported num of parameter for total statistics */
-    if ((nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0)) < 0)
+    if ((nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, flags)) < 0)
         goto failed_stats;
 
     if (!nparams) {
@@ -5665,7 +5671,7 @@ do_show_total:
         goto failed_params;
 
     /* passing start_cpu == -1 gives us domain's total status */
-    if ((nparams = virDomainGetCPUStats(dom, params, nparams, -1, 1, 0)) < 0)
+    if ((nparams = virDomainGetCPUStats(dom, params, nparams, -1, 1, flags)) < 0)
         goto failed_stats;
 
     vshPrint(ctl, _("Total:\n"));
-- 
1.7.1




More information about the libvir-list mailing list