powerpc: Add 64bit optimised memcmp
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.
--- 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
@@ -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
@@ -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
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
@@ -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
@@ -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
@@ -0,0 +1 @@ +../../../../../arch/powerpc/lib/memcmp_64.S
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
RnJvbTogQW50b24gQmxhbmNoYXJkDQo+IEkgbm90aWNlZCBrc20gc3BlbmRpbmcgcXVpdGUgYSBs b3Qgb2YgdGltZSBpbiBtZW1jbXAgb24gYSBsYXJnZQ0KPiBLVk0gYm94LiBUaGUgY3VycmVudCBt ZW1jbXAgbG9vcCBpcyB2ZXJ5IHVub3B0aW1pc2VkIC0gYnl0ZSBhdCBhDQo+IHRpbWUgY29tcGFy ZXMgd2l0aCBubyBsb29wIHVucm9sbGluZy4gV2UgY2FuIGRv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2Ugd2hlcmUgdGhlIGNvZGUgaXNuJ3QNCmFscmVhZHkgaW4gdGhlIGNh Y2hlIGFuZCB0aGUgY29tcGFyZSBpcyByZWxhdGl2ZWx5IHNob3J0Lg0KVHJ5IHNvbWV0aGluZyBi YXNlZCBvbjoNCglhMSA9ICphKys7DQoJYjEgPSAqYisrOw0KCXdoaWxlIHsNCgkJYTIgPSAqYSsr Ow0KCQliMiA9ICpiKys7DQoJCWlmIChhMSAhPSBhMikNCgkJCWJyZWFrOw0KCQlhMSA9ICphKys7 DQoJCWIxID0gKmIrKzsNCgl9IHdoaWxlIChhMiAhPSBhMSk7DQoNCglEYXZpZA0KDQo=
Hi David,
Quoted 7 lines
The unrolled loop (deleted) looks excessive.
On a modern cpu with multiple execution units you can usually manage to get the loop overhead to execute in parallel to the actual 'work'.
So I suspect that a much simpler 'word at a time' loop will be almost as fast - especially in the case where the code isn't already in the cache and the compare is relatively short.
I'm always keen to keep things as simple as possible, but your loop is over 50% slower. Once the loop hits a steady state you are going to run into front end issues with instruction fetch on POWER8.
Anton
Quoted 14 lines
Try something based on:
a1 = *a++; b1 = *b++; while { a2 = *a++; b2 = *b++; if (a1 != a2) break; a1 = *a++; b1 = *b++; } while (a2 != a1); David
Quoted 7 lines · Anton Blanchard
Hi David,
=20Quoted 5 lines
The unrolled loop (deleted) looks excessive.
On a modern cpu with multiple execution units you can usually manage to get the loop overhead to execute in parallel to the actual 'work'.
So I suspect that a much simpler 'word at a time' loop will be almost a=
s fast - especially in the case where the code isn't
Quoted 3 lines
Quoted 1 line
already in the cache and the compare is relatively short.
=20
I'm always keen to keep things as simple as possible, but your loop is ov=
er 50% slower. Once the loop hits a steady state you are going to run into = front end issues with instruction fetch on POWER8.
Quoted 1 line
=20
Out of curiosity, does preincrement make any difference(or can gcc do that = for you nowadays)?
a1 =3D *a;
b1 =3D *b;
while {
a2 =3D *++a;
b2 =3D *++b;
if (a1 !=3D a2)
break;
a1 =3D *++a;
b1 =3D *++b;
} while (a2 !=3D a1);Jocke
Quoted 16 lines
Anton
=20Quoted 11 lines
Try something based on:
a1 =3D *a++; b1 =3D *b++; while { a2 =3D *a++; b2 =3D *b++; if (a1 !=3D a2) break; a1 =3D *a++; b1 =3D *b++; } while (a2 !=3D a1);Quoted 3 lines
=20
David
=20
Quoted 3 lines
=20
_______________________________________________
Linuxppc-dev mailing list [email protected] https://lists.ozl=
abs.org/listinfo/linuxppc-dev=
From: Joakim Tjernlund=20
Quoted 8 lines · Anton Blanchard
Quoted 7 lines
Hi David,
Quoted 5 lines
The unrolled loop (deleted) looks excessive.
On a modern cpu with multiple execution units you can usually manage to get the loop overhead to execute in parallel to the actual 'work'.
So I suspect that a much simpler 'word at a time' loop will be almost=
as fast - especially in the
Quoted 4 lines
case where the code isn't
Quoted 3 lines
Quoted 1 line
already in the cache and the compare is relatively short.
I'm always keen to keep things as simple as possible, but your loop is =
over 50% slower. Once the
Quoted 1 line
loop hits a steady state you are going to run into front end issues with =
instruction fetch on POWER8.
Interesting, I'm not an expert on ppc scheduling, but on my old x86 Athon 7= 00 (I think
it was that one) a similar loop ran as fast as 'rep movsw'.
Quoted 1 line
Out of curiosity, does preincrement make any difference(or can gcc do tha=
t for you nowadays)?
It will only change register pressure slightly, and might allow any executi= on
delays be filled - but that is very processor dependant. Actually you probably want to do 'a +=3D 2' somewhere to reduce the instruc= tion count.
Similarly the end condition needs to compare one of the pointers.
Elsewhere (not ppc) I've used (++p)[-1] instead of *p++ to move the increme= nt
before the load to get better scheduling.
Quoted 6 lines
a1 =3D *a;
b1 =3D *b;
while {
a2 =3D *++a;
b2 =3D *++b;
if (a1 !=3D a2)That should have been a1 !=3D b1
Quoted 4 lines
break;
a1 =3D *++a;
b1 =3D *++b;
} while (a2 !=3D a1);and a2 !=3D b2 David
Quoted 15 lines · Anton Blanchard
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:
Quoted 8 lines
baseline: 29.93 s
modified: 1.70 s
Just over 17x faster.
Signed-off-by Anton Blanchard <[email protected]>
Why not use glibc implementations instead? All of them (ppc64, power4, and power7) avoids use byte at time compares for unaligned cases inputs; while showing the same performance for aligned one than this new implementation. To give you an example, a 8192 bytes compare with input alignment of 63/18 shows:
__memcmp_power7: 320 cycles __memcmp_power4: 320 cycles __memcmp_ppc64: 340 cycles
this memcmp: 3185 cycles