[PATCH V3 1/2] Btrfs: improve the performance of the csums lookup

2013-04-05 Thread Miao Xie
It is very likely that there are several blocks in bio, it is very
inefficient if we get their csums one by one. This patch improves
this problem by getting the csums in batch.

According to the result of the following test, the execute time of
__btrfs_lookup_bio_sums() is down by ~28%(300us - 217us).

 # dd if=mnt/file of=/dev/null bs=1M count=1024

Signed-off-by: Miao Xie mi...@cn.fujitsu.com
---
Changelog v2 - v3:
- address the problem that the logical offset of the pages in the same bio is
  not contiguous.

Changelog v1 - v2:
- fix 64bit division problem on i386 machine
---
 fs/btrfs/extent_io.c| 58 +
 fs/btrfs/extent_io.h|  4 
 fs/btrfs/file-item.c| 49 +
 fs/btrfs/ordered-data.c | 28 ++--
 fs/btrfs/ordered-data.h |  3 ++-
 5 files changed, 111 insertions(+), 31 deletions(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index cdee391..19dd3da 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -1777,6 +1777,64 @@ out:
return ret;
 }
 
+void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 
csums[],
+   int count)
+{
+   struct rb_node *node;
+   struct extent_state *state;
+
+   spin_lock(tree-lock);
+   /*
+* this search will find all the extents that end after
+* our range starts.
+*/
+   node = tree_search(tree, start);
+   BUG_ON(!node);
+
+   state = rb_entry(node, struct extent_state, rb_node);
+   BUG_ON(state-start != start);
+
+   while (count) {
+   state-private = *csums++;
+   count--;
+   state = next_state(state);
+   }
+   spin_unlock(tree-lock);
+}
+
+static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
+{
+   struct bio_vec *bvec = bio-bi_io_vec + bio_index;
+
+   return page_offset(bvec-bv_page) + bvec-bv_offset;
+}
+
+void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int 
bio_index,
+   u32 csums[], int count)
+{
+   struct rb_node *node;
+   struct extent_state *state = NULL;
+   u64 start;
+
+   spin_lock(tree-lock);
+   do {
+   start = __btrfs_get_bio_offset(bio, bio_index);
+   if (state == NULL || state-start != start) {
+   node = tree_search(tree, start);
+   BUG_ON(!node);
+
+   state = rb_entry(node, struct extent_state, rb_node);
+   BUG_ON(state-start != start);
+   }
+   state-private = *csums++;
+   count--;
+   bio_index++;
+
+   state = next_state(state);
+   } while (count);
+   spin_unlock(tree-lock);
+}
+
 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
 {
struct rb_node *node;
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index 258c921..db009d8 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -261,6 +261,10 @@ int extent_readpages(struct extent_io_tree *tree,
 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
__u64 start, __u64 len, get_extent_t *get_extent);
 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private);
+void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 
csums[],
+   int count);
+void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio,
+   int bvec_index, u32 csums[], int count);
 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private);
 void set_page_extent_mapped(struct page *page);
 
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index c4628a2..7e4df79 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -177,7 +177,8 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
   struct inode *inode, struct bio *bio,
   u64 logical_offset, u32 *dst, int dio)
 {
-   u32 sum;
+   u32 sum[16];
+   int len;
struct bio_vec *bvec = bio-bi_io_vec;
int bio_index = 0;
u64 offset = 0;
@@ -186,7 +187,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
u64 disk_bytenr;
u32 diff;
u16 csum_size = btrfs_super_csum_size(root-fs_info-super_copy);
-   int ret;
+   int count;
struct btrfs_path *path;
struct btrfs_csum_item *item = NULL;
struct extent_io_tree *io_tree = BTRFS_I(inode)-io_tree;
@@ -214,10 +215,12 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root 
*root,
if (dio)
offset = logical_offset;
while (bio_index  bio-bi_vcnt) {
+   len = min_t(int, ARRAY_SIZE(sum), bio-bi_vcnt - bio_index);
if (!dio)
offset = 

Re: [PATCH V3 1/2] Btrfs: improve the performance of the csums lookup

2013-04-05 Thread David Sterba
On Fri, Apr 05, 2013 at 03:20:56PM +0800, Miao Xie wrote:
 It is very likely that there are several blocks in bio, it is very
 inefficient if we get their csums one by one. This patch improves
 this problem by getting the csums in batch.
 
 According to the result of the following test, the execute time of
 __btrfs_lookup_bio_sums() is down by ~28%(300us - 217us).
 
  # dd if=mnt/file of=/dev/null bs=1M count=1024
 
 Signed-off-by: Miao Xie mi...@cn.fujitsu.com
 ---
 Changelog v2 - v3:
 - address the problem that the logical offset of the pages in the same bio is
   not contiguous.

Does not crash at 091 anymore (it's btrfs-next based on 3.9-rc5 + v3).

david
--
To unsubscribe from this list: send the line unsubscribe linux-btrfs in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html