From: John Snow <jsnow@redhat.com>
To: Stefan Hajnoczi <stefanha@gmail.com>
Cc: kwolf@redhat.com, famz@redhat.com, qemu-block@nongnu.org,
qemu-devel@nongnu.org, armbru@redhat.com,
vsementsov@parallels.com, stefanha@redhat.com, mreitz@redhat.com
Subject: Re: [Qemu-devel] [PATCH v2 14/17] block: Resize bitmaps on bdrv_truncate
Date: Wed, 11 Mar 2015 13:04:29 -0400 [thread overview]
Message-ID: <5500759D.7000203@redhat.com> (raw)
In-Reply-To: <20150311161802.GK10493@stefanha-thinkpad.redhat.com>
On 03/11/2015 12:18 PM, Stefan Hajnoczi wrote:
> On Mon, Mar 02, 2015 at 06:20:00PM -0500, John Snow wrote:
>> +static void dirty_bitmap_truncate(BdrvDirtyBitmap *bitmap, uint64_t size)
>> +{
>> + /* Should only be frozen during a block backup job, which should have
>> + * blocked any resize actions. */
>> + assert(!bdrv_dirty_bitmap_frozen(bitmap));
>> + hbitmap_truncate(bitmap->bitmap, size);
>> +}
>> +
>> +void bdrv_dirty_bitmap_truncate(BlockDriverState *bs)
>> +{
>> + BdrvDirtyBitmap *bitmap;
>> + uint64_t size = bdrv_nb_sectors(bs);
>> +
>> + QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
>> + if (bdrv_dirty_bitmap_frozen(bitmap)) {
>> + continue;
>> + }
>> + dirty_bitmap_truncate(bitmap, size);
>
> If you inline this function here then the discussion about assert() vs
> skipping frozen bitmaps goes away. Why is dirty_bitmap_truncate() a
> function?
>
Symmetry with other bitmap functions.
>> + }
>> +}
>
> Why is bdrv_dirty_bitmap_truncate() a public API? I expected this code
> to be inline or called as a static function by bdrv_truncate().
>
OK, fixing that.
>> /**
>> + * hbitmap_truncate:
>> + * @hb: The bitmap to change the size of.
>> + * @size: The number of elements to change the bitmap to accommodate.
>> + *
>> + * truncate or grow an existing bitmap to accommodate a new number of elements.
>> + * This may invalidate existing HBitmapIterators.
>> + */
>> +void hbitmap_truncate(HBitmap *hb, uint64_t size);
>
> Please include a tests/test-hbitmap.c test case.
>
> Interesting cases:
> 1. New size equals old size (odd but possible)
> 2. Growing less than sizeof(unsigned long)
> 3. Growing more than sizeof(unsigned long)
> 4. Shrinking less than sizeof(unsigned long)
> 5. Shrinking more than sizeof(unsigned long)
>
;_; OK, you're right...
>> +void hbitmap_truncate(HBitmap *hb, uint64_t size)
>> +{
>> + bool truncate;
>> + unsigned i;
>> + uint64_t num_elements = size;
>> + uint64_t old;
>> +
>> + /* Size comes in as logical elements, adjust for granularity. */
>> + size = (size + (1ULL << hb->granularity) - 1) >> hb->granularity;
>> + assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
>> + truncate = size < hb->size;
>
> Here "truncate" means "shrink".
>
> "shrink" is a clearer name since the function name is already "truncate"
> but that concept includes both increasing or decreasing size.
>
Yes, fair enough. Was relying on what I consider the colloquial
definition of truncate.
>> +
>> + if (size == hb->size) {
>> + /* A hard day's work */
>> + return;
>> + }
>> +
>> + hb->size = size;
>> + for (i = HBITMAP_LEVELS; i-- > 0; ) {
>> + size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
>> + if (hb->sizes[i] == size) {
>> + continue;
>> + }
>> + old = hb->sizes[i];
>> + hb->sizes[i] = size;
>
> I was wondering what sizes[] is used for. Not a very useful struct
> field since it's only needed by this rarely called function.
>
In future patches, we tend to recalculate the size of each array a lot.
I decided I wanted to cache it so we could stop duplicating that code
over and over.
It comes up in migration and persistence a lot. It's easier to just add
it now instead of allow the duplication to sneak in and then patch it
out everywhere.
> It would be clearer to calculate 'old' alongside 'size' each loop
> iteration. The size[] field can be dropped, 'old' becomes 'old_size',
> and 'size' becomes 'new_size':
>
> old_size = hb->size;
> for (i = HBITMAP_LEVELS; i-- > 0; ) {
> old_size = MAX((old_size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
> new_size = MAX((new_size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
>
>> + hb->levels[i] = g_realloc_n(hb->levels[i], size, sizeof(unsigned long));
>> + if (!truncate) {
>> + memset(&hb->levels[i][old], 0x00,
>> + (size - old) * sizeof(*hb->levels[i]));
>> + }
>> + }
>> + assert(size == 1);
>> +
>> + /* Clear out any "extra space" we may have that the user didn't request:
>> + * It may have garbage data in it, now. */
>> + if (truncate) {
>> + /* Due to granularity fuzziness, we may accidentally reset some of
>> + * the last bits that are actually valid. So, record the current value,
>> + * reset the "dead range," then re-set the one element we care about. */
>> + uint64_t fix_count = (hb->size << hb->granularity) - num_elements;
>> + if (fix_count) {
>> + bool set = hbitmap_get(hb, num_elements - 1);
>> + hbitmap_reset(hb, num_elements, fix_count);
>> + if (set) {
>> + hbitmap_set(hb, num_elements - 1, 1);
>> + }
>> + }
>
> Calling hbitmap_reset() with an out-of-bounds index seems hacky to me.
>
It's the simplest way to re-use the existing code to recursively clear
out any bits that are set that shouldn't be.
> Why doesn't the for loop's if (!truncate) have an else statement to mask
> no longer visible bits? Maybe I'm missing why that's hard to do.
>
I just didn't see a reason to replicate the logic of what hbitmap_reset
already does, so I didn't bother to try.
next prev parent reply other threads:[~2015-03-11 17:04 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-02 23:19 [Qemu-devel] [PATCH v2 00/17] block: transactionless incremental backup series John Snow
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 01/17] docs: incremental backup documentation John Snow
2015-03-03 15:28 ` Max Reitz
2015-03-11 13:43 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-03-11 14:19 ` John Snow
2015-03-11 14:43 ` Eric Blake
2015-03-11 14:45 ` John Snow
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 02/17] qapi: Add optional field "name" to block dirty bitmap John Snow
2015-03-11 15:34 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 03/17] qmp: Ensure consistent granularity type John Snow
2015-03-11 15:34 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 04/17] qmp: Add block-dirty-bitmap-add and block-dirty-bitmap-remove John Snow
2015-03-11 13:58 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-03-11 14:23 ` John Snow
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 05/17] block: Introduce bdrv_dirty_bitmap_granularity() John Snow
2015-03-11 15:34 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 06/17] hbitmap: add hbitmap_merge John Snow
2015-03-11 15:43 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 07/17] block: Add bitmap disabled status John Snow
2015-03-11 16:34 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 08/17] block: Add bitmap successors John Snow
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 09/17] qmp: Add support of "dirty-bitmap" sync mode for drive-backup John Snow
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 10/17] qmp: add block-dirty-bitmap-clear John Snow
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 11/17] qmp: Add dirty bitmap status fields in query-block John Snow
2015-03-11 16:54 ` Stefan Hajnoczi
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 12/17] block: add BdrvDirtyBitmap documentation John Snow
2015-03-02 23:19 ` [Qemu-devel] [PATCH v2 13/17] block: Ensure consistent bitmap function prototypes John Snow
2015-03-02 23:20 ` [Qemu-devel] [PATCH v2 14/17] block: Resize bitmaps on bdrv_truncate John Snow
2015-03-03 15:29 ` Max Reitz
2015-03-03 16:02 ` Max Reitz
2015-03-03 21:24 ` John Snow
2015-03-03 21:27 ` Max Reitz
2015-03-03 22:48 ` John Snow
2015-03-04 13:54 ` Max Reitz
2015-03-11 16:18 ` Stefan Hajnoczi
2015-03-11 17:04 ` John Snow [this message]
2015-03-02 23:20 ` [Qemu-devel] [PATCH v2 15/17] iotests: add invalid input incremental backup tests John Snow
2015-03-02 23:20 ` [Qemu-devel] [PATCH v2 16/17] iotests: add simple incremental backup case John Snow
2015-03-02 23:20 ` [Qemu-devel] [PATCH v2 17/17] iotests: add incremental backup failure recovery test John Snow
2015-03-03 15:26 ` [Qemu-devel] [PATCH v2 00/17] block: transactionless incremental backup series Max Reitz
2015-03-11 16:57 ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
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=5500759D.7000203@redhat.com \
--to=jsnow@redhat.com \
--cc=armbru@redhat.com \
--cc=famz@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
--cc=stefanha@redhat.com \
--cc=vsementsov@parallels.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).