bh_memory.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_memory.h"
  18. #include "mem_alloc.h"
  19. #include <stdio.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, MEMORY_MODE_POOL, MEMORY_MODE_ALLOCATOR
  45. } Memory_Mode;
  46. static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
  47. static mem_allocator_t pool_allocator = NULL;
  48. static void *(*malloc_func)(unsigned int size) = NULL;
  49. static void (*free_func)(void *ptr) = NULL;
  50. int bh_memory_init_with_pool(void *mem, unsigned int bytes)
  51. {
  52. mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
  53. if (_allocator) {
  54. memory_mode = MEMORY_MODE_POOL;
  55. pool_allocator = _allocator;
  56. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  57. vm_mutex_init(&profile_lock);
  58. #endif
  59. return 0;
  60. }
  61. printf("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  62. return -1;
  63. }
  64. int bh_memory_init_with_allocator(void *_malloc_func, void *_free_func)
  65. {
  66. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  67. memory_mode = MEMORY_MODE_ALLOCATOR;
  68. malloc_func = _malloc_func;
  69. free_func = _free_func;
  70. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  71. vm_mutex_init(&profile_lock);
  72. #endif
  73. return 0;
  74. }
  75. printf("Init memory with allocator (%p, %p) failed.\n", _malloc_func,
  76. _free_func);
  77. return -1;
  78. }
  79. void bh_memory_destroy()
  80. {
  81. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  82. vm_mutex_destroy(&profile_lock);
  83. #endif
  84. if (memory_mode == MEMORY_MODE_POOL)
  85. mem_allocator_destroy(pool_allocator);
  86. memory_mode = MEMORY_MODE_UNKNOWN;
  87. }
  88. void* bh_malloc_internal(unsigned int size)
  89. {
  90. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  91. 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. 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. memcpy(p, &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. memcpy(p, &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. 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. 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*/