From patchwork Sat Feb 22 13:00:22 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Lieven X-Patchwork-Id: 323152 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 7D7092C01AE for ; Sun, 23 Feb 2014 00:01:30 +1100 (EST) Received: from localhost ([::1]:49501 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WHCCt-0005ig-Pe for incoming@patchwork.ozlabs.org; Sat, 22 Feb 2014 08:01:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:32800) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WHCCZ-0005hF-Ri for qemu-devel@nongnu.org; Sat, 22 Feb 2014 08:01:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WHCCU-0003ZD-82 for qemu-devel@nongnu.org; Sat, 22 Feb 2014 08:01:07 -0500 Received: from mx.ipv6.kamp.de ([2a02:248:0:51::16]:46300 helo=mx01.kamp.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WHCCT-0003Z3-UO for qemu-devel@nongnu.org; Sat, 22 Feb 2014 08:01:02 -0500 Received: (qmail 6406 invoked by uid 89); 22 Feb 2014 13:00:59 -0000 Received: from [82.141.1.145] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2010/03/19-MF (clamdscan: 0.98.1/18505. hbedv: 8.2.14.12/7.11.133.40. spamassassin: 3.3.1. Clear:RC:1(82.141.1.145):SA:0(-1.5/4.0):. Processed in 1.556577 secs); 22 Feb 2014 13:00:59 -0000 Received: from ns.kamp-intra.net (HELO dns.kamp-intra.net) ([82.141.1.145]) by mx01.kamp.de with SMTP; 22 Feb 2014 13:00:58 -0000 X-GL_Whitelist: yes Received: from lieven-pc.kamp-intra.net (lieven-pc.kamp-intra.net [172.21.12.60]) by dns.kamp-intra.net (Postfix) with ESMTP id 0CD6820695; Sat, 22 Feb 2014 14:00:28 +0100 (CET) Received: by lieven-pc.kamp-intra.net (Postfix, from userid 1000) id F35075FAA3; Sat, 22 Feb 2014 14:00:27 +0100 (CET) From: Peter Lieven To: qemu-devel@nongnu.org Date: Sat, 22 Feb 2014 14:00:22 +0100 Message-Id: <1393074022-32388-1-git-send-email-pl@kamp.de> X-Mailer: git-send-email 1.7.9.5 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a02:248:0:51::16 Cc: kwolf@redhat.com, pbonzini@redhat.com, Peter Lieven , stefanha@redhat.com Subject: [Qemu-devel] [RFC PATCH] block: optimize zero writes with bdrv_write_zeroes 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org this patch tries to optimize zero write requests by automatically using bdrv_write_zeroes if it is supported by the format. i know that there is a lot of potential for discussion, but i would like to know what the others think. this should significantly speed up file system initialization and should speed zero write test used to test backend storage performance. the difference can simply be tested by e.g. dd if=/dev/zero of=/dev/vdX bs=1M Signed-off-by: Peter Lieven --- block.c | 8 ++++++++ include/qemu-common.h | 1 + util/iov.c | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/block.c b/block.c index 6f4baca..505888e 100644 --- a/block.c +++ b/block.c @@ -3145,6 +3145,14 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs, ret = notifier_with_return_list_notify(&bs->before_write_notifiers, req); + if (!ret && !(flags & BDRV_REQ_ZERO_WRITE) && + drv->bdrv_co_write_zeroes && qemu_iovec_is_zero(qiov)) { + flags |= BDRV_REQ_ZERO_WRITE; + /* if the device was not opened with discard=on the below flag + * is immediately cleared again in bdrv_co_do_write_zeroes */ + flags |= BDRV_REQ_MAY_UNMAP; + } + if (ret < 0) { /* Do nothing, write notifier decided to fail this request */ } else if (flags & BDRV_REQ_ZERO_WRITE) { diff --git a/include/qemu-common.h b/include/qemu-common.h index b0e34b2..f0ad0f9 100644 --- a/include/qemu-common.h +++ b/include/qemu-common.h @@ -330,6 +330,7 @@ void qemu_iovec_concat(QEMUIOVector *dst, void qemu_iovec_concat_iov(QEMUIOVector *dst, struct iovec *src_iov, unsigned int src_cnt, size_t soffset, size_t sbytes); +bool qemu_iovec_is_zero(QEMUIOVector *qiov); void qemu_iovec_destroy(QEMUIOVector *qiov); void qemu_iovec_reset(QEMUIOVector *qiov); size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset, diff --git a/util/iov.c b/util/iov.c index bb46c04..abbb374 100644 --- a/util/iov.c +++ b/util/iov.c @@ -342,6 +342,26 @@ void qemu_iovec_concat(QEMUIOVector *dst, qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes); } +/* + * check if the contents of the iovecs is all zero + */ +bool qemu_iovec_is_zero(QEMUIOVector *qiov) { + int i; + for (i = 0; i < qiov->niov; i++) { + size_t offs = qiov->iov[i].iov_len & ~(4 * sizeof(long) - 1); + uint8_t *ptr = qiov->iov[i].iov_base; + if (offs && !buffer_is_zero(qiov->iov[i].iov_base, offs)) { + return false; + } + for (; offs < qiov->iov[i].iov_len; offs++) { + if (ptr[offs]) { + return false; + } + } + } + return true; +} + void qemu_iovec_destroy(QEMUIOVector *qiov) { assert(qiov->nalloc != -1);