From patchwork Wed Jan 18 14:40:44 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 136624 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 83AB3B6F65 for ; Thu, 19 Jan 2012 01:42:10 +1100 (EST) Received: from localhost ([::1]:40061 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RnWiG-0003gz-Ha for incoming@patchwork.ozlabs.org; Wed, 18 Jan 2012 09:42:08 -0500 Received: from eggs.gnu.org ([140.186.70.92]:44777) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RnWhU-0001Ta-Nc for qemu-devel@nongnu.org; Wed, 18 Jan 2012 09:41:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RnWhN-0007mY-Si for qemu-devel@nongnu.org; Wed, 18 Jan 2012 09:41:20 -0500 Received: from e06smtp10.uk.ibm.com ([195.75.94.106]:55487) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RnWhN-0007m3-HN for qemu-devel@nongnu.org; Wed, 18 Jan 2012 09:41:13 -0500 Received: from /spool/local by e06smtp10.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 18 Jan 2012 14:41:12 -0000 Received: from d06nrmr1707.portsmouth.uk.ibm.com ([9.149.39.225]) by e06smtp10.uk.ibm.com ([192.168.101.140]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 18 Jan 2012 14:41:10 -0000 Received: from d06av12.portsmouth.uk.ibm.com (d06av12.portsmouth.uk.ibm.com [9.149.37.247]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q0IEf9mK2363506 for ; Wed, 18 Jan 2012 14:41:09 GMT Received: from d06av12.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av12.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q0IEf9Id007670 for ; Wed, 18 Jan 2012 07:41:09 -0700 Received: from localhost (sig-9-145-203-19.de.ibm.com [9.145.203.19]) by d06av12.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q0IEf8vj007643; Wed, 18 Jan 2012 07:41:09 -0700 From: Stefan Hajnoczi To: Date: Wed, 18 Jan 2012 14:40:44 +0000 Message-Id: <1326897655-2799-6-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.8.3 In-Reply-To: <1326897655-2799-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1326897655-2799-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 12011814-4966-0000-0000-000001325FF6 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.106 Cc: Kevin Wolf , Marcelo Tosatti , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v6 05/16] block: add image streaming block job 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 Signed-off-by: Stefan Hajnoczi --- Makefile.objs | 1 + block/stream.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ block_int.h | 3 + trace-events | 4 ++ 4 files changed, 141 insertions(+), 0 deletions(-) create mode 100644 block/stream.c diff --git a/Makefile.objs b/Makefile.objs index f4f52e0..949308d 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -35,6 +35,7 @@ block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow block-nested-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o block-nested-y += qed-check.o block-nested-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o +block-nested-y += stream.o block-nested-$(CONFIG_WIN32) += raw-win32.o block-nested-$(CONFIG_POSIX) += raw-posix.o block-nested-$(CONFIG_LIBISCSI) += iscsi.o diff --git a/block/stream.c b/block/stream.c new file mode 100644 index 0000000..3a99094 --- /dev/null +++ b/block/stream.c @@ -0,0 +1,133 @@ +/* + * Image streaming + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Stefan Hajnoczi + * + * This work is licensed under the terms of the GNU LGPL, version 2 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#include "trace.h" +#include "block_int.h" + +enum { + /* + * Size of data buffer for populating the image file. This should be large + * enough to process multiple clusters in a single call, so that populating + * contiguous regions of the image is efficient. + */ + STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */ +}; + +typedef struct StreamBlockJob { + BlockJob common; + BlockDriverState *base; +} StreamBlockJob; + +static int coroutine_fn stream_populate(BlockDriverState *bs, + int64_t sector_num, int nb_sectors, + void *buf) +{ + struct iovec iov = { + .iov_base = buf, + .iov_len = nb_sectors * BDRV_SECTOR_SIZE, + }; + QEMUIOVector qiov; + + qemu_iovec_init_external(&qiov, &iov, 1); + + /* Copy-on-read the unallocated clusters */ + return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov); +} + +static void coroutine_fn stream_run(void *opaque) +{ + StreamBlockJob *s = opaque; + BlockDriverState *bs = s->common.bs; + int64_t sector_num, end; + int ret = 0; + int n; + void *buf; + + s->common.len = bdrv_getlength(bs); + if (s->common.len < 0) { + block_job_complete(&s->common, s->common.len); + return; + } + + end = s->common.len >> BDRV_SECTOR_BITS; + buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE); + + /* Turn on copy-on-read for the whole block device so that guest read + * requests help us make progress. Only do this when copying the entire + * backing chain since the copy-on-read operation does not take base into + * account. + */ + if (!base) { + bdrv_enable_copy_on_read(bs); + } + + for (sector_num = 0; sector_num < end; sector_num += n) { + 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) { + ret = stream_populate(bs, sector_num, n, buf); + } + if (ret < 0) { + break; + } + + /* Publish progress */ + s->common.offset += n * BDRV_SECTOR_SIZE; + } + + if (!base) { + bdrv_disable_copy_on_read(bs); + } + + if (sector_num == end && ret == 0) { + ret = bdrv_change_backing_file(bs, NULL, NULL); + } + + qemu_vfree(buf); + block_job_complete(&s->common, ret); +} + +static BlockJobType stream_job_type = { + .instance_size = sizeof(StreamBlockJob), + .job_type = "stream", +}; + +int stream_start(BlockDriverState *bs, BlockDriverState *base, + BlockDriverCompletionFunc *cb, void *opaque) +{ + StreamBlockJob *s; + Coroutine *co; + + s = block_job_create(&stream_job_type, bs, cb, opaque); + if (!s) { + return -EBUSY; /* bs must already be in use */ + } + + s->base = base; + + co = qemu_coroutine_create(stream_run); + trace_stream_start(bs, base, s, co, opaque); + qemu_coroutine_enter(co, s); + return 0; +} diff --git a/block_int.h b/block_int.h index 3566acb..4f63870 100644 --- a/block_int.h +++ b/block_int.h @@ -327,4 +327,7 @@ int block_job_set_speed(BlockJob *job, int64_t value); void block_job_cancel(BlockJob *job); bool block_job_is_cancelled(BlockJob *job); +int stream_start(BlockDriverState *bs, BlockDriverState *base, + BlockDriverCompletionFunc *cb, void *opaque); + #endif /* BLOCK_INT_H */ diff --git a/trace-events b/trace-events index 656127c..84945e2 100644 --- a/trace-events +++ b/trace-events @@ -70,6 +70,10 @@ bdrv_co_writev(void *bs, int64_t sector_num, int nb_sector) "bs %p sector_num %" bdrv_co_io_em(void *bs, int64_t sector_num, int nb_sectors, int is_write, void *acb) "bs %p sector_num %"PRId64" nb_sectors %d is_write %d acb %p" bdrv_co_do_copy_on_readv(void *bs, int64_t sector_num, int nb_sectors, int64_t cluster_sector_num, int cluster_nb_sectors) "bs %p sector_num %"PRId64" nb_sectors %d cluster_sector_num %"PRId64" cluster_nb_sectors %d" +# block/stream.c +stream_one_iteration(void *s, int64_t sector_num, int nb_sectors, int is_allocated) "s %p sector_num %"PRId64" nb_sectors %d is_allocated %d" +stream_start(void *bs, void *base, void *s, void *co, void *opaque) "bs %p base %p s %p co %p opaque %p" + # hw/virtio-blk.c virtio_blk_req_complete(void *req, int status) "req %p status %d" virtio_blk_rw_complete(void *req, int ret) "req %p ret %d"