From f970d8481ecadf9928ad23b6191ebaf04a5c6698 Mon Sep 17 00:00:00 2001 From: Hu Tao Date: Fri, 14 Sep 2012 15:46:58 +0800 Subject: [PATCH] use virBitmap to store cpupin info --- src/conf/domain_conf.c | 148 +++++++++++++-------------------------- src/conf/domain_conf.h | 6 +- src/libvirt_private.syms | 1 + src/qemu/qemu_cgroup.c | 3 +- src/qemu/qemu_driver.c | 29 ++++---- src/qemu/qemu_process.c | 20 +++--- 6 files changed, 80 insertions(+), 127 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index b43195ff5b..c66262ba55 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -52,6 +52,7 @@ #include "netdev_bandwidth_conf.h" #include "netdev_vlan_conf.h" #include "device_conf.h" +#include "bitmap.h" #define VIR_FROM_THIS VIR_FROM_DOMAIN @@ -1529,10 +1530,9 @@ virDomainVcpuPinDefCopy(virDomainVcpuPinDefPtr *src, int nvcpupin) for (i = 0; i < nvcpupin; i++) { if (VIR_ALLOC(ret[i]) < 0) goto no_memory; - if (VIR_ALLOC_N(ret[i]->cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0) - goto no_memory; ret[i]->vcpuid = src[i]->vcpuid; - memcpy(ret[i]->cpumask, src[i]->cpumask, VIR_DOMAIN_CPUMASK_LEN); + if ((ret[i]->cpumask = virBitmapNewCopy(src[i]->cpumask)) == NULL) + goto no_memory; } return ret; @@ -1541,7 +1541,7 @@ no_memory: if (ret) { for ( ; i >= 0; --i) { if (ret[i]) { - VIR_FREE(ret[i]->cpumask); + virBitmapFree(ret[i]->cpumask); VIR_FREE(ret[i]); } } @@ -1553,8 +1553,17 @@ no_memory: } void -virDomainVcpuPinDefFree(virDomainVcpuPinDefPtr *def, - int nvcpupin) +virDomainVcpuPinDefFree(virDomainVcpuPinDefPtr def) +{ + if (def) { + virBitmapFree(def->cpumask); + VIR_FREE(def); + } +} + +void +virDomainVcpuPinDefArrayFree(virDomainVcpuPinDefPtr *def, + int nvcpupin) { int i; @@ -1562,8 +1571,7 @@ virDomainVcpuPinDefFree(virDomainVcpuPinDefPtr *def, return; for(i = 0; i < nvcpupin; i++) { - VIR_FREE(def[i]->cpumask); - VIR_FREE(def[i]); + virDomainVcpuPinDefFree(def[i]); } VIR_FREE(def); @@ -1687,7 +1695,9 @@ void virDomainDefFree(virDomainDefPtr def) virCPUDefFree(def->cpu); - virDomainVcpuPinDefFree(def->cputune.vcpupin, def->cputune.nvcpupin); + virDomainVcpuPinDefArrayFree(def->cputune.vcpupin, def->cputune.nvcpupin); + + virDomainVcpuPinDefFree(def->cputune.emulatorpin); VIR_FREE(def->numatune.memory.nodemask); @@ -8252,12 +8262,8 @@ virDomainVcpuPinDefParseXML(const xmlNodePtr node, char *set = tmp; int cpumasklen = VIR_DOMAIN_CPUMASK_LEN; - if (VIR_ALLOC_N(def->cpumask, cpumasklen) < 0) { - virReportOOMError(); - goto error; - } - if (virDomainCpuSetParse(set, 0, def->cpumask, - cpumasklen) < 0) + if (virBitmapParse(set, 0, &def->cpumask, + cpumasklen) < 0) goto error; VIR_FREE(tmp); } else { @@ -8692,18 +8698,11 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps, goto error; } - if (VIR_ALLOC(def->cputune.emulatorpin) < 0) { - goto no_memory; - } + def->cputune.emulatorpin = virDomainVcpuPinDefParseXML(nodes[0], ctxt, + def->maxvcpus, 1); - virDomainVcpuPinDefPtr emulatorpin = NULL; - emulatorpin = virDomainVcpuPinDefParseXML(nodes[0], ctxt, - def->maxvcpus, 1); - - if (!emulatorpin) + if (!def->cputune.emulatorpin) goto error; - - def->cputune.emulatorpin = emulatorpin; } VIR_FREE(nodes); @@ -11422,34 +11421,6 @@ virDomainVcpuPinFindByVcpu(virDomainVcpuPinDefPtr *def, return NULL; } -static char *bitmapFromBytemap(unsigned char *bytemap, int maplen) -{ - char *bitmap = NULL; - int i; - - if (VIR_ALLOC_N(bitmap, VIR_DOMAIN_CPUMASK_LEN) < 0) { - virReportOOMError(); - goto cleanup; - } - - /* Reset bitmap to all 0s. */ - for (i = 0; i < VIR_DOMAIN_CPUMASK_LEN; i++) - bitmap[i] = 0; - - /* Convert bitmap (bytemap) to bitmap, which is byte map? */ - for (i = 0; i < maplen; i++) { - int cur; - - for (cur = 0; cur < 8; cur++) { - if (bytemap[i] & (1 << cur)) - bitmap[i * 8 + cur] = 1; - } - } - -cleanup: - return bitmap; -} - int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr **vcpupin_list, int *nvcpupin, unsigned char *cpumap, @@ -11457,20 +11428,21 @@ int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr **vcpupin_list, int vcpu) { virDomainVcpuPinDefPtr vcpupin = NULL; - char *cpumask = NULL; if (!vcpupin_list) return -1; - if ((cpumask = bitmapFromBytemap(cpumap, maplen)) == NULL) - return -1; - vcpupin = virDomainVcpuPinFindByVcpu(*vcpupin_list, *nvcpupin, vcpu); if (vcpupin) { vcpupin->vcpuid = vcpu; - vcpupin->cpumask = cpumask; + virBitmapFree(vcpupin->cpumask); + vcpupin->cpumask = virBitmapNewData(cpumap, maplen); + if (!vcpupin->cpumask) { + virReportOOMError(); + return -1; + } return 0; } @@ -11479,16 +11451,17 @@ int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr **vcpupin_list, if (VIR_ALLOC(vcpupin) < 0) { virReportOOMError(); - VIR_FREE(cpumask); return -1; } vcpupin->vcpuid = vcpu; - vcpupin->cpumask = cpumask; - + vcpupin->cpumask = virBitmapNewData(cpumap, maplen); + if (!vcpupin->cpumask) { + virReportOOMError(); + return -1; + } if (VIR_REALLOC_N(*vcpupin_list, *nvcpupin + 1) < 0) { virReportOOMError(); - VIR_FREE(cpumask); VIR_FREE(vcpupin); return -1; } @@ -11543,68 +11516,43 @@ virDomainEmulatorPinAdd(virDomainDefPtr def, int maplen) { virDomainVcpuPinDefPtr emulatorpin = NULL; - char *cpumask = NULL; - int i; - - if (VIR_ALLOC_N(cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0) { - virReportOOMError(); - goto cleanup; - } - - /* Convert bitmap (cpumap) to cpumask, which is byte map. */ - for (i = 0; i < maplen; i++) { - int cur; - - for (cur = 0; cur < 8; cur++) { - if (cpumap[i] & (1 << cur)) - cpumask[i * 8 + cur] = 1; - } - } if (!def->cputune.emulatorpin) { /* No emulatorpin exists yet. */ if (VIR_ALLOC(emulatorpin) < 0) { virReportOOMError(); - goto cleanup; + return -1; } emulatorpin->vcpuid = -1; - emulatorpin->cpumask = cpumask; + emulatorpin->cpumask = virBitmapNewData(cpumap, maplen); + if (!emulatorpin->cpumask) + return -1; + def->cputune.emulatorpin = emulatorpin; } else { /* Since there is only 1 emulatorpin for each vm, * juest replace the old one. */ - VIR_FREE(def->cputune.emulatorpin->cpumask); - def->cputune.emulatorpin->cpumask = cpumask; + virBitmapFree(def->cputune.emulatorpin->cpumask); + def->cputune.emulatorpin->cpumask = virBitmapNewData(cpumap, maplen); + if (!def->cputune.emulatorpin->cpumask) + return -1; } return 0; - -cleanup: - VIR_FREE(cpumask); - return -1; } int virDomainEmulatorPinDel(virDomainDefPtr def) { - virDomainVcpuPinDefPtr emulatorpin = NULL; - - /* No emulatorpin exists yet */ if (!def->cputune.emulatorpin) { return 0; } - emulatorpin = def->cputune.emulatorpin; - - VIR_FREE(emulatorpin->cpumask); - VIR_FREE(emulatorpin); + virDomainVcpuPinDefFree(def->cputune.emulatorpin); def->cputune.emulatorpin = NULL; - if (def->cputune.emulatorpin) - return -1; - return 0; } @@ -13610,8 +13558,7 @@ virDomainDefFormatInternal(virDomainDefPtr def, def->cputune.vcpupin[i]->vcpuid); char *cpumask = NULL; - cpumask = virDomainCpuSetFormat(def->cputune.vcpupin[i]->cpumask, - VIR_DOMAIN_CPUMASK_LEN); + cpumask = virBitmapFormat(def->cputune.vcpupin[i]->cpumask); if (cpumask == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -13628,8 +13575,7 @@ virDomainDefFormatInternal(virDomainDefPtr def, virBufferAsprintf(buf, " cputune.emulatorpin->cpumask, - VIR_DOMAIN_CPUMASK_LEN); + cpumask = virBitmapFormat(def->cputune.emulatorpin->cpumask); if (cpumask == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to format cpuset for emulator")); diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index b6fb0dbbff..f6e5a5ab35 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -46,6 +46,7 @@ # include "virnetdevvlan.h" # include "virobject.h" # include "device_conf.h" +# include "bitmap.h" /* forward declarations of all device types, required by * virDomainDeviceDef @@ -1565,10 +1566,11 @@ typedef struct _virDomainVcpuPinDef virDomainVcpuPinDef; typedef virDomainVcpuPinDef *virDomainVcpuPinDefPtr; struct _virDomainVcpuPinDef { int vcpuid; - char *cpumask; + virBitmapPtr cpumask; }; -void virDomainVcpuPinDefFree(virDomainVcpuPinDefPtr *def, int nvcpupin); +void virDomainVcpuPinDefFree(virDomainVcpuPinDefPtr def); +void virDomainVcpuPinDefArrayFree(virDomainVcpuPinDefPtr *def, int nvcpupin); virDomainVcpuPinDefPtr *virDomainVcpuPinDefCopy(virDomainVcpuPinDefPtr *src, int nvcpupin); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6725912860..d9284844cb 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -527,6 +527,7 @@ virDomainTimerTickpolicyTypeToString; virDomainTimerTrackTypeFromString; virDomainTimerTrackTypeToString; virDomainVcpuPinAdd; +virDomainVcpuPinDefArrayFree; virDomainVcpuPinDefCopy; virDomainVcpuPinDefFree; virDomainVcpuPinDel; diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index 71558c3a1b..7c331556ed 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -513,8 +513,7 @@ int qemuSetupCgroupEmulatorPin(virCgroupPtr cgroup, int rc = 0; char *new_cpus = NULL; - new_cpus = virDomainCpuSetFormat(vcpupin->cpumask, - VIR_DOMAIN_CPUMASK_LEN); + new_cpus = virBitmapFormat(vcpupin->cpumask); if (!new_cpus) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to convert cpu mask")); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 0f7ae10c77..e1729fa167 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -3790,7 +3790,7 @@ qemudDomainPinVcpuFlags(virDomainPtr dom, if (virDomainVcpuPinAdd(&newVcpuPin, &newVcpuPinNum, cpumap, maplen, vcpu) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to update vcpupin")); - virDomainVcpuPinDefFree(newVcpuPin, newVcpuPinNum); + virDomainVcpuPinDefArrayFree(newVcpuPin, newVcpuPinNum); goto cleanup; } @@ -3823,7 +3823,7 @@ qemudDomainPinVcpuFlags(virDomainPtr dom, } } else { if (vm->def->cputune.vcpupin) - virDomainVcpuPinDefFree(vm->def->cputune.vcpupin, vm->def->cputune.nvcpupin); + virDomainVcpuPinDefArrayFree(vm->def->cputune.vcpupin, vm->def->cputune.nvcpupin); vm->def->cputune.vcpupin = newVcpuPin; vm->def->cputune.nvcpupin = newVcpuPinNum; @@ -3831,7 +3831,7 @@ qemudDomainPinVcpuFlags(virDomainPtr dom, } if (newVcpuPin) - virDomainVcpuPinDefFree(newVcpuPin, newVcpuPinNum); + virDomainVcpuPinDefArrayFree(newVcpuPin, newVcpuPinNum); if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0) goto cleanup; @@ -3906,8 +3906,9 @@ qemudDomainGetVcpuPinInfo(virDomainPtr dom, int maxcpu, hostcpus, vcpu, pcpu; int n; virDomainVcpuPinDefPtr *vcpupin_list; - char *cpumask = NULL; + virBitmapPtr cpumask = NULL; unsigned char *cpumap; + bool pinned; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -3966,7 +3967,9 @@ qemudDomainGetVcpuPinInfo(virDomainPtr dom, cpumask = vcpupin_list[n]->cpumask; cpumap = VIR_GET_CPUMAP(cpumaps, maplen, vcpu); for (pcpu = 0; pcpu < maxcpu; pcpu++) { - if (cpumask[pcpu] == 0) + if (virBitmapGetBit(cpumask, pcpu, &pinned) < 0) + goto cleanup; + if (!pinned) VIR_UNUSE_CPU(cpumap, pcpu); } } @@ -4050,7 +4053,7 @@ qemudDomainPinEmulator(virDomainPtr dom, if (virDomainVcpuPinAdd(&newVcpuPin, &newVcpuPinNum, cpumap, maplen, -1) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to update vcpupin")); - virDomainVcpuPinDefFree(newVcpuPin, newVcpuPinNum); + virDomainVcpuPinDefArrayFree(newVcpuPin, newVcpuPinNum); goto cleanup; } @@ -4088,16 +4091,13 @@ qemudDomainPinEmulator(virDomainPtr dom, goto cleanup; } } else { - if (vm->def->cputune.emulatorpin) { - VIR_FREE(vm->def->cputune.emulatorpin->cpumask); - VIR_FREE(vm->def->cputune.emulatorpin); - } + virDomainVcpuPinDefFree(vm->def->cputune.emulatorpin); vm->def->cputune.emulatorpin = newVcpuPin[0]; VIR_FREE(newVcpuPin); } if (newVcpuPin) - virDomainVcpuPinDefFree(newVcpuPin, newVcpuPinNum); + virDomainVcpuPinDefArrayFree(newVcpuPin, newVcpuPinNum); } else { virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("cpu affinity is not supported")); @@ -4156,7 +4156,8 @@ qemudDomainGetEmulatorPinInfo(virDomainPtr dom, int ret = -1; int maxcpu, hostcpus, pcpu; virDomainVcpuPinDefPtr emulatorpin = NULL; - char *cpumask = NULL; + virBitmapPtr cpumask = NULL; + bool pinned; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4205,7 +4206,9 @@ qemudDomainGetEmulatorPinInfo(virDomainPtr dom, cpumask = emulatorpin->cpumask; for (pcpu = 0; pcpu < maxcpu; pcpu++) { - if (cpumask[pcpu] == 0) + if (virBitmapGetBit(cpumask, pcpu, &pinned) < 0) + goto cleanup; + if (!pinned) VIR_UNUSE_CPU(cpumaps, pcpu); } diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 5ac1d2b04c..4a42956e72 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -1959,7 +1959,7 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn, virDomainDefPtr def = vm->def; virNodeInfo nodeinfo; pid_t vcpupid; - unsigned char *cpumask; + virBitmapPtr cpumask; int vcpu, cpumaplen, hostcpus, maxcpu, n; unsigned char *cpumap = NULL; int ret = -1; @@ -1994,13 +1994,12 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn, vcpu = def->cputune.vcpupin[n]->vcpuid; memset(cpumap, 0, cpumaplen); - cpumask = (unsigned char *)def->cputune.vcpupin[n]->cpumask; + cpumask = def->cputune.vcpupin[n]->cpumask; vcpupid = priv->vcpupids[vcpu]; - for (i = 0 ; i < VIR_DOMAIN_CPUMASK_LEN ; i++) { - if (cpumask[i]) - VIR_USE_CPU(cpumap, i); - } + i = -1; + while ((i = virBitmapNextSetBit(cpumask, i)) >= 0) + VIR_USE_CPU(cpumap, i); if (virProcessInfoSetAffinity(vcpupid, cpumap, @@ -2023,11 +2022,12 @@ qemuProcessSetEmulatorAffinites(virConnectPtr conn, { virDomainDefPtr def = vm->def; pid_t pid = vm->pid; - unsigned char *cpumask = NULL; + virBitmapPtr cpumask = NULL; unsigned char *cpumap = NULL; virNodeInfo nodeinfo; int cpumaplen, hostcpus, maxcpu, i; int ret = -1; + bool result; if (virNodeGetInfo(conn, &nodeinfo) != 0) return -1; @@ -2047,9 +2047,11 @@ qemuProcessSetEmulatorAffinites(virConnectPtr conn, return -1; } - cpumask = (unsigned char *)def->cputune.emulatorpin->cpumask; + cpumask = def->cputune.emulatorpin->cpumask; for (i = 0; i < VIR_DOMAIN_CPUMASK_LEN; i++) { - if (cpumask[i]) + if (virBitmapGetBit(cpumask, i, &result) < 0) + goto cleanup; + if (result) VIR_USE_CPU(cpumap, i); } -- GitLab