Skip to contentSkip to conversation

support dirty restraint on vCPU

PATCH Superseded [email protected] 2 participants 5 messages Nov 18, 2021

v1 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

cover support dirty restraint on vCPU

From: Hyman Huang(黄勇) <[email protected]>

this patchset introduce a mechanism to impose dirty restraint on vCPU, aiming to keep the vCPU running in a certain dirtyrate given by user. dirty restraint on vCPU maybe an alternative method to implement convergence logic for live migration, which could improve guest memory performance during migration compared with traditional method in theory.

For the current live migration implementation, the convergence logic throttles all vCPUs of the VM, which has some side effects. -'read processes' on vCPU will be unnecessarily penalized
- throttle increase percentage step by step, which seems struggling to find the optimal throttle percentage when dirtyrate is high.
- hard to predict the remaining time of migration if the throttling percentage reachs 99%

to a certain extent, the dirty restraint machnism can fix these effects by throttling at vCPU granularity during migration.

the implementation is rather straightforward, we calculate vCPU dirtyrate via the Dirty Ring mechanism periodically as the commit 0e21bf246 "implement dirty-ring dirtyrate calculation" does, for vCPU that be specified to impose dirty restraint, we throttle it periodically as the auto-converge does, once after throttling, we compare the quota dirtyrate with current dirtyrate, if current dirtyrate is not under the quota, increase the throttling percentage until current dirtyrate is under the quota.

this patchset is the basis of implmenting a new auto-converge method for live migration, we introduce two qmp commands for impose/cancel the dirty restraint on specified vCPU, so it also can be an independent api to supply the upper app such as libvirt, which can use it to implement the convergence logic during live migration, supplemented with the qmp 'calc-dirty-rate' command or whatever.

we post this patchset for RFC and any corrections and suggetions about the implementation, api, throttleing algorithm or whatever are very appreciated!

Please review, thanks !

Best Regards !

Hyman Huang (3):
migration/dirtyrate: implement vCPU dirtyrate calculation periodically
cpu-throttle: implement vCPU throttle
cpus-common: implement dirty restraint on vCPU

 cpus-common.c                   |  45 ++++++
 include/exec/memory.h           |   5 +-
 include/hw/core/cpu.h           |   7 +
 include/sysemu/cpu-throttle.h   |  21 +++
 include/sysemu/dirtyrestraint.h |  22 +++
 migration/dirtyrate.c           | 125 +++++++++++++++++
 migration/dirtyrate.h           |   2 +
 qapi/misc.json                  |  44 ++++++
 softmmu/cpu-throttle.c          | 304 ++++++++++++++++++++++++++++++++++++++++
 softmmu/trace-events            |   5 +
 softmmu/vl.c                    |   1 +
 11 files changed, 580 insertions(+), 1 deletion(-)
 create mode 100644 include/sysemu/dirtyrestraint.h
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 migration/dirtyrate: implement vCPU dirtyrate calculation periodically

From: Hyman Huang(黄勇) <[email protected]>

introduce the third method GLOBAL_DIRTY_RESTRAINT of dirty tracking for calculate dirtyrate periodly for dirty restraint.

implement thread for calculate dirtyrate periodly, which will be used for dirty restraint.

add dirtyrestraint.h to introduce the util function for dirty restrain.

---
 include/exec/memory.h           |   5 +-
 include/sysemu/dirtyrestraint.h |  20 +++++++
 migration/dirtyrate.c           | 118 ++++++++++++++++++++++++++++++++++++++++
 migration/dirtyrate.h           |   2 +
 4 files changed, 144 insertions(+), 1 deletion(-)
 create mode 100644 include/sysemu/dirtyrestraint.h
