Skip to content

Add CLI option for iwasm to create and attach shared heap to wasm app #4499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)

heap->heap_handle = NULL;
heap->base_addr = init_args->pre_allocated_addr;
LOG_VERBOSE("Create preallocated shared heap %p with size %u",
heap->base_addr, size);
}
else {
if (!(heap->heap_handle =
Expand All @@ -225,6 +227,8 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
LOG_WARNING("init share heap failed");
goto fail4;
}
LOG_VERBOSE("Create pool shared heap %p with size %u", heap->base_addr,
size);
}

os_mutex_lock(&shared_heap_list_lock);
Expand Down Expand Up @@ -509,6 +513,8 @@ wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
os_mutex_lock(&shared_heap_list_lock);
shared_heap->attached_count++;
os_mutex_unlock(&shared_heap_list_lock);
LOG_VERBOSE("Shared heap %p is attached to module instance %p", shared_heap,
module_inst);
return true;
}

Expand Down Expand Up @@ -570,6 +576,7 @@ wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
e->shared_heap_base_addr_adj = NULL;
}
#endif /* end of WASM_ENABLE_AOT != 0 */
LOG_VERBOSE("Shared heap is detached from module instance %p", module_inst);
}

void
Expand Down
37 changes: 37 additions & 0 deletions product-mini/platforms/posix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ print_help(void)
#else
printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB when libc wasi is diabled\n");
#endif
#if WASM_ENABLE_SHARED_HEAP != 0
printf(" --shared-heap-size=n Create shared heap of n bytes and attach to the wasm app.\n");
printf(" The size n will be adjusted to a minumum number aligned to page size\n");
#endif
#if WASM_ENABLE_FAST_JIT != 0
printf(" --jit-codecache-size=n Set fast jit maximum code cache size in bytes,\n");
printf(" default is %u KB\n", FAST_JIT_DEFAULT_CODE_CACHE_SIZE / 1024);
Expand Down Expand Up @@ -578,6 +582,11 @@ main(int argc, char *argv[])
#else
uint32 heap_size = 16 * 1024;
#endif
#if WASM_ENABLE_SHARED_HEAP != 0
SharedHeapInitArgs heap_init_args;
uint32 shared_heap_size = 0;
void *shared_heap = NULL;
#endif
#if WASM_ENABLE_FAST_JIT != 0
uint32 jit_code_cache_size = FAST_JIT_DEFAULT_CODE_CACHE_SIZE;
#endif
Expand Down Expand Up @@ -685,6 +694,13 @@ main(int argc, char *argv[])
return print_help();
heap_size = atoi(argv[0] + 12);
}
#if WASM_ENABLE_SHARED_HEAP != 0
else if (!strncmp(argv[0], "--shared-heap-size=", 19)) {
if (argv[0][19] == '\0')
return print_help();
shared_heap_size = atoi(argv[0] + 19);
}
#endif
#if WASM_ENABLE_FAST_JIT != 0
else if (!strncmp(argv[0], "--jit-codecache-size=", 21)) {
if (argv[0][21] == '\0')
Expand Down Expand Up @@ -1007,6 +1023,24 @@ main(int argc, char *argv[])
}
#endif

#if WASM_ENABLE_SHARED_HEAP != 0
if (shared_heap_size > 0) {
memset(&heap_init_args, 0, sizeof(heap_init_args));
heap_init_args.size = shared_heap_size;
shared_heap = wasm_runtime_create_shared_heap(&heap_init_args);
if (!shared_heap) {
printf("Create preallocated shared heap failed\n");
goto fail6;
}

/* attach module instance 2 to the shared heap */
if (!wasm_runtime_attach_shared_heap(wasm_module_inst, shared_heap)) {
printf("Attach shared heap failed.\n");
goto fail6;
}
}
#endif

ret = 0;
const char *exception = NULL;
if (is_repl_mode) {
Expand Down Expand Up @@ -1050,6 +1084,9 @@ main(int argc, char *argv[])
}
#endif

#if WASM_ENABLE_SHARED_HEAP != 0
fail6:
#endif
#if WASM_ENABLE_THREAD_MGR != 0
fail5:
#endif
Expand Down
Loading