Re-org memory allocation interfaces, add --stack-size and --heap-size option (#193)

This commit is contained in:
wenyongh
2020-03-10 19:54:44 +08:00
committed by GitHub
parent 381859d530
commit 0fdd49ea31
110 changed files with 1264 additions and 2125 deletions

View File

@ -5,6 +5,9 @@ set (IWASM_COMMON_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories (${IWASM_COMMON_DIR})
add_definitions(-DBH_MALLOC=wasm_runtime_malloc)
add_definitions(-DBH_FREE=wasm_runtime_free)
file (GLOB c_source_all ${IWASM_COMMON_DIR}/*.c)
if (${WAMR_BUILD_TARGET} STREQUAL "X86_64" OR ${WAMR_BUILD_TARGET} STREQUAL "AMD_64")

View File

@ -4,7 +4,6 @@
*/
#include "wasm_exec_env.h"
#include "bh_memory.h"
#include "wasm_runtime_common.h"
WASMExecEnv *
@ -16,14 +15,14 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
WASMExecEnv *exec_env;
if (total_size >= UINT32_MAX
|| !(exec_env = wasm_malloc((uint32)total_size)))
|| !(exec_env = wasm_runtime_malloc((uint32)total_size)))
return NULL;
memset(exec_env, 0, (uint32)total_size);
#if WASM_ENABLE_AOT != 0
if (!(exec_env->argv_buf = wasm_malloc(sizeof(uint32) * 64))) {
wasm_free(exec_env);
if (!(exec_env->argv_buf = wasm_runtime_malloc(sizeof(uint32) * 64))) {
wasm_runtime_free(exec_env);
return NULL;
}
#endif
@ -40,9 +39,9 @@ void
wasm_exec_env_destroy(WASMExecEnv *exec_env)
{
#if WASM_ENABLE_AOT != 0
wasm_free(exec_env->argv_buf);
wasm_runtime_free(exec_env->argv_buf);
#endif
wasm_free(exec_env);
wasm_runtime_free(exec_env);
}
WASMModuleInstanceCommon *

View File

@ -0,0 +1,372 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_runtime_common.h"
#include "bh_platform.h"
#include "mem_alloc.h"
#include "bh_thread.h"
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
/* Memory profile data of a function */
typedef struct memory_profile {
struct memory_profile *next;
const char *function_name;
const char *file_name;
int line_in_file;
int malloc_num;
int free_num;
int total_malloc;
int total_free;
} memory_profile_t;
/* Memory in use which grows when BH_MALLOC was called
* and decreases when bh_free was called */
static unsigned int memory_in_use = 0;
/* Memory profile data list */
static memory_profile_t *memory_profiles_list = NULL;
/* Lock of the memory profile list */
static korp_mutex profile_lock;
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#ifndef MALLOC_MEMORY_FROM_SYSTEM
typedef enum Memory_Mode {
MEMORY_MODE_UNKNOWN = 0,
MEMORY_MODE_POOL,
MEMORY_MODE_ALLOCATOR
} Memory_Mode;
static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
static mem_allocator_t pool_allocator = NULL;
static void *(*malloc_func)(unsigned int size) = NULL;
static void *(*realloc_func)(void *ptr, unsigned int size) = NULL;
static void (*free_func)(void *ptr) = NULL;
static unsigned int global_pool_size;
static bool
wasm_memory_init_with_pool(void *mem, unsigned int bytes)
{
mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
if (_allocator) {
memory_mode = MEMORY_MODE_POOL;
pool_allocator = _allocator;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_init(&profile_lock);
#endif
global_pool_size = bytes;
return true;
}
bh_printf("Init memory with pool (%p, %u) failed.\n", mem, bytes);
return false;
}
static bool
wasm_memory_init_with_allocator(void *_malloc_func,
void *_realloc_func,
void *_free_func)
{
if (_malloc_func && _free_func && _malloc_func != _free_func) {
memory_mode = MEMORY_MODE_ALLOCATOR;
malloc_func = _malloc_func;
realloc_func = _realloc_func;
free_func = _free_func;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_init(&profile_lock);
#endif
return true;
}
bh_printf("Init memory with allocator (%p, %p, %p) failed.\n",
_malloc_func, _realloc_func, _free_func);
return false;
}
bool
wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
const MemAllocOption *alloc_option)
{
if (mem_alloc_type == Alloc_With_Pool)
return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
alloc_option->pool.heap_size);
else if (mem_alloc_type == Alloc_With_Allocator)
return wasm_memory_init_with_allocator(alloc_option->allocator.malloc_func,
alloc_option->allocator.realloc_func,
alloc_option->allocator.free_func);
else if (mem_alloc_type == Alloc_With_System_Allocator)
return wasm_memory_init_with_allocator(os_malloc, os_realloc, os_free);
else
return false;
}
void
wasm_runtime_memory_destroy()
{
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_destroy(&profile_lock);
#endif
if (memory_mode == MEMORY_MODE_POOL)
mem_allocator_destroy(pool_allocator);
memory_mode = MEMORY_MODE_UNKNOWN;
}
unsigned
wasm_runtime_memory_pool_size()
{
if (memory_mode == MEMORY_MODE_POOL)
return global_pool_size;
else
return 1 * BH_GB;
}
void *
wasm_runtime_malloc(unsigned int size)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
bh_printf("wasm_runtime_malloc failed: memory hasn't been initialize.\n");
return NULL;
} else if (memory_mode == MEMORY_MODE_POOL) {
return mem_allocator_malloc(pool_allocator, size);
} else {
return malloc_func(size);
}
}
void *
wasm_runtime_realloc(void *ptr, unsigned int size)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
bh_printf("wasm_runtime_realloc failed: memory hasn't been initialize.\n");
return NULL;
} else if (memory_mode == MEMORY_MODE_POOL) {
return mem_allocator_realloc(pool_allocator, ptr, size);
} else {
if (realloc_func)
return realloc_func(ptr, size);
else
return NULL;
}
}
void
wasm_runtime_free(void *ptr)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
bh_printf("wasm_runtime_free failed: memory hasn't been initialize.\n");
} else if (memory_mode == MEMORY_MODE_POOL) {
mem_allocator_free(pool_allocator, ptr);
} else {
free_func(ptr);
}
}
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
void *
wasm_runtime_malloc_profile(const char *file, int line,
const char *func, unsigned int size)
{
void *p = wasm_rutime_malloc(size + 8);
if (p) {
memory_profile_t *profile;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
if (strcmp(profile->function_name, func) == 0
&& strcmp(profile->file_name, file) == 0) {
break;
}
profile = profile->next;
}
if (profile) {
profile->total_malloc += size;/* TODO: overflow check */
profile->malloc_num++;
} else {
profile = wasm_runtime_malloc(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
bh_memcpy_s(p, size + 8, &size, sizeof(size));
return (char *)p + 8;
}
memset(profile, 0, sizeof(memory_profile_t));
profile->file_name = file;
profile->line_in_file = line;
profile->function_name = func;
profile->malloc_num = 1;
profile->total_malloc = size;
profile->next = memory_profiles_list;
memory_profiles_list = profile;
}
vm_mutex_unlock(&profile_lock);
bh_memcpy_s(p, size + 8, &size, sizeof(size));
memory_in_use += size;
memory_profile_print(file, line, func, size);
return (char *)p + 8;
}
return NULL;
}
void
wasm_runtime_free_profile(const char *file, int line,
const char *func, void *ptr)
{
unsigned int size = *(unsigned int *)((char *)ptr - 8);
memory_profile_t *profile;
wasm_runtime_free((char *)ptr - 8);
if (memory_in_use >= size)
memory_in_use -= size;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
if (strcmp(profile->function_name, func) == 0
&& strcmp(profile->file_name, file) == 0) {
break;
}
profile = profile->next;
}
if (profile) {
profile->total_free += size;/* TODO: overflow check */
profile->free_num++;
} else {
profile = wasm_runtime_malloc(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
return;
}
memset(profile, 0, sizeof(memory_profile_t));
profile->file_name = file;
profile->line_in_file = line;
profile->function_name = func;
profile->free_num = 1;
profile->total_free = size;
profile->next = memory_profiles_list;
memory_profiles_list = profile;
}
vm_mutex_unlock(&profile_lock);
}
/**
* Summarize memory usage and print it out
* Can use awk to analyze the output like below:
* awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
*/
void memory_usage_summarize()
{
memory_profile_t *profile;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
bh_printf("malloc:%d:malloc_num:%d:free:%d:free_num:%d:%s\n",
profile->total_malloc,
profile->malloc_num,
profile->total_free,
profile->free_num,
profile->function_name);
profile = profile->next;
}
vm_mutex_unlock(&profile_lock);
}
void
memory_profile_print(const char *file, int line,
const char *func, int alloc)
{
bh_printf("location:%s@%d:used:%d:contribution:%d\n",
func, line, memory_in_use, alloc);
}
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#else /* else of MALLOC_MEMORY_FROM_SYSTEM */
void *
wasm_runtime_malloc(unsigned int size)
{
return malloc(size);
}
void *
wasm_runtime_realloc(void *ptr, unsigned int size)
{
return realloc(ptr, size);
}
void
wasm_runtime_free(void *ptr)
{
if (ptr)
free(ptr);
}
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
void *
wasm_runtime_malloc_profile(const char *file, int line,
const char *func, unsigned int size)
{
(void)file;
(void)line;
(void)func;
(void)memory_profiles_list;
(void)profile_lock;
(void)memory_in_use;
return malloc(size);
}
void *
wasm_runtime_realloc_profile(const char *file, int line,
const char *func, void *ptr, unsigned int size)
{
(void)file;
(void)line;
(void)func;
(void)memory_profiles_list;
(void)profile_lock;
(void)memory_in_use;
return realloc(ptr, size);
}
void
wasm_runtime_free_profile(const char *file, int line,
const char *func, void *ptr)
{
(void)file;
(void)line;
(void)func;
if (ptr)
free(ptr);
}
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/

