The implementation of the inner guts of the function is similar for all
drivers, so we can add a helper and not have to reimplement it three
times.
---
src/conf/domain_conf.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++
src/conf/domain_conf.h | 8 ++++++
src/libvirt_private.syms | 1 +
src/libxl/libxl_driver.c | 38 +++-------------------------
src/qemu/qemu_driver.c | 43 +++-----------------------------
src/test/test_driver.c | 38 ++++------------------------
6 files changed, 84 insertions(+), 108 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 3b15cb4..c7843dc 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1449,6 +1449,70 @@ virDomainDefHasVcpuPin(const virDomainDef *def)
}
+/**
+ * virDomainDefGetVcpuPinInfoHelper:
+ * @def: domain definition
+ * @maplen: length of one cpumap passed from caller (@cpumaps)
+ * @ncpumaps: count of cpumaps of @maplen length in @cpumaps
+ * @cpumaps: array of pinning information bitmaps to be filled
+ * @hostcpus: number of cpus in the host
+ * @autoCpuset: Cpu pinning bitmap used in case of automatic cpu pinning
+ *
+ * Fills the @cpumaps array as documented by the virDomainGetVcpuPinInfo API.
+ * In case when automatic cpu pinning is supported, the bitmap should be passed
+ * as @autoCpuset. If @hostcpus is < 0 no error is reported (to pass through
+ * error message).
+ *
+ * Returns number of filled entries or -1 on error.
+ */
+int
+virDomainDefGetVcpuPinInfoHelper(virDomainDefPtr def,
+ int maplen,
+ int ncpumaps,
+ unsigned char *cpumaps,
+ int hostcpus,
+ virBitmapPtr autoCpuset)
+{
+ virBitmapPtr allcpumap = NULL;
+ size_t i;
+
+ if (hostcpus < 0)
+ return -1;
+
+ if (!(allcpumap = virBitmapNew(hostcpus)))
+ return -1;
+
+ virBitmapSetAll(allcpumap);
+
+ /* Clamp to actual number of vcpus */
+ if (ncpumaps > virDomainDefGetVcpus(def))
+ ncpumaps = virDomainDefGetVcpus(def);
+
+ for (i = 0; i < ncpumaps; i++) {
+ virDomainVcpuInfoPtr vcpu = virDomainDefGetVcpu(def, i);
+ virBitmapPtr bitmap = NULL;
+
+ if (!vcpu->online)
+ continue;
+
+ if (vcpu->cpumask)
+ bitmap = vcpu->cpumask;
+ else if (def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO &&
+ autoCpuset)
+ bitmap = autoCpuset;
+ else if (def->cpumask)
+ bitmap = def->cpumask;
+ else
+ bitmap = allcpumap;
+
+ virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
+ }
+
+ virBitmapFree(allcpumap);
+ return ncpumaps;
+}
+
+
virDomainDiskDefPtr
virDomainDiskDefNew(virDomainXMLOptionPtr xmlopt)
{
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 1de3be3..c1e63e4 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -3128,4 +3128,12 @@ int virDomainDiskDefCheckDuplicateInfo(virDomainDiskDefPtr a,
int virDomainDefCheckDuplicateDiskInfo(virDomainDefPtr def)
ATTRIBUTE_NONNULL(1);
+int virDomainDefGetVcpuPinInfoHelper(virDomainDefPtr def,
+ int maplen,
+ int ncpumaps,
+ unsigned char *cpumaps,
+ int hostcpus,
+ virBitmapPtr autoCpuset)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4) ATTRIBUTE_RETURN_CHECK;
+
#endif /* __DOMAIN_CONF_H */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4b40612..6da9b5c 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -221,6 +221,7 @@ virDomainDefGetMemoryInitial;
virDomainDefGetOnlineVcpumap;
virDomainDefGetSecurityLabelDef;
virDomainDefGetVcpu;
+virDomainDefGetVcpuPinInfoHelper;
virDomainDefGetVcpus;
virDomainDefGetVcpusMax;
virDomainDefHasDeviceAddress;
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 404016e..a99c99c 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -2423,8 +2423,7 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
virDomainObjPtr vm = NULL;
virDomainDefPtr targetDef = NULL;
- int hostcpus, vcpu, ret = -1;
- virBitmapPtr allcpumap = NULL;
+ int ret = -1;
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -2445,41 +2444,10 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
/* Make sure coverity knows targetDef is valid at this point. */
sa_assert(targetDef);
- /* Clamp to actual number of vcpus */
- if (ncpumaps > virDomainDefGetVcpus(targetDef))
- ncpumaps = virDomainDefGetVcpus(targetDef);
-
- if ((hostcpus = libxl_get_max_cpus(cfg->ctx)) < 0)
- goto cleanup;
-
- if (!(allcpumap = virBitmapNew(hostcpus)))
- goto cleanup;
-
- virBitmapSetAll(allcpumap);
-
- memset(cpumaps, 0x00, maplen * ncpumaps);
-
- for (vcpu = 0; vcpu < ncpumaps; vcpu++) {
- virDomainVcpuInfoPtr vcpuinfo = virDomainDefGetVcpu(targetDef, vcpu);
- virBitmapPtr bitmap = NULL;
-
- if (!vcpuinfo->online)
- continue;
-
- if (vcpuinfo->cpumask)
- bitmap = vcpuinfo->cpumask;
- else if (targetDef->cpumask)
- bitmap = targetDef->cpumask;
- else
- bitmap = allcpumap;
-
- virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, vcpu), maplen);
- }
-
- ret = ncpumaps;
+ ret = virDomainDefGetVcpuPinInfoHelper(targetDef, maplen, ncpumaps, cpumaps,
+ libxl_get_max_cpus(cfg->ctx), NULL);
cleanup:
- virBitmapFree(allcpumap);
if (vm)
virObjectUnlock(vm);
virObjectUnref(cfg);
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 45ff3c0..c8b996b 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -5147,9 +5147,6 @@ qemuDomainGetVcpuPinInfo(virDomainPtr dom,
virDomainObjPtr vm = NULL;
virDomainDefPtr def;
int ret = -1;
- int hostcpus;
- size_t i;
- virBitmapPtr allcpumap = NULL;
qemuDomainObjPrivatePtr priv = NULL;
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
@@ -5164,46 +5161,12 @@ qemuDomainGetVcpuPinInfo(virDomainPtr dom,
if (!(def = virDomainObjGetOneDef(vm, flags)))
goto cleanup;
- if ((hostcpus = nodeGetCPUCount(NULL)) < 0)
- goto cleanup;
-
- if (!(allcpumap = virBitmapNew(hostcpus)))
- goto cleanup;
-
- virBitmapSetAll(allcpumap);
priv = vm->privateData;
- /* Clamp to actual number of vcpus */
- if (ncpumaps > virDomainDefGetVcpus(def))
- ncpumaps = virDomainDefGetVcpus(def);
-
- if (ncpumaps < 1)
- goto cleanup;
-
- for (i = 0; i < ncpumaps; i++) {
- virDomainVcpuInfoPtr vcpu = virDomainDefGetVcpu(def, i);
- virBitmapPtr bitmap = NULL;
-
- if (!vcpu->online)
- continue;
-
- if (vcpu->cpumask)
- bitmap = vcpu->cpumask;
- else if (vm->def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO
&&
- priv->autoCpuset)
- bitmap = priv->autoCpuset;
- else if (def->cpumask)
- bitmap = def->cpumask;
- else
- bitmap = allcpumap;
-
- virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
- }
-
- ret = ncpumaps;
-
+ ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
+ nodeGetCPUCount(NULL),
+ priv->autoCpuset);
cleanup:
- virBitmapFree(allcpumap);
virDomainObjEndAPI(&vm);
return ret;
}
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index 21c66db..a51eb09 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -2534,11 +2534,10 @@ testDomainGetVcpuPinInfo(virDomainPtr dom,
int maplen,
unsigned int flags)
{
- testDriverPtr privconn = dom->conn->privateData;
+ testDriverPtr driver = dom->conn->privateData;
virDomainObjPtr privdom;
virDomainDefPtr def;
- int ret = -1, hostcpus, vcpu;
- virBitmapPtr allcpumap = NULL;
+ int ret = -1;
if (!(privdom = testDomObjFromDomain(dom)))
return -1;
@@ -2546,38 +2545,11 @@ testDomainGetVcpuPinInfo(virDomainPtr dom,
if (!(def = virDomainObjGetOneDef(privdom, flags)))
goto cleanup;
- hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
-
- if (!(allcpumap = virBitmapNew(hostcpus)))
- goto cleanup;
-
- virBitmapSetAll(allcpumap);
-
- /* Clamp to actual number of vcpus */
- if (ncpumaps > virDomainDefGetVcpus(def))
- ncpumaps = virDomainDefGetVcpus(def);
-
- for (vcpu = 0; vcpu < ncpumaps; vcpu++) {
- virDomainVcpuInfoPtr vcpuinfo = virDomainDefGetVcpu(def, vcpu);
- virBitmapPtr bitmap = NULL;
-
- if (!vcpuinfo->online)
- continue;
-
- if (vcpuinfo->cpumask)
- bitmap = vcpuinfo->cpumask;
- else if (def->cpumask)
- bitmap = def->cpumask;
- else
- bitmap = allcpumap;
-
- virBitmapToDataBuf(bitmap, VIR_GET_CPUMAP(cpumaps, maplen, vcpu), maplen);
- }
-
- ret = ncpumaps;
+ ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
+ VIR_NODEINFO_MAXCPUS(driver->nodeInfo),
+ NULL);
cleanup:
- virBitmapFree(allcpumap);
virDomainObjEndAPI(&privdom);
return ret;
}
--
2.6.2