Skip to contentSkip to conversation

f2fs-tools: cache free segments count to improve perfmance

PATCH Superseded Wu Bo via Linux-f2fs-devel 2 participants 6 messages Sep 15, 2023 – Sep 20, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover f2fs-tools: cache free segments count to improve perfmance

When I looking at the performance of resize via flame graph, I can see that 'find_next_free_block()->get_free_segments()' cosume most user space time.

Every calling 'get_free_segments()', it will traverses all segments to calculate the free segments count. And this path is called a lot in resize & sload & defrag.

If the free segments count is cached, these tools performance will be improved.

Wu Bo (3):
f2fs-tools: use 'IS_CUR_SEGNO()' to check if it is current segment
f2fs-tools: skip not matched segment when finding free block
f2fs-tools: cache free segments count to improve perfmance

 fsck/f2fs.h    |  1 +
 fsck/mount.c   | 28 +++++++++++++---------------
 fsck/segment.c |  2 ++
 3 files changed, 16 insertions(+), 15 deletions(-)
Signature · 8 lines
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH 1/3 f2fs-tools: use 'IS_CUR_SEGNO()' to check if it is current segment

Use IS_CUR_SEGNO() here can make code more concise and readable.

---
 fsck/mount.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
fsck/mount.c +4 -13
index df0314d..00940b8 100644
@@ -2531,20 +2531,11 @@ void build_sit_area_bitmap(struct f2fs_sb_info *sbi)
 		memcpy(ptr, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
 		ptr += SIT_VBLOCK_MAP_SIZE;
 
-		if (se->valid_blocks == 0x0 && is_usable_seg(sbi, segno)) {
-			if (le32_to_cpu(sbi->ckpt->cur_node_segno[0]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_data_segno[0]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_node_segno[1]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_data_segno[1]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_node_segno[2]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_data_segno[2]) == segno) {
-				continue;
-			} else {
-				free_segs++;
-			}
-		} else {
+		if (se->valid_blocks == 0x0 && is_usable_seg(sbi, segno) &&
+				!IS_CUR_SEGNO(sbi, segno))
+			free_segs++;
+		else
 			sum_vblocks += se->valid_blocks;
-		}
 	}
 	fsck->chk.sit_valid_blocks = sum_vblocks;
 	fsck->chk.sit_free_segs = free_segs;
Signature · 8 lines
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#5 lore raw

PATCH 3/3 f2fs-tools: cache free segments count to improve perfmance

In reply to Wu Bo via Linux-f2fs-devel

'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(-)
fsck/f2fs.h +1 -0
index e65644e..a7cdc4c 100644
@@ -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 {
fsck/mount.c +7 -2
index dccaae2..b965c5d 100644
@@ -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);
@@ -2805,7 +2810,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) {
fsck/segment.c +2 -0
index 0307bdd..1cb7d02 100644
@@ -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)) {
Signature · 8 lines
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#3 lore raw

PATCH 2/3 f2fs-tools: skip not matched segment when finding free block

In reply to Wu Bo via Linux-f2fs-devel

If the segment type is not matched, goto next segment to save time.

---
 fsck/mount.c | 2 ++
 1 file changed, 2 insertions(+)
fsck/mount.c +2 -0
index 00940b8..dccaae2 100644
@@ -2854,6 +2854,8 @@ next_segment:
 		if (type == want_type && !new_sec &&
 			!f2fs_test_bit(offset, (const char *)bitmap))
 			return 0;
+		if (type != want_type)
+			goto next_segment;
 
 		*to = left ? *to - 1: *to + 1;
 	}
Signature · 8 lines
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#2 lore raw

Re: [f2fs-dev] [PATCH 1/3] f2fs-tools: use 'IS_CUR_SEGNO()' to check if it is current segment

In reply to Wu Bo via Linux-f2fs-devel
Quoted 20 lines · Wu Bo

Use IS_CUR_SEGNO() here can make code more concise and readable.

---
  fsck/mount.c | 17 ++++-------------
  1 file changed, 4 insertions(+), 13 deletions(-)
fsck/mount.c +0 -7
index df0314d..00940b8 100644
@@ -2531,20 +2531,11 @@ void build_sit_area_bitmap(struct f2fs_sb_info *sbi)
  		memcpy(ptr, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
  		ptr += SIT_VBLOCK_MAP_SIZE;
 
-		if (se->valid_blocks == 0x0 && is_usable_seg(sbi, segno)) {
-			if (le32_to_cpu(sbi->ckpt->cur_node_segno[0]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_data_segno[0]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_node_segno[1]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_data_segno[1]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_node_segno[2]) == segno ||
-				le32_to_cpu(sbi->ckpt->cur_data_segno[2]) == segno) {

fsck/mount.c void build_sit_area_bitmap(struct f2fs_sb_info *sbi) · line 2538

Quoted 10 lines
fsck/mount.c +4 -5
-				continue;
-			} else {
-				free_segs++;
-			}
-		} else {
+		if (se->valid_blocks == 0x0 && is_usable_seg(sbi, segno) &&
+				!IS_CUR_SEGNO(sbi, segno))
+			free_segs++;
+		else
  			sum_vblocks += se->valid_blocks;

IIUC, it should be?

if (se->valid_blocks == 0x0 && is_usable_seg(sbi, segno)) {
	if (!IS_CUR_SEGNO(sbi, segno))
		free_segs++;
} else {
	sum_vblocks += se->valid_blocks;
}

Thanks,

fsck/mount.c void build_sit_area_bitmap(struct f2fs_sb_info *sbi) · line 2541

Quoted 4 lines
fsck/mount.c +0 -1
-		}
  	}
  	fsck->chk.sit_valid_blocks = sum_vblocks;
  	fsck->chk.sit_free_segs = free_segs;

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#4 lore raw

Re: [f2fs-dev] [PATCH 2/3] f2fs-tools: skip not matched segment when finding free block

In reply to Wu Bo via Linux-f2fs-devel
Quoted 10 lines · Wu Bo

If the segment type is not matched, goto next segment to save time.

---
  fsck/mount.c | 2 ++
  1 file changed, 2 insertions(+)
fsck/mount.c +0 -0
index 00940b8..dccaae2 100644

fsck/mount.c next_segment: · line 2858

Quoted 5 lines
fsck/mount.c +2 -0
@@ -2854,6 +2854,8 @@ next_segment:
  		if (type == want_type && !new_sec &&
  			!f2fs_test_bit(offset, (const char *)bitmap))
  			return 0;
+		if (type != want_type)
+			goto next_segment;

if (type != want_type)

	goto next_segment;
else if (!new_sec && !f2fs_test_bit(offset, (const char *)bitmap))
	return 0;

Thanks,

fsck/mount.c next_segment: · line 2861

Quoted 3 lines
fsck/mount.c +0 -0
  		*to = left ? *to - 1: *to + 1;
  	}

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel