Skip to content

Commit ff7cbdd

Browse files
authored
Implement memory profiler, optimize memory usage, modify code indent (#35)
1 parent 9136abf commit ff7cbdd

File tree

18 files changed

+463
-106
lines changed

18 files changed

+463
-106
lines changed

core/iwasm/lib/native/base/timer_wrapper.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ void * thread_modulers_timer_check(void * arg)
5555
while (1) {
5656
ms_to_expiry = -1;
5757
vm_mutex_lock(&g_timer_ctx_list_mutex);
58-
timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
59-
&g_timer_ctx_list);
58+
timer_ctx_node_t* elem = (timer_ctx_node_t*)
59+
bh_list_first_elem(&g_timer_ctx_list);
6060
while (elem) {
6161
int next = check_app_timers(elem->timer_ctx);
6262
if (next != -1) {
@@ -72,7 +72,7 @@ void * thread_modulers_timer_check(void * arg)
7272
ms_to_expiry = 60 * 1000;
7373
vm_mutex_lock(&g_timer_ctx_list_mutex);
7474
vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
75-
ms_to_expiry);
75+
ms_to_expiry);
7676
vm_mutex_unlock(&g_timer_ctx_list_mutex);
7777
}
7878
}
@@ -94,20 +94,21 @@ void init_wasm_timer()
9494
vm_recursive_mutex_init(&g_timer_ctx_list_mutex);
9595

9696
vm_thread_create(&tm_tid, thread_modulers_timer_check,
97-
NULL,
98-
BH_APPLET_PRESERVED_STACK_SIZE);
97+
NULL, BH_APPLET_PRESERVED_STACK_SIZE);
9998
}
10099

101100
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
102101
{
103102
timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback,
104-
wakeup_modules_timer_thread, prealloc_num, module_id);
103+
wakeup_modules_timer_thread,
104+
prealloc_num,
105+
module_id);
105106

106107
if (ctx == NULL)
107108
return NULL;
108109

109-
timer_ctx_node_t * node = (timer_ctx_node_t*) bh_malloc(
110-
sizeof(timer_ctx_node_t));
110+
timer_ctx_node_t * node = (timer_ctx_node_t*)
111+
bh_malloc(sizeof(timer_ctx_node_t));
111112
if (node == NULL) {
112113
destroy_timer_ctx(ctx);
113114
return NULL;
@@ -125,8 +126,8 @@ timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
125126
void destory_module_timer_ctx(unsigned int module_id)
126127
{
127128
vm_mutex_lock(&g_timer_ctx_list_mutex);
128-
timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
129-
&g_timer_ctx_list);
129+
timer_ctx_node_t* elem = (timer_ctx_node_t*)
130+
bh_list_first_elem(&g_timer_ctx_list);
130131
while (elem) {
131132
if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
132133
bh_list_remove(&g_timer_ctx_list, elem);
@@ -151,7 +152,7 @@ timer_ctx_t get_wasm_timer_ctx()
151152
timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start)
152153
{
153154
return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
154-
auto_start);
155+
auto_start);
155156
}
156157

157158
void wasm_timer_destory(timer_id_t timer_id)

core/iwasm/lib/native/extension/sensor/sensor_mgr_ref.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,31 @@ void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
4040
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
4141
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
4242

43-
sensor_event_data_t *payload = (sensor_event_data_t*) bh_message_payload(
44-
msg);
43+
sensor_event_data_t *payload = (sensor_event_data_t*)
44+
bh_message_payload(msg);
4545
if (payload == NULL)
4646
return;
4747

4848
func_onSensorEvent = wasm_runtime_lookup_function(inst, "_on_sensor_event",
49-
"(i32i32i32)");
49+
"(i32i32i32)");
5050
if (!func_onSensorEvent) {
5151
printf("Cannot find function onRequest\n");
5252
} else {
5353
int32 sensor_data_offset;
5454
uint32 sensor_data_len;
5555

5656
if (payload->data_fmt == FMT_ATTR_CONTAINER) {
57-
sensor_data_len = attr_container_get_serialize_length(
58-
payload->data);
57+
sensor_data_len = attr_container_get_serialize_length(payload->data);
5958
} else {
6059
printf("Unsupported sensor data format: %d\n", payload->data_fmt);
6160
return;
6261
}
6362

6463
sensor_data_offset = wasm_runtime_module_dup_data(inst, payload->data,
65-
sensor_data_len);
64+
sensor_data_len);
6665
if (sensor_data_offset == 0) {
6766
printf("Got exception running wasm code: %s\n",
68-
wasm_runtime_get_exception(inst));
67+
wasm_runtime_get_exception(inst));
6968
wasm_runtime_clear_exception(inst);
7069
return;
7170
}
@@ -76,7 +75,7 @@ void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
7675

