[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 12/18] block: Cache total_sectors to reduce bdrv_get
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PATCH 12/18] block: Cache total_sectors to reduce bdrv_getlength calls |
Date: |
Fri, 30 Apr 2010 16:00:34 +0200 |
From: Stefan Hajnoczi <address@hidden>
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 <address@hidden> 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 <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block.c | 43 ++++++++++++++++++++++++++++++++++++++-----
1 files changed, 38 insertions(+), 5 deletions(-)
diff --git a/block.c b/block.c
index b318355..91fecab 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);
--
1.6.6.1
- [Qemu-devel] [PATCH 02/18] block: Split bdrv_open, (continued)
- [Qemu-devel] [PATCH 02/18] block: Split bdrv_open, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 03/18] block: Avoid forward declaration of bdrv_open_common, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 05/18] block: bdrv_has_zero_init, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 06/18] vmdk: Fix COW, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 08/18] vmdk: Convert to bdrv_open, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 07/18] vmdk: Clean up backing file handling, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 04/18] block: Open the underlying image file in generic code, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 09/18] block: Set backing_hd to NULL after deleting it, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 10/18] qcow2: Avoid shadowing variable in alloc_clusters_noref(), Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 11/18] raw-posix: Use pread/pwrite instead of lseek+read/write, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 12/18] block: Cache total_sectors to reduce bdrv_getlength calls,
Kevin Wolf <=
- [Qemu-devel] [PATCH 14/18] qcow2: Remove abort on free_clusters failure, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 17/18] qemu-img rebase: Fix output image corruption, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 15/18] qcow2: Implement bdrv_truncate() for growing images, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 18/18] block: Release allocated options after bdrv_open, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 13/18] qemu-img: Add 'resize' command to grow/shrink disk images, Kevin Wolf, 2010/04/30
- [Qemu-devel] [PATCH 16/18] block: Add wr_highest_sector blockstat, Kevin Wolf, 2010/04/30