From nobody Wed Dec 17 21:59:23 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1642169632; cv=none; d=zohomail.com; s=zohoarc; b=U6gp2tashUw1SNgPeH4ohoax/Hw7k3fkCNxuoB5jvh0SaiK+nVv0mqKjk/1HUQBI85P4aRHvh5PeBGXpprNW0WjomrixRHAjNq9v3Hugj7efDa04dzUEhKO5lFZGbjB9Mxzp1SRyAwGBriTpNxMUfgnFXggx5Ccd9YUNFelQHe0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1642169632; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=yBeDq1TQZkaS+a6CTH6lheHgQ07lW02DNpjs6pu+mhc=; b=MgQCE//Vj2cESvH6C4zUo9dbSh60U5qtli8guAPtzWbi2cpuUuijJ+i+gBGNbqkNvio30TNMzh1XiIhn8pwVj10WHf8ECg3f4cHB4hTWFn91iJN/eTvp97ZNN4HFD+XDm6M6ZO0Ed6YBZ4/M3FhLY6rcRsvQWAO/51vh7ie/a2k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1642169632357370.57741276939066; Fri, 14 Jan 2022 06:13:52 -0800 (PST) Received: from localhost ([::1]:56172 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n8NKx-0005dP-5Z for importer@patchew.org; Fri, 14 Jan 2022 09:13:51 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44174) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n8N0i-0003JX-8W for qemu-devel@nongnu.org; Fri, 14 Jan 2022 08:53:00 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:56765) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n8N0g-0000OJ-Fy for qemu-devel@nongnu.org; Fri, 14 Jan 2022 08:52:55 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-500-BuSpUOGqPvilPo0t3swPVQ-1; Fri, 14 Jan 2022 08:52:50 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 72A3D760C0; Fri, 14 Jan 2022 13:52:49 +0000 (UTC) Received: from merkur.redhat.com (unknown [10.39.194.212]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6B3C2105B20E; Fri, 14 Jan 2022 13:52:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642168374; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yBeDq1TQZkaS+a6CTH6lheHgQ07lW02DNpjs6pu+mhc=; b=cCbjwRKqYJ4i8/PUNguc9S/OTKfR3D2+d4TzVuoFa5HPNuaGMrv2xuneZLSPYRceZDcbJI ajBXGayNwUbWCKTfh++c5xvc9V4gO5eyI/GikP2gYs5/M85hJrWukCx7wGH5r8vJkblVtn 9pf1gaOaljjuXnAXibdeH0q3ly/Q1Ho= X-MC-Unique: BuSpUOGqPvilPo0t3swPVQ-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PULL 14/16] qemu-img: make is_allocated_sectors() more efficient Date: Fri, 14 Jan 2022 14:52:24 +0100 Message-Id: <20220114135226.185407-15-kwolf@redhat.com> In-Reply-To: <20220114135226.185407-1-kwolf@redhat.com> References: <20220114135226.185407-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kwolf@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -33 X-Spam_score: -3.4 X-Spam_bar: --- X-Spam_report: (-3.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.595, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1642169632836100001 Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Consider the case when the whole buffer is zero and end is unaligned. If i <=3D tail, we return 1 and do one unaligned WRITE, RMW happens. If i > tail, we do on aligned WRITE_ZERO (or skip if target is zeroed) and again one unaligned WRITE, RMW happens. Let's do better: don't fragment the whole-zero buffer and report it as ZERO: in case of zeroed target we just do nothing and avoid RMW. If target is not zeroes, one unaligned WRITE_ZERO should not be much worse than one unaligned WRITE. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20211217164654.1184218-3-vsementsov@virtuozzo.com> Tested-by: Peter Lieven Signed-off-by: Kevin Wolf --- qemu-img.c | 23 +++++++++++++++++++---- tests/qemu-iotests/122.out | 8 ++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 21ba1e6800..6fe2466032 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1171,19 +1171,34 @@ static int is_allocated_sectors(const uint8_t *buf,= int n, int *pnum, } } =20 + if (i =3D=3D n) { + /* + * The whole buf is the same. + * No reason to split it into chunks, so return now. + */ + *pnum =3D i; + return !is_zero; + } + tail =3D (sector_num + i) & (alignment - 1); if (tail) { if (is_zero && i <=3D tail) { - /* treat unallocated areas which only consist - * of a small tail as allocated. */ + /* + * For sure next sector after i is data, and it will rewrite t= his + * tail anyway due to RMW. So, let's just write data now. + */ is_zero =3D false; } if (!is_zero) { - /* align up end offset of allocated areas. */ + /* If possible, align up end offset of allocated areas. */ i +=3D alignment - tail; i =3D MIN(i, n); } else { - /* align down end offset of zero areas. */ + /* + * For sure next sector after i is data, and it will rewrite t= his + * tail anyway due to RMW. Better is avoid RMW and write zeroe= s up + * to aligned bound. + */ i -=3D tail; } } diff --git a/tests/qemu-iotests/122.out b/tests/qemu-iotests/122.out index 69b8e8b803..e18766e167 100644 --- a/tests/qemu-iotests/122.out +++ b/tests/qemu-iotests/122.out @@ -201,9 +201,7 @@ convert -S 4k { "start": 8192, "length": 4096, "depth": 0, "present": true, "zero": fals= e, "data": true, "offset": OFFSET}, { "start": 12288, "length": 4096, "depth": 0, "present": false, "zero": tr= ue, "data": false}, { "start": 16384, "length": 4096, "depth": 0, "present": true, "zero": fal= se, "data": true, "offset": OFFSET}, -{ "start": 20480, "length": 46080, "depth": 0, "present": false, "zero": t= rue, "data": false}, -{ "start": 66560, "length": 1024, "depth": 0, "present": true, "zero": fal= se, "data": true, "offset": OFFSET}, -{ "start": 67584, "length": 67041280, "depth": 0, "present": false, "zero"= : true, "data": false}] +{ "start": 20480, "length": 67088384, "depth": 0, "present": false, "zero"= : true, "data": false}] =20 convert -c -S 4k [{ "start": 0, "length": 1024, "depth": 0, "present": true, "zero": false,= "data": true}, @@ -215,9 +213,7 @@ convert -c -S 4k =20 convert -S 8k [{ "start": 0, "length": 24576, "depth": 0, "present": true, "zero": false= , "data": true, "offset": OFFSET}, -{ "start": 24576, "length": 41984, "depth": 0, "present": false, "zero": t= rue, "data": false}, -{ "start": 66560, "length": 1024, "depth": 0, "present": true, "zero": fal= se, "data": true, "offset": OFFSET}, -{ "start": 67584, "length": 67041280, "depth": 0, "present": false, "zero"= : true, "data": false}] +{ "start": 24576, "length": 67084288, "depth": 0, "present": false, "zero"= : true, "data": false}] =20 convert -c -S 8k [{ "start": 0, "length": 1024, "depth": 0, "present": true, "zero": false,= "data": true}, --=20 2.31.1