7776
if (!wasm_runtime_call_wasm(inst, NULL, func_onSensorEvent, 3, argv)) {
7877
printf(":Got exception running wasm code: %s\n",
79-
wasm_runtime_get_exception(inst));
78+
wasm_runtime_get_exception(inst));
8079
wasm_runtime_clear_exception(inst);
8180
wasm_runtime_module_free(inst, sensor_data_offset);
8281
return;
@@ -130,17 +129,16 @@ void init_sensor_framework()
130129

131130
// add the sys sensor objects
132131
add_sys_sensor("sensor_test", "This is a sensor for test", 0, 1000,
133-
read_test_sensor, config_test_sensor);
132+
read_test_sensor, config_test_sensor);
134133

135134
set_sensor_reshceduler(cb_wakeup_thread);
136135

137136
wasm_register_msg_callback(SENSOR_EVENT_WASM,
138-
app_mgr_sensor_event_callback);
137+
app_mgr_sensor_event_callback);
139138

140139
wasm_register_cleanup_callback(sensor_cleanup_callback);
141140

142141
vm_thread_create(&tid, (void *)thread_sensor_check, NULL,
143-
BH_APPLET_PRESERVED_STACK_SIZE);
144-
142+
BH_APPLET_PRESERVED_STACK_SIZE);
145143
}
146144

core/iwasm/runtime/vmcore-wasm/wasm.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ typedef struct WASMDataSeg {
219219
uint8 *data;
220220
} WASMDataSeg;
221221

222+
typedef struct BlockAddr {
223+
const uint8 *start_addr;
224+
uint8 *else_addr;
225+
uint8 *end_addr;
226+
} BlockAddr;
227+
228+
#define BLOCK_ADDR_CACHE_SIZE 64
229+
#define BLOCK_ADDR_CONFLICT_SIZE 4
230+
222231
typedef struct WASMModule {
223232
uint32 type_count;
224233
uint32 import_count;
@@ -252,7 +261,11 @@ typedef struct WASMModule {
252261
uint32 start_function;
253262

254263
HashMap *const_str_set;
264+
#if WASM_ENABLE_HASH_BLOCK_ADDR != 0
255265
HashMap *branch_set;
266+
#else
267+
BlockAddr block_addr_cache[BLOCK_ADDR_CACHE_SIZE][BLOCK_ADDR_CONFLICT_SIZE];
268+
#endif
256269
} WASMModule;
257270

258271
typedef struct WASMBranchBlock {

core/iwasm/runtime/vmcore-wasm/wasm_interp.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,12 @@ wasm_interp_call_func_bytecode(WASMThread *self,
760760
HANDLE_OP (WASM_OP_BLOCK):
761761
read_leb_uint32(frame_ip, frame_ip_end, block_ret_type);
762762

763-
if (!wasm_loader_find_block_addr(module->branch_set, frame_ip,
764-
frame_ip_end, BLOCK_TYPE_BLOCK,
763+
if (!wasm_loader_find_block_addr(module->module,
764+
frame_ip, frame_ip_end,
765+
BLOCK_TYPE_BLOCK,
765766
&else_addr, &end_addr,
766767
NULL, 0)) {
767-
wasm_runtime_set_exception(module, "wasm loader find block addr failed");
768+
wasm_runtime_set_exception(module, "find block addr failed");
768769
goto got_exception;
769770
}
770771

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

777-
if (!wasm_loader_find_block_addr(module->branch_set, frame_ip,
778-
frame_ip_end, BLOCK_TYPE_LOOP,
778+
if (!wasm_loader_find_block_addr(module->module,
779+
frame_ip, frame_ip_end,
780+
BLOCK_TYPE_LOOP,
779781
&else_addr, &end_addr,
780782
NULL, 0)) {
781-
wasm_runtime_set_exception(module, "wasm loader find block addr failed");
783+
wasm_runtime_set_exception(module, "find block addr failed");
782784
goto got_exception;
783785
}
784786

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

791-
if (!wasm_loader_find_block_addr(module->branch_set, frame_ip,
792-
frame_ip_end, BLOCK_TYPE_IF,
793+
if (!wasm_loader_find_block_addr(module->module,
794+
frame_ip, frame_ip_end,
795+
BLOCK_TYPE_IF,
793796
&else_addr, &end_addr,
794797
NULL, 0)) {
795-
wasm_runtime_set_exception(module, "wasm loader find block addr failed");
798+
wasm_runtime_set_exception(module, "find block addr failed");
796799
goto got_exception;
797800
}
798801

0 commit comments

Comments
 (0)