diff mbox

[RFC,v3,10/27] COLO: Save VM state to slave when do checkpoint

Message ID 1423711034-5340-11-git-send-email-zhang.zhanghailiang@huawei.com
State New
Headers show

Commit Message

Zhanghailiang Feb. 12, 2015, 3:16 a.m. UTC
We should save PVM's RAM/device to slave when needed.

For VM state, we  will cache them in slave, we use QEMUSizedBuffer
to store the data, we need know the data size of VM state, so in master,
we use qsb to store VM state temporarily, and then migrate the data to
slave.

Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com>
Signed-off-by: Gonglei <arei.gonglei@huawei.com>
Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
---
 arch_init.c                        | 33 ++++++++++++++++--
 include/migration/migration-colo.h |  2 ++
 migration/colo.c                   | 68 +++++++++++++++++++++++++++++++++++---
 savevm.c                           |  2 +-
 stubs/migration-colo.c             |  5 +++
 5 files changed, 102 insertions(+), 8 deletions(-)
diff mbox

Patch

diff --git a/arch_init.c b/arch_init.c
index 89c8fa4..a07ca76 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -52,6 +52,7 @@ 
 #include "exec/ram_addr.h"
 #include "hw/acpi/acpi.h"
 #include "qemu/host-utils.h"
+#include "migration/migration-colo.h"
 
 #ifdef DEBUG_ARCH_INIT
 #define DPRINTF(fmt, ...) \
@@ -767,11 +768,17 @@  static void ram_migration_cancel(void *opaque)
 
 static void reset_ram_globals(void)
 {
-    last_seen_block = NULL;
     last_sent_block = NULL;
     last_offset = 0;
     last_version = ram_list.version;
-    ram_bulk_stage = true;
+    if (migrate_in_colo_state()) {
+        ram_bulk_stage = false;
+    } else {
+        ram_bulk_stage = true;
+    }
+    if (ram_bulk_stage) {
+        last_seen_block = NULL;
+    }
 }
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */
@@ -781,6 +788,18 @@  static int ram_save_setup(QEMUFile *f, void *opaque)
     RAMBlock *block;
     int64_t ram_bitmap_pages; /* Size of bitmap in pages, including gaps */
 
+    /*
+     * migration has already setup the bitmap, reuse it.
+     */
+    if (migrate_in_colo_state()) {
+        qemu_mutex_lock_ramlist();
+        bytes_transferred = 0;
+        migration_dirty_pages = 0;
+        reset_ram_globals();
+        DPRINTF("ram_save_setup for colo\n");
+        goto out_setup;
+    }
+
     mig_throttle_on = false;
     dirty_rate_high_cnt = 0;
     bitmap_sync_count = 0;
@@ -841,6 +860,7 @@  static int ram_save_setup(QEMUFile *f, void *opaque)
     migration_bitmap_sync();
     qemu_mutex_unlock_iothread();
 
+out_setup:
     qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
 
     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
@@ -950,7 +970,14 @@  static int ram_save_complete(QEMUFile *f, void *opaque)
     }
 
     ram_control_after_iterate(f, RAM_CONTROL_FINISH);
-    migration_end();
+
+    /*
+     * Since we need to reuse dirty bitmap in colo,
+     * don't cleanup the bitmap.
+     */
+    if (!migrate_enable_colo() || migration_has_failed(migrate_get_current())) {
+        migration_end();
+    }
 
     qemu_mutex_unlock_ramlist();
     qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
diff --git a/include/migration/migration-colo.h b/include/migration/migration-colo.h
index 9f1443b..0e281b9 100644
--- a/include/migration/migration-colo.h
+++ b/include/migration/migration-colo.h
@@ -26,6 +26,8 @@  struct colo_incoming {
 };
 
 void colo_init_checkpointer(MigrationState *s);
+bool migrate_in_colo_state(void);
+
 /* loadvm */
 extern Coroutine *migration_incoming_co;
 bool loadvm_enable_colo(void);
diff --git a/migration/colo.c b/migration/colo.c
index bb7a1b1..8de0303 100644
--- a/migration/colo.c
+++ b/migration/colo.c
@@ -60,6 +60,9 @@  enum {
 
 static QEMUBH *colo_bh;
 static Coroutine *colo;
+/* colo buffer */
+#define COLO_BUFFER_BASE_SIZE (1000*1000*4ULL)
+QEMUSizedBuffer *colo_buffer;
 
 /* colo checkpoint control helper */
 static int colo_ctl_put(QEMUFile *f, uint64_t request)
@@ -109,9 +112,17 @@  static int colo_ctl_get(QEMUFile *f, uint64_t require)
     return ret;
 }
 
