Skip to content

Commit 389d206

Browse files
Enhance memory profiling with structured logging support (#4896)
* Enhance memory profiling with structured logging support - Enable memory profiling and structured logging in CMake configuration. - Update memory tracing definitions to support custom sections. - Replace os_printf with MEM_PROF_PRINTF for memory-related outputs. - Add verbose logging for memory consumption in various modules.
1 parent f5a1c39 commit 389d206

File tree

13 files changed

+115
-59
lines changed

13 files changed

+115
-59
lines changed

build-scripts/SConscript_config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ if GetDepend(['WAMR_BUILD_MEMORY_PROFILING']):
103103
CPPDEFINES += ['WASM_ENABLE_MEMORY_PROFILING=1']
104104
print('[WAMR] Memory profiling enabled')
105105

106+
if GetDepend(['WAMR_BUILD_MEMORY_TRACING']):
107+
CPPDEFINES += ['WASM_ENABLE_MEMORY_TRACING=1']
108+
print('[WAMR] Memory tracing enabled')
109+
106110
if GetDepend(['WAMR_BUILD_CUSTOM_NAME_SECTION']):
107111
CPPDEFINES += ['WASM_ENABLE_CUSTOM_NAME_SECTION=1']
108112
print('[WAMR] Custom name section enabled')

build-scripts/config_common.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ if (WAMR_BUILD_MEMORY_PROFILING EQUAL 1)
478478
add_definitions (-DWASM_ENABLE_MEMORY_PROFILING=1)
479479
message (" Memory profiling enabled")
480480
endif ()
481+
if (WAMR_BUILD_MEMORY_TRACING EQUAL 1)
482+
add_definitions (-DWASM_ENABLE_MEMORY_TRACING=1)
483+
message (" Memory tracing enabled")
484+
endif ()
481485
if (WAMR_BUILD_PERF_PROFILING EQUAL 1)
482486
add_definitions (-DWASM_ENABLE_PERF_PROFILING=1)
483487
message (" Performance profiling enabled")

build-scripts/esp-idf/wamr/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ if (NOT CMAKE_BUILD_EARLY_EXPANSION)
5656
set (WAMR_BUILD_MEMORY_PROFILING 1)
5757
endif ()
5858

59+
if (CONFIG_WAMR_ENABLE_MEMORY_TRACING)
60+
set (WAMR_BUILD_MEMORY_TRACING 1)
61+
endif ()
62+
5963
if (CONFIG_WAMR_ENABLE_PERF_PROFILING)
6064
set (WAMR_BUILD_PERF_PROFILING 1)
6165
endif ()

core/iwasm/aot/aot_runtime.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,6 +3781,17 @@ aot_get_module_mem_consumption(const AOTModule *module,
37813781
mem_conspn->aot_code_size += sizeof(uint8) * obj_data->size;
37823782
}
37833783

3784+
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
3785+
{
3786+
WASMCustomSection *section = module->custom_section_list;
3787+
while (section) {
3788+
mem_conspn->custom_sections_size +=
3789+
sizeof(WASMCustomSection) + section->content_len;
3790+
section = section->next;
3791+
}
3792+
}
3793+
#endif
3794+
37843795
mem_conspn->total_size += mem_conspn->module_struct_size;
37853796
mem_conspn->total_size += mem_conspn->types_size;
37863797
mem_conspn->total_size += mem_conspn->imports_size;
@@ -3793,6 +3804,9 @@ aot_get_module_mem_consumption(const AOTModule *module,
37933804
mem_conspn->total_size += mem_conspn->data_segs_size;
37943805
mem_conspn->total_size += mem_conspn->const_strs_size;
37953806
mem_conspn->total_size += mem_conspn->aot_code_size;
3807+
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
3808+
mem_conspn->total_size += mem_conspn->custom_sections_size;
3809+
#endif
37963810
}
37973811

37983812
void

core/iwasm/common/wasm_native.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ register_natives(const char *module_name, NativeSymbol *native_symbols,
268268
if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
269269
return false;
270270
#if WASM_ENABLE_MEMORY_TRACING != 0
271-
os_printf("Register native, size: %u\n", sizeof(NativeSymbolsNode));
271+
LOG_VERBOSE("Register native, size: %u", sizeof(NativeSymbolsNode));
272272
#endif
273273

274274
node->module_name = module_name;

core/iwasm/common/wasm_runtime_common.c

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,21 +2029,25 @@ wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
20292029
}
20302030
#endif
20312031

2032-
os_printf("WASM module memory consumption, total size: %u\n",
2033-
mem_conspn.total_size);
2034-
os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
2035-
os_printf(" types size: %u\n", mem_conspn.types_size);
2036-
os_printf(" imports size: %u\n", mem_conspn.imports_size);
2037-
os_printf(" funcs size: %u\n", mem_conspn.functions_size);
2038-
os_printf(" tables size: %u\n", mem_conspn.tables_size);
2039-
os_printf(" memories size: %u\n", mem_conspn.memories_size);
2040-
os_printf(" globals size: %u\n", mem_conspn.globals_size);
2041-
os_printf(" exports size: %u\n", mem_conspn.exports_size);
2042-
os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
2043-
os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
2044-
os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
2032+
LOG_VERBOSE("WASM module memory consumption, total size: %u",
2033+
mem_conspn.total_size);
2034+
LOG_VERBOSE(" module struct size: %u", mem_conspn.module_struct_size);
2035+
LOG_VERBOSE(" types size: %u", mem_conspn.types_size);
2036+
LOG_VERBOSE(" imports size: %u", mem_conspn.imports_size);
2037+
LOG_VERBOSE(" funcs size: %u", mem_conspn.functions_size);
2038+
LOG_VERBOSE(" tables size: %u", mem_conspn.tables_size);
2039+
LOG_VERBOSE(" memories size: %u", mem_conspn.memories_size);
2040+
LOG_VERBOSE(" globals size: %u", mem_conspn.globals_size);
2041+
LOG_VERBOSE(" exports size: %u", mem_conspn.exports_size);
2042+
LOG_VERBOSE(" table segs size: %u", mem_conspn.table_segs_size);
2043+
LOG_VERBOSE(" data segs size: %u", mem_conspn.data_segs_size);
2044+
LOG_VERBOSE(" const strings size: %u", mem_conspn.const_strs_size);
2045+
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
2046+
LOG_VERBOSE(" custom sections size: %u",
2047+
mem_conspn.custom_sections_size);
2048+
#endif
20452049
#if WASM_ENABLE_AOT != 0
2046-
os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
2050+
LOG_VERBOSE(" aot code size: %u", mem_conspn.aot_code_size);
20472051
#endif
20482052
}
20492053

