diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index d376a2cfc3..9bb12044c0 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 cb7b0e2366..146d90663c 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3132,4 +3132,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 c8a2f0a9b1..4644203ce9 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 b9aa32ab30..87ec5a530c 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -2388,8 +2388,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); @@ -2410,41 +2409,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 a90c1b8baa..3b02cdbe17 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -5153,9 +5153,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 | @@ -5170,46 +5167,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 21c66dbb66..a51eb09993 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; }