[libvirt] [PATCH v2] Implement pretty flag for vcpuinfo and nodecpumap

Ján Tomko jtomko at redhat.com
Thu Jun 5 17:28:13 UTC 2014


Report CPU affinities / online CPUs in human-readable form when
this flag is present:

Before:
CPU Affinity:   y-yy

After:
CPU Affinity:   0,2-3 (out of 4)

https://bugzilla.redhat.com/show_bug.cgi?id=985980
---
v2: man page additions,
    added total cpu count to vcpuinfo output
    fixed example in the commit message

 tools/virsh-domain.c | 21 +++++++++++++++++++--
 tools/virsh-host.c   | 25 ++++++++++++++++++++++---
 tools/virsh.pod      |  8 ++++++--
 3 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index e85c906..4f45ed1 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -5528,6 +5528,10 @@ static const vshCmdOptDef opts_vcpuinfo[] = {
      .flags = VSH_OFLAG_REQ,
      .help = N_("domain name, id or uuid")
     },
+    {.name = "pretty",
+     .type = VSH_OT_BOOL,
+     .help = N_("return human readable output")
+    },
     {.name = NULL}
 };
 
@@ -5541,6 +5545,7 @@ cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd)
     int ncpus, maxcpu;
     size_t cpumaplen;
     bool ret = false;
+    bool pretty = vshCommandOptBool(cmd, "pretty");
     int n, m;
 
     if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
@@ -5589,8 +5594,20 @@ cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd)
             vshPrint(ctl, "%-15s %s\n", _("CPU time"), _("N/A"));
         }
         vshPrint(ctl, "%-15s ", _("CPU Affinity:"));
-        for (m = 0; m < maxcpu; m++) {
-            vshPrint(ctl, "%c", VIR_CPU_USABLE(cpumaps, cpumaplen, n, m) ? 'y' : '-');
+        if (pretty) {
+            char *str;
+
+            str = virBitmapDataToString(VIR_GET_CPUMAP(cpumaps, cpumaplen, n),
+                                        cpumaplen);
+            if (!str)
+                goto cleanup;
+            vshPrint(ctl, _("%s (out of %d)"), str, maxcpu);
+            VIR_FREE(str);
+        } else {
+            for (m = 0; m < maxcpu; m++) {
+                vshPrint(ctl, "%c",
+                         VIR_CPU_USABLE(cpumaps, cpumaplen, n, m) ? 'y' : '-');
+            }
         }
         vshPrint(ctl, "\n");
         if (n < (ncpus - 1))
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index cac6086..8091437 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -32,6 +32,7 @@
 #include <libxml/xmlsave.h>
 
 #include "internal.h"
+#include "virbitmap.h"
 #include "virbuffer.h"
 #include "viralloc.h"
 #include "virsh-domain.h"
@@ -278,12 +279,21 @@ static const vshCmdInfo info_node_cpumap[] = {
     {.name = NULL}
 };
 
+static const vshCmdOptDef opts_node_cpumap[] = {
+    {.name = "pretty",
+     .type = VSH_OT_BOOL,
+     .help = N_("return human readable output")
+    },
+    {.name = NULL}
+};
+
 static bool
 cmdNodeCpuMap(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
     int cpu, cpunum;
     unsigned char *cpumap = NULL;
     unsigned int online;
+    bool pretty = vshCommandOptBool(cmd, "pretty");
     bool ret = false;
 
     cpunum = virNodeGetCPUMap(ctl->conn, &cpumap, &online, 0);
@@ -296,8 +306,17 @@ cmdNodeCpuMap(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
     vshPrint(ctl, "%-15s %d\n", _("CPUs online:"), online);
 
     vshPrint(ctl, "%-15s ", _("CPU map:"));
-    for (cpu = 0; cpu < cpunum; cpu++)
-        vshPrint(ctl, "%c", VIR_CPU_USED(cpumap, cpu) ? 'y' : '-');
+    if (pretty) {
+        char *str = virBitmapDataToString(cpumap, cpunum);
+
+        if (!str)
+            goto cleanup;
+        vshPrint(ctl, "%s", str);
+        VIR_FREE(str);
+    } else {
+        for (cpu = 0; cpu < cpunum; cpu++)
+            vshPrint(ctl, "%c", VIR_CPU_USED(cpumap, cpu) ? 'y' : '-');
+    }
     vshPrint(ctl, "\n");
 
     ret = true;
@@ -978,7 +997,7 @@ const vshCmdDef hostAndHypervisorCmds[] = {
     },
     {.name = "nodecpumap",
      .handler = cmdNodeCpuMap,
-     .opts = NULL,
+     .opts = opts_node_cpumap,
      .info = info_node_cpumap,
      .flags = 0
     },
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 02671b4..80501f9 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -290,11 +290,13 @@ and size of the physical memory. The output corresponds to virNodeInfo
 structure. Specifically, the "CPU socket(s)" field means number of CPU
 sockets per NUMA cell.
 
-=item B<nodecpumap>
+=item B<nodecpumap> [I<--pretty>]
 
 Displays the node's total number of CPUs, the number of online CPUs
 and the list of online CPUs.
 
+With I<--pretty> the online CPUs are printed as a range instead of a list.
+
 =item B<nodecpustats> [I<cpu>] [I<--percent>]
 
 Returns cpu stats of the node.
@@ -1954,11 +1956,13 @@ If I<--guest> is specified, then the count of cpus is reported from
 the perspective of the guest. This flag is usable only for live domains
 and may require guest agent to be configured in the guest.
 
-=item B<vcpuinfo> I<domain>
+=item B<vcpuinfo> I<domain> [I<--pretty>]
 
 Returns basic information about the domain virtual CPUs, like the number of
 vCPUs, the running time, the affinity to physical processors.
 
+With I<--pretty>, cpu affinities are shown as ranges.
+
 =item B<vcpupin> I<domain> [I<vcpu>] [I<cpulist>] [[I<--live>]
 [I<--config>] | [I<--current>]]
 
-- 
1.8.3.2




More information about the libvir-list mailing list