From patchwork Thu Jan 19 16:35:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Lieven X-Patchwork-Id: 717218 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3v48gH3fNwz9sDF for ; Fri, 20 Jan 2017 03:39:07 +1100 (AEDT) Received: from localhost ([::1]:49526 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cUFjs-0000Hb-Vk for incoming@patchwork.ozlabs.org; Thu, 19 Jan 2017 11:39:05 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59120) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cUFgX-0005O5-NG for qemu-devel@nongnu.org; Thu, 19 Jan 2017 11:35:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cUFgW-00054o-Kp for qemu-devel@nongnu.org; Thu, 19 Jan 2017 11:35:37 -0500 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:46601 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 1cUFgW-000541-Bj for qemu-devel@nongnu.org; Thu, 19 Jan 2017 11:35:36 -0500 Received: (qmail 12585 invoked by uid 89); 19 Jan 2017 16:35:33 -0000 Received: from [195.62.97.28] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2010/03/19-MF (clamdscan: 0.99.2/22913. avast: 1.2.2/17010300. spamassassin: 3.4.1. Clear:RC:1(195.62.97.28):. Processed in 0.054936 secs); 19 Jan 2017 16:35:33 -0000 Received: from smtp.kamp.de (HELO submission.kamp.de) ([195.62.97.28]) by mx01.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted); 19 Jan 2017 16:35:32 -0000 X-GL_Whitelist: yes Received: (qmail 4444 invoked from network); 19 Jan 2017 16:35:32 -0000 Received: from lieven-pc.kamp-intra.net (HELO lieven-pc) (relay@kamp.de@::ffff:172.21.12.60) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 19 Jan 2017 16:35:32 -0000 Received: by lieven-pc (Postfix, from userid 1000) id 8715B20A89; Thu, 19 Jan 2017 17:35:32 +0100 (CET) From: Peter Lieven To: qemu-devel@nongnu.org Date: Thu, 19 Jan 2017 17:35:30 +0100 Message-Id: <1484843730-23848-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 V2] qemu-img: optimize is_allocated_sectors_min 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 , qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" the current implementation always splits requests if a buffer begins or ends with zeroes independent of the length of the zero area. Change this to really only split off zero areas that have at least a length of 'min' bytes. Signed-off-by: Peter Lieven --- qemu-img.c | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 5df66fe..8e7357d 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1010,45 +1010,29 @@ static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) } /* - * Like is_allocated_sectors, but if the buffer starts with a used sector, - * up to 'min' consecutive sectors containing zeros are ignored. This avoids - * breaking up write requests for only small sparse areas. + * Like is_allocated_sectors, but only at least 'min' consecutive sectors + * containing zeros are considered unallocated. This avoids breaking up write + * requests for only small sparse areas. */ static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, - int min) + int min) { - int ret; - int num_checked, num_used; - - if (n < min) { - min = n; - } - - ret = is_allocated_sectors(buf, n, pnum); - if (!ret) { - return ret; - } - - num_used = *pnum; - buf += BDRV_SECTOR_SIZE * *pnum; - n -= *pnum; - num_checked = num_used; + int num_used = 0; while (n > 0) { - ret = is_allocated_sectors(buf, n, pnum); - - buf += BDRV_SECTOR_SIZE * *pnum; - n -= *pnum; - num_checked += *pnum; - if (ret) { - num_used = num_checked; - } else if (*pnum >= min) { + if (!is_allocated_sectors(buf, n, pnum) && *pnum >= min) { break; } + num_used += *pnum; + buf += BDRV_SECTOR_SIZE * *pnum; + n -= *pnum; } - *pnum = num_used; - return 1; + if (num_used) { + *pnum = num_used; + } + + return !!num_used; } /*