Skip to content
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

Shared heap #5

Closed
wants to merge 42 commits into from
Closed

Shared heap #5

wants to merge 42 commits into from

Conversation

WenLY1
Copy link
Owner

@WenLY1 WenLY1 commented Aug 14, 2024

No description provided.

And enable merged os_mmap for aot data and text sections except on
platform nuttx and esp-idf.

Fix issue that aarch64 AOT module fails to load on android:
bytecodealliance#2274
@WenLY1 WenLY1 force-pushed the shared_heap branch 6 times, most recently from d0e7615 to 72f4345 Compare August 15, 2024 05:54

#define addr_native_to_app(ptr) \
wasm_runtime_addr_native_to_app(module_inst, ptr)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From line 7 ~ line 23, this code has already in wasm_export.h, can it be deleted here?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@WenLY1 WenLY1 force-pushed the shared_heap branch 3 times, most recently from c53b434 to cbdcfa2 Compare August 20, 2024 02:21
Merge branch main into dev/merge_aot_data_text
@WenLY1 WenLY1 changed the base branch from main to dev_shared_heap August 22, 2024 01:36
Comment on lines 262 to 264
else ()
add_definitions (-DWASM_ENABLE_SHARED_HEAP=0)
message (" Shared heap disabled")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好把else分支去掉,不常用的feature一般不显示message,而core/config.h已经设置该宏为默认0,不需要再add_definitions

@@ -24,6 +24,10 @@ static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;

static mem_allocator_t pool_allocator = NULL;

#if WASM_ENABLE_SHARED_HEAP != 0
static WASMSharedHeap *shared_heap_list = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好考虑有个lock来保护它?比如static korp_mutex shared_heap_list_lock,然后在wasm_runtime_memory_init里面初始化,在wasm_runtime_memory_destroy里面销毁。

WASMSharedHeap *heap =
runtime_malloc(heap_struct_size, error_buf, error_buf_size);

if (!(heap = runtime_malloc(heap_struct_size, error_buf, error_buf_size))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重复runtime_malloc,L169已经调用了一次

goto fail2;
}

size = align_uint(size, os_getpagesize());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边要不要检查一下size,限制它的最大大小和最小大小

Copy link
Owner Author

@WenLY1 WenLY1 Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如何确认最大大小和最小大小,以什么为准呢

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以参考一下app heap,感觉和它设置一样就可以吧:
https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/config.h#L390-L395

