From nobody Fri Apr 11 16:02:38 2025 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; 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 1493413021536335.5042130515999; Fri, 28 Apr 2017 13:57:01 -0700 (PDT) Received: from localhost ([::1]:38830 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d4Cwm-0004DD-4s for importer@patchew.org; Fri, 28 Apr 2017 16:57:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56158) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d4CbV-0002Ho-Om for qemu-devel@nongnu.org; Fri, 28 Apr 2017 16:35:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d4CbU-0001cf-PP for qemu-devel@nongnu.org; Fri, 28 Apr 2017 16:35:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51780) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d4CbR-0001YU-17; Fri, 28 Apr 2017 16:34:57 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EC3CAA0900; Fri, 28 Apr 2017 20:34:55 +0000 (UTC) Received: from noname.redhat.com (ovpn-117-227.ams2.redhat.com [10.36.117.227]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9BE259533F; Fri, 28 Apr 2017 20:34:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com EC3CAA0900 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=kwolf@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com EC3CAA0900 From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 28 Apr 2017 22:33:39 +0200 Message-Id: <1493411622-5343-32-git-send-email-kwolf@redhat.com> In-Reply-To: <1493411622-5343-1-git-send-email-kwolf@redhat.com> References: <1493411622-5343-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 28 Apr 2017 20:34:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 31/34] qemu-img: use blk_co_pwrite_zeroes for zero sectors when compressed 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, qemu-devel@nongnu.org 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" From: Lidong Chen When the buffer is zero, blk_co_pwrite_zeroes is more effective than blk_co_pwritev with BDRV_REQ_WRITE_COMPRESSED. This patch can reduce the time for converting qcow2 images with lots of zero data. Signed-off-by: Lidong Chen Message-id: 1493261907-18734-1-git-send-email-lidongchen@tencent.com Signed-off-by: Max Reitz --- qemu-img.c | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 9b6e728..c719636 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1649,6 +1649,8 @@ static int coroutine_fn convert_co_write(ImgConvertSt= ate *s, int64_t sector_num, =20 while (nb_sectors > 0) { int n =3D nb_sectors; + BdrvRequestFlags flags =3D s->compressed ? BDRV_REQ_WRITE_COMPRESS= ED : 0; + switch (status) { case BLK_BACKING_FILE: /* If we have a backing file, leave clusters unallocated that = are @@ -1658,43 +1660,24 @@ static int coroutine_fn convert_co_write(ImgConvert= State *s, int64_t sector_num, break; =20 case BLK_DATA: - /* We must always write compressed clusters as a whole, so don= 't - * try to find zeroed parts in the buffer. We can only save the - * write if the buffer is completely zeroed and we're allowed = to - * keep the target sparse. */ - if (s->compressed) { - if (s->has_zero_init && s->min_sparse && - buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) - { - assert(!s->target_has_backing); - break; - } - - iov.iov_base =3D buf; - iov.iov_len =3D n << BDRV_SECTOR_BITS; - qemu_iovec_init_external(&qiov, &iov, 1); - - ret =3D blk_co_pwritev(s->target, sector_num << BDRV_SECTO= R_BITS, - n << BDRV_SECTOR_BITS, &qiov, - BDRV_REQ_WRITE_COMPRESSED); - if (ret < 0) { - return ret; - } - break; - } - - /* If there is real non-zero data or we're told to keep the ta= rget - * fully allocated (-S 0), we must write it. Otherwise we can = treat - * it as zero sectors. */ + /* If we're told to keep the target fully allocated (-S 0) or = there + * is real non-zero data, we must write it. Otherwise we can t= reat + * it as zero sectors. + * Compressed clusters need to be written as a whole, so in th= at + * case we can only save the write if the buffer is completely + * zeroed. */ if (!s->min_sparse || - is_allocated_sectors_min(buf, n, &n, s->min_sparse)) + (!s->compressed && + is_allocated_sectors_min(buf, n, &n, s->min_sparse)) || + (s->compressed && + !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) { iov.iov_base =3D buf; iov.iov_len =3D n << BDRV_SECTOR_BITS; qemu_iovec_init_external(&qiov, &iov, 1); =20 ret =3D blk_co_pwritev(s->target, sector_num << BDRV_SECTO= R_BITS, - n << BDRV_SECTOR_BITS, &qiov, 0); + n << BDRV_SECTOR_BITS, &qiov, flags); if (ret < 0) { return ret; } @@ -1704,6 +1687,7 @@ static int coroutine_fn convert_co_write(ImgConvertSt= ate *s, int64_t sector_num, =20 case BLK_ZERO: if (s->has_zero_init) { + assert(!s->target_has_backing); break; } ret =3D blk_co_pwrite_zeroes(s->target, --=20 1.8.3.1