qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>,
	Adam Litke <agl@us.ibm.com>
Subject: [Qemu-devel] [PATCH 11/15] qmp: add block_job_set_speed command
Date: Wed, 27 Jul 2011 14:44:51 +0100	[thread overview]
Message-ID: <1311774295-8696-12-git-send-email-stefanha@linux.vnet.ibm.com> (raw)
In-Reply-To: <1311774295-8696-1-git-send-email-stefanha@linux.vnet.ibm.com>

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 --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     = "",
-- 
1.7.5.4

  parent reply	other threads:[~2011-07-27 13:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-27 13:44 [Qemu-devel] [RFC v2 00/15] QED image streaming Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 01/15] block: add -drive copy-on-read=on|off Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 02/15] qed: replace is_write with flags field Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 03/15] qed: extract qed_start_allocating_write() Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 04/15] qed: make qed_aio_write_alloc() reusable Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 05/15] qed: add support for copy-on-read Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 06/15] qed: avoid deadlock on emulated synchronous I/O Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 07/15] block: add bdrv_aio_copy_backing() Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 08/15] qmp: add block_stream command Stefan Hajnoczi
2011-07-28 15:53   ` Marcelo Tosatti
2011-07-28 15:57     ` Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 09/15] qmp: add block_job_cancel command Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 10/15] qmp: add query-block-jobs command Stefan Hajnoczi
2011-07-27 13:44 ` Stefan Hajnoczi [this message]
2011-07-27 13:44 ` [Qemu-devel] [PATCH 12/15] block: add -drive stream=on|off Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 13/15] qed: intelligent streaming implementation Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 14/15] trace: trace bdrv_aio_readv/writev error paths Stefan Hajnoczi
2011-07-27 13:44 ` [Qemu-devel] [PATCH 15/15] tests: add image streaming QMP interface tests Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1311774295-8696-12-git-send-email-stefanha@linux.vnet.ibm.com \
    --to=stefanha@linux.vnet.ibm.com \
    --cc=agl@us.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).