diff mbox

[11/15] qmp: add block_job_set_speed command

Message ID 1311774295-8696-12-git-send-email-stefanha@linux.vnet.ibm.com
State New
Headers show

Commit Message

Stefan Hajnoczi July 27, 2011, 1:44 p.m. UTC
The block_job_set_speed command sets a throughput limit on an active
image streaming operation.  This can be used to isolate the streaming
operation and control the amount of I/O bandwidth it consumes.

The command synopsis is as follows:

block_job_set_speed
-------------------

Set maximum speed for a background block operation.

This is a per-block device command that can only be issued
when there is an active block job.

Throttling can be disabled by setting the speed to 0.

Arguments:

- device: device name (json-string)
- value:  maximum speed, in bytes per second (json-int)

Errors:
DeviceNotActive: streaming is not active on this device
NotSupported:    job type does not support speed setting

Example:

-> { "execute": "block_job_set_speed",
    "arguments": { "device": "virtio0", "value": 1024 } }

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 blockdev.c      |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 blockdev.h      |    2 +
 hmp-commands.hx |   14 ++++++++++++
 qmp-commands.hx |   35 ++++++++++++++++++++++++++++++
 4 files changed, 111 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/blockdev.c b/blockdev.c
index 422b43b..a044830 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -51,12 +51,20 @@  static const int if_max_devs[IF_COUNT] = {
     [IF_SCSI] = 7,
 };
 
+enum {
+    SLICE_TIME_NS = 100000000,  /* 100 ms rate-limiting slice time */
+};
+
 typedef struct StreamState {
     MonitorCompletion *cancel_cb;
     void *cancel_opaque;
     int64_t offset;             /* current position in block device */
     BlockDriverState *bs;
     QEMUTimer *timer;
+    int64_t bytes_per_sec;      /* rate limit */
+    int64_t bytes_per_slice;    /* rate limit scaled to slice */
+    int64_t slice_end_time;     /* when this slice finishes */
+    int64_t slice_start_offset; /* offset when slice started */
     QLIST_ENTRY(StreamState) list;
 } StreamState;
 
@@ -71,7 +79,7 @@  static QObject *stream_get_qobject(StreamState *s)
     return qobject_from_jsonf("{ 'device': %s, 'type': 'stream', "
                               "'offset': %" PRId64 ", 'len': %" PRId64 ", "
                               "'speed': %" PRId64 " }",
-                              name, s->offset, len, (int64_t)0);
+                              name, s->offset, len, s->bytes_per_sec);
 }
 
 static void stream_mon_event(StreamState *s, int ret)
@@ -107,6 +115,27 @@  static void stream_complete(StreamState *s, int ret)
     stream_free(s);
 }
 
+static void stream_schedule_next_iteration(StreamState *s)
+{
+    int64_t next = qemu_get_clock_ns(rt_clock);
+
+    /* New slice */
+    if (next >= s->slice_end_time) {
+        s->slice_end_time = next + SLICE_TIME_NS;
+        s->slice_start_offset = s->offset;
+    }
+
+    /* Throttle */
+    if (s->bytes_per_slice &&
+        s->offset - s->slice_start_offset >= s->bytes_per_slice) {
+        next = s->slice_end_time;
+        s->slice_end_time = next + SLICE_TIME_NS;
+        s->slice_start_offset += s->bytes_per_slice;
+    }
+
+    qemu_mod_timer(s->timer, next);
+}
+
 static void stream_cb(void *opaque, int nb_sectors)
 {
     StreamState *s = opaque;
@@ -124,7 +153,7 @@  static void stream_cb(void *opaque, int nb_sectors)
     } else if (s->cancel_cb) {
         stream_free(s);
     } else {
-        qemu_mod_timer(s->timer, qemu_get_clock_ns(rt_clock));
+        stream_schedule_next_iteration(s);
     }
 }
 
@@ -202,6 +231,20 @@  static int stream_stop(const char *device, MonitorCompletion *cb, void *opaque)
     return 0;
 }
 
