Skip to contentSkip to conversation

support dirty restraint on vCPU

PATCH Superseded [email protected] 2 participants 7 messages Nov 25, 2021 – Nov 26, 2021

v6 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

v6 cover support dirty restraint on vCPU

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

v6:
- rebase on master
- fix dirtylimit setup crash found by Markus
- polish the comments according to the advice given by Markus
- adjust the qemu qmp command tag to 7.0

v5:
- rebase on master
- adjust the throttle algorithm by removing the tuning in RESTRAINT_RATIO case so that dirty page rate could reachs the quota more quickly.
- fix percentage update in throttle iteration.

v4:
- rebase on master
- modify the following points according to the advice given by Markus
1. move the defination into migration.json
2. polish the comments of set-dirty-limit
3. do the syntax check and change dirty rate to dirty page rate

Thanks for the carefule reviews made by Markus.

Please review, thanks!

v3:
- rebase on master
- modify the following points according to the advice given by Markus
1. remove the DirtyRateQuotaVcpu and use its field as option directly
2. add comments to show details of what dirtylimit setup do
3. explain how to use dirtylimit in combination with existing qmp commands "calc-dirty-rate" and "query-dirty-rate" in documentation.

Thanks for the carefule reviews made by Markus.

Please review, thanks!

Hyman

v2:
- rebase on master
- modify the following points according to the advices given by Juan
1. rename dirtyrestraint to dirtylimit
2. implement the full lifecyle function of dirtylimit_calc, include dirtylimit_calc and dirtylimit_calc_quit
3. introduce 'quit' field in dirtylimit_calc_state to implement the dirtylimit_calc_quit
4. remove the ready_cond and ready_mtx since it may not be suitable
5. put the 'record_dirtypage' function code at the beggining of the file
6. remove the unnecesary return;
- other modifications has been made after code review
1. introduce 'bmap' and 'nr' field in dirtylimit_state to record the number of running thread forked by dirtylimit
2. stop the dirtyrate calculation thread if all the dirtylimit thread are stopped
3. do some renaming works

     dirtyrate calulation thread -> dirtylimit-calc
     dirtylimit thread -> dirtylimit-{cpu_index}
     function name do_dirtyrestraint -> dirtylimit_check
     qmp command dirty-restraint -> set-drity-limit
     qmp command dirty-restraint-cancel -> cancel-dirty-limit
     header file dirtyrestraint.h -> dirtylimit.h

Please review, thanks !

thanks for the accurate and timely advices given by Juan. we really appreciate it if corrections and suggetions about this patchset are proposed.

Best Regards !

Hyman

v1:
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 limit on vCPU

 cpus-common.c                 |  41 ++++++
 include/exec/memory.h         |   5 +-
 include/hw/core/cpu.h         |   9 ++
 include/sysemu/cpu-throttle.h |  23 +++
 include/sysemu/dirtylimit.h   |  44 ++++++
 migration/dirtyrate.c         | 139 +++++++++++++++++--
 migration/dirtyrate.h         |   2 +
 qapi/migration.json           |  47 +++++++
 softmmu/cpu-throttle.c        | 316 ++++++++++++++++++++++++++++++++++++++++++
 softmmu/trace-events          |   5 +
 softmmu/vl.c                  |   1 +
 11 files changed, 621 insertions(+), 11 deletions(-)
 create mode 100644 include/sysemu/dirtylimit.h
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v6 2/3 cpu-throttle: implement vCPU throttle

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

Impose dirty restraint on vCPU by kicking it and sleep as the auto-converge does during migration, but just kick the specified vCPU instead, not all vCPUs of vm.

Start a thread to track the dirtylimit status and adjust the throttle pencentage dynamically depend on current and quota dirtyrate.

Introduce the util function in the header for dirty page limit implementation.

---
 include/sysemu/cpu-throttle.h |  23 +++
 softmmu/cpu-throttle.c        | 316 ++++++++++++++++++++++++++++++++++++++++++
 softmmu/trace-events          |   5 +
 3 files changed, 344 insertions(+)
include/sysemu/cpu-throttle.h +23 -0
index d65bdef..726c1ce 100644
@@ -65,4 +65,27 @@ bool cpu_throttle_active(void);
  */
 int cpu_throttle_get_percentage(void);
 
