Skip to contentSkip to conversation

buffer: Avoid setting buffer bits that are already set

PATCH In review Kemi Wang 2 participants 4 messages Oct 24, 2017 – Feb 2, 2018 Block layer

lore
Share & tools
lore

Kemi Wang

author
#0 lore raw

PATCH v2 1/1 buffer: Avoid setting buffer bits that are already set

It's expensive to set buffer flags that are already set, because that causes a costly cache line transition.

A common case is setting the "verified" flag during ext4 writes. This patch checks for the flag being set first.

With the AIM7/creat-clo benchmark testing on a 48G ramdisk based-on ext4 file system, we see 3.3%(15431->15936) improvement of aim7.jobs-per-min on a 2-sockets broadwell platform.

What the benchmark does is: it forks 3000 processes, and each process do the following:
a) open a new file
b) close the file
c) delete the file
until loop=100*1000 times.

The original patch is contributed by Andi Kleen.

---
 include/linux/buffer_head.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/buffer_head.h +4 -1
index c8dae55..211d8f5 100644
@@ -80,11 +80,14 @@ struct buffer_head {
 /*
  * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  * and buffer_foo() functions.
+ * To avoid reset buffer flags that are already set, because that causes
+ * a costly cache line transition, check the flag first.
  */
 #define BUFFER_FNS(bit, name)						\
 static __always_inline void set_buffer_##name(struct buffer_head *bh)	\
 {									\
-	set_bit(BH_##bit, &(bh)->b_state);				\
+	if (!test_bit(BH_##bit, &(bh)->b_state))			\
+		set_bit(BH_##bit, &(bh)->b_state);              \
 }									\
 static __always_inline void clear_buffer_##name(struct buffer_head *bh)	\
 {									\
Signature · 1 line
2.7.4
author
#1 lore raw
Quoted 17 lines · Kemi Wang

It's expensive to set buffer flags that are already set, because that causes a costly cache line transition.

A common case is setting the "verified" flag during ext4 writes. This patch checks for the flag being set first.

With the AIM7/creat-clo benchmark testing on a 48G ramdisk based-on ext4 file system, we see 3.3%(15431->15936) improvement of aim7.jobs-per-min on a 2-sockets broadwell platform.

What the benchmark does is: it forks 3000 processes, and each process do the following:
a) open a new file
b) close the file
c) delete the file
until loop=100*1000 times.

Quoted 7 lines

The original patch is contributed by Andi Kleen.

---

Seems that this patch is still not merged. Anything wrong with that? thanks

Quoted 24 lines
 include/linux/buffer_head.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/buffer_head.h +4 -1
index c8dae55..211d8f5 100644
@@ -80,11 +80,14 @@ struct buffer_head {
 /*
  * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  * and buffer_foo() functions.
+ * To avoid reset buffer flags that are already set, because that causes
+ * a costly cache line transition, check the flag first.
  */
 #define BUFFER_FNS(bit, name)						\
 static __always_inline void set_buffer_##name(struct buffer_head *bh)	\
 {									\
-	set_bit(BH_##bit, &(bh)->b_state);				\
+	if (!test_bit(BH_##bit, &(bh)->b_state))			\
+		set_bit(BH_##bit, &(bh)->b_state);              \
 }									\
 static __always_inline void clear_buffer_##name(struct buffer_head *bh)	\
 {									\
author
#2 lore raw

Hi, Jens
Could you help to merge this patch to your tree? Thanks

Quoted 54 lines · kemi
Quoted 17 lines · Kemi Wang

It's expensive to set buffer flags that are already set, because that causes a costly cache line transition.

A common case is setting the "verified" flag during ext4 writes. This patch checks for the flag being set first.

With the AIM7/creat-clo benchmark testing on a 48G ramdisk based-on ext4 file system, we see 3.3%(15431->15936) improvement of aim7.jobs-per-min on a 2-sockets broadwell platform.

What the benchmark does is: it forks 3000 processes, and each process do the following:
a) open a new file
b) close the file
c) delete the file
until loop=100*1000 times.

Quoted 7 lines

The original patch is contributed by Andi Kleen.

---

Seems that this patch is still not merged. Anything wrong with that? thanks

Quoted 24 lines
 include/linux/buffer_head.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/buffer_head.h +4 -1
index c8dae55..211d8f5 100644
@@ -80,11 +80,14 @@ struct buffer_head {
 /*
  * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  * and buffer_foo() functions.
+ * To avoid reset buffer flags that are already set, because that causes
+ * a costly cache line transition, check the flag first.
  */
 #define BUFFER_FNS(bit, name)						\
 static __always_inline void set_buffer_##name(struct buffer_head *bh)	\
 {									\
-	set_bit(BH_##bit, &(bh)->b_state);				\
+	if (!test_bit(BH_##bit, &(bh)->b_state))			\
+		set_bit(BH_##bit, &(bh)->b_state);              \
 }									\
 static __always_inline void clear_buffer_##name(struct buffer_head *bh)	\
 {									\
maintainer
#3 lore raw
Quoted 2 lines · kemi

Hi, Jens
Could you help to merge this patch to your tree? Thanks

Yes, I'll queue it up, thanks.

Signature · 1 line
Jens Axboe