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

Introduce virDomainStartDirtyRateCalc API for start calculation of
a domain's memory dirty rate with a specified time.

cherry-pick from df5c5c3e60e5a3b8e9b827cc51984f055ba1ce01

Signed-off-by: Hao Wang <wanghao232@huawei.com>
Signed-off-by: Hyman Huang(黄勇) <huangy81@chinatelecom.cn>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
---
 include/libvirt/libvirt-domain.h |  4 +++
 src/driver-hypervisor.h          |  6 +++++
 src/libvirt-domain.c             | 44 ++++++++++++++++++++++++++++++++
 src/libvirt_public.syms          |  1 +
 src/remote/remote_driver.c       |  1 +
 src/remote/remote_protocol.x     | 12 +++++++++
 src/remote_protocol-structs      |  6 +++++
 7 files changed, 74 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 4ab0c9c0b2..24df273045 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -5011,4 +5011,8 @@ virDomainHotpatchManage(virDomainPtr domain,
                         const char *id,
                         unsigned int flags);
 
+int virDomainStartDirtyRateCalc(virDomainPtr domain,
+                                int seconds,
+                                unsigned int flags);
+
 #endif /* LIBVIRT_DOMAIN_H */
diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h
index afc21a0b3f..82f808905d 100644
--- a/src/driver-hypervisor.h
+++ b/src/driver-hypervisor.h
@@ -1394,6 +1394,11 @@ typedef char *
                               const char *id,
                               unsigned int flags);
 
+typedef int
+(*virDrvDomainStartDirtyRateCalc)(virDomainPtr domain,
+                                  int seconds,
+                                  unsigned int flags);
+
 typedef struct _virHypervisorDriver virHypervisorDriver;
 typedef virHypervisorDriver *virHypervisorDriverPtr;
 
@@ -1658,4 +1663,5 @@ struct _virHypervisorDriver {
     virDrvDomainBackupBegin domainBackupBegin;
     virDrvDomainBackupGetXMLDesc domainBackupGetXMLDesc;
     virDrvDomainHotpatchManage domainHotpatchManage;
+    virDrvDomainStartDirtyRateCalc domainStartDirtyRateCalc;
 };
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index 3cf6bcb3b4..974a5a5f36 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -12800,3 +12800,47 @@ virDomainHotpatchManage(virDomainPtr domain,
     virDispatchError(conn);
     return NULL;
 }
+
+
+/**
+ * virDomainStartDirtyRateCalc:
+ * @domain: a domain object
+ * @seconds: specified calculating time in seconds
+ * @flags: extra flags; not used yet, so callers should always pass 0
+ *
+ * Calculate the current domain's memory dirty rate in next @seconds.
+ * The calculated dirty rate information is available by calling
+ * virConnectGetAllDomainStats.
+ *
+ * Returns 0 in case of success, -1 otherwise.
+ */
+int
+virDomainStartDirtyRateCalc(virDomainPtr domain,
+                            int seconds,
+                            unsigned int flags)
+{
+    virConnectPtr conn;
+
+    VIR_DOMAIN_DEBUG(domain, "seconds=%d, flags=0x%x", seconds, flags);
+
+    virResetLastError();
+
+    virCheckDomainReturn(domain, -1);
+    conn = domain->conn;
+
+    virCheckReadOnlyGoto(conn->flags, error);
+
+    if (conn->driver->domainStartDirtyRateCalc) {
+        int ret;
+        ret = conn->driver->domainStartDirtyRateCalc(domain, seconds, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+
+    virReportUnsupportedError();
+
+ error:
+    virDispatchError(conn);
+    return -1;
+}
diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index 0ad0b9e489..f006516208 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -876,5 +876,6 @@ LIBVIRT_6.0.0 {
 LIBVIRT_6.2.0 {
     global:
         virDomainHotpatchManage;
+        virDomainStartDirtyRateCalc;
 } LIBVIRT_6.0.0;
 # .... define new API here using predicted next version number ....
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 1202d44017..e8ccbcb4f8 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -8685,6 +8685,7 @@ static virHypervisorDriver hypervisor_driver = {
     .domainBackupBegin = remoteDomainBackupBegin, /* 6.0.0 */
     .domainBackupGetXMLDesc = remoteDomainBackupGetXMLDesc, /* 6.0.0 */
     .domainHotpatchManage = remoteDomainHotpatchManage, /* 6.2.0 */
+    .domainStartDirtyRateCalc = remoteDomainStartDirtyRateCalc, /* 6.2.0 */
 };
 
 static virNetworkDriver network_driver = {
diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x
index ee13075ce1..d89cc1a087 100644
--- a/src/remote/remote_protocol.x
+++ b/src/remote/remote_protocol.x
@@ -3783,6 +3783,12 @@ struct remote_domain_hotpatch_manage_ret {
     remote_string info;
 };
 
+struct remote_domain_start_dirty_rate_calc_args {
+    remote_nonnull_domain dom;
+    int seconds;
+    unsigned int flags;
+};
+
 /*----- Protocol. -----*/
 
 /* Define the program number, protocol version and procedure numbers here. */
@@ -6682,6 +6688,12 @@ enum remote_procedure {
      */
     REMOTE_PROC_DOMAIN_BACKUP_GET_XML_DESC = 422,
 
+    /**
+     * @generate: both
+     * @acl: domain:read
+     */
+    REMOTE_PROC_DOMAIN_START_DIRTY_RATE_CALC = 427,
+
     /**
      * @generate: both
      * @acl: domain:read
diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs
index bae0f0b545..1e81814690 100644
--- a/src/remote_protocol-structs
+++ b/src/remote_protocol-structs
@@ -3135,6 +3135,11 @@ struct remote_domain_backup_get_xml_desc_args {
 struct remote_domain_backup_get_xml_desc_ret {
         remote_nonnull_string      xml;
 };
+struct remote_domain_start_dirty_rate_calc_args {
+        remote_nonnull_domain      dom;
+        int                        seconds;
+        u_int                      flags;
+};
 enum remote_procedure {
         REMOTE_PROC_CONNECT_OPEN = 1,
         REMOTE_PROC_CONNECT_CLOSE = 2,
@@ -3558,4 +3563,5 @@ enum remote_procedure {
         REMOTE_PROC_DOMAIN_AGENT_SET_RESPONSE_TIMEOUT = 420,
         REMOTE_PROC_DOMAIN_BACKUP_BEGIN = 421,
         REMOTE_PROC_DOMAIN_BACKUP_GET_XML_DESC = 422,
+        REMOTE_PROC_DOMAIN_START_DIRTY_RATE_CALC = 427,
 };
-- 
2.27.0