+/**
+ * dirtylimit_state_init:
+ *
+ * initialize golobal state for dirtylimit
+ */
+void dirtylimit_state_init(int max_cpus);
+
+/**
+ * dirtylimit_vcpu:
+ *
+ * impose dirtylimit on vcpu util reaching the quota dirtyrate
+ */
+void dirtylimit_vcpu(int cpu_index,
+                     uint64_t quota);
+/**
+ * dirtylimit_cancel_vcpu:
+ *
+ * cancel dirtylimit for the specified vcpu
+ *
+ * Returns: the number of running threads for dirtylimit
+ */
+int dirtylimit_cancel_vcpu(int cpu_index);
+
 #endif /* SYSEMU_CPU_THROTTLE_H */
softmmu/cpu-throttle.c +316 -0

328 lines not on this page · raw message

softmmu/trace-events +5 -0
index 9c88887..a7c9c04 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
+dirtylimit_state_init(int max_cpus) "dirtylimit state init: max cpus %d"
+dirtylimit_impose(int cpu_index, uint64_t quota, uint64_t current, int pct) "CPU[%d] impose dirtylimit: quota %" PRIu64 ", current %" PRIu64 ", percentage %d"
+dirtylimit_vcpu(int cpu_index, uint64_t quota) "CPU[%d] set quota dirtylimit %"PRIu64
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v6 3/3 cpus-common: implement dirty page limit 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 dirty page rate given by user.

Introduce qmp commands set-dirty-limit/cancel-dirty-limit to set/cancel dirty page limit on vCPU.

---
 cpus-common.c         | 41 +++++++++++++++++++++++++++++++++++++++++
 include/hw/core/cpu.h |  9 +++++++++
 qapi/migration.json   | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 softmmu/vl.c          |  1 +
 4 files changed, 98 insertions(+)
cpus-common.c +41 -0
index 6e73d3e..3c156b3 100644
@@ -23,6 +23,11 @@
 #include "hw/core/cpu.h"
 #include "sysemu/cpus.h"
 #include "qemu/lockable.h"
+#include "sysemu/dirtylimit.h"
+#include "sysemu/cpu-throttle.h"
+#include "sysemu/kvm.h"
+#include "qapi/error.h"
+#include "qapi/qapi-commands-migration.h"
 
 static QemuMutex qemu_cpu_list_lock;
 static QemuCond exclusive_cond;
@@ -352,3 +357,39 @@ void process_queued_cpu_work(CPUState *cpu)
     qemu_mutex_unlock(&cpu->work_mutex);
     qemu_cond_broadcast(&qemu_work_cond);
 }
+
+void qmp_set_dirty_limit(int64_t idx,
+                         uint64_t dirtyrate,
+                         Error **errp)
+{
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        error_setg(errp, "setting a dirty page limit requires support from dirty ring");
+        return;
+    }
+
+    dirtylimit_calc();
+    dirtylimit_vcpu(idx, dirtyrate);
+}
+
+void qmp_cancel_dirty_limit(int64_t idx,
+                            Error **errp)
+{
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        error_setg(errp, "no need to cancel a dirty page limit as dirty ring not enabled");
+        return;
+    }
+
+    if (unlikely(!dirtylimit_cancel_vcpu(idx))) {
+        dirtylimit_calc_quit();
+    }
+}
+
+void dirtylimit_setup(int max_cpus)
+{
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        return;
+    }
+
+    dirtylimit_calc_state_init(max_cpus);
+    dirtylimit_state_init(max_cpus);
+}
include/hw/core/cpu.h +9 -0
index e948e81..11df012 100644
@@ -881,6 +881,15 @@ void end_exclusive(void);
  */
 void qemu_init_vcpu(CPUState *cpu);
 
+/**
+ * dirtylimit_setup:
+ *
+ * Initializes the global state of dirtylimit calculation and
+ * dirtylimit itself. This is prepared for vCPU dirtylimit which
+ * could be triggered during vm lifecycle.
+ */
+void dirtylimit_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/migration.json +47 -0

53 lines not on this page · raw message

softmmu/vl.c +1 -0
index 620a1f1..0f83ce3 100644
@@ -3777,5 +3777,6 @@ void qemu_init(int argc, char **argv, char **envp)
     qemu_init_displays();
     accel_setup_post(current_machine);
     os_setup_post();
