Skip to content

Commit

Permalink
Implement memory profiler, optimize memory usage, modify code indent
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyongh committed May 23, 2019
1 parent b5e21ea commit d9c98f6
Show file tree
Hide file tree
Showing 18 changed files with 463 additions and 106 deletions.
23 changes: 12 additions & 11 deletions core/iwasm/lib/native/base/timer_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void * thread_modulers_timer_check(void * arg)
while (1) {
ms_to_expiry = -1;
vm_mutex_lock(&g_timer_ctx_list_mutex);
timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
&g_timer_ctx_list);
timer_ctx_node_t* elem = (timer_ctx_node_t*)
bh_list_first_elem(&g_timer_ctx_list);
while (elem) {
int next = check_app_timers(elem->timer_ctx);
if (next != -1) {
Expand All @@ -72,7 +72,7 @@ void * thread_modulers_timer_check(void * arg)
ms_to_expiry = 60 * 1000;
vm_mutex_lock(&g_timer_ctx_list_mutex);
vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
ms_to_expiry);
ms_to_expiry);
vm_mutex_unlock(&g_timer_ctx_list_mutex);
}
}
Expand All @@ -94,20 +94,21 @@ void init_wasm_timer()
vm_recursive_mutex_init(&g_timer_ctx_list_mutex);

vm_thread_create(&tm_tid, thread_modulers_timer_check,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
NULL, BH_APPLET_PRESERVED_STACK_SIZE);
}

timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
{
timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback,
wakeup_modules_timer_thread, prealloc_num, module_id);
wakeup_modules_timer_thread,
prealloc_num,
module_id);

if (ctx == NULL)
return NULL;

timer_ctx_node_t * node = (timer_ctx_node_t*) bh_malloc(
sizeof(timer_ctx_node_t));
timer_ctx_node_t * node = (timer_ctx_node_t*)
bh_malloc(sizeof(timer_ctx_node_t));
if (node == NULL) {
destroy_timer_ctx(ctx);
return NULL;
Expand All @@ -125,8 +126,8 @@ timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
void destory_module_timer_ctx(unsigned int module_id)
{
vm_mutex_lock(&g_timer_ctx_list_mutex);
timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
&g_timer_ctx_list);
timer_ctx_node_t* elem = (timer_ctx_node_t*)
bh_list_first_elem(&g_timer_ctx_list);
while (elem) {
if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
bh_list_remove(&g_timer_ctx_list, elem);
Expand All @@ -151,7 +152,7 @@ timer_ctx_t get_wasm_timer_ctx()
timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start)
{
return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
auto_start);
auto_start);
}

void wasm_timer_destory(timer_id_t timer_id)
Expand Down
22 changes: 10 additions & 12 deletions core/iwasm/lib/native/extension/sensor/sensor_mgr_ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,31 @@ void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;

sensor_event_data_t *payload = (sensor_event_data_t*) bh_message_payload(
msg);
sensor_event_data_t *payload = (sensor_event_data_t*)
bh_message_payload(msg);
if (payload == NULL)
return;

func_onSensorEvent = wasm_runtime_lookup_function(inst, "_on_sensor_event",
"(i32i32i32)");
"(i32i32i32)");
if (!func_onSensorEvent) {
printf("Cannot find function onRequest\n");
} else {
int32 sensor_data_offset;
uint32 sensor_data_len;

if (payload->data_fmt == FMT_ATTR_CONTAINER) {
sensor_data_len = attr_container_get_serialize_length(
payload->data);
sensor_data_len = attr_container_get_serialize_length(payload->data);
} else {
printf("Unsupported sensor data format: %d\n", payload->data_fmt);
return;
}

sensor_data_offset = wasm_runtime_module_dup_data(inst, payload->data,
sensor_data_len);
sensor_data_len);
if (sensor_data_offset == 0) {
printf("Got exception running wasm code: %s\n",
wasm_runtime_get_exception(inst));
wasm_runtime_get_exception(inst));
wasm_runtime_clear_exception(inst);
return;
}
Expand All @@ -76,7 +75,7 @@ void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)

