Skip to contentSkip to conversation

btrfs-progs: align btrfs receive buffer to enable fast CRC

PATCH In review [email protected] 3 participants 5 messages Dec 26, 2020 – Jan 14, 2021 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: align btrfs receive buffer to enable fast CRC

From: Sheng Mao <[email protected]>

To use optimized CRC implemention, the input buffer must be unsigned long aligned. btrfs receive calculates checksum based on read_buf, including btrfs_cmd_header (with zero-ed CRC field) and command content. GCC attribute is added to both struct btrfs_send_stream and read_buf to make sure read_buf is allocated with proper alignment.

Issue: #324

---
 common/send-stream.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
common/send-stream.c +3 -2
index 69d75168..b13aa16e 100644
@@ -26,7 +26,8 @@
 
 struct btrfs_send_stream {
 	int fd;
-	char read_buf[BTRFS_SEND_BUF_SIZE];
+	char read_buf[BTRFS_SEND_BUF_SIZE]
+		__attribute__((aligned(sizeof(unsigned long))));
 
 	int cmd;
 	struct btrfs_cmd_header *cmd_hdr;
@@ -41,7 +42,7 @@ struct btrfs_send_stream {
 
 	struct btrfs_send_ops *ops;
 	void *user;
-};
+} __attribute__((aligned(sizeof(unsigned long))));
 
 /*
  * Read len bytes to buf.
Signature · 1 line
2.29.2
#1 lore raw

Hi, Sheng

Quoted 20 lines

From: Sheng Mao <[email protected]>

To use optimized CRC implemention, the input buffer must be unsigned long aligned. btrfs receive calculates checksum based on read_buf, including btrfs_cmd_header (with zero-ed CRC field) and command content. GCC attribute is added to both struct btrfs_send_stream and read_buf to make sure read_buf is allocated with proper alignment.

Issue: #324

---
 common/send-stream.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
common/send-stream.c +0 -0
index 69d75168..b13aa16e 100644

common/send-stream.c line 32

Quoted 8 lines
common/send-stream.c +2 -1
@@ -26,7 +26,8 @@
 
 struct btrfs_send_stream {
 	int fd;
-	char read_buf[BTRFS_SEND_BUF_SIZE];
+	char read_buf[BTRFS_SEND_BUF_SIZE]
+		__attribute__((aligned(sizeof(unsigned long))));
 
 	int cmd;

Can we move 'int cmd' to before 'char read_buf' too?

There is a hole between 'int fd' and 'char read_buf' after the addiatioin of '__attribute__((aligned(sizeof(unsigned long))))' in x86_64.

Best Regards
Wang Yugui ([email protected])
2021/01/03

Quoted 12 lines
common/send-stream.c +1 -1
 	struct btrfs_cmd_header *cmd_hdr;
@@ -41,7 +42,7 @@ struct btrfs_send_stream {
 
 	struct btrfs_send_ops *ops;
 	void *user;
-};
+} __attribute__((aligned(sizeof(unsigned long))));
 
 /*
  * Read len bytes to buf.
Signature · 1 line
2.29.2
#2 lore raw
In reply to [email protected]
Quoted 10 lines · On Sat, Dec 26, 2020 at 02:46:06PM -0700, [email protected] wrote:

From: Sheng Mao <[email protected]>

To use optimized CRC implemention, the input buffer must be unsigned long aligned. btrfs receive calculates checksum based on read_buf, including btrfs_cmd_header (with zero-ed CRC field) and command content. GCC attribute is added to both struct btrfs_send_stream and read_buf to make sure read_buf is allocated with proper alignment.

Issue: #324

The issue has a lot of interesting debugging and performance information so I'd put something to the changelog as well.

The alignment fixup sounds correct, though I'd push it a bit further and move read_buf to the beginning of the structure and align the whole structure to 64 bytes, as this is the common cache line size. This could potentially help too with memcpy.

author
#3 lore raw

Alignment to cache line is a great idea. I guess compiler can issue faster memcpy version.

Thank you!

Quoted 20 lines · David Sterba
Quoted 10 lines · On Sat, Dec 26, 2020 at 02:46:06PM -0700, [email protected] wrote:

From: Sheng Mao <[email protected]>

To use optimized CRC implemention, the input buffer must be unsigned long aligned. btrfs receive calculates checksum based on read_buf, including btrfs_cmd_header (with zero-ed CRC field) and command content. GCC attribute is added to both struct btrfs_send_stream and read_buf to make sure read_buf is allocated with proper alignment.

Issue: #324

The issue has a lot of interesting debugging and performance information so I'd put something to the changelog as well.

The alignment fixup sounds correct, though I'd push it a bit further and move read_buf to the beginning of the structure and align the whole structure to 64 bytes, as this is the common cache line size. This could potentially help too with memcpy.

#4 lore raw
In reply to [email protected]
Quoted 11 lines · On Sat, Dec 26, 2020 at 02:46:06PM -0700, [email protected] wrote:

From: Sheng Mao <[email protected]>

To use optimized CRC implemention, the input buffer must be unsigned long aligned. btrfs receive calculates checksum based on read_buf, including btrfs_cmd_header (with zero-ed CRC field) and command content. GCC attribute is added to both struct btrfs_send_stream and read_buf to make sure read_buf is allocated with proper alignment.

Issue: #324

Fix added to devel, with the mentined changes: read_buf is at the begninning and whole structure 64 bytes aligned. Thanks.