On 2/26/21 9:35 AM, Hao Wang wrote:
Introduce domdirtyrate-calc virsh api to start calculating domain's
memory dirty rate:
        # virsh domdirtyrate-calc <domain> [--seconds <sec>]

Signed-off-by: Hao Wang <wanghao...@huawei.com>
---
  docs/manpages/virsh.rst | 17 +++++++++++
  tools/virsh-domain.c    | 63 +++++++++++++++++++++++++++++++++++++++++
  2 files changed, 80 insertions(+)

diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst
index 8a4328faa0..417ea444f4 100644
--- a/docs/manpages/virsh.rst
+++ b/docs/manpages/virsh.rst
@@ -1704,6 +1704,23 @@ states other than "ok" or "error" the command also 
prints number of
  seconds elapsed since the control interface entered its current state.
+domdirtyrate-calc
+-----------------
+
+**Syntax:**
+
+::
+
+   domdirtyrate-calc <domain> [--seconds <sec>]
+
+Calculate an active domain's memory dirty rate which may be expected by
+user in order to decide whether it's proper to be migrated out or not.
+The ``seconds`` parameter can be used to calculate dirty rate in a
+specific time which allows 60s at most now and would be default to 1s
+if missing. The calculated dirty rate infomation is available by calling
+'domstats --dirtyrate'.
+
+
  domdisplay
  ----------
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index df33467646..ccb5d61a25 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -14412,6 +14412,63 @@ cmdSetUserSSHKeys(vshControl *ctl, const vshCmd *cmd)
  }
+/*
+ * "domdirtyrate" command
+ */
+static const vshCmdInfo info_domdirtyrate_calc[] = {
+    {.name = "help",
+     .data = N_("Calculate a vm's memory dirty rate")
+    },
+    {.name = "desc",
+     .data = N_("Calculate memory dirty rate of a domain in order to decide whether 
"
+                "it's proper to be migrated out or not.\n"
+                "The calculated dirty rate infomation is available by calling "
+                "'domstats --dirtyrate'.")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_domdirtyrate_calc[] = {
+    VIRSH_COMMON_OPT_DOMAIN_FULL(0),
+    {.name = "seconds",
+     .type = VSH_OT_INT,
+     .help = N_("calculate memory dirty rate within specified seconds, "
+                "the supported value range from 1 to 60, default to 1.")
+    },
+    {.name = NULL}
+};
+
+static bool
+cmdDomDirtyRateCalc(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom = NULL;
+    int seconds;
+    int rc;
+    bool ret = false;
+
+    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    rc = vshCommandOptInt(ctl, cmd, "seconds", &seconds);
+    if (rc < 0)
+        goto cleanup;
+
+    /* if no inputted seconds, default to 1s */
+    if (!rc)
+        seconds = 1;

The vshCommandOptInt() (and others too for that matter) are designed so that if --argument is not present, then the destination variable is not touched. So this can be rewritten as:

int seconds = 1; /* the default value is 1 */

rc = vshCommandOptInt(ctl, cmd, "seconds", &seconds);
if (rc < 0)
  goto cleanup;

and since @rc is not used anywhere else it's not needed and thus can be dropped.

+
+    if (virDomainStartDirtyRateCalc(dom, seconds, 0) < 0)
+        goto cleanup;
+
+    vshPrint(ctl, _("Start to calculate domain's memory dirty rate 
successfully.\n"));

This could be vshPrintExtra(). That's what we tend to use for this affirmation prints. 'virsh -q' is quiet and can be used from a script, 'virsh' run by users gives them good feeling of something happening.

Michal

Reply via email to