Skip to contentSkip to conversation

configure: move malloc_trim/tcmalloc/jemalloc to meson

PATCH In review Paolo Bonzini 4 participants 5 messages Sep 16, 2020 – Sep 18, 2020 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 configure: move malloc_trim/tcmalloc/jemalloc to meson

Because LIBS is not used anymore, tcmalloc/jemalloc does not work with binaries whose description is in Meson. The fix is simply to move them to Meson too.

For consistency with other configure options, specifying --enable-malloc-trim together with --enable-{tc,je}malloc becomes a fatal error.

---
 configure         | 84 +++++------------------------------------------
 meson.build       | 28 +++++++++++++---
 meson_options.txt |  5 +++
 3 files changed, 38 insertions(+), 79 deletions(-)
configure +9 -75
index ce27eafb0a..13f53ba231 100755
@@ -540,7 +540,7 @@ skip_meson=no
 gettext=""
 
 bogus_os="no"
-malloc_trim=""
+malloc_trim="auto"
 
 deprecated_features=""
 
@@ -1222,9 +1222,9 @@ for opt do
   ;;
   --enable-tcg) tcg="yes"
   ;;
-  --disable-malloc-trim) malloc_trim="no"
+  --disable-malloc-trim) malloc_trim="disabled"
   ;;
-  --enable-malloc-trim) malloc_trim="yes"
+  --enable-malloc-trim) malloc_trim="enabled"
   ;;
   --disable-spice) spice="no"
   ;;
@@ -4428,77 +4428,14 @@ EOF
   fi
 fi
 
+malloc=system
 if test "$tcmalloc" = "yes" && test "$jemalloc" = "yes" ; then
     echo "ERROR: tcmalloc && jemalloc can't be used at the same time"
     exit 1
-fi
-
-# Even if malloc_trim() is available, these non-libc memory allocators
-# do not support it.
-if test "$tcmalloc" = "yes" || test "$jemalloc" = "yes" ; then
-    if test "$malloc_trim" = "yes" ; then
-        echo "Disabling malloc_trim with non-libc memory allocator"
-    fi
-    malloc_trim="no"
-fi
-
-#######################################
-# malloc_trim
-
-if test "$malloc_trim" != "no" ; then
-    cat > $TMPC << EOF
-#include <malloc.h>
-int main(void) { malloc_trim(0); return 0; }
-EOF
-    if compile_prog "" "" ; then
-        malloc_trim="yes"
-    else
-        malloc_trim="no"
-    fi
-fi
-
-##########################################
-# tcmalloc probe
-
-if test "$tcmalloc" = "yes" ; then
-  cat > $TMPC << EOF
-#include <stdlib.h>
-int main(void) {
-    void *tmp = malloc(1);
-    if (tmp != NULL) {
-        return 0;
-    }
-    return 1;
-}
-EOF
-
-  if compile_prog "" "-ltcmalloc" ; then
-    LIBS="-ltcmalloc $LIBS"
-  else
-    feature_not_found "tcmalloc" "install gperftools devel"
-  fi
-fi
-
-##########################################
-# jemalloc probe
-
-if test "$jemalloc" = "yes" ; then
-  cat > $TMPC << EOF
-#include <stdlib.h>
-int main(void) {
-    void *tmp = malloc(1);
-    if (tmp != NULL) {
-        return 0;
-    }
-    return 1;
-}
-EOF
-
-  if compile_prog "" "-ljemalloc" ; then
-    LIBS="-ljemalloc $LIBS"
-  else
-    feature_not_found "jemalloc" "install jemalloc devel"
-  fi
+elif test "$tcmalloc" = "yes" ; then
+    malloc=tcmalloc
+elif test "$jemalloc" = "yes" ; then
+    malloc=jemalloc
 fi
 
 ##########################################
@@ -7023,10 +6960,6 @@ if test "$gbm" = "yes" ; then
 fi
 
 
-if test "$malloc_trim" = "yes" ; then
-  echo "CONFIG_MALLOC_TRIM=y" >> $config_host_mak
-fi
-
 if test "$avx2_opt" = "yes" ; then
   echo "CONFIG_AVX2_OPT=y" >> $config_host_mak
 fi
