Skip to contentSkip to conversation

Some modifications about ram_save_host_page()

PATCH Superseded Kunkun Jiang 3 participants 9 messages Mar 1, 2021 – Mar 3, 2021

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Some modifications about ram_save_host_page()

Hi,

This series include patches as below:
Patch 1-2:
- modified the comment and code of ram_save_host_page() to make them match each other

Patch 3:
- optimized ram_save_host_page() by using migration_bitmap_find_dirty() to find dirty pages

History:

v1 -> v2:
- Modify ram_save_host_page() comment [David Edmondson]
- Remove 'goto' [David Edmondson]

Kunkun Jiang (3):
migration/ram: Modify the code comment of ram_save_host_page() migration/ram: Modify ram_save_host_page() to match the comment migration/ram: Optimize ram_save_host_page()

 migration/ram.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
Signature · 1 line
2.23.0
#1 lore raw

PATCH v2 1/3 migration/ram: Modify the code comment of ram_save_host_page()

The ram_save_host_page() has been modified several times since its birth. But the comment hasn't been modified as it should be. It'd better to modify the comment to explain ram_save_host_page() more clearly.

---
 migration/ram.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
migration/ram.c +6 -8
index 72143da0ac..24967cb970 100644
@@ -1970,15 +1970,13 @@ static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss,
 }
 
 /**
- * ram_save_host_page: save a whole host page
+ * ram_save_host_page: save a whole host page or the rest of a RAMBlock
  *
- * Starting at *offset send pages up to the end of the current host
- * page. It's valid for the initial offset to point into the middle of
- * a host page in which case the remainder of the hostpage is sent.
- * Only dirty target pages are sent. Note that the host page size may
- * be a huge page for this block.
- * The saving stops at the boundary of the used_length of the block
- * if the RAMBlock isn't a multiple of the host page size.
+ * Send dirty pages between pss->page and either the end of that page
+ * or the used_length of the RAMBlock, whichever is smaller.
+ *
+ * Note that if the host page is a huge page, pss->page may be in the
+ * middle of that page.
  *
  * Returns the number of pages written or negative on error
  *
Signature · 1 line
2.23.0
#2 lore raw

Re: [PATCH v2 1/3] migration/ram: Modify the code comment of ram_save_host_page()

Quoted 4 lines · Kunkun Jiang

The ram_save_host_page() has been modified several times since its birth. But the comment hasn't been modified as it should be. It'd better to modify the comment to explain ram_save_host_page() more clearly.

I don't think that it's reasonable for me to send Reviewed-by for this, given that I suggested the text.

Could someone else check the sense and correctness?

Quoted 34 lines
---
 migration/ram.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
migration/ram.c +6 -8
index 72143da0ac..24967cb970 100644
@@ -1970,15 +1970,13 @@ static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss,
 }
 
 /**
- * ram_save_host_page: save a whole host page
+ * ram_save_host_page: save a whole host page or the rest of a RAMBlock
  *
- * Starting at *offset send pages up to the end of the current host
- * page. It's valid for the initial offset to point into the middle of
- * a host page in which case the remainder of the hostpage is sent.
- * Only dirty target pages are sent. Note that the host page size may
- * be a huge page for this block.
- * The saving stops at the boundary of the used_length of the block
- * if the RAMBlock isn't a multiple of the host page size.
+ * Send dirty pages between pss->page and either the end of that page
+ * or the used_length of the RAMBlock, whichever is smaller.
+ *
+ * Note that if the host page is a huge page, pss->page may be in the
+ * middle of that page.
  *
  * Returns the number of pages written or negative on error
  *
Signature · 1 line
2.23.0

dme.

Signature · 1 line
Leaves are falling all around, it's time I was on my way.
#3 lore raw

PATCH v2 2/3 migration/ram: Modify ram_save_host_page() to match the comment

In reply to Kunkun Jiang

According to the comment, when the host page is a huge page, the migration_rate_limit() should be executed. If not, this function can be omitted to save time.

---
 migration/ram.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
migration/ram.c +3 -1
index 24967cb970..3a9115b6dc 100644
@@ -2014,7 +2014,9 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
         pages += tmppages;
         pss->page++;
         /* Allow rate limiting to happen in the middle of huge pages */
