softmmu/dirtylimit: Implement dirty page rate limit

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.

"query-vcpu-dirty-limit" success depends on enabling dirty
page rate limit, so just add it to the list of skipped
command to ensure qmp-cmd-test run successfully.

Signed-off-by: Hyman Huang(黄勇) <huangy81@chinatelecom.cn>
Acked-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
Message-Id: <4143f26706d413dd29db0b672fe58b3d3fbe34bc.1656177590.git.huangy81@chinatelecom.cn>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
This commit is contained in:
Hyman Huang(黄勇) 2022-06-26 01:38:36 +08:00 committed by Dr. David Alan Gilbert
parent baa609832e
commit f3b2e38cfb
6 changed files with 324 additions and 0 deletions

View File

@ -865,6 +865,19 @@ SRST
Display the vcpu dirty rate information. Display the vcpu dirty rate information.
ERST 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) #if defined(TARGET_I386)
{ {
.name = "sgx", .name = "sgx",

View File

@ -1768,3 +1768,35 @@ ERST
"\n\t\t\t -b to specify dirty bitmap as method of calculation)", "\n\t\t\t -b to specify dirty bitmap as method of calculation)",
.cmd = hmp_calc_dirty_rate, .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\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\t limit on a specified virtual cpu",
.cmd = hmp_cancel_vcpu_dirty_limit,
},

View File

@ -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_replay_seek(Monitor *mon, const QDict *qdict);
void hmp_info_dirty_rate(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_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, void hmp_human_readable_text_helper(Monitor *mon,
HumanReadableText *(*qmp_handler)(Error **)); HumanReadableText *(*qmp_handler)(Error **));
void hmp_info_stats(Monitor *mon, const QDict *qdict); void hmp_info_stats(Monitor *mon, const QDict *qdict);

View File

@ -1868,6 +1868,86 @@
## ##
{ 'command': 'query-dirty-rate', 'returns': 'DirtyRateInfo' } { 'command': 'query-dirty-rate', 'returns': 'DirtyRateInfo' }
##
# @DirtyLimitInfo:
#
# Dirty page rate limit information of a virtual CPU.
#
# @cpu-index: index of a virtual CPU.
#
# @limit-rate: upper limit of dirty page rate (MB/s) for a virtual
# CPU, 0 means unlimited.
#
# @current-rate: current dirty page rate (MB/s) for a virtual CPU.
#
# Since: 7.1
#
##
{ 'struct': 'DirtyLimitInfo',
'data': { 'cpu-index': 'int',
'limit-rate': 'uint64',
'current-rate': 'uint64' } }
##
# @set-vcpu-dirty-limit:
#
# 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 a virtual CPU, default is all.
#
# @dirty-rate: upper limit of dirty page rate (MB/s) for virtual CPUs.
#
# Since: 7.1
#
# Example:
# {"execute": "set-vcpu-dirty-limit"}
# "arguments": { "dirty-rate": 200,
# "cpu-index": 1 } }
#
##
{ 'command': 'set-vcpu-dirty-limit',
'data': { '*cpu-index': 'int',
'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 a virtual CPU, default is all.
#
# Since: 7.1
#
# Example:
# {"execute": "cancel-vcpu-dirty-limit"}
# "arguments": { "cpu-index": 1 } }
#
##
{ 'command': 'cancel-vcpu-dirty-limit',
'data': { '*cpu-index': 'int'} }
##
# @query-vcpu-dirty-limit:
#
# Returns information about virtual CPU dirty page rate limits, if any.
#
# Since: 7.1
#
# Example:
# {"execute": "query-vcpu-dirty-limit"}
#
##
{ 'command': 'query-vcpu-dirty-limit',
'returns': [ 'DirtyLimitInfo' ] }
## ##
# @snapshot-save: # @snapshot-save:
# #

View File

@ -14,8 +14,12 @@
#include "qapi/error.h" #include "qapi/error.h"
#include "qemu/main-loop.h" #include "qemu/main-loop.h"
#include "qapi/qapi-commands-migration.h" #include "qapi/qapi-commands-migration.h"
#include "qapi/qmp/qdict.h"
#include "qapi/error.h"
#include "sysemu/dirtyrate.h" #include "sysemu/dirtyrate.h"
#include "sysemu/dirtylimit.h" #include "sysemu/dirtylimit.h"
#include "monitor/hmp.h"
#include "monitor/monitor.h"
#include "exec/memory.h" #include "exec/memory.h"
#include "hw/boards.h" #include "hw/boards.h"
#include "sysemu/kvm.h" #include "sysemu/kvm.h"
@ -405,3 +409,193 @@ void dirtylimit_vcpu_execute(CPUState *cpu)
usleep(cpu->throttle_us_per_full); usleep(cpu->throttle_us_per_full);
} }
} }
static void dirtylimit_init(void)
{
dirtylimit_state_initialize();
dirtylimit_change(true);
vcpu_dirty_rate_stat_initialize();
vcpu_dirty_rate_stat_start();
}
static void dirtylimit_cleanup(void)
{
vcpu_dirty_rate_stat_stop();
vcpu_dirty_rate_stat_finalize();
dirtylimit_change(false);
dirtylimit_state_finalize();
}
void qmp_cancel_vcpu_dirty_limit(bool has_cpu_index,
int64_t cpu_index,
Error **errp)
{
if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
return;
}
if (has_cpu_index && !dirtylimit_vcpu_index_valid(cpu_index)) {
error_setg(errp, "incorrect cpu index specified");
return;
}
if (!dirtylimit_in_service()) {
return;
}
dirtylimit_state_lock();
if (has_cpu_index) {
dirtylimit_set_vcpu(cpu_index, 0, false);
} else {
dirtylimit_set_all(0, false);
}
if (!dirtylimit_state->limited_nvcpu) {
dirtylimit_cleanup();
}
dirtylimit_state_unlock();
}
void hmp_cancel_vcpu_dirty_limit(Monitor *mon, const QDict *qdict)
{
int64_t cpu_index = qdict_get_try_int(qdict, "cpu_index", -1);
Error *err = NULL;
qmp_cancel_vcpu_dirty_limit(!!(cpu_index != -1), cpu_index, &err);
if (err) {
hmp_handle_error(mon, err);
return;
}
monitor_printf(mon, "[Please use 'info vcpu_dirty_limit' to query "
"dirty limit for virtual CPU]\n");
}
void qmp_set_vcpu_dirty_limit(bool has_cpu_index,
int64_t cpu_index,
uint64_t dirty_rate,
Error **errp)
{
if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
error_setg(errp, "dirty page limit feature requires KVM with"
" accelerator property 'dirty-ring-size' set'");
return;
}
if (has_cpu_index && !dirtylimit_vcpu_index_valid(cpu_index)) {
error_setg(errp, "incorrect cpu index specified");
return;
}
if (!dirty_rate) {
qmp_cancel_vcpu_dirty_limit(has_cpu_index, cpu_index, errp);
return;
}
dirtylimit_state_lock();
if (!dirtylimit_in_service()) {
dirtylimit_init();
}
if (has_cpu_index) {
dirtylimit_set_vcpu(cpu_index, dirty_rate, true);
} else {
dirtylimit_set_all(dirty_rate, true);
}
dirtylimit_state_unlock();
}
void hmp_set_vcpu_dirty_limit(Monitor *mon, const QDict *qdict)
{
int64_t dirty_rate = qdict_get_int(qdict, "dirty_rate");
int64_t cpu_index = qdict_get_try_int(qdict, "cpu_index", -1);
Error *err = NULL;
qmp_set_vcpu_dirty_limit(!!(cpu_index != -1), cpu_index, dirty_rate, &err);
if (err) {
hmp_handle_error(mon, err);
return;
}
monitor_printf(mon, "[Please use 'info vcpu_dirty_limit' to query "
"dirty limit for virtual CPU]\n");
}
static struct DirtyLimitInfo *dirtylimit_query_vcpu(int cpu_index)
{
DirtyLimitInfo *info = NULL;
info = g_malloc0(sizeof(*info));
info->cpu_index = cpu_index;
info->limit_rate = dirtylimit_vcpu_get_state(cpu_index)->quota;
info->current_rate = vcpu_dirty_rate_get(cpu_index);
return info;
}
static struct DirtyLimitInfoList *dirtylimit_query_all(void)
{
int i, index;
DirtyLimitInfo *info = NULL;
DirtyLimitInfoList *head = NULL, **tail = &head;
dirtylimit_state_lock();
if (!dirtylimit_in_service()) {
dirtylimit_state_unlock();
return NULL;
}
for (i = 0; i < dirtylimit_state->max_cpus; i++) {
index = dirtylimit_state->states[i].cpu_index;
if (dirtylimit_vcpu_get_state(index)->enabled) {
info = dirtylimit_query_vcpu(index);
QAPI_LIST_APPEND(tail, info);
}
}
dirtylimit_state_unlock();
return head;
}
struct DirtyLimitInfoList *qmp_query_vcpu_dirty_limit(Error **errp)
{
if (!dirtylimit_in_service()) {
return NULL;
}
return dirtylimit_query_all();
}
void hmp_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict)
{
DirtyLimitInfoList *limit, *head, *info = NULL;
Error *err = NULL;
if (!dirtylimit_in_service()) {
monitor_printf(mon, "Dirty page limit not enabled!\n");
return;
}
info = qmp_query_vcpu_dirty_limit(&err);
if (err) {
hmp_handle_error(mon, err);
return;
}
head = info;
for (limit = head; limit != NULL; limit = limit->next) {
monitor_printf(mon, "vcpu[%"PRIi64"], limit rate %"PRIi64 " (MB/s),"
" current rate %"PRIi64 " (MB/s)\n",
limit->value->cpu_index,
limit->value->limit_rate,
limit->value->current_rate);
}
g_free(info);
}

View File

@ -110,6 +110,8 @@ static bool query_is_ignored(const char *cmd)
"query-sev-capabilities", "query-sev-capabilities",
"query-sgx", "query-sgx",
"query-sgx-capabilities", "query-sgx-capabilities",
/* Success depends on enabling dirty page rate limit */
"query-vcpu-dirty-limit",
NULL NULL
}; };
int i; int i;