xfs: reorder xfs_inode structure elements to remove unneeded padding.
PATCH v2 1/1 xfs: reorder xfs_inode structure elements to remove unneeded padding.
By reordering the elements in the xfs_inode structure, we can reduce the padding needed on an x86_64 system by 8 bytes.
Furthermore, it also enables denser packing of xfs_inode structures within slab pages. In the Debian 6.8.12-amd64, before applying the patch, the size of xfs_inode is 1000 bytes, allowing 32 xfs_inode structures to be allocated from an order-3 slab. After applying the patch, the size of xfs_inode is reduced to 992 bytes, allowing 33 xfs_inode structures to be allocated from an order-3 slab.
This improvement is also observed in the mainline kernel with the same config.
--- fs/xfs/xfs_inode.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/xfs/xfs_inode.h +6 -6
@@ -37,12 +37,6 @@ typedef struct xfs_inode {struct xfs_ifork i_df;/* data fork */ struct xfs_ifork i_af;/* attribute fork */ -/* Transaction and locking information. */ -struct xfs_inode_log_item *i_itemp;/* logging information */ -struct rw_semaphore i_lock;/* inode lock */ -atomic_t i_pincount;/* inode pin count */ -struct llist_node i_gclist;/* deferred inactivation list */ -/* * Bitsets of inode metadata that have been checked and/or are sick. * Callers must hold i_flags_lock before accessing this field.
@@ -88,6 +82,12 @@ typedef struct xfs_inode {/* VFS inode */ struct inode i_vnode;/* embedded VFS inode */ +/* Transaction and locking information. */ +struct xfs_inode_log_item *i_itemp;/* logging information */ +struct rw_semaphore i_lock;/* inode lock */ +struct llist_node i_gclist;/* deferred inactivation list */ +atomic_t i_pincount;/* inode pin count */ +/* pending io completions */ spinlock_t i_ioend_lock;struct work_struct i_ioend_work;
Signature · 1 line
2.39.2
Quoted 6 lines · Junchao Sun
By reordering the elements in the
xfs_inodestructure, we can reduce the padding needed on anx86_64system by 8 bytes.Furthermore, it also enables denser packing of
xfs_inodestructures within slab pages. In the Debian 6.8.12-amd64, before applying the patch, the size ofxfs_inodeis 1000 bytes,
Please use pahole to show where the holes are in the current TOT structure, not reference a distro kernel build that has a largely unknown config.
Quoted 18 lines
allowing 32
xfs_inodestructures to be allocated from an order-3 slab. After applying the patch, the size ofxfs_inodeis reduced to 992 bytes, allowing 33xfs_inodestructures to be allocated from an order-3 slab.This improvement is also observed in the mainline kernel with the same config.
Signed-off-by Junchao Sun <[email protected]>--- fs/xfs/xfs_inode.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)fs/xfs/xfs_inode.h +0
-0
fs/xfs/xfs_inode.h typedef struct xfs_inode { · line 44
Quoted 8 lines
fs/xfs/xfs_inode.h +0
-5@@ -37,12 +37,6 @@ typedef struct xfs_inode {struct xfs_ifork i_df;/* data fork */ struct xfs_ifork i_af;/* attribute fork */ -/* Transaction and locking information. */ -struct xfs_inode_log_item *i_itemp;/* logging information */ -struct rw_semaphore i_lock;/* inode lock */ -atomic_t i_pincount;/* inode pin count */ -struct llist_node i_gclist;/* deferred inactivation list */
There's lots of 4 byte holes in the structure due to stuff like xfs_ifork and xfs_imap being 4 byte aligned structures.
This only addresses a coupel fo them, and in doing so destroys the attempt at creating locality of access to the inode structure.
fs/xfs/xfs_inode.h typedef struct xfs_inode { · line 89
Quoted 13 lines
fs/xfs/xfs_inode.h +5
-1-/* * Bitsets of inode metadata that have been checked and/or are sick. * Callers must hold i_flags_lock before accessing this field. @@ -88,6 +82,12 @@ typedef struct xfs_inode {/* VFS inode */ struct inode i_vnode;/* embedded VFS inode */ +/* Transaction and locking information. */ +struct xfs_inode_log_item *i_itemp;/* logging information */ +struct rw_semaphore i_lock;/* inode lock */ +struct llist_node i_gclist;/* deferred inactivation list */ +atomic_t i_pincount;/* inode pin count */
This separates the items commonly accessed together in the core XFS code and so should be located near to each other (on the same cachelines if possible). It places them on the side of the VFS inode (at least 700 bytes further down the structure) and places them on the same cacheline as IO completion marshalling structures. These shouldn't be on the same cacheline as IO completion variables as they run concurrently and independently and so need to be separated.
really, if we are going to optimise the layout of the xfs_inode, let's just do it properly the first time. See the patch below, it reduces the struct xfs_inode to 960 bytes without changing much in it's layout at all.
-Dave.
Signature · 89 lines
Dave Chinner [email protected] xfs: repack the xfs_inode to reduce space. From: Dave Chinner <[email protected]> pahole reports several 4 byte holes in the xfs_inode with a size of 1000 bytes. We can reduce that by packing holes better without affecting the data access patterns to the inode structure. Some of these holes are a result of 4 byte aligned tail structures being padded out to 16 bytes in the xfs_inode. These structures are already tightly packed and 8 byte aligned, so if we are careful with the layout we can add __attribute(packed) to them so their tail padding gets. This allows us to add a 4 byte variable into the hole that they would have left with 8 byte alignment padding. This reduces the struct xfs_inode to 960 bytes, a 4% reduction from the original 1000 bytes, and it largely doesn't change access patterns or data cache footprint as the alignment of all the critical structures is completely unchanged. pahole output now reports: struct xfs_inode { struct xfs_mount * i_mount; /* 0 8 */ struct xfs_dquot * i_udquot; /* 8 8 */ struct xfs_dquot * i_gdquot; /* 16 8 */ struct xfs_dquot * i_pdquot; /* 24 8 */ xfs_ino_t i_ino; /* 32 8 */ struct xfs_imap i_imap; /* 40 12 */ spinlock_t i_flags_lock; /* 52 4 */ unsigned long i_flags; /* 56 8 */ /* --- cacheline 1 boundary (64 bytes) --- */ struct xfs_ifork * i_cowfp; /* 64 8 */ struct xfs_ifork i_df; /* 72 44 */ xfs_extlen_t i_extsize; /* 116 4 */ struct xfs_ifork i_af; /* 120 44 */ /* --- cacheline 2 boundary (128 bytes) was 36 bytes ago --- */ union { xfs_extlen_t i_cowextsize; /* 164 4 */ uint16_t i_flushiter; /* 164 2 */ }; /* 164 4 */ union { xfs_extlen_t i_cowextsize; /* 0 4 */ uint16_t i_flushiter; /* 0 2 */ }; struct xfs_inode_log_item * i_itemp; /* 168 8 */ struct rw_semaphore i_lock; /* 176 40 */ /* --- cacheline 3 boundary (192 bytes) was 24 bytes ago --- */ struct llist_node i_gclist; /* 216 8 */ atomic_t i_pincount; /* 224 4 */ uint16_t i_checked; /* 228 2 */ uint16_t i_sick; /* 230 2 */ uint64_t i_delayed_blks; /* 232 8 */ xfs_fsize_t i_disk_size; /* 240 8 */ xfs_rfsblock_t i_nblocks; /* 248 8 */ /* --- cacheline 4 boundary (256 bytes) --- */ prid_t i_projid; /* 256 4 */ uint8_t i_forkoff; /* 260 1 */ /* XXX 1 byte hole, try to pack */ uint16_t i_diflags; /* 262 2 */ uint64_t i_diflags2; /* 264 8 */ struct timespec64 i_crtime; /* 272 16 */ xfs_agino_t i_next_unlinked; /* 288 4 */ xfs_agino_t i_prev_unlinked; /* 292 4 */ struct inode i_vnode; /* 296 608 */ /* --- cacheline 14 boundary (896 bytes) was 8 bytes ago --- */ struct work_struct i_ioend_work; /* 904 32 */ struct list_head i_ioend_list; /* 936 16 */ spinlock_t i_ioend_lock; /* 952 4 */ /* size: 960, cachelines: 15, members: 33 */ /* sum members: 955, holes: 1, sum holes: 1 */ /* padding: 4 */ }; We have a 1 byte hole in the middle, and 4 bytes of tail padding. Signed-off-by: Dave Chinner <[email protected]> --- fs/xfs/libxfs/xfs_inode_buf.h | 2 +- fs/xfs/libxfs/xfs_inode_fork.h | 2 +- fs/xfs/xfs_inode.h | 21 +++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-)
fs/xfs/libxfs/xfs_inode_buf.h +1 -1
@@ -17,7 +17,7 @@ struct xfs_imap {xfs_daddr_t im_blkno;/* starting BB of inode chunk */ unsigned short im_len;/* length in BBs of inode chunk */ unsigned short im_boffset;/* inode offset in block in bytes */ -}; +} __attribute__((packed));int xfs_imap_to_bp (struct xfs_mount *mp,struct xfs_trans *tp,struct xfs_imap *imap,struct xfs_buf **bpp);
fs/xfs/libxfs/xfs_inode_fork.h +1 -1
@@ -23,7 +23,7 @@ struct xfs_ifork {short if_broot_bytes;/* bytes allocated for root */ int8_t if_format;/* format of this fork */ uint8_t if_needextents;/* extents have not been read */ -}; +} __attribute__((packed));/* * Worst-case increase in the fork extent count when we're adding a single
fs/xfs/xfs_inode.h +11 -10
@@ -32,16 +32,25 @@ typedef struct xfs_inode {xfs_ino_t i_ino;/* inode number (agno/agino)*/ struct xfs_imap i_imap;/* location for xfs_imap() */ +spinlock_t i_flags_lock;/* inode i_flags lock */ +unsigned long i_flags;/* see defined flags below */ +/* Extent information. */ struct xfs_ifork *i_cowfp;/* copy on write extents */ struct xfs_ifork i_df;/* data fork */ +xfs_extlen_t i_extsize;/* basic/minimum extent size */ struct xfs_ifork i_af;/* attribute fork */ +/* cowextsize is only used for v3 inodes, flushiter for v1/2 */ +union { +xfs_extlen_t i_cowextsize;/* basic cow extent size */ +uint16_t i_flushiter;/* incremented on flush */ + };/* Transaction and locking information. */ struct xfs_inode_log_item *i_itemp;/* logging information */ struct rw_semaphore i_lock;/* inode lock */ -atomic_t i_pincount;/* inode pin count */ struct llist_node i_gclist;/* deferred inactivation list */ +atomic_t i_pincount;/* inode pin count */ /* * Bitsets of inode metadata that have been checked and/or are sick.
@@ -50,19 +59,11 @@ typedef struct xfs_inode {uint16_t i_checked;uint16_t i_sick; -spinlock_t i_flags_lock;/* inode i_flags lock */ /* Miscellaneous state. */ -unsigned long i_flags;/* see defined flags below */ uint64_t i_delayed_blks;/* count of delay alloc blks */ xfs_fsize_t i_disk_size;/* number of bytes in file */ xfs_rfsblock_t i_nblocks;/* # of direct & btree blocks */ prid_t i_projid;/* owner's project id */ -xfs_extlen_t i_extsize;/* basic/minimum extent size */ -/* cowextsize is only used for v3 inodes, flushiter for v1/2 */ -union { -xfs_extlen_t i_cowextsize;/* basic cow extent size */ -uint16_t i_flushiter;/* incremented on flush */ - };uint8_t i_forkoff;/* attr fork offset >> 3 */ uint16_t i_diflags;/* XFS_DIFLAG_... */ uint64_t i_diflags2;/* XFS_DIFLAG2_... */
@@ -89,9 +90,9 @@ typedef struct xfs_inode {struct inode i_vnode;/* embedded VFS inode */ /* pending io completions */ -spinlock_t i_ioend_lock;struct work_struct i_ioend_work;struct list_head i_ioend_list; +spinlock_t i_ioend_lock; } xfs_inode_t;static inline bool xfs_inode_on_unlinked_list (const struct xfs_inode *ip)
Quoted 5 lines · Dave Chinner <[email protected]> 于2024年7月1日周一 10:16写道:
Quoted 3 lines · Junchao Sun
By reordering the elements in the
xfs_inodestructure, we can reduce the padding needed on anx86_64system by 8 bytes.
Quoted 8 lines
Quoted 8 lines
Furthermore, it also enables denser packing of
xfs_inodestructures within slab pages. In the Debian 6.8.12-amd64, before applying the patch, the size ofxfs_inodeis 1000 bytes,Please use pahole to show where the holes are in the current TOT structure, not reference a distro kernel build that has a largely unknown config.
Ok.
Quoted 31 lines
Quoted 19 lines
allowing 32
xfs_inodestructures to be allocated from an order-3 slab. After applying the patch, the size ofxfs_inodeis reduced to 992 bytes, allowing 33xfs_inodestructures to be allocated from an order-3 slab.This improvement is also observed in the mainline kernel with the same config.
Signed-off-by Junchao Sun <[email protected]>--- fs/xfs/xfs_inode.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)fs/xfs/xfs_inode.h +0
-0fs/xfs/xfs_inode.h typedef struct xfs_inode { · line 44
Quoted 8 lines
fs/xfs/xfs_inode.h +0
-5@@ -37,12 +37,6 @@ typedef struct xfs_inode {struct xfs_ifork i_df;/* data fork */ struct xfs_ifork i_af;/* attribute fork */ -/* Transaction and locking information. */ -struct xfs_inode_log_item *i_itemp;/* logging information */ -struct rw_semaphore i_lock;/* inode lock */ -atomic_t i_pincount;/* inode pin count */ -struct llist_node i_gclist;/* deferred inactivation list */ There's lots of 4 byte holes in the structure due to stuff like
xfs_iforkandxfs_imapbeing 4 byte aligned structures.
Quoted 30 lines
This only addresses a coupel fo them, and in doing so destroys the attempt at creating locality of access to the inode structure.
Quoted 27 lines
fs/xfs/xfs_inode.h +5
-1-/* * Bitsets of inode metadata that have been checked and/or are sick. * Callers must hold i_flags_lock before accessing this field. @@ -88,6 +82,12 @@ typedef struct xfs_inode {/* VFS inode */ struct inode i_vnode;/* embedded VFS inode */ +/* Transaction and locking information. */ +struct xfs_inode_log_item *i_itemp;/* logging information */ +struct rw_semaphore i_lock;/* inode lock */ +struct llist_node i_gclist;/* deferred inactivation list */ +atomic_t i_pincount;/* inode pin count */ This separates the items commonly accessed together in the core XFS code and so should be located near to each other (on the same cachelines if possible). It places them on the side of the VFS inode (at least 700 bytes further down the structure) and places them on the same cacheline as IO completion marshalling structures. These shouldn't be on the same cacheline as IO completion variables as they run concurrently and independently and so need to be separated.
really, if we are going to optimise the layout of the
xfs_inode, let's just do it properly the first time. See the patch below, it reduces the structxfs_inodeto 960 bytes without changing much in it's layout at all.
Ok, I see. Really appreciate for your detailed explanation and suggestions!
Quoted 181 lines
-Dave.
--
Dave Chinner
[email protected]xfs: repack the
xfs_inodeto reduce space.From: Dave Chinner <[email protected]>
pahole reports several 4 byte holes in the
xfs_inodewith a size of 1000 bytes. We can reduce that by packing holes better without affecting the data access patterns to the inode structure.Some of these holes are a result of 4 byte aligned tail structures being padded out to 16 bytes in the
xfs_inode. These structures are already tightly packed and 8 byte aligned, so if we are careful with the layout we can add__attribute(packed)to them so their tail padding gets. This allows us to add a 4 byte variable into the hole that they would have left with 8 byte alignment padding.This reduces the struct
xfs_inodeto 960 bytes, a 4% reduction from the original 1000 bytes, and it largely doesn't change access patterns or data cache footprint as the alignment of all the critical structures is completely unchanged.pahole output now reports:
struct
xfs_inode{struct xfs_mount * i_mount; /* 0 8 */ struct xfs_dquot * i_udquot; /* 8 8 */ struct xfs_dquot * i_gdquot; /* 16 8 */ struct xfs_dquot * i_pdquot; /* 24 8 */ xfs_ino_t i_ino; /* 32 8 */ struct xfs_imap i_imap; /* 40 12 */ spinlock_t i_flags_lock; /* 52 4 */ unsigned long i_flags; /* 56 8 *//* --- cacheline 1 boundary (64 bytes) --- */
structxfs_ifork*i_cowfp; /* 64 8 */
structxfs_iforki_df; /* 72 44 */
xfs_extlen_ti_extsize; /* 116 4 */
structxfs_iforki_af; /* 120 44 */
/* --- cacheline 2 boundary (128 bytes) was 36 bytes ago --- */ union {
xfs_extlen_ti_cowextsize; /* 164 4 */
uint16_ti_flushiter; /* 164 2 */
}; /* 164 4 */
union {
xfs_extlen_ti_cowextsize; /* 0 4 */
uint16_ti_flushiter; /* 0 2 */
};struct
xfs_inode_log_item*i_itemp; /* 168 8 */
structrw_semaphorei_lock; /* 176 40 */
/* --- cacheline 3 boundary (192 bytes) was 24 bytes ago --- */
structllist_nodei_gclist; /* 216 8 */
atomic_ti_pincount; /* 224 4 */
uint16_ti_checked; /* 228 2 */
uint16_ti_sick; /* 230 2 */
uint64_ti_delayed_blks; /* 232 8 */
xfs_fsize_ti_disk_size; /* 240 8 */
xfs_rfsblock_ti_nblocks; /* 248 8 */
/* --- cacheline 4 boundary (256 bytes) --- */
prid_ti_projid; /* 256 4 */
uint8_ti_forkoff; /* 260 1 *//* XXX 1 byte hole, try to pack */
uint16_ti_diflags; /* 262 2 */
uint64_ti_diflags2; /* 264 8 */
struct timespec64i_crtime; /* 272 16 */
xfs_agino_ti_next_unlinked; /* 288 4 */
xfs_agino_ti_prev_unlinked; /* 292 4 */
struct inodei_vnode; /* 296 608 */
/* --- cacheline 14 boundary (896 bytes) was 8 bytes ago --- */
structwork_structi_ioend_work; /* 904 32 */
structlist_headi_ioend_list; /* 936 16 */
spinlock_ti_ioend_lock; /* 952 4 *//* size: 960, cachelines: 15, members: 33 */ /* sum members: 955, holes: 1, sum holes: 1 */ /* padding: 4 */
};We have a 1 byte hole in the middle, and 4 bytes of tail padding.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/libxfs/xfs_inode_buf.h | 2 +- fs/xfs/libxfs/xfs_inode_fork.h | 2 +- fs/xfs/xfs_inode.h | 21 +++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-)fs/xfs/libxfs/xfs_inode_buf.h +1
-1@@ -17,7 +17,7 @@ struct xfs_imap {xfs_daddr_t im_blkno;/* starting BB of inode chunk */ unsigned short im_len;/* length in BBs of inode chunk */ unsigned short im_boffset;/* inode offset in block in bytes */ -}; +} __attribute__((packed));int xfs_imap_to_bp (struct xfs_mount *mp,struct xfs_trans *tp,struct xfs_imap *imap,struct xfs_buf **bpp);fs/xfs/libxfs/xfs_inode_fork.h +1
-1@@ -23,7 +23,7 @@ struct xfs_ifork {short if_broot_bytes;/* bytes allocated for root */ int8_t if_format;/* format of this fork */ uint8_t if_needextents;/* extents have not been read */ -}; +} __attribute__((packed));/* * Worst-case increase in the fork extent count when we're adding a single fs/xfs/xfs_inode.h +11
-10@@ -32,16 +32,25 @@ typedef struct xfs_inode {xfs_ino_t i_ino;/* inode number (agno/agino)*/ struct xfs_imap i_imap;/* location for xfs_imap() */ +spinlock_t i_flags_lock;/* inode i_flags lock */ +unsigned long i_flags;/* see defined flags below */ +/* Extent information. */ struct xfs_ifork *i_cowfp;/* copy on write extents */ struct xfs_ifork i_df;/* data fork */ +xfs_extlen_t i_extsize;/* basic/minimum extent size */ struct xfs_ifork i_af;/* attribute fork */ +/* cowextsize is only used for v3 inodes, flushiter for v1/2 */ +union { +xfs_extlen_t i_cowextsize;/* basic cow extent size */ +uint16_t i_flushiter;/* incremented on flush */ + };/* Transaction and locking information. */ struct xfs_inode_log_item *i_itemp;/* logging information */ struct rw_semaphore i_lock;/* inode lock */ -atomic_t i_pincount;/* inode pin count */ struct llist_node i_gclist;/* deferred inactivation list */ +atomic_t i_pincount;/* inode pin count */ /* * Bitsets of inode metadata that have been checked and/or are sick. @@ -50,19 +59,11 @@ typedef struct xfs_inode {uint16_t i_checked;uint16_t i_sick; -spinlock_t i_flags_lock;/* inode i_flags lock */ /* Miscellaneous state. */ -unsigned long i_flags;/* see defined flags below */ uint64_t i_delayed_blks;/* count of delay alloc blks */ xfs_fsize_t i_disk_size;/* number of bytes in file */ xfs_rfsblock_t i_nblocks;/* # of direct & btree blocks */ prid_t i_projid;/* owner's project id */ -xfs_extlen_t i_extsize;/* basic/minimum extent size */ -/* cowextsize is only used for v3 inodes, flushiter for v1/2 */ -union { -xfs_extlen_t i_cowextsize;/* basic cow extent size */ -uint16_t i_flushiter;/* incremented on flush */ - };uint8_t i_forkoff;/* attr fork offset >> 3 */ uint16_t i_diflags;/* XFS_DIFLAG_... */ uint64_t i_diflags2;/* XFS_DIFLAG2_... */ @@ -89,9 +90,9 @@ typedef struct xfs_inode {struct inode i_vnode;/* embedded VFS inode */ /* pending io completions */ -spinlock_t i_ioend_lock;struct work_struct i_ioend_work;struct list_head i_ioend_list; +spinlock_t i_ioend_lock; } xfs_inode_t;static inline bool xfs_inode_on_unlinked_list (const struct xfs_inode *ip)
Signature · 1 line
Junchao Sun <[email protected]>