if (!wasm_runtime_call_wasm(inst, NULL, func_onSensorEvent, 3, argv)) {
printf(":Got exception running wasm code: %s\n",
wasm_runtime_get_exception(inst));
wasm_runtime_get_exception(inst));
wasm_runtime_clear_exception(inst);
wasm_runtime_module_free(inst, sensor_data_offset);
return;
Expand Down Expand Up @@ -130,17 +129,16 @@ void init_sensor_framework()

// add the sys sensor objects
add_sys_sensor("sensor_test", "This is a sensor for test", 0, 1000,
read_test_sensor, config_test_sensor);
read_test_sensor, config_test_sensor);

set_sensor_reshceduler(cb_wakeup_thread);

wasm_register_msg_callback(SENSOR_EVENT_WASM,
app_mgr_sensor_event_callback);
app_mgr_sensor_event_callback);

wasm_register_cleanup_callback(sensor_cleanup_callback);

vm_thread_create(&tid, (void *)thread_sensor_check, NULL,
BH_APPLET_PRESERVED_STACK_SIZE);

BH_APPLET_PRESERVED_STACK_SIZE);
}

13 changes: 13 additions & 0 deletions core/iwasm/runtime/vmcore-wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ typedef struct WASMDataSeg {
uint8 *data;
} WASMDataSeg;

typedef struct BlockAddr {
const uint8 *start_addr;
uint8 *else_addr;
uint8 *end_addr;
} BlockAddr;

#define BLOCK_ADDR_CACHE_SIZE 64
#define BLOCK_ADDR_CONFLICT_SIZE 4

typedef struct WASMModule {
uint32 type_count;
uint32 import_count;
Expand Down Expand Up @@ -252,7 +261,11 @@ typedef struct WASMModule {
uint32 start_function;

HashMap *const_str_set;
#if WASM_ENABLE_HASH_BLOCK_ADDR != 0
HashMap *branch_set;
#else
BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
#endif
} WASMModule;

typedef struct WASMBranchBlock {
Expand Down
21 changes: 12 additions & 9 deletions core/iwasm/runtime/vmcore-wasm/wasm_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,11 +760,12 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP (WASM_OP_BLOCK):
read_leb_uint32(frame_ip, frame_ip_end, block_ret_type);

if (!wasm_loader_find_block_addr(module->branch_set, frame_ip,
frame_ip_end, BLOCK_TYPE_BLOCK,
if (!wasm_loader_find_block_addr(module->module,
frame_ip, frame_ip_end,
BLOCK_TYPE_BLOCK,
&else_addr, &end_addr,
NULL, 0)) {
wasm_runtime_set_exception(module, "wasm loader find block addr failed");
wasm_runtime_set_exception(module, "find block addr failed");
goto got_exception;
}

Expand All @@ -774,11 +775,12 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP (WASM_OP_LOOP):
read_leb_uint32(frame_ip, frame_ip_end, block_ret_type);

if (!wasm_loader_find_block_addr(module->branch_set, frame_ip,
frame_ip_end, BLOCK_TYPE_LOOP,
if (!wasm_loader_find_block_addr(module->module,
frame_ip, frame_ip_end,
BLOCK_TYPE_LOOP,
&else_addr, &end_addr,
NULL, 0)) {
wasm_runtime_set_exception(module, "wasm loader find block addr failed");
wasm_runtime_set_exception(module, "find block addr failed");
goto got_exception;
}

Expand All @@ -788,11 +790,12 @@ wasm_interp_call_func_bytecode(WASMThread *self,
HANDLE_OP (WASM_OP_IF):
read_leb_uint32(frame_ip, frame_ip_end, block_ret_type);

if (!wasm_loader_find_block_addr(module->branch_set, frame_ip,
frame_ip_end, BLOCK_TYPE_IF,
if (!wasm_loader_find_block_addr(module->module,
frame_ip, frame_ip_end,
BLOCK_TYPE_IF,
&else_addr, &end_addr,
NULL, 0)) {
wasm_runtime_set_exception(module, "wasm loader find block addr failed");
wasm_runtime_set_exception(module, "find block addr failed");
goto got_exception;
}

Expand Down
Loading

0 comments on commit d9c98f6

Please sign in to comment.