Skip to content

Commit f2d9e6f

Browse files
Wu BoJaegeuk Kim
authored andcommitted
f2fs-tools: optimize the addrs_per_block/inode loop
ADDRS_PER_INODE and ADDRS_PER_BLOCK are macros of a function, and if the compiler does not optimize the code (-O0 for example), they would be called in every single loop, which could cause significant performance drops in some cases. This scenario has been found before, refer to: Commit: 1bb669e ("fsck.f2fs: avoid unnecessary recalculation") To avoid this performance drop as much as possible, I changed the function to 'inline' definition, which could mitigate such cases in the future. Signed-off-by: Wu Bo <wuboo@amazon.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent f5bc9fb commit f2d9e6f

5 files changed

Lines changed: 25 additions & 19 deletions

File tree

fsck/dump.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ static int dump_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
495495
{
496496
u32 i = 0;
497497
u64 ofs = 0;
498-
u32 addr_per_block;
498+
u32 addr_per_block, addr_per_inode;
499499
u16 type = le16_to_cpu(node_blk->i.i_mode);
500500
int ret = 0;
501501

@@ -543,9 +543,10 @@ static int dump_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
543543
}
544544

545545
addr_per_block = ADDRS_PER_BLOCK(&node_blk->i);
546+
addr_per_inode = ADDRS_PER_INODE(&node_blk->i);
546547

547548
/* check data blocks in inode */
548-
for (i = 0; i < ADDRS_PER_INODE(&node_blk->i); i++, ofs++)
549+
for (i = 0; i < addr_per_inode; i++, ofs++)
549550
dump_data_blk(sbi, ofs * F2FS_BLKSIZE, le32_to_cpu(
550551
node_blk->i.i_addr[get_extra_isize(node_blk) + i]), type);
551552

fsck/fsck.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,8 +1516,9 @@ int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
15161516
bool compressed = i_flags & F2FS_COMPR_FL;
15171517
bool compr_rel = inode->i_inline & F2FS_COMPRESS_RELEASED;
15181518
u32 cluster_size = 1 << inode->i_log_cluster_size;
1519+
u32 addrs = ADDRS_PER_BLOCK(inode);
15191520

1520-
for (idx = 0; idx < ADDRS_PER_BLOCK(inode); idx++, child->pgofs++) {
1521+
for (idx = 0; idx < addrs; idx++, child->pgofs++) {
15211522
block_t blkaddr = le32_to_cpu(node_blk->dn.addr[idx]);
15221523

15231524
check_extent_info(child, blkaddr, 0);
@@ -3173,7 +3174,7 @@ static void fsck_disconnect_file_dnode(struct f2fs_sb_info *sbi,
31733174
{
31743175
struct f2fs_node *node;
31753176
struct node_info ni;
3176-
u32 addr;
3177+
u32 addr, addr_per_block;
31773178
int i, err;
31783179

31793180
node = calloc(F2FS_BLKSIZE, 1);
@@ -3187,7 +3188,8 @@ static void fsck_disconnect_file_dnode(struct f2fs_sb_info *sbi,
31873188
release_block_cnt(sbi, dealloc);
31883189
release_block(sbi, ni.blk_addr, dealloc);
31893190

3190-
for (i = 0; i < ADDRS_PER_BLOCK(inode); i++) {
3191+
addr_per_block = ADDRS_PER_BLOCK(inode);
3192+
for (i = 0; i < addr_per_block; i++) {
31913193
addr = le32_to_cpu(node->dn.addr[i]);
31923194
if (!addr)
31933195
continue;
@@ -3302,8 +3304,10 @@ static void fsck_disconnect_file(struct f2fs_sb_info *sbi, nid_t ino,
33023304

33033305
/* clear data counters */
33043306
if (!(node->i.i_inline & (F2FS_INLINE_DATA | F2FS_INLINE_DENTRY))) {
3307+
u32 addrs = ADDRS_PER_INODE(&node->i);
3308+
33053309
ofs = get_extra_isize(node);
3306-
for (i = 0; i < ADDRS_PER_INODE(&node->i); i++) {
3310+
for (i = 0; i < addrs; i++) {
33073311
block_t addr = le32_to_cpu(node->i.i_addr[ofs + i]);
33083312
if (!addr)
33093313
continue;

fsck/mount.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ void print_inode_info(struct f2fs_sb_info *sbi,
281281
u32 namelen = le32_to_cpu(inode->i_namelen);
282282
int enc_name = file_enc_name(inode);
283283
int ofs = get_extra_isize(node);
284+
u32 addrs;
284285

285286
pretty_print_filename(inode->i_name, namelen, en, enc_name);
286287
if (name && en[0]) {
@@ -350,7 +351,8 @@ void print_inode_info(struct f2fs_sb_info *sbi,
350351
}
351352
}
352353

353-
for (i = 0; i < ADDRS_PER_INODE(inode); i++) {
354+
addrs = ADDRS_PER_INODE(inode);
355+
for (i = 0; i < addrs; i++) {
354356
block_t blkaddr;
355357
char *flag = "";
356358

include/f2fs_fs.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,6 @@ struct f2fs_configuration {
16601660
extern int utf8_to_utf16(char *, const char *, size_t, size_t);
16611661
extern int utf16_to_utf8(char *, const char *, size_t, size_t);
16621662
extern int log_base_2(uint32_t);
1663-
extern unsigned int addrs_per_page(struct f2fs_inode *, bool);
16641663
extern u64 f2fs_max_file_offset(struct f2fs_inode *);
16651664
extern __u32 f2fs_inode_chksum(struct f2fs_node *);
16661665
extern __u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *);
@@ -2212,4 +2211,15 @@ static inline bool __time_to_inject(int type, const char *func,
22122211
return false;
22132212
}
22142213

2214+
static inline unsigned int addrs_per_page(struct f2fs_inode *i, bool is_inode)
2215+
{
2216+
unsigned int addrs = is_inode ? CUR_ADDRS_PER_INODE(i) -
2217+
get_inline_xattr_addrs(i) : DEF_ADDRS_PER_BLOCK;
2218+
2219+
if (!LINUX_S_ISREG(le16_to_cpu(i->i_mode)) ||
2220+
!(le32_to_cpu(i->i_flags) & F2FS_COMPR_FL))
2221+
return addrs;
2222+
return ALIGN_DOWN(addrs, 1 << i->i_log_cluster_size);
2223+
}
2224+
22152225
#endif /*__F2FS_FS_H */

lib/libf2fs.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,17 +516,6 @@ f2fs_hash_t f2fs_dentry_hash(int encoding, int casefolded,
516516
return __f2fs_dentry_hash(name, len);
517517
}
518518

519-
unsigned int addrs_per_page(struct f2fs_inode *i, bool is_inode)
520-
{
521-
unsigned int addrs = is_inode ? CUR_ADDRS_PER_INODE(i) -
522-
get_inline_xattr_addrs(i) : DEF_ADDRS_PER_BLOCK;
523-
524-
if (!LINUX_S_ISREG(le16_to_cpu(i->i_mode)) ||
525-
!(le32_to_cpu(i->i_flags) & F2FS_COMPR_FL))
526-
return addrs;
527-
return ALIGN_DOWN(addrs, 1 << i->i_log_cluster_size);
528-
}
529-
530519
u64 f2fs_max_file_offset(struct f2fs_inode *i)
531520
{
532521
if (!LINUX_S_ISREG(le16_to_cpu(i->i_mode)) ||

0 commit comments

Comments
 (0)