Skip to contentSkip to conversation

btrfs: round down stripe size and chunk size to pow of 2

PATCH Superseded Wang Yugui 2 participants 3 messages Aug 16, 2022 Btrfs file system

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wang Yugui

author
#0 lore raw

PATCH 1/1 btrfs: round down stripe size and chunk size to pow of 2

In decide_stripe_size_regular(), when new disk is added to RAID0/RAID10/RAID56, it is better to alloc/reuse the free space if stripe size is kept or changed to 1/2. so stripe size and chunk size of pow of 2 will be more friendly.

---
 fs/btrfs/volumes.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
fs/btrfs/volumes.c +9 -11
index 6595755..fab9765 100644
@@ -5083,9 +5083,9 @@ static void init_alloc_chunk_ctl_policy_regular(
 	if (ctl->type & BTRFS_BLOCK_GROUP_SYSTEM)
 		ctl->devs_max = min_t(int, ctl->devs_max, BTRFS_MAX_DEVS_SYS_CHUNK);
 
-	/* We don't want a chunk larger than 10% of writable space */
-	ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
-				  ctl->max_chunk_size);
+	/* We don't want a chunk larger than 1/8 of writable space */
+	ctl->max_chunk_size = min_t(u64, ctl->max_chunk_size,
+			rounddown_pow_of_two(fs_devices->total_rw_bytes >> 3));
 	ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes;
 }
 
@@ -5143,10 +5143,9 @@ static void init_alloc_chunk_ctl_policy_zoned(
 		BUG();
 	}
 
-	/* We don't want a chunk larger than 10% of writable space */
-	limit = max(round_down(div_factor(fs_devices->total_rw_bytes, 1),
-			       zone_size),
-		    min_chunk_size);
+	/* We don't want a chunk larger than 1/8 of writable space */
+	limit = max_t(u64, min_chunk_size,
+		rounddown_pow_of_two(fs_devices->total_rw_bytes >> 3));
 	ctl->max_chunk_size = min(limit, ctl->max_chunk_size);
 	ctl->dev_extent_min = zone_size * ctl->dev_stripes;
 }
@@ -5284,13 +5283,12 @@ static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl,
 	 */
 	if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
 		/*
-		 * Reduce stripe_size, round it up to a 16MB boundary again and
+		 * Reduce stripe_size, round down to pow of 2 boundary again and
 		 * then use it, unless it ends up being even bigger than the
 		 * previous value we had already.
 		 */
-		ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size,
-							data_stripes), SZ_16M),
-				       ctl->stripe_size);
+		ctl->stripe_size = min_t(u64, ctl->stripe_size,
+			rounddown_pow_of_two(div_u64(ctl->max_chunk_size, data_stripes)));
 	}
 
 	/* Align to BTRFS_STRIPE_LEN */
Signature · 1 line
2.36.2
#1 lore raw
Quoted 15 lines · Wang Yugui

In decide_stripe_size_regular(), when new disk is added to RAID0/RAID10/RAID56, it is better to alloc/reuse the free space if stripe size is kept or changed to 1/2. so stripe size and chunk size of pow of 2 will be more friendly.

---
  fs/btrfs/volumes.c | 20 +++++++++-----------
  1 file changed, 9 insertions(+), 11 deletions(-)
fs/btrfs/volumes.c +0 -0
index 6595755..fab9765 100644

fs/btrfs/volumes.c static void init_alloc_chunk_ctl_policy_regular( · line 5086

Quoted 7 lines
fs/btrfs/volumes.c +1 -3
@@ -5083,9 +5083,9 @@ static void init_alloc_chunk_ctl_policy_regular(
  	if (ctl->type & BTRFS_BLOCK_GROUP_SYSTEM)
  		ctl->devs_max = min_t(int, ctl->devs_max, BTRFS_MAX_DEVS_SYS_CHUNK);
 
-	/* We don't want a chunk larger than 10% of writable space */
-	ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
-				  ctl->max_chunk_size);
+	/* We don't want a chunk larger than 1/8 of writable space */

For the 1/8 change I'm completely fine.

fs/btrfs/volumes.c static void init_alloc_chunk_ctl_policy_regular( · line 5088

Quoted 2 lines
fs/btrfs/volumes.c +2 -0
+	ctl->max_chunk_size = min_t(u64, ctl->max_chunk_size,
+			rounddown_pow_of_two(fs_devices->total_rw_bytes >> 3));

But I'm not sure if there is any benefit for the extra dounwdown_pow_of_two().

Our chunk size doesn't really need to be power of 2 at all.

Any extra explanation on why power of 2 chunk size has any benefit?

Quoted 21 lines
fs/btrfs/volumes.c +3 -4
  	ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes;
  }
 
@@ -5143,10 +5143,9 @@ static void init_alloc_chunk_ctl_policy_zoned(
  		BUG();
  	}
 
-	/* We don't want a chunk larger than 10% of writable space */
-	limit = max(round_down(div_factor(fs_devices->total_rw_bytes, 1),
-			       zone_size),
-		    min_chunk_size);
+	/* We don't want a chunk larger than 1/8 of writable space */
+	limit = max_t(u64, min_chunk_size,
+		rounddown_pow_of_two(fs_devices->total_rw_bytes >> 3));
  	ctl->max_chunk_size = min(limit, ctl->max_chunk_size);
  	ctl->dev_extent_min = zone_size * ctl->dev_stripes;
  }
