block: seriously improve savevm performance
v4 cover block: seriously improve savevm performance
This series do standard basic things:
- it creates intermediate buffer for all writes from QEMU migration code to QCOW2 image,
- this buffer is sent to disk asynchronously, allowing several writes to run in parallel.
In general, migration code is fantastically inefficent (by observation), buffers are not aligned and sent with arbitrary pieces, a lot of time less than 100 bytes at a chunk, which results in read-modify-write operations with non-cached operations. It should also be noted that all operations are performed into unallocated image blocks, which also suffer due to partial writes to such new clusters.
This patch series is an implementation of idea discussed in the RFC posted by Denis Plotnikov
https://lists.gnu.org/archive/html/qemu-devel/2020-04/msg01925.html
Results with this series over NVME are better than original code
original rfc this cached: 1.79s 2.38s 1.27s non-cached: 3.29s 1.31s 0.81s
Changes from v3:
- rebased to master
- added patch 3 which removes aio_task_pool_wait_one()
- added R-By to patch 1
- patch 4 is rewritten via bdrv_run_co
- error path in blk_save_vmstate() is rewritten to call bdrv_flush_vmstate unconditionally
- added some comments
- fixes initialization in bdrv_co_vmstate_save_task_entry as suggested
Changes from v2:
- code moved from QCOW2 level to generic block level
- created bdrv_flush_vmstate helper to fix 022, 029 tests
- added recursive for bs->file in bdrv_co_flush_vmstate (fix 267)
- fixed blk_save_vmstate helper
- fixed coroutine wait as Vladimir suggested with waiting fixes from me
Changes from v1:
- patchew warning fixed
- fixed validation that only 1 waiter is allowed in patch 1
PATCH 1/5 migration/savevm: respect qemu_fclose() error code in save_snapshot()
qemu_fclose() could return error, f.e. if bdrv_co_flush() will return the error.
This validation will become more important once we will start waiting of asynchronous IO operations, started from bdrv_write_vmstate(), which are coming soon.
--- migration/savevm.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
migration/savevm.c +6 -2
@@ -2628,7 +2628,7 @@ int save_snapshot(const char *name, Error **errp) {BlockDriverState *bs, *bs1;QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; -int ret =-1 ; +int ret =-1 , ret2;QEMUFile *f;int saved_vm_running;uint64_t vm_state_size;
@@ -2712,10 +2712,14 @@ int save_snapshot(const char *name, Error **errp) } ret =qemu_savevm_state (f, errp); vm_state_size =qemu_ftell (f); -qemu_fclose (f); + ret2 =qemu_fclose (f);if (ret <0 ) { goto the_end; } +if (ret2 <0 ) { + ret = ret2; + goto the_end; + }/* The bdrv_all_create_snapshot() call that follows acquires the AioContext * for itself. BDRV_POLL_WHILE() does not support nested locking because
Signature · 1 line
2.17.1
PATCH 2/5 block/aio_task: allow start/wait task from any coroutine
In reply to Denis V. LunevFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
Currently, aio task pool assumes that there is a main coroutine, which creates tasks and wait for them. Let's remove the restriction by using CoQueue. Code becomes clearer, interface more obvious.
--- block/aio_task.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-)
block/aio_task.c +6 -15
@@ -27,11 +27,10 @@#include "block/aio_task.h" struct AioTaskPool { -Coroutine *main_co;int status;int max_busy_tasks;int busy_tasks; -bool waiting; +CoQueue waiters; }; static void coroutine_fn aio_task_co(void *opaque)
@@ -52,31 +51,23 @@ static void coroutine_fn aio_task_co(void *opaque)g_free (task); -if (pool->waiting) { - pool->waiting = false; -aio_co_wake (pool->main_co); - } +qemu_co_queue_restart_all (&pool->waiters); } void coroutine_fn aio_task_pool_wait_one(AioTaskPool *pool) {assert (pool->busy_tasks >0 ); -assert (qemu_coroutine_self () == pool->main_co); - pool->waiting = true; -qemu_coroutine_yield (); +qemu_co_queue_wait (&pool->waiters,NULL ); -assert (!pool->waiting);assert (pool->busy_tasks < pool->max_busy_tasks); } void coroutine_fn aio_task_pool_wait_slot(AioTaskPool *pool) { -if (pool->busy_tasks < pool->max_busy_tasks) { -return ; +while (pool->busy_tasks >= pool->max_busy_tasks) { +aio_task_pool_wait_one (pool); } - -aio_task_pool_wait_one (pool); } void coroutine_fn aio_task_pool_wait_all(AioTaskPool *pool)
@@ -98,8 +89,8 @@ AioTaskPool *coroutine_fn aio_task_pool_new(int max_busy_tasks) {AioTaskPool *pool =g_new0 (AioTaskPool,1 ); - pool->main_co =qemu_coroutine_self (); pool->max_busy_tasks = max_busy_tasks; +qemu_co_queue_init (&pool->waiters);return pool; }
Signature · 1 line
2.17.1
Re: [PATCH 2/5] block/aio_task: allow start/wait task from any coroutine
Quoted 8 lines · Denis V. Lunev
From: Vladimir Sementsov-Ogievskiy<[email protected]>
Currently, aio task pool assumes that there is a main coroutine, which creates tasks and wait for them. Let's remove the restriction by using CoQueue. Code becomes clearer, interface more obvious.
Signed-off-by Vladimir Sementsov-Ogievskiy<[email protected]>Signed-off-by Denis V. Lunev<[email protected]>
if it applicable:
Signature · 2 lines
Best regards, Vladimir
PATCH 3/5 block/aio_task: drop aio_task_pool_wait_one() helper
In reply to Denis V. LunevIt is not used outside the module.
Actually there are 2 kind of waiters:
- for a slot and
- for all tasks to finish
This patch limits external API to listed types.
--- block/aio_task.c | 13 ++----------- include/block/aio_task.h | 1 - 2 files changed, 2 insertions(+), 12 deletions(-)
block/aio_task.c +2 -11
@@ -54,26 +54,17 @@ static void coroutine_fn aio_task_co(void *opaque)qemu_co_queue_restart_all (&pool->waiters); } -void coroutine_fn aio_task_pool_wait_one(AioTaskPool *pool) -{ -assert (pool->busy_tasks >0 ); - -qemu_co_queue_wait (&pool->waiters,NULL ); - -assert (pool->busy_tasks < pool->max_busy_tasks); -} - void coroutine_fn aio_task_pool_wait_slot(AioTaskPool *pool) {while (pool->busy_tasks >= pool->max_busy_tasks) { -aio_task_pool_wait_one (pool); +qemu_co_queue_wait (&pool->waiters,NULL ); } } void coroutine_fn aio_task_pool_wait_all(AioTaskPool *pool) {while (pool->busy_tasks >0 ) { -aio_task_pool_wait_one (pool); +qemu_co_queue_wait (&pool->waiters,NULL ); } }
include/block/aio_task.h +0 -1
@@ -48,7 +48,6 @@ bool aio_task_pool_empty(AioTaskPool *pool); void coroutine_fn aio_task_pool_start_task(AioTaskPool *pool, AioTask *task); void coroutine_fn aio_task_pool_wait_slot(AioTaskPool *pool); -void coroutine_fn aio_task_pool_wait_one(AioTaskPool *pool); void coroutine_fn aio_task_pool_wait_all(AioTaskPool *pool);#endif /* BLOCK_AIO_TASK_H */
Signature · 1 line
2.17.1
Re: [PATCH 3/5] block/aio_task: drop aio_task_pool_wait_one() helper
Quoted 9 lines · Denis V. Lunev
It is not used outside the module.
Actually there are 2 kind of waiters:
- for a slot and
- for all tasks to finish
This patch limits external API to listed types.Signed-off-by Denis V. Lunev<[email protected]>Suggested-by Vladimir Sementsov-Ogievskiy<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH 4/5 block, migration: add bdrv_flush_vmstate helper
In reply to Denis V. LunevRight now bdrv_fclose() is just calling bdrv_flush().
The problem is that migration code is working inefficently from black layer terms and are frequently called for very small pieces of not properly aligned data. Block layer is capable to work this way, but this is very slow.
This patch is a preparation for the introduction of the intermediate buffer at block driver state. It would be beneficial to separate conventional bdrv_flush() from closing QEMU file from migration code.
The patch also forces bdrv_flush_vmstate() operation inside synchronous blk_save_vmstate() operation. This helper is used from qemu-io only.
--- block/block-backend.c | 6 +++++- block/io.c | 15 +++++++++++++++ include/block/block.h | 3 +++ migration/savevm.c | 3 +++ 4 files changed, 26 insertions(+), 1 deletion(-)
block/block-backend.c +5 -1
@@ -2177,16 +2177,20 @@ int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,int blk_save_vmstate (BlockBackend *blk,const uint8_t *buf,int64_t pos,int size) { -int ret; +int ret, ret2;if (!blk_is_available (blk)) {return -ENOMEDIUM ; } ret =bdrv_save_vmstate (blk_bs (blk), buf, pos, size); + ret2 =bdrv_flush_vmstate (blk_bs (blk));if (ret <0 ) {return ret; } +if (ret2 <0 ) { +return ret2; + }if (ret == size && !blk->enable_write_cache) { ret =bdrv_flush (blk_bs (blk));
block/io.c +15 -0
@@ -2724,6 +2724,21 @@ int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)return bdrv_rw_vmstate (bs, qiov, pos, true); } +static int coroutine_fn bdrv_co_flush_vmstate(BlockDriverState *bs) +{ +return 0 ; +} + +static int coroutine_fn bdrv_flush_vmstate_co_entry(void *opaque) +{ +return bdrv_co_flush_vmstate (opaque); +} + +int bdrv_flush_vmstate (BlockDriverState *bs) +{ +return bdrv_run_co (bs, bdrv_flush_vmstate_co_entry, bs); +} +/**************************************************************/ /* async I/Os */
include/block/block.h +3 -0
@@ -572,6 +572,9 @@ int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,int bdrv_load_vmstate (BlockDriverState *bs,uint8_t *buf,int64_t pos,int size); +/* bdrv_flush_vmstate() is mandatory to commit vmstate changes if +bdrv_save_vmstate() was ever called */ +int bdrv_flush_vmstate (BlockDriverState *bs); void bdrv_img_create(const char *filename, const char *fmt, const char *base_filename, const char *base_fmt,
migration/savevm.c +3 -0
@@ -150,6 +150,9 @@ static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,static int bdrv_fclose (void *opaque,Error **errp) { +int err =bdrv_flush_vmstate (opaque); +if (err <0 ) +return err;return bdrv_flush (opaque); }
Signature · 1 line
2.17.1
Re: [PATCH 4/5] block, migration: add bdrv_flush_vmstate helper
Quoted 3 lines · Denis V. Lunev
Right now
bdrv_fclose()is just callingbdrv_flush().The problem is that migration code is working inefficently from black
inefficiently, block
Quoted 2 lines
layer terms and are frequently called for very small pieces of not properly aligned data. Block layer is capable to work this way, but
s/not properly aligned/unaligned/
Quoted 15 lines
this is very slow.
This patch is a preparation for the introduction of the intermediate buffer at block driver state. It would be beneficial to separate conventional
bdrv_flush()from closing QEMU file from migration code.The patch also forces
bdrv_flush_vmstate()operation inside synchronousblk_save_vmstate()operation. This helper is used from qemu-io only.+++
b/block/block-backend.cblock/block-backend.c +0
-0@@ -2177,16 +2177,20 @@ int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,int blk_save_vmstate (BlockBackend *blk,const uint8_t *buf,int64_t pos,int size)
block/block-backend.c int blk_truncate(BlockBackend *blk, int64_t offset, bool exact, · line 2187
Quoted 10 lines
block/block-backend.c +2
-1{ -int ret; +int ret, ret2;if (!blk_is_available (blk)) {return -ENOMEDIUM ; } ret =bdrv_save_vmstate (blk_bs (blk), buf, pos, size); + ret2 =bdrv_flush_vmstate (blk_bs (blk));
Do you really want to be attempting bdrv_flush_vmstate() even after bdrv_save_vmstate() failed? Better might be...
block/block-backend.c int blk_truncate(BlockBackend *blk, int64_t offset, bool exact, · line 2190
Quoted 3 lines
block/block-backend.c +0
-0if (ret <0 ) {return ret; }
...attempting it here, at which point it looks like the only reason you need ret2 is to preserve ret long enough...
block/block-backend.c int blk_truncate(BlockBackend *blk, int64_t offset, bool exact, · line 2195
Quoted 5 lines
block/block-backend.c +3
-0+if (ret2 <0 ) { +return ret2; + }if (ret == size && !blk->enable_write_cache) {
...for this check. But a quick look at bdrv_save_vmstate() says this check is dead: the function can only return negative error or exactly size, and we already filtered out negative error above.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [PATCH 4/5] block, migration: add bdrv_flush_vmstate helper
Quoted 40 lines · Eric Blake
Quoted 3 lines · Denis V. Lunev
Right now
bdrv_fclose()is just callingbdrv_flush().The problem is that migration code is working inefficently from black
inefficiently, block
Quoted 2 lines
layer terms and are frequently called for very small pieces of not properly aligned data. Block layer is capable to work this way, but
s/not properly aligned/unaligned/
Quoted 14 lines
this is very slow.
This patch is a preparation for the introduction of the intermediate buffer at block driver state. It would be beneficial to separate conventional
bdrv_flush()from closing QEMU file from migration code.The patch also forces
bdrv_flush_vmstate()operation inside synchronousblk_save_vmstate()operation. This helper is used from qemu-io only.+++
b/block/block-backend.cblock/block-backend.c +0
-0@@ -2177,16 +2177,20 @@ int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,block/block-backend.c int blk_truncate(BlockBackend *blk, int64_t · line 2186
Quoted 10 lines
block/block-backend.c +2
-1int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, int64_t pos, int size) { - int ret; + int ret, ret2; if (!blk_is_available(blk)) { return -ENOMEDIUM; } ret = bdrv_save_vmstate(blk_bs(blk), buf, pos, size); + ret2 = bdrv_flush_vmstate(blk_bs(blk));Do you really want to be attempting
bdrv_flush_vmstate()even afterbdrv_save_vmstate()failed? Better might be...
Quoted 6 lines
block/block-backend.c int blk_truncate(BlockBackend *blk, int64_t offset, bool exact, · line 2190
Quoted 3 lines
block/block-backend.c +0
-0if (ret < 0) { return ret; }...attempting it here, at which point it looks like the only reason you need ret2 is to preserve ret long enough...
no, I would like to be sure that intermediate state is always cleared at the end.
In the next patch I am going to put intermediate buffer to BlockDriverState and thus it has to be removed in any case.
May be flush would be a bad name... clean or finalize?
Quoted 11 lines
block/block-backend.c int blk_truncate(BlockBackend *blk, int64_t offset, bool exact, · line 2195
Quoted 5 lines
block/block-backend.c +3
-0+ if (ret2 < 0) { + return ret2; + } if (ret == size && !blk->enable_write_cache) {...for this check. But a quick look at
bdrv_save_vmstate()says this check is dead: the function can only return negative error or exactly size, and we already filtered out negative error above.
hmm. you are right. good point. this check is dead. Worth to remove :) I'll add this as a separate patch.
Den
Re: [PATCH 4/5] block, migration: add bdrv_flush_vmstate helper
Quoted 12 lines · Denis V. Lunev
Quoted 5 lines
Quoted 3 lines
if (ret < 0) {
return ret;
}...attempting it here, at which point it looks like the only reason you need ret2 is to preserve ret long enough...
no, I would like to be sure that intermediate state is always cleared at the end.
In the next patch I am going to put intermediate buffer to BlockDriverState and thus it has to be removed in any case.May be flush would be a bad name... clean or finalize?
finalize sounds good for me
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 4/5] block, migration: add bdrv_flush_vmstate helper
In reply to Denis V. LunevQuoted 102 lines · Denis V. Lunev
Right now
bdrv_fclose()is just callingbdrv_flush().The problem is that migration code is working inefficently from black layer terms and are frequently called for very small pieces of not properly aligned data. Block layer is capable to work this way, but this is very slow.
This patch is a preparation for the introduction of the intermediate buffer at block driver state. It would be beneficial to separate conventional
bdrv_flush()from closing QEMU file from migration code.The patch also forces
bdrv_flush_vmstate()operation inside synchronousblk_save_vmstate()operation. This helper is used from qemu-io only.Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Max Reitz <[email protected]>CC Stefan Hajnoczi <[email protected]>CC Fam Zheng <[email protected]>CC Juan Quintela <[email protected]>CC "Dr. David Alan Gilbert" <[email protected]>CC Vladimir Sementsov-Ogievskiy <[email protected]>CC Denis Plotnikov <[email protected]>--- block/block-backend.c | 6 +++++- block/io.c | 15 +++++++++++++++ include/block/block.h | 3 +++ migration/savevm.c | 3 +++ 4 files changed, 26 insertions(+), 1 deletion(-)block/block-backend.c +5
-1@@ -2177,16 +2177,20 @@ int blk_truncate(BlockBackend *blk, int64_t offset, bool exact,int blk_save_vmstate (BlockBackend *blk,const uint8_t *buf,int64_t pos,int size) { -int ret; +int ret, ret2;if (!blk_is_available (blk)) {return -ENOMEDIUM ; } ret =bdrv_save_vmstate (blk_bs (blk), buf, pos, size); + ret2 =bdrv_flush_vmstate (blk_bs (blk));if (ret <0 ) {return ret; } +if (ret2 <0 ) { +return ret2; + }if (ret == size && !blk->enable_write_cache) { ret =bdrv_flush (blk_bs (blk));block/io.c +15
-0@@ -2724,6 +2724,21 @@ int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)return bdrv_rw_vmstate (bs, qiov, pos, true); } +static int coroutine_fn bdrv_co_flush_vmstate(BlockDriverState *bs) +{ +return 0 ; +} + +static int coroutine_fn bdrv_flush_vmstate_co_entry(void *opaque) +{ +return bdrv_co_flush_vmstate (opaque); +} + +int bdrv_flush_vmstate (BlockDriverState *bs) +{ +return bdrv_run_co (bs, bdrv_flush_vmstate_co_entry, bs); +} +/**************************************************************/ /* async I/Os */ include/block/block.h +3
-0@@ -572,6 +572,9 @@ int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,int bdrv_load_vmstate (BlockDriverState *bs,uint8_t *buf,int64_t pos,int size); +/* bdrv_flush_vmstate() is mandatory to commit vmstate changes if +bdrv_save_vmstate() was ever called */ +int bdrv_flush_vmstate (BlockDriverState *bs); void bdrv_img_create(const char *filename, const char *fmt, const char *base_filename, const char *base_fmt,migration/savevm.c +0
-0
migration/savevm.c static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, · line 155
Quoted 6 lines
migration/savevm.c +3
-0@@ -150,6 +150,9 @@ static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,static int bdrv_fclose (void *opaque,Error **errp) { +int err =bdrv_flush_vmstate (opaque); +if (err <0 ) +return err;
Qemu style wants braces anyway..
migration/savevm.c static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, · line 157
Quoted 2 lines
migration/savevm.c +0
-0return bdrv_flush (opaque); }
I'm not sure, that we want this on read path, neither, why we already do normal flush on read path. Anyway, it should be no-op on read path and won't hurt.
Signature · 2 lines
Best regards, Vladimir
PATCH 5/5 block/io: improve savevm performance
In reply to Denis V. LunevThis patch does 2 standard basic things:
- it creates intermediate buffer for all writes from QEMU migration code to block driver,
- this buffer is sent to disk asynchronously, allowing several writes to run in parallel.
Thus bdrv_vmstate_write() is becoming asynchronous. All pending operations completion are performed in newly invented bdrv_flush_vmstate().
In general, migration code is fantastically inefficent (by observation), buffers are not aligned and sent with arbitrary pieces, a lot of time less than 100 bytes at a chunk, which results in read-modify-write operations if target file descriptor is opened with O_DIRECT. It should also be noted that all operations are performed into unallocated image blocks, which also suffer due to partial writes to such new clusters even on cached file descriptors.
Snapshot creation time (2 GB Fedora-31 VM running over NVME storage):
original fixed cached: 1.79s 1.27s non-cached: 3.29s 0.81s
The difference over HDD would be more significant :)
--- block/io.c | 123 +++++++++++++++++++++++++++++++++++++- include/block/block_int.h | 8 +++ 2 files changed, 129 insertions(+), 2 deletions(-)
block/io.c +121 -2
@@ -26,6 +26,7 @@#include "trace.h" #include "sysemu/block-backend.h" #include "block/aio-wait.h" +#include "block/aio_task.h" #include "block/blockjob.h" #include "block/blockjob_int.h" #include "block/block_int.h"
@@ -33,6 +34,7 @@#include "qapi/error.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" +#include "qemu/units.h" #include "sysemu/replay.h" /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */
@@ -2640,6 +2642,100 @@ typedef struct BdrvVmstateCo {bool is_read; } BdrvVmstateCo; +typedef struct BdrvVMStateTask { +AioTask task; + +BlockDriverState *bs; +int64_t offset; +void *buf; +size_t bytes; +}BdrvVMStateTask ; + +typedef struct BdrvSaveVMState { +AioTaskPool *pool; +BdrvVMStateTask *t; +}BdrvSaveVMState ; + + +static coroutine_fn int bdrv_co_vmstate_save_task_entry(AioTask *task) +{ +int err =0 ; +BdrvVMStateTask *t =container_of (task, BdrvVMStateTask, task); + +if (t->bytes !=0 ) { +QEMUIOVector qiov =QEMU_IOVEC_INIT_BUF (qiov, t->buf, t->bytes); + +bdrv_inc_in_flight (t->bs); + err = t->bs->drv->bdrv_save_vmstate (t->bs, &qiov, t->offset); +bdrv_dec_in_flight (t->bs); + } + +qemu_vfree (t->buf); +return err; +} + +static BdrvVMStateTask *bdrv_vmstate_task_create (BlockDriverState *bs, +int64_t pos,size_t size) +{ +BdrvVMStateTask *t =g_new (BdrvVMStateTask,1 ); + + *t = (BdrvVMStateTask ) { + .task.func = bdrv_co_vmstate_save_task_entry, + .buf =qemu_blockalign (bs, size), + .offset = pos, + .bs = bs, + }; + +return t; +} + +static int bdrv_co_do_save_vmstate (BlockDriverState *bs,QEMUIOVector *qiov, +int64_t pos) +{ +BdrvSaveVMState *state = bs->savevm_state; +BdrvVMStateTask *t; +size_t buf_size =MAX (bdrv_get_cluster_size (bs),1 * MiB); +size_t to_copy, off; + +if (state ==NULL ) { + state =g_new (BdrvSaveVMState,1 ); + *state = (BdrvSaveVMState ) { + .pool =aio_task_pool_new (BDRV_VMSTATE_WORKERS_MAX ), + .t =bdrv_vmstate_task_create (bs, pos, buf_size), + }; + + bs->savevm_state = state; + } + +if (aio_task_pool_status (state->pool) <0 ) { +/* Caller is responsible for cleanup. We should block all further +* save operations for this exact state */ +return aio_task_pool_status (state->pool); + } + + t = state->t; +if (t->offset + t->bytes != pos) { +/* Normally this branch is not reachable from migration */ +return bs->drv->bdrv_save_vmstate (bs, qiov, pos); + } + + off =0 ; +while (1 ) { + to_copy =MIN (qiov->size - off, buf_size - t->bytes); +qemu_iovec_to_buf (qiov, off, t->buf + t->bytes, to_copy); + t->bytes += to_copy; +if (t->bytes < buf_size) { +return qiov->size; + } + +aio_task_pool_start_task (state->pool, &t->task); + + pos += to_copy; + off += to_copy; + state->t = t =bdrv_vmstate_task_create (bs, pos, buf_size); + } +} + static int coroutine_fnbdrv_co_rw_vmstate (BlockDriverState *bs,QEMUIOVector *qiov,int64_t pos,bool is_read)
@@ -2655,7 +2751,7 @@ bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,if (is_read) { ret = drv->bdrv_load_vmstate (bs, qiov, pos); }else { - ret = drv->bdrv_save_vmstate (bs, qiov, pos); + ret =bdrv_co_do_save_vmstate (bs, qiov, pos); } }else if (bs->file) { ret =bdrv_co_rw_vmstate (bs->file->bs, qiov, pos, is_read);
@@ -2726,7 +2822,30 @@ int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) static int coroutine_fn bdrv_co_flush_vmstate(BlockDriverState *bs) { -return 0 ; +int err; +BdrvSaveVMState *state = bs->savevm_state; + +if (bs->drv->bdrv_save_vmstate ==NULL && bs->file !=NULL ) { +return bdrv_co_flush_vmstate (bs->file->bs); + } +if (state ==NULL ) { +return 0 ; + } + +if (aio_task_pool_status (state->pool) >=0 ) { +/* We are on success path, commit last chunk if possible */ +aio_task_pool_start_task (state->pool, &state->t->task); + } + +aio_task_pool_wait_all (state->pool); + err =aio_task_pool_status (state->pool); + +aio_task_pool_free (state->pool); +g_free (state); + + bs->savevm_state =NULL ; + +return err; } static int coroutine_fn bdrv_flush_vmstate_co_entry(void *opaque)
include/block/block_int.h +8 -0
@@ -61,6 +61,8 @@#define BLOCK_PROBE_BUF_SIZE 512 +#define BDRV_VMSTATE_WORKERS_MAX 8 +enum BdrvTrackedRequestType {BDRV_TRACKED_READ ,BDRV_TRACKED_WRITE ,
@@ -784,6 +786,9 @@ struct BdrvChild {QLIST_ENTRY (BdrvChild) next_parent; }; + +typedef struct BdrvSaveVMState BdrvSaveVMState ; +/* * Note: the function bdrv_append() copies and swaps contents of * BlockDriverStates, so if you add new fields to this struct, please
@@ -947,6 +952,9 @@ struct BlockDriverState {/* BdrvChild links to this node may never be frozen */ bool never_freeze; + +/* Intermediate buffer for VM state saving from snapshot creation code */ +BdrvSaveVMState *savevm_state; };struct BlockBackendRootState {
Signature · 1 line
2.17.1
Re: [PATCH 5/5] block/io: improve savevm performance
Quoted 131 lines · Denis V. Lunev
This patch does 2 standard basic things:
- it creates intermediate buffer for all writes from QEMU migration code to block driver,
- this buffer is sent to disk asynchronously, allowing several writes to run in parallel.Thus
bdrv_vmstate_write()is becoming asynchronous. All pending operations completion are performed in newly inventedbdrv_flush_vmstate().In general, migration code is fantastically inefficent (by observation), buffers are not aligned and sent with arbitrary pieces, a lot of time less than 100 bytes at a chunk, which results in read-modify-write operations if target file descriptor is opened with
O_DIRECT. It should also be noted that all operations are performed into unallocated image blocks, which also suffer due to partial writes to such new clusters even on cached file descriptors.Snapshot creation time (2 GB Fedora-31 VM running over NVME storage):
original fixedcached: 1.79s 1.27s non-cached: 3.29s 0.81sThe difference over HDD would be more significant :)
Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Max Reitz <[email protected]>CC Stefan Hajnoczi <[email protected]>CC Fam Zheng <[email protected]>CC Juan Quintela <[email protected]>CC "Dr. David Alan Gilbert" <[email protected]>CC Vladimir Sementsov-Ogievskiy <[email protected]>CC Denis Plotnikov <[email protected]>--- block/io.c | 123 +++++++++++++++++++++++++++++++++++++- include/block/block_int.h | 8 +++ 2 files changed, 129 insertions(+), 2 deletions(-)block/io.c +71
-0@@ -26,6 +26,7 @@#include "trace.h" #include "sysemu/block-backend.h" #include "block/aio-wait.h" +#include "block/aio_task.h" #include "block/blockjob.h" #include "block/blockjob_int.h" #include "block/block_int.h" @@ -33,6 +34,7 @@#include "qapi/error.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" +#include "qemu/units.h" #include "sysemu/replay.h" /* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */ @@ -2640,6 +2642,100 @@ typedef struct BdrvVmstateCo {bool is_read; } BdrvVmstateCo; +typedef struct BdrvVMStateTask { +AioTask task; + +BlockDriverState *bs; +int64_t offset; +void *buf; +size_t bytes; +}BdrvVMStateTask ; + +typedef struct BdrvSaveVMState { +AioTaskPool *pool; +BdrvVMStateTask *t; +}BdrvSaveVMState ; + + +static coroutine_fn int bdrv_co_vmstate_save_task_entry(AioTask *task) +{ +int err =0 ; +BdrvVMStateTask *t =container_of (task, BdrvVMStateTask, task); + +if (t->bytes !=0 ) { +QEMUIOVector qiov =QEMU_IOVEC_INIT_BUF (qiov, t->buf, t->bytes); + +bdrv_inc_in_flight (t->bs); + err = t->bs->drv->bdrv_save_vmstate (t->bs, &qiov, t->offset); +bdrv_dec_in_flight (t->bs); + } + +qemu_vfree (t->buf); +return err; +} + +static BdrvVMStateTask *bdrv_vmstate_task_create (BlockDriverState *bs, +int64_t pos,size_t size) +{ +BdrvVMStateTask *t =g_new (BdrvVMStateTask,1 ); + + *t = (BdrvVMStateTask ) { + .task.func = bdrv_co_vmstate_save_task_entry, + .buf =qemu_blockalign (bs, size), + .offset = pos, + .bs = bs, + }; + +return t; +} + +static int bdrv_co_do_save_vmstate (BlockDriverState *bs,QEMUIOVector *qiov, +int64_t pos) +{ +BdrvSaveVMState *state = bs->savevm_state; +BdrvVMStateTask *t; +size_t buf_size =MAX (bdrv_get_cluster_size (bs),1 * MiB); +size_t to_copy, off; + +if (state ==NULL ) { + state =g_new (BdrvSaveVMState,1 ); + *state = (BdrvSaveVMState ) { + .pool =aio_task_pool_new (BDRV_VMSTATE_WORKERS_MAX ), + .t =bdrv_vmstate_task_create (bs, pos, buf_size), + }; + + bs->savevm_state = state; + } + +if (aio_task_pool_status (state->pool) <0 ) { +/* Caller is responsible for cleanup. We should block all further +* save operations for this exact state */ +return aio_task_pool_status (state->pool);
block/io.c typedef struct BdrvVmstateCo { · line 2723
Quoted 10 lines
block/io.c +10
-0+ } + + t = state->t; +if (t->offset + t->bytes != pos) { +/* Normally this branch is not reachable from migration */ +return bs->drv->bdrv_save_vmstate (bs, qiov, pos); + } + + off =0 ; +while (1 ) {
"while (aio_task_pool_status(state->pool) == 0)" + "return aio_task_pool_status(state->pool)" after loop will improve interactivity on failure path, but shouldn't be necessary.
block/io.c typedef struct BdrvVmstateCo { · line 2728
Quoted 5 lines
block/io.c +5
-0+ to_copy =MIN (qiov->size - off, buf_size - t->bytes); +qemu_iovec_to_buf (qiov, off, t->buf + t->bytes, to_copy); + t->bytes += to_copy; +if (t->bytes < buf_size) { +return qiov->size;
Intersting: we are substituting .bdrv_save_vmstate by this function. There are two existing now:
qcow2_save_vmstate, which doesn't care to return qiov->size and sd_save_vmstate which does it.
So, it seems safe to return qiov->size now, but I'm sure that it's actually unused and should be refactored to return 0 on success everywhere.
Quoted 23 lines
block/io.c +11
-1+ } + +aio_task_pool_start_task (state->pool, &t->task); + + pos += to_copy; + off += to_copy; + state->t = t =bdrv_vmstate_task_create (bs, pos, buf_size); + } +} + static int coroutine_fnbdrv_co_rw_vmstate (BlockDriverState *bs,QEMUIOVector *qiov,int64_t pos,bool is_read)@@ -2655,7 +2751,7 @@ bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,if (is_read) { ret = drv->bdrv_load_vmstate (bs, qiov, pos); }else { - ret = drv->bdrv_save_vmstate (bs, qiov, pos); + ret =bdrv_co_do_save_vmstate (bs, qiov, pos); } }else if (bs->file) { ret =bdrv_co_rw_vmstate (bs->file->bs, qiov, pos, is_read);
block/io.c int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) · line 2825
Quoted 5 lines
block/io.c +1
-1@@ -2726,7 +2822,30 @@ int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) static int coroutine_fn bdrv_co_flush_vmstate(BlockDriverState *bs) { -return 0 ; +int err;
nitpicking: you use "err" to store return codes, but everywhere in this file (and mostly in block/*) "ret" is used for it.
Quoted 51 lines
block/io.c +23
-0+BdrvSaveVMState *state = bs->savevm_state; + +if (bs->drv->bdrv_save_vmstate ==NULL && bs->file !=NULL ) { +return bdrv_co_flush_vmstate (bs->file->bs); + } +if (state ==NULL ) { +return 0 ; + } + +if (aio_task_pool_status (state->pool) >=0 ) { +/* We are on success path, commit last chunk if possible */ +aio_task_pool_start_task (state->pool, &state->t->task); + } + +aio_task_pool_wait_all (state->pool); + err =aio_task_pool_status (state->pool); + +aio_task_pool_free (state->pool); +g_free (state); + + bs->savevm_state =NULL ; + +return err; } static int coroutine_fn bdrv_flush_vmstate_co_entry(void *opaque)include/block/block_int.h +5
-0@@ -61,6 +61,8 @@#define BLOCK_PROBE_BUF_SIZE 512 +#define BDRV_VMSTATE_WORKERS_MAX 8 +enum BdrvTrackedRequestType {BDRV_TRACKED_READ ,BDRV_TRACKED_WRITE ,@@ -784,6 +786,9 @@ struct BdrvChild {QLIST_ENTRY (BdrvChild) next_parent; }; + +typedef struct BdrvSaveVMState BdrvSaveVMState ; +/* * Note: the function bdrv_append() copies and swaps contents of * BlockDriverStates, so if you add new fields to this struct, please
include/block/block_int.h struct BlockDriverState { · line 960
Quoted 9 lines
include/block/block_int.h +3
-0@@ -947,6 +952,9 @@ struct BlockDriverState {/* BdrvChild links to this node may never be frozen */ bool never_freeze; + +/* Intermediate buffer for VM state saving from snapshot creation code */ +BdrvSaveVMState *savevm_state; };struct BlockBackendRootState {
Minor improvements and further refactoring may be done in separate, I'm OK with it as is:
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 5/5] block/io: improve savevm performance
Quoted 160 lines · Vladimir Sementsov-Ogievskiy
Quoted 136 lines · Denis V. Lunev
This patch does 2 standard basic things:
- it creates intermediate buffer for all writes from QEMU migration code to block driver,
- this buffer is sent to disk asynchronously, allowing several writes to run in parallel.Thus
bdrv_vmstate_write()is becoming asynchronous. All pending operations
completion are performed in newly inventedbdrv_flush_vmstate().In general, migration code is fantastically inefficent (by observation), buffers are not aligned and sent with arbitrary pieces, a lot of time less than 100 bytes at a chunk, which results in read-modify-write operations if target file descriptor is opened with
O_DIRECT. It should also be noted that all operations are performed into unallocated image blocks, which also suffer due to partial writes to such new clusters even on cached file descriptors.Snapshot creation time (2 GB Fedora-31 VM running over NVME storage):
original fixed
cached: 1.79s 1.27s
non-cached: 3.29s 0.81sThe difference over HDD would be more significant :)
Signed-off-by Denis V. Lunev <[email protected]>CC Kevin Wolf <[email protected]>CC Max Reitz <[email protected]>CC Stefan Hajnoczi <[email protected]>CC Fam Zheng <[email protected]>CC Juan Quintela <[email protected]>CC "Dr. David Alan Gilbert" <[email protected]>CC Vladimir Sementsov-Ogievskiy <[email protected]>CC Denis Plotnikov <[email protected]>---
block/io.c | 123 +++++++++++++++++++++++++++++++++++++-include/block/block_int.h| 8 +++2 files changed, 129 insertions(+), 2 deletions(-)block/io.c +2
-0@@ -26,6 +26,7 @@ #include "trace.h" #include "sysemu/block-backend.h" #include "block/aio-wait.h" +#include "block/aio_task.h" #include "block/blockjob.h" #include "block/blockjob_int.h" #include "block/block_int.h"@@ -33,6 +34,7 @@ #include "qapi/error.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" +#include "qemu/units.h" #include "sysemu/replay.h" /* Maximum bounce buffer for copy-on-read and write zeroes, inbytes */
block/io.c +66
-0@@ -2640,6 +2642,100 @@ typedef struct BdrvVmstateCo { bool is_read; } BdrvVmstateCo; +typedef struct BdrvVMStateTask { + AioTask task; + + BlockDriverState *bs; + int64_t offset; + void *buf; + size_t bytes; +} BdrvVMStateTask; + +typedef struct BdrvSaveVMState { + AioTaskPool *pool; + BdrvVMStateTask *t; +}BdrvSaveVMState ; + + +static coroutine_fn int bdrv_co_vmstate_save_task_entry(AioTask *task) +{ + int err = 0; + BdrvVMStateTask *t = container_of(task, BdrvVMStateTask, task); + + if (t->bytes != 0) { + QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, t->buf, t->bytes); + + bdrv_inc_in_flight(t->bs); + err = t->bs->drv->bdrv_save_vmstate(t->bs, &qiov, t->offset); + bdrv_dec_in_flight(t->bs); + } + + qemu_vfree(t->buf); + return err; +} + +static BdrvVMStateTask *bdrv_vmstate_task_create (BlockDriverState *bs, + int64_t pos, size_t size) +{ + BdrvVMStateTask *t = g_new(BdrvVMStateTask, 1); + + *t = (BdrvVMStateTask) { + .task.func = bdrv_co_vmstate_save_task_entry, + .buf = qemu_blockalign(bs, size), + .offset = pos, + .bs = bs, + }; + + return t; +} + +static int bdrv_co_do_save_vmstate (BlockDriverState *bs,QEMUIOVector *qiov, + int64_t pos) +{ + BdrvSaveVMState *state = bs->savevm_state; + BdrvVMStateTask *t; + size_t buf_size = MAX(bdrv_get_cluster_size(bs), 1 * MiB); + size_t to_copy, off; + + if (state == NULL) { + state = g_new(BdrvSaveVMState, 1); + *state = (BdrvSaveVMState) { + .pool = aio_task_pool_new(BDRV_VMSTATE_WORKERS_MAX), + .t = bdrv_vmstate_task_create(bs, pos, buf_size), + }; + + bs->savevm_state = state; + } + + if (aio_task_pool_status(state->pool) < 0) { + /* Caller is responsible for cleanup. We should block allfurther
+ * save operations for this exact state */ + returnaio_task_pool_status(state->pool); + }block/io.c typedef struct BdrvVmstateCo { · line 2723
Quoted 9 lines
block/io.c +9
-0+ + t = state->t; + if (t->offset + t->bytes != pos) { + /* Normally this branch is not reachable from migration */ + return bs->drv->bdrv_save_vmstate(bs, qiov, pos); + } + + off = 0; + while (1) {"while (
aio_task_pool_status(state->pool)== 0)" + "returnaio_task_pool_status(state->pool)" after loop will improve interactivity on failure path, but shouldn't be necessary.Quoted 5 lines
+
to_copy=MIN(qiov->size - off, buf_size - t->bytes); +qemu_iovec_to_buf(qiov, off, t->buf + t->bytes, to_copy); +t->bytes+=to_copy;
+ if (t->bytes<buf_size) {
+ returnqiov->size;Intersting: we are substituting .bdrv_save_vmstate by this function. There are two existing now:
Quoted 6 lines
qcow2_save_vmstate, which doesn't care to returnqiov->sizeandsd_save_vmstatewhich does it.So, it seems safe to return
qiov->sizenow, but I'm sure that it's actually unused and should be
refactored to return 0 on success everywhere.
This looks like my mistake now. I have spent some time with error codes working with Eric's suggestions and believe that 0 should be returned now.
Will fix in next revision.
Den
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Subject: [PATCH v4 0/4] block: seriously improve savevm performance
Type: series
Message-id: [email protected]
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
551826e block/io: improve savevm performance
81a93fd block, migration: add bdrv_flush_vmstate helper
05e3395 block/aio_task: drop aio_task_pool_wait_one() helper 61e0ce3 block/aio_task: allow start/wait task from any coroutine d04a716 migration/savevm: respect qemu_fclose() error code in save_snapshot()
=== OUTPUT BEGIN ===
1/5 Checking commit d04a7160d2c0 (migration/savevm: respect qemu_fclose() error code in save_snapshot()) 2/5 Checking commit 61e0ce32d320 (block/aio_task: allow start/wait task from any coroutine) 3/5 Checking commit 05e339500af8 (block/aio_task: drop aio_task_pool_wait_one() helper) 4/5 Checking commit 81a93fdf74a9 (block, migration: add bdrv_flush_vmstate helper)
WARNING: Block comments use a leading /* on a separate line
#93: FILE: include/block/block.h:575:
+/* bdrv_flush_vmstate() is mandatory to commit vmstate changes if
WARNING: Block comments use * on subsequent lines
#94: FILE: include/block/block.h:576:
+/* bdrv_flush_vmstate() is mandatory to commit vmstate changes if
+ bdrv_save_vmstate() was ever called */
WARNING: Block comments use a trailing */ on a separate line #94: FILE: include/block/block.h:576:
+ bdrv_save_vmstate() was ever called */
ERROR: braces {} are necessary for all arms of this statement #108: FILE: migration/savevm.c:154:
+ if (err < 0)
[...]
total: 1 errors, 3 warnings, 60 lines checked
Patch 4/5 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
5/5 Checking commit 551826e4ae66 (block/io: improve savevm performance)
WARNING: Block comments use a leading /* on a separate line #132: FILE: block/io.c:2711:
+ /* Caller is responsible for cleanup. We should block all further
WARNING: Block comments use a trailing */ on a separate line #133: FILE: block/io.c:2712:
+ * save operations for this exact state */
total: 0 errors, 2 warnings, 179 lines checked
Patch 5/5 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]