btrfs: reduce extent tree lock contention when searching for inline backref
PATCH 1/1 btrfs: reduce extent tree lock contention when searching for inline backref
From: Robbie Ko <[email protected]>
When inserting extent backref, in order to check whether refs other than inline refs are used, we always use keep locks for tree search, which will increase the lock contention of extent-tree.
We do not need the parent node every time to determine whether normal refs are used.
It is only needed when the extent-item is the last item in a leaf.
Therefore, we change to first use keep_locks=0 for search. If the extent-item happens to be the last item in the leaf, we then change to keep_locks=1 for the second search to reduce lock contention.
--- fs/btrfs/extent-tree.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)
fs/btrfs/extent-tree.c +23 -3
@@ -795,7 +795,6 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,if (insert) { extra_size =btrfs_extent_inline_ref_size (want); path->search_for_extension =1 ; - path->keep_locks =1 ; }else extra_size =-1 ;
@@ -946,6 +945,25 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, ret = -EAGAIN ; goto out; } + +if (path->slots[0 ] +1 <btrfs_header_nritems (path->nodes[0 ])) { +struct btrfs_key tmp_key; + +btrfs_item_key_to_cpu (path->nodes[0 ], &tmp_key, path->slots[0 ] +1 ); +if (tmp_key.objectid == bytenr && + tmp_key.type <BTRFS_BLOCK_GROUP_ITEM_KEY ) { + ret = -EAGAIN ; + goto out; + } + goto enoent; + } + +if (!path->keep_locks) { +btrfs_release_path (path); + path->keep_locks =1 ; + goto again; + } +/* * To add new inline back ref, we have to make sure * there is no corresponding back ref item.
@@ -959,13 +977,15 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, goto out; } } +enoent: *ref_ret = (struct btrfs_extent_inline_ref *)ptr; out: -if (insert) { +if (path->keep_locks) { path->keep_locks =0 ; - path->search_for_extension =0 ;btrfs_unlock_up_safe (path,1 ); } +if (insert) + path->search_for_extension =0 ;return ret; }
Signature · 1 line
2.17.1
Quoted 11 lines · robbieko
From: Robbie Ko <[email protected]>
When inserting extent backref, in order to check whether refs other than inline refs are used, we always use keep locks for tree search, which will increase the lock contention of extent-tree.
We do not need the parent node every time to determine whether normal refs are used.
It is only needed when the extent-item is the last item in a leaf.
Quoted 5 lines
Therefore, we change to first use keep_locks=0 for search. If the extent-item happens to be the last item in the leaf, we then change to keep_locks=1 for the second search to reduce lock contention.
Signed-off-by Robbie Ko <[email protected]>
Looks good now, thanks.
Quoted 64 lines
--- fs/btrfs/extent-tree.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)fs/btrfs/extent-tree.c +23
-4@@ -795,7 +795,6 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,if (insert) { extra_size =btrfs_extent_inline_ref_size (want); path->search_for_extension =1 ; - path->keep_locks =1 ; }else extra_size =-1 ;@@ -946,6 +945,25 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, ret = -EAGAIN ; goto out; } + +if (path->slots[0 ] +1 <btrfs_header_nritems (path->nodes[0 ])) { +struct btrfs_key tmp_key; + +btrfs_item_key_to_cpu (path->nodes[0 ], &tmp_key, path->slots[0 ] +1 ); +if (tmp_key.objectid == bytenr && + tmp_key.type <BTRFS_BLOCK_GROUP_ITEM_KEY ) { + ret = -EAGAIN ; + goto out; + } + goto enoent; + } + +if (!path->keep_locks) { +btrfs_release_path (path); + path->keep_locks =1 ; + goto again; + } +/* * To add new inline back ref, we have to make sure * there is no corresponding back ref item. @@ -959,13 +977,15 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, goto out; } } +enoent: *ref_ret = (struct btrfs_extent_inline_ref *)ptr; out: -if (insert) { +if (path->keep_locks) { path->keep_locks =0 ; - path->search_for_extension =0 ;btrfs_unlock_up_safe (path,1 ); } +if (insert) + path->search_for_extension =0 ;return ret; } --2.17.1