support dirty restraint on vCPU
v12 cover support dirty restraint on vCPU
From: Hyman Huang(黄勇) <[email protected]>
v12
- rebase on master
- add a new commmit to refactor per-vcpu dirty ring reaping, which can resolve the "vcpu miss the chances to sleep" problem
- remove the dirtylimit_thread and implemtment throttle in bottom half instead.
- let the dirty ring reaper thread keep sleeping when dirtylimit is in service
- introduce cpu_list_generation_id to identify cpu_list changing.
- keep taking the cpu_list_lock during dirty_stat_wait to prevent vcpu plug/unplug when calculating the dirty page rate
- move the dirtylimit global initializations out of dirtylimit_set_vcpu and do some code clean
- add DIRTYLIMIT_LINEAR_ADJUSTMENT_WATERMARK in case of oscillation when throttling
- remove the unmatched count field in dirtylimit_state
- add stub to fix build on non-x86
- refactor the documents
Thanks Peter and Markus for reviewing the previous versions, please review.
Thanks,
Yong
v11
- rebase on master
- add a commit " refactor dirty page rate calculation" so that dirty page rate limit can reuse the calculation logic.
- handle the cpu hotplug/unplug case in the dirty page rate calculation logic.
- modify the qmp commands according to Markus's advice.
- introduce a standalone file dirtylimit.c to implement dirty page rate limit
- check if dirty limit in service by dirtylimit_state pointer instead of global variable
- introduce dirtylimit_mutex to protect dirtylimit_state
- do some code clean and docs
See the commit for more detail, thanks Markus and Peter very mush for the code review and give the experienced and insightful advices, most modifications are based on these advices.
v10:
- rebase on master
- make the following modifications on patch [1/3]:
1. Make "dirtylimit-calc" thread joinable and join it after quitting.
2. Add finalize function to free dirtylimit_calc_state
3. Do some code clean work
- make the following modifications on patch [2/3]:
1. Remove the original implementation of throttle according to Peter's advice.
2. Introduce a negative feedback system and implement the throttle on all vcpu in one thread named "dirtylimit".
3. Simplify the algo when calculation the throttle_us_per_full:
increase/decrease linearly when there exists a wide difference between quota and current dirty page rate, increase/decrease a fixed time slice when the difference is narrow. This makes throttle responds faster and reach the quota smoothly.
4. Introduce a unfit_cnt in algo to make sure throttle really takes effect.
5. Set the max sleep time 99 times more than "ring_full_time_us".
6. Make "dirtylimit" thread joinable and join it after quitting.
- make the following modifications on patch [3/3]:
1. Remove the unplug cpu handling logic.
2. "query-vcpu-dirty-limit" only return dirtylimit information of vcpus that enable dirtylimit
3. Remove the "dirtylimit_setup" function
4. Trigger the dirtylimit and initialize the global state only when someone enable dirtylimit, and finalize it after the last dirtylimit be canceled.
5. Redefine the qmp command vcpu-dirty-limit/query-vcpu-dirty-limit:
enable/disable dirtylimit use a single command "vcpu-dirty-limit", to enable/disabled dirtylimit on specified vcpu only if "cpu-index" is specified, otherwise, all vcpu will be affected.
6. Redefine the hmp command vcpu_dirty_limit/info vcpu_dirty_limit
- other points about the code review:
1. "merge the code of calculation dirty page rate"
I think maybe it's not suitable to touch the 'calc-dirty-rate', because 'calc-dirty-rate' will stop sync log after calculating the dirtyrate and the 'dirtylimit-cal' will not untill the last dirtylimit be canceled, if we merge the GLOBAL_DIRTY_LIMIT into GLOBAL_DIRTY_DIRTYRATE, the two are interacted with each other.
2. The new implementaion of throttle algo enlightened by Peter responds faster and consume less cpu resource than the older, we make a impressed progress.
And there is a viewpoint may be discussed, it is that the new throttle logic is "passive", vcpu sleeps only after dirty ring, is full, unlike the "auto-converge" which will kick vcpu instead in a fixed slice time. If the vcpu is memory-write intensive and the ring size is large, it will produce dirty memory during the dirty ring full time and the throttle works not so good, it means the throttle depends on the dirty ring size.
I actually tested the new algo in two case:
case 1: dirty-ring-size: 4096, dirtyrate: 1170MB/s
result: minimum quota dirtyrate is 25MB/s or even less
minimum vcpu util is 6%
case 2: dirty-ring-size: 65536, dirtyrate: 1170MB/s
result: minimum quota dirtyrate is 256MB/s
minimum vcpu util is 24%
I post this just for discussion, i think this is not a big deal beacase if we set the dirty-ring-size to the maximum value(65536), we assume the server's bandwidth is capable of handling it.
3. I hard-code the acceptable deviation value to 25MB/s, see the macro DIRTYLIMIT_TOLERANCE_RANGE. I'm struggling to decide whether to let it configurable
4. Another point is the unplug cpu handle, current algo affects the unplugged vcpu, if we set dirty limit on it, we should fork 2 thread "dirtylimit" and "dirtylimit-calc" but do nothing, once the vcpu is hot-plugged, dirty limit works, i think the logic is ok but still there can be different advice.
- to let developers play with it easier, i post the hmp usage example:
(qemu) vcpu_dirty_limit -g on -1 500
[Please use 'info vcpu_dirty_limit' to query dirty limit for virtual CPU]
(qemu) info vcpu_dirty_limit
vcpu[0], limit rate 500 (MB/s), current rate 415 (MB/s) vcpu[1], limit rate 500 (MB/s), current rate 496 (MB/s) vcpu[2], limit rate 500 (MB/s), current rate 0 (MB/s) vcpu[3], limit rate 500 (MB/s), current rate 0 (MB/s)
(qemu) vcpu_dirty_limit -g off
[Please use 'info vcpu_dirty_limit' to query dirty limit for virtual CPU]
(qemu) info vcpu_dirty_limit
Dirty page limit not enabled!
(qemu) vcpu_dirty_limit on 0 300
[Please use 'info vcpu_dirty_limit' to query dirty limit for virtual CPU]
(qemu) vcpu_dirty_limit on 1 500
[Please use 'info vcpu_dirty_limit' to query dirty limit for virtual CPU]
(qemu) info vcpu_dirty_limit
vcpu[0], limit rate 300 (MB/s), current rate 342 (MB/s) vcpu[1], limit rate 500 (MB/s), current rate 485 (MB/s)
(qemu) vcpu_dirty_limit off 0
[Please use 'info vcpu_dirty_limit' to query dirty limit for virtual CPU]
(qemu) info vcpu_dirty_limit
vcpu[1], limit rate 500 (MB/s), current rate 528 (MB/s)
(qemu) vcpu_dirty_limit off 1
[Please use 'info vcpu_dirty_limit' to query dirty limit for virtual CPU]
(qemu) info vcpu_dirty_limit
Dirty page limit not enabled!
Thanks very much for the instructive algo suggestion given by Peter, the comment and other code reviews made by Markus.
Please review, thanks!
v9:
- rebase on master
- fix the meson directory change, keep it untouched.
v8:
- rebase on master
- polish the error message and remove the "unlikely" compilation syntax according to the advice given by Markus.
- keep the dirty tracking enabled during "dirtylimit-calc" lifecycle so that the overhead can be reduced according to the advice given by Peter.
- merge the "set/cancel" qmp commands into one named "vcpu-dirty-limit" and introduce qmp command "query-vcpu-dirty-limit" to query dirty limit information about virtual CPU, according to the advice given by Peter.
- check if vcpu index is valid and handle the unplug case before enabling, disabling dirty limit for virtual CPU.
- introduce hmp commands so developers can play with them easier, use "vcpu_dirty_limit" to enable dirty limit and "info vcpu_dirty_limit" to query.
The patch [2/3] has not been touched so far. Any corrections and suggetions are welcome.
Please review, thanks!
v7:
- rebase on master
- polish the comments and error message according to the advices given by Markus
- introduce dirtylimit_enabled function to pre-check if dirty page limit is enabled before canceling.
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.hPlease 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 (5):
accel/kvm/kvm-all: refactor per-vcpu dirty ring reaping migration/dirtyrate: refactor dirty page rate calculation softmmu/dirtylimit: implement vCPU dirtyrate calculation periodically softmmu/dirtylimit: implement virtual CPU throttle softmmu/dirtylimit: implement dirty page rate limit
accel/kvm/kvm-all.c | 36 ++- accel/stubs/kvm-stub.c | 5 + hmp-commands-info.hx | 13 + hmp-commands.hx | 32 +++ include/exec/memory.h | 5 +- include/hw/core/cpu.h | 6 + include/monitor/hmp.h | 3 + include/sysemu/dirtylimit.h | 38 +++ include/sysemu/dirtyrate.h | 29 +++ include/sysemu/kvm.h | 2 + migration/dirtyrate.c | 211 +++++++++------ migration/dirtyrate.h | 7 +- qapi/migration.json | 80 ++++++ softmmu/dirtylimit.c | 622 ++++++++++++++++++++++++++++++++++++++++++++ softmmu/meson.build | 1 + softmmu/trace-events | 8 + 16 files changed, 1002 insertions(+), 96 deletions(-) create mode 100644 include/sysemu/dirtylimit.h create mode 100644 include/sysemu/dirtyrate.h create mode 100644 softmmu/dirtylimit.c
Signature · 1 line
1.8.3.1
PATCH v12 1/5 accel/kvm/kvm-all: refactor per-vcpu dirty ring reaping
From: Hyman Huang(黄勇) <[email protected]>
Add a non-required argument 'CPUState' to kvm_dirty_ring_reap so that it can cover single vcpu dirty ring reaping scenario.
Only just reaping the target vcpu dirty ring instead of all when vcpu thread return to user space caused by KVM_EXIT_DIRTY_RING_FULL.
--- accel/kvm/kvm-all.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
accel/kvm/kvm-all.c +13 -10
@@ -756,17 +756,20 @@ static uint32_t kvm_dirty_ring_reap_one(KVMState *s, CPUState *cpu) }/* Must be with slots_lock held */ -static uint64_t kvm_dirty_ring_reap_locked (KVMState *s) +static uint64_t kvm_dirty_ring_reap_locked (KVMState *s,CPUState * cpu) {int ret; -CPUState *cpu;uint64_t total =0 ;int64_t stamp; stamp =get_clock (); -CPU_FOREACH (cpu) { - total +=kvm_dirty_ring_reap_one (s, cpu); +if (cpu) { + total =kvm_dirty_ring_reap_one (s, cpu); + }else { +CPU_FOREACH (cpu) { + total +=kvm_dirty_ring_reap_one (s, cpu); + } }if (total) {
@@ -787,7 +790,7 @@ static uint64_t kvm_dirty_ring_reap_locked(KVMState *s)* Currently for simplicity, we must hold BQL before calling this. We can * consider to drop the BQL if we're clear with all the race conditions. */ -static uint64_t kvm_dirty_ring_reap (KVMState *s) +static uint64_t kvm_dirty_ring_reap (KVMState *s,CPUState *cpu) {uint64_t total;
@@ -807,7 +810,7 @@ static uint64_t kvm_dirty_ring_reap(KVMState *s)* reset below. */ kvm_slots_lock (); - total =kvm_dirty_ring_reap_locked (s); + total =kvm_dirty_ring_reap_locked (s, cpu);kvm_slots_unlock ();return total;
@@ -854,7 +857,7 @@ static void kvm_dirty_ring_flush(void)* vcpus out in a synchronous way. */ kvm_cpu_synchronize_kick_all (); -kvm_dirty_ring_reap (kvm_state); +kvm_dirty_ring_reap (kvm_state,NULL );trace_kvm_dirty_ring_flush (1 ); }
@@ -1398,7 +1401,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,* Not easy. Let's cross the fingers until it's fixed. */ if (kvm_state->kvm_dirty_ring_size) { -kvm_dirty_ring_reap_locked (kvm_state); +kvm_dirty_ring_reap_locked (kvm_state,NULL ); }else {kvm_slot_get_dirty_log (kvm_state, mem); }
@@ -1470,7 +1473,7 @@ static void *kvm_dirty_ring_reaper_thread(void *data) r->reaper_state =KVM_DIRTY_RING_REAPER_REAPING ;qemu_mutex_lock_iothread (); -kvm_dirty_ring_reap (s); +kvm_dirty_ring_reap (s,NULL );qemu_mutex_unlock_iothread (); r->reaper_iteration++;
@@ -2956,7 +2959,7 @@ int kvm_cpu_exec(CPUState *cpu)*/ trace_kvm_dirty_ring_full (cpu->cpu_index);qemu_mutex_lock_iothread (); -kvm_dirty_ring_reap (kvm_state); +kvm_dirty_ring_reap (kvm_state, cpu);qemu_mutex_unlock_iothread (); ret =0 ;break ;
Signature · 1 line
1.8.3.1
Re: [PATCH v12 1/5] accel/kvm/kvm-all: refactor per-vcpu dirty ring reaping
accel/kvm/kvm-all.c int kvm_cpu_exec(CPUState *cpu) · line 2962
Quoted 6 lines · On Mon, Jan 24, 2022 at 10:10:36PM +0800, [email protected] wrote:
accel/kvm/kvm-all.c +1
-1@@ -2956,7 +2959,7 @@ int kvm_cpu_exec(CPUState *cpu)*/ trace_kvm_dirty_ring_full (cpu->cpu_index);qemu_mutex_lock_iothread (); -kvm_dirty_ring_reap (kvm_state); +kvm_dirty_ring_reap (kvm_state, cpu);
Shall we keep passing in NULL in this patch, and make it conditionally taking cpu parameter if dirty limit enabled?
Ring reset can still be expensive, so ideally we can still try the best to reap as much PFNs as possible, as long as we still don't need accuracy on RING_FULL exit events.
Quoted 6 lines
accel/kvm/kvm-all.c +0
-0qemu_mutex_unlock_iothread (); ret =0 ;break ;Signature · 1 line
1.8.3.1
Signature · 1 line
Peter Xu
Re: [PATCH v12 1/5] accel/kvm/kvm-all: refactor per-vcpu dirty ring reaping
Quoted 11 lines · Peter Xu
accel/kvm/kvm-all.c int kvm_cpu_exec(CPUState *cpu) · line 2962
Quoted 6 lines · On Mon, Jan 24, 2022 at 10:10:36PM +0800, [email protected] wrote:
accel/kvm/kvm-all.c +1
-1@@ -2956,7 +2959,7 @@ int kvm_cpu_exec(CPUState *cpu)*/ trace_kvm_dirty_ring_full (cpu->cpu_index);qemu_mutex_lock_iothread (); -kvm_dirty_ring_reap (kvm_state); +kvm_dirty_ring_reap (kvm_state, cpu);Shall we keep passing in NULL in this patch, and make it conditionally taking cpu parameter if dirty limit enabled?
Ok,so we should pass the cpu parameter only if dirtylimit in service.
Quoted 11 lines
Ring reset can still be expensive, so ideally we can still try the best to reap as much PFNs as possible, as long as we still don't need accuracy on
RING_FULLexit events.Quoted 6 lines
accel/kvm/kvm-all.c +0
-0qemu_mutex_unlock_iothread (); ret =0 ;break ;Signature · 1 line
1.8.3.1
Signature · 3 lines
Best regard Hyman Huang(黄勇)
PATCH v12 2/5 migration/dirtyrate: refactor dirty page rate calculation
In reply to [email protected]From: Hyman Huang(黄勇) <[email protected]>
abstract out dirty log change logic into functionglobal_dirty_log_change.
abstract out dirty page rate calculation logic via dirty-ring into function vcpu_calculate_dirtyrate.
abstract out mathematical dirty page rate calculation into do_calculate_dirtyrate, decouple it from DirtyStat.
rename set_sample_page_period to dirty_stat_wait, which is well-understood and will be reused in dirtylimit.
add cpu_list_lock and cpu_list_generation_id to handle cpu hotplug/unplug case when calculating dirty page rate.
export util functions outside migration.
--- cpus-common.c | 3 + include/sysemu/dirtyrate.h | 31 +++++++ migration/dirtyrate.c | 222 +++++++++++++++++++++++++++++---------------- migration/dirtyrate.h | 7 +- 4 files changed, 178 insertions(+), 85 deletions(-) create mode 100644 include/sysemu/dirtyrate.h
cpus-common.c +3 -0
@@ -73,6 +73,7 @@ static int cpu_get_free_index(void) }CPUTailQ cpus =QTAILQ_HEAD_INITIALIZER (cpus); +unsigned int cpu_list_generation_id;void cpu_list_add (CPUState *cpu) {
@@ -84,6 +85,7 @@ void cpu_list_add(CPUState *cpu)assert (!cpu_index_auto_assigned); }QTAILQ_INSERT_TAIL_RCU (&cpus, cpu, node); + cpu_list_generation_id++; }void cpu_list_remove (CPUState *cpu)
@@ -96,6 +98,7 @@ void cpu_list_remove(CPUState *cpu)QTAILQ_REMOVE_RCU (&cpus, cpu, node); cpu->cpu_index =UNASSIGNED_CPU_INDEX ; + cpu_list_generation_id++; }CPUState *qemu_get_cpu (int index)
include/sysemu/dirtyrate.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* dirty page rate helper functions +* +* Copyright (c) 2022 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_DIRTYRATE_H +#define QEMU_DIRTYRATE_H + +extern unsigned int cpu_list_generation_id; + +typedef struct VcpuStat { +int nvcpu;/* number of vcpu */ +DirtyRateVcpu *rates;/* array of dirty rate for each vcpu */ +}VcpuStat ; + +int64_t vcpu_calculate_dirtyrate (int64_t calc_time_ms, +int64_t init_time_ms, +VcpuStat *stat, +unsigned int flag, +bool one_shot); + +void global_dirty_log_change (unsigned int flag, +bool start); +#endif
migration/dirtyrate.c +142 -80
288 lines not on this page · raw message
migration/dirtyrate.h +2 -5
@@ -13,6 +13,8 @@#ifndef QEMU_MIGRATION_DIRTYRATE_H #define QEMU_MIGRATION_DIRTYRATE_H +#include "sysemu/dirtyrate.h" +/* * Sample 512 pages per GB as default. */
@@ -65,11 +67,6 @@ typedef struct SampleVMStat {uint64_t total_block_mem_MB;/* size of total sampled pages in MB */ } SampleVMStat; -typedef struct VcpuStat { -int nvcpu;/* number of vcpu */ -DirtyRateVcpu *rates;/* array of dirty rate for each vcpu */ -}VcpuStat ; -/* * Store calculation statistics for each measure. */
Signature · 1 line
1.8.3.1
Re: [PATCH v12 2/5] migration/dirtyrate: refactor dirty page rate calculation
Quoted 21 lines · On Mon, Jan 24, 2022 at 10:10:37PM +0800, [email protected] wrote:
cpus-common.c +2
-0@@ -73,6 +73,7 @@ static int cpu_get_free_index(void) }CPUTailQ cpus =QTAILQ_HEAD_INITIALIZER (cpus); +unsigned int cpu_list_generation_id;void cpu_list_add (CPUState *cpu) {@@ -84,6 +85,7 @@ void cpu_list_add(CPUState *cpu)assert (!cpu_index_auto_assigned); }QTAILQ_INSERT_TAIL_RCU (&cpus, cpu, node); + cpu_list_generation_id++; }void cpu_list_remove (CPUState *cpu)
cpus-common.c void cpu_list_remove(CPUState *cpu) · line 102
Quoted 5 lines
cpus-common.c +1
-0@@ -96,6 +98,7 @@ void cpu_list_remove(CPUState *cpu)QTAILQ_REMOVE_RCU (&cpus, cpu, node); cpu->cpu_index =UNASSIGNED_CPU_INDEX ; + cpu_list_generation_id++; }
Could you move the cpu list gen id changes into a separate patch?
Quoted 14 lines
cpus-common.c +0
-0CPUState *qemu_get_cpu (int index)include/sysemu/dirtyrate.h new file +6
-0@@ -0,0 +1,31 @@ +/* +* dirty page rate helper functions +* +* Copyright (c) 2022 CHINA TELECOM CO.,LTD. +* +* Authors:
include/sysemu/dirtyrate.h line 16
Quoted 10 lines
include/sysemu/dirtyrate.h new file +10
-0+* 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_DIRTYRATE_H +#define QEMU_DIRTYRATE_H + +extern unsigned int cpu_list_generation_id;
How about exporting a function cpu_list_generation_id_get() from the cpu code, rather than referencing it directly?
Quoted 10 lines
include/sysemu/dirtyrate.h new file +10
-0+int64_t vcpu_calculate_dirtyrate (int64_t calc_time_ms, +int64_t init_time_ms, +VcpuStat *stat, +unsigned int flag, +bool one_shot) +{ +DirtyPageRecord *records; +int64_t duration; +int64_t dirtyrate; +int i =0 ;
include/sysemu/dirtyrate.h
Quoted 10 lines
include/sysemu/dirtyrate.h new file +10
-0+unsigned int gen_id; + +retry: +cpu_list_lock (); + gen_id = cpu_list_generation_id; + records =vcpu_dirty_stat_alloc (stat); +vcpu_dirty_stat_collect (stat, records, true); + + duration =dirty_stat_wait (calc_time_ms, init_time_ms); +cpu_list_unlock ();
Should release the lock before sleep (dirty_stat_wait)?
Quoted 16 lines
include/sysemu/dirtyrate.h new file +16
-0+ +global_dirty_log_sync (flag, one_shot); + +cpu_list_lock (); +if (gen_id != cpu_list_generation_id) { +g_free (records); +g_free (stat->rates); +cpu_list_unlock (); + goto retry; + } +vcpu_dirty_stat_collect (stat, records, false); +cpu_list_unlock (); + +for (i =0 ; i < stat->nvcpu; i++) { + dirtyrate =do_calculate_dirtyrate (records[i], duration); +
include/sysemu/dirtyrate.h
Quoted 10 lines
include/sysemu/dirtyrate.h new file +10
-0+ stat->rates[i].id = i; + stat->rates[i].dirty_rate = dirtyrate; + +trace_dirtyrate_do_calculate_vcpu (i, dirtyrate); + } + +g_free (records); + +return duration; +}
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v12 2/5] migration/dirtyrate: refactor dirty page rate calculation
Quoted 23 lines · Peter Xu
Quoted 22 lines · On Mon, Jan 24, 2022 at 10:10:37PM +0800, [email protected] wrote:
cpus-common.c +2
-0@@ -73,6 +73,7 @@ static int cpu_get_free_index(void) }CPUTailQ cpus =QTAILQ_HEAD_INITIALIZER (cpus); +unsigned int cpu_list_generation_id;void cpu_list_add (CPUState *cpu) {@@ -84,6 +85,7 @@ void cpu_list_add(CPUState *cpu)assert (!cpu_index_auto_assigned); }QTAILQ_INSERT_TAIL_RCU (&cpus, cpu, node); + cpu_list_generation_id++; }void cpu_list_remove (CPUState *cpu)@@ -96,6 +98,7 @@ void cpu_list_remove(CPUState *cpu)
Quoted 6 lines
cpus-common.c void cpu_list_remove(CPUState *cpu) · line 102
Quoted 4 lines
cpus-common.c +1
-0QTAILQ_REMOVE_RCU (&cpus, cpu, node); cpu->cpu_index =UNASSIGNED_CPU_INDEX ; + cpu_list_generation_id++; }Could you move the cpu list gen id changes into a separate patch?
Yes, of course
Quoted 20 lines
Quoted 20 lines
cpus-common.c +0
-0CPUState *qemu_get_cpu (int index)include/sysemu/dirtyrate.h new file +11
-0@@ -0,0 +1,31 @@ +/* +* dirty page rate helper functions +* +* Copyright (c) 2022 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. +*/
Quoted 8 lines
include/sysemu/dirtyrate.h line 16
Quoted 5 lines
include/sysemu/dirtyrate.h new file +5
-0+ +#ifndef QEMU_DIRTYRATE_H +#define QEMU_DIRTYRATE_H + +extern unsigned int cpu_list_generation_id;How about exporting a function
cpu_list_generation_id_get()from the cpu code, rather than referencing it directly?
Ok, this will be done along with "cpu list gen id changes" in a separate patch
Quoted 15 lines
Quoted 15 lines
include/sysemu/dirtyrate.h new file +14
-0+int64_t vcpu_calculate_dirtyrate (int64_t calc_time_ms, +int64_t init_time_ms, +VcpuStat *stat, +unsigned int flag, +bool one_shot) +{ +DirtyPageRecord *records; +int64_t duration; +int64_t dirtyrate; +int i =0 ; +unsigned int gen_id; + +retry: +cpu_list_lock ();
Quoted 8 lines
include/sysemu/dirtyrate.h
Quoted 6 lines
include/sysemu/dirtyrate.h new file +6
-0+ gen_id = cpu_list_generation_id; + records =vcpu_dirty_stat_alloc (stat); +vcpu_dirty_stat_collect (stat, records, true); + + duration =dirty_stat_wait (calc_time_ms, init_time_ms); +cpu_list_unlock ();Should release the lock before sleep (
dirty_stat_wait)?
Good point, since we have introduced the cpu_list_generation_id and make sure the we can handle the plug/unplug scenario, we can make the cpu plug/unplug as fast as it can. :)
Quoted 30 lines
Quoted 17 lines
include/sysemu/dirtyrate.h new file +16
-0+ +global_dirty_log_sync (flag, one_shot); + +cpu_list_lock (); +if (gen_id != cpu_list_generation_id) { +g_free (records); +g_free (stat->rates); +cpu_list_unlock (); + goto retry; + } +vcpu_dirty_stat_collect (stat, records, false); +cpu_list_unlock (); + +for (i =0 ; i < stat->nvcpu; i++) { + dirtyrate =do_calculate_dirtyrate (records[i], duration); +include/sysemu/dirtyrate.h
Quoted 10 lines
include/sysemu/dirtyrate.h new file +10
-0+ stat->rates[i].id = i; + stat->rates[i].dirty_rate = dirtyrate; + +trace_dirtyrate_do_calculate_vcpu (i, dirtyrate); + } + +g_free (records); + +return duration; +}Thanks,
Signature · 3 lines
Best regard Hyman Huang(黄勇)
PATCH v12 3/5 softmmu/dirtylimit: 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 for dirty page rate limit.
Add dirtylimit.c to implement dirtyrate calculation periodly, which will be used for dirty page rate limit.
Add dirtylimit.h to export util functions for dirty page rate limit implementation.
--- include/exec/memory.h | 5 +- include/sysemu/dirtylimit.h | 22 ++++++++ softmmu/dirtylimit.c | 120 ++++++++++++++++++++++++++++++++++++++++++++ softmmu/meson.build | 1 + 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 include/sysemu/dirtylimit.h create mode 100644 softmmu/dirtylimit.c
include/exec/memory.h +4 -1
@@ -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 +22 -0
@@ -0,0 +1,22 @@ +/* +* Dirty page rate limit common functions +* +* Copyright (c) 2022 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_DIRTYRLIMIT_H +#define QEMU_DIRTYRLIMIT_H + +#define DIRTYLIMIT_CALC_TIME_MS 1000/* 1000ms */ + +int64_t vcpu_dirty_rate_get (int cpu_index); +void vcpu_dirty_rate_stat_start (void ); +void vcpu_dirty_rate_stat_stop (void ); +void vcpu_dirty_rate_stat_initialize (void ); +void vcpu_dirty_rate_stat_finalize (void ); +#endif
softmmu/dirtylimit.c new file +120 -0
120 lines not on this page · raw message
softmmu/meson.build +1 -0
@@ -15,6 +15,7 @@ specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files( 'vl.c', 'cpu-timers.c', 'runstate-action.c', + 'dirtylimit.c', )]) specific_ss.add(when: ['CONFIG_SOFTMMU', 'CONFIG_TCG'], if_true: [files(
Signature · 1 line
1.8.3.1
PATCH v12 4/5 softmmu/dirtylimit: implement virtual CPU throttle
In reply to [email protected]From: Hyman Huang(黄勇) <[email protected]>
Setup a negative feedback system when vCPU thread handling KVM_EXIT_DIRTY_RING_FULL exit by introducing throttle_us_per_full field in struct CPUState. Sleep throttle_us_per_full microseconds to throttle vCPU if dirtylimit is enabled.
Start a thread to track current dirty page rates and tune the throttle_us_per_full dynamically untill current dirty page rate reach the quota.
Introduce the util function in the header for dirtylimit implementation.
--- accel/kvm/kvm-all.c | 13 ++ accel/stubs/kvm-stub.c | 5 + include/hw/core/cpu.h | 6 + include/sysemu/dirtylimit.h | 16 +++ include/sysemu/kvm.h | 2 + softmmu/dirtylimit.c | 308 ++++++++++++++++++++++++++++++++++++++++++++ softmmu/trace-events | 8 ++ 7 files changed, 358 insertions(+)
accel/kvm/kvm-all.c +13 -0
@@ -45,6 +45,7 @@#include "qemu/guest-random.h" #include "sysemu/hw_accel.h" #include "kvm-cpus.h" +#include "sysemu/dirtylimit.h" #include "hw/boards.h"
@@ -476,6 +477,7 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) cpu->kvm_state = s; cpu->vcpu_dirty = true; cpu->dirty_pages =0 ; + cpu->throttle_us_per_full =0 ; mmap_size =kvm_ioctl (s,KVM_GET_VCPU_MMAP_SIZE ,0 );if (mmap_size <0 ) {
@@ -1469,6 +1471,11 @@ static void *kvm_dirty_ring_reaper_thread(void *data)*/ sleep (1 ); +/* keep sleeping in order to not interfere the dirtylimit */ +if (dirtylimit_in_service ()) { +continue ; + } +trace_kvm_dirty_ring_reaper ("wakeup" ); r->reaper_state =KVM_DIRTY_RING_REAPER_REAPING ;
@@ -2312,6 +2319,11 @@ bool kvm_dirty_ring_enabled(void)return kvm_state->kvm_dirty_ring_size ? true : false; } +uint32_t kvm_dirty_ring_size (void ) +{ +return kvm_state->kvm_dirty_ring_size; +} +static int kvm_init (MachineState *ms) {MachineClass *mc =MACHINE_GET_CLASS (ms);
@@ -2961,6 +2973,7 @@ int kvm_cpu_exec(CPUState *cpu)qemu_mutex_lock_iothread ();kvm_dirty_ring_reap (kvm_state, cpu);qemu_mutex_unlock_iothread (); +dirtylimit_vcpu_execute (cpu); ret =0 ;break ;case KVM_EXIT_SYSTEM_EVENT :
accel/stubs/kvm-stub.c +5 -0
@@ -152,4 +152,9 @@ bool kvm_dirty_ring_enabled(void) {return false; } + +uint32_t kvm_dirty_ring_size (void ) +{ +return 0 ; +}#endif
include/hw/core/cpu.h +6 -0
@@ -411,6 +411,12 @@ struct CPUState {*/ bool throttle_thread_scheduled; +/* +* Sleep throttle_us_per_full microseconds once dirty ring is full +* if dirty page rate limit is enabled. +*/ +int64_t throttle_us_per_full; +bool ignore_memory_transaction_failures;/* Used for user-only emulation of prctl(PR_SET_UNALIGN). */
include/sysemu/dirtylimit.h +16 -0
@@ -19,4 +19,20 @@ void vcpu_dirty_rate_stat_start(void);void vcpu_dirty_rate_stat_stop (void );void vcpu_dirty_rate_stat_initialize (void );void vcpu_dirty_rate_stat_finalize (void ); + +void dirtylimit_state_lock (void ); +void dirtylimit_state_unlock (void ); +void dirtylimit_state_initialize (void ); +void dirtylimit_state_finalize (void ); +void dirtylimit_thread_finalize (void ); +bool dirtylimit_in_service (void ); +bool dirtylimit_vcpu_index_valid (int cpu_index); +void dirtylimit_start (void ); +void dirtylimit_stop (void ); +void dirtylimit_set_vcpu (int cpu_index, +uint64_t quota, +bool enable); +void dirtylimit_set_all (uint64_t quota, +bool enable); +void dirtylimit_vcpu_execute (CPUState *cpu);#endif
include/sysemu/kvm.h +2 -0
@@ -563,4 +563,6 @@ bool kvm_cpu_check_are_resettable(void);bool kvm_arch_cpu_check_are_resettable (void );bool kvm_dirty_ring_enabled (void ); + +uint32_t kvm_dirty_ring_size (void );#endif
softmmu/dirtylimit.c +308 -0
329 lines not on this page · raw message
softmmu/trace-events +8 -0
@@ -31,3 +31,11 @@ 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) "" + +#dirtylimit.c +dirtylimit_state_initialize(int max_cpus) "dirtylimit state initialize: max cpus %d" +dirtylimit_state_finalize(void) +dirtylimit_adjust_throttle(int cpu_index, uint64_t quota, uint64_t current, int64_t time_us) "CPU[%d] throttle: quota %" PRIu64 ", current %" PRIu64 ", throttle %"PRIi64 " us" +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"
Signature · 1 line
1.8.3.1
Re: [PATCH v12 4/5] softmmu/dirtylimit: implement virtual CPU throttle
Quoted 59 lines · On Mon, Jan 24, 2022 at 10:10:39PM +0800, [email protected] wrote:
From: Hyman Huang(黄勇) <[email protected]>
Setup a negative feedback system when vCPU thread handling
KVM_EXIT_DIRTY_RING_FULLexit by introducingthrottle_us_per_fullfield in struct CPUState. Sleepthrottle_us_per_fullmicroseconds to throttle vCPU if dirtylimit is enabled.Start a thread to track current dirty page rates and tune the
throttle_us_per_fulldynamically untill current dirty page rate reach the quota.Introduce the util function in the header for dirtylimit implementation.
Signed-off-by Hyman Huang(黄勇) <[email protected]>--- accel/kvm/kvm-all.c | 13 ++ accel/stubs/kvm-stub.c | 5 + include/hw/core/cpu.h | 6 + include/sysemu/dirtylimit.h | 16 +++ include/sysemu/kvm.h | 2 + softmmu/dirtylimit.c | 308 ++++++++++++++++++++++++++++++++++++++++++++ softmmu/trace-events | 8 ++ 7 files changed, 358 insertions(+)accel/kvm/kvm-all.c +7
-0@@ -45,6 +45,7 @@#include "qemu/guest-random.h" #include "sysemu/hw_accel.h" #include "kvm-cpus.h" +#include "sysemu/dirtylimit.h" #include "hw/boards.h" @@ -476,6 +477,7 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) cpu->kvm_state = s; cpu->vcpu_dirty = true; cpu->dirty_pages =0 ; + cpu->throttle_us_per_full =0 ; mmap_size =kvm_ioctl (s,KVM_GET_VCPU_MMAP_SIZE ,0 );if (mmap_size <0 ) {@@ -1469,6 +1471,11 @@ static void *kvm_dirty_ring_reaper_thread(void *data)*/ sleep (1 ); +/* keep sleeping in order to not interfere the dirtylimit */ +if (dirtylimit_in_service ()) { +continue ; + } +trace_kvm_dirty_ring_reaper ("wakeup" ); r->reaper_state =KVM_DIRTY_RING_REAPER_REAPING ;
accel/kvm/kvm-all.c bool kvm_dirty_ring_enabled(void) · line 2325
Quoted 7 lines
accel/kvm/kvm-all.c +4
-0@@ -2312,6 +2319,11 @@ bool kvm_dirty_ring_enabled(void)return kvm_state->kvm_dirty_ring_size ? true : false; } +uint32_t kvm_dirty_ring_size (void ) +{ +return kvm_state->kvm_dirty_ring_size; +}
Please consider moving this into a small patch too along with the stub.
Quoted 91 lines
accel/kvm/kvm-all.c +2
-0+static int kvm_init (MachineState *ms) {MachineClass *mc =MACHINE_GET_CLASS (ms);@@ -2961,6 +2973,7 @@ int kvm_cpu_exec(CPUState *cpu)qemu_mutex_lock_iothread ();kvm_dirty_ring_reap (kvm_state, cpu);qemu_mutex_unlock_iothread (); +dirtylimit_vcpu_execute (cpu); ret =0 ;break ;case KVM_EXIT_SYSTEM_EVENT :accel/stubs/kvm-stub.c +5
-0@@ -152,4 +152,9 @@ bool kvm_dirty_ring_enabled(void) {return false; } + +uint32_t kvm_dirty_ring_size (void ) +{ +return 0 ; +}#endif include/hw/core/cpu.h +6
-0@@ -411,6 +411,12 @@ struct CPUState {*/ bool throttle_thread_scheduled; +/* +* Sleep throttle_us_per_full microseconds once dirty ring is full +* if dirty page rate limit is enabled. +*/ +int64_t throttle_us_per_full; +bool ignore_memory_transaction_failures;/* Used for user-only emulation of prctl(PR_SET_UNALIGN). */ include/sysemu/dirtylimit.h +16
-0@@ -19,4 +19,20 @@ void vcpu_dirty_rate_stat_start(void);void vcpu_dirty_rate_stat_stop (void );void vcpu_dirty_rate_stat_initialize (void );void vcpu_dirty_rate_stat_finalize (void ); + +void dirtylimit_state_lock (void ); +void dirtylimit_state_unlock (void ); +void dirtylimit_state_initialize (void ); +void dirtylimit_state_finalize (void ); +void dirtylimit_thread_finalize (void ); +bool dirtylimit_in_service (void ); +bool dirtylimit_vcpu_index_valid (int cpu_index); +void dirtylimit_start (void ); +void dirtylimit_stop (void ); +void dirtylimit_set_vcpu (int cpu_index, +uint64_t quota, +bool enable); +void dirtylimit_set_all (uint64_t quota, +bool enable); +void dirtylimit_vcpu_execute (CPUState *cpu);#endif include/sysemu/kvm.h +2
-0@@ -563,4 +563,6 @@ bool kvm_cpu_check_are_resettable(void);bool kvm_arch_cpu_check_are_resettable (void );bool kvm_dirty_ring_enabled (void ); + +uint32_t kvm_dirty_ring_size (void );#endif softmmu/dirtylimit.c +4
-0@@ -18,6 +18,32 @@#include "sysemu/dirtylimit.h" #include "exec/memory.h" #include "hw/boards.h" +#include "sysemu/kvm.h" +#include "trace.h" + +/*
softmmu/dirtylimit.c line 34
Quoted 10 lines
softmmu/dirtylimit.c +10
-0+* Dirtylimit stop working if dirty page rate error +* value less than DIRTYLIMIT_TOLERANCE_RANGE +*/ +#define DIRTYLIMIT_TOLERANCE_RANGE 25/* MB/s */ +/* +* Dirty page rate error greater than +* DIRTYLIMIT_LINEAR_ADJUSTMENT_WATERMARK should use +* linear adjustment police. +*/ +#define DIRTYLIMIT_LINEAR_ADJUSTMENT_WATERMARK 400/* MB/s */
Is this magic value really useful?
Asked since it smells like a workaround to avoid oscillation, at the meantime, AFAICT it's never used, see [1] below.
Again, I really hope we use as less magic values as possible. Piling up magic code (without solid proof that it'll help) is scary..
Quoted 48 lines
softmmu/dirtylimit.c +37
-0+/* +* Plus or minus vcpu sleep time linearly if dirty +* page rate error value percentage over +* DIRTYLIMIT_LINEAR_ADJUSTMENT_PCT. +* Otherwise, plus or minus a fixed vcpu sleep time. +*/ +#define DIRTYLIMIT_LINEAR_ADJUSTMENT_PCT 50 +/* +* Max vcpu sleep time percentage during a cycle +* composed of dirty ring full and sleep time. +*/ +#define DIRTYLIMIT_THROTTLE_PCT_MAX 99struct {VcpuStat stat;@@ -25,6 +51,31 @@ struct {QemuThread thread; } *vcpu_dirty_rate_stat; +typedef struct VcpuDirtyLimitState { +int cpu_index; +bool enabled; +/* +* Quota dirty page rate, unit is MB/s +* zero if not enabled. +*/ +uint64_t quota; +}VcpuDirtyLimitState ; + +struct { +VcpuDirtyLimitState *states; +/* Max cpus number configured by user */ +int max_cpus; +/* Number of vcpu under dirtylimit */ +int limited_nvcpu; +QEMUBH *bh; +} *dirtylimit_state; + +/* protect dirtylimit_state */ +static QemuMutex dirtylimit_mutex; + +/* dirtylimit thread quit if dirtylimit_quit is true */ +static bool dirtylimit_quit; +static void vcpu_dirty_rate_stat_collect (void ) {int64_t start_time;
softmmu/dirtylimit.c static void *vcpu_dirty_rate_stat_thread(void *opaque) · line 112
Quoted 4 lines
softmmu/dirtylimit.c +1
-0@@ -58,6 +109,7 @@ static void *vcpu_dirty_rate_stat_thread(void *opaque)while (qatomic_read (&vcpu_dirty_rate_stat->running)) {vcpu_dirty_rate_stat_collect (); +qemu_bh_schedule (dirtylimit_state->bh);
When I mentioned "a hook" previously, I meant something like a function pointer. Even simpler, IMHO you could call dirtylimit code directly, assuming this thread only services dirty limit.
Or is this BH required for some reason?
Quoted 119 lines
softmmu/dirtylimit.c +112
-0}/* stop log sync */ @@ -118,3 +170,259 @@ void vcpu_dirty_rate_stat_finalize(void)free (vcpu_dirty_rate_stat); vcpu_dirty_rate_stat =NULL ; } + +void dirtylimit_state_lock (void ) +{ +qemu_mutex_lock (&dirtylimit_mutex); +} + +void dirtylimit_state_unlock (void ) +{ +qemu_mutex_unlock (&dirtylimit_mutex); +} + +static void +__attribute__((__constructor__))dirtylimit_mutex_init (void ) +{ +qemu_mutex_init (&dirtylimit_mutex); +} + +static inline VcpuDirtyLimitState *dirtylimit_vcpu_get_state (int cpu_index) +{ +return &dirtylimit_state->states[cpu_index]; +} + +void dirtylimit_state_initialize (void ) +{ +MachineState *ms =MACHINE (qdev_get_machine ()); +int max_cpus = ms->smp.max_cpus; +int i; + + dirtylimit_state =g_malloc0 (sizeof (*dirtylimit_state)); + + dirtylimit_state->states = +g_malloc0 (sizeof (VcpuDirtyLimitState) * max_cpus); + +for (i =0 ; i < max_cpus; i++) { + dirtylimit_state->states[i].cpu_index = i; + } + + dirtylimit_state->max_cpus = max_cpus; +trace_dirtylimit_state_initialize (max_cpus); +} + +void dirtylimit_state_finalize (void ) +{ +free (dirtylimit_state->states); + dirtylimit_state->states =NULL ; + +free (dirtylimit_state); + dirtylimit_state =NULL ; + +trace_dirtylimit_state_finalize (); +} + +bool dirtylimit_in_service (void ) +{ +return !!dirtylimit_state; +} + +bool dirtylimit_vcpu_index_valid (int cpu_index) +{ +MachineState *ms =MACHINE (qdev_get_machine ()); + +return !(cpu_index <0 || + cpu_index >= ms->smp.max_cpus); +} + +static inline void dirtylimit_vcpu_set_quota (int cpu_index, +uint64_t quota, +bool on) +{ +if (on) { + dirtylimit_state->states[cpu_index].quota = quota; +if (!dirtylimit_vcpu_get_state (cpu_index)->enabled) { + dirtylimit_state->limited_nvcpu++; + } + }else { + dirtylimit_state->states[cpu_index].quota =0 ; +if (dirtylimit_state->states[cpu_index].enabled) { + dirtylimit_state->limited_nvcpu--; + } + } + + dirtylimit_state->states[cpu_index].enabled = on; +} + +static inline int64_t dirtylimit_dirty_ring_full_time (uint64_t dirtyrate) +{ +static uint64_t max_dirtyrate; +uint32_t dirty_ring_size =kvm_dirty_ring_size (); +uint64_t dirty_ring_size_meory_MB = + dirty_ring_size *TARGET_PAGE_SIZE >>20 ; + +if (max_dirtyrate < dirtyrate) { + max_dirtyrate = dirtyrate; + } + +return dirty_ring_size_meory_MB *1000000 / max_dirtyrate; +} + +static inline bool dirtylimit_done (uint64_t quota, +uint64_t current) +{ +uint64_t min, max; + + min =MIN (quota, current); + max =MAX (quota, current); + +return ((max - min) <=DIRTYLIMIT_TOLERANCE_RANGE ) ? true : false; +} + +static inline bool +dirtylimit_need_linear_adjustment (uint64_t quota, +uint64_t current)
softmmu/dirtylimit.c void vcpu_dirty_rate_stat_finalize(void) · line 294
Quoted 10 lines
softmmu/dirtylimit.c +10
-0+{ +uint64_t min, max, pct, error; + + min =MIN (quota, current); + max =MAX (quota, current); + + error = (max - min); + +if (error <DIRTYLIMIT_LINEAR_ADJUSTMENT_WATERMARK ) { + false;
[1]
I think you wanted to use "return false"... Maybe this also means the macro never helped at all hence it can be dropped.
softmmu/dirtylimit.c void vcpu_dirty_rate_stat_finalize(void) · line 300
Quoted 6 lines
softmmu/dirtylimit.c +6
-0+ } + + pct = error *100 / max; + +return pct >DIRTYLIMIT_LINEAR_ADJUSTMENT_PCT ; +}
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v12 4/5] softmmu/dirtylimit: implement virtual CPU throttle
Quoted 63 lines · Peter Xu
Quoted 62 lines · On Mon, Jan 24, 2022 at 10:10:39PM +0800, [email protected] wrote:
From: Hyman Huang(黄勇) <[email protected]>
Setup a negative feedback system when vCPU thread handling
KVM_EXIT_DIRTY_RING_FULLexit by introducingthrottle_us_per_fullfield in struct CPUState. Sleepthrottle_us_per_fullmicroseconds to throttle vCPU if dirtylimit is enabled.Start a thread to track current dirty page rates and tune the
throttle_us_per_fulldynamically untill current dirty page rate reach the quota.Introduce the util function in the header for dirtylimit implementation.
Signed-off-by Hyman Huang(黄勇) <[email protected]>--- accel/kvm/kvm-all.c | 13 ++ accel/stubs/kvm-stub.c | 5 + include/hw/core/cpu.h | 6 + include/sysemu/dirtylimit.h | 16 +++ include/sysemu/kvm.h | 2 + softmmu/dirtylimit.c | 308 ++++++++++++++++++++++++++++++++++++++++++++ softmmu/trace-events | 8 ++ 7 files changed, 358 insertions(+)accel/kvm/kvm-all.c +7
-0@@ -45,6 +45,7 @@#include "qemu/guest-random.h" #include "sysemu/hw_accel.h" #include "kvm-cpus.h" +#include "sysemu/dirtylimit.h" #include "hw/boards.h" @@ -476,6 +477,7 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) cpu->kvm_state = s; cpu->vcpu_dirty = true; cpu->dirty_pages =0 ; + cpu->throttle_us_per_full =0 ; mmap_size =kvm_ioctl (s,KVM_GET_VCPU_MMAP_SIZE ,0 );if (mmap_size <0 ) {@@ -1469,6 +1471,11 @@ static void *kvm_dirty_ring_reaper_thread(void *data)*/ sleep (1 ); +/* keep sleeping in order to not interfere the dirtylimit */ +if (dirtylimit_in_service ()) { +continue ; + } +trace_kvm_dirty_ring_reaper ("wakeup" ); r->reaper_state =KVM_DIRTY_RING_REAPER_REAPING ;@@ -2312,6 +2319,11 @@ bool kvm_dirty_ring_enabled(void)return kvm_state->kvm_dirty_ring_size ? true : false; }
Quoted 6 lines
accel/kvm/kvm-all.c bool kvm_dirty_ring_enabled(void) · line 2325
Quoted 4 lines
accel/kvm/kvm-all.c +4
-0+uint32_t kvm_dirty_ring_size (void ) +{ +return kvm_state->kvm_dirty_ring_size; +}Please consider moving this into a small patch too along with the stub.
Ok
Quoted 105 lines
Quoted 92 lines
accel/kvm/kvm-all.c +2
-0+static int kvm_init (MachineState *ms) {MachineClass *mc =MACHINE_GET_CLASS (ms);@@ -2961,6 +2973,7 @@ int kvm_cpu_exec(CPUState *cpu)qemu_mutex_lock_iothread ();kvm_dirty_ring_reap (kvm_state, cpu);qemu_mutex_unlock_iothread (); +dirtylimit_vcpu_execute (cpu); ret =0 ;break ;case KVM_EXIT_SYSTEM_EVENT :accel/stubs/kvm-stub.c +5
-0@@ -152,4 +152,9 @@ bool kvm_dirty_ring_enabled(void) {return false; } + +uint32_t kvm_dirty_ring_size (void ) +{ +return 0 ; +}#endif include/hw/core/cpu.h +6
-0@@ -411,6 +411,12 @@ struct CPUState {*/ bool throttle_thread_scheduled; +/* +* Sleep throttle_us_per_full microseconds once dirty ring is full +* if dirty page rate limit is enabled. +*/ +int64_t throttle_us_per_full; +bool ignore_memory_transaction_failures;/* Used for user-only emulation of prctl(PR_SET_UNALIGN). */ include/sysemu/dirtylimit.h +16
-0@@ -19,4 +19,20 @@ void vcpu_dirty_rate_stat_start(void);void vcpu_dirty_rate_stat_stop (void );void vcpu_dirty_rate_stat_initialize (void );void vcpu_dirty_rate_stat_finalize (void ); + +void dirtylimit_state_lock (void ); +void dirtylimit_state_unlock (void ); +void dirtylimit_state_initialize (void ); +void dirtylimit_state_finalize (void ); +void dirtylimit_thread_finalize (void ); +bool dirtylimit_in_service (void ); +bool dirtylimit_vcpu_index_valid (int cpu_index); +void dirtylimit_start (void ); +void dirtylimit_stop (void ); +void dirtylimit_set_vcpu (int cpu_index, +uint64_t quota, +bool enable); +void dirtylimit_set_all (uint64_t quota, +bool enable); +void dirtylimit_vcpu_execute (CPUState *cpu);#endif include/sysemu/kvm.h +2
-0@@ -563,4 +563,6 @@ bool kvm_cpu_check_are_resettable(void);bool kvm_arch_cpu_check_are_resettable (void );bool kvm_dirty_ring_enabled (void ); + +uint32_t kvm_dirty_ring_size (void );#endif softmmu/dirtylimit.c +4
-0@@ -18,6 +18,32 @@#include "sysemu/dirtylimit.h" #include "exec/memory.h" #include "hw/boards.h" +#include "sysemu/kvm.h" +#include "trace.h" + +/* softmmu/dirtylimit.c line 34
Quoted 10 lines
softmmu/dirtylimit.c +10
-0+* Dirtylimit stop working if dirty page rate error +* value less than DIRTYLIMIT_TOLERANCE_RANGE +*/ +#define DIRTYLIMIT_TOLERANCE_RANGE 25/* MB/s */ +/* +* Dirty page rate error greater than +* DIRTYLIMIT_LINEAR_ADJUSTMENT_WATERMARK should use +* linear adjustment police. +*/ +#define DIRTYLIMIT_LINEAR_ADJUSTMENT_WATERMARK 400/* MB/s */ Is this magic value really useful?
Quoted 3 lines
Asked since it smells like a workaround to avoid oscillation, at the meantime, AFAICT it's never used, see [1] below.
Ok, my fault, i'll remove this if it doesn't help avoiding oscillation
Quoted 194 lines
Again, I really hope we use as less magic values as possible. Piling up magic code (without solid proof that it'll help) is scary..
Quoted 48 lines
softmmu/dirtylimit.c +37
-0+/* +* Plus or minus vcpu sleep time linearly if dirty +* page rate error value percentage over +* DIRTYLIMIT_LINEAR_ADJUSTMENT_PCT. +* Otherwise, plus or minus a fixed vcpu sleep time. +*/ +#define DIRTYLIMIT_LINEAR_ADJUSTMENT_PCT 50 +/* +* Max vcpu sleep time percentage during a cycle +* composed of dirty ring full and sleep time. +*/ +#define DIRTYLIMIT_THROTTLE_PCT_MAX 99struct {VcpuStat stat;@@ -25,6 +51,31 @@ struct {QemuThread thread; } *vcpu_dirty_rate_stat; +typedef struct VcpuDirtyLimitState { +int cpu_index; +bool enabled; +/* +* Quota dirty page rate, unit is MB/s +* zero if not enabled. +*/ +uint64_t quota; +}VcpuDirtyLimitState ; + +struct { +VcpuDirtyLimitState *states; +/* Max cpus number configured by user */ +int max_cpus; +/* Number of vcpu under dirtylimit */ +int limited_nvcpu; +QEMUBH *bh; +} *dirtylimit_state; + +/* protect dirtylimit_state */ +static QemuMutex dirtylimit_mutex; + +/* dirtylimit thread quit if dirtylimit_quit is true */ +static bool dirtylimit_quit; +static void vcpu_dirty_rate_stat_collect (void ) {int64_t start_time;softmmu/dirtylimit.c static void *vcpu_dirty_rate_stat_thread(void *opaque) · line 112
Quoted 4 lines
softmmu/dirtylimit.c +1
-0@@ -58,6 +109,7 @@ static void *vcpu_dirty_rate_stat_thread(void *opaque)while (qatomic_read (&vcpu_dirty_rate_stat->running)) {vcpu_dirty_rate_stat_collect (); +qemu_bh_schedule (dirtylimit_state->bh);When I mentioned "a hook" previously, I meant something like a function pointer. Even simpler, IMHO you could call dirtylimit code directly, assuming this thread only services dirty limit.
Or is this BH required for some reason?
Quoted 119 lines
softmmu/dirtylimit.c +112
-0}/* stop log sync */ @@ -118,3 +170,259 @@ void vcpu_dirty_rate_stat_finalize(void)free (vcpu_dirty_rate_stat); vcpu_dirty_rate_stat =NULL ; } + +void dirtylimit_state_lock (void ) +{ +qemu_mutex_lock (&dirtylimit_mutex); +} + +void dirtylimit_state_unlock (void ) +{ +qemu_mutex_unlock (&dirtylimit_mutex); +} + +static void +__attribute__((__constructor__))dirtylimit_mutex_init (void ) +{ +qemu_mutex_init (&dirtylimit_mutex); +} + +static inline VcpuDirtyLimitState *dirtylimit_vcpu_get_state (int cpu_index) +{ +return &dirtylimit_state->states[cpu_index]; +} + +void dirtylimit_state_initialize (void ) +{ +MachineState *ms =MACHINE (qdev_get_machine ()); +int max_cpus = ms->smp.max_cpus; +int i; + + dirtylimit_state =g_malloc0 (sizeof (*dirtylimit_state)); + + dirtylimit_state->states = +g_malloc0 (sizeof (VcpuDirtyLimitState) * max_cpus); + +for (i =0 ; i < max_cpus; i++) { + dirtylimit_state->states[i].cpu_index = i; + } + + dirtylimit_state->max_cpus = max_cpus; +trace_dirtylimit_state_initialize (max_cpus); +} + +void dirtylimit_state_finalize (void ) +{ +free (dirtylimit_state->states); + dirtylimit_state->states =NULL ; + +free (dirtylimit_state); + dirtylimit_state =NULL ; + +trace_dirtylimit_state_finalize (); +} + +bool dirtylimit_in_service (void ) +{ +return !!dirtylimit_state; +} + +bool dirtylimit_vcpu_index_valid (int cpu_index) +{ +MachineState *ms =MACHINE (qdev_get_machine ()); + +return !(cpu_index <0 || + cpu_index >= ms->smp.max_cpus); +} + +static inline void dirtylimit_vcpu_set_quota (int cpu_index, +uint64_t quota, +bool on) +{ +if (on) { + dirtylimit_state->states[cpu_index].quota = quota; +if (!dirtylimit_vcpu_get_state (cpu_index)->enabled) { + dirtylimit_state->limited_nvcpu++; + } + }else { + dirtylimit_state->states[cpu_index].quota =0 ; +if (dirtylimit_state->states[cpu_index].enabled) { + dirtylimit_state->limited_nvcpu--; + } + } + + dirtylimit_state->states[cpu_index].enabled = on; +} + +static inline int64_t dirtylimit_dirty_ring_full_time (uint64_t dirtyrate) +{ +static uint64_t max_dirtyrate; +uint32_t dirty_ring_size =kvm_dirty_ring_size (); +uint64_t dirty_ring_size_meory_MB = + dirty_ring_size *TARGET_PAGE_SIZE >>20 ; + +if (max_dirtyrate < dirtyrate) { + max_dirtyrate = dirtyrate; + } + +return dirty_ring_size_meory_MB *1000000 / max_dirtyrate; +} + +static inline bool dirtylimit_done (uint64_t quota, +uint64_t current) +{ +uint64_t min, max; + + min =MIN (quota, current); + max =MAX (quota, current); + +return ((max - min) <=DIRTYLIMIT_TOLERANCE_RANGE ) ? true : false; +} + +static inline bool +dirtylimit_need_linear_adjustment (uint64_t quota, +uint64_t current)softmmu/dirtylimit.c void vcpu_dirty_rate_stat_finalize(void) · line 294
Quoted 10 lines
softmmu/dirtylimit.c +10
-0+{ +uint64_t min, max, pct, error; + + min =MIN (quota, current); + max =MAX (quota, current); + + error = (max - min); + +if (error <DIRTYLIMIT_LINEAR_ADJUSTMENT_WATERMARK ) { + false;[1]
Quoted 2 lines
I think you wanted to use "return false"... Maybe this also means the macro never helped at all hence it can be dropped.
Yes, :( dropping this block can be a good choice.
Quoted 10 lines
softmmu/dirtylimit.c void vcpu_dirty_rate_stat_finalize(void) · line 300
Quoted 7 lines
softmmu/dirtylimit.c +6
-0+ } + + pct = error *100 / max; + +return pct >DIRTYLIMIT_LINEAR_ADJUSTMENT_PCT ; +}Thanks,
Signature · 3 lines
Best regard Hyman Huang(黄勇)
PATCH v12 5/5 softmmu/dirtylimit: implement dirty page rate limit
In reply to [email protected]From: Hyman Huang(黄勇) <[email protected]>
Implement dirtyrate calculation periodically basing on dirty-ring and throttle virtual CPU until it reachs the quota dirty page rate given by user.
Introduce qmp commands "set-vcpu-dirty-limit", "cancel-vcpu-dirty-limit", "query-vcpu-dirty-limit" to enable, disable, query dirty page limit for virtual CPU.
Meanwhile, introduce corresponding hmp commands "set_vcpu_dirty_limit", "cancel_vcpu_dirty_limit", "info vcpu_dirty_limit" so the feature can be more usable.
--- hmp-commands-info.hx | 13 ++++ hmp-commands.hx | 32 +++++++++ include/monitor/hmp.h | 3 + qapi/migration.json | 80 +++++++++++++++++++++ softmmu/dirtylimit.c | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 322 insertions(+)
hmp-commands-info.hx +13 -0
@@ -865,6 +865,19 @@ SRST Display the vcpu dirty rate information. ERST + { + .name = "vcpu_dirty_limit", + .args_type = "", + .params = "", + .help = "show dirty page limit information of all vCPU", + .cmd = hmp_info_vcpu_dirty_limit, + }, + +SRST + ``info vcpu_dirty_limit`` + Display the vcpu dirty page limit information. +ERST + #if defined(TARGET_I386) { .name = "sgx",
hmp-commands.hx +32 -0
@@ -1744,3 +1744,35 @@ ERST "\n\t\t\t -b to specify dirty bitmap as method of calculation)", .cmd = hmp_calc_dirty_rate, }, + +SRST +``set_vcpu_dirty_limit`` + Set dirty page rate limit on virtual CPU, the information about all the + virtual CPU dirty limit status can be observed with ``info vcpu_dirty_limit`` + command. +ERST + + { + .name = "set_vcpu_dirty_limit", + .args_type = "dirty_rate:l,cpu_index:l?", + .params = "dirty_rate [cpu_index]", + .help = "set dirty page rate limit, use cpu_index to set limit" + "\n\t\t\t\t on a specified virtual cpu", + .cmd = hmp_set_vcpu_dirty_limit, + }, + +SRST +``cancel_vcpu_dirty_limit`` + Cancel dirty page rate limit on virtual CPU, the information about all the + virtual CPU dirty limit status can be observed with ``info vcpu_dirty_limit`` + command. +ERST + + { + .name = "cancel_vcpu_dirty_limit", + .args_type = "cpu_index:l?", + .params = "[cpu_index]", + .help = "cancel dirty page rate limit, use cpu_index to cancel" + "\n\t\t\t\t limit on a specified virtual cpu", + .cmd = hmp_cancel_vcpu_dirty_limit, + },
include/monitor/hmp.h +3 -0
@@ -131,6 +131,9 @@ void hmp_replay_delete_break(Monitor *mon, const QDict *qdict);void hmp_replay_seek (Monitor *mon,const QDict *qdict);void hmp_info_dirty_rate (Monitor *mon,const QDict *qdict);void hmp_calc_dirty_rate (Monitor *mon,const QDict *qdict); +void hmp_set_vcpu_dirty_limit (Monitor *mon,const QDict *qdict); +void hmp_cancel_vcpu_dirty_limit (Monitor *mon,const QDict *qdict); +void hmp_info_vcpu_dirty_limit (Monitor *mon,const QDict *qdict);void hmp_human_readable_text_helper (Monitor *mon,HumanReadableText *(*qmp_handler)(Error **));
qapi/migration.json +80 -0
86 lines not on this page · raw message
softmmu/dirtylimit.c +194 -0
205 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [PATCH v12 5/5] softmmu/dirtylimit: implement dirty page rate limit
Quoted 15 lines · [email protected] writes:
From: Hyman Huang(黄勇) <[email protected]>
Implement dirtyrate calculation periodically basing on dirty-ring and throttle virtual CPU until it reachs the quota dirty page rate given by user.
Introduce qmp commands "set-vcpu-dirty-limit", "cancel-vcpu-dirty-limit", "query-vcpu-dirty-limit" to enable, disable, query dirty page limit for virtual CPU.
Meanwhile, introduce corresponding hmp commands "
set_vcpu_dirty_limit", "cancel_vcpu_dirty_limit", "infovcpu_dirty_limit" so the feature can be more usable.Signed-off-by Hyman Huang(黄勇) <[email protected]>
[...]
Quoted 19 lines
include/monitor/hmp.h +3
-0@@ -131,6 +131,9 @@ void hmp_replay_delete_break(Monitor *mon, const QDict *qdict);void hmp_replay_seek (Monitor *mon,const QDict *qdict);void hmp_info_dirty_rate (Monitor *mon,const QDict *qdict);void hmp_calc_dirty_rate (Monitor *mon,const QDict *qdict); +void hmp_set_vcpu_dirty_limit (Monitor *mon,const QDict *qdict); +void hmp_cancel_vcpu_dirty_limit (Monitor *mon,const QDict *qdict); +void hmp_info_vcpu_dirty_limit (Monitor *mon,const QDict *qdict);void hmp_human_readable_text_helper (Monitor *mon,HumanReadableText *(*qmp_handler)(Error **));qapi/migration.json +0
-0
qapi/migration.json line 1855
Quoted 6 lines
qapi/migration.json +3
-0@@ -1850,6 +1850,86 @@ { 'command': 'query-dirty-rate', 'returns': 'DirtyRateInfo' } ## +# @DirtyLimitInfo: +# +# Dirty page rate limit information of virtual CPU.
"of a virtual CPU"
qapi/migration.json line 1860
Quoted 5 lines
qapi/migration.json +5
-0+# +# @cpu-index: index of virtual CPU. +# +# @limit-rate: upper limit of dirty page rate in units of MB/s for virtual +# CPU, 0 means unlimited.
Could abridge "in units of MB/s" to "(MB/s)". Same below.
Quoted 13 lines
qapi/migration.json +13
-0+# +# @current-rate: current dirty page rate in units of MB/s for virtual CPU. +# +# Since: 7.0 +# +## +{ 'struct': 'DirtyLimitInfo', + 'data': { 'cpu-index': 'int', + 'limit-rate': 'uint64', + 'current-rate': 'uint64' } } + +## +# @set-vcpu-dirty-limit:
qapi/migration.json line 1883
Quoted 10 lines
qapi/migration.json +10
-0+# +# Set the upper limit of dirty page rate for virtual CPUs. +# +# 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. +# +# @cpu-index: index of virtual CPU, default is all. +# +# @dirty-rate: upper limit of dirty page rate in units of MB/s for virtual CPU.
Long line, please wrap.
qapi/migration.json line 1894
Quoted 11 lines
qapi/migration.json +11
-0+# +# Since: 7.0 +# +# Example: +# {"execute": "set-vcpu-dirty-limit"} +# "arguments": { "dirty-rate": 200, +# "cpu-index": 1 } } +# +## +{ 'command': 'set-vcpu-dirty-limit', + 'data': { '*cpu-index': 'uint64',
Use 'int' for consistency with cpu-index arguments elsewhere.
Quoted 12 lines
qapi/migration.json +12
-0+ 'dirty-rate': 'uint64' } } + +## +# @cancel-vcpu-dirty-limit: +# +# Cancel the upper limit of dirty page rate for virtual CPUs. +# +# Cancel the dirty page limit for the vCPU which has been set with +# set-vcpu-dirty-limit command. Note that this command requires +# support from dirty ring, same as the "set-vcpu-dirty-limit". +# +# @cpu-index: index of virtual CPU, default is all.
qapi/migration.json line 1916
Quoted 10 lines
qapi/migration.json +10
-0+# +# Since: 7.0 +# +# Example: +# {"execute": "cancel-vcpu-dirty-limit"} +# "arguments": { "cpu-index": 1 } } +# +## +{ 'command': 'cancel-vcpu-dirty-limit', + 'data': { '*cpu-index': 'uint64'} }
Likewise.
Quoted 19 lines
qapi/migration.json +16
-0+ +## +# @query-vcpu-dirty-limit: +# +# Returns information about virtual CPU dirty page rate limits, if any. +# +# Since: 7.0 +# +# Example: +# {"execute": "query-vcpu-dirty-limit"} +# +## +{ 'command': 'query-vcpu-dirty-limit', + 'returns': [ 'DirtyLimitInfo' ] } + +## # @snapshot-save: # # Save a VM snapshot
[...]