From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51586) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPTDC-00020W-MS for qemu-devel@nongnu.org; Sat, 30 Jan 2016 05:57:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aPTDA-0002Wq-DQ for qemu-devel@nongnu.org; Sat, 30 Jan 2016 05:57:02 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:36993 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPTDA-0002SV-1A for qemu-devel@nongnu.org; Sat, 30 Jan 2016 05:57:00 -0500 From: Vladimir Sementsov-Ogievskiy Date: Sat, 30 Jan 2016 13:56:29 +0300 Message-Id: <1454151394-52320-2-git-send-email-vsementsov@virtuozzo.com> In-Reply-To: <1454151394-52320-1-git-send-email-vsementsov@virtuozzo.com> References: <1454151394-52320-1-git-send-email-vsementsov@virtuozzo.com> Subject: [Qemu-devel] [PATCH 1/6] block dirty bitmap: add next_zero function List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, Vladimir Sementsov-Ogievskiy , famz@redhat.com, den@virtuozzo.com, armbru@redhat.com, jsnow@redhat.com The function searches for next zero bit (corresponding to next not-dirty bit). Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/dirty-bitmap.c | 5 +++++ include/block/dirty-bitmap.h | 2 ++ include/qemu/hbitmap.h | 8 ++++++++ util/hbitmap.c | 26 ++++++++++++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c index 919ce10..6da27d9 100644 --- a/block/dirty-bitmap.c +++ b/block/dirty-bitmap.c @@ -490,3 +490,8 @@ int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) { return hbitmap_count(bitmap->bitmap); } + +int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t start) +{ + return hbitmap_next_zero(bitmap->bitmap, start); +} diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h index 6ba5bec..0033942 100644 --- a/include/block/dirty-bitmap.h +++ b/include/block/dirty-bitmap.h @@ -68,4 +68,6 @@ void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap, bool finish); void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap); +int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t start); + #endif diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index 00dbb60..852e423 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -259,6 +259,14 @@ void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first); */ unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi); +/* hbitmap_next_zero: + * @hb: The HBitmap to operate on + * @start: The bit to start from. + * + * Find next not dirty bit. + */ +int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start); + /* hbitmap_create_meta: * Create a "meta" hbitmap to track dirtiness of the bits in this HBitmap. * The caller owns the created bitmap and must call hbitmap_free_meta(hb) to diff --git a/util/hbitmap.c b/util/hbitmap.c index 1e49ab7..bcddca9 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -169,6 +169,32 @@ void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first) } } +int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start) +{ + size_t pos = (start >> hb->granularity) >> BITS_PER_LEVEL; + unsigned long *last_lev = hb->levels[HBITMAP_LEVELS - 1]; + uint64_t sz = hb->sizes[HBITMAP_LEVELS - 1]; + unsigned long cur = last_lev[pos]; + unsigned start_bit_offset = + (start >> hb->granularity) & (BITS_PER_LONG - 1); + cur |= (1UL << start_bit_offset) - 1; + + if (cur == (unsigned long)-1) { + do { + pos++; + } while (pos < sz && last_lev[pos] == (unsigned long)-1); + + if (pos >= sz) { + return -1; + } + + cur = last_lev[pos]; + } + + return MIN((pos << BITS_PER_LEVEL) + ctol(cur), hb->size - 1) + << hb->granularity; +} + bool hbitmap_empty(const HBitmap *hb) { return hb->count == 0; -- 1.8.3.1