Skip to contentSkip to conversation

migration: auto-converge refinements for huge VM

PATCH Applied [email protected] 3 participants 16 messages Oct 17, 2024 – Oct 18, 2024 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v4 cover migration: auto-converge refinements for huge VM

From: Hyman Huang <[email protected]>

v4:
1. split the [PATCH v3 1/6] into smaller patches.
2. refine some comment and commit message
3. fix race issue for the throttle_dirty_sync_timer
4. refine the util function cpu_throttle_dirty_sync_timer

Thanks Peter for the suggestions.

Please review.

Yong

v3:
1. drop the responsive throttle patchset
2. rename background sync to periodic ramblock dirty sync
3. move the cpu-throttle.* from system to migration
4. remove "rs" parameter in migration_bitmap_sync_precopy
5. implement periodic ramblock dirty sync in cpu-throttle.c
6. move the test change into a separate patch

To simplify the cover letter, i have dropped the test data, please refer to
https://lore.kernel.org/qemu-devel/[email protected]/
for more test details.

Thanks Peter and Fabiano for the suggestions and comments.

Please review.

Yong

Hyman Huang (6):
accel/tcg/icount-common: Remove the reference to the unused header
file
migration: Stop CPU throttling conditionally
migration: Move cpu-throttole.c from system to migration
migration: Remove "rs" parameter in migration_bitmap_sync_precopy
migration: Support periodic RAMBlock dirty bitmap sync tests/migration: Add case for periodic ramblock dirty sync

 accel/tcg/icount-common.c                    |  1 -
 {system => migration}/cpu-throttle.c         | 67 +++++++++++++++++++-
 {include/sysemu => migration}/cpu-throttle.h | 14 ++++
 migration/meson.build                        |  1 +
 migration/migration.c                        | 23 +++++--
 migration/migration.h                        |  1 +
 migration/ram.c                              | 13 ++--
 migration/trace-events                       |  4 ++
 system/cpu-timers.c                          |  3 -
 system/meson.build                           |  1 -
 system/trace-events                          |  3 -
 tests/qtest/migration-test.c                 | 32 ++++++++++
 12 files changed, 143 insertions(+), 20 deletions(-)
 rename {system => migration}/cpu-throttle.c (67%)
 rename {include/sysemu => migration}/cpu-throttle.h (87%)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v4 1/6 accel/tcg/icount-common: Remove the reference to the unused header file

From: Hyman Huang <[email protected]>

---
 accel/tcg/icount-common.c | 1 -
 1 file changed, 1 deletion(-)
accel/tcg/icount-common.c +0 -1
index 8d3d3a7e9d..30bf8500dc 100644
@@ -36,7 +36,6 @@
 #include "sysemu/runstate.h"
 #include "hw/core/cpu.h"
 #include "sysemu/cpu-timers.h"
-#include "sysemu/cpu-throttle.h"
 #include "sysemu/cpu-timers-internal.h"
 
 /*
Signature · 1 line
2.27.0
Reviewed-by on v4 1/6
#2 lore raw

Re: [PATCH v4 1/6] accel/tcg/icount-common: Remove the reference to the unused header file

Quoted 12 lines · [email protected] writes:

From: Hyman Huang <[email protected]>

---
 accel/tcg/icount-common.c | 1 -
 1 file changed, 1 deletion(-)
accel/tcg/icount-common.c +0 -0
index 8d3d3a7e9d..30bf8500dc 100644

accel/tcg/icount-common.c line 41

Quoted 7 lines
accel/tcg/icount-common.c +0 -1
@@ -36,7 +36,6 @@
 #include "sysemu/runstate.h"
 #include "hw/core/cpu.h"
 #include "sysemu/cpu-timers.h"
-#include "sysemu/cpu-throttle.h"
 #include "sysemu/cpu-timers-internal.h"
 
 /*
#3 lore raw

PATCH v4 2/6 migration: Stop CPU throttling conditionally

In reply to [email protected]

From: Hyman Huang <[email protected]>

Since CPU throttling only occurs when auto-converge is on, stop it conditionally.

---
 migration/migration.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
migration/migration.c +3 -1
index 021faee2f3..37a200a177 100644
@@ -3289,7 +3289,9 @@ static MigIterateState migration_iteration_run(MigrationState *s)
 static void migration_iteration_finish(MigrationState *s)
 {
     /* If we enabled cpu throttling for auto-converge, turn it off. */
