wasm_memory.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_runtime_common.h"
  6. #include "bh_platform.h"
  7. #include "mem_alloc.h"
  8. #if BH_ENABLE_MEMORY_PROFILING != 0
  9. /* Memory profile data of a function */
  10. typedef struct memory_profile {
  11. struct memory_profile *next;
  12. const char *function_name;
  13. const char *file_name;
  14. int line_in_file;
  15. int malloc_num;
  16. int free_num;
  17. int total_malloc;
  18. int total_free;
  19. } memory_profile_t;
  20. /* Memory in use which grows when BH_MALLOC was called
  21. * and decreases when bh_free was called */
  22. static unsigned int memory_in_use = 0;
  23. /* Memory profile data list */
  24. static memory_profile_t *memory_profiles_list = NULL;
  25. /* Lock of the memory profile list */
  26. static korp_mutex profile_lock;
  27. #endif /* end of BH_ENABLE_MEMORY_PROFILING */
  28. #ifndef MALLOC_MEMORY_FROM_SYSTEM
  29. typedef enum Memory_Mode {
  30. MEMORY_MODE_UNKNOWN = 0,
  31. MEMORY_MODE_POOL,
  32. MEMORY_MODE_ALLOCATOR
  33. } Memory_Mode;
  34. static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
  35. static mem_allocator_t pool_allocator = NULL;
  36. static void *(*malloc_func)(unsigned int size) = NULL;
  37. static void *(*realloc_func)(void *ptr, unsigned int size) = NULL;
  38. static void (*free_func)(void *ptr) = NULL;
  39. static unsigned int global_pool_size;
  40. static bool
  41. wasm_memory_init_with_pool(void *mem, unsigned int bytes)
  42. {
  43. mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
  44. if (_allocator) {
  45. memory_mode = MEMORY_MODE_POOL;
  46. pool_allocator = _allocator;
  47. #if BH_ENABLE_MEMORY_PROFILING != 0
  48. os_mutex_init(&profile_lock);
  49. #endif
  50. global_pool_size = bytes;
  51. return true;
  52. }
  53. LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  54. return false;
  55. }
  56. static bool
  57. wasm_memory_init_with_allocator(void *_malloc_func,
  58. void *_realloc_func,
  59. void *_free_func)
  60. {
  61. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  62. memory_mode = MEMORY_MODE_ALLOCATOR;
  63. malloc_func = _malloc_func;
  64. realloc_func = _realloc_func;
  65. free_func = _free_func;
  66. #if BH_ENABLE_MEMORY_PROFILING != 0
  67. os_mutex_init(&profile_lock);
  68. #endif
  69. return true;
  70. }
  71. LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n",
  72. _malloc_func, _realloc_func, _free_func);
  73. return false;
  74. }
  75. bool
  76. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  77. const MemAllocOption *alloc_option)
  78. {
  79. if (mem_alloc_type == Alloc_With_Pool)
  80. return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
  81. alloc_option->pool.heap_size);
  82. else if (mem_alloc_type == Alloc_With_Allocator)
  83. return wasm_memory_init_with_allocator(alloc_option->allocator.malloc_func,
  84. alloc_option->allocator.realloc_func,
  85. alloc_option->allocator.free_func);
  86. else if (mem_alloc_type == Alloc_With_System_Allocator)
  87. return wasm_memory_init_with_allocator(os_malloc, os_realloc, os_free);
  88. else
  89. return false;
  90. }
  91. void
  92. wasm_runtime_memory_destroy()
  93. {
  94. #if BH_ENABLE_MEMORY_PROFILING != 0
  95. os_mutex_destroy(&profile_lock);
  96. #endif
  97. if (memory_mode == MEMORY_MODE_POOL)
  98. mem_allocator_destroy(pool_allocator);
  99. memory_mode = MEMORY_MODE_UNKNOWN;
  100. }
  101. unsigned
  102. wasm_runtime_memory_pool_size()
  103. {
  104. if (memory_mode == MEMORY_MODE_POOL)
  105. return global_pool_size;
  106. else
  107. return 1 * BH_GB;
  108. }
  109. void *
  110. wasm_runtime_malloc(unsigned int size)
  111. {
  112. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  113. LOG_WARNING("wasm_runtime_malloc failed: memory hasn't been initialize.\n");
  114. return NULL;
  115. } else if (memory_mode == MEMORY_MODE_POOL) {
  116. return mem_allocator_malloc(pool_allocator, size);
  117. } else {
  118. return malloc_func(size);
  119. }
  120. }
  121. void *
  122. wasm_runtime_realloc(void *ptr, unsigned int size)
  123. {
  124. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  125. LOG_WARNING("wasm_runtime_realloc failed: memory hasn't been initialize.\n");
  126. return NULL;
  127. } else if (memory_mode == MEMORY_MODE_POOL) {
  128. return mem_allocator_realloc(pool_allocator, ptr, size);
  129. } else {
  130. if (realloc_func)
  131. return realloc_func(ptr, size);
  132. else
  133. return NULL;
  134. }
  135. }
  136. void
  137. wasm_runtime_free(void *ptr)
  138. {
  139. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  140. LOG_WARNING("wasm_runtime_free failed: memory hasn't been initialize.\n");
  141. } else if (memory_mode == MEMORY_MODE_POOL) {
  142. mem_allocator_free(pool_allocator, ptr);
  143. } else {
  144. free_func(ptr);
  145. }
  146. }
  147. #if BH_ENABLE_MEMORY_PROFILING != 0
  148. void
  149. memory_profile_print(const char *file, int line,
  150. const char *func, int alloc)
  151. {
  152. os_printf("location:%s@%d:used:%d:contribution:%d\n",
  153. func, line, memory_in_use, alloc);
  154. }
  155. void *
  156. wasm_runtime_malloc_profile(const char *file, int line,
  157. const char *func, unsigned int size)
  158. {
  159. void *p = wasm_runtime_malloc(size + 8);
  160. if (p) {
  161. memory_profile_t *profile;
  162. os_mutex_lock(&profile_lock);
  163. profile = memory_profiles_list;
  164. while (profile) {
  165. if (strcmp(profile->function_name, func) == 0
  166. && strcmp(profile->file_name, file) == 0) {
  167. break;
  168. }
  169. profile = profile->next;
  170. }
  171. if (profile) {
  172. profile->total_malloc += size;/* TODO: overflow check */
  173. profile->malloc_num++;
  174. } else {
  175. profile = wasm_runtime_malloc(sizeof(memory_profile_t));
  176. if (!profile) {
  177. os_mutex_unlock(&profile_lock);
  178. bh_memcpy_s(p, size + 8, &size, sizeof(size));
  179. return (char *)p + 8;
  180. }
  181. memset(profile, 0, sizeof(memory_profile_t));
  182. profile->file_name = file;
  183. profile->line_in_file = line;
  184. profile->function_name = func;
  185. profile->malloc_num = 1;
  186. profile->total_malloc = size;
  187. profile->next = memory_profiles_list;
  188. memory_profiles_list = profile;
  189. }
  190. os_mutex_unlock(&profile_lock);
  191. bh_memcpy_s(p, size + 8, &size, sizeof(size));
  192. memory_in_use += size;
  193. memory_profile_print(file, line, func, size);
  194. return (char *)p + 8;
  195. }
  196. return NULL;
  197. }
  198. void
  199. wasm_runtime_free_profile(const char *file, int line,
  200. const char *func, void *ptr)
  201. {
  202. unsigned int size = *(unsigned int *)((char *)ptr - 8);
  203. memory_profile_t *profile;
  204. wasm_runtime_free((char *)ptr - 8);
  205. if (memory_in_use >= size)
  206. memory_in_use -= size;
  207. os_mutex_lock(&profile_lock);
  208. profile = memory_profiles_list;
  209. while (profile) {
  210. if (strcmp(profile->function_name, func) == 0
  211. && strcmp(profile->file_name, file) == 0) {
  212. break;
  213. }
  214. profile = profile->next;
  215. }
  216. if (profile) {
  217. profile->total_free += size;/* TODO: overflow check */
  218. profile->free_num++;
  219. } else {
  220. profile = wasm_runtime_malloc(sizeof(memory_profile_t));
  221. if (!profile) {
  222. os_mutex_unlock(&profile_lock);
  223. return;
  224. }
  225. memset(profile, 0, sizeof(memory_profile_t));
  226. profile->file_name = file;
  227. profile->line_in_file = line;
  228. profile->function_name = func;
  229. profile->free_num = 1;
  230. profile->total_free = size;
  231. profile->next = memory_profiles_list;
  232. memory_profiles_list = profile;
  233. }
  234. os_mutex_unlock(&profile_lock);
  235. }
  236. /**
  237. * Summarize memory usage and print it out
  238. * Can use awk to analyze the output like below:
  239. * awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
  240. */
  241. void memory_usage_summarize()
  242. {
  243. memory_profile_t *profile;
  244. os_mutex_lock(&profile_lock);
  245. profile = memory_profiles_list;
  246. while (profile) {
  247. os_printf("malloc:%d:malloc_num:%d:free:%d:free_num:%d:%s\n",
  248. profile->total_malloc,
  249. profile->malloc_num,
  250. profile->total_free,
  251. profile->free_num,
  252. profile->function_name);
  253. profile = profile->next;
  254. }
  255. os_mutex_unlock(&profile_lock);
  256. }
  257. #endif /* end of BH_ENABLE_MEMORY_PROFILING */
  258. #else /* else of MALLOC_MEMORY_FROM_SYSTEM */
  259. void *
  260. wasm_runtime_malloc(unsigned int size)
  261. {
  262. return malloc(size);
  263. }
  264. void *
  265. wasm_runtime_realloc(void *ptr, unsigned int size)
  266. {
  267. return realloc(ptr, size);
  268. }
  269. void
  270. wasm_runtime_free(void *ptr)
  271. {
  272. if (ptr)
  273. free(ptr);
  274. }
  275. #if BH_ENABLE_MEMORY_PROFILING != 0
  276. void *
  277. wasm_runtime_malloc_profile(const char *file, int line,
  278. const char *func, unsigned int size)
  279. {
  280. (void)file;
  281. (void)line;
  282. (void)func;
  283. (void)memory_profiles_list;
  284. (void)profile_lock;
  285. (void)memory_in_use;
  286. return malloc(size);
  287. }
  288. void *
  289. wasm_runtime_realloc_profile(const char *file, int line,
  290. const char *func, void *ptr, unsigned int size)
  291. {
  292. (void)file;
  293. (void)line;
  294. (void)func;
  295. (void)memory_profiles_list;
  296. (void)profile_lock;
  297. (void)memory_in_use;
  298. return realloc(ptr, size);
  299. }
  300. void
  301. wasm_runtime_free_profile(const char *file, int line,
  302. const char *func, void *ptr)
  303. {
  304. (void)file;
  305. (void)line;
  306. (void)func;
  307. if (ptr)
  308. free(ptr);
  309. }
  310. #endif /* end of BH_ENABLE_MEMORY_PROFILING */
  311. #endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/