bh_memory.c 8.2 KB

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