Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpthread Set default stack size to the current limit o...



details:   https://anonhg.NetBSD.org/src/rev/c3402cb37d84
branches:  trunk
changeset: 555773:c3402cb37d84
user:      cl <cl%NetBSD.org@localhost>
date:      Thu Nov 27 16:30:54 2003 +0000

description:
Set default stack size to the current limit on the stack size as set
with the shell's command to change limits.  Make the PTHREAD_STACKSIZE
environment variable override the default stack size.  The old fixed
stack size behaviour can be enable with PT_FIXEDSTACKSIZE_LG when building
libpthread.

diffstat:

 lib/libpthread/Makefile        |   5 ++-
 lib/libpthread/pthread.3       |  16 +++++++++-
 lib/libpthread/pthread_int.h   |  23 ++++++++++++--
 lib/libpthread/pthread_stack.c |  64 +++++++++++++++++++++++++++++++++++++++--
 4 files changed, 97 insertions(+), 11 deletions(-)

diffs (195 lines):

diff -r c62c45e01631 -r c3402cb37d84 lib/libpthread/Makefile
--- a/lib/libpthread/Makefile   Thu Nov 27 16:19:16 2003 +0000
+++ b/lib/libpthread/Makefile   Thu Nov 27 16:30:54 2003 +0000
@@ -1,8 +1,11 @@
-#      $NetBSD: Makefile,v 1.23 2003/11/12 02:44:22 christos Exp $
+#      $NetBSD: Makefile,v 1.24 2003/11/27 16:30:54 cl Exp $
 #
 
 WARNS= 2
 
+# Define PT_FIXEDSTACKSIZE_LG to set a fixed stacksize
+#CPPFLAGS+=-DPT_FIXEDSTACKSIZE_LG=18
+
 .include <bsd.own.mk>
 
 .if exists(${.CURDIR}/arch/${MACHINE_ARCH})
diff -r c62c45e01631 -r c3402cb37d84 lib/libpthread/pthread.3
--- a/lib/libpthread/pthread.3  Thu Nov 27 16:19:16 2003 +0000
+++ b/lib/libpthread/pthread.3  Thu Nov 27 16:30:54 2003 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: pthread.3,v 1.2 2003/07/26 19:25:06 salo Exp $
+.\"    $NetBSD: pthread.3,v 1.3 2003/11/27 16:30:54 cl Exp $
 .\"
 .\" Copyright (c) 2003 Hubert Feyrer <hubertf%NetBSD.org@localhost>
 .\" and Thomas Klausner <wiz%NetBSD.org@localhost>
@@ -27,7 +27,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd June 21, 2003
+.Dd November 27, 2003
 .Dt PTHREAD 3
 .Os
 .Sh NAME
@@ -88,6 +88,18 @@
 Integer value giving the round-robin interval in ms.
 The default is 100.
 If set to 0, timer-based round-robin scheduling is disabled.
+.It Ev PTHREAD_STACKSIZE
+Integer value giving the stack size in kilobytes.
+This allows to set a smaller stack size than the default stack size.
+The default stack size is the current limit on the stack size as
+set with the shell's command to change limits
+.Ic ( limit
+for
+.Xr csh 1 ,
+or
+.Ic ulimit
+for
+.Xr sh 1 ) .
 .El
 .Sh SEE ALSO
 .Xr pthread_attr 3 ,
diff -r c62c45e01631 -r c3402cb37d84 lib/libpthread/pthread_int.h
--- a/lib/libpthread/pthread_int.h      Thu Nov 27 16:19:16 2003 +0000
+++ b/lib/libpthread/pthread_int.h      Thu Nov 27 16:30:54 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_int.h,v 1.21 2003/11/25 22:36:32 christos Exp $        */
+/*     $NetBSD: pthread_int.h,v 1.22 2003/11/27 16:30:54 cl Exp $      */
 
 /*-
  * Copyright (c) 2001,2002,2003 The NetBSD Foundation, Inc.
@@ -203,9 +203,24 @@
 #define PT_ATTR_MAGIC  0x22220002
 #define PT_ATTR_DEAD   0xDEAD0002
 
-#define PT_STACKSIZE_LG        18
-#define PT_STACKSIZE   (1<<(PT_STACKSIZE_LG)) 
-#define PT_STACKMASK   (PT_STACKSIZE-1)
+#ifdef PT_FIXEDSTACKSIZE_LG
+
+#define        PT_STACKSIZE_LG PT_FIXEDSTACKSIZE_LG 
+#define        PT_STACKSIZE    (1<<(PT_STACKSIZE_LG)) 
+#define        PT_STACKMASK    (PT_STACKSIZE-1)
+
+#else  /* PT_FIXEDSTACKSIZE_LG */
+
+extern int             pt_stacksize_lg;
+extern size_t          pt_stacksize;
+extern vaddr_t         pt_stackmask;
+
+#define        PT_STACKSIZE_LG pt_stacksize_lg
+#define        PT_STACKSIZE    pt_stacksize
+#define        PT_STACKMASK    pt_stackmask
+
+#endif /* PT_FIXEDSTACKSIZE_LG */
+
 
 #define PT_UPCALLSTACKS        16
 