@@ -2066,16 +2070,16 @@ wasm_runtime_dump_module_inst_mem_consumption(
20662070
}
20672071
#endif
20682072

2069-
os_printf("WASM module inst memory consumption, total size: %lu\n",
2070-
mem_conspn.total_size);
2071-
os_printf(" module inst struct size: %u\n",
2072-
mem_conspn.module_inst_struct_size);
2073-
os_printf(" memories size: %lu\n", mem_conspn.memories_size);
2074-
os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
2075-
os_printf(" tables size: %u\n", mem_conspn.tables_size);
2076-
os_printf(" functions size: %u\n", mem_conspn.functions_size);
2077-
os_printf(" globals size: %u\n", mem_conspn.globals_size);
2078-
os_printf(" exports size: %u\n", mem_conspn.exports_size);
2073+
LOG_VERBOSE("WASM module inst memory consumption, total size: %lu",
2074+
mem_conspn.total_size);
2075+
LOG_VERBOSE(" module inst struct size: %u",
2076+
mem_conspn.module_inst_struct_size);
2077+
LOG_VERBOSE(" memories size: %lu", mem_conspn.memories_size);
2078+
LOG_VERBOSE(" app heap size: %u", mem_conspn.app_heap_size);
2079+
LOG_VERBOSE(" tables size: %u", mem_conspn.tables_size);
2080+
LOG_VERBOSE(" functions size: %u", mem_conspn.functions_size);
2081+
LOG_VERBOSE(" globals size: %u", mem_conspn.globals_size);
2082+
LOG_VERBOSE(" exports size: %u", mem_conspn.exports_size);
20792083
}
20802084

