qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: John Snow <jsnow@redhat.com>, qemu-block@nongnu.org
Cc: kwolf@redhat.com, famz@redhat.com, qemu-devel@nongnu.org,
	armbru@redhat.com, vsementsov@parallels.com, stefanha@redhat.com
Subject: Re: [Qemu-devel] [2.4 PATCH v3 16/19] hbitmap: truncate tests
Date: Tue, 17 Mar 2015 10:53:11 -0400	[thread overview]
Message-ID: <55083FD7.3080004@redhat.com> (raw)
In-Reply-To: <1426271419-8277-17-git-send-email-jsnow@redhat.com>

On 2015-03-13 at 14:30, John Snow wrote:
> The general approach is to set bits close to the boundaries of
> where we are truncating and ensure that everything appears to
> have gone OK.
>
> We test growing and shrinking by different amounts:
> - Less than the granularity
> - Less than the granularity, but across a boundary
> - Less than sizeof(unsigned long)
> - Less than sizeof(unsigned long), but across a ulong boundary
> - More than sizeof(unsigned long)
>
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>   tests/test-hbitmap.c | 247 +++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 247 insertions(+)
>
> diff --git a/tests/test-hbitmap.c b/tests/test-hbitmap.c
> index 8c902f2..65401ab 100644
> --- a/tests/test-hbitmap.c
> +++ b/tests/test-hbitmap.c
> @@ -11,6 +11,8 @@
>   
>   #include <glib.h>
>   #include <stdarg.h>
> +#include <string.h>
> +#include <sys/types.h>
>   #include "qemu/hbitmap.h"
>   
>   #define LOG_BITS_PER_LONG          (BITS_PER_LONG == 32 ? 5 : 6)
> @@ -23,6 +25,7 @@ typedef struct TestHBitmapData {
>       HBitmap       *hb;
>       unsigned long *bits;
>       size_t         size;
> +    size_t         old_size;
>       int            granularity;
>   } TestHBitmapData;
>   
> @@ -91,6 +94,44 @@ static void hbitmap_test_init(TestHBitmapData *data,
>       }
>   }
>   
> +static inline size_t hbitmap_test_array_size(size_t bits)
> +{
> +    size_t n = (bits + BITS_PER_LONG - 1) / BITS_PER_LONG;
> +    return n ? n : 1;
> +}
> +
> +static void hbitmap_test_truncate_impl(TestHBitmapData *data,
> +                                       size_t size)
> +{
> +    size_t n;
> +    size_t m;
> +    data->old_size = data->size;
> +    data->size = size;
> +
> +    if (data->size == data->old_size) {
> +        return;
> +    }
> +
> +    n = hbitmap_test_array_size(size);
> +    m = hbitmap_test_array_size(data->old_size);
> +    data->bits = g_realloc(data->bits, sizeof(unsigned long) * n);
> +    if (n > m) {
> +        memset(&data->bits[m], 0x00, sizeof(unsigned long) * (n - m));
> +    }
> +
> +    /* If we shrink to an uneven multiple of sizeof(unsigned long),
> +     * scrub the leftover memory. */
> +    if (data->size < data->old_size) {
> +        m = size % (sizeof(unsigned long) * 8);
> +        if (m) {
> +            unsigned long mask = (1ULL << m) - 1;
> +            data->bits[n-1] &= mask;
> +        }
> +    }
> +
> +    hbitmap_truncate(data->hb, size);
> +}
> +
>   static void hbitmap_test_teardown(TestHBitmapData *data,
>                                     const void *unused)
>   {
> @@ -369,6 +410,190 @@ static void test_hbitmap_iter_granularity(TestHBitmapData *data,
>       g_assert_cmpint(hbitmap_iter_next(&hbi), <, 0);
>   }
>   
> +static void hbitmap_test_set_boundary_bits(TestHBitmapData *data, ssize_t diff)
> +{
> +    size_t size = data->size;
> +
> +    /* First bit */
> +    hbitmap_test_set(data, 0, 1);
> +    if (diff < 0) {
> +        /* Last bit in new, shortened map */
> +        hbitmap_test_set(data, size + diff - 1, 1);
> +
> +        /* First bit to be truncated away */
> +        hbitmap_test_set(data, size + diff, 1);
> +    }
> +    /* Last bit */
> +    hbitmap_test_set(data, size - 1, 1);
> +    if (data->granularity == 0) {
> +        hbitmap_test_check_get(data);
> +    }
> +}
> +
> +static void hbitmap_test_check_boundary_bits(TestHBitmapData *data)
> +{
> +    size_t size = MIN(data->size, data->old_size);
> +
> +    if (data->granularity == 0) {
> +        hbitmap_test_check_get(data);
> +        hbitmap_test_check(data, 0);
> +    } else {
> +        g_assert(hbitmap_get(data->hb, 0));
> +        g_assert(hbitmap_get(data->hb, size - 1));
> +        g_assert_cmpint(2 << data->granularity, ==, hbitmap_count(data->hb));

Hm, where does this come from?

> +    }
> +}
> +
> +/* Generic truncate test. */
> +static void hbitmap_test_truncate(TestHBitmapData *data,
> +                                  size_t size,
> +                                  ssize_t diff,
> +                                  int granularity)
> +{
> +    hbitmap_test_init(data, size, granularity);
> +    hbitmap_test_set_boundary_bits(data, diff);
> +    hbitmap_test_truncate_impl(data, size + diff);
> +    hbitmap_test_check_boundary_bits(data);
> +}
> +
> +static void test_hbitmap_truncate_nop(TestHBitmapData *data,
> +                                      const void *unused)
> +{
> +    hbitmap_test_truncate(data, L2, 0, 0);
> +}
> +
> +/**
> + * Grow by an amount smaller than the granularity, without crossing
> + * a granularity alignment boundary. Effectively a NOP.
> + */
> +static void test_hbitmap_truncate_grow_negligible(TestHBitmapData *data,
> +                                                  const void *unused)
> +{
> +    size_t size = L2 - 1;
> +    size_t diff = 1;
> +    int granularity = 1;
> +
> +    hbitmap_test_truncate(data, size, diff, granularity);
> +}
> +
> +/**
> + * Shrink by an amount smaller than the granularity, without crossing
> + * a granularity alignment boundary. Effectively a NOP.
> + */
> +static void test_hbitmap_truncate_shrink_negligible(TestHBitmapData *data,
> +                                                    const void *unused)
> +{
> +    size_t size = L2;
> +    ssize_t diff = -1;
> +    int granularity = 1;
> +
> +    hbitmap_test_truncate(data, size, diff, granularity);
> +}
> +
> +/**
> + * Grow by an amount smaller than the granularity, but crossing over
> + * a granularity alignment boundary.
> + */
> +static void test_hbitmap_truncate_grow_tiny(TestHBitmapData *data,
> +                                            const void *unused)
> +{
> +    size_t size = L2 - 2;
> +    ssize_t diff = 1;
> +    int granularity = 1;
> +
> +    hbitmap_test_truncate(data, size, diff, granularity);
> +}
> +
> +/**
> + * Shrink by an amount smaller than the granularity, but crossing over
> + * a granularity alignment boundary.
> + */
> +static void test_hbitmap_truncate_shrink_tiny(TestHBitmapData *data,
> +                                              const void *unused)
> +{
> +    size_t size = L2 - 1;
> +    ssize_t diff = -1;
> +    int granularity = 1;
> +
> +    hbitmap_test_truncate(data, size, diff, granularity);
> +}
> +
> +/**
> + * Grow by an amount smaller than sizeof(long), and not crossing over
> + * a sizeof(long) alignment boundary.
> + */
> +static void test_hbitmap_truncate_grow_small(TestHBitmapData *data,
> +                                             const void *unused)
> +{
> +    size_t size = L2 + 1;
> +    size_t diff = sizeof(long) / 2;
> +
> +    hbitmap_test_truncate(data, size, diff, 0);
> +}
> +
> +/**
> + * Shrink by an amount smaller than sizeof(long), and not crossing over
> + * a sizeof(long) alignment boundary.
> + */
> +static void test_hbitmap_truncate_shrink_small(TestHBitmapData *data,
> +                                               const void *unused)
> +{
> +    size_t size = L2;
> +    size_t diff = sizeof(long) / 2;
> +
> +    hbitmap_test_truncate(data, size, -diff, 0);
> +}
> +
> +/**
> + * Grow by an amount smaller than sizeof(long), while crossing over
> + * a sizeof(long) alignment boundary.
> + */
> +static void test_hbitmap_truncate_grow_medium(TestHBitmapData *data,
> +                                              const void *unused)
> +{
> +    size_t size = L2 - 1;
> +    size_t diff = sizeof(long) / 2;
> +
> +    hbitmap_test_truncate(data, size, diff, 0);
> +}
> +
> +/**
> + * Shrink by an amount smaller than sizeof(long), while crossing over
> + * a sizeof(long) alignment boundary.
> + */
> +static void test_hbitmap_truncate_shrink_medium(TestHBitmapData *data,
> +                                                const void *unused)
> +{
> +    size_t size = L2 + 1;
> +    size_t diff = sizeof(long) / 2;
> +
> +    hbitmap_test_truncate(data, size, -diff, 0);
> +}
> +
> +/**
> + * Grow by an amount larger than sizeof(long).
> + */
> +static void test_hbitmap_truncate_grow_large(TestHBitmapData *data,
> +                                             const void *unused)
> +{
> +    size_t size = L2;
> +    size_t diff = 8 * sizeof(long);

You can use L1 here. But you don't have to. Do as you please. (just saying)

Max

> +
> +    hbitmap_test_truncate(data, size, diff, 0);
> +}
> +
> +/**
> + * Shrink by an amount larger than sizeof(long).
> + */
> +static void test_hbitmap_truncate_shrink_large(TestHBitmapData *data,
> +                                               const void *unused)
> +{
> +    size_t size = L2;
> +    size_t diff = 8 * sizeof(long);
> +
> +    hbitmap_test_truncate(data, size, -diff, 0);
> +}
> +
>   static void hbitmap_test_add(const char *testpath,
>                                      void (*test_func)(TestHBitmapData *data, const void *user_data))
>   {
> @@ -395,6 +620,28 @@ int main(int argc, char **argv)
>       hbitmap_test_add("/hbitmap/reset/empty", test_hbitmap_reset_empty);
>       hbitmap_test_add("/hbitmap/reset/general", test_hbitmap_reset);
>       hbitmap_test_add("/hbitmap/granularity", test_hbitmap_granularity);
> +
> +    hbitmap_test_add("/hbitmap/truncate/nop", test_hbitmap_truncate_nop);
> +    hbitmap_test_add("/hbitmap/truncate/grow/negligible",
> +                     test_hbitmap_truncate_grow_negligible);
> +    hbitmap_test_add("/hbitmap/truncate/shrink/negligible",
> +                     test_hbitmap_truncate_shrink_negligible);
> +    hbitmap_test_add("/hbitmap/truncate/grow/tiny",
> +                     test_hbitmap_truncate_grow_tiny);
> +    hbitmap_test_add("/hbitmap/truncate/shrink/tiny",
> +                     test_hbitmap_truncate_shrink_tiny);
> +    hbitmap_test_add("/hbitmap/truncate/grow/small",
> +                     test_hbitmap_truncate_grow_small);
> +    hbitmap_test_add("/hbitmap/truncate/shrink/small",
> +                     test_hbitmap_truncate_shrink_small);
> +    hbitmap_test_add("/hbitmap/truncate/grow/medium",
> +                     test_hbitmap_truncate_grow_medium);
> +    hbitmap_test_add("/hbitmap/truncate/shrink/medium",
> +                     test_hbitmap_truncate_shrink_medium);
> +    hbitmap_test_add("/hbitmap/truncate/grow/large",
> +                     test_hbitmap_truncate_grow_large);
> +    hbitmap_test_add("/hbitmap/truncate/shrink/large",
> +                     test_hbitmap_truncate_shrink_large);
>       g_test_run();
>   
>       return 0;

  reply	other threads:[~2015-03-17 14:53 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-13 18:30 [Qemu-devel] [2.4 PATCH v3 00/19] block: transactionless incremental backup series John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 01/19] docs: incremental backup documentation John Snow
2015-03-16 20:36   ` Max Reitz
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 02/19] qapi: Add optional field "name" to block dirty bitmap John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 03/19] qmp: Ensure consistent granularity type John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 04/19] qmp: Add block-dirty-bitmap-add and block-dirty-bitmap-remove John Snow
2015-03-16 20:44   ` Max Reitz
2015-03-16 20:53     ` John Snow
2015-03-16 20:54       ` Max Reitz
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 05/19] block: Introduce bdrv_dirty_bitmap_granularity() John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 06/19] hbitmap: cache array lengths John Snow
2015-03-16 20:53   ` Max Reitz
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 07/19] hbitmap: add hbitmap_merge John Snow
2015-03-16 20:55   ` Max Reitz
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 08/19] block: Add bitmap disabled status John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 09/19] block: Add bitmap successors John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 10/19] qmp: Add support of "dirty-bitmap" sync mode for drive-backup John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 11/19] qmp: add block-dirty-bitmap-clear John Snow
2015-03-16 20:57   ` Max Reitz
2015-03-16 21:03     ` John Snow
2015-03-17 13:11       ` Max Reitz
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 12/19] qmp: Add dirty bitmap status field in query-block John Snow
2015-03-16 20:58   ` Max Reitz
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 13/19] block: add BdrvDirtyBitmap documentation John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 14/19] block: Ensure consistent bitmap function prototypes John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 15/19] block: Resize bitmaps on bdrv_truncate John Snow
2015-03-17 13:50   ` Max Reitz
2015-03-17 17:13     ` John Snow
2015-03-17 17:17       ` Max Reitz
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 16/19] hbitmap: truncate tests John Snow
2015-03-17 14:53   ` Max Reitz [this message]
2015-03-17 17:21     ` John Snow
2015-03-17 17:28       ` Max Reitz
2015-03-17 17:44         ` John Snow
2015-03-17 17:45           ` Max Reitz
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 17/19] iotests: add invalid input incremental backup tests John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 18/19] iotests: add simple incremental backup case John Snow
2015-03-13 18:30 ` [Qemu-devel] [2.4 PATCH v3 19/19] iotests: add incremental backup failure recovery test John Snow

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=55083FD7.3080004@redhat.com \
    --to=mreitz@redhat.com \
    --cc=armbru@redhat.com \
    --cc=famz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --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).