Browse Source

libpthread/nptl: make default stack size configurable

Threads currently have 2-4 MiB stacks by default (depending on the
platform). This is fine on MMU platforms, where this stack space is not
actually allocated until it is used, but tends to waste a large amount
of memory on no-MMU platforms.

This patch adds a PTHREADS_STACK_DEFAULT_SIZE Kconfig option that allows
the user to override the default stack size at build time. This allows
the user to select a reasonable default stack size for the software that
runs on their system, and avoids the need to patch every package to add
calls to pthread_attr_setstacksize().

An alternative to this patch would be to change the hardcoded default
stack size on no-MMU platforms, but it is difficult to choose an
appropriate value because the minimum required stack depends on the
software in use. This would also be a breaking change.

Signed-off-by: Ben Wolsieffer <ben.wolsieffer@hefring.com>
Ben Wolsieffer 6 months ago
parent
commit
c303df56d3

+ 9 - 0
extra/Configs/Config.in

@@ -644,6 +644,15 @@ config PTHREADS_DEBUG_SUPPORT
 	  If you are doing development and want to debug applications using
 	  uClibc's pthread library, answer Y.  Otherwise, answer N.
 
+config PTHREADS_STACK_DEFAULT_SIZE
+	int "Default thread stack size"
+	default 4194304 if TARGET_alpha # 4 MiB
+	default 4194304 if TARGET_powerpc # 4 MiB
+	default 2097152 # 2 MiB
+	help
+	  Set the default thread stack size. This option is useful on MMU-less
+	  systems where the stack size is fixed and the default stack size may
+	  be excessively large and waste memory.
 
 config UCLIBC_HAS_SYSLOG
 	bool "Syslog support"

+ 7 - 7
libpthread/nptl/init.c

@@ -278,17 +278,17 @@ __pthread_initialize_minimal_internal (void)
   struct rlimit limit;
   if (getrlimit (RLIMIT_STACK, &limit) != 0
       || limit.rlim_cur == RLIM_INFINITY)
-    /* The system limit is not usable.  Use an architecture-specific
-       default.  */
-    limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
-  else if (limit.rlim_cur < PTHREAD_STACK_MIN)
+    /* The system limit is not usable.  Use a user-specified or
+       architecture-specific default.  */
+    limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
+  if (limit.rlim_cur < PTHREAD_STACK_MIN)
     /* The system limit is unusably small.
        Use the minimal size acceptable.  */
     limit.rlim_cur = PTHREAD_STACK_MIN;
 
-  /* Do not exceed architecture specific default */
-  if (limit.rlim_cur > ARCH_STACK_DEFAULT_SIZE)
-    limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
+  /* Do not exceed the user-specified or architecture-specific default */
+  if (limit.rlim_cur > __PTHREADS_STACK_DEFAULT_SIZE__)
+    limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
 
   /* Make sure it meets the minimum size that allocate_stack
      (allocatestack.c) will demand, which depends on the page size.  */

+ 0 - 3
libpthread/nptl/sysdeps/aarch64/pthreaddef.h

@@ -14,9 +14,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN 16
 

+ 0 - 3
libpthread/nptl/sysdeps/alpha/pthreaddef.h

@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(4 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  The ABI requires 16.  */
 #define STACK_ALIGN		16
 

+ 0 - 3
libpthread/nptl/sysdeps/arc/pthreaddef.h

@@ -17,9 +17,6 @@
 
 #include <sysdep.h>
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN		8
 

+ 0 - 3
libpthread/nptl/sysdeps/arm/pthreaddef.h

@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN		16

+ 0 - 3
libpthread/nptl/sysdeps/csky/pthreaddef.h

@@ -5,9 +5,6 @@
  * in this tarball.
  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN		16

+ 0 - 3
libpthread/nptl/sysdeps/i386/pthreaddef.h

@@ -16,9 +16,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN		16

+ 0 - 3
libpthread/nptl/sysdeps/kvx/pthreaddef.h

@@ -6,9 +6,6 @@
  * Copyright (C) 2019 Kalray Inc.
  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN 32
 

+ 0 - 3
libpthread/nptl/sysdeps/m68k/pthreaddef.h

@@ -15,9 +15,6 @@
    License along with the GNU C Library.  If not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN		16
 

+ 0 - 3
libpthread/nptl/sysdeps/metag/pthreaddef.h

@@ -17,9 +17,6 @@
 
 #include <sysdep.h>
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN		8
 

+ 0 - 3
libpthread/nptl/sysdeps/microblaze/pthreaddef.h

@@ -19,9 +19,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE  (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN         16
 

+ 0 - 3
libpthread/nptl/sysdeps/mips/pthreaddef.h

@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN		16
 

+ 0 - 3
libpthread/nptl/sysdeps/nds32/pthreaddef.h

@@ -14,9 +14,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN		16

+ 0 - 3
libpthread/nptl/sysdeps/nios2/pthreaddef.h

@@ -16,9 +16,6 @@
    License along with the GNU C Library.  If not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN		4
 

+ 0 - 3
libpthread/nptl/sysdeps/or1k/pthreaddef.h

@@ -16,9 +16,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN 16
 

+ 0 - 3
libpthread/nptl/sysdeps/powerpc/pthreaddef.h

@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(4 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  The ABI requires 16
    bytes (for both 32-bit and 64-bit PowerPC).  */
 #define STACK_ALIGN		16

+ 0 - 3
libpthread/nptl/sysdeps/riscv64/pthreaddef.h

@@ -14,9 +14,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE (2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN 16
 

+ 0 - 3
libpthread/nptl/sysdeps/sh/pthreaddef.h

@@ -17,9 +17,6 @@
 
 #include <sysdep.h>
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN		8
 

+ 0 - 3
libpthread/nptl/sysdeps/sparc/pthreaddef.h

@@ -15,9 +15,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN		16
 

+ 0 - 3
libpthread/nptl/sysdeps/x86_64/pthreaddef.h

@@ -16,9 +16,6 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  SSE requires 16
    bytes.  */
 #define STACK_ALIGN		16

+ 0 - 3
libpthread/nptl/sysdeps/xtensa/pthreaddef.h

@@ -15,9 +15,6 @@
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <http://www.gnu.org/licenses/>.  */
 
-/* Default stack size.  */
-#define ARCH_STACK_DEFAULT_SIZE	(2 * 1024 * 1024)
-
 /* Required stack pointer alignment at beginning.  */
 #define STACK_ALIGN		16