From nobody Mon Feb 9 04:45:26 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 1497620531474191.81986716499023; Fri, 16 Jun 2017 06:42:11 -0700 (PDT) Received: from localhost ([::1]:58956 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLrVq-0008Oq-7s for importer@patchew.org; Fri, 16 Jun 2017 09:42:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54081) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLrRd-00051d-CH 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-0000g8-EX for qemu-devel@nongnu.org; Fri, 16 Jun 2017 09:37:49 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:35110) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dLrRX-0000eJ-VI; 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 1dLrRU-0003tM-Td; Fri, 16 Jun 2017 15:37:41 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dLrRD-000197-7Y; 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=OI2bI+aXZl69OoVmfb6Zs4UNPl4aSorf/2hxDSnNc08=; b=EDUVOmpWihyVdo9O8wA5HYKagPRcs09MBRBTcjxC9cU4mMoSf1xm5Gtk7SH7lfDQfG9R6ZoXxwO+noGB5rxES6xK9kdtjSrJxZc4fHkRkL5SKcyg9uvO+SWIkq4nUIpj/gZphn5ZpwjBqBO0evV1YDTLOGMqt+Kfz+pkIRJ8jAR7v20Z0SAl+WaNvtNUNSWcXCTcfbk3R4ZclL1kYXtzGRny2Fn3wHaobj71xDOpTtZ7L1p2MPYZmeimOq2DOnV+AYdcBmLEq0bdm9RopFbl9cZwtSKMVHDHEmepvvVlvc4tMDtOogJM9hp0P5KnuM+icPZrrAax8FoYlDQxQyCEXw==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Fri, 16 Jun 2017 16:37:05 +0300 Message-Id: <0e6efbc82b41c83ea4845f37d7020a5a46fb834f.1497619885.git.berto@igalia.com> 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 5/7] qcow2: Allow reading both COW regions with only one request 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" Reading both COW regions requires two separate requests, but it's perfectly possible to merge them and perform only one. This generally improves performance, particularly on rotating disk drives. The downside is that the data in the middle region is read but discarded. This patch takes a conservative approach and only merges reads when the size of the middle region is <=3D 16KB. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 51 ++++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 3c9ace8a96..20fb531932 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -777,20 +777,38 @@ static int perform_cow(BlockDriverState *bs, QCowL2Me= ta *m) Qcow2COWRegion *start =3D &m->cow_start; Qcow2COWRegion *end =3D &m->cow_end; unsigned buffer_size; + unsigned data_bytes =3D end->offset - (start->offset + start->nb_bytes= ); + bool merge_reads; uint8_t *start_buffer, *end_buffer; int ret; =20 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); =20 if (start->nb_bytes =3D=3D 0 && end->nb_bytes =3D=3D 0) { return 0; } =20 - /* Reserve a buffer large enough to store the data from both the - * start and end COW regions. Add some padding in the middle if - * necessary to make sure that the end region is optimally aligned */ - buffer_size =3D QEMU_ALIGN_UP(start->nb_bytes, bdrv_opt_mem_align(bs))= + - end->nb_bytes; + /* If we have to read both the start and end COW regions and the + * middle region is not too large then perform just one read + * operation */ + merge_reads =3D start->nb_bytes && end->nb_bytes && data_bytes <=3D 16= 384; + if (merge_reads) { + buffer_size =3D start->nb_bytes + data_bytes + end->nb_bytes; + } else { + /* If we have to do two reads, add some padding in the middle + * if necessary to make sure that the end region is optimally + * aligned. */ + size_t align =3D bdrv_opt_mem_align(bs); + assert(align > 0 && align <=3D UINT_MAX); + assert(QEMU_ALIGN_UP(start->nb_bytes, align) <=3D + UINT_MAX - end->nb_bytes); + buffer_size =3D QEMU_ALIGN_UP(start->nb_bytes, align) + end->nb_by= tes; + } + + /* Reserve a buffer large enough to store all the data that we're + * going to read */ start_buffer =3D qemu_try_blockalign(bs, buffer_size); if (start_buffer =3D=3D NULL) { return -ENOMEM; @@ -799,15 +817,22 @@ static int perform_cow(BlockDriverState *bs, QCowL2Me= ta *m) end_buffer =3D start_buffer + buffer_size - end->nb_bytes; =20 qemu_co_mutex_unlock(&s->lock); - /* First we read the existing data from both COW regions */ - ret =3D do_perform_cow_read(bs, m->offset, start->offset, - start_buffer, start->nb_bytes); - if (ret < 0) { - goto fail; - } + /* First we read the existing data from both COW regions. We + * either read the whole region in one go, or the start and end + * regions separately. */ + if (merge_reads) { + ret =3D do_perform_cow_read(bs, m->offset, start->offset, + start_buffer, buffer_size); + } else { + ret =3D do_perform_cow_read(bs, m->offset, start->offset, + start_buffer, start->nb_bytes); + if (ret < 0) { + goto fail; + } =20 - ret =3D do_perform_cow_read(bs, m->offset, end->offset, - end_buffer, end->nb_bytes); + ret =3D do_perform_cow_read(bs, m->offset, end->offset, + end_buffer, end->nb_bytes); + } if (ret < 0) { goto fail; } --=20 2.11.0