Normalize how the global heap pool is configured across iwasm apps (#1628)

Use the cmake variable `WAMR_BUILD_GLOBAL_HEAP_POOL` and
`WAMR_BUILD_GLOBAL_HEAP_SIZE` to enable/disable the global heap pool
and set its size. And set the default global heap size in core/config.h and
the cmake files.

As a result, the developers who build iwasm can easily enable/disable the
global heap pool and change its size regardless of the iwasm implementation,
without manually finding and patching the right location for that value.
This commit is contained in:
Jämes Ménétrey 2022-10-25 15:36:24 +02:00 committed by GitHub
parent b5eea934cf
commit 6adf9194d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 137 additions and 54 deletions

View File

@ -262,6 +262,28 @@ if (WAMR_BUILD_LOAD_CUSTOM_SECTION EQUAL 1)
add_definitions (-DWASM_ENABLE_LOAD_CUSTOM_SECTION=1)
message (" Load custom section enabled")
endif ()
if (WAMR_BUILD_GLOBAL_HEAP_POOL EQUAL 1)
add_definitions(-DWASM_ENABLE_GLOBAL_HEAP_POOL=1)
message (" Global heap pool enabled")
endif ()
if (WAMR_BUILD_GLOBAL_HEAP_SIZE GREATER 0)
add_definitions (-DWASM_GLOBAL_HEAP_SIZE=${WAMR_BUILD_GLOBAL_HEAP_SIZE})
message (" Custom global heap size: " ${WAMR_BUILD_GLOBAL_HEAP_SIZE})
else ()
# Spec test requires more heap pool size
if (WAMR_BUILD_SPEC_TEST EQUAL 1)
if (WAMR_BUILD_PLATFORM STREQUAL "linux-sgx")
math(EXPR WAMR_BUILD_GLOBAL_HEAP_SIZE "100 * 1024 * 1024")
else ()
math(EXPR WAMR_BUILD_GLOBAL_HEAP_SIZE "300 * 1024 * 1024")
endif ()
add_definitions (-DWASM_GLOBAL_HEAP_SIZE=${WAMR_BUILD_GLOBAL_HEAP_SIZE})
else ()
# By default, the global heap size is of 10 MB
math(EXPR WAMR_BUILD_GLOBAL_HEAP_SIZE "10 * 1024 * 1024")
add_definitions (-DWASM_GLOBAL_HEAP_SIZE=${WAMR_BUILD_GLOBAL_HEAP_SIZE})
endif ()
endif ()
if (WAMR_BUILD_STACK_GUARD_SIZE GREATER 0)
add_definitions (-DWASM_STACK_GUARD_SIZE=${WAMR_BUILD_STACK_GUARD_SIZE})
message (" Custom stack guard size: " ${WAMR_BUILD_STACK_GUARD_SIZE})

View File

@ -302,13 +302,8 @@
/* Global heap pool size in bytes */
#ifndef WASM_GLOBAL_HEAP_SIZE
#if WASM_ENABLE_SPEC_TEST != 0
/* Spec test requires more heap pool size */
#define WASM_GLOBAL_HEAP_SIZE (300 * 1024 * 1024)
#else
#define WASM_GLOBAL_HEAP_SIZE (10 * 1024 * 1024)
#endif
#endif
/* Max app number of all modules */
#define MAX_APP_INSTALLATIONS 3

View File

@ -114,6 +114,19 @@ Currently we only profile the memory consumption of module, module_instance and
> The function name searching sequence is the same with dump call stack feature.
#### **Enable the global heap**
- **WAMR_BUILD_GLOBAL_HEAP_POOL**=1/0, default to disable if not set for all *iwasm* applications, except for the platforms Alios and Zephyr.
> **WAMR_BUILD_GLOBAL_HEAP_POOL** is used in the *iwasm* applications provided in the directory `product-mini`. When writing your own host application using WAMR, if you want to use a global heap and allocate memory from it, you must set the initialization argument `mem_alloc_type` to `Alloc_With_Pool`.
> The global heap is defined in the documentation [Memory model and memory usage tunning](memory_tune.md).
#### **Set the global heap size**
- **WAMR_BUILD_GLOBAL_HEAP_SIZE**=n, default to 10 MB (10485760) if not set for all *iwasm* applications, except for the platforms Alios (256 kB), Riot (256 kB) and Zephyr (128 kB).
> **WAMR_BUILD_GLOBAL_HEAP_SIZE** is used in the *iwasm* applications provided in the directory `product-mini`. When writing your own host application using WAMR, if you want to set the amount of memory dedicated to the global heap pool, you must set the initialization argument `mem_alloc_option.pool` with the appropriate values.
> The global heap is defined in the documentation [Memory model and memory usage tunning](memory_tune.md).
> Note: if `WAMR_BUILD_GLOBAL_HEAP_SIZE` is not set and the flag `WAMR_BUILD_SPEC_TEST` is set, the global heap size is equal to 300 MB (314572800), or 100 MB (104857600) when compiled for Intel SGX (Linux).
#### **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).

View File

@ -50,6 +50,18 @@ WAMR_BUILD_INTERP = 1
# Enable AOT by default.
WAMR_BUILD_AOT = 1
# Override the global heap usage
ifndef WAMR_BUILD_GLOBAL_HEAP_POOL
WAMR_BUILD_GLOBAL_HEAP_POOL=1
endif
GLOBAL_DEFINES += WASM_ENABLE_GLOBAL_HEAP_POOL=${WAMR_BUILD_GLOBAL_HEAP_POOL}
# Override the global heap size for small devices
ifndef WAMR_BUILD_GLOBAL_HEAP_SIZE
WAMR_BUILD_GLOBAL_HEAP_SIZE = 262144 # 256 kB
endif
GLOBAL_DEFINES += WASM_GLOBAL_HEAP_SIZE=${WAMR_BUILD_GLOBAL_HEAP_SIZE}
ifeq (${WAMR_BUILD_INTERP}, 1)
GLOBAL_DEFINES += WASM_ENABLE_INTERP=1
endif

View File

@ -38,7 +38,9 @@ app_instance_main(wasm_module_inst_t module_inst)
return NULL;
}
static char global_heap_buf[256 * 1024] = { 0 };
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
#endif
void
iwasm_main(void *arg1)
@ -57,9 +59,13 @@ iwasm_main(void *arg1)
memset(&init_args, 0, sizeof(RuntimeInitArgs));
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#else
#error Another memory allocation scheme than global heap pool is not implemented yet for Alios.
#endif
/* initialize runtime environment */
if (!wasm_runtime_full_init(&init_args)) {

View File

@ -70,10 +70,14 @@ Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz)
memset(&init_args, 0, sizeof(RuntimeInitArgs));
#if WASM_ENABLE_GLOBAL_HEAP_POOL == 0
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void *)malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void *)realloc;
init_args.mem_alloc_option.allocator.free_func = (void *)free;
#else
#error The usage of a global heap pool is not implemented yet for Android.
#endif
LOGI("wasm_runtime_full_init");
/* initialize runtime environment */

