Browse Source

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

TianlongLiang 5 months ago
parent
commit
b322e297c6
2 changed files with 44 additions and 0 deletions
  1. 7 0
      core/iwasm/common/wasm_memory.c
  2. 37 0
      product-mini/platforms/posix/main.c

+ 7 - 0
core/iwasm/common/wasm_memory.c

@@ -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 =
@@ -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);
@@ -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;
 }
 
@@ -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

+ 37 - 0
product-mini/platforms/posix/main.c

@@ -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);
@@ -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
@@ -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')
@@ -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) {
@@ -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