@@ -8003,6 +7936,7 @@ NINJA=${ninja:-$PWD/ninjatool} $meson setup \
         -Dstrip=$(if test "$strip_opt" = yes; then echo true; else echo false; fi) \
         -Db_pie=$(if test "$pie" = yes; then echo true; else echo false; fi) \
         -Db_coverage=$(if test "$gcov" = yes; then echo true; else echo false; fi) \
+	-Dmalloc=$malloc -Dmalloc_trim=$malloc_trim \
 	-Dsdl=$sdl -Dsdl_image=$sdl_image \
 	-Dvnc=$vnc -Dvnc_sasl=$vnc_sasl -Dvnc_jpeg=$vnc_jpeg -Dvnc_png=$vnc_png \
 	-Dgettext=$gettext -Dxkbcommon=$xkbcommon -Du2f=$u2f\
meson.build +24 -4
index bba766b4bc..d3b1e5c34c 100644
@@ -439,6 +439,26 @@ keyutils = dependency('libkeyutils', required: false,
 
 has_gettid = cc.has_function('gettid')
 
+# Malloc tests
+
+malloc = []
+if get_option('malloc') == 'system'
+  has_malloc_trim = \
+    not get_option('malloc_trim').disabled() and \
+    cc.compiles('''#include <malloc.h>
+                   int main(void) { malloc_trim(0); return 0; }''')
+else
+  has_malloc_trim = false
+  malloc = cc.find_library(get_option('malloc'), required: true)
+endif
+if not has_malloc_trim and get_option('malloc_trim').enabled()
+  if get_option('malloc') == 'system'
+    error('malloc_trim not available on this platform.')
+  else
+    error('malloc_trim not available with non-libc memory allocator')
+  endif
+endif
+
 # Create config-host.h
 
 config_host_data.set('CONFIG_SDL', sdl.found())
@@ -450,6 +470,7 @@ config_host_data.set('CONFIG_VNC_SASL', sasl.found())
 config_host_data.set('CONFIG_XKBCOMMON', xkbcommon.found())
 config_host_data.set('CONFIG_KEYUTILS', keyutils.found())
 config_host_data.set('CONFIG_GETTID', has_gettid)
+config_host_data.set('CONFIG_MALLOC_TRIM', has_malloc_trim)
 config_host_data.set('QEMU_VERSION', '"@0@"'.format(meson.project_version()))
 config_host_data.set('QEMU_VERSION_MAJOR', meson.project_version().split('.')[0])
 config_host_data.set('QEMU_VERSION_MINOR', meson.project_version().split('.')[1])
@@ -818,7 +839,7 @@ util_ss.add_all(trace_ss)
 util_ss = util_ss.apply(config_all, strict: false)
 libqemuutil = static_library('qemuutil',
                              sources: util_ss.sources() + stub_ss.sources() + genh,
-                             dependencies: [util_ss.dependencies(), m, glib, socket])
+                             dependencies: [util_ss.dependencies(), m, glib, socket, malloc])
 qemuutil = declare_dependency(link_with: libqemuutil,
                               sources: genh + version_res)
 
@@ -1442,7 +1463,7 @@ summary_info += {'Install blobs':     config_host.has_key('INSTALL_BLOBS')}
 #  summary_info += {'TCG debug enabled': config_host.has_key('CONFIG_DEBUG_TCG')}
 #  summary_info += {'TCG interpreter':   config_host.has_key('CONFIG_TCG_INTERPRETER')}
 #endif
-summary_info += {'malloc trim support': config_host.has_key('CONFIG_MALLOC_TRIM')}
+summary_info += {'malloc trim support': has_malloc_trim}
 summary_info += {'RDMA support':      config_host.has_key('CONFIG_RDMA')}
 summary_info += {'PVRDMA support':    config_host.has_key('CONFIG_PVRDMA')}
 summary_info += {'fdt support':       config_host.has_key('CONFIG_FDT')}
