Skip to content

Commit eaedcec

Browse files
authored
Add bh_print_proc_mem() to dump memory info of current process (#1734)
Only support Posix platforms currently, read memory consumption info from file "/proc/self/status".
1 parent 29b76dd commit eaedcec

File tree

14 files changed

+178
-8
lines changed

14 files changed

+178
-8
lines changed

core/iwasm/common/wasm_c_api.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ WASM_DEFINE_VEC_OWN(module, wasm_module_delete_internal)
258258
WASM_DEFINE_VEC_OWN(store, wasm_store_delete)
259259
WASM_DEFINE_VEC_OWN(valtype, wasm_valtype_delete)
260260

261+
#ifndef NDEBUG
262+
#define WASM_C_DUMP_PROC_MEM() LOG_PROC_MEM()
263+
#else
264+
#define WASM_C_DUMP_PROC_MEM() (void)0
265+
#endif
266+
261267
/* Runtime Environment */
262268
own wasm_config_t *
263269
wasm_config_new(void)
@@ -307,6 +313,14 @@ wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts)
307313
RuntimeInitArgs init_args = { 0 };
308314
init_args.mem_alloc_type = type;
309315

316+
#ifndef NDEBUG
317+
bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE);
318+
#else
319+
bh_log_set_verbose_level(BH_LOG_LEVEL_WARNING);
320+
#endif
321+
322+
WASM_C_DUMP_PROC_MEM();
323+
310324
if (type == Alloc_With_Pool) {
311325
if (!opts) {
312326
return NULL;
@@ -337,14 +351,6 @@ wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts)
337351
goto failed;
338352
}
339353

340-
#ifndef NDEBUG
341-
/*DEBUG*/
342-
bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE);
343-
#else
344-
/*VERBOSE*/
345-
bh_log_set_verbose_level(BH_LOG_LEVEL_WARNING);
346-
#endif
347-
348354
/* create wasm_engine_t */
349355
if (!(engine = malloc_internal(sizeof(wasm_engine_t)))) {
350356
goto failed;
@@ -358,6 +364,8 @@ wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts)
358364

359365
engine->ref_count = 1;
360366

367+
WASM_C_DUMP_PROC_MEM();
368+
361369
RETURN_OBJ(engine, wasm_engine_delete_internal)
362370
}
363371

@@ -442,6 +450,8 @@ wasm_store_new(wasm_engine_t *engine)
442450
{
443451
wasm_store_t *store = NULL;
444452

453+
WASM_C_DUMP_PROC_MEM();
454+
445455
if (!engine || singleton_engine != engine) {
446456
return NULL;
447457
}
@@ -474,6 +484,8 @@ wasm_store_new(wasm_engine_t *engine)
474484
goto failed;
475485
}
476486

487+
WASM_C_DUMP_PROC_MEM();
488+
477489
return store;
478490
failed:
479491
wasm_store_delete(store);
@@ -1903,6 +1915,8 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
19031915

19041916
bh_assert(singleton_engine);
19051917

1918+
WASM_C_DUMP_PROC_MEM();
1919+
19061920
if (!store || !binary || binary->size == 0 || binary->size > UINT32_MAX)
19071921
goto quit;
19081922

@@ -1958,6 +1972,9 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
19581972
goto destroy_lock;
19591973

19601974
module_ex->ref_count = 1;
1975+
1976+
WASM_C_DUMP_PROC_MEM();
1977+
19611978
return module_ext_to_module(module_ex);
19621979

19631980
destroy_lock:
@@ -4453,6 +4470,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
44534470
return NULL;
44544471
}
44554472

4473+
WASM_C_DUMP_PROC_MEM();
4474+
44564475
instance = malloc_internal(sizeof(wasm_instance_t));
44574476
if (!instance) {
44584477
goto failed;
@@ -4595,6 +4614,8 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
45954614
goto failed;
45964615
}
45974616

4617+
WASM_C_DUMP_PROC_MEM();
4618+
45984619
return instance;
45994620

46004621
failed:

core/shared/platform/alios/alios_platform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ void
4040
os_free(void *ptr)
4141
{}
4242

