[v2] malloc: Reduce maximum arenas

Message ID PAWPR08MB8982921A10F2BF38CB943D7D83122@PAWPR08MB8982.eurprd08.prod.outlook.com (mailing list archive)
State Superseded
Headers
Series [v2] malloc: Reduce maximum arenas |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Wilco Dijkstra June 2, 2026, 3:38 p.m. UTC
  v2: rebased

The default maximum arenas is 8 times the number of cores in a 64-bit system.
Since modern CPUs have many cores and big servers have 256 cores, this results
in excessive number of arenas, which wastes memory.  Limit the number of arenas
to max (8, ncores) which is less extreme.  In the future the limit should be
lowered further for large systems.

Passes regress, OK for commit?

---
  

Patch

diff --git a/malloc/arena.c b/malloc/arena.c
index 023cb3ba067d0de097a687b73c81f5d860393413..1ed419a30569f59e169c5c3138e38a2bdb9e5de2 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -799,16 +799,11 @@  arena_get2 (size_t size, mstate avoid_arena)
         {
           if (mp_.arena_max != 0)
             narenas_limit = mp_.arena_max;
-          else if (narenas > mp_.arena_test)
+          else if (narenas >= mp_.arena_test)
             {
-              int n = __get_nprocs ();
-
-              if (n >= 1)
-                narenas_limit = NARENAS_FROM_NCORES (n);
-              else
-                /* We have no information about the system.  Assume two
-                   cores.  */
-                narenas_limit = NARENAS_FROM_NCORES (2);
+	      narenas_limit = __get_nprocs ();
+	      if (narenas_limit < mp_.arena_test)
+		narenas_limit = mp_.arena_test;
             }
         }
     repeat:;
diff --git a/malloc/malloc.c b/malloc/malloc.c
index dfd9ea81d9165a90f4e37a418f4c827994890c89..19ef892030b357a7f9636786f3ca77b2dfc3fe34 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1650,8 +1650,7 @@  static struct malloc_par mp_ =
   .n_mmaps_max = DEFAULT_MMAP_MAX,
   .mmap_threshold = DEFAULT_MMAP_THRESHOLD,
   .trim_threshold = DEFAULT_TRIM_THRESHOLD,
-#define NARENAS_FROM_NCORES(n) ((n) * (sizeof (long) == 4 ? 2 : 8))
-  .arena_test = NARENAS_FROM_NCORES (1),
+  .arena_test = 8,
   .thp_mode = thp_mode_not_supported
 #if USE_TCACHE
   ,
diff --git a/manual/memory.texi b/manual/memory.texi
index f8b6be6ee781cafda1774598585b50524d6cdca0..818d7837833b377f439409074f6563a20e4553f9 100644
--- a/manual/memory.texi
+++ b/manual/memory.texi
@@ -1378,8 +1378,7 @@  This parameter specifies the number of arenas that can be created before the
 test on the limit to the number of arenas is conducted. The value is ignored if
 @code{M_ARENA_MAX} is set.
 
-The default value of this parameter is 2 on 32-bit systems and 8 on 64-bit
-systems.
+The default value of this parameter is 8.
 
 This parameter can also be set for the process at startup by setting the
 environment variable @env{MALLOC_ARENA_TEST} to the desired value.
@@ -1389,10 +1388,9 @@  This parameter sets the number of arenas to use regardless of the number of
 cores in the system.
 
 The default value of this tunable is @code{0}, meaning that the limit on the
-number of arenas is determined by the number of CPU cores online. For 32-bit
-systems the limit is twice the number of cores online and on 64-bit systems, it
-is eight times the number of cores online.  Note that the default value is not
-derived from the default value of M_ARENA_TEST and is computed independently.
+number of arenas is determined by the number of CPU cores online. The limit is
+the number of cores online.  Note that the default value is not derived from
+the default value of M_ARENA_TEST and is computed independently.
 
 This parameter can also be set for the process at startup by setting the
 environment variable @env{MALLOC_ARENA_MAX} to the desired value.