[libvirt] [PATCH 4/4] Enable the pcpuinfo command in virsh

Lai Jiangshan laijs at cn.fujitsu.com
Thu Dec 8 03:41:00 UTC 2011


This command gets information about the physic CPUs.
Example:

# virsh pcpuinfo rhel6
CPU:            0
Curr VCPU:      -
Usage:          47.3

CPU:            1
Curr VCPU:      1
Usage:          46.8

CPU:            2
Curr VCPU:      0
Usage:          52.7

CPU:            3
Curr VCPU:      -
Usage:          44.1


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

diff --git a/tools/virsh.c b/tools/virsh.c
index 0fccf88..4a3833c 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -4012,6 +4012,98 @@ cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd)
 }
 
 /*
+ * "pcpuinfo" command
+ */
+static const vshCmdInfo info_pcpuinfo[] = {
+    {"help", N_("detailed domain pcpu information")},
+    {"desc", N_("Returns basic information about the domain physic CPUs.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_pcpuinfo[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdPcpuinfo(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainInfo info;
+    virDomainPtr dom;
+    virNodeInfo nodeinfo;
+    virVcpuInfoPtr cpuinfo;
+    unsigned char *cpumaps;
+    int ncpus, maxcpu;
+    size_t cpumaplen;
+    bool ret = true;
+    int n, m;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return false;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    if (virNodeGetInfo(ctl->conn, &nodeinfo) != 0) {
+        virDomainFree(dom);
+        return false;
+    }
+
+    if (virDomainGetInfo(dom, &info) != 0) {
+        virDomainFree(dom);
+        return false;
+    }
+
+    cpuinfo = vshMalloc(ctl, sizeof(virVcpuInfo)*info.nrVirtCpu);
+    maxcpu = VIR_NODEINFO_MAXCPUS(nodeinfo);
+    cpumaplen = VIR_CPU_MAPLEN(maxcpu);
+    cpumaps = vshMalloc(ctl, info.nrVirtCpu * cpumaplen);
+
+    if ((ncpus = virDomainGetVcpus(dom,
+                                   cpuinfo, info.nrVirtCpu,
+                                   cpumaps, cpumaplen)) >= 0) {
+        unsigned long long *usages;
+        int nr_usages = maxcpu;
+
+        if (VIR_ALLOC_N(usages, nr_usages) < 0) {
+            virReportOOMError();
+            goto fail;
+        }
+
+        if (virDomainGetPcpusUsage(dom, usages, &nr_usages, 0) < 0) {
+            VIR_FREE(usages);
+            goto fail;
+        }
+
+        for (n = 0; n < MIN(maxcpu, nr_usages); n++) {
+            vshPrint(ctl, "%-15s %d\n", _("CPU:"), n);
+            for (m = 0; m < ncpus; m++) {
+                if (cpuinfo[m].cpu == n) {
+                    vshPrint(ctl, "%-15s %d\n", _("Curr VCPU:"), m);
+                    break;
+                }
+            }
+            if (m == ncpus) {
+                vshPrint(ctl, "%-15s %s\n", _("Curr VCPU:"), _("-"));
+            }
+            vshPrint(ctl, "%-15s %.1lf\n\n", _("Usage:"),
+                     usages[n] / 1000000000.0);
+        }
+        VIR_FREE(usages);
+        goto cleanup;
+    }
+
+fail:
+    ret = false;
+
+cleanup:
+    VIR_FREE(cpumaps);
+    VIR_FREE(cpuinfo);
+    virDomainFree(dom);
+    return ret;
+}
+
+/*
  * "vcpupin" command
  */
 static const vshCmdInfo info_vcpupin[] = {
@@ -15130,6 +15222,7 @@ static const vshCmdDef domManagementCmds[] = {
      opts_migrate_setspeed, info_migrate_setspeed, 0},
     {"migrate-getspeed", cmdMigrateGetMaxSpeed,
      opts_migrate_getspeed, info_migrate_getspeed, 0},
+    {"pcpuinfo", cmdPcpuinfo, opts_pcpuinfo, info_pcpuinfo, 0},
     {"reboot", cmdReboot, opts_reboot, info_reboot, 0},
     {"reset", cmdReset, opts_reset, info_reset, 0},
     {"restore", cmdRestore, opts_restore, info_restore, 0},
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 5131ade..241e0dc 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -1217,6 +1217,11 @@ Thus, this command always takes exactly zero or two flags.
 Returns basic information about the domain virtual CPUs, like the number of
 vCPUs, the running time, the affinity to physical processors.
 
+=item B<pcpuinfo> I<domain-id>
+
+Returns information about the physic CPUs of the domain, like the usage of
+CPUs, the current attached vCPUs.
+
 =item B<vcpupin> I<domain-id> [I<vcpu>] [I<cpulist>] [[I<--live>]
 [I<--config>] | [I<--current>]]
 
-- 
1.7.4.4




More information about the libvir-list mailing list