@@ -1504,8 +1525,7 @@ summary_info += {'lzfse support':     config_host.has_key('CONFIG_LZFSE')}
 summary_info += {'zstd support':      config_host.has_key('CONFIG_ZSTD')}
 summary_info += {'NUMA host support': config_host.has_key('CONFIG_NUMA')}
 summary_info += {'libxml2':           config_host.has_key('CONFIG_LIBXML2')}
-summary_info += {'tcmalloc support':  config_host.has_key('CONFIG_TCMALLOC')}
-summary_info += {'jemalloc support':  config_host.has_key('CONFIG_JEMALLOC')}
+summary_info += {'memory allocator':  get_option('malloc')}
 summary_info += {'avx2 optimization': config_host.has_key('CONFIG_AVX2_OPT')}
 summary_info += {'avx512f optimization': config_host.has_key('CONFIG_AVX512F_OPT')}
 summary_info += {'replication support': config_host.has_key('CONFIG_REPLICATION')}
meson_options.txt +5 -0
index 543cf70043..894e006799 100644
@@ -6,6 +6,11 @@ option('docdir', type : 'string', value : 'doc',
 option('gettext', type : 'boolean', value : true,
        description: 'Localization of the GTK+ user interface')
 
+option('malloc_trim', type : 'feature', value : 'auto',
+       description: 'enable libc malloc_trim() for memory optimization')
+option('malloc', type : 'combo', choices : ['system', 'tcmalloc', 'jemalloc'],
+       value: 'system', description: 'choose memory allocator to use')
+
 option('sdl', type : 'feature', value : 'auto',
        description: 'SDL user interface')
 option('sdl_image', type : 'feature', value : 'auto',
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Paolo Bonzini

Because LIBS is not used anymore, tcmalloc/jemalloc does not work with binaries whose description is in Meson. The fix is simply to move them to Meson too.

For consistency with other configure options, specifying --enable-malloc-trim together with --enable-{tc,je}malloc becomes a fatal error.

Quoted 7 lines
---
 configure         | 84 +++++------------------------------------------
 meson.build       | 28 +++++++++++++---
 meson_options.txt |  5 +++
 3 files changed, 38 insertions(+), 79 deletions(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

Because LIBS is not used anymore, tcmalloc/jemalloc does not work with binaries whose description is in Meson. The fix is simply to move them to Meson too.

For consistency with other configure options, specifying --enable-malloc-trim together with --enable-{tc,je}malloc becomes a fatal error.

Quoted 7 lines
---
 configure         | 84 +++++------------------------------------------
 meson.build       | 28 +++++++++++++---
 meson_options.txt |  5 +++
 3 files changed, 38 insertions(+), 79 deletions(-)

r~

#3 lore raw
In reply to Paolo Bonzini
Quoted 43 lines · Paolo Bonzini

Because LIBS is not used anymore, tcmalloc/jemalloc does not work with binaries whose description is in Meson. The fix is simply to move them to Meson too.

For consistency with other configure options, specifying --enable-malloc-trim together with --enable-{tc,je}malloc becomes a fatal error.

---
 configure         | 84 +++++------------------------------------------
 meson.build       | 28 +++++++++++++---
 meson_options.txt |  5 +++
 3 files changed, 38 insertions(+), 79 deletions(-)
configure +3 -3
index ce27eafb0a..13f53ba231 100755
@@ -540,7 +540,7 @@ skip_meson=no
 gettext=""
 
 bogus_os="no"
-malloc_trim=""
+malloc_trim="auto"
 
 deprecated_features=""
 
@@ -1222,9 +1222,9 @@ for opt do
   ;;
   --enable-tcg) tcg="yes"
   ;;
-  --disable-malloc-trim) malloc_trim="no"
+  --disable-malloc-trim) malloc_trim="disabled"
   ;;
-  --enable-malloc-trim) malloc_trim="yes"
+  --enable-malloc-trim) malloc_trim="enabled"
   ;;
   --disable-spice) spice="no"
   ;;

configure EOF · line 4434

