ems_kfc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "ems_gc_internal.h"
  6. static gc_handle_t
  7. gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size)
  8. {
  9. hmu_tree_node_t *root = NULL, *q = NULL;
  10. int ret;
  11. memset(heap, 0, sizeof *heap);
  12. memset(base_addr, 0, heap_max_size);
  13. ret = os_mutex_init(&heap->lock);
  14. if (ret != BHT_OK) {
  15. os_printf("[GC_ERROR]failed to init lock\n");
  16. return NULL;
  17. }
  18. /* init all data structures*/
  19. heap->current_size = heap_max_size;
  20. heap->base_addr = (gc_uint8 *)base_addr;
  21. heap->heap_id = (gc_handle_t)heap;
  22. heap->total_free_size = heap->current_size;
  23. heap->highmark_size = 0;
  24. #if WASM_ENABLE_GC != 0
  25. heap->gc_threshold_factor = GC_DEFAULT_THRESHOLD_FACTOR;
  26. gc_update_threshold(heap);
  27. #endif
  28. root = heap->kfc_tree_root = (hmu_tree_node_t *)heap->kfc_tree_root_buf;
  29. memset(root, 0, sizeof *root);
  30. root->size = sizeof *root;
  31. hmu_set_ut(&root->hmu_header, HMU_FC);
  32. hmu_set_size(&root->hmu_header, sizeof *root);
  33. q = (hmu_tree_node_t *)heap->base_addr;
  34. memset(q, 0, sizeof *q);
  35. hmu_set_ut(&q->hmu_header, HMU_FC);
  36. hmu_set_size(&q->hmu_header, heap->current_size);
  37. ASSERT_TREE_NODE_ALIGNED_ACCESS(q);
  38. ASSERT_TREE_NODE_ALIGNED_ACCESS(root);
  39. hmu_mark_pinuse(&q->hmu_header);
  40. root->right = q;
  41. q->parent = root;
  42. q->size = heap->current_size;
  43. bh_assert(root->size <= HMU_FC_NORMAL_MAX_SIZE);
  44. return heap;
  45. }
  46. gc_handle_t
  47. gc_init_with_pool(char *buf, gc_size_t buf_size)
  48. {
  49. char *buf_end = buf + buf_size;
  50. char *buf_aligned = (char *)(((uintptr_t)buf + 7) & (uintptr_t)~7);
  51. char *base_addr = buf_aligned + sizeof(gc_heap_t);
  52. gc_heap_t *heap = (gc_heap_t *)buf_aligned;
  53. gc_size_t heap_max_size;
  54. if (buf_size < APP_HEAP_SIZE_MIN) {
  55. os_printf("[GC_ERROR]heap init buf size (%" PRIu32 ") < %" PRIu32 "\n",
  56. buf_size, (uint32)APP_HEAP_SIZE_MIN);
  57. return NULL;
  58. }
  59. base_addr =
  60. (char *)(((uintptr_t)base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING;
  61. heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
  62. #if WASM_ENABLE_MEMORY_TRACING != 0
  63. os_printf("Heap created, total size: %u\n", buf_size);
  64. os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
  65. os_printf(" actual heap size: %u\n", heap_max_size);
  66. os_printf(" padding bytes: %u\n",
  67. buf_size - sizeof(gc_heap_t) - heap_max_size);
  68. #endif
  69. return gc_init_internal(heap, base_addr, heap_max_size);
  70. }
  71. gc_handle_t
  72. gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
  73. char *pool_buf, gc_size_t pool_buf_size)
  74. {
  75. gc_heap_t *heap = (gc_heap_t *)struct_buf;
  76. char *base_addr = pool_buf + GC_HEAD_PADDING;
  77. char *pool_buf_end = pool_buf + pool_buf_size;
  78. gc_size_t heap_max_size;
  79. if ((((uintptr_t)struct_buf) & 7) != 0) {
  80. os_printf("[GC_ERROR]heap init struct buf not 8-byte aligned\n");
  81. return NULL;
  82. }
  83. if (struct_buf_size < sizeof(gc_handle_t)) {
  84. os_printf("[GC_ERROR]heap init struct buf size (%" PRIu32 ") < %zu\n",
  85. struct_buf_size, sizeof(gc_handle_t));
  86. return NULL;
  87. }
  88. if ((((uintptr_t)pool_buf) & 7) != 0) {
  89. os_printf("[GC_ERROR]heap init pool buf not 8-byte aligned\n");
  90. return NULL;
  91. }
  92. if (pool_buf_size < APP_HEAP_SIZE_MIN) {
  93. os_printf("[GC_ERROR]heap init buf size (%" PRIu32 ") < %u\n",
  94. pool_buf_size, APP_HEAP_SIZE_MIN);
  95. return NULL;
  96. }
  97. heap_max_size = (uint32)(pool_buf_end - base_addr) & (uint32)~7;
  98. #if WASM_ENABLE_MEMORY_TRACING != 0
  99. os_printf("Heap created, total size: %u\n",
  100. struct_buf_size + pool_buf_size);
  101. os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
  102. os_printf(" actual heap size: %u\n", heap_max_size);
  103. os_printf(" padding bytes: %u\n", pool_buf_size - heap_max_size);
  104. #endif
  105. return gc_init_internal(heap, base_addr, heap_max_size);
  106. }
  107. int
  108. gc_destroy_with_pool(gc_handle_t handle)
  109. {
  110. gc_heap_t *heap = (gc_heap_t *)handle;
  111. int ret = GC_SUCCESS;
  112. #if BH_ENABLE_GC_VERIFY != 0
  113. hmu_t *cur = (hmu_t *)heap->base_addr;
  114. hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
  115. if (!heap->is_heap_corrupted
  116. && (hmu_t *)((char *)cur + hmu_get_size(cur)) != end) {
  117. os_printf("Memory leak detected:\n");
  118. gci_dump(heap);
  119. ret = GC_ERROR;
  120. }
  121. #endif
  122. os_mutex_destroy(&heap->lock);
  123. memset(heap->base_addr, 0, heap->current_size);
  124. memset(heap, 0, sizeof(gc_heap_t));
  125. return ret;
  126. }
  127. #if WASM_ENABLE_GC != 0
  128. #if WASM_ENABLE_THREAD_MGR == 0
  129. void
  130. gc_enable_gc_reclaim(gc_handle_t handle, void *exec_env)
  131. {
  132. gc_heap_t *heap = (gc_heap_t *)handle;
  133. heap->is_reclaim_enabled = 1;
  134. heap->exec_env = exec_env;
  135. }
  136. #else
  137. void
  138. gc_enable_gc_reclaim(gc_handle_t handle, void *cluster)
  139. {
  140. gc_heap_t *heap = (gc_heap_t *)handle;
  141. heap->is_reclaim_enabled = 1;
  142. heap->cluster = cluster;
  143. }
  144. #endif
  145. #endif
  146. uint32
  147. gc_get_heap_struct_size()
  148. {
  149. return sizeof(gc_heap_t);
  150. }
  151. static void
  152. adjust_ptr(uint8 **p_ptr, intptr_t offset)
  153. {
  154. if (*p_ptr)
  155. *p_ptr = (uint8 *)((intptr_t)(*p_ptr) + offset);
  156. }
  157. int
  158. gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size)
  159. {
  160. gc_heap_t *heap = (gc_heap_t *)handle;
  161. char *base_addr_new = pool_buf_new + GC_HEAD_PADDING;
  162. char *pool_buf_end = pool_buf_new + pool_buf_size;
  163. intptr_t offset = (uint8 *)base_addr_new - (uint8 *)heap->base_addr;
  164. hmu_t *cur = NULL, *end = NULL;
  165. hmu_tree_node_t *tree_node;
  166. uint8 **p_left, **p_right, **p_parent;
  167. gc_size_t heap_max_size, size;
  168. if ((((uintptr_t)pool_buf_new) & 7) != 0) {
  169. os_printf("[GC_ERROR]heap migrate pool buf not 8-byte aligned\n");
  170. return GC_ERROR;
  171. }
  172. heap_max_size = (uint32)(pool_buf_end - base_addr_new) & (uint32)~7;
  173. if (pool_buf_end < base_addr_new || heap_max_size < heap->current_size) {
  174. os_printf("[GC_ERROR]heap migrate invlaid pool buf size\n");
  175. return GC_ERROR;
  176. }
  177. if (offset == 0)
  178. return 0;
  179. if (heap->is_heap_corrupted) {
  180. os_printf("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
  181. return GC_ERROR;
  182. }
  183. heap->base_addr = (uint8 *)base_addr_new;
  184. ASSERT_TREE_NODE_ALIGNED_ACCESS(heap->kfc_tree_root);
  185. p_left = (uint8 **)((uint8 *)heap->kfc_tree_root
  186. + offsetof(hmu_tree_node_t, left));
  187. p_right = (uint8 **)((uint8 *)heap->kfc_tree_root
  188. + offsetof(hmu_tree_node_t, right));
  189. p_parent = (uint8 **)((uint8 *)heap->kfc_tree_root
  190. + offsetof(hmu_tree_node_t, parent));
  191. adjust_ptr(p_left, offset);
  192. adjust_ptr(p_right, offset);
  193. adjust_ptr(p_parent, offset);
  194. cur = (hmu_t *)heap->base_addr;
  195. end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
  196. while (cur < end) {
  197. size = hmu_get_size(cur);
  198. if (size <= 0 || size > (uint32)((uint8 *)end - (uint8 *)cur)) {
  199. os_printf("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
  200. heap->is_heap_corrupted = true;
  201. return GC_ERROR;
  202. }
  203. if (hmu_get_ut(cur) == HMU_FC && !HMU_IS_FC_NORMAL(size)) {
  204. tree_node = (hmu_tree_node_t *)cur;
  205. ASSERT_TREE_NODE_ALIGNED_ACCESS(tree_node);
  206. p_left = (uint8 **)((uint8 *)tree_node
  207. + offsetof(hmu_tree_node_t, left));
  208. p_right = (uint8 **)((uint8 *)tree_node
  209. + offsetof(hmu_tree_node_t, right));
  210. p_parent = (uint8 **)((uint8 *)tree_node
  211. + offsetof(hmu_tree_node_t, parent));
  212. adjust_ptr(p_left, offset);
  213. adjust_ptr(p_right, offset);
  214. if (tree_node->parent != heap->kfc_tree_root)
  215. /* The root node belongs to heap structure,
  216. it is fixed part and isn't changed. */
  217. adjust_ptr(p_parent, offset);
  218. }
  219. cur = (hmu_t *)((char *)cur + size);
  220. }
  221. if (cur != end) {
  222. os_printf("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
  223. heap->is_heap_corrupted = true;
  224. return GC_ERROR;
  225. }
  226. return 0;
  227. }
  228. bool
  229. gc_is_heap_corrupted(gc_handle_t handle)
  230. {
  231. gc_heap_t *heap = (gc_heap_t *)handle;
  232. return heap->is_heap_corrupted ? true : false;
  233. }
  234. #if BH_ENABLE_GC_VERIFY != 0
  235. void
  236. gci_verify_heap(gc_heap_t *heap)
  237. {
  238. hmu_t *cur = NULL, *end = NULL;
  239. bh_assert(heap && gci_is_heap_valid(heap));
  240. cur = (hmu_t *)heap->base_addr;
  241. end = (hmu_t *)(heap->base_addr + heap->current_size);
  242. while (cur < end) {
  243. hmu_verify(heap, cur);
  244. cur = (hmu_t *)((gc_uint8 *)cur + hmu_get_size(cur));
  245. }
  246. bh_assert(cur == end);
  247. }
  248. #endif
  249. void
  250. gc_heap_stat(void *heap_ptr, gc_stat_t *stat)
  251. {
  252. hmu_t *cur = NULL, *end = NULL;
  253. hmu_type_t ut;
  254. gc_size_t size;
  255. gc_heap_t *heap = (gc_heap_t *)heap_ptr;
  256. memset(stat, 0, sizeof(gc_stat_t));
  257. cur = (hmu_t *)heap->base_addr;
  258. end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
  259. while (cur < end) {
  260. ut = hmu_get_ut(cur);
  261. size = hmu_get_size(cur);
  262. bh_assert(size > 0);
  263. if (ut == HMU_FC || ut == HMU_FM
  264. || (ut == HMU_VO && hmu_is_vo_freed(cur))
  265. || (ut == HMU_WO && !hmu_is_wo_marked(cur))) {
  266. if (ut == HMU_VO)
  267. stat->vo_free += size;
  268. if (ut == HMU_WO)
  269. stat->wo_free += size;
  270. stat->free += size;
  271. stat->free_block++;
  272. if (size / sizeof(int) < GC_HEAP_STAT_SIZE - 1)
  273. stat->free_sizes[size / sizeof(int)] += 1;
  274. else
  275. stat->free_sizes[GC_HEAP_STAT_SIZE - 1] += 1;
  276. }
  277. else {
  278. if (ut == HMU_VO)
  279. stat->vo_usage += size;
  280. if (ut == HMU_WO)
  281. stat->wo_usage += size;
  282. stat->usage += size;
  283. stat->usage_block++;
  284. if (size / sizeof(int) < GC_HEAP_STAT_SIZE - 1)
  285. stat->usage_sizes[size / sizeof(int)] += 1;
  286. else
  287. stat->usage_sizes[GC_HEAP_STAT_SIZE - 1] += 1;
  288. }
  289. cur = (hmu_t *)((char *)cur + size);
  290. }
  291. }
  292. void
  293. gc_print_stat(void *heap_ptr, int verbose)
  294. {
  295. gc_stat_t stat;
  296. int i;
  297. bh_assert(heap_ptr != NULL);
  298. gc_heap_t *heap = (gc_heap_t *)(heap_ptr);
  299. gc_heap_stat(heap, &stat);
  300. os_printf("# stat %s %p use %d free %d \n", "instance", heap, stat.usage,
  301. stat.free);
  302. os_printf("# stat %s %p wo_usage %d vo_usage %d \n", "instance", heap,
  303. stat.wo_usage, stat.vo_usage);
  304. os_printf("# stat %s %p wo_free %d vo_free %d \n", "instance", heap,
  305. stat.wo_free, stat.vo_free);
  306. #if WASM_ENABLE_GC == 0
  307. os_printf("# stat free size %" PRIu32 " high %" PRIu32 "\n",
  308. heap->total_free_size, heap->highmark_size);
  309. #else
  310. os_printf("# stat gc %" PRIu32 " free size %" PRIu32 " high %" PRIu32 "\n",
  311. heap->total_gc_count, heap->total_free_size, heap->highmark_size);
  312. #endif
  313. if (verbose) {
  314. os_printf("usage sizes: \n");
  315. for (i = 0; i < GC_HEAP_STAT_SIZE; i++)
  316. if (stat.usage_sizes[i])
  317. os_printf(" %d: %d; ", i * 4, stat.usage_sizes[i]);
  318. os_printf(" \n");
  319. os_printf("free sizes: \n");
  320. for (i = 0; i < GC_HEAP_STAT_SIZE; i++)
  321. if (stat.free_sizes[i])
  322. os_printf(" %d: %d; ", i * 4, stat.free_sizes[i]);
  323. }
  324. }
  325. void *
  326. gc_heap_stats(void *heap_arg, uint32 *stats, int size)
  327. {
  328. int i;
  329. gc_heap_t *heap = (gc_heap_t *)heap_arg;
  330. if (!gci_is_heap_valid(heap)) {
  331. for (i = 0; i < size; i++)
  332. stats[i] = 0;
  333. return NULL;
  334. }
  335. for (i = 0; i < size; i++) {
  336. switch (i) {
  337. case GC_STAT_TOTAL:
  338. stats[i] = heap->current_size;
  339. break;
  340. case GC_STAT_FREE:
  341. stats[i] = heap->total_free_size;
  342. break;
  343. case GC_STAT_HIGHMARK:
  344. stats[i] = heap->highmark_size;
  345. break;
  346. #if WASM_ENABLE_GC != 0
  347. case GC_STAT_COUNT:
  348. stats[i] = heap->total_gc_count;
  349. break;
  350. case GC_STAT_TIME:
  351. stats[i] = heap->total_gc_time;
  352. break;
  353. #endif
  354. default:
  355. break;
  356. }
  357. }
  358. return heap;
  359. }
  360. void
  361. gc_traverse_tree(hmu_tree_node_t *node, gc_size_t *stats, int *n)
  362. {
  363. if (!node)
  364. return;
  365. if (*n > 0)
  366. gc_traverse_tree(node->right, stats, n);
  367. if (*n > 0) {
  368. (*n)--;
  369. stats[*n] = node->size;
  370. }
  371. if (*n > 0)
  372. gc_traverse_tree(node->left, stats, n);
  373. }
  374. void
  375. gc_show_stat(void *heap)
  376. {
  377. uint32 stats[GC_STAT_MAX];
  378. heap = gc_heap_stats(heap, stats, GC_STAT_MAX);
  379. os_printf("\n[GC stats %p] %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32
  380. " %" PRIu32 "\n",
  381. heap, stats[0], stats[1], stats[2], stats[3], stats[4]);
  382. }
  383. #if WASM_ENABLE_GC != 0
  384. void
  385. gc_show_fragment(void *heap_arg)
  386. {
  387. int stats[3];
  388. int n = 3;
  389. gc_heap_t *heap = (gc_heap_t *)heap_arg;
  390. memset(stats, 0, n * sizeof(int));
  391. gct_vm_mutex_lock(&heap->lock);
  392. gc_traverse_tree(heap->kfc_tree_root, (gc_size_t *)stats, &n);
  393. gct_vm_mutex_unlock(&heap->lock);
  394. os_printf("\n[GC %p top sizes] %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", heap,
  395. stats[0], stats[1], stats[2]);
  396. }
  397. #endif