ems_kfc.c 13 KB

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