Skip to contentSkip to conversation

add avx2 instruction optimization

PATCH Superseded Liang Li 3 participants 7 messages Jan 27, 2016 – Feb 22, 2016

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Liang Li

author
#0 lore raw

v5 cover add avx2 instruction optimization

buffer_find_nonzero_offset() is a hot function during live migration. Now it use SSE2 instructions for optimization. For platform supports AVX2 instructions, use the AVX2 instructions for optimization can help to improve the performance of zero page checking about 30% comparing to SSE2.
Live migration can be faster with this optimization, the test result shows that for an 8GB RAM idle guest, this patch can help to shorten the total live migration time about 6%.

This patch use the ifunc mechanism to select the proper function when running, for platform supports AVX2, execute the AVX2 instructions, else, execute the original instructions.

With this patch, the QEMU binary can run on both platforms support AVX2 or not.

Compiler which doesn't support the AVX2 and ifunc attribute can also build the source code successfully.

v5 -> v4 changes:
* Enhance the ifunc attribute detection (Paolo's suggestion)

v3 -> v4 changes:
* Use the GCC #pragma to make things simple (Paolo's suggestion)
* Put avx2 related code in cutils.c (Richard's suggestion)
* Change the configure, detect ifunc and avx2 attributes together

v2 -> v3 changes:
* Detect the ifunc attribute support (Paolo's suggestion)
* Use the ifunc attribute instead of the inline asm (Richard's suggestion)
* Change the configure (Juan's suggestion)

Liang Li (2):
configure: detect ifunc and avx2 attribute
cutils: add avx2 instruction optimization

 configure             |  21 +++++++++
 include/qemu-common.h |   8 +---
 util/cutils.c         | 118 ++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 136 insertions(+), 11 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v5 1/2 configure: detect ifunc and avx2 attribute

Detect if the compiler can support the ifun and avx2, if so, set CONFIG_AVX2_OPT which will be used to turn on the avx2 instruction optimization.

---
 configure | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
configure +21 -0
index 3506e44..a50dcf5 100755
@@ -311,6 +311,7 @@ smartcard=""
 libusb=""
 usb_redir=""
 opengl=""
+avx2_opt="no"
 zlib="yes"
 lzo=""
 snappy=""
@@ -1832,6 +1833,21 @@ EOF
 fi
 
 ##########################################
+# avx2 optimization requirement check
+
+cat > $TMPC << EOF
+static void bar(void) {}
+static void *bar_ifunc(void) {return (void*) bar;}
+static void foo(void) __attribute__((ifunc("bar_ifunc")));
+int main(void) { foo(); return 0; }
+EOF
+if compile_prog "-mavx2" "" ; then
+    if readelf --syms $TMPE |grep "IFUNC.*foo" >/dev/null 2>&1; then
+        avx2_opt="yes"
+    fi
+fi
+
+#########################################
 # zlib check
 
 if test "$zlib" != "no" ; then
@@ -4922,6 +4938,7 @@ echo "bzip2 support     $bzip2"
 echo "NUMA host support $numa"
 echo "tcmalloc support  $tcmalloc"
 echo "jemalloc support  $jemalloc"
+echo "avx2 optimization $avx2_opt"
 
 if test "$sdl_too_old" = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -5306,6 +5323,10 @@ if test "$opengl" = "yes" ; then
   echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
 fi
 
+if test "$avx2_opt" = "yes" ; then
+  echo "CONFIG_AVX2_OPT=y" >> $config_host_mak
+fi
+
 if test "$lzo" = "yes" ; then
   echo "CONFIG_LZO=y" >> $config_host_mak
 fi
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH v5 2/2 cutils: add avx2 instruction optimization

In reply to Liang Li

buffer_find_nonzero_offset() is a hot function during live migration. Now it use SSE2 instructions for optimization. For platform supports AVX2 instructions, use AVX2 instructions for optimization can help to improve the performance of buffer_find_nonzero_offset() about 30% comparing to SSE2.

Live migration can be faster with this optimization, the test result shows that for an 8GiB RAM idle guest just boots, this patch can help to shorten the total live migration time about 6%.

This patch use the ifunc mechanism to select the proper function when running, for platform supports AVX2, execute the AVX2 instructions, else, execute the original instructions.

---
 include/qemu-common.h |   8 +---
 util/cutils.c         | 118 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 115 insertions(+), 11 deletions(-)
include/qemu-common.h +1 -7
index 22b010c..f4c8c24 100644
@@ -483,13 +483,7 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
 #endif
 
 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
-static inline bool
-can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
-{
-    return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
-                   * sizeof(VECTYPE)) == 0
-            && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
-}
+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);
 
 /*
util/cutils.c +114 -4

147 lines not on this page · raw message

Signature · 1 line
1.9.1
Reviewed-by on the v5 series
#3 lore raw
In reply to Liang Li
Quoted 32 lines · Liang Li

buffer_find_nonzero_offset() is a hot function during live migration. Now it use SSE2 instructions for optimization. For platform supports AVX2 instructions, use the AVX2 instructions for optimization can help to improve the performance of zero page checking about 30% comparing to SSE2.
Live migration can be faster with this optimization, the test result shows that for an 8GB RAM idle guest, this patch can help to shorten the total live migration time about 6%.

This patch use the ifunc mechanism to select the proper function when running, for platform supports AVX2, execute the AVX2 instructions, else, execute the original instructions.

With this patch, the QEMU binary can run on both platforms support AVX2 or not.

Compiler which doesn't support the AVX2 and ifunc attribute can also build the source code successfully.

v5 -> v4 changes:
* Enhance the ifunc attribute detection (Paolo's suggestion)

v3 -> v4 changes:
* Use the GCC #pragma to make things simple (Paolo's suggestion)
* Put avx2 related code in cutils.c (Richard's suggestion)
* Change the configure, detect ifunc and avx2 attributes together

v2 -> v3 changes:
* Detect the ifunc attribute support (Paolo's suggestion)
* Use the ifunc attribute instead of the inline asm (Richard's suggestion)
* Change the configure (Juan's suggestion)

Quoted 8 lines

Liang Li (2):
configure: detect ifunc and avx2 attribute
cutils: add avx2 instruction optimization

 configure             |  21 +++++++++
 include/qemu-common.h |   8 +---
 util/cutils.c         | 118 ++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 136 insertions(+), 11 deletions(-)
#4 lore raw
Quoted 37 lines · Liang Li
Quoted 36 lines

buffer_find_nonzero_offset() is a hot function during live migration. Now it use SSE2 instructions for optimization. For platform supports AVX2 instructions, use the AVX2 instructions for optimization can help to improve the performance of zero page checking about 30% comparing to SSE2.
Live migration can be faster with this optimization, the test result shows that for an 8GB RAM idle guest, this patch can help to shorten the total live migration time about 6%.

This patch use the ifunc mechanism to select the proper function when running, for platform supports AVX2, execute the AVX2 instructions, else, execute the original instructions.

With this patch, the QEMU binary can run on both platforms support AVX2 or not.

Compiler which doesn't support the AVX2 and ifunc attribute can also build the source code successfully.

v5 -> v4 changes:
* Enhance the ifunc attribute detection (Paolo's suggestion)

v3 -> v4 changes:
* Use the GCC #pragma to make things simple (Paolo's suggestion)
* Put avx2 related code in cutils.c (Richard's suggestion)
* Change the configure, detect ifunc and avx2 attributes together

v2 -> v3 changes:
* Detect the ifunc attribute support (Paolo's suggestion)
* Use the ifunc attribute instead of the inline asm (Richard's suggestion)
* Change the configure (Juan's suggestion)

Liang Li (2):
configure: detect ifunc and avx2 attribute
cutils: add avx2 instruction optimization

Quoted 7 lines
Quoted 3 lines
 configure             |  21 +++++++++
 include/qemu-common.h |   8 +---
 util/cutils.c         | 118

++++++++++++++++++++++++++++++++++++++++++++++++--

Quoted 1 line
 3 files changed, 136 insertions(+), 11 deletions(-)

This patch set is pending here for a long time, who can help to make it merged?

Liang

#5 lore raw
Quoted 40 lines · Liang Z
Quoted 40 lines · Liang Li
Quoted 39 lines

buffer_find_nonzero_offset() is a hot function during live migration. Now it use SSE2 instructions for optimization. For platform supports AVX2 instructions, use the AVX2 instructions for optimization can help to improve the performance of zero page checking about 30% comparing to SSE2.
Live migration can be faster with this optimization, the test result shows that for an 8GB RAM idle guest, this patch can help to shorten the total live migration time about 6%.

This patch use the ifunc mechanism to select the proper function when running, for platform supports AVX2, execute the AVX2 instructions, else, execute the original instructions.

With this patch, the QEMU binary can run on both platforms support AVX2 or not.

Compiler which doesn't support the AVX2 and ifunc attribute can also build the source code successfully.

v5 -> v4 changes:
* Enhance the ifunc attribute detection (Paolo's suggestion)

v3 -> v4 changes:
* Use the GCC #pragma to make things simple (Paolo's suggestion)
* Put avx2 related code in cutils.c (Richard's suggestion)
* Change the configure, detect ifunc and avx2 attributes together

v2 -> v3 changes:
* Detect the ifunc attribute support (Paolo's suggestion)
* Use the ifunc attribute instead of the inline asm (Richard's suggestion)
* Change the configure (Juan's suggestion)

Liang Li (2):
configure: detect ifunc and avx2 attribute
cutils: add avx2 instruction optimization

 configure             |  21 +++++++++
 include/qemu-common.h |   8 +---
 util/cutils.c         | 118
Quoted 6 lines
Quoted 4 lines

++++++++++++++++++++++++++++++++++++++++++++++++--

Quoted 1 line
 3 files changed, 136 insertions(+), 11 deletions(-)

This patch set is pending here for a long time, who can help to make it merged?

Not sure; I could take it from the migration tree if no one objects.

Amit

#6 lore raw
Quoted 3 lines

Not sure; I could take it from the migration tree if no one objects.

Amit

Thanks, Amit. If rework is needed, just let me know.

Liang