Implement memory profiler, optimize memory usage, modify code indent (#35)

This commit is contained in:
wenyongh
2019-05-23 05:03:31 -05:00
committed by GitHub
parent 9136abfe31
commit ff7cbdd2fb
18 changed files with 462 additions and 105 deletions

View File

@ -52,6 +52,8 @@ int bh_memory_init_with_allocator(void *malloc_func, void *free_func);
*/
void bh_memory_destroy();
#if BEIHAI_ENABLE_MEMORY_PROFILING == 0
/**
* This function allocates a memory chunk from system
*
@ -68,6 +70,32 @@ void* bh_malloc(unsigned int size);
*/
void bh_free(void *ptr);
#else
void* bh_malloc_profile(const char *file, int line, const char *func, unsigned int size);
void bh_free_profile(const char *file, int line, const char *func, void *ptr);
#define bh_malloc(size) bh_malloc_profile(__FILE__, __LINE__, __func__, size)
#define bh_free(ptr) bh_free_profile(__FILE__, __LINE__, __func__, ptr)
/**
* Print current memory profiling data
*
* @param file file name of the caller
* @param line line of the file of the caller
* @param func function name of the caller
*/
void memory_profile_print(const char *file, int line, const char *func, int alloc);
/**
* Summarize memory usage and print it out
* Can use awk to analyze the output like below:
* awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
*/
void memory_usage_summarize();
#endif
#ifdef __cplusplus
}
#endif

View File

@ -48,6 +48,9 @@
/* WASM Interpreter labels-as-values feature */
#define WASM_ENABLE_LABELS_AS_VALUES 1
/* WASM Branch Block address hashmap */
#define WASM_ENABLE_HASH_BLOCK_ADDR 0
/* Heap and stack profiling */
#define BEIHAI_ENABLE_MEMORY_PROFILING 0
@ -77,14 +80,22 @@
#define WORKING_FLOW_HEAP_SIZE 0
*/
/* Default/min/max heap size of each app */
#define APP_HEAP_SIZE_DEFAULT (48 * 1024)
/* Default min/max heap size of each app */
#define APP_HEAP_SIZE_DEFAULT (8 * 1024)
#define APP_HEAP_SIZE_MIN (2 * 1024)
#define APP_HEAP_SIZE_MAX (1024 * 1024)
/* Default wasm stack size of each app */
#define DEFAULT_WASM_STACK_SIZE (8 * 1024)
/* Default/min/max stack size of each app thread */
#ifndef __ZEPHYR__
#define APP_THREAD_STACK_SIZE_DEFAULT (20 * 1024)
#define APP_THREAD_STACK_SIZE_MIN (16 * 1024)
#define APP_THREAD_STACK_SIZE_MAX (256 * 1024)
#else
#define APP_THREAD_STACK_SIZE_DEFAULT (4 * 1024)
#define APP_THREAD_STACK_SIZE_MIN (2 * 1024)
#define APP_THREAD_STACK_SIZE_MAX (256 * 1024)
#endif
#endif

View File

