[PATCH v1 04/10] virsh: Introduce vcpudirtylimit api

huangy81 at chinatelecom.cn huangy81 at chinatelecom.cn
Fri Aug 26 17:20:08 UTC 2022


From: Hyman Huang(黄勇) <huangy81 at chinatelecom.cn>

Introduce vcpudirtylimit virsh api to set dirty page
rate limit for virtual CPUs:
$ virsh vcpudirtylimit <domain> <rate> [--vcpu <number>]

Signed-off-by: Hyman Huang(黄勇) <huangy81 at chinatelecom.cn>
---
 tools/virsh-domain.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 20aadb5..8705090 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -13764,6 +13764,83 @@ cmdDomDirtyRateCalc(vshControl *ctl, const vshCmd *cmd)
     return true;
 }
 
+/*
+ * "vcpudirtylimit" command
+ */
+static const vshCmdInfo info_vcpu_dirty_limit[] = {
+    {.name = "help",
+     .data = N_("Set vcpu dirty page rate limit.")
+    },
+    {.name = "desc",
+     .data = N_("Set dirty page rate limit on virtual CPU.")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_vcpu_dirty_limit[] = {
+    VIRSH_COMMON_OPT_DOMAIN_FULL(0),
+    {.name = "rate",
+     .type = VSH_OT_INT,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("Upper limit of dirty page rate (MB/s) for "
+                "virtual CPUs.")
+    },
+    {.name = "vcpu",
+     .type = VSH_OT_INT,
+     .help = N_("Index of a virtual CPU.")
+    },
+    {.name = NULL}
+};
+
+static bool
+cmdVcpuDirtyLimit(vshControl *ctl, const vshCmd *cmd)
+{
+    g_autoptr(virshDomain) dom = NULL;
+    int vcpu_idx = -1;
+    unsigned long long rate = 0;
+    bool vcpu = vshCommandOptBool(cmd, "vcpu");
+    unsigned int flags = 0;
+
+    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    if (vcpu) {
+        if (vshCommandOptInt(ctl, cmd, "vcpu", &vcpu_idx) < 0)
+            return false;
+
+        if (vcpu_idx < 0) {
+            vshError(ctl, "%s", _("Invalid vcpu index, using --vcpu "
+                                  "to specify cpu index"));
+            return false;
+        }
+    }
+
+    if (vshCommandOptULongLong(ctl, cmd, "rate", &rate) < 0)
+        return false;
+
+    if (!rate) {
+        vshError(ctl, "%s", _("Invalid dirty page rate limit"));
+        return false;
+    }
+
+    if (vcpu) {
+        /* set specified vcpu dirty page rate limit of vm */
+        if (virDomainSetVcpuDirtyLimit(dom, vcpu_idx,
+                rate, flags | VIR_DOMAIN_DIRTYLIMIT_VCPU) < 0)
+            return false;
+        vshPrintExtra(ctl, _("Set vcpu[%d] dirty page rate limit "
+                             "%lld successfully.\n"), vcpu_idx, rate);
+    } else {
+        /* set all vcpu dirty page rate limit of vm */
+        if (virDomainSetVcpuDirtyLimit(dom, -1,
+                rate, flags | VIR_DOMAIN_DIRTYLIMIT_ALL) < 0)
+            return false;
+        vshPrintExtra(ctl, _("Set dirty page rate limit %lld on all "
+                             "virtual CPUs successfully.\n"), rate);
+    }
+
+    return true;
+}
 
 const vshCmdDef domManagementCmds[] = {
     {.name = "attach-device",
@@ -14422,5 +14499,11 @@ const vshCmdDef domManagementCmds[] = {
      .info = info_domdirtyrate_calc,
      .flags = 0
     },
+    {.name = "vcpudirtylimit",
+     .handler = cmdVcpuDirtyLimit,
+     .opts = opts_vcpu_dirty_limit,
+     .info = info_vcpu_dirty_limit,
+     .flags = 0
+    },
     {.name = NULL}
 };
-- 
1.8.3.1



More information about the libvir-list mailing list