26ba25
From d3cab6731c84ccc36d7a2655451486d1877afcf0 Mon Sep 17 00:00:00 2001
26ba25
From: John Snow <jsnow@redhat.com>
26ba25
Date: Tue, 20 Nov 2018 18:18:07 +0000
26ba25
Subject: [PATCH 13/35] hbitmap: Add @advance param to hbitmap_iter_next()
26ba25
26ba25
RH-Author: John Snow <jsnow@redhat.com>
26ba25
Message-id: <20181120181828.15132-4-jsnow@redhat.com>
26ba25
Patchwork-id: 83063
26ba25
O-Subject: [RHEL8/rhel qemu-kvm PATCH 03/24] hbitmap: Add @advance param to hbitmap_iter_next()
26ba25
Bugzilla: 1518989
26ba25
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
26ba25
From: Max Reitz <mreitz@redhat.com>
26ba25
26ba25
This new parameter allows the caller to just query the next dirty
26ba25
position without moving the iterator.
26ba25
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
Reviewed-by: Fam Zheng <famz@redhat.com>
26ba25
Reviewed-by: John Snow <jsnow@redhat.com>
26ba25
Message-id: 20180613181823.13618-8-mreitz@redhat.com
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit a33fbb4f8b64226becf502a123733776ce319b24)
26ba25
Signed-off-by: John Snow <jsnow@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 block/backup.c         |  2 +-
26ba25
 block/dirty-bitmap.c   |  2 +-
26ba25
 include/qemu/hbitmap.h |  5 ++++-
26ba25
 tests/test-hbitmap.c   | 26 +++++++++++++-------------
26ba25
 util/hbitmap.c         | 10 +++++++---
26ba25
 5 files changed, 26 insertions(+), 19 deletions(-)
26ba25
26ba25
diff --git a/block/backup.c b/block/backup.c
26ba25
index 524e0ff..ac17db6 100644
26ba25
--- a/block/backup.c
26ba25
+++ b/block/backup.c
26ba25
@@ -409,7 +409,7 @@ static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
26ba25
     HBitmapIter hbi;
26ba25
 
26ba25
     hbitmap_iter_init(&hbi, job->copy_bitmap, 0);
26ba25
-    while ((cluster = hbitmap_iter_next(&hbi)) != -1) {
26ba25
+    while ((cluster = hbitmap_iter_next(&hbi, true)) != -1) {
26ba25
         do {
26ba25
             if (yield_and_check(job)) {
26ba25
                 return 0;
26ba25
diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c
26ba25
index dac8d74..236dce1 100644
26ba25
--- a/block/dirty-bitmap.c
26ba25
+++ b/block/dirty-bitmap.c
26ba25
@@ -525,7 +525,7 @@ void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
26ba25
 
26ba25
 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
26ba25
 {
26ba25
-    return hbitmap_iter_next(&iter->hbi);
26ba25
+    return hbitmap_iter_next(&iter->hbi, true);
26ba25
 }
26ba25
 
26ba25
 /* Called within bdrv_dirty_bitmap_lock..unlock */
26ba25
diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h
26ba25
index 6b6490e..ddca52c 100644
26ba25
--- a/include/qemu/hbitmap.h
26ba25
+++ b/include/qemu/hbitmap.h
26ba25
@@ -324,11 +324,14 @@ void hbitmap_free_meta(HBitmap *hb);
26ba25
 /**
26ba25
  * hbitmap_iter_next:
26ba25
  * @hbi: HBitmapIter to operate on.
26ba25
+ * @advance: If true, advance the iterator.  Otherwise, the next call
26ba25
+ *           of this function will return the same result (if that
26ba25
+ *           position is still dirty).
26ba25
  *
26ba25
  * Return the next bit that is set in @hbi's associated HBitmap,
26ba25
  * or -1 if all remaining bits are zero.
26ba25
  */
26ba25
-int64_t hbitmap_iter_next(HBitmapIter *hbi);
26ba25
+int64_t hbitmap_iter_next(HBitmapIter *hbi, bool advance);
26ba25
 
26ba25
 /**
26ba25
  * hbitmap_iter_next_word:
26ba25
diff --git a/tests/test-hbitmap.c b/tests/test-hbitmap.c
26ba25
index f29631f..f2158f7 100644
26ba25
--- a/tests/test-hbitmap.c
26ba25
+++ b/tests/test-hbitmap.c
26ba25
@@ -46,7 +46,7 @@ static void hbitmap_test_check(TestHBitmapData *data,
26ba25
 
26ba25
     i = first;
26ba25
     for (;;) {
26ba25
-        next = hbitmap_iter_next(&hbi);
26ba25
+        next = hbitmap_iter_next(&hbi, true);
26ba25
         if (next < 0) {
26ba25
             next = data->size;
26ba25
         }
26ba25
@@ -435,25 +435,25 @@ static void test_hbitmap_iter_granularity(TestHBitmapData *data,
26ba25
     /* Note that hbitmap_test_check has to be invoked manually in this test.  */
26ba25
     hbitmap_test_init(data, 131072 << 7, 7);
26ba25
     hbitmap_iter_init(&hbi, data->hb, 0);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), <, 0);
26ba25
 
26ba25
     hbitmap_test_set(data, ((L2 + L1 + 1) << 7) + 8, 8);
26ba25
     hbitmap_iter_init(&hbi, data->hb, 0);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), ==, (L2 + L1 + 1) << 7);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), ==, (L2 + L1 + 1) << 7);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), <, 0);
