Skip to contentSkip to conversation

add avx2 instruction optimization

PATCH RESEND Superseded Liang Li 2 participants 4 messages Nov 10, 2015

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Liang Li

author
#0 lore raw

v2 cover add avx2 instruction optimization

buffer_find_nonzero_offset() is a hot function during live migration. Now it use SSE2 intructions for optimization. For platform supports AVX2 instructions, use the AVX2 instructions for optimization can help to improve the performance about 30% comparing to SSE2. Zero page check 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, excute the AVX2 instructions, else, excute the original code.

With patch, if build QEMU binary with AVX2 enabled, the binary can run on both platforms support AVX2 or not.

If build QEMU binary with AVX2 diabled, or if compiler can not support AVX2, the binary will not contain the AVX2 instruction, and it can run on both platforms support AVX2 or not.

Liang Li (2):
cutils: add avx2 instruction optimization
configure: add options to config avx2

 configure             | 29 ++++++++++++++++++++++
 include/qemu-common.h | 28 +++++++++++++++------
 util/Makefile.objs    |  2 ++
 util/avx2.c           | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++
 util/cutils.c         | 47 +++++++++++++++++++++++++++++++++--
 5 files changed, 165 insertions(+), 9 deletions(-)
 create mode 100644 util/avx2.c
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH RESEND v2 1/2 cutils: add avx2 instruction optimization

buffer_find_nonzero_offset() is a hot function during live migration. Now it use SSE2 intructions for optimization. For platform supports AVX2 instructions, use the AVX2 instructions for optimization can help to improve the performance about 30% comparing to SSE2. Zero page check 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, excute the AVX2 instructions, else, excute the original code.

---
 include/qemu-common.h | 28 +++++++++++++++------
 util/Makefile.objs    |  2 ++
 util/avx2.c           | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++
 util/cutils.c         | 47 +++++++++++++++++++++++++++++++++--
 4 files changed, 136 insertions(+), 9 deletions(-)
 create mode 100644 util/avx2.c
include/qemu-common.h +21 -7
index 2f74540..9fa7501 100644
@@ -484,15 +484,29 @@ 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);
 
+extern bool
+can_use_buffer_find_nonzero_offset_avx2(const void *buf, size_t len);
+
+extern size_t buffer_find_nonzero_offset_avx2(const void *buf, size_t len);
+
+extern bool
+can_use_buffer_find_nonzero_offset_inner(const void *buf, size_t len);
+
+extern size_t buffer_find_nonzero_offset_inner(const void *buf, size_t len);
+
+__asm__(".type can_use_buffer_find_nonzero_offset, \%gnu_indirect_function");
+__asm__(".type buffer_find_nonzero_offset, \%gnu_indirect_function");
+
+
+void *can_use_buffer_find_nonzero_offset_ifunc(void) \
+                     __asm__("can_use_buffer_find_nonzero_offset");
+
+void *buffer_find_nonzero_offset_ifunc(void) \
+                     __asm__("buffer_find_nonzero_offset");
 /*
  * helper to parse debug environment variables
  */
util/Makefile.objs +2 -0
index d7cc399..6aacad7 100644
@@ -1,4 +1,5 @@
 util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o
+util-obj-y += avx2.o
 util-obj-$(CONFIG_POSIX) += compatfd.o
 util-obj-$(CONFIG_POSIX) += event_notifier-posix.o
 util-obj-$(CONFIG_POSIX) += mmap-alloc.o
@@ -29,3 +30,4 @@ util-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
 util-obj-y += qemu-coroutine-sleep.o
 util-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
 util-obj-y += buffer.o
+avx2.o-cflags      := $(AVX2_CFLAGS)
util/avx2.c new file +68 -0

68 lines not on this page · raw message

util/cutils.c +45 -2

70 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#2 lore raw

PATCH RESEND v2 2/2 configure: add options to config avx2

In reply to Liang Li

Add the '--enable-avx2' & '--disable-avx2' option so as to config the AVX2 instruction optimization.

By default, avx2 optimization is enabled, if '--disable-avx2' is not set, configure will detect if the compiler can support AVX2 option, if yes, AVX2 optimization is eabled, else disabled.

---
 configure | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
configure +29 -0
index 42e57c0..4d81be2 100755
@@ -310,6 +310,7 @@ smartcard=""
 libusb=""
 usb_redir=""
 opengl=""
+avx2="yes"
 zlib="yes"
 lzo=""
 snappy=""
@@ -1057,6 +1058,10 @@ for opt do
   ;;
   --enable-usb-redir) usb_redir="yes"
   ;;
+  --disable-avx2) avx2="no"
+  ;;
+  --enable-avx2) avx2="yes"
+  ;;
   --disable-zlib-test) zlib="no"
   ;;
   --disable-lzo) lzo="no"
