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".
This commit is contained in:
@ -40,6 +40,12 @@ void
|
||||
os_free(void *ptr)
|
||||
{}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *
|
||||
os_mmap(void *hint, size_t size, int prot, int flags)
|
||||
{
|
||||
|
||||
@ -20,3 +20,9 @@ os_realloc(void *ptr, unsigned size)
|
||||
void
|
||||
os_free(void *ptr)
|
||||
{}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -22,3 +22,51 @@ os_free(void *ptr)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
int ret = -1;
|
||||
FILE *f;
|
||||
char line[128] = { 0 };
|
||||
unsigned int out_idx = 0;
|
||||
|
||||
if (!out || !size)
|
||||
goto quit;
|
||||
|
||||
f = fopen("/proc/self/status", "r");
|
||||
if (!f) {
|
||||
perror("fopen failed: ");
|
||||
goto quit;
|
||||
}
|
||||
|
||||
memset(out, 0, size);
|
||||
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
#if WASM_ENABLE_MEMORY_PROFILING != 0
|
||||
if (strncmp(line, "Vm", 2) == 0 || strncmp(line, "Rss", 3) == 0) {
|
||||
#else
|
||||
if (strncmp(line, "VmRSS", 5) == 0
|
||||
|| strncmp(line, "RssAnon", 7) == 0) {
|
||||
#endif
|
||||
size_t line_len = strlen(line);
|
||||
if (line_len >= size - 1 - out_idx)
|
||||
goto close_file;
|
||||
|
||||
/* copying without null-terminated byte */
|
||||
memcpy(out + out_idx, line, line_len);
|
||||
out_idx += line_len;
|
||||
}
|
||||
}
|
||||
|
||||
if (ferror(f)) {
|
||||
perror("fgets failed: ");
|
||||
goto close_file;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
close_file:
|
||||
fclose(f);
|
||||
quit:
|
||||
return ret;
|
||||
}
|
||||
@ -76,3 +76,9 @@ os_free(void *ptr)
|
||||
free(mem_origin);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -977,6 +977,19 @@ os_socket_set_broadcast(bh_socket_t socket, bool is_enabled);
|
||||
int
|
||||
os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled);
|
||||
|
||||
/**
|
||||
* Dump memory information of the current process
|
||||
* It may have variant implementations in different platforms
|
||||
*
|
||||
* @param out the output buffer. It is for sure the return content
|
||||
* is a c-string which ends up with '\0'
|
||||
* @param size the size of the output buffer
|
||||
*
|
||||
* @return 0 if success, -1 otherwise
|
||||
*/
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -51,6 +51,12 @@ os_free(void *ptr)
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
putchar(int c)
|
||||
{
|
||||
|
||||
@ -38,6 +38,12 @@ os_free(void *ptr)
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *
|
||||
os_mmap(void *hint, size_t size, int prot, int flags)
|
||||
{
|
||||
|
||||
@ -43,6 +43,12 @@ os_free(void *ptr)
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *
|
||||
os_mmap(void *hint, size_t size, int prot, int flags)
|
||||
{
|
||||
|
||||
@ -88,6 +88,12 @@ os_free(void *ptr)
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
static char wamr_vprint_buf[RT_CONSOLEBUF_SIZE * 2];
|
||||
|
||||
int
|
||||
|
||||
@ -22,3 +22,9 @@ os_free(void *ptr)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@ -85,6 +85,12 @@ void
|
||||
os_free(void *ptr)
|
||||
{}
|
||||
|
||||
int
|
||||
os_dumps_proc_mem_info(char *out, unsigned int size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
struct out_context {
|
||||
int count;
|
||||
|
||||
@ -79,3 +79,29 @@ bh_print_time(const char *prompt)
|
||||
|
||||
last_time_ms = curr_time_ms;
|
||||
}
|
||||
|
||||
void
|
||||
bh_print_proc_mem(const char *prompt)
|
||||
{
|
||||
char buf[1024] = { 0 };
|
||||
|
||||
if (log_verbose_level < BH_LOG_LEVEL_DEBUG)
|
||||
return;
|
||||
|
||||
if (os_dumps_proc_mem_info(buf, sizeof(buf)) != 0)
|
||||
return;
|
||||
|
||||
os_printf("%s\n", prompt);
|
||||
os_printf("===== memory usage =====\n");
|
||||
os_printf("%s", buf);
|
||||
os_printf("==========\n");
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
bh_log_proc_mem(const char *function, uint32 line)
|
||||
{
|
||||
char prompt[128] = { 0 };
|
||||
snprintf(prompt, sizeof(prompt), "[MEM] %s(...) L%u", function, line);
|
||||
return bh_print_proc_mem(prompt);
|
||||
}
|
||||
@ -73,6 +73,14 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...);
|
||||
void
|
||||
bh_print_time(const char *prompt);
|
||||
|
||||
void
|
||||
bh_print_proc_mem(const char *prompt);
|
||||
|
||||
void
|
||||
bh_log_proc_mem(const char *function, uint32 line);
|
||||
|
||||
#define LOG_PROC_MEM(...) bh_log_proc_mem(__FUNCTION__, __LINE__)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user