diff -r c62c45e01631 -r c3402cb37d84 lib/libpthread/pthread_stack.c
--- a/lib/libpthread/pthread_stack.c    Thu Nov 27 16:19:16 2003 +0000
+++ b/lib/libpthread/pthread_stack.c    Thu Nov 27 16:30:54 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_stack.c,v 1.8 2003/07/17 21:07:39 nathanw Exp $        */
+/*     $NetBSD: pthread_stack.c,v 1.9 2003/11/27 16:30:54 cl Exp $     */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_stack.c,v 1.8 2003/07/17 21:07:39 nathanw Exp $");
+__RCSID("$NetBSD: pthread_stack.c,v 1.9 2003/11/27 16:30:54 cl Exp $");
 
 #include <err.h>
 #include <errno.h>
@@ -48,13 +48,29 @@
 #include <unistd.h>
 #include <sys/queue.h>
 #include <sys/mman.h>
+#include <sys/resource.h>
 
 #include <sched.h>
 #include "pthread.h"
 #include "pthread_int.h"
 
 static pthread_t
-pthread__stackid_setup(void *base, int size);
+pthread__stackid_setup(void *base, size_t size);
+
+#ifndef PT_FIXEDSTACKSIZE_LG
+/* 
+ * We have to initialize the pt_stack* variables here because mutexes
+ * are used before pthread_init() and thus pthread__initmain() are
+ * called.  Since mutexes only save the stack pointer and not a
+ * pointer to the thread data, it is safe to change the mapping from
+ * stack pointer to thread data afterwards.
+ */
+#define        _STACKSIZE_LG 18
+int    pt_stacksize_lg = _STACKSIZE_LG;
+size_t pt_stacksize = 1 << _STACKSIZE_LG;
+vaddr_t        pt_stackmask = (1 << _STACKSIZE_LG) - 1;
+#undef _STACKSIZE_LG
+#endif /* !PT_FIXEDSTACKSIZE_LG */
 
 
 /*
@@ -89,6 +105,46 @@
 {
        void *base;
 
+#ifndef PT_FIXEDSTACKSIZE_LG
+       struct rlimit slimit;
+       size_t pagesize;
+       char *value;
+       int ret;
+
+       pagesize = (size_t)sysconf(_SC_PAGESIZE);
+       pt_stacksize = 0;
+       ret = getrlimit(RLIMIT_STACK, &slimit);
+       if (ret == -1)
+               err(1, "Couldn't get stack resource consumption limits");
+       value = getenv("PTHREAD_STACKSIZE");
+       if (value) {
+               pt_stacksize = atoi(value) * 1024;
+               if (pt_stacksize > slimit.rlim_cur)
+                       pt_stacksize = (size_t)slimit.rlim_cur;
+       }
+       if (pt_stacksize == 0)
+               pt_stacksize = (size_t)slimit.rlim_cur;
+       if (pt_stacksize < 4 * pagesize)
+               errx(1, "Stacksize limit is too low, minimum %zd kbyte.",
+                   4 * pagesize / 1024);
+
+       pt_stacksize_lg = -1;
+       while (pt_stacksize) {
+               pt_stacksize >>= 1;
+               pt_stacksize_lg++;
+       }
+
+       pt_stacksize = (1 << pt_stacksize_lg);
+       pt_stackmask = pt_stacksize - 1;
+
+       /*
+        * XXX The "initial" thread stack can be smaller than
+        * requested because we don't control the end of the stack.
+        * On i386 the stack usually ends at 0xbfc00000 and for
+        * requested sizes >=8MB, we get a 4MB smaller stack.
+        */
+#endif /* PT_FIXEDSTACKSIZE_LG */
+
        base = (void *) (pthread__sp() & ~PT_STACKMASK);
 
        *newt = pthread__stackid_setup(base, PT_STACKSIZE);
@@ -96,7 +152,7 @@
 
 static pthread_t
 /*ARGSUSED*/
-pthread__stackid_setup(void *base, int size)
+pthread__stackid_setup(void *base, size_t size)
 {
        pthread_t t;
        size_t pagesize;



Home | Main Index | Thread Index | Old Index