unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org>
To: libc-alpha@sourceware.org
Cc: Norbert Manthey <nmanthey@conp-solutions.com>,
	Siddhesh Poyarekar <siddhesh@sourceware.org>,
	Guillaume Morin <guillaume@morinfr.org>
Subject: [PATCH 2/3] malloc: Add THP/madvise support for sbrk
Date: Fri, 13 Aug 2021 18:04:28 -0300	[thread overview]
Message-ID: <20210813210429.1147112-3-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20210813210429.1147112-1-adhemerval.zanella@linaro.org>

For the main arena, the sbrk() might the preferable syscall instead of
mmap().  And the granularity used when increasing the program segment
is the default page size.

To increase effectiveness with Transparent Huge Page with madvise, the
large page size is use instead.  This is enabled with the new tunable
'glibc.malloc.thp_pagesize'.

Checked on x86_64-linux-gnu.
---
 malloc/malloc.c | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 52ea84a63d..7cd586c866 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -2028,6 +2028,38 @@ sysmadvise_thp (void *p, INTERNAL_SIZE_T size)
 #endif
 }
 
+static inline long int
+thp_brk_align_up (long int size)
+{
+  INTERNAL_SIZE_T r = size;
+#if HAVE_TUNABLES && defined (MADV_HUGEPAGE)
+  /* Defined in brk.c.  */
+  extern void *__curbrk;
+  if (mp_.thp_pagesize != 0)
+    {
+      uintptr_t top = ALIGN_UP ((uintptr_t)__curbrk + size, mp_.thp_pagesize);
+      r = top - (uintptr_t)__curbrk;
+    }
+  else
+#endif
+    r = ALIGN_UP (size, GLRO(dl_pagesize));
+  return r;
+}
+
+static inline long
+thp_brk_align_down (long int top)
+{
+  long r;
+#if HAVE_TUNABLES && defined (MADV_HUGEPAGE)
+  if (mp_.thp_pagesize != 0)
+    r = ALIGN_DOWN (top, mp_.thp_pagesize);
+  else
+#endif
+    r = ALIGN_DOWN (top, GLRO(dl_pagesize));
+  return r;
+}
+
+
 /* ------------------- Support for multiple arenas -------------------- */
 #include "arena.c"
 
@@ -2610,14 +2642,14 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av)
         size -= old_size;
 
       /*
-         Round to a multiple of page size.
+         Round to a multiple of page size or huge page size.
          If MORECORE is not contiguous, this ensures that we only call it
          with whole-page arguments.  And if MORECORE is contiguous and
          this is not first time through, this preserves page-alignment of
          previous calls. Otherwise, we correct to page-align below.
        */
 
-      size = ALIGN_UP (size, pagesize);
+      size = thp_brk_align_up (size);
 
       /*
          Don't try to call MORECORE if argument is so big as to appear
@@ -2900,10 +2932,8 @@ systrim (size_t pad, mstate av)
   long released;         /* Amount actually released */
   char *current_brk;     /* address returned by pre-check sbrk call */
   char *new_brk;         /* address returned by post-check sbrk call */
-  size_t pagesize;
   long top_area;
 
-  pagesize = GLRO (dl_pagesize);
   top_size = chunksize (av->top);
 
   top_area = top_size - MINSIZE - 1;
@@ -2911,7 +2941,7 @@ systrim (size_t pad, mstate av)
     return 0;
 
   /* Release in pagesize units and round down to the nearest page.  */
-  extra = ALIGN_DOWN(top_area - pad, pagesize);
+  extra = thp_brk_align_down (top_area - pad);
 
   if (extra == 0)
     return 0;
-- 
2.30.2


  parent reply	other threads:[~2021-08-13 21:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-13 21:04 [PATCH 0/3] malloc: improve THP effectiveness Adhemerval Zanella via Libc-alpha
2021-08-13 21:04 ` [PATCH 1/3] malloc: Add madvise support for Transparent Huge Pages Adhemerval Zanella via Libc-alpha
2021-08-13 21:04 ` Adhemerval Zanella via Libc-alpha [this message]
2021-08-13 21:04 ` [PATCH 3/3] malloc: Add arch-specific malloc_verify_thp_pagesize for Linux Adhemerval Zanella via Libc-alpha
2021-08-13 21:37 ` [PATCH 0/3] malloc: improve THP effectiveness Guillaume Morin
2021-08-16 20:55   ` Adhemerval Zanella via Libc-alpha
2021-08-17  4:00     ` Siddhesh Poyarekar via Libc-alpha

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

  List information: https://www.gnu.org/software/libc/involved.html

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

  git send-email \
    --in-reply-to=20210813210429.1147112-3-adhemerval.zanella@linaro.org \
    --to=libc-alpha@sourceware.org \
    --cc=adhemerval.zanella@linaro.org \
    --cc=guillaume@morinfr.org \
    --cc=nmanthey@conp-solutions.com \
    --cc=siddhesh@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).