From nobody Mon Apr 29 09:03:11 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 1492768720004769.5364213359934; Fri, 21 Apr 2017 02:58:40 -0700 (PDT) Received: from localhost ([::1]:58296 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1VKo-0000Eq-Nh for importer@patchew.org; Fri, 21 Apr 2017 05:58:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46409) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1VJy-0008Kg-Aa for qemu-devel@nongnu.org; Fri, 21 Apr 2017 05:57:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1VJu-0003Yd-Cy for qemu-devel@nongnu.org; Fri, 21 Apr 2017 05:57:46 -0400 Received: from mail-oi0-x243.google.com ([2607:f8b0:4003:c06::243]:35335) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1VJu-0003YZ-8p; Fri, 21 Apr 2017 05:57:42 -0400 Received: by mail-oi0-x243.google.com with SMTP id m34so9646535oik.2; Fri, 21 Apr 2017 02:57:42 -0700 (PDT) Received: from localhost.localdomain ([119.28.70.25]) by smtp.gmail.com with ESMTPSA id v128sm15166401pgb.20.2017.04.21.02.57.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Apr 2017 02:57:40 -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=UVp9AzSV8wPQe9slqk7aTk4QbOsxPFe18Hp8EznwkQQ=; b=GAM1flINQX1JARcD10kHlYdJf68Sfwn0hv4YsHQjqlX7d4+Nix3SmivVNQbZkPWWzc /qWjmn+DUwQpaeD8K+KvHmbVslwS3YKEpvljloN1LLwlhOaYLcyUJ6qL0/4qoz+VFaxh 2tjbBOJzk2rNgM2z5OVox25a9hp298cevp92e1+BTTCxJRKbYtTZx3JYCqrlDczRdElb jFEX15WtkYWSpb6xezPe1Ry5FgnChO+/+A3lUZdejorrH/1muI6sYi73Vn0Vr3+xHxRG TsT9J4rAOdWq+zreT5eOJagoABwj86OtnkCmo/t/Csvds6xEY018bEjn2RK4mtjLOYWy zwbw== 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=UVp9AzSV8wPQe9slqk7aTk4QbOsxPFe18Hp8EznwkQQ=; b=nqImGD+zvPEBzsodhCdjTtAKqJbY6sMIw0KdzqPx1u7uc8yCrO/asZlyY1w9B0QPL2 PL/g1PgEydBTTuiMJ3VnC8WNLErK3kTrYj8QJpQgewp9p/8+GsrwaoNyBlru5JkOnxh4 JcKgygqeCnllwiUFy/a8x0l87VUore8TNnZjaM154b2NNOokwBqQOyfdKVxUyln6TVmp gK+ninIXODrHf0jtMlOoQ4p3IjQr/IGe6N29KEqanJBry47oX9KAw6B7CvyGD8nMOXpb UbaXo3FDu/uprNFV9N71WHBk1+j17h0feYH0gc61YTzwyT14i4svqUlJS2IEC1kHILty 3O0g== X-Gm-Message-State: AN3rC/51uk4OCkCUDNF7C8KsQhgtCBbb0e5Hb6DUTzJ0IVmyvXRZAUJ6 ocPrnDOoEAn5RA== X-Received: by 10.84.211.137 with SMTP id c9mr15157393pli.8.1492768661364; Fri, 21 Apr 2017 02:57:41 -0700 (PDT) From: jemmy858585@gmail.com X-Google-Original-From: lidongchen@tencent.com To: qemu-devel@nongnu.org Date: Fri, 21 Apr 2017 17:57:34 +0800 Message-Id: <1492768654-11053-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:4003:c06::243 Subject: [Qemu-devel] [PATCH v2] 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 effectively than blk_co_pwritev with BDRV_REQ_WRITE_COMPRESSED. this patch can reduces the time when converts the qcow2 image with lots of zero. Signed-off-by: Lidong Chen --- v2 changelog: unify the compressed and non-compressed code paths --- qemu-img.c | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index b220cf7..60c9adf 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,21 @@ 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 + /* 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. */ 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 +1696,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