Quoted 8 lines
configure +1 -1
@@ -4428,77 +4428,14 @@ EOF
   fi
 fi
 
+malloc=system
 if test "$tcmalloc" = "yes" && test "$jemalloc" = "yes" ; then
     echo "ERROR: tcmalloc && jemalloc can't be used at the same time"
     exit 1
-fi

This patch looks good for me, but I still have a small advice than move thse judgement from
configure to meson, because finally they need to be converted. and getting tcmalloc / jemalloc to be "enabled" "disabled" for consistence

Quoted 86 lines
configure +4 -71
-
-# Even if malloc_trim() is available, these non-libc memory allocators
-# do not support it.
-if test "$tcmalloc" = "yes" || test "$jemalloc" = "yes" ; then
-    if test "$malloc_trim" = "yes" ; then
-        echo "Disabling malloc_trim with non-libc memory allocator"
-    fi
-    malloc_trim="no"
-fi
-
-#######################################
-# malloc_trim
-
-if test "$malloc_trim" != "no" ; then
-    cat > $TMPC << EOF
-#include <malloc.h>
-int main(void) { malloc_trim(0); return 0; }
-EOF
-    if compile_prog "" "" ; then
-        malloc_trim="yes"
-    else
-        malloc_trim="no"
-    fi
-fi
-
-##########################################
-# tcmalloc probe
-
-if test "$tcmalloc" = "yes" ; then
-  cat > $TMPC << EOF
-#include <stdlib.h>
-int main(void) {
-    void *tmp = malloc(1);
-    if (tmp != NULL) {
-        return 0;
-    }
-    return 1;
-}
-EOF
-
-  if compile_prog "" "-ltcmalloc" ; then
-    LIBS="-ltcmalloc $LIBS"
-  else
-    feature_not_found "tcmalloc" "install gperftools devel"
-  fi
-fi
-
-##########################################
-# jemalloc probe
-
-if test "$jemalloc" = "yes" ; then
-  cat > $TMPC << EOF
-#include <stdlib.h>
-int main(void) {
-    void *tmp = malloc(1);
-    if (tmp != NULL) {
-        return 0;
-    }
-    return 1;
-}
-EOF
-
-  if compile_prog "" "-ljemalloc" ; then
-    LIBS="-ljemalloc $LIBS"
-  else
-    feature_not_found "jemalloc" "install jemalloc devel"
-  fi
+elif test "$tcmalloc" = "yes" ; then
+    malloc=tcmalloc
+elif test "$jemalloc" = "yes" ; then
+    malloc=jemalloc
 fi
 
 ##########################################
@@ -7023,10 +6960,6 @@ if test "$gbm" = "yes" ; then
 fi
 
 
-if test "$malloc_trim" = "yes" ; then
-  echo "CONFIG_MALLOC_TRIM=y" >> $config_host_mak
-fi
-
 if test "$avx2_opt" = "yes" ; then
   echo "CONFIG_AVX2_OPT=y" >> $config_host_mak
 fi

configure NINJA=${ninja:-$PWD/ninjatool} $meson setup \ · line 7936

Quoted 1 line
configure +0 -0
@@ -8003,6 +7936,7 @@ NINJA=${ninja:-$PWD/ninjatool} $meson setup \
         -Dstrip=$(if test "$strip_opt" = yes; then echo true; else echo

false; fi) \

configure NINJA=${ninja:-$PWD/ninjatool} $meson setup \ · line 7937

Quoted 1 line
configure +0 -0
         -Db_pie=$(if test "$pie" = yes; then echo true; else echo false;

fi) \

configure NINJA=${ninja:-$PWD/ninjatool} $meson setup \ · line 7938

Quoted 1 line
configure +0 -0
         -Db_coverage=$(if test "$gcov" = yes; then echo true; else echo

false; fi) \

configure NINJA=${ninja:-$PWD/ninjatool} $meson setup \

