Blame SOURCES/00282-obmalloc-mmap-threshold.patch

25c6e4
Make it more likely for the system allocator to release free()d memory arenas on glibc-based systems. 
25c6e4
Patch by Charles-François Natali.
25c6e4
https://bugs.python.org/issue20494
25c6e4
25c6e4
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
25c6e4
--- a/Objects/obmalloc.c
25c6e4
+++ b/Objects/obmalloc.c
25c6e4
@@ -2,6 +2,13 @@
25c6e4
 
25c6e4
 #ifdef WITH_PYMALLOC
25c6e4
 
25c6e4
+#ifdef HAVE_MMAP
25c6e4
+ #include <sys/mman.h>
25c6e4
+ #ifdef MAP_ANONYMOUS
25c6e4
+  #define ARENAS_USE_MMAP
25c6e4
+ #endif
25c6e4
+#endif
25c6e4
+
25c6e4
 #ifdef WITH_VALGRIND
25c6e4
 #include <valgrind/valgrind.h>
25c6e4
 
25c6e4
@@ -75,7 +82,8 @@ static int running_on_valgrind = -1;
25c6e4
  * Allocation strategy abstract:
25c6e4
  *
25c6e4
  * For small requests, the allocator sub-allocates <Big> blocks of memory.
25c6e4
- * Requests greater than 256 bytes are routed to the system's allocator.
25c6e4
+ * Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the
25c6e4
+ * system's allocator. 
25c6e4
  *
25c6e4
  * Small requests are grouped in size classes spaced 8 bytes apart, due
25c6e4
  * to the required valid alignment of the returned address. Requests of
25c6e4
@@ -107,10 +115,11 @@ static int running_on_valgrind = -1;
25c6e4
  *       57-64                   64                       7
25c6e4
  *       65-72                   72                       8
25c6e4
  *        ...                   ...                     ...
25c6e4
- *      241-248                 248                      30
25c6e4
- *      249-256                 256                      31
25c6e4
+ *      497-504                 504                      62
25c6e4
+ *      505-512                 512                      63 
25c6e4
  *
25c6e4
- *      0, 257 and up: routed to the underlying allocator.
25c6e4
+ *      0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying
25c6e4
+ *      allocator.
25c6e4
  */
25c6e4
 
25c6e4
 /*==========================================================================*/
25c6e4
@@ -143,10 +152,13 @@ static int running_on_valgrind = -1;
25c6e4
  *      1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256
25c6e4
  *      2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
25c6e4
  *
25c6e4
+ * Note: a size threshold of 512 guarantees that newly created dictionaries
25c6e4
+ * will be allocated from preallocated memory pools on 64-bit.
25c6e4
+ *
25c6e4
  * Although not required, for better performance and space efficiency,
25c6e4
  * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
25c6e4
  */
25c6e4
-#define SMALL_REQUEST_THRESHOLD 256
25c6e4
+#define SMALL_REQUEST_THRESHOLD 512 
25c6e4
 #define NB_SMALL_SIZE_CLASSES   (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
25c6e4
 
25c6e4
 /*
25c6e4
@@ -174,15 +186,15 @@ static int running_on_valgrind = -1;
25c6e4
 /*
25c6e4
  * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned
25c6e4
  * on a page boundary. This is a reserved virtual address space for the
25c6e4
- * current process (obtained through a malloc call). In no way this means
25c6e4
- * that the memory arenas will be used entirely. A malloc(<Big>) is usually
25c6e4
- * an address range reservation for <Big> bytes, unless all pages within this
25c6e4
- * space are referenced subsequently. So malloc'ing big blocks and not using
25c6e4
- * them does not mean "wasting memory". It's an addressable range wastage...
25c6e4
+ * current process (obtained through a malloc()/mmap() call). In no way this
25c6e4
+ * means that the memory arenas will be used entirely. A malloc(<Big>) is
25c6e4
+ * usually an address range reservation for <Big> bytes, unless all pages within
25c6e4
+ * this space are referenced subsequently. So malloc'ing big blocks and not
25c6e4
+ * using them does not mean "wasting memory". It's an addressable range
25c6e4
+ * wastage... 
25c6e4
  *
25c6e4
- * Therefore, allocating arenas with malloc is not optimal, because there is
25c6e4
- * some address space wastage, but this is the most portable way to request
25c6e4
- * memory from the system across various platforms.
25c6e4
+ * Arenas are allocated with mmap() on systems supporting anonymous memory
25c6e4
+ * mappings to reduce heap fragmentation.
25c6e4
  */
