[PATCH 3/4] util: consolidate QEMU and LXC block io related duplicate code

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

 



Both qemu and lxc drivers have some duplicate code when
dealing with cgroups, such as virLXCCgroupSetupBlkioTune()
and qemuSetupBlkioCgroup(), duplicate code over 80, so
consolidate the same chunk into a separate routine.

Signed-off-by: Mao Zhongyi <maozhongyi@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Zhang Shengju <zhangshengju@xxxxxxxxxxxxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/lxc/lxc_cgroup.c     | 56 +-----------------------------------
 src/qemu/qemu_cgroup.c   | 47 +------------------------------
 src/util/vircgroup.c     | 61 ++++++++++++++++++++++++++++++++++++++++
 src/util/vircgroup.h     |  3 ++
 5 files changed, 67 insertions(+), 101 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 94509d6f43..3df2af55bd 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1685,6 +1685,7 @@ virCgroupSetMemoryHardLimit;
 virCgroupSetMemorySoftLimit;
 virCgroupSetMemSwapHardLimit;
 virCgroupSetOwner;
+virCgroupSetupBlkio;
 virCgroupSupportsCpuBW;
 virCgroupTerminateMachine;
 
diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 0a019dc813..fdf8df79aa 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -102,60 +102,6 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def,
 }
 
 
-static int virLXCCgroupSetupBlkioTune(virDomainDefPtr def,
-                                      virCgroupPtr cgroup)
-{
-    size_t i;
-
-    if (def->blkio.weight &&
-        virCgroupSetBlkioWeight(cgroup, def->blkio.weight) < 0)
-        return -1;
-
-    if (def->blkio.ndevices) {
-        for (i = 0; i < def->blkio.ndevices; i++) {
-            virBlkioDevicePtr dev = &def->blkio.devices[i];
-
-            if (dev->weight &&
-                (virCgroupSetBlkioDeviceWeight(cgroup, dev->path,
-                                               dev->weight) < 0 ||
-                 virCgroupGetBlkioDeviceWeight(cgroup, dev->path,
-                                               &dev->weight) < 0))
-                return -1;
-
-            if (dev->riops &&
-                (virCgroupSetBlkioDeviceReadIops(cgroup, dev->path,
-                                                 dev->riops) < 0 ||
-                 virCgroupGetBlkioDeviceReadIops(cgroup, dev->path,
-                                                 &dev->riops) < 0))
-                return -1;
-
-            if (dev->wiops &&
-                (virCgroupSetBlkioDeviceWriteIops(cgroup, dev->path,
-                                                  dev->wiops) < 0 ||
-                 virCgroupGetBlkioDeviceWriteIops(cgroup, dev->path,
-                                                  &dev->wiops) < 0))
-                return -1;
-
-            if (dev->rbps &&
-                (virCgroupSetBlkioDeviceReadBps(cgroup, dev->path,
-                                                dev->rbps) < 0 ||
-                 virCgroupGetBlkioDeviceReadBps(cgroup, dev->path,
-                                                &dev->rbps) < 0))
-                return -1;
-
-            if (dev->wbps &&
-                (virCgroupSetBlkioDeviceWriteBps(cgroup, dev->path,
-                                                 dev->wbps) < 0 ||
-                 virCgroupGetBlkioDeviceWriteBps(cgroup, dev->path,
-                                                 &dev->wbps) < 0))
-                return -1;
-        }
-    }
-
-    return 0;
-}
-
-
 static int virLXCCgroupSetupMemTune(virDomainDefPtr def,
                                     virCgroupPtr cgroup)
 {
@@ -489,7 +435,7 @@ int virLXCCgroupSetup(virDomainDefPtr def,
     if (virLXCCgroupSetupCpusetTune(def, cgroup, nodemask) < 0)
         goto cleanup;
 
-    if (virLXCCgroupSetupBlkioTune(def, cgroup) < 0)
+    if (virCgroupSetupBlkio(cgroup, def) < 0)
         goto cleanup;
 
     if (virLXCCgroupSetupMemTune(def, cgroup) < 0)
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index 131cdd1134..c9925ee5ca 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -521,7 +521,6 @@ static int
 qemuSetupBlkioCgroup(virDomainObjPtr vm)
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;
-    size_t i;
 
     if (!virCgroupHasController(priv->cgroup,
                                 VIR_CGROUP_CONTROLLER_BLKIO)) {
@@ -534,51 +533,7 @@ qemuSetupBlkioCgroup(virDomainObjPtr vm)
         }
     }
 
-    if (vm->def->blkio.weight != 0 &&
-        virCgroupSetBlkioWeight(priv->cgroup, vm->def->blkio.weight) < 0)
-        return -1;
-
-    if (vm->def->blkio.ndevices) {
-        for (i = 0; i < vm->def->blkio.ndevices; i++) {
-            virBlkioDevicePtr dev = &vm->def->blkio.devices[i];
-            if (dev->weight &&
-                (virCgroupSetBlkioDeviceWeight(priv->cgroup, dev->path,
-                                               dev->weight) < 0 ||
-                 virCgroupGetBlkioDeviceWeight(priv->cgroup, dev->path,
-                                               &dev->weight) < 0))
-                return -1;
-
-            if (dev->riops &&
-                (virCgroupSetBlkioDeviceReadIops(priv->cgroup, dev->path,
-                                                 dev->riops) < 0 ||
-                 virCgroupGetBlkioDeviceReadIops(priv->cgroup, dev->path,
-                                                 &dev->riops) < 0))
-                return -1;
-
-            if (dev->wiops &&
-                (virCgroupSetBlkioDeviceWriteIops(priv->cgroup, dev->path,
-                                                  dev->wiops) < 0 ||
-                 virCgroupGetBlkioDeviceWriteIops(priv->cgroup, dev->path,
-                                                  &dev->wiops) < 0))
-                return -1;
-
-            if (dev->rbps &&
-                (virCgroupSetBlkioDeviceReadBps(priv->cgroup, dev->path,
-                                                dev->rbps) < 0 ||
-                 virCgroupGetBlkioDeviceReadBps(priv->cgroup, dev->path,
-                                                &dev->rbps) < 0))
-                return -1;
-
-            if (dev->wbps &&
-                (virCgroupSetBlkioDeviceWriteBps(priv->cgroup, dev->path,
-                                                 dev->wbps) < 0 ||
-                 virCgroupGetBlkioDeviceWriteBps(priv->cgroup, dev->path,
-                                                 &dev->wbps) < 0))
-                return -1;
-        }
-    }
-
-    return 0;
+    return virCgroupSetupBlkio(priv->cgroup, vm->def);
 }
 
 
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index b46f20abfd..165e5f4854 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -1419,6 +1419,67 @@ virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight)
                             getBlkioWeight, -1, weight);
 }
 
