[libvirt] [PATCH 1/2] tool/virsh-domain.c: Add a vshNodeGetCPUMap function to get cpumap of host.

yangdongsheng yangds.fnst at cn.fujitsu.com
Fri May 24 09:21:06 UTC 2013


Get CPU map of host node CPUs by trying virNodeGetCPUMap
and falling back to virNodeGetCPUStats.

Signed-off-by: yangdongsheng <yangds.fnst at cn.fujitsu.com>
---
 tools/virsh-domain.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index bc42408..ebaca2d 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -149,6 +149,61 @@ vshNodeGetCPUCount(virConnectPtr conn)
 }
 
 /*
+ * Get CPU map of host node CPUs by trying virNodeGetCPUMap
+ * and falling back to virNodeGetCPUStats.
+ */
+static int
+vshNodeGetCPUMap(virConnectPtr conn,
+                 unsigned char **cpumap)
+{
+    int ret = -1;
+    int cpuNum = VIR_NODE_CPU_STATS_ALL_CPUS;
+    int nparams = 0;
+    virNodeCPUStatsPtr params;
+    int i = 0;
+    int dummy;
+    virBitmapPtr cpus = NULL;
+
+    if ((ret = virNodeGetCPUMap(conn, cpumap, NULL, 0)) < 0 ) {
+        /* fall back to virNodeCpuStats */
+        vshResetLibvirtError();
+        if (virNodeGetCPUStats(conn, cpuNum, NULL, &nparams, 0) != 0) {
+            goto cleanup;
+        }
+
+        if ((params = vshCalloc(NULL, nparams, sizeof(*params))) == NULL)
+            goto cleanup;
+
+        if ((cpus = virBitmapNew(nparams)) == NULL){
+            goto cleanup;
+        }
+
+        for (i=0; i < nparams; i++) {
+            if ((ret = virNodeGetCPUStats(conn, i, params,
+                                                &nparams, 0)) != 0) {
+                if (virBitmapClearBit(cpus, i) < 0)
+                    goto cleanup;
+            }
+            else {
+                if (virBitmapSetBit(cpus, i) < 0)
+                    goto cleanup;
+            }
+        }
+        if (cpumap && virBitmapToData(cpus, cpumap, &dummy) < 0){
+            goto cleanup;
+        }
+        ret = virBitmapCountBits(cpus);
+    }
+cleanup:
+    if (ret < 0 && params)
+        VIR_FREE(params);
+    if (ret < 0 && cpumap)
+        VIR_FREE(*cpumap);
+    virBitmapFree(cpus);
+    return ret;
+}
+
+/*
  * "attach-device" command
  */
 static const vshCmdInfo info_attach_device[] = {
-- 
1.7.10.1




More information about the libvir-list mailing list