25c6e4
 #define ARENA_SIZE              (256 << 10)     /* 256KB */
25c6e4
 
25c6e4
@@ -440,6 +452,9 @@ static poolp usedpools[2 * ((NB_SMALL_SI
25c6e4
     , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
25c6e4
 #if NB_SMALL_SIZE_CLASSES > 56
25c6e4
     , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
25c6e4
+#if NB_SMALL_SIZE_CLASSES > 64
25c6e4
+#error "NB_SMALL_SIZE_CLASSES should be less than 64"
25c6e4
+#endif /* NB_SMALL_SIZE_CLASSES > 64 */
25c6e4
 #endif /* NB_SMALL_SIZE_CLASSES > 56 */
25c6e4
 #endif /* NB_SMALL_SIZE_CLASSES > 48 */
25c6e4
 #endif /* NB_SMALL_SIZE_CLASSES > 40 */
25c6e4
@@ -577,7 +592,12 @@ new_arena(void)
25c6e4
     arenaobj = unused_arena_objects;
25c6e4
     unused_arena_objects = arenaobj->nextarena;
25c6e4
     assert(arenaobj->address == 0);
25c6e4
+#ifdef ARENAS_USE_MMAP
25c6e4
+    arenaobj->address = (uptr)mmap(NULL, ARENA_SIZE, PROT_READ|PROT_WRITE,
25c6e4
+                                   MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
25c6e4
+#else
25c6e4
     arenaobj->address = (uptr)malloc(ARENA_SIZE);
25c6e4
+#endif    
25c6e4
     if (arenaobj->address == 0) {
25c6e4
         /* The allocation failed: return NULL after putting the
25c6e4
          * arenaobj back.
25c6e4
@@ -1054,7 +1074,11 @@ PyObject_Free(void *p)
25c6e4
                 unused_arena_objects = ao;
25c6e4
 
25c6e4
                 /* Free the entire arena. */
25c6e4
+#ifdef ARENAS_USE_MMAP
25c6e4
+                munmap((void *)ao->address, ARENA_SIZE);
25c6e4
+#else
25c6e4
                 free((void *)ao->address);
25c6e4
+#endif
25c6e4
                 ao->address = 0;                        /* mark unassociated */
25c6e4
                 --narenas_currently_allocated;
25c6e4
 
25c6e4
diff --git a/configure b/configure
25c6e4
--- a/configure
25c6e4
+++ b/configure
25c6e4
@@ -10164,7 +10164,7 @@ for ac_func in alarm setitimer getitimer
25c6e4
  clock confstr ctermid execv fchmod fchown fork fpathconf ftime ftruncate \
25c6e4
  gai_strerror getgroups getlogin getloadavg getpeername getpgid getpid \
25c6e4
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
25c6e4
- initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime \
25c6e4
+ initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime mmap \
25c6e4
  mremap nice pathconf pause plock poll pthread_init \
25c6e4
  putenv readlink realpath \
25c6e4
  select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
25c6e4
diff --git a/configure.ac b/configure.ac
25c6e4
--- a/configure.ac
25c6e4
+++ b/configure.ac
25c6e4
@@ -2905,7 +2905,7 @@ AC_CHECK_FUNCS(alarm setitimer getitimer
25c6e4
  clock confstr ctermid execv fchmod fchown fork fpathconf ftime ftruncate \
25c6e4
  gai_strerror getgroups getlogin getloadavg getpeername getpgid getpid \
25c6e4
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
25c6e4
- initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime \
25c6e4
+ initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime mmap \
25c6e4
  mremap nice pathconf pause plock poll pthread_init \
25c6e4
  putenv readlink realpath \
25c6e4
  select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
25c6e4
diff --git a/pyconfig.h.in b/pyconfig.h.in
25c6e4
--- a/pyconfig.h.in
25c6e4
+++ b/pyconfig.h.in
25c6e4
@@ -475,6 +475,9 @@
25c6e4
 /* Define to 1 if you have the `mktime' function. */
25c6e4
 #undef HAVE_MKTIME
25c6e4
 
25c6e4
+/* Define to 1 if you have the `mmap' function. */
25c6e4
+#undef HAVE_MMAP
25c6e4
+
25c6e4
 /* Define to 1 if you have the `mremap' function. */
25c6e4
 #undef HAVE_MREMAP
25c6e4