introduce WAMR memory profiling tool (experimental) (#390)

This commit is contained in:
Xu Jun
2020-09-18 18:04:56 +08:00
committed by GitHub
parent 04a7cc322f
commit 0226dbbb3d
27 changed files with 848 additions and 205 deletions

View File

@ -47,7 +47,7 @@ bh_hash_map_create(uint32 size, bool use_lock,
}
total_size = offsetof(HashMap, elements) +
sizeof(HashMapElem) * (uint64)size +
sizeof(HashMapElem *) * (uint64)size +
(use_lock ? sizeof(korp_mutex) : 0);
if (total_size >= UINT32_MAX
@ -61,7 +61,7 @@ bh_hash_map_create(uint32 size, bool use_lock,
if (use_lock) {
map->lock = (korp_mutex*)
((uint8*)map + offsetof(HashMap, elements)
+ sizeof(HashMapElem) * size);
+ sizeof(HashMapElem *) * size);
if (os_mutex_init(map->lock)) {
LOG_ERROR("HashMap create failed: init map lock failed.\n");
BH_FREE(map);
@ -188,8 +188,8 @@ bh_hash_map_update(HashMap *map, void *key, void *value,
os_mutex_unlock(map->lock);
}
return true;
}
elem = elem->next;
}
elem = elem->next;
}
if (map->lock) {
@ -286,3 +286,53 @@ bh_hash_map_destroy(HashMap *map)
BH_FREE(map);
return true;
}
uint32
bh_hash_map_get_struct_size(HashMap *hashmap)
{
uint32 size = offsetof(HashMap, elements)
+ sizeof(HashMapElem *) * hashmap->size;
if (hashmap->lock) {
size += sizeof(korp_mutex);
}
return size;
}
uint32
bh_hash_map_get_elem_struct_size()
{
return sizeof(HashMapElem);
}
bool
bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback)
{
uint32 index;
HashMapElem *elem, *next;
if (!map || !callback) {
LOG_ERROR("HashMap traverse failed: map or callback is NULL.\n");
return false;
}
if (map->lock) {
os_mutex_lock(map->lock);
}
for (index = 0; index < map->size; index++) {
elem = map->elements[index];
while (elem) {
next = elem->next;
callback(elem->key, elem->value);
elem = next;
}
}
if (map->lock) {
os_mutex_unlock(map->lock);
}
return true;
}

View File

@ -32,6 +32,10 @@ typedef void (*KeyDestroyFunc)(void *key);
when an hash element is removed. */
typedef void (*ValueDestroyFunc)(void *key);
/* traverse callback function:
auto called when traverse every hash element */
typedef void (*TraverseCallbackFunc)(void *key, void *value);
/**
* Create a hash map.
*
@ -124,6 +128,37 @@ bh_hash_map_remove(HashMap *map, void *key,
bool
bh_hash_map_destroy(HashMap *map);
/**
* Get the structure size of HashMap
*
* @param map the hash map to calculate
*
* @return the memory space occupied by HashMap structure
*/
uint32
bh_hash_map_get_struct_size(HashMap *hashmap);
/**
* Get the structure size of HashMap Element
*
* @return the memory space occupied by HashMapElem structure
*/
uint32
bh_hash_map_get_elem_struct_size();
/**
* Traverse the hash map and call the callback function
*
* @param map the hash map to traverse
* @callback the function to be called for every element
*
* @return true if success, false otherwise
* Note: if the hash map has lock, the map will be locked during traverse,
* keep the callback function as simple as possible.
*/
bool
bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback);
#ifdef __cplusplus
}
#endif

View File

@ -41,6 +41,9 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
_close(file);
return NULL;
}
#if WASM_ENABLE_MEMORY_TRACING != 0
printf("Read file, total size: %u\n", file_size);
#endif
read_size = _read(file, buffer, file_size);
_close(file);
@ -88,6 +91,9 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
close(file);
return NULL;
}
#if WASM_ENABLE_MEMORY_TRACING != 0
printf("Read file, total size: %u\n", file_size);
#endif
read_size = (uint32)read(file, buffer, file_size);
close(file);