Skip to content

Commit b322e29

Browse files
Add CLI option for iwasm to create and attach shared heap to wasm app (#4499)
1 parent f34d28c commit b322e29

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
199199

200200
heap->heap_handle = NULL;
201201
heap->base_addr = init_args->pre_allocated_addr;
202+
LOG_VERBOSE("Create preallocated shared heap %p with size %u",
203+
heap->base_addr, size);
202204
}
203205
else {
204206
if (!(heap->heap_handle =
@@ -225,6 +227,8 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
225227
LOG_WARNING("init share heap failed");
226228
goto fail4;
227229
}
230+
LOG_VERBOSE("Create pool shared heap %p with size %u", heap->base_addr,
231+
size);
228232
}
229233

230234
os_mutex_lock(&shared_heap_list_lock);
@@ -509,6 +513,8 @@ wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
509513
os_mutex_lock(&shared_heap_list_lock);
510514
shared_heap->attached_count++;
511515
os_mutex_unlock(&shared_heap_list_lock);
516+
LOG_VERBOSE("Shared heap %p is attached to module instance %p", shared_heap,
517+
module_inst);
512518
return true;
513519
}
514520

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

575582
void

product-mini/platforms/posix/main.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ print_help(void)
5757
#else
5858
printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB when libc wasi is diabled\n");
5959
#endif
60+
#if WASM_ENABLE_SHARED_HEAP != 0
61+
printf(" --shared-heap-size=n Create shared heap of n bytes and attach to the wasm app.\n");
62+
printf(" The size n will be adjusted to a minumum number aligned to page size\n");
63+
#endif
6064
#if WASM_ENABLE_FAST_JIT != 0
6165
printf(" --jit-codecache-size=n Set fast jit maximum code cache size in bytes,\n");
6266
printf(" default is %u KB\n", FAST_JIT_DEFAULT_CODE_CACHE_SIZE / 1024);
@@ -578,6 +582,11 @@ main(int argc, char *argv[])
578582
#else
579583
uint32 heap_size = 16 * 1024;
580584
#endif
585+
#if WASM_ENABLE_SHARED_HEAP != 0
586+
SharedHeapInitArgs heap_init_args;
587+
uint32 shared_heap_size = 0;
588+
void *shared_heap = NULL;
589+
#endif
581590
#if WASM_ENABLE_FAST_JIT != 0
582591
uint32 jit_code_cache_size = FAST_JIT_DEFAULT_CODE_CACHE_SIZE;
583592
#endif
@@ -685,6 +694,13 @@ main(int argc, char *argv[])
685694
return print_help();
686695
heap_size = atoi(argv[0] + 12);
687696
}
697+
#if WASM_ENABLE_SHARED_HEAP != 0
698+
else if (!strncmp(argv[0], "--shared-heap-size=", 19)) {
699+
if (argv[0][19] == '\0')
700+
return print_help();
701+
shared_heap_size = atoi(argv[0] + 19);
702+
}
703+
#endif
688704
#if WASM_ENABLE_FAST_JIT != 0
689705
else if (!strncmp(argv[0], "--jit-codecache-size=", 21)) {
690706
if (argv[0][21] == '\0')
@@ -1007,6 +1023,24 @@ main(int argc, char *argv[])
10071023
}
10081024
#endif
10091025

1026+
#if WASM_ENABLE_SHARED_HEAP != 0
1027+
if (shared_heap_size > 0) {
1028+
memset(&heap_init_args, 0, sizeof(heap_init_args));
1029+
heap_init_args.size = shared_heap_size;
1030+
shared_heap = wasm_runtime_create_shared_heap(&heap_init_args);
1031+
if (!shared_heap) {
1032+
printf("Create preallocated shared heap failed\n");
1033+
goto fail6;
1034+
}
1035+
1036+
/* attach module instance 2 to the shared heap */
1037+
if (!wasm_runtime_attach_shared_heap(wasm_module_inst, shared_heap)) {
1038+
printf("Attach shared heap failed.\n");
1039+
goto fail6;
1040+
}
1041+
}
1042+
#endif
1043+
10101044
ret = 0;
10111045
const char *exception = NULL;
10121046
if (is_repl_mode) {
@@ -1050,6 +1084,9 @@ main(int argc, char *argv[])
10501084
}
10511085
#endif
10521086

1087+
#if WASM_ENABLE_SHARED_HEAP != 0
1088+
fail6:
1089+
#endif
10531090
#if WASM_ENABLE_THREAD_MGR != 0
10541091
fail5:
10551092
#endif

0 commit comments

Comments
 (0)