+    dirtylimit_setup(current_machine->smp.max_cpus);
     resume_mux_open();
 }
Signature · 1 line
1.8.3.1
#1 lore raw

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

In reply to [email protected]

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

Introduce the third method GLOBAL_DIRTY_LIMIT of dirty tracking for calculate dirtyrate periodly.

Implement thread for calculating dirtyrate periodly, which will be used for for vCPU throttle implementation.

Add dirtylimit.h to introduce the util function for dirty page limit implementation.

---
 include/exec/memory.h       |   5 +-
 include/sysemu/dirtylimit.h |  44 ++++++++++++++
 migration/dirtyrate.c       | 139 ++++++++++++++++++++++++++++++++++++++++----
 migration/dirtyrate.h       |   2 +
 4 files changed, 179 insertions(+), 11 deletions(-)
 create mode 100644 include/sysemu/dirtylimit.h
include/exec/memory.h +4 -1
index 20f1b27..606bec8 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 limit */
+#define GLOBAL_DIRTY_LIMIT      (1U << 2)
+
+#define GLOBAL_DIRTY_MASK  (0x7)
 
 extern unsigned int global_dirty_tracking;
 
include/sysemu/dirtylimit.h new file +44 -0

44 lines not on this page · raw message

migration/dirtyrate.c +129 -10

157 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
#5 lore raw

Re: [PATCH v6 3/3] cpus-common: implement dirty page limit on vCPU

In reply to [email protected]
Quoted 34 lines · [email protected] writes:

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

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

Introduce qmp commands set-dirty-limit/cancel-dirty-limit to set/cancel dirty page limit on vCPU.

---
 cpus-common.c         | 41 +++++++++++++++++++++++++++++++++++++++++
 include/hw/core/cpu.h |  9 +++++++++
 qapi/migration.json   | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 softmmu/vl.c          |  1 +
 4 files changed, 98 insertions(+)
cpus-common.c +5 -0
index 6e73d3e..3c156b3 100644
@@ -23,6 +23,11 @@
 #include "hw/core/cpu.h"
 #include "sysemu/cpus.h"
 #include "qemu/lockable.h"
+#include "sysemu/dirtylimit.h"
+#include "sysemu/cpu-throttle.h"
+#include "sysemu/kvm.h"
+#include "qapi/error.h"
+#include "qapi/qapi-commands-migration.h"
 
 static QemuMutex qemu_cpu_list_lock;
 static QemuCond exclusive_cond;

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 366

Quoted 10 lines
cpus-common.c +7 -0
@@ -352,3 +357,39 @@ void process_queued_cpu_work(CPUState *cpu)
     qemu_mutex_unlock(&cpu->work_mutex);
     qemu_cond_broadcast(&qemu_work_cond);
 }
+
+void qmp_set_dirty_limit(int64_t idx,
+                         uint64_t dirtyrate,
+                         Error **errp)
+{
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        error_setg(errp, "setting a dirty page limit requires support from dirty ring");

Can we phrase the message in a way that gives the user a chance to guess what he needs to do to avoid it?

Perhaps: "setting a dirty page limit requires KVM with accelerator property 'dirty-ring-size' set".

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 376

Quoted 10 lines
cpus-common.c +10 -0
+        return;
+    }
+
+    dirtylimit_calc();
+    dirtylimit_vcpu(idx, dirtyrate);
+}
+
+void qmp_cancel_dirty_limit(int64_t idx,
+                            Error **errp)
+{

Three cases:

Case 1: enable is impossible, so nothing to do.

Case 2: enable is possible and we actually enabled.

Case 3: enable is possible, but we didn't. Nothing to do.

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 379

Quoted 3 lines
cpus-common.c +3 -0
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        error_setg(errp, "no need to cancel a dirty page limit as dirty ring not enabled");
+        return;

This is case 1. We error out.

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 382

Quoted 3 lines
cpus-common.c +3 -0
+    }
+
+    if (unlikely(!dirtylimit_cancel_vcpu(idx))) {

I don't think unlikely() matters here.

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 384

Quoted 2 lines
cpus-common.c +2 -0
+        dirtylimit_calc_quit();
+    }

In case 2, dirtylimit_calc_quit() returns zero if this was the last limit, else non-zero. If the former, we request the thread to stop.

In case 3, dirtylimit_calc_quit() returns zero, and we do nothing.

