From patchwork Tue Dec 13 13:52:26 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 131098 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2AC581007D5 for ; Wed, 14 Dec 2011 00:55:37 +1100 (EST) Received: from localhost ([::1]:54783 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaSpP-0002wo-BT for incoming@patchwork.ozlabs.org; Tue, 13 Dec 2011 08:55:31 -0500 Received: from eggs.gnu.org ([140.186.70.92]:55310) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaSoq-0001ut-IE for qemu-devel@nongnu.org; Tue, 13 Dec 2011 08:54:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RaSok-0004CU-KE for qemu-devel@nongnu.org; Tue, 13 Dec 2011 08:54:56 -0500 Received: from e06smtp13.uk.ibm.com ([195.75.94.109]:49926) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaSok-0004CN-Al for qemu-devel@nongnu.org; Tue, 13 Dec 2011 08:54:50 -0500 Received: from /spool/local by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 13 Dec 2011 13:54:49 -0000 Received: from d06nrmr1507.portsmouth.uk.ibm.com ([9.149.38.233]) by e06smtp13.uk.ibm.com ([192.168.101.143]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 13 Dec 2011 13:54:47 -0000 Received: from d06av01.portsmouth.uk.ibm.com (d06av01.portsmouth.uk.ibm.com [9.149.37.212]) by d06nrmr1507.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pBDDskL22474104 for ; Tue, 13 Dec 2011 13:54:46 GMT Received: from d06av01.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pBDDskPf002570 for ; Tue, 13 Dec 2011 06:54:46 -0700 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pBDDskxd002563; Tue, 13 Dec 2011 06:54:46 -0700 From: Stefan Hajnoczi To: Date: Tue, 13 Dec 2011 13:52:26 +0000 Message-Id: <1323784351-25531-5-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1323784351-25531-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1323784351-25531-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 11121313-2966-0000-0000-0000029C31DE X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.109 Cc: Kevin Wolf , Marcelo Tosatti , Stefan Hajnoczi , Luiz Capitulino Subject: [Qemu-devel] [PATCH v3 4/9] block: rate-limit streaming operations X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- block/stream.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 59 insertions(+), 6 deletions(-) diff --git a/block/stream.c b/block/stream.c index 7d362ab..7f5b983 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; @@ -61,20 +92,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) { @@ -83,6 +118,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); @@ -96,9 +136,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,