From fd8fd0f916a63656a67300b6435073cf45d64842 Mon Sep 17 00:00:00 2001 From: Hu Tao Date: Tue, 21 Aug 2012 17:18:28 +0800 Subject: [PATCH] refactor virDomainVcpuPinAdd() --- src/conf/domain_conf.c | 81 ++++++++++++++++------------------------ src/conf/domain_conf.h | 3 +- src/libxl/libxl_driver.c | 13 ++++++- src/qemu/qemu_driver.c | 26 ++++++++++++- src/xen/xend_internal.c | 13 ++++++- 5 files changed, 82 insertions(+), 54 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index d2b0284115..38522fd64c 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10963,69 +10963,52 @@ cleanup: return bitmap; } -int -virDomainVcpuPinAdd(virDomainDefPtr def, - unsigned char *cpumap, - int maplen, - int vcpu) +int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr *vcpupin_list, + int *nvcpupin, + unsigned char *cpumap, + int maplen, + int vcpu) { - virDomainVcpuPinDefPtr *vcpupin_list = NULL; virDomainVcpuPinDefPtr vcpupin = NULL; char *cpumask = NULL; - if ((cpumask = bitmapFromBytemap(cpumap, maplen)) == NULL) - goto cleanup; - - /* No vcpupin exists yet. */ - if (!def->cputune.nvcpupin) { - if (VIR_ALLOC(vcpupin) < 0) { - virReportOOMError(); - goto cleanup; - } + if (!vcpupin_list) + return -1; - if (VIR_ALLOC(vcpupin_list) < 0) { - virReportOOMError(); - VIR_FREE(vcpupin); - goto cleanup; - } + if ((cpumask = bitmapFromBytemap(cpumap, maplen)) == NULL) + return -1; + vcpupin = virDomainVcpuPinFindByVcpu(vcpupin_list, + *nvcpupin, + vcpu); + if (vcpupin) { vcpupin->vcpuid = vcpu; vcpupin->cpumask = cpumask; - vcpupin_list[def->cputune.nvcpupin++] = vcpupin; - def->cputune.vcpupin = vcpupin_list; - } else { - if (virDomainVcpuPinIsDuplicate(def->cputune.vcpupin, - def->cputune.nvcpupin, - vcpu)) { - vcpupin = virDomainVcpuPinFindByVcpu(def->cputune.vcpupin, - def->cputune.nvcpupin, - vcpu); - vcpupin->vcpuid = vcpu; - vcpupin->cpumask = cpumask; - } else { - if (VIR_ALLOC(vcpupin) < 0) { - virReportOOMError(); - goto cleanup; - } + return 0; + } + + /* No existing vcpupin matches vcpu, adding a new one */ + + if (VIR_ALLOC(vcpupin) < 0) { + virReportOOMError(); + VIR_FREE(cpumask); + return -1; + } + vcpupin->vcpuid = vcpu; + vcpupin->cpumask = cpumask; - if (VIR_REALLOC_N(def->cputune.vcpupin, def->cputune.nvcpupin + 1) < 0) { - virReportOOMError(); - VIR_FREE(vcpupin); - goto cleanup; - } - vcpupin->vcpuid = vcpu; - vcpupin->cpumask = cpumask; - def->cputune.vcpupin[def->cputune.nvcpupin++] = vcpupin; - } + if (VIR_REALLOC_N(vcpupin_list, *nvcpupin + 1) < 0) { + virReportOOMError(); + VIR_FREE(cpumask); + VIR_FREE(vcpupin); + return -1; } - return 0; + vcpupin_list[(*nvcpupin)++] = vcpupin; -cleanup: - VIR_FREE(cpumask); - return -1; + return 0; } int diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index ce3c0fc953..bf2585a69e 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1984,7 +1984,8 @@ int virDomainCpuSetParse(const char *str, char *virDomainCpuSetFormat(char *cpuset, int maxcpu); -int virDomainVcpuPinAdd(virDomainDefPtr def, +int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr *vcpupin_list, + int *nvcpupin, unsigned char *cpumap, int maplen, int vcpu); diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 398a9a2ea4..d8ecf13304 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -2454,7 +2454,18 @@ libxlDomainPinVcpu(virDomainPtr dom, unsigned int vcpu, unsigned char *cpumap, goto cleanup; } - if (virDomainVcpuPinAdd(vm->def, cpumap, maplen, vcpu) < 0) { + if (!vm->def->cputune.vcpupin) { + if (VIR_ALLOC(vm->def->cputune.vcpupin) < 0) { + virReportOOMError(); + goto cleanup; + } + vm->def->cputune.nvcpupin = 0; + } + if (virDomainVcpuPinAdd(vm->def->cputune.vcpupin, + &vm->def->cputune.nvcpupin, + cpumap, + maplen, + vcpu) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to update or add vcpupin xml")); goto cleanup; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index f94ddb18ff..0c6be6bbe6 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -3787,7 +3787,18 @@ qemudDomainPinVcpuFlags(virDomainPtr dom, goto cleanup; } } else { - if (virDomainVcpuPinAdd(vm->def, cpumap, maplen, vcpu) < 0) { + if (!vm->def->cputune.vcpupin) { + if (VIR_ALLOC(vm->def->cputune.vcpupin) < 0) { + virReportOOMError(); + goto cleanup; + } + vm->def->cputune.nvcpupin = 0; + } + if (virDomainVcpuPinAdd(vm->def->cputune.vcpupin, + &vm->def->cputune.nvcpupin, + cpumap, + maplen, + vcpu) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to update or add vcpupin xml of " "a running domain")); @@ -3809,7 +3820,18 @@ qemudDomainPinVcpuFlags(virDomainPtr dom, goto cleanup; } } else { - if (virDomainVcpuPinAdd(persistentDef, cpumap, maplen, vcpu) < 0) { + if (!persistentDef->cputune.vcpupin) { + if (VIR_ALLOC(persistentDef->cputune.vcpupin) < 0) { + virReportOOMError(); + goto cleanup; + } + persistentDef->cputune.nvcpupin = 0; + } + if (virDomainVcpuPinAdd(persistentDef->cputune.vcpupin, + &persistentDef->cputune.nvcpupin, + cpumap, + maplen, + vcpu) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to update or add vcpupin xml of " "a persistent domain")); diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c index f93b249e61..99def429a3 100644 --- a/src/xen/xend_internal.c +++ b/src/xen/xend_internal.c @@ -2296,7 +2296,18 @@ xenDaemonDomainPinVcpu(virDomainPtr domain, unsigned int vcpu, goto cleanup; if (ret == 0) { - if (virDomainVcpuPinAdd(def, cpumap, maplen, vcpu) < 0) { + if (!def->cputune.vcpupin) { + if (VIR_ALLOC(def->cputune.vcpupin) < 0) { + virReportOOMError(); + goto cleanup; + } + def->cputune.nvcpupin = 0; + } + if (virDomainVcpuPinAdd(def->cputune.vcpupin, + &def->cputune.nvcpupin, + cpumap, + maplen, + vcpu) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to add vcpupin xml entry")); return -1; -- GitLab