From nobody Mon Feb 9 01:43:17 2026 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; dkim=fail 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 1497620665537438.05375568034776; Fri, 16 Jun 2017 06:44:25 -0700 (PDT) Received: from localhost ([::1]:58977 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLrY0-0001iA-7D for importer@patchew.org; Fri, 16 Jun 2017 09:44:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54086) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLrRd-00051h-DA for qemu-devel@nongnu.org; Fri, 16 Jun 2017 09:37:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dLrRY-0000ge-GN for qemu-devel@nongnu.org; Fri, 16 Jun 2017 09:37:49 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:35104) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dLrRX-0000eO-Ul; Fri, 16 Jun 2017 09:37:44 -0400 Received: from [194.100.51.2] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dLrRV-0003tO-1A; Fri, 16 Jun 2017 15:37:41 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dLrRD-00019D-9V; Fri, 16 Jun 2017 16:37:23 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=kCkDoWHAcBfihZ2ZkNonbGvdFm8G9F/Y0N1s7VSgMCE=; b=d4ygvqtdzmrAyG+G0uDwOhuF1GeX0MQsbs5cVsNkmjLkvidfNKaA17KrtyrWMu/ZoILsTbW0fcfbvTIzn1U2ME+dacp0bjttRmaexjA9tbMNJBrq92/K1RConrimd6Lx1jZwldx0tw3yIdpVw6txRhBe4kJGDpnfe000zx4t8fJMAnwaY2qZ0vTRpZInNiMNHsd/lTV8h8hltNVSLuGkuiyWUGHsYndfLiFM5/rhF182WXcYIT+ZYP6KYkHJfnDX7y3qkU4y0lmR2URXSkPY6EHJEwGcdDGA5vdurlsDlvGFZMVLhNCiCy0IauK3wbbdZ+FpEZAsH91797S0ptaL/w==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Fri, 16 Jun 2017 16:37:07 +0300 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH v3 7/7] qcow2: Merge the writing of the COW regions with the guest data 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: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If the guest tries to write data that results on the allocation of a new cluster, instead of writing the guest data first and then the data from the COW regions, write everything together using one single I/O operation. This can improve the write performance by 25% or more, depending on several factors such as the media type, the cluster size and the I/O request size. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 38 ++++++++++++++++++++++-------- block/qcow2.c | 64 +++++++++++++++++++++++++++++++++++++++++++----= ---- block/qcow2.h | 7 ++++++ 3 files changed, 90 insertions(+), 19 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index e1e45bd122..3e2dc77372 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -776,6 +776,7 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta= *m) assert(start->nb_bytes <=3D UINT_MAX - end->nb_bytes); assert(start->nb_bytes + end->nb_bytes <=3D UINT_MAX - data_bytes); assert(start->offset + start->nb_bytes <=3D end->offset); + assert(!m->data_qiov || m->data_qiov->size =3D=3D data_bytes); =20 if (start->nb_bytes =3D=3D 0 && end->nb_bytes =3D=3D 0) { return 0; @@ -842,17 +843,36 @@ static int perform_cow(BlockDriverState *bs, QCowL2Me= ta *m) } } =20 - /* And now we can write everything */ - qemu_iovec_reset(&qiov); - qemu_iovec_add(&qiov, start_buffer, start->nb_bytes); - ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov= ); - if (ret < 0) { - goto fail; + /* And now we can write everything. If we have the guest data we + * can write everything in one single operation */ + if (m->data_qiov) { + qemu_iovec_reset(&qiov); + if (start->nb_bytes) { + qemu_iovec_add(&qiov, start_buffer, start->nb_bytes); + } + qemu_iovec_concat(&qiov, m->data_qiov, 0, data_bytes); + if (end->nb_bytes) { + qemu_iovec_add(&qiov, end_buffer, end->nb_bytes); + } + /* NOTE: we have a write_aio blkdebug event here followed by + * a cow_write one in do_perform_cow_write(), but there's only + * one single I/O operation */ + BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); + ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, &= qiov); + } else { + /* If there's no guest data then write both COW regions separately= */ + qemu_iovec_reset(&qiov); + qemu_iovec_add(&qiov, start_buffer, start->nb_bytes); + ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, &= qiov); + if (ret < 0) { + goto fail; + } + + qemu_iovec_reset(&qiov); + qemu_iovec_add(&qiov, end_buffer, end->nb_bytes); + ret =3D do_perform_cow_write(bs, m->alloc_offset, end->offset, &qi= ov); } =20 - qemu_iovec_reset(&qiov); - qemu_iovec_add(&qiov, end_buffer, end->nb_bytes); - ret =3D do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov); fail: qemu_co_mutex_lock(&s->lock); =20 diff --git a/block/qcow2.c b/block/qcow2.c index b3ba5daa93..89be2083d4 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1575,6 +1575,44 @@ fail: return ret; } =20 +/* Check if it's possible to merge a write request with the writing of + * the data from the COW regions */ +static bool can_merge_cow(uint64_t offset, unsigned bytes, + QEMUIOVector *hd_qiov, QCowL2Meta *l2meta) +{ + QCowL2Meta *m; + + for (m =3D l2meta; m !=3D NULL; m =3D m->next) { + /* If both COW regions are empty then there's nothing to merge */ + if (m->cow_start.nb_bytes =3D=3D 0 && m->cow_end.nb_bytes =3D=3D 0= ) { + continue; + } + + /* The data (middle) region must be immediately after the + * start region */ + if (l2meta_cow_start(m) + m->cow_start.nb_bytes !=3D offset) { + continue; + } + + /* The end region must be immediately after the data (middle) + * region */ + if (m->offset + m->cow_end.offset !=3D offset + bytes) { + continue; + } + + /* Make sure that adding both COW regions to the QEMUIOVector + * does not exceed IOV_MAX */ + if (hd_qiov->niov > IOV_MAX - 2) { + continue; + } + + m->data_qiov =3D hd_qiov; + return true; + } + + return false; +} + static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t of= fset, uint64_t bytes, QEMUIOVector *qio= v, int flags) @@ -1657,16 +1695,22 @@ static coroutine_fn int qcow2_co_pwritev(BlockDrive= rState *bs, uint64_t offset, goto fail; } =20 - qemu_co_mutex_unlock(&s->lock); - BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); - trace_qcow2_writev_data(qemu_coroutine_self(), - cluster_offset + offset_in_cluster); - ret =3D bdrv_co_pwritev(bs->file, - cluster_offset + offset_in_cluster, - cur_bytes, &hd_qiov, 0); - qemu_co_mutex_lock(&s->lock); - if (ret < 0) { - goto fail; + /* If we need to do COW, check if it's possible to merge the + * writing of the guest data together with that of the COW regions. + * If it's not possible (or not necessary) then write the + * guest data now. */ + if (!can_merge_cow(offset, cur_bytes, &hd_qiov, l2meta)) { + qemu_co_mutex_unlock(&s->lock); + BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); + trace_qcow2_writev_data(qemu_coroutine_self(), + cluster_offset + offset_in_cluster); + ret =3D bdrv_co_pwritev(bs->file, + cluster_offset + offset_in_cluster, + cur_bytes, &hd_qiov, 0); + qemu_co_mutex_lock(&s->lock); + if (ret < 0) { + goto fail; + } } =20 while (l2meta !=3D NULL) { diff --git a/block/qcow2.h b/block/qcow2.h index c26ee0a33d..87b15eb4aa 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -343,6 +343,13 @@ typedef struct QCowL2Meta */ Qcow2COWRegion cow_end; =20 + /** + * The I/O vector with the data from the actual guest write request. + * If non-NULL, this is meant to be merged together with the data + * from @cow_start and @cow_end into one single write operation. + */ + QEMUIOVector *data_qiov; + /** Pointer to next L2Meta of the same write request */ struct QCowL2Meta *next; =20 --=20 2.11.0