[PATCH v2 02/14] lxc,qemu: use virCgroupSetupBlkioDevice* helpers

Daniel Henrique Barboza danielhb413 at gmail.com
Mon Feb 17 21:29:09 UTC 2020


There are code repetition of set() and get() blkio device
parameters across lxc and qemu files. Use the new vircgroup
helpers to trim the repetition a bit.

Signed-off-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 src/lxc/lxc_cgroup.c   | 30 ++++++++++--------------------
 src/lxc/lxc_driver.c   | 41 +++++++++++------------------------------
 src/qemu/qemu_cgroup.c | 32 ++++++++++++--------------------
 src/qemu/qemu_driver.c | 41 +++++++++++------------------------------
 4 files changed, 44 insertions(+), 100 deletions(-)

diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 7f3701593a..3c7e31c36b 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -112,38 +112,28 @@ static int virLXCCgroupSetupBlkioTune(virDomainDefPtr def,
             virBlkioDevicePtr dev = &def->blkio.devices[i];
 
             if (dev->weight &&
-                (virCgroupSetBlkioDeviceWeight(cgroup, dev->path,
-                                               dev->weight) < 0 ||
-                 virCgroupGetBlkioDeviceWeight(cgroup, dev->path,
-                                               &dev->weight) < 0))
+                virCgroupSetupBlkioDeviceWeight(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))
+                virCgroupSetupBlkioDeviceReadIops(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))
+                virCgroupSetupBlkioDeviceWriteIops(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))
+                virCgroupSetupBlkioDeviceReadBps(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))
+                virCgroupSetupBlkioDeviceWriteBps(cgroup, dev->path,
+                                                  &dev->wbps) < 0)
                 return -1;
         }
     }
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index f7376188f0..439cc317c6 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -2581,6 +2581,7 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
                        STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS) ||
                        STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS) ||
                        STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) {
+                virCgroupPtr cgroup = priv->cgroup;
                 size_t ndevices;
                 virBlkioDevicePtr devices = NULL;
                 size_t j;
@@ -2595,60 +2596,40 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
 
                 if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceWeight(priv->cgroup,
-                                                          devices[j].path,
-                                                          devices[j].weight) < 0 ||
-                            virCgroupGetBlkioDeviceWeight(priv->cgroup,
-                                                          devices[j].path,
-                                                          &devices[j].weight) < 0) {
+                        if (virCgroupSetupBlkioDeviceWeight(cgroup, devices[j].path,
+                                                            &devices[j].weight) < 0) {
                             ret = -1;
                             break;
                         }
                     }
                 } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceReadIops(priv->cgroup,
-                                                            devices[j].path,
-                                                            devices[j].riops) < 0 ||
-                            virCgroupGetBlkioDeviceReadIops(priv->cgroup,
-                                                            devices[j].path,
-                                                            &devices[j].riops) < 0) {
+                        if (virCgroupSetupBlkioDeviceReadIops(cgroup, devices[j].path,
+                                                              &devices[j].riops) < 0) {
                             ret = -1;
                             break;
                         }
                     }
                 } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceWriteIops(priv->cgroup,
-                                                             devices[j].path,
-                                                             devices[j].wiops) < 0 ||
-                            virCgroupGetBlkioDeviceWriteIops(priv->cgroup,
-                                                             devices[j].path,
-                                                             &devices[j].wiops) < 0) {
+                        if (virCgroupSetupBlkioDeviceWriteIops(cgroup, devices[j].path,
+                                                               &devices[j].wiops) < 0) {
                             ret = -1;
                             break;
                         }
                     }
                 } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceReadBps(priv->cgroup,
-                                                           devices[j].path,
-                                                           devices[j].rbps) < 0 ||
-                            virCgroupGetBlkioDeviceReadBps(priv->cgroup,
-                                                           devices[j].path,
-                                                           &devices[j].rbps) < 0) {
+                        if (virCgroupSetupBlkioDeviceReadBps(cgroup, devices[j].path,
+                                                             &devices[j].rbps) < 0) {
                             ret = -1;
                             break;
                         }
                     }
                 } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceWriteBps(priv->cgroup,
-                                                            devices[j].path,
-                                                            devices[j].wbps) < 0 ||
-                            virCgroupGetBlkioDeviceWriteBps(priv->cgroup,
-                                                            devices[j].path,
-                                                            &devices[j].wbps) < 0) {
+                        if (virCgroupSetupBlkioDeviceWriteBps(cgroup, devices[j].path,
+                                                              &devices[j].wbps) < 0) {
                             ret = -1;
                             break;
                         }
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index 45701b4c6e..da96a60a08 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -611,39 +611,31 @@ qemuSetupBlkioCgroup(virDomainObjPtr vm)
     if (vm->def->blkio.ndevices) {
         for (i = 0; i < vm->def->blkio.ndevices; i++) {
             virBlkioDevicePtr dev = &vm->def->blkio.devices[i];
+            virCgroupPtr cgroup = priv->cgroup;
+
             if (dev->weight &&
-                (virCgroupSetBlkioDeviceWeight(priv->cgroup, dev->path,
-                                               dev->weight) < 0 ||
-                 virCgroupGetBlkioDeviceWeight(priv->cgroup, dev->path,
-                                               &dev->weight) < 0))
+                virCgroupSetupBlkioDeviceWeight(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))
+                virCgroupSetupBlkioDeviceReadIops(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))
+                virCgroupSetupBlkioDeviceWriteIops(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))
+                virCgroupSetupBlkioDeviceReadBps(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))
+                virCgroupSetupBlkioDeviceWriteBps(cgroup, dev->path,
+                                                  &dev->wbps) < 0)
                 return -1;
         }
     }
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index f686b858cf..c1a79dcda6 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9564,6 +9564,7 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
                        STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS) ||
                        STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS) ||
                        STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) {
+                virCgroupPtr cgroup = priv->cgroup;
                 size_t ndevices;
                 virBlkioDevicePtr devices = NULL;
                 size_t j;
@@ -9578,60 +9579,40 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
 
                 if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceWeight(priv->cgroup,
-                                                          devices[j].path,
-                                                          devices[j].weight) < 0 ||
-                            virCgroupGetBlkioDeviceWeight(priv->cgroup,
-                                                          devices[j].path,
-                                                          &devices[j].weight) < 0) {
+                        if (virCgroupSetupBlkioDeviceWeight(cgroup, devices[j].path,
+                                                            &devices[j].weight) < 0) {
                             ret = -1;
                             break;
                         }
                     }
                 } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceReadIops(priv->cgroup,
-                                                            devices[j].path,
-                                                            devices[j].riops) < 0 ||
-                            virCgroupGetBlkioDeviceReadIops(priv->cgroup,
-                                                            devices[j].path,
-                                                            &devices[j].riops) < 0) {
+                        if (virCgroupSetupBlkioDeviceReadIops(cgroup, devices[j].path,
+                                                              &devices[j].riops) < 0) {
                             ret = -1;
                             break;
                         }
                     }
                 } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceWriteIops(priv->cgroup,
-                                                             devices[j].path,
-                                                             devices[j].wiops) < 0 ||
-                            virCgroupGetBlkioDeviceWriteIops(priv->cgroup,
-                                                             devices[j].path,
-                                                             &devices[j].wiops) < 0) {
+                        if (virCgroupSetupBlkioDeviceWriteIops(cgroup, devices[j].path,
+                                                               &devices[j].wiops) < 0) {
                             ret = -1;
                             break;
                         }
                     }
                 } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_BPS)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceReadBps(priv->cgroup,
-                                                           devices[j].path,
-                                                           devices[j].rbps) < 0 ||
-                            virCgroupGetBlkioDeviceReadBps(priv->cgroup,
-                                                           devices[j].path,
-                                                           &devices[j].rbps) < 0) {
+                        if (virCgroupSetupBlkioDeviceReadBps(cgroup, devices[j].path,
+                                                             &devices[j].rbps) < 0) {
                             ret = -1;
                             break;
                         }
                     }
                 } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS)) {
                     for (j = 0; j < ndevices; j++) {
-                        if (virCgroupSetBlkioDeviceWriteBps(priv->cgroup,
-                                                            devices[j].path,
-                                                            devices[j].wbps) < 0 ||
-                            virCgroupGetBlkioDeviceWriteBps(priv->cgroup,
-                                                            devices[j].path,
-                                                            &devices[j].wbps) < 0) {
+                        if (virCgroupSetupBlkioDeviceWriteBps(cgroup, devices[j].path,
+                                                              &devices[j].wbps) < 0) {
                             ret = -1;
                             break;
                         }
-- 
2.24.1





More information about the libvir-list mailing list