Blame SOURCES/kvm-hbitmap-Add-advance-param-to-hbitmap_iter_next.patch

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