From 7908c78d3e1a117ae6b9545e601409eee3d33863 Mon Sep 17 00:00:00 2001 From: Juan Quintela Date: Tue, 26 Jun 2012 18:46:10 +0200 Subject: savevm: Live migration handlers register the struct directly Notice that the live migration users never unregister, so no problem about freeing the ops structure. Signed-off-by: Juan Quintela --- arch_init.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index 5b0f562..606b848 100644 --- a/arch_init.c +++ b/arch_init.c @@ -298,7 +298,7 @@ static void migration_end(void) #define MAX_WAIT 50 /* ms, half buffered_file limit */ -int ram_save_live(QEMUFile *f, int stage, void *opaque) +static int ram_save_live(QEMUFile *f, int stage, void *opaque) { ram_addr_t addr; uint64_t bytes_transferred_last; @@ -437,7 +437,7 @@ static inline void *host_from_stream_offset(QEMUFile *f, return NULL; } -int ram_load(QEMUFile *f, void *opaque, int version_id) +static int ram_load(QEMUFile *f, void *opaque, int version_id) { ram_addr_t addr; int flags, ret = 0; @@ -534,6 +534,11 @@ done: return ret; } +SaveVMHandlers savevm_ram_handlers = { + .save_live_state = ram_save_live, + .load_state = ram_load, +}; + #ifdef HAS_AUDIO struct soundhw { const char *name; -- cgit v1.1 From 9b5bfab05f7c2a56d5908117e8cc62015e6b2c63 Mon Sep 17 00:00:00 2001 From: Juan Quintela Date: Tue, 26 Jun 2012 19:26:41 +0200 Subject: savevm: Refactor cancel operation in its own operation Intead of abusing stage with value -1. Signed-off-by: Juan Quintela --- arch_init.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index 606b848..f6bc3fe 100644 --- a/arch_init.c +++ b/arch_init.c @@ -296,6 +296,11 @@ static void migration_end(void) memory_global_dirty_log_stop(); } +static void ram_migration_cancel(void *opaque) +{ + migration_end(); +} + #define MAX_WAIT 50 /* ms, half buffered_file limit */ static int ram_save_live(QEMUFile *f, int stage, void *opaque) @@ -306,11 +311,6 @@ static int ram_save_live(QEMUFile *f, int stage, void *opaque) int ret; int i; - if (stage < 0) { - migration_end(); - return 0; - } - memory_global_sync_dirty_bitmap(get_system_memory()); if (stage == 1) { @@ -537,6 +537,7 @@ done: SaveVMHandlers savevm_ram_handlers = { .save_live_state = ram_save_live, .load_state = ram_load, + .cancel = ram_migration_cancel, }; #ifdef HAS_AUDIO -- cgit v1.1 From d1315aac6e4df1f472a6f87ef6e310b8c109f498 Mon Sep 17 00:00:00 2001 From: Juan Quintela Date: Thu, 28 Jun 2012 15:11:57 +0200 Subject: savevm: split save_live_setup from save_live_state This patch splits stage 1 to its own function for both save_live users, ram and block. It is just a copy of the function, removing the parts of the other stages. Optimizations would came later. Signed-off-by: Juan Quintela --- arch_init.c | 87 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 22 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index f6bc3fe..2a4903c 100644 --- a/arch_init.c +++ b/arch_init.c @@ -303,44 +303,86 @@ static void ram_migration_cancel(void *opaque) #define MAX_WAIT 50 /* ms, half buffered_file limit */ -static int ram_save_live(QEMUFile *f, int stage, void *opaque) +static int ram_save_setup(QEMUFile *f, void *opaque) { ram_addr_t addr; - uint64_t bytes_transferred_last; + RAMBlock *block; double bwidth = 0; int ret; int i; memory_global_sync_dirty_bitmap(get_system_memory()); - if (stage == 1) { - RAMBlock *block; - bytes_transferred = 0; - last_block = NULL; - last_offset = 0; - sort_ram_list(); - - /* Make sure all dirty bits are set */ - QLIST_FOREACH(block, &ram_list.blocks, next) { - for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) { - if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE, - DIRTY_MEMORY_MIGRATION)) { - memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE); - } + bytes_transferred = 0; + last_block = NULL; + last_offset = 0; + sort_ram_list(); + + /* Make sure all dirty bits are set */ + QLIST_FOREACH(block, &ram_list.blocks, next) { + for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) { + if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE, + DIRTY_MEMORY_MIGRATION)) { + memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE); } } + } - memory_global_dirty_log_start(); + memory_global_dirty_log_start(); + + qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE); + + QLIST_FOREACH(block, &ram_list.blocks, next) { + qemu_put_byte(f, strlen(block->idstr)); + qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); + qemu_put_be64(f, block->length); + } + + bwidth = qemu_get_clock_ns(rt_clock); - qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE); + i = 0; + while ((ret = qemu_file_rate_limit(f)) == 0) { + int bytes_sent; - QLIST_FOREACH(block, &ram_list.blocks, next) { - qemu_put_byte(f, strlen(block->idstr)); - qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); - qemu_put_be64(f, block->length); + bytes_sent = ram_save_block(f); + bytes_transferred += bytes_sent; + if (bytes_sent == 0) { /* no more blocks */ + break; + } + /* we want to check in the 1st loop, just in case it was the 1st time + and we had to sync the dirty bitmap. + qemu_get_clock_ns() is a bit expensive, so we only check each some + iterations + */ + if ((i & 63) == 0) { + uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000; + if (t1 > MAX_WAIT) { + DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n", + t1, i); + break; + } } + i++; + } + + if (ret < 0) { + return ret; } + qemu_put_be64(f, RAM_SAVE_FLAG_EOS); + + return 0; +} + +static int ram_save_live(QEMUFile *f, int stage, void *opaque) +{ + uint64_t bytes_transferred_last; + double bwidth = 0; + int ret; + int i; + + memory_global_sync_dirty_bitmap(get_system_memory()); + bytes_transferred_last = bytes_transferred; bwidth = qemu_get_clock_ns(rt_clock); @@ -535,6 +577,7 @@ done: } SaveVMHandlers savevm_ram_handlers = { + .save_live_setup = ram_save_setup, .save_live_state = ram_save_live, .load_state = ram_load, .cancel = ram_migration_cancel, -- cgit v1.1 From 16310a3cca7320edb9341c976f7819de0a8c27e0 Mon Sep 17 00:00:00 2001 From: Juan Quintela Date: Thu, 28 Jun 2012 15:31:37 +0200 Subject: savevm: split save_live into stage2 and stage3 We split it into 2 functions, foo_live_iterate, and foo_live_complete. At this point, we only remove the bits that are for the other stage, functionally this is equivalent to previous code. Signed-off-by: Juan Quintela --- arch_init.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 17 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index 2a4903c..7e04ea5 100644 --- a/arch_init.c +++ b/arch_init.c @@ -374,12 +374,13 @@ static int ram_save_setup(QEMUFile *f, void *opaque) return 0; } -static int ram_save_live(QEMUFile *f, int stage, void *opaque) +static int ram_save_iterate(QEMUFile *f, void *opaque) { uint64_t bytes_transferred_last; double bwidth = 0; int ret; int i; + uint64_t expected_time; memory_global_sync_dirty_bitmap(get_system_memory()); @@ -424,28 +425,64 @@ static int ram_save_live(QEMUFile *f, int stage, void *opaque) bwidth = 0.000001; } - /* try transferring iterative blocks of memory */ - if (stage == 3) { - int bytes_sent; + qemu_put_be64(f, RAM_SAVE_FLAG_EOS); + + expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; + + DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n", + expected_time, migrate_max_downtime()); + + return expected_time <= migrate_max_downtime(); +} + +static int ram_save_complete(QEMUFile *f, void *opaque) +{ + double bwidth = 0; + int ret; + int i; + int bytes_sent; - /* flush all remaining blocks regardless of rate limiting */ - while ((bytes_sent = ram_save_block(f)) != 0) { - bytes_transferred += bytes_sent; + memory_global_sync_dirty_bitmap(get_system_memory()); + + bwidth = qemu_get_clock_ns(rt_clock); + + i = 0; + while ((ret = qemu_file_rate_limit(f)) == 0) { + bytes_sent = ram_save_block(f); + bytes_transferred += bytes_sent; + if (bytes_sent == 0) { /* no more blocks */ + break; } - memory_global_dirty_log_stop(); + /* we want to check in the 1st loop, just in case it was the 1st time + and we had to sync the dirty bitmap. + qemu_get_clock_ns() is a bit expensive, so we only check each some + iterations + */ + if ((i & 63) == 0) { + uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000; + if (t1 > MAX_WAIT) { + DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n", + t1, i); + break; + } + } + i++; } - qemu_put_be64(f, RAM_SAVE_FLAG_EOS); - - if (stage == 2) { - uint64_t expected_time; - expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; + if (ret < 0) { + return ret; + } - DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n", - expected_time, migrate_max_downtime()); + /* try transferring iterative blocks of memory */ - return expected_time <= migrate_max_downtime(); + /* flush all remaining blocks regardless of rate limiting */ + while ((bytes_sent = ram_save_block(f)) != 0) { + bytes_transferred += bytes_sent; } + memory_global_dirty_log_stop(); + + qemu_put_be64(f, RAM_SAVE_FLAG_EOS); + return 0; } @@ -578,7 +615,8 @@ done: SaveVMHandlers savevm_ram_handlers = { .save_live_setup = ram_save_setup, - .save_live_state = ram_save_live, + .save_live_iterate = ram_save_iterate, + .save_live_complete = ram_save_complete, .load_state = ram_load, .cancel = ram_migration_cancel, }; -- cgit v1.1 From 5910d1e6cbb190809d5dd0d75bb5e8398dd3ffb7 Mon Sep 17 00:00:00 2001 From: Juan Quintela Date: Thu, 28 Jun 2012 19:41:52 +0200 Subject: ram: save_live_setup() don't need to sent pages We should send pages on interate phase, not in setup one. This was a "bug". Just removing the loop does what we want. Tested that it works with current ram_load(). Signed-off-by: Juan Quintela --- arch_init.c | 34 ---------------------------------- 1 file changed, 34 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index 7e04ea5..2a6fc77 100644 --- a/arch_init.c +++ b/arch_init.c @@ -307,9 +307,6 @@ static int ram_save_setup(QEMUFile *f, void *opaque) { ram_addr_t addr; RAMBlock *block; - double bwidth = 0; - int ret; - int i; memory_global_sync_dirty_bitmap(get_system_memory()); @@ -338,37 +335,6 @@ static int ram_save_setup(QEMUFile *f, void *opaque) qemu_put_be64(f, block->length); } - bwidth = qemu_get_clock_ns(rt_clock); - - i = 0; - while ((ret = qemu_file_rate_limit(f)) == 0) { - int bytes_sent; - - bytes_sent = ram_save_block(f); - bytes_transferred += bytes_sent; - if (bytes_sent == 0) { /* no more blocks */ - break; - } - /* we want to check in the 1st loop, just in case it was the 1st time - and we had to sync the dirty bitmap. - qemu_get_clock_ns() is a bit expensive, so we only check each some - iterations - */ - if ((i & 63) == 0) { - uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000; - if (t1 > MAX_WAIT) { - DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n", - t1, i); - break; - } - } - i++; - } - - if (ret < 0) { - return ret; - } - qemu_put_be64(f, RAM_SAVE_FLAG_EOS); return 0; -- cgit v1.1 From b81e712d252cf41a0825f5abad0ce2f4746864f1 Mon Sep 17 00:00:00 2001 From: Juan Quintela Date: Thu, 28 Jun 2012 19:48:09 +0200 Subject: ram: save_live_complete() only do one loop We were doing the same loop that stage2, and a new one for stage3. We only need the one for stage3. Signed-off-by: Juan Quintela --- arch_init.c | 32 -------------------------------- 1 file changed, 32 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index 2a6fc77..3c38a52 100644 --- a/arch_init.c +++ b/arch_init.c @@ -403,42 +403,10 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) static int ram_save_complete(QEMUFile *f, void *opaque) { - double bwidth = 0; - int ret; - int i; int bytes_sent; memory_global_sync_dirty_bitmap(get_system_memory()); - bwidth = qemu_get_clock_ns(rt_clock); - - i = 0; - while ((ret = qemu_file_rate_limit(f)) == 0) { - bytes_sent = ram_save_block(f); - bytes_transferred += bytes_sent; - if (bytes_sent == 0) { /* no more blocks */ - break; - } - /* we want to check in the 1st loop, just in case it was the 1st time - and we had to sync the dirty bitmap. - qemu_get_clock_ns() is a bit expensive, so we only check each some - iterations - */ - if ((i & 63) == 0) { - uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000; - if (t1 > MAX_WAIT) { - DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n", - t1, i); - break; - } - } - i++; - } - - if (ret < 0) { - return ret; - } - /* try transferring iterative blocks of memory */ /* flush all remaining blocks regardless of rate limiting */ -- cgit v1.1 From 00d94f3f7648bc3d7126bc6b84d42e22194d9230 Mon Sep 17 00:00:00 2001 From: Juan Quintela Date: Thu, 28 Jun 2012 19:51:17 +0200 Subject: ram: iterate phase We only need to synchronize the bitmap when the number of dirty pages is low. Not every time that we call the function. Signed-off-by: Juan Quintela --- arch_init.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index 3c38a52..b08c615 100644 --- a/arch_init.c +++ b/arch_init.c @@ -348,8 +348,6 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) int i; uint64_t expected_time; - memory_global_sync_dirty_bitmap(get_system_memory()); - bytes_transferred_last = bytes_transferred; bwidth = qemu_get_clock_ns(rt_clock); @@ -398,7 +396,13 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n", expected_time, migrate_max_downtime()); - return expected_time <= migrate_max_downtime(); + if (expected_time <= migrate_max_downtime()) { + memory_global_sync_dirty_bitmap(get_system_memory()); + expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; + + return expected_time <= migrate_max_downtime(); + } + return 0; } static int ram_save_complete(QEMUFile *f, void *opaque) -- cgit v1.1 From 6d2fe810ed995dc79090c2ba179f4f0c694243b1 Mon Sep 17 00:00:00 2001 From: Juan Quintela Date: Thu, 28 Jun 2012 19:58:47 +0200 Subject: ram: save_live_setup() we don't need to synchronize the dirty bitmap. 1st: we were synchonizing the dirty bitmap before calling memory_global_dirty_log_start(). 2nd: We are marking all pages as dirty anywhere, no reason to go through all the bitmap to "mark" dirty same pages twice. So, call removed. Signed-off-by: Juan Quintela --- arch_init.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index b08c615..8cbf8f4 100644 --- a/arch_init.c +++ b/arch_init.c @@ -308,8 +308,6 @@ static int ram_save_setup(QEMUFile *f, void *opaque) ram_addr_t addr; RAMBlock *block; - memory_global_sync_dirty_bitmap(get_system_memory()); - bytes_transferred = 0; last_block = NULL; last_offset = 0; -- cgit v1.1 From 6c779f22a93cc6e4565b940ef616e3efc5b50ba5 Mon Sep 17 00:00:00 2001 From: Orit Wasserman Date: Tue, 10 Jul 2012 12:37:13 +0300 Subject: Change ram_save_block to return -1 if there are no more changes It will return 0 if the page is unmodifed. Signed-off-by: Orit Wasserman --- arch_init.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'arch_init.c') diff --git a/arch_init.c b/arch_init.c index 8cbf8f4..78cdf50 100644 --- a/arch_init.c +++ b/arch_init.c @@ -184,11 +184,19 @@ static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset, static RAMBlock *last_block; static ram_addr_t last_offset; +/* + * ram_save_block: Writes a page of memory to the stream f + * + * Returns: 0: if the page hasn't changed + * -1: if there are no more dirty pages + * n: the amount of bytes written in other case + */ + static int ram_save_block(QEMUFile *f) { RAMBlock *block = last_block; ram_addr_t offset = last_offset; - int bytes_sent = 0; + int bytes_sent = -1; MemoryRegion *mr; if (!block) @@ -354,10 +362,11 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) int bytes_sent; bytes_sent = ram_save_block(f); - bytes_transferred += bytes_sent; - if (bytes_sent == 0) { /* no more blocks */ + /* no more blocks to sent */ + if (bytes_sent < 0) { break; } + bytes_transferred += bytes_sent; /* we want to check in the 1st loop, just in case it was the 1st time and we had to sync the dirty bitmap. qemu_get_clock_ns() is a bit expensive, so we only check each some @@ -405,14 +414,19 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) static int ram_save_complete(QEMUFile *f, void *opaque) { - int bytes_sent; - memory_global_sync_dirty_bitmap(get_system_memory()); /* try transferring iterative blocks of memory */ /* flush all remaining blocks regardless of rate limiting */ - while ((bytes_sent = ram_save_block(f)) != 0) { + while (true) { + int bytes_sent; + + bytes_sent = ram_save_block(f); + /* no more blocks to sent */ + if (bytes_sent < 0) { + break; + } bytes_transferred += bytes_sent; } memory_global_dirty_log_stop(); -- cgit v1.1