View File

@ -0,0 +1,28 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _WASM_MEMORY_H
#define _WASM_MEMORY_H
#include "bh_common.h"
#include "../include/wasm_export.h"
#ifdef __cplusplus
extern "C" {
#endif
bool
wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
const MemAllocOption *alloc_option);
void
wasm_runtime_memory_destroy();
#ifdef __cplusplus
}
#endif
#endif /* end of _WASM_MEMORY_H */

View File

@ -184,7 +184,7 @@ wasm_native_register_natives(const char *module_name,
{
NativeSymbolsNode *node;
if (!(node = bh_malloc(sizeof(NativeSymbolsNode))))
if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
return false;
node->module_name = module_name;
@ -256,7 +256,7 @@ wasm_native_destroy()
node = g_native_symbols_list;
while (node) {
node_next = node->next;
bh_free(node);
wasm_runtime_free(node);
node = node_next;
}

View File

@ -9,6 +9,7 @@
#include "bh_assert.h"
#include "bh_log.h"
#include "wasm_runtime_common.h"
#include "wasm_memory.h"
#if WASM_ENABLE_INTERP != 0
#include "../interpreter/wasm_runtime.h"
#endif
@ -23,8 +24,8 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
snprintf(error_buf, error_buf_size, "%s", string);
}
bool
wasm_runtime_init()
static bool
wasm_runtime_env_init()
{
if (bh_platform_init() != 0)
return false;
@ -35,8 +36,20 @@ wasm_runtime_init()
if (vm_thread_sys_init() != 0)
return false;
if (wasm_native_init() == false) {
wasm_runtime_destroy();
if (wasm_native_init() == false)
return false;
return true;
}
bool
wasm_runtime_init()
{
if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
return false;
if (!wasm_runtime_env_init()) {
wasm_runtime_memory_destroy();
return false;
}
@ -48,56 +61,30 @@ wasm_runtime_destroy()
{
wasm_native_destroy();
vm_thread_sys_destroy();
wasm_runtime_memory_destroy();
}
int bh_memory_init_with_allocator_internal(void *_malloc_func,
void *_realloc_func,
void *_free_func);
bool
wasm_runtime_full_init(RuntimeInitArgs *init_args)
{
if (init_args->mem_alloc_type == Alloc_With_Pool) {
void *heap_buf = init_args->mem_alloc.pool.heap_buf;
uint32 heap_size = init_args->mem_alloc.pool.heap_size;
if (bh_memory_init_with_pool(heap_buf, heap_size) != 0)
return false;
}
else if (init_args->mem_alloc_type == Alloc_With_Allocator) {
void *malloc_func = init_args->mem_alloc.allocator.malloc_func;
void *realloc_func = init_args->mem_alloc.allocator.realloc_func;
void *free_func = init_args->mem_alloc.allocator.free_func;
if (bh_memory_init_with_allocator_internal(malloc_func,
realloc_func,
free_func) != 0)
return false;
}
else
if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
&init_args->mem_alloc_option))
return false;
if (!wasm_runtime_init())
goto fail1;
if (!wasm_runtime_env_init()) {
wasm_runtime_memory_destroy();
return false;
}
if (init_args->n_native_symbols > 0
&& !wasm_runtime_register_natives(init_args->native_module_name,
init_args->native_symbols,
init_args->n_native_symbols))
goto fail2;
init_args->n_native_symbols)) {
wasm_runtime_destroy();
return false;
}
return true;
fail2:
wasm_runtime_destroy();
fail1:
bh_memory_destroy();
return false;
}
void
wasm_runtime_full_destroy()
{
wasm_runtime_destroy();
bh_memory_destroy();
}
PackageType
@ -723,7 +710,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
uint64 total_size;
uint32 i;
if (!(wasi_ctx = wasm_malloc(sizeof(WASIContext)))) {
if (!(wasi_ctx = wasm_runtime_malloc(sizeof(WASIContext)))) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: allocate memory failed.");
return false;
@ -952,7 +939,7 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
fd_table_destroy(wasi_ctx->curfds);
if (wasi_ctx->prestats)
fd_prestats_destroy(wasi_ctx->prestats);
bh_free(wasi_ctx);
wasm_runtime_free(wasi_ctx);
}
}
@ -1272,7 +1259,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
total_size = sizeof(uint32) * (uint64)(argc1 > 2 ? argc1 : 2);
if (total_size >= UINT32_MAX
|| (!(argv1 = wasm_malloc((uint32)total_size)))) {
|| (!(argv1 = wasm_runtime_malloc((uint32)total_size)))) {
wasm_runtime_set_exception(module_inst, "allocate memory failed.");
goto fail;
}
@ -1417,12 +1404,12 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
}
bh_printf("\n");
wasm_free(argv1);
wasm_runtime_free(argv1);
return true;
fail:
if (argv1)
wasm_free(argv1);
wasm_runtime_free(argv1);
exception = wasm_runtime_get_exception(module_inst);
bh_assert(exception);
@ -1551,7 +1538,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
size = sizeof(uint32) * (uint32)argc1;
if (size >= UINT32_MAX
|| !(argv1 = wasm_malloc((uint32)size))) {
|| !(argv1 = wasm_runtime_malloc((uint32)size))) {
wasm_runtime_set_exception(exec_env->module_inst,
"allocate memory failed.");
return false;
@ -1679,7 +1666,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
wasm_runtime_free(argv1);
return ret;
}
#endif /* end of defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) */
@ -1726,7 +1713,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
size = sizeof(uint32) * (uint64)argc1;
if (size >= UINT_MAX
|| !(argv1 = wasm_malloc((uint32)size))) {
|| !(argv1 = wasm_runtime_malloc((uint32)size))) {
wasm_runtime_set_exception(exec_env->module_inst,
"allocate memory failed.");
return false;
@ -1819,7 +1806,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
wasm_runtime_free(argv1);
return ret;
}
@ -1874,7 +1861,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
size = sizeof(uint64) * (uint64)argc1;
if (size >= UINT32_MAX
|| !(argv1 = wasm_malloc((uint32)size))) {
|| !(argv1 = wasm_runtime_malloc((uint32)size))) {
wasm_runtime_set_exception(exec_env->module_inst,
"allocate memory failed.");
return false;
@ -1976,7 +1963,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
ret = true;
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
wasm_runtime_free(argv1);
return ret;
}

View File

@ -22,9 +22,6 @@
extern "C" {
#endif
#define wasm_malloc bh_malloc
#define wasm_realloc bh_realloc
#define wasm_free bh_free
typedef struct WASMModuleCommon {
/* Module type, for module loaded from WASM bytecode binary,
@ -63,17 +60,13 @@ typedef wasm_section_t WASMSection, AOTSection;
bool
wasm_runtime_init();
/* See wasm_export.h for description */
void
wasm_runtime_destroy();
/* See wasm_export.h for description */
bool
wasm_runtime_full_init(RuntimeInitArgs *init_args);
/* See wasm_export.h for description */
void
wasm_runtime_full_destroy();
wasm_runtime_destroy();
/* See wasm_export.h for description */
PackageType