improve write IO performance when fragmentation is high
Heming Zhao
reviewercover improve write IO performance when fragmentation is high
In v4 coding, I found new issues and split 3 patch files for easy reviewing:
- (1/3, existing) improve write IO performance when fragmentation is high
- (2/3, new) adjust enabling place for la-window
- (3/3, new) speed up chain-list searching
Heming Zhao (3):
ocfs2: improve write IO performance when fragmentation is high
ocfs2: adjust enabling place for la window
ocfs2: speed up chain-list searching
fs/ocfs2/localalloc.c | 17 ++++--- fs/ocfs2/move_extents.c | 2 +- fs/ocfs2/ocfs2_fs.h | 3 +- fs/ocfs2/resize.c | 8 +++ fs/ocfs2/suballoc.c | 110 ++++++++++++++++++++++++++++++++++------ fs/ocfs2/suballoc.h | 6 ++- 6 files changed, 119 insertions(+), 27 deletions(-)
Signature · 1 line
2.35.3
Heming Zhao
reviewerPATCH 1/3 ocfs2: improve write IO performance when fragmentation is high
The group_search function ocfs2_cluster_group_search() should bypass groups with insufficient space to avoid unnecessary searches.
This patch is particularly useful when ocfs2 is handling huge number small files, and volume fragmentation is very high. In this case, ocfs2 is busy with looking up available la window from //global_bitmap.
This patch introduces a new member in the Group Description (gd) struct called 'bg_contig_free_bits', representing the max contigous free bits in this gd. When ocfs2 allocates a new la window from //global_bitmap, 'bg_contig_free_bits' helps expedite the search process.
Let's image below path.
1. la state (->local_alloc_state) is set THROTTLED or DISABLED.
2. when user delete a large file and trigger ocfs2_local_alloc_seen_free_bits set osb->local_alloc_state unconditionally.
3. a write IOs thread run and trigger the worst performance path
```
ocfs2_reserve_clusters_with_limit
ocfs2_reserve_local_alloc_bits
ocfs2_local_alloc_slide_window //[1]
+ ocfs2_local_alloc_reserve_for_window //[2]
+ ocfs2_local_alloc_new_window //[3]
ocfs2_recalc_la_window
```[1]:
will be called when la window bits used up.
[2]:
under la state is ENABLED, and this func only check global_bitmap free bits, it will succeed in general.
[3]:
will use the default la window size to search clusters then fail. ocfs2_recalc_la_window attempts other la window sizes. the timing complexity is O(n^4), resulting in a significant time cost for scanning global bitmap. This leads to a dramatic slowdown in write I/Os (e.g., user space 'dd').
i.e.
an ocfs2 partition size: 1.45TB, cluster size: 4KB,
la window default size: 106MB.
The partition is fragmentation by creating & deleting huge mount of small files.
before this patch, the timing of [3] should be
(the number got from real world):
- la window size change order (size: MB):
106, 53, 26.5, 13, 6.5, 3.25, 1.6, 0.8
only 0.8MB succeed, 0.8MB also triggers la window to disable. ocfs2_local_alloc_new_window retries 8 times, first 7 times totally runs in worst case.
- group chain number: 242
ocfs2_claim_suballoc_bits calls for-loop 242 times
- each chain has 49 block group
ocfs2_search_chain calls while-loop 49 times
- each bg has 32256 blocks ocfs2_block_group_find_clear_bits calls while-loop for 32256 bits. for ocfs2_find_next_zero_bit uses ffz() to find zero bit, let's use (32256/64) (this is not worst value) for timing calucation.
the loop times: 7*242*49*(32256/64) = 41835024 (~42 million times)
In the worst case, user space writes 1MB data will trigger 42M scanning times.
under this patch, the timing is '7*242*49 = 83006', reduced by three orders of magnitude.
---
v4:
fix sparse warning:
- in ocfs2_update_last_group_and_inode(), change 'old_bg_contig_free_bits' type from 'u16' to '__le16'.
- in _ocfs2_free_suballoc_bits(), do le16_to_cpu convert for assigning 'old_bg_contig_free_bits'.
v3:
1. Fix wrong var length for 'struct ocfs2_group_desc' .bg_contig_free_bits, change from '__le32' to '__le16'.
2. change all related code to use '__le16' instead of '__le32'. Please note: change ocfs2_find_max_contig_free_bits() input parameters and return type to 'u16'.
v2:
1. fix wrong length converting from cpu_to_le16() to cpu_to_le32() for setting bg->bg_contig_free_bits.
2. change ocfs2_find_max_contig_free_bits return type from 'void' to 'unsigned int'.
3. restore ocfs2_block_group_set_bits() input parameters style, change parameter 'failure_path' to 'fastpath'.
4. after <3>, add new parameter 'unsigned int max_contig_bits'.
5. after <3>, restore define 'struct ocfs2_suballoc_result' from 'suballoc.h' to 'suballoc.c'.
6. modify some code indent error.
--- fs/ocfs2/move_extents.c | 2 +- fs/ocfs2/ocfs2_fs.h | 3 +- fs/ocfs2/resize.c | 8 ++++ fs/ocfs2/suballoc.c | 99 +++++++++++++++++++++++++++++++++++++---- fs/ocfs2/suballoc.h | 6 ++- 5 files changed, 107 insertions(+), 11 deletions(-)
fs/ocfs2/move_extents.c +1 -1
@@ -685,7 +685,7 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, } ret =ocfs2_block_group_set_bits (handle, gb_inode, gd, gd_bh, - goal_bit, len); + goal_bit, len,0 ,0 );if (ret) {ocfs2_rollback_alloc_dinode_counts (gb_inode, gb_bh, len,le16_to_cpu (gd->bg_chain));
fs/ocfs2/ocfs2_fs.h +2 -1
@@ -883,7 +883,8 @@ struct ocfs2_group_desc__le16 bg_free_bits_count;/* Free bits count */ __le16 bg_chain;/* What chain I am in. */ /*10*/ __le32 bg_generation; -__le32 bg_reserved1; +__le16 bg_contig_free_bits;/* max contig free bits length */ +__le16 bg_reserved1;__le64 bg_next_group;/* Next group in my list, in blocks */ /*20*/ __le64 bg_parent_dinode;/* dinode which owns me, in
fs/ocfs2/resize.c +8 -0
@@ -91,6 +91,8 @@ static int ocfs2_update_last_group_and_inode(handle_t *handle,u16 cl_bpc =le16_to_cpu (cl->cl_bpc);u16 cl_cpg =le16_to_cpu (cl->cl_cpg);u16 old_bg_clusters; +u16 contig_bits; +__le16 old_bg_contig_free_bits;trace_ocfs2_update_last_group_and_inode (new_clusters, first_new_cluster);
@@ -122,6 +124,11 @@ static int ocfs2_update_last_group_and_inode(handle_t *handle,le16_add_cpu (&group->bg_free_bits_count,-1 * backups); } + contig_bits =ocfs2_find_max_contig_free_bits (group->bg_bitmap, +le16_to_cpu (group->bg_bits),0 ); + old_bg_contig_free_bits = group->bg_contig_free_bits; + group->bg_contig_free_bits =cpu_to_le16 (contig_bits); +ocfs2_journal_dirty (handle, group_bh);/* update the inode accordingly. */
@@ -160,6 +167,7 @@ static int ocfs2_update_last_group_and_inode(handle_t *handle,le16_add_cpu (&group->bg_free_bits_count, backups);le16_add_cpu (&group->bg_bits,-1 * num_bits);le16_add_cpu (&group->bg_free_bits_count,-1 * num_bits); + group->bg_contig_free_bits = old_bg_contig_free_bits; } out:if (ret)
fs/ocfs2/suballoc.c +91 -8
212 lines not on this page · raw message
fs/ocfs2/suballoc.h +5 -1
@@ -79,12 +79,16 @@ void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,struct buffer_head *di_bh, u32 num_bits, u16 chain); +u16 ocfs2_find_max_contig_free_bits (void *bitmap, +u16 total_bits,u16 start);int ocfs2_block_group_set_bits (handle_t *handle,struct inode *alloc_inode,struct ocfs2_group_desc *bg,struct buffer_head *group_bh,unsigned int bit_off, -unsigned int num_bits); +unsigned int num_bits, +unsigned int max_contig_bits, +int fastpath); int ocfs2_claim_metadata(handle_t *handle, struct ocfs2_alloc_context *ac,
Signature · 1 line
2.35.3
Heming Zhao
reviewerPATCH 2/3 ocfs2: adjust enabling place for la window
In reply to Heming ZhaoAfter introducing gd->bg_contig_free_bits, the code path 'ocfs2_cluster_group_search() => ocfs2_local_alloc_seen_free_bits()' becomes death when all the gd->bg_contig_free_bits are set to the correct value. This patch relocates ocfs2_local_alloc_seen_free_bits() to a more appropriate location. (The new place being ocfs2_block_group_set_bits().)
In ocfs2_local_alloc_seen_free_bits(), the scope of the spin-lock has been adjusted to reduce meaningless lock races. e.g: when userspace creates & deletes 1 cluster_size files in parallel, acquiring the spin-lock in ocfs2_local_alloc_seen_free_bits() is totally pointless and impedes IO performance.
--- fs/ocfs2/localalloc.c | 15 ++++++++------- fs/ocfs2/suballoc.c | 9 ++------- 2 files changed, 10 insertions(+), 14 deletions(-)
fs/ocfs2/localalloc.c +8 -7
@@ -212,14 +212,15 @@ static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)void ocfs2_local_alloc_seen_free_bits (struct ocfs2_super *osb,unsigned int num_clusters) { -spin_lock (&osb->osb_lock); -if (osb->local_alloc_state ==OCFS2_LA_DISABLED || - osb->local_alloc_state ==OCFS2_LA_THROTTLED ) -if (num_clusters >= osb->local_alloc_default_bits) { +if (num_clusters >= osb->local_alloc_default_bits) { +spin_lock (&osb->osb_lock); +if (osb->local_alloc_state ==OCFS2_LA_DISABLED || + osb->local_alloc_state ==OCFS2_LA_THROTTLED )cancel_delayed_work (&osb->la_enable_wq); - osb->local_alloc_state =OCFS2_LA_ENABLED ; - } -spin_unlock (&osb->osb_lock); + + osb->local_alloc_state =OCFS2_LA_ENABLED ; +spin_unlock (&osb->osb_lock); + } }void ocfs2_la_enable_worker (struct work_struct *work)
fs/ocfs2/suballoc.c +2 -7
@@ -1374,6 +1374,7 @@ int ocfs2_block_group_set_bits(handle_t *handle,int journal_type =OCFS2_JOURNAL_ACCESS_WRITE ;unsigned int start = bit_off + num_bits;u16 contig_bits; +struct ocfs2_super *osb =OCFS2_SB (alloc_inode->i_sb);/* All callers get the descriptor via * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
@@ -1423,6 +1424,7 @@ int ocfs2_block_group_set_bits(handle_t *handle,if (contig_bits > max_contig_bits) max_contig_bits = contig_bits; bg->bg_contig_free_bits =cpu_to_le16 (max_contig_bits); +ocfs2_local_alloc_seen_free_bits (osb, max_contig_bits); }else { bg->bg_contig_free_bits =0 ; }
@@ -1589,13 +1591,6 @@ static int ocfs2_cluster_group_search(struct inode *inode,* of bits. */ if (min_bits <= res->sr_bits) search =0 ;/* success */ -else if (res->sr_bits) { -/* -* Don't show bits which we'll be returning -* for allocation to the local alloc bitmap. -*/ -ocfs2_local_alloc_seen_free_bits (osb, res->sr_bits); - } }return search;
Signature · 1 line
2.35.3
Heming Zhao
reviewerPATCH 3/3 ocfs2: speed up chain-list searching
In reply to Heming Zhaoocfs2_claim_suballoc_bits():
- Add short-circuit code to speed up searching
ocfs2_local_alloc_new_window():
- remove 1 sparse warning
``` fs/ocfs2/localalloc.c:1224:41: warning: incorrect type in argument 1 (different base types) fs/ocfs2/localalloc.c:1224:41: expected unsigned long long val1 fs/ocfs2/localalloc.c:1224:41: got restricted __le32 [usertype] la_bm_off ```
--- fs/ocfs2/localalloc.c | 2 +- fs/ocfs2/suballoc.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/ocfs2/localalloc.c +1 -1
@@ -1221,7 +1221,7 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb, OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);trace_ocfs2_local_alloc_new_window_result ( -OCFS2_LOCAL_ALLOC (alloc)->la_bm_off, +le32_to_cpu (OCFS2_LOCAL_ALLOC (alloc)->la_bm_off),le32_to_cpu (alloc->id1.bitmap1.i_total)); bail:
fs/ocfs2/suballoc.c +1 -1
@@ -2008,7 +2008,7 @@ static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,for (i =0 ; i <le16_to_cpu (cl->cl_next_free_rec); i ++) {if (i == victim)continue ; -if (!cl->cl_recs[i].c_free) +if (le32_to_cpu (cl->cl_recs[i].c_free) >= bits_wanted)continue ; ac->ac_chain = i;
Signature · 1 line
2.35.3
Heming Zhao
reviewerSorry, I forgot add v4 tag and there is a mistake in patch. I will re-send the v4 patch.
-Heming
Quoted 18 lines · Heming Zhao
In v4 coding, I found new issues and split 3 patch files for easy reviewing:
- (1/3, existing) improve write IO performance when fragmentation is high
- (2/3, new) adjust enabling place for la-window
- (3/3, new) speed up chain-list searchingHeming Zhao (3):
ocfs2: improve write IO performance when fragmentation is high
ocfs2: adjust enabling place for la window
ocfs2: speed up chain-list searchingfs/ocfs2/localalloc.c | 17 ++++--- fs/ocfs2/move_extents.c | 2 +- fs/ocfs2/ocfs2_fs.h | 3 +- fs/ocfs2/resize.c | 8 +++ fs/ocfs2/suballoc.c | 110 ++++++++++++++++++++++++++++++++++------ fs/ocfs2/suballoc.h | 6 ++- 6 files changed, 119 insertions(+), 27 deletions(-)