[libvirt] [PATCH 5/9] blkio: Setting throttle blkio cgroup for domain

Gao feng gaofeng at cn.fujitsu.com
Mon Dec 2 06:48:00 UTC 2013


This patch extends virCgroupSetBlkioWeightDevice and
rename it the virCgroupSetBlkioDevice, now we can use
this interface to set up throttle blkio cgroup too.

Signed-off-by: Guan Qiang <hzguanqiang at corp.netease.com>
Signed-off-by: Gao feng <gaofeng at cn.fujitsu.com>
---
 src/libvirt_private.syms |   3 +
 src/lxc/lxc_cgroup.c     |  10 ++-
 src/qemu/qemu_cgroup.c   |  11 +--
 src/qemu/qemu_driver.c   |  10 ++-
 src/util/vircgroup.c     | 170 ++++++++++++++++++++++++++++++++++++++++++++---
 src/util/vircgroup.h     |  18 +++++
 6 files changed, 203 insertions(+), 19 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e5f7cbd..c008e2b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1024,6 +1024,9 @@ virCgroupNewVcpu;
 virCgroupPathOfController;
 virCgroupRemove;
 virCgroupRemoveRecursively;
+virCgroupSetBlkioDevice;
+virCgroupSetBlkioDeviceBps;
+virCgroupSetBlkioDeviceIops;
 virCgroupSetBlkioDeviceWeight;
 virCgroupSetBlkioWeight;
 virCgroupSetCpuCfsPeriod;
diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 310a476..1c4bee5 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -113,9 +113,13 @@ static int virLXCCgroupSetupBlkioTune(virDomainDefPtr def,
     if (def->blkio.ndevices) {
         for (i = 0; i < def->blkio.ndevices; i++) {
             virBlkioDevicePtr dev = &def->blkio.devices[i];
-            if (!dev->weight)
-                continue;
-            if (virCgroupSetBlkioDeviceWeight(cgroup, dev->path, dev->weight) < 0)
+            if (virCgroupSetBlkioDevice(cgroup,
+                                        dev->path,
+                                        dev->weight,
+                                        dev->riops,
+                                        dev->wiops,
+                                        dev->rbps,
+                                        dev->wbps) < 0)
                 return -1;
         }
     }
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index a18955e..3b579ca 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -400,10 +400,13 @@ qemuSetupBlkioCgroup(virDomainObjPtr vm)
     if (vm->def->blkio.ndevices) {
         for (i = 0; i < vm->def->blkio.ndevices; i++) {
             virBlkioDevicePtr dev = &vm->def->blkio.devices[i];
-            if (!dev->weight)
-                continue;
-            if (virCgroupSetBlkioDeviceWeight(priv->cgroup, dev->path,
-                                              dev->weight) < 0)
+            if (virCgroupSetBlkioDevice(priv->cgroup,
+                                        dev->path,
+                                        dev->weight,
+                                        dev->riops,
+                                        dev->wiops,
+                                        dev->rbps,
+                                        dev->wbps) < 0)
                 return -1;
         }
     }
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 5c0c5e5..61dbe7f 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -7616,9 +7616,13 @@ qemuDomainSetBlkioParameters(virDomainPtr dom,
                     continue;
                 }
                 for (j = 0; j < ndevices; j++) {
-                    if (virCgroupSetBlkioDeviceWeight(priv->cgroup,
-                                                      devices[j].path,
-                                                      devices[j].weight) < 0) {
+                    if (virCgroupSetBlkioDevice(priv->cgroup,
+                                                devices[j].path,
+                                                devices[j].weight,
+                                                devices[j].riops,
+                                                devices[j].wiops,
+                                                devices[j].rbps,
+                                                devices[j].wbps) < 0) {
                         ret = -1;
                         break;
                     }
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 5c43e10..7f9ba49 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -1824,18 +1824,110 @@ virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight)
     return ret;
 }
 
+/**
+ * virCgroupSetBlkioDeviceIops:
+ * @group: The cgroup to change block io setting for
+ * @path: The path of device
+ * @read: setup the read iops or write iops
+ * @iops: The new device iops throttle, or 0 to clear
+ *
+ * Returns: 0 on success, -1 on error
+ */
+int
+virCgroupSetBlkioDeviceIops(virCgroupPtr group,
+                            const char *path,
+                            bool read,
+                            unsigned int iops)
+{
+    char *str;
+    struct stat sb;
+    int ret;
+
+    if (stat(path, &sb) < 0) {
+        virReportSystemError(errno,
+                             _("Path '%s' is not accessible"),
+                             path);
+        return -1;
+    }
+
+    if (!S_ISBLK(sb.st_mode)) {
+        virReportSystemError(EINVAL,
+                             _("Path '%s' must be a block device"),
+                             path);
+        return -1;
+    }
+
+    if (virAsprintf(&str, "%d:%d %u", major(sb.st_rdev),
+                    minor(sb.st_rdev), iops) < 0)
+        return -1;
+
+    ret = virCgroupSetValueStr(group,
+                               VIR_CGROUP_CONTROLLER_BLKIO,
+                               read ?
+                               "blkio.throttle.read_iops_device" :
+                               "blkio.throttle.write_iops_device",
+                               str);
+
+    VIR_FREE(str);
+    return ret;
+}
+
 
 /**
- * virCgroupSetBlkioDeviceWeight:
+ * virCgroupSetBlkioDeviceBps:
+ * @group: The cgroup to change block io setting for
+ * @path: The path of device
+ * @read: setup the read bps or write bps
+ * @bps: The new device bps throttle, or 0 to clear
  *
- * @group: The cgroup to change io device weight device for
- * @path: The device with a weight to alter
+ * Returns: 0 on success, -1 on error
+ */
+int
+virCgroupSetBlkioDeviceBps(virCgroupPtr group,
+                           const char *path,
+                           bool read,
+                           unsigned long long bps)
+{
+    char *str;
+    struct stat sb;
+    int ret;
+
+    if (stat(path, &sb) < 0) {
+        virReportSystemError(errno,
+                             _("Path '%s' is not accessible"),
+                             path);
+        return -1;
+    }
+
+    if (!S_ISBLK(sb.st_mode)) {
+        virReportSystemError(EINVAL,
+                             _("Path '%s' must be a block device"),
+                             path);
+        return -1;
+    }
+
+    if (virAsprintf(&str, "%d:%d %llu", major(sb.st_rdev),
+                    minor(sb.st_rdev), bps) < 0)
+        return -1;
+
+    ret = virCgroupSetValueStr(group,
+                               VIR_CGROUP_CONTROLLER_BLKIO,
+                               read ?
+                               "blkio.throttle.read_bps_device" :
+                               "blkio.throttle.write_bps_device",
+                               str);
+
+    VIR_FREE(str);
+    return ret;
+}
+
+/**
+ * virCgroupSetBlkioDeviceWeight:
+ * @group: The cgroup to change block io setting for
+ * @path: The path of device
  * @weight: The new device weight (100-1000),
  * (10-1000) after kernel 2.6.39, or 0 to clear
  *
- * device_weight is treated as a write-only parameter, so
- * there isn't a getter counterpart.
- *
  * Returns: 0 on success, -1 on error
  */
 int
@@ -1861,8 +1953,8 @@ virCgroupSetBlkioDeviceWeight(virCgroupPtr group,
         return -1;
     }
 
-    if (virAsprintf(&str, "%d:%d %d", major(sb.st_rdev), minor(sb.st_rdev),
-                    weight) < 0)
+    if (virAsprintf(&str, "%d:%d %u", major(sb.st_rdev),
+                    minor(sb.st_rdev), weight) < 0)
         return -1;
 
     ret = virCgroupSetValueStr(group,
@@ -1874,6 +1966,47 @@ virCgroupSetBlkioDeviceWeight(virCgroupPtr group,
 }
 
 
+/**
+ * virCgroupSetBlkioDevice:
+ *
+ * @group: The cgroup to change block io setting for
+ * @path: The path of device
+ * @weight: The new device weight (100-1000),
+ * (10-1000) after kernel 2.6.39
+ * @riops: The new device read iops throttle
+ * @wiops: The new device write iops throttle
+ * @rbps: The new device read bps throttle
+ * @wbps: The new device write bps throttle
+ *
+ * Returns: 0 on success, -1 on error
+ */
+int
+virCgroupSetBlkioDevice(virCgroupPtr group,
+                        const char *path,
+                        unsigned int weight,
+                        unsigned int riops,
+                        unsigned int wiops,
+                        unsigned long long rbps,
+                        unsigned long long wbps)
+{
+    if (weight && (virCgroupSetBlkioDeviceWeight(group, path, weight) < 0))
+        return -1;
+
+    if (riops && (virCgroupSetBlkioDeviceIops(group, path, 1, riops) < 0))
+        return -1;
+
+    if (wiops && (virCgroupSetBlkioDeviceIops(group, path, 0, wiops) < 0))
+        return -1;
+
+    if (rbps && (virCgroupSetBlkioDeviceBps(group, path, 1, rbps) < 0))
+        return -1;
+
+    if (wbps && (virCgroupSetBlkioDeviceBps(group, path, 0, wbps) < 0))
+        return -1;
+
+    return 0;
+}
+
 
 /**
  * virCgroupSetMemory:
@@ -3280,11 +3413,30 @@ virCgroupGetBlkioWeight(virCgroupPtr group ATTRIBUTE_UNUSED,
     return -1;
 }
 
-
 int
 virCgroupSetBlkioDeviceWeight(virCgroupPtr group ATTRIBUTE_UNUSED,
                               const char *path ATTRIBUTE_UNUSED,
                               unsigned int weight ATTRIBUTE_UNUSED)
+
+int
+virCgroupSetBlkioDeviceIops(virCgroupPtr group ATTRIBUTE_UNUSED,
+                            const char *path ATTRIBUTE_UNUSED,
+                            bool read ATTRIBUTE_UNUSED,
+                            unsigned long long iops ATTRIBUTE_UNUSED)
+
+int
+virCgroupSetBlkioDeviceBps(virCgroupPtr group ATTRIBUTE_UNUSED,
+                           const char *path ATTRIBUTE_UNUSED,
+                           bool read ATTRIBUTE_UNUSED,
+                           unsigned long long bps ATTRIBUTE_UNUSED)
+int
+virCgroupSetBlkioDevice(virCgroupPtr group ATTRIBUTE_UNUSED,
+                        const char *path ATTRIBUTE_UNUSED,
+                        unsigned int weight ATTRIBUTE_UNUSED,
+                        unsigned int riops ATTRIBUTE_UNUSED,
+                        unsigned int wiops ATTRIBUTE_UNUSED,
+                        unsigned long long rbps ATTRIBUTE_UNUSED,
+                        unsigned long long wbps ATTRIBUTE_UNUSED)
 {
     virReportSystemError(ENOSYS, "%s",
                          _("Control groups not supported on this platform"));
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index 835eb30..99c3a35 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -126,6 +126,24 @@ int virCgroupSetBlkioDeviceWeight(virCgroupPtr group,
                                   const char *path,
                                   unsigned int weight);
 
+int virCgroupSetBlkioDeviceIops(virCgroupPtr group,
+                                const char *path,
+                                bool read,
+                                unsigned int iops);
+
+int virCgroupSetBlkioDeviceBps(virCgroupPtr group,
+                                const char *path,
+                                bool read,
+                                unsigned long long bps);
+
+int virCgroupSetBlkioDevice(virCgroupPtr group,
+                            const char *path,
+                            unsigned int weight,
+                            unsigned int riops,
+                            unsigned int wiops,
+                            unsigned long long rbps,
+                            unsigned long long wbps);
+
 int virCgroupSetMemory(virCgroupPtr group, unsigned long long kb);
 int virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb);
 
-- 
1.8.3.1




More information about the libvir-list mailing list