Improve buffer_is_zero
v2 cover Improve buffer_is_zero
Patches 1-4 remove the use of ifunc from the implementation.
Patch 6 adjusts the x86 implementation a bit more to take advantage of ptest (in sse4.1) and unaligned accesses (in avx1).
Patches 3 and 7 are the result of my conversation with Vijaya Kumar with respect to ThunderX.
Patch 8 is the result of seeing some really really horrible code produced for ppc64le (gcc 4.9 and mainline).
This has had limited testing. What I don't know is the best way to benchmark this -- the only way I know to trigger this is via the console, by hand, which doesn't make for reasonable timing.
Changes v1-v2:
* Add patch 1, moving everything to a new file.
* Fix a typo or two, which had the wrong sense of zero test. These had mostly beed fixed in the intermediate patches, but it wouldn't have helped bisection.
r~
Richard Henderson (8):
cutils: Move buffer_is_zero and subroutines to a new file
cutils: Remove SPLAT macro
cutils: Export only buffer_is_zero
cutils: Rearrange buffer_is_zero acceleration
cutils: Add generic prefetch
cutils: Rewrite x86 buffer zero checking
cutils: Rewrite aarch64 buffer zero checking
cutils: Rewrite ppc buffer zero checking
configure | 21 +-- include/qemu/cutils.h | 2 - migration/ram.c | 2 +- migration/rdma.c | 5 +- util/Makefile.objs | 1 + util/bufferiszero.c | 432 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 244 ---------------------------- 7 files changed, 441 insertions(+), 266 deletions(-) create mode 100644 util/bufferiszero.c
Signature · 1 line
2.7.4
PATCH v2 1/8 cutils: Move buffer_is_zero and subroutines to a new file
--- util/Makefile.objs | 1 + util/bufferiszero.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 244 ---------------------------------------------- 3 files changed, 273 insertions(+), 244 deletions(-) create mode 100644 util/bufferiszero.c
util/Makefile.objs +1 -0
@@ -1,4 +1,5 @@ util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o +util-obj-y += bufferiszero.o util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-$(CONFIG_POSIX) += event_notifier-posix.o util-obj-$(CONFIG_POSIX) += mmap-alloc.o
util/bufferiszero.c new file +272 -0
272 lines not on this page · raw message
util/cutils.c +0 -244
250 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 3/8 cutils: Export only buffer_is_zero
In reply to Richard HendersonSince the two users don't make use of the returned offset, beyond ensuring that the entire buffer is zero, consider the can_use_buffer_find_nonzero_offset and buffer_find_nonzero_offset functions internal.
--- include/qemu/cutils.h | 2 -- migration/ram.c | 2 +- migration/rdma.c | 5 +---- util/bufferiszero.c | 8 ++++---- 4 files changed, 6 insertions(+), 11 deletions(-)
include/qemu/cutils.h +0 -2
@@ -168,8 +168,6 @@ int64_t qemu_strtosz_suffix_unit(const char *nptr, char **end,/* used to print char* safely */ #define STR_OR_NULL (str) ((str) ? (str) : "null") -bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len); -size_t buffer_find_nonzero_offset (const void *buf,size_t len);bool buffer_is_zero (const void *buf,size_t len);/*
migration/ram.c +1 -1
@@ -73,7 +73,7 @@ static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE];static inline bool is_zero_range (uint8_t *p,uint64_t size) { -return buffer_find_nonzero_offset (p, size) == size; +return buffer_is_zero (p, size); }/* struct contains XBZRLE cache and a static page
migration/rdma.c +1 -4
@@ -1934,10 +1934,7 @@ retry:* memset() + madvise() the entire chunk without RDMA. */ -if (can_use_buffer_find_nonzero_offset ((void *)(uintptr_t )sge.addr, - length) - &&buffer_find_nonzero_offset ((void *)(uintptr_t )sge.addr, - length) == length) { +if (buffer_is_zero ((void *)(uintptr_t )sge.addr, length)) {RDMACompress comp = { .offset = current_addr, .value =0 ,
util/bufferiszero.c +4 -4
@@ -192,9 +192,9 @@ static bool avx2_support(void)return b & bit_AVX2; } -bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len) \ +static bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len) \ __attribute__ ((ifunc ("can_use_buffer_find_nonzero_offset_ifunc" ))); -size_t buffer_find_nonzero_offset (const void *buf,size_t len) \ +static size_t buffer_find_nonzero_offset (const void *buf,size_t len) \ __attribute__ ((ifunc ("buffer_find_nonzero_offset_ifunc" )));static void *buffer_find_nonzero_offset_ifunc (void )
@@ -215,12 +215,12 @@ static void *can_use_buffer_find_nonzero_offset_ifunc(void) }#pragma GCC pop_options#else -bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len) +static bool can_use_buffer_find_nonzero_offset (const void *buf,size_t len) {return can_use_buffer_find_nonzero_offset_inner (buf, len); } -size_t buffer_find_nonzero_offset (const void *buf,size_t len) +static size_t buffer_find_nonzero_offset (const void *buf,size_t len) {return buffer_find_nonzero_offset_inner (buf, len); }
Signature · 1 line
2.7.4
PATCH v2 4/8 cutils: Rearrange buffer_is_zero acceleration
In reply to Richard HendersonAllow selection of several acceleration functions based on the size and alignment of the buffer. Do not require ifunc support for AVX2 acceleration.
--- configure | 21 +--- util/bufferiszero.c | 352 +++++++++++++++++++++++++--------------------------- 2 files changed, 172 insertions(+), 201 deletions(-)
configure +6 -15
@@ -1788,28 +1788,19 @@ fi ########################################## # avx2 optimization requirement check - -if test "$static" = "no" ; then - cat > $TMPC << EOF +cat > $TMPC << EOF #pragma GCC push_options #pragma GCC target("avx2") #include <cpuid.h> #include <immintrin.h> - static int bar(void *a) { - return _mm256_movemask_epi8(_mm256_cmpeq_epi8(*(__m256i *)a, (__m256i){0})); + __m256i x = *(__m256i *)a; + return _mm256_testz_si256(x, x); } -static void *bar_ifunc(void) {return (void*) bar;} -int foo(void *a) __attribute__((ifunc("bar_ifunc"))); -int main(int argc, char *argv[]) { return foo(argv[0]);} +int main(int argc, char *argv[]) { return bar(argv[0]); } EOF - if compile_object "" ; then - if has readelf; then - if readelf --syms $TMPO 2>/dev/null |grep -q "IFUNC.*foo"; then - avx2_opt="yes" - fi - fi - fi +if compile_object "" ; then + avx2_opt="yes" fi #########################################
util/bufferiszero.c +166 -186
408 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 5/8 cutils: Add generic prefetch
In reply to Richard HendersonThere's no real knowledge of the cacheline size,
just prefetching one loop ahead.
--- util/bufferiszero.c | 5 +++++ 1 file changed, 5 insertions(+)
util/bufferiszero.c +5 -0
@@ -38,6 +38,8 @@ NAME(const void *buf, size_t len) \ do { \ const VECTYPE *p = buf; \ VECTYPE t; \ + __builtin_prefetch(buf + SIZE); \ + barrier(); \ if (SIZE == sizeof(VECTYPE) * 4) { \ t = (p[0] | p[1]) | (p[2] | p[3]); \ } else if (SIZE == sizeof(VECTYPE) * 8) { \
@@ -241,6 +243,9 @@ bool buffer_is_zero(const void *buf, size_t len)return true; } +/* Fetch the beginning of the buffer while we select the accelerator. */ +__builtin_prefetch (buf); +/* Use an optimized zero check if possible. Note that this also includes a check for an unrolled loop over longs, as well as the unsized, unaligned fallback to buffer_zero_base. */
Signature · 1 line
2.7.4
PATCH v2 6/8 cutils: Rewrite x86 buffer zero checking
In reply to Richard HendersonUse unaligned load operations.
Add versions for avx1 and sse4.1.
--- util/bufferiszero.c | 169 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 145 insertions(+), 24 deletions(-)
util/bufferiszero.c +145 -24
@@ -131,21 +131,127 @@ static bool select_accel_fn(const void *buf, size_t len)return select_accel_int (buf, len); } -#elif defined(CONFIG_AVX2_OPT ) +#elif defined(CONFIG_AVX2_OPT ) || defined(__SSE2__)#include <cpuid.h> #include <x86intrin.h> +#ifdef CONFIG_AVX2_OPT #pragma GCC push_options#pragma GCC target("avx2") -#define AVX2_NONZERO (X ) !_mm256_testz_si256((X), (X)) -ACCEL_BUFFER_ZERO(buffer_zero_avx2, 128, __m256i, AVX2_NONZERO) + +static bool __attribute__((noinline)) +buffer_zero_avx2 (const void *buf,size_t len) +{ +const __m256i *p = buf; +const __m256i *end = buf + len; +__m256i t; + +do { + p +=4 ; +/* Note that most AVX insns handle unaligned operands by +default; we only need take care for the initial load. */ + __asm("prefetcht0 (%1)\n\t" +"vmovdqu -0x80(%1),%0\n\t" +"vpor -0x60(%1),%0,%0\n\t" +"vpor -0x40(%1),%0,%0\n\t" +"vpor -0x20(%1),%0,%0" + :"=x" (t) :"r" (p)); +if (unlikely (!_mm256_testz_si256 (t, t))) { +return false; + } + }while (p < end); +return true; +} + +#pragma GCC pop_options +#pragma GCC push_options +#pragma GCC target("avx") + +static bool __attribute__((noinline)) +buffer_zero_avx (const void *buf,size_t len) +{ +const __m128i *p = buf; +const __m128i *end = buf + len; +__m128i t; + +do { + p +=4 ; +/* Note that most AVX insns handle unaligned operands by +default; we only need take care for the initial load. */ + __asm("prefetcht0 (%1)\n\t" +"vmovdqu -0x40(%1),%0\n\t" +"vpor -0x20(%1),%0,%0\n\t" +"vpor -0x20(%1),%0,%0\n\t" +"vpor -0x10(%1),%0,%0" + :"=x" (t) :"r" (p)); +if (unlikely (!_mm_testz_si128 (t, t))) { +return false; + } + }while (p < end); +return true; +} +#pragma GCC pop_options +#pragma GCC push_options +#pragma GCC target("sse4") + +static bool __attribute__((noinline)) +buffer_zero_sse4 (const void *buf,size_t len) +{ +const __m128i *p = buf; +const __m128i *end = buf + len; +__m128i t0, t1, t2, t3; + +do { + p +=4 ; + __asm("prefetcht0 (%4)\n\t" +"movdqu -0x40(%4),%0\n\t" +"movdqu -0x20(%4),%1\n\t" +"movdqu -0x20(%4),%2\n\t" +"movdqu -0x10(%4),%3\n\t" +"por %1,%0\n\t" +"por %3,%2\n\t" +"por %2,%0" + :"=x" (t0),"=x" (t1),"=x" (t2),"=x" (t3) :"r" (p)); +if (unlikely (!_mm_testz_si128 (t0, t0))) { +return false; + } + }while (p < end); +return true; +} +#pragma GCC pop_options#pragma GCC push_options#pragma GCC target("sse2") -#define SSE2_NONZERO (X ) \ - (_mm_movemask_epi8(_mm_cmpeq_epi8((X), _mm_setzero_si128())) != 0xFFFF) -ACCEL_BUFFER_ZERO(buffer_zero_sse2, 64, __m128i, SSE2_NONZERO) +#endif /* CONFIG_AVX2_OPT */ + +static bool __attribute__((noinline)) +buffer_zero_sse2 (const void *buf,size_t len) +{ +const __m128i *p = buf; +const __m128i *end = buf + len; +__m128i zero =_mm_setzero_si128 (); +__m128i t0, t1, t2, t3; + +do { + p +=4 ; + __asm("prefetcht0 (%4)\n\t" +"movdqu -0x40(%4),%0\n\t" +"movdqu -0x20(%4),%1\n\t" +"movdqu -0x20(%4),%2\n\t" +"movdqu -0x10(%4),%3\n\t" +"por %1,%0\n\t" +"por %3,%2\n\t" +"por %2,%0" + :"=x" (t0),"=x" (t1),"=x" (t2),"=x" (t3) :"r" (p)); +if (unlikely (_mm_movemask_epi8 (_mm_cmpeq_epi8 (t0, zero)) !=0xFFFF )) { +return false; + } + }while (p < end); +return true; +} + +#ifdef CONFIG_AVX2_OPT #pragma GCC pop_options#define CACHE_SSE2 1
@@ -186,32 +292,47 @@ static void __attribute__((constructor)) init_cpuid_cache(void) } cpuid_cache = cache; } +#endif /* CONFIG_AVX2_OPT */ static bool select_accel_fn (const void *buf,size_t len) { -uintptr_t ibuf = (uintptr_t )buf; -if (len %128 ==0 && ibuf %32 ==0 && (cpuid_cache &CACHE_AVX2 )) { +#ifdef CONFIG_AVX2_OPT +int cache = cpuid_cache; + +/* Force bits that the compiler tells us must be there. +This allows the compiler to optimize subsequent tests. */ +#ifdef __AVX2__ + cache |=CACHE_AVX2 ; +#endif +#ifdef __AVX__ + cache |=CACHE_AVX1 ; +#endif +#ifdef __SSE4_1__ + cache |=CACHE_SSE4 ; +#endif +#ifdef __SSE2__ + cache |=CACHE_SSE2 ; +#endif + +if (len %128 ==0 && (cache &CACHE_AVX2 )) {return buffer_zero_avx2 (buf, len); } -if (len %64 ==0 && ibuf %16 ==0 && (cpuid_cache &CACHE_SSE2 )) { -return buffer_zero_sse2 (buf, len); +if (len %64 ==0 ) { +if (cache &CACHE_AVX1 ) { +return buffer_zero_avx (buf, len); + } +if (cache &CACHE_SSE4 ) { +return buffer_zero_sse4 (buf, len); + } +if (cache &CACHE_SSE2 ) { +return buffer_zero_sse2 (buf, len); + } } -return select_accel_int (buf, len); -} - -#elif defined __SSE2__ -#include <emmintrin.h> - -#define SSE2_NONZERO (X ) \ - (_mm_movemask_epi8(_mm_cmpeq_epi8((X), _mm_setzero_si128())) != 0xFFFF) -ACCEL_BUFFER_ZERO(buffer_zero_sse2, 64, __m128i, SSE2_NONZERO) - -static bool select_accel_fn (const void *buf,size_t len) -{ -uintptr_t ibuf = (uintptr_t )buf; -if (len %64 ==0 && ibuf %sizeof (__m128i) ==0 ) { +#else +if (len %64 ==0 ) {return buffer_zero_sse2 (buf, len); } +#endif return select_accel_int (buf, len); }
Signature · 1 line
2.7.4
PATCH v2 7/8 cutils: Rewrite aarch64 buffer zero checking
In reply to Richard HendersonProvide 64-byte and 128-byte versions.
Use dczid_el0 as a proxy for the cacheline size.
--- util/bufferiszero.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-)
util/bufferiszero.c +25 -3
@@ -340,13 +340,35 @@ static bool select_accel_fn(const void *buf, size_t len)#include "arm_neon.h" #define DO_NONZERO (X ) (vgetq_lane_u64((X), 0) | vgetq_lane_u64((X), 1)) -ACCEL_BUFFER_ZERO(buffer_zero_neon, 128, uint64x2_t, DO_NONZERO) +ACCEL_BUFFER_ZERO(buffer_zero_neon_64, 64, uint64x2_t, DO_NONZERO) +ACCEL_BUFFER_ZERO(buffer_zero_neon_128, 128, uint64x2_t, DO_NONZERO) + +static uint32_t buffer_zero_line_mask; +static accel_zero_fn buffer_zero_accel; + +static void __attribute__((constructor))init_buffer_zero_accel (void ) +{ +uint64_t t; + +/* Use the DZP block size as a proxy for the cacheline size, +since the later is not available to userspace. This seems +to work in practice for existing implementations. */ + asm("mrs %0, dczid_el0" :"=r" (t)); +if ((t &15 ) *16 >=128 ) { + buffer_zero_line_mask =128 -1 ; + buffer_zero_accel = buffer_zero_neon_128; + }else { + buffer_zero_line_mask =64 -1 ; + buffer_zero_accel = buffer_zero_neon_64; + } +}static bool select_accel_fn (const void *buf,size_t len) {uintptr_t ibuf = (uintptr_t )buf; -if (len %128 ==0 && ibuf %sizeof (uint64x2_t) ==0 ) { -return buffer_zero_neon (buf, len); +if (likely (ibuf %sizeof (uint64_t ) ==0 ) + && (len & buffer_zero_line_mask) ==0 ) { +return buffer_zero_accel (buf, len); }return select_accel_int (buf, len); }
Signature · 1 line
2.7.4
PATCH v2 8/8 cutils: Rewrite ppc buffer zero checking
In reply to Richard HendersonGCC versions through 6 do a poor job with the indexed addressing, and (for ppc64le) issues unnecessary xxswapd insns.
--- util/bufferiszero.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-)
util/bufferiszero.c +38 -2
@@ -119,8 +119,44 @@ static bool select_accel_int(const void *buf, size_t len)#undef pixel#undef bool#define bool _Bool -#define DO_NONZERO (X ) vec_any_ne(X, (__vector unsigned char){ 0 }) -ACCEL_BUFFER_ZERO(buffer_zero_ppc, 128, __vector unsigned char, DO_NONZERO) + +static bool __attribute__((noinline)) +buffer_zero_ppc (const void *buf,size_t len) +{ +typedef unsigned char vec __attribute__((vector_size (16 ))); +const vec *p = buf; +const vec *end = buf + len; +vec t0, t1, t2, t3, zero = (vec ){0 }; + +do { + p +=8 ; +__builtin_prefetch (p); +barrier (); +/* ??? GCC6 does poorly with power64le; extra xxswap. */ + __asm("lvebx %0,%4,%5\n\t" +"lvebx %1,%4,%6\n\t" +"lvebx %2,%4,%7\n\t" +"lvebx %3,%4,%8\n\t" +"vor %0,%0,%1\n\t" +"vor %1,%2,%3\n\t" +"lvebx %2,%4,%9\n\t" +"lvebx %3,%4,%10\n\t" +"vor %0,%0,%1\n\t" +"vor %1,%2,%3\n\t" +"lvebx %2,%4,%11\n\t" +"lvebx %3,%4,%12\n\t" +"vor %0,%0,%1\n\t" +"vor %1,%2,%3\n\t" +"vor %0,%0,%1" + :"=v" (t0),"=v" (t1),"=v" (t2),"=v" (t3) + :"b" (p),"b" (-8 *16 ),"b" (-7 *16 ),"b" (-6 *16 ),"b" (-5 *16 ), +"b" (-4 *16 ),"b" (-3 *16 ),"b" (-2 *16 ),"b" (-1 *16 )); +if (unlikely (vec_any_ne (t0, zero))) { +return false; + } + }while (p < end); +return true; +}static bool select_accel_fn (const void *buf,size_t len) {
Signature · 1 line
2.7.4
Quoted 4 lines · Richard Henderson
Patches 1-4 remove the use of ifunc from the implementation.
Patch 6 adjusts the x86 implementation a bit more to take advantage of ptest (in sse4.1) and unaligned accesses (in avx1).
Do we really care about unaligned access? Or can we guarantee that all our calls to buffer_is_zero are already aligned, and make optimizations along those lines?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 9 lines · Eric Blake
Quoted 4 lines · Richard Henderson
Patches 1-4 remove the use of ifunc from the implementation.
Patch 6 adjusts the x86 implementation a bit more to take advantage of ptest (in sse4.1) and unaligned accesses (in avx1).
Do we really care about unaligned access? Or can we guarantee that all our calls to
buffer_is_zeroare already aligned, and make optimizations along those lines?
The old code asserted alignment of at least sizeof(long), although a survey of call sites doesn't make this obvious. I could imagine that we get alignment consistent with that of malloc, but can't prove it.
However, we're certainly not going to be able to assert arbitrary alignment, such as the 32-byte for AVX2, or the 64-byte for AVX512 (when that comes along).
Thankfully, at least AVX capable cpus are very efficient with unaligned accesses.
r~
Quoted 35 lines · Richard Henderson
Patches 1-4 remove the use of ifunc from the implementation.
Patch 6 adjusts the x86 implementation a bit more to take advantage of ptest (in sse4.1) and unaligned accesses (in avx1).
Patches 3 and 7 are the result of my conversation with Vijaya Kumar with respect to ThunderX.
Patch 8 is the result of seeing some really really horrible code produced for ppc64le (gcc 4.9 and mainline).
This has had limited testing. What I don't know is the best way to benchmark this -- the only way I know to trigger this is via the console, by hand, which doesn't make for reasonable timing.
Changes v1-v2:
* Add patch 1, moving everything to a new file.
* Fix a typo or two, which had the wrong sense of zero test. These had mostly beed fixed in the intermediate patches, but it wouldn't have helped bisection.r~
Richard Henderson (8):
cutils: Movebuffer_is_zeroand subroutines to a new file
cutils: Remove SPLAT macro
cutils: Export onlybuffer_is_zero
cutils: Rearrangebuffer_is_zeroacceleration
cutils: Add generic prefetch
cutils: Rewrite x86 buffer zero checking
cutils: Rewrite aarch64 buffer zero checking
cutils: Rewrite ppc buffer zero checking
Quoted 9 lines
configure | 21 +-- include/qemu/cutils.h | 2 - migration/ram.c | 2 +- migration/rdma.c | 5 +- util/Makefile.objs | 1 + util/bufferiszero.c | 432 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 244 ---------------------------- 7 files changed, 441 insertions(+), 266 deletions(-) create mode 100644 util/bufferiszero.c
Since your v1 series has a report of breaking arm64, I thnk this is a good candidate for adding unit tests eg a tests/test-bufferiszero.c file which exercises & validates the various codepaths.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|