diff mbox series

configure: move malloc_trim/tcmalloc/jemalloc to meson

Message ID 20200916080608.17689-1-pbonzini@redhat.com
State New
Headers show
Series configure: move malloc_trim/tcmalloc/jemalloc to meson | expand

Commit Message

Paolo Bonzini Sept. 16, 2020, 8:06 a.m. UTC
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.

Reported-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 configure         | 84 +++++------------------------------------------
 meson.build       | 28 +++++++++++++---
 meson_options.txt |  5 +++
 3 files changed, 38 insertions(+), 79 deletions(-)

Comments

Stefan Hajnoczi Sept. 16, 2020, 8:58 a.m. UTC | #1
On Wed, Sep 16, 2020 at 04:06:08AM -0400, Paolo Bonzini wrote:
> 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.

> 

> Reported-by: Stefan Hajnoczi <stefanha@redhat.com>

> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

> ---

>  configure         | 84 +++++------------------------------------------

>  meson.build       | 28 +++++++++++++---

>  meson_options.txt |  5 +++

>  3 files changed, 38 insertions(+), 79 deletions(-)


Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Richard Henderson Sept. 16, 2020, 4:01 p.m. UTC | #2
On 9/16/20 1:06 AM, Paolo Bonzini wrote:
> 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.

> 

> Reported-by: Stefan Hajnoczi <stefanha@redhat.com>

> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

> ---

>  configure         | 84 +++++------------------------------------------

>  meson.build       | 28 +++++++++++++---

>  meson_options.txt |  5 +++

>  3 files changed, 38 insertions(+), 79 deletions(-)


Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~
罗勇刚(Yonggang Luo) Sept. 18, 2020, 12:55 p.m. UTC | #3
On Wed, Sep 16, 2020 at 4:07 PM Paolo Bonzini <pbonzini@redhat.com> wrote:
>

> 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.

>

> Reported-by: Stefan Hajnoczi <stefanha@redhat.com>

> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

> ---

>  configure         | 84 +++++------------------------------------------

>  meson.build       | 28 +++++++++++++---

>  meson_options.txt |  5 +++

>  3 files changed, 38 insertions(+), 79 deletions(-)

>

> diff --git a/configure b/configure

> index ce27eafb0a..13f53ba231 100755

> --- a/configure

> +++ b/configure

> @@ -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

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


> -

> -# 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\

> diff --git a/meson.build b/meson.build

> index bba766b4bc..d3b1e5c34c 100644

> --- a/meson.build

> +++ b/meson.build

> @@ -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')}
> diff --git a/meson_options.txt b/meson_options.txt

> index 543cf70043..894e006799 100644

> --- a/meson_options.txt

> +++ b/meson_options.txt

> @@ -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',

> --

> 2.26.2

>

>



--
         此致
礼
罗勇刚
Yours
    sincerely,