View File

@ -40,10 +40,14 @@ iwasm_main(void *arg)
/* configure memory allocation */
memset(&init_args, 0, sizeof(RuntimeInitArgs));
#if WASM_ENABLE_GLOBAL_HEAP_POOL == 0
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void *)os_malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void *)os_realloc;
init_args.mem_alloc_option.allocator.free_func = (void *)os_free;
#else
#error The usage of a global heap pool is not implemented yet for esp-idf.
#endif
ESP_LOGI(LOG_TAG, "Initialize WASM runtime");
/* initialize runtime environment */

View File

@ -111,6 +111,19 @@ add_custom_command (
add_custom_target (vmlib_untrusted ALL DEPENDS libvmlib_untrusted.a)
if (DEFINED WAMR_BUILD_GLOBAL_HEAP_POOL)
execute_process(
COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_GLOBAL_HEAP_POOL = .*/WAMR_BUILD_GLOBAL_HEAP_POOL = ${WAMR_BUILD_GLOBAL_HEAP_POOL}/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile"
OUTPUT_VARIABLE cmdOutput
)
if (DEFINED WAMR_BUILD_GLOBAL_HEAP_SIZE)
execute_process(
COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_GLOBAL_HEAP_SIZE = .*/WAMR_BUILD_GLOBAL_HEAP_SIZE = ${WAMR_BUILD_GLOBAL_HEAP_SIZE}/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile"
OUTPUT_VARIABLE cmdOutput
)
endif()
endif()
if (WAMR_BUILD_LIB_RATS EQUAL 1)
execute_process(
COMMAND bash -c "sed -i -E 's/^#define LIB_RATS 0 /#define LIB_RATS 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl"

View File

@ -370,15 +370,14 @@ set_log_verbose_level(int log_verbose_level)
}
static bool
init_runtime(bool alloc_with_pool, uint32_t max_thread_num)
init_runtime(uint32_t max_thread_num)
{
uint64_t ecall_args[2];
uint64_t ecall_args[1];
ecall_args[0] = alloc_with_pool;
ecall_args[1] = max_thread_num;
ecall_args[0] = max_thread_num;
if (SGX_SUCCESS
!= ecall_handle_command(g_eid, CMD_INIT_RUNTIME, (uint8_t *)ecall_args,
sizeof(uint64_t) * 2)) {
sizeof(ecall_args))) {
printf("Call ecall_handle_command() failed.\n");
return false;
}
@ -612,7 +611,7 @@ main(int argc, char *argv[])
void *wasm_module_inst = NULL;
char error_buf[128] = { 0 };
int log_verbose_level = 2;
bool is_repl_mode = false, alloc_with_pool = false;
bool is_repl_mode = false;
const char *dir_list[8] = { NULL };
uint32_t dir_list_size = 0;
const char *env_list[8] = { NULL };
@ -628,7 +627,7 @@ main(int argc, char *argv[])
#if TEST_OCALL_API != 0
{
if (!init_runtime(alloc_with_pool, max_thread_num)) {
if (!init_runtime(max_thread_num)) {
return -1;
}
ecall_iwasm_test(g_eid);
@ -735,7 +734,7 @@ main(int argc, char *argv[])
wasm_file = argv[0];
/* Init runtime */
if (!init_runtime(alloc_with_pool, max_thread_num)) {
if (!init_runtime(max_thread_num)) {
return -1;
}
@ -828,7 +827,7 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
{
uint32_t stack_size = 16 * 1024, heap_size = 16 * 1024;
int log_verbose_level = 2;
bool is_repl_mode = false, alloc_with_pool = false;
bool is_repl_mode = false;
const char *dir_list[8] = { NULL };
uint32_t dir_list_size = 0;
const char *env_list[8] = { NULL };
@ -871,7 +870,7 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
}
/* Init runtime */
if (!init_runtime(alloc_with_pool, max_thread_num)) {
if (!init_runtime(max_thread_num)) {
printf("Failed to init runtime\n");
return -1;
}

View File

@ -3,7 +3,7 @@
<ProdID>0</ProdID>
<ISVSVN>0</ISVSVN>
<StackMaxSize>0x100000</StackMaxSize>
<HeapMaxSize>0x2000000</HeapMaxSize>
<HeapMaxSize>0x8000000</HeapMaxSize>
<ReservedMemMaxSize>0x1000000</ReservedMemMaxSize>
<ReservedMemExecutable>1</ReservedMemExecutable>
<TCSNum>10</TCSNum>

View File

@ -64,10 +64,8 @@ typedef struct EnclaveModule {
uint32 total_size_mapped;
} EnclaveModule;
#if WASM_ENABLE_SPEC_TEST == 0
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
#else
static char global_heap_buf[100 * 1024 * 1024] = { 0 };
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
#endif
static void
@ -80,32 +78,25 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
static void
handle_cmd_init_runtime(uint64 *args, uint32 argc)
{
bool alloc_with_pool;
uint32 max_thread_num;
RuntimeInitArgs init_args;
bh_assert(argc == 2);
bh_assert(argc == 1);
os_set_print_function(enclave_print);
#if WASM_ENABLE_SPEC_TEST == 0
alloc_with_pool = (bool)args[0];
#else
alloc_with_pool = true;
#endif
max_thread_num = (uint32)args[1];
max_thread_num = (uint32)args[0];
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.max_thread_num = max_thread_num;
if (alloc_with_pool) {
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
}
else {
init_args.mem_alloc_type = Alloc_With_System_Allocator;
}
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#else
init_args.mem_alloc_type = Alloc_With_System_Allocator;
#endif
/* initialize runtime environment */
if (!wasm_runtime_full_init(&init_args)) {
@ -604,9 +595,13 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
memset(&init_args, 0, sizeof(RuntimeInitArgs));
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#else
init_args.mem_alloc_type = Alloc_With_System_Allocator;
#endif
/* initialize runtime environment */
if (!wasm_runtime_full_init(&init_args)) {

View File

@ -9,8 +9,10 @@ SGX_ARCH ?= x64
SGX_DEBUG ?= 0
SPEC_TEST ?= 0
# This variable is automatically set by CMakeLists.txt
# These variables are automatically set by CMakeLists.txt
SGX_IPFS = 0
WAMR_BUILD_GLOBAL_HEAP_POOL = 0
WAMR_BUILD_GLOBAL_HEAP_SIZE = 10485760
VMLIB_BUILD_DIR ?= $(CURDIR)/../build
LIB_RATS_SRC ?= $(VMLIB_BUILD_DIR)/_deps/librats-build
@ -132,7 +134,7 @@ ifeq ($(LIB_RATS), 1)
Enclave_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR)
endif
Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths)
Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths) -DWASM_GLOBAL_HEAP_SIZE=$(WAMR_BUILD_GLOBAL_HEAP_SIZE) -DWASM_ENABLE_GLOBAL_HEAP_POOL=$(WAMR_BUILD_GLOBAL_HEAP_POOL)
ifeq ($(SPEC_TEST), 1)
Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=1
else

View File

@ -45,6 +45,11 @@ if (NOT DEFINED WAMR_ROOT_DIR)
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif ()
# Override the global heap size for small devices
if (NOT DEFINED WAMR_BUILD_GLOBAL_HEAP_SIZE)
add_definitions (-DWASM_GLOBAL_HEAP_SIZE=262144) # 256 kB
endif ()
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)

View File

@ -55,9 +55,8 @@ iwasm_t(void *arg1)
return NULL;
}
/* choose allocator */
/* enable FUNC_ALLOC to use custom memory allocation functions */
#define FUNC_ALLOC
/* #define POOL_ALLOC */
void *
iwasm_main(void *arg1)
@ -71,17 +70,17 @@ iwasm_main(void *arg1)
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
#ifdef POOL_ALLOC
static char global_heap_buf[256 * 1024] = { 0 }; /* 256 kB */
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#elif defined(FUNC_ALLOC)
#if defined(FUNC_ALLOC) && WASM_ENABLE_GLOBAL_HEAP_POOL == 0
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = malloc;
init_args.mem_alloc_option.allocator.realloc_func = realloc;
init_args.mem_alloc_option.allocator.free_func = free;
#elif WASM_ENABLE_GLOBAL_HEAP_POOL != 0
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#else
init_args.mem_alloc_type = Alloc_With_System_Allocator;
#endif

View File

@ -174,10 +174,8 @@ validate_env_str(char *env)
}
#endif
#define USE_GLOBAL_HEAP_BUF 0
#if USE_GLOBAL_HEAP_BUF != 0
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
#endif
#if WASM_ENABLE_MULTI_MODULE != 0
@ -359,7 +357,7 @@ main(int argc, char *argv[])
memset(&init_args, 0, sizeof(RuntimeInitArgs));
#if USE_GLOBAL_HEAP_BUF != 0
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);

View File

@ -40,6 +40,16 @@ if (WAMR_BUILD_TARGET STREQUAL "RISCV64_LP64" OR WAMR_BUILD_TARGET STREQUAL "RIS
set (WAMR_BUILD_FAST_INTERP 1)
endif ()
# Override the global heap usage
if (NOT DEFINED WAMR_BUILD_GLOBAL_HEAP_POOL)
add_definitions (-DWASM_ENABLE_GLOBAL_HEAP_POOL=1)
endif
# Override the global heap size for small devices
if (NOT DEFINED WAMR_BUILD_GLOBAL_HEAP_SIZE)
add_definitions (-DWASM_GLOBAL_HEAP_SIZE=131072) # 128 kB
endif ()
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..)
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)

View File

@ -31,7 +31,7 @@
#define CONFIG_APP_HEAP_SIZE 256
#else /* else of BUILD_TARGET_RISCV64_LP64 || BUILD_TARGET_RISCV32_ILP32 */
#define CONFIG_GLOBAL_HEAP_BUF_SIZE 131072
#define CONFIG_GLOBAL_HEAP_BUF_SIZE WASM_GLOBAL_HEAP_SIZE
#define CONFIG_APP_STACK_SIZE 8192
#define CONFIG_APP_HEAP_SIZE 8192
@ -103,7 +103,9 @@ app_instance_main(wasm_module_inst_t module_inst)
return NULL;
}
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
static char global_heap_buf[CONFIG_GLOBAL_HEAP_BUF_SIZE] = { 0 };
#endif
#ifdef CONFIG_BOARD_ESP32
#include "mem_alloc.h"
@ -176,9 +178,13 @@ iwasm_main(void *arg1, void *arg2, void *arg3)
memset(&init_args, 0, sizeof(RuntimeInitArgs));
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#else
#error Another memory allocation scheme than global heap pool is not implemented yet for Zephyr.
#endif
/* initialize runtime environment */
if (!wasm_runtime_full_init(&init_args)) {