From nobody Mon Apr 29 19:21:14 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.zoho.com; dkim=fail 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 1493261969580808.4665459066332; Wed, 26 Apr 2017 19:59:29 -0700 (PDT) Received: from localhost ([::1]:58403 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3ZeR-0007k2-3x for importer@patchew.org; Wed, 26 Apr 2017 22:59:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55103) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3Zdf-0007He-0k for qemu-devel@nongnu.org; Wed, 26 Apr 2017 22:58:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d3Zdb-0000hz-VQ for qemu-devel@nongnu.org; Wed, 26 Apr 2017 22:58:39 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:33174) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d3Zdb-0000ht-Pj; Wed, 26 Apr 2017 22:58:35 -0400 Received: by mail-pf0-x243.google.com with SMTP id c198so5083877pfc.0; Wed, 26 Apr 2017 19:58:35 -0700 (PDT) Received: from localhost.localdomain ([119.28.70.25]) by smtp.gmail.com with ESMTPSA id m29sm1066014pfj.32.2017.04.26.19.58.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Apr 2017 19:58:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=CnyS1FEiqrvv74/HDl0HoQQpzf/S8efmvK+byuabaf8=; b=V6QBiiBCg3GhAbZfE0UlniGYIj1SiTPdLEmDIqj3f4MTT1b+9p0ACvw7uXPhkpVjIE 2zzSow+DtwoGgVXcd3X3anqOPySR96vbb0Xy55E/c4YTvuMCq67ynebrnFDFroDFv2eh ayrMzjCZ94k+6TWMpPBN4rDuUcBp91Y/45pBTuKoRUJXX3SKLa94uZhU8tLrewBdeh1w 6tYcVuDmkZQOROtDzr0/tPecVTBpspsoBkF9o00kF4NUFn6a4Ly+0732lAU0o7MIZlP+ bzbF08r8KSkckiwziXz2tPgXTvS9TYwmw8tA6ujvguGTNZqw7fNvftpynT75MYM6J9sM bPUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=CnyS1FEiqrvv74/HDl0HoQQpzf/S8efmvK+byuabaf8=; b=aTUQOmLyegvcNlo+QnR2XQ2OvCbQrww33icJCAFaEzBtPauUGpcHPguLwSJ3BuWtG5 YNq5kMOzBMCvpfEEjW6ouZSIG8JkDh1YIB2BhAJU6Eg+IKAbOgg66AIfX+KaazlmHRrq i6BYUQe9DCJq6T2SoWg+hKP9qdzd/hJY9XZdQuDDXpUHz/BqK9BZYmJ0VXl6KJrjRtko H9Mm0jI+nJSieZ2xTHwnpU/j2D5zYRLHp/A9YlmAuDsIXZu4pBedV8h6SCYlOuO0n1XP yO7zZLykyTUJ+3qtqn4JmQ7dsoAUeQNBuy58Tis/KqACXLgLVsMo76qHXiqrorVMMFau TlQA== X-Gm-Message-State: AN3rC/5ZLIwhspEWFPO/e6DabfmHMQ3erbZd8yBqGUFGxnQVzavCb0Qv UHCD0SY8pEV2AA== X-Received: by 10.84.217.205 with SMTP id d13mr4101032plj.114.1493261914604; Wed, 26 Apr 2017 19:58:34 -0700 (PDT) From: jemmy858585@gmail.com X-Google-Original-From: lidongchen@tencent.com To: qemu-devel@nongnu.org Date: Thu, 27 Apr 2017 10:58:27 +0800 Message-Id: <1493261907-18734-1-git-send-email-lidongchen@tencent.com> X-Mailer: git-send-email 1.8.3.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH v3] 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, Lidong Chen , qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 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 --- v3 changelog: fix some spelling errors and code style. v2 changelog: unify the compressed and non-compressed code paths. --- qemu-img.c | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index bbe1574..872a91b7 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1661,6 +1661,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 @@ -1670,43 +1672,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; } @@ -1716,6 +1699,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