bh_memory.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_config.h"
  6. #include "bh_platform.h"
  7. #include "bh_memory.h"
  8. #include "mem_alloc.h"
  9. #include <stdlib.h>
  10. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  11. #include "bh_thread.h"
  12. /* Memory profile data of a function */
  13. typedef struct memory_profile {
  14. struct memory_profile *next;
  15. const char *function_name;
  16. const char *file_name;
  17. int line_in_file;
  18. int malloc_num;
  19. int free_num;
  20. int total_malloc;
  21. int total_free;
  22. } memory_profile_t;
  23. /* Memory in use which grows when bh_malloc was called
  24. * and decreases when bh_free was called */
  25. static unsigned int memory_in_use = 0;
  26. /* Memory profile data list */
  27. static memory_profile_t *memory_profiles_list = NULL;
  28. /* Lock of the memory profile list */
  29. static korp_mutex profile_lock;
  30. #endif
  31. #ifndef MALLOC_MEMORY_FROM_SYSTEM
  32. typedef enum Memory_Mode {
  33. MEMORY_MODE_UNKNOWN = 0,
  34. MEMORY_MODE_POOL,
  35. MEMORY_MODE_ALLOCATOR
  36. } Memory_Mode;
  37. static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
  38. static mem_allocator_t pool_allocator = NULL;
  39. static void *(*malloc_func)(unsigned int size) = NULL;
  40. static void (*free_func)(void *ptr) = NULL;
  41. static unsigned int global_pool_size;
  42. int bh_memory_init_with_pool(void *mem, unsigned int bytes)
  43. {
  44. mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
  45. if (_allocator) {
  46. memory_mode = MEMORY_MODE_POOL;
  47. pool_allocator = _allocator;
  48. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  49. vm_mutex_init(&profile_lock);
  50. #endif
  51. global_pool_size = bytes;
  52. return 0;
  53. }
  54. bh_printf("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  55. return -1;
  56. }
  57. int bh_memory_init_with_allocator(void *_malloc_func, void *_free_func)
  58. {
  59. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  60. memory_mode = MEMORY_MODE_ALLOCATOR;
  61. malloc_func = _malloc_func;
  62. free_func = _free_func;
  63. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  64. vm_mutex_init(&profile_lock);
  65. #endif
  66. return 0;
  67. }
  68. bh_printf("Init memory with allocator (%p, %p) failed.\n", _malloc_func,
  69. _free_func);
  70. return -1;
  71. }
  72. void bh_memory_destroy()
  73. {
  74. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  75. vm_mutex_destroy(&profile_lock);
  76. #endif
  77. if (memory_mode == MEMORY_MODE_POOL)
  78. mem_allocator_destroy(pool_allocator);
  79. memory_mode = MEMORY_MODE_UNKNOWN;
  80. }
  81. unsigned bh_memory_pool_size()
  82. {
  83. if (memory_mode == MEMORY_MODE_POOL)
  84. return global_pool_size;
  85. else
  86. return 1 * BH_GB;
  87. }
  88. void* bh_malloc_internal(unsigned int size)
  89. {
  90. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  91. bh_printf("bh_malloc failed: memory hasn't been initialize.\n");
  92. return NULL;
  93. } else if (memory_mode == MEMORY_MODE_POOL) {
  94. return mem_allocator_malloc(pool_allocator, size);
  95. } else {
  96. return malloc_func(size);
  97. }
  98. }
  99. void bh_free_internal(void *ptr)
  100. {
  101. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  102. bh_printf("bh_free failed: memory hasn't been initialize.\n");
  103. } else if (memory_mode == MEMORY_MODE_POOL) {
  104. mem_allocator_free(pool_allocator, ptr);
  105. } else {
  106. free_func(ptr);
  107. }
  108. }
  109. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  110. void* bh_malloc_profile(const char *file,
  111. int line,
  112. const char *func,
  113. unsigned int size)
  114. {
  115. void *p = bh_malloc_internal(size + 8);
  116. if (p) {
  117. memory_profile_t *profile;
  118. vm_mutex_lock(&profile_lock);
  119. profile = memory_profiles_list;
  120. while (profile) {
  121. if (strcmp(profile->function_name, func) == 0
  122. && strcmp(profile->file_name, file) == 0) {
  123. break;
  124. }
  125. profile = profile->next;
  126. }
  127. if (profile) {
  128. profile->total_malloc += size;/* TODO: overflow check */
  129. profile->malloc_num++;
  130. } else {
  131. profile = bh_malloc_internal(sizeof(memory_profile_t));
  132. if (!profile) {
  133. vm_mutex_unlock(&profile_lock);
  134. bh_memcpy_s(p, size + 8, &size, sizeof(size));
  135. return (char *)p + 8;
  136. }
  137. memset(profile, 0, sizeof(memory_profile_t));
  138. profile->file_name = file;
  139. profile->line_in_file = line;
  140. profile->function_name = func;
  141. profile->malloc_num = 1;
  142. profile->total_malloc = size;
  143. profile->next = memory_profiles_list;
  144. memory_profiles_list = profile;
  145. }
  146. vm_mutex_unlock(&profile_lock);
  147. bh_memcpy_s(p, size + 8, &size, sizeof(size));
  148. memory_in_use += size;
  149. memory_profile_print(file, line, func, size);
  150. return (char *)p + 8;
  151. }
  152. return NULL;
  153. }
  154. void bh_free_profile(const char *file, int line, const char *func, void *ptr)
  155. {
  156. unsigned int size = *(unsigned int *)((char *)ptr - 8);
  157. memory_profile_t *profile;
  158. bh_free_internal((char *)ptr - 8);
  159. if (memory_in_use >= size)
  160. memory_in_use -= size;
  161. vm_mutex_lock(&profile_lock);
  162. profile = memory_profiles_list;
  163. while (profile) {
  164. if (strcmp(profile->function_name, func) == 0
  165. && strcmp(profile->file_name, file) == 0) {
  166. break;
  167. }
  168. profile = profile->next;
  169. }
  170. if (profile) {
  171. profile->total_free += size;/* TODO: overflow check */
  172. profile->free_num++;
  173. } else {
  174. profile = bh_malloc_internal(sizeof(memory_profile_t));
  175. if (!profile) {
  176. vm_mutex_unlock(&profile_lock);
  177. return;
  178. }
  179. memset(profile, 0, sizeof(memory_profile_t));
  180. profile->file_name = file;
  181. profile->line_in_file = line;
  182. profile->function_name = func;
  183. profile->free_num = 1;
  184. profile->total_free = size;
  185. profile->next = memory_profiles_list;
  186. memory_profiles_list = profile;
  187. }
  188. vm_mutex_unlock(&profile_lock);
  189. }
  190. /**
  191. * Summarize memory usage and print it out
  192. * Can use awk to analyze the output like below:
  193. * awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
  194. */
  195. void memory_usage_summarize()
  196. {
  197. memory_profile_t *profile;
  198. vm_mutex_lock(&profile_lock);
  199. profile = memory_profiles_list;
  200. while (profile) {
  201. bh_printf("malloc:%d:malloc_num:%d:free:%d:free_num:%d:%s\n",
  202. profile->total_malloc,
  203. profile->malloc_num,
  204. profile->total_free,
  205. profile->free_num,
  206. profile->function_name);
  207. profile = profile->next;
  208. }
  209. vm_mutex_unlock(&profile_lock);
  210. }
  211. void memory_profile_print(const char *file,
  212. int line,
  213. const char *func,
  214. int alloc)
  215. {
  216. bh_printf("location:%s@%d:used:%d:contribution:%d\n",
  217. func, line, memory_in_use, alloc);
  218. }
  219. #else
  220. void* bh_malloc(unsigned int size)
  221. {
  222. return bh_malloc_internal(size);
  223. }
  224. void bh_free(void *ptr)
  225. {
  226. bh_free_internal(ptr);
  227. }
  228. #endif
  229. #else /* else of MALLOC_MEMORY_FROM_SYSTEM */
  230. #if BEIHAI_ENABLE_MEMORY_PROFILING == 0
  231. void* bh_malloc(unsigned int size)
  232. {
  233. return malloc(size);
  234. }
  235. void bh_free(void *ptr)
  236. {
  237. if (ptr)
  238. free(ptr);
  239. }
  240. #else /* else of BEIHAI_ENABLE_MEMORY_PROFILING */
  241. void* bh_malloc_profile(const char *file,
  242. int line,
  243. const char *func,
  244. unsigned int size)
  245. {
  246. (void)file;
  247. (void)line;
  248. (void)func;
  249. (void)memory_profiles_list;
  250. (void)profile_lock;
  251. (void)memory_in_use;
  252. return malloc(size);
  253. }
  254. void bh_free_profile(const char *file, int line, const char *func, void *ptr)
  255. {
  256. (void)file;
  257. (void)line;
  258. (void)func;
  259. if (ptr)
  260. free(ptr);
  261. }
  262. #endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
  263. #endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/