@ -14,11 +14,38 @@
* limitations under the License.
*/
#include "bh_config.h"
#include "bh_memory.h"
#include "mem_alloc.h"
#include <stdio.h>
#include <stdlib.h>
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
#include "bh_thread.h"
/* Memory profile data of a function */
typedef struct memory_profile {
struct memory_profile *next;
const char *function_name;
const char *file_name;
int line_in_file;
int malloc_num;
int free_num;
int total_malloc;
int total_free;
} memory_profile_t;
/* Memory in use which grows when bh_malloc was called
* and decreases when bh_free was called */
static unsigned int memory_in_use = 0;
/* Memory profile data list */
static memory_profile_t *memory_profiles_list = NULL;
/* Lock of the memory profile list */
static korp_mutex profile_lock;
#endif
#ifndef MALLOC_MEMORY_FROM_SYSTEM
typedef enum Memory_Mode {
@ -39,6 +66,9 @@ int bh_memory_init_with_pool(void *mem, unsigned int bytes)
if (_allocator) {
memory_mode = MEMORY_MODE_POOL;
pool_allocator = _allocator;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_init(&profile_lock);
#endif
return 0;
}
printf("Init memory with pool (%p, %u) failed.\n", mem, bytes);
@ -51,6 +81,9 @@ int bh_memory_init_with_allocator(void *_malloc_func, void *_free_func)
memory_mode = MEMORY_MODE_ALLOCATOR;
malloc_func = _malloc_func;
free_func = _free_func;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_init(&profile_lock);
#endif
return 0;
}
printf("Init memory with allocator (%p, %p) failed.\n", _malloc_func,
@ -60,12 +93,15 @@ int bh_memory_init_with_allocator(void *_malloc_func, void *_free_func)
void bh_memory_destroy()
{
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_destroy(&profile_lock);
#endif
if (memory_mode == MEMORY_MODE_POOL)
mem_allocator_destroy(pool_allocator);
memory_mode = MEMORY_MODE_UNKNOWN;
}
void* bh_malloc(unsigned int size)
void* bh_malloc_internal(unsigned int size)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
printf("bh_malloc failed: memory hasn't been initialize.\n");
@ -77,7 +113,7 @@ void* bh_malloc(unsigned int size)
}
}
void bh_free(void *ptr)
void bh_free_internal(void *ptr)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
printf("bh_free failed: memory hasn't been initialize.\n");
@ -88,8 +124,157 @@ void bh_free(void *ptr)
}
}
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
void* bh_malloc_profile(const char *file,
int line,
const char *func,
unsigned int size)
{
void *p = bh_malloc_internal(size + 8);
if (p) {
memory_profile_t *profile;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
if (strcmp(profile->function_name, func) == 0
&& strcmp(profile->file_name, file) == 0) {
break;
}
profile = profile->next;
}
if (profile) {
profile->total_malloc += size;/* TODO: overflow check */
profile->malloc_num++;
} else {
profile = bh_malloc_internal(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
memcpy(p, &size, sizeof(size));
return (char *)p + 8;
}
memset(profile, 0, sizeof(memory_profile_t));
profile->file_name = file;
profile->line_in_file = line;
profile->function_name = func;
profile->malloc_num = 1;
profile->total_malloc = size;
profile->next = memory_profiles_list;
memory_profiles_list = profile;
}
vm_mutex_unlock(&profile_lock);
memcpy(p, &size, sizeof(size));
memory_in_use += size;
memory_profile_print(file, line, func, size);
return (char *)p + 8;
}
return NULL;
}
void bh_free_profile(const char *file, int line, const char *func, void *ptr)
{
unsigned int size = *(unsigned int *)((char *)ptr - 8);
memory_profile_t *profile;
bh_free_internal((char *)ptr - 8);
if (memory_in_use >= size)
memory_in_use -= size;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
if (strcmp(profile->function_name, func) == 0
&& strcmp(profile->file_name, file) == 0) {
break;
}
profile = profile->next;
}
if (profile) {
profile->total_free += size;/* TODO: overflow check */
profile->free_num++;
} else {
profile = bh_malloc_internal(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
return;
}
memset(profile, 0, sizeof(memory_profile_t));
profile->file_name = file;
profile->line_in_file = line;
profile->function_name = func;
profile->free_num = 1;
profile->total_free = size;
profile->next = memory_profiles_list;
memory_profiles_list = profile;
}
vm_mutex_unlock(&profile_lock);
}
/**
* Summarize memory usage and print it out
* Can use awk to analyze the output like below:
* awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
*/
void memory_usage_summarize()
{
memory_profile_t *profile;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
printf("malloc:%d:malloc_num:%d:free:%d:free_num:%d:%s\n",
profile->total_malloc,
profile->malloc_num,
profile->total_free,
profile->free_num,
profile->function_name);
profile = profile->next;
}
vm_mutex_unlock(&profile_lock);
}
void memory_profile_print(const char *file,
int line,
const char *func,
int alloc)
{
printf("location:%s@%d:used:%d:contribution:%d\n",
func, line, memory_in_use, alloc);
}
#else
void* bh_malloc(unsigned int size)
{
return bh_malloc_internal(size);
}
void bh_free(void *ptr)
{
bh_free_internal(ptr);
}
#endif
#else /* else of MALLOC_MEMORY_FROM_SYSTEM */
#if BEIHAI_ENABLE_MEMORY_PROFILING == 0
void* bh_malloc(unsigned int size)
{
return malloc(size);
@ -98,8 +283,36 @@ void* bh_malloc(unsigned int size)
void bh_free(void *ptr)
{
if (ptr)
free(ptr);
free(ptr);
}
#else /* else of BEIHAI_ENABLE_MEMORY_PROFILING */
void* bh_malloc_profile(const char *file,
int line,
const char *func,
unsigned int size)
{
(void)file;
(void)line;
(void)func;
(void)memory_profiles_list;
(void)profile_lock;
(void)memory_in_use;
return malloc(size);
}
void bh_free_profile(const char *file, int line, const char *func, void *ptr)
{
(void)file;
(void)line;
(void)func;
if (ptr)
free(ptr);
}
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/

View File

@ -128,7 +128,7 @@ int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
BH_THREAD_DEFAULT_PRIORITY);
}
korp_tid _vm_self_thread()