From 5a9dda22de6131f37bed3e4fc79bce66a4fa3a44 Mon Sep 17 00:00:00 2001 From: bluhm Date: Fri, 21 May 2021 20:21:10 +0000 Subject: [PATCH] Set the stack size attribute of the pthreads large enough to allocate the specified amount of stack memory. On 32 bit architectures regress fork-exit failed as the default stack size for pthreads is smaller. With the limit set to the expected size we can test even larger thread stacks. --- regress/sys/kern/fork-exit/Makefile | 14 ++++++++++++-- regress/sys/kern/fork-exit/fork-exit.c | 23 ++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/regress/sys/kern/fork-exit/Makefile b/regress/sys/kern/fork-exit/Makefile index a45ac4bb7b1..8d7032870e5 100644 --- a/regress/sys/kern/fork-exit/Makefile +++ b/regress/sys/kern/fork-exit/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.3 2021/05/04 13:24:49 bluhm Exp $ +# $OpenBSD: Makefile,v 1.4 2021/05/21 20:21:10 bluhm Exp $ # Copyright (c) 2021 Alexander Bluhm # @@ -64,6 +64,11 @@ run-fork-heap: ${PROG} # allocate 400 MB of heap memory in processes ulimit -p 500 -n 1000; ./fork-exit -p 100 -h 1000 +REGRESS_TARGETS += run-fork1-thread1-heap +run-fork1-thread1-heap: ${PROG} + # allocate 400 MB of heap memory in single child and one thread + ulimit -p 500 -n 1000; ./fork-exit -t 1 -h 100000 + REGRESS_TARGETS += run-fork-thread-heap run-fork-thread-heap: ${PROG} # allocate 400 MB of heap memory in threads @@ -79,10 +84,15 @@ run-fork-stack: ${PROG} # allocate 400 MB of stack memory in processes ulimit -p 500 -n 1000; ulimit -s 32768; ./fork-exit -p 100 -s 1000 +REGRESS_TARGETS += run-fork1-thread1-stack +run-fork1-thread1-stack: ${PROG} + # allocate 400 MB of stack memory in single child and one thread + ulimit -p 500 -n 1000; ./fork-exit -t 1 -s 100000 + REGRESS_TARGETS += run-fork-thread-stack run-fork-thread-stack: ${PROG} # allocate 400 MB of stack memory in threads - ulimit -p 500 -n 1000; ulimit -s 32768; ./fork-exit -p 10 -t 100 -s 100 + ulimit -p 500 -n 1000; ./fork-exit -p 10 -t 100 -s 100 REGRESS_CLEANUP = cleanup cleanup: diff --git a/regress/sys/kern/fork-exit/fork-exit.c b/regress/sys/kern/fork-exit/fork-exit.c index 2eba0ffdeba..6d52ade44ef 100644 --- a/regress/sys/kern/fork-exit/fork-exit.c +++ b/regress/sys/kern/fork-exit/fork-exit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fork-exit.c,v 1.3 2021/05/04 13:24:49 bluhm Exp $ */ +/* $OpenBSD: fork-exit.c,v 1.4 2021/05/21 20:21:10 bluhm Exp $ */ /* * Copyright (c) 2021 Alexander Bluhm @@ -113,19 +113,31 @@ run_thread(void *arg) static void create_threads(void) { + pthread_attr_t tattr; pthread_t *thrs; int i, error; + error = pthread_attr_init(&tattr); + if (error) + errc(1, error, "pthread_attr_init"); + if (stack) { + /* thread start and function call overhead needs a bit more */ + error = pthread_attr_setstacksize(&tattr, + (stack + 2) * (4096 + 32)); + if (error) + errc(1, error, "pthread_attr_setstacksize"); + } + error = pthread_barrier_init(&thread_barrier, NULL, threads + 1); if (error) - errc(1, errno, "pthread_barrier_init"); + errc(1, error, "pthread_barrier_init"); thrs = reallocarray(NULL, threads, sizeof(pthread_t)); if (thrs == NULL) err(1, "thrs"); for (i = 0; i < threads; i++) { - error = pthread_create(&thrs[i], NULL, run_thread, NULL); + error = pthread_create(&thrs[i], &tattr, run_thread, NULL); if (error) errc(1, error, "pthread_create"); } @@ -217,13 +229,14 @@ main(int argc, char *argv[]) errstr, optarg); break; case 'p': - procs = strtonum(optarg, 0, INT_MAX, &errstr); + procs = strtonum(optarg, 0, INT_MAX / 4096, &errstr); if (errstr != NULL) errx(1, "number of procs is %s: %s", errstr, optarg); break; case 's': - stack = strtonum(optarg, 0, INT_MAX, &errstr); + stack = strtonum(optarg, 0, + (INT_MAX - 2) / (4096 + 32), &errstr); if (errstr != NULL) errx(1, "number of stack allocations is %s: %s", errstr, optarg); -- 2.20.1