qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] rcu: reduce more than 7MB heap memory by malloc_trim()
@ 2017-11-23  6:41 Yang Zhong
  2017-11-23 11:19 ` Stefan Hajnoczi
  2017-12-07 15:33 ` Marc-André Lureau
  0 siblings, 2 replies; 5+ messages in thread
From: Yang Zhong @ 2017-11-23  6:41 UTC (permalink / raw)
  To: qemu-devel
  Cc: stefanha, berrange, pbonzini, yang.zhong, stone.xulei,
	arei.gonglei, wangxinxin.wang, weidong.huang, zhang.zhanghailiang,
	liujunjie23

Since there are some issues in memory alloc/free machenism
in glibc for little chunk memory, if Qemu frequently
alloc/free little chunk memory, the glibc doesn't alloc
little chunk memory from free list of glibc and still
allocate from OS, which make the heap size bigger and bigger.

This patch introduce malloc_trim(), which will free heap memory.

Below are test results from smaps file.
(1)without patch
55f0783e1000-55f07992a000 rw-p 00000000 00:00 0  [heap]
Size:              21796 kB
Rss:               14260 kB
Pss:               14260 kB

(2)with patch
55cc5fadf000-55cc61008000 rw-p 00000000 00:00 0  [heap]
Size:              21668 kB
Rss:                6940 kB
Pss:                6940 kB

Signed-off-by: Yang Zhong <yang.zhong@intel.com>
---
 configure  | 4 ++++
 util/rcu.c | 6 ++++++
 2 files changed, 10 insertions(+)

diff --git a/configure b/configure
index 0e856bb..5b463d4 100755
--- a/configure
+++ b/configure
@@ -6012,6 +6012,10 @@ if test "$opengl" = "yes" ; then
   fi
 fi
 
+if test "$tcmalloc" = "yes" || test "$jemalloc" = "yes" ; then
+  echo "CONFIG_NONGLIBMALLOC=y" >> $config_host_mak
+fi
+
 if test "$avx2_opt" = "yes" ; then
   echo "CONFIG_AVX2_OPT=y" >> $config_host_mak
 fi
diff --git a/util/rcu.c b/util/rcu.c
index ca5a63e..f3e96a8 100644
--- a/util/rcu.c
+++ b/util/rcu.c
@@ -32,6 +32,9 @@
 #include "qemu/atomic.h"
 #include "qemu/thread.h"
 #include "qemu/main-loop.h"
