From nobody Wed Nov 5 08:27:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1533318550922556.8892244967889; Fri, 3 Aug 2018 10:49:10 -0700 (PDT) Received: from localhost ([::1]:52523 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fleCD-0001Br-Um for importer@patchew.org; Fri, 03 Aug 2018 13:49:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43734) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fleAJ-00007I-Ca for qemu-devel@nongnu.org; Fri, 03 Aug 2018 13:47:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fleAE-0008QU-H1 for qemu-devel@nongnu.org; Fri, 03 Aug 2018 13:47:03 -0400 Received: from relay.sw.ru ([185.231.240.75]:42014) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fleAE-0008PQ-87; Fri, 03 Aug 2018 13:46:58 -0400 Received: from vz-out.virtuozzo.com ([185.231.240.5] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fleAB-0003t9-2J; Fri, 03 Aug 2018 20:46:55 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 3 Aug 2018 20:46:50 +0300 Message-Id: <20180803174654.278336-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180803174654.278336-1-vsementsov@virtuozzo.com> References: <20180803174654.278336-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 2/6] dirty-bitmap: add bdrv_dirty_bitmap_next_dirty_area X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, famz@redhat.com, jcody@redhat.com, mreitz@redhat.com, den@openvz.org, pbonzini@redhat.com, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The function alters bdrv_dirty_iter_next_area(), which is wrong and less efficient (see next commit for description). Signed-off-by: Vladimir Sementsov-Ogievskiy --- include/block/dirty-bitmap.h | 3 +++ include/qemu/hbitmap.h | 15 +++++++++++++++ block/dirty-bitmap.c | 7 +++++++ util/hbitmap.c | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h index 5dc146abf3..c02be67564 100644 --- a/include/block/dirty-bitmap.h +++ b/include/block/dirty-bitmap.h @@ -100,6 +100,9 @@ BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverStat= e *bs, char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp= ); int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t star= t, uint64_t bytes); +bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, + uint64_t *offset, uint64_t end, + uint64_t *length); BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, Error **errp); diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index 259bfc2936..347a46ef48 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -303,6 +303,21 @@ unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi= ); */ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t start, uint64_t byte= s); =20 +/* hbitmap_next_dirty_area: + * @hb: The HBitmap to operate on + * @offset: in-out parameter. + * in: the offset to start from + * out: (if area found) start of found area + * @end: end of requested region. (*@offset + *@length) will be <=3D @end + * @length: length of found area + * + * If dirty area found within [@offset, @end), returns true and sets @offs= et + * and @length appropriately. Otherwise returns true and leaves @offset and + * @length unchanged. + */ +bool hbitmap_next_dirty_area(const HBitmap *hb, uint64_t *offset, + uint64_t end, uint64_t *length); + /* 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/block/dirty-bitmap.c b/block/dirty-bitmap.c index a9ee814da7..c24aa0e229 100644 --- a/block/dirty-bitmap.c +++ b/block/dirty-bitmap.c @@ -791,6 +791,13 @@ int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *b= itmap, uint64_t offset, return hbitmap_next_zero(bitmap->bitmap, offset, bytes); } =20 +bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, + uint64_t *offset, uint64_t end, + uint64_t *length) +{ + return hbitmap_next_dirty_area(bitmap->bitmap, offset, end, length); +} + void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap = *src, Error **errp) { diff --git a/util/hbitmap.c b/util/hbitmap.c index 73137c10a0..7bfcb878c1 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -53,6 +53,9 @@ */ =20 struct HBitmap { + /* Size of the bitmap, as requested in hbitmap_alloc. */ + uint64_t orig_size; + /* Number of total bits in the bottom level. */ uint64_t size; =20 @@ -238,6 +241,40 @@ int64_t hbitmap_next_zero(const HBitmap *hb, uint64_t = start, uint64_t bytes) return res; } =20 +bool hbitmap_next_dirty_area(const HBitmap *hb, uint64_t *offset, + uint64_t end, uint64_t *length) +{ + HBitmapIter hbi; + int64_t off1, off0; + uint32_t granularity =3D 1UL << hb->granularity; + + if (end =3D=3D 0) { + end =3D hb->orig_size; + } + + hbitmap_iter_init(&hbi, hb, *offset << hb->granularity); + off1 =3D hbitmap_iter_next(&hbi, true); + + if (off1 < 0 || off1 >=3D end) { + return false; + } + + if (off1 + granularity >=3D end) { + *offset =3D off1; + *length =3D end - off1; + return true; + } + + off0 =3D hbitmap_next_zero(hb, off1 + granularity, end); + if (off0 < 0) { + off0 =3D end; + } + + *offset =3D off1; + *length =3D off0 - off1; + return true; +} + bool hbitmap_empty(const HBitmap *hb) { return hb->count =3D=3D 0; @@ -659,6 +696,8 @@ HBitmap *hbitmap_alloc(uint64_t size, int granularity) HBitmap *hb =3D g_new0(struct HBitmap, 1); unsigned i; =20 + hb->orig_size =3D size; + assert(granularity >=3D 0 && granularity < 64); size =3D (size + (1ULL << granularity) - 1) >> granularity; assert(size <=3D ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE)); --=20 2.11.1