[PATCH v1 2/8] use virBitmap to store cpupin info

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 src/conf/domain_conf.c  |   98 +++++++++++------------------------------------
 src/conf/domain_conf.h  |    3 +-
 src/qemu/qemu_cgroup.c  |    3 +-
 src/qemu/qemu_driver.c  |   14 +++++--
 src/qemu/qemu_process.c |   20 ++++++----
 5 files changed, 49 insertions(+), 89 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 224aec5..2f496fa 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
 
@@ -1505,17 +1506,16 @@ 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 = virBitmapCopy(src[i]->cpumask)) == NULL)
+            goto no_memory;
     }
 
     return ret;
 
 no_memory:
     while (i >= 0) {
-        VIR_FREE(ret[i]->cpumask);
+        virBitmapFree(ret[i]->cpumask);
         VIR_FREE(ret[i]);
     }
     VIR_FREE(ret);
@@ -7947,12 +7947,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 {
@@ -11005,34 +11001,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,
@@ -11040,20 +11008,19 @@ 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 = virBitmapAllocFromData(cpumap, maplen);
+        if (!vcpupin->cpumask)
+            return -1;
 
         return 0;
     }
@@ -11062,16 +11029,15 @@ int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr *vcpupin_list,
 
     if (VIR_ALLOC(vcpupin) < 0) {
         virReportOOMError();
-        VIR_FREE(cpumask);
         return -1;
     }
     vcpupin->vcpuid = vcpu;
-    vcpupin->cpumask = cpumask;
-
+    vcpupin->cpumask = virBitmapAllocFromData(cpumap, maplen);
+    if (!vcpupin->cpumask)
+        return -1;
 
     if (VIR_REALLOC_N(vcpupin_list, *nvcpupin + 1) < 0) {
         virReportOOMError();
-        VIR_FREE(cpumask);
         VIR_FREE(vcpupin);
         return -1;
     }
@@ -11126,47 +11092,31 @@ 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 = virBitmapAllocFromData(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;
+        def->cputune.emulatorpin->cpumask = virBitmapAllocFromData(cpumap, maplen);
+        if (!def->cputune.emulatorpin->cpumask)
+            return -1;
     }
 
     return 0;
-
-cleanup:
-    VIR_FREE(cpumask);
-    return -1;
 }
 
 int
@@ -13133,8 +13083,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,
@@ -13151,8 +13100,7 @@ virDomainDefFormatInternal(virDomainDefPtr def,
         virBufferAsprintf(buf, "    <emulatorpin ");
 
         char *cpumask = NULL;
-        cpumask = virDomainCpuSetFormat(def->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 9ee57e1..ba05fde 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
@@ -1524,7 +1525,7 @@ typedef struct _virDomainVcpuPinDef virDomainVcpuPinDef;
 typedef virDomainVcpuPinDef *virDomainVcpuPinDefPtr;
 struct _virDomainVcpuPinDef {
     int vcpuid;
-    char *cpumask;
+    virBitmapPtr cpumask;
 };
 
 void virDomainVcpuPinDefFree(virDomainVcpuPinDefPtr *def, int nvcpupin);
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index df67ff3..f093b75 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 9a25253..ae7cc02 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3953,8 +3953,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);
@@ -4013,7 +4014,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);
         }
     }
@@ -4203,7 +4206,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);
@@ -4252,7 +4256,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 7f85aea..e153d98 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -1945,10 +1945,11 @@ 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;
+    bool result;
 
     if (virNodeGetInfo(conn, &nodeinfo) != 0) {
         return  -1;
@@ -1980,11 +1981,13 @@ 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])
+        for (i = 0 ; i < virBitmapSize(cpumask); i++) {
+            if (virBitmapGetBit(cpumask, i, &result) < 0)
+                goto cleanup;
+            if (result)
                 VIR_USE_CPU(cpumap, i);
         }
 
@@ -2009,11 +2012,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;
@@ -2033,9 +2037,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);
     }
 
-- 
1.7.10.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]