+bool migrate_in_colo_state(void)
+{
+    MigrationState *s = migrate_get_current();
+    return (s->state == MIG_STATE_COLO);
+}
+
 static int do_colo_transaction(MigrationState *s, QEMUFile *control)
 {
     int ret;
+    size_t size;
+    QEMUFile *trans = NULL;
 
     ret = colo_ctl_put(s->file, COLO_CHECKPOINT_NEW);
     if (ret < 0) {
@@ -122,16 +133,47 @@  static int do_colo_transaction(MigrationState *s, QEMUFile *control)
     if (ret < 0) {
         goto out;
     }
+    /* Reset colo buffer and open it for write */
+    qsb_set_length(colo_buffer, 0);
+    trans = qemu_bufopen("w", colo_buffer);
+    if (!trans) {
+        error_report("Open colo buffer for write failed");
+        goto out;
+    }
+
+    /* suspend and save vm state to colo buffer */
+    qemu_mutex_lock_iothread();
+    vm_stop_force_state(RUN_STATE_COLO);
+    qemu_mutex_unlock_iothread();
+    DPRINTF("vm is stoped\n");
+
+    /* Disable block migration */
+    s->params.blk = 0;
+    s->params.shared = 0;
+    qemu_mutex_lock_iothread();
+    qemu_savevm_state_begin(trans, &s->params);
+    qemu_savevm_state_complete(trans);
+    qemu_mutex_unlock_iothread();
 
-    /* TODO: suspend and save vm state to colo buffer */
+    qemu_fflush(trans);
 
     ret = colo_ctl_put(s->file, COLO_CHECKPOINT_SEND);
     if (ret < 0) {
         goto out;
     }
+    /* we send the total size of the vmstate first */
+    size = qsb_get_length(colo_buffer);
+    ret = colo_ctl_put(s->file, size);
+    if (ret < 0) {
+        goto out;
+    }
 
-    /* TODO: send vmstate to slave */
-
+    qsb_put_buffer(s->file, colo_buffer, size);
+    qemu_fflush(s->file);
+    ret = qemu_file_get_error(s->file);
+    if (ret < 0) {
+        goto out;
+    }
     ret = colo_ctl_get(control, COLO_CHECKPOINT_RECEIVED);
     if (ret < 0) {
         goto out;
@@ -143,9 +185,18 @@  static int do_colo_transaction(MigrationState *s, QEMUFile *control)
     }
     DPRINTF("got COLO_CHECKPOINT_LOADED\n");
 
-    /* TODO: resume master */
+    ret = 0;
+    /* resume master */
+    qemu_mutex_lock_iothread();
+    vm_start();
+    qemu_mutex_unlock_iothread();
+    DPRINTF("vm resume to run again\n");
 
 out:
+    if (trans) {
+        qemu_fclose(trans);
+    }
+
     return ret;
 }
 
@@ -171,6 +222,12 @@  static void *colo_thread(void *opaque)
     }
     DPRINTF("get COLO_READY\n");
 
+    colo_buffer = qsb_create(NULL, COLO_BUFFER_BASE_SIZE);
+    if (colo_buffer == NULL) {
+        error_report("Failed to allocate colo buffer!");
+        goto out;
+    }
+
     qemu_mutex_lock_iothread();
     vm_start();
     qemu_mutex_unlock_iothread();
@@ -186,6 +243,9 @@  static void *colo_thread(void *opaque)
 out:
     migrate_set_state(s, MIG_STATE_COLO, MIG_STATE_COMPLETED);
 
+    if (colo_buffer) {
+        qsb_free(colo_buffer);
+    }
 
     if (colo_control) {
         qemu_fclose(colo_control);
diff --git a/savevm.c b/savevm.c
index 7d79a4b..4c8540a 100644
--- a/savevm.c
+++ b/savevm.c
@@ -42,7 +42,7 @@ 
 #include "qemu/iov.h"
 #include "block/snapshot.h"
 #include "block/qapi.h"
-
+#include "migration/migration-colo.h"
 
 #ifndef ETH_P_RARP
 #define ETH_P_RARP 0x8035
diff --git a/stubs/migration-colo.c b/stubs/migration-colo.c
index 7a3dbc5..274dfcf 100644
--- a/stubs/migration-colo.c
+++ b/stubs/migration-colo.c
@@ -20,3 +20,8 @@  void *colo_process_incoming_checkpoints(void *opaque)
 {
     return NULL;
 }
+
+bool migrate_in_colo_state(void)
+{
+    return false;
+}