[PATCH v1 08/14] vircgroup: add virCgroupSetupDomainBlkioParameters()

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

 



After the introduction of virDomainMergeBlkioDevice() in a
previous patch, it is now clear that lxcDomainSetBlkioParameters() and
qemuDomainSetBlkioParameters() uses the same loop to set cgroup
blkio parameter of a domain.

Avoid the repetition by adding a new helper called
virCgroupSetupDomainBlkioParameters().

Signed-off-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx>
---
 src/libvirt_private.syms |   1 +
 src/lxc/lxc_driver.c     | 104 +----------------------------------
 src/qemu/qemu_driver.c   | 105 +----------------------------------
 src/util/vircgroup.c     | 115 +++++++++++++++++++++++++++++++++++++++
 src/util/vircgroup.h     |   4 ++
 5 files changed, 124 insertions(+), 205 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index d483b75aeb..196c7bdb23 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1734,6 +1734,7 @@ virCgroupSetOwner;
 virCgroupSetupBlkioTune;
 virCgroupSetupCpuPeriodQuota;
 virCgroupSetupCpusetCpus;
+virCgroupSetupDomainBlkioParameters;
 virCgroupSetupMemtune;
 virCgroupSupportsCpuBW;
 virCgroupTerminateMachine;
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 96e61ff4ca..9a585d0d07 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -2364,108 +2364,8 @@ lxcDomainSetBlkioParameters(virDomainPtr dom,
 
     ret = 0;
     if (def) {
-        for (i = 0; i < nparams; i++) {
-            virTypedParameterPtr param = &params[i];
-
-            if (STREQ(param->field, VIR_DOMAIN_BLKIO_WEIGHT)) {
-                if (virCgroupSetBlkioWeight(priv->cgroup, params[i].value.ui) < 0)
-                    ret = -1;
-            } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT) ||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS) ||
-                       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)) {
-                size_t ndevices;
-                virBlkioDevicePtr devices = NULL;
-                size_t j;
-
-                if (virDomainParseBlkioDeviceStr(params[i].value.s,
-                                                 param->field,
-                                                 &devices,
-                                                 &ndevices) < 0) {
-                    ret = -1;
-                    continue;
-                }
-
-                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) {
-                            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) {
-                            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) {
-                            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) {
-                            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) {
-                            ret = -1;
-                            break;
-                        }
-                    }
-                } else {
-                    virReportError(VIR_ERR_INVALID_ARG, _("Unknown blkio parameter %s"),
-                                   param->field);
-                    ret = -1;
-                    virBlkioDeviceArrayClear(devices, ndevices);
-                    VIR_FREE(devices);
-
-                    continue;
-                }
-
-                if (j != ndevices ||
-                    virDomainMergeBlkioDevice(&def->blkio.devices,
-                                              &def->blkio.ndevices,
-                                              devices, ndevices, param->field) < 0)
-                    ret = -1;
-                virBlkioDeviceArrayClear(devices, ndevices);
-                VIR_FREE(devices);
-            }
-        }
+        ret = virCgroupSetupDomainBlkioParameters(priv->cgroup, def,
+                                                  params, nparams);
     }
     if (ret < 0)
         goto endjob;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index d458b59b99..da4e6d7a93 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9374,109 +9374,8 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
 
     ret = 0;
     if (def) {
-        for (i = 0; i < nparams; i++) {
-            virTypedParameterPtr param = &params[i];
-
-            if (STREQ(param->field, VIR_DOMAIN_BLKIO_WEIGHT)) {
-                if (virCgroupSetBlkioWeight(priv->cgroup, param->value.ui) < 0 ||
-                    virCgroupGetBlkioWeight(priv->cgroup, &def->blkio.weight) < 0)
-                    ret = -1;
-            } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT) ||
-                       STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS) ||
-                       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)) {
-                size_t ndevices;
-                virBlkioDevicePtr devices = NULL;
-                size_t j;
-
-                if (virDomainParseBlkioDeviceStr(param->value.s,
-                                                 param->field,
-                                                 &devices,
-                                                 &ndevices) < 0) {
-                    ret = -1;
-                    continue;
-                }
-
-                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) {
-                            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) {
-                            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) {
-                            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) {
-                            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) {
-                            ret = -1;
-                            break;
-                        }
-                    }
-                } else {
-                    virReportError(VIR_ERR_INVALID_ARG, _("Unknown blkio parameter %s"),
-                                   param->field);
-                    ret = -1;
-                    virBlkioDeviceArrayClear(devices, ndevices);
-                    VIR_FREE(devices);
-
-                    continue;
-                }
-
-                if (j != ndevices ||
-                    virDomainMergeBlkioDevice(&def->blkio.devices,
-                                              &def->blkio.ndevices,
-                                              devices, ndevices, param->field) < 0)
-                    ret = -1;
-                virBlkioDeviceArrayClear(devices, ndevices);
-                VIR_FREE(devices);
-            }
-        }
+        ret = virCgroupSetupDomainBlkioParameters(priv->cgroup, def,
+                                                  params, nparams);
 
         if (virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
             goto endjob;
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 9dc60f66c9..67855b685f 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -3729,3 +3729,118 @@ virCgroupSetupCpuPeriodQuota(virCgroupPtr cgroup,
 
     return -1;
 }
+
+
+int
+virCgroupSetupDomainBlkioParameters(virCgroupPtr cgroup, virDomainDefPtr def,
+                                    virTypedParameterPtr params, int nparams)
+{
+    size_t i;
+    int ret = 0;
+
+    for (i = 0; i < nparams; i++) {
+        virTypedParameterPtr param = &params[i];
+
+        if (STREQ(param->field, VIR_DOMAIN_BLKIO_WEIGHT)) {
+            if (virCgroupSetBlkioWeight(cgroup, param->value.ui) < 0)
+                ret = -1;
+        } else if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT) ||
+                   STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS) ||
+                   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)) {
+            size_t ndevices;
+            virBlkioDevicePtr devices = NULL;
+            size_t j;
+
+            if (virDomainParseBlkioDeviceStr(param->value.s,
+                                             param->field,
+                                             &devices,
+                                             &ndevices) < 0) {
+                ret = -1;
+                continue;
+            }
+
+            if (STREQ(param->field, VIR_DOMAIN_BLKIO_DEVICE_WEIGHT)) {
+                for (j = 0; j < ndevices; j++) {
+                    if (virCgroupSetBlkioDeviceWeight(cgroup,
+                                                      devices[j].path,
+                                                      devices[j].weight) < 0 ||
+                        virCgroupGetBlkioDeviceWeight(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(cgroup,
+                                                        devices[j].path,
+                                                        devices[j].riops) < 0 ||
+                        virCgroupGetBlkioDeviceReadIops(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(cgroup,
+                                                         devices[j].path,
+                                                         devices[j].wiops) < 0 ||
+                        virCgroupGetBlkioDeviceWriteIops(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(cgroup,
+                                                       devices[j].path,
+                                                       devices[j].rbps) < 0 ||
+                        virCgroupGetBlkioDeviceReadBps(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(cgroup,
+                                                        devices[j].path,
+                                                        devices[j].wbps) < 0 ||
+                        virCgroupGetBlkioDeviceWriteBps(cgroup,
+                                                        devices[j].path,
+                                                        &devices[j].wbps) < 0) {
+                        ret = -1;
+                        break;
+                    }
+                }
+            } else {
+                virReportError(VIR_ERR_INVALID_ARG, _("Unknown blkio parameter %s"),
+                               param->field);
+                ret = -1;
+                virBlkioDeviceArrayClear(devices, ndevices);
+                VIR_FREE(devices);
+
+                continue;
+            }
+
+            if (j != ndevices ||
+                virDomainMergeBlkioDevice(&def->blkio.devices,
+                                          &def->blkio.ndevices,
+                                          devices, ndevices, param->field) < 0)
+                ret = -1;
+
+            virBlkioDeviceArrayClear(devices, ndevices);
+            VIR_FREE(devices);
+        }
+    }
+
+    return ret;
+}
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index 97ffa3f7df..85309e800d 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -296,3 +296,7 @@ int virCgroupSetAndRetrieveCpuShares(virCgroupPtr cgroup,
 int virCgroupSetupCpuPeriodQuota(virCgroupPtr cgroup,
                                  unsigned long long period,
                                  long long quota);
+int virCgroupSetupDomainBlkioParameters(virCgroupPtr cgroup,
+                                        virDomainDefPtr def,
+                                        virTypedParameterPtr params,
+                                        int nparams);
-- 
2.24.1






[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