+/**
+ * virCgroupSetupBlkio
+ *
+ * @group: The cgroup to change block io setting for
+ * @def: pointer to domain def
+ *
+ * Returns: 0 on success, -1 on error
+ */
+int
+virCgroupSetupBlkio(virCgroupPtr group, virDomainDefPtr def)
+{
+    size_t i;
+
+    if (def->blkio.weight != 0 &&
+        virCgroupSetBlkioWeight(group, def->blkio.weight) < 0)
+        return -1;
+
+    if (def->blkio.ndevices) {
+        for (i = 0; i < def->blkio.ndevices; i++) {
+            virBlkioDevicePtr dev = &def->blkio.devices[i];
+            if (dev->weight &&
+                (virCgroupSetBlkioDeviceWeight(group, dev->path,
+                                               dev->weight) < 0 ||
+                 virCgroupGetBlkioDeviceWeight(group, dev->path,
+                                               &dev->weight) < 0))
+                return -1;
+
+            if (dev->riops &&
+                (virCgroupSetBlkioDeviceReadIops(group, dev->path,
+                                                 dev->riops) < 0 ||
+                 virCgroupGetBlkioDeviceReadIops(group, dev->path,
+                                                 &dev->riops) < 0))
+                return -1;
+
+            if (dev->wiops &&
+                (virCgroupSetBlkioDeviceWriteIops(group, dev->path,
+                                                  dev->wiops) < 0 ||
+                 virCgroupGetBlkioDeviceWriteIops(group, dev->path,
+                                                  &dev->wiops) < 0))
+                return -1;
+
+            if (dev->rbps &&
+                (virCgroupSetBlkioDeviceReadBps(group, dev->path,
+                                                dev->rbps) < 0 ||
+                 virCgroupGetBlkioDeviceReadBps(group, dev->path,
+                                                &dev->rbps) < 0))
+                return -1;
+
+            if (dev->wbps &&
+                (virCgroupSetBlkioDeviceWriteBps(group, dev->path,
+                                                 dev->wbps) < 0 ||
+                 virCgroupGetBlkioDeviceWriteBps(group, dev->path,
+                                                 &dev->wbps) < 0))
+                return -1;
+        }
+    }
+
+    return 0;
+}
+
+
 /**
  * virCgroupSetBlkioDeviceReadIops:
  * @group: The cgroup to change block io setting for
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index 3eefe78787..9815c51fda 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -24,6 +24,7 @@
 #include "virutil.h"
 #include "virbitmap.h"
 #include "virenum.h"
+#include "conf/domain_conf.h"
 
 struct _virCgroup;
 typedef struct _virCgroup virCgroup;
@@ -123,6 +124,8 @@ int virCgroupAddThread(virCgroupPtr group, pid_t pid);
 int virCgroupSetBlkioWeight(virCgroupPtr group, unsigned int weight);
 int virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight);
 
+int virCgroupSetupBlkio(virCgroupPtr group, virDomainDefPtr def);
+
 int virCgroupGetBlkioIoServiced(virCgroupPtr group,
                                 long long *bytes_read,
                                 long long *bytes_write,
-- 
2.17.1



--
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]

  Powered by Linux