20812085
void
@@ -2084,14 +2088,14 @@ wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
20842088
uint32 total_size =
20852089
offsetof(WASMExecEnv, wasm_stack_u.bottom) + exec_env->wasm_stack_size;
20862090

2087-
os_printf("Exec env memory consumption, total size: %u\n", total_size);
2088-
os_printf(" exec env struct size: %u\n",
2089-
offsetof(WASMExecEnv, wasm_stack_u.bottom));
2091+
LOG_VERBOSE("Exec env memory consumption, total size: %u", total_size);
2092+
LOG_VERBOSE(" exec env struct size: %u",
2093+
offsetof(WASMExecEnv, wasm_stack_u.bottom));
20902094
#if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
2091-
os_printf(" block addr cache size: %u\n",
2092-
sizeof(exec_env->block_addr_cache));
2095+
LOG_VERBOSE(" block addr cache size: %u",
2096+
sizeof(exec_env->block_addr_cache));
20932097
#endif
2094-
os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
2098+
LOG_VERBOSE(" stack size: %u", exec_env->wasm_stack_size);
20952099
}
20962100

20972101
uint32
@@ -2152,20 +2156,20 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
21522156
+ exec_env->wasm_stack_size + module_mem_consps.total_size
21532157
+ module_inst_mem_consps.total_size;
21542158

2155-
os_printf("\nMemory consumption summary (bytes):\n");
2159+
LOG_VERBOSE("Memory consumption summary (bytes):");
21562160
wasm_runtime_dump_module_mem_consumption(module_common);
21572161
wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
21582162
wasm_runtime_dump_exec_env_mem_consumption(exec_env);
2159-
os_printf("\nTotal memory consumption of module, module inst and "
2160-
"exec env: %" PRIu64 "\n",
2161-
total_size);
2162-
os_printf("Total interpreter stack used: %u\n",
2163-
exec_env->max_wasm_stack_used);
2163+
LOG_VERBOSE("Total memory consumption of module, module inst and "
2164+
"exec env: %" PRIu64,
2165+
total_size);
2166+
LOG_VERBOSE("Total interpreter stack used: %u",
2167+
exec_env->max_wasm_stack_used);
21642168

21652169
if (max_aux_stack_used != (uint32)-1)
2166-
os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
2170+
LOG_VERBOSE("Total auxiliary stack used: %u", max_aux_stack_used);
21672171
else
2168-
os_printf("Total aux stack used: no enough info to profile\n");
2172+
LOG_VERBOSE("Total aux stack used: no enough info to profile");
21692173

21702174
/*
21712175
* Report the native stack usage estimation.
@@ -2177,13 +2181,13 @@ wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
21772181
* It doesn't cover host func implementations, signal handlers, etc.
21782182
*/
21792183
if (exec_env->native_stack_top_min != (void *)UINTPTR_MAX)
2180-
os_printf("Native stack left: %zd\n",
2181-
exec_env->native_stack_top_min
2182-
- exec_env->native_stack_boundary);
2184+
LOG_VERBOSE("Native stack left: %zd",
2185+
exec_env->native_stack_top_min
2186+
- exec_env->native_stack_boundary);
21832187
else
2184-
os_printf("Native stack left: no enough info to profile\n");
2188+
LOG_VERBOSE("Native stack left: no enough info to profile");
21852189

2186-
os_printf("Total app heap used: %u\n", app_heap_peak_size);
2190+
LOG_VERBOSE("Total app heap used: %u", app_heap_peak_size);
21872191
}
21882192
#endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
21892193
|| (WASM_ENABLE_MEMORY_TRACING != 0) */

