Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Aug 2014 05:53:42 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r269909 - head/lib/libthr/thread
Message-ID:  <201408130553.s7D5rgIR014087@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Wed Aug 13 05:53:41 2014
New Revision: 269909
URL: http://svnweb.freebsd.org/changeset/base/269909

Log:
  Add a knob LIBPTHREAD_BIGSTACK_MAIN, which instructs libthr to leave
  the whole RLIMIT_STACK-sized region of the kernel-allocated stack as
  the stack of main thread.
  
  By default, the main thread stack is clamped at 2MB (4MB on 64bit
  ABIs) and the rest is used for other threads stack allocation.  Since
  there is no programmatic way to adjust the size of the main thread
  stack, pthread_attr_setstacksize() is too late, the knob allows user
  to manage the main stack size both for single-threaded and
  multi-threaded processes with the rlimit.
  
  Reported by:	"Ivan A. Kosarev" <ivan@ivan-labs.com>
  Tested by:	dim
  Sponsored by:	The FreeBSD Foundation
  MFC after:	3 days

Modified:
  head/lib/libthr/thread/thr_init.c
  head/lib/libthr/thread/thr_stack.c

Modified: head/lib/libthr/thread/thr_init.c
==============================================================================
--- head/lib/libthr/thread/thr_init.c	Wed Aug 13 05:47:49 2014	(r269908)
+++ head/lib/libthr/thread/thr_init.c	Wed Aug 13 05:53:41 2014	(r269909)
@@ -37,6 +37,7 @@
 #include <sys/types.h>
 #include <sys/signalvar.h>
 #include <sys/ioctl.h>
+#include <sys/resource.h>
 #include <sys/sysctl.h>
 #include <sys/ttycom.h>
 #include <sys/mman.h>
@@ -441,6 +442,7 @@ init_main_thread(struct pthread *thread)
 static void
 init_private(void)
 {
+	struct rlimit rlim;
 	size_t len;
 	int mib[2];
 	char *env;
@@ -471,6 +473,12 @@ init_private(void)
 		len = sizeof (_usrstack);
 		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
 			PANIC("Cannot get kern.usrstack from sysctl");
+		env = getenv("LIBPTHREAD_BIGSTACK_MAIN");
+		if (env != NULL) {
+			if (getrlimit(RLIMIT_STACK, &rlim) == -1)
+				PANIC("Cannot get stack rlimit");
+			_thr_stack_initial = rlim.rlim_cur;
+		}
 		len = sizeof(_thr_is_smp);
 		sysctlbyname("kern.smp.cpus", &_thr_is_smp, &len, NULL, 0);
 		_thr_is_smp = (_thr_is_smp > 1);

Modified: head/lib/libthr/thread/thr_stack.c
==============================================================================
--- head/lib/libthr/thread/thr_stack.c	Wed Aug 13 05:47:49 2014	(r269908)
+++ head/lib/libthr/thread/thr_stack.c	Wed Aug 13 05:53:41 2014	(r269909)
@@ -246,7 +246,10 @@ _thr_stack_alloc(struct pthread_attr *at
 		THREAD_LIST_UNLOCK(curthread);
 	}
 	else {
-		/* Allocate a stack from usrstack. */
+		/*
+		 * Allocate a stack from or below usrstack, depending
+		 * on the LIBPTHREAD_BIGSTACK_MAIN env variable.
+		 */
 		if (last_stack == NULL)
 			last_stack = _usrstack - _thr_stack_initial -
 			    _thr_guard_default;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201408130553.s7D5rgIR014087>