Why is case 1 and error, but case 3 isn't?

Both could silently do nothing, like case 3 does now.

Both could error out, like case 1 does now. A possible common error
message: "there is no dirty page limit to cancel".

I'd be okay with consistently doing nothing, and with consistently erroring out.

Quoted 36 lines
cpus-common.c +11 -0
+}
+
+void dirtylimit_setup(int max_cpus)
+{
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        return;
+    }
+
+    dirtylimit_calc_state_init(max_cpus);
+    dirtylimit_state_init(max_cpus);
+}
include/hw/core/cpu.h +9 -0
index e948e81..11df012 100644
@@ -881,6 +881,15 @@ void end_exclusive(void);
  */
 void qemu_init_vcpu(CPUState *cpu);
 
+/**
+ * dirtylimit_setup:
+ *
+ * Initializes the global state of dirtylimit calculation and
+ * dirtylimit itself. This is prepared for vCPU dirtylimit which
+ * could be triggered during vm lifecycle.
+ */
+void dirtylimit_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/migration.json +0 -0
index bbfd48c..2b0fe19 100644

qapi/migration.json line 1857

Quoted 8 lines
qapi/migration.json +5 -0
@@ -1850,6 +1850,53 @@
 { 'command': 'query-dirty-rate', 'returns': 'DirtyRateInfo' }
 
 ##
+# @set-dirty-limit:
+#
+# Set the upper limit of dirty page rate for a vCPU.
+#
+# This command could be used to cap the vCPU memory load, which is also

"Could be used" suggests there are other uses. I don't think there are.

qapi/migration.json line 1859

Quoted 2 lines
qapi/migration.json +2 -0
+# refered as "dirty page rate". Users can use set-dirty-limit unconditionally,
+# but if one want to know which vCPU is in high memory load and which vCPU

"one wants"

qapi/migration.json line 1860

Quoted 1 line
qapi/migration.json +1 -0
+# should be limited, using calc-dirty-rate command with dirty-ring mode maybe

"may be"

qapi/migration.json line 1861

Quoted 1 line
qapi/migration.json +1 -0
+# an availiable method. Note that this command requires support from dirty

"available". Consider using a spell checker.

qapi/migration.json line 1863

Quoted 2 lines
qapi/migration.json +2 -0
+# ring, which means property "dirty-ring-size" of accelerator object "kvm"
+# must be set.

Please limit doc comment line length to approximately 70 characters for legibility.

Here's my try:

##
# @set-dirty-limit:
#
# Set the upper limit of dirty page rate for a virtual CPU. #
# Requires KVM with accelerator property "dirty-ring-size" set. # A virtual CPU's dirty page rate is a measure of its memory load. # To observe dirty page rates, use @calc-dirty-rate.

qapi/migration.json line 1865

Quoted 2 lines
qapi/migration.json +2 -0
+#
+# @cpu-index: vCPU index to set dirty page limit.

I'd go with

# @cpu-index: index of the virtual CPU.

Quoted 48 lines
qapi/migration.json +34 -0
+#
+# @dirty-rate: upper limit for the specified vCPU's dirty page rate (MB/s)
+#
+# Since: 7.0
+#
+# Example:
+#   {"execute": "set-dirty-limit"}
+#    "arguments": { "cpu-index": 0,
+#                   "dirty-rate": 200 } }
+#
+##
+{ 'command': 'set-dirty-limit',
+  'data': { 'cpu-index': 'int', 'dirty-rate': 'uint64' } }
+
+##
+# @cancel-dirty-limit:
+#
+# Cancel the dirty page limit for the vCPU which has been set with
+# set-dirty-limit command. Note that this command requires support from
+# dirty ring, same as the "set-dirty-limit" command.
+#
+# @cpu-index: vCPU index to cancel the dirty page limit
+#
+# Since: 7.0
+#
+# Example:
+#   {"execute": "cancel-dirty-limit"}
+#    "arguments": { "cpu-index": 0 } }
+#
+##
+{ 'command': 'cancel-dirty-limit',
+  'data': { 'cpu-index': 'int' } }
+
+##
 # @snapshot-save:
 #
 # Save a VM snapshot
softmmu/vl.c +1 -0
index 620a1f1..0f83ce3 100644
@@ -3777,5 +3777,6 @@ void qemu_init(int argc, char **argv, char **envp)
     qemu_init_displays();
     accel_setup_post(current_machine);
     os_setup_post();
