qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Hyman Huang <yong.huang@smartx.com>
To: qemu-devel@nongnu.org
Cc: "Peter Xu" <peterx@redhat.com>, "Fabiano Rosas" <farosas@suse.de>,
	"Eric Blake" <eblake@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	yong.huang@smartx.com
Subject: [PATCH v1 4/7] migration: Implment background sync watcher
Date: Mon, 16 Sep 2024 00:08:47 +0800	[thread overview]
Message-ID: <4c105e23be9a2d1a6be71e6abf9c938a4d091dfc.1726390099.git.yong.huang@smartx.com> (raw)
In-Reply-To: <cover.1726390098.git.yong.huang@smartx.com>

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 --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"
-- 
2.39.1



  parent reply	other threads:[~2024-09-15 16:10 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-15 16:08 [PATCH v1 0/7] migration: auto-converge refinements for huge VM Hyman Huang
2024-09-15 16:08 ` [PATCH v1 1/7] migration: Introduce structs for background sync Hyman Huang
2024-09-16 21:11   ` Fabiano Rosas
2024-09-17  6:48     ` Yong Huang
2024-09-19 18:45       ` Peter Xu
2024-09-20  2:43         ` Yong Huang
2024-09-25 19:17           ` Peter Xu
2024-09-26 18:13             ` Yong Huang
2024-09-26 19:55               ` Peter Xu
2024-09-27  2:50                 ` Yong Huang
2024-09-27 15:35                   ` Peter Xu
2024-09-27 16:44                     ` Hyman Huang
2024-09-28  5:07                     ` Yong Huang
2024-09-20  3:02         ` Yong Huang
2024-09-20  3:13         ` Yong Huang
2024-09-15 16:08 ` [PATCH v1 2/7] migration: Refine util functions to support " Hyman Huang
2024-09-15 16:08 ` [PATCH v1 3/7] qapi/migration: Introduce the iteration-count Hyman Huang
2024-09-16 20:35   ` Fabiano Rosas
2024-09-17  6:52     ` Yong Huang
2024-09-18  8:29     ` Yong Huang
2024-09-18 14:39       ` Fabiano Rosas
2024-09-15 16:08 ` Hyman Huang [this message]
2024-09-15 16:08 ` [PATCH v1 5/7] migration: Support background dirty bitmap sync and throttle Hyman Huang
2024-09-16 20:50   ` Fabiano Rosas
2024-09-15 16:08 ` [PATCH v1 6/7] qapi/migration: Introduce cpu-responsive-throttle parameter Hyman Huang
2024-09-16 20:55   ` Fabiano Rosas
2024-09-17  6:54     ` Yong Huang
2024-09-15 16:08 ` [PATCH v1 7/7] migration: Support responsive CPU throttle Hyman Huang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4c105e23be9a2d1a6be71e6abf9c938a4d091dfc.1726390099.git.yong.huang@smartx.com \
    --to=yong.huang@smartx.com \
    --cc=armbru@redhat.com \
    --cc=david@redhat.com \
    --cc=eblake@redhat.com \
    --cc=farosas@suse.de \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).