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