26ba25
From 6d52c264fa9a7a78ee532f45d24704491ce4c431 Mon Sep 17 00:00:00 2001
26ba25
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
26ba25
Date: Wed, 1 Aug 2018 13:55:09 +0100
26ba25
Subject: [PATCH 05/21] migration: introduce control_save_page()
26ba25
26ba25
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
26ba25
Message-id: <20180801135522.11658-6-dgilbert@redhat.com>
26ba25
Patchwork-id: 81581
26ba25
O-Subject: [qemu-kvm RHEL8/virt212 PATCH 05/18] migration: introduce control_save_page()
26ba25
Bugzilla: 1594384
26ba25
RH-Acked-by: Peter Xu <peterx@redhat.com>
26ba25
RH-Acked-by: John Snow <jsnow@redhat.com>
26ba25
RH-Acked-by: Juan Quintela <quintela@redhat.com>
26ba25
26ba25
From: Xiao Guangrong <xiaoguangrong@tencent.com>
26ba25
26ba25
Abstract the common function control_save_page() to cleanup the code,
26ba25
no logic is changed
26ba25
26ba25
Reviewed-by: Peter Xu <peterx@redhat.com>
26ba25
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
26ba25
Signed-off-by: Xiao Guangrong <xiaoguangrong@tencent.com>
26ba25
Message-Id: <20180330075128.26919-6-xiaoguangrong@tencent.com>
26ba25
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
26ba25
(cherry picked from commit 059ff0fb29dd3a56ac2843676915efc279938c6b)
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 migration/ram.c | 174 +++++++++++++++++++++++++++++---------------------------
26ba25
 1 file changed, 89 insertions(+), 85 deletions(-)
26ba25
26ba25
diff --git a/migration/ram.c b/migration/ram.c
26ba25
index cd6d98a..8dc98a5 100644
26ba25
--- a/migration/ram.c
26ba25
+++ b/migration/ram.c
26ba25
@@ -975,6 +975,44 @@ static void ram_release_pages(const char *rbname, uint64_t offset, int pages)
26ba25
     ram_discard_range(rbname, offset, pages << TARGET_PAGE_BITS);
26ba25
 }
26ba25
 
