[libvirt] [PATCH 3/3 V7] cpu-stats command shows cpu statistics information of a domain.

Lai Jiangshan laijs at cn.fujitsu.com
Fri Mar 2 02:54:24 UTC 2012


From: KAMEZAWA Hiroyuki <kamezawa.hiroyu at jp.fujitsu.com>

Total:
	cpu_time         14.312
CPU0:
	cpu_time          3.253
CPU1:
	cpu_time          1.923
CPU2:
	cpu_time          7.424
CPU3:
	cpu_time          1.712

Changed from V5:
      add --all, --start, --count option
Changed from V:
	rebase

Signed-off-by: Lai Jiangshan <laijs at cn.fujitsu.com>
---
 tools/virsh.c   |  154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh.pod |    8 +++
 2 files changed, 162 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index dc362dc..a431826 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -5537,6 +5537,159 @@ cmdSetvcpus(vshControl *ctl, const vshCmd *cmd)
 }
 
 /*
+ * "cpu-stats" command
+ */
+static const vshCmdInfo info_cpu_stats[] = {
+    {"help", N_("show domain cpu statistics")},
+    {"desc", N_("Display statistics about the domain's CPUs, including per-CPU statistics.")},
+    {NULL, NULL},
+};
+
+static const vshCmdOptDef opts_cpu_stats[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"all", 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")},
+    {NULL, 0, 0, NULL},
+};
+
+static bool
+cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    virTypedParameterPtr params = NULL;
+    int i, j, pos, max_id, cpu = -1, show_count = -1, nparams;
+    bool show_all = false, show_per_cpu = false;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return false;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    show_all = vshCommandOptBool(cmd, "all");
+    if (vshCommandOptInt(cmd, "start", &cpu) > 0)
+        show_per_cpu = true;
+    if (vshCommandOptInt(cmd, "count", &show_count) > 0)
+        show_per_cpu = true;
+
+    /* default show per_cpu and total */
+    if (!show_all && !show_per_cpu) {
+        show_all = true;
+        show_per_cpu = true;
+    }
+
+    if (!show_all)
+        goto do_show_per_cpu;
+
+    /* get supported num of parameter for total statistics */
+    if ((nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0)) < 0)
+        goto failed_stats;
+    if (VIR_ALLOC_N(params, nparams))
+        goto failed_params;
+
+    /* passing start_cpu == -1 gives us domain's total status */
+    if ((nparams = virDomainGetCPUStats(dom, params, nparams, -1, 1, 0)) < 0)
+        goto failed_stats;
+
+    vshPrint(ctl, "Total:\n");
+    for (i = 0; i < nparams; i++) {
+        vshPrint(ctl, "\t%-10s ", params[i].field);
+        if (STREQ(params[i].field, VIR_DOMAIN_CPU_STATS_CPUTIME)) {
+            if (params[i].type == VIR_TYPED_PARAM_ULLONG) {
+                vshPrint(ctl, "%12.3lf\n",
+                         params[i].value.ul / 1000000000.0);
+            } else {
+                const char *s = vshGetTypedParamValue(ctl, &params[i]);
+                vshPrint(ctl, _("%s(ABI changed? ullong is expected)\n"), s);
+                VIR_FREE(s);
+            }
+        } else {
+            const char *s = vshGetTypedParamValue(ctl, &params[i]);
+            vshPrint(ctl, _("%s\n"), s);
+            VIR_FREE(s);
+        }
+    }
+    virTypedParameterArrayClear(params, nparams);
+    VIR_FREE(params);
+
+    if (!show_per_cpu) /* show all stats only */
+        goto cleanup;
+
+do_show_per_cpu:
+    /* check cpu, show_count, and ignore wrong argument */
+    if (cpu < 0)
+        cpu = 0;
+    if (show_count < 0)
+        show_count = INT_MAX;
+
+    /* get max cpu id on the node */
+    if ((max_id = virDomainGetCPUStats(dom, NULL, 0, 0, 0, 0)) < 0)
+        goto failed_stats;
+    /* get percpu information */
+    if ((nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0)) < 0)
+        goto failed_stats;
+
+    if (VIR_ALLOC_N(params, nparams * 128))
+        goto failed_params;
+
+    while (cpu <= max_id) {
+        int ncpus = 128;
+
+        if (cpu + ncpus - 1 > max_id) /* id starts from 0. */
+            ncpus = max_id + 1 - cpu;
+
+        if (virDomainGetCPUStats(dom, params, nparams, cpu, ncpus, 0) < 0)
+            goto failed_stats;
+
+        for (i = 0; i < ncpus; i++) {
+            if (params[i * nparams].type == 0) /* this cpu is not in the map */
+                continue;
+            vshPrint(ctl, "CPU%d:\n", cpu + i);
+
+            for (j = 0; j < nparams; j++) {
+                pos = i * nparams + j;
+                vshPrint(ctl, "\t%-10s ", params[pos].field);
+                if (STREQ(params[pos].field, VIR_DOMAIN_CPU_STATS_CPUTIME)) {
+                    if (params[j].type == VIR_TYPED_PARAM_ULLONG) {
+                        vshPrint(ctl, "%12.3lf\n",
+                                 params[pos].value.ul / 1000000000.0);
+                    } else {
+                        const char *s = vshGetTypedParamValue(ctl, &params[pos]);
+                        vshPrint(ctl, _("%s(ABI changed? ullong is expected)\n"), s);
+                        VIR_FREE(s);
+                    }
+                } else {
+                    const char *s = vshGetTypedParamValue(ctl, &params[pos]);
+                    vshPrint(ctl, _("%s\n"), s);
+                    VIR_FREE(s);
+                }
+            }
+
+            if (--show_count <= 0) /* mark done to exit the outmost loop */
+                cpu = max_id;
+        }
+        cpu += ncpus;
+        virTypedParameterArrayClear(params, nparams * ncpus);
+    }
+    VIR_FREE(params);
+cleanup:
+    virDomainFree(dom);
+    return true;
+
+failed_params:
+    virReportOOMError();
+    virDomainFree(dom);
+    return false;
+
+failed_stats:
+    vshError(ctl, _("Failed to virDomainGetCPUStats()\n"));
+    VIR_FREE(params);
+    virDomainFree(dom);
+    return false;
+}
+
+/*
  * "inject-nmi" command
  */
 static const vshCmdInfo info_inject_nmi[] = {
@@ -16910,6 +17063,7 @@ static const vshCmdDef domManagementCmds[] = {
 #endif
     {"cpu-baseline", cmdCPUBaseline, opts_cpu_baseline, info_cpu_baseline, 0},
     {"cpu-compare", cmdCPUCompare, opts_cpu_compare, info_cpu_compare, 0},
+    {"cpu-stats", cmdCPUStats, opts_cpu_stats, info_cpu_stats, 0},
     {"create", cmdCreate, opts_create, info_create, 0},
     {"define", cmdDefine, opts_define, info_define, 0},
     {"desc", cmdDesc, opts_desc, info_desc, 0},
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 8f6a2d6..b23868d 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -790,6 +790,14 @@ Provide the maximum number of virtual CPUs supported for a guest VM on
 this connection.  If provided, the I<type> parameter must be a valid
 type attribute for the <domain> element of XML.
 
+=item B<cpu-stats> I<domain> [I<--all>] [I<start>] [I<count>]
+
+Provide cpu statistics information of a domain. The domain should
+be running. Default it shows stats for all CPUs, and a total. Use
+I<--all> for only the total stats, I<start> for only the per-cpu
+stats of the CPUs from I<start>, I<count> for only I<count> CPUs'
+stats.
+
 =item B<migrate> [I<--live>] [I<--direct>] [I<--p2p> [I<--tunnelled>]]
 [I<--persistent>] [I<--undefinesource>] [I<--suspend>] [I<--copy-storage-all>]
 [I<--copy-storage-inc>] [I<--change-protection>] [I<--unsafe>] [I<--verbose>]
-- 
1.7.4.4




More information about the libvir-list mailing list