diff mbox series

[f2fs-dev,v2,3/3] f2fs-tools: cache free segments count to improve perfmance

Message ID 188b05d1764d1ba48f9c704ff28df853f3094f67.1695890686.git.bo.wu@vivo.com (mailing list archive)
State New
Headers show
Series f2fs-tools: cache free segments count to improve perfmance | expand

Commit Message

Wu Bo Sept. 28, 2023, 9:20 a.m. UTC
'get_free_segments()' is implemented by traversing all segments to
calculate the total free segments. It cosume much time.

Every time when call 'find_next_free_block()' this calculation will
do it again. So if we cache the free segments count, it will greatly
improve performance of dfrag & resize & sload.
---
 fsck/f2fs.h    | 1 +
 fsck/mount.c   | 9 +++++++--
 fsck/segment.c | 2 ++
 3 files changed, 10 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/fsck/f2fs.h b/fsck/f2fs.h
index e65644e..a7cdc4c 100644
--- a/fsck/f2fs.h
+++ b/fsck/f2fs.h
@@ -197,6 +197,7 @@  struct f2fs_sm_info {
 	unsigned int main_segments;
 	unsigned int reserved_segments;
 	unsigned int ovp_segments;
+	unsigned int free_segments;
 };
 
 struct f2fs_dentry_ptr {
diff --git a/fsck/mount.c b/fsck/mount.c
index 098e73d..0a37bc4 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -123,7 +123,7 @@  void update_free_segments(struct f2fs_sb_info *sbi)
 	if (c.dbg_lv)
 		return;
 
-	MSG(0, "\r [ %c ] Free segments: 0x%x", progress[i % 5], get_free_segments(sbi));
+	MSG(0, "\r [ %c ] Free segments: 0x%x", progress[i % 5], SM_I(sbi)->free_segments);
 	fflush(stdout);
 	i++;
 }
@@ -2430,6 +2430,10 @@  static int build_sit_entries(struct f2fs_sb_info *sbi)
 
 			check_block_count(sbi, segno, &sit);
 			seg_info_from_raw_sit(sbi, se, &sit);
+			if (se->valid_blocks == 0x0 &&
+				is_usable_seg(sbi, segno) &&
+				!IS_CUR_SEGNO(sbi, segno))
+				SM_I(sbi)->free_segments++;
 		}
 		start_blk += readed;
 	} while (start_blk < sit_blk_cnt);
@@ -2485,6 +2489,7 @@  static int early_build_segment_manager(struct f2fs_sb_info *sbi)
 	sm_info->ovp_segments = get_cp(overprov_segment_count);
 	sm_info->main_segments = get_sb(segment_count_main);
 	sm_info->ssa_blkaddr = get_sb(ssa_blkaddr);
+	sm_info->free_segments = 0;
 
 	if (build_sit_info(sbi) || build_curseg(sbi)) {
 		free(sm_info);
@@ -2806,7 +2811,7 @@  int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left,
 
 	if (*to > 0)
 		*to -= left;
-	if (get_free_segments(sbi) <= SM_I(sbi)->reserved_segments + 1)
+	if (SM_I(sbi)->free_segments <= SM_I(sbi)->reserved_segments + 1)
 		not_enough = 1;
 
 	while (*to >= SM_I(sbi)->main_blkaddr && *to < end_blkaddr) {
diff --git a/fsck/segment.c b/fsck/segment.c
index 0307bdd..1cb7d02 100644
--- a/fsck/segment.c
+++ b/fsck/segment.c
@@ -77,6 +77,8 @@  int reserve_new_block(struct f2fs_sb_info *sbi, block_t *to,
 	se = get_seg_entry(sbi, GET_SEGNO(sbi, blkaddr));
 	offset = OFFSET_IN_SEG(sbi, blkaddr);
 	se->type = type;
+	if (se->valid_blocks == 0)
+		SM_I(sbi)->free_segments--;
 	se->valid_blocks++;
 	f2fs_set_bit(offset, (char *)se->cur_valid_map);
 	if (need_fsync_data_record(sbi)) {