---
src/lxc/lxc_controller.c | 19 +++++-------
src/qemu/qemu_driver.c | 54 ++++++++++++++++++++-------------
src/qemu/qemu_process.c | 75 ++++++++++++----------------------------------
src/util/processinfo.c | 36 ++++++++++++----------
src/util/processinfo.h | 9 ++----
5 files changed, 83 insertions(+), 110 deletions(-)
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index e5aea11..d6298b0 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -493,8 +493,7 @@ static int virLXCControllerSetupCpuAffinity(virLXCControllerPtr ctrl)
{
int i, hostcpus, maxcpu = CPU_SETSIZE;
virNodeInfo nodeinfo;
- unsigned char *cpumap;
- int cpumaplen;
+ virBitmapPtr cpumap;
VIR_DEBUG("Setting CPU affinity");
@@ -507,37 +506,33 @@ static int virLXCControllerSetupCpuAffinity(virLXCControllerPtr
ctrl)
if (maxcpu > hostcpus)
maxcpu = hostcpus;
- cpumaplen = VIR_CPU_MAPLEN(maxcpu);
- if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
- virReportOOMError();
+ cpumap = virBitmapAlloc(maxcpu);
+ if (!cpumap)
return -1;
- }
if (ctrl->def->cpumask) {
/* XXX why don't we keep 'cpumask' in the libvirt cpumap
* format to start with ?!?! */
for (i = 0 ; i < maxcpu && i < ctrl->def->cpumasklen ; i++)
if (ctrl->def->cpumask[i])
- VIR_USE_CPU(cpumap, i);
+ ignore_value(virBitmapSetBit(cpumap, i));
} else {
/* You may think this is redundant, but we can't assume libvirtd
* itself is running on all pCPUs, so we need to explicitly set
* the spawned LXC instance to all pCPUs if no map is given in
* its config file */
- for (i = 0 ; i < maxcpu ; i++)
- VIR_USE_CPU(cpumap, i);
+ virBitmapSetAll(cpumap);
}
/* We are pressuming we are running between fork/exec of LXC
* so use '0' to indicate our own process ID. No threads are
* running at this point
*/
- if (virProcessInfoSetAffinity(0, /* Self */
- cpumap, cpumaplen, maxcpu) < 0) {
+ if (virProcessInfoSetAffinity(0 /* Self */, cpumap) < 0) {
VIR_FREE(cpumap);
return -1;
}
- VIR_FREE(cpumap);
+ virBitmapFree(cpumap);
return 0;
}
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index ae7cc02..cb7b156 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -92,6 +92,7 @@
#include "virnodesuspend.h"
#include "virtime.h"
#include "virtypedparam.h"
+#include "bitmap.h"
#define VIR_FROM_THIS VIR_FROM_QEMU
@@ -3762,10 +3763,10 @@ qemudDomainPinVcpuFlags(virDomainPtr dom,
virNodeInfo nodeinfo;
int ret = -1;
qemuDomainObjPrivatePtr priv;
- bool canResetting = true;
+ bool canResetting = false;
int newVcpuPinNum = 0;
virDomainVcpuPinDefPtr *newVcpuPin = NULL;
- int pcpu;
+ virBitmapPtr pcpumap = NULL;
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -3801,15 +3802,16 @@ qemudDomainPinVcpuFlags(virDomainPtr dom,
maxcpu = maplen * 8;
if (maxcpu > hostcpus)
maxcpu = hostcpus;
+
+ pcpumap = virBitmapAllocFromData(cpumap, maplen);
+ if (!pcpumap)
+ goto cleanup;
+
/* pinning to all physical cpus means resetting,
* so check if we can reset setting.
*/
- for (pcpu = 0; pcpu < hostcpus; pcpu++) {
- if ((cpumap[pcpu/8] & (1 << (pcpu % 8))) == 0) {
- canResetting = false;
- break;
- }
- }
+ if (virBitmapIsAllSet(pcpumap))
+ canResetting = true;
if (flags & VIR_DOMAIN_AFFECT_LIVE) {
@@ -3852,8 +3854,7 @@ qemudDomainPinVcpuFlags(virDomainPtr dom,
goto cleanup;
}
} else {
- if (virProcessInfoSetAffinity(priv->vcpupids[vcpu],
- cpumap, maplen, maxcpu) < 0) {
+ if (virProcessInfoSetAffinity(priv->vcpupids[vcpu], pcpumap) < 0) {
virReportError(VIR_ERR_SYSTEM_ERROR,
_("failed to set cpu affinity for vcpu %d"),
vcpu);
@@ -3926,6 +3927,7 @@ cleanup:
virCgroupFree(&cgroup_dom);
if (vm)
virDomainObjUnlock(vm);
+ virBitmapFree(pcpumap);
return ret;
}
@@ -4044,10 +4046,10 @@ qemudDomainPinEmulator(virDomainPtr dom,
virNodeInfo nodeinfo;
int ret = -1;
qemuDomainObjPrivatePtr priv;
- bool canResetting = true;
- int pcpu;
+ bool canResetting = false;
int newVcpuPinNum = 0;
virDomainVcpuPinDefPtr *newVcpuPin = NULL;
+ virBitmapPtr pcpumap = NULL;
virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -4076,15 +4078,16 @@ qemudDomainPinEmulator(virDomainPtr dom,
maxcpu = maplen * 8;
if (maxcpu > hostcpus)
maxcpu = hostcpus;
+
+ pcpumap = virBitmapAllocFromData(cpumap, maplen);
+ if (!pcpumap)
+ goto cleanup;
+
/* pinning to all physical cpus means resetting,
* so check if we can reset setting.
*/
- for (pcpu = 0; pcpu < hostcpus; pcpu++) {
- if ((cpumap[pcpu/8] & (1 << (pcpu % 8))) == 0) {
- canResetting = false;
- break;
- }
- }
+ if (virBitmapIsAllSet(pcpumap))
+ canResetting = true;
pid = vm->pid;
@@ -4122,7 +4125,7 @@ qemudDomainPinEmulator(virDomainPtr dom,
}
}
} else {
- if (virProcessInfoSetAffinity(pid, cpumap, maplen, maxcpu) < 0) {
+ if (virProcessInfoSetAffinity(pid, pcpumap) < 0) {
virReportError(VIR_ERR_SYSTEM_ERROR, "%s",
_("failed to set cpu affinity for "
"emulator threads"));
@@ -4187,6 +4190,7 @@ cleanup:
virCgroupFree(&cgroup_emulator);
if (cgroup_dom)
virCgroupFree(&cgroup_dom);
+ virBitmapFree(pcpumap);
if (vm)
virDomainObjUnlock(vm);
@@ -4341,10 +4345,20 @@ qemudDomainGetVcpus(virDomainPtr dom,
if (priv->vcpupids != NULL) {
for (v = 0 ; v < maxinfo ; v++) {
unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);
+ virBitmapPtr map = NULL;
+ char *tmpmap = NULL;
+ int tmpmapLen = 0;
if (virProcessInfoGetAffinity(priv->vcpupids[v],
- cpumap, maplen, maxcpu) < 0)
+ &map, maxcpu) < 0)
goto cleanup;
+ virBitmapToData(map, &tmpmap, &tmpmapLen);
+ if (tmpmapLen > maplen)
+ tmpmapLen = maplen;
+ memcpy(cpumap, tmpmap, tmpmapLen);
+
+ VIR_FREE(tmpmap);
+ virBitmapFree(map);
}
} else {
virReportError(VIR_ERR_OPERATION_INVALID,
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index e153d98..d4baaa5 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -1839,8 +1839,7 @@ qemuProcessInitCpuAffinity(struct qemud_driver *driver,
int ret = -1;
int i, hostcpus, maxcpu = QEMUD_CPUMASK_LEN;
virNodeInfo nodeinfo;
- unsigned char *cpumap;
- int cpumaplen;
+ virBitmapPtr cpumap;
VIR_DEBUG("Setting CPU affinity");
@@ -1853,8 +1852,8 @@ qemuProcessInitCpuAffinity(struct qemud_driver *driver,
if (maxcpu > hostcpus)
maxcpu = hostcpus;
- cpumaplen = VIR_CPU_MAPLEN(maxcpu);
- if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
+ cpumap = virBitmapAlloc(maxcpu);
+ if (!cpumap) {
virReportOOMError();
return -1;
}
@@ -1867,7 +1866,8 @@ qemuProcessInitCpuAffinity(struct qemud_driver *driver,
int cur_ncpus = driver->caps->host.numaCell[i]->ncpus;
if (nodemask[i]) {
for (j = 0; j < cur_ncpus; j++)
- VIR_USE_CPU(cpumap,
driver->caps->host.numaCell[i]->cpus[j]);
+ ignore_value(virBitmapSetBit(cpumap,
+
driver->caps->host.numaCell[i]->cpus[j]));
}
}
} else {
@@ -1877,14 +1877,13 @@ qemuProcessInitCpuAffinity(struct qemud_driver *driver,
* format to start with ?!?! */
for (i = 0 ; i < maxcpu && i < vm->def->cpumasklen ;
i++)
if (vm->def->cpumask[i])
- VIR_USE_CPU(cpumap, i);
+ ignore_value(virBitmapSetBit(cpumap, i));
} else {
/* You may think this is redundant, but we can't assume libvirtd
* itself is running on all pCPUs, so we need to explicitly set
* the spawned QEMU instance to all pCPUs if no map is given in
* its config file */
- for (i = 0 ; i < maxcpu ; i++)
- VIR_USE_CPU(cpumap, i);
+ virBitmapSetAll(cpumap);
}
}
@@ -1892,14 +1891,13 @@ qemuProcessInitCpuAffinity(struct qemud_driver *driver,
* so use '0' to indicate our own process ID. No threads are
* running at this point
*/
- if (virProcessInfoSetAffinity(0, /* Self */
- cpumap, cpumaplen, maxcpu) < 0)
+ if (virProcessInfoSetAffinity(0 /* Self */, cpumap) < 0)
goto cleanup;
ret = 0;
cleanup:
- VIR_FREE(cpumap);
+ virBitmapFree(cpumap);
return ret;
}
@@ -1946,10 +1944,8 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn,
virNodeInfo nodeinfo;
pid_t vcpupid;
virBitmapPtr cpumask;
- int vcpu, cpumaplen, hostcpus, maxcpu, n;
- unsigned char *cpumap = NULL;
+ int vcpu, hostcpus, maxcpu, n;
int ret = -1;
- bool result;
if (virNodeGetInfo(conn, &nodeinfo) != 0) {
return -1;
@@ -1965,43 +1961,26 @@ qemuProcessSetVcpuAffinites(virConnectPtr conn,
}
hostcpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
- cpumaplen = VIR_CPU_MAPLEN(hostcpus);
- maxcpu = cpumaplen * 8;
+ maxcpu = VIR_CPU_MAPLEN(hostcpus) * CHAR_BIT;
if (maxcpu > hostcpus)
maxcpu = hostcpus;
- if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
- virReportOOMError();
- return -1;
- }
-
for (n = 0; n < def->cputune.nvcpupin; n++) {
- int i;
vcpu = def->cputune.vcpupin[n]->vcpuid;
- memset(cpumap, 0, cpumaplen);
- cpumask = def->cputune.vcpupin[n]->cpumask;
+ cpumask = virBitmapCopy(def->cputune.vcpupin[n]->cpumask);
vcpupid = priv->vcpupids[vcpu];
- for (i = 0 ; i < virBitmapSize(cpumask); i++) {
- if (virBitmapGetBit(cpumask, i, &result) < 0)
- goto cleanup;
- if (result)
- VIR_USE_CPU(cpumap, i);
- }
-
+ /* TODO: constrain cpumask from maxcpu */
if (virProcessInfoSetAffinity(vcpupid,
- cpumap,
- cpumaplen,
- maxcpu) < 0) {
+ cpumask) < 0) {
goto cleanup;
}
}
ret = 0;
cleanup:
- VIR_FREE(cpumap);
return ret;
}
@@ -2013,11 +1992,9 @@ qemuProcessSetEmulatorAffinites(virConnectPtr conn,
virDomainDefPtr def = vm->def;
pid_t pid = vm->pid;
virBitmapPtr cpumask = NULL;
- unsigned char *cpumap = NULL;
virNodeInfo nodeinfo;
- int cpumaplen, hostcpus, maxcpu, i;
+ int hostcpus, maxcpu;
int ret = -1;
- bool result;
if (virNodeGetInfo(conn, &nodeinfo) != 0)
return -1;
@@ -2026,35 +2003,21 @@ qemuProcessSetEmulatorAffinites(virConnectPtr conn,
return 0;
hostcpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
- cpumaplen = VIR_CPU_MAPLEN(hostcpus);
- maxcpu = cpumaplen * CHAR_BIT;
+ maxcpu = VIR_CPU_MAPLEN(hostcpus) * CHAR_BIT;
if (maxcpu > hostcpus)
maxcpu = hostcpus;
- if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) {
- virReportOOMError();
- return -1;
- }
-
- cpumask = def->cputune.emulatorpin->cpumask;
- for (i = 0; i < VIR_DOMAIN_CPUMASK_LEN; i++) {
- if (virBitmapGetBit(cpumask, i, &result) < 0)
- goto cleanup;
- if (result)
- VIR_USE_CPU(cpumap, i);
- }
+ cpumask = virBitmapCopy(def->cputune.emulatorpin->cpumask);
+ /* TODO: constrain cpumask from maxcpu */
if (virProcessInfoSetAffinity(pid,
- cpumap,
- cpumaplen,
- maxcpu) < 0) {
+ cpumask) < 0) {
goto cleanup;
}
ret = 0;
cleanup:
- VIR_FREE(cpumap);
return ret;
}
diff --git a/src/util/processinfo.c b/src/util/processinfo.c
index 16ab8af..b7d2ed0 100644
--- a/src/util/processinfo.c
+++ b/src/util/processinfo.c
@@ -30,12 +30,10 @@
#if HAVE_SCHED_GETAFFINITY
-int virProcessInfoSetAffinity(pid_t pid,
- const unsigned char *map,
- size_t maplen,
- int maxcpu)
+int virProcessInfoSetAffinity(pid_t pid, virBitmapPtr map)
{
int i;
+ bool set = false;
# ifdef CPU_ALLOC
/* New method dynamically allocates cpu mask, allowing unlimted cpus */
int numcpus = 1024;
@@ -59,8 +57,10 @@ realloc:
}
CPU_ZERO_S(masklen, mask);
- for (i = 0 ; i < maxcpu ; i++) {
- if (VIR_CPU_USABLE(map, maplen, 0, i))
+ for (i = 0 ; i < virBitmapSize(map); i++) {
+ if (virBitmapGetBit(map, i, &set) < 0)
+ return -1;
+ if (set)
CPU_SET_S(i, masklen, mask);
}
@@ -81,8 +81,10 @@ realloc:
cpu_set_t mask;
CPU_ZERO(&mask);
- for (i = 0 ; i < maxcpu ; i++) {
- if (VIR_CPU_USABLE(map, maplen, 0, i))
+ for (i = 0 ; i < virBitmapSize(map); i++) {
+ if (virBitmapGetBit(map, i, &set) < 0)
+ return -1;
+ if (set)
CPU_SET(i, &mask);
}
@@ -97,8 +99,7 @@ realloc:
}
int virProcessInfoGetAffinity(pid_t pid,
- unsigned char *map,
- size_t maplen ATTRIBUTE_UNUSED,
+ virBitmapPtr *map,
int maxcpu)
{
int i;
@@ -137,9 +138,15 @@ realloc:
return -1;
}
+ *map = virBitmapAlloc(maxcpu);
+ if (!map) {
+ virReportOOMError();
+ return -1;
+ }
+
for (i = 0 ; i < maxcpu ; i++)
if (CPU_ISSET_S(i, masklen, mask))
- VIR_USE_CPU(map, i);
+ ignore_value(virBitmapSetBit(*map, i));
CPU_FREE(mask);
# else
/* Legacy method uses a fixed size cpu mask, only allows upto 1024 cpus */
@@ -163,9 +170,7 @@ realloc:
#else /* HAVE_SCHED_GETAFFINITY */
int virProcessInfoSetAffinity(pid_t pid ATTRIBUTE_UNUSED,
- const unsigned char *map ATTRIBUTE_UNUSED,
- size_t maplen ATTRIBUTE_UNUSED,
- int maxcpu ATTRIBUTE_UNUSED)
+ virBitmapPtr map ATTRIBUTE_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
_("Process CPU affinity is not supported on this
platform"));
@@ -173,8 +178,7 @@ int virProcessInfoSetAffinity(pid_t pid ATTRIBUTE_UNUSED,
}
int virProcessInfoGetAffinity(pid_t pid ATTRIBUTE_UNUSED,
- unsigned char *map ATTRIBUTE_UNUSED,
- size_t maplen ATTRIBUTE_UNUSED,
+ virBitmapPtr *map ATTRIBUTE_UNUSED,
int maxcpu ATTRIBUTE_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
diff --git a/src/util/processinfo.h b/src/util/processinfo.h
index a648bb8..0424643 100644
--- a/src/util/processinfo.h
+++ b/src/util/processinfo.h
@@ -23,15 +23,12 @@
# define __VIR_PROCESSINFO_H__
# include "internal.h"
+# include "bitmap.h"
-int virProcessInfoSetAffinity(pid_t pid,
- const unsigned char *map,
- size_t maplen,
- int maxcpu);
+int virProcessInfoSetAffinity(pid_t pid, virBitmapPtr map);
int virProcessInfoGetAffinity(pid_t pid,
- unsigned char *map,
- size_t maplen,
+ virBitmapPtr *map,
int maxcpu);
#endif /* __VIR_PROCESSINFO_H__ */
--
1.7.10.2