]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: Add helper to prepare cpumap for affinity setting
authorOsier Yang <jyang@redhat.com>
Wed, 24 Oct 2012 09:22:52 +0000 (17:22 +0800)
committerOsier Yang <jyang@redhat.com>
Wed, 24 Oct 2012 13:24:10 +0000 (21:24 +0800)
Abstract the codes to prepare cpumap into a helper a function,
which can be used later.

* src/qemu/qemu_process.h: Declare qemuPrepareCpumap
* src/qemu/qemu_process.c: Implement qemuPrepareCpumap, and use it.

src/qemu/qemu_process.c
src/qemu/qemu_process.h

index 969e3cedefa3cd8f77f36f66ce580117c2729c18..26be35ae83c34fa8cd2370adde88c13edafbfb53 100644 (file)
@@ -1890,23 +1890,20 @@ qemuGetNumadAdvice(virDomainDefPtr def ATTRIBUTE_UNUSED)
 }
 #endif
 
-/*
- * To be run between fork/exec of QEMU only
+/* Helper to prepare cpumap for affinity setting, convert
+ * NUMA nodeset into cpuset if @nodemask is not NULL, otherwise
+ * just return a new allocated bitmap.
  */
-static int
-qemuProcessInitCpuAffinity(struct qemud_driver *driver,
-                           virDomainObjPtr vm,
-                           virBitmapPtr nodemask)
+virBitmapPtr
+qemuPrepareCpumap(struct qemud_driver *driver,
+                  virBitmapPtr nodemask)
 {
-    int ret = -1;
     int i, hostcpus, maxcpu = QEMUD_CPUMASK_LEN;
     virNodeInfo nodeinfo;
-    virBitmapPtr cpumap, cpumapToSet;
-
-    VIR_DEBUG("Setting CPU affinity");
+    virBitmapPtr cpumap = NULL;
 
     if (nodeGetInfo(NULL, &nodeinfo) < 0)
-        return -1;
+        return NULL;
 
     /* setaffinity fails if you set bits for CPUs which
      * aren't present, so we have to limit ourselves */
@@ -1914,34 +1911,57 @@ qemuProcessInitCpuAffinity(struct qemud_driver *driver,
     if (maxcpu > hostcpus)
         maxcpu = hostcpus;
 
-    cpumap = virBitmapNew(maxcpu);
-    if (!cpumap) {
+    if (!(cpumap = virBitmapNew(maxcpu))) {
         virReportOOMError();
-        return -1;
+        return NULL;
     }
 
-    cpumapToSet = cpumap;
-
-    if (vm->def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO) {
-        VIR_DEBUG("Set CPU affinity with advisory nodeset from numad");
-        /* numad returns the NUMA node list, convert it to cpumap */
+    if (nodemask) {
         for (i = 0; i < driver->caps->host.nnumaCell; i++) {
             int j;
             int cur_ncpus = driver->caps->host.numaCell[i]->ncpus;
             bool result;
-            if (virBitmapGetBit(nodemask, i, &result) < 0)
-                goto cleanup;
+            if (virBitmapGetBit(nodemask, i, &result) < 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                               _("Failed to covert nodeset to cpuset"));
+                virBitmapFree(cpumap);
+                return NULL;
+            }
             if (result) {
                 for (j = 0; j < cur_ncpus; j++)
                     ignore_value(virBitmapSetBit(cpumap,
                                                  driver->caps->host.numaCell[i]->cpus[j]));
             }
         }
+    }
+
+    return cpumap;
+}
+
+/*
+ * To be run between fork/exec of QEMU only
+ */
+static int
+qemuProcessInitCpuAffinity(struct qemud_driver *driver,
+                           virDomainObjPtr vm,
+                           virBitmapPtr nodemask)
+{
+    int ret = -1;
+    virBitmapPtr cpumap = NULL;
+    virBitmapPtr cpumapToSet = NULL;
+
+    if (!(cpumap = qemuPrepareCpumap(driver, nodemask)))
+        return -1;
+
+    if (vm->def->placement_mode == VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO) {
+        VIR_DEBUG("Set CPU affinity with advisory nodeset from numad");
+        cpumapToSet = cpumap;
     } else {
         VIR_DEBUG("Set CPU affinity with specified cpuset");
         if (vm->def->cpumask) {
             cpumapToSet = vm->def->cpumask;
         } else {
+            cpumapToSet = cpumap;
             /* 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
index 38edde776bf13cadd22b48333c7478f97198631c..543c9ee66b9bea012e9524e76bc25ad729e3a51c 100644 (file)
@@ -96,5 +96,7 @@ int qemuProcessAutoDestroyRemove(struct qemud_driver *driver,
                                  virDomainObjPtr vm);
 bool qemuProcessAutoDestroyActive(struct qemud_driver *driver,
                                   virDomainObjPtr vm);
+virBitmapPtr qemuPrepareCpumap(struct qemud_driver *driver,
+                               virBitmapPtr nodemask);
 
 #endif /* __QEMU_PROCESS_H__ */