Skip copy-on-write when allocating a zero cluster
v3 cover Skip copy-on-write when allocating a zero cluster
Here's the new version, there are two patches this time.
Berto
v3:
- Add a new patch to improve the reporting of BDRV_BLOCK_ZERO [Vladimir]
- Rename function to bdrv_co_is_zero_fast() [Vladimir, Kevin]
- Don't call bdrv_common_block_status_above() if bytes == 0
v2: https://lists.gnu.org/archive/html/qemu-block/2020-08/msg01165.html
- Add new, simpler API: bdrv_is_unallocated_or_zero_above()
v1: https://lists.gnu.org/archive/html/qemu-block/2020-08/msg00403.html
Alberto Garcia (2):
qcow2: Report BDRV_BLOCK_ZERO more accurately in
bdrv_co_block_status()
qcow2: Skip copy-on-write when allocating a zero cluster
include/block/block.h | 2 ++ block/io.c | 35 +++++++++++++++++++++++++++++++---- block/qcow2.c | 35 +++++++++++++++++++---------------- 3 files changed, 52 insertions(+), 20 deletions(-)
Signature · 1 line
2.20.1
PATCH v3 2/2 qcow2: Skip copy-on-write when allocating a zero cluster
Since commit c8bb23cbdbe32f5c326365e0a82e1b0e68cdcd8a when a write request results in a new allocation QEMU first tries to see if the rest of the cluster outside the written area contains only zeroes.
In that case, instead of doing a normal copy-on-write operation and writing explicit zero buffers to disk, the code zeroes the whole cluster efficiently using pwrite_zeroes() with BDRV_REQ_NO_FALLBACK.
This improves performance very significantly but it only happens when we are writing to an area that was completely unallocated before. Zero clusters (QCOW2_CLUSTER_ZERO_*) are treated like normal clusters and are therefore slower to allocate.
This happens because the code uses bdrv_is_allocated_above() rather bdrv_block_status_above(). The former is not as accurate for this purpose but it is faster. However in the case of qcow2 the underlying call does already report zero clusters just fine so there is no reason why we cannot use that information.
After testing 4KB writes on an image that only contains zero clusters this patch results in almost five times more IOPS.
--- include/block/block.h | 2 ++ block/io.c | 27 +++++++++++++++++++++++++++ block/qcow2.c | 35 +++++++++++++++++++---------------- 3 files changed, 48 insertions(+), 16 deletions(-)
include/block/block.h +2 -0
@@ -496,6 +496,8 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes,int bdrv_is_allocated_above (BlockDriverState *top,BlockDriverState *base,bool include_base,int64_t offset,int64_t bytes,int64_t *pnum); +int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, +int64_t bytes);bool bdrv_is_read_only (BlockDriverState *bs); int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
block/io.c +27 -0
@@ -2557,6 +2557,33 @@ int bdrv_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes, offset, bytes, pnum, map, file); } +/* +* Check @bs (and its backing chain) to see if the range defined +* by @offset and @bytes is known to read as zeroes. +* Return 1 if that is the case, 0 otherwise and -errno on error. +* This test is meant to be fast rather than accurate so returning 0 +* does not guarantee non-zero data. +*/ +int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, +int64_t bytes) +{ +int ret; +int64_t pnum = bytes; + +if (!bytes) { +return 1 ; + } + + ret =bdrv_common_block_status_above (bs,NULL , false, offset, + bytes, &pnum,NULL ,NULL ); + +if (ret <0 ) { +return ret; + } + +return (pnum == bytes) && (ret &BDRV_BLOCK_ZERO ); +} + int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,int64_t bytes,int64_t *pnum) {
block/qcow2.c +19 -16
@@ -2391,26 +2391,26 @@ static bool merge_cow(uint64_t offset, unsigned bytes,return false; } -static bool is_unallocated (BlockDriverState *bs,int64_t offset,int64_t bytes) -{ -int64_t nr; -return !bytes || - (!bdrv_is_allocated_above (bs,NULL , false, offset, bytes, &nr) && - nr == bytes); -} - -static bool is_zero_cow (BlockDriverState *bs,QCowL2Meta *m) +/* +* Return 1 if the COW regions read as zeroes, 0 if not, < 0 on error. +* Note that returning 0 does not guarantee non-zero data. +*/ +static int is_zero_cow (BlockDriverState *bs,QCowL2Meta *m) {/* * This check is designed for optimization shortcut so it must be * efficient. -* Instead of is_zero(), use is_unallocated() as it is faster (but not -* as accurate and can result in false negatives). +* Instead of is_zero(), use bdrv_co_is_zero_fast() as it is +* faster (but not as accurate and can result in false negatives). */ -return is_unallocated (bs, m->offset + m->cow_start.offset, - m->cow_start.nb_bytes) && -is_unallocated (bs, m->offset + m->cow_end.offset, - m->cow_end.nb_bytes); +int ret =bdrv_co_is_zero_fast (bs, m->offset + m->cow_start.offset, + m->cow_start.nb_bytes); +if (ret <=0 ) { +return ret; + } + +return bdrv_co_is_zero_fast (bs, m->offset + m->cow_end.offset, + m->cow_end.nb_bytes); }static int handle_alloc_space (BlockDriverState *bs,QCowL2Meta *l2meta)
@@ -2436,7 +2436,10 @@ static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta)continue ; } -if (!is_zero_cow (bs, m)) { + ret =is_zero_cow (bs, m); +if (ret <0 ) { +return ret; + }else if (ret ==0 ) {continue ; }
Signature · 1 line
2.20.1
PATCH v3 1/2 qcow2: Report BDRV_BLOCK_ZERO more accurately in bdrv_co_block_status()
In reply to Alberto GarciaIf a BlockDriverState supports backing files but has none then any unallocated area reads back as zeroes.
bdrv_co_block_status() is only reporting this is if want_zero is true, but this is an inexpensive test and there is no reason not to do it in all cases.
--- block/io.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
block/io.c +4 -4
@@ -2408,16 +2408,16 @@ static int coroutine_fn bdrv_co_block_status(BlockDriverState *bs,if (ret & (BDRV_BLOCK_DATA |BDRV_BLOCK_ZERO )) { ret |=BDRV_BLOCK_ALLOCATED ; - }else if (want_zero && bs->drv->supports_backing) { -if (bs->backing) { + }else if (bs->drv->supports_backing) { +if (!bs->backing) { + ret |=BDRV_BLOCK_ZERO ; + }else if (want_zero) {BlockDriverState *bs2 = bs->backing->bs;int64_t size2 =bdrv_getlength (bs2);if (size2 >=0 && offset >= size2) { ret |=BDRV_BLOCK_ZERO ; } - }else { - ret |=BDRV_BLOCK_ZERO ; } }
Signature · 1 line
2.20.1
Re: [PATCH v3 1/2] qcow2: Report BDRV_BLOCK_ZERO more accurately in bdrv_co_block_status()
Quoted 9 lines · Alberto Garcia
If a BlockDriverState supports backing files but has none then any unallocated area reads back as zeroes.
bdrv_co_block_status()is only reporting this is ifwant_zerois true, but this is an inexpensive test and there is no reason not to do it in all cases.Suggested-by Vladimir Sementsov-Ogievskiy <[email protected]>Signed-off-by Alberto Garcia <[email protected]>
Quoted 30 lines
--- block/io.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)block/io.c +4
-4@@ -2408,16 +2408,16 @@ static int coroutine_fn bdrv_co_block_status(BlockDriverState *bs,if (ret & (BDRV_BLOCK_DATA |BDRV_BLOCK_ZERO )) { ret |=BDRV_BLOCK_ALLOCATED ; - }else if (want_zero && bs->drv->supports_backing) { -if (bs->backing) { + }else if (bs->drv->supports_backing) { +if (!bs->backing) { + ret |=BDRV_BLOCK_ZERO ; + }else if (want_zero) {BlockDriverState *bs2 = bs->backing->bs;int64_t size2 =bdrv_getlength (bs2);if (size2 >=0 && offset >= size2) { ret |=BDRV_BLOCK_ZERO ; } - }else { - ret |=BDRV_BLOCK_ZERO ; } }
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v3 2/2] qcow2: Skip copy-on-write when allocating a zero cluster
In reply to Alberto GarciaQuoted 127 lines · Alberto Garcia
Since commit
c8bb23cbdbe32f5c326365e0a82e1b0e68cdcd8awhen a write request results in a new allocation QEMU first tries to see if the rest of the cluster outside the written area contains only zeroes.In that case, instead of doing a normal copy-on-write operation and writing explicit zero buffers to disk, the code zeroes the whole cluster efficiently using
pwrite_zeroes()withBDRV_REQ_NO_FALLBACK.This improves performance very significantly but it only happens when we are writing to an area that was completely unallocated before. Zero clusters (QCOW2_CLUSTER_ZERO_*) are treated like normal clusters and are therefore slower to allocate.
This happens because the code uses
bdrv_is_allocated_above()ratherbdrv_block_status_above(). The former is not as accurate for this purpose but it is faster. However in the case of qcow2 the underlying call does already report zero clusters just fine so there is no reason why we cannot use that information.After testing 4KB writes on an image that only contains zero clusters this patch results in almost five times more IOPS.
Signed-off-by Alberto Garcia <[email protected]>--- include/block/block.h | 2 ++ block/io.c | 27 +++++++++++++++++++++++++++ block/qcow2.c | 35 +++++++++++++++++++---------------- 3 files changed, 48 insertions(+), 16 deletions(-)include/block/block.h +2
-0@@ -496,6 +496,8 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes,int bdrv_is_allocated_above (BlockDriverState *top,BlockDriverState *base,bool include_base,int64_t offset,int64_t bytes,int64_t *pnum); +int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, +int64_t bytes);bool bdrv_is_read_only (BlockDriverState *bs); int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,block/io.c +27
-0@@ -2557,6 +2557,33 @@ int bdrv_block_status(BlockDriverState *bs, int64_t offset, int64_t bytes, offset, bytes, pnum, map, file); } +/* +* Check @bs (and its backing chain) to see if the range defined +* by @offset and @bytes is known to read as zeroes. +* Return 1 if that is the case, 0 otherwise and -errno on error. +* This test is meant to be fast rather than accurate so returning 0 +* does not guarantee non-zero data. +*/ +int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, +int64_t bytes) +{ +int ret; +int64_t pnum = bytes; + +if (!bytes) { +return 1 ; + } + + ret =bdrv_common_block_status_above (bs,NULL , false, offset, + bytes, &pnum,NULL ,NULL ); + +if (ret <0 ) { +return ret; + } + +return (pnum == bytes) && (ret &BDRV_BLOCK_ZERO ); +} + int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,int64_t bytes,int64_t *pnum) {block/qcow2.c +15
-15@@ -2391,26 +2391,26 @@ static bool merge_cow(uint64_t offset, unsigned bytes,return false; } -static bool is_unallocated (BlockDriverState *bs,int64_t offset,int64_t bytes) -{ -int64_t nr; -return !bytes || - (!bdrv_is_allocated_above (bs,NULL , false, offset, bytes, &nr) && - nr == bytes); -} - -static bool is_zero_cow (BlockDriverState *bs,QCowL2Meta *m) +/* +* Return 1 if the COW regions read as zeroes, 0 if not, < 0 on error. +* Note that returning 0 does not guarantee non-zero data. +*/ +static int is_zero_cow (BlockDriverState *bs,QCowL2Meta *m) {/* * This check is designed for optimization shortcut so it must be * efficient. -* Instead of is_zero(), use is_unallocated() as it is faster (but not -* as accurate and can result in false negatives). +* Instead of is_zero(), use bdrv_co_is_zero_fast() as it is +* faster (but not as accurate and can result in false negatives). */ -return is_unallocated (bs, m->offset + m->cow_start.offset, - m->cow_start.nb_bytes) && -is_unallocated (bs, m->offset + m->cow_end.offset, - m->cow_end.nb_bytes); +int ret =bdrv_co_is_zero_fast (bs, m->offset + m->cow_start.offset, + m->cow_start.nb_bytes); +if (ret <=0 ) { +return ret; + } + +return bdrv_co_is_zero_fast (bs, m->offset + m->cow_end.offset, + m->cow_end.nb_bytes); }static int handle_alloc_space (BlockDriverState *bs,QCowL2Meta *l2meta)
block/qcow2.c static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta) · line 2441
Quoted 7 lines
block/qcow2.c +3
-1@@ -2436,7 +2436,10 @@ static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta)continue ; } -if (!is_zero_cow (bs, m)) { + ret =is_zero_cow (bs, m); +if (ret <0 ) { +return ret;
It's a common practice to treat block-status errors as "unknown" status and not error-out immediately:
- really, it's not critical, we can continue assuming non-zero
- if there are real problems with IO, we'll most probably fail on real read or write operation, and report its status, which seems better for user than block-status error
So, I'd keep existing logic in handle_alloc_space(). And, if you agree and resend, probably good to split this patch into two, one for block.h/io.c and one for qcow2.c (still, I'm OK with it as one patch).
Quoted 5 lines
block/qcow2.c +1
-0+ }else if (ret ==0 ) {continue ; }
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v3 2/2] qcow2: Skip copy-on-write when allocating a zero cluster
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
block/qcow2.c static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta) · line 2441
Quoted 4 lines
block/qcow2.c +3
-1-if (!is_zero_cow (bs, m)) { + ret =is_zero_cow (bs, m); +if (ret <0 ) { +return ret;
Quoted 7 lines
It's a common practice to treat block-status errors as "unknown" status and not error-out immediately:
- really, it's not critical, we can continue assuming non-zero
- if there are real problems with IO, we'll most probably fail on real read or write operation, and report its status, which seems better for user than block-status error
But what's the problem exactly, does this complicate the code too much? :-?
Quoted 3 lines
So, I'd keep existing logic in
handle_alloc_space(). And, if you agree and resend, probably good to split this patch into two, one forblock.h/io.cand one forqcow2.c(still, I'm OK with it as one patch).
Sure, I can split the patch if I have to resend it.
Berto
Vladimir Sementsov-Ogievskiy
Reviewed-byRe: [PATCH v3 2/2] qcow2: Skip copy-on-write when allocating a zero cluster
Quoted 9 lines · Alberto Garcia
Quoted 8 lines · Vladimir Sementsov-Ogievskiy
block/qcow2.c static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta) · line 2441
Quoted 4 lines
block/qcow2.c +3
-1-if (!is_zero_cow (bs, m)) { + ret =is_zero_cow (bs, m); +if (ret <0 ) { +return ret;It's a common practice to treat block-status errors as "unknown" status and not error-out immediately:
Quoted 7 lines
Quoted 4 lines
- really, it's not critical, we can continue assuming non-zero
- if there are real problems with IO, we'll most probably fail on real read or write operation, and report its status, which seems better for user than block-status errorBut what's the problem exactly, does this complicate the code too much? :-?
Of course not :) Hmm. OK, I don't know, I'm just used to this practice in block jobs. Patch is correct as is:
Quoted 9 lines
Quoted 4 lines
So, I'd keep existing logic in
handle_alloc_space(). And, if you agree and resend, probably good to split this patch into two, one forblock.h/io.cand one forqcow2.c(still, I'm OK with it as one patch).Sure, I can split the patch if I have to resend it.
Berto
Signature · 2 lines
Best regards, Vladimir