conf: Extract code filling data for virDomainGetVcpuPinInfo

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.
This commit is contained in:
Peter Krempa 2016-02-22 15:29:25 +01:00
parent e7e6980dd8
commit e498e90469
6 changed files with 84 additions and 108 deletions

View File

@ -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 virDomainDiskDefPtr
virDomainDiskDefNew(virDomainXMLOptionPtr xmlopt) virDomainDiskDefNew(virDomainXMLOptionPtr xmlopt)
{ {

View File

@ -3132,4 +3132,12 @@ int virDomainDiskDefCheckDuplicateInfo(virDomainDiskDefPtr a,
int virDomainDefCheckDuplicateDiskInfo(virDomainDefPtr def) int virDomainDefCheckDuplicateDiskInfo(virDomainDefPtr def)
ATTRIBUTE_NONNULL(1); 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 */ #endif /* __DOMAIN_CONF_H */

View File

@ -221,6 +221,7 @@ virDomainDefGetMemoryInitial;
virDomainDefGetOnlineVcpumap; virDomainDefGetOnlineVcpumap;
virDomainDefGetSecurityLabelDef; virDomainDefGetSecurityLabelDef;
virDomainDefGetVcpu; virDomainDefGetVcpu;
virDomainDefGetVcpuPinInfoHelper;
virDomainDefGetVcpus; virDomainDefGetVcpus;
virDomainDefGetVcpusMax; virDomainDefGetVcpusMax;
virDomainDefHasDeviceAddress; virDomainDefHasDeviceAddress;

View File

@ -2388,8 +2388,7 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
virDomainObjPtr vm = NULL; virDomainObjPtr vm = NULL;
virDomainDefPtr targetDef = NULL; virDomainDefPtr targetDef = NULL;
int hostcpus, vcpu, ret = -1; int ret = -1;
virBitmapPtr allcpumap = NULL;
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
VIR_DOMAIN_AFFECT_CONFIG, -1); VIR_DOMAIN_AFFECT_CONFIG, -1);
@ -2410,41 +2409,10 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
/* Make sure coverity knows targetDef is valid at this point. */ /* Make sure coverity knows targetDef is valid at this point. */
sa_assert(targetDef); sa_assert(targetDef);
/* Clamp to actual number of vcpus */ ret = virDomainDefGetVcpuPinInfoHelper(targetDef, maplen, ncpumaps, cpumaps,
if (ncpumaps > virDomainDefGetVcpus(targetDef)) libxl_get_max_cpus(cfg->ctx), NULL);
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;
cleanup: cleanup:
virBitmapFree(allcpumap);
if (vm) if (vm)
virObjectUnlock(vm); virObjectUnlock(vm);
virObjectUnref(cfg); virObjectUnref(cfg);

View File

@ -5153,9 +5153,6 @@ qemuDomainGetVcpuPinInfo(virDomainPtr dom,
virDomainObjPtr vm = NULL; virDomainObjPtr vm = NULL;
virDomainDefPtr def; virDomainDefPtr def;
int ret = -1; int ret = -1;
int hostcpus;
size_t i;
virBitmapPtr allcpumap = NULL;
qemuDomainObjPrivatePtr priv = NULL; qemuDomainObjPrivatePtr priv = NULL;
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
@ -5170,46 +5167,12 @@ qemuDomainGetVcpuPinInfo(virDomainPtr dom,
if (!(def = virDomainObjGetOneDef(vm, flags))) if (!(def = virDomainObjGetOneDef(vm, flags)))
goto cleanup; goto cleanup;
if ((hostcpus = nodeGetCPUCount(NULL)) < 0)
goto cleanup;
if (!(allcpumap = virBitmapNew(hostcpus)))
goto cleanup;
virBitmapSetAll(allcpumap);
priv = vm->privateData; priv = vm->privateData;
/* Clamp to actual number of vcpus */ ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
if (ncpumaps > virDomainDefGetVcpus(def)) nodeGetCPUCount(NULL),
ncpumaps = virDomainDefGetVcpus(def); priv->autoCpuset);
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;
cleanup: cleanup:
virBitmapFree(allcpumap);
virDomainObjEndAPI(&vm); virDomainObjEndAPI(&vm);
return ret; return ret;
} }

View File

@ -2534,11 +2534,10 @@ testDomainGetVcpuPinInfo(virDomainPtr dom,
int maplen, int maplen,
unsigned int flags) unsigned int flags)
{ {
testDriverPtr privconn = dom->conn->privateData; testDriverPtr driver = dom->conn->privateData;
virDomainObjPtr privdom; virDomainObjPtr privdom;
virDomainDefPtr def; virDomainDefPtr def;
int ret = -1, hostcpus, vcpu; int ret = -1;
virBitmapPtr allcpumap = NULL;
if (!(privdom = testDomObjFromDomain(dom))) if (!(privdom = testDomObjFromDomain(dom)))
return -1; return -1;
@ -2546,38 +2545,11 @@ testDomainGetVcpuPinInfo(virDomainPtr dom,
if (!(def = virDomainObjGetOneDef(privdom, flags))) if (!(def = virDomainObjGetOneDef(privdom, flags)))
goto cleanup; goto cleanup;
hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo); ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
VIR_NODEINFO_MAXCPUS(driver->nodeInfo),
if (!(allcpumap = virBitmapNew(hostcpus))) NULL);
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;
cleanup: cleanup:
virBitmapFree(allcpumap);
virDomainObjEndAPI(&privdom); virDomainObjEndAPI(&privdom);
return ret; return ret;
} }