include/exec/memory.h +4 -1
index 20f1b27..565d06b 100644
@@ -69,7 +69,10 @@ static inline void fuzz_dma_read_cb(size_t addr,
 /* Dirty tracking enabled because measuring dirty rate */
 #define GLOBAL_DIRTY_DIRTY_RATE (1U << 1)
 
-#define GLOBAL_DIRTY_MASK  (0x3)
+/* Dirty tracking enabled because dirty restraint */
+#define GLOBAL_DIRTY_RESTRAINT  (1U << 2)
+
+#define GLOBAL_DIRTY_MASK  (0x7)
 
 extern unsigned int global_dirty_tracking;
 
include/sysemu/dirtyrestraint.h new file +20 -0
new file mode 100644
index 0000000..ca744af
@@ -0,0 +1,20 @@
+/*
+ * dirty restraint helper functions
+ *
+ * Copyright (c) 2021 CHINA TELECOM CO.,LTD.
+ *
+ * Authors:
+ *  Hyman Huang(黄勇) <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QEMU_DIRTYRESTRAINT_H
+#define QEMU_DIRTYRESTRAINT_H
+
+#define DIRTYRESTRAINT_CALC_PERIOD_TIME_S   15      /* 15s */
+
+void dirtyrestraint_calc_start(void);
+
+void dirtyrestraint_calc_state_init(int max_cpus);
+#endif
migration/dirtyrate.c +118 -0

130 lines not on this page · raw message

migration/dirtyrate.h +2 -0
index 69d4c5b..e96acdc 100644
@@ -70,6 +70,8 @@ typedef struct VcpuStat {
     DirtyRateVcpu *rates; /* array of dirty rate for each vcpu */
 } VcpuStat;
 
+typedef struct VcpuStat DirtyRatesData;
+
 /*
  * Store calculation statistics for each measure.
  */
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH v1 1/3] migration/dirtyrate: implement vCPU dirtyrate calculation periodically

Quoted 5 lines · [email protected] wrote:

From: Hyman Huang(黄勇) <[email protected]>

introduce the third method GLOBAL_DIRTY_RESTRAINT of dirty tracking for calculate dirtyrate periodly for dirty restraint.

Quoted 7 lines

implement thread for calculate dirtyrate periodly, which will be used for dirty restraint.

add dirtyrestraint.h to introduce the util function for dirty restrain.

Some comentes:

include/sysemu/dirtyrestraint.h line 19

Quoted 3 lines
include/sysemu/dirtyrestraint.h new file +3 -0
+void dirtyrestraint_calc_start(void);
+
+void dirtyrestraint_calc_state_init(int max_cpus);

dirtylimit_? instead of restraint.

We have a start function, but I can't see a finish/end/stop functions.

migration/dirtyrate.c static struct DirtyRateStat DirtyStat; · line 55

