qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
	Kevin Wolf <kwolf@redhat.com>, John Snow <jsnow@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Jeff Cody <jcody@redhat.com>
Subject: [Qemu-devel] [PATCH v5 09/14] block/dirty-bitmap: Add bdrv_dirty_iter_next_area
Date: Wed, 13 Jun 2018 20:18:18 +0200	[thread overview]
Message-ID: <20180613181823.13618-10-mreitz@redhat.com> (raw)
In-Reply-To: <20180613181823.13618-1-mreitz@redhat.com>

This new function allows to look for a consecutively dirty area in a
dirty bitmap.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
---
 include/block/dirty-bitmap.h |  2 ++
 block/dirty-bitmap.c         | 55 ++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h
index 02e0cbabd2..288dc6adb6 100644
--- a/include/block/dirty-bitmap.h
+++ b/include/block/dirty-bitmap.h
@@ -82,6 +82,8 @@ void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
                                     int64_t offset, int64_t bytes);
 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter);
+bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
+                               uint64_t *offset, int *bytes);
 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *hbi, int64_t offset);
 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap);
 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap);
diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c
index cedb971765..db1782ec1f 100644
--- a/block/dirty-bitmap.c
+++ b/block/dirty-bitmap.c
@@ -522,6 +522,61 @@ int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
     return hbitmap_iter_next(&iter->hbi, true);
 }
 
+/**
+ * Return the next consecutively dirty area in the dirty bitmap
+ * belonging to the given iterator @iter.
+ *
+ * @max_offset: Maximum value that may be returned for
+ *              *offset + *bytes
+ * @offset:     Will contain the start offset of the next dirty area
+ * @bytes:      Will contain the length of the next dirty area
+ *
+ * Returns: True if a dirty area could be found before max_offset
+ *          (which means that *offset and *bytes then contain valid
+ *          values), false otherwise.
+ *
+ * Note that @iter is never advanced if false is returned.  If an area
+ * is found (which means that true is returned), it will be advanced
+ * past that area.
+ */
+bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
+                               uint64_t *offset, int *bytes)
+{
+    uint32_t granularity = bdrv_dirty_bitmap_granularity(iter->bitmap);
+    uint64_t gran_max_offset;
+    int64_t ret;
+    int size;
+
+    if (max_offset == iter->bitmap->size) {
+        /* If max_offset points to the image end, round it up by the
+         * bitmap granularity */
+        gran_max_offset = ROUND_UP(max_offset, granularity);
+    } else {
+        gran_max_offset = max_offset;
+    }
+
+    ret = hbitmap_iter_next(&iter->hbi, false);
+    if (ret < 0 || ret + granularity > gran_max_offset) {
+        return false;
+    }
+
+    *offset = ret;
+    size = 0;
+
+    assert(granularity <= INT_MAX);
+
+    do {
+        /* Advance iterator */
+        ret = hbitmap_iter_next(&iter->hbi, true);
+        size += granularity;
+    } while (ret + granularity <= gran_max_offset &&
+             hbitmap_iter_next(&iter->hbi, false) == ret + granularity &&
+             size <= INT_MAX - granularity);
+
+    *bytes = MIN(size, max_offset - *offset);
+    return true;
+}
+
 /* Called within bdrv_dirty_bitmap_lock..unlock */
 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
                                   int64_t offset, int64_t bytes)
-- 
2.17.1

  parent reply	other threads:[~2018-06-13 18:19 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-13 18:18 [Qemu-devel] [PATCH v5 00/14] block/mirror: Add active-sync mirroring Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 01/14] block/mirror: Pull out mirror_perform() Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 02/14] block/mirror: Convert to coroutines Max Reitz
2018-06-14 15:22   ` Kevin Wolf
2018-06-18 14:03     ` Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 03/14] block/mirror: Use CoQueue to wait on in-flight ops Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 04/14] block/mirror: Wait for in-flight op conflicts Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 05/14] block/mirror: Use source as a BdrvChild Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 06/14] block: Generalize should_update_child() rule Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 07/14] hbitmap: Add @advance param to hbitmap_iter_next() Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 08/14] test-hbitmap: Add non-advancing iter_next tests Max Reitz
2018-06-13 18:18 ` Max Reitz [this message]
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 10/14] block/mirror: Add MirrorBDSOpaque Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 11/14] job: Add job_progress_increase_remaining() Max Reitz
2018-06-14 15:50   ` Kevin Wolf
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 12/14] block/mirror: Add active mirroring Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 13/14] block/mirror: Add copy mode QAPI interface Max Reitz
2018-06-13 18:18 ` [Qemu-devel] [PATCH v5 14/14] iotests: Add test for active mirroring Max Reitz
2018-06-18 15:11 ` [Qemu-devel] [PATCH v5 00/14] block/mirror: Add active-sync mirroring Max Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180613181823.13618-10-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=jcody@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).