public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Adhemerval Zanella <azanella@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] malloc: Improve MAP_HUGETLB with glibc.malloc.hugetlb=2
Date: Wed, 29 Nov 2023 12:31:13 +0000 (GMT)	[thread overview]
Message-ID: <20231129123113.51E0D3858005@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=bc6d79f4ae99206e7ec7d6a8c5abf26cdefc8bff

commit bc6d79f4ae99206e7ec7d6a8c5abf26cdefc8bff
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Thu Nov 23 14:29:15 2023 -0300

    malloc: Improve MAP_HUGETLB with glibc.malloc.hugetlb=2
    
    Even for explicit large page support, allocation might use mmap without
    the hugepage bit set if the requested size is smaller than
    mmap_threshold.  For this case where mmap is issued, MAP_HUGETLB is set
    iff the allocation size is larger than the used large page.
    
    To force such allocations to use large pages, also tune the mmap_threhold
    (if it is not explicit set by a tunable).  This forces allocation to
    follow the sbrk path, which will fall back to mmap (which will try large
    pages before galling back to default mmap).
    
    Checked on x86_64-linux-gnu.
    Reviewed-by: DJ Delorie <dj@redhat.com>
    Tested-by: Zhangfei Gao <zhangfei.gao@linaro.org>

Diff:
---
 malloc/arena.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/malloc/arena.c b/malloc/arena.c
index a1a75e5a2b..c73f68890d 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -312,10 +312,17 @@ ptmalloc_init (void)
 # endif
   TUNABLE_GET (mxfast, size_t, TUNABLE_CALLBACK (set_mxfast));
   TUNABLE_GET (hugetlb, size_t, TUNABLE_CALLBACK (set_hugetlb));
+
   if (mp_.hp_pagesize > 0)
-    /* Force mmap for main arena instead of sbrk, so hugepages are explicitly
-       used.  */
-    __always_fail_morecore = true;
+    {
+      /* Force mmap for main arena instead of sbrk, so MAP_HUGETLB is always
+         tried.  Also tune the mmap threshold, so allocation smaller than the
+	 large page will also try to use large pages by falling back
+	 to sysmalloc_mmap_fallback on sysmalloc.  */
+      if (!TUNABLE_IS_INITIALIZED (mmap_threshold))
+	do_set_mmap_threshold (mp_.hp_pagesize);
+      __always_fail_morecore = true;
+    }
 }
 
 /* Managing heaps and arenas (for concurrent threads) */

                 reply	other threads:[~2023-11-29 12:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231129123113.51E0D3858005@sourceware.org \
    --to=azanella@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).