Yonggang Luo
<div dir="ltr"><br><br>On Wed, Sep 16, 2020 at 4:07 PM Paolo Bonzini &lt;<a href="mailto:pbonzini@redhat.com">pbonzini@redhat.com</a>&gt; wrote:<br>&gt;<br>&gt; Because LIBS is not used anymore, tcmalloc/jemalloc does<br>&gt; not work with binaries whose description is in Meson.<br>&gt; The fix is simply to move them to Meson too.<br>&gt;<br>&gt; For consistency with other configure options, specifying<br>&gt; --enable-malloc-trim together with --enable-{tc,je}malloc<br>&gt; becomes a fatal error.<br>&gt;<br>&gt; Reported-by: Stefan Hajnoczi &lt;<a href="mailto:stefanha@redhat.com">stefanha@redhat.com</a>&gt;<br>&gt; Signed-off-by: Paolo Bonzini &lt;<a href="mailto:pbonzini@redhat.com">pbonzini@redhat.com</a>&gt;<br>&gt; ---<br>&gt;  configure         | 84 +++++------------------------------------------<br>&gt;  meson.build       | 28 +++++++++++++---<br>&gt;  meson_options.txt |  5 +++<br>&gt;  3 files changed, 38 insertions(+), 79 deletions(-)<br>&gt;<br>&gt; diff --git a/configure b/configure<br>&gt; index ce27eafb0a..13f53ba231 100755<br>&gt; --- a/configure<br>&gt; +++ b/configure<br>&gt; @@ -540,7 +540,7 @@ skip_meson=no<br>&gt;  gettext=&quot;&quot;<br>&gt;<br>&gt;  bogus_os=&quot;no&quot;<br>&gt; -malloc_trim=&quot;&quot;<br>&gt; +malloc_trim=&quot;auto&quot;<br>&gt;<br>&gt;  deprecated_features=&quot;&quot;<br>&gt;<br>&gt; @@ -1222,9 +1222,9 @@ for opt do<br>&gt;    ;;<br>&gt;    --enable-tcg) tcg=&quot;yes&quot;<br>&gt;    ;;<br>&gt; -  --disable-malloc-trim) malloc_trim=&quot;no&quot;<br>&gt; +  --disable-malloc-trim) malloc_trim=&quot;disabled&quot;<br>&gt;    ;;<br>&gt; -  --enable-malloc-trim) malloc_trim=&quot;yes&quot;<br>&gt; +  --enable-malloc-trim) malloc_trim=&quot;enabled&quot;<br>&gt;    ;;<br>&gt;    --disable-spice) spice=&quot;no&quot;<br>&gt;    ;;<br>&gt; @@ -4428,77 +4428,14 @@ EOF<br>&gt;    fi<br>&gt;  fi<br>&gt;<br>&gt; +malloc=system<br>&gt;  if test &quot;$tcmalloc&quot; = &quot;yes&quot; &amp;&amp; test &quot;$jemalloc&quot; = &quot;yes&quot; ; then<br>&gt;      echo &quot;ERROR: tcmalloc &amp;&amp; jemalloc can&#39;t be used at the same time&quot;<br>&gt;      exit 1<br>&gt; -fi<div>This patch looks good for me, but I still have a small advice than move thse judgement from</div><div>configure to meson, because finally they need to be converted.</div><div>and getting tcmalloc / 

