btrfs: set of small optimizations for inode logging
cover btrfs: set of small optimizations for inode logging
From: Filipe Manana <[email protected]>
The following patchset adds a few optimizations for inode logging, along with some necessary refactorings/cleanups to be able to implement them. Test results are in the change log of the last patch.
Filipe Manana (10):
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
fs/btrfs/tree-log.c | 208 +++++++++++++++++++++++--------------------- 1 file changed, 110 insertions(+), 98 deletions(-)
Signature · 1 line
2.28.0
PATCH 1/10 btrfs: check if a log tree exists at inode_logged()
From: Filipe Manana <[email protected]>
In case an inode was never logged since it was loaded from disk and was modified in the current transaction (its ->last_trans matches the ID of the current transaction), inode_logged() returns true even if there's no existing log tree. In this case we can simply check if a log tree exists and return false if it does not. This avoids a caller of inode_logged() doing some unnecessary, but harmless, work.
For btrfs_log_new_name() it avoids it logging an inode in case it was never logged since it was loaded from disk and there is currently no log tree for the inode's root. For the remaining callers of inode_logged(), btrfs_del_dir_entries_in_log() and btrfs_del_inode_ref_in_log(), it has no effect since they already check if a log tree exists through their calls to join_running_log_trans().
So just add a check to inode_logged() to verify if a log tree exists, and return false if it does not.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 1/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 3 +++ 1 file changed, 3 insertions(+)
fs/btrfs/tree-log.c +3 -0
@@ -3434,6 +3434,9 @@ static bool inode_logged(struct btrfs_trans_handle *trans,if (inode->logged_trans == trans->transid)return true; +if (!test_bit (BTRFS_ROOT_HAS_LOG_TREE , &inode->root->state)) +return false; +/* * The inode's logged_trans is always 0 when we load it (because it is * not persisted in the inode item or elsewhere). So if it is 0, the
Signature · 1 line
2.28.0
PATCH 2/10 btrfs: remove no longer needed checks for NULL log context
In reply to [email protected]From: Filipe Manana <[email protected]>
Since commit 75b463d2b47aef ("btrfs: do not commit logs and transactions during link and rename operations"), we always pass a non-NULL log context to btrfs_log_inode_parent() and therefore to all the functions that it calls. So remove the checks we have all over the place that test for a NULL log context, making the code shorter and easier to read, as well as reducing the size of the generated code.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 2/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
fs/btrfs/tree-log.c +7 -13
@@ -207,7 +207,7 @@ static int start_log_trans(struct btrfs_trans_handle *trans, }atomic_inc (&root->log_writers); -if (ctx && !ctx->logging_new_name) { +if (!ctx->logging_new_name) {int index = root->log_transid %2 ;list_add_tail (&ctx->list, &root->log_ctxs[index]); ctx->log_transid = root->log_transid;
@@ -3019,9 +3019,6 @@ static void wait_for_writer(struct btrfs_root *root)static inline void btrfs_remove_log_ctx (struct btrfs_root *root,struct btrfs_log_ctx *ctx) { -if (!ctx) -return ; -mutex_lock (&root->log_mutex);list_del_init (&ctx->list);mutex_unlock (&root->log_mutex);
@@ -3765,8 +3762,7 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans,*/ di = btrfs_item_ptr(src, i, struct btrfs_dir_item);btrfs_dir_item_key_to_cpu (src, di, &tmp); -if (ctx && - (btrfs_dir_transid (src, di) == trans->transid || +if ((btrfs_dir_transid (src, di) == trans->transid ||btrfs_dir_type (src, di) ==BTRFS_FT_DIR ) && tmp.type !=BTRFS_ROOT_ITEM_KEY ) ctx->log_new_dentries = true;
@@ -5225,7 +5221,7 @@ static int copy_inode_items_to_log(struct btrfs_trans_handle *trans, &other_ino, &other_parent);if (ret <0 ) {return ret; - }else if (ret >0 && ctx && + }else if (ret >0 && other_ino !=btrfs_ino (BTRFS_I (ctx->inode))) {if (ins_nr >0 ) { ins_nr++;
@@ -5566,8 +5562,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,* So keep it simple for this case and just don't flag the ancestors as * logged. */ -if (!ctx || - !(S_ISDIR (inode->vfs_inode.i_mode) && ctx->logging_new_name && +if (!(S_ISDIR (inode->vfs_inode.i_mode) && ctx->logging_new_name && &inode->vfs_inode != ctx->inode)) {spin_lock (&inode->lock); inode->logged_trans = trans->transid;
@@ -5920,11 +5915,10 @@ static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,continue ; } -if (ctx) - ctx->log_new_dentries = false; + ctx->log_new_dentries = false; ret =btrfs_log_inode (trans, root,BTRFS_I (dir_inode),LOG_INODE_ALL , ctx); -if (!ret && ctx && ctx->log_new_dentries) +if (!ret && ctx->log_new_dentries) ret =log_new_dir_dentries (trans, root,BTRFS_I (dir_inode), ctx);btrfs_add_delayed_iput (dir_inode);
@@ -6185,7 +6179,7 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, goto end_trans; } -if (S_ISDIR (inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries) +if (S_ISDIR (inode->vfs_inode.i_mode) && ctx->log_new_dentries) log_dentries = true;/*
Signature · 1 line
2.28.0
PATCH 3/10 btrfs: do not log new dentries when logging that a new name exists
In reply to [email protected]From: Filipe Manana <[email protected]>
When logging a new name for an inode, due to a link or rename operation, we don't need to log all new dentries of the parent directories and their subdirectories. We only want to log the names of the inode and that any new parent directories exist. So in this case don't trigger logging of the new dentries, that is only need when doing an explicit fsync on a directory or on a file which requires logging its parent directories.
This avoids unnecessary work and reduces contention on the extent buffers of a log tree.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 3/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/btrfs/tree-log.c +8 -0
@@ -5695,6 +5695,14 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,struct btrfs_dir_list *dir_elem;int ret =0 ; +/* +* If we are logging a new name, as part of a link or rename operation, +* don't bother logging new dentries, as we just want to log the names +* of an inode and that any new parents exist. +*/ +if (ctx->logging_new_name) +return 0 ; + path =btrfs_alloc_path ();if (!path)return -ENOMEM ;
Signature · 1 line
2.28.0
PATCH 4/10 btrfs: always update the logged transaction when logging new names
In reply to [email protected]From: Filipe Manana <[email protected]>
When we are logging a new name for an inode, due to a link or rename operation, if the inode has ancestor inodes that are new, created in the current transaction, we need to log that these inodes exist. To ensure that a subsequent explicit fsync on one of these ancestor inodes does sync the log, we don't set the logged_trans field of these inodes. This was done in commit 75b463d2b47aef ("btrfs: do not commit logs and transactions during link and rename operations"), to avoid syncing a log after a rename or link operation.
In order to allow for future changes to do some optimizations, change this behaviour to always update the logged_trans of any logged inode and don't update the last_log_commit of the inode if we are logging that it exists. This accomplishes that same objective with simpler logic, allowing for some optimizations in the next patches.
So just do that simplification.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 4/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 73 +++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 39 deletions(-)
fs/btrfs/tree-log.c +34 -39
@@ -5552,47 +5552,42 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans, } } +spin_lock (&inode->lock); + inode->logged_trans = trans->transid;/* -* If we are logging that an ancestor inode exists as part of logging a -* new name from a link or rename operation, don't mark the inode as -* logged - otherwise if an explicit fsync is made against an ancestor, -* the fsync considers the inode in the log and doesn't sync the log, -* resulting in the ancestor missing after a power failure unless the -* log was synced as part of an fsync against any other unrelated inode. -* So keep it simple for this case and just don't flag the ancestors as -* logged. +* Don't update last_log_commit if we logged that an inode exists. +* We do this for three reasons: +* +* 1) We might have had buffered writes to this inode that were +* flushed and had their ordered extents completed in this +* transaction, but we did not previously log the inode with +* LOG_INODE_ALL. Later the inode was evicted and after that +* it was loaded again and this LOG_INODE_EXISTS log operation +* happened. We must make sure that if an explicit fsync against +* the inode is performed later, it logs the new extents, an +* updated inode item, etc, and syncs the log. The same logic +* applies to direct IO writes instead of buffered writes. +* +* 2) When we log the inode with LOG_INODE_EXISTS, its inode item +* is logged with an i_size of 0 or whatever value was logged +* before. If later the i_size of the inode is increased by a +* truncate operation, the log is synced through an fsync of +* some other inode and then finally an explicit fsync against +* this inode is made, we must make sure this fsync logs the +* inode with the new i_size, the hole between old i_size and +* the new i_size, and syncs the log. +* +* 3) If we are logging that an ancestor inode exists as part of +* logging a new name from a link or rename operation, don't update +* its last_log_commit - otherwise if an explicit fsync is made +* against an ancestor, the fsync considers the inode in the log +* and doesn't sync the log, resulting in the ancestor missing after +* a power failure unless the log was synced as part of an fsync +* against any other unrelated inode. */ -if (!(S_ISDIR (inode->vfs_inode.i_mode) && ctx->logging_new_name && - &inode->vfs_inode != ctx->inode)) { -spin_lock (&inode->lock); - inode->logged_trans = trans->transid; -/* -* Don't update last_log_commit if we logged that an inode exists. -* We do this for two reasons: -* -* 1) We might have had buffered writes to this inode that were -* flushed and had their ordered extents completed in this -* transaction, but we did not previously log the inode with -* LOG_INODE_ALL. Later the inode was evicted and after that -* it was loaded again and this LOG_INODE_EXISTS log operation -* happened. We must make sure that if an explicit fsync against -* the inode is performed later, it logs the new extents, an -* updated inode item, etc, and syncs the log. The same logic -* applies to direct IO writes instead of buffered writes. -* -* 2) When we log the inode with LOG_INODE_EXISTS, its inode item -* is logged with an i_size of 0 or whatever value was logged -* before. If later the i_size of the inode is increased by a -* truncate operation, the log is synced through an fsync of -* some other inode and then finally an explicit fsync against -* this inode is made, we must make sure this fsync logs the -* inode with the new i_size, the hole between old i_size and -* the new i_size, and syncs the log. -*/ -if (inode_only !=LOG_INODE_EXISTS ) - inode->last_log_commit = inode->last_sub_trans; -spin_unlock (&inode->lock); - } +if (inode_only !=LOG_INODE_EXISTS ) + inode->last_log_commit = inode->last_sub_trans; +spin_unlock (&inode->lock); out_unlock:mutex_unlock (&inode->log_mutex);
Signature · 1 line
2.28.0
PATCH 5/10 btrfs: avoid expensive search when dropping inode items from log
In reply to [email protected]From: Filipe Manana <[email protected]>
Whenever we are logging a directory inode, logging that an inode exists or logging an inode that has changes in its references or xattrs, we attempt to delete items of this inode we may have previously logged (through calls to drop_objectid_items()).
That attempt does a btree search for deletion, which is expensive because it always acquires write locks for extent buffers at levels 2, 1 and 0, and it balances any node that is less than half full. Acquiring the write locks can block the task if the extent buffers are already locked or block other tasks attempting to lock them, which is specially bad in case of log trees since they are small due to their short life, with a root node at a level typically not greater than level 2.
If we know that we are logging the inode for the first time in the current transaction, we can skip the search. This change does that.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 5/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
fs/btrfs/tree-log.c +13 -9
@@ -3864,17 +3864,21 @@ static noinline int log_directory_changes(struct btrfs_trans_handle *trans,* This cannot be run for file data extents because it does not * free the extents they point to. */ -static int drop_objectid_items (struct btrfs_trans_handle *trans, +static int drop_inode_items (struct btrfs_trans_handle *trans,struct btrfs_root *log,struct btrfs_path *path, -u64 objectid,int max_key_type) +struct btrfs_inode *inode, +int max_key_type) {int ret;struct btrfs_key key;struct btrfs_key found_key;int start_slot; - key.objectid = objectid; +if (!inode_logged (trans, inode)) +return 0 ; + + key.objectid =btrfs_ino (inode); key.type = max_key_type; key.offset = (u64)-1 ;
@@ -3891,7 +3895,7 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans,btrfs_item_key_to_cpu (path->nodes[0 ], &found_key, path->slots[0 ]); -if (found_key.objectid != objectid) +if (found_key.objectid != key.objectid)break ; found_key.offset =0 ;
@@ -5425,7 +5429,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,clear_bit (BTRFS_INODE_COPY_EVERYTHING , &inode->runtime_flags);if (inode_only ==LOG_INODE_EXISTS ) max_key_type =BTRFS_XATTR_ITEM_KEY ; - ret =drop_objectid_items (trans, log, path, ino, max_key_type); + ret =drop_inode_items (trans, log, path, inode, max_key_type); }else {if (inode_only ==LOG_INODE_EXISTS ) {/*
@@ -5449,8 +5453,8 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans, &inode->runtime_flags)) {if (inode_only ==LOG_INODE_EXISTS ) { max_key.type =BTRFS_XATTR_ITEM_KEY ; - ret =drop_objectid_items (trans, log, path, ino, - max_key.type); + ret =drop_inode_items (trans, log, path, inode, + max_key.type); }else {clear_bit (BTRFS_INODE_NEEDS_FULL_SYNC , &inode->runtime_flags);
@@ -5469,8 +5473,8 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,if (inode_only ==LOG_INODE_ALL ) fast_search = true; max_key.type =BTRFS_XATTR_ITEM_KEY ; - ret =drop_objectid_items (trans, log, path, ino, - max_key.type); + ret =drop_inode_items (trans, log, path, inode, + max_key.type); }else {if (inode_only ==LOG_INODE_ALL ) fast_search = true;
Signature · 1 line
2.28.0
PATCH 6/10 btrfs: add helper to truncate inode items when logging inode
In reply to [email protected]From: Filipe Manana <[email protected]>
Move the call to btrfs_truncate_inode_items(), and the surrounding retry loop, into a local helper function. This avoids some repetition and avoids making the next change a bit awkward due to a bit of too much indentation.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 6/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-)
fs/btrfs/tree-log.c +19 -13
@@ -3920,6 +3920,21 @@ static int drop_inode_items(struct btrfs_trans_handle *trans,return ret; } +static int truncate_inode_items (struct btrfs_trans_handle *trans, +struct btrfs_root *log_root, +struct btrfs_inode *inode, +u64 new_size,u32 min_type) +{ +int ret; + +do { + ret =btrfs_truncate_inode_items (trans, log_root, inode, + new_size, min_type,NULL ); + }while (ret == -EAGAIN ); + +return ret; +} + static void fill_inode_item(struct btrfs_trans_handle *trans, struct extent_buffer *leaf, struct btrfs_inode_item *item,
@@ -4508,13 +4523,9 @@ static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,* Avoid logging extent items logged in past fsync calls * and leading to duplicate keys in the log tree. */ -do { - ret =btrfs_truncate_inode_items (trans, - root->log_root, - inode, truncate_offset, -BTRFS_EXTENT_DATA_KEY , -NULL ); - }while (ret == -EAGAIN ); + ret =truncate_inode_items (trans, root->log_root, inode, + truncate_offset, +BTRFS_EXTENT_DATA_KEY );if (ret) goto out; dropped_extents = true;
@@ -5460,12 +5471,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans, &inode->runtime_flags);clear_bit (BTRFS_INODE_COPY_EVERYTHING , &inode->runtime_flags); -while (1 ) { - ret =btrfs_truncate_inode_items (trans, - log, inode,0 ,0 ,NULL ); -if (ret != -EAGAIN ) -break ; - } + ret =truncate_inode_items (trans, log, inode,0 ,0 ); } } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags) ||
Signature · 1 line
2.28.0
PATCH 7/10 btrfs: avoid expensive search when truncating inode items from the log
In reply to [email protected]From: Filipe Manana <[email protected]>
Whenever we are logging a file inode in full sync mode we call btrfs_truncate_inode_items() to delete items of the inode we may have previously logged.
That results in doing a btree search for deletion, which is expensive because it always acquires write locks for extent buffers at levels 2, 1 and 0, and it balances any node that is less than half full. Acquiring the write locks can block the task if the extent buffers are already locked by another task or block other tasks attempting to lock them, which is specially bad in case of log trees since they are small due to their short life, with a root node at a level typically not greater than level 2.
If we know that we are logging the inode for the first time in the current transaction, we can skip the call to btrfs_truncate_inode_items(), avoiding the deletion search. This change does that.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 7/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/btrfs/tree-log.c +3 -1
@@ -5471,7 +5471,9 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans, &inode->runtime_flags);clear_bit (BTRFS_INODE_COPY_EVERYTHING , &inode->runtime_flags); - ret =truncate_inode_items (trans, log, inode,0 ,0 ); +if (inode_logged (trans, inode)) + ret =truncate_inode_items (trans, log, + inode,0 ,0 ); } } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags) ||
Signature · 1 line
2.28.0
PATCH 8/10 btrfs: avoid search for logged i_size when logging inode if possible
In reply to [email protected]From: Filipe Manana <[email protected]>
If we are logging that an inode exists and the inode was not logged before, we can avoid searching in the log tree for the inode item since we know it does not exists. That wastes time and adds more lock contention on the extent buffers of the log tree when there are other tasks that are logging other inodes.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 8/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/tree-log.c +1 -1
@@ -5442,7 +5442,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans, max_key_type =BTRFS_XATTR_ITEM_KEY ; ret =drop_inode_items (trans, log, path, inode, max_key_type); }else { -if (inode_only ==LOG_INODE_EXISTS ) { +if (inode_only ==LOG_INODE_EXISTS &&inode_logged (trans, inode)) {/* * Make sure the new inode item we write to the log has * the same isize as the current one (if it exists).
Signature · 1 line
2.28.0
PATCH 9/10 btrfs: avoid attempt to drop extents when logging inode for the first time
In reply to [email protected]From: Filipe Manana <[email protected]>
When logging an extent, in the fast fsync path, we always attempt do drop or trim any existing extents with a range that match or overlap the range of the extent we are about to log. We do that through a call to btrfs_drop_extents().
However this is not needed when we are logging the inode for the first time in the current transaction, since we have no inode items of the inode in the log tree. Calling btrfs_drop_extents() does a deletion search on the log tree, which is expensive when we have concurrent tasks accessing the log tree because a deletion search always acquires a write lock on the extent buffers at levels 2, 1 and 0, adding significant lock contention, specially taking into account the height of a log tree rarely (if ever) goes beyond 2 or 3, due to its short life.
So skip the call to btrfs_drop_extents() when the inode was not previously logged in the current transaction.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 9/10 and test results are listed in the change log of the last patch in the set.
--- fs/btrfs/tree-log.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
fs/btrfs/tree-log.c +19 -8
@@ -4358,14 +4358,25 @@ static int log_one_extent(struct btrfs_trans_handle *trans,if (ret)return ret; - drop_args.path = path; - drop_args.start = em->start; - drop_args.end = em->start + em->len; - drop_args.replace_extent = true; - drop_args.extent_item_size =sizeof (*fi); - ret =btrfs_drop_extents (trans, log, inode, &drop_args); -if (ret) -return ret; +/* +* If this is the first time we are logging the inode in the current +* transaction, we can avoid btrfs_drop_extents(), which is expensive +* because it does a deletion search, which always acquires write locks +* for extent buffers at levels 2, 1 and 0. This not only wastes time +* but also adds significant contention in a log tree, since log trees +* are small, with a root at level 2 or 3 at most, due to their short +* life span. +*/ +if (inode_logged (trans, inode)) { + drop_args.path = path; + drop_args.start = em->start; + drop_args.end = em->start + em->len; + drop_args.replace_extent = true; + drop_args.extent_item_size =sizeof (*fi); + ret =btrfs_drop_extents (trans, log, inode, &drop_args); +if (ret) +return ret; + }if (!drop_args.extent_inserted) { key.objectid =btrfs_ino (inode);
Signature · 1 line
2.28.0
PATCH 10/10 btrfs: do not commit delayed inode when logging a file in full sync mode
In reply to [email protected]From: Filipe Manana <[email protected]>
When logging a regular file in full sync mode, we are currently committing its delayed inode item. This is to ensure that we never miss copying the inode item, with its most up to date data, into the log tree.
However that is not necessary since commit e4545de5b035 ("Btrfs: fix fsync data loss after append write"), because even if we don't find the leaf with the inode item when looking for leaves that changed in the current transaction, we end up logging the inode item later using the in-memory content. In case we find the leaf containing the inode item, we already end up using the in-memory inode for filling the inode item in the log tree, and not the inode item that is in the fs/subvolume tree, as it might be not up to date (copy_items() -> fill_inode_item()).
So don't commit the delayed inode item, which brings a couple of benefits:
1) Avoid writing the inode item to the fs/subvolume btree, saving time and reducing lock contention on the btree;
2) In case no other item for the inode was changed, added or deleted in the same leaf where the inode item is located, we ended up copying all the items in that leaf to the log tree - it's harmless from a functional point of view, but it wastes time and log tree space.
This patch is part of a patch set comprised of the following patches:
btrfs: check if a log tree exists at inode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for logged i_size when logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
This is patch 10/10 and the following test results compare a branch with the whole patch set applied versus a branch without any of the patches applied.
The following script was used to test dbench with 8 and 16 jobs on a machine with 12 cores, 64G of RAM, a NVME device and using a non-debug kernel config (Debian's default):
$ cat test.sh #!/bin/bash
if [ $# -ne 1 ]; then
echo "Use $0 NUM_JOBS"
exit 1
fi
NUM_JOBS=$1
DEV=/dev/nvme0n1
MNT=/mnt/nvme0n1
MOUNT_OPTIONS="-o ssd"
MKFS_OPTIONS="-m single -d single"
echo "performance" | \
tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor mkfs.btrfs -f $MKFS_OPTIONS $DEV
mount $MOUNT_OPTIONS $DEV $MNT
dbench -D $MNT -t 120 $NUM_JOBS
umount $MNT
The results were the following:
8 jobs, before patchset:
Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 4113896 0.009 238.665 Close 3021699 0.001 0.590 Rename 174215 0.082 238.733 Unlink 830977 0.049 238.642 Deltree 96 2.232 8.022 Mkdir 48 0.003 0.005 Qpathinfo 3729013 0.005 2.672 Qfileinfo 653206 0.001 0.152 Qfsinfo 683866 0.002 0.526 Sfileinfo 335055 0.004 1.571 Find 1441800 0.016 4.288 WriteX 2049644 0.010 3.982 ReadX 6449786 0.003 0.969 LockX 13400 0.002 0.043 UnlockX 13400 0.001 0.075 Flush 288349 2.521 245.516
Throughput 1075.73 MB/sec 8 clients 8 procs max_latency=245.520 ms
8 jobs, after patchset:
Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 4154282 0.009 156.675 Close 3051450 0.001 0.843 Rename 175912 0.072 4.444 Unlink 839067 0.048 66.050 Deltree 96 2.131 5.979 Mkdir 48 0.002 0.004 Qpathinfo 3765575 0.005 3.079 Qfileinfo 659582 0.001 0.099 Qfsinfo 690474 0.002 0.155 Sfileinfo 338366 0.004 1.419 Find 1455816 0.016 3.423 WriteX 2069538 0.010 4.328 ReadX 6512429 0.003 0.840 LockX 13530 0.002 0.078 UnlockX 13530 0.001 0.051 Flush 291158 2.500 163.468
Throughput 1105.45 MB/sec 8 clients 8 procs max_latency=163.474 ms
+2.7% throughput, -40.1% max latency
16 jobs, before patchset:
Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 5457602 0.033 337.098 Close 4008979 0.002 2.018 Rename 231051 0.323 254.054 Unlink 1102209 0.202 337.243 Deltree 160 6.521 31.720 Mkdir 80 0.003 0.007 Qpathinfo 4946147 0.014 6.988 Qfileinfo 867440 0.001 1.642 Qfsinfo 907081 0.003 1.821 Sfileinfo 444433 0.005 2.053 Find 1912506 0.067 7.854 WriteX 2724852 0.018 7.428 ReadX 8553883 0.003 2.059 LockX 17770 0.003 0.350 UnlockX 17770 0.002 0.627 Flush 382533 2.810 353.691
Throughput 1413.09 MB/sec 16 clients 16 procs max_latency=353.696 ms
16 jobs, after patchset:
Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 5393156 0.034 303.181 Close 3961986 0.002 1.502 Rename 228359 0.320 253.379 Unlink 1088920 0.206 303.409 Deltree 160 6.419 30.088 Mkdir 80 0.003 0.004 Qpathinfo 4887967 0.015 7.722 Qfileinfo 857408 0.001 1.651 Qfsinfo 896343 0.002 2.147 Sfileinfo 439317 0.005 4.298 Find 1890018 0.073 8.347 WriteX 2693356 0.018 6.373 ReadX 8453485 0.003 3.836 LockX 17562 0.003 0.486 UnlockX 17562 0.002 0.635 Flush 378023 2.802 315.904
Throughput 1454.46 MB/sec 16 clients 16 procs max_latency=315.910 ms
+2.9% throughput, -11.3% max latency
--- fs/btrfs/tree-log.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-)
fs/btrfs/tree-log.c +5 -16
@@ -5397,22 +5397,11 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,* Only run delayed items if we are a directory. We want to make sure * all directory indexes hit the fs/subvolume tree so we can find them * and figure out which index ranges have to be logged. -* -* Otherwise commit the delayed inode only if the full sync flag is set, -* as we want to make sure an up to date version is in the subvolume -* tree so copy_inode_items_to_log() / copy_items() can find it and copy -* it to the log tree. For a non full sync, we always log the inode item -* based on the in-memory struct btrfs_inode which is always up to date. */ -if (S_ISDIR (inode->vfs_inode.i_mode)) - ret =btrfs_commit_inode_delayed_items (trans, inode); -else if (test_bit (BTRFS_INODE_NEEDS_FULL_SYNC , &inode->runtime_flags)) - ret =btrfs_commit_inode_delayed_inode (inode); - -if (ret) { -btrfs_free_path (path); -btrfs_free_path (dst_path); -return ret; +if (S_ISDIR (inode->vfs_inode.i_mode)) { + err =btrfs_commit_inode_delayed_items (trans, inode); +if (err) + goto out; }if (inode_only ==LOG_OTHER_INODE || inode_only ==LOG_OTHER_INODE_ALL ) {
@@ -5613,7 +5602,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,spin_unlock (&inode->lock); out_unlock:mutex_unlock (&inode->log_mutex); - +out:btrfs_free_path (path);btrfs_free_path (dst_path);return err;
Signature · 1 line
2.28.0
Quoted 6 lines · On Tue, Aug 31, 2021 at 03:30:30PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
The following patchset adds a few optimizations for inode logging, along with some necessary refactorings/cleanups to be able to implement them. Test results are in the change log of the last patch.
Quoted 11 lines
Filipe Manana (10):
btrfs: check if a log tree exists atinode_logged()
btrfs: remove no longer needed checks for NULL log context
btrfs: do not log new dentries when logging that a new name exists
btrfs: always update the logged transaction when logging new names
btrfs: avoid expensive search when dropping inode items from log
btrfs: add helper to truncate inode items when logging inode
btrfs: avoid expensive search when truncating inode items from the log
btrfs: avoid search for loggedi_sizewhen logging inode if possible
btrfs: avoid attempt to drop extents when logging inode for the first time
btrfs: do not commit delayed inode when logging a file in full sync mode
Added to misc-next, thanks.