From nobody Thu May 2 05:22:42 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.zohomail.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 1528376229792745.6620699096586; Thu, 7 Jun 2018 05:57:09 -0700 (PDT) Received: from localhost ([::1]:57757 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQuTV-0001PC-3E for importer@patchew.org; Thu, 07 Jun 2018 08:57:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52519) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fQuPg-0007Gj-Dg for qemu-devel@nongnu.org; Thu, 07 Jun 2018 08:53:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fQuPd-0003hZ-BC for qemu-devel@nongnu.org; Thu, 07 Jun 2018 08:53:12 -0400 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:54224 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 1fQuPd-0003gH-1c for qemu-devel@nongnu.org; Thu, 07 Jun 2018 08:53:09 -0400 Received: (qmail 12967 invoked by uid 89); 7 Jun 2018 12:46:25 -0000 Received: from [195.62.97.192] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2010/03/19-MF (clamdscan: 0.100.0/24640. avast: 1.2.2/17010300. spamassassin: 3.4.1. Clear:RC:1(195.62.97.192):. Processed in 0.068345 secs); 07 Jun 2018 12:46:25 -0000 Received: from kerio.kamp.de ([195.62.97.192]) by mx01.kamp.de with ESMTPS (DHE-RSA-AES256-SHA encrypted); 7 Jun 2018 12:46:24 -0000 Received: from submission.kamp.de ([195.62.97.28]) by kerio.kamp.de with ESMTPS (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)) for qemu-devel@nongnu.org; Thu, 7 Jun 2018 14:46:20 +0200 Received: (qmail 16346 invoked from network); 7 Jun 2018 12:46:22 -0000 Received: from lieven-vm.kamp-intra.net (HELO lieven-vm-neu) (relay@kamp.de@::ffff:172.21.12.69) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 7 Jun 2018 12:46:22 -0000 Received: by lieven-vm-neu (Postfix, from userid 1060) id 7AA94202A5; Thu, 7 Jun 2018 14:46:22 +0200 (CEST) X-GL_Whitelist: yes X-Footer: a2FtcC5kZQ== From: Peter Lieven To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Thu, 7 Jun 2018 14:46:21 +0200 Message-Id: <1528375581-29538-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] [PATCH] qemu-img: align is_allocated_sectors to 4k 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 , 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" We currently don't enforce that the sparse segments we detect during conver= t are aligned. This leads to unnecessary and costly read-modify-write cycles eith= er internally in Qemu or in the background on the storage device as nearly all modern filesystems or hardware has a 4k alignment internally. As we per default set the min_sparse size to 4k it makes perfectly sense to= ensure that these sparse holes in the file are placed at 4k boundaries. The number of RMW cycles when converting an example image [1] to a raw devi= ce that has 4k sector size is about 4600 4k read requests to perform a total of abo= ut 15000 write requests. With this path the 4600 additional read requests are elimin= ated. [1] https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-ser= ver-cloudimg-amd64-disk1.vmdk Signed-off-by: Peter Lieven --- qemu-img.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 75f1610..68eefba 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1096,24 +1096,33 @@ static int64_t find_nonzero(const uint8_t *buf, int= 64_t n) * * 'pnum' is set to the number of sectors (including and immediately follo= wing * the first one) that are known to be in the same allocated/unallocated s= tate. + * The function will try to align 'pnum' to 8 sectors (4k) to avoid unneca= ssary + * RMW cycles on modern hardware. */ static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) { bool is_zero; - int i; + int i, alignment =3D 1; =20 if (n <=3D 0) { *pnum =3D 0; return 0; } - is_zero =3D buffer_is_zero(buf, 512); - for(i =3D 1; i < n; i++) { - buf +=3D 512; - if (is_zero !=3D buffer_is_zero(buf, 512)) { + + if (!(n & 7)) { + /* the buffer size is dividable by 4k */ + alignment =3D 8; + n /=3D 8; + } + + is_zero =3D buffer_is_zero(buf, BDRV_SECTOR_SIZE * alignment); + for (i =3D 1; i < n; i++) { + buf +=3D BDRV_SECTOR_SIZE * alignment; + if (is_zero !=3D buffer_is_zero(buf, BDRV_SECTOR_SIZE * alignment)= ) { break; } } - *pnum =3D i; + *pnum =3D i * alignment; return !is_zero; } =20 --=20 2.7.4