jemalloc to be &quot;enabled&quot; &quot;disabled&quot; for consistence</div><div><br></div><div><br></div><div>&gt; -<br>&gt; -# Even if malloc_trim() is available, these non-libc memory allocators<br>&gt; -# do not support it.<br>&gt; -if test &quot;$tcmalloc&quot; = &quot;yes&quot; || test &quot;$jemalloc&quot; = &quot;yes&quot; ; then<br>&gt; -    if test &quot;$malloc_trim&quot; = &quot;yes&quot; ; then<br>&gt; -        echo &quot;Disabling malloc_trim with non-libc memory allocator&quot;<br>&gt; -    fi<br>&gt; -    malloc_trim=&quot;no&quot;<br>&gt; -fi<br>&gt; -<br>&gt; -#######################################<br>&gt; -# malloc_trim<br>&gt; -<br>&gt; -if test &quot;$malloc_trim&quot; != &quot;no&quot; ; then<br>&gt; -    cat &gt; $TMPC &lt;&lt; EOF<br>&gt; -#include &lt;malloc.h&gt;<br>&gt; -int main(void) { malloc_trim(0); return 0; }<br>&gt; -EOF<br>&gt; -    if compile_prog &quot;&quot; &quot;&quot; ; then<br>&gt; -        malloc_trim=&quot;yes&quot;<br>&gt; -    else<br>&gt; -        malloc_trim=&quot;no&quot;<br>&gt; -    fi<br>&gt; -fi<br>&gt; -<br>&gt; -##########################################<br>&gt; -# tcmalloc probe<br>&gt; -<br>&gt; -if test &quot;$tcmalloc&quot; = &quot;yes&quot; ; then<br>&gt; -  cat &gt; $TMPC &lt;&lt; EOF<br>&gt; -#include &lt;stdlib.h&gt;<br>&gt; -int main(void) {<br>&gt; -    void *tmp = malloc(1);<br>&gt; -    if (tmp != NULL) {<br>&gt; -        return 0;<br>&gt; -    }<br>&gt; -    return 1;<br>&gt; -}<br>&gt; -EOF<br>&gt; -<br>&gt; -  if compile_prog &quot;&quot; &quot;-ltcmalloc&quot; ; then<br>&gt; -    LIBS=&quot;-ltcmalloc $LIBS&quot;<br>&gt; -  else<br>&gt; -    feature_not_found &quot;tcmalloc&quot; &quot;install gperftools devel&quot;<br>&gt; -  fi<br>&gt; -fi<br>&gt; -<br>&gt; -##########################################<br>&gt; -# jemalloc probe<br>&gt; -<br>&gt; -if test &quot;$jemalloc&quot; = &quot;yes&quot; ; then<br>&gt; -  cat &gt; $TMPC &lt;&lt; EOF<br>&gt; -#include &lt;stdlib.h&gt;<br>&gt; -int main(void) {<br>&gt; -    void *tmp = malloc(1);<br>&gt; -    if (tmp != NULL) {<br>&gt; -        return 0;<br>&gt; -    }<br>&gt; -    return 1;<br>&gt; -}<br>&gt; -EOF<br>&gt; -<br>&gt; -  if compile_prog &quot;&quot; &quot;-ljemalloc&quot; ; then<br>&gt; -    LIBS=&quot;-ljemalloc $LIBS&quot;<br>&gt; -  else<br>&gt; -    feature_not_found &quot;jemalloc&quot; &quot;install jemalloc devel&quot;<br>&gt; -  fi<br>&gt; +elif test &quot;$tcmalloc&quot; = &quot;yes&quot; ; then<br>&gt; +    malloc=tcmalloc<br>&gt; +elif test &quot;$jemalloc&quot; = &quot;yes&quot; ; then<br>&gt; +    malloc=jemalloc<br>&gt;  fi<br>&gt;<br>&gt;  ##########################################<br>&gt; @@ -7023,10 +6960,6 @@ if test &quot;$gbm&quot; = &quot;yes&quot; ; then<br>&gt;  fi<br>&gt;<br>&gt;<br>&gt; -if test &quot;$malloc_trim&quot; = &quot;yes&quot; ; then<br>&gt; -  echo &quot;CONFIG_MALLOC_TRIM=y&quot; &gt;&gt; $config_host_mak<br>&gt; -fi<br>&gt; -<br>&gt;  if test &quot;$avx2_opt&quot; = &quot;yes&quot; ; then<br>&gt;    echo &quot;CONFIG_AVX2_OPT=y&quot; &gt;&gt; $config_host_mak<br>&gt;  fi<br>&gt; @@ -8003,6 +7936,7 @@ NINJA=${ninja:-$PWD/ninjatool} $meson setup \<br>&gt;          -Dstrip=$(if test &quot;$strip_opt&quot; = yes; then echo true; else echo false; fi) \<br>&gt;          -Db_pie=$(if test &quot;$pie&quot; = yes; then echo true; else echo false; fi) \<br>&gt;          -Db_coverage=$(if test &quot;$gcov&quot; = yes; then echo true; else echo false; fi) \<br>&gt; +       -Dmalloc=$malloc -Dmalloc_trim=$malloc_trim \<br>&gt;         -Dsdl=$sdl -Dsdl_image=$sdl_image \<br>&gt;         -Dvnc=$vnc -Dvnc_sasl=$vnc_sasl -Dvnc_jpeg=$vnc_jpeg -Dvnc_png=$vnc_png \<br>&gt;         -Dgettext=$gettext -Dxkbcommon=$xkbcommon -Du2f=$u2f\<br>&gt; diff --git a/meson.build b/meson.build<br>&gt; index bba766b4bc..d3b1e5c34c 100644<br>&gt; --- a/meson.build<br>&gt; +++ b/meson.build<br>&gt; @@ -439,6 +439,26 @@ keyutils = dependency(&#39;libkeyutils&#39;, required: false,<br>&gt;<br>&gt;  has_gettid = cc.has_function(&#39;gettid&#39;)<br>&gt;<br>&gt; +# Malloc tests<br>&gt; +<br>&gt; +malloc = []<br>&gt; +if get_option(&#39;malloc&#39;) == &#39;system&#39;<br>&gt; +  has_malloc_trim = \<br>&gt; +    not get_option(&#39;malloc_trim&#39;).disabled() and \<br>&gt; +    cc.compiles(&#39;&#39;&#39;#include &lt;malloc.h&gt;<br>&gt; +                   int main(void) { malloc_trim(0); return 0; }&#39;&#39;&#39;)<br>&gt; +else<br>&gt; +  has_malloc_trim = false<br>&gt; +  malloc = cc.find_library(get_option(&#39;malloc&#39;), required: true)<br>&gt; +endif<br>&gt; +if not has_malloc_trim and get_option(&#39;malloc_trim&#39;).enabled()<br>&gt; +  if get_option(&#39;malloc&#39;) == &#39;system&#39;<br>&gt; +    error(&#39;malloc_trim not available on this platform.&#39;)<br>&gt; +  else<br>&gt; +    error(&#39;malloc_trim not available with non-libc memory allocator&#39;)<br>&gt; +  endif<br>&gt; +endif<br>&gt; +<br>&gt;  # Create config-host.h<br>&gt;<br>&gt;  config_host_data.set(&#39;CONFIG_SDL&#39;, sdl.found())<br>&gt; @@ -450,6 +470,7 @@ config_host_data.set(&#39;CONFIG_VNC_SASL&#39;, sasl.found())<br>&gt;  config_host_data.set(&#39;CONFIG_XKBCOMMON&#39;, xkbcommon.found())<br>&gt;  config_host_data.set(&#39;CONFIG_KEYUTILS&#39;, keyutils.found())<br>&gt;  config_host_data.set(&#39;CONFIG_GETTID&#39;, has_gettid)<br>&gt; +config_host_data.set(&#39;CONFIG_MALLOC_TRIM&#39;, has_malloc_trim)<br>&gt;  config_host_data.set(&#39;QEMU_VERSION&#39;, &#39;&quot;@0@&quot;&#39;.format(meson.project_version()))<br>&gt;  config_host_data.set(&#39;QEMU_VERSION_MAJOR&#39;, meson.project_version().split(&#39;.&#39;)[0])<br>&gt;  config_host_data.set(&#39;QEMU_VERSION_MINOR&#39;, meson.project_version().split(&#39;.&#39;)[1])<br>&gt; @@ -818,7 +839,7 @@ util_ss.add_all(trace_ss)<br>&gt;  util_ss = util_ss.apply(config_all, strict: false)<br>&gt;  libqemuutil = static_library(&#39;qemuutil&#39;,<br>&gt;                               sources: util_ss.sources() + stub_ss.sources() + genh,<br>&gt; -                             dependencies: [util_ss.dependencies(), m, glib, socket])<br>&gt; +                             dependencies: [util_ss.dependencies(), m, glib, socket, malloc])<br>&gt;  qemuutil = declare_dependency(link_with: libqemuutil,<br>&gt;                                sources: genh + version_res)<br>&gt;<br>&gt; @@ -1442,7 +1463,7 @@ summary_info += {&#39;Install blobs&#39;:     config_host.has_key(&#39;INSTALL_BLOBS&#39;)}<br>&gt;  #  summary_info += {&#39;TCG debug enabled&#39;: config_host.has_key(&#39;CONFIG_DEBUG_TCG&#39;)}<br>&gt;  #  summary_info += {&#39;TCG interpreter&#39;:   config_host.has_key(&#39;CONFIG_TCG_INTERPRETER&#39;)}<br>&gt;  #endif<br>&gt; -summary_info += {&#39;malloc trim support&#39;: config_host.has_key(&#39;CONFIG_MALLOC_TRIM&#39;)}<br>&gt; +summary_info += {&#39;malloc trim support&#39;: has_malloc_trim}<br>&gt;  summary_info += {&#39;RDMA support&#39;:      config_host.has_key(&#39;CONFIG_RDMA&#39;)}<br>&gt;  summary_info += {&#39;PVRDMA support&#39;:    config_host.has_key(&#39;CONFIG_PVRDMA&#39;)}<br>&gt;  summary_info += {&#39;fdt support&#39;:       config_host.has_key(&#39;CONFIG_FDT&#39;)}<br>&gt; @@ -1504,8 +1525,7 @@ summary_info += {&#39;lzfse support&#39;:     config_host.has_key(&#39;CONFIG_LZFSE&#39;)}<br>&gt;  summary_info += {&#39;zstd support&#39;:      config_host.has_key(&#39;CONFIG_ZSTD&#39;)}<br>&gt;  summary_info += {&#39;NUMA host support&#39;: config_host.has_key(&#39;CONFIG_NUMA&#39;)}<br>&gt;  summary_info += {&#39;libxml2&#39;:           config_host.has_key(&#39;CONFIG_LIBXML2&#39;)}<br>&gt; -summary_info += {&#39;tcmalloc support&#39;:  config_host.has_key(&#39;CONFIG_TCMALLOC&#39;)}<br>&gt; -summary_info += {&#39;jemalloc support&#39;:  config_host.has_key(&#39;CONFIG_JEMALLOC&#39;)}<br>&gt; +summary_info += {&#39;memory allocator&#39;:  get_option(&#39;malloc&#39;)}<br>&gt;  summary_info += {&#39;avx2 optimization&#39;: config_host.has_key(&#39;CONFIG_AVX2_OPT&#39;)}<br>&gt;  summary_info += {&#39;avx512f optimization&#39;: config_host.has_key(&#39;CONFIG_AVX512F_OPT&#39;)}<br>&gt;  summary_info += {&#39;replication support&#39;: config_host.has_key(&#39;CONFIG_REPLICATION&#39;)}<br>&gt; diff --git a/meson_options.txt b/meson_options.txt<br>&gt; index 543cf70043..894e006799 100644<br>&gt; --- a/meson_options.txt<br>&gt; +++ b/meson_options.txt<br>&gt; @@ -6,6 +6,11 @@ option(&#39;docdir&#39;, type : &#39;string&#39;, value : &#39;doc&#39;,<br>&gt;  option(&#39;gettext&#39;, type : &#39;boolean&#39;, value : true,<br>&gt;         description: &#39;Localization of the GTK+ user interface&#39;)<br>&gt;<br>&gt; +option(&#39;malloc_trim&#39;, type : &#39;feature&#39;, value : &#39;auto&#39;,<br>&gt; +       description: &#39;enable libc malloc_trim() for memory optimization&#39;)<br>&gt; +option(&#39;malloc&#39;, type : &#39;combo&#39;, choices : [&#39;system&#39;, &#39;tcmalloc&#39;, &#39;jemalloc&#39;],<br>&gt; +       value: &#39;system&#39;, description: &#39;choose memory allocator to use&#39;)<br>&gt; +<br>&gt;  option(&#39;sdl&#39;, type : &#39;feature&#39;, value : &#39;auto&#39;,<br>&gt;         description: &#39;SDL user interface&#39;)<br>&gt;  option(&#39;sdl_image&#39;, type : &#39;feature&#39;, value : &#39;auto&#39;,<br>&gt; --<br>&gt; 2.26.2<br>&gt;<br>&gt;<br><br><br>--<br>         此致<br>礼<br>罗勇刚<br>Yours<br>    sincerely,<br>Yonggang Luo</div></div>
Paolo Bonzini Sept. 18, 2020, 1:20 p.m. UTC | #4
On 18/09/20 14:55, 罗勇刚(Yonggang Luo) wrote:
> 

>>  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
diff mbox series

Patch

diff --git a/configure b/configure
index ce27eafb0a..13f53ba231 100755
--- a/configure
+++ b/configure
@@ -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\
diff --git a/meson.build b/meson.build
index bba766b4bc..d3b1e5c34c 100644
--- a/meson.build
+++ b/meson.build
@@ -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')}
diff --git a/meson_options.txt b/meson_options.txt
index 543cf70043..894e006799 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -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',