From patchwork Mon Apr 19 15:56:41 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 50472 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1CFC6B7D0B for ; Tue, 20 Apr 2010 02:06:45 +1000 (EST) Received: from localhost ([127.0.0.1]:38119 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O3tSl-0004vw-Si for incoming@patchwork.ozlabs.org; Mon, 19 Apr 2010 12:04:43 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O3tKj-0000mp-Ff for qemu-devel@nongnu.org; Mon, 19 Apr 2010 11:56:25 -0400 Received: from [140.186.70.92] (port=43258 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O3tKh-0000lI-28 for qemu-devel@nongnu.org; Mon, 19 Apr 2010 11:56:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O3tKa-0006uy-Bw for qemu-devel@nongnu.org; Mon, 19 Apr 2010 11:56:22 -0400 Received: from mtagate3.de.ibm.com ([195.212.17.163]:53427) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O3tKZ-0006tE-W5 for qemu-devel@nongnu.org; Mon, 19 Apr 2010 11:56:16 -0400 Received: from d12nrmr1607.megacenter.de.ibm.com (d12nrmr1607.megacenter.de.ibm.com [9.149.167.49]) by mtagate3.de.ibm.com (8.13.1/8.13.1) with ESMTP id o3JFu7Ig032764 for ; Mon, 19 Apr 2010 15:56:07 GMT Received: from d12av02.megacenter.de.ibm.com (d12av02.megacenter.de.ibm.com [9.149.165.228]) by d12nrmr1607.megacenter.de.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3JFu75g1630400 for ; Mon, 19 Apr 2010 17:56:07 +0200 Received: from d12av02.megacenter.de.ibm.com (loopback [127.0.0.1]) by d12av02.megacenter.de.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id o3JFu7nG031537 for ; Mon, 19 Apr 2010 17:56:07 +0200 Received: from localhost.localdomain (dyn-9-174-219-79.manchester-maybrook.uk.ibm.com [9.174.219.79]) by d12av02.megacenter.de.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id o3JFu6kL031527; Mon, 19 Apr 2010 17:56:06 +0200 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Date: Mon, 19 Apr 2010 16:56:41 +0100 Message-Id: <1271692601-6881-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Kevin Wolf , Jan Kiszka , Christoph Hellwig , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v2] block: Cache total_sectors to reduce bdrv_getlength calls X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The BlockDriver bdrv_getlength function is called from the I/O code path when checking that the request falls within the device. Unfortunately this involves an lseek system call in the raw protocol; every read or write request will incur this lseek cost. Jan Kiszka identified this issue and its latency overhead. This patch caches device length in the existing total_sectors variable so lseek calls can be avoided for fixed size devices. Growable devices fall back to the full bdrv_getlength code path because I have not added logic to detect extending the size of the device in a write. Signed-off-by: Stefan Hajnoczi --- v2: - Introduced refresh_total_sectors() to clean up total_sectors updates - Added error handling for refresh_total_sectors() callers block.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 files changed, 38 insertions(+), 5 deletions(-) diff --git a/block.c b/block.c index def3400..31e42c2 100644 --- a/block.c +++ b/block.c @@ -352,6 +352,26 @@ static BlockDriver *find_image_format(const char *filename) return drv; } +/** + * Set the current 'total_sectors' value + */ +static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) +{ + BlockDriver *drv = bs->drv; + + /* query actual device if possible, otherwise just trust the hint */ + if (drv->bdrv_getlength) { + int64_t length = drv->bdrv_getlength(bs); + if (length < 0) { + return length; + } + hint = length >> BDRV_SECTOR_BITS; + } + + bs->total_sectors = hint; + return 0; +} + /* * Common part for opening disk images and files */ @@ -363,6 +383,7 @@ static int bdrv_open_common(BlockDriverState *bs, const char *filename, assert(drv != NULL); bs->file = NULL; + bs->total_sectors = 0; bs->is_temporary = 0; bs->encrypted = 0; bs->valid_key = 0; @@ -416,9 +437,12 @@ static int bdrv_open_common(BlockDriverState *bs, const char *filename, } bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); - if (drv->bdrv_getlength) { - bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; + + ret = refresh_total_sectors(bs, bs->total_sectors); + if (ret < 0) { + goto free_and_fail; } + #ifndef _WIN32 if (bs->is_temporary) { unlink(filename); @@ -959,13 +983,18 @@ int bdrv_pwrite(BlockDriverState *bs, int64_t offset, int bdrv_truncate(BlockDriverState *bs, int64_t offset) { BlockDriver *drv = bs->drv; + int ret; if (!drv) return -ENOMEDIUM; if (!drv->bdrv_truncate) return -ENOTSUP; if (bs->read_only) return -EACCES; - return drv->bdrv_truncate(bs, offset); + ret = drv->bdrv_truncate(bs, offset); + if (ret == 0) { + ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); + } + return ret; } /** @@ -976,8 +1005,12 @@ int64_t bdrv_getlength(BlockDriverState *bs) BlockDriver *drv = bs->drv; if (!drv) return -ENOMEDIUM; - if (!drv->bdrv_getlength) { - /* legacy mode */ + + /* Fixed size devices use the total_sectors value for speed instead of + issuing a length query (like lseek) on each call. Also, legacy block + drivers don't provide a bdrv_getlength function and must use + total_sectors. */ + if (!bs->growable || !drv->bdrv_getlength) { return bs->total_sectors * BDRV_SECTOR_SIZE; } return drv->bdrv_getlength(bs);