else {
heap->next = shared_heap_list;
shared_heap_list = heap;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

将heap加到shared_heap_list最好加锁

@@ -173,6 +173,10 @@ static LLVMJITOptions llvm_jit_options = { 3, 3, 0, false };
static uint32 gc_heap_size_default = GC_HEAP_SIZE_DEFAULT;
#endif

#if WASM_ENABLE_SHARED_HEAP != 0
static uint32 shared_heap_size = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可能有多个shared heap,每个的大小都不一样,感觉这句以及下面的wasm_runtime_get_shared_heap_size等都不需要吧?由用户在每次wasm_runtime_create_shared_heap里面指定就好了

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasm_runtime_create_shared_heap这个接口会暴露给用户吗

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

会的,参考文档:

 - wasm_export.h
      struct WASMSharedHeap;
      typedef struct WASMSharedHeap wasm_shared_heap_t;
      export: wasm_runtime_create_shared_heap, wasm_runtime_destroy_shared_heap,
              wasm_rutnime_attach_shared_heap, wasm_runtime_detach_shared_heap
              wasm_runtime_shared_heap_malloc, wasm_runtime_shared_heap_free

比如先wasm_runtime_create_shared_heap, 然后把instance1, instance2, instance3 attach到它,
然后再wasm_runtime_create_shared_heap创建另一个shared heap, 然后把instance4, instance5, instance6 attach到它

Copy link
Owner Author

@WenLY1 WenLY1 Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

会的,参考文档:

 - wasm_export.h
      struct WASMSharedHeap;
      typedef struct WASMSharedHeap wasm_shared_heap_t;
      export: wasm_runtime_create_shared_heap, wasm_runtime_destroy_shared_heap,
              wasm_rutnime_attach_shared_heap, wasm_runtime_detach_shared_heap
              wasm_runtime_shared_heap_malloc, wasm_runtime_shared_heap_free

比如先wasm_runtime_create_shared_heap, 然后把instance1, instance2, instance3 attach到它, 然后再wasm_runtime_create_shared_heap创建另一个shared heap, 然后把instance4, instance5, instance6 attach到它

wasm_runtime_create_shared_heap/wasm_rutnime_attach_shared_heap/wasm_runtime_detach_shared_heap这几个API需要注册到wamr虚拟机吗,处理方式类似于shared_malloc/shared_free这两个接口吗

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些API是要加到wasm_export.h,类似其它API

Comment on lines 207 to 208
mem_alloc_type_t alloc_type;
MemAllocOption alloc_option;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像这两个field不需要用到

@@ -226,6 +242,9 @@ typedef struct RuntimeInitArgs {
uint32_t llvm_jit_size_level;
/* Segue optimization flags for LLVM JIT */
uint32_t segue_flags;

uint32_t shared_heap_size;
shared_heap_alloc_type_t shared_heap_alloc_type;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像这个field不需要用到

@@ -0,0 +1,50 @@
#include "bh_common.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一下license header

}

size = align_uint(size, os_getpagesize());
if (!(heap->base_addr = runtime_malloc(size, error_buf, error_buf_size))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在线性内存默认是用os_mmap分配的,感觉shared heap是不是也这样好点?改成调用wasm_mmap_linear_memory?

yamt and others added 2 commits August 22, 2024 12:35
Make wamrc normalize "arm64" to "aarch64v8". Previously the only way to
make the "arm64" target was to not specify a target on 64 bit arm-based
mac builds. Now arm64 and aarch64v8 are treated as the same.

Make aot_loader accept "aarch64v8" on arm-based apple (as well as
accepting legacy "arm64" based aot targets).

This also removes __APPLE__ and __MACH__ from the block that defaults
size_level to 1 since it doesn't seem to be supported for aarch64:
`LLVM ERROR: Only small, tiny and large code models are allowed on AArch64`
no1wudi and others added 8 commits August 28, 2024 16:05
…3756)

Fix the compilation error of this CI:
https://github.com/bytecodealliance/wasm-micro-runtime/actions/runs/10575515238

```
/__w/wasm-micro-runtime/wasm-micro-runtime/bloaty/third_party/abseil-cpp/absl/debugging/failure_signal_handler.cc:139:32: error: no matching function for call to 'max(long int, int)'
  139 |   size_t stack_size = (std::max(SIGSTKSZ, 65536) + page_mask) & ~page_mask;
      |                        ~~~~~~~~^~~~~~~~~~~~~~~~~
```
…rge_aot_data_text

Merge branch dev/merge_aot_data_text into main to keep the commit history.
…ck is enabled (bytecodealliance#3754)

In the AOT compiler, allow the user to control stack boundary check when the boundary
check is enabled (e.g. `wamrc --bounds-checks=1`). Now the code logic is:

1. When `--stack-bounds-checks` is not set, it will be the same value as `--bounds-checks`.
2. When `--stack-bounds-checks` is set, it will be the option value no matter what the
    status of `--bounds-checks` is.
…64 or riscv64 (bytecodealliance#3755)

Mac on aarch64 uses posix_memmap.c os_mmap which doesn't do anything with
the flag MMAP_MAP_32BIT for that build so this condition ends up asserting unless
the mapping ends up in the first 4 gigs worth of addressable space.

Thsi PR changes to call os_mmap with MMAP_MAP_32BIT flag only when the target
is x86-64 or riscv64, and the macro __APPLE__ isn't enabled. The behavior is similar
to what the posix os_mmap does.
The specific commit has been deleted, I am pointing to the same commit in the main branch though.
- Only retry on EAGAIN, ENOMEM or EINTR.
- On EINTR, don't count it against the retry budget, just keep retrying.
  EINTR can happen in bursts.
- Log the errno on failure, and don't conditionalize that logging on
  BH_ENABLE_TRACE_MMAP. In other parts of the code, error logging is not
  conditional on that define, while turning on that tracing define makes
  things overly verbose.
}

if (shared_heap_list == NULL) {
os_mutex_lock(&shared_heap_list_lock);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议把os_mutex_lock/unlock提到if () {..} else {..}的外面,减少点代码?

{
WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst;
uint32 linear_mem_size = 0;
WASMMemoryInstance *memory = NULL;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这行后面最好加一行空格,代码风格好点。

uint32 linear_mem_size = 0;
WASMMemoryInstance *memory = NULL;
if (wasm_module_inst->e->shared_heap
&& wasm_module_inst->e->shared_heap != heap) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果wasm_module_inst->e->shared_heap == heap,要吗直接返回false,要吗直接返回true, 不需要再进行后面操作。似乎返回true合理点?这边代码改成:

    if (wasm_module_inst->e->shared_heap) {
        if (wasm_module_inst->e->shared_heap != heap) {
            LOG_WARNING("A shared heap is already attached");
            return false;
        }
        return true;
    }

// check if linear memory and shared heap are overlapped

memory = wasm_get_default_memory(wasm_module_inst);
linear_mem_size += memory->num_bytes_per_page * memory->cur_page_count;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linear_mem_size +=改成linear_mem_size =好点,否则代码怪怪的。
另外可以 linear_mem_size = memory->memory_data_size;

WASMSharedHeap *heap)
{
WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst;
uint32 linear_mem_size = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uint32 -> uint64,即使memory32,大小也可能是4G = UINT32_MAX + 1,需要用64位表示

WASMSharedHeap *heap = module_inst->e->shared_heap;

if (heap && heap->base_addr && addr + bytes <= heap->base_addr + heap->size
&& addr + bytes >= heap->base_addr) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addr + bytes可能integer overflow。似乎:

addr >= heap->base_addr && addr + bytes <= heap->base_addr + heap->size && addr + bytes > addr

好些?

uint64 ptr);

void
wasm_runtime_shared_heap_destroy();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉这个,不需要export它

WASMSharedHeap *heap =
wasm_runtime_create_shared_heap(&args, error_buf, error_buf_size);
wasm_runtime_attach_shared_heap((WASMModuleInstanceCommon *)module_inst,
heap);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要在这里create并attach shared heap,去掉这部分代码

void **p_native_addr)
{
return wasm_module_shared_malloc_internal(module_inst, NULL, size,
p_native_addr);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasm_memory.c已经定义了wasm_runtime_shared_heap_malloc了,按理调用它就可以了

void
wasm_module_shared_free(WASMModuleInstance *module_inst, uint64 ptr)
{
wasm_module_shared_free_internal(module_inst, NULL, ptr);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

loganek and others added 3 commits September 10, 2024 09:05
- Implement TINY / STANDARD frame modes - tiny mode is only able to keep track on the IP
  and func idx, STANDARD mode provides more capabilities (parameters, stack pointer etc.).
- Implement FRAME_PER_FUNCTION / FRAME_PER_CALL modes - frame per function adds
  code at the beginning and at the end of each function for allocating / deallocating stack frame,
  whereas in per-call mode the frame is allocated before each call. The exception is call to
  the imported function, where frame-per-function mode also allocates the stack before the
  `call` instruction (as it can't instrument the imported function).

At the moment TINY + FRAME_PER_FUNCTION is automatically enabled in case GC and perf
profiling are disabled and `values` call stack feature is not requested. In all the other cases
STANDARD + FRAME_PER_CALL is used.

STANDARD + FRAME_PER_FUNCTION and TINY + FRAME_PER_CALL are currently not
implemented but possible, and might be enabled in the future.

ps. bytecodealliance#3758
if (module_inst->module_type == Wasm_Module_Bytecode) {
if (((WASMModuleInstance *)module_inst)->e->shared_heap) {
LOG_WARNING("A shared heap is already attached");
return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be return false

}

bool
wasm_runtime_detach_shared_heap(WASMModuleInstanceCommon *module_inst)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉wasm_runtime_detach_shared_heap, wasm_runtime_detach_shared_heap_internal和wasm_cluster_detach_shared_heap都做成返回值就可以,如果下面的wasm_runtime_detach_shared_heap_internal只是把module_inst的shared heap句柄设成NULL的话。
否则这边都是直接return true,不大需要。

{
WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
|| module_inst_comm->module_type == Wasm_Module_AoT);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果上面is_app_addr_in_shared_heap没有bh_assert,这边也不assert吧,并且取heap的代码和上面最好一致(区分interpreter module inst和aot module inst)

return NULL;
}

if (is_app_addr_in_shared_heap(module_inst, memory->is_memory64, ptr, 1)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面的shared_heap_addr_native_to_app没有检查native addr有没有在shared heap里面,这边最好也去掉?

#if WASM_ENABLE_SHARED_HEAP != 0
typedef struct SharedHeapInitArgs {
uint32 size;
} SharedHeapInitArgs;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个定义需要放在wasm_export.h,这边不保留
另外几个函数也要在wasm_export.h声明出来(参考其它函数),这个文件的声明也保留:

wasm_runtime_create_shared_heap
wasm_runtime_attach_shared_heap
wasm_runtime_detach_shared_heap
wasm_runtime_shared_heap_malloc
wasm_runtime_shared_heap_free

// TODO
#endif
}
#endif

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把这个文件的改动以及wasm_runtime_common.h的改动删了吧,shared_heap_wrapper.c里面直接调用wasm_runtime_shared_heap_malloc和wasm_runtime_shared_heap_free就可以

I'm not sure we want to use C99 %tu here.
While C99 %zu is more widely used in WAMR, %tu is rare (if any)
and I'm not sure if it's ubiquitously implemented in platforms
we support.
@@ -3164,6 +3164,9 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
of current module instance after it is deinstantiated. */
wasm_exec_env_destroy(module_inst->exec_env_singleton);
}
#if WASM_ENABLE_SHARED_HEAP != 0
wasm_runtime_detach_shared_heap((WASMModuleInstanceCommon *)module_inst);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边也可以不调用,因为detach也只是把module_inst->e->shared_heap指针设置NULL,并没有释放内存等动作。整个module_inst都被free掉,设置它的field也没意义。

@@ -3233,6 +3233,7 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
#if WASM_ENABLE_APP_FRAMEWORK == 0
max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
#endif

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要的改动,最好改回去

void *heap_handle;
uint8_t *base_addr;
uint32_t size;
struct WASMSharedHeap *next;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一句可以作为第一个field放到第一行吗?

The definition of os_get_invalid_handle in platform_internal.h did not
match the declaration in platform_api_extension.h.
*/
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_attach_shared_heap(wasm_module_inst_t module_inst,
void *shared_heap);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好将void *shared_heap 改成 wasm_shared_heap_t shared_heap
同时内部wasm_runtime_attach_shared_heap_internal, wasm_cluster_attach_shared_heap等也改一下

{
#if WASM_ENABLE_THREAD_MGR != 0
wasm_cluster_attach_shared_heap(module_inst, shared_heap);
return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该是return wasm_cluster_attach_shared_heap(module_inst, shared_heap);

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasm_cluster_attach_shared_heap这个函数内部调用traverse_list遍历所有的shared heap,traverse_list函数是没有返回值的,wasm_cluster_attach_shared_heap这个函数直接返回true可以吗

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要不先判断一下当前这个module_inst有没有已经attach了shared heap,有的话return false。否则wasm_cluster_attach_shared_heap并返回true?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要不先判断一下当前这个module_inst有没有已经attach了shared heap,有的话return false。否则wasm_cluster_attach_shared_heap并返回true?

嗯,把这个逻辑放到wasm_cluster_attach_shared_heap函数中了


bool
wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
void *shared_heap)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好把void *改成WASMSharedHeap *

}
}

bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好加static

return false;
}

bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好加static

loganek and others added 16 commits September 11, 2024 16:08
…dealliance#3785)

Also add a script that converts instruction pointers to function indexes (or function names).

bytecodealliance#3758
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
@WenLY1 WenLY1 deleted the branch dev_shared_heap September 27, 2024 08:59
@WenLY1 WenLY1 closed this Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.