From patchwork Wed Jan 20 16:24:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 8072801 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id BB429BEEE5 for ; Wed, 20 Jan 2016 16:25:31 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1CDD120306 for ; Wed, 20 Jan 2016 16:25:31 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 51BC620268 for ; Wed, 20 Jan 2016 16:25:30 +0000 (UTC) Received: from localhost ([::1]:43862 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLvZZ-0003RW-Di for patchwork-qemu-devel@patchwork.kernel.org; Wed, 20 Jan 2016 11:25:29 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58734) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLvZO-0003La-5k for qemu-devel@nongnu.org; Wed, 20 Jan 2016 11:25:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLvZN-0006qz-5W for qemu-devel@nongnu.org; Wed, 20 Jan 2016 11:25:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:38159) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLvZK-0006pk-Ns; Wed, 20 Jan 2016 11:25:14 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 5F21D341AC6; Wed, 20 Jan 2016 16:25:14 +0000 (UTC) Received: from noname.redhat.com (ovpn-116-62.ams2.redhat.com [10.36.116.62]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0KGP8CQ032534; Wed, 20 Jan 2016 11:25:13 -0500 From: Kevin Wolf To: qemu-block@nongnu.org Date: Wed, 20 Jan 2016 17:24:52 +0100 Message-Id: <1453307106-28330-4-git-send-email-kwolf@redhat.com> In-Reply-To: <1453307106-28330-1-git-send-email-kwolf@redhat.com> References: <1453307106-28330-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [PULL 03/17] qemu-img: Speed up comparing empty/zero images X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Fam Zheng Two empty raw files are always compared by actually reading data even if there is no data, because BDRV_BLOCK_ZERO is considered "allocated" in bdrv_is_allocated_above(). That is inefficient. Use bdrv_get_block_status_above() for more information, and skip the consecutive zero sectors. This brings a huge speed up in comparing sparse/empty raw images: $ qemu-img create a 1G $ time ~/build/master/bin/qemu-img compare a a Images are identical. real 0m6.583s user 0m0.191s sys 0m6.367s $ time qemu-img compare a a Images are identical. real 0m0.033s user 0m0.003s sys 0m0.031s Signed-off-by: Fam Zheng Reviewed-by: Paolo Bonzini Signed-off-by: Kevin Wolf --- qemu-img.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index a5949e6..6773943 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1071,28 +1071,50 @@ static int img_compare(int argc, char **argv) } for (;;) { + int64_t status1, status2; nb_sectors = sectors_to_process(total_sectors, sector_num); if (nb_sectors <= 0) { break; } - allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors, - &pnum1); - if (allocated1 < 0) { + status1 = bdrv_get_block_status_above(bs1, NULL, sector_num, + total_sectors1 - sector_num, + &pnum1); + if (status1 < 0) { ret = 3; error_report("Sector allocation test failed for %s", filename1); goto out; } + allocated1 = status1 & BDRV_BLOCK_ALLOCATED; - allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors, - &pnum2); - if (allocated2 < 0) { + status2 = bdrv_get_block_status_above(bs2, NULL, sector_num, + total_sectors2 - sector_num, + &pnum2); + if (status2 < 0) { ret = 3; error_report("Sector allocation test failed for %s", filename2); goto out; } - nb_sectors = MIN(pnum1, pnum2); + allocated2 = status2 & BDRV_BLOCK_ALLOCATED; + if (pnum1) { + nb_sectors = MIN(nb_sectors, pnum1); + } + if (pnum2) { + nb_sectors = MIN(nb_sectors, pnum2); + } - if (allocated1 == allocated2) { + if (strict) { + if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) != + (status2 & ~BDRV_BLOCK_OFFSET_MASK)) { + ret = 1; + qprintf(quiet, "Strict mode: Offset %" PRId64 + " block status mismatch!\n", + sectors_to_bytes(sector_num)); + goto out; + } + } + if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { + nb_sectors = MIN(pnum1, pnum2); + } else if (allocated1 == allocated2) { if (allocated1) { ret = blk_read(blk1, sector_num, buf1, nb_sectors); if (ret < 0) { @@ -1120,13 +1142,6 @@ static int img_compare(int argc, char **argv) } } } else { - if (strict) { - ret = 1; - qprintf(quiet, "Strict mode: Offset %" PRId64 - " allocation mismatch!\n", - sectors_to_bytes(sector_num)); - goto out; - } if (allocated1) { ret = check_empty_sectors(blk1, sector_num, nb_sectors,