Quoted 6 lines
migration/dirtyrate.c +6 -0
+#define DIRTYRESTRAINT_CALC_TIME_MS         1000    /* 1000ms */
+
+struct {
+    DirtyRatesData data;
+    int64_t period;
+    bool enable;

Related to previous comment. I can't see where we set enable to 1, but nowhere were we set it back to 0, so this never finish.

migration/dirtyrate.c static struct DirtyRateStat DirtyStat; · line 57

Quoted 2 lines
migration/dirtyrate.c +2 -0
+    QemuCond ready_cond;
+    QemuMutex ready_mtx;

This is a question of style, but when you only have a mutex and a cond in one struct, you can use the "cond" and "mutex" names.

But as said, it is a question of style, if you preffer do it this way.

migration/dirtyrate.c static struct DirtyRateStat DirtyStat; · line 62

Quoted 2 lines
migration/dirtyrate.c +2 -0
+static inline void record_dirtypages(DirtyPageRecord *dirty_pages,
+                                     CPUState *cpu, bool start);

You have put the code at the beggining of the file, if you put it at the end of it, I think you can avoid this forward declaration.

Quoted 34 lines
migration/dirtyrate.c +34 -0
+static void dirtyrestraint_calc_func(void)
+{
+    CPUState *cpu;
+    DirtyPageRecord *dirty_pages;
+    int64_t start_time, end_time, calc_time;
+    DirtyRateVcpu rate;
+    int i = 0;
+
+    dirty_pages = g_malloc0(sizeof(*dirty_pages) *
+        dirtyrestraint_calc_state->data.nvcpu);
+
+    dirtyrestraint_global_dirty_log_start();
+
+    CPU_FOREACH(cpu) {
+        record_dirtypages(dirty_pages, cpu, true);
+    }
+
+    start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    g_usleep(DIRTYRESTRAINT_CALC_TIME_MS * 1000);
+    end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    calc_time = end_time - start_time;
+
+    dirtyrestraint_global_dirty_log_stop();
+
+    CPU_FOREACH(cpu) {
+        record_dirtypages(dirty_pages, cpu, false);
+    }
+
+    for (i = 0; i < dirtyrestraint_calc_state->data.nvcpu; i++) {
+        uint64_t increased_dirty_pages =
+            dirty_pages[i].end_pages - dirty_pages[i].start_pages;
+        uint64_t memory_size_MB =
+            (increased_dirty_pages * TARGET_PAGE_SIZE) >> 20;
+        int64_t dirtyrate = (memory_size_MB * 1000) / calc_time;

migration/dirtyrate.c static struct DirtyRateStat DirtyStat; · line 122

Quoted 10 lines
migration/dirtyrate.c +10 -0
+
+        rate.id = i;
+        rate.dirty_rate  = dirtyrate;
+        dirtyrestraint_calc_state->data.rates[i] = rate;
+
+        trace_dirtyrate_do_calculate_vcpu(i,
+            dirtyrestraint_calc_state->data.rates[i].dirty_rate);
+    }
+
+    return;

unnecesary return;

migration/dirtyrate.c static struct DirtyRateStat DirtyStat; · line 131

Quoted 9 lines
migration/dirtyrate.c +9 -0
+}
+
+static void *dirtyrestraint_calc_thread(void *opaque)
+{
+    rcu_register_thread();
+
+    while (qatomic_read(&dirtyrestraint_calc_state->enable)) {
+        dirtyrestraint_calc_func();
+        dirtyrestraint_calc_state->ready = true;

You really need this to be a global variable? You can pass it on the opaque, no?

Later, Juan.

#3 lore raw

PATCH 2/3 cpu-throttle: implement vCPU throttle

In reply to [email protected]

From: Hyman Huang(黄勇) <[email protected]>

implement dirty restraint by kicking each vcpu as the auto-converge does during migration, but just kick the specified vcpu instead, not all the vcpu of vm.

start a thread to track the dirty restraint status and adjuct the throttle pencentage dynamically depend on current and quota dirtyrate .

introduce the util function in the header for the dirty restraint implemantataion.

---
 include/sysemu/cpu-throttle.h   |  21 +++
 include/sysemu/dirtyrestraint.h |   2 +
 migration/dirtyrate.c           |   7 +
 softmmu/cpu-throttle.c          | 304 ++++++++++++++++++++++++++++++++++++++++
 softmmu/trace-events            |   5 +
 5 files changed, 339 insertions(+)
include/sysemu/cpu-throttle.h +21 -0
index d65bdef..48215d2 100644
@@ -65,4 +65,25 @@ bool cpu_throttle_active(void);
  */
 int cpu_throttle_get_percentage(void);
 
+/**
+ * dirtyrestraint_state_init:
+ *
+ * initialize golobal state for dirty restraint
+ */
+void dirtyrestraint_state_init(int max_cpus);
+
+/**
+ * dirtyrestraint_vcpu:
+ *
+ * impose dirty restraint on vcpu util reaching the quota dirtyrate
+ */
+void dirtyrestraint_vcpu(int cpu_index,
+                         uint64_t quota);
+/**
+ * dirtyrestraint_cancel_vcpu:
+ *
+ * cancel dirty restraint for the specified vcpu
+ */
+void dirtyrestraint_cancel_vcpu(int cpu_index);
+
 #endif /* SYSEMU_CPU_THROTTLE_H */
include/sysemu/dirtyrestraint.h +2 -0
index ca744af..b84a5c0 100644
@@ -14,6 +14,8 @@
 
 #define DIRTYRESTRAINT_CALC_PERIOD_TIME_S   15      /* 15s */
 
+int64_t dirtyrestraint_calc_current(int cpu_index);
+
 void dirtyrestraint_calc_start(void);
 
 void dirtyrestraint_calc_state_init(int max_cpus);
migration/dirtyrate.c +7 -0
index b453b3a..26919ff 100644
@@ -137,6 +137,13 @@ static void *dirtyrestraint_calc_thread(void *opaque)
     return NULL;
 }
 
+int64_t dirtyrestraint_calc_current(int cpu_index)
+{
+    DirtyRateVcpu *rates = dirtyrestraint_calc_state->data.rates;
+
+    return qatomic_read(&rates[cpu_index].dirty_rate);
+}
+
 void dirtyrestraint_calc_start(void)
 {
     if (likely(!qatomic_read(&dirtyrestraint_calc_state->enable))) {
softmmu/cpu-throttle.c +304 -0

316 lines not on this page · raw message

softmmu/trace-events +5 -0
index 9c88887..0307567 100644
@@ -31,3 +31,8 @@ runstate_set(int current_state, const char *current_state_str, int new_state, co
 system_wakeup_request(int reason) "reason=%d"
 qemu_system_shutdown_request(int reason) "reason=%d"
 qemu_system_powerdown_request(void) ""
+
+#cpu-throttle.c
+dirtyrestraint_state_init(int max_cpus) "dirtyrate restraint init: max cpus %d"
+dirtyrestraint_impose(int cpu_index, uint64_t quota, uint64_t current, int pct) "CPU[%d] impose dirtyrate restraint: quota %" PRIu64 ", current %" PRIu64 ", percentage %d"
+dirtyrestraint_vcpu(int cpu_index, uint64_t quota) "CPU[%d] dirtyrate restraint, quota dirtyrate %"PRIu64
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 3/3 cpus-common: implement dirty restraint on vCPU

In reply to [email protected]

From: Hyman Huang(黄勇) <[email protected]>

implement dirtyrate calculation periodically basing on dirty-ring and throttle vCPU until it reachs the quota dirtyrate given by user.

introduce qmp commands dirty-restraint/dirty-restraint-cancel to impose/cancel dirty restraint on vCPU

---
 cpus-common.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/core/cpu.h |  7 +++++++
 qapi/misc.json        | 44 ++++++++++++++++++++++++++++++++++++++++++++
 softmmu/vl.c          |  1 +
 4 files changed, 97 insertions(+)
cpus-common.c +45 -0
index 6e73d3e..3c4dbbb 100644
@@ -23,6 +23,11 @@
 #include "hw/core/cpu.h"
 #include "sysemu/cpus.h"
 #include "qemu/lockable.h"
+#include "sysemu/dirtyrestraint.h"
+#include "sysemu/cpu-throttle.h"
+#include "sysemu/kvm.h"
+#include "qapi/error.h"
+#include "qapi/qapi-commands-misc.h"
 
 static QemuMutex qemu_cpu_list_lock;
 static QemuCond exclusive_cond;
@@ -352,3 +357,43 @@ void process_queued_cpu_work(CPUState *cpu)
     qemu_mutex_unlock(&cpu->work_mutex);
     qemu_cond_broadcast(&qemu_work_cond);
 }
+
+void qmp_dirty_restraint(int64_t idx,
+                         uint64_t dirtyrate,
+                         Error **errp)
+{
+    if (!kvm_dirty_ring_enabled()) {
+        error_setg(errp, "dirty ring not enable, needed by dirty restraint!");
+        return;
+    }
+
+    dirtyrestraint_calc_start();
+    dirtyrestraint_vcpu(idx, dirtyrate);
+
+    return;
+}
+
+void qmp_dirty_restraint_cancel(int64_t idx,
+                                Error **errp)
+{
+    if (!kvm_dirty_ring_enabled()) {
+        error_setg(errp, "dirty ring not enable, needed by dirty restraint!");
+        return;
+    }
+
+    dirtyrestraint_cancel_vcpu(idx);
+
+    return;
+}
+
+void dirtyrestraint_setup(int max_cpus)
+{
+    if (!kvm_dirty_ring_enabled()) {
+        return;
+    }
+
+    dirtyrestraint_calc_state_init(max_cpus);
+    dirtyrestraint_state_init(max_cpus);
+
+    return;
+}
include/hw/core/cpu.h +7 -0
index e948e81..d2a3978 100644
@@ -881,6 +881,13 @@ void end_exclusive(void);
  */
 void qemu_init_vcpu(CPUState *cpu);
 
+/**
+ * dirtyrestraint_setup:
+ *
+ * dirtyrestraint setup.
+ */
+void dirtyrestraint_setup(int max_cpus);
+
 #define SSTEP_ENABLE  0x1  /* Enable simulated HW single stepping */
 #define SSTEP_NOIRQ   0x2  /* Do not use IRQ while single stepping */
 #define SSTEP_NOTIMER 0x4  /* Do not Timers while single stepping */
qapi/misc.json +44 -0

47 lines not on this page · raw message

softmmu/vl.c +1 -0
index 1159a64..ab914cb 100644
@@ -3776,5 +3776,6 @@ void qemu_init(int argc, char **argv, char **envp)
     qemu_init_displays();
     accel_setup_post(current_machine);
     os_setup_post();
+    dirtyrestraint_setup(current_machine->smp.max_cpus);
     resume_mux_open();
 }
Signature · 1 line
1.8.3.1