43+
int
44+
os_dumps_proc_mem_info(char *out, unsigned int size)
45+
{
46+
return -1;
47+
}
48+
4349
void *
4450
os_mmap(void *hint, size_t size, int prot, int flags)
4551
{

core/shared/platform/common/freertos/freertos_malloc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ os_realloc(void *ptr, unsigned size)
2020
void
2121
os_free(void *ptr)
2222
{}
23+
24+
int
25+
os_dumps_proc_mem_info(char *out, unsigned int size)
26+
{
27+
return -1;
28+
}

core/shared/platform/common/posix/posix_malloc.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,51 @@ os_free(void *ptr)
2222
{
2323
free(ptr);
2424
}
25+
26+
int
27+
os_dumps_proc_mem_info(char *out, unsigned int size)
28+
{
29+
int ret = -1;
30+
FILE *f;
31+
char line[128] = { 0 };
32+
unsigned int out_idx = 0;
33+
34+
if (!out || !size)
35+
goto quit;
36+
37+
f = fopen("/proc/self/status", "r");
38+
if (!f) {
39+
perror("fopen failed: ");
40+
goto quit;
41+
}
42+
43+
memset(out, 0, size);
44+
45+
while (fgets(line, sizeof(line), f)) {
46+
#if WASM_ENABLE_MEMORY_PROFILING != 0
47+
if (strncmp(line, "Vm", 2) == 0 || strncmp(line, "Rss", 3) == 0) {
48+
#else
49+
if (strncmp(line, "VmRSS", 5) == 0
50+
|| strncmp(line, "RssAnon", 7) == 0) {
51+
#endif
52+
size_t line_len = strlen(line);
53+
if (line_len >= size - 1 - out_idx)
54+
goto close_file;
55+
56+
/* copying without null-terminated byte */
57+
memcpy(out + out_idx, line, line_len);
58+
out_idx += line_len;
59+
}
60+
}
61+
62+
if (ferror(f)) {
63+
perror("fgets failed: ");
64+
goto close_file;
65+
}
66+
67+
ret = 0;
68+
close_file:
69+
fclose(f);
70+
quit:
71+
return ret;
72+
}

core/shared/platform/esp-idf/espidf_malloc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,9 @@ os_free(void *ptr)
7676
free(mem_origin);
7777
}
7878
}
79+
80+
int
81+
os_dumps_proc_mem_info(char *out, unsigned int size)
82+
{
83+
return -1;
84+
}

core/shared/platform/include/platform_api_extension.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,19 @@ os_socket_set_broadcast(bh_socket_t socket, bool is_enabled);
977977
int
978978
os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled);
979979

980+
/**
981+
* Dump memory information of the current process
982+
* It may have variant implementations in different platforms
983+
*
984+
* @param out the output buffer. It is for sure the return content
985+
* is a c-string which ends up with '\0'
986+
* @param size the size of the output buffer
987+
*
988+
* @return 0 if success, -1 otherwise
989+
*/
990+
int
991+
os_dumps_proc_mem_info(char *out, unsigned int size);
992+
980993
#ifdef __cplusplus
981994
}
982995
#endif

core/shared/platform/linux-sgx/sgx_platform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ os_free(void *ptr)
5151
free(ptr);
5252
}
5353

54+
int
55+
os_dumps_proc_mem_info(char *out, unsigned int size)
56+
{
57+
return -1;
58+
}
59+
5460
int
5561
putchar(int c)
5662
{

core/shared/platform/nuttx/nuttx_platform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ os_free(void *ptr)
3838
free(ptr);
3939
}
4040

41+
int
42+
os_dumps_proc_mem_info(char *out, unsigned int size)
43+
{
44+
return -1;
45+
}
46+
4147
void *
4248
os_mmap(void *hint, size_t size, int prot, int flags)
4349
{

core/shared/platform/riot/riot_platform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ os_free(void *ptr)
4343
free(ptr);
4444
}
4545

46+
int
47+
os_dumps_proc_mem_info(char *out, unsigned int size)
48+
{
49+
return -1;
50+
}
51+
4652
void *
4753
os_mmap(void *hint, size_t size, int prot, int flags)
4854
{

core/shared/platform/rt-thread/rtt_platform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ os_free(void *ptr)
8888
}
8989
}
9090

91+
int
92+
os_dumps_proc_mem_info(char *out, unsigned int size)
93+
{
94+
return -1;
95+
}
96+
9197
static char wamr_vprint_buf[RT_CONSOLEBUF_SIZE * 2];
9298

9399
int

0 commit comments

Comments
 (0)