+    dirtylimit_setup(current_machine->smp.max_cpus);
     resume_mux_open();
 }
author
#6 lore raw

Re: [PATCH v6 3/3] cpus-common: implement dirty page limit on vCPU

In reply to Markus Armbruster
Quoted 49 lines · Markus Armbruster
Quoted 34 lines · [email protected] writes:

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

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

Introduce qmp commands set-dirty-limit/cancel-dirty-limit to set/cancel dirty page limit on vCPU.

---
  cpus-common.c         | 41 +++++++++++++++++++++++++++++++++++++++++
  include/hw/core/cpu.h |  9 +++++++++
  qapi/migration.json   | 47 +++++++++++++++++++++++++++++++++++++++++++++++
  softmmu/vl.c          |  1 +
  4 files changed, 98 insertions(+)
cpus-common.c +5 -0
index 6e73d3e..3c156b3 100644
@@ -23,6 +23,11 @@
  #include "hw/core/cpu.h"
  #include "sysemu/cpus.h"
  #include "qemu/lockable.h"
+#include "sysemu/dirtylimit.h"
+#include "sysemu/cpu-throttle.h"
+#include "sysemu/kvm.h"
+#include "qapi/error.h"
+#include "qapi/qapi-commands-migration.h"
 
  static QemuMutex qemu_cpu_list_lock;
  static QemuCond exclusive_cond;

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 366

Quoted 10 lines
cpus-common.c +7 -0
@@ -352,3 +357,39 @@ void process_queued_cpu_work(CPUState *cpu)
      qemu_mutex_unlock(&cpu->work_mutex);
      qemu_cond_broadcast(&qemu_work_cond);
  }
+
+void qmp_set_dirty_limit(int64_t idx,
+                         uint64_t dirtyrate,
+                         Error **errp)
+{
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        error_setg(errp, "setting a dirty page limit requires support from dirty ring");

Can we phrase the message in a way that gives the user a chance to guess what he needs to do to avoid it?

Quoted 2 lines
Quoted 1 line

Perhaps: "setting a dirty page limit requires KVM with accelerator

property 'dirty-ring-size' set".

Sound good, this make things more clear.

Quoted 30 lines

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 376

Quoted 11 lines
cpus-common.c +10 -0
+        return;
+    }
+
+    dirtylimit_calc();
+    dirtylimit_vcpu(idx, dirtyrate);
+}
+
+void qmp_cancel_dirty_limit(int64_t idx,
+                            Error **errp)
+{

Three cases:

Case 1: enable is impossible, so nothing to do.

Case 2: enable is possible and we actually enabled.

Case 3: enable is possible, but we didn't. Nothing to do.

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 379

Quoted 3 lines
cpus-common.c +3 -0
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        error_setg(errp, "no need to cancel a dirty page limit as dirty ring not enabled");
+        return;

This is case 1. We error out.

cpus-common.c void process_queued_cpu_work(CPUState *cpu) · line 382

Quoted 3 lines
cpus-common.c +3 -0
+    }
+
+    if (unlikely(!dirtylimit_cancel_vcpu(idx))) {
Quoted 7 lines

I don't think unlikely() matters here.

cpus-common.c

Quoted 2 lines
cpus-common.c +2 -0
+        dirtylimit_calc_quit();
+    }

In case 2, dirtylimit_calc_quit() returns zero if this was the last
limit, else non-zero. If the former, we request the thread to stop.I am wildly guessing you misunderstood the function

dirtylimit_cancel_vcpu, see below.

Quoted 2 lines

In case 3, dirtylimit_calc_quit() returns zero, and we do nothing.

In this case, we cancel the "dirtylimit thread" in function dirtylimit_cancel_vcpu actually, if it was the last limit thread of the whole vm, dirtylimit_cancel_vcpu return zero and we request the dirtyrate calculation thread to stop, so we call the function dirtylimit_calc_quit , which stop the "dirtyrate calculation thread" internally.

Quoted 49 lines

Why is case 1 and error, but case 3 isn't?

Both could silently do nothing, like case 3 does now.

Both could error out, like case 1 does now. A possible common error
message: "there is no dirty page limit to cancel".

I'd be okay with consistently doing nothing, and with consistently erroring out.

