]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Extract code filling data for virDomainGetVcpuPinInfo
authorPeter Krempa <pkrempa@redhat.com>
Mon, 22 Feb 2016 14:29:25 +0000 (15:29 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 9 Mar 2016 09:09:16 +0000 (10:09 +0100)
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.

src/conf/domain_conf.c
src/conf/domain_conf.h
src/libvirt_private.syms
src/libxl/libxl_driver.c
src/qemu/qemu_driver.c
src/test/test_driver.c

index d376a2cfc39ef88703e1874fcb9ec30bfb03d1cc..9bb12044c068a1419be6f9ec6ce61d670b48bc38 100644 (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
 virDomainDiskDefNew(virDomainXMLOptionPtr xmlopt)
 {
index cb7b0e236601ab12cafeadbc00e08aa7cd336907..146d90663c4600a367ab9902e0c31b2ac7418c41 100644 (file)
@@ -3132,4 +3132,12 @@ int virDomainDiskDefCheckDuplicateInfo(virDomainDiskDefPtr a,
 int virDomainDefCheckDuplicateDiskInfo(virDomainDefPtr def)
     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 */
index c8a2f0a9b15edd65db971dbf02904fe5c4e53c5d..4644203ce93e903bd846df3f34a2f8885634b943 100644 (file)
@@ -221,6 +221,7 @@ virDomainDefGetMemoryInitial;
 virDomainDefGetOnlineVcpumap;
 virDomainDefGetSecurityLabelDef;
 virDomainDefGetVcpu;
+virDomainDefGetVcpuPinInfoHelper;
 virDomainDefGetVcpus;
 virDomainDefGetVcpusMax;
 virDomainDefHasDeviceAddress;
index b9aa32ab302d1278c2cdba2bed51367b563e4d39..87ec5a530c4920adae6cf9b11446a15b95273a70 100644 (file)
@@ -2388,8 +2388,7 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
     libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
     virDomainObjPtr vm = NULL;
     virDomainDefPtr targetDef = NULL;
-    int hostcpus, vcpu, ret = -1;
-    virBitmapPtr allcpumap = NULL;
+    int ret = -1;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -2410,41 +2409,10 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps,
     /* Make sure coverity knows targetDef is valid at this point. */
     sa_assert(targetDef);
 
-    /* Clamp to actual number of vcpus */
-    if (ncpumaps > virDomainDefGetVcpus(targetDef))
-        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;
+    ret = virDomainDefGetVcpuPinInfoHelper(targetDef, maplen, ncpumaps, cpumaps,
+                                           libxl_get_max_cpus(cfg->ctx), NULL);
 
  cleanup:
-    virBitmapFree(allcpumap);
     if (vm)
         virObjectUnlock(vm);
     virObjectUnref(cfg);
index a90c1b8baa4799945a6acd50463aca474df2e727..3b02cdbe17a682c8bb29cb1286310cc0b93f3100 100644 (file)
@@ -5153,9 +5153,6 @@ qemuDomainGetVcpuPinInfo(virDomainPtr dom,
     virDomainObjPtr vm = NULL;
     virDomainDefPtr def;
     int ret = -1;
-    int hostcpus;
-    size_t i;
-    virBitmapPtr allcpumap = NULL;
     qemuDomainObjPrivatePtr priv = NULL;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
@@ -5170,46 +5167,12 @@ qemuDomainGetVcpuPinInfo(virDomainPtr dom,
     if (!(def = virDomainObjGetOneDef(vm, flags)))
         goto cleanup;
 
-    if ((hostcpus = nodeGetCPUCount(NULL)) < 0)
-        goto cleanup;
-
-    if (!(allcpumap = virBitmapNew(hostcpus)))
-        goto cleanup;
-
-    virBitmapSetAll(allcpumap);
     priv = vm->privateData;
 
-    /* Clamp to actual number of vcpus */
-    if (ncpumaps > virDomainDefGetVcpus(def))
-        ncpumaps = virDomainDefGetVcpus(def);
-
-    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;
-
+    ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
+                                           nodeGetCPUCount(NULL),
+                                           priv->autoCpuset);
  cleanup:
-    virBitmapFree(allcpumap);
     virDomainObjEndAPI(&vm);
     return ret;
 }
index 21c66dbb66ab97dbc03caa1f203445cf9ae0dbbf..a51eb0999309fefc01609c128d788a5f5948dc40 100644 (file)
@@ -2534,11 +2534,10 @@ testDomainGetVcpuPinInfo(virDomainPtr dom,
                         int maplen,
                         unsigned int flags)
 {
-    testDriverPtr privconn = dom->conn->privateData;
+    testDriverPtr driver = dom->conn->privateData;
     virDomainObjPtr privdom;
     virDomainDefPtr def;
-    int ret = -1, hostcpus, vcpu;
-    virBitmapPtr allcpumap = NULL;
+    int ret = -1;
 
     if (!(privdom = testDomObjFromDomain(dom)))
         return -1;
@@ -2546,38 +2545,11 @@ testDomainGetVcpuPinInfo(virDomainPtr dom,
     if (!(def = virDomainObjGetOneDef(privdom, flags)))
         goto cleanup;
 
-    hostcpus = VIR_NODEINFO_MAXCPUS(privconn->nodeInfo);
-
-    if (!(allcpumap = virBitmapNew(hostcpus)))
-        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;
+    ret = virDomainDefGetVcpuPinInfoHelper(def, maplen, ncpumaps, cpumaps,
+                                           VIR_NODEINFO_MAXCPUS(driver->nodeInfo),
+                                           NULL);
 
  cleanup:
-    virBitmapFree(allcpumap);
     virDomainObjEndAPI(&privdom);
     return ret;
 }