9ae3a8
From 2a46f74bc62cd8affeb7516d1adf5dd855f3ed5a Mon Sep 17 00:00:00 2001
9ae3a8
From: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
Date: Fri, 18 Oct 2013 08:14:26 +0200
9ae3a8
Subject: [PATCH 01/81] cow: make reads go at a decent speed
9ae3a8
9ae3a8
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
Message-id: <1382084091-16636-2-git-send-email-pbonzini@redhat.com>
9ae3a8
Patchwork-id: 54983
9ae3a8
O-Subject: [RHEL 7.0 qemu-kvm PATCH 01/26] cow: make reads go at a decent speed
9ae3a8
Bugzilla: 989646
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
9ae3a8
Do not do two reads for each sector; load each sector of the bitmap
9ae3a8
and use bitmap operations to process it.
9ae3a8
9ae3a8
Writes are still dog slow!
9ae3a8
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
(cherry picked from commit 276cbc7f2fc1bd3810887995dbc9cbb739c975bf)
9ae3a8
---
9ae3a8
 block/cow.c | 54 ++++++++++++++++++++++++++++++++----------------------
9ae3a8
 1 file changed, 32 insertions(+), 22 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/cow.c |   54 ++++++++++++++++++++++++++++++++----------------------
9ae3a8
 1 files changed, 32 insertions(+), 22 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/cow.c b/block/cow.c
9ae3a8
index 9f94599..c12088b 100644
9ae3a8
--- a/block/cow.c
9ae3a8
+++ b/block/cow.c
9ae3a8
@@ -126,18 +126,31 @@ static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum)
9ae3a8
     return 0;
9ae3a8
 }
9ae3a8
 
9ae3a8
-static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)
9ae3a8
+#define BITS_PER_BITMAP_SECTOR (512 * 8)
9ae3a8
+
9ae3a8
+/* Cannot use bitmap.c on big-endian machines.  */
9ae3a8
+static int cow_test_bit(int64_t bitnum, const uint8_t *bitmap)
9ae3a8
 {
9ae3a8
-    uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
9ae3a8
-    uint8_t bitmap;
9ae3a8
-    int ret;
9ae3a8
+    return (bitmap[bitnum / 8] & (1 << (bitnum & 7))) != 0;
9ae3a8
+}
9ae3a8
 
9ae3a8
-    ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
9ae3a8
-    if (ret < 0) {
9ae3a8
-       return ret;
9ae3a8
+static int cow_find_streak(const uint8_t *bitmap, int value, int start, int nb_sectors)
9ae3a8
+{
9ae3a8
+    int streak_value = value ? 0xFF : 0;
9ae3a8
+    int last = MIN(start + nb_sectors, BITS_PER_BITMAP_SECTOR);
9ae3a8
+    int bitnum = start;
9ae3a8
+    while (bitnum < last) {
9ae3a8
+        if ((bitnum & 7) == 0 && bitmap[bitnum / 8] == streak_value) {
9ae3a8
+            bitnum += 8;
9ae3a8
+            continue;
9ae3a8
+        }
9ae3a8
+        if (cow_test_bit(bitnum, bitmap) == value) {
9ae3a8
+            bitnum++;
9ae3a8
+            continue;
9ae3a8
+        }
9ae3a8
+        break;
9ae3a8
     }
9ae3a8
-
9ae3a8
-    return !!(bitmap & (1 << (bitnum % 8)));
9ae3a8
+    return MIN(bitnum, last) - start;
9ae3a8
 }
9ae3a8
 
9ae3a8
 /* Return true if first block has been changed (ie. current version is
9ae3a8
@@ -146,23 +159,20 @@ static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)
9ae3a8
 static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
9ae3a8
         int64_t sector_num, int nb_sectors, int *num_same)
9ae3a8
 {
9ae3a8
+    int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;
9ae3a8
+    uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;
9ae3a8
+    uint8_t bitmap[BDRV_SECTOR_SIZE];
9ae3a8
+    int ret;
9ae3a8
     int changed;
9ae3a8
 
9ae3a8
-    if (nb_sectors == 0) {
9ae3a8
-	*num_same = nb_sectors;
9ae3a8
-	return 0;
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    changed = is_bit_set(bs, sector_num);
9ae3a8
-    if (changed < 0) {
9ae3a8
-        return 0; /* XXX: how to return I/O errors? */
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) {
9ae3a8
-	if (is_bit_set(bs, sector_num + *num_same) != changed)
9ae3a8
-	    break;
9ae3a8
+    ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
9ae3a8
+    if (ret < 0) {
9ae3a8
+        return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
+    bitnum &= BITS_PER_BITMAP_SECTOR - 1;
9ae3a8
+    changed = cow_test_bit(bitnum, bitmap);
9ae3a8
+    *num_same = cow_find_streak(bitmap, changed, bitnum, nb_sectors);
9ae3a8
     return changed;
9ae3a8
 }
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8