@@ -1373,6 +1378,7 @@ disabled with --disable-FEATURE, default is enabled if available:
   smartcard       smartcard support (libcacard)
   libusb          libusb (for usb passthrough)
   usb-redir       usb network redirection support
+  avx2            support of avx2 instruction
   lzo             support of lzo compression library
   snappy          support of snappy compression library
   bzip2           support of bzip2 compression library
@@ -1809,6 +1815,24 @@ EOF
   fi
 fi
 
+########################################
+# avx2 check
+
+if test "$avx2" != "no" ; then
+    cat > $TMPC << EOF
+int main(void) { return 0; }
+EOF
+    if compile_prog "" "-mavx2" ; then
+        avx2="yes"
+    else
+        avx2="no"
+    fi
+fi
+
+if test "$avx2" = "yes" ; then
+    avx2_cflags=" -mavx2"
+fi
+
 ##########################################
 # zlib check
 
@@ -4782,6 +4806,7 @@ echo "libssh2 support   $libssh2"
 echo "TPM passthrough   $tpm_passthrough"
 echo "QOM debugging     $qom_cast_debug"
 echo "vhdx              $vhdx"
+echo "avx2 support      $avx2"
 echo "lzo support       $lzo"
 echo "snappy support    $snappy"
 echo "bzip2 support     $bzip2"
@@ -5166,6 +5191,10 @@ if test "$opengl" = "yes" ; then
   echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
 fi
 
+if test "$avx2" = "yes" ; then
+  echo "AVX2_CFLAGS=$avx2_cflags" >> $config_host_mak
+fi
+
 if test "$lzo" = "yes" ; then
   echo "CONFIG_LZO=y" >> $config_host_mak
 fi
Signature · 1 line
1.9.1
#3 lore raw

Re: [Qemu-devel] [v2 RESEND 2/2] configure: add options to config avx2

Quoted 48 lines · Liang Li

Add the '--enable-avx2' & '--disable-avx2' option so as to config the AVX2 instruction optimization.

By default, avx2 optimization is enabled, if '--disable-avx2' is not set, configure will detect if the compiler can support AVX2 option, if yes, AVX2 optimization is eabled, else disabled.

---
 configure | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
configure +7 -0
index 42e57c0..4d81be2 100755
@@ -310,6 +310,7 @@ smartcard=""
 libusb=""
 usb_redir=""
 opengl=""
+avx2="yes"
 zlib="yes"
 lzo=""
 snappy=""
@@ -1057,6 +1058,10 @@ for opt do
   ;;
   --enable-usb-redir) usb_redir="yes"
   ;;
+  --disable-avx2) avx2="no"
+  ;;
+  --enable-avx2) avx2="yes"
+  ;;
   --disable-zlib-test) zlib="no"
   ;;
   --disable-lzo) lzo="no"
@@ -1373,6 +1378,7 @@ disabled with --disable-FEATURE, default is enabled if available:
   smartcard       smartcard support (libcacard)
   libusb          libusb (for usb passthrough)
   usb-redir       usb network redirection support
+  avx2            support of avx2 instruction
   lzo             support of lzo compression library
   snappy          support of snappy compression library
   bzip2           support of bzip2 compression library
@@ -1809,6 +1815,24 @@ EOF
   fi
 fi
 
+########################################

configure EOF · line 1828

Quoted 10 lines
configure +10 -0
+# avx2 check
+
+if test "$avx2" != "no" ; then
+    cat > $TMPC << EOF
+int main(void) { return 0; }
+EOF
+    if compile_prog "" "-mavx2" ; then
+        avx2="yes"
+    else
+        avx2="no"

the else bit shouldn't be:

          if test "$avx2" = "yes"; then
              feature_not_found "avx2" "Your compiler don't support avx2"
          fi
          avx=2="no"

??

Quoted 29 lines
configure +12 -0
+    fi
+fi
+
+if test "$avx2" = "yes" ; then
+    avx2_cflags=" -mavx2"
+fi
+
 ##########################################
 # zlib check
 
@@ -4782,6 +4806,7 @@ echo "libssh2 support   $libssh2"
 echo "TPM passthrough   $tpm_passthrough"
 echo "QOM debugging     $qom_cast_debug"
 echo "vhdx              $vhdx"
+echo "avx2 support      $avx2"
 echo "lzo support       $lzo"
 echo "snappy support    $snappy"
 echo "bzip2 support     $bzip2"
@@ -5166,6 +5191,10 @@ if test "$opengl" = "yes" ; then
   echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
 fi
 
+if test "$avx2" = "yes" ; then
+  echo "AVX2_CFLAGS=$avx2_cflags" >> $config_host_mak
+fi
+
 if test "$lzo" = "yes" ; then
   echo "CONFIG_LZO=y" >> $config_host_mak
 fi