From d6dd8dba23726495026b721da0835f7916000f94 Mon Sep 17 00:00:00 2001
From: Hao Wang <wanghao232@huawei.com>
Date: Tue, 16 Mar 2021 20:32:46 +0800
Subject: [PATCH 2/7] migration/dirtyrate: Implement
 qemuDomainStartDirtyRateCalc
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Implement qemuDomainStartDirtyRateCalc which calculates domain's memory
dirty rate calling qmp "calc-dirty-rate".

cherry-pick from fbe99823e2a1b30e770620b0b51930e15c88de00

Signed-off-by: Hao Wang <wanghao232@huawei.com>
Signed-off-by: Hyman Huang(黄勇) <huangy81@chinatelecom.cn>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/qemu/qemu_driver.c       | 59 ++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor.c      | 12 ++++++++
 src/qemu/qemu_monitor.h      |  4 +++
 src/qemu/qemu_monitor_json.c | 21 +++++++++++++
 src/qemu/qemu_monitor_json.h |  4 +++
 5 files changed, 100 insertions(+)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index e7166e4af3..b0a4905279 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -23298,6 +23298,64 @@ qemuDomainHotpatchManage(virDomainPtr domain,
     return ret;
 }
 
+#define MIN_DIRTYRATE_CALC_PERIOD 1  /* supported min dirtyrate calculating time: 1s */
+#define MAX_DIRTYRATE_CALC_PERIOD 60 /* supported max dirtyrate calculating time: 60s */
+
+static int
+qemuDomainStartDirtyRateCalc(virDomainPtr dom,
+                             int seconds,
+                             unsigned int flags)
+{
+    virQEMUDriverPtr driver = dom->conn->privateData;
+    virDomainObjPtr vm;
+    qemuDomainObjPrivatePtr priv;
+    int ret = -1;
+
+    virCheckFlags(0, -1);
+
+    if (seconds < MIN_DIRTYRATE_CALC_PERIOD ||
+        seconds > MAX_DIRTYRATE_CALC_PERIOD) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("seconds=%d is invalid, please choose value within [%d, %d]."),
+                       seconds,
+                       MIN_DIRTYRATE_CALC_PERIOD,
+                       MAX_DIRTYRATE_CALC_PERIOD);
+        return -1;
+    }
+
+    if (!(vm = qemuDomainObjFromDomain(dom)))
+        return -1;
+
+    if (virDomainStartDirtyRateCalcEnsureACL(dom->conn, vm->def) < 0)
+        goto cleanup;
+
+    if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_QUERY) < 0)
+        goto cleanup;
+
+    if (!virDomainObjIsActive(vm)) {
+        virReportError(VIR_ERR_OPERATION_INVALID,
+                       "%s", _("domain is not running"));
+        goto endjob;
+    }
+
+    VIR_DEBUG("Calculate dirty rate in next %d seconds", seconds);
+
+    priv = vm->privateData;
+    qemuDomainObjEnterMonitor(driver, vm);
+    ret = qemuMonitorStartDirtyRateCalc(priv->mon, seconds);
+
+    if (qemuDomainObjExitMonitor(driver, vm) < 0)
+        ret = -1;
+
+ endjob:
+    qemuDomainObjEndJob(driver, vm);
+
+ cleanup:
+    virDomainObjEndAPI(&vm);
+    return ret;
+}
+
+
 static virHypervisorDriver qemuHypervisorDriver = {
     .name = QEMU_DRIVER_NAME,
     .connectURIProbe = qemuConnectURIProbe,
@@ -23538,6 +23596,7 @@ static virHypervisorDriver qemuHypervisorDriver = {
     .domainBackupBegin = qemuDomainBackupBegin, /* 6.0.0 */
     .domainBackupGetXMLDesc = qemuDomainBackupGetXMLDesc, /* 6.0.0 */
     .domainHotpatchManage = qemuDomainHotpatchManage, /* 6.2.0 */
+    .domainStartDirtyRateCalc = qemuDomainStartDirtyRateCalc, /* 6.2.0 */
 };
 
 
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index ec79fa6368..cdfe5fbed9 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -4615,3 +4615,15 @@ qemuMonitorTransactionBackup(virJSONValuePtr actions,
     return qemuMonitorJSONTransactionBackup(actions, device, jobname, target,
                                             bitmap, syncmode);
 }
+
+
+int
+qemuMonitorStartDirtyRateCalc(qemuMonitorPtr mon,
+                              int seconds)
+{
+    VIR_DEBUG("seconds=%d", seconds);
+
+    QEMU_CHECK_MONITOR(mon);
+
+    return qemuMonitorJSONStartDirtyRateCalc(mon, seconds);
+}
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 73c8af9e78..7b859155c3 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -1425,3 +1425,7 @@ qemuMonitorTransactionBackup(virJSONValuePtr actions,
                              const char *target,
                              const char *bitmap,
                              qemuMonitorTransactionBackupSyncMode syncmode);
+
+int
+qemuMonitorStartDirtyRateCalc(qemuMonitorPtr mon,
+                              int seconds);
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index cc6644c9c3..fad9a2c233 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -9374,3 +9374,24 @@ qemuMonitorJSONGetJobInfo(qemuMonitorPtr mon,
 
     return 0;
 }
+
+int
+qemuMonitorJSONStartDirtyRateCalc(qemuMonitorPtr mon,
+                                  int seconds)
+{
+    g_autoptr(virJSONValue) cmd = NULL;
+    g_autoptr(virJSONValue) reply = NULL;
+
+    if (!(cmd = qemuMonitorJSONMakeCommand("calc-dirty-rate",
+                                           "i:calc-time", seconds,
+                                           NULL)))
+        return -1;
+
+    if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
+        return -1;
+
+    if (qemuMonitorJSONCheckError(cmd, reply) < 0)
+        return -1;
+
+    return 0;
+}
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index 2c0e3ec07a..048e7c267c 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -691,3 +691,7 @@ int qemuMonitorJSONSetDBusVMStateIdList(qemuMonitorPtr mon,
                                         const char *vmstatepath,
                                         const char **list)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
+
+int
+qemuMonitorJSONStartDirtyRateCalc(qemuMonitorPtr mon,
+                                  int seconds);
-- 
2.27.0

