Skip to contentSkip to conversation

btrfs: speedup mount time with force readahead chunk tree

PATCH In review robbieko 3 participants 4 messages Jul 1, 2020 Btrfs file system

lore
Share & tools
lore

robbieko

author
#0 lore raw

PATCH 1/1 btrfs: speedup mount time with force readahead chunk tree

From: Robbie Ko <[email protected]>

When mounting, we always need to read the whole chunk tree, when there are too many chunk items, most of the time is spent on btrfs_read_chunk_tree, because we only read one leaf at a time.

We fix this by adding a new readahead mode READA_FORWARD_FORCE, which reads all the leaves after the key in the node when reading a level 1 node.

---
 fs/btrfs/ctree.c   | 7 +++++--
 fs/btrfs/ctree.h   | 2 +-
 fs/btrfs/volumes.c | 1 +
 3 files changed, 7 insertions(+), 3 deletions(-)
fs/btrfs/ctree.c +5 -2
index 3a7648bff42c..abb9108e2d7d 100644
@@ -2194,7 +2194,7 @@ static void reada_for_search(struct btrfs_fs_info *fs_info,
 			if (nr == 0)
 				break;
 			nr--;
-		} else if (path->reada == READA_FORWARD) {
+		} else if (path->reada == READA_FORWARD || path->reada == READA_FORWARD_FORCE) {
 			nr++;
 			if (nr >= nritems)
 				break;
@@ -2205,12 +2205,15 @@ static void reada_for_search(struct btrfs_fs_info *fs_info,
 				break;
 		}
 		search = btrfs_node_blockptr(node, nr);
-		if ((search <= target && target - search <= 65536) ||
+		if ((path->reada == READA_FORWARD_FORCE) ||
+		    (search <= target && target - search <= 65536) ||
 		    (search > target && search - target <= 65536)) {
 			readahead_tree_block(fs_info, search);
 			nread += blocksize;
 		}
 		nscan++;
+		if (path->reada == READA_FORWARD_FORCE)
+			continue;
 		if ((nread > 65536 || nscan > 32))
 			break;
 	}
fs/btrfs/ctree.h +1 -1
index d404cce8ae40..808bcbdc9530 100644
@@ -353,7 +353,7 @@ struct btrfs_node {
  * The slots array records the index of the item or block pointer
  * used while walking the tree.
  */
-enum { READA_NONE, READA_BACK, READA_FORWARD };
+enum { READA_NONE, READA_BACK, READA_FORWARD, READA_FORWARD_FORCE };
 struct btrfs_path {
 	struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
 	int slots[BTRFS_MAX_LEVEL];
fs/btrfs/volumes.c +1 -0
index 0d6e785bcb98..78fd65abff69 100644
@@ -7043,6 +7043,7 @@ int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
 	path = btrfs_alloc_path();
 	if (!path)
 		return -ENOMEM;
+	path->reada = READA_FORWARD_FORCE;
 
 	/*
 	 * uuid_mutex is needed only if we are mounting a sprout FS
Signature · 1 line
2.17.1
author
#1 lore raw

please ignore this.

Quoted 73 lines

From: Robbie Ko <[email protected]>

When mounting, we always need to read the whole chunk tree, when there are too many chunk items, most of the time is spent on btrfs_read_chunk_tree, because we only read one leaf at a time.

We fix this by adding a new readahead mode READA_FORWARD_FORCE, which reads all the leaves after the key in the node when reading a level 1 node.

---
  fs/btrfs/ctree.c   | 7 +++++--
  fs/btrfs/ctree.h   | 2 +-
  fs/btrfs/volumes.c | 1 +
  3 files changed, 7 insertions(+), 3 deletions(-)
fs/btrfs/ctree.c +5 -2
index 3a7648bff42c..abb9108e2d7d 100644
@@ -2194,7 +2194,7 @@ static void reada_for_search(struct btrfs_fs_info *fs_info,
  			if (nr == 0)
  				break;
  			nr--;
-		} else if (path->reada == READA_FORWARD) {
+		} else if (path->reada == READA_FORWARD || path->reada == READA_FORWARD_FORCE) {
  			nr++;
  			if (nr >= nritems)
  				break;
@@ -2205,12 +2205,15 @@ static void reada_for_search(struct btrfs_fs_info *fs_info,
  				break;
  		}
  		search = btrfs_node_blockptr(node, nr);
-		if ((search <= target && target - search <= 65536) ||
+		if ((path->reada == READA_FORWARD_FORCE) ||
+		    (search <= target && target - search <= 65536) ||
  		    (search > target && search - target <= 65536)) {
  			readahead_tree_block(fs_info, search);
  			nread += blocksize;
  		}
  		nscan++;
+		if (path->reada == READA_FORWARD_FORCE)
+			continue;
  		if ((nread > 65536 || nscan > 32))
  			break;
  	}
fs/btrfs/ctree.h +1 -1
index d404cce8ae40..808bcbdc9530 100644
@@ -353,7 +353,7 @@ struct btrfs_node {
   * The slots array records the index of the item or block pointer
   * used while walking the tree.
   */
-enum { READA_NONE, READA_BACK, READA_FORWARD };
+enum { READA_NONE, READA_BACK, READA_FORWARD, READA_FORWARD_FORCE };
  struct btrfs_path {
  	struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
  	int slots[BTRFS_MAX_LEVEL];
fs/btrfs/volumes.c +1 -0
index 0d6e785bcb98..78fd65abff69 100644
@@ -7043,6 +7043,7 @@ int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
  	path = btrfs_alloc_path();
  	if (!path)
  		return -ENOMEM;
+	path->reada = READA_FORWARD_FORCE;
 
  	/*
  	 * uuid_mutex is needed only if we are mounting a sprout FS
#2 lore raw
In reply to robbieko
Quoted 6 lines · robbieko

From: Robbie Ko <[email protected]>

When mounting, we always need to read the whole chunk tree, when there are too many chunk items, most of the time is spent on btrfs_read_chunk_tree, because we only read one leaf at a time.

Well, under most case it would be btrfs_read_block_groups(), unless all data chunks are very compact with just several large data extents.

Quoted 60 lines

We fix this by adding a new readahead mode READA_FORWARD_FORCE, which reads all the leaves after the key in the node when reading a level 1 node.

---
 fs/btrfs/ctree.c   | 7 +++++--
 fs/btrfs/ctree.h   | 2 +-
 fs/btrfs/volumes.c | 1 +
 3 files changed, 7 insertions(+), 3 deletions(-)
fs/btrfs/ctree.c +5 -2
index 3a7648bff42c..abb9108e2d7d 100644
@@ -2194,7 +2194,7 @@ static void reada_for_search(struct btrfs_fs_info *fs_info,
 			if (nr == 0)
 				break;
 			nr--;
-		} else if (path->reada == READA_FORWARD) {
+		} else if (path->reada == READA_FORWARD || path->reada == READA_FORWARD_FORCE) {
 			nr++;
 			if (nr >= nritems)
 				break;
@@ -2205,12 +2205,15 @@ static void reada_for_search(struct btrfs_fs_info *fs_info,
 				break;
 		}
 		search = btrfs_node_blockptr(node, nr);
-		if ((search <= target && target - search <= 65536) ||
+		if ((path->reada == READA_FORWARD_FORCE) ||
+		    (search <= target && target - search <= 65536) ||
 		    (search > target && search - target <= 65536)) {
 			readahead_tree_block(fs_info, search);
 			nread += blocksize;
 		}
 		nscan++;
+		if (path->reada == READA_FORWARD_FORCE)
+			continue;
 		if ((nread > 65536 || nscan > 32))
 			break;
 	}
fs/btrfs/ctree.h +1 -1
index d404cce8ae40..808bcbdc9530 100644
@@ -353,7 +353,7 @@ struct btrfs_node {
  * The slots array records the index of the item or block pointer
  * used while walking the tree.
  */
-enum { READA_NONE, READA_BACK, READA_FORWARD };
+enum { READA_NONE, READA_BACK, READA_FORWARD, READA_FORWARD_FORCE };
 struct btrfs_path {
 	struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
 	int slots[BTRFS_MAX_LEVEL];
fs/btrfs/volumes.c +0 -0
index 0d6e785bcb98..78fd65abff69 100644

fs/btrfs/volumes.c int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) · line 7046

Quoted 4 lines
fs/btrfs/volumes.c +1 -0
@@ -7043,6 +7043,7 @@ int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
 	path = btrfs_alloc_path();
 	if (!path)
 		return -ENOMEM;
+	path->reada = READA_FORWARD_FORCE;

Why not just use regular forward readahead?

Mind to share the reason here? Just to force reada for all tree leaves?

Thanks,
Qu

Quoted 4 lines
fs/btrfs/volumes.c +0 -0
 	/*
 	 * uuid_mutex is needed only if we are mounting a sprout FS
 
#3 lore raw
Quoted 5 lines · Qu Wenruo
Quoted 2 lines · robbieko

From: Robbie Ko <[email protected]>

Quoted 7 lines
Quoted 4 lines

When mounting, we always need to read the whole chunk tree, when there are too many chunk items, most of the time is spent on btrfs_read_chunk_tree, because we only read one leaf at a time.

Well, under most case it would be btrfs_read_block_groups(), unless all data chunks are very compact with just several large data extents.

I've checked chunk tree on some filesystems:

- 1T, 40% used, chunk tree size 80K, 1 node, the rest are leaves
- 1T, 93% used, chunk tree size 112K, 1 node, the rest are leaves

so yeah readahead of chunk tree is not the part where it takes long. For many-terabytes filesystems it would be stil in range of megabytes and the chunk tree is not scattered.

We could do the readahead of block group items, it could speed up some things and maybe worth trying. We have the async readahead API, ie. start readahead on a given key and forget about it. Either it will be in cache in time we read it or the proper read will be first.

Quoted 23 lines
Quoted 16 lines
fs/btrfs/ctree.h +1 -1
@@ -353,7 +353,7 @@ struct btrfs_node {
  * The slots array records the index of the item or block pointer
  * used while walking the tree.
  */
-enum { READA_NONE, READA_BACK, READA_FORWARD };
+enum { READA_NONE, READA_BACK, READA_FORWARD, READA_FORWARD_FORCE };
 struct btrfs_path {
 	struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
 	int slots[BTRFS_MAX_LEVEL];
fs/btrfs/volumes.c +0 -0
index 0d6e785bcb98..78fd65abff69 100644

fs/btrfs/volumes.c int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) · line 7046

Quoted 4 lines
fs/btrfs/volumes.c +1 -0
@@ -7043,6 +7043,7 @@ int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
 	path = btrfs_alloc_path();
 	if (!path)
 		return -ENOMEM;
+	path->reada = READA_FORWARD_FORCE;

Why not just use regular forward readahead?

Quoted 1 line

Mind to share the reason here? Just to force reada for all tree leaves?

Maybe the current readahead is a good idea to do here anyway, we know we'll need to read the whole chunk tree anyway so it's not wasteful.