@@ -5284,13 +5283,12 @@ static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl,
  	 */
  	if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
  		/*

fs/btrfs/volumes.c static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl, · line 5291

Quoted 10 lines
fs/btrfs/volumes.c +3 -4
-		 * Reduce stripe_size, round it up to a 16MB boundary again and
+		 * Reduce stripe_size, round down to pow of 2 boundary again and
  		 * then use it, unless it ends up being even bigger than the
  		 * previous value we had already.
  		 */
-		ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size,
-							data_stripes), SZ_16M),
-				       ctl->stripe_size);
+		ctl->stripe_size = min_t(u64, ctl->stripe_size,
+			rounddown_pow_of_two(div_u64(ctl->max_chunk_size, data_stripes)));

I think this can even be problematic since now stripe_size can be much smaller than what we want.

Thanks,
Qu

Quoted 3 lines
fs/btrfs/volumes.c +0 -0
  	}
 
  	/* Align to BTRFS_STRIPE_LEN */
author
#2 lore raw

Hi,

Quoted 32 lines · Wang Yugui
Quoted 15 lines

In decide_stripe_size_regular(), when new disk is added to RAID0/RAID10/RAID56, it is better to alloc/reuse the free space if stripe size is kept or changed to 1/2. so stripe size and chunk size of pow of 2 will be more friendly.

---
  fs/btrfs/volumes.c | 20 +++++++++-----------
  1 file changed, 9 insertions(+), 11 deletions(-)
fs/btrfs/volumes.c +0 -0
index 6595755..fab9765 100644

fs/btrfs/volumes.c static void init_alloc_chunk_ctl_policy_regular( · line 5086

Quoted 7 lines
fs/btrfs/volumes.c +1 -3
@@ -5083,9 +5083,9 @@ static void init_alloc_chunk_ctl_policy_regular(
  	if (ctl->type & BTRFS_BLOCK_GROUP_SYSTEM)
  		ctl->devs_max = min_t(int, ctl->devs_max, BTRFS_MAX_DEVS_SYS_CHUNK);
 
-	/* We don't want a chunk larger than 10% of writable space */
-	ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
-				  ctl->max_chunk_size);
+	/* We don't want a chunk larger than 1/8 of writable space */

For the 1/8 change I'm completely fine.

fs/btrfs/volumes.c static void init_alloc_chunk_ctl_policy_regular( · line 5088

Quoted 2 lines
fs/btrfs/volumes.c +2 -0
+	ctl->max_chunk_size = min_t(u64, ctl->max_chunk_size,
+			rounddown_pow_of_two(fs_devices->total_rw_bytes >> 3));

But I'm not sure if there is any benefit for the extra dounwdown_pow_of_two().

Quoted 3 lines

Our chunk size doesn't really need to be power of 2 at all.

Any extra explanation on why power of 2 chunk size has any benefit?

Becasue stripe_size is roundup_pow_of_two()/rounddown_pow_of_two() later, so real chunk size is roundup_pow_of_two() * N /rounddown_pow_of_two()
* N.

Here we get max_chunk_size base on pow of 2 too, so that in single/RAID1 profile, max_chunk_size and chunk size will be same.

Quoted 35 lines
Quoted 21 lines
fs/btrfs/volumes.c +3 -4
  	ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes;
  }
 
@@ -5143,10 +5143,9 @@ static void init_alloc_chunk_ctl_policy_zoned(
  		BUG();
  	}
 
-	/* We don't want a chunk larger than 10% of writable space */
-	limit = max(round_down(div_factor(fs_devices->total_rw_bytes, 1),
-			       zone_size),
-		    min_chunk_size);
+	/* We don't want a chunk larger than 1/8 of writable space */
+	limit = max_t(u64, min_chunk_size,
+		rounddown_pow_of_two(fs_devices->total_rw_bytes >> 3));
  	ctl->max_chunk_size = min(limit, ctl->max_chunk_size);
  	ctl->dev_extent_min = zone_size * ctl->dev_stripes;
  }
@@ -5284,13 +5283,12 @@ static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl,
  	 */
  	if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
  		/*

fs/btrfs/volumes.c static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl, · line 5291

Quoted 10 lines
fs/btrfs/volumes.c +3 -4
-		 * Reduce stripe_size, round it up to a 16MB boundary again and
+		 * Reduce stripe_size, round down to pow of 2 boundary again and
  		 * then use it, unless it ends up being even bigger than the
  		 * previous value we had already.
  		 */
-		ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size,
-							data_stripes), SZ_16M),
-				       ctl->stripe_size);
+		ctl->stripe_size = min_t(u64, ctl->stripe_size,
+			rounddown_pow_of_two(div_u64(ctl->max_chunk_size, data_stripes)));

I think this can even be problematic since now stripe_size can be much smaller than what we want.

Quoted 2 lines

Thanks,
Qu

rounddown_pow_of_two() is at least > round_up(,SZ_16M) * 0.5, so it is not too much smaller.

But roundup_pow_of_two() match with orig round_up(,SZ_16M) better than rounddown_pow_of_two().

It will be changed to roundup_pow_of_two() in v2.

Best Regards
Wang Yugui ([email protected])
2022/08/16