public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: GNU C Library <libc-alpha@sourceware.org>, DJ Delorie <dj@redhat.com>
Subject: malloc: Trim unused arenas on thread exit
Date: Thu, 09 Nov 2017 10:55:00 -0000	[thread overview]
Message-ID: <fa4afa75-6165-9655-fc59-21f7d5170c07@redhat.com> (raw)

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

I tried the attached patch to trim unused arenas on thread exit.  The 
trimming actually happens (the heap consolidation is visible in the 
malloc_info output from tst-malloc_info), but the arena heaps aren't 
deallocated.

I think trimming unused arenas as much as possible is a good heuristics 
to minimize RSS, so getting this to work might be worthwhile.

Thanks,
Florian

[-- Attachment #2: mtrim.patch --]
[-- Type: text/x-patch, Size: 1273 bytes --]

diff --git a/malloc/arena.c b/malloc/arena.c
index 85b985e193..758226c222 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -953,12 +953,22 @@ arena_thread_freeres (void)
       /* If this was the last attached thread for this arena, put the
 	 arena on the free list.  */
       assert (a->attached_threads > 0);
-      if (--a->attached_threads == 0)
+      bool arena_is_unused = --a->attached_threads == 0;
+      if (arena_is_unused)
 	{
 	  a->next_free = free_list;
 	  free_list = a;
 	}
       __libc_lock_unlock (free_list_lock);
+
+      /* If there are no more users, compact the arena as much as
+	 possible.  */
+      if (arena_is_unused)
+	{
+	  __libc_lock_lock (a->mutex);
+	  mtrim (a, 0);
+	  __libc_lock_unlock (a->mutex);
+	}
     }
 }
 text_set_element (__libc_thread_subfreeres, arena_thread_freeres);
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 1f003d2ef0..a0b11784d2 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1831,7 +1831,7 @@ malloc_init_state (mstate av)
 static void *sysmalloc (INTERNAL_SIZE_T, mstate);
 static int      systrim (size_t, mstate);
 static void     malloc_consolidate (mstate);
-
+static int mtrim (mstate av, size_t pad);
 
 /* -------------- Early definitions for debugging hooks ---------------- */
 

             reply	other threads:[~2017-11-09 10:55 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-09 10:55 Florian Weimer [this message]
2017-11-10 10:52 ` Siddhesh Poyarekar
2017-11-10 13:13   ` Florian Weimer
2017-11-13 21:57 ` DJ Delorie
2017-11-14  7:09   ` Florian Weimer
2017-11-14  7:12     ` Siddhesh Poyarekar
2017-11-14  7:14       ` Florian Weimer
2017-11-14  7:20         ` Siddhesh Poyarekar
2017-11-14 12:13           ` Florian Weimer
2017-11-15 10:45             ` Florian Weimer
2017-11-15 11:19               ` Siddhesh Poyarekar
2017-11-15 13:36                 ` Florian Weimer
2017-11-15 14:00                   ` Siddhesh Poyarekar
2017-11-15 14:10                     ` Florian Weimer
2017-11-15 14:18                       ` Siddhesh Poyarekar
2017-11-15 14:35                         ` Florian Weimer
2017-11-15 14:41                           ` Siddhesh Poyarekar

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=fa4afa75-6165-9655-fc59-21f7d5170c07@redhat.com \
    --to=fweimer@redhat.com \
    --cc=dj@redhat.com \
    --cc=libc-alpha@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).