26ba25
 
26ba25
     hbitmap_iter_init(&hbi, data->hb, (L2 + L1 + 2) << 7);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), <, 0);
26ba25
 
26ba25
     hbitmap_test_set(data, (131072 << 7) - 8, 8);
26ba25
     hbitmap_iter_init(&hbi, data->hb, 0);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), ==, (L2 + L1 + 1) << 7);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), ==, 131071 << 7);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), ==, (L2 + L1 + 1) << 7);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), ==, 131071 << 7);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), <, 0);
26ba25
 
26ba25
     hbitmap_iter_init(&hbi, data->hb, (L2 + L1 + 2) << 7);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), ==, 131071 << 7);
26ba25
-    g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), ==, 131071 << 7);
26ba25
+    g_assert_cmpint(hbitmap_iter_next(&hbi, true), <, 0);
26ba25
 }
26ba25
 
26ba25
 static void hbitmap_test_set_boundary_bits(TestHBitmapData *data, ssize_t diff)
26ba25
@@ -893,7 +893,7 @@ static void test_hbitmap_serialize_zeroes(TestHBitmapData *data,
26ba25
     for (i = 0; i < num_positions; i++) {
26ba25
         hbitmap_deserialize_zeroes(data->hb, positions[i], min_l1, true);
26ba25
         hbitmap_iter_init(&iter, data->hb, 0);
26ba25
-        next = hbitmap_iter_next(&iter);
26ba25
+        next = hbitmap_iter_next(&iter, true);
26ba25
         if (i == num_positions - 1) {
26ba25
             g_assert_cmpint(next, ==, -1);
26ba25
         } else {
26ba25
@@ -919,10 +919,10 @@ static void test_hbitmap_iter_and_reset(TestHBitmapData *data,
26ba25
 
26ba25
     hbitmap_iter_init(&hbi, data->hb, BITS_PER_LONG - 1);
26ba25
 
26ba25
-    hbitmap_iter_next(&hbi);
26ba25
+    hbitmap_iter_next(&hbi, true);
26ba25
 
26ba25
     hbitmap_reset_all(data->hb);
26ba25
-    hbitmap_iter_next(&hbi);
26ba25
+    hbitmap_iter_next(&hbi, true);
26ba25
 }
26ba25
 
26ba25
 static void test_hbitmap_next_zero_check(TestHBitmapData *data, int64_t start)
26ba25
diff --git a/util/hbitmap.c b/util/hbitmap.c
26ba25
index 58a2c93..bcd3040 100644
26ba25
--- a/util/hbitmap.c
26ba25
+++ b/util/hbitmap.c
26ba25
@@ -141,7 +141,7 @@ unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi)
26ba25
     return cur;
26ba25
 }
26ba25
 
26ba25
-int64_t hbitmap_iter_next(HBitmapIter *hbi)
26ba25
+int64_t hbitmap_iter_next(HBitmapIter *hbi, bool advance)
26ba25
 {
26ba25
     unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1] &
26ba25
             hbi->hb->levels[HBITMAP_LEVELS - 1][hbi->pos];
26ba25
@@ -154,8 +154,12 @@ int64_t hbitmap_iter_next(HBitmapIter *hbi)
26ba25
         }
26ba25
     }
26ba25
 
26ba25
-    /* The next call will resume work from the next bit.  */
26ba25
-    hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
26ba25
+    if (advance) {
26ba25
+        /* The next call will resume work from the next bit.  */
26ba25
+        hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
26ba25
+    } else {
26ba25
+        hbi->cur[HBITMAP_LEVELS - 1] = cur;
26ba25
+    }
26ba25
     item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur);
26ba25
 
26ba25
     return item << hbi->granularity;
26ba25
-- 
26ba25
1.8.3.1
26ba25