improve block_status() for cbw + snapshot setup
v2 cover improve block_status() for cbw + snapshot setup
Recently we had some reports about stuck full backups: running backup job had 0 progress for more than an hour. This reproduced only on big images, at least 30TB of allocated space.
We are using snapshot filter + cbw filter + blockdev-backup. The discovered problem lies in access bitmap in cbw filter.
To build bcs bitmap for backup, the job repeatedly asks for block status with a range (X, virtual_size). As we are accessing the disk via snapshot filter, we end up in cbw_co_snapshot_block_status()-> cbw_snapshot_read_lock(). Here we check that access bitmap does not have any zeroes in this range. After that, we ask the disk. Firstly, we always look for zero in access bitmap for the whole range. This is rather slow, because it is full and we need to scan every bit in a lower level. Secondly, the following block status call to the disk may return not-very-high amount of continious clusters, for example 512 (empirical value, guessing for the 'full' image it is 4K/clu_addr_size for qcow2 driver).
This way we are doomed to re-scan access bitmap on the next block status request (X + 512, virtual_size).
perf tracing example:
96.67% 9581 qemu-kvm qemu-kvm [.] hbitmap_next_zero 0.33% 32 qemu-kvm qemu-kvm [.] qcow2_cache_do_get.lto_priv.0 0.10% 10 qemu-kvm [kernel.vmlinux] [k] perf_adjust_freq_unthr_context 0.08% 8 qemu-kvm qemu-kvm [.] qcow2_get_host_offset
This can be clearly observed on the image with small clu_size=65536 and preallocated metadata.
size 10T 11T blockdev-backup 52s 57s cbw + snap 325s 413s cbw + snap + patches 55s 61s
The growth is also non-linear in this case, +10% size results in +20% time.
This patchset changes access-bitmap into 'deny' bitmap by reversing the bits within and making the code look for set bits. It is much faster due to hbitmap levels.
Also
- update iotest 257: now access bitmap on the cbw filter is empty,
so count is 0 instead of 67108864 (which is the image size)
- remove meta betmap leftovers
- report block_status() until the end of accessible section to
snapshot filter, instead of returning EINVAL on big requests
v2:
- use 'inverse' instead of reverse, add an example
Andrey Zhadchenko (4):
hbitmap: drop meta bitmap leftovers
hbitmap: introduce hbitmap_inverse() block/copy-before-write: inverse access bitmap block/copy-before-write: report partial block status to snapshot
block/copy-before-write.c | 33 +++++++++++++++++++++------------ block/dirty-bitmap.c | 9 +++++++++ include/block/block_int-io.h | 1 + include/qemu/hbitmap.h | 8 ++++++++ tests/qemu-iotests/257.out | 28 ++++++++++++++-------------- util/hbitmap.c | 32 +++++++++++++++++--------------- 6 files changed, 70 insertions(+), 41 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/4 hbitmap: drop meta bitmap leftovers
API to manipulate meta bitmap was removed with commit 0c88f1970c76
--- util/hbitmap.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
util/hbitmap.c +2 -15
@@ -84,9 +84,6 @@ struct HBitmap {*/ int granularity; -/* A meta dirty bitmap to track the dirtiness of bits in this HBitmap. */ -HBitmap *meta; -/* A number of progressively less coarse bitmaps (i.e. level 0 is the * coarsest). Each bit in level N represents a word in level N+1 that * has a set bit, except the last level where each bit represents the
@@ -480,10 +477,7 @@ void hbitmap_set(HBitmap *hb, uint64_t start, uint64_t count) n = last - first +1 ; hb->count += n -hb_count_between (hb, first, last); -if (hb_set_between (hb,HBITMAP_LEVELS -1 , first, last) && - hb->meta) { -hbitmap_set (hb->meta, start, count); - } +hb_set_between (hb,HBITMAP_LEVELS -1 , first, last); }/* Resetting works the other way round: propagate up if the new
@@ -577,10 +571,7 @@ void hbitmap_reset(HBitmap *hb, uint64_t start, uint64_t count)assert (last < hb->size); hb->count -=hb_count_between (hb, first, last); -if (hb_reset_between (hb,HBITMAP_LEVELS -1 , first, last) && - hb->meta) { -hbitmap_set (hb->meta, start, count); - } +hb_reset_between (hb,HBITMAP_LEVELS -1 , first, last); }void hbitmap_reset_all (HBitmap *hb)
@@ -784,7 +775,6 @@ void hbitmap_deserialize_finish(HBitmap *bitmap)void hbitmap_free (HBitmap *hb) {unsigned i; -assert (!hb->meta);for (i =HBITMAP_LEVELS ; i-- >0 ; ) {g_free (hb->levels[i]); }
@@ -868,9 +858,6 @@ void hbitmap_truncate(HBitmap *hb, uint64_t size) (size - old) * sizeof(*hb->levels[i])); } } -if (hb->meta) { -hbitmap_truncate (hb->meta, hb->size << hb->granularity); - } }/**
Signature · 1 line
2.43.0
Re: [PATCH v2 1/4] hbitmap: drop meta bitmap leftovers
Quoted 4 lines · Andrey Zhadchenko
API to manipulate meta bitmap was removed with commit
0c88f1970c76Signed-off-by Andrey Zhadchenko<[email protected]>Reviewed-by Eric Blake<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH v2 2/4 hbitmap: introduce hbitmap_inverse()
In reply to Andrey Zhadchenkoand bdrv_dirty_bitmap_inverse() helper
--- block/dirty-bitmap.c | 9 +++++++++ include/block/block_int-io.h | 1 + include/qemu/hbitmap.h | 8 ++++++++ util/hbitmap.c | 15 +++++++++++++++ 4 files changed, 33 insertions(+)
block/dirty-bitmap.c +9 -0
@@ -888,3 +888,12 @@ void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest, } } } + +void bdrv_dirty_bitmap_inverse (BdrvDirtyBitmap *bitmap) +{ +assert (!bdrv_dirty_bitmap_readonly (bitmap)); +assert (!bdrv_dirty_bitmap_inconsistent (bitmap)); +bdrv_dirty_bitmaps_lock (bitmap->bs); +hbitmap_inverse (bitmap->bitmap); +bdrv_dirty_bitmaps_unlock (bitmap->bs); +}
include/block/block_int-io.h +1 -0
@@ -109,6 +109,7 @@ void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out);void bdrv_dirty_bitmap_merge_internal (BdrvDirtyBitmap *dest,const BdrvDirtyBitmap *src,HBitmap **backup,bool lock); +void bdrv_dirty_bitmap_inverse (BdrvDirtyBitmap *bitmap);void bdrv_inc_in_flight (BlockDriverState *bs);void bdrv_dec_in_flight (BlockDriverState *bs);
include/qemu/hbitmap.h +8 -0
@@ -350,4 +350,12 @@ bool hbitmap_status(const HBitmap *hb, int64_t start, int64_t count,*/ int64_t hbitmap_iter_next (HBitmapIter *hbi); +/** +* hbitmap_inverse: +* @bitmap: The HBitmap to operate on +* +* Inverse the bits in the bitmap. For example, 00011 will become 11100. +*/ +void hbitmap_inverse (HBitmap *bitmap); +#endif
util/hbitmap.c +15 -0
@@ -940,3 +940,18 @@ char *hbitmap_sha256(const HBitmap *bitmap, Error **errp)return hash; } + +void hbitmap_inverse (HBitmap *bitmap) +{ +int64_t pnum, pos =0 ; +int64_t size = bitmap->orig_size; + +while (pos < size) { +if (hbitmap_status (bitmap, pos, size - pos, &pnum)) { +hbitmap_reset (bitmap, pos, pnum); + }else { +hbitmap_set (bitmap, pos, pnum); + } + pos += pnum; + } +}
Signature · 1 line
2.43.0
Re: [PATCH v2 2/4] hbitmap: introduce hbitmap_inverse()
Quoted 3 lines · Andrey Zhadchenko
and
bdrv_dirty_bitmap_inverse()helperSigned-off-by Andrey Zhadchenko<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 2/4] hbitmap: introduce hbitmap_inverse()
In reply to Andrey ZhadchenkoQuoted 3 lines · Andrey Zhadchenko
and
bdrv_dirty_bitmap_inverse()helperSigned-off-by Andrey Zhadchenko<[email protected]>
Please, also add a test into tests/unit/test-hbitmap.c for a new function.
Signature · 2 lines
Best regards, Vladimir
PATCH v2 3/4 block/copy-before-write: inverse access bitmap
In reply to Andrey ZhadchenkoHBitmaps allow us to search set bits pretty fast. On the contrary, when searching zeroes, we may be forced to fully traverse the lower level.
When we run blockdev-backup with mode=full on top of snapshot filter
+ cbw filter, the job fills copy bitmap by calling block_status() with range (X, virtual_size). The problem is that we check for zeroes in this whole range. We also hit the worst case here, as access bitmap is fully set and we need to scan the entire lowest level. After scanning the full bitmap we actually ask the block status of original image, which may return significantly lower amount of empty clusters.
Beacuse of this, the backup job 'hangs' on block copy initializaiton for a long time with 100% CPU.
Example copy bitmap buildup time for image with clu_size=65536 and preallocated metadata
size 10T 11T blockdev-backup 52s 57s cbw + snap 325s 413s cbw + snap + patch 55s 61s
To fix it, inverse the access bitmap in cbw filter: rather set it when the user is not allowed to read the cluster.
Update qemu-iotest 257: now access bitmap have count 0 instead of the image size 67108864
--- block/copy-before-write.c | 17 ++++++++++------- tests/qemu-iotests/257.out | 28 ++++++++++++++-------------- 2 files changed, 24 insertions(+), 21 deletions(-)
block/copy-before-write.c +10 -7
@@ -53,7 +53,7 @@ typedef struct BDRVCopyBeforeWriteState {CoMutex lock;/* -* @access_bitmap: represents areas allowed for reading by fleecing user. +* @access_bitmap: represents areas disallowed for reading by fleecing user. * Reading from non-dirty areas leads to -EACCES. */ BdrvDirtyBitmap *access_bitmap;
@@ -220,7 +220,7 @@ cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes,return NULL ; } -if (bdrv_dirty_bitmap_next_zero (s->access_bitmap, offset, bytes) !=-1 ) { +if (bdrv_dirty_bitmap_next_dirty (s->access_bitmap, offset, bytes) !=-1 ) {g_free (req);return NULL ; }
@@ -338,8 +338,8 @@ cbw_co_pdiscard_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes) aligned_bytes = aligned_end - aligned_offset; WITH_QEMU_LOCK_GUARD(&s->lock) { -bdrv_reset_dirty_bitmap (s->access_bitmap, aligned_offset, - aligned_bytes); +bdrv_set_dirty_bitmap (s->access_bitmap, aligned_offset, + aligned_bytes); }block_copy_reset (s->bcs, aligned_offset, aligned_bytes);
@@ -501,9 +501,12 @@ static int cbw_open(BlockDriverState *bs, QDict *options, int flags,return -EINVAL ; }bdrv_disable_dirty_bitmap (s->access_bitmap); -bdrv_dirty_bitmap_merge_internal (s->access_bitmap, -block_copy_dirty_bitmap (s->bcs),NULL , - true); +if (bitmap) { +bdrv_dirty_bitmap_merge_internal (s->access_bitmap, +block_copy_dirty_bitmap (s->bcs),NULL , + true); +bdrv_dirty_bitmap_inverse (s->access_bitmap); + }qemu_co_mutex_init (&s->lock);QLIST_INIT (&s->frozen_read_reqs);
tests/qemu-iotests/257.out +14 -14
@@ -109,7 +109,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -585,7 +585,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -854,7 +854,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -1330,7 +1330,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -1599,7 +1599,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -2075,7 +2075,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -2344,7 +2344,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -2820,7 +2820,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -3089,7 +3089,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -3565,7 +3565,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -3834,7 +3834,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -4310,7 +4310,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -4579,7 +4579,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
@@ -5055,7 +5055,7 @@ write -P0x67 0x3fe0000 0x20000 "backup-top": [ { "busy": false, - "count": 67108864, + "count": 0, "granularity": 65536, "persistent": false, "recording": false
Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] block/copy-before-write: inverse access bitmap
Quoted 21 lines · Andrey Zhadchenko
HBitmaps allow us to search set bits pretty fast. On the contrary, when searching zeroes, we may be forced to fully traverse the lower level.
When we run blockdev-backup with mode=full on top of snapshot filter
+ cbw filter, the job fills copy bitmap by callingblock_status()with range (X,virtual_size). The problem is that we check for zeroes in this whole range. We also hit the worst case here, as access bitmap is fully set and we need to scan the entire lowest level. After scanning the full bitmap we actually ask the block status of original image, which may return significantly lower amount of empty clusters.
Beacuse of this, the backup job 'hangs' on block copy initializaiton for a long time with 100% CPU.Example copy bitmap buildup time for image with clu_size=65536 and preallocated metadata
size 10T 11T blockdev-backup 52s 57s cbw + snap 325s 413s cbw + snap + patch 55s 61s
Quoted 7 lines
To fix it, inverse the access bitmap in cbw filter: rather set it when the user is not allowed to read the cluster.
Update qemu-iotest 257: now access bitmap have count 0 instead of the image size 67108864
Signed-off-by Andrey Zhadchenko<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH v2 4/4 block/copy-before-write: report partial block status to snapshot
In reply to Andrey Zhadchenkountil the non-accessible area
--- block/copy-before-write.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
block/copy-before-write.c +12 -6
@@ -207,10 +207,11 @@ static int coroutine_fn GRAPH_RDLOCK cbw_co_flush(BlockDriverState *bs)*/ static BlockReq * coroutine_fn GRAPH_RDLOCKcbw_snapshot_read_lock (BlockDriverState *bs,int64_t offset,int64_t bytes, -int64_t *pnum,BdrvChild **file) +int64_t *pnum,BdrvChild **file,bool query) {BDRVCopyBeforeWriteState *s = bs->opaque;BlockReq *req =g_new (BlockReq,1 ); +int64_t next_dirty;bool done;QEMU_LOCK_GUARD (&s->lock);
@@ -220,9 +221,13 @@ cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes,return NULL ; } -if (bdrv_dirty_bitmap_next_dirty (s->access_bitmap, offset, bytes) !=-1 ) { -g_free (req); -return NULL ; + next_dirty =bdrv_dirty_bitmap_next_dirty (s->access_bitmap, offset, bytes); +if (next_dirty !=-1 ) { +if (!query || next_dirty == offset) { +g_free (req); +return NULL ; + } + bytes = offset + bytes - next_dirty; } done =bdrv_dirty_bitmap_status (s->done_bitmap, offset, bytes, pnum);
@@ -270,7 +275,8 @@ cbw_co_preadv_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes,while (bytes) {int64_t cur_bytes; - req =cbw_snapshot_read_lock (bs, offset, bytes, &cur_bytes, &file); + req =cbw_snapshot_read_lock (bs, offset, bytes, &cur_bytes, &file, + false);if (!req) {return -EACCES ; }
@@ -302,7 +308,7 @@ cbw_co_snapshot_block_status(BlockDriverState *bs,int64_t cur_bytes;BdrvChild *child; - req =cbw_snapshot_read_lock (bs, offset, bytes, &cur_bytes, &child); + req =cbw_snapshot_read_lock (bs, offset, bytes, &cur_bytes, &child, true);if (!req) {return -EACCES ; }
Signature · 1 line
2.43.0
Re: [PATCH v2 4/4] block/copy-before-write: report partial block status to snapshot
Quoted 1 line · Andrey Zhadchenko
until the non-accessible area
Please write a bit more: what, why and how. What is the scenario when this change helps? I think the client of snapshot-access, should not try to read/get-block-status from "denied" areas?
Also, it's a lot more comfortable to read messages, starting from a full sentence, not just continuing a sentence of subject. It's OK to repeat the subject, may be in more detailed form.
block/copy-before-write.c static int coroutine_fn GRAPH_RDLOCK cbw_co_flush(BlockDriverState *bs) · line 207
Quoted 12 lines
Signed-off-by Andrey Zhadchenko <[email protected]>--- block/copy-before-write.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)block/copy-before-write.c +0
-0@@ -207,10 +207,11 @@ static int coroutine_fn GRAPH_RDLOCK cbw_co_flush(BlockDriverState *bs)*/
the comment above the function should be updated to cover new logic
Quoted 15 lines
block/copy-before-write.c +2
-1static BlockReq * coroutine_fn GRAPH_RDLOCKcbw_snapshot_read_lock (BlockDriverState *bs,int64_t offset,int64_t bytes, -int64_t *pnum,BdrvChild **file) +int64_t *pnum,BdrvChild **file,bool query) {BDRVCopyBeforeWriteState *s = bs->opaque;BlockReq *req =g_new (BlockReq,1 ); +int64_t next_dirty;bool done;QEMU_LOCK_GUARD (&s->lock);@@ -220,9 +221,13 @@ cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes,return NULL ; }
block/copy-before-write.c cbw_snapshot_read_lock(BlockDriverState *bs, int64_t offset, int64_t bytes, · line 230
Quoted 10 lines
block/copy-before-write.c +7
-3-if (bdrv_dirty_bitmap_next_dirty (s->access_bitmap, offset, bytes) !=-1 ) { -g_free (req); -return NULL ; + next_dirty =bdrv_dirty_bitmap_next_dirty (s->access_bitmap, offset, bytes); +if (next_dirty !=-1 ) { +if (!query || next_dirty == offset) { +g_free (req); +return NULL ; + } + bytes = offset + bytes - next_dirty;
I don't follow. Shouldn't this be
bytes = next_dirty - offset
I.e., number of bytes from offset up to first "denied" byte.
Quoted 22 lines
block/copy-before-write.c +3
-2} done =bdrv_dirty_bitmap_status (s->done_bitmap, offset, bytes, pnum);@@ -270,7 +275,8 @@ cbw_co_preadv_snapshot(BlockDriverState *bs, int64_t offset, int64_t bytes,while (bytes) {int64_t cur_bytes; - req =cbw_snapshot_read_lock (bs, offset, bytes, &cur_bytes, &file); + req =cbw_snapshot_read_lock (bs, offset, bytes, &cur_bytes, &file, + false);if (!req) {return -EACCES ; }@@ -302,7 +308,7 @@ cbw_co_snapshot_block_status(BlockDriverState *bs,int64_t cur_bytes;BdrvChild *child; - req =cbw_snapshot_read_lock (bs, offset, bytes, &cur_bytes, &child); + req =cbw_snapshot_read_lock (bs, offset, bytes, &cur_bytes, &child, true);if (!req) {return -EACCES ; }
Signature · 2 lines
Best regards, Vladimir