From patchwork Thu Jul 25 14:22:59 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 261709 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 84EE92C00D5 for ; Fri, 26 Jul 2013 00:24:26 +1000 (EST) Received: from localhost ([::1]:41971 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V2MSu-0002dT-C5 for incoming@patchwork.ozlabs.org; Thu, 25 Jul 2013 10:24:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35730) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V2MSN-0002VI-Jj for qemu-devel@nongnu.org; Thu, 25 Jul 2013 10:23:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V2MSK-0007pN-NT for qemu-devel@nongnu.org; Thu, 25 Jul 2013 10:23:51 -0400 Received: from mail-ye0-x22e.google.com ([2607:f8b0:4002:c04::22e]:52522) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V2MSK-0007p7-JB for qemu-devel@nongnu.org; Thu, 25 Jul 2013 10:23:48 -0400 Received: by mail-ye0-f174.google.com with SMTP id q9so194813yen.33 for ; Thu, 25 Jul 2013 07:23:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=YbPUTAiRi/SWEwbjRbwc4AZei4j5l0jCuIHRzSDPvHs=; b=yjITM68bpznIGxj0gPyruRovOuPBmanrzcCFPZ6AFZQW9ojEvbHtaWCvCyKK52aVVr 5tIVwWfUKAj0IRtah6+Co9Lk/lXktaTh6QNICzez2m854wpML8SNoqujr98xJN5aPNR8 CBB8CD3LfkiWhunkXgVaDKICv3kWgyj8Tbm96Y8JK6mjXZlzjn5aaFarZ0th3wB7NfOU 5dr0XgUO76z1L9HJfEvRFDRD99s8Ep4eHDLZF6I2MR5/8BNplVBazgSiw97whPYB6x4B wm1GN9iN9ptL7Zl0Ji6x0zYy5EoP6amPGUK7LJ9IeIcUEQr/H/rjJCXInWAJWpAqHtqr oDdQ== X-Received: by 10.236.129.194 with SMTP id h42mr8456645yhi.78.1374762228147; Thu, 25 Jul 2013 07:23:48 -0700 (PDT) Received: from yakj.usersys.redhat.com (net-2-39-8-162.cust.dsl.vodafone.it. [2.39.8.162]) by mx.google.com with ESMTPSA id b48sm58469338yhc.8.2013.07.25.07.23.45 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 25 Jul 2013 07:23:47 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 25 Jul 2013 16:22:59 +0200 Message-Id: <1374762197-7261-2-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1374762197-7261-1-git-send-email-pbonzini@redhat.com> References: <1374762197-7261-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4002:c04::22e Cc: kwolf@redhat.com, pl@kamp.de, stefanha@redhat.com Subject: [Qemu-devel] [PATCH v3 01/19] cow: make reads go at a decent speed X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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! Reviewed-by: Eric Blake 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..fa3d41f 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) +{ + 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[BDRV_SECTOR_SIZE]; + 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; }