From nobody Mon May 6 23:15:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486053063206109.82049162449607; Thu, 2 Feb 2017 08:31:03 -0800 (PST) Received: from localhost ([::1]:57681 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZKHl-0005pG-1M for importer@patchew.org; Thu, 02 Feb 2017 11:31:01 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57693) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZJuU-00008l-8B for qemu-devel@nongnu.org; Thu, 02 Feb 2017 11:07:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZJuR-000641-Gv for qemu-devel@nongnu.org; Thu, 02 Feb 2017 11:06:58 -0500 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:45443 helo=mx01.kamp.de) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZJuR-00061e-6O for qemu-devel@nongnu.org; Thu, 02 Feb 2017 11:06:55 -0500 Received: (qmail 9454 invoked by uid 89); 2 Feb 2017 16:06:48 -0000 Received: from [195.62.97.28] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2010/03/19-MF (clamdscan: 0.99.2/22989. avast: 1.2.2/17010300. spamassassin: 3.4.1. Clear:RC:1(195.62.97.28):. Processed in 0.056453 secs); 02 Feb 2017 16:06:48 -0000 Received: from smtp.kamp.de (HELO submission.kamp.de) ([195.62.97.28]) by mx01.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted); 2 Feb 2017 16:06:47 -0000 Received: (qmail 11862 invoked from network); 2 Feb 2017 16:06:47 -0000 Received: from lieven-pc.kamp-intra.net (HELO lieven-pc) (relay@kamp.de@::ffff:172.21.12.60) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 2 Feb 2017 16:06:47 -0000 Received: by lieven-pc (Postfix, from userid 1000) id 61D91202E7; Thu, 2 Feb 2017 17:06:47 +0100 (CET) X-GL_Whitelist: yes From: Peter Lieven To: qemu-devel@nongnu.org Date: Thu, 2 Feb 2017 17:06:44 +0100 Message-Id: <1486051604-32310-1-git-send-email-pl@kamp.de> X-Mailer: git-send-email 1.9.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a02:248:0:51::16 Subject: [Qemu-devel] [RFC][PATCH] qemu-img: make convert async X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Peter Lieven , qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" this is something I have been thinking about for almost 2 years now. we heavily have the following two use cases when using qemu-img convert. a) reading from NFS and writing to iSCSI for deploying templates b) reading from iSCSI and writing to NFS for backups In both processes we use libiscsi and libnfs so we have no kernel pagecache. As qemu-img convert is implemented with sync operations that means we read one buffer and then write it. No parallelism and each sync request takes as long as it takes until it is completed. What I put together is an approach to use aio routines for the conversion process to have ideally read and write happening in parallel. The code is far from clean or complete, but I would appreaciate comments and thoughts from you. So far I have the following runtimes when reading an uncompressed QCOW2 from NFS and writing it to iSCSI (raw): qemu-img (master) nfs -> iscsi 33 secs nfs -> ram 19 secs ram -> iscsi 14 secs qemu-img-async nfs -> iscsi 23 secs nfs -> ram 17 secs ram -> iscsi 14 secs Its visible that on master the runtimes add up as expected. The async branch is faster, but not as fast as I would have expected. I would expect the run= time to be as slow as the slowest of the two involved transfers. Thank you, Peter Signed-off-by: Peter Lieven --- qemu-img.c | 271 +++++++++++++++++++++++++++++++++++++++++++++------------= ---- 1 file changed, 199 insertions(+), 72 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 5df66fe..d06f968 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1446,6 +1446,29 @@ enum ImgConvertBlockStatus { BLK_BACKING_FILE, }; =20 +#define CONVERT_MAX_REQS 4 + +typedef struct ImgConvertState ImgConvertState; + +typedef struct ImgConvertReq { + ImgConvertState *s; + int64_t sector_num; + int64_t sector_offs; + int64_t next_sector; + int nb_sectors; + QEMUIOVector qiov; + uint8_t *buf; + enum ImgConvertBlockStatus status; + bool rd_completed; +} ImgConvertReq; + +typedef struct ImgConvertQueueElt { + int64_t sector_num; + enum ImgConvertBlockStatus status; + int nb_sectors; + QSIMPLEQ_ENTRY(ImgConvertQueueElt) next; +} ImgConvertQueueElt; + typedef struct ImgConvertState { BlockBackend **src; int64_t *src_sectors; @@ -1453,6 +1476,8 @@ typedef struct ImgConvertState { int64_t src_cur_offset; int64_t total_sectors; int64_t allocated_sectors; + int64_t allocated_done; + int64_t wr_offs; enum ImgConvertBlockStatus status; int64_t sector_next_status; BlockBackend *target; @@ -1462,11 +1487,15 @@ typedef struct ImgConvertState { int min_sparse; size_t cluster_sectors; size_t buf_sectors; + ImgConvertReq reqs[CONVERT_MAX_REQS]; + int ret; + QSIMPLEQ_HEAD(, ImgConvertQueueElt) queue; } ImgConvertState; =20 static void convert_select_part(ImgConvertState *s, int64_t sector_num) { - assert(sector_num >=3D s->src_cur_offset); + s->src_cur_offset =3D 0; + s->src_cur =3D 0; while (sector_num - s->src_cur_offset >=3D s->src_sectors[s->src_cur])= { s->src_cur_offset +=3D s->src_sectors[s->src_cur]; s->src_cur++; @@ -1542,16 +1571,89 @@ static int convert_iteration_sectors(ImgConvertStat= e *s, int64_t sector_num) return n; } =20 -static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sec= tors, - uint8_t *buf) +static void convert_aio_read(void *opaque, int ret); +static void convert_aio_write(void *opaque, int ret); + +static void convert_fill_write_queue(ImgConvertState *s) { + int i; + for (i =3D 0; i < CONVERT_MAX_REQS; i++) { + if (s->reqs[i].sector_num =3D=3D s->wr_offs && s->reqs[i].rd_comp= leted) { + s->reqs[i].rd_completed =3D false; + s->reqs[i].sector_offs =3D 0; + convert_aio_write(&s->reqs[i], 0); + break; + } + } +} + +static void convert_fill_read_queue(ImgConvertState *s) +{ + ImgConvertQueueElt *elt; + + while ((elt =3D QSIMPLEQ_FIRST(&s->queue)) && !s->ret) { + ImgConvertReq *req =3D NULL; + int i; + for (i =3D 0; i < CONVERT_MAX_REQS; i++) { + if (s->reqs[i].sector_num =3D=3D -1) { + req =3D &s->reqs[i]; + break; + } + } + if (!req) { + return; + } + + QSIMPLEQ_REMOVE_HEAD(&s->queue, next); + + printf("convert_fill_read_queue req #%d @%p sector_num %ld nb_sect= ors %d status %d\n", i, req, elt->sector_num, elt->nb_sectors, elt->status); + + if (elt->status =3D=3D BLK_DATA || (!s->min_sparse && elt->status = =3D=3D BLK_ZERO)) + { + s->allocated_done +=3D elt->nb_sectors; + qemu_progress_print(100.0 * s->allocated_done / s->allocated_s= ectors, + 0); + } + req->sector_num =3D elt->sector_num; + req->nb_sectors =3D elt->nb_sectors; + req->status =3D elt->status; + req->sector_offs =3D 0; + g_free(elt); + + if ((elt =3D QSIMPLEQ_FIRST(&s->queue))) { + req->next_sector =3D elt->sector_num; + } else { + req->next_sector =3D s->total_sectors; + } + + if (req->status =3D=3D BLK_DATA) { + req->rd_completed =3D false; + convert_aio_read(req, 0); + } else if (!s->min_sparse && s->status =3D=3D BLK_ZERO) { + memset(req->buf, 0, req->nb_sectors * BDRV_SECTOR_SIZE); + req->status =3D BLK_DATA; + req->rd_completed =3D true; + convert_fill_write_queue(s); + } + } +} + +static void convert_aio_read(void *opaque, int ret) +{ + ImgConvertReq *req =3D opaque; + ImgConvertState *s =3D req->s; + int64_t sector_num =3D req->sector_num + req->sector_offs; + int nb_sectors =3D req->nb_sectors - req->sector_offs; + uint8_t *buf =3D req->buf + req->sector_offs * BDRV_SECTOR_SIZE; int n; - int ret; + + printf("convert_aio_read enter req %p sector_num %ld nb_sectors %d off= s %ld ret %d\n", req, sector_num, nb_sectors, req->sector_offs, ret); =20 assert(nb_sectors <=3D s->buf_sectors); - while (nb_sectors > 0) { + if (nb_sectors > 0) { BlockBackend *blk; int64_t bs_sectors; + BlockAIOCB *acb; =20 /* In the case of compression with multiple source files, we can g= et a * nb_sectors that spreads into the next part. So we must be able = to @@ -1561,30 +1663,49 @@ static int convert_read(ImgConvertState *s, int64_t= sector_num, int nb_sectors, bs_sectors =3D s->src_sectors[s->src_cur]; =20 n =3D MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset= )); - ret =3D blk_pread(blk, + qemu_iovec_reset(&req->qiov); + qemu_iovec_add(&req->qiov, buf, n << BDRV_SECTOR_BITS); + req->sector_offs +=3D n; + acb =3D blk_aio_preadv(blk, (sector_num - s->src_cur_offset) << BDRV_SECTOR_BI= TS, - buf, n << BDRV_SECTOR_BITS); - if (ret < 0) { - return ret; + &req->qiov, 0, convert_aio_read, req); + if (!acb) { + s->ret =3D -ENOMEM; + return; } - - sector_num +=3D n; - nb_sectors -=3D n; - buf +=3D n * BDRV_SECTOR_SIZE; + return; } =20 - return 0; + req->rd_completed =3D true; + convert_fill_write_queue(s); } =20 -static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_se= ctors, - const uint8_t *buf) +static void convert_aio_write(void *opaque, int ret) { - int ret; + ImgConvertReq *req =3D opaque; + ImgConvertState *s =3D req->s; + int64_t sector_num; + int nb_sectors; + uint8_t *buf; + +again: + sector_num =3D req->sector_num + req->sector_offs; + nb_sectors =3D req->nb_sectors - req->sector_offs; + buf =3D req->buf + req->sector_offs * BDRV_SECTOR_SIZE; + printf("convert_aio_write enter req %p sector_num %ld nb_sectors %d of= fs %ld ret %d\n", req, sector_num, nb_sectors, req->sector_offs, ret); =20 - while (nb_sectors > 0) { + if (ret) { + error_report("error while writing sector %" PRId64 + ": %s", req->sector_num, strerror(-ret)); + s->ret =3D ret; + return; + } + + if (nb_sectors > 0) { int n =3D nb_sectors; + BlockAIOCB *acb; =20 - switch (s->status) { + switch (req->status) { case BLK_BACKING_FILE: /* If we have a backing file, leave clusters unallocated that = are * unallocated in the source image, so that the backing file is @@ -1598,6 +1719,7 @@ static int convert_write(ImgConvertState *s, int64_t = sector_num, int nb_sectors, * write if the buffer is completely zeroed and we're allowed = to * keep the target sparse. */ if (s->compressed) { + assert(0); //XXX TODO if (s->has_zero_init && s->min_sparse && buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) { @@ -1609,7 +1731,8 @@ static int convert_write(ImgConvertState *s, int64_t = sector_num, int nb_sectors, sector_num << BDRV_SECTOR_BITS, buf, n << BDRV_SECTOR_BITS); if (ret < 0) { - return ret; + s->ret =3D ret; + return; } break; } @@ -1620,10 +1743,15 @@ static int convert_write(ImgConvertState *s, int64_= t sector_num, int nb_sectors, if (!s->min_sparse || is_allocated_sectors_min(buf, n, &n, s->min_sparse)) { - ret =3D blk_pwrite(s->target, sector_num << BDRV_SECTOR_BI= TS, - buf, n << BDRV_SECTOR_BITS, 0); - if (ret < 0) { - return ret; + qemu_iovec_reset(&req->qiov); + qemu_iovec_add(&req->qiov, buf, n << BDRV_SECTOR_BITS); + req->sector_offs +=3D n; + acb =3D blk_aio_pwritev(s->target, + sector_num << BDRV_SECTOR_BITS, + &req->qiov, 0, convert_aio_write, re= q); + if (!acb) { + s->ret =3D -ENOMEM; + return; } break; } @@ -1631,30 +1759,39 @@ static int convert_write(ImgConvertState *s, int64_= t sector_num, int nb_sectors, =20 case BLK_ZERO: if (s->has_zero_init) { - break; + printf("convert_aio_write n %d blocks are zero\n", n); + req->sector_offs +=3D n; + goto again; } + assert(0); //XXX TODO ret =3D blk_pwrite_zeroes(s->target, sector_num << BDRV_SECTOR= _BITS, n << BDRV_SECTOR_BITS, 0); if (ret < 0) { - return ret; + s->ret =3D ret; + return; } break; } =20 - sector_num +=3D n; - nb_sectors -=3D n; - buf +=3D n * BDRV_SECTOR_SIZE; + return; } =20 - return 0; + /* request is available for reading again */ + printf("wr offs new =3D %ld\n", req->next_sector); + req->sector_num =3D -1; + s->wr_offs =3D req->next_sector; + + convert_fill_read_queue(s); + convert_fill_write_queue(s); + return; } =20 static int convert_do_copy(ImgConvertState *s) { - uint8_t *buf =3D NULL; - int64_t sector_num, allocated_done; int ret; int n; + int i; + int64_t sector_num; =20 /* Check whether we have zero initialisation or can get it efficiently= */ s->has_zero_init =3D s->min_sparse && !s->target_has_backing @@ -1680,20 +1817,38 @@ static int convert_do_copy(ImgConvertState *s) } s->buf_sectors =3D s->cluster_sectors; } - buf =3D blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); + + for (i =3D 0; i < CONVERT_MAX_REQS; i++) { + qemu_iovec_init(&s->reqs[i].qiov, 1); + s->reqs[i].buf =3D blk_blockalign(s->target, s->buf_sectors * BDRV= _SECTOR_SIZE); + s->reqs[i].sector_num =3D -1; + s->reqs[i].s =3D s; + } =20 /* Calculate allocated sectors for progress */ s->allocated_sectors =3D 0; sector_num =3D 0; + QSIMPLEQ_INIT(&s->queue); while (sector_num < s->total_sectors) { n =3D convert_iteration_sectors(s, sector_num); if (n < 0) { ret =3D n; goto fail; } + + if (!s->min_sparse && s->status =3D=3D BLK_ZERO) { + n =3D MIN(n, s->buf_sectors); + } + + printf("convert_iteration_sectors %ld n %d status %d\n", sector_nu= m, n, s->status); if (s->status =3D=3D BLK_DATA || (!s->min_sparse && s->status =3D= =3D BLK_ZERO)) { + ImgConvertQueueElt *elt =3D g_malloc(sizeof(ImgConvertQueueElt= )); + elt->sector_num =3D sector_num; + elt->status =3D s->status; + elt->nb_sectors =3D n; s->allocated_sectors +=3D n; + QSIMPLEQ_INSERT_TAIL(&s->queue, elt, next); } sector_num +=3D n; } @@ -1702,47 +1857,16 @@ static int convert_do_copy(ImgConvertState *s) s->src_cur =3D 0; s->src_cur_offset =3D 0; s->sector_next_status =3D 0; + s->wr_offs =3D 0; + s->allocated_done =3D 0; =20 - sector_num =3D 0; - allocated_done =3D 0; + convert_fill_read_queue(s); =20 - while (sector_num < s->total_sectors) { - n =3D convert_iteration_sectors(s, sector_num); - if (n < 0) { - ret =3D n; - goto fail; - } - if (s->status =3D=3D BLK_DATA || (!s->min_sparse && s->status =3D= =3D BLK_ZERO)) - { - allocated_done +=3D n; - qemu_progress_print(100.0 * allocated_done / s->allocated_sect= ors, - 0); - } - - if (s->status =3D=3D BLK_DATA) { - ret =3D convert_read(s, sector_num, n, buf); - if (ret < 0) { - error_report("error while reading sector %" PRId64 - ": %s", sector_num, strerror(-ret)); - goto fail; - } - } else if (!s->min_sparse && s->status =3D=3D BLK_ZERO) { - n =3D MIN(n, s->buf_sectors); - memset(buf, 0, n * BDRV_SECTOR_SIZE); - s->status =3D BLK_DATA; - } - - ret =3D convert_write(s, sector_num, n, buf); - if (ret < 0) { - error_report("error while writing sector %" PRId64 - ": %s", sector_num, strerror(-ret)); - goto fail; - } - - sector_num +=3D n; + while (s->wr_offs < s->total_sectors && !s->ret) { + main_loop_wait(false); } =20 - if (s->compressed) { + if (s->compressed && !s->ret) { /* signal EOF to align */ ret =3D blk_pwrite_compressed(s->target, 0, NULL, 0); if (ret < 0) { @@ -1750,9 +1874,12 @@ static int convert_do_copy(ImgConvertState *s) } } =20 - ret =3D 0; + ret =3D s->ret; fail: - qemu_vfree(buf); + for (i =3D 0; i < CONVERT_MAX_REQS; i++) { + qemu_iovec_destroy(&s->reqs[i].qiov); + qemu_vfree(s->reqs[i].buf); + } return ret; } =20 --=20 1.9.1