Modified the places where virNodeGetInfo was used for the purpose
of obtaining the maximum node CPU number. Transparently falling
back to virNodeGetInfo in case of failure.
Wrote utility function vshNodeGetCPUCount to compute node CPU
number.
Signed-off-by: Viktor Mihajlovski <mihajlov(a)linux.vnet.ibm.com>
---
V2 Changes:
Implemented Eric Blake's suggestion to remove code bloat
introduced by first patch version.
New helper function vshNodeGetCPUCount is now used to calculate
the number of node CPUs.
tools/virsh-domain.c | 32 +++++++++++++++++++++++---------
1 file changed, 23 insertions(+), 9 deletions(-)
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 86ed4d3..183a598 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -126,6 +126,26 @@ vshDomainVcpuStateToString(int state)
}
/*
+ * Determine number of CPU nodes present by trying
+ * virNodeGetCPUMap and falling back to virNodeGetInfo
+ * if needed.
+ */
+static int
+vshNodeGetCPUCount(virConnectPtr conn)
+{
+ int ret;
+ virNodeInfo nodeinfo;
+
+ if ((ret = virNodeGetCPUMap(conn, NULL, NULL, 0)) < 0) {
+ /* fall back to nodeinfo */
+ if (virNodeGetInfo(conn, &nodeinfo) == 0) {
+ ret = VIR_NODEINFO_MAXCPUS(nodeinfo);
+ }
+ }
+ return ret;
+}
+
+/*
* "attach-device" command
*/
static const vshCmdInfo info_attach_device[] = {
@@ -4497,7 +4517,6 @@ cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd)
{
virDomainInfo info;
virDomainPtr dom;
- virNodeInfo nodeinfo;
virVcpuInfoPtr cpuinfo;
unsigned char *cpumaps;
int ncpus, maxcpu;
@@ -4508,7 +4527,7 @@ cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd)
if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
return false;
- if (virNodeGetInfo(ctl->conn, &nodeinfo) != 0) {
+ if ((maxcpu = vshNodeGetCPUCount(ctl->conn)) < 0) {
virDomainFree(dom);
return false;
}
@@ -4519,7 +4538,6 @@ cmdVcpuinfo(vshControl *ctl, const vshCmd *cmd)
}
cpuinfo = vshMalloc(ctl, sizeof(virVcpuInfo)*info.nrVirtCpu);
- maxcpu = VIR_NODEINFO_MAXCPUS(nodeinfo);
cpumaplen = VIR_CPU_MAPLEN(maxcpu);
cpumaps = vshMalloc(ctl, info.nrVirtCpu * cpumaplen);
@@ -4645,7 +4663,6 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
{
virDomainInfo info;
virDomainPtr dom;
- virNodeInfo nodeinfo;
int vcpu = -1;
const char *cpulist = NULL;
bool ret = true;
@@ -4695,7 +4712,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
return false;
}
- if (virNodeGetInfo(ctl->conn, &nodeinfo) != 0) {
+ if ((maxcpu = vshNodeGetCPUCount(ctl->conn)) < 0) {
virDomainFree(dom);
return false;
}
@@ -4712,7 +4729,6 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
return false;
}
- maxcpu = VIR_NODEINFO_MAXCPUS(nodeinfo);
cpumaplen = VIR_CPU_MAPLEN(maxcpu);
/* Query mode: show CPU affinity information then exit.*/
@@ -4864,7 +4880,6 @@ static bool
cmdEmulatorPin(vshControl *ctl, const vshCmd *cmd)
{
virDomainPtr dom;
- virNodeInfo nodeinfo;
const char *cpulist = NULL;
bool ret = true;
unsigned char *cpumap = NULL;
@@ -4905,12 +4920,11 @@ cmdEmulatorPin(vshControl *ctl, const vshCmd *cmd)
}
query = !cpulist;
- if (virNodeGetInfo(ctl->conn, &nodeinfo) != 0) {
+ if ((maxcpu = vshNodeGetCPUCount(ctl->conn)) < 0) {
virDomainFree(dom);
return false;
}
- maxcpu = VIR_NODEINFO_MAXCPUS(nodeinfo);
cpumaplen = VIR_CPU_MAPLEN(maxcpu);
/* Query mode: show CPU affinity information then exit.*/
--
1.7.12.4