26ba25
+/*
26ba25
+ * @pages: the number of pages written by the control path,
26ba25
+ *        < 0 - error
26ba25
+ *        > 0 - number of pages written
26ba25
+ *
26ba25
+ * Return true if the pages has been saved, otherwise false is returned.
26ba25
+ */
26ba25
+static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
26ba25
+                              int *pages)
26ba25
+{
26ba25
+    uint64_t bytes_xmit = 0;
26ba25
+    int ret;
26ba25
+
26ba25
+    *pages = -1;
26ba25
+    ret = ram_control_save_page(rs->f, block->offset, offset, TARGET_PAGE_SIZE,
26ba25
+                                &bytes_xmit);
26ba25
+    if (ret == RAM_SAVE_CONTROL_NOT_SUPP) {
26ba25
+        return false;
26ba25
+    }
26ba25
+
26ba25
+    if (bytes_xmit) {
26ba25
+        ram_counters.transferred += bytes_xmit;
26ba25
+        *pages = 1;
26ba25
+    }
26ba25
+
26ba25
+    if (ret == RAM_SAVE_CONTROL_DELAYED) {
26ba25
+        return true;
26ba25
+    }
26ba25
+
26ba25
+    if (bytes_xmit > 0) {
26ba25
+        ram_counters.normal++;
26ba25
+    } else if (bytes_xmit == 0) {
26ba25
+        ram_counters.duplicate++;
26ba25
+    }
26ba25
+
26ba25
+    return true;
26ba25
+}
26ba25
+
26ba25
 /**
26ba25
  * ram_save_page: send the given page to the stream
26ba25
  *
26ba25
@@ -991,56 +1029,36 @@ static void ram_release_pages(const char *rbname, uint64_t offset, int pages)
26ba25
 static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage)
26ba25
 {
26ba25
     int pages = -1;
26ba25
-    uint64_t bytes_xmit;
26ba25
-    ram_addr_t current_addr;
26ba25
     uint8_t *p;
26ba25
-    int ret;
26ba25
     bool send_async = true;
26ba25
     RAMBlock *block = pss->block;
26ba25
     ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
26ba25
+    ram_addr_t current_addr = block->offset + offset;
26ba25
 
26ba25
     p = block->host + offset;
26ba25
     trace_ram_save_page(block->idstr, (uint64_t)offset, p);
26ba25
 
26ba25
-    /* In doubt sent page as normal */
26ba25
-    bytes_xmit = 0;
26ba25
-    ret = ram_control_save_page(rs->f, block->offset,
26ba25
-                           offset, TARGET_PAGE_SIZE, &bytes_xmit);
26ba25
-    if (bytes_xmit) {
26ba25
-        ram_counters.transferred += bytes_xmit;
26ba25
-        pages = 1;
26ba25
+    if (control_save_page(rs, block, offset, &pages)) {
26ba25
+        return pages;
26ba25
     }
26ba25
 
26ba25
     XBZRLE_cache_lock();
26ba25
-
26ba25
-    current_addr = block->offset + offset;
26ba25
-
26ba25
-    if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
26ba25
-        if (ret != RAM_SAVE_CONTROL_DELAYED) {
26ba25
-            if (bytes_xmit > 0) {
26ba25
-                ram_counters.normal++;
26ba25
-            } else if (bytes_xmit == 0) {
26ba25
-                ram_counters.duplicate++;
26ba25
-            }
26ba25
-        }
26ba25
-    } else {
26ba25
-        pages = save_zero_page(rs, block, offset);
26ba25
-        if (pages > 0) {
26ba25
-            /* Must let xbzrle know, otherwise a previous (now 0'd) cached
26ba25
-             * page would be stale
26ba25
+    pages = save_zero_page(rs, block, offset);
26ba25
+    if (pages > 0) {
26ba25
+        /* Must let xbzrle know, otherwise a previous (now 0'd) cached
26ba25
+         * page would be stale
26ba25
+         */
26ba25
+        xbzrle_cache_zero_page(rs, current_addr);
26ba25
+        ram_release_pages(block->idstr, offset, pages);
26ba25
+    } else if (!rs->ram_bulk_stage &&
26ba25
+               !migration_in_postcopy() && migrate_use_xbzrle()) {
26ba25
+        pages = save_xbzrle_page(rs, &p, current_addr, block,
26ba25
+                                 offset, last_stage);
26ba25
+        if (!last_stage) {
26ba25
+            /* Can't send this cached data async, since the cache page
26ba25
+             * might get updated before it gets to the wire
26ba25
              */
26ba25
-            xbzrle_cache_zero_page(rs, current_addr);
26ba25
-            ram_release_pages(block->idstr, offset, pages);
26ba25
-        } else if (!rs->ram_bulk_stage &&
26ba25
-                   !migration_in_postcopy() && migrate_use_xbzrle()) {
26ba25
-            pages = save_xbzrle_page(rs, &p, current_addr, block,
26ba25
-                                     offset, last_stage);
26ba25
-            if (!last_stage) {
26ba25
-                /* Can't send this cached data async, since the cache page
26ba25
-                 * might get updated before it gets to the wire
26ba25
-                 */
26ba25
-                send_async = false;
26ba25
-            }
26ba25
+            send_async = false;
26ba25
         }
26ba25
     }
26ba25
 
26ba25
@@ -1175,63 +1193,49 @@ static int ram_save_compressed_page(RAMState *rs, PageSearchStatus *pss,
26ba25
                                     bool last_stage)
26ba25
 {
26ba25
     int pages = -1;
26ba25
-    uint64_t bytes_xmit = 0;
26ba25
     uint8_t *p;
26ba25
-    int ret;
26ba25
     RAMBlock *block = pss->block;
26ba25
     ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
26ba25
 
26ba25
     p = block->host + offset;
26ba25
 
26ba25
-    ret = ram_control_save_page(rs->f, block->offset,
26ba25
-                                offset, TARGET_PAGE_SIZE, &bytes_xmit);
26ba25
-    if (bytes_xmit) {
26ba25
-        ram_counters.transferred += bytes_xmit;
26ba25
-        pages = 1;
26ba25
+    if (control_save_page(rs, block, offset, &pages)) {
26ba25
+        return pages;
26ba25
     }
26ba25
-    if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
26ba25
-        if (ret != RAM_SAVE_CONTROL_DELAYED) {
26ba25
-            if (bytes_xmit > 0) {
26ba25
-                ram_counters.normal++;
26ba25
-            } else if (bytes_xmit == 0) {
26ba25
-                ram_counters.duplicate++;
26ba25
-            }
26ba25
+
26ba25
+    /* When starting the process of a new block, the first page of
26ba25
+     * the block should be sent out before other pages in the same
26ba25
+     * block, and all the pages in last block should have been sent
26ba25
+     * out, keeping this order is important, because the 'cont' flag
26ba25
+     * is used to avoid resending the block name.
26ba25
+     */
26ba25
+    if (block != rs->last_sent_block) {
26ba25
+        flush_compressed_data(rs);
26ba25
+        pages = save_zero_page(rs, block, offset);
26ba25
+        if (pages > 0) {
26ba25
+            ram_release_pages(block->idstr, offset, pages);
26ba25
+        } else {
26ba25
+            /*
26ba25
+             * Make sure the first page is sent out before other pages.
26ba25
+             *
26ba25
+             * we post it as normal page as compression will take much
26ba25
+             * CPU resource.
26ba25
+             */
26ba25
+            ram_counters.transferred += save_page_header(rs, rs->f, block,
26ba25
+                                            offset | RAM_SAVE_FLAG_PAGE);
26ba25
+            qemu_put_buffer_async(rs->f, p, TARGET_PAGE_SIZE,
26ba25
+                                  migrate_release_ram() &
26ba25
+                                  migration_in_postcopy());
26ba25
+            ram_counters.transferred += TARGET_PAGE_SIZE;
26ba25
+            ram_counters.normal++;
26ba25
+            pages = 1;
26ba25
         }
26ba25
     } else {
26ba25
-        /* When starting the process of a new block, the first page of
26ba25
-         * the block should be sent out before other pages in the same
26ba25
-         * block, and all the pages in last block should have been sent
26ba25
-         * out, keeping this order is important, because the 'cont' flag
26ba25
-         * is used to avoid resending the block name.
26ba25
-         */
26ba25
-        if (block != rs->last_sent_block) {
26ba25
-            flush_compressed_data(rs);
26ba25
-            pages = save_zero_page(rs, block, offset);
26ba25
-            if (pages > 0) {
26ba25
-                ram_release_pages(block->idstr, offset, pages);
26ba25
-            } else {
26ba25
-                /*
26ba25
-                 * Make sure the first page is sent out before other pages.
26ba25
-                 *
26ba25
-                 * we post it as normal page as compression will take much
26ba25
-                 * CPU resource.
26ba25
-                 */
26ba25
-                ram_counters.transferred += save_page_header(rs, rs->f, block,
26ba25
-                                                offset | RAM_SAVE_FLAG_PAGE);
26ba25
-                qemu_put_buffer_async(rs->f, p, TARGET_PAGE_SIZE,
26ba25
-                                      migrate_release_ram() &
26ba25
-                                      migration_in_postcopy());
26ba25
-                ram_counters.transferred += TARGET_PAGE_SIZE;
26ba25
-                ram_counters.normal++;
26ba25
-                pages = 1;
26ba25
-            }
26ba25
+        pages = save_zero_page(rs, block, offset);
26ba25
+        if (pages == -1) {
26ba25
+            pages = compress_page_with_multi_thread(rs, block, offset);
26ba25
         } else {
26ba25
-            pages = save_zero_page(rs, block, offset);
26ba25
-            if (pages == -1) {
26ba25
-                pages = compress_page_with_multi_thread(rs, block, offset);
26ba25
-            } else {
26ba25
-                ram_release_pages(block->idstr, offset, pages);
26ba25
-            }
26ba25
+            ram_release_pages(block->idstr, offset, pages);
26ba25
         }
26ba25
     }
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25