Skip to content

Commit

Permalink
Add cmake variable to set the max app thread stack size (bytecodealli…
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyongh authored Aug 11, 2020
1 parent 8ad9c17 commit 8c82073
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@ if (WAMR_DISABLE_HW_BOUND_CHECK EQUAL 1)
add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=1)
message (" Hardware boundary check disabled")
endif ()
if (DEFINED WAMR_APP_THREAD_STACK_SIZE_MAX)
add_definitions (-DAPP_THREAD_STACK_SIZE_MAX=${WAMR_APP_THREAD_STACK_SIZE_MAX})
endif ()

5 changes: 3 additions & 2 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,12 @@ enum {
#if !defined(BH_PLATFORM_ZEPHYR) && !defined(BH_PLATFORM_ALIOS_THINGS)
#define APP_THREAD_STACK_SIZE_DEFAULT (32 * 1024)
#define APP_THREAD_STACK_SIZE_MIN (24 * 1024)
#define APP_THREAD_STACK_SIZE_MAX (256 * 1024)
#else
#define APP_THREAD_STACK_SIZE_DEFAULT (6 * 1024)
#define APP_THREAD_STACK_SIZE_MIN (4 * 1024)
#define APP_THREAD_STACK_SIZE_MAX (256 * 1024)
#endif
#if !defined(APP_THREAD_STACK_SIZE_MAX)
#define APP_THREAD_STACK_SIZE_MAX (8 * 1024 * 1024)
#endif

/* Reserved bytes to the native thread stack boundary, throw native
Expand Down
12 changes: 11 additions & 1 deletion core/shared/platform/common/posix/posix_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,19 @@ uint8 *os_thread_get_stack_boundary()
uint8 *addr = NULL;
size_t stack_size, guard_size;
int page_size = getpagesize();
size_t max_stack_size = (APP_THREAD_STACK_SIZE_MAX + page_size - 1)
& ~(page_size - 1);

if (max_stack_size < APP_THREAD_STACK_SIZE_DEFAULT)
max_stack_size = APP_THREAD_STACK_SIZE_DEFAULT;

#ifdef __linux__
if (pthread_getattr_np(self, &attr) == 0) {
pthread_attr_getstack(&attr, (void**)&addr, &stack_size);
pthread_attr_getguardsize(&attr, &guard_size);
pthread_attr_destroy(&attr);
if (stack_size > max_stack_size)
addr = addr + stack_size - max_stack_size;
if (guard_size < (size_t)page_size)
/* Reserved 1 guard page at least for safety */
guard_size = (size_t)page_size;
Expand All @@ -257,7 +264,10 @@ uint8 *os_thread_get_stack_boundary()
#elif defined(__APPLE__)
if ((addr = (uint8*)pthread_get_stackaddr_np(self))) {
stack_size = pthread_get_stacksize_np(self);
addr -= stack_size;
if (stack_size > max_stack_size)
addr -= max_stack_size;
else
addr -= stack_size;
/* Reserved 1 guard page at least for safety */
addr += page_size;
}
Expand Down
4 changes: 4 additions & 0 deletions doc/build_wamr.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM
- **WAMR_DISABLE_HW_BOUND_CHECK**=1/0, default to enable if not set and supported by platform
> Note: by default only platform linux/darwin/android/vxworks 64-bit will enable boundary check with hardware trap in AOT or JIT mode, and the wamrc tool will generate AOT code without boundary check instructions in all 64-bit targets except SGX to improve performance.
#### **Set maximum app thread stack size**
- **WAMR_APP_THREAD_STACK_SIZE_MAX**=n, default to 8 MB (8388608) if not set
> Note: the AOT boundary check with hardware trap mechanism might consume large stack since the OS may lazily grow the stack mapping as a guard page is hit, we may use this configuration to reduce the total stack usage, e.g. -DWAMR_APP_THREAD_STACK_SIZE_MAX=131072 (128 KB).
**Combination of configurations:**

We can combine the configurations. For example, if we want to disable interpreter, enable AOT and WASI, we can run command:
Expand Down

0 comments on commit 8c82073

Please sign in to comment.