Quoted 3 lines
configure +1 -0
+       -Dmalloc=$malloc -Dmalloc_trim=$malloc_trim \
        -Dsdl=$sdl -Dsdl_image=$sdl_image \
        -Dvnc=$vnc -Dvnc_sasl=$vnc_sasl -Dvnc_jpeg=$vnc_jpeg

-Dvnc_png=$vnc_png \

Quoted 33 lines
configure +0 -0
        -Dgettext=$gettext -Dxkbcommon=$xkbcommon -Du2f=$u2f\
meson.build +20 -0
index bba766b4bc..d3b1e5c34c 100644
@@ -439,6 +439,26 @@ keyutils = dependency('libkeyutils', required: false,
 
 has_gettid = cc.has_function('gettid')
 
+# Malloc tests
+
+malloc = []
+if get_option('malloc') == 'system'
+  has_malloc_trim = \
+    not get_option('malloc_trim').disabled() and \
+    cc.compiles('''#include <malloc.h>
+                   int main(void) { malloc_trim(0); return 0; }''')
+else
+  has_malloc_trim = false
+  malloc = cc.find_library(get_option('malloc'), required: true)
+endif
+if not has_malloc_trim and get_option('malloc_trim').enabled()
+  if get_option('malloc') == 'system'
+    error('malloc_trim not available on this platform.')
+  else
+    error('malloc_trim not available with non-libc memory allocator')
+  endif
+endif
+
 # Create config-host.h
 
 config_host_data.set('CONFIG_SDL', sdl.found())

meson.build config_host_data.set('CONFIG_VNC_SASL', sasl.found()) · line 474

Quoted 5 lines
meson.build +1 -0
@@ -450,6 +470,7 @@ config_host_data.set('CONFIG_VNC_SASL', sasl.found())
 config_host_data.set('CONFIG_XKBCOMMON', xkbcommon.found())
 config_host_data.set('CONFIG_KEYUTILS', keyutils.found())
 config_host_data.set('CONFIG_GETTID', has_gettid)
+config_host_data.set('CONFIG_MALLOC_TRIM', has_malloc_trim)
 config_host_data.set('QEMU_VERSION', '"@0@

"'.format(meson.project_version()))

meson.build config_host_data.set('CONFIG_VNC_SASL', sasl.found()) · line 475

Quoted 1 line
meson.build +0 -0
 config_host_data.set('QEMU_VERSION_MAJOR',

meson.project_version().split('.')[0])

meson.build config_host_data.set('CONFIG_VNC_SASL', sasl.found()) · line 476

Quoted 1 line
meson.build +0 -0
 config_host_data.set('QEMU_VERSION_MINOR',

meson.project_version().split('.')[1])

meson.build util_ss.add_all(trace_ss) · line 841

Quoted 4 lines
meson.build +0 -0
@@ -818,7 +839,7 @@ util_ss.add_all(trace_ss)
 util_ss = util_ss.apply(config_all, strict: false)
 libqemuutil = static_library('qemuutil',
                              sources: util_ss.sources() +

stub_ss.sources() + genh,

meson.build util_ss.add_all(trace_ss) · line 821

Quoted 1 line
meson.build +0 -1
-                             dependencies: [util_ss.dependencies(), m,

glib, socket])

meson.build util_ss.add_all(trace_ss) · line 842

Quoted 1 line
meson.build +1 -0
+                             dependencies: [util_ss.dependencies(), m,

glib, socket, malloc])

meson.build summary_info += {'Install blobs':

Quoted 4 lines
meson.build +0 -0
 qemuutil = declare_dependency(link_with: libqemuutil,
                               sources: genh + version_res)
 
@@ -1442,7 +1463,7 @@ summary_info += {'Install blobs':

config_host.has_key('INSTALL_BLOBS')}

meson.build summary_info += {'Install blobs': · line 1463

Quoted 1 line
meson.build +0 -0
 #  summary_info += {'TCG debug enabled':

config_host.has_key('CONFIG_DEBUG_TCG')}

meson.build summary_info += {'Install blobs': · line 1464

Quoted 1 line
meson.build +0 -0
 #  summary_info += {'TCG interpreter':

config_host.has_key('CONFIG_TCG_INTERPRETER')}

meson.build summary_info += {'Install blobs': · line 1445

Quoted 2 lines
meson.build +0 -1
 #endif
-summary_info += {'malloc trim support':

config_host.has_key('CONFIG_MALLOC_TRIM')}

meson.build summary_info += {'Install blobs': config_host.has_key('INSTALL_BLOBS')}

Quoted 3 lines
meson.build +1 -0
+summary_info += {'malloc trim support': has_malloc_trim}
 summary_info += {'RDMA support':      config_host.has_key('CONFIG_RDMA')}
 summary_info += {'PVRDMA support':

config_host.has_key('CONFIG_PVRDMA')}

meson.build summary_info += {'lzfse support':

Quoted 2 lines
meson.build +0 -0
 summary_info += {'fdt support':       config_host.has_key('CONFIG_FDT')}
@@ -1504,8 +1525,7 @@ summary_info += {'lzfse support':

config_host.has_key('CONFIG_LZFSE')}

meson.build summary_info += {'lzfse support': config_host.has_key('CONFIG_LZFSE')}

Quoted 3 lines
meson.build +0 -0
 summary_info += {'zstd support':      config_host.has_key('CONFIG_ZSTD')}
 summary_info += {'NUMA host support': config_host.has_key('CONFIG_NUMA')}
 summary_info += {'libxml2':

config_host.has_key('CONFIG_LIBXML2')}

meson.build summary_info += {'lzfse support': · line 1507

Quoted 1 line
meson.build +0 -1
-summary_info += {'tcmalloc support':

config_host.has_key('CONFIG_TCMALLOC')}

meson.build summary_info += {'lzfse support': · line 1508

Quoted 1 line
meson.build +0 -1
-summary_info += {'jemalloc support':

config_host.has_key('CONFIG_JEMALLOC')}

meson.build summary_info += {'lzfse support': · line 1529

Quoted 2 lines
meson.build +1 -0
+summary_info += {'memory allocator':  get_option('malloc')}
 summary_info += {'avx2 optimization':

config_host.has_key('CONFIG_AVX2_OPT')}

meson.build summary_info += {'lzfse support': · line 1530

Quoted 1 line
meson.build +0 -0
 summary_info += {'avx512f optimization':

config_host.has_key('CONFIG_AVX512F_OPT')}

meson.build summary_info += {'lzfse support': · line 1531

Quoted 1 line
meson.build +0 -0
 summary_info += {'replication support':

config_host.has_key('CONFIG_REPLICATION')}

meson_options.txt option('docdir', type : 'string', value : 'doc', · line 11

Quoted 11 lines
meson_options.txt +3 -0
index 543cf70043..894e006799 100644
@@ -6,6 +6,11 @@ option('docdir', type : 'string', value : 'doc',
 option('gettext', type : 'boolean', value : true,
        description: 'Localization of the GTK+ user interface')
 
+option('malloc_trim', type : 'feature', value : 'auto',
+       description: 'enable libc malloc_trim() for memory optimization')
+option('malloc', type : 'combo', choices : ['system', 'tcmalloc',

'jemalloc'],

Quoted 9 lines
meson_options.txt +2 -1
+       value: 'system', description: 'choose memory allocator to use')
+
 option('sdl', type : 'feature', value : 'auto',
        description: 'SDL user interface')
 option('sdl_image', type : 'feature', value : 'auto',
--

2.26.2

--
此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo

#4 lore raw
Quoted 10 lines · 罗勇刚(Yonggang Luo)
Quoted 5 lines

 if test "$tcmalloc" = "yes" && test "$jemalloc" = "yes" ; then echo "ERROR: tcmalloc && jemalloc can't be used at the same time" exit 1
-fi

This patch looks good for me, but I still have a small advice than move thse judgement from
configure to meson, because finally they need to be converted. and getting tcmalloc /  jemalloc to be "enabled" "disabled" for consistence

This error cannot apply to meson.build because it uses a single multi-choice option "-Dmalloc=system|tcmalloc|jemalloc".

Paolo