-        migration_rate_limit();
+        if (pagesize_bits > 1) {
+            migration_rate_limit();
+        }
     } while ((pss->page & (pagesize_bits - 1)) &&
              offset_in_ramblock(pss->block,
                                 ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));
Signature · 1 line
2.23.0
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] migration/ram: Modify ram_save_host_page() to match the comment

Quoted 6 lines · Kunkun Jiang

According to the comment, when the host page is a huge page, the migration_rate_limit() should be executed. If not, this function can be omitted to save time.

Quoted 21 lines
---
 migration/ram.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
migration/ram.c +3 -1
index 24967cb970..3a9115b6dc 100644
@@ -2014,7 +2014,9 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
         pages += tmppages;
         pss->page++;
         /* Allow rate limiting to happen in the middle of huge pages */
-        migration_rate_limit();
+        if (pagesize_bits > 1) {
+            migration_rate_limit();
+        }
     } while ((pss->page & (pagesize_bits - 1)) &&
              offset_in_ramblock(pss->block,
                                 ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));
Signature · 1 line
2.23.0

dme.

Signature · 1 line
Please don't stand so close to me.
#5 lore raw

PATCH v2 3/3 migration/ram: Optimize ram_save_host_page()

In reply to Kunkun Jiang

Starting from pss->page, ram_save_host_page() will check every page and send the dirty pages up to the end of the current host page or the boundary of used_length of the block. If the host page size is a huge page, the step "check" will take a lot of time.

This will improve performance to use migration_bitmap_find_dirty().

---
 migration/ram.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
migration/ram.c +7 -5
index 3a9115b6dc..a1374db356 100644
@@ -1991,6 +1991,8 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
     int tmppages, pages = 0;
     size_t pagesize_bits =
         qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
+    unsigned long hostpage_boundary =
+        QEMU_ALIGN_UP(pss->page + 1, pagesize_bits);
     unsigned long start_page = pss->page;
     int res;
 
@@ -2002,7 +2004,7 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
     do {
         /* Check the pages is dirty and if it is send it */
         if (!migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
-            pss->page++;
+            pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
             continue;
         }
 
@@ -2012,16 +2014,16 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
         }
 
         pages += tmppages;
-        pss->page++;
+        pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
         /* Allow rate limiting to happen in the middle of huge pages */
         if (pagesize_bits > 1) {
             migration_rate_limit();
         }
-    } while ((pss->page & (pagesize_bits - 1)) &&
+    } while ((pss->page < hostpage_boundary) &&
              offset_in_ramblock(pss->block,
                                 ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));
-    /* The offset we leave with is the last one we looked at */
-    pss->page--;
+    /* The offset we leave with is the min boundary of host page and block */
+    pss->page = MIN(pss->page, hostpage_boundary) - 1;
 
     res = ram_save_release_protection(rs, pss, start_page);
     return (res < 0 ? res : pages);
Signature · 1 line
2.23.0
#6 lore raw

Re: [PATCH v2 3/3] migration/ram: Optimize ram_save_host_page()

Quoted 6 lines · Kunkun Jiang

Starting from pss->page, ram_save_host_page() will check every page and send the dirty pages up to the end of the current host page or the boundary of used_length of the block. If the host page size is a huge page, the step "check" will take a lot of time.

This will improve performance to use migration_bitmap_find_dirty().

This is cleaner, thank you.

I was hoping to just invert the body of the loop - something like (completely untested):

do {
int pages_this_iteration = 0;

  /* Check if the page is dirty and, if so, send it. */
  if (migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
    pages_this_iteration = ram_save_target_page(rs, pss, last_stage);
    if (pages_this_iteration < 0) {
      return pages_this_iteration;
    }
    pages += pages_this_iteration;

    /*
     * Allow rate limiting to happen in the middle of huge pages if
     * the current iteration sent something.
     */
    if (pagesize_bits > 1 && pages_this_iteration > 0) {
      migration_rate_limit();
    }
  }
  pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
 } while ((pss->page < hostpage_boundary) &&
          offset_in_ramblock(pss->block,
                             ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));