+static int stream_set_speed(const char *device, int64_t bytes_per_sec)
+{
+    StreamState *s = stream_find(device);
+
+    if (!s) {
+        qerror_report(QERR_DEVICE_NOT_ACTIVE, device);
+        return -1;
+    }
+
+    s->bytes_per_sec = bytes_per_sec;
+    s->bytes_per_slice = bytes_per_sec * SLICE_TIME_NS / 1000000000LL;
+    return 0;
+}
+
 /*
  * We automatically delete the drive when a device using it gets
  * unplugged.  Questionable feature, but we can't just drop it.
@@ -814,7 +857,7 @@  static void monitor_print_block_stream(Monitor *mon, const QObject *data)
                    qdict_get_str(stream, "device"),
                    qdict_get_int(stream, "offset"),
                    qdict_get_int(stream, "len"),
-                   (int64_t)0);
+                   qdict_get_int(stream, "speed"));
 }
 
 void monitor_print_block_jobs(Monitor *mon, const QObject *data)
@@ -863,6 +906,20 @@  int do_block_job_cancel(Monitor *mon, const QDict *params,
     return stream_stop(device, cb, opaque);
 }
 
+int do_block_job_set_speed(Monitor *mon, const QDict *params,
+                           QObject **ret_data)
+{
+    const char *device = qdict_get_str(params, "device");
+    int64_t value;
+
+    value = qdict_get_int(params, "value");
+    if (value < 0) {
+        value = 0;
+    }
+
+    return stream_set_speed(device, value);
+}
+
 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
 {
     if (!force) {
diff --git a/blockdev.h b/blockdev.h
index 0a32793..6f09597 100644
--- a/blockdev.h
+++ b/blockdev.h
@@ -71,5 +71,7 @@  void do_info_block_jobs(Monitor *mon, QObject **ret_data);
 int do_block_stream(Monitor *mon, const QDict *params, QObject **ret_data);
 int do_block_job_cancel(Monitor *mon, const QDict *params,
                         MonitorCompletion cb, void *opaque);
+int do_block_job_set_speed(Monitor *mon, const QDict *params,
+                           QObject **ret_data);
 
 #endif
diff --git a/hmp-commands.hx b/hmp-commands.hx
index 74a74d8..2470c3f 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -67,6 +67,20 @@  Stop an active block streaming operation.
 ETEXI
 
     {
+        .name       = "block_job_set_speed",
+        .args_type  = "device:B,value:o",
+        .params     = "device value",
+        .help       = "Set the maximum speed for a background block operation",
+        .mhandler.cmd_new = do_block_job_set_speed,
+    },
+
+STEXI
+@item block_job_set_speed @var{device} @var{value}
+@findex block_job_set_speed
+Set the maximum speed for a background block operation.
+ETEXI
+
+    {
         .name       = "q|quit",
         .args_type  = "",
         .params     = "",
diff --git a/qmp-commands.hx b/qmp-commands.hx
index c3a72ad..c969909 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -1051,6 +1051,41 @@  Examples:
 EQMP
 
     {
+        .name       = "block_job_set_speed",
+        .args_type  = "device:B,value:o",
+        .params     = "device value",
+        .help       = "Set maximum speed for a background block operation",
+        .mhandler.cmd_new = do_block_job_set_speed,
+    },
+
+SQMP
+block_job_set_speed
+-------------------
+
+Set maximum speed for a background block operation.
+
+This is a per-block device command that can only be issued
+when there is an active block job.
+
+Throttling can be disabled by setting the speed to 0.
+
+Arguments:
+
+- device: device name (json-string)
+- value:  maximum speed, in bytes per second (json-int)
+
+Errors:
+DeviceNotActive: streaming is not active on this device
+NotSupported:    job type does not support speed setting
+
+Example:
+
+-> { "execute": "block_job_set_speed",
+    "arguments": { "device": "virtio0", "value": 1024 } }
+
+EQMP
+
+    {
         .name       = "qmp_capabilities",
         .args_type  = "",
         .params     = "",