From nobody Mon Apr 29 00:49:19 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513776185483417.3193289602849; Wed, 20 Dec 2017 05:23:05 -0800 (PST) Received: from localhost ([::1]:51302 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eReKn-0006lM-Bx for importer@patchew.org; Wed, 20 Dec 2017 08:22:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53266) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eReG4-00036Z-Mg for qemu-devel@nongnu.org; Wed, 20 Dec 2017 08:18:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eReG0-0003X6-Fh for qemu-devel@nongnu.org; Wed, 20 Dec 2017 08:18:04 -0500 Received: from mga02.intel.com ([134.134.136.20]:20228) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eReG0-0003Tk-5Y for qemu-devel@nongnu.org; Wed, 20 Dec 2017 08:18:00 -0500 Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Dec 2017 05:17:57 -0800 Received: from yangzhon-virtual.bj.intel.com ([10.238.145.49]) by fmsmga005.fm.intel.com with ESMTP; 20 Dec 2017 05:17:54 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,432,1508828400"; d="scan'208";a="188396562" From: Yang Zhong To: pbonzini@redhat.com, stefanha@redhat.com Date: Wed, 20 Dec 2017 21:16:46 +0800 Message-Id: <1513775806-19779-1-git-send-email-yang.zhong@intel.com> X-Mailer: git-send-email 1.9.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.20 Subject: [Qemu-devel] [PATCH v4] rcu: reduce more than 7MB heap memory by malloc_trim() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhong@intel.com, zhang.zhanghailiang@huawei.com, liujunjie23@huawei.com, wangxinxin.wang@huawei.com, stone.xulei@huawei.com, qemu-devel@nongnu.org, arei.gonglei@huawei.com, zhaoshenglong@huawei.com, weidong.huang@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 when there is no rcu call during rcu thread loop. malloc_trim() can be enabled/disabled by --enable-malloc-trim/ --disable-malloc-trim in the Qemu configure command. The default malloc_trim() is enabled for libc. 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 Reviewed-by: Stefan Hajnoczi --- configure | 35 +++++++++++++++++++++++++++++++++++ util/rcu.c | 6 ++++++ 2 files changed, 41 insertions(+) diff --git a/configure b/configure index 9c8aa5a..afdb1ef 100755 --- a/configure +++ b/configure @@ -426,6 +426,7 @@ vxhs=3D"" supported_cpu=3D"no" supported_os=3D"no" bogus_os=3D"no" +malloc_trim=3D"" =20 # parse CC options first for opt do @@ -1047,6 +1048,10 @@ for opt do ;; --enable-tcg) tcg=3D"yes" ;; + --disable-malloc-trim) malloc_trim=3D"no" + ;; + --enable-malloc-trim) malloc_trim=3D"yes" + ;; --disable-spice) spice=3D"no" ;; --enable-spice) spice=3D"yes" @@ -1466,6 +1471,7 @@ Advanced options (experts only): Default:trace- --disable-slirp disable SLIRP userspace network connectivity --enable-tcg-interpreter enable TCG with bytecode interpreter (TCI) + --enable-malloc-trim enable libc malloc_trim() for memory optimizati= on --oss-lib path to OSS library --cpu=3DCPU Build for host CPU [$cpu] --with-coroutine=3DBACKEND coroutine backend. Supported options: @@ -3860,6 +3866,30 @@ if test "$tcmalloc" =3D "yes" && test "$jemalloc" = =3D "yes" ; then exit 1 fi =20 +# Even if malloc_trim() is available, these non-libc memory allocators +# do not support it. +if test "$tcmalloc" =3D "yes" || test "$jemalloc" =3D "yes" ; then + if test "$malloc_trim" =3D "yes" ; then + echo "Disabling malloc_trim with non-libc memory allocator" + fi + malloc_trim=3D"no" +fi + +####################################### +# malloc_trim + +if test "$malloc_trim" !=3D "no" ; then + cat > $TMPC << EOF +#include +int main(void) { malloc_trim(0); return 0; } +EOF + if compile_prog "" "" ; then + malloc_trim=3D"yes" + else + malloc_trim=3D"no" + fi +fi + ########################################## # tcmalloc probe =20 @@ -5505,6 +5535,7 @@ if test "$tcg" =3D "yes" ; then echo "TCG debug enabled $debug_tcg" echo "TCG interpreter $tcg_interpreter" fi +echo "malloc trim support $malloc_trim" echo "RDMA support $rdma" echo "fdt support $fdt" echo "preadv support $preadv" @@ -6015,6 +6046,10 @@ if test "$opengl" =3D "yes" ; then fi fi =20 +if test "$malloc_trim" =3D "yes" ; then + echo "CONFIG_MALLOC_TRIM=3Dy" >> $config_host_mak +fi + if test "$avx2_opt" =3D "yes" ; then echo "CONFIG_AVX2_OPT=3Dy" >> $config_host_mak fi diff --git a/util/rcu.c b/util/rcu.c index ca5a63e..f4d09c8 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_MALLOC_TRIM) +#include +#endif =20 /* * Global grace period counter. Bit 0 is always one in rcu_gp_ctr. @@ -246,6 +249,9 @@ static void *call_rcu_thread(void *opaque) qemu_event_reset(&rcu_call_ready_event); n =3D atomic_read(&rcu_call_count); if (n =3D=3D 0) { +#if defined(CONFIG_MALLOC_TRIM) + malloc_trim(4 * 1024 * 1024); +#endif qemu_event_wait(&rcu_call_ready_event); } } --=20 1.9.1