Skip to contentSkip to conversation

f2fs: support background_gc=adjust mount option

PATCH Superseded Chao Yu 1 participant 1 message May 18, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH 1/1 f2fs: support background_gc=adjust mount option

As JuHyung reported in [1]:

"In most consumer-grade blackbox SSDs, device-side GCs are handled automatically for various workloads. f2fs, however, leaves that responsibility to the userspace with conservative tuning on the kernel-side by default. Android handles this by init.rc tunings and a separate code running in vold to trigger gc_urgent.

For regular Linux desktop distros, f2fs just runs on the default configuration set on the kernel and unless it’s running 24/7 with plentiful idle time, it quickly runs out of free segments and starts triggering foreground GC. This is giving people the wrong impression that f2fs slows down far drastically than other file-systems when that’s quite the contrary (i.e., less fragmentation overtime)."

This patch supports background_gc=adjust mount option.

If background_gc=adjust, gc will adjust its policy depends on conditions: speed up if there no free segments, and slow down if there is no free space.

The main logic is as below:

1. performance mode
- condition: if free_segments is less than 10 * ovp_segments and reclaimable_block is more than 20 * unused_user_block
- action: reduce sleep time of GC thread based on free user block ratio, that is to say, the more reclaimable blocks, the less time thread will sleep

2. lifetime mode:
- condition: if free space is less than 90%
- action:
a) reset min_sleep_time to default 30000 ms
b) reduce cost weight of age when cacluating cost of dirty segment, so that GC may select victim which contains less blocks

3. balance mode
- condition: it is default mode
- action: reduce min_sleep_time from 30000 ms to 10000 ms

[1] https://lore.kernel.org/linux-f2fs-devel/CAD14+f3z=kS9E+NTKH7t1J2xL1PpLOVMNx=CabD_t2K6U=T9uQ@mail.gmail.com

Original patch was developed by Weichao Guo, I refactor it a bit and rebase the code.

---
 Documentation/filesystems/f2fs.rst |  7 ++-
 fs/f2fs/f2fs.h                     |  4 ++
 fs/f2fs/gc.c                       | 92 +++++++++++++++++++++++++++++-
 fs/f2fs/gc.h                       | 23 ++++++++
 fs/f2fs/super.c                    |  4 ++
 5 files changed, 126 insertions(+), 4 deletions(-)
Documentation/filesystems/f2fs.rst +5 -2
index 9359978a5af2..764301f7391e 100644
@@ -112,8 +112,11 @@ background_gc=%s	 Turn on/off cleaning operations, namely garbage
 			 collection and if background_gc=off, garbage collection
 			 will be turned off. If background_gc=sync, it will turn
 			 on synchronous garbage collection running in background.
-			 Default value for this option is on. So garbage
-			 collection is on by default.
+			 If background_gc=adjust, gc will adjust its policy depends
+			 on conditions: speed up if there no free segments, and slow
+			 down if there is no free space.
+			 Default value for this option is on. So garbage collection
+			 is on by default.
 gc_merge		 When background_gc is on, this option can be enabled to
 			 let background GC thread to handle foreground GC requests,
 			 it can eliminate the sluggish issue caused by slow foreground
fs/f2fs/f2fs.h +4 -0
index 8d4eaf4d2246..4c2f65d3c208 100644
@@ -1333,6 +1333,10 @@ enum {
 				 * background gc is on, migrating blocks
 				 * like foreground gc
 				 */
+	BGGC_MODE_ADJUST,	/*
+				 * background gc is on, and tune its speed
+				 * dependso n conditions
+				 */
 };
 
 enum {
fs/f2fs/gc.c +90 -2

135 lines not on this page · raw message

fs/f2fs/gc.h +23 -0
index 28a00942802c..66f6a30dd494 100644
@@ -15,6 +15,14 @@
 #define DEF_GC_THREAD_MAX_SLEEP_TIME	60000
 #define DEF_GC_THREAD_NOGC_SLEEP_TIME	300000	/* wait 5 min */
 
+/* for BGGC_MODE_ADJUST */
+#define DEF_GC_PERFORMANCE_MIN_SLEEP_TIME	100	/* 100 ms */
+#define DEF_GC_BALANCE_MIN_SLEEP_TIME		10000	/* 10 sec */
+#define DEF_FEW_FREE_SPACE_RATIO		10	/* few free space ratio */
+#define DEF_FEW_FREE_SEGMENT_MULTIPLE		10	/* few free segments multiple */
+#define DEF_ENOUGH_RECLAIMABLE_BLOCK_RATIO	20	/* enough reclaimable block ratio */
+#define DEF_FEW_FREE_SEGMENT_RATIO		10	/* few free segment ratio */
+
 /* choose candidates from sections which has age of more than 7 days */
 #define DEF_GC_THREAD_AGE_THRESHOLD		(60 * 60 * 24 * 7)
 #define DEF_GC_THREAD_CANDIDATE_RATIO		20	/* select 20% oldest sections as candidates */
@@ -32,6 +40,19 @@
 
 #define NR_GC_CHECKPOINT_SECS (3)	/* data/node/dentry sections */
 
+/* GC preference */
+enum {
+	GC_PERFORMANCE_MODE,		/*
+					 * speed up background gc to recycle
+					 * slack space for better performance
+					 */
+	GC_LIFETIME_MODE,		/*
+					 * slow down background gc to avoid high
+					 * WAF if there is less free space.
+					 */
+	GC_BALANCE_MODE,		/* tradeoff in between perf and lifetime */
+};
+
 struct f2fs_gc_kthread {
 	struct task_struct *f2fs_gc_task;
 	wait_queue_head_t gc_wait_queue_head;
@@ -42,6 +63,8 @@ struct f2fs_gc_kthread {
 	unsigned int max_sleep_time;
 	unsigned int no_gc_sleep_time;
 
+	unsigned char gc_preference;	/* gc perference */
+
 	/* for changing gc mode */
 	bool gc_wake;
 
fs/f2fs/super.c +4 -0
index f19217219c3b..806c8119f021 100644
@@ -693,6 +693,8 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
 				F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_OFF;
 			} else if (!strcmp(name, "sync")) {
 				F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
+			} else if (!strcmp(name, "adjust")) {
+				F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ADJUST;
 			} else {
 				kfree(name);
 				return -EINVAL;
@@ -1927,6 +1929,8 @@ static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
 		seq_printf(seq, ",background_gc=%s", "on");
 	else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
 		seq_printf(seq, ",background_gc=%s", "off");
+	else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ADJUST)
+		seq_printf(seq, ",background_gc=%s", "adjust");
 
 	if (test_opt(sbi, GC_MERGE))
 		seq_puts(seq, ",gc_merge");
Signature · 8 lines
2.40.1



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