From nobody Mon Feb 9 21:00:40 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1639480879889192.83599875133166; Tue, 14 Dec 2021 03:21:19 -0800 (PST) Received: from localhost ([::1]:53416 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mx5ry-0006Ox-QV for importer@patchew.org; Tue, 14 Dec 2021 06:21:18 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50456) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mx5f6-0003dF-FB for qemu-devel@nongnu.org; Tue, 14 Dec 2021 06:08:00 -0500 Received: from prt-mail.chinatelecom.cn ([42.123.76.223]:41277 helo=chinatelecom.cn) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mx5f1-0007Wh-Hb for qemu-devel@nongnu.org; Tue, 14 Dec 2021 06:07:59 -0500 Received: from clientip-118.116.19.154 (unknown [172.18.0.218]) by chinatelecom.cn (HERMES) with SMTP id D32ED2800BA; Tue, 14 Dec 2021 19:07:44 +0800 (CST) Received: from ([172.18.0.218]) by app0025 with ESMTP id dc2b3206073c4924ad418b54750933c9 for qemu-devel@nongnu.org; Tue, 14 Dec 2021 19:07:47 CST HMM_SOURCE_IP: 172.18.0.218:48586.2140870327 HMM_ATTACHE_NUM: 0000 HMM_SOURCE_TYPE: SMTP X-189-SAVE-TO-SEND: +huangy81@chinatelecom.cn X-Transaction-ID: dc2b3206073c4924ad418b54750933c9 X-Real-From: huangy81@chinatelecom.cn X-Receive-IP: 172.18.0.218 X-MEDUSA-Status: 0 From: huangy81@chinatelecom.cn To: qemu-devel Subject: [PATCH v10 3/3] cpus-common: implement dirty page limit on virtual CPU Date: Tue, 14 Dec 2021 19:07:34 +0800 Message-Id: X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=42.123.76.223; envelope-from=huangy81@chinatelecom.cn; helo=chinatelecom.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , David Hildenbrand , Hyman , Juan Quintela , Richard Henderson , Markus ArmBruster , Peter Xu , "Dr. David Alan Gilbert" , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1639480881832100001 From: Hyman Huang(=E9=BB=84=E5=8B=87) 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 "vcpu-dirty-limit", "query-vcpu-dirty-limit" to enable, disable, query dirty page limit for virtual CPU. Meanwhile, introduce corresponding hmp commands "vcpu_dirty_limit", "info vcpu_dirty_limit" so developers can play with them easier. Signed-off-by: Hyman Huang(=E9=BB=84=E5=8B=87) --- cpus-common.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ hmp-commands-info.hx | 13 +++++ hmp-commands.hx | 17 ++++++ include/monitor/hmp.h | 2 + qapi/migration.json | 44 ++++++++++++++ 5 files changed, 231 insertions(+) diff --git a/cpus-common.c b/cpus-common.c index 6e73d3e..37c3584 100644 --- a/cpus-common.c +++ b/cpus-common.c @@ -23,6 +23,15 @@ #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 "monitor/hmp.h" +#include "monitor/monitor.h" +#include "qapi/qmp/qdict.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-migration.h" +#include "hw/boards.h" =20 static QemuMutex qemu_cpu_list_lock; static QemuCond exclusive_cond; @@ -352,3 +361,149 @@ void process_queued_cpu_work(CPUState *cpu) qemu_mutex_unlock(&cpu->work_mutex); qemu_cond_broadcast(&qemu_work_cond); } + +void qmp_vcpu_dirty_limit(bool enable, + bool has_cpu_index, + uint64_t cpu_index, + bool has_dirty_rate, + uint64_t dirty_rate, + Error **errp) +{ + static bool initialized; + + if (!kvm_enabled() || !kvm_dirty_ring_enabled()) { + if (enable) { + error_setg(errp, "dirty page limit feature requires KVM with" + " accelerator property 'dirty-ring-size' set'"); + } + return; + } + + if (!enable && !dirtylimit_in_service()) { + return; + } + + if (!initialized) { + MachineState *ms =3D MACHINE(qdev_get_machine()); + dirtylimit_calc_state_init(ms->smp.max_cpus); + dirtylimit_state_init(ms->smp.max_cpus); + initialized =3D true; + } + + if (enable && !has_dirty_rate) { + error_setg(errp, "enable dirty page limit feature requires" + " 'dirty-rate' set'"); + return; + } + + if (has_cpu_index && !dirtylimit_is_vcpu_index_valid(cpu_index)) { + error_setg(errp, "incorrect cpu index specified"); + return; + } + + if (enable) { + dirtylimit_calc_start(); + if (has_cpu_index) { + dirtylimit_vcpu(cpu_index, dirty_rate); + } else { + dirtylimit_all(dirty_rate); + } + } else { + if (has_cpu_index) { + dirtylimit_cancel_vcpu(cpu_index); + } else { + dirtylimit_cancel_all(); + } + + if (!dirtylimit_in_service()) { + dirtylimit_calc_quit(); + dirtylimit_state_finalize(); + dirtylimit_calc_state_finalize(); + initialized =3D false; + } + } +} + +void hmp_vcpu_dirty_limit(Monitor *mon, const QDict *qdict) +{ + bool global =3D qdict_get_try_bool(qdict, "global", false); + bool enable =3D qdict_get_bool(qdict, "enable"); + int64_t cpu_index =3D qdict_get_try_int(qdict, "cpu_index", -1); + int64_t dirty_rate =3D qdict_get_try_int(qdict, "dirty_rate", -1); + Error *err =3D NULL; + + if (enable && dirty_rate < 0) { + monitor_printf(mon, "Dirty page limit requires dirty_rate set!\n"); + return; + } + + if (enable && !global && cpu_index < 0) { + monitor_printf(mon, "Dirty page limit requires cpu_index set!\n"); + return; + } + + if (global && cpu_index !=3D -1) { + monitor_printf(mon, "Either global option or cpu_index can be set!= \n"); + return; + } + + if (global) { + if (enable) { + qmp_vcpu_dirty_limit(true, false, -1, true, dirty_rate, &err); + } else { + qmp_vcpu_dirty_limit(false, false, -1, false, -1, &err); + } + } else { + if (enable) { + qmp_vcpu_dirty_limit(true, true, cpu_index, true, dirty_rate, = &err); + } else { + qmp_vcpu_dirty_limit(false, true, cpu_index, false, -1, &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"); +} + +struct DirtyLimitInfoList *qmp_query_vcpu_dirty_limit(Error **errp) +{ + if (!dirtylimit_in_service()) { + error_setg(errp, "dirty page limit not enabled"); + return NULL; + } + + return dirtylimit_query_all(); +} + +void hmp_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict) +{ + DirtyLimitInfoList *limit, *head, *info =3D NULL; + Error *err =3D NULL; + + if (!dirtylimit_in_service()) { + monitor_printf(mon, "Dirty page limit not enabled!\n"); + return; + } + + info =3D qmp_query_vcpu_dirty_limit(&err); + if (err) { + hmp_handle_error(mon, err); + return; + } + + head =3D info; + for (limit =3D head; limit !=3D NULL; limit =3D 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); +} diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx index 407a1da..5dd3001 100644 --- a/hmp-commands-info.hx +++ b/hmp-commands-info.hx @@ -863,6 +863,19 @@ SRST Display the vcpu dirty rate information. ERST =20 + { + .name =3D "vcpu_dirty_limit", + .args_type =3D "", + .params =3D "", + .help =3D "show dirty page limit information of all vCPU", + .cmd =3D hmp_info_vcpu_dirty_limit, + }, + +SRST + ``info vcpu_dirty_limit`` + Display the vcpu dirty page limit information. +ERST + #if defined(TARGET_I386) { .name =3D "sgx", diff --git a/hmp-commands.hx b/hmp-commands.hx index 70a9136..ef0f7cc 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -1744,3 +1744,20 @@ ERST "\n\t\t\t -b to specify dirty bitmap as method of ca= lculation)", .cmd =3D hmp_calc_dirty_rate, }, + +SRST +``vcpu_dirty_limit`` + Start dirty page rate limit on a virtual CPU, the information about all = the + virtual CPU dirty limit status can be observed with ``info vcpu_dirty_li= mit`` + command. +ERST + + { + .name =3D "vcpu_dirty_limit", + .args_type =3D "global:-g,enable:b,cpu_index:l?,dirty_rate:l?", + .params =3D "[-g] on|off [cpu_index] [dirty_rate]", + .help =3D "turn on,off dirty page rate limit" + "\n\t\t (use -g to affect all vCPU, cpu_index requir= ed be set to -1 if" + "\n\t\t enable all vCPU. dirty_rate should be specif= ied if turned on)", + .cmd =3D hmp_vcpu_dirty_limit, + }, diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h index 96d0148..04879a2 100644 --- a/include/monitor/hmp.h +++ b/include/monitor/hmp.h @@ -131,6 +131,8 @@ 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_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict); +void hmp_vcpu_dirty_limit(Monitor *mon, const QDict *qdict); void hmp_human_readable_text_helper(Monitor *mon, HumanReadableText *(*qmp_handler)(Erro= r **)); =20 diff --git a/qapi/migration.json b/qapi/migration.json index ac5fa56..7d8da4f 100644 --- a/qapi/migration.json +++ b/qapi/migration.json @@ -1869,6 +1869,50 @@ 'current-rate': 'int64' } } =20 ## +# @vcpu-dirty-limit: +# +# Set or cancel 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. +# +# @enable: true to enable, false to disable. +# +# @cpu-index: index of virtual CPU, default is all. +# +# @dirty-rate: upper limit of dirty page rate for virtual CPU, to +# cancel dirty limit, this field will be ignored. +# +# Since: 7.0 +# +# Example: +# {"execute": "vcpu-dirty-limit"} +# "arguments": { "enable": true, +# "cpu-index": 1, +# "dirty-rate": 200 } } +# +## +{ 'command': 'vcpu-dirty-limit', + 'data': { 'enable': 'bool', + '*cpu-index': 'uint64', + '*dirty-rate': 'uint64'} } + +## +# @query-vcpu-dirty-limit: +# +# Returns information about all virtual CPU dirty limit if enabled. +# +# Since: 7.0 +# +# Example: +# {"execute": "query-vcpu-dirty-limit"} +# +## +{ 'command': 'query-vcpu-dirty-limit', + 'returns': [ 'DirtyLimitInfo' ] } + +## # @snapshot-save: # # Save a VM snapshot --=20 1.8.3.1