[libvirt] [PATCH 3/8] Implement Block Io Throttle setting for the qemu driver

Lei Li lilei at linux.vnet.ibm.com
Thu Oct 27 09:20:05 UTC 2011


This patch implement the block io throtte setting for the qemu driver through 
  - qmp/hmp command 'block_set_io_throttle'.

Signed-off-by: Zhi Yong Wu <wuzhy at linux.vnet.ibm.com>
Signed-off-by: Lei Li <lilei at linux.vnet.ibm.com>
---
 src/qemu/qemu_command.c      |   35 +++++++++++++++++++++++++++
 src/qemu/qemu_driver.c       |   53 ++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor.c      |   18 ++++++++++++++
 src/qemu/qemu_monitor.h      |    5 ++++
 src/qemu/qemu_monitor_json.c |   45 +++++++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor_json.h |    5 ++++
 src/qemu/qemu_monitor_text.c |   39 ++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor_text.h |    5 ++++
 8 files changed, 205 insertions(+), 0 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 0c5bfab..9422e17 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1734,6 +1734,41 @@ qemuBuildDriveStr(virDomainDiskDefPtr disk,
         }
     }
 
+    /*block I/O throttling*/
+    if (disk->blkiothrottle.bps || disk->blkiothrottle.bps_rd
+        || disk->blkiothrottle.bps_wr || disk->blkiothrottle.iops
+        || disk->blkiothrottle.iops_rd || disk->blkiothrottle.iops_wr) {
+        if (disk->blkiothrottle.bps) {
+            virBufferAsprintf(&opt, ",bps=%llu",
+                              disk->blkiothrottle.bps);
+        }
+
+        if (disk->blkiothrottle.bps_rd) {
+            virBufferAsprintf(&opt, ",bps_rd=%llu",
+                              disk->blkiothrottle.bps_rd);
+        }
+
+        if (disk->blkiothrottle.bps_wr) {
+            virBufferAsprintf(&opt, ",bps_wr=%llu",
+                              disk->blkiothrottle.bps_wr);
+        }
+
+        if (disk->blkiothrottle.iops) {
+            virBufferAsprintf(&opt, ",iops=%llu",
+                              disk->blkiothrottle.iops);
+        }
+
+        if (disk->blkiothrottle.iops_rd) {
+            virBufferAsprintf(&opt, ",iops_rd=%llu",
+                              disk->blkiothrottle.iops_rd);
+        }
+
+        if (disk->blkiothrottle.iops_wr) {
+            virBufferAsprintf(&opt, ",iops_wr=%llu",
+                              disk->blkiothrottle.iops_wr);
+        }
+    }
+
     if (virBufferError(&opt)) {
         virReportOOMError();
         goto error;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index e053a97..b41a7d3 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -10660,6 +10660,58 @@ qemuDomainBlockPull(virDomainPtr dom, const char *path, unsigned long bandwidth,
     return ret;
 }
 
+static int
+qemuDomainSetBlockIoThrottle(virDomainPtr dom,
+                             const char *disk,
+                             virDomainBlockIoThrottleInfoPtr info,
+                             unsigned int flags)
+{
+    struct qemud_driver *driver = dom->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    qemuDomainObjPrivatePtr priv;
+    char uuidstr[VIR_UUID_STRING_BUFLEN];
+    const char *device = NULL;
+    int ret = -1;
+
+    qemuDriverLock(driver);
+    virUUIDFormat(dom->uuid, uuidstr);
+    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
+    if (!vm) {
+        qemuReportError(VIR_ERR_NO_DOMAIN,
+                        _("no domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if (!virDomainObjIsActive(vm)) {
+        qemuReportError(VIR_ERR_OPERATION_INVALID,
+                        "%s", _("domain is not running"));
+        goto cleanup;
+    }
+
+    device = qemuDiskPathToAlias(vm, disk);
+    if (!device) {
+        goto cleanup;
+    }
+
+    if (qemuDomainObjBeginJobWithDriver(driver, vm, QEMU_JOB_MODIFY) < 0)
+        goto cleanup;
+    qemuDomainObjEnterMonitorWithDriver(driver, vm);
+    priv = vm->privateData;
+    ret = qemuMonitorSetBlockIoThrottle(priv->mon, device, info, flags);
+    qemuDomainObjExitMonitorWithDriver(driver, vm);
+    if (qemuDomainObjEndJob(driver, vm) == 0) {
+        vm = NULL;
+        goto cleanup;
+    }
+
+cleanup:
+    VIR_FREE(device);
+    if (vm)
+        virDomainObjUnlock(vm);
+    qemuDriverUnlock(driver);
+    return ret;
+}
+
 static virDriver qemuDriver = {
     .no = VIR_DRV_QEMU,
     .name = "QEMU",
@@ -10802,6 +10854,7 @@ static virDriver qemuDriver = {
     .domainGetBlockJobInfo = qemuDomainGetBlockJobInfo, /* 0.9.4 */
     .domainBlockJobSetSpeed = qemuDomainBlockJobSetSpeed, /* 0.9.4 */
     .domainBlockPull = qemuDomainBlockPull, /* 0.9.4 */
+    .domainSetBlockIoThrottle = qemuDomainSetBlockIoThrottle, /* 0.9.8 */
 };
 
 
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 182e63d..b061631 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -2542,6 +2542,24 @@ int qemuMonitorBlockJob(qemuMonitorPtr mon,
     return ret;
 }
 
+int qemuMonitorSetBlockIoThrottle(qemuMonitorPtr mon,
+                                  const char *device,
+                                  virDomainBlockIoThrottleInfoPtr info,
+                                  unsigned int flags)
+{
+    int ret;
+
+    VIR_DEBUG("mon=%p, device=%p, info=%p, flags=%x",
+              mon, device, info, flags);
+
+    if (mon->json) {
+        ret = qemuMonitorJSONSetBlockIoThrottle(mon, device, info, flags);
+    } else {
+        ret = qemuMonitorTextSetBlockIoThrottle(mon, device, info, flags);
+    }
+    return ret;
+}
+
 int qemuMonitorVMStatusToPausedReason(const char *status)
 {
     int st;
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 90e7b45..1269430 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -515,6 +515,11 @@ int qemuMonitorBlockJob(qemuMonitorPtr mon,
                         virDomainBlockJobInfoPtr info,
                         int mode);
 
+int qemuMonitorSetBlockIoThrottle(qemuMonitorPtr mon,
+                                  const char *device,
+                                  virDomainBlockIoThrottleInfoPtr info,
+                                  unsigned int flags);
+
 /**
  * When running two dd process and using <> redirection, we need a
  * shell that will not truncate files.  These two strings serve that
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index cd8f1e5..2f93830 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -3245,3 +3245,48 @@ int qemuMonitorJSONBlockJob(qemuMonitorPtr mon,
     virJSONValueFree(reply);
     return ret;
 }
+
+int qemuMonitorJSONSetBlockIoThrottle(qemuMonitorPtr mon,
+                                      const char *device,
+                                      virDomainBlockIoThrottleInfoPtr info,
+                                      unsigned int flags)
+{
+    int ret = -1;
+    virJSONValuePtr cmd = NULL;
+    virJSONValuePtr result = NULL;
+
+    if (flags) {
+        cmd = qemuMonitorJSONMakeCommand("block_set_io_throttle",
+                                         "s:device", device,
+                                         "U:bps", info->bps,
+                                         "U:bps_rd", info->bps_rd,
+                                         "U:bps_wr", info->bps_wr,
+                                         "U:iops", info->iops,
+                                         "U:iops_rd", info->iops_rd,
+                                         "U:iops_wr", info->iops_wr,
+                                          NULL);
+    }
+
+    if (!cmd)
+        return -1;
+
+    ret = qemuMonitorJSONCommand(mon, cmd, &result);
+
+    if (ret == 0 && virJSONValueObjectHasKey(result, "error")) {
+        if (qemuMonitorJSONHasError(result, "DeviceNotActive"))
+            qemuReportError(VIR_ERR_OPERATION_INVALID,
+                _("No active operation on device: %s"), device);
+        else if (qemuMonitorJSONHasError(result, "NotSupported"))
+            qemuReportError(VIR_ERR_OPERATION_INVALID,
+                _("Operation is not supported for device: %s"), device);
+        else
+            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                _("Unexpected error"));
+        ret = -1;
+    }
+
+    virJSONValueFree(cmd);
+    virJSONValueFree(result);
+    return ret;
+}
+
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index a638b41..36cae79 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -250,4 +250,9 @@ int qemuMonitorJSONSetLink(qemuMonitorPtr mon,
                            const char *name,
                            enum virDomainNetInterfaceLinkState state);
 
+int qemuMonitorJSONSetBlockIoThrottle(qemuMonitorPtr mon,
+                                      const char *device,
+                                      virDomainBlockIoThrottleInfoPtr info,
+                                      unsigned int flags);
+
 #endif /* QEMU_MONITOR_JSON_H */
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index 4774df9..13fdf55 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -3394,3 +3394,42 @@ cleanup:
     VIR_FREE(reply);
     return ret;
 }
+
+int qemuMonitorTextSetBlockIoThrottle(qemuMonitorPtr mon,
+                                      const char *device,
+                                      virDomainBlockIoThrottleInfoPtr info,
+                                      unsigned int flags)
+{
+    char *cmd = NULL;
+    char *result = NULL;
+    int ret = 0;
+
+    if (flags) {
+        ret = virAsprintf(&cmd, "block_set_io_throttle %s %llu %llu %llu %llu %llu %llu",
+                                 device,
+                                 info->bps,
+                                 info->bps_rd,
+                                 info->bps_wr,
+                                 info->iops,
+                                 info->iops_rd,
+                                 info->iops_wr);
+    }
+
+    if (ret < 0) {
+        virReportOOMError();
+        return -1;
+    }
+
+    if (qemuMonitorHMPCommand(mon, cmd, &result) < 0) {
+        qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                        "%s", _("cannot run monitor command"));
+        ret = -1;
+        goto cleanup;
+    }
+
+cleanup:
+    VIR_FREE(cmd);
+    VIR_FREE(result);
+    return ret;
+}
+
diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h
index cc2a252..d64fc55 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -243,4 +243,9 @@ int qemuMonitorTextSetLink(qemuMonitorPtr mon,
                            const char *name,
                            enum virDomainNetInterfaceLinkState state);
 
+int qemuMonitorTextSetBlockIoThrottle(qemuMonitorPtr mon,
+                                      const char *device,
+                                      virDomainBlockIoThrottleInfoPtr info,
+                                      unsigned int flags);
+
 #endif /* QEMU_MONITOR_TEXT_H */
-- 
1.7.1




More information about the libvir-list mailing list