From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Marcelo Tosatti <mtosatti@redhat.com>,
Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>,
Luiz Capitulino <lcapitulino@redhat.com>
Subject: [Qemu-devel] [PATCH v5 05/15] block: rate-limit streaming operations
Date: Fri, 13 Jan 2012 13:14:07 +0000 [thread overview]
Message-ID: <1326460457-19446-6-git-send-email-stefanha@linux.vnet.ibm.com> (raw)
In-Reply-To: <1326460457-19446-1-git-send-email-stefanha@linux.vnet.ibm.com>
This patch implements rate-limiting for image streaming. If we've
exceeded the bandwidth quota for a 100 ms time slice we sleep the
coroutine until the next slice begins.
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
block/stream.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 59 insertions(+), 6 deletions(-)
diff --git a/block/stream.c b/block/stream.c
index 5255a61..93f0305 100644
--- a/block/stream.c
+++ b/block/stream.c
@@ -23,8 +23,39 @@ enum {
STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */
};
+#define SLICE_TIME 100000000ULL /* ns */
+
+typedef struct {
+ int64_t next_slice_time;
+ uint64_t slice_quota;
+ uint64_t dispatched;
+} RateLimit;
+
+static int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)
+{
+ int64_t delay_ns = 0;
+ int64_t now = qemu_get_clock_ns(rt_clock);
+
+ if (limit->next_slice_time < now) {
+ limit->next_slice_time = now + SLICE_TIME;
+ limit->dispatched = 0;
+ }
+ if (limit->dispatched + n > limit->slice_quota) {
+ delay_ns = limit->next_slice_time - now;
+ } else {
+ limit->dispatched += n;
+ }
+ return delay_ns;
+}
+
+static void ratelimit_set_speed(RateLimit *limit, uint64_t speed)
+{
+ limit->slice_quota = speed / (1000000000ULL / SLICE_TIME);
+}
+
typedef struct StreamBlockJob {
BlockJob common;
+ RateLimit limit;
BlockDriverState *base;
} StreamBlockJob;
@@ -65,20 +96,24 @@ static void coroutine_fn stream_run(void *opaque)
bdrv_enable_copy_on_read(bs);
for (sector_num = 0; sector_num < end; sector_num += n) {
+retry:
if (block_job_is_cancelled(&s->common)) {
break;
}
- /* TODO rate-limit */
- /* Note that even when no rate limit is applied we need to yield with
- * no pending I/O here so that qemu_aio_flush() is able to return.
- */
- co_sleep_ns(rt_clock, 0);
-
ret = bdrv_co_is_allocated(bs, sector_num,
STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
trace_stream_one_iteration(s, sector_num, n, ret);
if (ret == 0) {
+ if (s->common.speed) {
+ uint64_t delay_ns = ratelimit_calculate_delay(&s->limit, n);
+ if (delay_ns > 0) {
+ co_sleep_ns(rt_clock, delay_ns);
+
+ /* Recheck cancellation and that sectors are unallocated */
+ goto retry;
+ }
+ }
ret = stream_populate(bs, sector_num, n, buf);
}
if (ret < 0) {
@@ -87,6 +122,11 @@ static void coroutine_fn stream_run(void *opaque)
/* Publish progress */
s->common.offset += n * BDRV_SECTOR_SIZE;
+
+ /* Note that even when no rate limit is applied we need to yield
+ * with no pending I/O here so that qemu_aio_flush() returns.
+ */
+ co_sleep_ns(rt_clock, 0);
}
bdrv_disable_copy_on_read(bs);
@@ -99,9 +139,22 @@ static void coroutine_fn stream_run(void *opaque)
block_job_complete(&s->common, ret);
}
+static int stream_set_speed(BlockJob *job, int64_t value)
+{
+ StreamBlockJob *s = container_of(job, StreamBlockJob, common);
+
+ if (value < 0) {
+ return -EINVAL;
+ }
+ job->speed = value;
+ ratelimit_set_speed(&s->limit, value / BDRV_SECTOR_SIZE);
+ return 0;
+}
+
static BlockJobType stream_job_type = {
.instance_size = sizeof(StreamBlockJob),
.job_type = "stream",
+ .set_speed = stream_set_speed,
};
int stream_start(BlockDriverState *bs, BlockDriverState *base,
--
1.7.7.3
next prev parent reply other threads:[~2012-01-13 13:15 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-01-13 13:14 [Qemu-devel] [PATCH v5 00/15] block: generic image streaming Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 01/15] coroutine: add co_sleep_ns() coroutine sleep function Stefan Hajnoczi
2012-01-17 12:54 ` Kevin Wolf
2012-01-17 13:31 ` Stefan Hajnoczi
2012-01-17 13:38 ` Kevin Wolf
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 02/15] block: check bdrv_in_use() before blockdev operations Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 03/15] block: add BlockJob interface for long-running operations Stefan Hajnoczi
2012-01-17 13:00 ` Kevin Wolf
2012-01-17 13:33 ` Stefan Hajnoczi
2012-01-17 13:44 ` Kevin Wolf
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 04/15] block: add image streaming block job Stefan Hajnoczi
2012-01-13 13:14 ` Stefan Hajnoczi [this message]
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 06/15] qmp: add block_stream command Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 07/15] qmp: add block_job_set_speed command Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 08/15] qmp: add block_job_cancel command Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 09/15] qmp: add query-block-jobs Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 10/15] blockdev: make image streaming safe across hotplug Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 11/15] block: add bdrv_find_backing_image Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 12/15] add QERR_BASE_NOT_FOUND Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 13/15] block: add support for partial streaming Stefan Hajnoczi
2012-01-17 13:27 ` Kevin Wolf
2012-01-17 13:50 ` Marcelo Tosatti
2012-01-17 14:05 ` Kevin Wolf
2012-01-17 15:47 ` Marcelo Tosatti
2012-01-17 15:55 ` Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 14/15] docs: describe live block operations Stefan Hajnoczi
2012-01-13 13:14 ` [Qemu-devel] [PATCH v5 15/15] test: add image streaming test cases Stefan Hajnoczi
2012-01-13 16:49 ` Stefan Hajnoczi
2012-01-17 19:07 ` Lucas Meneghel Rodrigues
2012-01-18 8:47 ` Stefan Hajnoczi
2012-01-16 11:20 ` [Qemu-devel] [PATCH v5 00/15] block: generic image streaming Luiz Capitulino
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=1326460457-19446-6-git-send-email-stefanha@linux.vnet.ibm.com \
--to=stefanha@linux.vnet.ibm.com \
--cc=kwolf@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=mtosatti@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).