+#if defined(CONFIG_LINUX)
+#include <malloc.h>
+#endif
 
 /*
  * Global grace period counter.  Bit 0 is always one in rcu_gp_ctr.
@@ -272,6 +275,9 @@ static void *call_rcu_thread(void *opaque)
             node->func(node);
         }
         qemu_mutex_unlock_iothread();
+#if defined(CONFIG_LINUX) && !defined(CONFIG_NONGLIBMALLOC)
+        malloc_trim(4 * 1024 * 1024);
+#endif
     }
     abort();
 }
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2] rcu: reduce more than 7MB heap memory by malloc_trim()
  2017-11-23  6:41 [Qemu-devel] [PATCH v2] rcu: reduce more than 7MB heap memory by malloc_trim() Yang Zhong
@ 2017-11-23 11:19 ` Stefan Hajnoczi
  2017-11-24  6:29   ` Zhong Yang
  2017-12-07 15:33 ` Marc-André Lureau
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Hajnoczi @ 2017-11-23 11:19 UTC (permalink / raw)
  To: Yang Zhong
  Cc: qemu-devel, berrange, pbonzini, stone.xulei, arei.gonglei,
	wangxinxin.wang, weidong.huang, zhang.zhanghailiang, liujunjie23

[-- Attachment #1: Type: text/plain, Size: 2336 bytes --]

On Thu, Nov 23, 2017 at 02:41:16PM +0800, Yang Zhong wrote:
> Since there are some issues in memory alloc/free machenism
> in glibc for little chunk memory, if Qemu frequently
> alloc/free little chunk memory, the glibc doesn't alloc
> little chunk memory from free list of glibc and still
> allocate from OS, which make the heap size bigger and bigger.
> 
> This patch introduce malloc_trim(), which will free heap memory.
> 
> Below are test results from smaps file.
> (1)without patch
> 55f0783e1000-55f07992a000 rw-p 00000000 00:00 0  [heap]
> Size:              21796 kB
> Rss:               14260 kB
> Pss:               14260 kB
> 
> (2)with patch
> 55cc5fadf000-55cc61008000 rw-p 00000000 00:00 0  [heap]
> Size:              21668 kB
> Rss:                6940 kB
> Pss:                6940 kB
> 
> Signed-off-by: Yang Zhong <yang.zhong@intel.com>
> ---
>  configure  | 4 ++++
>  util/rcu.c | 6 ++++++
>  2 files changed, 10 insertions(+)
> 
> diff --git a/configure b/configure
> index 0e856bb..5b463d4 100755
> --- a/configure
> +++ b/configure
> @@ -6012,6 +6012,10 @@ if test "$opengl" = "yes" ; then
>    fi
>  fi
>  
> +if test "$tcmalloc" = "yes" || test "$jemalloc" = "yes" ; then
> +  echo "CONFIG_NONGLIBMALLOC=y" >> $config_host_mak

malloc(3) is provided by glibc, not glib, so the name
CONFIG_NONGLIBMALLOC is confusing.

I suggest calling it CONFIG_MALLOC_TRIM instead:

  # 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

  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

  ...

  if test "$malloc_trim" = "yes" ; then
      echo "CONFIG_MALLOC_TRIM=y" >> $config_host_mak
  fi

Then the code in rcu.c just has to #ifdef CONFIG_MALLOC_TRIM and there's
no need for Linux-specific checks.  If other operating systems support
malloc_trim() then QEMU will use it by default.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 455 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2] rcu: reduce more than 7MB heap memory by malloc_trim()
  2017-11-23 11:19 ` Stefan Hajnoczi
@ 2017-11-24  6:29   ` Zhong Yang
  0 siblings, 0 replies; 5+ messages in thread
From: Zhong Yang @ 2017-11-24  6:29 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: qemu-devel, berrange, pbonzini, stone.xulei, arei.gonglei,
	wangxinxin.wang, weidong.huang, zhang.zhanghailiang, liujunjie23,
	yang.zhong

On Thu, Nov 23, 2017 at 11:19:43AM +0000, Stefan Hajnoczi wrote:
> On Thu, Nov 23, 2017 at 02:41:16PM +0800, Yang Zhong wrote:
> > Since there are some issues in memory alloc/free machenism
> > in glibc for little chunk memory, if Qemu frequently
> > alloc/free little chunk memory, the glibc doesn't alloc
> > little chunk memory from free list of glibc and still
> > allocate from OS, which make the heap size bigger and bigger.
> > 
> > This patch introduce malloc_trim(), which will free heap memory.
> > 
> > Below are test results from smaps file.
> > (1)without patch
> > 55f0783e1000-55f07992a000 rw-p 00000000 00:00 0  [heap]
> > Size:              21796 kB
> > Rss:               14260 kB
> > Pss:               14260 kB
> > 
> > (2)with patch
> > 55cc5fadf000-55cc61008000 rw-p 00000000 00:00 0  [heap]
> > Size:              21668 kB
> > Rss:                6940 kB
> > Pss:                6940 kB
> > 
> > Signed-off-by: Yang Zhong <yang.zhong@intel.com>
> > ---
> >  configure  | 4 ++++
> >  util/rcu.c | 6 ++++++
> >  2 files changed, 10 insertions(+)
> > 
> > diff --git a/configure b/configure
> > index 0e856bb..5b463d4 100755
> > --- a/configure
> > +++ b/configure
> > @@ -6012,6 +6012,10 @@ if test "$opengl" = "yes" ; then
> >    fi
> >  fi
> >  
> > +if test "$tcmalloc" = "yes" || test "$jemalloc" = "yes" ; then
> > +  echo "CONFIG_NONGLIBMALLOC=y" >> $config_host_mak
> 
> malloc(3) is provided by glibc, not glib, so the name
> CONFIG_NONGLIBMALLOC is confusing.
> 
> I suggest calling it CONFIG_MALLOC_TRIM instead:
> 
>   # 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
> 
>   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
> 
>   ...
> 
>   if test "$malloc_trim" = "yes" ; then
>       echo "CONFIG_MALLOC_TRIM=y" >> $config_host_mak
>   fi
> 
> Then the code in rcu.c just has to #ifdef CONFIG_MALLOC_TRIM and there's
> no need for Linux-specific checks.  If other operating systems support
> malloc_trim() then QEMU will use it by default.

  Hello Stefan,

  Thanks for your detailed infomation!

  I did test with this new patch, which are okay, thanks again!

  I will send V3 patch soon, please help review again! thanks!

  Regards,

  Yang

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2] rcu: reduce more than 7MB heap memory by malloc_trim()
  2017-11-23  6:41 [Qemu-devel] [PATCH v2] rcu: reduce more than 7MB heap memory by malloc_trim() Yang Zhong
  2017-11-23 11:19 ` Stefan Hajnoczi
@ 2017-12-07 15:33 ` Marc-André Lureau
  2017-12-08  5:14   ` Yang Zhong
  1 sibling, 1 reply; 5+ messages in thread
From: Marc-André Lureau @ 2017-12-07 15:33 UTC (permalink / raw)
  To: Yang Zhong
  Cc: QEMU, zhanghailiang, liujunjie23, wangxinxin.wang, stone.xulei,
	Gonglei, Stefan Hajnoczi, Paolo Bonzini, weidong.huang

Hi

On Thu, Nov 23, 2017 at 7:41 AM, Yang Zhong <yang.zhong@intel.com> wrote:
> Since there are some issues in memory alloc/free machenism
> in glibc for little chunk memory, if Qemu frequently
> alloc/free little chunk memory, the glibc doesn't alloc
> little chunk memory from free list of glibc and still
> allocate from OS, which make the heap size bigger and bigger.
>
> This patch introduce malloc_trim(), which will free heap memory.
>
> Below are test results from smaps file.
> (1)without patch
> 55f0783e1000-55f07992a000 rw-p 00000000 00:00 0  [heap]
> Size:              21796 kB
> Rss:               14260 kB
> Pss:               14260 kB
>
> (2)with patch
> 55cc5fadf000-55cc61008000 rw-p 00000000 00:00 0  [heap]
> Size:              21668 kB
> Rss:                6940 kB
> Pss:                6940 kB

Have you opened a bug to glibc malloc (or discussed that issue with
the glibc malloc developpers) ?

Or is there a justification that qemu should have its own trim
heuristic on top of malloc?

>
> Signed-off-by: Yang Zhong <yang.zhong@intel.com>
> ---
>  configure  | 4 ++++
>  util/rcu.c | 6 ++++++
>  2 files changed, 10 insertions(+)
>
> diff --git a/configure b/configure
> index 0e856bb..5b463d4 100755
> --- a/configure
> +++ b/configure
> @@ -6012,6 +6012,10 @@ if test "$opengl" = "yes" ; then
>    fi
>  fi
>
> +if test "$tcmalloc" = "yes" || test "$jemalloc" = "yes" ; then
> +  echo "CONFIG_NONGLIBMALLOC=y" >> $config_host_mak
> +fi
> +
>  if test "$avx2_opt" = "yes" ; then
>    echo "CONFIG_AVX2_OPT=y" >> $config_host_mak
>  fi
> diff --git a/util/rcu.c b/util/rcu.c
> index ca5a63e..f3e96a8 100644
> --- a/util/rcu.c
> +++ b/util/rcu.c
> @@ -32,6 +32,9 @@
>  #include "qemu/atomic.h"
>  #include "qemu/thread.h"
>  #include "qemu/main-loop.h"
> +#if defined(CONFIG_LINUX)
> +#include <malloc.h>
> +#endif
>
>  /*
>   * Global grace period counter.  Bit 0 is always one in rcu_gp_ctr.
> @@ -272,6 +275,9 @@ static void *call_rcu_thread(void *opaque)
>              node->func(node);
>          }
>          qemu_mutex_unlock_iothread();
> +#if defined(CONFIG_LINUX) && !defined(CONFIG_NONGLIBMALLOC)
> +        malloc_trim(4 * 1024 * 1024);
> +#endif
>      }
>      abort();
>  }
> --
> 1.9.1
>
>



-- 
Marc-André Lureau

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2] rcu: reduce more than 7MB heap memory by malloc_trim()
  2017-12-07 15:33 ` Marc-André Lureau
@ 2017-12-08  5:14   ` Yang Zhong
  0 siblings, 0 replies; 5+ messages in thread
From: Yang Zhong @ 2017-12-08  5:14 UTC (permalink / raw)
  To: Marc-André Lureau
  Cc: qemu-devel, stefanha, pbonzini, berrange, weidong.huang,
	arei.gonglei, liujunjie23, wangxinxin.wang, stone.xulei,
	zhaoshenglong, yang.zhong

On Thu, Dec 07, 2017 at 04:33:10PM +0100, Marc-André Lureau wrote:
> Hi
> 
> On Thu, Nov 23, 2017 at 7:41 AM, Yang Zhong <yang.zhong@intel.com> wrote:
> > Since there are some issues in memory alloc/free machenism
> > in glibc for little chunk memory, if Qemu frequently
> > alloc/free little chunk memory, the glibc doesn't alloc
> > little chunk memory from free list of glibc and still
> > allocate from OS, which make the heap size bigger and bigger.
> >
> > This patch introduce malloc_trim(), which will free heap memory.
> >
> > Below are test results from smaps file.
> > (1)without patch
> > 55f0783e1000-55f07992a000 rw-p 00000000 00:00 0  [heap]
> > Size:              21796 kB
> > Rss:               14260 kB
> > Pss:               14260 kB
> >
> > (2)with patch
> > 55cc5fadf000-55cc61008000 rw-p 00000000 00:00 0  [heap]
> > Size:              21668 kB
> > Rss:                6940 kB
> > Pss:                6940 kB
> 
> Have you opened a bug to glibc malloc (or discussed that issue with
> the glibc malloc developpers) ?
> 
> Or is there a justification that qemu should have its own trim
> heuristic on top of malloc?

  Hello Marc-Andr,
 
  Thanks for your comments!
  I did not open a bug for glibc community, maybe this issue is not new
  issue for them because there are lots of complains about malloc/free 
  issues in the internet. If need, i can file this bug to them.

  Qemu(v2.3) introduce the rcu mechanism for VM performance, but the side 
  effect is heap memory never shrink like below case
  http://lists.nongnu.org/archive/html/qemu-devel/2017-11/msg02748.html 
  
  Glibc let free memory in their free list for next malloc, but the fact 
  is glibc still allocate memory from OS in Qemu. There are lots of memory 
  hole in the heap, escepially the batch malloc and free with rcu mechanism,
  which made heap memory bigger and bigger, and never shrink.

  from the test by mallinfo() and malloc_trim(), i found malloc_trim() not
  only trim heap top free memory, but also can free hole memory in the heap.
  
  This is V2 patch thread and we are talking in V3 patch thread now.
  http://lists.nongnu.org/archive/html/qemu-devel/2017-11/msg04519.html

  Regards,

  Yang
 

> Marc-André Lureau

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-12-08  5:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-23  6:41 [Qemu-devel] [PATCH v2] rcu: reduce more than 7MB heap memory by malloc_trim() Yang Zhong
2017-11-23 11:19 ` Stefan Hajnoczi
2017-11-24  6:29   ` Zhong Yang
2017-12-07 15:33 ` Marc-André Lureau
2017-12-08  5:14   ` Yang Zhong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).