core/iwasm/common/wasm_runtime_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ typedef struct WASMModuleMemConsumption {
529529
uint32 table_segs_size;
530530
uint32 data_segs_size;
531531
uint32 const_strs_size;
532+
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
533+
uint32 custom_sections_size;
534+
#endif
532535
#if WASM_ENABLE_AOT != 0
533536
uint32 aot_code_size;
534537
#endif

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4214,6 +4214,17 @@ wasm_get_module_mem_consumption(const WASMModule *module,
42144214
}
42154215
}
42164216

4217+
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
4218+
{
4219+
WASMCustomSection *section = module->custom_section_list;
4220+
while (section) {
4221+
mem_conspn->custom_sections_size +=
4222+
sizeof(WASMCustomSection) + section->content_len;
4223+
section = section->next;
4224+
}
4225+
}
4226+
#endif
4227+
42174228
mem_conspn->total_size += mem_conspn->module_struct_size;
42184229
mem_conspn->total_size += mem_conspn->types_size;
42194230
mem_conspn->total_size += mem_conspn->imports_size;
@@ -4225,6 +4236,9 @@ wasm_get_module_mem_consumption(const WASMModule *module,
42254236
mem_conspn->total_size += mem_conspn->table_segs_size;
42264237
mem_conspn->total_size += mem_conspn->data_segs_size;
42274238
mem_conspn->total_size += mem_conspn->const_strs_size;
4239+
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
4240+
mem_conspn->total_size += mem_conspn->custom_sections_size;
4241+
#endif
42284242
}
42294243

42304244
void

core/shared/mem-alloc/ems/ems_kfc.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ gc_init_with_pool(char *buf, gc_size_t buf_size)
7676
heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
7777

7878
#if WASM_ENABLE_MEMORY_TRACING != 0
79-
os_printf("Heap created, total size: %u\n", buf_size);
80-
os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
81-
os_printf(" actual heap size: %u\n", heap_max_size);
82-
os_printf(" padding bytes: %u\n",
83-
buf_size - sizeof(gc_heap_t) - heap_max_size);
79+
LOG_VERBOSE("Heap created, total size: %u", buf_size);
80+
LOG_VERBOSE(" heap struct size: %u", sizeof(gc_heap_t));
81+
LOG_VERBOSE(" actual heap size: %u", heap_max_size);
82+
LOG_VERBOSE(" padding bytes: %u",
83+
buf_size - sizeof(gc_heap_t) - heap_max_size);
8484
#endif
8585
return gc_init_internal(heap, base_addr, heap_max_size);
8686
}
@@ -119,11 +119,11 @@ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
119119
heap_max_size = (uint32)(pool_buf_end - base_addr) & (uint32)~7;
120120

121121
#if WASM_ENABLE_MEMORY_TRACING != 0
122-
os_printf("Heap created, total size: %u\n",
123-
struct_buf_size + pool_buf_size);
124-
os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
125-
os_printf(" actual heap size: %u\n", heap_max_size);
126-
os_printf(" padding bytes: %u\n", pool_buf_size - heap_max_size);
122+
LOG_VERBOSE("Heap created, total size: %u",
123+
struct_buf_size + pool_buf_size);
124+
LOG_VERBOSE(" heap struct size: %u", sizeof(gc_heap_t));
125+
LOG_VERBOSE(" actual heap size: %u", heap_max_size);
126+
LOG_VERBOSE(" padding bytes: %u", pool_buf_size - heap_max_size);
127127
#endif
128128
return gc_init_internal(heap, base_addr, heap_max_size);
129129
}

core/shared/utils/uncommon/bh_read_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
4848
return NULL;
4949
}
5050
#if WASM_ENABLE_MEMORY_TRACING != 0
51-
printf("Read file, total size: %u\n", file_size);
51+
LOG_VERBOSE("Read file, total size: %u", file_size);
5252
#endif
5353

5454
read_size = _read(file, buffer, file_size);
@@ -99,7 +99,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
9999
return NULL;
100100
}
101101
#if WASM_ENABLE_MEMORY_TRACING != 0
102-
printf("Read file, total size: %u\n", file_size);
102+
LOG_VERBOSE("Read file, total size: %u", file_size);
103103
#endif
104104

105105
read_size = (uint32)read(file, buffer, file_size);

0 commit comments

Comments
 (0)