/* The offset we leave with is the min boundary of host page and block */ pss->page = MIN(pss->page, hostpage_boundary) - 1;

Quoted 51 lines
---
 migration/ram.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
migration/ram.c +7 -5
index 3a9115b6dc..a1374db356 100644
@@ -1991,6 +1991,8 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
     int tmppages, pages = 0;
     size_t pagesize_bits =
         qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
+    unsigned long hostpage_boundary =
+        QEMU_ALIGN_UP(pss->page + 1, pagesize_bits);
     unsigned long start_page = pss->page;
     int res;
 
@@ -2002,7 +2004,7 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
     do {
         /* Check the pages is dirty and if it is send it */
         if (!migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
-            pss->page++;
+            pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
             continue;
         }
 
@@ -2012,16 +2014,16 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
         }
 
         pages += tmppages;
-        pss->page++;
+        pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
         /* Allow rate limiting to happen in the middle of huge pages */
         if (pagesize_bits > 1) {
             migration_rate_limit();
         }
-    } while ((pss->page & (pagesize_bits - 1)) &&
+    } while ((pss->page < hostpage_boundary) &&
              offset_in_ramblock(pss->block,
                                 ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));
-    /* The offset we leave with is the last one we looked at */
-    pss->page--;
+    /* The offset we leave with is the min boundary of host page and block */
+    pss->page = MIN(pss->page, hostpage_boundary) - 1;
 
     res = ram_save_release_protection(rs, pss, start_page);
     return (res < 0 ? res : pages);
Signature · 1 line
2.23.0

dme.

Signature · 1 line
Don't you know you're never going to get to France.
#7 lore raw

Re: [PATCH v2 3/3] migration/ram: Optimize ram_save_host_page()

Quoted 7 lines · David Edmondson
Quoted 5 lines · Kunkun Jiang

Starting from pss->page, ram_save_host_page() will check every page and send the dirty pages up to the end of the current host page or the boundary of used_length of the block. If the host page size is a huge page, the step "check" will take a lot of time.

Quoted 5 lines
Quoted 1 line

This will improve performance to use migration_bitmap_find_dirty().

This is cleaner, thank you.

I was hoping to just invert the body of the loop - something like (completely untested):

Sorry for my misunderstanding.
I will improve it in the next version.

Quoted 12 lines
do {
   int pages_this_iteration = 0;

   /* Check if the page is dirty and, if so, send it. */
   if (migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
     pages_this_iteration = ram_save_target_page(rs, pss, last_stage);
     if (pages_this_iteration < 0) {
       return pages_this_iteration;
     }

     pages += pages_this_iteration;
Quoted 7 lines
     /*
      * Allow rate limiting to happen in the middle of huge pages if
      * the current iteration sent something.
      */
     if (pagesize_bits > 1 && pages_this_iteration > 0) {
       migration_rate_limit();
     }

I missed the case that the value of pages_this_iteration is 0. 😅

Quoted 7 lines
   }
   pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
  } while ((pss->page < hostpage_boundary) &&
           offset_in_ramblock(pss->block,
                              ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));

/* The offset we leave with is the min boundary of host page and block */ pss->page = MIN(pss->page, hostpage_boundary) - 1;

Best Regards.

Kunkun Jiang

Quoted 52 lines
Quoted 51 lines
---
  migration/ram.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
migration/ram.c +7 -5
index 3a9115b6dc..a1374db356 100644
@@ -1991,6 +1991,8 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
      int tmppages, pages = 0;
      size_t pagesize_bits =
          qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
+    unsigned long hostpage_boundary =
+        QEMU_ALIGN_UP(pss->page + 1, pagesize_bits);
      unsigned long start_page = pss->page;
      int res;
 
@@ -2002,7 +2004,7 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
      do {
          /* Check the pages is dirty and if it is send it */
          if (!migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
-            pss->page++;
+            pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
              continue;
          }
 
@@ -2012,16 +2014,16 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
          }
 
          pages += tmppages;
