diff mbox series

[v1,4/7] migration: Implment background sync watcher

Message ID 4c105e23be9a2d1a6be71e6abf9c938a4d091dfc.1726390099.git.yong.huang@smartx.com (mailing list archive)
State New, archived
Headers show
Series migration: auto-converge refinements for huge VM | expand

Commit Message

Yong Huang Sept. 15, 2024, 4:08 p.m. UTC
The background sync watcher is used to detect that if the
iteration lasts a long time, if so, trigger the background
sync.

Signed-off-by: Hyman Huang <yong.huang@smartx.com>
---
 migration/ram.c        | 110 +++++++++++++++++++++++++++++++++++++++++
 migration/ram.h        |   3 ++
 migration/trace-events |   3 ++
 3 files changed, 116 insertions(+)
diff mbox series

Patch

diff --git a/migration/ram.c b/migration/ram.c
index ca5a1b5f16..799eaa0382 100644
--- a/migration/ram.c
+++ b/migration/ram.c
@@ -416,6 +416,11 @@  struct RAMState {
      * RAM migration.
      */
     unsigned int postcopy_bmap_sync_requested;
+
+    /* Background throttle information */
+    bool background_sync_running;
+    QemuThread background_sync_thread;
+    QemuSemaphore quit_sem;
 };
 typedef struct RAMState RAMState;
 
@@ -1125,6 +1130,111 @@  static void migration_bitmap_sync(RAMState *rs,
     }
 }
 
+/*
+ * Iteration lasting more than five seconds is undesirable;
+ * launch a background dirty bitmap sync.
+ */
+#define MIGRATION_MAX_ITERATION_DURATION  5
+
+static void *migration_background_sync_watcher(void *opaque)
+{
+    RAMState *rs = opaque;
+    uint64_t iter_cnt, prev_iter_cnt = 2;
+    bool iter_cnt_unchanged = false;
+    int max_pct = migrate_max_cpu_throttle();
+
+    trace_migration_background_sync_watcher_start();
+    rcu_register_thread();
+
+    while (qatomic_read(&rs->background_sync_running)) {
+        int cur_pct = cpu_throttle_get_percentage();
+        if ((cur_pct == max_pct) || (!migration_is_active())) {
+            break;
+        }
+
+        if (qemu_sem_timedwait(&rs->quit_sem, 1000) == 0) {
+            /* We were woken by background_sync_cleanup, quit */
+            break;
+        }
+
+        /*
+         * The first iteration copies all memory anyhow and has no
+         * effect on guest performance, therefore omit it to avoid
+         * paying extra for the sync penalty.
+         */
+        iter_cnt = stat64_get(&mig_stats.iteration_count);
+        if (iter_cnt <= 1) {
+            continue;
+        }
+
+        iter_cnt_unchanged = (iter_cnt == prev_iter_cnt);
+        prev_iter_cnt = iter_cnt;
+
+        if (iter_cnt_unchanged) {
+            int64_t curr_time, iter_duration;
+
+            curr_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+            iter_duration = curr_time - rs->time_last_bitmap_sync;
+
+            if (iter_duration >
+                    MIGRATION_MAX_ITERATION_DURATION * 1000) {
+                sync_mode = RAMBLOCK_SYN_MODERN;
+                bql_lock();
+                trace_migration_background_sync();
+                WITH_RCU_READ_LOCK_GUARD() {
+                    migration_bitmap_sync(rs, false, true);
+                }
+                bql_unlock();
+            }
+        }
+    }
+
+    qatomic_set(&rs->background_sync_running, 0);
+
+    rcu_unregister_thread();
+    trace_migration_background_sync_watcher_end();
+
+    return NULL;
+}
+
+void migration_background_sync_setup(void)
+{
+    RAMState *rs = ram_state;
+
+    if (!rs) {
+        return;
+    }
+
+    if (qatomic_read(&rs->background_sync_running)) {
+        return;
+    }
+
+    qemu_sem_init(&rs->quit_sem, 0);
+    qatomic_set(&rs->background_sync_running, 1);
+
+    qemu_thread_create(&rs->background_sync_thread,
+                       NULL, migration_background_sync_watcher,
+                       rs, QEMU_THREAD_JOINABLE);
+}
+
+void migration_background_sync_cleanup(void)
+{
+    RAMState *rs = ram_state;
+
+    if (!rs) {
+        return;
+    }
+
+    if (!qatomic_read(&rs->background_sync_running)) {
+        return;
+    }
+
+    qatomic_set(&rs->background_sync_running, 0);
+    qemu_sem_post(&rs->quit_sem);
+    qemu_thread_join(&rs->background_sync_thread);
+    qemu_sem_destroy(&rs->quit_sem);
+}
+
 static void migration_bitmap_sync_precopy(RAMState *rs, bool last_stage)
 {
     Error *local_err = NULL;
diff --git a/migration/ram.h b/migration/ram.h
index bc0318b834..0315d22a66 100644
--- a/migration/ram.h
+++ b/migration/ram.h
@@ -93,4 +93,7 @@  void ram_write_tracking_prepare(void);
 int ram_write_tracking_start(void);
 void ram_write_tracking_stop(void);
 
+/* Migration background sync */
+void migration_background_sync_setup(void);
+void migration_background_sync_cleanup(void);
 #endif
diff --git a/migration/trace-events b/migration/trace-events
index c65902f042..4f95f9fe14 100644
--- a/migration/trace-events
+++ b/migration/trace-events
@@ -92,6 +92,9 @@  qemu_file_fclose(void) ""
 # ram.c
 get_queued_page(const char *block_name, uint64_t tmp_offset, unsigned long page_abs) "%s/0x%" PRIx64 " page_abs=0x%lx"
 get_queued_page_not_dirty(const char *block_name, uint64_t tmp_offset, unsigned long page_abs) "%s/0x%" PRIx64 " page_abs=0x%lx"
+migration_background_sync(void) ""
+migration_background_sync_watcher_start(void) ""
+migration_background_sync_watcher_end(void) ""
 migration_bitmap_sync_start(void) ""
 migration_bitmap_sync_end(uint64_t dirty_pages) "dirty_pages %" PRIu64
 migration_bitmap_clear_dirty(char *str, uint64_t start, uint64_t size, unsigned long page) "rb %s start 0x%"PRIx64" size 0x%"PRIx64" page 0x%lx"