Quoted 38 lines
cpus-common.c +11 -0
+}
+
+void dirtylimit_setup(int max_cpus)
+{
+    if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
+        return;
+    }
+
+    dirtylimit_calc_state_init(max_cpus);
+    dirtylimit_state_init(max_cpus);
+}
include/hw/core/cpu.h +9 -0
index e948e81..11df012 100644
@@ -881,6 +881,15 @@ void end_exclusive(void);
   */
  void qemu_init_vcpu(CPUState *cpu);
 
+/**
+ * dirtylimit_setup:
+ *
+ * Initializes the global state of dirtylimit calculation and
+ * dirtylimit itself. This is prepared for vCPU dirtylimit which
+ * could be triggered during vm lifecycle.
+ */
+void dirtylimit_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/migration.json +0 -0
index bbfd48c..2b0fe19 100644
@@ -1850,6 +1850,53 @@
  { 'command': 'query-dirty-rate', 'returns': 'DirtyRateInfo' }
 
Quoted 9 lines

qapi/migration.json line 1857

Quoted 6 lines
qapi/migration.json +5 -0
  ##
+# @set-dirty-limit:
+#
+# Set the upper limit of dirty page rate for a vCPU.
+#
+# This command could be used to cap the vCPU memory load, which is also

"Could be used" suggests there are other uses. I don't think there are.

Quoted 1 line
Quoted 1 line

+# refered as "dirty page rate". Users can use set-dirty-limit

unconditionally,

Quoted 29 lines
Quoted 1 line

+# but if one want to know which vCPU is in high memory load and which vCPU

"one wants"

Quoted 1 line

+# should be limited, using calc-dirty-rate command with dirty-ring mode maybe

"may be"

Quoted 1 line

+# an availiable method. Note that this command requires support from dirty

"available". Consider using a spell checker.

qapi/migration.json line 1863

Quoted 2 lines
qapi/migration.json +2 -0
+# ring, which means property "dirty-ring-size" of accelerator object "kvm"
+# must be set.

Please limit doc comment line length to approximately 70 characters for legibility.

Here's my try:

##
# @set-dirty-limit:
#
# Set the upper limit of dirty page rate for a virtual CPU. #
# Requires KVM with accelerator property "dirty-ring-size" set. # A virtual CPU's dirty page rate is a measure of its memory load. # To observe dirty page rates, use @calc-dirty-rate.

Quoted 7 lines

qapi/migration.json

Quoted 2 lines
qapi/migration.json +2 -0
+#
+# @cpu-index: vCPU index to set dirty page limit.

I'd go with

# @cpu-index: index of the virtual CPU.

Indeed, this make the document more legible. Thank Markus very much for the careful code review, i'll pick up all this next version.

Quoted 49 lines
Quoted 48 lines
qapi/migration.json +34 -0
+#
+# @dirty-rate: upper limit for the specified vCPU's dirty page rate (MB/s)
+#
+# Since: 7.0
+#
+# Example:
+#   {"execute": "set-dirty-limit"}
+#    "arguments": { "cpu-index": 0,
+#                   "dirty-rate": 200 } }
+#
+##
+{ 'command': 'set-dirty-limit',
+  'data': { 'cpu-index': 'int', 'dirty-rate': 'uint64' } }
+
+##
+# @cancel-dirty-limit:
+#
+# Cancel the dirty page limit for the vCPU which has been set with
+# set-dirty-limit command. Note that this command requires support from
+# dirty ring, same as the "set-dirty-limit" command.
+#
+# @cpu-index: vCPU index to cancel the dirty page limit
+#
+# Since: 7.0
+#
+# Example:
+#   {"execute": "cancel-dirty-limit"}
+#    "arguments": { "cpu-index": 0 } }
+#
+##
+{ 'command': 'cancel-dirty-limit',
+  'data': { 'cpu-index': 'int' } }
+
+##
  # @snapshot-save:
  #
  # Save a VM snapshot
softmmu/vl.c +1 -0
index 620a1f1..0f83ce3 100644
@@ -3777,5 +3777,6 @@ void qemu_init(int argc, char **argv, char **envp)
      qemu_init_displays();
      accel_setup_post(current_machine);
      os_setup_post();
+    dirtylimit_setup(current_machine->smp.max_cpus);
      resume_mux_open();
  }