Postcopy implementation
cover Postcopy implementation
From: "Dr. David Alan Gilbert" <[email protected]>
Hi,
This is the 1st cut of my version of postcopy; it is designed for use with
the Linux kernel additions recently posted by Andrea Arcangeli here:
http://lists.gnu.org/archive/html/qemu-devel/2014-07/msg00525.html
The current status is:
1) It works - I've done testing on large/busy VMs using google-stress app test, and it completes cleanly even over a 1Gbps link.
2) It's still rather rough around the corner cases and exit code, so it's probably not ready for review yet, but I thought I'd put it out for people working in the area and for people to see the general shape.
I've taken some ideas from Isaku Yamahata's work, but the code is structured a bit differently, however I'm greatful for the understanding I got from Isaku's code of the problems postcopy has to deal with.
Note, the last patch in the series is a chunk of documentation, which it's probably useful to read for detail.
Some points:
a) I've tried to keep the pieces I've added general rather than postcopy specific.
b) Precopy is done using the main precopy code (with very few modifications) and it automatically switches to postcopy after a timeout.
c) A 'command' section type is added that controls the progress of postcopy and can be used for anything else people need to control that's not actually guest state.
d) There is a 'return path' mechanism for sending data from the destination back to the source, postcopy uses this for the page requests, but I think it's also useful for things like the failover implementations, and can be used to signal failure from the destination to the source.
e) I've added a 'migration_set_parameter' command as somewhere to put integer parameters associated with migration.
e.1) And I use that initially for the length of precopy to try.
f) I've added a 'MigrationIncomingState' where postcopy keeps it's data, it seems a good idea to move other incoming state there as well.
c & d together provide a 'reqack' that's basically a ping, quite useful for debugging
Current TODO:
1) It's not bisectable yet
2) There are no testsuite additions (although I have a virt-test modification I've been using).
3) End-of-migration cleanup is unfinished, as is some of the error handling.
4) Not all the code is there for systems with hostpagesize!=qemupagesize
5) xbzrle needs disabling once in postcopy
6) RDMA needs some rework
7) I moved QEMUFile into a public header, it's a bad idea and I'll undo that.
8) My latency measurements on page requests are generally pretty good, but I'm seeing a few high spikes, I need to investigate.
9) Andrea has suggestions on ways to avoid some of the huge-page splitting that occurs during the discard phase after precopy.
10) I'd like to format the data on the return path in a more structured way (i.e. maybe using stuff from my BER world).
I'm hoping to get that lot cleared up by 2.2
How to use:
migrate_set_capability x-postcopy-ram on
migrate_set_parameter x-postcopy-start-time 500 <--- that's in ms
migrate -d tcp:whereever:port
Other ideas:
I think the returnpath+command could be used to send qmp commands to the destination to allow the src/destination to coordinate a hotplug or to set up the destination state.
Dave
Dr. David Alan Gilbert (46):
qemu_ram_foreach_block: pass up error value, and down the ramblock
name
Move QEMUFile structure to qemu-file.h QEMUSizedBuffer/QEMUFile
improve DPRINTF macros, add to savevm
Add qemu_get_counted_string to read a string prefixed by a count byte Create MigrationIncomingState
Return path: Open a return path on QEMUFile for sockets Return path: socket_writev_buffer: Block even on non-blocking fd's Migration commands
Return path: Control commands
Return path: Send responses from destination to source Return path: Source handling of return path
qemu_loadvm debug
ram_debug_dump_bitmap: Dump a migration bitmap as text Rework loadvm path for subloops
Add migration-capability boolean for postcopy-ram.
Add wrappers and handlers for sending/receiving the postcopy-ram
migration messages.
QEMU_VM_CMD_PACKAGED: Send a packaged chunk of migration stream
migrate_init: Call from savevm
Allow savevm handlers to state whether they could go into postcopy
postcopy: OS support test
Migration parameters: Add qmp/hmp commands for setting/viewing
MIG_STATE_POSTCOPY_ACTIVE: Add new migration state qemu_savevm_state_complete: Postcopy changes
Postcopy: Maintain sentmap during postcopy pre phase Postcopy page-map-incoming (PMI) structure
postcopy: Add incoming_init/cleanup functions
postcopy: Incoming initialisation
postcopy: ram_enable_notify to switch on userfault
Postcopy: postcopy_start
Postcopy: Rework migration thread for postcopy mode
mig fd_connect: open return path
Postcopy: Create a fault handler thread before marking the ram as
userfault
Page request: Add MIG_RPCOMM_REQPAGES reverse command Page request: Process incoming page request
Page request: Consume pages off the post-copy queue
Add assertion to check migration_dirty_pages doesn't go -ve; have seen
it happen once but not sure why
postcopy_ram.c: place_page and helpers
Postcopy: Use helpers to map pages during migration qemu_ram_block_from_host
Handle userfault requests (although userfaultfd not done yet) Start up a postcopy/listener thread ready for incoming page data
postcopy: Wire up loadvm_postcopy_ram_handle_{run,end} commands
postcopy: Use userfaultfd
End of migration for postcopy
Start documenting how postcopy works.
Makefile.objs | 2 +- arch_init.c | 477 ++++++++++++++++++++-- docs/migration.txt | 148 +++++++ exec.c | 60 ++- hmp-commands.hx | 17 + hmp.c | 54 +++ hmp.h | 4 + include/exec/cpu-common.h | 7 +- include/migration/migration.h | 115 ++++++ include/migration/postcopy-ram.h | 83 ++++ include/migration/qemu-file.h | 62 +++ include/migration/vmstate.h | 2 +- include/qemu/typedefs.h | 8 +- include/sysemu/sysemu.h | 41 +- migration-rdma.c | 4 +- migration.c | 695 ++++++++++++++++++++++++++++++-- monitor.c | 25 ++ postcopy-ram.c | 833 +++++++++++++++++++++++++++++++++++++++ qapi-schema.json | 56 ++- qemu-file.c | 578 +++++++++++++++++++++++++-- qmp-commands.hx | 23 ++ savevm.c | 822 +++++++++++++++++++++++++++++++++++--- 22 files changed, 3933 insertions(+), 183 deletions(-) create mode 100644 include/migration/postcopy-ram.h create mode 100644 postcopy-ram.c
Signature · 1 line
1.9.3
PATCH 2/46 Move QEMUFile structure to qemu-file.h
From: "Dr. David Alan Gilbert" <[email protected]>
This is mostly as an easy way to get to the MigrationIncomingState that I'm hanging off the file.
--- include/migration/qemu-file.h | 22 ++++++++++++++++++++++ qemu-file.c | 40 +++++++++------------------------------- 2 files changed, 31 insertions(+), 31 deletions(-)
include/migration/qemu-file.h +22 -0
@@ -82,6 +82,9 @@ typedef size_t (QEMURamSaveFunc)(QEMUFile *f, void *opaque,size_t size, int *bytes_sent); +#define QEMUFILE_IO_BUF_SIZE 32768 +#define QEMUFILE_MAX_IOV_SIZE MIN(IOV_MAX, 64) + typedef struct QEMUFileOps { QEMUFilePutBufferFunc *put_buffer; QEMUFileGetBufferFunc *get_buffer;
@@ -94,6 +97,25 @@ typedef struct QEMUFileOps {QEMURamSaveFunc *save_page; } QEMUFileOps; +struct QEMUFile { +const QEMUFileOps *ops; +void *opaque; + +int64_t bytes_xfer; +int64_t xfer_limit; + +int64_t pos;/* start of buffer when writing, end of buffer +when reading */ +int buf_index; +int buf_size;/* 0 when writing */ +uint8_t buf[QEMUFILE_IO_BUF_SIZE ]; + +struct iovec iov[QEMUFILE_MAX_IOV_SIZE ]; +unsigned int iovcnt; + +int last_error; +}; +QEMUFile *qemu_fopen_ops (void *opaque,const QEMUFileOps *ops);QEMUFile *qemu_fopen (const char *filename,const char *mode);QEMUFile *qemu_fdopen (int fd,const char *mode);
qemu-file.c +9 -31
@@ -6,28 +6,6 @@#include "migration/qemu-file.h" #include "trace.h" -#define IO_BUF_SIZE 32768 -#define MAX_IOV_SIZE MIN(IOV_MAX, 64) - -struct QEMUFile { -const QEMUFileOps *ops; -void *opaque; - -int64_t bytes_xfer; -int64_t xfer_limit; - -int64_t pos;/* start of buffer when writing, end of buffer -when reading */ -int buf_index; -int buf_size;/* 0 when writing */ -uint8_t buf[IO_BUF_SIZE ]; - -struct iovec iov[MAX_IOV_SIZE ]; -unsigned int iovcnt; - -int last_error; -}; - typedef struct QEMUFileStdio { FILE *stdio_file; QEMUFile *file;
@@ -553,7 +531,7 @@ static ssize_t qemu_fill_buffer(QEMUFile *f) f->buf_size = pending; len = f->ops->get_buffer (f->opaque, f->buf + pending, f->pos, -IO_BUF_SIZE - pending); +QEMUFILE_IO_BUF_SIZE - pending);if (len >0 ) { f->buf_size += len; f->pos += len;
@@ -621,7 +599,7 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size) f->iov[f->iovcnt++].iov_len = size; } -if (f->iovcnt >=MAX_IOV_SIZE ) { +if (f->iovcnt >=QEMUFILE_MAX_IOV_SIZE ) {qemu_fflush (f); } }
@@ -650,7 +628,7 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) }while (size >0 ) { - l =IO_BUF_SIZE - f->buf_index; + l =QEMUFILE_IO_BUF_SIZE - f->buf_index;if (l > size) { l = size; }
@@ -660,7 +638,7 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)add_to_iovec (f, f->buf + f->buf_index, l); } f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { +if (f->buf_index ==QEMUFILE_IO_BUF_SIZE ) {qemu_fflush (f); }if (qemu_file_get_error (f)) {
@@ -683,7 +661,7 @@ void qemu_put_byte(QEMUFile *f, int v)add_to_iovec (f, f->buf + f->buf_index,1 ); } f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { +if (f->buf_index ==QEMUFILE_IO_BUF_SIZE ) {qemu_fflush (f); } }
@@ -709,8 +687,8 @@ int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)int index;assert (!qemu_file_is_writable (f)); -assert (offset <IO_BUF_SIZE ); -assert (size <=IO_BUF_SIZE - offset); +assert (offset <QEMUFILE_IO_BUF_SIZE ); +assert (size <=QEMUFILE_IO_BUF_SIZE - offset);/* The 1st byte to read from */ index = f->buf_index + offset;
@@ -759,7 +737,7 @@ int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)while (pending >0 ) {int res; - res =qemu_peek_buffer (f, buf,MIN (pending,IO_BUF_SIZE ),0 ); + res =qemu_peek_buffer (f, buf,MIN (pending,QEMUFILE_IO_BUF_SIZE ),0 );if (res ==0 ) {return done; }
@@ -780,7 +758,7 @@ int qemu_peek_byte(QEMUFile *f, int offset)int index = f->buf_index + offset;assert (!qemu_file_is_writable (f)); -assert (offset <IO_BUF_SIZE ); +assert (offset <QEMUFILE_IO_BUF_SIZE );if (index >= f->buf_size) {qemu_fill_buffer (f);
Signature · 1 line
1.9.3
PATCH 1/46 qemu_ram_foreach_block: pass up error value, and down the ramblock name
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
check the return value of the function it calls and error if it's none-0 Fixup qemu_rdma_init_one_block that is the only current caller,
and __qemu_rdma_add_block the only function it calls using it.
Pass the name of the ramblock to the function; helps in debugging.
--- exec.c | 10 ++++++++-- include/exec/cpu-common.h | 4 ++-- migration-rdma.c | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-)
exec.c +8 -2
@@ -2786,12 +2786,18 @@ bool cpu_physical_memory_is_io(hwaddr phys_addr) memory_region_is_romd(mr)); } -void qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque) +int qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque) {RAMBlock *block; +int ret; QTAILQ_FOREACH(block, &ram_list.blocks, next) { -func (block->host, block->offset, block->length, opaque); + ret =func (block->idstr, block->host, block->offset, block->length, + opaque); +if (ret) { +return ret; + } } +return 0 ; }#endif
include/exec/cpu-common.h +2 -2
@@ -118,10 +118,10 @@ void cpu_flush_icache_range(hwaddr start, int len);extern struct MemoryRegion io_mem_rom;extern struct MemoryRegion io_mem_notdirty; -typedef void (RAMBlockIterFunc )(void *host_addr, +typedef int (RAMBlockIterFunc )(const char *block_name,void *host_addr,ram_addr_t offset,ram_addr_t length,void *opaque); -void qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque); +int qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque);#endif
migration-rdma.c +2 -2
@@ -595,10 +595,10 @@ static int __qemu_rdma_add_block(RDMAContext *rdma, void *host_addr,* in advanced before the migration starts. This tells us where the RAM blocks * are so that we can register them individually. */ -static void qemu_rdma_init_one_block (void *host_addr, +static int qemu_rdma_init_one_block (const char *block_name,void *host_addr,ram_addr_t block_offset,ram_addr_t length,void *opaque) { -__qemu_rdma_add_block (opaque, host_addr, block_offset, length); +return __qemu_rdma_add_block (opaque, host_addr, block_offset, length); }/*
Signature · 1 line
1.9.3
PATCH 3/46 QEMUSizedBuffer/QEMUFile
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Stefan Berger's patch to create a QEMUFile that goes to a memory buffer; from:
http://lists.gnu.org/archive/html/qemu-devel/2013-03/msg05036.html
Using the QEMUFile interface, this patch adds support functions for operating on in-memory sized buffers that can be written to or read from.
For minor tweeks/rebase I've done to it:
--- include/migration/qemu-file.h | 28 +++ include/qemu/typedefs.h | 1 + qemu-file.c | 410 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 439 insertions(+)
include/migration/qemu-file.h +28 -0
@@ -25,6 +25,8 @@#define QEMU_FILE_H 1#include "exec/cpu-common.h" +#include <stdint.h> +/* This function writes a chunk of data to a file at the given position. * The pos argument can be ignored if the file is only being used for * streaming. The handler should try to write all of the data it can.
@@ -116,11 +118,21 @@ struct QEMUFile {int last_error; }; +struct QEMUSizedBuffer { +struct iovec *iov; +size_t n_iov; +size_t size;/* total allocated size in all iov's */ +size_t used;/* number of used bytes */ +}; + +typedef struct QEMUSizedBuffer QEMUSizedBuffer ; +QEMUFile *qemu_fopen_ops (void *opaque,const QEMUFileOps *ops);QEMUFile *qemu_fopen (const char *filename,const char *mode);QEMUFile *qemu_fdopen (int fd,const char *mode);QEMUFile *qemu_fopen_socket (int fd,const char *mode);QEMUFile *qemu_popen_cmd (const char *command,const char *mode); +QEMUFile *qemu_bufopen (const char *mode,QEMUSizedBuffer *input);int qemu_get_fd (QEMUFile *f);int qemu_fclose (QEMUFile *f);int64_t qemu_ftell (QEMUFile *f);
@@ -133,6 +145,22 @@ void qemu_put_byte(QEMUFile *f, int v);void qemu_put_buffer_async (QEMUFile *f,const uint8_t *buf,int size);bool qemu_file_mode_is_not_valid (const char *mode); +QEMUSizedBuffer *qsb_create (const uint8_t *buffer,size_t len); +QEMUSizedBuffer *qsb_clone (const QEMUSizedBuffer *); +void qsb_free (QEMUSizedBuffer *); +size_t qsb_set_length (QEMUSizedBuffer *qsb,size_t length); +size_t qsb_get_length (const QEMUSizedBuffer *qsb); +ssize_t qsb_get_buffer (const QEMUSizedBuffer *,off_t start,size_t count, +uint8_t **buf); +ssize_t qsb_write_at (QEMUSizedBuffer *qsb,const uint8_t *buf, +off_t pos,size_t count); + + +/* +* For use on files opened with qemu_bufopen +*/ +const QEMUSizedBuffer *qemu_buf_get (QEMUFile *f); +static inline void qemu_put_ubyte (QEMUFile *f,unsigned int v) {qemu_put_byte (f, (int )v);
include/qemu/typedefs.h +1 -0
@@ -60,6 +60,7 @@ typedef struct PCIEAERLog PCIEAERLog;typedef struct PCIEAERErr PCIEAERErr ;typedef struct PCIEPort PCIEPort ;typedef struct PCIESlot PCIESlot ; +typedef struct QEMUSizedBuffer QEMUSizedBuffer ;typedef struct MSIMessage MSIMessage ;typedef struct SerialState SerialState ;typedef struct PCMCIACardState PCMCIACardState ;
qemu-file.c +410 -0
413 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 4/46 improve DPRINTF macros, add to savevm
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Improve the existing DPRINTF macros in migration.c and arch_init by:
1) Making them go to stderr rather than stdout (so you can run with
-nographic and redirect your debug to a file)
2) Making them print the ms time with each debug - useful for
debugging latency issues
Add the same macro to savevm
--- arch_init.c | 5 ++++- migration.c | 12 ++++++++++++ savevm.c | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-)
arch_init.c +4 -1
@@ -53,9 +53,12 @@#include "hw/acpi/acpi.h" #include "qemu/host-utils.h" +// #define DEBUG_ARCH_INIT #ifdef DEBUG_ARCH_INIT #define DPRINTF (fmt, ...) \ - do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0) + do { fprintf(stderr, "arch_init@%" PRId64 " " fmt "\n", \ + qemu_clock_get_ms(QEMU_CLOCK_REALTIME), \ + ## __VA_ARGS__); } while (0)#else #define DPRINTF (fmt, ...) \ do { } while (0)
migration.c +12 -0
@@ -26,6 +26,18 @@#include "qmp-commands.h" #include "trace.h" +//#define DEBUG_MIGRATION + +#ifdef DEBUG_MIGRATION +#define DPRINTF (fmt, ...) \ + do { fprintf(stderr, "migration@%" PRId64 " " fmt "\n", \ + qemu_clock_get_ms(QEMU_CLOCK_REALTIME), \ + ## __VA_ARGS__); } while (0) +#else +#define DPRINTF (fmt, ...) \ + do { } while (0) +#endif +enum {MIG_STATE_ERROR =-1 ,MIG_STATE_NONE ,
savevm.c +10 -0
@@ -43,6 +43,16 @@#include "block/snapshot.h" #include "block/qapi.h" +#ifdef DEBUG_SAVEVM +#define DPRINTF (fmt, ...) \ + do { fprintf(stderr, "savevm@%" PRId64 " " fmt "\n", \ + qemu_clock_get_ms(QEMU_CLOCK_REALTIME), \ + ## __VA_ARGS__); } while (0) +#else +#define DPRINTF (fmt, ...) \ + do { } while (0) +#endif +#ifndef ETH_P_RARP #define ETH_P_RARP 0x8035
Signature · 1 line
1.9.3
PATCH 5/46 Add qemu_get_counted_string to read a string prefixed by a count byte
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
and use it in loadvm_state.
--- include/migration/qemu-file.h | 2 ++ qemu-file.c | 15 +++++++++++++++ savevm.c | 18 ++++++++++-------- 3 files changed, 27 insertions(+), 8 deletions(-)
include/migration/qemu-file.h +2 -0
@@ -322,4 +322,6 @@ static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv) {qemu_get_be64s (f, (uint64_t *)pv); } + +int qemu_get_counted_string (QEMUFile *f,uint8_t *buf);#endif
qemu-file.c +15 -0
@@ -857,6 +857,21 @@ uint64_t qemu_get_be64(QEMUFile *f)return v; } +/* +* Get a string whose length is determined by a single preceding byte +* A preallocated 256 byte buffer must be passed in. +* Returns: 0 on success and a 0 terminated string in the buffer +*/ +int qemu_get_counted_string (QEMUFile *f,uint8_t *buf) +{ +unsigned int len =qemu_get_byte (f); +int res =qemu_get_buffer (f, buf, len); + + buf[len] =0 ; + +return res != len; +} +#define QSB_CHUNK_SIZE (1 << 10)#define QSB_MAX_CHUNK_SIZE (10 * QSB_CHUNK_SIZE)
savevm.c +10 -8
@@ -908,7 +908,7 @@ int qemu_loadvm_state(QEMUFile *f) v =qemu_get_be32 (f);if (v ==QEMU_VM_FILE_VERSION_COMPAT ) { -fprintf (stderr,"SaveVM v2 format is obsolete and don't work anymore\n" ); +error_report ("SaveVM v2 format is obsolete and don't work anymore" );return -ENOTSUP ; }if (v !=QEMU_VM_FILE_VERSION ) {
@@ -918,31 +918,33 @@ int qemu_loadvm_state(QEMUFile *f)while ((section_type =qemu_get_byte (f)) !=QEMU_VM_EOF ) {uint32_t instance_id, version_id, section_id;SaveStateEntry *se; -char idstr[257 ]; -int len; +char idstr[256 ];switch (section_type) {case QEMU_VM_SECTION_START :case QEMU_VM_SECTION_FULL :/* Read section start */ section_id =qemu_get_be32 (f); - len =qemu_get_byte (f); -qemu_get_buffer (f, (uint8_t *)idstr, len); - idstr[len] =0 ; +if (qemu_get_counted_string (f, (uint8_t *)idstr)) { +error_report ("Unable to read ID string for section %u" , + section_id); +return -EINVAL ; + } instance_id =qemu_get_be32 (f); version_id =qemu_get_be32 (f);/* Find savevm section */ se =find_se (idstr, instance_id);if (se ==NULL ) { -fprintf (stderr,"Unknown savevm section or instance '%s' %d\n" , idstr, instance_id); +error_report ("Unknown savevm section or instance '%s' %d" , + idstr, instance_id); ret = -EINVAL ; goto out; }/* Validate version */ if (version_id > se->version_id) { -fprintf (stderr,"savevm: unsupported version %d for '%s' v%d\n" , +error_report ("savevm: unsupported version %d for '%s' v%d" , version_id, idstr, se->version_id); ret = -EINVAL ; goto out;
Signature · 1 line
1.9.3
PATCH 6/46 Create MigrationIncomingState
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
There are currently lots of pieces of incoming migration state scattered around, and postcopy is adding more, and it seems better to try and keep it together.
allocate MIS in process_incoming_migration_co
Add MIS to QEMUFile
--- include/migration/migration.h | 8 ++++++++ include/migration/qemu-file.h | 2 ++ include/qemu/typedefs.h | 2 ++ migration.c | 22 ++++++++++++++++++++++ savevm.c | 2 ++ 5 files changed, 36 insertions(+)
include/migration/migration.h +8 -0
@@ -41,6 +41,14 @@ struct MigrationParams {typedef struct MigrationState MigrationState ; +/* State for the incoming migration */ +struct MigrationIncomingState { +QEMUFile *file; +}; + +MigrationIncomingState *migration_incoming_state_init (QEMUFile *f); +void migration_incoming_state_destroy (MigrationIncomingState *mis); +struct MigrationState {int64_t bandwidth_limit;
include/migration/qemu-file.h +2 -0
@@ -116,6 +116,8 @@ struct QEMUFile {unsigned int iovcnt;int last_error; + +MigrationIncomingState *mis; };struct QEMUSizedBuffer {
include/qemu/typedefs.h +2 -0
@@ -14,6 +14,7 @@ typedef struct Visitor Visitor;struct Monitor ;typedef struct Monitor Monitor ; +typedef struct MigrationIncomingState MigrationIncomingState ;typedef struct MigrationParams MigrationParams ;typedef struct Property Property ;
@@ -44,6 +45,7 @@ typedef struct PixelFormat PixelFormat;typedef struct QemuConsole QemuConsole ;typedef struct CharDriverState CharDriverState ;typedef struct MACAddr MACAddr ; +typedef struct MigrationIncomingState MigrationIncomingState ;typedef struct NetClientState NetClientState ;typedef struct I2CBus I2CBus ;typedef struct ISABus ISABus ;
migration.c +22 -0
@@ -100,15 +100,37 @@ void qemu_start_incoming_migration(const char *uri, Error **errp) } } +MigrationIncomingState *migration_incoming_state_init (QEMUFile * f) +{ +MigrationIncomingState *mis =g_malloc0 (sizeof (MigrationIncomingState)); + mis->file = f; + +return mis; +} + +void migration_incoming_state_destroy (MigrationIncomingState *mis) +{ +g_free (mis); +} +static void process_incoming_migration_co (void *opaque) {QEMUFile *f = opaque;Error *local_err =NULL ; +MigrationIncomingState *mis;int ret; + mis =migration_incoming_state_init (f); + f->mis = mis; + ret =qemu_loadvm_state (f); + + f->mis =NULL ;qemu_fclose (f);free_xbzrle_decoded_buf (); +migration_incoming_state_destroy (mis); + mis =NULL ; +if (ret <0 ) {error_report ("load of migration failed: %s" ,strerror (-ret));exit (EXIT_FAILURE );
savevm.c +2 -0
@@ -1244,9 +1244,11 @@ int load_vmstate(const char *name) }qemu_system_reset (VMRESET_SILENT ); + f->mis =migration_incoming_state_init (f); ret =qemu_loadvm_state (f);qemu_fclose (f); +migration_incoming_state_destroy (f->mis);if (ret <0 ) {error_report ("Error %d while loading VM state" , ret);return ret;
Signature · 1 line
1.9.3
PATCH 7/46 Return path: Open a return path on QEMUFile for sockets
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs a method to send messages from the destination back to the source, this is the 'return path'.
Wire it up for 'socket' QEMUFile's using a dup'd fd.
--- include/migration/qemu-file.h | 8 +++++ qemu-file.c | 74 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 6 deletions(-)
include/migration/qemu-file.h +8 -0
@@ -87,6 +87,11 @@ typedef size_t (QEMURamSaveFunc)(QEMUFile *f, void *opaque,#define QEMUFILE_IO_BUF_SIZE 32768#define QEMUFILE_MAX_IOV_SIZE MIN(IOV_MAX, 64) +/* +* Return a QEMUFile for comms in the opposite direction +*/ +typedef QEMUFile *(QEMURetPathFunc )(void *opaque); + typedef struct QEMUFileOps { QEMUFilePutBufferFunc *put_buffer; QEMUFileGetBufferFunc *get_buffer;
@@ -97,6 +102,7 @@ typedef struct QEMUFileOps {QEMURamHookFunc *after_ram_iterate;QEMURamHookFunc *hook_ram_load;QEMURamSaveFunc *save_page; +QEMURetPathFunc *get_return_path; } QEMUFileOps;struct QEMUFile {
@@ -117,6 +123,7 @@ struct QEMUFile {int last_error; +struct QEMUFile *return_path;MigrationIncomingState *mis; };
@@ -202,6 +209,7 @@ void qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate);int64_t qemu_file_get_rate_limit (QEMUFile *f);int qemu_file_get_error (QEMUFile *f);void qemu_file_set_error (QEMUFile *f,int ret); +QEMUFile *qemu_file_get_return_path (QEMUFile *f);void qemu_fflush (QEMUFile *f);static inline void qemu_put_be64s (QEMUFile *f,const uint64_t *pv)
qemu-file.c +68 -6
91 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 8/46 Return path: socket_writev_buffer: Block even on non-blocking fd's
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The return path uses a non-blocking fd so as not to block waiting for the (possibly broken) destination to finish returning a message, however we still want outbound data to behave in the same way and block.
--- qemu-file.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-)
qemu-file.c +35 -4
@@ -70,12 +70,43 @@ static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,QEMUFileSocket *s = opaque;ssize_t len;ssize_t size =iov_size (iov, iovcnt); +ssize_t offset =0 ; +int err; - len =iov_send (s->fd, iov, iovcnt,0 , size); -if (len < size) { - len = -socket_error (); +while (size >0 ) { + len =iov_send (s->fd, iov, iovcnt, offset, size); + +if (len >0 ) { + size -= len; + offset += len; + } + +if (size >0 ) { + err =socket_error (); + +if (err !=EAGAIN ) { +error_report ("socket_writev_buffer: Got err=%d for (%zd/%zd)" , + err, size, len); +/* +* If I've already sent some but only just got the error, I +* could return the amount validly sent so far and wait for the +* next call to report the error, but I'd rather flag the error +* immediately. +*/ +return -err; + } + +/* Emulate blocking */ +GPollFD pfd; + + pfd.fd = s->fd; + pfd.events =G_IO_OUT |G_IO_ERR ; + pfd.revents =0 ; +g_poll (&pfd,1 /* 1 fd */ ,-1 /* no timeout */ ); + } } -return len; + +return offset; }static int socket_get_fd (void *opaque)
Signature · 1 line
1.9.3
PATCH 9/46 Migration commands
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Create QEMU_VM_COMMAND section type for sending commands from source to destination. These commands are not intended to convey guest state but to control the migration process.
For use in postcopy.
--- include/migration/migration.h | 1 + include/sysemu/sysemu.h | 7 +++++ savevm.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+)
include/migration/migration.h +1 -0
@@ -33,6 +33,7 @@#define QEMU_VM_SECTION_END 0x03#define QEMU_VM_SECTION_FULL 0x04#define QEMU_VM_SUBSECTION 0x05 +#define QEMU_VM_COMMAND 0x06struct MigrationParams {bool blk;
include/sysemu/sysemu.h +7 -0
@@ -81,6 +81,13 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict);void qemu_announce_self (void ); +/* Subcommands for QEMU_VM_COMMAND */ +enum qemu_vm_cmd { +QEMU_VM_CMD_INVALID =0 ,/* Must be 0 */ + +QEMU_VM_CMD_AFTERLASTVALID +}; +bool qemu_savevm_state_blocked (Error **errp);void qemu_savevm_state_begin (QEMUFile *f,const MigrationParams *params);
savevm.c +61 -0
79 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 10/46 Return path: Control commands
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add two src->dest commands:
* OPENRP - To request that the destination open the return path
* REQACK - Request an acknowledge from the destination
--- include/migration/migration.h | 2 ++ include/sysemu/sysemu.h | 2 ++ savevm.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+)
include/migration/migration.h +2 -0
@@ -45,6 +45,8 @@ typedef struct MigrationState MigrationState;/* State for the incoming migration */ struct MigrationIncomingState {QEMUFile *file; + +QEMUFile *return_path; };MigrationIncomingState *migration_incoming_state_init (QEMUFile *f);
include/sysemu/sysemu.h +2 -0
@@ -84,6 +84,8 @@ void qemu_announce_self(void);/* Subcommands for QEMU_VM_COMMAND */ enum qemu_vm_cmd {QEMU_VM_CMD_INVALID =0 ,/* Must be 0 */ +QEMU_VM_CMD_OPENRP ,/* Tell the dest to open the Return path */ +QEMU_VM_CMD_REQACK ,/* Request an ACK on the RP */ QEMU_VM_CMD_AFTERLASTVALID };
savevm.c +43 -0
61 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 11/46 Return path: Send responses from destination to source
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add migrate_send_rp_message to send a message from destination to source along the return path.
(It uses a mutex to let it be called from multiple threads)
Add migrate_send_rp_ack to send an 'ack' message
Use it in the CMD_REQACK handler
--- include/migration/migration.h | 15 +++++++++++++++ migration.c | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)
include/migration/migration.h +15 -0
@@ -40,6 +40,12 @@ struct MigrationParams {bool shared; }; +/* Commands sent on the return path from destination to source*/ +enum mig_rpcomm_cmd { +MIG_RPCOMM_INVALID =0 ,/* Must be 0 */ +MIG_RPCOMM_ACK ,/* data (seq: be32 ) */ +MIG_RPCOMM_AFTERLASTVALID +};typedef struct MigrationState MigrationState ;/* State for the incoming migration */
@@ -47,6 +53,7 @@ struct MigrationIncomingState {QEMUFile *file;QEMUFile *return_path; +QemuMutex rp_mutex;/* We send replies from multiple threads */ };MigrationIncomingState *migration_incoming_state_init (QEMUFile *f);
@@ -167,6 +174,14 @@ int64_t migrate_xbzrle_cache_size(void);int64_t xbzrle_cache_resize (int64_t new_size); +/* Sending on the return path - generic and then for each message type */ +void migrate_send_rp_message (MigrationIncomingState *mis, +enum mig_rpcomm_cmd cmd, +uint16_t len,uint8_t *data); +void migrate_send_rp_ack (MigrationIncomingState *mis, +uint32_t value); + +void ram_control_before_iterate (QEMUFile *f,uint64_t flags);void ram_control_after_iterate (QEMUFile *f,uint64_t flags);void ram_control_load_hook (QEMUFile *f,uint64_t flags);
migration.c +27 -0
@@ -77,6 +77,32 @@ MigrationState *migrate_get_current(void)return ¤t_migration; } +/* Send a message on the return channel back to the source +* of the migration. +*/ +void migrate_send_rp_message (MigrationIncomingState *mis, +enum mig_rpcomm_cmd cmd, +uint16_t len,uint8_t *data) +{ +DPRINTF ("migrate_send_rp_message: cmd=%d, len=%d\n" , (int )cmd, len); +qemu_mutex_lock (&mis->rp_mutex); +qemu_put_be16 (mis->return_path, (unsigned int )cmd); +qemu_put_be16 (mis->return_path, len); +qemu_put_buffer (mis->return_path, data, len); +qemu_fflush (mis->return_path); +qemu_mutex_unlock (&mis->rp_mutex); +} + +/* Send an 'ACK' message on the return channel with the given value */ +void migrate_send_rp_ack (MigrationIncomingState *mis, +uint32_t value) +{ +uint32_t buf; + + buf =cpu_to_be32 (value); +migrate_send_rp_message (mis,MIG_RPCOMM_ACK ,4 , (uint8_t *)&buf); +} +void qemu_start_incoming_migration (const char *uri,Error **errp) {const char *p;
@@ -104,6 +130,7 @@ MigrationIncomingState *migration_incoming_state_init(QEMUFile* f) {MigrationIncomingState *mis =g_malloc0 (sizeof (MigrationIncomingState)); mis->file = f; +qemu_mutex_init (&mis->rp_mutex);return mis; }
Signature · 1 line
1.9.3
PATCH 12/46 Return path: Source handling of return path
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Open a return path, and handle messages that are received upon it.
--- include/migration/migration.h | 10 +++ migration.c | 142 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 151 insertions(+), 1 deletion(-)
include/migration/migration.h +10 -0
@@ -46,6 +46,14 @@ enum mig_rpcomm_cmd {MIG_RPCOMM_ACK ,/* data (seq: be32 ) */ MIG_RPCOMM_AFTERLASTVALID }; + +struct MigrationRetPathState { +uint16_t header_com;/* Headers of last (partially?) received command */ +uint16_t header_len; +uint32_t latest_ack; +bool error;/* True if something bad happened on the RP */ +}; +typedef struct MigrationState MigrationState ;/* State for the incoming migration */
@@ -67,9 +75,11 @@ struct MigrationStateQemuThread thread;QEMUBH *cleanup_bh;QEMUFile *file; +QEMUFile *return_path;int state;MigrationParams params; +struct MigrationRetPathState rp_state;double mbps;int64_t total_time;int64_t downtime;
migration.c +141 -1
161 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 14/46 ram_debug_dump_bitmap: Dump a migration bitmap as text
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Misses out lines that are all 0 so can be quite compact depending on the circumstance.
--- arch_init.c | 39 +++++++++++++++++++++++++++++++++++++++ include/migration/migration.h | 1 + 2 files changed, 40 insertions(+)
arch_init.c +39 -0
@@ -767,6 +767,45 @@ static void reset_ram_globals(void)#define MAX_WAIT 50/* ms, half buffered_file limit */ +/* +* 'expected' is the value you expect the bitmap mostly to be full +* of and it won't bother printing lines that are all this value +* if 'todump' is null the migration bitmap is dumped. +*/ +void ram_debug_dump_bitmap (unsigned long *todump,bool expected) +{ +int64_t ram_pages =last_ram_offset () >>TARGET_PAGE_BITS ; + +int64_t cur; +int64_t linelen =128l ; +char linebuf[129 ]; + +if (!todump) { + todump = migration_bitmap; + } + +for (cur =0 ; cur < ram_pages; cur += linelen) { +int64_t curb; +bool found = false; +/* +* Last line; catch the case where the line length +* is longer than remaining ram +*/ +if (cur+linelen > ram_pages) { + linelen = ram_pages - cur; + } +for (curb =0 ; curb < linelen; curb++) { +bool thisbit =test_bit (cur+curb, todump); + linebuf[curb] = thisbit ?'1' :'.' ; + found |= (thisbit ^ expected); + } +if (found) { + linebuf[curb] ='\0' ; +fprintf (stderr,"0x%08lx : %s\n" , cur, linebuf); + } + } +} +static int ram_save_setup (QEMUFile *f,void *opaque) {RAMBlock *block;
include/migration/migration.h +1 -0
@@ -155,6 +155,7 @@ uint64_t xbzrle_mig_pages_cache_miss(void);double xbzrle_mig_cache_miss_rate (void );void ram_handle_compressed (void *host,uint8_t ch,uint64_t size); +void ram_debug_dump_bitmap (unsigned long *todump,bool expected);/** * @migrate_add_blocker - prevent migration from proceeding
Signature · 1 line
1.9.3
PATCH 13/46 qemu_loadvm debug
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add lots of DPRINTF debug in qemu_loadvm*
--- savevm.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
savevm.c +17 -6
@@ -719,6 +719,8 @@ int qemu_savevm_state_iterate(QEMUFile *f)trace_savevm_section_end (se->idstr, se->section_id);if (ret <0 ) { +DPRINTF ("%s: setting error state after iterate on id=%d/%s" , + __func__, se->section_id, se->idstr);qemu_file_set_error (f, ret); }if (ret <=0 ) {
@@ -1018,6 +1020,7 @@ int qemu_loadvm_state(QEMUFile *f)SaveStateEntry *se;char idstr[256 ]; +DPRINTF ("qemu_loadvm_state loop: section_type=%d" , section_type);switch (section_type) {case QEMU_VM_SECTION_START :case QEMU_VM_SECTION_FULL :
@@ -1031,6 +1034,9 @@ int qemu_loadvm_state(QEMUFile *f) instance_id =qemu_get_be32 (f); version_id =qemu_get_be32 (f); +DPRINTF ("qemu_loadvm_state loop START/FULL: id=%d(%s)" , + section_id, idstr); +/* Find savevm section */ se =find_se (idstr, instance_id);if (se ==NULL ) {
@@ -1058,8 +1064,9 @@ int qemu_loadvm_state(QEMUFile *f) ret =vmstate_load (f, le->se, le->version_id);if (ret <0 ) { -fprintf (stderr,"qemu: warning: error while loading state for instance 0x%x of device '%s'\n" , - instance_id, idstr); +error_report ("qemu: error while loading state for" +"instance 0x%x of device '%s'" , + instance_id, idstr); goto out; }break ;
@@ -1067,23 +1074,25 @@ int qemu_loadvm_state(QEMUFile *f)case QEMU_VM_SECTION_END : section_id =qemu_get_be32 (f); +DPRINTF ("QEMU_VM_SECTION_PART/END entry for id=%d" , section_id); QLIST_FOREACH(le, &loadvm_handlers, entry) {if (le->section_id == section_id) {break ; } }if (le ==NULL ) { -fprintf (stderr,"Unknown savevm section %d\n" , section_id); +error_report ("Unknown savevm section %d" , section_id); ret = -EINVAL ; goto out; } ret =vmstate_load (f, le->se, le->version_id);if (ret <0 ) { -fprintf (stderr,"qemu: warning: error while loading state section id %d\n" , - section_id); +error_report ("qemu: error while loading state section" +" id %d (%s)" , section_id, le->se->idstr); goto out; } +DPRINTF ("QEMU_VM_SECTION_PART/END done for id=%d" , section_id);break ;case QEMU_VM_COMMAND : ret =loadvm_process_command (f);
@@ -1092,11 +1101,12 @@ int qemu_loadvm_state(QEMUFile *f) }break ;default : -fprintf (stderr,"Unknown savevm section type %d\n" , section_type); +error_report ("Unknown savevm section type %d" , section_type); ret = -EINVAL ; goto out; } } +DPRINTF ("qemu_loadvm_state loop: exited loop" );cpu_synchronize_all_post_init ();
@@ -1112,6 +1122,7 @@ out: ret =qemu_file_get_error (f); } +DPRINTF ("qemu_loadvm_state out: ret=%d" , ret);return ret; }
Signature · 1 line
1.9.3
PATCH 15/46 Rework loadvm path for subloops
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs to have two migration streams loading concurrently; one from memory (with the device state) and the other from the fd with the memory transactions.
Split the core of qemu_loadvm_state out so we can use it for both.
Allow the inner loadvm loop to quit and signal whether the parent should.
loadvm_handlers is made static since it's lifetime is greater than the outer qemu_loadvm_state.
--- savevm.c | 136 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 84 insertions(+), 52 deletions(-)
savevm.c +84 -52
@@ -915,6 +915,26 @@ static SaveStateEntry *find_se(const char *idstr, int instance_id)return NULL ; } +/* These are ORable flags */ +const int LOADVM_EXITCODE_QUITLOOP =1 ; +const int LOADVM_EXITCODE_QUITPARENT =2 ; +const int LOADVM_EXITCODE_KEEPHANDLERS =4 ; + +typedef struct LoadStateEntry { +QLIST_ENTRY (LoadStateEntry) entry; +SaveStateEntry *se; +int section_id; +int version_id; +}LoadStateEntry ; + +typedef QLIST_HEAD(, LoadStateEntry) LoadStateEntry_Head; + +static LoadStateEntry_Head loadvm_handlers = +QLIST_HEAD_INITIALIZER (loadvm_handlers); + +static int qemu_loadvm_state_main (QEMUFile *f, +LoadStateEntry_Head *loadvm_handlers); +static int loadvm_process_command_simple_lencheck (const char *name,unsigned int actual,unsigned int expected)
@@ -930,8 +950,11 @@ static int loadvm_process_command_simple_lencheck(const char *name,/* Process an incoming 'QEMU_VM_COMMAND' * -ve return on error (will issue error message) +* 0 just a normal return +* 1 All good, but exit the loop */ -static int loadvm_process_command (QEMUFile *f) +static int loadvm_process_command (QEMUFile *f, +LoadStateEntry_Head *loadvm_handlers) {MigrationIncomingState *mis = f->mis;uint16_t com;
@@ -981,39 +1004,13 @@ static int loadvm_process_command(QEMUFile *f)return 0 ; } -typedef struct LoadStateEntry { -QLIST_ENTRY (LoadStateEntry) entry; -SaveStateEntry *se; -int section_id; -int version_id; -}LoadStateEntry ; - -int qemu_loadvm_state (QEMUFile *f) +static int qemu_loadvm_state_main (QEMUFile *f, +LoadStateEntry_Head *loadvm_handlers) { - QLIST_HEAD(, LoadStateEntry) loadvm_handlers = -QLIST_HEAD_INITIALIZER (loadvm_handlers); -LoadStateEntry *le, *new_le; +LoadStateEntry *le;uint8_t section_type; -unsigned int v;int ret; - -if (qemu_savevm_state_blocked (NULL )) { -return -EINVAL ; - } - - v =qemu_get_be32 (f); -if (v !=QEMU_VM_FILE_MAGIC ) { -return -EINVAL ; - } - - v =qemu_get_be32 (f); -if (v ==QEMU_VM_FILE_VERSION_COMPAT ) { -error_report ("SaveVM v2 format is obsolete and don't work anymore" ); -return -ENOTSUP ; - } -if (v !=QEMU_VM_FILE_VERSION ) { -return -ENOTSUP ; - } +int exitcode =0 ;while ((section_type =qemu_get_byte (f)) !=QEMU_VM_EOF ) {uint32_t instance_id, version_id, section_id;
@@ -1042,16 +1039,14 @@ int qemu_loadvm_state(QEMUFile *f)if (se ==NULL ) {error_report ("Unknown savevm section or instance '%s' %d" , idstr, instance_id); - ret = -EINVAL ; - goto out; +return -EINVAL ; }/* Validate version */ if (version_id > se->version_id) {error_report ("savevm: unsupported version %d for '%s' v%d" , version_id, idstr, se->version_id); - ret = -EINVAL ; - goto out; +return -EINVAL ; }/* Add entry */
@@ -1060,14 +1055,14 @@ int qemu_loadvm_state(QEMUFile *f) le->se = se; le->section_id = section_id; le->version_id = version_id; -QLIST_INSERT_HEAD (&loadvm_handlers, le, entry); +QLIST_INSERT_HEAD (loadvm_handlers, le, entry); ret =vmstate_load (f, le->se, le->version_id);if (ret <0 ) {error_report ("qemu: error while loading state for" "instance 0x%x of device '%s'" , instance_id, idstr); - goto out; +return ret; }break ;case QEMU_VM_SECTION_PART :
@@ -1075,47 +1070,84 @@ int qemu_loadvm_state(QEMUFile *f) section_id =qemu_get_be32 (f);DPRINTF ("QEMU_VM_SECTION_PART/END entry for id=%d" , section_id); - QLIST_FOREACH(le, &loadvm_handlers, entry) { + QLIST_FOREACH(le, loadvm_handlers, entry) {if (le->section_id == section_id) {break ; } }if (le ==NULL ) {error_report ("Unknown savevm section %d" , section_id); - ret = -EINVAL ; - goto out; +return -EINVAL ; } ret =vmstate_load (f, le->se, le->version_id);if (ret <0 ) {error_report ("qemu: error while loading state section" " id %d (%s)" , section_id, le->se->idstr); - goto out; +return ret; }DPRINTF ("QEMU_VM_SECTION_PART/END done for id=%d" , section_id);break ;case QEMU_VM_COMMAND : - ret =loadvm_process_command (f); -if (ret <0 ) { - goto out; + ret =loadvm_process_command (f, loadvm_handlers); +DPRINTF ("%s QEMU_VM_COMMAND ret: %d" , __func__, ret); +if ((ret <0 ) || (ret &LOADVM_EXITCODE_QUITLOOP )) { +return ret; } + exitcode |= ret;/* Lets us pass flags up to the parent */ break ;default :error_report ("Unknown savevm section type %d" , section_type); - ret = -EINVAL ; - goto out; +return -EINVAL ; } }DPRINTF ("qemu_loadvm_state loop: exited loop" ); -cpu_synchronize_all_post_init (); +if (exitcode &LOADVM_EXITCODE_QUITPARENT ) { +DPRINTF ("loadvm_handlers_state_main: End of loop with QUITPARENT" ); + exitcode &= ~LOADVM_EXITCODE_QUITPARENT ; + exitcode &=LOADVM_EXITCODE_QUITLOOP ; + } + +return exitcode; +} + +int qemu_loadvm_state (QEMUFile *f) +{ +LoadStateEntry *le, *new_le; +unsigned int v; +int ret; + +if (qemu_savevm_state_blocked (NULL )) { +return -EINVAL ; + } + + v =qemu_get_be32 (f); +if (v !=QEMU_VM_FILE_MAGIC ) { +return -EINVAL ; + } - ret =0 ; + v =qemu_get_be32 (f); +if (v ==QEMU_VM_FILE_VERSION_COMPAT ) { +error_report ("SaveVM v2 format is obsolete and don't work anymore" ); +return -ENOTSUP ; + } +if (v !=QEMU_VM_FILE_VERSION ) { +return -ENOTSUP ; + } + +QLIST_INIT (&loadvm_handlers); + ret =qemu_loadvm_state_main (f, &loadvm_handlers); -out: - QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) { -QLIST_REMOVE (le, entry); -g_free (le); +if (ret ==0 ) { +cpu_synchronize_all_post_init (); + } + +if ((ret <0 ) || !(ret &LOADVM_EXITCODE_KEEPHANDLERS )) { + QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) { +QLIST_REMOVE (le, entry); +g_free (le); + } }if (ret ==0 ) {
Signature · 1 line
1.9.3
PATCH 17/46 Add wrappers and handlers for sending/receiving the postcopy-ram migration messages.
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add state variable showing current incoming postcopy state.
--- include/migration/migration.h | 8 ++ include/sysemu/sysemu.h | 23 ++++ savevm.c | 313 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 344 insertions(+)
include/migration/migration.h +8 -0
@@ -60,6 +60,14 @@ typedef struct MigrationState MigrationState;struct MigrationIncomingState {QEMUFile *file; +enum { +POSTCOPY_RAM_INCOMING_NONE =0 ,/* Initial state - no postcopy */ +POSTCOPY_RAM_INCOMING_ADVISE , +POSTCOPY_RAM_INCOMING_LISTENING , +POSTCOPY_RAM_INCOMING_RUNNING , +POSTCOPY_RAM_INCOMING_END + } postcopy_ram_state; +QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ };
include/sysemu/sysemu.h +23 -0
@@ -87,6 +87,16 @@ enum qemu_vm_cmd {QEMU_VM_CMD_OPENRP ,/* Tell the dest to open the Return path */ QEMU_VM_CMD_REQACK ,/* Request an ACK on the RP */ +QEMU_VM_CMD_POSTCOPY_RAM_ADVISE =20 ,/* Prior to any page transfers, just +warn we might want to do PC */ +QEMU_VM_CMD_POSTCOPY_RAM_DISCARD ,/* A list of pages to discard that +were previously sent during +precopy but are dirty. */ +QEMU_VM_CMD_POSTCOPY_RAM_LISTEN ,/* Start listening for incoming +pages as it's running. */ +QEMU_VM_CMD_POSTCOPY_RAM_RUN ,/* Start execution */ +QEMU_VM_CMD_POSTCOPY_RAM_END ,/* Postcopy is finished. */ + QEMU_VM_CMD_AFTERLASTVALID };
@@ -97,6 +107,19 @@ int qemu_savevm_state_iterate(QEMUFile *f);void qemu_savevm_state_complete (QEMUFile *f);void qemu_savevm_state_cancel (void );uint64_t qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size); +void qemu_savevm_command_send (QEMUFile *f,enum qemu_vm_cmd command, +uint16_t len,uint8_t *data); +void qemu_savevm_send_reqack (QEMUFile *f,uint32_t value); +void qemu_savevm_send_openrp (QEMUFile *f); +void qemu_savevm_send_postcopy_ram_advise (QEMUFile *f); +void qemu_savevm_send_postcopy_ram_discard (QEMUFile *f,const char *name, +uint16_t len,uint8_t offset, +uint64_t *pagelist); + +void qemu_savevm_send_postcopy_ram_listen (QEMUFile *f); +void qemu_savevm_send_postcopy_ram_run (QEMUFile *f); +void qemu_savevm_send_postcopy_ram_end (QEMUFile *f,uint8_t status); +int qemu_loadvm_state (QEMUFile *f);/* SLIRP */
savevm.c +313 -0
343 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 18/46 QEMU_VM_CMD_PACKAGED: Send a packaged chunk of migration stream
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
QEMU_VM_CMD_PACKAGED is a migration command that allows a chunk of migration stream to be sent in one go, and be received by a separate instance of the loadvm loop while not interacting with the migration stream.
This is used by postcopy to load device state (from the package) while loading memory pages from the main stream.
--- include/sysemu/sysemu.h | 4 +++ savevm.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)
include/sysemu/sysemu.h +4 -0
@@ -86,6 +86,7 @@ enum qemu_vm_cmd {QEMU_VM_CMD_INVALID =0 ,/* Must be 0 */ QEMU_VM_CMD_OPENRP ,/* Tell the dest to open the Return path */ QEMU_VM_CMD_REQACK ,/* Request an ACK on the RP */ +QEMU_VM_CMD_PACKAGED ,/* Send a wrapped stream within this stream */ QEMU_VM_CMD_POSTCOPY_RAM_ADVISE = 20, /* Prior to any page transfers, justwarn we might want to do PC */
@@ -100,6 +101,8 @@ enum qemu_vm_cmd { QEMU_VM_CMD_AFTERLASTVALID }; +#define MAX_VM_CMD_PACKAGED_SIZE (1ul << 24) +bool qemu_savevm_state_blocked (Error **errp);void qemu_savevm_state_begin (QEMUFile *f,const MigrationParams *params);
@@ -111,6 +114,7 @@ void qemu_savevm_command_send(QEMUFile *f, enum qemu_vm_cmd command, uint16_t len, uint8_t *data);void qemu_savevm_send_reqack (QEMUFile *f,uint32_t value);void qemu_savevm_send_openrp (QEMUFile *f); +void qemu_savevm_send_packaged (QEMUFile *f,const QEMUSizedBuffer *qsb);void qemu_savevm_send_postcopy_ram_advise (QEMUFile *f); void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name, uint16_t len, uint8_t offset,
savevm.c +80 -0
98 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 16/46 Add migration-capability boolean for postcopy-ram.
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- include/migration/migration.h | 1 + migration.c | 9 +++++++++ qapi-schema.json | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-)
include/migration/migration.h +1 -0
@@ -171,6 +171,7 @@ void migrate_add_blocker(Error *reason);*/ void migrate_del_blocker (Error *reason); +bool migrate_postcopy_ram (void );bool migrate_rdma_pin_all (void );bool migrate_zero_blocks (void );
migration.c +9 -0
@@ -612,6 +612,15 @@ bool migrate_rdma_pin_all(void)return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL ]; } +bool migrate_postcopy_ram (void ) +{ +MigrationState *s; + + s =migrate_get_current (); + +return s->enabled_capabilities[MIGRATION_CAPABILITY_X_POSTCOPY_RAM ]; +} +bool migrate_auto_converge (void ) {MigrationState *s;
qapi-schema.json +5 -1
@@ -491,10 +491,14 @@ # @auto-converge: If enabled, QEMU will automatically throttle down the guest # to speed up convergence of RAM migration. (since 1.6) # +# @x-postcopy-ram: Start executing on the migration target before all of RAM has been +# migrated, pulling the remaining pages along as needed. NOTE: If the +# migration fails during postcopy the VM will fail. (since 2.2) +# # Since: 1.2 ## { 'enum': 'MigrationCapability', - 'data': ['xbzrle', 'rdma-pin-all', 'auto-converge', 'zero-blocks'] } + 'data': ['xbzrle', 'rdma-pin-all', 'auto-converge', 'zero-blocks', 'x-postcopy-ram'] } ## # @MigrationCapabilityStatus
Signature · 1 line
1.9.3
PATCH 19/46 migrate_init: Call from savevm
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Suspend to file is very much like a migrate, and it makes life easier if we have the Migration state available, so initialise it in the savevm.c code for suspending.
--- include/migration/migration.h | 1 + include/qemu/typedefs.h | 1 + migration.c | 2 +- savevm.c | 2 ++ 4 files changed, 5 insertions(+), 1 deletion(-)
include/migration/migration.h +1 -0
@@ -138,6 +138,7 @@ int migrate_fd_close(MigrationState *s);void add_migration_state_change_notifier (Notifier *notify);void remove_migration_state_change_notifier (Notifier *notify); +MigrationState *migrate_init (const MigrationParams *params);bool migration_in_setup (MigrationState *);bool migration_has_finished (MigrationState *);bool migration_has_failed (MigrationState *);
include/qemu/typedefs.h +1 -0
@@ -16,6 +16,7 @@ struct Monitor;typedef struct Monitor Monitor ;typedef struct MigrationIncomingState MigrationIncomingState ;typedef struct MigrationParams MigrationParams ; +typedef struct MigrationState MigrationState ;typedef struct Property Property ;typedef struct PropertyInfo PropertyInfo ;
migration.c +1 -1
@@ -442,7 +442,7 @@ bool migration_has_failed(MigrationState *s) s->state == MIG_STATE_ERROR); } -static MigrationState *migrate_init (const MigrationParams *params) +MigrationState *migrate_init (const MigrationParams *params) {MigrationState *s =migrate_get_current ();int64_t bandwidth_limit = s->bandwidth_limit;
savevm.c +2 -0
@@ -941,6 +941,8 @@ static int qemu_savevm_state(QEMUFile *f) .blk = 0, .shared = 0 }; +MigrationState *ms =migrate_init (¶ms); + ms->file = f;if (qemu_savevm_state_blocked (NULL )) {return -EINVAL ;
Signature · 1 line
1.9.3
PATCH 20/46 Allow savevm handlers to state whether they could go into postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Use that to split the qemu_savevm_state_pending counts into postcopiable and non-postcopiable amounts
--- arch_init.c | 7 +++++++ include/migration/vmstate.h | 2 +- include/sysemu/sysemu.h | 4 +++- migration.c | 9 ++++++++- savevm.c | 23 +++++++++++++++++++---- 5 files changed, 38 insertions(+), 7 deletions(-)
arch_init.c +7 -0
@@ -1190,6 +1190,12 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)return ret; } +/* RAM's always up for postcopying */ +static bool ram_can_postcopy (void *opaque) +{ +return true; +} +static SaveVMHandlers savevm_ram_handlers = { .save_live_setup = ram_save_setup, .save_live_iterate = ram_save_iterate,
@@ -1197,6 +1203,7 @@ static SaveVMHandlers savevm_ram_handlers = { .save_live_pending = ram_save_pending, .load_state = ram_load, .cancel = ram_migration_cancel, + .can_postcopy = ram_can_postcopy, };void ram_mig_init (void )
include/migration/vmstate.h +1 -1
@@ -54,7 +54,7 @@ typedef struct SaveVMHandlers {/* This runs outside the iothread lock! */ int (*save_live_setup)(QEMUFile *f,void *opaque);uint64_t (*save_live_pending)(QEMUFile *f,void *opaque,uint64_t max_size); - +bool (*can_postcopy)(void *opaque);LoadStateHandler *load_state; } SaveVMHandlers;
include/sysemu/sysemu.h +3 -1
@@ -109,7 +109,9 @@ void qemu_savevm_state_begin(QEMUFile *f,int qemu_savevm_state_iterate (QEMUFile *f);void qemu_savevm_state_complete (QEMUFile *f);void qemu_savevm_state_cancel (void ); -uint64_t qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size); +void qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size, +uint64_t *res_non_postcopiable, +uint64_t *res_postcopiable);void qemu_savevm_command_send (QEMUFile *f,enum qemu_vm_cmd command,uint16_t len,uint8_t *data);void qemu_savevm_send_reqack (QEMUFile *f,uint32_t value);
migration.c +8 -1
@@ -806,8 +806,15 @@ static void *migration_thread(void *opaque)uint64_t pending_size;if (!qemu_file_rate_limit (s->file)) { - pending_size =qemu_savevm_state_pending (s->file, max_size); +uint64_t pend_post, pend_nonpost; +DPRINTF ("iterate\n" ); +qemu_savevm_state_pending (s->file, max_size, &pend_nonpost, + &pend_post); + pending_size = pend_nonpost + pend_post;trace_migrate_pending (pending_size, max_size); +DPRINTF ("pending size %" PRIu64" max %" PRIu64" (post=%" PRIu64 +" nonpost=%" PRIu64")\n" , + pending_size, max_size, pend_post, pend_nonpost);if (pending_size && pending_size >= max_size) {qemu_savevm_state_iterate (s->file); }else {
savevm.c +19 -4
@@ -903,10 +903,18 @@ void qemu_savevm_state_complete(QEMUFile *f)qemu_fflush (f); } -uint64_t qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size) +/* Give an estimate of the amount left to be transferred, +* the result is split into the amount for units that can and +* for units that can't do postcopy. +*/ +void qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size, +uint64_t *res_non_postcopiable, +uint64_t *res_postcopiable) {SaveStateEntry *se; -uint64_t ret =0 ; +uint64_t res_nonpc =0 ; +uint64_t res_pc =0 ; +uint64_t tmp; QTAILQ_FOREACH(se, &savevm_handlers, entry) {if (!se->ops || !se->ops->save_live_pending) {
@@ -917,9 +925,16 @@ uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)continue ; } } - ret += se->ops->save_live_pending (f, se->opaque, max_size); + tmp = se->ops->save_live_pending (f, se->opaque, max_size); + +if (se->ops->can_postcopy (se->opaque)) { + res_pc += tmp; + }else { + res_nonpc += tmp; + } } -return ret; + *res_non_postcopiable = res_nonpc; + *res_postcopiable = res_pc; }void qemu_savevm_state_cancel (void )
Signature · 1 line
1.9.3
PATCH 22/46 Migration parameters: Add qmp/hmp commands for setting/viewing
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add somewhere for the various migration parameters to be set with one command;
As suggested in the thread:
http://lists.gnu.org/archive/html/qemu-devel/2012-11/msg00243.html
There are many existing migration parameters that are scattered over many individual commands; moving those to this scheme would probably break things for others, so I've left them be.
Preserve the migration tunable values across the reinit of the migration status in the same way that the capability flags are preserved.
Add completion routine for it.
Use the postcopy time out setting as the first parameter.
--- hmp-commands.hx | 17 ++++++++++ hmp.c | 54 ++++++++++++++++++++++++++++++ hmp.h | 4 +++ include/migration/migration.h | 4 +++ migration.c | 78 ++++++++++++++++++++++++++++++++++--------- monitor.c | 25 ++++++++++++++ qapi-schema.json | 50 +++++++++++++++++++++++++++ qmp-commands.hx | 23 +++++++++++++ 8 files changed, 239 insertions(+), 16 deletions(-)
hmp-commands.hx +17 -0
@@ -987,6 +987,21 @@ Enable/Disable the usage of a capability @var{capability} for migration. ETEXI { + .name = "migrate_set_parameter", + .args_type = "parameter:s,value:l", + .params = "parameter value", + .help = "Change the value of the given parameter", + .mhandler.cmd = hmp_migrate_set_parameter, + .command_completion = migrate_set_parameter_completion, + }, + +STEXI +@item migrate_set_parameter @var{parameter} @var{value} +@findex migrate_set_parameter +Change the value of a migration parameter @var{parameter}. +ETEXI + + { .name = "client_migrate_info", .args_type = "protocol:s,hostname:s,port:i?,tls-port:i?,cert-subject:s?", .params = "protocol hostname port tls-port cert-subject",
@@ -1770,6 +1785,8 @@ show migration status show current migration capabilities @item info migrate_cache_size show current migration XBZRLE cache size +@item info migrate_parameters +show current migration parameters @item info balloon show balloon information @item info qtree
hmp.c +54 -0
72 lines not on this page · raw message
hmp.h +4 -0
@@ -29,6 +29,7 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict);void hmp_info_migrate (Monitor *mon,const QDict *qdict);void hmp_info_migrate_capabilities (Monitor *mon,const QDict *qdict);void hmp_info_migrate_cache_size (Monitor *mon,const QDict *qdict); +void hmp_info_migrate_parameters (Monitor *mon,const QDict *qdict);void hmp_info_cpus (Monitor *mon,const QDict *qdict);void hmp_info_block (Monitor *mon,const QDict *qdict);void hmp_info_blockstats (Monitor *mon,const QDict *qdict);
@@ -64,6 +65,7 @@ void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict);void hmp_migrate_set_speed (Monitor *mon,const QDict *qdict);void hmp_migrate_set_capability (Monitor *mon,const QDict *qdict);void hmp_migrate_set_cache_size (Monitor *mon,const QDict *qdict); +void hmp_migrate_set_parameter (Monitor *mon,const QDict *qdict);void hmp_set_password (Monitor *mon,const QDict *qdict);void hmp_expire_password (Monitor *mon,const QDict *qdict);void hmp_eject (Monitor *mon,const QDict *qdict);
@@ -110,6 +112,8 @@ void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str);void migrate_set_capability_completion (ReadLineState *rs,int nb_args,const char *str); +void migrate_set_parameter_completion (ReadLineState *rs,int nb_args, +const char *str);void host_net_add_completion (ReadLineState *rs,int nb_args,const char *str);void host_net_remove_completion (ReadLineState *rs,int nb_args,const char *str);
include/migration/migration.h +4 -0
@@ -35,6 +35,7 @@#define QEMU_VM_SUBSECTION 0x05#define QEMU_VM_COMMAND 0x06 +/* This is state currently only used by block */ struct MigrationParams {bool blk;bool shared;
@@ -75,6 +76,7 @@ struct MigrationIncomingState {MigrationIncomingState *migration_incoming_state_init (QEMUFile *f);void migration_incoming_state_destroy (MigrationIncomingState *mis); +/* State for the outgoing migration */ struct MigrationState {int64_t bandwidth_limit;
@@ -95,6 +97,8 @@ struct MigrationStateint64_t dirty_pages_rate;int64_t dirty_bytes_rate;bool enabled_capabilities[MIGRATION_CAPABILITY_MAX ]; +/* For migrate_set_parameters command */ +int64_t tunables[MIGRATION_PARAMETER_NAME_MAX ];int64_t xbzrle_cache_size;int64_t setup_time;int64_t dirty_sync_count;
migration.c +62 -16
105 lines not on this page · raw message
monitor.c +25 -0
@@ -2869,6 +2869,13 @@ static mon_cmd_t info_cmds[] = { .mhandler.cmd = hmp_info_migrate_cache_size, }, { + .name = "migrate_parameters", + .args_type = "", + .params = "", + .help = "show current migration parameters", + .mhandler.cmd = hmp_info_migrate_parameters, + }, + { .name = "balloon", .args_type = "", .params = "",
@@ -4553,6 +4560,24 @@ void migrate_set_capability_completion(ReadLineState *rs, int nb_args, } } +void migrate_set_parameter_completion (ReadLineState *rs,int nb_args, +const char *str) +{ +size_t len; + + len =strlen (str); +readline_set_completion_index (rs, len); +if (nb_args ==2 ) { +int i; +for (i =0 ; i <MIGRATION_PARAMETER_NAME_MAX ; i++) { +const char *name = MigrationParameterName_lookup[i]; +if (!strncmp (str, name, len)) { +readline_add_completion (rs, name); + } + } + } +} +void host_net_add_completion (ReadLineState *rs,int nb_args,const char *str) {int i;
qapi-schema.json +50 -0
56 lines not on this page · raw message
qmp-commands.hx +23 -0
@@ -3202,6 +3202,29 @@ EQMP .mhandler.cmd_new = qmp_marshal_input_migrate_set_capabilities, }, SQMP +migrate-set-parameters +------------------------ + +Set migration parameters + +- "x-postcopy-start-time": Postcopy start time + +Arguments: + +Example: + +-> { "execute": "migrate-set-parameters" , "arguments": + { "parameters": [ { "parameter": "x-postcopy-start-time", "value": 30 } ] } } + +EQMP + + { + .name = "migrate-set-parameters", + .args_type = "parameters:O", + .params = "parameter:s,value:i", + .mhandler.cmd_new = qmp_marshal_input_migrate_set_parameters, + }, +SQMP query-migrate-capabilities --------------------------
Signature · 1 line
1.9.3
PATCH 21/46 postcopy: OS support test
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Provide a check to see if the OS we're running on has all the bits needed for postcopy.
Creates postcopy-ram.c which will get most of the other helpers we need.
--- Makefile.objs | 2 +- include/migration/postcopy-ram.h | 19 ++++++ postcopy-ram.c | 129 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 include/migration/postcopy-ram.h create mode 100644 postcopy-ram.c
Makefile.objs +1 -1
@@ -55,7 +55,7 @@ common-obj-y += qemu-file.o common-obj-$(CONFIG_RDMA) += migration-rdma.o common-obj-y += qemu-char.o #aio.o common-obj-y += block-migration.o -common-obj-y += page_cache.o xbzrle.o +common-obj-y += page_cache.o xbzrle.o postcopy-ram.o common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
include/migration/postcopy-ram.h new file +19 -0
@@ -0,0 +1,19 @@ +/* +* Postcopy migration for RAM +* +* Copyright 2013 Red Hat, Inc. and/or its affiliates +* +* Authors: +* Dave Gilbert <[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_POSTCOPY_RAM_H +#define QEMU_POSTCOPY_RAM_H + +/* Return 0 if the host supports everything we need to do postcopy-ram */ +int postcopy_ram_hosttest (void ); + +#endif
postcopy-ram.c new file +129 -0
129 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 23/46 MIG_STATE_POSTCOPY_ACTIVE: Add new migration state
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
'MIG_STATE_POSTCOPY_ACTIVE' is entered after the precopy timelimit has expired and migration switches to postcopy.
'migration_postcopy_phase' is provided for other sections to know if they're in postcopy.
--- include/migration/migration.h | 2 ++ migration.c | 76 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 7 deletions(-)
include/migration/migration.h +2 -0
@@ -146,6 +146,8 @@ MigrationState *migrate_init(const MigrationParams *params);bool migration_in_setup (MigrationState *);bool migration_has_finished (MigrationState *);bool migration_has_failed (MigrationState *); +/* True if outgoing migration has entered postcopy phase */ +bool migration_postcopy_phase (MigrationState *);MigrationState *migrate_get_current (void );uint64_t ram_bytes_remaining (void );
migration.c +69 -7
142 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 24/46 qemu_savevm_state_complete: Postcopy changes
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
When postcopy calls qemu_savevm_state_complete it's not really the end of migration, so skip:
a) Finishing postcopiable iterative devices - they'll carry on
b) The termination byte on the end of the stream.
We then also add:
qemu_savevm_state_postcopy_complete
which is called at the end of a postcopy migration to call the complete methods on devices skipped in the _complete call.
--- include/sysemu/sysemu.h | 1 + savevm.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-)
include/sysemu/sysemu.h +1 -0
@@ -112,6 +112,7 @@ void qemu_savevm_state_cancel(void);void qemu_savevm_state_pending (QEMUFile *f,uint64_t max_size,uint64_t *res_non_postcopiable,uint64_t *res_postcopiable); +void qemu_savevm_state_postcopy_complete (QEMUFile *f);void qemu_savevm_command_send (QEMUFile *f,enum qemu_vm_cmd command,uint16_t len,uint8_t *data);void qemu_savevm_send_reqack (QEMUFile *f,uint32_t value);
savevm.c +51 -1
74 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 25/46 Postcopy: Maintain sentmap during postcopy pre phase
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Where postcopy is preceeded by a period of precopy, the destination will have received pages that may have been dirtied on the source after the page was sent. The destination must throw these pages away before starting it's CPUs.
Maintain a 'sentmap' of pages that have already been sent. Calculate list of sent & dirty pages
Provide helpers on the destination side to discard these.
--- arch_init.c | 162 ++++++++++++++++++++++++++++++++++++++- include/migration/migration.h | 5 ++ include/migration/postcopy-ram.h | 20 +++++ migration.c | 2 + postcopy-ram.c | 156 +++++++++++++++++++++++++++++++++++++ savevm.c | 3 - 6 files changed, 342 insertions(+), 6 deletions(-)
arch_init.c +159 -3
@@ -40,6 +40,7 @@#include "hw/audio/audio.h" #include "sysemu/kvm.h" #include "migration/migration.h" +#include "migration/postcopy-ram.h" #include "hw/i386/smbios.h" #include "exec/address-spaces.h" #include "hw/audio/pcspk.h"
@@ -413,9 +414,15 @@ static int save_xbzrle_page(QEMUFile *f, uint8_t **current_data,return bytes_sent; } +/* mr: The region to search for dirty pages in +* start: Start address (typically so we can continue from previous page) +* bitoffset: Pointer into which to store the offset into the dirty map +* at which the bit was found. +*/ static inline ram_addr_t migration_bitmap_find_and_reset_dirty (MemoryRegion *mr, -ram_addr_t start) +ram_addr_t start, +unsigned long *bitoffset) {unsigned long base = mr->ram_addr >>TARGET_PAGE_BITS ;unsigned long nr = base + (start >>TARGET_PAGE_BITS );
@@ -434,6 +441,7 @@ ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,clear_bit (next, migration_bitmap); migration_dirty_pages--; } + *bitoffset = next;return (next - base) <<TARGET_PAGE_BITS ; }
@@ -562,6 +570,19 @@ static void migration_bitmap_sync(void) } } +static RAMBlock *ram_find_block (const char *id) +{ +RAMBlock *block; + + QTAILQ_FOREACH(block, &ram_list.blocks, next) { +if (!strcmp (id, block->idstr)) { +return block; + } + } + +return NULL ; +} +/* * ram_save_page: Send the given page to the stream *
@@ -650,13 +671,14 @@ static int ram_find_and_save_block(QEMUFile *f, bool last_stage)bool complete_round = false;int bytes_sent =0 ;MemoryRegion *mr; +unsigned long bitoffset;if (!block) block =QTAILQ_FIRST (&ram_list.blocks);while (true) { mr = block->mr; - offset =migration_bitmap_find_and_reset_dirty (mr, offset); + offset =migration_bitmap_find_and_reset_dirty (mr, offset, &bitoffset);if (complete_round && block == last_seen_block && offset >= last_offset) {break ;
@@ -674,6 +696,11 @@ static int ram_find_and_save_block(QEMUFile *f, bool last_stage)/* if page is unmodified, continue to the next */ if (bytes_sent >0 ) { +MigrationState *s =migrate_get_current (); +if (s->sentmap) { +set_bit (bitoffset, s->sentmap); + } + last_sent_block = block;break ; }
@@ -733,12 +760,19 @@ void free_xbzrle_decoded_buf(void)static void migration_end (void ) { +MigrationState *s =migrate_get_current (); +if (migration_bitmap) {memory_global_dirty_log_stop ();g_free (migration_bitmap); migration_bitmap =NULL ; } +if (s->sentmap) { +g_free (s->sentmap); + s->sentmap =NULL ; + } +XBZRLE_cache_lock ();if (XBZRLE .cache) {cache_fini (XBZRLE .cache);
@@ -806,6 +840,123 @@ void ram_debug_dump_bitmap(unsigned long *todump, bool expected) } } +/* +* Utility for the outgoing postcopy code; this performs +* sentmap &= migration_bitmap +* returning the length of the bitmap +*/ +int64_t ram_mask_postcopy_bitmap (MigrationState *ms) +{ +int64_t ram_pages =last_ram_offset () >>TARGET_PAGE_BITS ; + +migration_bitmap_sync (); +bitmap_and (ms->sentmap, ms->sentmap, migration_bitmap, ram_pages); +return ram_pages; +} + +/* +* Utility for the outgoing postcopy code. +* Calls postcopy_send_discard_bm_ram for each RAMBlock +* passing it bitmap indexes and name. +* Returns: 0 on success +* (qemu_ram_foreach_block ends up passing unscaled lengths +* which would mean postcopy code would have to deal with target page) +*/ +int ram_postcopy_each_ram_discard (MigrationState *ms) +{ +struct RAMBlock *block; +int ret; + + QTAILQ_FOREACH(block, &ram_list.blocks, next) { +/* +* Postcopy sends chunks of bitmap over the wire, but it +* just needs indexes at this point, avoids it having +* target page specific code. +*/ +unsigned long first, last; + first = block->offset >>TARGET_PAGE_BITS ; + last = (block->offset + (block->length-1 )) >>TARGET_PAGE_BITS ; + ret =postcopy_send_discard_bm_ram (ms, block->idstr, first, last); +if (ret) { +return ret; + } + } + +return 0 ; +} + +/* +* At the start of the postcopy phase of migration, any now-dirty +* precopied pages are discarded. +* +* start..end is an inclusive range of bits indexed in the source +* VMs bitmap for this RAMBlock, source_target_page_bits tells +* us what one of those bits represents. +* +* start/end are offsets from the start of the bitmap for RAMBlock 'block_name' +* +* Returns 0 on success. +*/ +int ram_discard_range (MigrationIncomingState *mis, +const char *block_name, +int source_target_page_bits, +uint64_t start,uint64_t end) +{ +assert (end >= start); +unsigned int bitdif; + +RAMBlock *rb =ram_find_block (block_name); + +if (!rb) { +error_report ("ram_discard_range: Failed to find block '%s'" , + block_name); +return -1 ; + } + +if (source_target_page_bits !=TARGET_PAGE_BITS ) { +if (source_target_page_bits <TARGET_PAGE_BITS ) { +/* +* e.g. source is 4K and we're 64k - we'll have to discard +* on the larger boundary +* e.g. a range of 70K...132K we would discard from +* 64K..192K, so round start down, and end up +*/ + bitdif =TARGET_PAGE_BITS - source_target_page_bits; + start = start >> bitdif; +if (end & ((1 <<bitdif)-1 )) { + end = end >> bitdif; + end++; + }else { + end = end >> bitdif; + } + + }else { +/* e.g. source is 64K and we're 4K - easy just scale the indexes */ + bitdif = source_target_page_bits -TARGET_PAGE_BITS ; + + start = start << bitdif; + end = end << bitdif; + } + } + +uint64_t index_offset = rb->offset >>TARGET_PAGE_BITS ; +postcopy_pmi_discard_range (mis, start + index_offset, (end - start) +1 ); + +/* +1 gives the byte after the end of the last page to be discarded */ +ram_addr_t end_offset = (end+1 ) <<TARGET_PAGE_BITS ; +uint8_t *host_startaddr = rb->host + (start <<TARGET_PAGE_BITS ); +uint8_t *host_endaddr; + +if (end_offset <= rb->length) { + host_endaddr = rb->host + (end_offset-1 ); +return postcopy_ram_discard_range (mis, host_startaddr, host_endaddr); + }else { +error_report ("ram_discard_range: Overrun block '%s' (%zu/%zu/%zu)" , + block_name, start, end, rb->length); +return -1 ; + } +} +static int ram_save_setup (QEMUFile *f,void *opaque) {RAMBlock *block;
@@ -844,7 +995,6 @@ static int ram_save_setup(QEMUFile *f, void *opaque)acct_clear (); } -qemu_mutex_lock_iothread ();qemu_mutex_lock_ramlist (); bytes_transferred =0 ;
@@ -854,6 +1004,12 @@ static int ram_save_setup(QEMUFile *f, void *opaque) migration_bitmap =bitmap_new (ram_bitmap_pages);bitmap_set (migration_bitmap,0 , ram_bitmap_pages); +if (migrate_postcopy_ram ()) { +MigrationState *s =migrate_get_current (); + s->sentmap =bitmap_new (ram_bitmap_pages); +bitmap_clear (s->sentmap,0 , ram_bitmap_pages); + } +/* * Count the total number of pages used by ram blocks not including any * gaps due to alignment or unplugs.
include/migration/migration.h +5 -0
@@ -171,6 +171,11 @@ double xbzrle_mig_cache_miss_rate(void);void ram_handle_compressed (void *host,uint8_t ch,uint64_t size);void ram_debug_dump_bitmap (unsigned long *todump,bool expected); +int64_t ram_mask_postcopy_bitmap (MigrationState *ms); +int ram_postcopy_each_ram_discard (MigrationState *ms); +int ram_discard_range (MigrationIncomingState *mis,const char *block_name, +int source_target_page_bits, +uint64_t start,uint64_t end);/** * @migrate_add_blocker - prevent migration from proceeding
include/migration/postcopy-ram.h +20 -0
@@ -13,7 +13,27 @@#ifndef QEMU_POSTCOPY_RAM_H #define QEMU_POSTCOPY_RAM_H +#include "migration/migration.h" +/* Return 0 if the host supports everything we need to do postcopy-ram */ int postcopy_ram_hosttest (void ); +/* Send the list of sent-but-dirty pages */ +int postcopy_send_discard_bitmap (MigrationState *ms); + +/* +* Discard the contents of memory start..end inclusive. +* We can assume that if we've been called postcopy_ram_hosttest returned true +*/ +int postcopy_ram_discard_range (MigrationIncomingState *mis,uint8_t *start, +uint8_t *end); + + +/* +* Called back from arch_init's ram_postcopy_each_ram_discard to handle +* discarding one RAMBlock's pre-postcopy dirty pages +*/ +int postcopy_send_discard_bm_ram (MigrationState *ms,const char *name, +unsigned long start,unsigned long end); +#endif
migration.c +2 -0
@@ -22,6 +22,7 @@#include "block/block.h" #include "qemu/sockets.h" #include "migration/block.h" +#include "migration/postcopy-ram.h" #include "qemu/thread.h" #include "qmp-commands.h" #include "trace.h"
@@ -928,6 +929,7 @@ static void *migration_thread(void *opaque) }else {int ret; +DPRINTF ("done iterating\n" );qemu_mutex_lock_iothread (); start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER );
postcopy-ram.c +156 -0
173 lines not on this page · raw message
savevm.c +0 -3
@@ -1238,12 +1238,9 @@ static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,* we know there must be at least 1 bit set due to the loop entry * If there is no 0 firstzero will be 64 */ -/* TODO - ram_discard_range gets added in a later patch int ret =ram_discard_range (mis, ramid, source_target_page_bits, startaddr + firstset - first_bit_offset, startaddr + (firstzero -1 ) - first_bit_offset); -*/ - ret =-1 ;/* TODO */ if (ret) {return ret; }
Signature · 1 line
1.9.3
PATCH 26/46 Postcopy page-map-incoming (PMI) structure
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The PMI holds the state of each page on the incoming side, so that we can tell if the page is missing, already received or there is a request outstanding for it.
--- include/migration/migration.h | 18 ++++++ include/migration/postcopy-ram.h | 4 ++ include/qemu/typedefs.h | 1 + postcopy-ram.c | 118 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+)
include/migration/migration.h +18 -0
@@ -57,6 +57,23 @@ struct MigrationRetPathState {typedef struct MigrationState MigrationState ; +/* Postcopy page-map-incoming - data about each page on the inbound side */ + +typedef enum { +POSTCOPY_PMI_MISSING ,/* page hasn't yet been received */ +POSTCOPY_PMI_REQUESTED ,/* Kernel asked for a page, but we've not got it */ +POSTCOPY_PMI_RECEIVED /* We've got the page */ +}PostcopyPMIState ; + +struct PostcopyPMI { +/* TODO: I'm expecting to rework this using some atomic compare-exchange +* thing, which will require merging the maps together +*/ +QemuMutex mutex; +unsigned long *received_map;/* Pages that we have received */ +unsigned long *requested_map;/* Pages that we're sending a request for */ +}; +/* State for the incoming migration */ struct MigrationIncomingState {QEMUFile *file;
@@ -71,6 +88,7 @@ struct MigrationIncomingState {QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ +PostcopyPMI postcopy_pmi; };MigrationIncomingState *migration_incoming_state_init (QEMUFile *f);
include/migration/postcopy-ram.h +4 -0
@@ -36,4 +36,8 @@ int postcopy_ram_discard_range(MigrationIncomingState *mis, uint8_t *start,int postcopy_send_discard_bm_ram (MigrationState *ms,const char *name,unsigned long start,unsigned long end); +void postcopy_pmi_destroy (MigrationIncomingState *mis); +void postcopy_pmi_discard_range (MigrationIncomingState *mis, +size_t start,size_t npages); +#endif
include/qemu/typedefs.h +1 -0
@@ -77,6 +77,7 @@ typedef struct QEMUSGList QEMUSGList;typedef struct SHPCDevice SHPCDevice ;typedef struct FWCfgState FWCfgState ;typedef struct PcGuestInfo PcGuestInfo ; +typedef struct PostcopyPMI PostcopyPMI ;typedef struct Range Range ;typedef struct AdapterInfo AdapterInfo ;
postcopy-ram.c +118 -0
130 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 27/46 postcopy: Add incoming_init/cleanup functions
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Provide functions to be called before the start of a postcopy enabled migration (even if it's not eventually used) and at the end.
During the init we must disable huge pages in the RAM that we will receive postcopy data into, since if they start off as hugepage and get a 4k page written to them, the rest of the hugepage won't get userfault'd and won't work as a destination for remap.
--- include/migration/postcopy-ram.h | 12 +++++ postcopy-ram.c | 100 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+)
include/migration/postcopy-ram.h +12 -0
@@ -30,6 +30,18 @@ int postcopy_ram_discard_range(MigrationIncomingState *mis, uint8_t *start,/* +* Initialise postcopy-ram, setting the RAM to a state where we can go into +* postcopy later; must be called prior to any precopy. +* called from arch_init's similarly named ram_postcopy_incoming_init +*/ +int postcopy_ram_incoming_init (MigrationIncomingState *mis,size_t ram_pages); + +/* +* At the end of a migration where postcopy_ram_incoming_init was called. +*/ +int postcopy_ram_incoming_cleanup (MigrationIncomingState *mis); + +/* * Called back from arch_init's ram_postcopy_each_ram_discard to handle * discarding one RAMBlock's pre-postcopy dirty pages */
postcopy-ram.c +100 -0
112 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 29/46 postcopy: ram_enable_notify to switch on userfault
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- include/migration/postcopy-ram.h | 5 +++++ postcopy-ram.c | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-)
include/migration/postcopy-ram.h +5 -0
@@ -28,6 +28,11 @@ int postcopy_send_discard_bitmap(MigrationState *ms);int postcopy_ram_discard_range (MigrationIncomingState *mis,uint8_t *start,uint8_t *end); +/* +* Make all of RAM sensitive to accesses to areas that haven't yet been written +* and wire up anything necessary to deal with it. +*/ +int postcopy_ram_enable_notify (MigrationIncomingState *mis);/* * Initialise postcopy-ram, setting the RAM to a state where we can go into
postcopy-ram.c +35 -1
@@ -337,9 +337,38 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) return 0; } +/* +* Mark the given area of RAM as requiring notification to unwritten areas +* Used as a callback on qemu_ram_foreach_block. +* host_addr: Base of area to mark +* offset: Offset in the whole ram arena +* length: Length of the section +* opaque: Unused +* Returns 0 on success +*/ +static int postcopy_ram_sensitise_area (const char *block_name,void *host_addr, +ram_addr_t offset,ram_addr_t length, +void *opaque) +{ +if (madvise (host_addr, length,MADV_USERFAULT )) { +perror ("postcopy_ram_sensitise_area" ); +return -1 ; + } +return 0 ; +} + +int postcopy_ram_enable_notify (MigrationIncomingState *mis) +{ +/* Mark so that we get notified of accesses to unwritten areas */ +if (qemu_ram_foreach_block (postcopy_ram_sensitise_area,NULL )) { +return -1 ; + } + +return 0 ; +} +#else /* No target OS support, stubs just fail */ -int postcopy_ram_hosttest (void ) {error_report ("postcopy_ram_hosttest: No OS support" );
@@ -365,6 +394,11 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis) return -1; } +int postcopy_ram_enable_notify (MigrationIncomingState *mis) +{ +fprintf (stderr,"postcopy_ram_enable_notify: No OS support\n" ); +return -1 ; +}#endif /* ------------------------------------------------------------------------- */
Signature · 1 line
1.9.3
PATCH 28/46 postcopy: Incoming initialisation
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- arch_init.c | 11 +++++++++++ include/migration/migration.h | 1 + migration.c | 2 ++ 3 files changed, 14 insertions(+)
arch_init.c +11 -0
@@ -1234,6 +1234,17 @@ void ram_handle_compressed(void *host, uint8_t ch, uint64_t size) } } +/* +* Allocate data structures etc needed by incoming migration with postcopy-ram +* postcopy-ram's similarly names postcopy_ram_incoming_init does the work +*/ +int ram_postcopy_incoming_init (MigrationIncomingState *mis) +{ +size_t ram_pages =last_ram_offset () >>TARGET_PAGE_BITS ; + +return postcopy_ram_incoming_init (mis, ram_pages); +} +static int ram_load (QEMUFile *f,void *opaque,int version_id) {ram_addr_t addr;
include/migration/migration.h +1 -0
@@ -194,6 +194,7 @@ int ram_postcopy_each_ram_discard(MigrationState *ms);int ram_discard_range (MigrationIncomingState *mis,const char *block_name,int source_target_page_bits,uint64_t start,uint64_t end); +int ram_postcopy_incoming_init (MigrationIncomingState *mis);/** * @migrate_add_blocker - prevent migration from proceeding
migration.c +2 -0
@@ -139,6 +139,8 @@ MigrationIncomingState *migration_incoming_state_init(QEMUFile* f)void migration_incoming_state_destroy (MigrationIncomingState *mis) { +postcopy_pmi_destroy (mis); +g_free (mis); }
Signature · 1 line
1.9.3
PATCH 30/46 Postcopy: postcopy_start
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
postcopy_start:
Perform all the initialisation associated with starting up postcopy
mode from the source.
--- migration.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
migration.c +85 -0
@@ -890,6 +890,91 @@ static int open_outgoing_return_path(MigrationState *ms)return 0 ; } +/* Switch from normal iteration to postcopy +* Returns non-0 on error +*/ +static int postcopy_start (MigrationState *ms) +{ +int ret; +const QEMUSizedBuffer *qsb; +migrate_set_state (ms,MIG_STATE_ACTIVE ,MIG_STATE_POSTCOPY_ACTIVE ); + +DPRINTF ("postcopy_start\n" ); +qemu_mutex_lock_iothread (); +DPRINTF ("postcopy_start: setting run state\n" ); + ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE ); + +if (ret <0 ) { +migrate_set_state (ms,MIG_STATE_POSTCOPY_ACTIVE ,MIG_STATE_ERROR ); +qemu_mutex_unlock_iothread (); +return -1 ; + } + +/* +* in Finish migrate and with the io-lock held everything should +* be quiet, but we've potentially still got dirty pages and we +* need to tell the destination to throw any pages it's already received +* that are dirty +*/ +if (postcopy_send_discard_bitmap (ms)) { +DPRINTF ("postcopy send discard bitmap failed\n" ); +migrate_set_state (ms,MIG_STATE_POSTCOPY_ACTIVE ,MIG_STATE_ERROR ); +qemu_mutex_unlock_iothread (); +return -1 ; + } + +DPRINTF ("postcopy_start: sending req 2\n" ); +qemu_savevm_send_reqack (ms->file,2 ); +/* +* send rest of state - note things that are doing postcopy +* will notice we're in MIG_STATE_POSTCOPY_ACTIVE and not actually +* wrap their state up here +*/ +qemu_file_set_rate_limit (ms->file,INT64_MAX ); +DPRINTF ("postcopy_start: do state_complete\n" ); + +/* +* We need to leave the fd free for page transfers during the +* loading of the device state, so wrap all the remaining +* commands and state into a package that gets sent in one go +*/ +QEMUFile *fb =qemu_bufopen ("w" ,NULL ); + +qemu_savevm_state_complete (fb); +DPRINTF ("postcopy_start: sending req 3\n" ); +qemu_savevm_send_reqack (fb,3 ); + +qemu_savevm_send_postcopy_ram_run (fb); + +/* <><> end of stuff going into the package */ + qsb =qemu_buf_get (fb); + +/* Now send that blob */ +if (qsb_get_length (qsb) >MAX_VM_CMD_PACKAGED_SIZE ) { +DPRINTF ("postcopy_start: Unreasonably large packaged state: %lu\n" , + (unsigned long )(qsb_get_length (qsb))); +migrate_set_state (ms,MIG_STATE_POSTCOPY_ACTIVE ,MIG_STATE_ERROR ); +qemu_mutex_unlock_iothread (); +qemu_fclose (fb); +return -1 ; + } +qemu_savevm_send_packaged (ms->file, qsb); +qemu_fclose (fb); + +qemu_mutex_unlock_iothread (); + +DPRINTF ("postcopy_start not finished sending ack\n" ); +qemu_savevm_send_reqack (ms->file,4 ); + + ret =qemu_file_get_error (ms->file); +if (ret) { +error_report ("postcopy_start: Migration stream errored" ); +migrate_set_state (ms,MIG_STATE_POSTCOPY_ACTIVE ,MIG_STATE_ERROR ); + } + +return ret; +} +/* * Master migration thread on the source VM. * It drives the migration and pumps the data down the outgoing channel.
Signature · 1 line
1.9.3
PATCH 31/46 Postcopy: Rework migration thread for postcopy mode
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Switch to postcopy if:
1) There's still a significant amount to transfer
2) Postcopy is enabled
3) It's taken longer than the time set by the parameter.
and change the cleanup at the end of migration to match.
--- migration.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 73 insertions(+), 19 deletions(-)
migration.c +73 -19
@@ -982,16 +982,40 @@ static int postcopy_start(MigrationState *ms)static void *migration_thread (void *opaque) {MigrationState *s = opaque; +/* Used by the bandwidth calcs, updated later */ int64_t initial_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +/* Really, the time we started */ +const int64_t initial_time_fixed = initial_time;int64_t setup_start =qemu_clock_get_ms (QEMU_CLOCK_HOST );int64_t initial_bytes =0 ;int64_t max_size =0 ;int64_t start_time = initial_time; +int64_t pc_start_time; +bool old_vm_running = false; + pc_start_time = s->tunables[MIGRATION_PARAMETER_NAME_X_POSTCOPY_START_TIME ]; + +/* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */ +enum MigrationPhase current_active_type =MIG_STATE_ACTIVE ;qemu_savevm_state_begin (s->file, &s->params); +if (migrate_postcopy_ram ()) { +/* Now tell the dest that it should open it's end so it can reply */ +qemu_savevm_send_openrp (s->file); + +/* And ask it to send an ack that will make stuff easier to debug */ +qemu_savevm_send_reqack (s->file,1 ); + +/* Tell the destination that we *might* want to do postcopy later; +* if the other end can't do postcopy it should fail now, nice and +* early. +*/ +qemu_savevm_send_postcopy_ram_advise (s->file); + } + s->setup_time =qemu_clock_get_ms (QEMU_CLOCK_HOST ) - setup_start; + current_active_type =MIG_STATE_ACTIVE ;migrate_set_state (s,MIG_STATE_SETUP ,MIG_STATE_ACTIVE );DPRINTF ("setup complete\n" );
@@ -1012,37 +1036,66 @@ static void *migration_thread(void *opaque)" nonpost=%" PRIu64")\n" , pending_size, max_size, pend_post, pend_nonpost);if (pending_size && pending_size >= max_size) { -qemu_savevm_state_iterate (s->file); +/* Still a significant amount to transfer */ + + current_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +if (migrate_postcopy_ram () && + s->state !=MIG_STATE_POSTCOPY_ACTIVE && + pend_nonpost ==0 && + (current_time >= initial_time_fixed + pc_start_time)) { + +if (!postcopy_start (s)) { + current_active_type =MIG_STATE_POSTCOPY_ACTIVE ; + } + +continue ; + }else { +/* Just another iteration step */ +qemu_savevm_state_iterate (s->file); + } }else {int ret; -DPRINTF ("done iterating\n" ); -qemu_mutex_lock_iothread (); - start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); -qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); - old_vm_running =runstate_is_running (); - - ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE ); -if (ret >=0 ) { -qemu_file_set_rate_limit (s->file,INT64_MAX ); -qemu_savevm_state_complete (s->file); - } -qemu_mutex_unlock_iothread (); - -if (ret <0 ) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_ERROR ); -break ; +DPRINTF ("done iterating pending size %" PRIu64"\n" , + pending_size); + +if (s->state ==MIG_STATE_ACTIVE ) { +qemu_mutex_lock_iothread (); + start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); + old_vm_running =runstate_is_running (); + + ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE ); +if (ret >=0 ) { +qemu_file_set_rate_limit (s->file,INT64_MAX ); +qemu_savevm_state_complete (s->file); + } +qemu_mutex_unlock_iothread (); + +if (ret <0 ) { +migrate_set_state (s, current_active_type, +MIG_STATE_ERROR ); +break ; + } + }else { +assert (s->state ==MIG_STATE_POSTCOPY_ACTIVE ); +DPRINTF ("postcopy end\n" ); + +qemu_savevm_state_postcopy_complete (s->file); +DPRINTF ("postcopy end after complete\n" ); }if (!qemu_file_get_error (s->file)) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_COMPLETED ); +migrate_set_state (s, current_active_type, +MIG_STATE_COMPLETED );break ; } } }if (qemu_file_get_error (s->file)) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_ERROR ); +migrate_set_state (s, current_active_type,MIG_STATE_ERROR ); +DPRINTF ("migration_thread: file is in error state\n" );break ; } current_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );
@@ -1073,6 +1126,7 @@ static void *migration_thread(void *opaque) } } +DPRINTF ("migration_thread: Hit error: case\n" );qemu_mutex_lock_iothread ();if (s->state ==MIG_STATE_COMPLETED ) {int64_t end_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );
Signature · 1 line
1.9.3
PATCH 32/46 mig fd_connect: open return path
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- migration.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
migration.c +12 -0
@@ -1164,6 +1164,18 @@ void migrate_fd_connect(MigrationState *s)/* Notify before starting migration thread */ notifier_list_notify (&migration_state_notifiers, s); +/* Open the return path; currently for postcopy but other things might +* also want it. +*/ +if (migrate_postcopy_ram ()) { +if (open_outgoing_return_path (s)) { +error_report ("Unable to open return-path for postcopy" ); +migrate_set_state (s,MIG_STATE_SETUP ,MIG_STATE_ERROR ); +migrate_fd_cleanup (s); +return ; + } + } +qemu_thread_create (&s->thread,"migration" , migration_thread, s,QEMU_THREAD_JOINABLE ); }
Signature · 1 line
1.9.3
PATCH 33/46 Postcopy: Create a fault handler thread before marking the ram as userfault
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- include/migration/migration.h | 3 +++ postcopy-ram.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+)
include/migration/migration.h +3 -0
@@ -86,6 +86,9 @@ struct MigrationIncomingState { POSTCOPY_RAM_INCOMING_END } postcopy_ram_state; +QemuThread fault_thread; +QemuSemaphore fault_thread_sem; +QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ PostcopyPMI postcopy_pmi;
postcopy-ram.c +23 -0
@@ -357,8 +357,31 @@ static int postcopy_ram_sensitise_area(const char *block_name, void *host_addr,return 0 ; } +/* +* Handle faults detected by the USERFAULT markings +*/ +static void *postcopy_ram_fault_thread (void *opaque) +{ +MigrationIncomingState *mis = (MigrationIncomingState *)opaque; + +fprintf (stderr,"postcopy_ram_fault_thread\n" ); +/* TODO: In later patch */ +qemu_sem_post (&mis->fault_thread_sem); +while (1 ) { +/* TODO: In later patch */ + } + +return NULL ; +} +int postcopy_ram_enable_notify (MigrationIncomingState *mis) { +/* Create the fault handler thread and wait for it to be ready */ +qemu_sem_init (&mis->fault_thread_sem,0 ); +qemu_thread_create (&mis->fault_thread,"postcopy/fault" , + postcopy_ram_fault_thread, mis,QEMU_THREAD_JOINABLE ); +qemu_sem_wait (&mis->fault_thread_sem); +/* Mark so that we get notified of accesses to unwritten areas */ if (qemu_ram_foreach_block (postcopy_ram_sensitise_area,NULL )) {return -1 ;
Signature · 1 line
1.9.3
PATCH 34/46 Page request: Add MIG_RPCOMM_REQPAGES reverse command
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Add MIG_RPCOMM_REQPAGES command on Return path for the postcopy destination to request a page from the source.
--- include/migration/migration.h | 3 ++ migration.c | 75 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-)
include/migration/migration.h +3 -0
@@ -45,6 +45,7 @@ struct MigrationParams {enum mig_rpcomm_cmd {MIG_RPCOMM_INVALID =0 ,/* Must be 0 */ MIG_RPCOMM_ACK ,/* data (seq: be32 ) */ +MIG_RPCOMM_REQPAGES ,/* data (start: be64, len: be64) */ MIG_RPCOMM_AFTERLASTVALID };
@@ -234,6 +235,8 @@ void migrate_send_rp_message(MigrationIncomingState *mis, uint16_t len, uint8_t *data);void migrate_send_rp_ack (MigrationIncomingState *mis,uint32_t value); +void migrate_send_rp_reqpages (MigrationIncomingState *mis,const char * rbname, +ram_addr_t start,ram_addr_t len);void ram_control_before_iterate (QEMUFile *f,uint64_t flags);
migration.c +74 -1
114 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 35/46 Page request: Process incoming page request
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
On receiving MIG_RPCOMM_REQPAGES look up the address and queue the page.
--- arch_init.c | 52 +++++++++++++++++++++++++++++++++++++++++++ include/migration/migration.h | 26 ++++++++++++++++++++++ include/qemu/typedefs.h | 3 ++- migration.c | 34 +++++++++++++++++++++++++++- 4 files changed, 113 insertions(+), 2 deletions(-)
arch_init.c +52 -0
@@ -658,6 +658,58 @@ static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset, }/* +* Queue the pages for transmission, e.g. a request from postcopy destination +* ms: MigrationStatus in which the queue is held +* rbname: The RAMBlock the request is for - may be NULL (to mean reuse last) +* start: Offset from the start of the RAMBlock +* len: Length (in bytes) to send +* Return: 0 on success +*/ +int ram_save_queue_pages (MigrationState *ms,const char *rbname, +ram_addr_t start,ram_addr_t len) +{ +RAMBlock *ramblock; + +if (!rbname) { +/* Reuse last RAMBlock */ + ramblock = ms->last_req_rb; + +if (!ramblock) { +error_report ("ram_save_queue_pages no previous block" ); +return -1 ; + } + }else { + ramblock =ram_find_block (rbname); + +if (!ramblock) { +error_report ("ram_save_queue_pages no block '%s'" , rbname); +return -1 ; + } + } +DPRINTF ("ram_save_queue_pages: Block %s start %zx len %zx" , + ramblock->idstr, start, len); + +if (start+len > ramblock->length) { +error_report ("%s request overrun start=%zx len=%zx blocklen=%zx" , + __func__, start, len, ramblock->length); +return -1 ; + } + +struct MigrationSrcPageRequest *new_entry = +g_malloc0 (sizeof (struct MigrationSrcPageRequest )); + new_entry->rb = ramblock; + new_entry->offset = start; + new_entry->len = len; + ms->last_req_rb = ramblock; + +qemu_mutex_lock (&ms->src_page_req_mutex); +QSIMPLEQ_INSERT_TAIL (&ms->src_page_requests, new_entry, next_req); +qemu_mutex_unlock (&ms->src_page_req_mutex); + +return 0 ; +} + +/* * ram_find_and_save_block: Finds a page to send and sends it to f * * Returns: The number of bytes written.
include/migration/migration.h +26 -0
@@ -97,6 +97,16 @@ struct MigrationIncomingState {MigrationIncomingState *migration_incoming_state_init (QEMUFile *f);void migration_incoming_state_destroy (MigrationIncomingState *mis); +/* An outstanding page request, on the source, having been received +* and queued +*/ +struct MigrationSrcPageRequest { +RAMBlock *rb; +hwaddr offset; +hwaddr len; + +QSIMPLEQ_ENTRY (MigrationSrcPageRequest) next_req; +};/* State for the outgoing migration */ struct MigrationState
@@ -124,6 +134,19 @@ struct MigrationStateint64_t xbzrle_cache_size;int64_t setup_time;int64_t dirty_sync_count; + +/* bitmap of pages that have been sent at least once +* only maintained and used in postcopy at the moment +* Where it's used to send the dirtymap at the start +* of the postcopy phase, then cleared +*/ +unsigned long *sentmap; + +/* Queue of outstanding page requests from the destination */ +QemuMutex src_page_req_mutex; + QSIMPLEQ_HEAD(src_page_requests, MigrationSrcPageRequest) src_page_requests; +/* The RAMBlock used in the last src_page_request */ +RAMBlock *last_req_rb; };void process_incoming_migration (QEMUFile *f);
@@ -257,4 +280,7 @@ size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, ram_addr_t offset, size_t size, int *bytes_sent); +int ram_save_queue_pages (MigrationState *ms,const char *rbname, +ram_addr_t start,ram_addr_t len); +#endif
include/qemu/typedefs.h +2 -1
@@ -8,6 +8,7 @@ typedef struct QEMUTimerListGroup QEMUTimerListGroup;typedef struct QEMUFile QEMUFile ;typedef struct QEMUBH QEMUBH ; +typedef struct AdapterInfo AdapterInfo ;typedef struct AioContext AioContext ;typedef struct Visitor Visitor ;
@@ -79,6 +80,6 @@ typedef struct FWCfgState FWCfgState;typedef struct PcGuestInfo PcGuestInfo ;typedef struct PostcopyPMI PostcopyPMI ;typedef struct Range Range ; -typedef struct AdapterInfo AdapterInfo ; +typedef struct RAMBlock RAMBlock ;#endif /* QEMU_TYPEDEFS_H */
migration.c +33 -1
@@ -26,6 +26,8 @@#include "qemu/thread.h" #include "qmp-commands.h" #include "trace.h" +#include "exec/memory.h" +#include "exec/address-spaces.h" //#define DEBUG_MIGRATION
@@ -500,6 +502,15 @@ static void migrate_fd_cleanup(void *opaque)migrate_fd_cleanup_src_rp (s); +/* This queue generally should be empty - but in the case of a failed +* migration might have some droppings in. +*/ +struct MigrationSrcPageRequest *mspr, *next_mspr; + QSIMPLEQ_FOREACH_SAFE(mspr, &s->src_page_requests, next_req, next_mspr) { +QSIMPLEQ_REMOVE_HEAD (&s->src_page_requests, next_req); +g_free (mspr); + } +if (s->file) {trace_migrate_fd_cleanup ();qemu_mutex_unlock_iothread ();
@@ -602,6 +613,9 @@ MigrationState *migrate_init(const MigrationParams *params) s->state =MIG_STATE_SETUP ;trace_migrate_set_state (MIG_STATE_SETUP ); +qemu_mutex_init (&s->src_page_req_mutex); +QSIMPLEQ_INIT (&s->src_page_requests); + s->total_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );return s; }
@@ -815,7 +829,25 @@ static void source_return_path_bad(MigrationState *s)static void migrate_handle_rp_reqpages (MigrationState *ms,const char * rbname,ram_addr_t start,ram_addr_t len) { -DPRINTF ("migrate_handle_rp_reqpages: at %zx for len %zx" , start, len); +DPRINTF ("migrate_handle_rp_reqpages: in %s start %zx len %zx" , + rbname, start, len); + +/* Round everything up to our host page size */ +long our_host_ps =sysconf (_SC_PAGESIZE); +if (start & (our_host_ps-1 )) { +long roundings = start & (our_host_ps-1 ); + start -= roundings; + len += roundings; + } +if (len & (our_host_ps-1 )) { +long roundings = len & (our_host_ps-1 ); + len -= roundings; + len += our_host_ps; + } + +if (ram_save_queue_pages (ms, rbname, start, len)) { +source_return_path_bad (ms); + } }/*
Signature · 1 line
1.9.3
PATCH 36/46 Page request: Consume pages off the post-copy queue
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
When transmitting RAM pages, consume pages that have been queued by MIG_RPCOMM_REQPAGE commands and send them ahead of normal page scanning.
Note:
a) After a queued page the linear walk carries on from after the
unqueued page; there is a reasonable chance that the destination was about to ask for other closeby pages anyway.
b) We have to be careful of any assumptions that the page walking
code makes, in particular it does some short cuts on its first linear walk that break as soon as we do a queued page.
--- arch_init.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 106 insertions(+), 24 deletions(-)
arch_init.c +106 -24
@@ -458,6 +458,19 @@ static inline bool migration_bitmap_set_dirty(ram_addr_t addr)return ret; } +static inline bool migration_bitmap_clear_dirty (ram_addr_t addr) +{ +bool ret; +int nr = addr >>TARGET_PAGE_BITS ; + + ret =test_and_clear_bit (nr, migration_bitmap); + +if (ret) { + migration_dirty_pages--; + } +return ret; +} +static void migration_bitmap_sync_range (ram_addr_t start,ram_addr_t length) {ram_addr_t addr;
@@ -658,6 +671,39 @@ static int ram_save_page(QEMUFile *f, RAMBlock* block, ram_addr_t offset, }/* +* Unqueue a page from the queue fed by postcopy page requests +* +* Returns: The RAMBlock* to transmit from (or NULL if the queue is empty) +* ms: MigrationState in +* offset: the byte offset within the RAMBlock for the start of the page +* bitoffset: global offset in the dirty/sent bitmaps +*/ +static RAMBlock *ram_save_unqueue_page (MigrationState *ms,ram_addr_t *offset, +unsigned long *bitoffset) +{ +RAMBlock *result =NULL ; +qemu_mutex_lock (&ms->src_page_req_mutex); +if (!QSIMPLEQ_EMPTY (&ms->src_page_requests)) { +struct MigrationSrcPageRequest *entry = +QSIMPLEQ_FIRST (&ms->src_page_requests); + result = entry->rb; + *offset = entry->offset; + *bitoffset = (entry->offset + entry->rb->offset) >>TARGET_PAGE_BITS ; + +if (entry->len >TARGET_PAGE_SIZE ) { + entry->len -=TARGET_PAGE_SIZE ; + entry->offset +=TARGET_PAGE_SIZE ; + }else { +QSIMPLEQ_REMOVE_HEAD (&ms->src_page_requests, next_req); +g_free (entry); + } + } +qemu_mutex_unlock (&ms->src_page_req_mutex); + +return result; +} + +/* * Queue the pages for transmission, e.g. a request from postcopy destination * ms: MigrationStatus in which the queue is held * rbname: The RAMBlock the request is for - may be NULL (to mean reuse last)
@@ -718,44 +764,80 @@ int ram_save_queue_pages(MigrationState *ms, const char *rbname,static int ram_find_and_save_block (QEMUFile *f,bool last_stage) { +MigrationState *ms =migrate_get_current ();RAMBlock *block = last_seen_block; +RAMBlock *tmpblock;ram_addr_t offset = last_offset; +ram_addr_t tmpoffset;bool complete_round = false;int bytes_sent =0 ; -MemoryRegion *mr;unsigned long bitoffset;if (!block) block =QTAILQ_FIRST (&ram_list.blocks); -while (true) { - mr = block->mr; - offset =migration_bitmap_find_and_reset_dirty (mr, offset, &bitoffset); -if (complete_round && block == last_seen_block && - offset >= last_offset) { -break ; - } -if (offset >= block->length) { - offset =0 ; - block =QTAILQ_NEXT (block, next); -if (!block) { - block =QTAILQ_FIRST (&ram_list.blocks); - complete_round = true; - ram_bulk_stage = false; +while (true) {/* Until we send a block or run out of stuff to send */ + tmpblock =ram_save_unqueue_page (ms, &tmpoffset, &bitoffset); +if (tmpblock) { +/* We've got a block from the postcopy queue */ +DPRINTF ("%s: Got postcopy item '%s' offset=%zx bitoffset=%zx" , + __func__, tmpblock->idstr, tmpoffset, bitoffset); +/* We're sending this page, and since it's postcopy nothing else +* will dirty it, and we must make sure it doesn't get sent again. +*/ +if (!migration_bitmap_clear_dirty (bitoffset <<TARGET_PAGE_BITS )) { +DPRINTF ("%s: Not dirty for postcopy %s/%zx bito=%zx (sent=%d)" , + __func__, tmpblock->idstr, tmpoffset, bitoffset, +test_bit (bitoffset, ms->sentmap)); +continue ; } +/* +* As soon as we start servicing pages out of order, then we have +* to kill the bulk stage, since the bulk stage assumes +* in (migration_bitmap_find_and_reset_dirty) that every page is +* dirty, that's no longer true. +*/ + ram_bulk_stage = false; +/* +* We mustn't change block/offset unless it's to a valid one +* otherwise we can go down some of the exit cases in the normal +* path. +*/ + block = tmpblock; + offset = tmpoffset; }else { - bytes_sent =ram_save_page (f, block, offset, last_stage); - -/* if page is unmodified, continue to the next */ -if (bytes_sent >0 ) { -MigrationState *s =migrate_get_current (); -if (s->sentmap) { -set_bit (bitoffset, s->sentmap); +MemoryRegion *mr; +/* priority queue empty, so just search for something dirty */ + mr = block->mr; + offset =migration_bitmap_find_and_reset_dirty (mr, offset, + &bitoffset); +if (complete_round && block == last_seen_block && + offset >= last_offset) { +break ; + } +if (offset >= block->length) { + offset =0 ; + block =QTAILQ_NEXT (block, next); +if (!block) { + block =QTAILQ_FIRST (&ram_list.blocks); + complete_round = true; + ram_bulk_stage = false; } +continue ;/* pick an offset in the new block */ + } + } - last_sent_block = block; -break ; +/* We have a page to send, so send it */ + bytes_sent =ram_save_page (f, block, offset, last_stage); + +/* if page is unmodified, continue to the next */ +if (bytes_sent >0 ) { +if (ms->sentmap) { +set_bit (bitoffset, ms->sentmap); } + + last_sent_block = block; +break ; } } last_seen_block = block;
Signature · 1 line
1.9.3
PATCH 37/46 Add assertion to check migration_dirty_pages doesn't go -ve; have seen it happen once but not sure why
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- arch_init.c | 1 + 1 file changed, 1 insertion(+)
arch_init.c +1 -0
@@ -439,6 +439,7 @@ ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,if (next < size) {clear_bit (next, migration_bitmap); +assert (migration_dirty_pages >0 ); migration_dirty_pages--; } *bitoffset = next;
Signature · 1 line
1.9.3
PATCH 38/46 postcopy_ram.c: place_page and helpers
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
postcopy_place_page (etc) provide a way for postcopy to place a page into guests memory atomically (using the new remap_anon_pages syscall).
--- include/migration/migration.h | 1 + include/migration/postcopy-ram.h | 23 +++++++++ postcopy-ram.c | 105 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+)
include/migration/migration.h +1 -0
@@ -93,6 +93,7 @@ struct MigrationIncomingState {QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */ PostcopyPMI postcopy_pmi; +void *postcopy_tmp_page; };MigrationIncomingState *migration_incoming_state_init (QEMUFile *f);
include/migration/postcopy-ram.h +23 -0
@@ -53,6 +53,29 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis);int postcopy_send_discard_bm_ram (MigrationState *ms,const char *name,unsigned long start,unsigned long end); +/* +* Place a zero'd page of memory at *host +* returns 0 on success +*/ +int postcopy_place_zero_page (MigrationIncomingState *mis,void *host, +long bitmap_offset); + +/* +* Place a page (from) at (host) efficiently +* There are restrictions on how 'from' must be mapped, in general best +* to use other postcopy_ routines to allocate. +* returns 0 on success +*/ +int postcopy_place_page (MigrationIncomingState *mis,void *host,void *from, +long bitmap_offset); + +/* +* Allocate a page of memory that can be mapped at a later point in time +* using postcopy_place_page +* Returns: Pointer to allocated page +*/ +void *postcopy_get_tmp_page (MigrationIncomingState *mis); +void postcopy_pmi_destroy (MigrationIncomingState *mis);void postcopy_pmi_discard_range (MigrationIncomingState *mis,size_t start,size_t npages);
postcopy-ram.c +105 -0
123 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 39/46 Postcopy: Use helpers to map pages during migration
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
In postcopy, the destination guest is running at the same time as it's receiving pages; as we receive new pages we must put them into the guests address space atomically to avoid a running CPU accessing a partially written page.
Use the helpers in postcopy-ram.c to map these pages.
--- arch_init.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 7 deletions(-)
arch_init.c +73 -7
@@ -1328,9 +1328,18 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)return 0 ; } +/* +* Read a RAMBlock ID from the stream f, find the host address of the +* start of that block and add on 'offset' +* +* f: Stream to read from +* offset: Offset within the block +* flags: Page flags (mostly to see if it's a continuation of previous block) +* rb: Pointer to RAMBlock* that gets filled in with the RB we find +*/ static inline void *host_from_stream_offset (QEMUFile *f,ram_addr_t offset, -int flags) +int flags,RAMBlock **rb) {static RAMBlock *block =NULL ;char id[256 ];
@@ -1341,6 +1350,9 @@ static inline void *host_from_stream_offset(QEMUFile *f,error_report ("Ack, bad migration stream!" );return NULL ; } +if (rb) { + *rb = block; + }return memory_region_get_ram_ptr (block->mr) + offset; }
@@ -1350,8 +1362,12 @@ static inline void *host_from_stream_offset(QEMUFile *f, id[len] =0 ; QTAILQ_FOREACH(block, &ram_list.blocks, next) { -if (!strncmp (id, block->idstr,sizeof (id))) +if (!strncmp (id, block->idstr,sizeof (id))) { +if (rb) { + *rb = block; + }return memory_region_get_ram_ptr (block->mr) + offset; + } }error_report ("Can't find block %s!" , id);
@@ -1385,6 +1401,12 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)ram_addr_t addr;int flags, ret =0 ;static uint64_t seq_iter; +/* +* System is running in postcopy mode, page inserts to host memory must be +* atomic +*/ +bool postcopy_running = f->mis->postcopy_ram_state >= +POSTCOPY_RAM_INCOMING_LISTENING ; seq_iter++;
@@ -1439,8 +1461,9 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) }else if (flags &RAM_SAVE_FLAG_COMPRESS ) {void *host;uint8_t ch; +RAMBlock *rb; - host =host_from_stream_offset (f, addr, flags); + host =host_from_stream_offset (f, addr, flags, &rb);if (!host) {error_report ("Illegal RAM offset " RAM_ADDR_FMT , addr); ret = -EINVAL ;
@@ -1448,20 +1471,63 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) } ch =qemu_get_byte (f); -ram_handle_compressed (host, ch,TARGET_PAGE_SIZE ); +if (!postcopy_running) { +ram_handle_compressed (host, ch,TARGET_PAGE_SIZE ); + }else { +if (!ch) { + ret =postcopy_place_zero_page (f->mis, host, + (addr + rb->offset) >>TARGET_PAGE_BITS ); + }else { +void *tmp; + tmp =postcopy_get_tmp_page (f->mis); +if (!tmp) { +return -ENOMEM ; + } +memset (tmp, ch,TARGET_PAGE_SIZE ); + ret =postcopy_place_page (f->mis, host, tmp, + (addr + rb->offset) >>TARGET_PAGE_BITS ); + } +if (ret) { +error_report ("ram_load: Failure in postcopy compress @" +"%zx/%p;%s+%zx" , + addr, host, rb->idstr, rb->offset); +return ret; + } + } }else if (flags &RAM_SAVE_FLAG_PAGE ) {void *host; +RAMBlock *rb; - host =host_from_stream_offset (f, addr, flags); + host =host_from_stream_offset (f, addr, flags, &rb);if (!host) {error_report ("Illegal RAM offset " RAM_ADDR_FMT , addr); ret = -EINVAL ;break ; } -qemu_get_buffer (f, host,TARGET_PAGE_SIZE ); +if (!postcopy_running) { +qemu_get_buffer (f, host,TARGET_PAGE_SIZE ); + }else { +void *tmp =postcopy_get_tmp_page (f->mis); +if (!tmp) { +return -ENOMEM ; + } +qemu_get_buffer (f, tmp,TARGET_PAGE_SIZE ); + ret =postcopy_place_page (f->mis, host, tmp, + (addr + rb->offset) >>TARGET_PAGE_BITS ); +if (ret) { +error_report ("ram_load: Failure in postcopy simple" +"@%zx/%p;%s+%zx" , + addr, host, rb->idstr, rb->offset); +return ret; + } + } }else if (flags &RAM_SAVE_FLAG_XBZRLE ) { -void *host =host_from_stream_offset (f, addr, flags); +if (postcopy_running) { +error_report ("XBZRLE RAM block in postcopy mode @%zx\n" , addr); +return -EINVAL ; + } +void *host =host_from_stream_offset (f, addr, flags,NULL );if (!host) {error_report ("Illegal RAM offset " RAM_ADDR_FMT , addr); ret = -EINVAL ;
Signature · 1 line
1.9.3
PATCH 40/46 qemu_ram_block_from_host
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy sends RAMBlock names and offsets over the wire (since it can't rely on the order of ramaddr being the same), and it starts out with HVA fault addresses from the kernel.
qemu_ram_block_from_host translates a HVA into a RAMBlock, an offset in the RAMBlock, the global ram_addr_t value and it's bitmap position.
Rewrite qemu_ram_addr_from_host to use qemu_ram_block_from_host.
Provide qemu_ram_get_idstr since it's the actual name text sent on the wire.
--- exec.c | 50 ++++++++++++++++++++++++++++++++++++++++++----- include/exec/cpu-common.h | 3 +++ 2 files changed, 48 insertions(+), 5 deletions(-)
exec.c +45 -5
@@ -1176,6 +1176,11 @@ static RAMBlock *find_ram_block(ram_addr_t addr)return NULL ; } +const char *qemu_ram_get_idstr (RAMBlock *rb) +{ +return rb->idstr; +} +void qemu_ram_set_idstr (ram_addr_t addr,const char *name,DeviceState *dev) {RAMBlock *new_block =find_ram_block (addr);
@@ -1515,16 +1520,32 @@ static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size) } } -/* Some of the softmmu routines need to translate from a host pointer -(typically a TLB entry) back to a ram offset. */ -MemoryRegion *qemu_ram_addr_from_host (void *ptr,ram_addr_t *ram_addr) +/* +* Translates a host ptr back to a RAMBlock, a ram_addr and an offset +* in that RAMBlock. +* +* ptr: Host pointer to look up +* *ram_addr: set to result ram_addr +* *offset: set to result offset within the RAMBlock +* *bm_index: bitmap index (i.e. scaled ram_addr for use where the scale +* isn't available) +* +* Returns: RAMBlock (or NULL if not found) +*/ +RAMBlock *qemu_ram_block_from_host (void *ptr,ram_addr_t *ram_addr, +ram_addr_t *offset,unsigned long *bm_index) {RAMBlock *block;uint8_t *host = ptr;if (xen_enabled ()) { *ram_addr =xen_ram_addr_from_mapcache (ptr); -return qemu_get_ram_block (*ram_addr)->mr; + block =qemu_get_ram_block (*ram_addr); +if (!block) { +return NULL ; + } + *offset = (host - block->host); +return block; } block = ram_list.mru_block;
@@ -1545,7 +1566,26 @@ MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)return NULL ; found: - *ram_addr = block->offset + (host - block->host); + *offset = (host - block->host); + *ram_addr = block->offset + *offset; + *bm_index = *ram_addr >>TARGET_PAGE_BITS ; +return block; +} + +/* Some of the softmmu routines need to translate from a host pointer +(typically a TLB entry) back to a ram offset. */ +MemoryRegion *qemu_ram_addr_from_host (void *ptr,ram_addr_t *ram_addr) +{ +RAMBlock *block; +ram_addr_t offset;/* Not used */ +unsigned long index;/* Not used */ + + block =qemu_ram_block_from_host (ptr, ram_addr, &offset, &index); + +if (!block) { +return NULL ; + } +return block->mr; }
include/exec/cpu-common.h +3 -0
@@ -55,8 +55,11 @@ typedef uint32_t CPUReadMemoryFunc(void *opaque, hwaddr addr);void qemu_ram_remap (ram_addr_t addr,ram_addr_t length);/* This should not be used by devices. */ MemoryRegion *qemu_ram_addr_from_host (void *ptr,ram_addr_t *ram_addr); +RAMBlock *qemu_ram_block_from_host (void *ptr,ram_addr_t *ram_addr, +ram_addr_t *offset,unsigned long *bm_index);void qemu_ram_set_idstr (ram_addr_t addr,const char *name,DeviceState *dev);void qemu_ram_unset_idstr (ram_addr_t addr); +const char *qemu_ram_get_idstr (RAMBlock *rb);void cpu_physical_memory_rw (hwaddr addr,uint8_t *buf,int len,int is_write);
Signature · 1 line
1.9.3
PATCH 41/46 Handle userfault requests (although userfaultfd not done yet)
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- include/migration/migration.h | 1 + postcopy-ram.c | 93 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 4 deletions(-)
include/migration/migration.h +1 -0
@@ -89,6 +89,7 @@ struct MigrationIncomingState {QemuThread fault_thread;QemuSemaphore fault_thread_sem; +int userfault_fd;QEMUFile *return_path;QemuMutex rp_mutex;/* We send replies from multiple threads */
postcopy-ram.c +89 -4
114 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 42/46 Start up a postcopy/listener thread ready for incoming page data
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
The loading of a device state (during postcopy) may access guest memory that's still on the source machine and thus might need a page fill; split off a separate thread that handles the incoming page data so that the original incoming migration code can finish off the device data.
--- include/migration/migration.h | 4 +++ migration.c | 6 +++++ savevm.c | 63 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 2 deletions(-)
include/migration/migration.h +4 -0
@@ -89,6 +89,10 @@ struct MigrationIncomingState {QemuThread fault_thread;QemuSemaphore fault_thread_sem; +bool have_listen_thread; +QemuThread listen_thread; +QemuSemaphore listen_thread_sem; +int userfault_fd;QEMUFile *return_path;
migration.c +6 -0
@@ -1045,6 +1045,12 @@ static int postcopy_start(MigrationState *ms)*/ QEMUFile *fb =qemu_bufopen ("w" ,NULL ); +/* +* Make sure the receiver can get incoming pages before we send the rest +* of the state +*/ +qemu_savevm_send_postcopy_ram_listen (fb); +qemu_savevm_state_complete (fb);DPRINTF ("postcopy_start: sending req 3\n" );qemu_savevm_send_reqack (fb,3 );
savevm.c +61 -2
84 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH 43/46 postcopy: Wire up loadvm_postcopy_ram_handle_{run, end} commands
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Wire up more of the handlers for the commands on the destination side, in particular loadvm_postcopy_ram_handle_run now has enough to start the guest running.
--- savevm.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-)
savevm.c +42 -5
@@ -1341,6 +1341,8 @@ static int loadvm_postcopy_ram_handle_listen(MigrationIncomingState *mis)/* After all discards we can start running and asking for pages */ static int loadvm_postcopy_ram_handle_run (MigrationIncomingState *mis) { +Error *local_err =NULL ; +DPRINTF ("%s" , __func__);if (mis->postcopy_ram_state !=POSTCOPY_RAM_INCOMING_LISTENING ) { error_report("CMD_POSTCOPY_RAM_RUN in wrong postcopy state (%d)",
@@ -1349,14 +1351,39 @@ static int loadvm_postcopy_ram_handle_run(MigrationIncomingState *mis) } mis->postcopy_ram_state =POSTCOPY_RAM_INCOMING_RUNNING ; + +/* TODO we should move all of this lot into postcopy_ram.c or a shared code +* in migration.c +*/ +cpu_synchronize_all_post_init (); + +qemu_announce_self (); +bdrv_clear_incoming_migration_all (); + +/* Make sure all file formats flush their mutable metadata */ +bdrv_invalidate_cache_all (&local_err); +if (local_err) { +qerror_report_err (local_err); +error_free (local_err); +return -1 ; + } + +DPRINTF ("loadvm_postcopy_ram_handle_run: cpu_synchronize_all_post_init" ); +cpu_synchronize_all_post_init (); + +DPRINTF ("loadvm_postcopy_ram_handle_run: vm_start" );/* Hold onto your hats, starting the CPU */ vm_start (); -return 0 ; +return LOADVM_EXITCODE_QUITLOOP ; } -/* The end - with a byte from the source which can tell us to fail. */ -static int loadvm_postcopy_ram_handle_end (MigrationIncomingState *mis) +/* The end - with a byte from the source which can tell us to fail. +* The source sends this either if there is a failure, or if it believes it's +* sent everything +*/ +static int loadvm_postcopy_ram_handle_end (MigrationIncomingState *mis, +uint8_t status) {DPRINTF ("%s" , __func__);if (mis->postcopy_ram_state ==POSTCOPY_RAM_INCOMING_NONE ) {
@@ -1364,7 +1391,17 @@ static int loadvm_postcopy_ram_handle_end(MigrationIncomingState *mis) mis->postcopy_ram_state);return -1 ; } -return -1 ;/* TODO - expecting 1 byte good/fail */ + +DPRINTF ("loadvm_postcopy_ram_handle_end status=%d" , status); + +/* TODO: Give up on non-0 status +* TODO: If 0 status, check we've received everything (all outstanding +* requests should already have been completed) +* TODO: Indicate completion back to source +*/ +/* TODO: Or should this be none? */ + mis->postcopy_ram_state =POSTCOPY_RAM_INCOMING_END ; +return LOADVM_EXITCODE_QUITLOOP ; } static int loadvm_process_command_simple_lencheck(const char *name,
@@ -1505,7 +1542,7 @@ static int loadvm_process_command(QEMUFile *f, len, 1)) {return -1 ; } -return loadvm_postcopy_ram_handle_end (mis); +return loadvm_postcopy_ram_handle_end (mis,qemu_get_byte (f));default :error_report ("VM_COMMAND 0x%x unknown (len 0x%x)" , com, len);
Signature · 1 line
1.9.3
PATCH 44/46 postcopy: Use userfaultfd
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
userfaultfd is a Linux syscall that gives an fd that receives a stream of notifications of accesses to pages marked as MADV_USERFAULT, and allows the program to acknowledge those stalls and tell the accessing thread to carry on.
--- postcopy-ram.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 6 deletions(-)
postcopy-ram.c +89 -6
@@ -68,6 +68,14 @@#define __NR_remap_anon_pages 317#endif +#ifndef __NR_userfaultfd +#define __NR_userfaultfd 318 +#endif + +#ifndef USERFAULTFD_PROTOCOL +#define USERFAULTFD_PROTOCOL (uint64_t)0xaa +#endif +/* ---------------------------------------------------------------------- */ /* Postcopy pagemap-inbound (pmi) - data structures that record the */ /* state of each page used by the inbound postcopy */
@@ -192,6 +200,7 @@ int postcopy_ram_hosttest(void)*/ void *testarea, *testarea2;long pagesize =getpagesize (); +int ufd; testarea =mmap (NULL , pagesize,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 );
@@ -201,15 +210,24 @@ int postcopy_ram_hosttest(void) }g_assert (((size_t )testarea & (pagesize-1 )) ==0 ); + ufd =syscall (__NR_userfaultfd,O_CLOEXEC ); +if (ufd ==-1 ) { +perror ("postcopy_ram_hosttest: userfaultfd not available" ); +munmap (testarea, pagesize); +return -1 ; + } +if (madvise (testarea, pagesize,MADV_USERFAULT )) {perror ("postcopy_ram_hosttest: MADV_USERFAULT not available" );munmap (testarea, pagesize); +close (ufd);return -1 ; }if (madvise (testarea, pagesize,MADV_NOUSERFAULT )) {perror ("postcopy_ram_hosttest: MADV_NOUSERFAULT not available" );munmap (testarea, pagesize); +close (ufd);return -1 ; }
@@ -226,11 +244,13 @@ int postcopy_ram_hosttest(void)perror ("postcopy_ram_hosttest: remap_anon_pages not available" );munmap (testarea, pagesize);munmap (testarea2, pagesize); +close (ufd);return -1 ; }munmap (testarea, pagesize);munmap (testarea2, pagesize); +close (ufd);return 0 ; }
@@ -361,6 +381,39 @@ static int postcopy_ram_sensitise_area(const char *block_name, void *host_addr, }/* +* Tell the kernel that we've now got some memory it previously asked for. +* Note: We're not allowed to ack a page which wasn't requested. +*/ +static int ack_userfault (MigrationIncomingState *mis,void *start,size_t len) +{ +uint64_t tmp[2 ]; + +/* Kernel wants the range that's now safe to access */ + tmp[0 ] = (uint64_t )start; + tmp[1 ] = (uint64_t )start + (uint64_t )(len-1 ); + +if (write (mis->userfault_fd, tmp,16 ) !=16 ) { +int e = errno; + +if (e ==ENOENT ) { +/* Kernel said it wasn't waiting - one case where this can +* happen is where two threads triggered the userfault +* and we receive the page and ack it just after we received +* the 2nd request and that ends up deciding it should ack it +* We could optimise it out, but it's rare. +*/ +/*fprintf(stderr, "ack_userfault: %p/%zx ENOENT\n", start, len); */ +return 0 ; + } +error_report ("postcopy_ram: Failed to notify kernel for %p/%zx (%d)" , + start, len, e); +return -errno; + } + +return 0 ; +} + +/* * Handle faults detected by the USERFAULT markings */ static void *postcopy_ram_fault_thread (void *opaque)
@@ -420,10 +473,9 @@ static void *postcopy_ram_fault_thread(void *opaque)/* Already arrived - no state change, just kick the kernel */ DPRINTF ("postcopy_ram_fault_thread: notify pre of %p" , hostaddr); -/* TODO! Send ack if (ack_userfault (mis, hostaddr, hostpagesize)) {assert (0 ); -} */ + }break ;case POSTCOPY_PMI_MISSING :
@@ -464,8 +516,33 @@ static void *postcopy_ram_fault_thread(void *opaque)int postcopy_ram_enable_notify (MigrationIncomingState *mis) { -/* Create the fault handler thread and wait for it to be ready */ - mis->userfault_fd =-1 ;/* TODO */ +uint64_t tmp64; + +/* Open the fd for the kernel to give us userfaults */ + mis->userfault_fd =syscall (__NR_userfaultfd,O_CLOEXEC ); +if (mis->userfault_fd ==-1 ) { +perror ("Failed to open userfault fd" ); +return -1 ; + } + +/* +* Version handshake, we send it the version we want and expect to get the +* same back. +*/ + tmp64 =USERFAULTFD_PROTOCOL ; +if (write (mis->userfault_fd, &tmp64,sizeof (tmp64)) !=sizeof (tmp64)) { +perror ("Writing userfaultfd version" ); +return -1 ; + } +if (read (mis->userfault_fd, &tmp64,sizeof (tmp64)) !=sizeof (tmp64)) { +perror ("Reading userfaultfd version" ); +return -1 ; + } +if (tmp64 !=USERFAULTFD_PROTOCOL ) { +error_report ("Mismatched userfaultfd version, expected %zx, got %zx" , + (size_t )USERFAULTFD_PROTOCOL , (size_t )tmp64); + } +qemu_sem_init (&mis->fault_thread_sem,0 );qemu_thread_create (&mis->fault_thread,"postcopy/fault" , postcopy_ram_fault_thread, mis,QEMU_THREAD_JOINABLE );
@@ -476,6 +553,8 @@ int postcopy_ram_enable_notify(MigrationIncomingState *mis)return -1 ; } +DPRINTF ("postcopy_ram_enable_notify: Sensitised" ); +return 0 ; }
@@ -509,11 +588,12 @@ int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,if (syscall (__NR_remap_anon_pages, host, from,getpagesize (),0 ) !=getpagesize ()) { +int e = errno;perror ("remap_anon_pages in postcopy_place_page" );fprintf (stderr,"host: %p from: %p pmi=%d\n" , host, from,postcopy_pmi_get_state (mis, bitmap_offset)); -return -errno; +return -e; } tmp_state =postcopy_pmi_get_state (mis, bitmap_offset);
@@ -526,7 +606,10 @@ int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,if (old_state ==POSTCOPY_PMI_REQUESTED ) { -/* TODO: Notify kernel */ +/* Send the kernel the host address that should now be accessible */ +DPRINTF ("%s: Notifying kernel bitmap_offset=0x%lx host=%p" , + __func__, bitmap_offset, host); +return ack_userfault (mis, host,getpagesize ()); }/* TODO: hostpagesize!=targetpagesize case */
Signature · 1 line
1.9.3
PATCH 45/46 End of migration for postcopy
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
Tweak the end of migration cleanup; we don't want to close stuff down at the end of the main stream, since the postcopy is still sending pages on the other thread.
--- migration.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
migration.c +20 -0
@@ -190,6 +190,26 @@ static void process_incoming_migration_co(void *opaque) ret =qemu_loadvm_state (f); +if (mis->postcopy_ram_state ==POSTCOPY_RAM_INCOMING_ADVISE ) { +/* +* Where a migration had postcopy enabled (and thus went to advise) +* but managed to complete within the precopy period +*/ +postcopy_ram_incoming_cleanup (mis); + } + +DPRINTF ("%s: ret=%d postcopy_ram_state=%d" , __func__, ret, + mis->postcopy_ram_state); +if ((ret >=0 ) && + (mis->postcopy_ram_state >POSTCOPY_RAM_INCOMING_ADVISE )) { +/* +* Postcopy was started, cleanup should happen at the end of the +* postcopy thread. +*/ +DPRINTF ("process_incoming_migration_co: exiting main branch" ); +return ; + } + f->mis =NULL ;qemu_fclose (f);free_xbzrle_decoded_buf ();
Signature · 1 line
1.9.3
PATCH 46/46 Start documenting how postcopy works.
In reply to Dr. David Alan Gilbert (git)From: "Dr. David Alan Gilbert" <[email protected]>
--- docs/migration.txt | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+)
docs/migration.txt +148 -0
@@ -294,3 +294,151 @@ save/send this state when we are in the middle of a pio operation (that is what ide_drive_pio_state_needed() checks). If DRQ_STAT is not enabled, the values on that fields are garbage and don't need to be sent. + += Return path = + +In most migration scenarios there is only a single data path that runs +from the source VM to the destination, typically along a single fd (although +possibly with another fd or similar for some fast way of throwing pages across). + +However, some uses need two way comms; in particular the Postcopy destination +needs to be able to request pages on demand from the source. + +For these scenarios there is a 'return path' from the destination to the source; +qemu_file_get_return_path(QEMUFile* fwdpath) gives the QEMUFile* for the return +path. + + Source side + Forward path - written by migration thread + Return path - opened by main thread, read by fd_handler on main thread + + Destination side + Forward path - read by main thread + Return path - opened by main thread, written by main thread AND postcopy + thread (protected by rp_mutex) + +Opening the return path generally sets the fd to be non-blocking so that a +failed destination can't block the source; and since the non-blockingness seems +to follow both directions it does alter the semantics of the forward path. + += Postcopy = +'Postcopy' migration is a way to deal with migrations that refuse to converge; +it's plus side is that there is an upper bound on the amount of migration traffic +and time it takes, the down side is that during the postcopy phase, a failure of +*either* side or the network connection causes the guest to be lost. + +In postcopy the destination CPUs are started before all the memory has been +transferred, and accesses to pages that are yet to be transferred cause +a fault that's translated by QEMU into a request to the source QEMU. + +Postcopy can be combined with precopy (i.e. normal migration) so that if precopy +doesn't finish in a given time the switch is automatically made to precopy. + +=== Enabling postcopy === + +To enable pure postcopy: + +migrate_set_capability x-postcopy-ram on + +To add a period of precopy: + +migrate_set_parameter x-postcopy-start-time 500 + +(time in ms) + +=== Postcopy states === +Postcopy moves through a series of states (see postcopy_ram_state) +from ADVISE->LISTEN->RUNNING->END + + Advise: Set at the start of migration if postcopy is enabled, even + if it hasn't passed the start-time threshold; here the destination + checks it's OS has the support needed for postcopy, and performs + setup to ensure the RAM mappings are suitable for later postcopy. + (Triggered by reception of POSTCOPY_RAM_ADVISE command) + +Normal precopy now carries on as normal, until the point that the source +hits the start-time threshold and transitions to postcopy. The source +stops it's CPUs and transmits a 'discard bitmap' indicating pages that +have been previously sent but are now dirty again and hence are out of +date on the destination. + +The migration stream now contains a 'package' containing it's own chunk +of migration stream, followed by a return to a normal stream containing +page data. The package (sent as CMD_PACKAGED) contains the commands to +cycle the states on the destination, followed by all of the device +state excluding RAM. This lets the destination request pages from the +source in parallel with loading device state, this is required since +some devices (virtio) access guest memory during device initialisation. + + Listen: The first command in the package, POSTCOPY_RAM_LISTEN, switches + the destination state to Listen, and starts a new thread + (the 'listen thread') which takes over the job of receiving + pages off the migration stream, while the main thread carries + on processing the blob. With this thread able to process page + reception, the destination now 'sensitises' the RAM to detect + any access to missing pages (on Linux using the 'userfault' + system). + +The package now contains all the remaining state data and the command +to transition to the next state. + + Running: POSTCOPY_RAM_RUN causes the destination to synchronise all + state and start the CPUs and IO devices running. The main + thread now finishes processing the migration package and + now carries on as it would for normal precopy migration + (although it can't do the cleanup it would do as it + finishes a normal migration). + +Page data is sent from the source to the destination both as part +of a linear scan (like normal migration), and received by the 'listen thread', +When the destination tries to use a page it hasn't got, it requests +it from the source (down the return path) and the source sends this +page in the same stream. When the source has transmitted all pages +it sends a POSTCOPY_RAM_END command to transition to + + End: The listen thread can now quit, and perform the cleanup of migration +state, the migration is now complete. + +=== Source side page maps === +The source side keeps two bitmaps during postcopy; 'the migration bitmap' +and 'sent map'. The 'migration bitmap' is basically the same as in +the precopy case, and holds a bit to indicate that page is 'dirty' - +i.e. needs sending. During the precopy phase this is updated as the CPU +dirties pages, however during postcopy the CPUs are stopped and nothing +should dirty anything any more. + +The 'sent map' is used for the transition to postcopy. It is a bitmap that +has a bit set whenever a page is sent to the destination, however during +the transition to postcopy mode it is masked against the migration bitmap +(sentmap &= migrationbitmap) to generate a bitmap recording pages that +have been previously been sent but are now dirty again. This masked +sentmap is sent to the destination which discards those now dirty pages +before starting the CPUs. + +Note that once in postcopy mode, the sent map is still updated, however it's +contents are not-consistent as a local view of what's been sent since it's +only got the masked result. + +=== Destination side page maps === +(Needs to be changed so we can update both easily - at the moment updates are done + with a lock) +The destination keeps a 'requested map' and a 'received map'. +Both maps are initially 0, as pages are received the bits are set in 'received map'. +Incoming requests from the kernel cause the bit to be set in the 'requested map'. +When a page is received that is marked as 'requested' the kernel is notified. +If the kernel requests a page that has already been 'received' the kernel is notified +without re-requesting. + +This leads to three valid page states: +page states: + missing (!rc,!rq) - page not yet received or requested + received (rc,!rq) - Page received + requested (!rc,rq) - page requested but not yet received + +state transitions: + received -> missing (only during setup/discard) + + missing -> received (normal incoming page) + requested -> received (incoming page previously requested) + missing -> requested (userfault request) +
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH 07/46] Return path: Open a return path on QEMUFile for sockets
In reply to Dr. David Alan Gilbert (git)Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 67 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs a method to send messages from the destination back to the source, this is the 'return path'.
Wire it up for 'socket' QEMUFile's using a dup'd fd.
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/migration/qemu-file.h | 8 +++++ qemu-file.c | 74 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 6 deletions(-)include/migration/qemu-file.h +8
-0@@ -87,6 +87,11 @@ typedef size_t (QEMURamSaveFunc)(QEMUFile *f, void *opaque,#define QEMUFILE_IO_BUF_SIZE 32768#define QEMUFILE_MAX_IOV_SIZE MIN(IOV_MAX, 64) +/* +* Return a QEMUFile for comms in the opposite direction +*/ +typedef QEMUFile *(QEMURetPathFunc )(void *opaque); + typedef struct QEMUFileOps { QEMUFilePutBufferFunc *put_buffer; QEMUFileGetBufferFunc *get_buffer;@@ -97,6 +102,7 @@ typedef struct QEMUFileOps {QEMURamHookFunc *after_ram_iterate;QEMURamHookFunc *hook_ram_load;QEMURamSaveFunc *save_page; +QEMURetPathFunc *get_return_path; } QEMUFileOps;struct QEMUFile {@@ -117,6 +123,7 @@ struct QEMUFile {int last_error; +struct QEMUFile *return_path;MigrationIncomingState *mis; };@@ -202,6 +209,7 @@ void qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate);int64_t qemu_file_get_rate_limit (QEMUFile *f);int qemu_file_get_error (QEMUFile *f);void qemu_file_set_error (QEMUFile *f,int ret); +QEMUFile *qemu_file_get_return_path (QEMUFile *f);void qemu_fflush (QEMUFile *f);static inline void qemu_put_be64s (QEMUFile *f,const uint64_t *pv)qemu-file.c +6
-0@@ -16,6 +16,54 @@ typedef struct QEMUFileSocket {QEMUFile *file; } QEMUFileSocket; +/* Give a QEMUFile* off the same socket but data in the opposite +* direction. +* qemu_fopen_socket marks write fd's as blocking, but doesn't +* touch read fd's status, so we dup the fd just to keep settings +* separate. [TBD: Do I need to explicitly mark as non-block on read?] +*/
qemu-file.c typedef struct QEMUFileSocket { · line 34
Quoted 10 lines
qemu-file.c +10
-0+static QEMUFile *socket_dup_return_path (void *opaque) +{ +QEMUFileSocket *qfs = opaque; +int revfd; +bool this_is_read; +QEMUFile *result; + +/* If it's already open, return it */ +if (qfs->file->return_path) { +return qfs->file->return_path;
Wouldn't this leave a dangling file descriptor if you call socket_dup_return_path twice, and then close the original QEMUFile?
Quoted 8 lines
qemu-file.c +8
-0+ } + +if (qemu_file_get_error (qfs->file)) { +/* If the forward file is in error, don't try and open a return */ +return NULL ; + } + +/* I don't think there's a better way to tell which direction 'this' is */
qemu-file.c typedef struct QEMUFileSocket { · line 52
Quoted 10 lines
qemu-file.c +10
-0+ this_is_read = qfs->file->ops->get_buffer !=NULL ; + + revfd =dup (qfs->fd); +if (revfd ==-1 ) { +error_report ("Error duplicating fd for return path: %s" , +strerror (errno)); +return NULL ; + } + +qemu_set_nonblock (revfd);
Blocking/nonblocking is per-file *description*, not descriptor. So you're making the original QEMUFile nonblocking as well. Can you explain why this is needed before I reach the meat of the patch series?
In other words, can you draw a table with source/dest and read/write, and whether it should be blocking or non-blocking?
Paolo
Quoted 56 lines
qemu-file.c +34
-6+ result =qemu_fopen_socket (revfd, this_is_read ?"wb" :"rb" ); + qfs->file->return_path = result; + +if (result) { +/* We are the reverse path of our reverse path (although I don't +expect this to be used, it would stop another dup if it was */ + result->return_path = qfs->file; + }else { +close (revfd); + } + +return result; +} +static ssize_t socket_writev_buffer (void *opaque,struct iovec *iov,int iovcnt,int64_t pos) {@@ -313,17 +361,31 @@ QEMUFile *qemu_fdopen(int fd, const char *mode) }static const QEMUFileOps socket_read_ops = { - .get_fd = socket_get_fd, - .get_buffer = socket_get_buffer, - .close = socket_close + .get_fd = socket_get_fd, + .get_buffer = socket_get_buffer, + .close = socket_close, + .get_return_path = socket_dup_return_path };static const QEMUFileOps socket_write_ops = { - .get_fd = socket_get_fd, - .writev_buffer = socket_writev_buffer, - .close = socket_close + .get_fd = socket_get_fd, + .writev_buffer = socket_writev_buffer, + .close = socket_close, + .get_return_path = socket_dup_return_path }; +/* +* Result: QEMUFile* for a 'return path' for comms in the opposite direction +* NULL if not available +*/ +QEMUFile *qemu_file_get_return_path (QEMUFile *f) +{ +if (!f->ops->get_return_path) { +return NULL ; + } +return f->ops->get_return_path (f->opaque); +} +bool qemu_file_mode_is_not_valid (const char *mode) { if (mode == NULL ||
Re: [Qemu-devel] [PATCH 08/46] Return path: socket_writev_buffer: Block even on non-blocking fd's
In reply to Dr. David Alan Gilbert (git)Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 54 lines
From: "Dr. David Alan Gilbert" <[email protected]>
The return path uses a non-blocking fd so as not to block waiting for the (possibly broken) destination to finish returning a message, however we still want outbound data to behave in the same way and block.
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- qemu-file.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-)qemu-file.c +30
-3@@ -70,12 +70,43 @@ static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,QEMUFileSocket *s = opaque;ssize_t len;ssize_t size =iov_size (iov, iovcnt); +ssize_t offset =0 ; +int err; - len =iov_send (s->fd, iov, iovcnt,0 , size); -if (len < size) { - len = -socket_error (); +while (size >0 ) { + len =iov_send (s->fd, iov, iovcnt, offset, size); + +if (len >0 ) { + size -= len; + offset += len; + } + +if (size >0 ) { + err =socket_error (); + +if (err !=EAGAIN ) { +error_report ("socket_writev_buffer: Got err=%d for (%zd/%zd)" , + err, size, len); +/* +* If I've already sent some but only just got the error, I +* could return the amount validly sent so far and wait for the +* next call to report the error, but I'd rather flag the error +* immediately. +*/ +return -err; + } + +/* Emulate blocking */ +GPollFD pfd; + + pfd.fd = s->fd; + pfd.events =G_IO_OUT |G_IO_ERR ;
qemu-file.c static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt, · line 112
Quoted 10 lines
qemu-file.c +5
-1+ pfd.revents =0 ; +g_poll (&pfd,1 /* 1 fd */ ,-1 /* no timeout */ ); + } } -return len; + +return offset; }static int socket_get_fd (void *opaque)
I guess the table I just asked about would help clarifying this as well.
Also note that g_poll doesn't work on sockets on Windows, but I hope
we can avoid it altogether. :)
Paolo
Re: [Qemu-devel] [PATCH 31/46] Postcopy: Rework migration thread for postcopy mode
In reply to Dr. David Alan Gilbert (git)Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 68 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Switch to postcopy if:
1) There's still a significant amount to transfer
2) Postcopy is enabled
3) It's taken longer than the time set by the parameter.and change the cleanup at the end of migration to match.
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- migration.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 73 insertions(+), 19 deletions(-)migration.c +28
-1@@ -982,16 +982,40 @@ static int postcopy_start(MigrationState *ms)static void *migration_thread (void *opaque) {MigrationState *s = opaque; +/* Used by the bandwidth calcs, updated later */ int64_t initial_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +/* Really, the time we started */ +const int64_t initial_time_fixed = initial_time;int64_t setup_start =qemu_clock_get_ms (QEMU_CLOCK_HOST );int64_t initial_bytes =0 ;int64_t max_size =0 ;int64_t start_time = initial_time; +int64_t pc_start_time; +bool old_vm_running = false; + pc_start_time = s->tunables[MIGRATION_PARAMETER_NAME_X_POSTCOPY_START_TIME ]; + +/* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */ +enum MigrationPhase current_active_type =MIG_STATE_ACTIVE ;qemu_savevm_state_begin (s->file, &s->params); +if (migrate_postcopy_ram ()) { +/* Now tell the dest that it should open it's end so it can reply */ +qemu_savevm_send_openrp (s->file); + +/* And ask it to send an ack that will make stuff easier to debug */ +qemu_savevm_send_reqack (s->file,1 ); + +/* Tell the destination that we *might* want to do postcopy later; +* if the other end can't do postcopy it should fail now, nice and +* early. +*/ +qemu_savevm_send_postcopy_ram_advise (s->file); + } + s->setup_time =qemu_clock_get_ms (QEMU_CLOCK_HOST ) - setup_start; + current_active_type =MIG_STATE_ACTIVE ;migrate_set_state (s,MIG_STATE_SETUP ,MIG_STATE_ACTIVE );DPRINTF ("setup complete\n" );@@ -1012,37 +1036,66 @@ static void *migration_thread(void *opaque)" nonpost=%" PRIu64")\n" , pending_size, max_size, pend_post, pend_nonpost);if (pending_size && pending_size >= max_size) { -qemu_savevm_state_iterate (s->file); +/* Still a significant amount to transfer */ + + current_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); + if (migrate_postcopy_ram() &&
migration.c static void *migration_thread(void *opaque) · line 1052
Quoted 10 lines
migration.c +10
-0+ s->state != MIG_STATE_POSTCOPY_ACTIVE && + pend_nonpost == 0 && + (current_time >= initial_time_fixed + pc_start_time)) { + + if (!postcopy_start(s)) { + current_active_type = MIG_STATE_POSTCOPY_ACTIVE; + } + + continue; + } else {
You don't really need the "else" if you have a continue. However, do you need _any_ of the "else" and "continue"? Would the next iteration of the "while" loop do anything else but invoking qemu_savevm_state_iterate.
Quoted 32 lines
migration.c +13
-16+/* Just another iteration step */ +qemu_savevm_state_iterate (s->file); + } }else {int ret; -DPRINTF ("done iterating\n" ); -qemu_mutex_lock_iothread (); - start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); -qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); - old_vm_running =runstate_is_running (); - - ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE ); -if (ret >=0 ) { -qemu_file_set_rate_limit (s->file,INT64_MAX ); -qemu_savevm_state_complete (s->file); - } -qemu_mutex_unlock_iothread (); - -if (ret <0 ) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_ERROR ); -break ; +DPRINTF ("done iterating pending size %" PRIu64"\n" , + pending_size); + +if (s->state ==MIG_STATE_ACTIVE ) { +qemu_mutex_lock_iothread (); + start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); + old_vm_running =runstate_is_running (); + + ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE );
migration.c static void *migration_thread(void *opaque) · line 1079
Quoted 10 lines
migration.c +10
-0+if (ret >=0 ) { +qemu_file_set_rate_limit (s->file,INT64_MAX ); +qemu_savevm_state_complete (s->file); + } +qemu_mutex_unlock_iothread (); +if (ret <0 ) { +migrate_set_state (s, current_active_type, +MIG_STATE_ERROR ); +break ; + }
I think all this code applies to postcopy as well. Only the body of the first "if" must be replaced by qemu_savevm_state_postcopy_complete for postcopy.
migration.c static void *migration_thread(void *opaque) · line 1080
Quoted 2 lines
migration.c +2
-0+ }else { +assert (s->state ==MIG_STATE_POSTCOPY_ACTIVE );
This can fail if you get a cancel in the meanwhile. You can replace the "if (s->state == MIG_STATE_ACTIVE" by "if (current_active_type == MIG_STATE_ACTIVE)" and remove the assert here. Alternatively:
if (migrate_postcopy_ram()) {
assert(current_active_type == MIG_STATE_ACTIVE);
...
} else {
assert(current_active_type == MIG_STATE_POSTCOPY_ACTIVE);
...
}Quoted 23 lines
migration.c +8
-2+DPRINTF ("postcopy end\n" ); + +qemu_savevm_state_postcopy_complete (s->file); +DPRINTF ("postcopy end after complete\n" ); }if (!qemu_file_get_error (s->file)) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_COMPLETED ); +migrate_set_state (s, current_active_type, +MIG_STATE_COMPLETED );break ; } } }if (qemu_file_get_error (s->file)) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_ERROR ); +migrate_set_state (s, current_active_type,MIG_STATE_ERROR ); +DPRINTF ("migration_thread: file is in error state\n" );break ; } current_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );
migration.c static void *migration_thread(void *opaque) · line 1129
Quoted 4 lines
migration.c +1
-0@@ -1073,6 +1126,7 @@ static void *migration_thread(void *opaque) } } +DPRINTF ("migration_thread: Hit error: case\n" );
This dprintf looks weird.
Paolo
Quoted 4 lines
migration.c +0
-0qemu_mutex_lock_iothread ();if (s->state ==MIG_STATE_COMPLETED ) {int64_t end_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );
Re: [Qemu-devel] [PATCH 15/46] Rework loadvm path for subloops
In reply to Dr. David Alan Gilbert (git)Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 5 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs to have two migration streams loading concurrently; one from memory (with the device state) and the other from the fd with the memory transactions.
Can you explain this?
I would have though the order is
precopy RAM and everything
prepare postcopy RAM ("sent && dirty" bitmap)
finish precopy non-RAM
finish devices
postcopy RAM
Why do you need to have all the packaging stuff and a separate memory-based migration stream for devices? I'm sure I'm missing something. :)
Paolo
Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 3 lines
e) I've added a '
migration_set_parameter' command as somewhere to put integer parameters associated with migration.
e.1) And I use that initially for the length of precopy to try.
Could you have instead a "migrate_start_postcopy" command, and leave the policy to management instead?
We could also (later) add an event for the end of the migration bulk phase, that can help management deciding when to switch?
Paolo
Quoted 7 lines · * Paolo Bonzini ([email protected]) wrote:
Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 3 lines
e) I've added a '
migration_set_parameter' command as somewhere to put integer parameters associated with migration.
e.1) And I use that initially for the length of precopy to try.Could you have instead a "
migrate_start_postcopy" command, and leave the policy to management instead?
Hmm; yes that is probably possible - although with the migration_set_parameter configuration you get the best of both worlds:
1) You can set the parameter to say a few seconds and let QEMU handle it
2) You can set the parameter really large, but (I need to check) you could drop the parameter later and then cause it to kick in.
I also did it this way because it was similar to the way the auto-throttling mechanism.
Quoted 2 lines
We could also (later) add an event for the end of the migration bulk phase, that can help management deciding when to switch?
Yeh, the libvirt people want an event to know when (the existing) migration completes, and this would easily fit onto that.
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 15/46] Rework loadvm path for subloops
In reply to Paolo BonziniQuoted 11 lines · * Paolo Bonzini ([email protected]) wrote:
Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 5 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs to have two migration streams loading concurrently; one from memory (with the device state) and the other from the fd with the memory transactions.
Can you explain this?
I would have though the order is
Quoted 8 lines
precopy RAM and everything
prepare postcopy RAM ("sent && dirty" bitmap)
finish precopy non-RAM
finish devices
postcopy RAMWhy do you need to have all the packaging stuff and a separate memory-based migration stream for devices? I'm sure I'm missing something. :)
The thing you're missing is the details of 'finish devices'. The device emulation may access guest memory as part of loading it's state, so you can't successfully complete 'finish devices' without having the 'postcopy RAM' available to provide pages.
Thus you need to be able to start up 'postcopy RAM' before 'finish devices' has completed, and you can't do that if 'finish devices' is still stuffing data down the fd.
Now, if hypothetically you had:
1) A migration format that let you separate out device state so that you
could load all the state of the device off the fd without calling the device IO code.
2) All devices were good and didn't touch guest memory while loading their
state.
then you could avoid this complexity. However, if you look at how Stefan's BER code tried to do 1 (which I don't do in my way of doing it), it was by using the same trick of stuffing the device data into a dummy memory file
to find out the size of the data. And I'm not convinced (2) will happen
this century.
Quoted 1 line
Paolo
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Il 07/07/2014 16:02, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
Could you have instead a "
migrate_start_postcopy" command, and leave the policy to management instead?Hmm; yes that is probably possible - although with the
migration_set_parameterconfiguration you get the best of both worlds:
1) You can set the parameter to say a few seconds and let QEMU handle it
2) You can set the parameter really large, but (I need to check) you could drop the parameter later and then cause it to kick in.I also did it this way because it was similar to the way the auto-throttling mechanism.
Auto-throttling doesn't let you configure when it kicks in (it doesn't even need support from the destination side). For postcopy you would still have a capability, like auto-throttling, just not the argument.
The reason why I prefer a manual step from management, is because postcopy is a one-way street. Suppose a newer version of management software has started migration with postcopy configured, and then an older version is started. It is probably an invalid thing to do, but the confusion in the older version could be fatal and it's nice if there's an easy way to prevent it.
Paolo
Re: [Qemu-devel] [PATCH 15/46] Rework loadvm path for subloops
In reply to Dr. David Alan GilbertIl 07/07/2014 16:35, Dr. David Alan Gilbert ha scritto:
Quoted 18 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 17 lines
Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 5 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs to have two migration streams loading concurrently; one from memory (with the device state) and the other from the fd with the memory transactions.
Can you explain this?
I would have though the order is
precopy RAM and everything
prepare postcopy RAM ("sent && dirty" bitmap)
finish precopy non-RAM
finish devices
postcopy RAM
Quoted 7 lines
Quoted 2 lines
Why do you need to have all the packaging stuff and a separate memory-based migration stream for devices? I'm sure I'm missing something. :)
The thing you're missing is the details of 'finish devices'. The device emulation may access guest memory as part of loading it's state, so you can't successfully complete 'finish devices' without having the 'postcopy RAM' available to provide pages.
I see. Can you document the flow (preferrably as a reply to this email _and_ in docs/ when you send v2 of the code :))?
From my cursory read of the code it is something like this on the source:
finish precopy non-RAM
start RAM postcopy
for each device
pack up data
send it to destination
and on the destination:
while source sends packet
pick up packet atomically
pass the packet to device loader
(while the loader works, userfaultfd does background magic)
But something is missing still, either some kind of ack is needed between device data sends or userfaultfd needs to be able to process device data packets.
Paolo
Quoted 23 lines
Thus you need to be able to start up 'postcopy RAM' before 'finish devices' has completed, and you can't do that if 'finish devices' is still stuffing data down the fd.
Now, if hypothetically you had:
1) A migration format that let you separate out device state so that you
could load all the state of the device off the fd without calling the device IO code.
2) All devices were good and didn't touch guest memory while loading their
state.then you could avoid this complexity. However, if you look at how Stefan's BER code tried to do 1 (which I don't do in my way of doing it), it was by using the same trick of stuffing the device data into a dummy memory file
to find out the size of the data. And I'm not convinced (2) will happen
this century.Quoted 1 line
Paolo
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 9 lines · * Paolo Bonzini ([email protected]) wrote:
Il 07/07/2014 16:02, Dr. David Alan Gilbert ha scritto:
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Could you have instead a "
migrate_start_postcopy" command, and leave the policy to management instead?Hmm; yes that is probably possible - although with the
migration_set_parameterconfiguration you get the best of both worlds:
1) You can set the parameter to say a few seconds and let QEMU handle it
2) You can set the parameter really large, but (I need to check) you could drop the parameter later and then cause it to kick in.
Quoted 6 lines
Quoted 2 lines
I also did it this way because it was similar to the way the auto-throttling mechanism.
Auto-throttling doesn't let you configure when it kicks in (it doesn't even need support from the destination side). For postcopy you would still have a capability, like auto-throttling, just not the argument.
But auto-throttling is handled automatically by qemu rather than management; and it seems right that it should be a configurable threshold.
Quoted 6 lines
The reason why I prefer a manual step from management, is because postcopy is a one-way street. Suppose a newer version of management software has started migration with postcopy configured, and then an older version is started. It is probably an invalid thing to do, but the confusion in the older version could be fatal and it's nice if there's an easy way to prevent it.
Hmm, the way the setup at the moment is setup the new-version would have had to start the VMs down the one way street, I'm not sure what the old software would do bad to it.
Having said that, I can see some advantages - in particular knowing when it's safe to cancel.
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 15/46] Rework loadvm path for subloops
In reply to Paolo BonziniQuoted 22 lines · * Paolo Bonzini ([email protected]) wrote:
Il 07/07/2014 16:35, Dr. David Alan Gilbert ha scritto:
Quoted 21 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 19 lines
Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 5 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs to have two migration streams loading concurrently; one from memory (with the device state) and the other from the fd with the memory transactions.
Can you explain this?
I would have though the order is
precopy RAM and everything
prepare postcopy RAM ("sent && dirty" bitmap)
finish precopy non-RAM
finish devices
postcopy RAMWhy do you need to have all the packaging stuff and a separate memory-based migration stream for devices? I'm sure I'm missing something. :)
Quoted 7 lines
Quoted 4 lines
The thing you're missing is the details of 'finish devices'. The device emulation may access guest memory as part of loading it's state, so you can't successfully complete 'finish devices' without having the 'postcopy RAM' available to provide pages.
I see. Can you document the flow (preferrably as a reply to this email _and_ in docs/ when you send v2 of the code :))?
Yep, will do; I need to go through and check through it before I write the detail reply.
Dave
Quoted 16 lines
From my cursory read of the code it is something like this on the source:
finish precopy non-RAM
start RAM postcopy
for each device
pack up data
send it to destinationand on the destination:
while source sends packet
pick up packet atomically
pass the packet to device loader
(while the loader works, userfaultfd does background magic)
Quoted 30 lines
But something is missing still, either some kind of ack is needed between device data sends or userfaultfd needs to be able to process device data packets.
Paolo
Quoted 23 lines
Thus you need to be able to start up 'postcopy RAM' before 'finish devices' has completed, and you can't do that if 'finish devices' is still stuffing data down the fd.
Now, if hypothetically you had:
1) A migration format that let you separate out device state so that you
could load all the state of the device off the fd without calling the device IO code.
2) All devices were good and didn't touch guest memory while loading their
state.then you could avoid this complexity. However, if you look at how Stefan's BER code tried to do 1 (which I don't do in my way of doing it), it was by using the same trick of stuffing the device data into a dummy memory file
to find out the size of the data. And I'm not convinced (2) will happen
this century.Quoted 1 line
Paolo
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 01/46] qemu_ram_foreach_block: pass up error value, and down the ramblock name
In reply to Dr. David Alan Gilbert (git)Quoted 5 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
check the return value of the function it calls and error if it's none-0 Fixup
qemu_rdma_init_one_blockthat is the only current caller,
and__qemu_rdma_add_blockthe only function it calls using it.
As long as you are at it, is it worth renaming the use of __qemu prefix in a function name? That namespace is reserved for the compiler/libc, and we shouldn't be stomping on it. Probably best as a separate patch.
Quoted 9 lines
Pass the name of the ramblock to the function; helps in debugging.
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- exec.c | 10 ++++++++-- include/exec/cpu-common.h | 4 ++-- migration-rdma.c | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 01/46] qemu_ram_foreach_block: pass up error value, and down the ramblock name
Quoted 10 lines · * Eric Blake ([email protected]) wrote:
Quoted 5 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
check the return value of the function it calls and error if it's none-0 Fixup
qemu_rdma_init_one_blockthat is the only current caller,
and__qemu_rdma_add_blockthe only function it calls using it.As long as you are at it, is it worth renaming the use of
__qemuprefix in a function name? That namespace is reserved for the compiler/libc, and we shouldn't be stomping on it. Probably best as a separate patch.
Yeh it sounds separate, but worth doing but this was getting a large enough set of patches already.
Dave
Quoted 4 lines
Quoted 4 lines
Pass the name of the ramblock to the function; helps in debugging.
Quoted 13 lines
Quoted 6 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- exec.c | 10 ++++++++-- include/exec/cpu-common.h | 4 ++-- migration-rdma.c | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-)Reviewed-by Eric Blake <[email protected]>Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 16/46] Add migration-capability boolean for postcopy-ram.
In reply to Dr. David Alan Gilbert (git)Quoted 12 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/migration/migration.h | 1 + migration.c | 9 +++++++++ qapi-schema.json | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-)+++
b/qapi-schema.json
qapi-schema.json line 496
Quoted 6 lines
qapi-schema.json +3
-0@@ -491,10 +491,14 @@ # @auto-converge: If enabled, QEMU will automatically throttle down the guest # to speed up convergence of RAM migration. (since 1.6) # +# @x-postcopy-ram: Start executing on the migration target before all of RAM has been +# migrated, pulling the remaining pages along as needed. NOTE: If the +# migration fails during postcopy the VM will fail. (since 2.2)
How does this work with libvirt's current insistence that it manually resumes the guest on the destination in order to give feedback to the source on whether it was successful? I'm not sure if enabling this bool is the right thing to do, or if we just need more visibility (such as events rather than the current state of polling) for libvirt to know that it is time to resume the destination and start the post-copy phase.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 22/46] Migration parameters: Add qmp/hmp commands for setting/viewing
In reply to Dr. David Alan Gilbert (git)Quoted 7 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Add somewhere for the various migration parameters to be set with one command;
As suggested in the thread:
http://lists.gnu.org/archive/html/qemu-devel/2012-11/msg00243.html
That's a bit old; I had a newer suggestion here:
https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02274.html
with positive feedback here and in further followups:
https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg00653.html
Quoted 4 lines
There are many existing migration parameters that are scattered over many individual commands; moving those to this scheme would probably break things for others, so I've left them be.
I don't mind having information duplicated into two places. I'd much rather have all the old commands continue to work one item at a time, but have the new command (or new extension to existing command) globally show all tunables, than trying to force management to mix both old and new calls. That is, if the new approach works, libvirt would rather use JUST the new approach instead of a mix of old and new, when managing all the tunables.
Quoted 26 lines
Preserve the migration tunable values across the reinit of the migration status in the same way that the capability flags are preserved.
Add completion routine for it.
Use the postcopy time out setting as the first parameter.
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- hmp-commands.hx | 17 ++++++++++ hmp.c | 54 ++++++++++++++++++++++++++++++ hmp.h | 4 +++ include/migration/migration.h | 4 +++ migration.c | 78 ++++++++++++++++++++++++++++++++++--------- monitor.c | 25 ++++++++++++++ qapi-schema.json | 50 +++++++++++++++++++++++++++ qmp-commands.hx | 23 +++++++++++++ 8 files changed, 239 insertions(+), 16 deletions(-)+++
b/qapi-schema.jsonqapi-schema.json +0
-0@@ -538,6 +538,56 @@ { 'command': 'query-migrate-capabilities', 'returns': ['MigrationCapabilityStatus']} ##
qapi-schema.json line 550
Quoted 10 lines
qapi-schema.json +10
-0+# @MigrationParameterName +# +# Migration parameter enumeration +# Most existing parameters have separate commands/entries but it seems +# better to group them in the same way as the capability flags +# +# @x-postcopy-start-time: Time (in ms) after the start of migration to consider +# switching to postcopy mode +# +# Since: 2.0
Not even close to 2.0 material. This is 2.2 at the earliest. (Several more instances in this patch)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 16/46] Add migration-capability boolean for postcopy-ram.
In reply to Eric BlakeQuoted 11 lines · * Eric Blake ([email protected]) wrote:
Quoted 9 lines · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- include/migration/migration.h | 1 + migration.c | 9 +++++++++ qapi-schema.json | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-)
Quoted 15 lines
qapi-schema.json line 496
Quoted 8 lines
+++
b/qapi-schema.jsonqapi-schema.json +3
-0@@ -491,10 +491,14 @@ # @auto-converge: If enabled, QEMU will automatically throttle down the guest # to speed up convergence of RAM migration. (since 1.6) # +# @x-postcopy-ram: Start executing on the migration target before all of RAM has been +# migrated, pulling the remaining pages along as needed. NOTE: If the +# migration fails during postcopy the VM will fail. (since 2.2)How does this work with libvirt's current insistence that it manually resumes the guest on the destination in order to give feedback to the source on whether it was successful? I'm not sure if enabling this bool is the right thing to do, or if we just need more visibility (such as events rather than the current state of polling) for libvirt to know that it is time to resume the destination and start the post-copy phase.
That's an interesting overlap with Paolo's question. (and approximately the same answer)
I think what I need to do for that is:
1) As for precopy add the option not to start the destination CPU on entry to postcopy; I think that's OK, because we can carry on in postcopy mode even if the destination CPU isn't running, we just won't generate page requests.
2) Finally fix up the old request libvirt has for events based on migration state.
Admittedly I don't quite understand how (1) is supposed to interact with device state.
Dave
Quoted 4 lines
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 16 lines · * Paolo Bonzini ([email protected]) wrote:
Il 07/07/2014 16:02, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
Could you have instead a "
migrate_start_postcopy" command, and leave the policy to management instead?Hmm; yes that is probably possible - although with the
migration_set_parameterconfiguration you get the best of both worlds:
1) You can set the parameter to say a few seconds and let QEMU handle it
2) You can set the parameter really large, but (I need to check) you could drop the parameter later and then cause it to kick in.I also did it this way because it was similar to the way the auto-throttling mechanism.
Auto-throttling doesn't let you configure when it kicks in (it doesn't even need support from the destination side). For postcopy you would still have a capability, like auto-throttling, just not the argument.
Quoted 6 lines
The reason why I prefer a manual step from management, is because postcopy is a one-way street. Suppose a newer version of management software has started migration with postcopy configured, and then an older version is started. It is probably an invalid thing to do, but the confusion in the older version could be fatal and it's nice if there's an easy way to prevent it.
Actually the 'migrate_start_postcopy' idea is growing on me; Eric is that also your preferred way of doing it?
If we did this I'd:
1) Remove the migration_set_parameter code I added
2) and the x-postcopy_ram_start_time parameter
3) Add a new command migrate_start_postcopy that just sets a flag which is tested in the same place as I currently check the timeout. If it's issued after a migration has finished it doesn't fail because that would be racy. If issued before a migration starts that's OK as long as postcopy is enabled and means to start postcopy mode immediately.
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 27 lines · Dr. David Alan Gilbert
Quoted 16 lines · * Paolo Bonzini ([email protected]) wrote:
Il 07/07/2014 16:02, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
Could you have instead a "
migrate_start_postcopy" command, and leave the policy to management instead?Hmm; yes that is probably possible - although with the
migration_set_parameterconfiguration you get the best of both worlds:
1) You can set the parameter to say a few seconds and let QEMU handle it
2) You can set the parameter really large, but (I need to check) you could drop the parameter later and then cause it to kick in.I also did it this way because it was similar to the way the auto-throttling mechanism.
Auto-throttling doesn't let you configure when it kicks in (it doesn't even need support from the destination side). For postcopy you would still have a capability, like auto-throttling, just not the argument.
Quoted 6 lines
The reason why I prefer a manual step from management, is because postcopy is a one-way street. Suppose a newer version of management software has started migration with postcopy configured, and then an older version is started. It is probably an invalid thing to do, but the confusion in the older version could be fatal and it's nice if there's an easy way to prevent it.
Actually the '
migrate_start_postcopy' idea is growing on me; Eric is that also your preferred way of doing it?
Quoted 9 lines
If we did this I'd:
1) Remove themigration_set_parametercode I added
2) and the x-postcopy_ram_start_time parameter
3) Add a new commandmigrate_start_postcopythat just sets a flag which is tested in the same place as I currently check the timeout. If it's issued after a migration has finished it doesn't fail because that would be racy. If issued before a migration starts that's OK as long as postcopy is enabled and means to start postcopy mode immediately.
So to make sure I understand, the idea is that the management starts migration as normal, then after enough time has elapsed, issues a migrate_start_postcopy to tell qemu that it is okay to switch to postcopy at the next convenient opportunity? Is there any need for an event telling libvirt that enough pre-copy has occurred to make a postcopy worthwhile? And of course, I _still_ want an event for when normal precopy migration is ready (instead of the current solution of libvirt having to poll to track progress).
But in answer to your question, yes, it sounds like adding a new command (actually, per QMP conventions it should probably be migrate-start-postcopy with dashes instead of underscore) for management to determine if/when to allow postcopy to kick in seems okay.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 38 lines · * Eric Blake ([email protected]) wrote:
Quoted 27 lines · Dr. David Alan Gilbert
Quoted 16 lines · * Paolo Bonzini ([email protected]) wrote:
Il 07/07/2014 16:02, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
Could you have instead a "
migrate_start_postcopy" command, and leave the policy to management instead?Hmm; yes that is probably possible - although with the
migration_set_parameterconfiguration you get the best of both worlds:
1) You can set the parameter to say a few seconds and let QEMU handle it
2) You can set the parameter really large, but (I need to check) you could drop the parameter later and then cause it to kick in.I also did it this way because it was similar to the way the auto-throttling mechanism.
Auto-throttling doesn't let you configure when it kicks in (it doesn't even need support from the destination side). For postcopy you would still have a capability, like auto-throttling, just not the argument.
Quoted 6 lines
The reason why I prefer a manual step from management, is because postcopy is a one-way street. Suppose a newer version of management software has started migration with postcopy configured, and then an older version is started. It is probably an invalid thing to do, but the confusion in the older version could be fatal and it's nice if there's an easy way to prevent it.
Actually the '
migrate_start_postcopy' idea is growing on me; Eric is that also your preferred way of doing it?Quoted 9 lines
If we did this I'd:
1) Remove themigration_set_parametercode I added
2) and the x-postcopy_ram_start_time parameter
3) Add a new commandmigrate_start_postcopythat just sets a flag which is tested in the same place as I currently check the timeout. If it's issued after a migration has finished it doesn't fail because that would be racy. If issued before a migration starts that's OK as long as postcopy is enabled and means to start postcopy mode immediately.
Quoted 4 lines
So to make sure I understand, the idea is that the management starts migration as normal, then after enough time has elapsed, issues a
migrate_start_postcopyto tell qemu that it is okay to switch to postcopy at the next convenient opportunity?
Yep, that's the idea.
Quoted 3 lines
Is there any need for an
event telling libvirt that enough pre-copy has occurred to make a postcopy worthwhile?
I'm not sure that qemu knows much more than management does at that point; any such decision you can make based on an arbitrary cut off (i.e. migration is taking too long) or you could consider something based on some of the other stats that migration already exposes (like the dirty pages stats); if we've got any more stats that you need we can always expose them.
Quoted 3 lines
And of course, I _still_ want an event for when
normal precopy migration is ready (instead of the current solution of libvirt having to poll to track progress).
Agreed; although we can just do that independently of this big patch set.
Quoted 4 lines
But in answer to your question, yes, it sounds like adding a new command (actually, per QMP conventions it should probably be migrate-start-postcopy with dashes instead of underscore) for management to determine if/when to allow postcopy to kick in seems okay.
OK, I'll do it.
Dave
Quoted 5 lines
Signature · 3 lines
-- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 5 lines · Dr. David Alan Gilbert
Quoted 3 lines · * Eric Blake ([email protected]) wrote:
Is there any need for an
event telling libvirt that enough pre-copy has occurred to make a postcopy worthwhile?
Quoted 8 lines
I'm not sure that qemu knows much more than management does at that point; any such decision you can make based on an arbitrary cut off (i.e. migration is taking too long) or you could consider something based on some of the other stats that migration already exposes (like the dirty pages stats); if we've got any more stats that you need we can always expose them.
Agreed; although we can just do that independently of this big patch set.
It can be independent yes, but I think such event is needed (and once we add such event I hope we can get rid of the polling libvirt is doing for pure precopy too).
I think for very large guests what should happen is a single _lazy_ pass of precopy and then immediately postcopy.
That's why I think an event that notifies libvirt when it should issue the postcopy command is good, to be able to implement the single _lazy_ pass and nothing more than that.
qemu should stop precopy and the source guest just before sending the event, so then libvirt can assign all storage to the destination just before issuing the postcopy commmand. By the time the event has been raised by qemu, the guest in the source qemu must never run anymore. So it is actually the same event needed in pure precopy too (except when using precopy+postcopy the "precopy complete" event will fire much sooner). We'll still need a parameter to precopy to tell qemu when precopy should stop.
The single precopy lazy pass would consist of clearing the dirty bitmap, starting precopy, then if any page is found dirty by the time precopy tries to send it, we skip it. We only send those pages in precopy that haven't been modified yet by the time we reach them in precopy.
Pages heavily modified will be sent purely through postcopy. Ultimately postcopy will be a page sorting feature to massively decrease the downtime latency, and to reduce to 2*ramsize the maximum amount of data transferred on the network without having to slow down the guest artificially. We'll also know exactly the maximum time in advance that it takes to migrate a large host no matter the load in it (2*ramsize divided by the network bandwidth available at the migration time). It'll be totally deterministic, no black magic slowdowns anymore.
Quoted 26 lines · * Andrea Arcangeli ([email protected]) wrote:
Quoted 5 lines · Dr. David Alan Gilbert
Quoted 3 lines · * Eric Blake ([email protected]) wrote:
Is there any need for an
event telling libvirt that enough pre-copy has occurred to make a postcopy worthwhile?Quoted 8 lines
I'm not sure that qemu knows much more than management does at that point; any such decision you can make based on an arbitrary cut off (i.e. migration is taking too long) or you could consider something based on some of the other stats that migration already exposes (like the dirty pages stats); if we've got any more stats that you need we can always expose them.
Agreed; although we can just do that independently of this big patch set.
It can be independent yes, but I think such event is needed (and once we add such event I hope we can get rid of the polling libvirt is doing for pure precopy too).
I think for very large guests what should happen is a single _lazy_ pass of precopy and then immediately postcopy.
That's why I think an event that notifies libvirt when it should issue the postcopy command is good, to be able to implement the single _lazy_ pass and nothing more than that.
Quoted 8 lines
qemu should stop precopy and the source guest just before sending the event, so then libvirt can assign all storage to the destination just before issuing the postcopy commmand. By the time the event has been raised by qemu, the guest in the source qemu must never run anymore. So it is actually the same event needed in pure precopy too (except when using precopy+postcopy the "precopy complete" event will fire much sooner). We'll still need a parameter to precopy to tell qemu when precopy should stop.
That's an interesting different type of event; I think we probably have that first pass information but it's not part of the 'state' (i.e. whether it's started/completed/cancelled enum).
Quoted 6 lines
The single precopy lazy pass would consist of clearing the dirty bitmap, starting precopy, then if any page is found dirty by the time precopy tries to send it, we skip it. We only send those pages in precopy that haven't been modified yet by the time we reach them in precopy.
Quoted 9 lines
Pages heavily modified will be sent purely through postcopy. Ultimately postcopy will be a page sorting feature to massively decrease the downtime latency, and to reduce to 2*ramsize the maximum amount of data transferred on the network without having to slow down the guest artificially. We'll also know exactly the maximum time in advance that it takes to migrate a large host no matter the load in it (2*ramsize divided by the network bandwidth available at the migration time). It'll be totally deterministic, no black magic slowdowns anymore.
There is a trade off; killing the precopy does reduce network bandwidth, but the other side of it is that you would incur more postcopy round trips, so your average latency will probably increase.
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 16/46] Add migration-capability boolean for postcopy-ram.
In reply to Dr. David Alan GilbertIl 07/07/2014 22:23, Dr. David Alan Gilbert ha scritto:
Quoted 7 lines
I think what I need to do for that is:
1) As for precopy add the option not to start the destination CPU on entry to postcopy; I think that's OK, because we can carry on in postcopy mode even if the destination CPU isn't running, we just won't generate page requests.Admittedly I don't quite understand how (1) is supposed to interact with device state.
This is just passing "-S" on the destination side. Device state is treated the same as without "-S" and can still generate page requests. The only difference is whether you have a vm_start() or not.
I think it should be possible to restart the VM on the source side after postcopy migration, as long as migration has failed or has been canceled. Whether that makes sense or causes dire disk corruption depends on the particular scenario, but then the same holds for precopy and we don't try at all to prevent "cont" at the end of migration. It makes it much easier for libvirt to restart the source if it cannot continue on the destination.
Paolo
Re: [Qemu-devel] [PATCH 16/46] Add migration-capability boolean for postcopy-ram.
Quoted 6 lines · * Paolo Bonzini ([email protected]) wrote:
Il 07/07/2014 22:23, Dr. David Alan Gilbert ha scritto:
Quoted 5 lines
I think what I need to do for that is:
1) As for precopy add the option not to start the destination CPU on entry to postcopy; I think that's OK, because we can carry on in postcopy mode even if the destination CPU isn't running, we just won't generate page requests.
Quoted 6 lines
Quoted 2 lines
Admittedly I don't quite understand how (1) is supposed to interact with device state.
This is just passing "-S" on the destination side. Device state is treated the same as without "-S" and can still generate page requests. The only difference is whether you have a
vm_start()or not.
Good, that sounds easy enough.
Quoted 6 lines
I think it should be possible to restart the VM on the source side after postcopy migration, as long as migration has failed or has been canceled. Whether that makes sense or causes dire disk corruption depends on the particular scenario, but then the same holds for precopy and we don't try at all to prevent "cont" at the end of migration. It makes it much easier for libvirt to restart the source if it cannot continue on the destination.
Interesting; Andrea fell into accidentally starting his source and was somewhat surprised.
I was just going to add the RAN_STATE_MEMORY_STALE that Lei Li added in the exec-migration patchset.
Dave
Quoted 2 lines
Paolo
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 17 lines
Quoted 7 lines
The single precopy lazy pass would consist of clearing the dirty bitmap, starting precopy, then if any page is found dirty by the time precopy tries to send it, we skip it. We only send those pages in precopy that haven't been modified yet by the time we reach them in precopy.
Quoted 9 lines
Pages heavily modified will be sent purely through postcopy. Ultimately postcopy will be a page sorting feature to massively decrease the downtime latency, and to reduce to 2*ramsize the maximum amount of data transferred on the network without having to slow down the guest artificially. We'll also know exactly the maximum time in advance that it takes to migrate a large host no matter the load in it (2*ramsize divided by the network bandwidth available at the migration time). It'll be totally deterministic, no black magic slowdowns anymore.
Quoted 4 lines
There is a trade off; killing the precopy does reduce network bandwidth, but the other side of it is that you would incur more postcopy round trips, so your average latency will probably increase.
I agree with David on the latency issue. I (along with my colleague) have tried the idea of single iteration precopy and then postcopy (with our own version of pre+post implementation). In case of workloads with huge writable working set size, the VM remains a bit inactive, because of transfer of pages. We coined a new term i.e. perceivable downtime, which can be measured for workloads running some network intensive tasks.
The multiple postcopy round trips will certainly worsen the performance of a memory intensive workload like mcf of SPECCPU 2006 or even memcached based guest is migrated (some of the workloads on which we tested our prototype).
Currently, I don't know how does David's postcopy implementation handles multiple pages, which I will try to investigate in sometime.
--
Sanidhya
Re: [Qemu-devel] [PATCH 37/46] Add assertion to check migration_dirty_pages doesn't go -ve; have seen it happen once but not sure why
In reply to Dr. David Alan Gilbert (git)Quoted 1 line · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Long subject line. Also, spell out "negative" instead of abbreviating "-ve"
Quoted 19 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- arch_init.c | 1 + 1 file changed, 1 insertion(+)arch_init.c +1
-0@@ -439,6 +439,7 @@ ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,if (next < size) {clear_bit (next, migration_bitmap); +assert (migration_dirty_pages >0 ); migration_dirty_pages--; } *bitoffset = next;
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 37/46] Add assertion to check migration_dirty_pages doesn't go -ve; have seen it happen once but not sure why
Quoted 4 lines · * Eric Blake ([email protected]) wrote:
Quoted 1 line · Dr. David Alan Gilbert (git)
From: "Dr. David Alan Gilbert" <[email protected]>
Long subject line. Also, spell out "negative" instead of abbreviating "-ve"
Fixed.
Dave
Quoted 13 lines
Quoted 13 lines
Signed-off-by Dr. David Alan Gilbert <[email protected]>--- arch_init.c | 1 + 1 file changed, 1 insertion(+)arch_init.c +0
-0@@ -439,6 +439,7 @@ ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
Quoted 12 lines
Quoted 7 lines
arch_init.c +1
-0if (next < size) {clear_bit (next, migration_bitmap); +assert (migration_dirty_pages >0 ); migration_dirty_pages--; } *bitoffset = next;Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 15/46] Rework loadvm path for subloops
In reply to Paolo BonziniQuoted 22 lines · * Paolo Bonzini ([email protected]) wrote:
Il 07/07/2014 16:35, Dr. David Alan Gilbert ha scritto:
Quoted 21 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 19 lines
Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 5 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs to have two migration streams loading concurrently; one from memory (with the device state) and the other from the fd with the memory transactions.
Can you explain this?
I would have though the order is
precopy RAM and everything
prepare postcopy RAM ("sent && dirty" bitmap)
finish precopy non-RAM
finish devices
postcopy RAMWhy do you need to have all the packaging stuff and a separate memory-based migration stream for devices? I'm sure I'm missing something. :)
Quoted 7 lines
Quoted 4 lines
The thing you're missing is the details of 'finish devices'. The device emulation may access guest memory as part of loading it's state, so you can't successfully complete 'finish devices' without having the 'postcopy RAM' available to provide pages.
I see. Can you document the flow (preferrably as a reply to this email _and_ in docs/ when you send v2 of the code :))?
I thought I documented enough in the docs/migration.txt stuff in the last patch (see the 'Postcopy states' section); however lets see if I the following is better:
----
Postcopy stream
Loading of device data may cause the device emulation to access guest RAM that may trigger faults that have to be resolved by the source, as such the migration stream has to be able to respond with page data *during* the device load, and hence the device data has to be read from the stream completely before the device load begins to free the stream up. This is acheived by 'packaging' the device data into a blob that's read in one go.
Source behaviour
Until postcopy is entered the migration stream is identical to normal postcopy, except for the addition of a 'postcopy advise' command at the beginning to let the destination know that postcopy might happen. When postcopy starts the source sends the page discard data and then forms the 'package' containing:
Command: 'postcopy ram listen'
The device state
A series of sections, identical to the precopy streams device state stream containing everything except postcopiable devices (i.e. RAM)
Command: 'postcopy ram run'
The 'package' is sent as the data part of a Command: 'CMD_PACKAGED', and the contents are formatted in the same way as the main migration stream.
Destination behaviour
Initially the destination looks the same as precopy, with a single thread reading the migration stream; the 'postcopy advise' and 'discard' commands are processed to change the way RAM is managed, but don't affect the stream processing.
------------------------------------------------------------------------------
1 2 3 4 5 6 7
main -----DISCARD-CMD_PACKAGED ( LISTEN DEVICE DEVICE DEVICE RUN )
thread | |
| (page request)
| \___
v \listen thread: --- page -- page -- page -- page -- page --
a b c
------------------------------------------------------------------------------
On receipt of CMD_PACKAGED (1)
All the data associated with the package - the ( ... ) section in the
diagram - is read into memory (into a QEMUSizedBuffer), and the main thread recurses into qemu_loadvm_state_main to process the contents of the package (2) which contains commands (3,6) and devices (4...)
On receipt of 'postcopy ram listen' - 3 -(i.e. the 1st command in the package) a new thread (a) is started that takes over servicing the migration stream,
while the main thread carries on loading the package. It loads normal
background page data (b) but if during a device load a fault happens (5) the returned page (c) is loaded by the listen thread allowing the main threads device load to carry on.
The last thing in the CMD_PACKAGED is a 'RUN' command (6) letting the destination CPUs start running.
At the end of the CMD_PACKAGED (7) the main thread returns to normal running behaviour and is no longer used by migration, while the listen thread carries
on servicing page data until the end of migration.
----
Is that any better?
DaveP.S. I know of at least one bug in this code at the moment, it happens on a VM that doesn't have many dirty pages where all the pages are transmitted, and hence the listen thread finishes, before the main thread gets to 'run'.
Quoted 15 lines
From my cursory read of the code it is something like this on the source:
finish precopy non-RAM
start RAM postcopy
for each device
pack up data
send it to destinationand on the destination:
while source sends packet
pick up packet atomically
pass the packet to device loader
(while the loader works, userfaultfd does background magic)
Quoted 30 lines
But something is missing still, either some kind of ack is needed between device data sends or userfaultfd needs to be able to process device data packets.
Paolo
Quoted 23 lines
Thus you need to be able to start up 'postcopy RAM' before 'finish devices' has completed, and you can't do that if 'finish devices' is still stuffing data down the fd.
Now, if hypothetically you had:
1) A migration format that let you separate out device state so that you
could load all the state of the device off the fd without calling the device IO code.
2) All devices were good and didn't touch guest memory while loading their
state.then you could avoid this complexity. However, if you look at how Stefan's BER code tried to do 1 (which I don't do in my way of doing it), it was by using the same trick of stuffing the device data into a dummy memory file
to find out the size of the data. And I'm not convinced (2) will happen
this century.Quoted 1 line
Paolo
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 07/46] Return path: Open a return path on QEMUFile for sockets
In reply to Paolo BonziniQuoted 6 lines · * Paolo Bonzini ([email protected]) wrote:
Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 5 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Postcopy needs a method to send messages from the destination back to the source, this is the 'return path'.
<snip>
Quoted 11 lines
Quoted 11 lines
qemu-file.c +11
-0+/* Give a QEMUFile* off the same socket but data in the opposite +* direction. +* qemu_fopen_socket marks write fd's as blocking, but doesn't +* touch read fd's status, so we dup the fd just to keep settings +* separate. [TBD: Do I need to explicitly mark as non-block on read?] +*/ +static QEMUFile *socket_dup_return_path (void *opaque) +{ +QEMUFileSocket *qfs = opaque; +int revfd; +bool this_is_read;
Quoted 8 lines
qemu-file.c typedef struct QEMUFileSocket { · line 34
Quoted 5 lines
qemu-file.c +5
-0+QEMUFile *result; + +/* If it's already open, return it */ +if (qfs->file->return_path) { +return qfs->file->return_path;Wouldn't this leave a dangling file descriptor if you call
socket_dup_return_pathtwice, and then close the original QEMUFile?
Hmm - how?
Quoted 14 lines
Quoted 14 lines
qemu-file.c +14
-0+ } + +if (qemu_file_get_error (qfs->file)) { +/* If the forward file is in error, don't try and open a return */ +return NULL ; + } + +/* I don't think there's a better way to tell which direction 'this' is */ + this_is_read = qfs->file->ops->get_buffer !=NULL ; + + revfd =dup (qfs->fd); +if (revfd ==-1 ) { +error_report ("Error duplicating fd for return path: %s" , +strerror (errno));
Quoted 8 lines
qemu-file.c typedef struct QEMUFileSocket { · line 52
Quoted 4 lines
qemu-file.c +4
-0+return NULL ; + } + +qemu_set_nonblock (revfd);Blocking/nonblocking is per-file *description*, not descriptor. So you're making the original QEMUFile nonblocking as well. Can you explain why this is needed before I reach the meat of the patch series?
Yes, I went through that a few times until I got that it was per-entity not the fd itself, it still makes life easier for the rest of the QEMUFile code to have a separate fd for it (hence still worth the dup).
Quoted 2 lines
In other words, can you draw a table with source/dest and read/write, and whether it should be blocking or non-blocking?
Sure; the non-blocking ness is mostly on the source side; modifying the table in the docs patch a little:
Source side
Forward path - written by migration thread
: It's OK for this to be blocking, but we end up with it being
non-blocking, and modify the socket code to emulate blocking.
Return path - opened by main thread, read by fd_handler on main thread
: Must be non-blocking so as not to block the main thread while
waiting for a partially sent command.
Destination side
Forward path - read by main thread
Return path - opened by main thread, written by main thread AND postcopy
thread (protected by rp_mutex)
I think I'm OK with both these being blocking.
Dave
Quoted 43 lines
Paolo
Quoted 40 lines
qemu-file.c +14
-0+ result =qemu_fopen_socket (revfd, this_is_read ?"wb" :"rb" ); + qfs->file->return_path = result; + +if (result) { +/* We are the reverse path of our reverse path (although I don't +expect this to be used, it would stop another dup if it was */ + result->return_path = qfs->file; + }else { +close (revfd); + } + +return result; +} +static
ssize_tsocket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,int64_t pos){
qemu-file.c +8
-6@@ -313,17 +361,31 @@ QEMUFile *qemu_fdopen(int fd, const char *mode) }static const QEMUFileOps socket_read_ops = { - .get_fd = socket_get_fd, - .get_buffer = socket_get_buffer, - .close = socket_close + .get_fd = socket_get_fd, + .get_buffer = socket_get_buffer, + .close = socket_close, + .get_return_path = socket_dup_return_path };static const QEMUFileOps socket_write_ops = { - .get_fd = socket_get_fd, - .writev_buffer = socket_writev_buffer, - .close = socket_close + .get_fd = socket_get_fd, + .writev_buffer = socket_writev_buffer, + .close = socket_close, + .get_return_path = socket_dup_return_path };
Quoted 17 lines
Quoted 6 lines
qemu-file.c +5
-0+/* +* Result: QEMUFile* for a 'return path' for comms in the opposite direction +* NULL if not available +*/ +QEMUFile *qemu_file_get_return_path (QEMUFile *f)qemu-file.c QEMUFile *qemu_fdopen(int fd, const char *mode) · line 391
Quoted 10 lines
qemu-file.c +7
-0+{ +if (!f->ops->get_return_path) { +return NULL ; + } +return f->ops->get_return_path (f->opaque); +} +bool qemu_file_mode_is_not_valid (const char *mode) { if (mode == NULL ||
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 07/46] Return path: Open a return path on QEMUFile for sockets
Il 16/07/2014 11:37, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 8 lines
qemu-file.c typedef struct QEMUFileSocket { · line 34
Quoted 5 lines
qemu-file.c +4
-0+ +/* If it's already open, return it */ +if (qfs->file->return_path) { +return qfs->file->return_path;Wouldn't this leave a dangling file descriptor if you call
socket_dup_return_pathtwice, and then close the original QEMUFile?Hmm - how?
The problem is that there is no reference count on QEMUFile, so if you do
f1 = open_return_path(f0); f2 = open_return_path(f0); /* now f1 == f2 */ qemu_fclose(f1); /* now f2 is dangling */
The remark about "closing the original QEMUFile" is also related to this part:
if (result) {
/* We are the reverse path of our reverse path (although I don't
expect this to be used, it would stop another dup if it was /
result->return_path = qfs->file;which has a similar bug
f1 = open_return_path(f0); f2 = open_return_path(f1); /* now f2 == f0 */ qemu_fclose(f0); /* now f2 is dangling */
If this is correct, the simplest fix is to drop the optimization.
Quoted 5 lines
Source side
Forward path - written by migration thread
: It's OK for this to be blocking, but we end up with it being
non-blocking, and modify the socket code to emulate blocking.
This likely has a performance impact though. The first migration thread code drop from Juan already improved throughput a lot, even if it kept the iothread all the time and only converted from nonblocking writes to blocking.
Quoted 3 lines
Return path - opened by main thread, read by
fd_handleron main thread
: Must be non-blocking so as not to block the main thread while
waiting for a partially sent command.
Why can't you handle this in the migration thread (or a new postcopy thread on the source side)? Then it can stay blocking.
Quoted 2 lines
Destination side
Forward path - read by main thread
This must be nonblocking so that the monitor keeps responding.
Quoted 2 lines
Return path - opened by main thread, written by main thread AND postcopy
thread (protected byrp_mutex)
When does the main thread needs to write?
If it doesn't need that, you can just switch to blocking when you process the listen command (i.e. when the postcopy thread starts).
Paolo
Quoted 1 line
I think I'm OK with both these being blocking.
Re: [Qemu-devel] [PATCH 07/46] Return path: Open a return path on QEMUFile for sockets
Quoted 12 lines · * Paolo Bonzini ([email protected]) wrote:
Il 16/07/2014 11:37, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 8 lines
qemu-file.c typedef struct QEMUFileSocket { · line 34
Quoted 5 lines
qemu-file.c +4
-0+ +/* If it's already open, return it */ +if (qfs->file->return_path) { +return qfs->file->return_path;Wouldn't this leave a dangling file descriptor if you call
socket_dup_return_pathtwice, and then close the original QEMUFile?Hmm - how?
Quoted 7 lines
The problem is that there is no reference count on QEMUFile, so if you do
f1 = open_return_path(f0); f2 = open_return_path(f0); /* now f1 == f2 */ qemu_fclose(f1); /* now f2 is dangling */
I think from the way I'm using it, I can remove the optimisation, but I do need to check.
I'm not too sure what your worry is about 'f2' in this case; I guess the caller needs to know that it should only close the return path once - is that your worry?
Quoted 10 lines
The remark about "closing the original QEMUFile" is also related to this part:
if (result) { /* We are the reverse path of our reverse path (although I don't expect this to be used, it would stop another dup if it was / result->return_path = qfs->file;which has a similar bug
Quoted 7 lines
f1 = open_return_path(f0); f2 = open_return_path(f1); /* now f2 == f0 */ qemu_fclose(f0); /* now f2 is dangling */If this is correct, the simplest fix is to drop the optimization.
I'm more nervous about dropping that one, because the current scheme does provide a clean way of finding the forward path if you've got the reverse (although I don't think I make use of it).
Quoted 9 lines
Quoted 4 lines
Source side
Forward path - written by migration thread
: It's OK for this to be blocking, but we end up with it being
non-blocking, and modify the socket code to emulate blocking.This likely has a performance impact though. The first migration thread code drop from Juan already improved throughput a lot, even if it kept the iothread all the time and only converted from nonblocking writes to blocking.
Can you give some more reasoning as to why you think this will hit the performance so much; I thought the output buffers were quite big anyway.
Quoted 6 lines
Quoted 3 lines
Return path - opened by main thread, read by
fd_handleron main thread
: Must be non-blocking so as not to block the main thread while
waiting for a partially sent command.Why can't you handle this in the migration thread (or a new postcopy thread on the source side)? Then it can stay blocking.
Handling it within the migration thread would make it much more complicated (which would be bad since it's already complex enough);
A return path thread on the source side, hmm yes that could do it - especially since the migration thread is already a separate thread from the main thread handling this and thus already needs locking paraphernalia.
Quoted 4 lines
Quoted 2 lines
Destination side
Forward path - read by main threadThis must be nonblocking so that the monitor keeps responding.
Interesting, I suspect none of the code in there is set up for that at the moment, so how does that work during migration at the moment?
Actually, I see I missed something here; this should be:
Destination side
Forward path - read by main thread, and listener thread (see the
separate mail that described that listner thread)
and that means that once postcopy is going (and the listener thread started) it can't block the monitor.
Quoted 4 lines
Quoted 2 lines
Return path - opened by main thread, written by main thread AND postcopy
thread (protected byrp_mutex)When does the main thread needs to write?
Not much; the only things the main thread currently responds to are the ReqAck (ping like) requests; those are turning out to be very useful during debug; I've also got the ability for the destination to send a migration result back to the source which seems useful to be able to 'fail' early.
Quoted 2 lines
If it doesn't need that, you can just switch to blocking when you process the listen command (i.e. when the postcopy thread starts).
Why don't I just do it anyway? Prior to postcopy starting we're in the same situation as we're in with precopy today, so can already get mainblock threading.
Dave
Quoted 5 lines
Paolo
Quoted 1 line
I think I'm OK with both these being blocking.
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 07/46] Return path: Open a return path on QEMUFile for sockets
Il 16/07/2014 13:52, Dr. David Alan Gilbert ha scritto:
Quoted 21 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 12 lines
Il 16/07/2014 11:37, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 8 lines
qemu-file.c typedef struct QEMUFileSocket { · line 34
Quoted 5 lines
qemu-file.c +4
-0+ +/* If it's already open, return it */ +if (qfs->file->return_path) { +return qfs->file->return_path;Wouldn't this leave a dangling file descriptor if you call
socket_dup_return_pathtwice, and then close the original QEMUFile?Hmm - how?
Quoted 7 lines
The problem is that there is no reference count on QEMUFile, so if you do
f1 = open_return_path(f0); f2 = open_return_path(f0); /* now f1 == f2 */ qemu_fclose(f1); /* now f2 is dangling */
Quoted 6 lines
I think from the way I'm using it, I can remove the optimisation, but I do need to check.
I'm not too sure what your worry is about 'f2' in this case; I guess the caller needs to know that it should only close the return path once - is that your worry?
Yes. The API is not well encapsulated; a "random" caller of open_return_path does not know (and cannot know) whether it should close the returned file or not.
Quoted 3 lines
I'm more nervous about dropping that one, because the current scheme does provide a clean way of finding the forward path if you've got the reverse (although I don't think I make use of it).
If it isn't used, why keep it?
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Source side
Forward path - written by migration thread
: It's OK for this to be blocking, but we end up with it being
non-blocking, and modify the socket code to emulate blocking.
Quoted 7 lines
Quoted 4 lines
This likely has a performance impact though. The first migration thread code drop from Juan already improved throughput a lot, even if it kept the iothread all the time and only converted from nonblocking writes to blocking.
Can you give some more reasoning as to why you think this will hit the performance so much; I thought the output buffers were quite big anyway.
I don't really know, it's
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Return path - opened by main thread, read by
fd_handleron main thread
: Must be non-blocking so as not to block the main thread while
waiting for a partially sent command.Why can't you handle this in the migration thread (or a new postcopy thread on the source side)? Then it can stay blocking.
Handling it within the migration thread would make it much more complicated (which would be bad since it's already complex enough);
Ok. I'm not sure why it is more complicated since migration is essentially two-phase, one where the source drives it and one where the source just waits for requests, but I'll trust you on this. :)
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Destination side
Forward path - read by main threadThis must be nonblocking so that the monitor keeps responding.
Interesting, I suspect none of the code in there is set up for that at the moment, so how does that work during migration at the moment?
It sure is. :)
On the destination side, migration is done in a coroutine (see process_incoming_migration) so it's all transparent. Only socket_get_buffer has to know about this:
len = qemu_recv(s->fd, buf, size, 0);
if (len != -1) {
break;
}
if (socket_error() == EAGAIN) {
yield_until_fd_readable(s->fd);
} else if (socket_error() != EINTR) {
break;
}If the socket is put in blocking mode recv will never return EAGAIN, so this code will only run if the socket is nonblocking.
Quoted 8 lines
Actually, I see I missed something here; this should be:
Destination side
Forward path - read by main thread, and listener thread (see the
separate mail that described that listner thread)and that means that once postcopy is going (and the listener thread started) it can't block the monitor.
Ok, so the listener thread can do socket_set_block(qemu_get_fd(file)) once it gets its hands on the QEMUFile.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Return path - opened by main thread, written by main thread AND postcopy
thread (protected byrp_mutex)When does the main thread needs to write?
Not much; the only things the main thread currently responds to are the ReqAck (ping like) requests; those are turning out to be very useful during debug; I've also got the ability for the destination to send a migration result back to the source which seems useful to be able to 'fail' early.
Why can't this be done in the listener thread? (Thus transforming it into a more general postcopy migration thread; later we could even change incoming migration from a coroutine to a thread).
Quoted 5 lines
Quoted 2 lines
If it doesn't need that, you can just switch to blocking when you process the listen command (i.e. when the postcopy thread starts).
Why don't I just do it anyway? Prior to postcopy starting we're in the same situation as we're in with precopy today, so can already get mainblock threading.
See above for the explanation.
Paolo
Re: [Qemu-devel] [PATCH 07/46] Return path: Open a return path on QEMUFile for sockets
Quoted 25 lines · * Paolo Bonzini ([email protected]) wrote:
Il 16/07/2014 13:52, Dr. David Alan Gilbert ha scritto:
Quoted 24 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 12 lines
Il 16/07/2014 11:37, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 8 lines
qemu-file.c typedef struct QEMUFileSocket { · line 34
Quoted 5 lines
qemu-file.c +4
-0+ +/* If it's already open, return it */ +if (qfs->file->return_path) { +return qfs->file->return_path;Wouldn't this leave a dangling file descriptor if you call
socket_dup_return_pathtwice, and then close the original QEMUFile?Hmm - how?
Quoted 7 lines
The problem is that there is no reference count on QEMUFile, so if you do
f1 =
open_return_path(f0);
f2 =open_return_path(f0);
/* now f1 == f2 */
qemu_fclose(f1);
/* now f2 is dangling */I think from the way I'm using it, I can remove the optimisation, but I do need to check.
Quoted 7 lines
Quoted 3 lines
I'm not too sure what your worry is about 'f2' in this case; I guess the caller needs to know that it should only close the return path once - is that your worry?
Yes. The API is not well encapsulated; a "random" caller of
open_return_pathdoes not know (and cannot know) whether it should close the returned file or not.
OK, then yes I'll give that a go taking out those optimisations.
Quoted 5 lines
Quoted 3 lines
I'm more nervous about dropping that one, because the current scheme does provide a clean way of finding the forward path if you've got the reverse (although I don't think I make use of it).
If it isn't used, why keep it?
It just felt pleasently symmetric being able to get the forward path by asking for the return path on the return path; but I can remove it.
Quoted 21 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Source side
Forward path - written by migration thread
: It's OK for this to be blocking, but we end up with it being
non-blocking, and modify the socket code to emulate blocking.Quoted 7 lines
Quoted 4 lines
This likely has a performance impact though. The first migration thread code drop from Juan already improved throughput a lot, even if it kept the iothread all the time and only converted from nonblocking writes to blocking.
Can you give some more reasoning as to why you think this will hit the performance so much; I thought the output buffers were quite big anyway.
I don't really know, it's
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Return path - opened by main thread, read by
fd_handleron main thread
: Must be non-blocking so as not to block the main thread while
waiting for a partially sent command.Why can't you handle this in the migration thread (or a new postcopy thread on the source side)? Then it can stay blocking.
Quoted 6 lines
Quoted 2 lines
Handling it within the migration thread would make it much more complicated (which would be bad since it's already complex enough);
Ok. I'm not sure why it is more complicated since migration is essentially two-phase, one where the source drives it and one where the source just waits for requests, but I'll trust you on this. :)
It's not as clean a split like that; during the postcopy phase we still do the linear page scan to send pages before they're requested, so the main migration thread code keeps going.
(There's an 'interesting' balance here, send too many linear pages and they get in the way of the postcopy requests and increase the latency, but sending them means that you get a lot of the pages without having to request them which is 0 latency)
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Destination side
Forward path - read by main threadThis must be nonblocking so that the monitor keeps responding.
Interesting, I suspect none of the code in there is set up for that at the moment, so how does that work during migration at the moment?
It sure is. :)
Oh so it is; I missed the 'qemu_set_nonblock(fd);' in process_incoming_migration
Quoted 14 lines
On the destination side, migration is done in a coroutine (see
process_incoming_migration) so it's all transparent. Onlysocket_get_bufferhas to know about this:len = qemu_recv(s->fd, buf, size, 0); if (len != -1) { break; } if (socket_error() == EAGAIN) { yield_until_fd_readable(s->fd); } else if (socket_error() != EINTR) { break; }
Quoted 2 lines
If the socket is put in blocking mode recv will never return EAGAIN, so this code will only run if the socket is nonblocking.
OK, yes.
Quoted 17 lines
Quoted 8 lines
Actually, I see I missed something here; this should be:
Destination side
Forward path - read by main thread, and listener thread (see the
separate mail that described that listner thread)and that means that once postcopy is going (and the listener thread started) it can't block the monitor.
Ok, so the listener thread can do socket_set_block(qemu_get_fd(file)) once it gets its hands on the QEMUFile.
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Return path - opened by main thread, written by main thread AND postcopy
thread (protected byrp_mutex)When does the main thread needs to write?
Quoted 8 lines
Quoted 4 lines
Not much; the only things the main thread currently responds to are the ReqAck (ping like) requests; those are turning out to be very useful during debug; I've also got the ability for the destination to send a migration result back to the source which seems useful to be able to 'fail' early.
Why can't this be done in the listener thread? (Thus transforming it into a more general postcopy migration thread; later we could even change incoming migration from a coroutine to a thread).
It depends when the ReqAck is sent; i.e. if it's received when the listener thread is running and processing the stream then it's the listener thread that sends the reply.
However, that's not necessarily a big issue now that you've pointed out that
the destination fd is already running non-blocking. If the worst comes to the
worst I could just disable the ReqAck's in non-debug.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
If it doesn't need that, you can just switch to blocking when you process the listen command (i.e. when the postcopy thread starts).
Why don't I just do it anyway? Prior to postcopy starting we're in the same situation as we're in with precopy today, so can already get mainblock threading.
See above for the explanation.
Paolo
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 07/46] Return path: Open a return path on QEMUFile for sockets
Il 16/07/2014 19:10, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Handling it within the migration thread would make it much more complicated (which would be bad since it's already complex enough);
Ok. I'm not sure why it is more complicated since migration is essentially two-phase, one where the source drives it and one where the source just waits for requests, but I'll trust you on this. :)
It's not as clean a split like that; during the postcopy phase we still do the linear page scan to send pages before they're requested, so the main migration thread code keeps going.
Ah, right. As I said, I trusted you! ;)
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Return path - opened by main thread, written by main thread AND postcopy
thread (protected byrp_mutex)When does the main thread needs to write?
Not much; the only things the main thread currently responds to are the ReqAck (ping like) requests; those are turning out to be very useful during debug; I've also got the ability for the destination to send a migration result back to the source which seems useful to be able to 'fail' early.
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Why can't this be done in the listener thread? (Thus transforming it into a more general postcopy migration thread; later we could even change incoming migration from a coroutine to a thread).
It depends when the ReqAck is sent; i.e. if it's received when the listener thread is running and processing the stream then it's the listener thread that sends the reply.
Could the start of the listener thread basically coincide with a handover from the main thread to the listener thread? If so, perhaps you can avoid sending ReqAcks packets before the "start destination" command (which I suppose should move processing of incoming page data from the main thread to the userfaultfd thread). Then the "start destination" command can also change the socket back to blocking.
Paolo
Quoted 4 lines
However, that's not necessarily a big issue now that you've pointed out that
the destination fd is already running non-blocking. If the worst comes to the
worst I could just disable the ReqAck's in non-debug.
Quoted 47 lines · * Eric Blake ([email protected]) wrote:
Quoted 27 lines · Dr. David Alan Gilbert
Quoted 16 lines · * Paolo Bonzini ([email protected]) wrote:
Il 07/07/2014 16:02, Dr. David Alan Gilbert ha scritto:
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
Could you have instead a "
migrate_start_postcopy" command, and leave the policy to management instead?Hmm; yes that is probably possible - although with the
migration_set_parameterconfiguration you get the best of both worlds:
1) You can set the parameter to say a few seconds and let QEMU handle it
2) You can set the parameter really large, but (I need to check) you could drop the parameter later and then cause it to kick in.I also did it this way because it was similar to the way the auto-throttling mechanism.
Auto-throttling doesn't let you configure when it kicks in (it doesn't even need support from the destination side). For postcopy you would still have a capability, like auto-throttling, just not the argument.
Quoted 6 lines
The reason why I prefer a manual step from management, is because postcopy is a one-way street. Suppose a newer version of management software has started migration with postcopy configured, and then an older version is started. It is probably an invalid thing to do, but the confusion in the older version could be fatal and it's nice if there's an easy way to prevent it.
Actually the '
migrate_start_postcopy' idea is growing on me; Eric is that also your preferred way of doing it?Quoted 9 lines
If we did this I'd:
1) Remove themigration_set_parametercode I added
2) and the x-postcopy_ram_start_time parameter
3) Add a new commandmigrate_start_postcopythat just sets a flag which is tested in the same place as I currently check the timeout. If it's issued after a migration has finished it doesn't fail because that would be racy. If issued before a migration starts that's OK as long as postcopy is enabled and means to start postcopy mode immediately.So to make sure I understand, the idea is that the management starts migration as normal, then after enough time has elapsed, issues a
migrate_start_postcopyto tell qemu that it is okay to switch to postcopy at the next convenient opportunity? Is there any need for an event telling libvirt that enough pre-copy has occurred to make a postcopy worthwhile? And of course, I _still_ want an event for when normal precopy migration is ready (instead of the current solution of libvirt having to poll to track progress).
Quoted 4 lines
But in answer to your question, yes, it sounds like adding a new command (actually, per QMP conventions it should probably be migrate-start-postcopy with dashes instead of underscore) for management to determine if/when to allow postcopy to kick in seems okay.
That's implemented in the v2 I just posted.
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 31/46] Postcopy: Rework migration thread for postcopy mode
In reply to Paolo Bonzini* Paolo Bonzini ([email protected]) wrote:
Hi Paolo,
Apologies, I realised I hadn't dug into this comment.
Quoted 75 lines
Il 04/07/2014 19:41, Dr. David Alan Gilbert (git) ha scritto:
Quoted 74 lines
From: "Dr. David Alan Gilbert" <[email protected]>
Switch to postcopy if:
1) There's still a significant amount to transfer
2) Postcopy is enabled
3) It's taken longer than the time set by the parameter.and change the cleanup at the end of migration to match.
Signed-off-by Dr. David Alan Gilbert <[email protected]>---
migration.c| 92 ++++++++++++++++++++++++++++++++++++++++++++++++-------------1 file changed, 73 insertions(+), 19 deletions(-)migration.c +34
-1@@ -982,16 +982,40 @@ static int postcopy_start(MigrationState *ms)static void *migration_thread (void *opaque) {MigrationState *s = opaque; +/* Used by the bandwidth calcs, updated later */ int64_t initial_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +/* Really, the time we started */ +const int64_t initial_time_fixed = initial_time;int64_t setup_start =qemu_clock_get_ms (QEMU_CLOCK_HOST );int64_t initial_bytes =0 ;int64_t max_size =0 ;int64_t start_time = initial_time; +int64_t pc_start_time; +bool old_vm_running = false; + pc_start_time = s->tunables[MIGRATION_PARAMETER_NAME_X_POSTCOPY_START_TIME ]; + +/* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */ +enum MigrationPhase current_active_type =MIG_STATE_ACTIVE ;qemu_savevm_state_begin (s->file, &s->params); +if (migrate_postcopy_ram ()) { +/* Now tell the dest that it should open it's end so it can reply */ +qemu_savevm_send_openrp (s->file); + +/* And ask it to send an ack that will make stuff easier to debug */ +qemu_savevm_send_reqack (s->file,1 ); + +/* Tell the destination that we *might* want to do postcopy later; +* if the other end can't do postcopy it should fail now, nice and +* early. +*/ +qemu_savevm_send_postcopy_ram_advise (s->file); + } + s->setup_time =qemu_clock_get_ms (QEMU_CLOCK_HOST ) - setup_start; + current_active_type =MIG_STATE_ACTIVE ;migrate_set_state (s,MIG_STATE_SETUP ,MIG_STATE_ACTIVE );DPRINTF ("setup complete\n" );@@ -1012,37 +1036,66 @@ static void *migration_thread(void *opaque)" nonpost=%" PRIu64")\n" , pending_size, max_size, pend_post, pend_nonpost);if (pending_size && pending_size >= max_size) { -qemu_savevm_state_iterate (s->file); +/* Still a significant amount to transfer */ + + current_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +if (migrate_postcopy_ram () && + s->state !=MIG_STATE_POSTCOPY_ACTIVE && + pend_nonpost ==0 && + (current_time >= initial_time_fixed + pc_start_time)) { + +if (!postcopy_start (s)) { + current_active_type =MIG_STATE_POSTCOPY_ACTIVE ;
Quoted 8 lines
migration.c static void *migration_thread(void *opaque) · line 1052
Quoted 4 lines
migration.c +4
-0+ } + +continue ; + }else {You don't really need the "else" if you have a continue. However, do you need _any_ of the "else" and "continue"? Would the next iteration of the "while" loop do anything else but invoking
qemu_savevm_state_iterate.
Yes, I've dropped that 'else'; however, I've kept the continue - we're about 3 if's deep here inside the loop and there's a bunch of stuff at the end of the if's but still inside the loop that I'm not 100% sure I want to run again at this point (although it's probably OK).
Quoted 38 lines
Quoted 38 lines
migration.c +19
-16+/* Just another iteration step */ +qemu_savevm_state_iterate (s->file); + } }else {int ret; -DPRINTF ("done iterating\n" ); -qemu_mutex_lock_iothread (); - start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); -qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); - old_vm_running =runstate_is_running (); - - ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE ); -if (ret >=0 ) { -qemu_file_set_rate_limit (s->file,INT64_MAX ); -qemu_savevm_state_complete (s->file); - } -qemu_mutex_unlock_iothread (); - -if (ret <0 ) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_ERROR ); -break ; +DPRINTF ("done iterating pending size %" PRIu64"\n" , + pending_size); + +if (s->state ==MIG_STATE_ACTIVE ) { +qemu_mutex_lock_iothread (); + start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +qemu_system_wakeup_request (QEMU_WAKEUP_REASON_OTHER ); + old_vm_running =runstate_is_running (); + + ret =vm_stop_force_state (RUN_STATE_FINISH_MIGRATE ); +if (ret >=0 ) { +qemu_file_set_rate_limit (s->file,INT64_MAX ); +qemu_savevm_state_complete (s->file); + } +qemu_mutex_unlock_iothread (); +if (ret <0 ) {
Quoted 8 lines
migration.c static void *migration_thread(void *opaque) · line 1079
Quoted 4 lines
migration.c +4
-0+migrate_set_state (s, current_active_type, +MIG_STATE_ERROR ); +break ; + }I think all this code applies to postcopy as well. Only the body of the first "if" must be replaced by
qemu_savevm_state_postcopy_completefor postcopy.
A lot of this stuff is done, but it's done at the point we transition into postcopy, not at the end (see postcopy_start). However, I've not got the wakup_request and old_vm_running check; so I probably need to think where they should go; what's the purpose of the qemu_system_wakeup_request there ? it seems to be getting the guest into running state - which is where I'd assumed it was already.
Quoted 6 lines
migration.c static void *migration_thread(void *opaque) · line 1080
Quoted 2 lines
migration.c +2
-0+ }else { +assert (s->state ==MIG_STATE_POSTCOPY_ACTIVE );This can fail if you get a cancel in the meanwhile. You can replace the "if (
s->state==MIG_STATE_ACTIVE" by "if (current_active_type==MIG_STATE_ACTIVE)" and remove the assert here. Alternatively:
Ah, thanks - fixed in the next version.
Quoted 34 lines
if (migrate_postcopy_ram()) { assert(current_active_type == MIG_STATE_ACTIVE); ... } else { assert(current_active_type == MIG_STATE_POSTCOPY_ACTIVE); ... }Quoted 26 lines
migration.c +8
-2+DPRINTF ("postcopy end\n" ); + +qemu_savevm_state_postcopy_complete (s->file); +DPRINTF ("postcopy end after complete\n" ); }if (!qemu_file_get_error (s->file)) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_COMPLETED ); +migrate_set_state (s, current_active_type, +MIG_STATE_COMPLETED );break ; } } }if (qemu_file_get_error (s->file)) { -migrate_set_state (s,MIG_STATE_ACTIVE ,MIG_STATE_ERROR ); +migrate_set_state (s, current_active_type,MIG_STATE_ERROR ); +DPRINTF ("migration_thread: file is in error state\n" );break ; } current_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );@@ -1073,6 +1126,7 @@ static void *migration_thread(void *opaque) } }
Quoted 3 lines
migration.c static void *migration_thread(void *opaque) · line 1129
Quoted 1 line
migration.c +1
-0+DPRINTF ("migration_thread: Hit error: case\n" );This dprintf looks weird.
Fixed.
Dave
Quoted 8 lines
Paolo
Quoted 4 lines
migration.c +0
-0qemu_mutex_lock_iothread ();if (s->state ==MIG_STATE_COMPLETED ) {int64_t end_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 31/46] Postcopy: Rework migration thread for postcopy mode
Il 28/08/2014 13:04, Dr. David Alan Gilbert ha scritto:
Quoted 8 lines
Quoted 3 lines
You don't really need the "else" if you have a continue. However, do you need _any_ of the "else" and "continue"? Would the next iteration of the "while" loop do anything else but invoking
qemu_savevm_state_iterate.Yes, I've dropped that 'else'; however, I've kept the continue - we're about 3 if's deep here inside the loop and there's a bunch of stuff at the end of the if's but still inside the loop that I'm not 100% sure I want to run again at this point (although it's probably OK).
My point is that the next iteration would start exactly with the else (calling qemu_savevm_state_iterate) and then do the stuff at the end of the if's (bandwidth calculation and all that). So why not do that immediately, without the "continue"?
Quoted 2 lines
A lot of this stuff is done, but it's done at the point we transition into postcopy, not at the end (see
postcopy_start).
Perhaps you can move the common parts to a separate function instead of cut-and-paste? ;)
Quoted 5 lines
However, I've not
got thewakup_requestandold_vm_runningcheck; so I probably need to think where they should go; what's the purpose of theqemu_system_wakeup_requestthere ? it seems to be getting the guest into running state - which is where I'd assumed it was already.
No, it doesn't have to---you could be doing non-live migration. old_vm_running makes sure that if migration fails the VM restarts. You need to grab the state just before force-stopping the VM.
Regarding qemu_system_wakeup_request, it only does something if the virtual machine is suspended-to-RAM; the call should be handling migration of such a VM. The idea is that since we don't transmit the runstate, we just wakeup the VM on the destination. But you need to prepare the VM for that (which is basically a reset plus setting a couple of ACPI registers).
The handling of the request is done here:
if (qemu_wakeup_requested()) {
pause_all_vcpus();
cpu_synchronize_all_states();
qemu_system_reset(VMRESET_SILENT);
notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
wakeup_reason = QEMU_WAKEUP_REASON_NONE;
resume_all_vcpus();
qapi_event_send_wakeup(&error_abort);
}However, it might be broken or might be working by chance only.
Paolo