btrfs: speedup bulk item insertions
cover btrfs: speedup bulk item insertions
From: Filipe Manana <[email protected]>
This patchset does some minor improvements to speedup bulk insertion of items into a btree, which is used when logging directories, when running delayed items for directories (fsync and transaction commits) and when running the slow path (full sync) of an fsync. The last patch in the series contains test results in its changelog.
Filipe Manana (3):
btrfs: loop only once over data sizes array when inserting an item batch
btrfs: unexport setup_items_for_insert()
btrfs: use single bulk copy operations when logging directories
fs/btrfs/ctree.c | 148 +++++++++++++++------------ fs/btrfs/ctree.h | 45 ++++++-- fs/btrfs/delayed-inode.c | 41 ++++---- fs/btrfs/file.c | 3 +- fs/btrfs/inode.c | 8 +- fs/btrfs/tests/extent-buffer-tests.c | 2 +- fs/btrfs/tests/inode-tests.c | 4 +- fs/btrfs/tree-log.c | 52 +++++++--- 8 files changed, 188 insertions(+), 115 deletions(-)
Signature · 1 line
2.33.0
PATCH 1/3 btrfs: loop only once over data sizes array when inserting an item batch
From: Filipe Manana <[email protected]>
When inserting a batch of items into a btree, we end up looping over the data sizes array 3 times:
1) Once in the caller of btrfs_insert_empty_items(), when it populates the array with the data sizes for each item;
2) Once at btrfs_insert_empty_items() to sum the elements of the data sizes array and compute the total data size;
3) And then once again at setup_items_for_insert(), where we do exactly the same as what we do at btrfs_insert_empty_items(), to compute the total data size.
That is not bad for small arrays, but when the arrays have hundreds of elements, the time spent on looping is not negligible. For example when doing batch inserts of delayed items for dir index items or when logging a directory, it's common to have 200 to 260 dir index items in a single batch when using a leaf size of 16K and using file names between 8 and 12 characters. For a 64K leaf size, multiply that by 4. Taking into account that during directory logging or when flushing delayed dir index items we can have many of those large batches, the time spent on the looping adds up quickly.
It's also more important to avoid it at setup_items_for_insert(), since we are holding a write lock on a leaf and, in some cases, on upper nodes of the btree, which causes us to block other tasks that want to access the leaf and nodes for longer than necessary.
So change the code so that setup_items_for_insert() and btrfs_insert_empty_items() no longer compute the total data size, and instead rely on the caller to supply it. This makes us loop over the array only once, where we can both populate the data size array and compute the total data size, taking advantage of spatial and temporal locality. To make this more manageable, use a structure to contain all the relevant details for a batch of items (keys array, data sizes array, total data size, number of items), and use it as an argument for btrfs_insert_empty_items() and setup_items_for_insert().
This patch is part of a small patchset that is comprised of the following patches:
btrfs: loop only once over data sizes array when inserting an item batch
btrfs: unexport setup_items_for_insert()
btrfs: use single bulk copy operations when logging directories
This is patch 1/3 and performance results, and the specific tests, are included in the changelog of patch 3/3.
--- fs/btrfs/ctree.c | 57 ++++++++++++---------------- fs/btrfs/ctree.h | 57 +++++++++++++++++++++++++--- fs/btrfs/delayed-inode.c | 41 ++++++++++---------- fs/btrfs/file.c | 3 +- fs/btrfs/inode.c | 8 +++- fs/btrfs/tests/extent-buffer-tests.c | 2 +- fs/btrfs/tests/inode-tests.c | 4 +- fs/btrfs/tree-log.c | 31 ++++++++++----- 8 files changed, 131 insertions(+), 72 deletions(-)
fs/btrfs/ctree.c +25 -32
@@ -3605,7 +3605,7 @@ int btrfs_duplicate_item(struct btrfs_trans_handle *trans,return ret; path->slots[0 ]++; -setup_items_for_insert (root, path, new_key, &item_size,1 ); +setup_item_for_insert (root, path, new_key, item_size); leaf = path->nodes[0 ]; memcpy_extent_buffer(leaf, btrfs_item_ptr_offset(leaf, path->slots[0]),
@@ -3785,13 +3785,10 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size)* * @root: root we are inserting items to * @path: points to the leaf/slot where we are going to insert new items -* @cpu_key: array of keys for items to be inserted -* @data_size: size of the body of each item we are going to insert -* @nr: size of @cpu_key/@data_size arrays +* @batch: information about the batch of items to insert */ void setup_items_for_insert (struct btrfs_root *root,struct btrfs_path *path, -const struct btrfs_key *cpu_key,u32 *data_size, -int nr) +const struct btrfs_item_batch *batch) {struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_item *item;
@@ -3803,14 +3800,14 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,int slot;struct btrfs_map_token token;u32 total_size; -u32 total_data =0 ; - -for (i =0 ; i < nr; i++) - total_data += data_size[i]; - total_size = total_data + (nr *sizeof (struct btrfs_item )); +/* +* Before anything else, update keys in the parent and other ancestors +* if needed, then release the write locks on them, so that other tasks +* can use them while we modify the leaf. +*/ if (path->slots[0 ] ==0 ) { -btrfs_cpu_key_to_disk (&disk_key, cpu_key); +btrfs_cpu_key_to_disk (&disk_key, &batch->keys[0 ]);fixup_low_keys (path, &disk_key,1 ); }btrfs_unlock_up_safe (path,1 );
@@ -3820,6 +3817,7 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, nritems =btrfs_header_nritems (leaf); data_end =leaf_data_end (leaf); + total_size = batch->total_data_size + (batch->nr *sizeof (struct btrfs_item ));if (btrfs_leaf_free_space (leaf) < total_size) {btrfs_print_leaf (leaf);
@@ -3849,31 +3847,32 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, item =btrfs_item_nr (i); ioff =btrfs_token_item_offset (&token, item);btrfs_set_token_item_offset (&token, item, - ioff - total_data); + ioff - batch->total_data_size); }/* shift the items */ -memmove_extent_buffer (leaf,btrfs_item_nr_offset (slot + nr), +memmove_extent_buffer (leaf,btrfs_item_nr_offset (slot + batch->nr),btrfs_item_nr_offset (slot), (nritems - slot) *sizeof (struct btrfs_item ));/* shift the data */ memmove_extent_buffer (leaf,BTRFS_LEAF_DATA_OFFSET + - data_end - total_data,BTRFS_LEAF_DATA_OFFSET + - data_end, old_data - data_end); + data_end - batch->total_data_size, +BTRFS_LEAF_DATA_OFFSET + data_end, + old_data - data_end); data_end = old_data; }/* setup the item for the new data */ -for (i =0 ; i < nr; i++) { -btrfs_cpu_key_to_disk (&disk_key, cpu_key + i); +for (i =0 ; i < batch->nr; i++) { +btrfs_cpu_key_to_disk (&disk_key, &batch->keys[i]);btrfs_set_item_key (leaf, &disk_key, slot + i); item =btrfs_item_nr (slot + i); - data_end -= data_size[i]; + data_end -= batch->data_sizes[i];btrfs_set_token_item_offset (&token, item, data_end); -btrfs_set_token_item_size (&token, item, data_size[i]); +btrfs_set_token_item_size (&token, item, batch->data_sizes[i]); } -btrfs_set_header_nritems (leaf, nritems + nr); +btrfs_set_header_nritems (leaf, nritems + batch->nr);btrfs_mark_buffer_dirty (leaf);if (btrfs_leaf_free_space (leaf) <0 ) {
@@ -3889,20 +3888,14 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,int btrfs_insert_empty_items (struct btrfs_trans_handle *trans,struct btrfs_root *root,struct btrfs_path *path, -const struct btrfs_key *cpu_key,u32 *data_size, -int nr) +const struct btrfs_item_batch *batch) {int ret =0 ;int slot; -int i; -u32 total_size =0 ; -u32 total_data =0 ; - -for (i =0 ; i < nr; i++) - total_data += data_size[i]; +u32 total_size; - total_size = total_data + (nr *sizeof (struct btrfs_item )); - ret =btrfs_search_slot (trans, root, cpu_key, path, total_size,1 ); + total_size = batch->total_data_size + (batch->nr *sizeof (struct btrfs_item )); + ret =btrfs_search_slot (trans, root, &batch->keys[0 ], path, total_size,1 );if (ret ==0 )return -EEXIST ;if (ret <0 )
@@ -3911,7 +3904,7 @@ int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, slot = path->slots[0 ];BUG_ON (slot <0 ); -setup_items_for_insert (root, path, cpu_key, data_size, nr); +setup_items_for_insert (root, path, batch);return 0 ; }
fs/btrfs/ctree.h +52 -5
75 lines not on this page · raw message
fs/btrfs/delayed-inode.c +22 -19
87 lines not on this page · raw message
fs/btrfs/file.c +1 -2
@@ -1021,8 +1021,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,if (btrfs_comp_cpu_keys (&key, &slot_key) >0 ) path->slots[0 ]++; } -setup_items_for_insert (root, path, &key, - &args->extent_item_size,1 ); +setup_item_for_insert (root, path, &key, args->extent_item_size); args->extent_inserted = true; }
fs/btrfs/inode.c +6 -2
@@ -6445,7 +6445,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,struct btrfs_inode_ref *ref;struct btrfs_key key[2 ];u32 sizes[2 ]; -int nitems = name ?2 :1 ; +struct btrfs_item_batch batch;unsigned long ptr;unsigned int nofs_flag;int ret;
@@ -6537,7 +6537,11 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, goto fail; } - ret =btrfs_insert_empty_items (trans, root, path, key, sizes, nitems); + batch.keys = &key[0 ]; + batch.data_sizes = &sizes[0 ]; + batch.total_data_size = sizes[0 ] + (name ? sizes[1 ] :0 ); + batch.nr = name ?2 :1 ; + ret =btrfs_insert_empty_items (trans, root, path, &batch);if (ret !=0 ) goto fail_unlock;
fs/btrfs/tests/extent-buffer-tests.c +1 -1
@@ -60,7 +60,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) key.type =BTRFS_EXTENT_CSUM_KEY ; key.offset =0 ; -setup_items_for_insert (root, path, &key, &value_len,1 ); +setup_item_for_insert (root, path, &key, value_len); item =btrfs_item_nr (0 );write_extent_buffer (eb, value,btrfs_item_ptr_offset (eb,0 ), value_len);
fs/btrfs/tests/inode-tests.c +2 -2
@@ -33,7 +33,7 @@ static void insert_extent(struct btrfs_root *root, u64 start, u64 len, key.type =BTRFS_EXTENT_DATA_KEY ; key.offset = start; -setup_items_for_insert (root, &path, &key, &value_len,1 ); +setup_item_for_insert (root, &path, &key, value_len); fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);btrfs_set_file_extent_generation (leaf, fi,1 );btrfs_set_file_extent_type (leaf, fi, type);
@@ -63,7 +63,7 @@ static void insert_inode_item_key(struct btrfs_root *root) key.type =BTRFS_INODE_ITEM_KEY ; key.offset =0 ; -setup_items_for_insert (root, &path, &key, &value_len,1 ); +setup_item_for_insert (root, &path, &key, value_len); }/*
fs/btrfs/tree-log.c +22 -9
@@ -3651,8 +3651,7 @@ static int flush_dir_items_batch(struct btrfs_trans_handle *trans, int count) {char *ins_data =NULL ; -struct btrfs_key *ins_keys; -u32 *ins_sizes; +struct btrfs_item_batch batch;struct extent_buffer *dst;struct btrfs_key key;u32 item_size;
@@ -3660,13 +3659,18 @@ static int flush_dir_items_batch(struct btrfs_trans_handle *trans,int i;ASSERT (count >0 ); + batch.nr = count;if (count ==1 ) {btrfs_item_key_to_cpu (src, &key, start_slot); item_size =btrfs_item_size_nr (src, start_slot); - ins_keys = &key; - ins_sizes = &item_size; + batch.keys = &key; + batch.data_sizes = &item_size; + batch.total_data_size = item_size; }else { +struct btrfs_key *ins_keys; +u32 *ins_sizes; + ins_data =kmalloc (count *sizeof (u32) + count *sizeof (struct btrfs_key ),GFP_NOFS );if (!ins_data)
@@ -3674,17 +3678,20 @@ static int flush_dir_items_batch(struct btrfs_trans_handle *trans, ins_sizes = (u32 *)ins_data; ins_keys = (struct btrfs_key *)(ins_data + count *sizeof (u32)); + batch.keys = ins_keys; + batch.data_sizes = ins_sizes; + batch.total_data_size =0 ;for (i =0 ; i < count; i++) {const int slot = start_slot + i;btrfs_item_key_to_cpu (src, &ins_keys[i], slot); ins_sizes[i] =btrfs_item_size_nr (src, slot); + batch.total_data_size += ins_sizes[i]; } } - ret =btrfs_insert_empty_items (trans, log, dst_path, ins_keys, ins_sizes, - count); + ret =btrfs_insert_empty_items (trans, log, dst_path, &batch);if (ret) goto out;
@@ -3695,7 +3702,8 @@ static int flush_dir_items_batch(struct btrfs_trans_handle *trans, dst_offset =btrfs_item_ptr_offset (dst, dst_path->slots[0 ]); src_offset =btrfs_item_ptr_offset (src, start_slot + i); -copy_extent_buffer (dst, src, dst_offset, src_offset, ins_sizes[i]); +copy_extent_buffer (dst, src, dst_offset, src_offset, + batch.data_sizes[i]); dst_path->slots[0 ]++; }btrfs_release_path (dst_path);
@@ -4305,6 +4313,7 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,int ret;struct btrfs_key *ins_keys;u32 *ins_sizes; +struct btrfs_item_batch batch;char *ins_data;int i;struct list_head ordered_sums;
@@ -4319,13 +4328,17 @@ static noinline int copy_items(struct btrfs_trans_handle *trans, ins_sizes = (u32 *)ins_data; ins_keys = (struct btrfs_key *)(ins_data + nr *sizeof (u32)); + batch.keys = ins_keys; + batch.data_sizes = ins_sizes; + batch.total_data_size =0 ; + batch.nr = nr;for (i =0 ; i < nr; i++) { ins_sizes[i] =btrfs_item_size_nr (src, i + start_slot); + batch.total_data_size += ins_sizes[i];btrfs_item_key_to_cpu (src, ins_keys + i, i + start_slot); } - ret =btrfs_insert_empty_items (trans, log, dst_path, - ins_keys, ins_sizes, nr); + ret =btrfs_insert_empty_items (trans, log, dst_path, &batch);if (ret) {kfree (ins_data);return ret;
Signature · 1 line
2.33.0
PATCH 2/3 btrfs: unexport setup_items_for_insert()
In reply to [email protected]From: Filipe Manana <[email protected]>
Since setup_items_for_insert() is not used anymore outside of ctree.c, make it static and remove its prototype from ctree.h. This also requires to move the definition of setup_item_for_insert() from ctree.h to ctree.c and move down btrfs_duplicate_item() so that it's defined after setup_items_for_insert().
Further, since setup_item_for_insert() is used outside ctree.c, rename it to btrfs_setup_item_for_insert().
This patch is part of a small patchset that is comprised of the following patches:
btrfs: loop only once over data sizes array when inserting an item batch
btrfs: unexport setup_items_for_insert()
btrfs: use single bulk copy operations when logging directories
This is patch 2/3 and performance results, and the specific tests, are included in the changelog of patch 3/3.
--- fs/btrfs/ctree.c | 95 +++++++++++++++++----------- fs/btrfs/ctree.h | 24 ++----- fs/btrfs/file.c | 2 +- fs/btrfs/tests/extent-buffer-tests.c | 2 +- fs/btrfs/tests/inode-tests.c | 4 +- 5 files changed, 68 insertions(+), 59 deletions(-)
fs/btrfs/ctree.c +59 -36
@@ -3580,40 +3580,6 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,return ret; } -/* -* This function duplicate a item, giving 'new_key' to the new item. -* It guarantees both items live in the same tree leaf and the new item -* is contiguous with the original item. -* -* This allows us to split file extent in place, keeping a lock on the -* leaf the entire time. -*/ -int btrfs_duplicate_item (struct btrfs_trans_handle *trans, -struct btrfs_root *root, -struct btrfs_path *path, -const struct btrfs_key *new_key) -{ -struct extent_buffer *leaf; -int ret; -u32 item_size; - - leaf = path->nodes[0 ]; - item_size =btrfs_item_size_nr (leaf, path->slots[0 ]); - ret =setup_leaf_for_split (trans, root, path, - item_size +sizeof (struct btrfs_item )); -if (ret) -return ret; - - path->slots[0 ]++; -setup_item_for_insert (root, path, new_key, item_size); - leaf = path->nodes[0 ]; -memcpy_extent_buffer (leaf, -btrfs_item_ptr_offset (leaf, path->slots[0 ]), -btrfs_item_ptr_offset (leaf, path->slots[0 ] -1 ), - item_size); -return 0 ; -} -/* * make the item pointed to by the path smaller. new_size indicates * how small to make it, and from_end tells us if we just chop bytes
@@ -3787,8 +3753,8 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size)* @path: points to the leaf/slot where we are going to insert new items * @batch: information about the batch of items to insert */ -void setup_items_for_insert (struct btrfs_root *root,struct btrfs_path *path, -const struct btrfs_item_batch *batch) +static void setup_items_for_insert (struct btrfs_root *root,struct btrfs_path *path, +const struct btrfs_item_batch *batch) {struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_item *item;
@@ -3881,6 +3847,29 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, } } +/* +* Insert a new item into a leaf. +* +* @root: The root of the btree. +* @path: A path pointing to the target leaf and slot. +* @key: The key of the new item. +* @data_size: The size of the data associated with the new key. +*/ +void btrfs_setup_item_for_insert (struct btrfs_root *root, +struct btrfs_path *path, +const struct btrfs_key *key, +u32 data_size) +{ +struct btrfs_item_batch batch; + + batch.keys = key; + batch.data_sizes = &data_size; + batch.total_data_size = data_size; + batch.nr =1 ; + +setup_items_for_insert (root, path, &batch); +} +/* * Given a key and some data, insert items into the tree. * This does all the path init required, making room in the tree if needed.
@@ -3935,6 +3924,40 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,return ret; } +/* +* This function duplicates an item, giving 'new_key' to the new item. +* It guarantees both items live in the same tree leaf and the new item is +* contiguous with the original item. +* +* This allows us to split a file extent in place, keeping a lock on the leaf +* the entire time. +*/ +int btrfs_duplicate_item (struct btrfs_trans_handle *trans, +struct btrfs_root *root, +struct btrfs_path *path, +const struct btrfs_key *new_key) +{ +struct extent_buffer *leaf; +int ret; +u32 item_size; + + leaf = path->nodes[0 ]; + item_size =btrfs_item_size_nr (leaf, path->slots[0 ]); + ret =setup_leaf_for_split (trans, root, path, + item_size +sizeof (struct btrfs_item )); +if (ret) +return ret; + + path->slots[0 ]++; +btrfs_setup_item_for_insert (root, path, new_key, item_size); + leaf = path->nodes[0 ]; +memcpy_extent_buffer (leaf, +btrfs_item_ptr_offset (leaf, path->slots[0 ]), +btrfs_item_ptr_offset (leaf, path->slots[0 ] -1 ), + item_size); +return 0 ; +} +/* * delete the pointer from a given node. *
fs/btrfs/ctree.h +5 -19
@@ -2899,7 +2899,7 @@ static inline int btrfs_del_item(struct btrfs_trans_handle *trans,/* * Describes a batch of items to insert in a btree. This is used by -* btrfs_insert_empty_items() and setup_items_for_insert(). +* btrfs_insert_empty_items(). */ struct btrfs_item_batch {/*
@@ -2923,24 +2923,10 @@ struct btrfs_item_batch {int nr; }; -void setup_items_for_insert (struct btrfs_root *root,struct btrfs_path *path, -const struct btrfs_item_batch *batch); - -static inline void setup_item_for_insert (struct btrfs_root *root, -struct btrfs_path *path, -const struct btrfs_key *key, -u32 data_size) -{ -struct btrfs_item_batch batch; - - batch.keys = key; - batch.data_sizes = &data_size; - batch.total_data_size = data_size; - batch.nr =1 ; - -setup_items_for_insert (root, path, &batch); -} - +void btrfs_setup_item_for_insert (struct btrfs_root *root, +struct btrfs_path *path, +const struct btrfs_key *key, +u32 data_size);int btrfs_insert_item (struct btrfs_trans_handle *trans,struct btrfs_root *root,const struct btrfs_key *key,void *data,u32 data_size); int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
fs/btrfs/file.c +1 -1
@@ -1021,7 +1021,7 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,if (btrfs_comp_cpu_keys (&key, &slot_key) >0 ) path->slots[0 ]++; } -setup_item_for_insert (root, path, &key, args->extent_item_size); +btrfs_setup_item_for_insert (root, path, &key, args->extent_item_size); args->extent_inserted = true; }
fs/btrfs/tests/extent-buffer-tests.c +1 -1
@@ -60,7 +60,7 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) key.type =BTRFS_EXTENT_CSUM_KEY ; key.offset =0 ; -setup_item_for_insert (root, path, &key, value_len); +btrfs_setup_item_for_insert (root, path, &key, value_len); item =btrfs_item_nr (0 );write_extent_buffer (eb, value,btrfs_item_ptr_offset (eb,0 ), value_len);
fs/btrfs/tests/inode-tests.c +2 -2
@@ -33,7 +33,7 @@ static void insert_extent(struct btrfs_root *root, u64 start, u64 len, key.type =BTRFS_EXTENT_DATA_KEY ; key.offset = start; -setup_item_for_insert (root, &path, &key, value_len); +btrfs_setup_item_for_insert (root, &path, &key, value_len); fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);btrfs_set_file_extent_generation (leaf, fi,1 );btrfs_set_file_extent_type (leaf, fi, type);
@@ -63,7 +63,7 @@ static void insert_inode_item_key(struct btrfs_root *root) key.type =BTRFS_INODE_ITEM_KEY ; key.offset =0 ; -setup_item_for_insert (root, &path, &key, value_len); +btrfs_setup_item_for_insert (root, &path, &key, value_len); }/*
Signature · 1 line
2.33.0
PATCH 3/3 btrfs: use single bulk copy operations when logging directories
In reply to [email protected]From: Filipe Manana <[email protected]>
When logging a directory and inserting a batch of directory items, we are copying the data of each item from a leaf in the fs/subvolume tree to a leaf in a log tree, separately. This is not really needed, since we are copying from a contiguous memory area into another one, so we can use a single copy operation to copy all items at once.
This patch is part of a small patchset that is comprised of the following patches:
btrfs: loop only once over data sizes array when inserting an item batch
btrfs: unexport setup_items_for_insert()
btrfs: use single bulk copy operations when logging directories
This is patch 3/3.
The following test was used to compare performance of a branch without the patchset versus one branch that has the whole patchset applied:
$ cat dir-fsync-test.sh #!/bin/bash
DEV=/dev/nvme0n1
MNT=/mnt/nvme0n1
NUM_NEW_FILES=1000000
NUM_FILE_DELETES=1000
LEAF_SIZE=16K
mkfs.btrfs -f -n $LEAF_SIZE $DEV
mount -o ssd $DEV $MNT
mkdir $MNT/testdir
for ((i = 1; i <= $NUM_NEW_FILES; i++)); do
echo -n > $MNT/testdir/file_$i
done
# Fsync the directory, this will log the new dir items and the inodes # they point to, because these are new inodes. start=$(date +%s%N) xfs_io -c "fsync" $MNT/testdir end=$(date +%s%N)
dur=$(( (end - start) / 1000000 ))
echo "dir fsync took $dur ms after adding $NUM_NEW_FILES files"
# sync to force transaction commit and wipeout the log. sync
del_inc=$(( $NUM_NEW_FILES / $NUM_FILE_DELETES ))
for ((i = 1; i <= $NUM_NEW_FILES; i += $del_inc)); do
rm -f $MNT/testdir/file_$i
done
# Fsync the directory, this will only log dir items, there are no
# dentries pointing to new inodes.
start=$(date +%s%N)
xfs_io -c "fsync" $MNT/testdir
end=$(date +%s%N) dur=$(( (end - start) / 1000000 ))
echo "dir fsync took $dur ms after deleting $NUM_FILE_DELETES files"
umount $MNT
The tests were run on a non-debug kernel (Debian's default kernel config) and were the following:
*** with a leaf size of 16K, before patchset ***
dir fsync took 8482 ms after adding 1000000 files
dir fsync took 166 ms after deleting 1000 files
*** with a leaf size of 16K, after patchset ***
dir fsync took 8196 ms after adding 1000000 files (-3.4%)
dir fsync took 143 ms after deleting 1000 files (-14.9%)
*** with a leaf size of 64K, before patchset ***
dir fsync took 12851 ms after adding 1000000 files dir fsync took 466 ms after deleting 1000 files
*** with a leaf size of 64K, after patchset ***
dir fsync took 12287 ms after adding 1000000 files (-4.5%)
dir fsync took 414 ms after deleting 1000 files (-11.8%)
--- fs/btrfs/tree-log.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
fs/btrfs/tree-log.c +15 -10
@@ -3653,6 +3653,8 @@ static int flush_dir_items_batch(struct btrfs_trans_handle *trans,char *ins_data =NULL ;struct btrfs_item_batch batch;struct extent_buffer *dst; +unsigned long src_offset; +unsigned long dst_offset;struct btrfs_key key;u32 item_size;int ret;
@@ -3696,16 +3698,19 @@ static int flush_dir_items_batch(struct btrfs_trans_handle *trans, goto out; dst = dst_path->nodes[0 ]; -for (i =0 ; i < count; i++) { -unsigned long src_offset; -unsigned long dst_offset; - - dst_offset =btrfs_item_ptr_offset (dst, dst_path->slots[0 ]); - src_offset =btrfs_item_ptr_offset (src, start_slot + i); -copy_extent_buffer (dst, src, dst_offset, src_offset, - batch.data_sizes[i]); - dst_path->slots[0 ]++; - } +/* +* Copy all the items in bulk, in a single copy operation. Item data is +* organized such that it's placed at the end of a leaf and from right +* to left. For example, the data for the second item ends at an offset +* that matches the offset where the data for the first item starts, the +* data for the third item ends at an offset that matches the offset +* where the data of the second items starts, and so on. +* Therefore our source and destination start offsets for copy match the +* offsets of the last items (highest slots). +*/ + dst_offset =btrfs_item_ptr_offset (dst, dst_path->slots[0 ] + count -1 ); + src_offset =btrfs_item_ptr_offset (src, start_slot + count -1 ); +copy_extent_buffer (dst, src, dst_offset, src_offset, batch.total_data_size);btrfs_release_path (dst_path); out:kfree (ins_data);
Signature · 1 line
2.33.0
Quoted 8 lines · On Fri, Sep 24, 2021 at 12:28:12PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
This patchset does some minor improvements to speedup bulk insertion of items into a btree, which is used when logging directories, when running delayed items for directories (fsync and transaction commits) and when running the slow path (full sync) of an fsync. The last patch in the series contains test results in its changelog.
Quoted 4 lines
Filipe Manana (3):
btrfs: loop only once over data sizes array when inserting an item batch
btrfs: unexportsetup_items_for_insert()
btrfs: use single bulk copy operations when logging directories
Added to misc-next, thanks.