From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36322) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uueay-0006ND-7z for qemu-devel@nongnu.org; Thu, 04 Jul 2013 04:08:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Uueau-0002vf-91 for qemu-devel@nongnu.org; Thu, 04 Jul 2013 04:08:52 -0400 Received: from mail-wi0-x236.google.com ([2a00:1450:400c:c05::236]:45084) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uueau-0002vS-05 for qemu-devel@nongnu.org; Thu, 04 Jul 2013 04:08:48 -0400 Received: by mail-wi0-f182.google.com with SMTP id m6so931575wiv.9 for ; Thu, 04 Jul 2013 01:08:47 -0700 (PDT) Sender: Paolo Bonzini Message-ID: <51D52D85.9040406@redhat.com> Date: Thu, 04 Jul 2013 10:08:37 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <1372862071-28225-1-git-send-email-pbonzini@redhat.com> <1372862071-28225-2-git-send-email-pbonzini@redhat.com> <20130704022042.GA6659@T430s.nay.redhat.com> In-Reply-To: <20130704022042.GA6659@T430s.nay.redhat.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH 01/17] cow: make reads go at a decent speed List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: famz@redhat.com Cc: kwolf@redhat.com, pl@kamp.de, qemu-devel@nongnu.org, stefanha@redhat.com Il 04/07/2013 04:20, Fam Zheng ha scritto: > On Wed, 07/03 16:34, Paolo Bonzini wrote: >> Do not do two reads for each sector; load each sector of the bitmap >> and use bitmap operations to process it. >> >> Writes are still dog slow! >> >> Signed-off-by: Paolo Bonzini >> --- >> block/cow.c | 54 ++++++++++++++++++++++++++++++++---------------------- >> 1 file changed, 32 insertions(+), 22 deletions(-) >> >> diff --git a/block/cow.c b/block/cow.c >> index 1cc2e89..204451e 100644 >> --- a/block/cow.c >> +++ b/block/cow.c >> @@ -126,18 +126,31 @@ static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum) >> return 0; >> } >> >> -static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum) >> +#define BITS_PER_BITMAP_SECTOR (512 * 8) >> + >> +/* Cannot use bitmap.c on big-endian machines. */ >> +static int cow_test_bit(int64_t bitnum, const uint8_t *bitmap) >> { >> - uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8; >> - uint8_t bitmap; >> - int ret; >> + return (bitmap[bitnum / 8] & (1 << (bitnum & 7))) != 0; >> +} >> >> - ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)); >> - if (ret < 0) { >> - return ret; >> +static int cow_find_streak(const uint8_t *bitmap, int value, int start, int nb_sectors) > I think type bool is better for 'value' as you don't booleanize it. And > also int64_t for start? start is always between 0 and BITS_PER_BITMAP_SECTOR. "value" here is a bit value, so 0 or 1 rather than true or false. I prefer to keep it as "int", but it can be changed. Paolo >> +{ >> + int streak_value = value ? 0xFF : 0; >> + int last = MIN(start + nb_sectors, BITS_PER_BITMAP_SECTOR); >> + int bitnum = start; >> + while (bitnum < last) { >> + if ((bitnum & 7) == 0 && bitmap[bitnum / 8] == streak_value) { >> + bitnum += 8; >> + continue; >> + } >> + if (cow_test_bit(bitnum, bitmap) == value) { >> + bitnum++; >> + continue; >> + } >> + break; >> } >> - >> - return !!(bitmap & (1 << (bitnum % 8))); >> + return MIN(bitnum, last) - start; >> } >> >> /* Return true if first block has been changed (ie. current version is >> @@ -146,23 +159,20 @@ static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum) >> static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs, >> int64_t sector_num, int nb_sectors, int *num_same) >> { >> + int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8; >> + uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE; >> + uint8_t bitmap[512]; >> + int ret; >> int changed; >> >> - if (nb_sectors == 0) { >> - *num_same = nb_sectors; >> - return 0; >> - } >> - >> - changed = is_bit_set(bs, sector_num); >> - if (changed < 0) { >> - return 0; /* XXX: how to return I/O errors? */ >> - } >> - >> - for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) { >> - if (is_bit_set(bs, sector_num + *num_same) != changed) >> - break; >> + ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)); >> + if (ret < 0) { >> + return ret; >> } >> >> + bitnum &= BITS_PER_BITMAP_SECTOR - 1; >> + changed = cow_test_bit(bitnum, bitmap); >> + *num_same = cow_find_streak(bitmap, changed, bitnum, nb_sectors); >> return changed; >> } >> >> -- >> 1.8.2.1 >> >> >> >