btrfs: re-define btrfs_raid_types
v2 cover btrfs: re-define btrfs_raid_types
By the nature of BTRFS_BLOCK_GROUP_* profiles, converting the flag into an index should only need one bits AND, one if () check for SINGLE profile, one right shift to align the values, one ilog2() call which is normally converted into ffs() assembly code.
But we're using a lot of if () branches to do the convert.
This patch will re-define btrfs_raid_types by:
- Move it to volumes.h
btrfs_raid_types are only used internally, no need to be exposed through UAPI.
- Re-order btrfs_raid_types
To make them match their value order
- Use ilog2() to convert them into index
- Inline btrfs_bg_flags_to_raid_index()
It's just 5 assembly commands now.
Changelog:
v2:
- Fix the start value of BTRFS_RAID_RAID0
Qu Wenruo (2):
btrfs: move definition of btrfs_raid_types to volumes.h
btrfs: use ilog2() to replace if () branches for
btrfs_bg_flags_to_raid_index()
fs/btrfs/space-info.h | 2 ++ fs/btrfs/volumes.c | 26 ----------------------- fs/btrfs/volumes.h | 37 ++++++++++++++++++++++++++++++++- include/uapi/linux/btrfs_tree.h | 13 ------------ 4 files changed, 38 insertions(+), 40 deletions(-)
Signature · 1 line
2.33.1
PATCH v2 1/2 btrfs: move definition of btrfs_raid_types to volumes.h
It's only internally used as another way to represent btrfs profiles, it's not exposed through any on-disk format.
Furthermore, since it's internal structure, its definition can change in the future.
--- fs/btrfs/volumes.h | 13 +++++++++++++ include/uapi/linux/btrfs_tree.h | 13 ------------- 2 files changed, 13 insertions(+), 13 deletions(-)
fs/btrfs/volumes.h +13 -0
@@ -17,6 +17,19 @@ extern struct mutex uuid_mutex;#define BTRFS_STRIPE_LEN SZ_64K +enum btrfs_raid_types { +BTRFS_RAID_RAID10 , +BTRFS_RAID_RAID1 , +BTRFS_RAID_DUP , +BTRFS_RAID_RAID0 , +BTRFS_RAID_SINGLE , +BTRFS_RAID_RAID5 , +BTRFS_RAID_RAID6 , +BTRFS_RAID_RAID1C3 , +BTRFS_RAID_RAID1C4 , +BTRFS_NR_RAID_TYPES +}; +struct btrfs_io_geometry {/* remaining bytes before crossing a stripe */ u64 len;
include/uapi/linux/btrfs_tree.h +0 -13
@@ -875,19 +875,6 @@ struct btrfs_dev_replace_item {#define BTRFS_BLOCK_GROUP_RESERVED (BTRFS_AVAIL_ALLOC_BIT_SINGLE | \ BTRFS_SPACE_INFO_GLOBAL_RSV) -enum btrfs_raid_types { -BTRFS_RAID_RAID10 , -BTRFS_RAID_RAID1 , -BTRFS_RAID_DUP , -BTRFS_RAID_RAID0 , -BTRFS_RAID_SINGLE , -BTRFS_RAID_RAID5 , -BTRFS_RAID_RAID6 , -BTRFS_RAID_RAID1C3 , -BTRFS_RAID_RAID1C4 , -BTRFS_NR_RAID_TYPES -}; -#define BTRFS_BLOCK_GROUP_TYPE_MASK (BTRFS_BLOCK_GROUP_DATA | \ BTRFS_BLOCK_GROUP_SYSTEM | \ BTRFS_BLOCK_GROUP_METADATA)
Signature · 1 line
2.33.1
PATCH v2 2/2 btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
In reply to Qu WenruoIn function btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.
But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set inside BTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily use ilog2() to calculate their values.
Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).
Even with that calculation involved (one if(), one ilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.
--- fs/btrfs/space-info.h | 2 ++ fs/btrfs/volumes.c | 26 -------------------------- fs/btrfs/volumes.h | 42 ++++++++++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 36 deletions(-)
fs/btrfs/space-info.h +2 -0
@@ -3,6 +3,8 @@ #ifndef BTRFS_SPACE_INFO_H #define BTRFS_SPACE_INFO_H +#include "volumes.h" + struct btrfs_space_info { spinlock_t lock;
fs/btrfs/volumes.c +0 -26
@@ -154,32 +154,6 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { }, }; -/* -* Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which -* can be used as index to access btrfs_raid_array[]. -*/ -enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags) -{ -if (flags &BTRFS_BLOCK_GROUP_RAID10 ) -return BTRFS_RAID_RAID10 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID1 ) -return BTRFS_RAID_RAID1 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID1C3 ) -return BTRFS_RAID_RAID1C3 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID1C4 ) -return BTRFS_RAID_RAID1C4 ; -else if (flags &BTRFS_BLOCK_GROUP_DUP ) -return BTRFS_RAID_DUP ; -else if (flags &BTRFS_BLOCK_GROUP_RAID0 ) -return BTRFS_RAID_RAID0 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID5 ) -return BTRFS_RAID_RAID5 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID6 ) -return BTRFS_RAID_RAID6 ; - -return BTRFS_RAID_SINGLE ;/* BTRFS_BLOCK_GROUP_SINGLE */ -} -const char *btrfs_bg_type_to_raid_name (u64 flags) {const int index =btrfs_bg_flags_to_raid_index (flags);
fs/btrfs/volumes.h +32 -10
58 lines not on this page · raw message
Signature · 1 line
2.33.1
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 9 lines · Qu Wenruo
In function
btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.
Quoted 8 lines
Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).
Even with that calculation involved (one
if(), oneilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.
Is this used in a performance critical path, are there any numbers which prove that it's indeed faster?
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 16 lines · Nikolay Borisov
Quoted 13 lines · Qu Wenruo
In function
btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).
Quoted 6 lines
Quoted 4 lines
Even with that calculation involved (one
if(), oneilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.Is this used in a performance critical path,
Not really in a hot path.
Most of them are called in a per block group/chunk base.
The only hotter path is in __btrfs_map_block() where if we need full stripe, we will call btrfs_chunk_max_errors() which in turn call the function.
That's the hottest path I can find, and even for that case it's just per-bio base.
Quoted 1 line
are there any numbers which prove that it's indeed faster?
No real world bench for it.
But from x86_75 asm code, it's definitely smaller, with only one branch.
New:btrfs_bg_flags_to_raid_index:
xorl %eax, %eax
andl $2040, %edi
je .L2499
shrq $2, %rdi
movl $-1, %eax bsrq %rdi,%rax
.L2499:
ret
Old:btrfs_bg_flags_to_raid_index:
xorl %eax, %eax
testb $64, %dil
jne .L429
movl $1, %eax
testb $16, %dil
jne .L429
movl $7, %eax
testl $512, %edi
jne .L429
movl $8, %eax
testl $1024, %edi
jne .L429
movl $2, %eax
testb $32, %dil
jne .L429
movl $3, %eax
testb $8, %dil
jne .L429
movl $5, %eax
testb $-128, %dil
jne .L429
andl $256, %edi
cmpq $1, %rdi
sbbl %eax, %eax
andl $-2, %eax
addl $6, %eax.L429:
ret
Which I don't really believe the older code can be any faster, considering so many branches, and pure lines of asm.
Thanks,
Qu
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
In reply to Qu WenruoQuoted 9 lines · Qu Wenruo
In function
btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.
Quoted 8 lines
Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).
Even with that calculation involved (one
if(), oneilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.
Why not just use reverse static index similar to
const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { <snip>
}
Thanks, Anand
Quoted 124 lines
Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/space-info.h | 2 ++ fs/btrfs/volumes.c | 26 -------------------------- fs/btrfs/volumes.h | 42 ++++++++++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 36 deletions(-)fs/btrfs/space-info.h +2
-0@@ -3,6 +3,8 @@ #ifndef BTRFS_SPACE_INFO_H #define BTRFS_SPACE_INFO_H +#include "volumes.h" + struct btrfs_space_info { spinlock_t lock;fs/btrfs/volumes.c +0
-26@@ -154,32 +154,6 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { }, }; -/* -* Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which -* can be used as index to access btrfs_raid_array[]. -*/ -enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags) -{ -if (flags &BTRFS_BLOCK_GROUP_RAID10 ) -return BTRFS_RAID_RAID10 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID1 ) -return BTRFS_RAID_RAID1 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID1C3 ) -return BTRFS_RAID_RAID1C3 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID1C4 ) -return BTRFS_RAID_RAID1C4 ; -else if (flags &BTRFS_BLOCK_GROUP_DUP ) -return BTRFS_RAID_DUP ; -else if (flags &BTRFS_BLOCK_GROUP_RAID0 ) -return BTRFS_RAID_RAID0 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID5 ) -return BTRFS_RAID_RAID5 ; -else if (flags &BTRFS_BLOCK_GROUP_RAID6 ) -return BTRFS_RAID_RAID6 ; - -return BTRFS_RAID_SINGLE ;/* BTRFS_BLOCK_GROUP_SINGLE */ -} -const char *btrfs_bg_type_to_raid_name (u64 flags) {const int index =btrfs_bg_flags_to_raid_index (flags);fs/btrfs/volumes.h +32
-9@@ -17,19 +17,42 @@ extern struct mutex uuid_mutex;#define BTRFS_STRIPE_LEN SZ_64K +/* +* Here we use ilog2(BTRFS_BLOCK_GROUP_*) to convert the profile bits to +* an index. +* We reserve 0 for BTRFS_RAID_SINGLE, while the lowest profile, ilog2(RAID0), +* is 3, thus we need this shift to make all index numbers sequential. +*/ +#define BTRFS_RAID_SHIFT (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1) +enum btrfs_raid_types { -BTRFS_RAID_RAID10 , -BTRFS_RAID_RAID1 , -BTRFS_RAID_DUP , -BTRFS_RAID_RAID0 , -BTRFS_RAID_SINGLE , -BTRFS_RAID_RAID5 , -BTRFS_RAID_RAID6 , -BTRFS_RAID_RAID1C3 , -BTRFS_RAID_RAID1C4 , +BTRFS_RAID_SINGLE =0 , +BTRFS_RAID_RAID0 =ilog2 (BTRFS_BLOCK_GROUP_RAID0 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1 =ilog2 (BTRFS_BLOCK_GROUP_RAID1 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_DUP =ilog2 (BTRFS_BLOCK_GROUP_DUP >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID10 =ilog2 (BTRFS_BLOCK_GROUP_RAID10 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID5 =ilog2 (BTRFS_BLOCK_GROUP_RAID5 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID6 =ilog2 (BTRFS_BLOCK_GROUP_RAID6 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1C3 =ilog2 (BTRFS_BLOCK_GROUP_RAID1C3 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1C4 =ilog2 (BTRFS_BLOCK_GROUP_RAID1C4 >>BTRFS_RAID_SHIFT ),BTRFS_NR_RAID_TYPES }; +/* +* Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which +* can be used as index to access btrfs_raid_array[]. +*/ +static inline enum btrfs_raid_types __attribute_const__ +btrfs_bg_flags_to_raid_index (u64 flags) +{ +u64 profile = flags &BTRFS_BLOCK_GROUP_PROFILE_MASK ; + +if (!profile) +return BTRFS_RAID_SINGLE ; + +return ilog2 (profile >>BTRFS_RAID_SHIFT ); +} +struct btrfs_io_geometry {/* remaining bytes before crossing a stripe */ u64 len;
fs/btrfs/volumes.h +0
-1@@ -646,7 +669,6 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,struct block_device *bdev, const char *device_path); -enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags);int btrfs_bg_type_to_factor (u64 flags);const char *btrfs_bg_type_to_raid_name (u64 flags);int btrfs_verify_dev_extents (struct btrfs_fs_info *fs_info);
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 19 lines · Anand Jain
Quoted 9 lines · Qu Wenruo
In function
btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.Quoted 8 lines
Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).
Even with that calculation involved (one
if(), oneilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.
Quoted 5 lines
Why not just use reverse static index similar to
const struct
btrfs_raid_attrbtrfs_raid_array[BTRFS_NR_RAID_TYPES] = { <snip>
}
Sorry, I didn't get the point.
Mind to share more details?
Thanks,
Qu
Quoted 136 lines
Thanks, Anand
Quoted 132 lines
Signed-off-by Qu Wenruo <[email protected]>---
fs/btrfs/space-info.h| 2 ++
fs/btrfs/volumes.c | 26 -------------------------- fs/btrfs/volumes.h | 42 ++++++++++++++++++++++++++++++++----------3 files changed, 34 insertions(+), 36 deletions(-)fs/btrfs/space-info.h +2
-0@@ -3,6 +3,8 @@ #ifndef BTRFS_SPACE_INFO_H #define BTRFS_SPACE_INFO_H +#include "volumes.h" + struct btrfs_space_info { spinlock_t lock;fs/btrfs/volumes.c +0
-2@@ -154,32 +154,6 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { }, }; -/* -* Convert block group flags (BTRFS_BLOCK_GROUP_*) to
btrfs_raid_types, which
- * can be used as index to access btrfs_raid_array[].
- */
-enumbtrfs_raid_types__attribute_const__btrfs_bg_flags_to_raid_index(u64 flags)fs/btrfs/volumes.c +0
-21-{ - if (flags & BTRFS_BLOCK_GROUP_RAID10) - return BTRFS_RAID_RAID10; - else if (flags & BTRFS_BLOCK_GROUP_RAID1) - return BTRFS_RAID_RAID1; - else if (flags & BTRFS_BLOCK_GROUP_RAID1C3) - return BTRFS_RAID_RAID1C3; - else if (flags & BTRFS_BLOCK_GROUP_RAID1C4) - return BTRFS_RAID_RAID1C4; - else if (flags & BTRFS_BLOCK_GROUP_DUP) - return BTRFS_RAID_DUP; - else if (flags & BTRFS_BLOCK_GROUP_RAID0) - return BTRFS_RAID_RAID0; - else if (flags & BTRFS_BLOCK_GROUP_RAID5) - return BTRFS_RAID_RAID5; - else if (flags & BTRFS_BLOCK_GROUP_RAID6) - return BTRFS_RAID_RAID6; - - return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ -} -const char *btrfs_bg_type_to_raid_name(u64 flags) {
const int index =btrfs_bg_flags_to_raid_index(flags);fs/btrfs/volumes.h +17
-9@@ -17,19 +17,42 @@ extern struct mutex uuid_mutex; #define BTRFS_STRIPE_LEN SZ_64K +/* +* Here we use ilog2(BTRFS_BLOCK_GROUP_*) to convert the profile bits to +* an index. +* We reserve 0 for BTRFS_RAID_SINGLE, while the lowest profile, ilog2(RAID0), +* is 3, thus we need this shift to make all index numbers sequential. +*/ +#define BTRFS_RAID_SHIFT (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1) + enum btrfs_raid_types { - BTRFS_RAID_RAID10, - BTRFS_RAID_RAID1, - BTRFS_RAID_DUP, - BTRFS_RAID_RAID0, - BTRFS_RAID_SINGLE, - BTRFS_RAID_RAID5, - BTRFS_RAID_RAID6, - BTRFS_RAID_RAID1C3, - BTRFS_RAID_RAID1C4, + BTRFS_RAID_SINGLE = 0, + BTRFS_RAID_RAID0 = ilog2(BTRFS_BLOCK_GROUP_RAID0 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID1 = ilog2(BTRFS_BLOCK_GROUP_RAID1 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_DUP = ilog2(BTRFS_BLOCK_GROUP_DUP >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID10 = ilog2(BTRFS_BLOCK_GROUP_RAID10 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID5 = ilog2(BTRFS_BLOCK_GROUP_RAID5 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID6 = ilog2(BTRFS_BLOCK_GROUP_RAID6 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID1C3 = ilog2(BTRFS_BLOCK_GROUP_RAID1C3 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID1C4 = ilog2(BTRFS_BLOCK_GROUP_RAID1C4 >>
BTRFS_RAID_SHIFT),
BTRFS_NR_RAID_TYPES
};+/*
+ * Convert block group flags (BTRFS_BLOCK_GROUP_*) tobtrfs_raid_types, which
+ * can be used as index to access btrfs_raid_array[].
+ */fs/btrfs/volumes.h +11
-0+static inline enum btrfs_raid_types __attribute_const__ +btrfs_bg_flags_to_raid_index (u64 flags) +{ + u64 profile = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK; + + if (!profile) + return BTRFS_RAID_SINGLE; + + return ilog2(profile >> BTRFS_RAID_SHIFT); +} +struct
btrfs_io_geometry{
/* remaining bytes before crossing a stripe */ u64 len;fs/btrfs/volumes.h +0
-1@@ -646,7 +669,6 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct block_device *bdev, const char *device_path); -enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags); int btrfs_bg_type_to_factor(u64 flags); const char *btrfs_bg_type_to_raid_name(u64 flags);int
btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 24 lines · Qu Wenruo
Quoted 21 lines · Anand Jain
Quoted 9 lines · Qu Wenruo
In function
btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.Quoted 8 lines
Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).
Even with that calculation involved (one
if(), oneilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.Why not just use reverse static index similar to
Quoted 8 lines
Quoted 3 lines
const struct
btrfs_raid_attrbtrfs_raid_array[BTRFS_NR_RAID_TYPES] = { <snip>
}Sorry, I didn't get the point.
Mind to share more details?
Something like
/* Must match with the order of BTRFS_BLOCK_GROUP_XYZ */ +enum btrfs_raid_types {
+ BTRFS_RAID_RAID0, + BTRFS_RAID_RAID1, + BTRFS_RAID_DUP, + BTRFS_RAID_RAID10, + BTRFS_RAID_RAID5, + BTRFS_RAID_RAID6, + BTRFS_RAID_RAID1C3, + BTRFS_RAID_RAID1C4, + BTRFS_RAID_SINGLE, + BTRFS_NR_RAID_TYPES
+};
btrfs_bg_flags_to_raid_index(u64 flags)
{
int ret;
flags = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK;
ret = ilog2(flags);
if (ret == 0)
return BTRFS_RAID_SINGLE;
return ret - 3;
}Should work. No?
Thanks, Anand
Quoted 140 lines
Thanks,
QuQuoted 137 lines
Thanks, Anand
Quoted 133 lines
Signed-off-by Qu Wenruo <[email protected]>---
fs/btrfs/space-info.h| 2 ++
fs/btrfs/volumes.c | 26 -------------------------- fs/btrfs/volumes.h | 42 ++++++++++++++++++++++++++++++++----------3 files changed, 34 insertions(+), 36 deletions(-)fs/btrfs/space-info.h +2
-0@@ -3,6 +3,8 @@ #ifndef BTRFS_SPACE_INFO_H #define BTRFS_SPACE_INFO_H +#include "volumes.h" + struct btrfs_space_info { spinlock_t lock;fs/btrfs/volumes.c +0
-2@@ -154,32 +154,6 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { }, }; -/* -* Convert block group flags (BTRFS_BLOCK_GROUP_*) to
btrfs_raid_types, which
- * can be used as index to access btrfs_raid_array[].
- */
-enumbtrfs_raid_types__attribute_const__btrfs_bg_flags_to_raid_index(u64 flags)fs/btrfs/volumes.c +0
-21-{ - if (flags & BTRFS_BLOCK_GROUP_RAID10) - return BTRFS_RAID_RAID10; - else if (flags & BTRFS_BLOCK_GROUP_RAID1) - return BTRFS_RAID_RAID1; - else if (flags & BTRFS_BLOCK_GROUP_RAID1C3) - return BTRFS_RAID_RAID1C3; - else if (flags & BTRFS_BLOCK_GROUP_RAID1C4) - return BTRFS_RAID_RAID1C4; - else if (flags & BTRFS_BLOCK_GROUP_DUP) - return BTRFS_RAID_DUP; - else if (flags & BTRFS_BLOCK_GROUP_RAID0) - return BTRFS_RAID_RAID0; - else if (flags & BTRFS_BLOCK_GROUP_RAID5) - return BTRFS_RAID_RAID5; - else if (flags & BTRFS_BLOCK_GROUP_RAID6) - return BTRFS_RAID_RAID6; - - return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ -} -const char *btrfs_bg_type_to_raid_name(u64 flags) {
const int index =btrfs_bg_flags_to_raid_index(flags);fs/btrfs/volumes.h +2
-0@@ -17,19 +17,42 @@ extern struct mutex uuid_mutex; #define BTRFS_STRIPE_LEN SZ_64K +/* +* Here we use ilog2(BTRFS_BLOCK_GROUP_*) to convert the profile bits to
+ * an index.
+ * We reserve 0 forBTRFS_RAID_SINGLE, while the lowest profile, ilog2(RAID0),
+ * is 3, thus we need this shift to make all index numbers sequential.
+ */fs/btrfs/volumes.h +2
-0+#define BTRFS_RAID_SHIFT (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1) +enum
btrfs_raid_types{
-BTRFS_RAID_RAID10,
-BTRFS_RAID_RAID1,
-BTRFS_RAID_DUP,
-BTRFS_RAID_RAID0,
-BTRFS_RAID_SINGLE,
-BTRFS_RAID_RAID5,
-BTRFS_RAID_RAID6,
-BTRFS_RAID_RAID1C3,
-BTRFS_RAID_RAID1C4,
+ BTRFS_RAID_SINGLE = 0,
+ BTRFS_RAID_RAID0 =ilog2(BTRFS_BLOCK_GROUP_RAID0 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID1 =ilog2(BTRFS_BLOCK_GROUP_RAID1 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_DUP =ilog2(BTRFS_BLOCK_GROUP_DUP >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID10 =ilog2(BTRFS_BLOCK_GROUP_RAID10 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID5 =ilog2(BTRFS_BLOCK_GROUP_RAID5 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID6 =ilog2(BTRFS_BLOCK_GROUP_RAID6 >> BTRFS_RAID_SHIFT),
+BTRFS_RAID_RAID1C3=ilog2(BTRFS_BLOCK_GROUP_RAID1C3 >> BTRFS_RAID_SHIFT),
+BTRFS_RAID_RAID1C4=ilog2(BTRFS_BLOCK_GROUP_RAID1C4 >> BTRFS_RAID_SHIFT),
BTRFS_NR_RAID_TYPES
};+/*
+ * Convert block group flags (BTRFS_BLOCK_GROUP_*) tobtrfs_raid_types, which
+ * can be used as index to access btrfs_raid_array[].
+ */fs/btrfs/volumes.h +11
-0+static inline enum btrfs_raid_types __attribute_const__ +btrfs_bg_flags_to_raid_index (u64 flags) +{ + u64 profile = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK; + + if (!profile) + return BTRFS_RAID_SINGLE; + + return ilog2(profile >> BTRFS_RAID_SHIFT); +} +struct
btrfs_io_geometry{
/* remaining bytes before crossing a stripe */ u64 len;fs/btrfs/volumes.h +0
-1@@ -646,7 +669,6 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct block_device *bdev, const char *device_path); -enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags); int btrfs_bg_type_to_factor(u64 flags); const char *btrfs_bg_type_to_raid_name(u64 flags);int
btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 38 lines · Anand Jain
Quoted 24 lines · Qu Wenruo
Quoted 21 lines · Anand Jain
Quoted 9 lines · Qu Wenruo
In function
btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.Quoted 8 lines
Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).
Even with that calculation involved (one
if(), oneilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.Why not just use reverse static index similar to
Quoted 8 lines
Quoted 3 lines
const struct
btrfs_raid_attrbtrfs_raid_array[BTRFS_NR_RAID_TYPES] = { <snip>
}Sorry, I didn't get the point.
Mind to share more details?
Something like
Quoted 12 lines
/* Must match with the order of
BTRFS_BLOCK_GROUP_XYZ*/ +enumbtrfs_raid_types{
+BTRFS_RAID_RAID0,
+BTRFS_RAID_RAID1,
+BTRFS_RAID_DUP,
+BTRFS_RAID_RAID10,
+BTRFS_RAID_RAID5,
+BTRFS_RAID_RAID6,
+BTRFS_RAID_RAID1C3,
+BTRFS_RAID_RAID1C4,
+BTRFS_RAID_SINGLE,
+BTRFS_NR_RAID_TYPES
OK I got your point.
But the point here is, I don't want to enum btrfs_raid_types to hide the value and rely on us to make it in proper order.
Thus that's why my version have all the ilog2() values.
And in that case, it's just a difference between the shift value and where we put RAID_SINGLE.
Quoted 10 lines
+};
btrfs_bg_flags_to_raid_index(u64 flags)
{
int ret;
flags = flags &BTRFS_BLOCK_GROUP_PROFILE_MASK;ret = ilog2(flags);
if (ret == 0)
returnBTRFS_RAID_SINGLE;
In fact, ilog2(0) behavior is undefined, and should be avoided.
In this case, we still need to check if (flags == 0), and just change the shift value.
Thanks,
Qu
Quoted 148 lines
return ret - 3;
}Should work. No?
Thanks, Anand
Quoted 140 lines
Thanks,
QuQuoted 137 lines
Thanks, Anand
Quoted 133 lines
Signed-off-by Qu Wenruo <[email protected]>---
fs/btrfs/space-info.h| 2 ++
fs/btrfs/volumes.c | 26 -------------------------- fs/btrfs/volumes.h | 42 ++++++++++++++++++++++++++++++++----------3 files changed, 34 insertions(+), 36 deletions(-)fs/btrfs/space-info.h +2
-0@@ -3,6 +3,8 @@ #ifndef BTRFS_SPACE_INFO_H #define BTRFS_SPACE_INFO_H +#include "volumes.h" + struct btrfs_space_info { spinlock_t lock;fs/btrfs/volumes.c +0
-2@@ -154,32 +154,6 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { }, }; -/* -* Convert block group flags (BTRFS_BLOCK_GROUP_*) to
btrfs_raid_types, which
- * can be used as index to access btrfs_raid_array[].
- */
-enumbtrfs_raid_types__attribute_const__btrfs_bg_flags_to_raid_index(u64 flags)fs/btrfs/volumes.c +0
-21-{ - if (flags & BTRFS_BLOCK_GROUP_RAID10) - return BTRFS_RAID_RAID10; - else if (flags & BTRFS_BLOCK_GROUP_RAID1) - return BTRFS_RAID_RAID1; - else if (flags & BTRFS_BLOCK_GROUP_RAID1C3) - return BTRFS_RAID_RAID1C3; - else if (flags & BTRFS_BLOCK_GROUP_RAID1C4) - return BTRFS_RAID_RAID1C4; - else if (flags & BTRFS_BLOCK_GROUP_DUP) - return BTRFS_RAID_DUP; - else if (flags & BTRFS_BLOCK_GROUP_RAID0) - return BTRFS_RAID_RAID0; - else if (flags & BTRFS_BLOCK_GROUP_RAID5) - return BTRFS_RAID_RAID5; - else if (flags & BTRFS_BLOCK_GROUP_RAID6) - return BTRFS_RAID_RAID6; - - return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ -} -const char *btrfs_bg_type_to_raid_name(u64 flags) {
const int index =btrfs_bg_flags_to_raid_index(flags);fs/btrfs/volumes.h +2
-0@@ -17,19 +17,42 @@ extern struct mutex uuid_mutex; #define BTRFS_STRIPE_LEN SZ_64K +/* +* Here we use ilog2(BTRFS_BLOCK_GROUP_*) to convert the profile bits to
+ * an index.
+ * We reserve 0 forBTRFS_RAID_SINGLE, while the lowest profile, ilog2(RAID0),
+ * is 3, thus we need this shift to make all index numbers sequential.
+ */fs/btrfs/volumes.h +2
-0+#define BTRFS_RAID_SHIFT (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1) +enum
btrfs_raid_types{
-BTRFS_RAID_RAID10,
-BTRFS_RAID_RAID1,
-BTRFS_RAID_DUP,
-BTRFS_RAID_RAID0,
-BTRFS_RAID_SINGLE,
-BTRFS_RAID_RAID5,
-BTRFS_RAID_RAID6,
-BTRFS_RAID_RAID1C3,
-BTRFS_RAID_RAID1C4,
+ BTRFS_RAID_SINGLE = 0,
+ BTRFS_RAID_RAID0 =ilog2(BTRFS_BLOCK_GROUP_RAID0 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID1 =ilog2(BTRFS_BLOCK_GROUP_RAID1 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_DUP =ilog2(BTRFS_BLOCK_GROUP_DUP >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID10 =ilog2(BTRFS_BLOCK_GROUP_RAID10 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID5 =ilog2(BTRFS_BLOCK_GROUP_RAID5 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID6 =ilog2(BTRFS_BLOCK_GROUP_RAID6 >> BTRFS_RAID_SHIFT),
+BTRFS_RAID_RAID1C3=ilog2(BTRFS_BLOCK_GROUP_RAID1C3 >> BTRFS_RAID_SHIFT),
+BTRFS_RAID_RAID1C4=ilog2(BTRFS_BLOCK_GROUP_RAID1C4 >> BTRFS_RAID_SHIFT),
BTRFS_NR_RAID_TYPES
};+/*
+ * Convert block group flags (BTRFS_BLOCK_GROUP_*) tobtrfs_raid_types, which
+ * can be used as index to access btrfs_raid_array[].
+ */fs/btrfs/volumes.h +11
-0+static inline enum btrfs_raid_types __attribute_const__ +btrfs_bg_flags_to_raid_index (u64 flags) +{ + u64 profile = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK; + + if (!profile) + return BTRFS_RAID_SINGLE; + + return ilog2(profile >> BTRFS_RAID_SHIFT); +} +struct
btrfs_io_geometry{
/* remaining bytes before crossing a stripe */ u64 len;fs/btrfs/volumes.h +0
-1@@ -646,7 +669,6 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct block_device *bdev, const char *device_path); -enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags); int btrfs_bg_type_to_factor(u64 flags); const char *btrfs_bg_type_to_raid_name(u64 flags);int
btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 56 lines · Qu Wenruo
Quoted 38 lines · Anand Jain
Quoted 24 lines · Qu Wenruo
Quoted 21 lines · Anand Jain
Quoted 9 lines · Qu Wenruo
In function
btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.Quoted 8 lines
Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).
Even with that calculation involved (one
if(), oneilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.Why not just use reverse static index similar to
Quoted 8 lines
Quoted 3 lines
const struct
btrfs_raid_attrbtrfs_raid_array[BTRFS_NR_RAID_TYPES] = { <snip>
}Sorry, I didn't get the point.
Mind to share more details?
Something like
Quoted 12 lines
/* Must match with the order of
BTRFS_BLOCK_GROUP_XYZ*/ +enumbtrfs_raid_types{
+BTRFS_RAID_RAID0,
+BTRFS_RAID_RAID1,
+BTRFS_RAID_DUP,
+BTRFS_RAID_RAID10,
+BTRFS_RAID_RAID5,
+BTRFS_RAID_RAID6,
+BTRFS_RAID_RAID1C3,
+BTRFS_RAID_RAID1C4,
+BTRFS_RAID_SINGLE,
+BTRFS_NR_RAID_TYPESOK I got your point.
Quoted 7 lines
But the point here is, I don't want to enum
btrfs_raid_typesto hide the value and rely on us to make it in proper order.Thus that's why my version have all the
ilog2()values.And in that case, it's just a difference between the shift value and where we put
RAID_SINGLE.
Agree. If some commit misses the order, we are in the soup.
More comments below.
Quoted 101 lines
Quoted 11 lines
+};
btrfs_bg_flags_to_raid_index(u64 flags)
{
int ret;
flags = flags &BTRFS_BLOCK_GROUP_PROFILE_MASK;ret = ilog2(flags);
if (ret == 0)
returnBTRFS_RAID_SINGLE;In fact, ilog2(0) behavior is undefined, and should be avoided.
In this case, we still need to check if (flags == 0), and just change the shift value.
Thanks,
QuQuoted 81 lines
return ret - 3;
}Should work. No?
Thanks, Anand
Quoted 73 lines
Thanks,
QuQuoted 70 lines
Thanks, Anand
Quoted 67 lines
Signed-off-by Qu Wenruo <[email protected]>---
fs/btrfs/space-info.h| 2 ++ fs/btrfs/volumes.c | 26 -------------------------- fs/btrfs/volumes.h | 42
++++++++++++++++++++++++++++++++----------3 files changed, 34 insertions(+), 36 deletions(-)fs/btrfs/space-info.h +2
-0@@ -3,6 +3,8 @@ #ifndef BTRFS_SPACE_INFO_H #define BTRFS_SPACE_INFO_H +#include "volumes.h" + struct btrfs_space_info { spinlock_t lock;fs/btrfs/volumes.c +0
-2@@ -154,32 +154,6 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { }, }; -/* -* Convert block group flags (BTRFS_BLOCK_GROUP_*) to
btrfs_raid_types, which
- * can be used as index to access btrfs_raid_array[].
- */
-enumbtrfs_raid_types__attribute_const__btrfs_bg_flags_to_raid_index(u64 flags)fs/btrfs/volumes.c +0
-21-{ - if (flags & BTRFS_BLOCK_GROUP_RAID10) - return BTRFS_RAID_RAID10; - else if (flags & BTRFS_BLOCK_GROUP_RAID1) - return BTRFS_RAID_RAID1; - else if (flags & BTRFS_BLOCK_GROUP_RAID1C3) - return BTRFS_RAID_RAID1C3; - else if (flags & BTRFS_BLOCK_GROUP_RAID1C4) - return BTRFS_RAID_RAID1C4; - else if (flags & BTRFS_BLOCK_GROUP_DUP) - return BTRFS_RAID_DUP; - else if (flags & BTRFS_BLOCK_GROUP_RAID0) - return BTRFS_RAID_RAID0; - else if (flags & BTRFS_BLOCK_GROUP_RAID5) - return BTRFS_RAID_RAID5; - else if (flags & BTRFS_BLOCK_GROUP_RAID6) - return BTRFS_RAID_RAID6; - - return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ -} -const char *btrfs_bg_type_to_raid_name(u64 flags) {
const int index =btrfs_bg_flags_to_raid_index(flags);fs/btrfs/volumes.h +2
-0@@ -17,19 +17,42 @@ extern struct mutex uuid_mutex; #define BTRFS_STRIPE_LEN SZ_64K +/* +* Here we use ilog2(BTRFS_BLOCK_GROUP_*) to convert the profile bits to
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/btrfs/volumes.h
Quoted 8 lines
+ * an index.
+ * We reserve 0 forBTRFS_RAID_SINGLE, while the lowest profile, ilog2(RAID0),
+ * is 3, thus we need this shift to make all index numbers sequential.
+ */fs/btrfs/volumes.h +2
-0+#define BTRFS_RAID_SHIFT (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1) +
David may need this to be.
#define BTRFS_BG_FLAG_TO_INDEX(f) \ ilog2(f >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1))
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
enum
btrfs_raid_types{
-BTRFS_RAID_RAID10,
-BTRFS_RAID_RAID1,
-BTRFS_RAID_DUP,
-BTRFS_RAID_RAID0,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
-
BTRFS_RAID_SINGLE,
-BTRFS_RAID_RAID5,
-BTRFS_RAID_RAID6,
-BTRFS_RAID_RAID1C3,
-BTRFS_RAID_RAID1C4,
+ BTRFS_RAID_SINGLE = 0,
+ BTRFS_RAID_RAID0 =ilog2(BTRFS_BLOCK_GROUP_RAID0 >> BTRFS_RAID_SHIFT),
BTRFS_RAID_RAID0 = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_RAID0),
::
etc.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 16 lines
+ BTRFS_RAID_RAID1 =
ilog2(BTRFS_BLOCK_GROUP_RAID1 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_DUP =ilog2(BTRFS_BLOCK_GROUP_DUP >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID10 =ilog2(BTRFS_BLOCK_GROUP_RAID10 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID5 =ilog2(BTRFS_BLOCK_GROUP_RAID5 >> BTRFS_RAID_SHIFT),
+ BTRFS_RAID_RAID6 =ilog2(BTRFS_BLOCK_GROUP_RAID6 >> BTRFS_RAID_SHIFT),
+BTRFS_RAID_RAID1C3=ilog2(BTRFS_BLOCK_GROUP_RAID1C3 >> BTRFS_RAID_SHIFT),
+BTRFS_RAID_RAID1C4=ilog2(BTRFS_BLOCK_GROUP_RAID1C4 >> BTRFS_RAID_SHIFT),
BTRFS_NR_RAID_TYPES
};
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
+/*
+ * Convert block group flags (BTRFS_BLOCK_GROUP_*) tobtrfs_raid_types, which
+ * can be used as index to access btrfs_raid_array[].
+ */
+static inline enumbtrfs_raid_types__attribute_const__Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/btrfs/volumes.h extern struct mutex uuid_mutex; · line 53
Quoted 8 lines
fs/btrfs/volumes.h +8
-0+btrfs_bg_flags_to_raid_index(u64 flags) +{ + u64 profile = flags & BTRFS_BLOCK_GROUP_PROFILE_MASK; + + if (!profile) + return BTRFS_RAID_SINGLE; + + return ilog2(profile >> BTRFS_RAID_SHIFT);
return BTRFS_BG_FLAG_TO_INDEX(profile);
Otherwise looks good.
Thanks, Anand
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
fs/btrfs/volumes.h +2
-0+} +struct
btrfs_io_geometry{
/* remaining bytes before crossing a stripe */ u64 len;fs/btrfs/volumes.h +0
-1@@ -646,7 +669,6 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct block_device *bdev, const char *device_path); -enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags); int btrfs_bg_type_to_factor(u64 flags); const char *btrfs_bg_type_to_raid_name(u64 flags);int
btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
In reply to Qu WenruoQuoted 11 lines · Qu Wenruo
fs/btrfs/volumes.h +2
-8+#define BTRFS_RAID_SHIFT (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1) +enum btrfs_raid_types { -BTRFS_RAID_RAID10 , -BTRFS_RAID_RAID1 , -BTRFS_RAID_DUP , -BTRFS_RAID_RAID0 , -BTRFS_RAID_SINGLE , -BTRFS_RAID_RAID5 , -BTRFS_RAID_RAID6 , -BTRFS_RAID_RAID1C3 ,
fs/btrfs/volumes.h extern struct mutex uuid_mutex; · line 37
Quoted 10 lines
fs/btrfs/volumes.h +9
-1- BTRFS_RAID_RAID1C4, +BTRFS_RAID_SINGLE =0 , +BTRFS_RAID_RAID0 =ilog2 (BTRFS_BLOCK_GROUP_RAID0 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1 =ilog2 (BTRFS_BLOCK_GROUP_RAID1 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_DUP =ilog2 (BTRFS_BLOCK_GROUP_DUP >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID10 =ilog2 (BTRFS_BLOCK_GROUP_RAID10 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID5 =ilog2 (BTRFS_BLOCK_GROUP_RAID5 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID6 =ilog2 (BTRFS_BLOCK_GROUP_RAID6 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1C3 =ilog2 (BTRFS_BLOCK_GROUP_RAID1C3 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID1C4 = ilog2(BTRFS_BLOCK_GROUP_RAID1C4 >> BTRFS_RAID_SHIFT),
Please use const_ilog2 in case all the terms in the expression are constants that can be evaluated at the enum definition time.
I agree that deriving the indexes from the flags is safe but all the magic around that scares me a bit.
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 17 lines · David Sterba
Quoted 16 lines · Qu Wenruo
fs/btrfs/volumes.h +6
-9+#define BTRFS_RAID_SHIFT (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1) +enum btrfs_raid_types { -BTRFS_RAID_RAID10 , -BTRFS_RAID_RAID1 , -BTRFS_RAID_DUP , -BTRFS_RAID_RAID0 , -BTRFS_RAID_SINGLE , -BTRFS_RAID_RAID5 , -BTRFS_RAID_RAID6 , -BTRFS_RAID_RAID1C3 , -BTRFS_RAID_RAID1C4 , +BTRFS_RAID_SINGLE =0 , +BTRFS_RAID_RAID0 =ilog2 (BTRFS_BLOCK_GROUP_RAID0 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1 =ilog2 (BTRFS_BLOCK_GROUP_RAID1 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_DUP =ilog2 (BTRFS_BLOCK_GROUP_DUP >>BTRFS_RAID_SHIFT ),
Quoted 8 lines
fs/btrfs/volumes.h extern struct mutex uuid_mutex; · line 37
Quoted 5 lines
fs/btrfs/volumes.h +5
-0+BTRFS_RAID_RAID10 =ilog2 (BTRFS_BLOCK_GROUP_RAID10 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID5 =ilog2 (BTRFS_BLOCK_GROUP_RAID5 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID6 =ilog2 (BTRFS_BLOCK_GROUP_RAID6 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1C3 =ilog2 (BTRFS_BLOCK_GROUP_RAID1C3 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID1C4 = ilog2(BTRFS_BLOCK_GROUP_RAID1C4 >> BTRFS_RAID_SHIFT),Please use
const_ilog2in case all the terms in the expression are constants that can be evaluated at the enum definition time.
Why? Kernel ilog2() is already handling the constants.
That's the biggest difference between kernel ilog2() and btrfs-progs ilog2().
Only in btrfs-progs we need const_ilog2().
In fact, the comment of kernel ilog2() already shows this:
/**
* ilog2 - log base 2 of 32-bit or a 64-bit unsigned value
* @n: parameter
*
* constant-capable log of base 2 calculation
* - this can be used to initialise global variables from constant
data, hence
* the massive ternary operator construction
*
* selects the appropriately-sized optimised version depending on sizeof(n) */
Quoted 3 lines
I agree that deriving the indexes from the flags is safe but all the magic around that scares me a bit.
That's indeed a little concerning.
That's why I spare no code to make it as hard as possible to break, like all the explicit definition of each BTRFS_RAID_* number.
To me the only possible problem in the future is someone adding two or more profiles in one go, and possibly corrupt the BTRFS_NR_RAID_TYPES.
But all BTRFS_RAID_* number should be pretty safe.
Thanks,
Qu
Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 36 lines · Qu Wenruo
Quoted 17 lines · David Sterba
Quoted 16 lines · Qu Wenruo
fs/btrfs/volumes.h +6
-9+#define BTRFS_RAID_SHIFT (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1) +enum btrfs_raid_types { -BTRFS_RAID_RAID10 , -BTRFS_RAID_RAID1 , -BTRFS_RAID_DUP , -BTRFS_RAID_RAID0 , -BTRFS_RAID_SINGLE , -BTRFS_RAID_RAID5 , -BTRFS_RAID_RAID6 , -BTRFS_RAID_RAID1C3 , -BTRFS_RAID_RAID1C4 , +BTRFS_RAID_SINGLE =0 , +BTRFS_RAID_RAID0 =ilog2 (BTRFS_BLOCK_GROUP_RAID0 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1 =ilog2 (BTRFS_BLOCK_GROUP_RAID1 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_DUP =ilog2 (BTRFS_BLOCK_GROUP_DUP >>BTRFS_RAID_SHIFT ),Quoted 8 lines
fs/btrfs/volumes.h extern struct mutex uuid_mutex; · line 37
Quoted 5 lines
fs/btrfs/volumes.h +5
-0+BTRFS_RAID_RAID10 =ilog2 (BTRFS_BLOCK_GROUP_RAID10 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID5 =ilog2 (BTRFS_BLOCK_GROUP_RAID5 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID6 =ilog2 (BTRFS_BLOCK_GROUP_RAID6 >>BTRFS_RAID_SHIFT ), +BTRFS_RAID_RAID1C3 =ilog2 (BTRFS_BLOCK_GROUP_RAID1C3 >>BTRFS_RAID_SHIFT ), + BTRFS_RAID_RAID1C4 = ilog2(BTRFS_BLOCK_GROUP_RAID1C4 >> BTRFS_RAID_SHIFT),Please use
const_ilog2in case all the terms in the expression are constants that can be evaluated at the enum definition time.Why? Kernel
ilog2()is already handling the constants.That's the biggest difference between kernel
ilog2()and btrfs-progsilog2().Only in btrfs-progs we need
const_ilog2().In fact, the comment of kernel
ilog2()already shows this:
Quoted 11 lines
/**
* ilog2 - log base 2 of 32-bit or a 64-bit unsigned value
* @n: parameter
*
* constant-capable log of base 2 calculation
* - this can be used to initialise global variables from constant
data, hence
* the massive ternary operator construction
*
* selects the appropriately-sized optimised version depending on sizeof(n) */
Right, ilog2 is fine.
Quoted 7 lines
Quoted 2 lines
I agree that deriving the indexes from the flags is safe but all the magic around that scares me a bit.
That's indeed a little concerning.
That's why I spare no code to make it as hard as possible to break, like all the explicit definition of each BTRFS_RAID_* number.
Yeah, as somebody else commented adding a wrapper for the definitions may be more explicit, ie. the part
ilog2(BTRFS_BLOCK_GROUP_RAID1C4 >> BTRFS_RAID_SHIFT)
hiding that it's ilog2 and doing the shift
DEFINE_IT(BTRFS_BLOCK_GROUP_RAID1C4)
using a macro should be all fine for the enum so it's all compile-time constant.
Quoted 2 lines
To me the only possible problem in the future is someone adding two or more profiles in one go, and possibly corrupt the
BTRFS_NR_RAID_TYPES.
I may have two more profiles to add, I'll add them one by one anyway as it's the cleaner.