-        pss->page++;
+        pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
          /* Allow rate limiting to happen in the middle of huge pages */
          if (pagesize_bits > 1) {
              migration_rate_limit();
          }
-    } while ((pss->page & (pagesize_bits - 1)) &&
+    } while ((pss->page < hostpage_boundary) &&
               offset_in_ramblock(pss->block,
                                  ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));
-    /* The offset we leave with is the last one we looked at */
-    pss->page--;
+    /* The offset we leave with is the min boundary of host page and block */
+    pss->page = MIN(pss->page, hostpage_boundary) - 1;
 
      res = ram_save_release_protection(rs, pss, start_page);
      return (res < 0 ? res : pages);
Signature · 1 line
2.23.0

dme.

#8 lore raw

Re: [PATCH v2 3/3] migration/ram: Optimize ram_save_host_page()

Quoted 8 lines · Kunkun Jiang
Quoted 7 lines · David Edmondson
Quoted 5 lines · Kunkun Jiang

Starting from pss->page, ram_save_host_page() will check every page and send the dirty pages up to the end of the current host page or the boundary of used_length of the block. If the host page size is a huge page, the step "check" will take a lot of time.

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

This will improve performance to use migration_bitmap_find_dirty().

This is cleaner, thank you.

I was hoping to just invert the body of the loop - something like (completely untested):

Sorry for my misunderstanding.

No, I explained myself poorly.

Quoted 13 lines

I will improve it in the next version.

Quoted 12 lines
do {
   int pages_this_iteration = 0;

   /* Check if the page is dirty and, if so, send it. */
   if (migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
     pages_this_iteration = ram_save_target_page(rs, pss, last_stage);
     if (pages_this_iteration < 0) {
       return pages_this_iteration;
     }

     pages += pages_this_iteration;
Quoted 8 lines
Quoted 7 lines
     /*
      * Allow rate limiting to happen in the middle of huge pages if
      * the current iteration sent something.
      */
     if (pagesize_bits > 1 && pages_this_iteration > 0) {
       migration_rate_limit();
     }

I missed the case that the value of pages_this_iteration is 0. 😅

I don't think that your version was wrong, because it returned early from the loop if there were no candidate pages.

Quoted 8 lines
Quoted 7 lines
   }
   pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
  } while ((pss->page < hostpage_boundary) &&
           offset_in_ramblock(pss->block,
                              ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));

/* The offset we leave with is the min boundary of host page and block */ pss->page = MIN(pss->page, hostpage_boundary) - 1;

Quoted 56 lines

Best Regards.

Kunkun Jiang

Quoted 52 lines
Quoted 51 lines
---
  migration/ram.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
migration/ram.c +7 -5
index 3a9115b6dc..a1374db356 100644
@@ -1991,6 +1991,8 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
      int tmppages, pages = 0;
      size_t pagesize_bits =
          qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
+    unsigned long hostpage_boundary =
+        QEMU_ALIGN_UP(pss->page + 1, pagesize_bits);
      unsigned long start_page = pss->page;
      int res;
 
@@ -2002,7 +2004,7 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
      do {
          /* Check the pages is dirty and if it is send it */
          if (!migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
-            pss->page++;
+            pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
              continue;
          }
 
@@ -2012,16 +2014,16 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
          }
 
          pages += tmppages;
-        pss->page++;
+        pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
          /* Allow rate limiting to happen in the middle of huge pages */
          if (pagesize_bits > 1) {
              migration_rate_limit();
          }
-    } while ((pss->page & (pagesize_bits - 1)) &&
+    } while ((pss->page < hostpage_boundary) &&
               offset_in_ramblock(pss->block,
                                  ((ram_addr_t)pss->page) << TARGET_PAGE_BITS));
-    /* The offset we leave with is the last one we looked at */
-    pss->page--;
+    /* The offset we leave with is the min boundary of host page and block */
+    pss->page = MIN(pss->page, hostpage_boundary) - 1;
 
      res = ram_save_release_protection(rs, pss, start_page);
      return (res < 0 ? res : pages);
Signature · 1 line
2.23.0

dme.

dme.

Signature · 1 line
Too much information, running through my brain.