-    cpu_throttle_stop();
+    if (migrate_auto_converge()) {
+        cpu_throttle_stop();
+    }
 
     bql_lock();
     switch (s->state) {
Signature · 1 line
2.27.0
Reviewed-by on v4 2/6
#4 lore raw

Re: [PATCH v4 2/6] migration: Stop CPU throttling conditionally

Quoted 15 lines · [email protected] writes:

From: Hyman Huang <[email protected]>

Since CPU throttling only occurs when auto-converge is on, stop it conditionally.

---
 migration/migration.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
migration/migration.c +0 -0
index 021faee2f3..37a200a177 100644

migration/migration.c static MigIterateState migration_iteration_run(MigrationState *s) · line 3297

Quoted 10 lines
migration/migration.c +3 -1
@@ -3289,7 +3289,9 @@ static MigIterateState migration_iteration_run(MigrationState *s)
 static void migration_iteration_finish(MigrationState *s)
 {
     /* If we enabled cpu throttling for auto-converge, turn it off. */
-    cpu_throttle_stop();
+    if (migrate_auto_converge()) {
+        cpu_throttle_stop();
+    }
 
     bql_lock();
     switch (s->state) {
#5 lore raw

PATCH v4 3/6 migration: Move cpu-throttole.c from system to migration

In reply to [email protected]

From: Hyman Huang <[email protected]>

Move cpu-throttle.c from system to migration since it's only used for migration; this makes us avoid exporting the util functions and variables in misc.h but export them in migration.h when implementing the periodic ramblock dirty sync feature in the upcoming commits.

Since CPU throttle timers are only used in migration, move their registry to migration_object_init.

---
 {system => migration}/cpu-throttle.c         | 2 +-
 {include/sysemu => migration}/cpu-throttle.h | 0
 migration/meson.build                        | 1 +
 migration/migration.c                        | 5 ++++-
 migration/ram.c                              | 2 +-
 migration/trace-events                       | 3 +++
 system/cpu-timers.c                          | 3 ---
 system/meson.build                           | 1 -
 system/trace-events                          | 3 ---
 9 files changed, 10 insertions(+), 10 deletions(-)
 rename {system => migration}/cpu-throttle.c (99%)
 rename {include/sysemu => migration}/cpu-throttle.h (100%)
migration/cpu-throttle.c renamed from system/cpu-throttle.c +1 -1
similarity index 99%
rename from system/cpu-throttle.c
rename to migration/cpu-throttle.c
index 7632dc6143..fa47ee2e21 100644
@@ -27,7 +27,7 @@
 #include "hw/core/cpu.h"
 #include "qemu/main-loop.h"
 #include "sysemu/cpus.h"
-#include "sysemu/cpu-throttle.h"
+#include "cpu-throttle.h"
 #include "trace.h"
 
 /* vcpu throttling controls */
migration/cpu-throttle.h renamed from include/sysemu/cpu-throttle.h +0 -0
similarity index 100%
rename from include/sysemu/cpu-throttle.h
rename to migration/cpu-throttle.h
migration/meson.build +1 -0
index 66d3de86f0..d53cf3417a 100644
@@ -13,6 +13,7 @@ system_ss.add(files(
   'block-dirty-bitmap.c',
   'channel.c',
   'channel-block.c',
+  'cpu-throttle.c',
   'dirtyrate.c',
   'exec.c',
   'fd.c',
migration/migration.c +4 -1
index 37a200a177..2e10ca77af 100644
@@ -24,7 +24,7 @@
 #include "socket.h"
 #include "sysemu/runstate.h"
 #include "sysemu/sysemu.h"
-#include "sysemu/cpu-throttle.h"
+#include "cpu-throttle.h"
 #include "rdma.h"
 #include "ram.h"
 #include "migration/global_state.h"
@@ -263,6 +263,9 @@ void migration_object_init(void)
 
     ram_mig_init();
     dirty_bitmap_mig_init();
+
+    /* Initialize cpu throttle timers */
+    cpu_throttle_init();
 }
 
 typedef struct {
migration/ram.c +1 -1
index 326ce7eb79..54d352b152 100644
@@ -52,7 +52,7 @@
 #include "exec/target_page.h"
 #include "qemu/rcu_queue.h"
 #include "migration/colo.h"
-#include "sysemu/cpu-throttle.h"
+#include "cpu-throttle.h"
 #include "savevm.h"
 #include "qemu/iov.h"
 #include "multifd.h"
migration/trace-events +3 -0
index c65902f042..9a19599804 100644
@@ -378,3 +378,6 @@ migration_block_progression(unsigned percent) "Completed %u%%"
 # page_cache.c
 migration_pagecache_init(int64_t max_num_items) "Setting cache buckets to %" PRId64
 migration_pagecache_insert(void) "Error allocating page"
+
+# cpu-throttle.c
+cpu_throttle_set(int new_throttle_pct)  "set guest CPU throttled by %d%%"
system/cpu-timers.c +0 -3
index 0b31c9a1b6..856e502e34 100644
@@ -35,7 +35,6 @@
 #include "sysemu/runstate.h"
 #include "hw/core/cpu.h"
 #include "sysemu/cpu-timers.h"
-#include "sysemu/cpu-throttle.h"
 #include "sysemu/cpu-timers-internal.h"
 
 /* clock and ticks */
@@ -272,6 +271,4 @@ void cpu_timers_init(void)
     seqlock_init(&timers_state.vm_clock_seqlock);
     qemu_spin_init(&timers_state.vm_clock_lock);
     vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
-
-    cpu_throttle_init();
 }
system/meson.build +0 -1
index a296270cb0..4952f4b2c7 100644
@@ -10,7 +10,6 @@ system_ss.add(files(
   'balloon.c',
   'bootdevice.c',
   'cpus.c',
-  'cpu-throttle.c',
   'cpu-timers.c',
   'datadir.c',
   'dirtylimit.c',
system/trace-events +0 -3
index 074d001e90..2ed1d59b1f 100644
@@ -44,6 +44,3 @@ dirtylimit_state_finalize(void)
 dirtylimit_throttle_pct(int cpu_index, uint64_t pct, int64_t time_us) "CPU[%d] throttle percent: %" PRIu64 ", throttle adjust time %"PRIi64 " us"
 dirtylimit_set_vcpu(int cpu_index, uint64_t quota) "CPU[%d] set dirty page rate limit %"PRIu64
 dirtylimit_vcpu_execute(int cpu_index, int64_t sleep_time_us) "CPU[%d] sleep %"PRIi64 " us"
-
-# cpu-throttle.c
-cpu_throttle_set(int new_throttle_pct)  "set guest CPU throttled by %d%%"
Signature · 1 line
2.27.0
Reviewed-by on v4 3/6
#6 lore raw

Re: [PATCH v4 3/6] migration: Move cpu-throttole.c from system to migration

Quoted 8 lines · [email protected] writes:

From: Hyman Huang <[email protected]>

Move cpu-throttle.c from system to migration since it's only used for migration; this makes us avoid exporting the util functions and variables in misc.h but export them in migration.h when implementing the periodic ramblock dirty sync feature in the upcoming commits.

Quoted 4 lines

Since CPU throttle timers are only used in migration, move their registry to migration_object_init.

#7 lore raw

PATCH v4 4/6 migration: Remove "rs" parameter in migration_bitmap_sync_precopy

In reply to [email protected]

From: Hyman Huang <[email protected]>

The global static variable ram_state in fact is referred to by the "rs" parameter in migration_bitmap_sync_precopy. For ease of calling by the callees, use the global variable directly in migration_bitmap_sync_precopy and remove "rs" parameter.

The migration_bitmap_sync_precopy will be exported in the next commit.

---
 migration/ram.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
migration/ram.c +6 -5
index 54d352b152..9b5b350405 100644
@@ -1088,9 +1088,10 @@ static void migration_bitmap_sync(RAMState *rs, bool last_stage)
     }
 }
 
-static void migration_bitmap_sync_precopy(RAMState *rs, bool last_stage)
+static void migration_bitmap_sync_precopy(bool last_stage)
 {
     Error *local_err = NULL;
+    assert(ram_state);
 
     /*
      * The current notifier usage is just an optimization to migration, so we
@@ -1101,7 +1102,7 @@ static void migration_bitmap_sync_precopy(RAMState *rs, bool last_stage)
         local_err = NULL;
     }
 
-    migration_bitmap_sync(rs, last_stage);
+    migration_bitmap_sync(ram_state, last_stage);
 
     if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC, &local_err)) {
         error_report_err(local_err);
@@ -2782,7 +2783,7 @@ static bool ram_init_bitmaps(RAMState *rs, Error **errp)
             if (!ret) {
                 goto out_unlock;
             }
-            migration_bitmap_sync_precopy(rs, false);
+            migration_bitmap_sync_precopy(false);
         }
     }
 out_unlock:
@@ -3248,7 +3249,7 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
 
     WITH_RCU_READ_LOCK_GUARD() {
         if (!migration_in_postcopy()) {
-            migration_bitmap_sync_precopy(rs, true);
+            migration_bitmap_sync_precopy(true);
         }
 
         ret = rdma_registration_start(f, RAM_CONTROL_FINISH);
@@ -3330,7 +3331,7 @@ static void ram_state_pending_exact(void *opaque, uint64_t *must_precopy,
     if (!migration_in_postcopy()) {
         bql_lock();
         WITH_RCU_READ_LOCK_GUARD() {
-            migration_bitmap_sync_precopy(rs, false);
+            migration_bitmap_sync_precopy(false);
         }
         bql_unlock();
     }
Signature · 1 line
2.27.0
#8 lore raw

PATCH v4 5/6 migration: Support periodic RAMBlock dirty bitmap sync

In reply to [email protected]

From: Hyman Huang <[email protected]>

When VM is configured with huge memory, the current throttle logic doesn't look like to scale, because migration_trigger_throttle() is only called for each iteration, so it won't be invoked for a long time if one iteration can take a long time.

The periodic dirty sync aims to fix the above issue by synchronizing the ramblock from remote dirty bitmap and, when necessary, triggering the CPU throttle multiple times during a long iteration.

This is a trade-off between synchronization overhead and CPU throttle impact.

---
 migration/cpu-throttle.c | 65 +++++++++++++++++++++++++++++++++++++++-
 migration/cpu-throttle.h | 14 +++++++++
 migration/migration.c    | 14 +++++++--
 migration/migration.h    |  1 +
 migration/ram.c          |  2 +-
 migration/trace-events   |  1 +
 6 files changed, 93 insertions(+), 4 deletions(-)
migration/cpu-throttle.c +64 -1
index fa47ee2e21..342681cdd4 100644
@@ -28,16 +28,22 @@
 #include "qemu/main-loop.h"
 #include "sysemu/cpus.h"
 #include "cpu-throttle.h"
+#include "migration.h"
+#include "migration-stats.h"
 #include "trace.h"
 
 /* vcpu throttling controls */
-static QEMUTimer *throttle_timer;
+static QEMUTimer *throttle_timer, *throttle_dirty_sync_timer;
 static unsigned int throttle_percentage;
+static bool throttle_dirty_sync_timer_active;
 
 #define CPU_THROTTLE_PCT_MIN 1
 #define CPU_THROTTLE_PCT_MAX 99
 #define CPU_THROTTLE_TIMESLICE_NS 10000000
 
+/* Making sure RAMBlock dirty bitmap is synchronized every five seconds */
+#define CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS 5000
+
 static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
 {
     double pct;
@@ -112,6 +118,7 @@ void cpu_throttle_set(int new_throttle_pct)
 void cpu_throttle_stop(void)
 {
     qatomic_set(&throttle_percentage, 0);
+    cpu_throttle_dirty_sync_timer(false);
 }
 
 bool cpu_throttle_active(void)
@@ -124,8 +131,64 @@ int cpu_throttle_get_percentage(void)
     return qatomic_read(&throttle_percentage);
 }
 
+void cpu_throttle_dirty_sync_timer_tick(void *opaque)
+{
+    static uint64_t prev_sync_cnt;
+    uint64_t sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+
+    /*
+     * 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.
+     */
+    if (sync_cnt <= 1) {
+        goto end;
+    }
+
+    if (sync_cnt == prev_sync_cnt) {
+        trace_cpu_throttle_dirty_sync();
+        WITH_RCU_READ_LOCK_GUARD() {
+            migration_bitmap_sync_precopy(false);
+        }
+    }
+
+end:
+    prev_sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+
+    timer_mod(throttle_dirty_sync_timer,
+        qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
+            CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS);
+}
+
+static bool cpu_throttle_dirty_sync_active(void)
+{
+    return qatomic_read(&throttle_dirty_sync_timer_active);
+}
+
+void cpu_throttle_dirty_sync_timer(bool enable)
+{
+    assert(throttle_dirty_sync_timer);
+
+    if (enable) {
+        if (!cpu_throttle_dirty_sync_active()) {
+            timer_mod(throttle_dirty_sync_timer,
+                qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
+                    CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS);
+            qatomic_set(&throttle_dirty_sync_timer_active, 1);
+        }
+    } else {
+        if (cpu_throttle_dirty_sync_active()) {
+            timer_del(throttle_dirty_sync_timer);
+            qatomic_set(&throttle_dirty_sync_timer_active, 0);
+        }
+    }
+}
+
 void cpu_throttle_init(void)
 {
     throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
                                   cpu_throttle_timer_tick, NULL);
+    throttle_dirty_sync_timer =
+        timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
+                     cpu_throttle_dirty_sync_timer_tick, NULL);
 }
migration/cpu-throttle.h +14 -0
index d65bdef6d0..420702b8d3 100644
@@ -65,4 +65,18 @@ bool cpu_throttle_active(void);
  */
 int cpu_throttle_get_percentage(void);
 
+/**
+ * cpu_throttle_dirty_sync_timer_tick:
+ *
+ * Dirty sync timer hook.
+ */
+void cpu_throttle_dirty_sync_timer_tick(void *opaque);
+
+/**
+ * cpu_throttle_dirty_sync_timer:
+ *
+ * Start or stop the dirty sync timer.
+ */
+void cpu_throttle_dirty_sync_timer(bool enable);
+
 #endif /* SYSEMU_CPU_THROTTLE_H */
migration/migration.c +12 -2
index 2e10ca77af..f673e30069 100644
@@ -3291,12 +3291,17 @@ static MigIterateState migration_iteration_run(MigrationState *s)
 
 static void migration_iteration_finish(MigrationState *s)
 {
-    /* If we enabled cpu throttling for auto-converge, turn it off. */
+    bql_lock();
+
+    /*
+     * If we enabled cpu throttling for auto-converge, turn it off.
+     * Stopping CPU throttle should be serialized by BQL to avoid
+     * racing for the throttle_dirty_sync_timer.
+     */
     if (migrate_auto_converge()) {
         cpu_throttle_stop();
     }
 
-    bql_lock();
     switch (s->state) {
     case MIGRATION_STATUS_COMPLETED:
         runstate_set(RUN_STATE_POSTMIGRATE);
@@ -3513,6 +3518,11 @@ static void *migration_thread(void *opaque)
         qemu_savevm_send_colo_enable(s->to_dst_file);
     }
 
+    if (migrate_auto_converge()) {
+        /* Start RAMBlock dirty bitmap sync timer */
+        cpu_throttle_dirty_sync_timer(true);
+    }
+
     bql_lock();
     ret = qemu_savevm_state_setup(s->to_dst_file, &local_err);
     bql_unlock();
migration/migration.h +1 -0
index 38aa1402d5..fbd0d19092 100644
@@ -537,4 +537,5 @@ int migration_rp_wait(MigrationState *s);
  */
 void migration_rp_kick(MigrationState *s);
 
+void migration_bitmap_sync_precopy(bool last_stage);
 #endif
migration/ram.c +1 -1
index 9b5b350405..d284f63854 100644
@@ -1088,7 +1088,7 @@ static void migration_bitmap_sync(RAMState *rs, bool last_stage)
     }
 }
 
-static void migration_bitmap_sync_precopy(bool last_stage)
+void migration_bitmap_sync_precopy(bool last_stage)
 {
     Error *local_err = NULL;
     assert(ram_state);
migration/trace-events +1 -0
index 9a19599804..0638183056 100644
@@ -381,3 +381,4 @@ migration_pagecache_insert(void) "Error allocating page"
 
 # cpu-throttle.c
 cpu_throttle_set(int new_throttle_pct)  "set guest CPU throttled by %d%%"
+cpu_throttle_dirty_sync(void) ""
Signature · 1 line
2.27.0
Reviewed-by on v4 5/6
#9 lore raw

Re: [PATCH v4 5/6] migration: Support periodic RAMBlock dirty bitmap sync

Quoted 7 lines · [email protected] writes:

From: Hyman Huang <[email protected]>

When VM is configured with huge memory, the current throttle logic doesn't look like to scale, because migration_trigger_throttle() is only called for each iteration, so it won't be invoked for a long time if one iteration can take a long time.

Quoted 8 lines

The periodic dirty sync aims to fix the above issue by synchronizing the ramblock from remote dirty bitmap and, when necessary, triggering the CPU throttle multiple times during a long iteration.

This is a trade-off between synchronization overhead and CPU throttle impact.

#10 lore raw

Re: [PATCH v4 5/6] migration: Support periodic RAMBlock dirty bitmap sync

In reply to [email protected]

Review

Quoted 3 lines · On Thu, Oct 17, 2024 at 02:42:54PM +0800, [email protected] wrote:
(quoted diff) +3 -0
+void cpu_throttle_dirty_sync_timer_tick(void *opaque)
+{
+    static uint64_t prev_sync_cnt;

We may need to reset this in case migration got cancelled and invoked again, to make sure it keeps working in the 2nd run.

Quoted 15 lines
(quoted diff) +15 -0
+    uint64_t sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+
+    /*
+     * 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.
+     */
+    if (sync_cnt <= 1) {
+        goto end;
+    }
+
+    if (sync_cnt == prev_sync_cnt) {
+        trace_cpu_throttle_dirty_sync();
+        WITH_RCU_READ_LOCK_GUARD() {
+            migration_bitmap_sync_precopy(false);

Review

Quoted 10 lines
(quoted diff) +10 -0
+        }
+    }
+
+end:
+    prev_sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+
+    timer_mod(throttle_dirty_sync_timer,
+        qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
+            CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS);
+}

Please both of you have a look on whether you agree I squash below into this patch when merge:

===8<===
From 84a2544eab73e35dbd35fed3b1440169915f9aa4 Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Thu, 17 Oct 2024 15:27:19 -0400
Subject: [PATCH] fixup! migration: Support periodic RAMBlock dirty bitmap sync

---
 migration/cpu-throttle.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
migration/cpu-throttle.c +8 -3
index 342681cdd4..3df287d8d3 100644
@@ -36,6 +36,7 @@
 static QEMUTimer *throttle_timer, *throttle_dirty_sync_timer;
 static unsigned int throttle_percentage;
 static bool throttle_dirty_sync_timer_active;
+static uint64_t throttle_dirty_sync_count_prev;
 
 #define CPU_THROTTLE_PCT_MIN 1
 #define CPU_THROTTLE_PCT_MAX 99
@@ -133,7 +134,6 @@ int cpu_throttle_get_percentage(void)
 
 void cpu_throttle_dirty_sync_timer_tick(void *opaque)
 {
-    static uint64_t prev_sync_cnt;
     uint64_t sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
 
     /*
@@ -145,7 +145,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
         goto end;
     }
 
-    if (sync_cnt == prev_sync_cnt) {
+    if (sync_cnt == throttle_dirty_sync_count_prev) {
         trace_cpu_throttle_dirty_sync();
         WITH_RCU_READ_LOCK_GUARD() {
             migration_bitmap_sync_precopy(false);
@@ -153,7 +153,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
     }
 
 end:
-    prev_sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+    throttle_dirty_sync_count_prev = stat64_get(&mig_stats.dirty_sync_count);
 
     timer_mod(throttle_dirty_sync_timer,
         qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
@@ -171,6 +171,11 @@ void cpu_throttle_dirty_sync_timer(bool enable)
 
     if (enable) {
         if (!cpu_throttle_dirty_sync_active()) {
+            /*
+             * Always reset the dirty sync count cache, in case migration
+             * was cancelled once.
+             */
+            throttle_dirty_sync_count_prev = 0;
             timer_mod(throttle_dirty_sync_timer,
                 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
                     CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS);
Signature · 4 lines
2.45.0

-- 
Peter Xu
#11 lore raw

Re: [PATCH v4 5/6] migration: Support periodic RAMBlock dirty bitmap sync

Quoted 100 lines · Peter Xu

Review

Quoted 3 lines · On Thu, Oct 17, 2024 at 02:42:54PM +0800, [email protected] wrote:
(quoted diff) +3 -0
+void cpu_throttle_dirty_sync_timer_tick(void *opaque)
+{
+    static uint64_t prev_sync_cnt;

We may need to reset this in case migration got cancelled and invoked again, to make sure it keeps working in the 2nd run.

Quoted 15 lines
(quoted diff) +15 -0
+    uint64_t sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+
+    /*
+     * 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.
+     */
+    if (sync_cnt <= 1) {
+        goto end;
+    }
+
+    if (sync_cnt == prev_sync_cnt) {
+        trace_cpu_throttle_dirty_sync();
+        WITH_RCU_READ_LOCK_GUARD() {
+            migration_bitmap_sync_precopy(false);

Review

Quoted 10 lines
(quoted diff) +10 -0
+        }
+    }
+
+end:
+    prev_sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+
+    timer_mod(throttle_dirty_sync_timer,
+        qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
+            CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS);
+}

Please both of you have a look on whether you agree I squash below into this patch when merge:

===8<===
From 84a2544eab73e35dbd35fed3b1440169915f9aa4 Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Thu, 17 Oct 2024 15:27:19 -0400
Subject: [PATCH] fixup! migration: Support periodic RAMBlock dirty bitmap sync

---
 migration/cpu-throttle.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
migration/cpu-throttle.c +8 -3
index 342681cdd4..3df287d8d3 100644
@@ -36,6 +36,7 @@
 static QEMUTimer *throttle_timer, *throttle_dirty_sync_timer;
 static unsigned int throttle_percentage;
 static bool throttle_dirty_sync_timer_active;
+static uint64_t throttle_dirty_sync_count_prev;
 
 #define CPU_THROTTLE_PCT_MIN 1
 #define CPU_THROTTLE_PCT_MAX 99
@@ -133,7 +134,6 @@ int cpu_throttle_get_percentage(void)
 
 void cpu_throttle_dirty_sync_timer_tick(void *opaque)
 {
-    static uint64_t prev_sync_cnt;
     uint64_t sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
 
     /*
@@ -145,7 +145,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
         goto end;
     }
 
-    if (sync_cnt == prev_sync_cnt) {
+    if (sync_cnt == throttle_dirty_sync_count_prev) {
         trace_cpu_throttle_dirty_sync();
         WITH_RCU_READ_LOCK_GUARD() {
             migration_bitmap_sync_precopy(false);
@@ -153,7 +153,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
     }
 
 end:
-    prev_sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+    throttle_dirty_sync_count_prev = stat64_get(&mig_stats.dirty_sync_count);
 
     timer_mod(throttle_dirty_sync_timer,
         qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
@@ -171,6 +171,11 @@ void cpu_throttle_dirty_sync_timer(bool enable)
 
     if (enable) {
         if (!cpu_throttle_dirty_sync_active()) {
+            /*
+             * Always reset the dirty sync count cache, in case migration
+             * was cancelled once.
+             */
+            throttle_dirty_sync_count_prev = 0;
             timer_mod(throttle_dirty_sync_timer,
                 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
                     CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS);
Signature · 1 line
2.45.0

LGTM

author Acked-by
#12 lore raw

Re: [PATCH v4 5/6] migration: Support periodic RAMBlock dirty bitmap sync

In reply to Peter Xu
Quoted 28 lines · Peter Xu

Review

Quoted 3 lines · On Thu, Oct 17, 2024 at 02:42:54PM +0800, [email protected] wrote:
(quoted diff) +3 -0
+void cpu_throttle_dirty_sync_timer_tick(void *opaque)
+{
+    static uint64_t prev_sync_cnt;

We may need to reset this in case migration got cancelled and invoked again, to make sure it keeps working in the 2nd run.

Quoted 20 lines
(quoted diff) +20 -0
+    uint64_t sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+
+    /*
+     * 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.
+     */
+    if (sync_cnt <= 1) {
+        goto end;
+    }
+
+    if (sync_cnt == prev_sync_cnt) {
+        trace_cpu_throttle_dirty_sync();
+        WITH_RCU_READ_LOCK_GUARD() {
+            migration_bitmap_sync_precopy(false);
+        }
+    }
+
+end:
+    prev_sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
Quoted 9 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+    timer_mod(throttle_dirty_sync_timer,
+        qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
+            CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS);
+}

Please both of you have a look on whether you agree I squash below into this patch when merge:

Thanks for the fixup, it looks good to me.

Quoted 67 lines

===8<===
From 84a2544eab73e35dbd35fed3b1440169915f9aa4 Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Thu, 17 Oct 2024 15:27:19 -0400
Subject: [PATCH] fixup! migration: Support periodic RAMBlock dirty bitmap sync

---
 migration/cpu-throttle.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
migration/cpu-throttle.c +3 -3
index 342681cdd4..3df287d8d3 100644
@@ -36,6 +36,7 @@
 static QEMUTimer *throttle_timer, *throttle_dirty_sync_timer;
 static unsigned int throttle_percentage;
 static bool throttle_dirty_sync_timer_active;
+static uint64_t throttle_dirty_sync_count_prev;
 
 #define CPU_THROTTLE_PCT_MIN 1
 #define CPU_THROTTLE_PCT_MAX 99
@@ -133,7 +134,6 @@ int cpu_throttle_get_percentage(void)
 
 void cpu_throttle_dirty_sync_timer_tick(void *opaque)
 {
-    static uint64_t prev_sync_cnt;
     uint64_t sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
 
     /*
@@ -145,7 +145,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
         goto end;
     }
 
-    if (sync_cnt == prev_sync_cnt) {
+    if (sync_cnt == throttle_dirty_sync_count_prev) {
         trace_cpu_throttle_dirty_sync();
         WITH_RCU_READ_LOCK_GUARD() {
             migration_bitmap_sync_precopy(false);
@@ -153,7 +153,7 @@ void cpu_throttle_dirty_sync_timer_tick(void *opaque)
     }
 
 end:
-    prev_sync_cnt = stat64_get(&mig_stats.dirty_sync_count);
+    throttle_dirty_sync_count_prev =
 stat64_get(&mig_stats.dirty_sync_count);
 
     timer_mod(throttle_dirty_sync_timer,

qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +

migration/cpu-throttle.c +5 -1
@@ -171,6 +171,11 @@ void cpu_throttle_dirty_sync_timer(bool enable)
 
     if (enable) {
         if (!cpu_throttle_dirty_sync_active()) {
+            /*
+             * Always reset the dirty sync count cache, in case migration
+             * was cancelled once.
+             */
+            throttle_dirty_sync_count_prev = 0;
             timer_mod(throttle_dirty_sync_timer,
                 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) +
                     CPU_THROTTLE_DIRTY_SYNC_TIMESLICE_MS);
--

2.45.0

Quoted 4 lines

--
Peter Xu

Signature · 1 line
Best regards
#13 lore raw

PATCH v4 6/6 tests/migration: Add case for periodic ramblock dirty sync

In reply to [email protected]

From: Hyman Huang <[email protected]>

---
 tests/qtest/migration-test.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
tests/qtest/migration-test.c +32 -0
index 95e45b5029..e6a2803e71 100644
@@ -2791,6 +2791,8 @@ static void test_migrate_auto_converge(void)
      * so we need to decrease a bandwidth.
      */
     const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
+    uint64_t prev_dirty_sync_cnt, dirty_sync_cnt;
+    int max_try_count, hit = 0;
 
     if (test_migrate_start(&from, &to, uri, &args)) {
         return;
@@ -2827,6 +2829,36 @@ static void test_migrate_auto_converge(void)
     } while (true);
     /* The first percentage of throttling should be at least init_pct */
     g_assert_cmpint(percentage, >=, init_pct);
+
+    /*
+     * End the loop when the dirty sync count greater than 1.
+     */
+    while ((dirty_sync_cnt = get_migration_pass(from)) < 2) {
+        usleep(1000 * 1000);
+    }
+
+    prev_dirty_sync_cnt = dirty_sync_cnt;
+
+    /*
+     * The RAMBlock dirty sync count must changes in 5 seconds, here we set
+     * the timeout to 10 seconds to ensure it changes.
+     *
+     * Note that migrate_ensure_non_converge set the max-bandwidth to 3MB/s,
+     * while the qtest mem is >= 100MB, one iteration takes at least 33s (100/3)
+     * to complete; this ensures that the RAMBlock dirty sync occurs.
+     */
+    max_try_count = 10;
+    while (--max_try_count) {
+        dirty_sync_cnt = get_migration_pass(from);
+        if (dirty_sync_cnt != prev_dirty_sync_cnt) {
+            hit = 1;
+            break;
+        }
+        prev_dirty_sync_cnt = dirty_sync_cnt;
+        sleep(1);
+    }
+    g_assert_cmpint(hit, ==, 1);
+
     /* Now, when we tested that throttling works, let it converge */
     migrate_ensure_converge(from);
 
Signature · 1 line
2.27.0
Applied
#15 lore raw
In reply to [email protected]
Quoted 7 lines · On Thu, Oct 17, 2024 at 02:42:49PM +0800, [email protected] wrote:

From: Hyman Huang <[email protected]>

v4:
1. split the [PATCH v3 1/6] into smaller patches.
2. refine some comment and commit message
3. fix race issue for the throttle_dirty_sync_timer
4. refine the util function cpu_throttle_dirty_sync_timer

queued, with a fixup squashed to patch 5 per discussion, thanks.

Signature · 1 line
Peter Xu