This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Setting default/max thread stack size with rlimit
- From: Corey Minyard <minyard at acm dot org>
- To: libc-alpha at sources dot redhat dot com
- Date: Thu, 06 Feb 2003 15:10:23 -0600
- Subject: Setting default/max thread stack size with rlimit
I was looking at the code to set the max stack size using rlimit, and I
saw something a little wierd. LinuxThreads set the max stack size to
the current stack size from rlimit, which seems a little wierd. It
seems more intuative to set it to the max stack size from rlimit, and
use the current stack size to set the default stack size.
I have attached a patch to do this.
-Corey
--- glibc-2.2.5.old/linuxthreads/internals.h Fri Dec 13 14:52:11 2002
+++ glibc-2.2.5/linuxthreads/internals.h Thu Feb 6 13:37:33 2003
@@ -343,7 +343,7 @@
THREAD_SELF implementation is used, this must be a power of two and
a multiple of PAGE_SIZE. */
#ifndef STACK_SIZE
-#define STACK_SIZE (2 * 1024 * 1024)
+#define STACK_SIZE __pthread_default_stacksize
#endif
/* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
--- glibc-2.2.5.old/linuxthreads/pthread.c Mon Dec 16 15:44:58 2002
+++ glibc-2.2.5/linuxthreads/pthread.c Thu Feb 6 14:55:43 2003
@@ -190,6 +190,8 @@ int __pthread_exit_code;
/* Maximum stack size. */
size_t __pthread_max_stacksize;
+size_t __pthread_default_stacksize = (2 * 1024 * 1024);
+
/* Nozero if the machine has more than one processor. */
int __pthread_smp_kernel;
@@ -405,16 +407,20 @@ void
__pthread_init_max_stacksize(void)
{
struct rlimit limit;
- size_t max_stack;
+ size_t max_stack;
+ unsigned long default_stack;
+#ifndef FLOATING_STACKS
+ unsigned long pow_2;
+#endif
getrlimit(RLIMIT_STACK, &limit);
#ifdef FLOATING_STACKS
- if (limit.rlim_cur == RLIM_INFINITY)
- limit.rlim_cur = ARCH_STACK_MAX_SIZE;
+ if (limit.rlim_max == RLIM_INFINITY)
+ limit.rlim_max = ARCH_STACK_MAX_SIZE;
# ifdef NEED_SEPARATE_REGISTER_STACK
- max_stack = limit.rlim_cur / 2;
+ max_stack = limit.rlim_max / 2;
# else
- max_stack = limit.rlim_cur;
+ max_stack = limit.rlim_max;
# endif
#else
/* Play with the stack size limit to make sure that no stack ever grows
@@ -426,12 +432,47 @@ __pthread_init_max_stacksize(void)
# else
max_stack = STACK_SIZE - __getpagesize();
# endif
- if (limit.rlim_cur > max_stack) {
- limit.rlim_cur = max_stack;
+ if (limit.rlim_max > max_stack) {
+ limit.rlim_max = max_stack;
setrlimit(RLIMIT_STACK, &limit);
}
#endif
__pthread_max_stacksize = max_stack;
+
+ /* Set the default stack stack size */
+ if (limit.rlim_cur == RLIM_INFINITY)
+ limit.rlim_cur = ARCH_STACK_MAX_SIZE;
+# ifdef NEED_SEPARATE_REGISTER_STACK
+ default_stack = limit.rlim_cur / 2;
+#else
+ default_stack = limit.rlim_cur;
+#endif
+
+ /* Silently lower the default stack to the max stack, if
+ necessary. */
+ if (default_stack > max_stack)
+ default_stack = max_stack;
+
+#ifndef FLOATING_STACKS
+ /* Make sure it's not ridiculously large, so
+ the pow_2 loop is guaranteed to terminate. */
+ if (default_stack > (ULONG_MAX/2))
+ default_stack = ULONG_MAX/2;
+
+ /* Enforce a 1-page minimum stack and a power-of-two stack
+ size. */
+ pow_2 = PAGE_SIZE;
+ while (default_stack > pow_2)
+ pow_2 *= 2;
+
+ /* Make doubly sure it's not bigger than max_stack, after
+ we've increased it to the next power of 2. */
+ if (pow_2 > max_stack)
+ pow_2 /= 2;
+ default_stack = pow_2;
+#endif
+
+ __pthread_default_stacksize = default_stack;
}