00db10
Description: Make trimming logic consistent.
00db10
Author: Carlos O'Donell
00db10
Origin: git://sourceware.org/git/glibc.git
00db10
Bug-RHEL: N/A
00db10
Bug-Fedora: N/A
00db10
Bug-Upstream: #17195
00db10
Upstream status: committed
00db10
00db10
commit e4bc326dbbf7328775fe7dd39de1178821363e0a
00db10
Author: Carlos O'Donell <carlos@systemhalted.org>
00db10
Date:   Wed Oct 7 22:21:36 2015 -0400
00db10
00db10
    malloc: Consistently apply trim_threshold to all heaps (Bug 17195)
00db10
    
00db10
    In the per-thread arenas we apply trim_threshold-based checks
00db10
    to the extra space between the pad and the top_area. This isn't
00db10
    quite accurate and instead we should be harmonizing with the way
00db10
    in which trim_treshold is applied everywhere else like sysrtim
00db10
    and _int_free. The trimming check should be based on the size of
00db10
    the top chunk and only the size of the top chunk. The following
00db10
    patch harmonizes the trimming and make it consistent for the main
00db10
    arena and thread arenas.
00db10
    
00db10
    In the old code a large padding request might have meant that
00db10
    trimming was not triggered. Now trimming is considered first based
00db10
    on the chunk, then the pad is subtracted, and the remainder trimmed.
00db10
    This is how all the other trimmings operate. I didn't measure the
00db10
    performance difference of this change because it corrects what I
00db10
    consider to be a behavioural anomaly. We'll need some profile driven
00db10
    optimization to make this code better, and even there Ondrej and
00db10
    others have better ideas on how to speedup malloc.
00db10
    
00db10
    Tested on x86_64 with no regressions. Already reviewed by Siddhesh
00db10
    Poyarekar and Mel Gorman here and discussed here:
00db10
    https://sourceware.org/ml/libc-alpha/2015-05/msg00002.html
00db10
00db10
Index: glibc-2.17-c758a686/malloc/arena.c
00db10
===================================================================
00db10
--- glibc-2.17-c758a686.orig/malloc/arena.c
00db10
+++ glibc-2.17-c758a686/malloc/arena.c
00db10
@@ -697,14 +697,20 @@ heap_trim(heap_info *heap, size_t pad)
00db10
   }
00db10
 
00db10
   /* Uses similar logic for per-thread arenas as the main arena with systrim
00db10
-     by preserving the top pad and at least a page.  */
00db10
+     and _int_free by preserving the top pad and rounding down to the nearest
00db10
+     page.  */
00db10
   top_size = chunksize(top_chunk);
00db10
+  if ((unsigned long)(top_size) <
00db10
+      (unsigned long)(mp_.trim_threshold))
00db10
+    return 0;
00db10
+
00db10
   top_area = top_size - MINSIZE - 1;
00db10
   if (top_area <= pad)
00db10
     return 0;
00db10
 
00db10
+  /* Release in pagesize units and round down to the nearest page.  */
00db10
   extra = ALIGN_DOWN(top_area - pad, pagesz);
00db10
-  if ((unsigned long) extra < mp_.trim_threshold)
00db10
+  if (extra == 0)
00db10
     return 0;
00db10
   /* Try to shrink. */
00db10
   if(shrink_heap(heap, extra) != 0)