migration/snap-tool: External snapshot utility
cover migration/snap-tool: External snapshot utility
This series is a kind of PoC for asynchronous snapshot reverting. This is about external snapshots only and doesn't involve block devices. Thus, it's mainly intended to be used with the new 'background-snapshot' migration capability and otherwise standard QEMU migration mechanism.
The major ideas behind this first version were:
* Make it compatible with 'exec:'-style migration - options can be create some separate tool or integrate into qemu-system.
* Support asynchronous revert stage by using unaltered postcopy logic at destination. To do this, we should be capable of saving RAM pages so that any particular page can be directly addressed by it's block ID and page offset. Possible solutions here seem to be:
use separate index (and storing it somewhere)
create sparse file on host FS and address pages with file offset use QCOW2 (or other) image container with inherent sparsity support
* Make snapshot image file dense on the host FS so we don't depend on copy/backup tools and how they deal with sparse files. Off course, there's some performance cost for this choice.
* Make the code which is parsing unstructered format of migration stream, at least, not very sophisticated. Also, try to have minimum dependencies on QEMU migration code, both RAM and device.
* Try to keep page save latencies small while not degrading migration bandwidth too much.
For this first version I decided not to integrate into main QEMU code but create a separate tool. The main reason is that there's not too much migration code that is target-specific and can be used in it's unmodified form. Also, it's still not very clear how to make 'qemu-system' integration in terms of command-line (or monitor/QMP?) interface extension.
For the storage format, QCOW2 as a container and large (1MB) cluster size seem to be an optimal choice. Larger cluster is beneficial for performance particularly in the case when image preallocation is disabled. Such cluster size does not result in too high internal fragmentation level (~10% of space waste in most cases) yet allows to reduce significantly the number of expensive cluster allocations.
A bit tricky part is dispatching QEMU migration stream cause it is mostly unstructered and depends on configuration parameters like 'send-configuration' and 'send-section-footer'. But, for the case with default values in migration globals it seems that implemented dispatching code works well and won't have compatibility issues in a reasonably long time frame.
I decided to keep RAM save path synchronous, anyhow it's better to use writeback cache mode for the live snapshots cause of it's interleaving page address pattern. Page coalescing buffer is used to merge contiguous pages to optimize block layer writes.
Since for snapshot loading opening image file in cached mode would not do any good, it implies that Linux native AIO and O_DIRECT mode is used in a common scenario. AIO support in RAM loading path is implemented by using a ring of preallocated fixed-sized buffers in such a way that there's always a number of outstanding block requests anytime. It also ensures in-order request completion.
How to use:
**Save:**
* qemu> migrate_set_capability background-snapshot on
* qemu> migrate "exec:<qemu-bin-path>/qemu-snap -s <virtual-size>
--cache=writeback --aio=threads save <image-file.qcow2>"
**Load:**
* Use 'qemu-system-* -incoming defer'
* qemu> migrate_incoming "exec:<qemu-bin-path>/qemu-snap
--cache=none --aio=native load <image-file.qcow2>"
**Load with postcopy:**
* Use 'qemu-system-* -incoming defer'
* qemu> migrate_set_capability postcopy-ram on
* qemu> migrate_incoming "exec:<qemu-bin-path>/qemu-snap --postcopy=60
--cache=none --aio=native load <image-file.qcow2>"
And yes, asynchronous revert works well only with SSD, not with rotational disk..
Some performance stats:
* SATA SSD drive with ~500/450 MB/s sequantial read/write and ~60K IOPS max.
* 220 MB/s average save rate (depends on workload)
* 440 MB/s average load rate in precopy
* 260 MB/s average load rate in postcopy
Andrey Gruzdev (9):
migration/snap-tool: Introduce qemu-snap tool migration/snap-tool: Snapshot image create/open routines for qemu-snap
tool
migration/snap-tool: Preparations to run code in main loop context migration/snap-tool: Introduce qemu_ftell2() routine to qemu-file.c migration/snap-tool: Block layer AIO support and file utility routines migration/snap-tool: Move RAM_SAVE_FLAG_xxx defines to migration/ram.h migration/snap-tool: Complete implementation of snapshot saving migration/snap-tool: Implementation of snapshot loading in precopy migration/snap-tool: Implementation of snapshot loading in postcopy
include/qemu-snap.h | 163 ++++ meson.build | 2 + migration/qemu-file.c | 6 + migration/qemu-file.h | 1 + migration/ram.c | 16 - migration/ram.h | 16 + qemu-snap-handlers.c | 1801 +++++++++++++++++++++++++++++++++++++++++ qemu-snap-io.c | 325 ++++++++ qemu-snap.c | 673 +++++++++++++++ 9 files changed, 2987 insertions(+), 16 deletions(-) create mode 100644 include/qemu-snap.h create mode 100644 qemu-snap-handlers.c create mode 100644 qemu-snap-io.c create mode 100644 qemu-snap.c
Signature · 1 line
2.25.1
RFC PATCH 1/9 migration/snap-tool: Introduce qemu-snap tool
Initial commit with code to set up execution environment, parse command-line arguments, show usage/version info and so on.
--- include/qemu-snap.h | 35 ++++ meson.build | 2 + qemu-snap.c | 414 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 451 insertions(+) create mode 100644 include/qemu-snap.h create mode 100644 qemu-snap.c
include/qemu-snap.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* QEMU External Snapshot Utility +* +* Copyright Virtuozzo GmbH, 2021 +* +* Authors: +* Andrey Gruzdev <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#ifndef QEMU_SNAP_H +#define QEMU_SNAP_H + +/* Target page size, if not specified explicitly in command-line */ +#define DEFAULT_PAGE_SIZE 4096 +/* +* Maximum supported target page size, cause we use QEMUFile and +* qemu_get_buffer_in_place(). IO_BUF_SIZE is currently 32KB. +*/ +#define PAGE_SIZE_MAX 16384 + +typedef struct SnapSaveState { +BlockBackend *blk;/* Block backend */ +}SnapSaveState ; + +typedef struct SnapLoadState { +BlockBackend *blk;/* Block backend */ +}SnapLoadState ; + +SnapSaveState *snap_save_get_state (void ); +SnapLoadState *snap_load_get_state (void ); + +#endif /* QEMU_SNAP_H */
meson.build +2 -0
@@ -2324,6 +2324,8 @@ if have_tools dependencies: [block, qemuutil], install: true) qemu_nbd = executable('qemu-nbd', files('qemu-nbd.c'), dependencies: [blockdev, qemuutil, gnutls], install: true) + qemu_snap = executable('qemu-snap', files('qemu-snap.c'), + dependencies: [blockdev, qemuutil, migration], install: true) subdir('storage-daemon') subdir('contrib/rdmacm-mux')
qemu-snap.c new file +414 -0
414 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH 2/9 migration/snap-tool: Snapshot image create/open routines for qemu-snap tool
In reply to Andrey GruzdevImplementation of routines for QCOW2 image creation and opening. Some predefined parameters for image creation and opening are introduced that provide reasonable tradeoff between performance, file size and usability.
Thus, it was chosen to disable preallocation and keep image file dense on host file system, the apparent file size equals allocated in this case which is anyways beneficial for the user experience.
Larger 1MB cluster size is adopted to reduce allocation overhead and improve I/O performance while keeping internal fragmentation of snapshot image reasonably small.
--- qemu-snap.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 4 deletions(-)
qemu-snap.c +90 -4
@@ -31,6 +31,16 @@#include "migration/qemu-file.h" #include "qemu-snap.h" +/* QCOW2 image options */ +#define BLK_FORMAT_DRIVER "qcow2" +#define BLK_CREATE_OPT_STRING "preallocation=off,lazy_refcounts=on," \ + "extended_l2=off,compat=v3,cluster_size=1M," \ + "refcount_bits=8" +/* L2 cache size to cover 2TB of memory */ +#define BLK_L2_CACHE_SIZE "16M" +/* Single L2 cache entry for the whole L2 table */ +#define BLK_L2_CACHE_ENTRY_SIZE "1M" +#define OPT_CACHE 256#define OPT_AIO 257
@@ -104,30 +114,106 @@ static void snap_load_destroy_state(void)/* TODO: implement */ } +static BlockBackend *snap_create (const char *filename,int64_t image_size, +int flags,bool writethrough) +{ +char *create_opt_string; +QDict *blk_opts; +BlockBackend *blk; +Error *local_err =NULL ; + +/* Create QCOW2 image with given parameters */ + create_opt_string =g_strdup (BLK_CREATE_OPT_STRING ); +bdrv_img_create (filename,BLK_FORMAT_DRIVER ,NULL ,NULL , + create_opt_string, image_size, flags, true, &local_err); +g_free (create_opt_string); + +if (local_err) { +error_reportf_err (local_err,"Could not create '%s': " , filename); + goto fail; + } + +/* Block backend open options */ + blk_opts =qdict_new (); +qdict_put_str (blk_opts,"driver" ,BLK_FORMAT_DRIVER ); +qdict_put_str (blk_opts,"l2-cache-size" ,BLK_L2_CACHE_SIZE ); +qdict_put_str (blk_opts,"l2-cache-entry-size" ,BLK_L2_CACHE_ENTRY_SIZE ); + +/* Open block backend instance for the created image */ + blk =blk_new_open (filename,NULL , blk_opts, flags, &local_err); +if (!blk) { +error_reportf_err (local_err,"Could not open '%s': " , filename); +/* Delete image file */ +qemu_unlink (filename); + goto fail; + } + +blk_set_enable_write_cache (blk, !writethrough); +return blk; + +fail: +return NULL ; +} + +static BlockBackend *snap_open (const char *filename,int flags) +{ +QDict *blk_opts; +BlockBackend *blk; +Error *local_err =NULL ; + +/* Block backend open options */ + blk_opts =qdict_new (); +qdict_put_str (blk_opts,"driver" ,BLK_FORMAT_DRIVER ); +qdict_put_str (blk_opts,"l2-cache-size" ,BLK_L2_CACHE_SIZE ); +qdict_put_str (blk_opts,"l2-cache-entry-size" ,BLK_L2_CACHE_ENTRY_SIZE ); + +/* Open block backend instance */ + blk =blk_new_open (filename,NULL , blk_opts, flags, &local_err); +if (!blk) { +error_reportf_err (local_err,"Could not open '%s': " , filename); +return NULL ; + } + +return blk; +} +static int snap_save (const SnapSaveParams *params) {SnapSaveState *sn; +int res =-1 ;snap_save_init_state (); sn =snap_save_get_state (); - (void ) sn; + sn->blk =snap_create (params->filename, params->image_size, + params->bdrv_flags, params->writethrough); +if (!sn->blk) { + goto fail; + } + +fail:snap_save_destroy_state (); -return 0 ; +return res; }static int snap_load (SnapLoadParams *params) {SnapLoadState *sn; +int res =-1 ;snap_load_init_state (); sn =snap_load_get_state (); - (void ) sn; + sn->blk =snap_open (params->filename, params->bdrv_flags); +if (!sn->blk) { + goto fail; + } + +fail:snap_load_destroy_state (); -return 0 ; +return res; } static int64_t cvtnum_full(const char *name, const char *value,
Signature · 1 line
2.25.1
RFC PATCH 3/9 migration/snap-tool: Preparations to run code in main loop context
In reply to Andrey GruzdevMajor part of code is using QEMUFile and block layer routines, thus to take advantage from concurrent I/O operations we need to use coroutines and run in the the main loop context.
--- include/qemu-snap.h | 3 +++ meson.build | 2 +- qemu-snap-handlers.c | 38 ++++++++++++++++++++++++++ qemu-snap.c | 63 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 qemu-snap-handlers.c
include/qemu-snap.h +3 -0
@@ -32,4 +32,7 @@ typedef struct SnapLoadState {SnapSaveState *snap_save_get_state (void );SnapLoadState *snap_load_get_state (void ); +int coroutine_fn snap_save_state_main(SnapSaveState *sn); +int coroutine_fn snap_load_state_main(SnapLoadState *sn); +#endif /* QEMU_SNAP_H */
meson.build +1 -1
@@ -2324,7 +2324,7 @@ if have_tools dependencies: [block, qemuutil], install: true) qemu_nbd = executable('qemu-nbd', files('qemu-nbd.c'), dependencies: [blockdev, qemuutil, gnutls], install: true) - qemu_snap = executable('qemu-snap', files('qemu-snap.c'), + qemu_snap = executable('qemu-snap', files('qemu-snap.c', 'qemu-snap-handlers.c'), dependencies: [blockdev, qemuutil, migration], install: true) subdir('storage-daemon')
qemu-snap-handlers.c new file +38 -0
@@ -0,0 +1,38 @@ +/* +* QEMU External Snapshot Utility +* +* Copyright Virtuozzo GmbH, 2021 +* +* Authors: +* Andrey Gruzdev <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* later. See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "sysemu/block-backend.h" +#include "qemu/coroutine.h" +#include "qemu/cutils.h" +#include "qemu/bitmap.h" +#include "qemu/error-report.h" +#include "io/channel-buffer.h" +#include "migration/qemu-file-channel.h" +#include "migration/qemu-file.h" +#include "migration/savevm.h" +#include "migration/ram.h" +#include "qemu-snap.h" + +/* Save snapshot data from incoming migration stream */ +int coroutine_fn snap_save_state_main(SnapSaveState *sn) +{ +/* TODO: implement */ +return 0 ; +} + +/* Load snapshot data and send it with outgoing migration stream */ +int coroutine_fn snap_load_state_main(SnapLoadState *sn) +{ +/* TODO: implement */ +return 0 ; +}
qemu-snap.c +63 -0
87 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH 4/9 migration/snap-tool: Introduce qemu_ftell2() routine to qemu-file.c
In reply to Andrey GruzdevIn several place we need to get QEMUFile input position in the meaning of the number of bytes read by qemu_get_byte()/qemu_get_buffer() routines.
Existing qemu_ftell() returns offset in terms of the number of bytes read from underlying IOChannel object which is not suitable here.
--- migration/qemu-file.c | 6 ++++++ migration/qemu-file.h | 1 + 2 files changed, 7 insertions(+)
migration/qemu-file.c +6 -0
@@ -657,6 +657,12 @@ int64_t qemu_ftell(QEMUFile *f)return f->pos; } +int64_t qemu_ftell2 (QEMUFile *f) +{ +qemu_fflush (f); +return f->pos + f->buf_index - f->buf_size; +} +int qemu_file_rate_limit (QEMUFile *f) {if (f->shutdown) {
migration/qemu-file.h +1 -0
@@ -124,6 +124,7 @@ void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks);int qemu_get_fd (QEMUFile *f);int qemu_fclose (QEMUFile *f);int64_t qemu_ftell (QEMUFile *f); +int64_t qemu_ftell2 (QEMUFile *f);int64_t qemu_ftell_fast (QEMUFile *f);/* * put_buffer without copying the buffer.
Signature · 1 line
2.25.1
RFC PATCH 5/9 migration/snap-tool: Block layer AIO support and file utility routines
In reply to Andrey GruzdevIntroducing support for asynchronous block layer requests with in-order completion guerantee using simple buffer descriptor ring and coroutines.
Added support for opening QEMUFile with VMSTATE area of QCOW2 image as backing, also introduced several file utility routines.
--- include/qemu-snap.h | 48 +++++++ meson.build | 2 +- qemu-snap-io.c | 325 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 374 insertions(+), 1 deletion(-) create mode 100644 qemu-snap-io.c
include/qemu-snap.h +48 -0
@@ -13,6 +13,11 @@#ifndef QEMU_SNAP_H #define QEMU_SNAP_H +/* Synthetic value for invalid offset */ +#define INVALID_OFFSET ((int64_t) -1) +/* Max. byte count for QEMUFile inplace read */ +#define INPLACE_READ_MAX (32768 - 4096) +/* Target page size, if not specified explicitly in command-line */ #define DEFAULT_PAGE_SIZE 4096/*
@@ -21,6 +26,31 @@*/ #define PAGE_SIZE_MAX 16384 +typedef struct AioBufferPool AioBufferPool ; + +typedef struct AioBufferStatus { +/* BDRV operation start offset */ +int64_t offset; +/* BDRV operation byte count or negative error code */ +int count; +}AioBufferStatus ; + +typedef struct AioBuffer { +void *data;/* Data buffer */ +int size;/* Size of data buffer */ + +AioBufferStatus status;/* Status returned by task->func() */ +}AioBuffer ; + +typedef struct AioBufferTask { +AioBuffer *buffer;/* AIO buffer */ + +int64_t offset;/* BDRV operation start offset */ +int size;/* BDRV requested transfer size */ +}AioBufferTask ; + +typedef AioBufferStatus coroutine_fn (*AioBufferFunc)(AioBufferTask *task); +typedef struct SnapSaveState {BlockBackend *blk;/* Block backend */ }SnapSaveState ;
@@ -35,4 +65,22 @@ SnapLoadState *snap_load_get_state(void); int coroutine_fn snap_save_state_main(SnapSaveState *sn); int coroutine_fn snap_load_state_main(SnapLoadState *sn); +QEMUFile *qemu_fopen_bdrv_vmstate (BlockDriverState *bs,int is_writable); + +AioBufferPool *coroutine_fn aio_pool_new(int buf_align, int buf_size, int buf_count); +void aio_pool_free (AioBufferPool *pool); +void aio_pool_set_max_in_flight (AioBufferPool *pool,int max_in_flight); +int aio_pool_status (AioBufferPool *pool); + +bool coroutine_fn aio_pool_can_acquire_next(AioBufferPool *pool); +AioBuffer *coroutine_fn aio_pool_try_acquire_next(AioBufferPool *pool); +AioBuffer *coroutine_fn aio_pool_wait_compl_next(AioBufferPool *pool); +void coroutine_fn aio_buffer_release(AioBuffer *buffer); + +void coroutine_fn aio_buffer_start_task(AioBuffer *buffer, AioBufferFunc func, +int64_t offset,int size); + +void file_transfer_to_eof (QEMUFile *f_dst,QEMUFile *f_src); +void file_transfer_bytes (QEMUFile *f_dst,QEMUFile *f_src,size_t size); +#endif /* QEMU_SNAP_H */
meson.build +1 -1
@@ -2324,7 +2324,7 @@ if have_tools dependencies: [block, qemuutil], install: true) qemu_nbd = executable('qemu-nbd', files('qemu-nbd.c'), dependencies: [blockdev, qemuutil, gnutls], install: true) - qemu_snap = executable('qemu-snap', files('qemu-snap.c', 'qemu-snap-handlers.c'), + qemu_snap = executable('qemu-snap', files('qemu-snap.c', 'qemu-snap-handlers.c', 'qemu-snap-io.c'), dependencies: [blockdev, qemuutil, migration], install: true) subdir('storage-daemon')
qemu-snap-io.c new file +325 -0
325 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH 6/9 migration/snap-tool: Move RAM_SAVE_FLAG_xxx defines to migration/ram.h
In reply to Andrey GruzdevMove RAM_SAVE_FLAG_xxx defines from migration/ram.c to migration/ram.h
--- migration/ram.c | 16 ---------------- migration/ram.h | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-)
migration/ram.c +0 -16
@@ -65,22 +65,6 @@/***********************************************************/ /* ram save/restore */ -/* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it -* worked for pages that where filled with the same char. We switched -* it to only search for the zero value. And to avoid confusion with -* RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it. -*/ - -#define RAM_SAVE_FLAG_FULL 0x01/* Obsolete, not used anymore */ -#define RAM_SAVE_FLAG_ZERO 0x02 -#define RAM_SAVE_FLAG_MEM_SIZE 0x04 -#define RAM_SAVE_FLAG_PAGE 0x08 -#define RAM_SAVE_FLAG_EOS 0x10 -#define RAM_SAVE_FLAG_CONTINUE 0x20 -#define RAM_SAVE_FLAG_XBZRLE 0x40 -/* 0x80 is reserved in migration.h start with 0x100 next */ -#define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 -static inline bool is_zero_range (uint8_t *p,uint64_t size) {return buffer_is_zero (p, size);
migration/ram.h +16 -0
@@ -33,6 +33,22 @@#include "exec/cpu-common.h" #include "io/channel.h" +/* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it +* worked for pages that where filled with the same char. We switched +* it to only search for the zero value. And to avoid confusion with +* RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it. +*/ + +#define RAM_SAVE_FLAG_FULL 0x01/* Obsolete, not used anymore */ +#define RAM_SAVE_FLAG_ZERO 0x02 +#define RAM_SAVE_FLAG_MEM_SIZE 0x04 +#define RAM_SAVE_FLAG_PAGE 0x08 +#define RAM_SAVE_FLAG_EOS 0x10 +#define RAM_SAVE_FLAG_CONTINUE 0x20 +#define RAM_SAVE_FLAG_XBZRLE 0x40 +/* 0x80 is reserved in migration.h start with 0x100 next */ +#define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 +extern MigrationStats ram_counters;extern XBZRLECacheStats xbzrle_counters;extern CompressionStats compression_counters;
Signature · 1 line
2.25.1
RFC PATCH 7/9 migration/snap-tool: Complete implementation of snapshot saving
In reply to Andrey GruzdevIncludes code to parse incoming migration stream, dispatch data to section handlers and deal with complications of open-coded migration format without introducing strong dependencies on QEMU migration code.
--- include/qemu-snap.h | 42 +++ qemu-snap-handlers.c | 717 ++++++++++++++++++++++++++++++++++++++++++- qemu-snap.c | 54 +++- 3 files changed, 810 insertions(+), 3 deletions(-)
include/qemu-snap.h +42 -0
@@ -51,8 +51,47 @@ typedef struct AioBufferTask { typedef AioBufferStatus coroutine_fn (*AioBufferFunc)(AioBufferTask *task); +typedef struct QIOChannelBuffer QIOChannelBuffer ; +typedef struct SnapSaveState { +const char *filename;/* Image file name */ BlockBackend *blk;/* Block backend */ + +QEMUFile *f_fd;/* Incoming migration stream QEMUFile */ +QEMUFile *f_vmstate;/* Block backend vmstate area QEMUFile */ +/* +* Buffer to stash first few KBs of incoming stream, part of it later will +* go the the VMSTATE area of the image file. Specifically, these are VM +* state header, configuration section and the section which contains +* RAM block list. +*/ +QIOChannelBuffer *ioc_lbuf; +/* Page coalescing buffer channel */ +QIOChannelBuffer *ioc_pbuf; + +/* BDRV offset matching start of ioc_pbuf */ +int64_t bdrv_offset; +/* Last BDRV offset saved to ioc_pbuf */ +int64_t last_bdrv_offset; + +/* Stream read position, updated at the beginning of each new section */ +int64_t stream_pos; + +/* Stream read position at the beginning of RAM block list section */ +int64_t ram_list_pos; +/* Stream read position at the beginning of the first RAM data section */ +int64_t ram_pos; +/* Stream read position at the beginning of the first device state section */ +int64_t device_pos; + +/* Final status */ +int status; + +/* +* Keep first few bytes from the beginning of each section for the case +* when we meet device state section and go into 'default_handler'. +*/ +uint8_t section_header[512 ]; }SnapSaveState ; typedef struct SnapLoadState {
@@ -65,6 +104,9 @@ SnapLoadState *snap_load_get_state(void); int coroutine_fn snap_save_state_main(SnapSaveState *sn); int coroutine_fn snap_load_state_main(SnapLoadState *sn); +void snap_ram_init_state (int page_bits); +void snap_ram_destroy_state (void ); +QEMUFile *qemu_fopen_bdrv_vmstate (BlockDriverState *bs,int is_writable); AioBufferPool *coroutine_fn aio_pool_new(int buf_align, int buf_size, int buf_count);
qemu-snap-handlers.c +715 -2
729 lines not on this page · raw message
qemu-snap.c +53 -1
93 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH 8/9 migration/snap-tool: Implementation of snapshot loading in precopy
In reply to Andrey GruzdevThis part implements snapshot loading in precopy mode.
--- include/qemu-snap.h | 24 ++ qemu-snap-handlers.c | 586 ++++++++++++++++++++++++++++++++++++++++++- qemu-snap.c | 44 +++- 3 files changed, 649 insertions(+), 5 deletions(-)
include/qemu-snap.h +24 -0
@@ -26,6 +26,11 @@*/ #define PAGE_SIZE_MAX 16384 +/* Buffer size for RAM chunk loads via AIO buffer_pool */ +#define AIO_BUFFER_SIZE (1024 * 1024) +/* Max. concurrent AIO tasks */ +#define AIO_TASKS_MAX 8 +typedef struct AioBufferPool AioBufferPool ; typedef struct AioBufferStatus {
@@ -96,6 +101,25 @@ typedef struct SnapSaveState {typedef struct SnapLoadState {BlockBackend *blk;/* Block backend */ + +QEMUFile *f_fd;/* Outgoing migration stream QEMUFile */ +QEMUFile *f_vmstate;/* Block backend vmstate area QEMUFile */ +/* +* Buffer to keep first few KBs of BDRV vmstate that we stashed at the +* start. Within this buffer are VM state header, configuration section +* and the first 'ram' section with RAM block list. +*/ +QIOChannelBuffer *ioc_lbuf; + +/* AIO buffer pool */ +AioBufferPool *aio_pool; + +/* BDRV vmstate offset of RAM block list section */ +int64_t state_ram_list_offset; +/* BDRV vmstate offset of the first device section */ +int64_t state_device_offset; +/* BDRV vmstate End-Of-File */ +int64_t state_eof; }SnapLoadState ;SnapSaveState *snap_save_get_state (void );
qemu-snap-handlers.c +582 -4
646 lines not on this page · raw message
qemu-snap.c +43 -1
77 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH 9/9 migration/snap-tool: Implementation of snapshot loading in postcopy
In reply to Andrey GruzdevImplementation of asynchronous snapshot loading using standard postcopy migration mechanism on destination VM.
The point of switchover to postcopy is trivially selected based on percentage of non-zero pages loaded in precopy.
--- include/qemu-snap.h | 11 + qemu-snap-handlers.c | 482 ++++++++++++++++++++++++++++++++++++++++++- qemu-snap.c | 16 ++ 3 files changed, 504 insertions(+), 5 deletions(-)
include/qemu-snap.h +11 -0
@@ -30,6 +30,8 @@#define AIO_BUFFER_SIZE (1024 * 1024)/* Max. concurrent AIO tasks */ #define AIO_TASKS_MAX 8 +/* Max. concurrent AIO tasks in postcopy */ +#define AIO_TASKS_POSTCOPY_MAX 4typedef struct AioBufferPool AioBufferPool ;
@@ -103,6 +105,7 @@ typedef struct SnapLoadState {BlockBackend *blk;/* Block backend */ QEMUFile *f_fd;/* Outgoing migration stream QEMUFile */ +QEMUFile *f_rp_fd;/* Return path stream QEMUFile */ QEMUFile *f_vmstate;/* Block backend vmstate area QEMUFile */ /* * Buffer to keep first few KBs of BDRV vmstate that we stashed at the
@@ -114,6 +117,14 @@ typedef struct SnapLoadState {/* AIO buffer pool */ AioBufferPool *aio_pool; +bool postcopy;/* From command-line --postcopy */ +int postcopy_percent;/* From command-line --postcopy */ +bool in_postcopy;/* Switched to postcopy mode */ + +/* Return path listening thread */ +QemuThread rp_listen_thread; +bool has_rp_listen_thread; +/* BDRV vmstate offset of RAM block list section */ int64_t state_ram_list_offset;/* BDRV vmstate offset of the first device section */
qemu-snap-handlers.c +477 -5
558 lines not on this page · raw message
qemu-snap.c +16 -0
@@ -141,6 +141,10 @@ static void snap_load_destroy_state(void) {SnapLoadState *sn =snap_load_get_state (); +if (sn->has_rp_listen_thread) { +qemu_thread_join (&sn->rp_listen_thread); + } +if (sn->aio_pool) {aio_pool_free (sn->aio_pool); }
@@ -330,6 +334,7 @@ static int snap_load(SnapLoadParams *params) {SnapLoadState *sn;QIOChannel *ioc_fd; +QIOChannel *ioc_rp_fd;uint8_t *buf;size_t count;int res =-1 ;
@@ -338,11 +343,22 @@ static int snap_load(SnapLoadParams *params)snap_load_init_state (); sn =snap_load_get_state (); + sn->postcopy = params->postcopy; + sn->postcopy_percent = params->postcopy_percent; + ioc_fd =qio_channel_new_fd (params->fd,NULL );qio_channel_set_name (QIO_CHANNEL (ioc_fd),"snap-channel-outgoing" ); sn->f_fd =qemu_fopen_channel_output (ioc_fd);object_unref (OBJECT (ioc_fd)); +/* Open return path QEMUFile in case we shall use postcopy */ +if (params->postcopy) { + ioc_rp_fd =qio_channel_new_fd (params->rp_fd,NULL ); +qio_channel_set_name (QIO_CHANNEL (ioc_fd),"snap-channel-rp" ); + sn->f_rp_fd =qemu_fopen_channel_input (ioc_rp_fd); +object_unref (OBJECT (ioc_rp_fd)); + } + sn->blk =snap_open (params->filename, params->bdrv_flags);if (!sn->blk) { goto fail;
Signature · 1 line
2.25.1
Ping
Quoted 106 lines · Andrey Gruzdev
This series is a kind of PoC for asynchronous snapshot reverting. This is about external snapshots only and doesn't involve block devices. Thus, it's mainly intended to be used with the new 'background-snapshot' migration capability and otherwise standard QEMU migration mechanism.
The major ideas behind this first version were:
* Make it compatible with 'exec:'-style migration - options can be create some separate tool or integrate into qemu-system.
* Support asynchronous revert stage by using unaltered postcopy logic at destination. To do this, we should be capable of saving RAM pages so that any particular page can be directly addressed by it's block ID and page offset. Possible solutions here seem to be:
use separate index (and storing it somewhere)
create sparse file on host FS and address pages with file offset use QCOW2 (or other) image container with inherent sparsity support
* Make snapshot image file dense on the host FS so we don't depend on copy/backup tools and how they deal with sparse files. Off course, there's some performance cost for this choice.
* Make the code which is parsing unstructered format of migration stream, at least, not very sophisticated. Also, try to have minimum dependencies on QEMU migration code, both RAM and device.
* Try to keep page save latencies small while not degrading migration bandwidth too much.For this first version I decided not to integrate into main QEMU code but create a separate tool. The main reason is that there's not too much migration code that is target-specific and can be used in it's unmodified form. Also, it's still not very clear how to make 'qemu-system' integration in terms of command-line (or monitor/QMP?) interface extension.
For the storage format, QCOW2 as a container and large (1MB) cluster size seem to be an optimal choice. Larger cluster is beneficial for performance particularly in the case when image preallocation is disabled. Such cluster size does not result in too high internal fragmentation level (~10% of space waste in most cases) yet allows to reduce significantly the number of expensive cluster allocations.
A bit tricky part is dispatching QEMU migration stream cause it is mostly unstructered and depends on configuration parameters like 'send-configuration' and 'send-section-footer'. But, for the case with default values in migration globals it seems that implemented dispatching code works well and won't have compatibility issues in a reasonably long time frame.
I decided to keep RAM save path synchronous, anyhow it's better to use writeback cache mode for the live snapshots cause of it's interleaving page address pattern. Page coalescing buffer is used to merge contiguous pages to optimize block layer writes.
Since for snapshot loading opening image file in cached mode would not do any good, it implies that Linux native AIO and
O_DIRECTmode is used in a common scenario. AIO support in RAM loading path is implemented by using a ring of preallocated fixed-sized buffers in such a way that there's always a number of outstanding block requests anytime. It also ensures in-order request completion.How to use:
**Save:**
* qemu>migrate_set_capabilitybackground-snapshot on
* qemu> migrate "exec:<qemu-bin-path>/qemu-snap -s <virtual-size>--cache=writeback --aio=threads save <image-file.qcow2>"**Load:**
* Use 'qemu-system-* -incoming defer'
* qemu>migrate_incoming"exec:<qemu-bin-path>/qemu-snap--cache=none --aio=native load <image-file.qcow2>"**Load with postcopy:**
* Use 'qemu-system-* -incoming defer'
* qemu>migrate_set_capabilitypostcopy-ram on
* qemu>migrate_incoming"exec:<qemu-bin-path>/qemu-snap --postcopy=60--cache=none --aio=native load <image-file.qcow2>"And yes, asynchronous revert works well only with SSD, not with rotational disk..
Some performance stats:
* SATA SSD drive with ~500/450 MB/s sequantial read/write and ~60K IOPS max.
* 220 MB/s average save rate (depends on workload)
* 440 MB/s average load rate in precopy
* 260 MB/s average load rate in postcopyAndrey Gruzdev (9):
migration/snap-tool: Introduce qemu-snap tool migration/snap-tool: Snapshot image create/open routines for qemu-snap
tool
migration/snap-tool: Preparations to run code in main loop context migration/snap-tool: Introduceqemu_ftell2()routine toqemu-file.cmigration/snap-tool: Block layer AIO support and file utility routines migration/snap-tool: MoveRAM_SAVE_FLAG_xxxdefines tomigration/ram.hmigration/snap-tool: Complete implementation of snapshot saving migration/snap-tool: Implementation of snapshot loading in precopy migration/snap-tool: Implementation of snapshot loading in postcopyinclude/qemu-snap.h | 163 ++++ meson.build | 2 + migration/qemu-file.c | 6 + migration/qemu-file.h | 1 + migration/ram.c | 16 - migration/ram.h | 16 + qemu-snap-handlers.c | 1801 +++++++++++++++++++++++++++++++++++++++++ qemu-snap-io.c | 325 ++++++++ qemu-snap.c | 673 +++++++++++++++ 9 files changed, 2987 insertions(+), 16 deletions(-)create mode 100644
include/qemu-snap.h
create mode 100644qemu-snap-handlers.c
create mode 100644qemu-snap-io.c
create mode 100644qemu-snap.c
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.comQuoted 71 lines · Andrey Gruzdev
This series is a kind of PoC for asynchronous snapshot reverting. This is about external snapshots only and doesn't involve block devices. Thus, it's mainly intended to be used with the new 'background-snapshot' migration capability and otherwise standard QEMU migration mechanism.
The major ideas behind this first version were:
* Make it compatible with 'exec:'-style migration - options can be create some separate tool or integrate into qemu-system.
* Support asynchronous revert stage by using unaltered postcopy logic at destination. To do this, we should be capable of saving RAM pages so that any particular page can be directly addressed by it's block ID and page offset. Possible solutions here seem to be:
use separate index (and storing it somewhere)
create sparse file on host FS and address pages with file offset use QCOW2 (or other) image container with inherent sparsity support
* Make snapshot image file dense on the host FS so we don't depend on copy/backup tools and how they deal with sparse files. Off course, there's some performance cost for this choice.
* Make the code which is parsing unstructered format of migration stream, at least, not very sophisticated. Also, try to have minimum dependencies on QEMU migration code, both RAM and device.
* Try to keep page save latencies small while not degrading migration bandwidth too much.For this first version I decided not to integrate into main QEMU code but create a separate tool. The main reason is that there's not too much migration code that is target-specific and can be used in it's unmodified form. Also, it's still not very clear how to make 'qemu-system' integration in terms of command-line (or monitor/QMP?) interface extension.
For the storage format, QCOW2 as a container and large (1MB) cluster size seem to be an optimal choice. Larger cluster is beneficial for performance particularly in the case when image preallocation is disabled. Such cluster size does not result in too high internal fragmentation level (~10% of space waste in most cases) yet allows to reduce significantly the number of expensive cluster allocations.
A bit tricky part is dispatching QEMU migration stream cause it is mostly unstructered and depends on configuration parameters like 'send-configuration' and 'send-section-footer'. But, for the case with default values in migration globals it seems that implemented dispatching code works well and won't have compatibility issues in a reasonably long time frame.
I decided to keep RAM save path synchronous, anyhow it's better to use writeback cache mode for the live snapshots cause of it's interleaving page address pattern. Page coalescing buffer is used to merge contiguous pages to optimize block layer writes.
Since for snapshot loading opening image file in cached mode would not do any good, it implies that Linux native AIO and
O_DIRECTmode is used in a common scenario. AIO support in RAM loading path is implemented by using a ring of preallocated fixed-sized buffers in such a way that there's always a number of outstanding block requests anytime. It also ensures in-order request completion.How to use:
**Save:**
* qemu>migrate_set_capabilitybackground-snapshot on
* qemu> migrate "exec:<qemu-bin-path>/qemu-snap -s <virtual-size>--cache=writeback --aio=threads save <image-file.qcow2>"**Load:**
* Use 'qemu-system-* -incoming defer'
* qemu>migrate_incoming"exec:<qemu-bin-path>/qemu-snap--cache=none --aio=native load <image-file.qcow2>"**Load with postcopy:**
* Use 'qemu-system-* -incoming defer'
* qemu>migrate_set_capabilitypostcopy-ram on
* qemu>migrate_incoming"exec:<qemu-bin-path>/qemu-snap --postcopy=60--cache=none --aio=native load <image-file.qcow2>"
Quoted 7 lines
And yes, asynchronous revert works well only with SSD, not with rotational disk..
Some performance stats:
* SATA SSD drive with ~500/450 MB/s sequantial read/write and ~60K IOPS max.
* 220 MB/s average save rate (depends on workload)
* 440 MB/s average load rate in precopy
* 260 MB/s average load rate in postcopy
Andrey,
Before I try to read it (since I'm probably not the best person to review it..).. Would you remind me on the major difference of external snapshots comparing to the existing one, and problems to solve?
Thanks,
Signature · 1 line
Peter Xu
Quoted 79 lines · Peter Xu
Quoted 71 lines · Andrey Gruzdev
This series is a kind of PoC for asynchronous snapshot reverting. This is about external snapshots only and doesn't involve block devices. Thus, it's mainly intended to be used with the new 'background-snapshot' migration capability and otherwise standard QEMU migration mechanism.
The major ideas behind this first version were:
* Make it compatible with 'exec:'-style migration - options can be create some separate tool or integrate into qemu-system.
* Support asynchronous revert stage by using unaltered postcopy logic at destination. To do this, we should be capable of saving RAM pages so that any particular page can be directly addressed by it's block ID and page offset. Possible solutions here seem to be:
use separate index (and storing it somewhere)
create sparse file on host FS and address pages with file offset use QCOW2 (or other) image container with inherent sparsity support
* Make snapshot image file dense on the host FS so we don't depend on copy/backup tools and how they deal with sparse files. Off course, there's some performance cost for this choice.
* Make the code which is parsing unstructered format of migration stream, at least, not very sophisticated. Also, try to have minimum dependencies on QEMU migration code, both RAM and device.
* Try to keep page save latencies small while not degrading migration bandwidth too much.For this first version I decided not to integrate into main QEMU code but create a separate tool. The main reason is that there's not too much migration code that is target-specific and can be used in it's unmodified form. Also, it's still not very clear how to make 'qemu-system' integration in terms of command-line (or monitor/QMP?) interface extension.
For the storage format, QCOW2 as a container and large (1MB) cluster size seem to be an optimal choice. Larger cluster is beneficial for performance particularly in the case when image preallocation is disabled. Such cluster size does not result in too high internal fragmentation level (~10% of space waste in most cases) yet allows to reduce significantly the number of expensive cluster allocations.
A bit tricky part is dispatching QEMU migration stream cause it is mostly unstructered and depends on configuration parameters like 'send-configuration' and 'send-section-footer'. But, for the case with default values in migration globals it seems that implemented dispatching code works well and won't have compatibility issues in a reasonably long time frame.
I decided to keep RAM save path synchronous, anyhow it's better to use writeback cache mode for the live snapshots cause of it's interleaving page address pattern. Page coalescing buffer is used to merge contiguous pages to optimize block layer writes.
Since for snapshot loading opening image file in cached mode would not do any good, it implies that Linux native AIO and
O_DIRECTmode is used in a common scenario. AIO support in RAM loading path is implemented by using a ring of preallocated fixed-sized buffers in such a way that there's always a number of outstanding block requests anytime. It also ensures in-order request completion.How to use:
**Save:**
* qemu>migrate_set_capabilitybackground-snapshot on
* qemu> migrate "exec:<qemu-bin-path>/qemu-snap -s <virtual-size>--cache=writeback --aio=threads save <image-file.qcow2>"**Load:**
* Use 'qemu-system-* -incoming defer'
* qemu>migrate_incoming"exec:<qemu-bin-path>/qemu-snap--cache=none --aio=native load <image-file.qcow2>"**Load with postcopy:**
* Use 'qemu-system-* -incoming defer'
* qemu>migrate_set_capabilitypostcopy-ram on
* qemu>migrate_incoming"exec:<qemu-bin-path>/qemu-snap --postcopy=60--cache=none --aio=native load <image-file.qcow2>"Quoted 7 lines
And yes, asynchronous revert works well only with SSD, not with rotational disk..
Some performance stats:
* SATA SSD drive with ~500/450 MB/s sequantial read/write and ~60K IOPS max.
* 220 MB/s average save rate (depends on workload)
* 440 MB/s average load rate in precopy
* 260 MB/s average load rate in postcopy
Quoted 8 lines
Andrey,
Before I try to read it (since I'm probably not the best person to review it..).. Would you remind me on the major difference of external snapshots comparing to the existing one, and problems to solve?
Thanks,
Hi Peter,
For the external snapshots - the difference (compared to internal) is that snapshot data is going to storage objects which are not part VM config. I mean that for internal snapshots we use configured storage of the VM instance to store both vm state and blockdev snapshot data. The opposite is for external snapshots when we save vmstate and blockdev snapshots to separate files on the host. Also external snapshots are not managed by QEMU.
One of the problems is that the vmstate part of external snapshot is essentially the migration stream which is schema-less and it's structure is dependent on QEMU target. That means that currently we can do a revert-to-snapshot operation with the sequence of QMP commands but we can do that only in a synchronous way, i.e. vcpus can't be started until all of the vmstate data has been transferred. The reason for this synchronous behavior is that we cannot locate arbitrary RAM page in raw migration stream if we start vcpus early and get faults for the pages that are missing on destination vm.
So the major goal of this PoC is to demonstrate asynchronous snapshot reverting in QEMU while keeping migration code mostly unchanged. To do that we need to split migration stream into two parts, particularly these parts are RAM pages and the rest of vmstate. And then, if we can do this, RAM pages can be dispatched directly to a block device with block offsets deduced from page GPAs.
Signature · 3 lines
Andrey Gruzdev, Principal Engineer
Virtuozzo GmbH +7-903-247-6397
virtuzzo.com