Skip to contentSkip to conversation

powerpc: Add 64bit optimised memcmp

PATCH In review Anton Blanchard 2 participants 4 messages Jan 21, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 powerpc: Add 64bit optimised memcmp

I noticed ksm spending quite a lot of time in memcmp on a large KVM box. The current memcmp loop is very unoptimised - byte at a time compares with no loop unrolling. We can do much much better.

Optimise the loop in a few ways:

- Unroll the byte at a time loop

- For large (at least 32 byte) comparisons that are also 8 byte aligned, use an unrolled modulo scheduled loop using 8 byte loads. This is similar to our glibc memcmp.

A simple microbenchmark testing 10000000 iterations of an 8192 byte memcmp was used to measure the performance:

baseline: 29.93 s

modified: 1.70 s

Just over 17x faster.

v2: Incorporated some suggestions from Segher:

- Use andi. instead of rdlicl.

- Convert bdnzt eq, to bdnz. It's just duplicating the earlier compare and was a relic from a previous version.

- Don't use cr5, we have plans to use that CR field for fast local atomics.

---
 arch/powerpc/lib/Makefile    |   3 +-
 arch/powerpc/lib/memcmp_64.S | 233 +++++++++++++++++++++++++++++++++++++++++++
 arch/powerpc/lib/string.S    |   2 +
 3 files changed, 237 insertions(+), 1 deletion(-)
 create mode 100644 arch/powerpc/lib/memcmp_64.S
arch/powerpc/lib/Makefile +2 -1
index 1b01159..5526156 100644
@@ -15,7 +15,8 @@ obj-$(CONFIG_PPC32)	+= div64.o copy_32.o
 
 obj-$(CONFIG_PPC64)	+= copypage_64.o copyuser_64.o \
 			   usercopy_64.o mem_64.o hweight_64.o \
-			   copyuser_power7.o string_64.o copypage_power7.o
+			   copyuser_power7.o string_64.o copypage_power7.o \
+			   memcmp_64.o
 ifeq ($(CONFIG_GENERIC_CSUM),)
 obj-y			+= checksum_$(CONFIG_WORD_SIZE).o
 obj-$(CONFIG_PPC64)	+= checksum_wrappers_64.o
arch/powerpc/lib/memcmp_64.S new file +233 -0

233 lines not on this page · raw message

arch/powerpc/lib/string.S +2 -0
index 1b5a0a0..c80fb49 100644
@@ -93,6 +93,7 @@ _GLOBAL(strlen)
 	subf	r3,r3,r4
 	blr
 
+#ifdef CONFIG_PPC32
 _GLOBAL(memcmp)
 	PPC_LCMPI 0,r5,0
 	beq-	2f
@@ -106,6 +107,7 @@ _GLOBAL(memcmp)
 	blr
 2:	li	r3,0
 	blr
+#endif
 
 _GLOBAL(memchr)
 	PPC_LCMPI 0,r5,0
Signature · 1 line
2.1.0
#1 lore raw

PATCH 2/2 powerpc: Add memcmp testcase

Add a testcase for the new ppc64 memcmp.

---
 .../testing/selftests/powerpc/stringloops/Makefile |  21 +++++
 .../selftests/powerpc/stringloops/asm/ppc_asm.h    |   7 ++
 .../selftests/powerpc/stringloops/memcmp_64.S      |   1 +
 .../selftests/powerpc/stringloops/test_memcmp.c    | 103 +++++++++++++++++++++
 4 files changed, 132 insertions(+)
 create mode 100644 tools/testing/selftests/powerpc/stringloops/Makefile
 create mode 100644 tools/testing/selftests/powerpc/stringloops/asm/ppc_asm.h
 create mode 120000 tools/testing/selftests/powerpc/stringloops/memcmp_64.S
 create mode 100644 tools/testing/selftests/powerpc/stringloops/test_memcmp.c
tools/testing/selftests/powerpc/stringloops/Makefile new file +21 -0
new file mode 100644
index 0000000..159a299
@@ -0,0 +1,21 @@
+# The loops are all 64-bit code
+CFLAGS += -m64
+CFLAGS += -I$(CURDIR)
+CFLAGS += -D SELFTEST
+
+PROGS := test_memcmp
+EXTRA_SOURCES := memcmp_64.S ../harness.c
+
+all: $(PROGS)
+
+$(PROGS): $(EXTRA_SOURCES)
+
+run_tests: all
+	@-for PROG in $(PROGS); do \
+		./$$PROG; \
+	done;
+
+clean:
+	rm -f $(PROGS) *.o
+
+.PHONY: all run_tests clean
tools/testing/selftests/powerpc/stringloops/asm/ppc_asm.h new file +7 -0
new file mode 100644
index 0000000..11bece8
@@ -0,0 +1,7 @@
+#include <ppc-asm.h>
+
+#ifndef r1
+#define r1 sp
+#endif
+
+#define _GLOBAL(A) FUNC_START(test_ ## A)
tools/testing/selftests/powerpc/stringloops/memcmp_64.S new file +1 -0
new file mode 120000
index 0000000..9bc87e4
@@ -0,0 +1 @@
+../../../../../arch/powerpc/lib/memcmp_64.S
\ No newline at end of file
tools/testing/selftests/powerpc/stringloops/test_memcmp.c new file +103 -0

103 lines not on this page · raw message

Signature · 1 line
2.1.0
#2 lore raw
In reply to Anton Blanchard
Quoted 25 lines · On Wednesday 21 January 2015 12:27:38 Anton Blanchard wrote:

I noticed ksm spending quite a lot of time in memcmp on a large KVM box. The current memcmp loop is very unoptimised - byte at a time compares with no loop unrolling. We can do much much better.

Optimise the loop in a few ways:

- Unroll the byte at a time loop

- For large (at least 32 byte) comparisons that are also 8 byte aligned, use an unrolled modulo scheduled loop using 8 byte loads. This is similar to our glibc memcmp.

A simple microbenchmark testing 10000000 iterations of an 8192 byte memcmp was used to measure the performance:

baseline: 29.93 s

modified: 1.70 s

Just over 17x faster.

v2: Incorporated some suggestions from Segher:

- Use andi. instead of rdlicl.

Quoted 7 lines

- Convert bdnzt eq, to bdnz. It's just duplicating the earlier compare and was a relic from a previous version.

- Don't use cr5, we have plans to use that CR field for fast local atomics.

Would it help to also add a way for an architecture to override memcmp_pages() with its own implementation? That way you could skip the unaligned part, hardcode the loop counter and avoid the preempt_disable() in kmap_atomic().

Arnd

#3 lore raw

Hi Arnd,

Quoted 4 lines

Would it help to also add a way for an architecture to override memcmp_pages() with its own implementation? That way you could skip the unaligned part, hardcode the loop counter and avoid the preempt_disable() in kmap_atomic().

Good idea. We could also have a generic implementation that did long comparisons, so everyone would get a decent speed up.

Anton