/vendor/gc/backgraph.c

http://github.com/feyeleanor/RubyGoLightly · C · 475 lines · 335 code · 44 blank · 96 comment · 96 complexity · 49192b819842ef56c3c372456494e460 MD5 · raw file

  1. /*
  2. * Copyright (c) 2001 by Hewlett-Packard Company. All rights reserved.
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to use or copy this program
  8. * for any purpose, provided the above notices are retained on all copies.
  9. * Permission to modify the code and to distribute modified code is granted,
  10. * provided the above notices are retained, and a notice that the code was
  11. * modified is included with the above copyright notice.
  12. *
  13. */
  14. /*
  15. * This implements a full, though not well-tuned, representation of the
  16. * backwards points-to graph. This is used to test for non-GC-robust
  17. * data structures; the code is not used during normal garbage collection.
  18. *
  19. * One restriction is that we drop all back-edges from nodes with very
  20. * high in-degree, and simply add them add them to a list of such
  21. * nodes. They are then treated as permanent roots. Id this by itself
  22. * doesn't introduce a space leak, then such nodes can't contribute to
  23. * a growing space leak.
  24. */
  25. #ifdef MAKE_BACK_GRAPH
  26. #define MAX_IN 10 /* Maximum in-degree we handle directly */
  27. #include "private/dbg_mlc.h"
  28. #include <unistd.h>
  29. #if !defined(DBG_HDRS_ALL) || (ALIGNMENT != CPP_WORDSZ/8) || !defined(UNIX_LIKE)
  30. # error Configuration doesnt support MAKE_BACK_GRAPH
  31. #endif
  32. /* We store single back pointers directly in the object's oh_bg_ptr field. */
  33. /* If there is more than one ptr to an object, we store q | FLAG_MANY, */
  34. /* where q is a pointer to a back_edges object. */
  35. /* Every once in a while we use a back_edges object even for a single */
  36. /* pointer, since we need the other fields in the back_edges structure to */
  37. /* be present in some fraction of the objects. Otherwise we get serious */
  38. /* performance issues. */
  39. #define FLAG_MANY 2
  40. typedef struct back_edges_struct {
  41. word n_edges; /* Number of edges, including those in continuation */
  42. /* structures. */
  43. unsigned short flags;
  44. # define RETAIN 1 /* Directly points to a reachable object; */
  45. /* retain for next GC. */
  46. unsigned short height_gc_no;
  47. /* If height > 0, then the GC_gc_no value when it */
  48. /* was computed. If it was computed this cycle, then */
  49. /* it is current. If it was computed during the */
  50. /* last cycle, then it represents the old height, */
  51. /* which is only saved for live objects referenced by */
  52. /* dead ones. This may grow due to refs from newly */
  53. /* dead objects. */
  54. signed_word height;
  55. /* Longest path through unreachable nodes to this node */
  56. /* that we found using depth first search. */
  57. # define HEIGHT_UNKNOWN ((signed_word)(-2))
  58. # define HEIGHT_IN_PROGRESS ((signed_word)(-1))
  59. ptr_t edges[MAX_IN];
  60. struct back_edges_struct *cont;
  61. /* Pointer to continuation structure; we use only the */
  62. /* edges field in the continuation. */
  63. /* also used as free list link. */
  64. } back_edges;
  65. /* Allocate a new back edge structure. Should be more sophisticated */
  66. /* if this were production code. */
  67. #define MAX_BACK_EDGE_STRUCTS 100000
  68. static back_edges *back_edge_space = 0;
  69. int GC_n_back_edge_structs = 0; /* Serves as pointer to never used */
  70. /* back_edges space. */
  71. static back_edges *avail_back_edges = 0;
  72. /* Pointer to free list of deallocated */
  73. /* back_edges structures. */
  74. static back_edges * new_back_edges(void)
  75. {
  76. if (0 == back_edge_space) {
  77. back_edge_space = (back_edges *)
  78. GET_MEM(MAX_BACK_EDGE_STRUCTS*sizeof(back_edges));
  79. GC_add_to_our_memory((ptr_t)back_edge_space,
  80. MAX_BACK_EDGE_STRUCTS*sizeof(back_edges));
  81. }
  82. if (0 != avail_back_edges) {
  83. back_edges * result = avail_back_edges;
  84. avail_back_edges = result -> cont;
  85. result -> cont = 0;
  86. return result;
  87. }
  88. if (GC_n_back_edge_structs >= MAX_BACK_EDGE_STRUCTS - 1) {
  89. ABORT("needed too much space for back edges: adjust "
  90. "MAX_BACK_EDGE_STRUCTS");
  91. }
  92. return back_edge_space + (GC_n_back_edge_structs++);
  93. }
  94. /* Deallocate p and its associated continuation structures. */
  95. static void deallocate_back_edges(back_edges *p)
  96. {
  97. back_edges *last = p;
  98. while (0 != last -> cont) last = last -> cont;
  99. last -> cont = avail_back_edges;
  100. avail_back_edges = p;
  101. }
  102. /* Table of objects that are currently on the depth-first search */
  103. /* stack. Only objects with in-degree one are in this table. */
  104. /* Other objects are identified using HEIGHT_IN_PROGRESS. */
  105. /* FIXME: This data structure NEEDS IMPROVEMENT. */
  106. #define INITIAL_IN_PROGRESS 10000
  107. static ptr_t * in_progress_space = 0;
  108. static size_t in_progress_size = 0;
  109. static size_t n_in_progress = 0;
  110. static void push_in_progress(ptr_t p)
  111. {
  112. if (n_in_progress >= in_progress_size)
  113. if (in_progress_size == 0) {
  114. in_progress_size = INITIAL_IN_PROGRESS;
  115. in_progress_space = (ptr_t *)GET_MEM(in_progress_size * sizeof(ptr_t));
  116. GC_add_to_our_memory((ptr_t)in_progress_space,
  117. in_progress_size * sizeof(ptr_t));
  118. } else {
  119. ptr_t * new_in_progress_space;
  120. in_progress_size *= 2;
  121. new_in_progress_space = (ptr_t *)
  122. GET_MEM(in_progress_size * sizeof(ptr_t));
  123. GC_add_to_our_memory((ptr_t)new_in_progress_space,
  124. in_progress_size * sizeof(ptr_t));
  125. BCOPY(in_progress_space, new_in_progress_space,
  126. n_in_progress * sizeof(ptr_t));
  127. in_progress_space = new_in_progress_space;
  128. /* FIXME: This just drops the old space. */
  129. }
  130. if (in_progress_space == 0)
  131. ABORT("MAKE_BACK_GRAPH: Out of in-progress space: "
  132. "Huge linear data structure?");
  133. in_progress_space[n_in_progress++] = p;
  134. }
  135. static GC_bool is_in_progress(ptr_t p)
  136. {
  137. int i;
  138. for (i = 0; i < n_in_progress; ++i) {
  139. if (in_progress_space[i] == p) return TRUE;
  140. }
  141. return FALSE;
  142. }
  143. static void pop_in_progress(ptr_t p)
  144. {
  145. --n_in_progress;
  146. GC_ASSERT(in_progress_space[n_in_progress] == p);
  147. }
  148. #define GET_OH_BG_PTR(p) \
  149. (ptr_t)REVEAL_POINTER(((oh *)(p)) -> oh_bg_ptr)
  150. #define SET_OH_BG_PTR(p,q) (((oh *)(p)) -> oh_bg_ptr) = HIDE_POINTER(q)
  151. /* Execute s once for each predecessor q of p in the points-to graph. */
  152. /* s should be a bracketed statement. We declare q. */
  153. #define FOR_EACH_PRED(q, p, s) \
  154. { \
  155. ptr_t q = GET_OH_BG_PTR(p); \
  156. if (!((word)q & FLAG_MANY)) { \
  157. if (q && !((word)q & 1)) s \
  158. /* !((word)q & 1) checks for a misnterpreted freelist link */ \
  159. } else { \
  160. back_edges *orig_be_ = (back_edges *)((word)q & ~FLAG_MANY); \
  161. back_edges *be_ = orig_be_; \
  162. int total_, local_; \
  163. int n_edges_ = be_ -> n_edges; \
  164. for (total_ = 0, local_ = 0; total_ < n_edges_; ++local_, ++total_) { \
  165. if (local_ == MAX_IN) { \
  166. be_ = be_ -> cont; \
  167. local_ = 0; \
  168. } \
  169. q = be_ -> edges[local_]; s \
  170. } \
  171. } \
  172. }
  173. /* Ensure that p has a back_edges structure associated with it. */
  174. static void ensure_struct(ptr_t p)
  175. {
  176. ptr_t old_back_ptr = GET_OH_BG_PTR(p);
  177. if (!((word)old_back_ptr & FLAG_MANY)) {
  178. back_edges *be = new_back_edges();
  179. be -> flags = 0;
  180. if (0 == old_back_ptr) {
  181. be -> n_edges = 0;
  182. } else {
  183. be -> n_edges = 1;
  184. be -> edges[0] = old_back_ptr;
  185. }
  186. be -> height = HEIGHT_UNKNOWN;
  187. be -> height_gc_no = GC_gc_no - 1;
  188. GC_ASSERT(be >= back_edge_space);
  189. SET_OH_BG_PTR(p, (word)be | FLAG_MANY);
  190. }
  191. }
  192. /* Add the (forward) edge from p to q to the backward graph. Both p */
  193. /* q are pointers to the object base, i.e. pointers to an oh. */
  194. static void add_edge(ptr_t p, ptr_t q)
  195. {
  196. ptr_t old_back_ptr = GET_OH_BG_PTR(q);
  197. back_edges * be, *be_cont;
  198. word i;
  199. static unsigned random_number = 13;
  200. # define GOT_LUCKY_NUMBER (((++random_number) & 0x7f) == 0)
  201. /* A not very random number we use to occasionally allocate a */
  202. /* back_edges structure even for a single backward edge. This */
  203. /* prevents us from repeatedly tracing back through very long */
  204. /* chains, since we will have some place to store height and */
  205. /* in_progress flags along the way. */
  206. GC_ASSERT(p == GC_base(p) && q == GC_base(q));
  207. if (!GC_HAS_DEBUG_INFO(q) || !GC_HAS_DEBUG_INFO(p)) {
  208. /* This is really a misinterpreted free list link, since we saw */
  209. /* a pointer to a free list. Dont overwrite it! */
  210. return;
  211. }
  212. if (0 == old_back_ptr) {
  213. SET_OH_BG_PTR(q, p);
  214. if (GOT_LUCKY_NUMBER) ensure_struct(q);
  215. return;
  216. }
  217. /* Check whether it was already in the list of predecessors. */
  218. FOR_EACH_PRED(pred, q, { if (p == pred) return; });
  219. ensure_struct(q);
  220. old_back_ptr = GET_OH_BG_PTR(q);
  221. be = (back_edges *)((word)old_back_ptr & ~FLAG_MANY);
  222. for (i = be -> n_edges, be_cont = be; i > MAX_IN;
  223. be_cont = be_cont -> cont, i -= MAX_IN) {}
  224. if (i == MAX_IN) {
  225. be_cont -> cont = new_back_edges();
  226. be_cont = be_cont -> cont;
  227. i = 0;
  228. }
  229. be_cont -> edges[i] = p;
  230. be -> n_edges++;
  231. if (be -> n_edges == 100) {
  232. # if 0
  233. if (GC_print_stats) {
  234. GC_err_printf("The following object has in-degree >= 100:\n");
  235. GC_print_heap_obj(q);
  236. }
  237. # endif
  238. }
  239. }
  240. typedef void (*per_object_func)(ptr_t p, size_t n_bytes, word gc_descr);
  241. static void per_object_helper(struct hblk *h, word fn)
  242. {
  243. hdr * hhdr = HDR(h);
  244. size_t sz = hhdr -> hb_sz;
  245. word descr = hhdr -> hb_descr;
  246. per_object_func f = (per_object_func)fn;
  247. int i = 0;
  248. do {
  249. f((ptr_t)(h -> hb_body + i), sz, descr);
  250. i += sz;
  251. } while (i + sz <= BYTES_TO_WORDS(HBLKSIZE));
  252. }
  253. void GC_apply_to_each_object(per_object_func f)
  254. {
  255. GC_apply_to_all_blocks(per_object_helper, (word)f);
  256. }
  257. static void reset_back_edge(ptr_t p, size_t n_bytes, word gc_descr)
  258. {
  259. /* Skip any free list links, or dropped blocks */
  260. if (GC_HAS_DEBUG_INFO(p)) {
  261. ptr_t old_back_ptr = GET_OH_BG_PTR(p);
  262. if ((word)old_back_ptr & FLAG_MANY) {
  263. back_edges *be = (back_edges *)((word)old_back_ptr & ~FLAG_MANY);
  264. if (!(be -> flags & RETAIN)) {
  265. deallocate_back_edges(be);
  266. SET_OH_BG_PTR(p, 0);
  267. } else {
  268. word *currentp;
  269. GC_ASSERT(GC_is_marked(p));
  270. /* Back edges may point to objects that will not be retained. */
  271. /* Delete them for now, but remember the height. */
  272. /* Some will be added back at next GC. */
  273. be -> n_edges = 0;
  274. if (0 != be -> cont) {
  275. deallocate_back_edges(be -> cont);
  276. be -> cont = 0;
  277. }
  278. GC_ASSERT(GC_is_marked(p));
  279. /* We only retain things for one GC cycle at a time. */
  280. be -> flags &= ~RETAIN;
  281. }
  282. } else /* Simple back pointer */ {
  283. /* Clear to avoid dangling pointer. */
  284. SET_OH_BG_PTR(p, 0);
  285. }
  286. }
  287. }
  288. static void add_back_edges(ptr_t p, size_t n_bytes, word gc_descr)
  289. {
  290. word *currentp = (word *)(p + sizeof(oh));
  291. /* For now, fix up non-length descriptors conservatively. */
  292. if((gc_descr & GC_DS_TAGS) != GC_DS_LENGTH) {
  293. gc_descr = n_bytes;
  294. }
  295. while (currentp < (word *)(p + gc_descr)) {
  296. word current = *currentp++;
  297. FIXUP_POINTER(current);
  298. if (current >= (word)GC_least_plausible_heap_addr &&
  299. current <= (word)GC_greatest_plausible_heap_addr) {
  300. ptr_t target = GC_base((void *)current);
  301. if (0 != target) {
  302. add_edge(p, target);
  303. }
  304. }
  305. }
  306. }
  307. /* Rebuild the representation of the backward reachability graph. */
  308. /* Does not examine mark bits. Can be called before GC. */
  309. void GC_build_back_graph(void)
  310. {
  311. GC_apply_to_each_object(add_back_edges);
  312. }
  313. /* Return an approximation to the length of the longest simple path */
  314. /* through unreachable objects to p. We refer to this as the height */
  315. /* of p. */
  316. static word backwards_height(ptr_t p)
  317. {
  318. word result;
  319. ptr_t back_ptr = GET_OH_BG_PTR(p);
  320. back_edges *be;
  321. if (0 == back_ptr) return 1;
  322. if (!((word)back_ptr & FLAG_MANY)) {
  323. if (is_in_progress(p)) return 0; /* DFS back edge, i.e. we followed */
  324. /* an edge to an object already */
  325. /* on our stack: ignore */
  326. push_in_progress(p);
  327. result = backwards_height(back_ptr)+1;
  328. pop_in_progress(p);
  329. return result;
  330. }
  331. be = (back_edges *)((word)back_ptr & ~FLAG_MANY);
  332. if (be -> height >= 0 && be -> height_gc_no == GC_gc_no)
  333. return be -> height;
  334. /* Ignore back edges in DFS */
  335. if (be -> height == HEIGHT_IN_PROGRESS) return 0;
  336. result = (be -> height > 0? be -> height : 1);
  337. be -> height = HEIGHT_IN_PROGRESS;
  338. FOR_EACH_PRED(q, p, {
  339. word this_height;
  340. if (GC_is_marked(q) && !(FLAG_MANY & (word)GET_OH_BG_PTR(p))) {
  341. if (GC_print_stats)
  342. GC_log_printf("Found bogus pointer from 0x%lx to 0x%lx\n", q, p);
  343. /* Reachable object "points to" unreachable one. */
  344. /* Could be caused by our lax treatment of GC descriptors. */
  345. this_height = 1;
  346. } else {
  347. this_height = backwards_height(q);
  348. }
  349. if (this_height >= result) result = this_height + 1;
  350. });
  351. be -> height = result;
  352. be -> height_gc_no = GC_gc_no;
  353. return result;
  354. }
  355. word GC_max_height;
  356. ptr_t GC_deepest_obj;
  357. /* Compute the maximum height of every unreachable predecessor p of a */
  358. /* reachable object. Arrange to save the heights of all such objects p */
  359. /* so that they can be used in calculating the height of objects in the */
  360. /* next GC. */
  361. /* Set GC_max_height to be the maximum height we encounter, and */
  362. /* GC_deepest_obj to be the corresponding object. */
  363. static void update_max_height(ptr_t p, size_t n_bytes, word gc_descr)
  364. {
  365. if (GC_is_marked(p) && GC_HAS_DEBUG_INFO(p)) {
  366. int i;
  367. word p_height = 0;
  368. ptr_t p_deepest_obj = 0;
  369. ptr_t back_ptr;
  370. back_edges *be = 0;
  371. /* If we remembered a height last time, use it as a minimum. */
  372. /* It may have increased due to newly unreachable chains pointing */
  373. /* to p, but it can't have decreased. */
  374. back_ptr = GET_OH_BG_PTR(p);
  375. if (0 != back_ptr && ((word)back_ptr & FLAG_MANY)) {
  376. be = (back_edges *)((word)back_ptr & ~FLAG_MANY);
  377. if (be -> height != HEIGHT_UNKNOWN) p_height = be -> height;
  378. }
  379. FOR_EACH_PRED(q, p, {
  380. if (!GC_is_marked(q) && GC_HAS_DEBUG_INFO(q)) {
  381. word q_height;
  382. q_height = backwards_height(q);
  383. if (q_height > p_height) {
  384. p_height = q_height;
  385. p_deepest_obj = q;
  386. }
  387. }
  388. });
  389. if (p_height > 0) {
  390. /* Remember the height for next time. */
  391. if (be == 0) {
  392. ensure_struct(p);
  393. back_ptr = GET_OH_BG_PTR(p);
  394. be = (back_edges *)((word)back_ptr & ~FLAG_MANY);
  395. }
  396. be -> flags |= RETAIN;
  397. be -> height = p_height;
  398. be -> height_gc_no = GC_gc_no;
  399. }
  400. if (p_height > GC_max_height) {
  401. GC_max_height = p_height;
  402. GC_deepest_obj = p_deepest_obj;
  403. }
  404. }
  405. }
  406. word GC_max_max_height = 0;
  407. void GC_traverse_back_graph(void)
  408. {
  409. GC_max_height = 0;
  410. GC_apply_to_each_object(update_max_height);
  411. if (0 != GC_deepest_obj)
  412. GC_set_mark_bit(GC_deepest_obj); /* Keep it until we can print it. */
  413. }
  414. void GC_print_back_graph_stats(void)
  415. {
  416. GC_printf("Maximum backwards height of reachable objects at GC %lu is %ld\n",
  417. (unsigned long) GC_gc_no, (unsigned long)GC_max_height);
  418. if (GC_max_height > GC_max_max_height) {
  419. GC_max_max_height = GC_max_height;
  420. GC_printf("The following unreachable object is last in a longest chain "
  421. "of unreachable objects:\n");
  422. GC_print_heap_obj(GC_deepest_obj);
  423. }
  424. if (GC_print_stats) {
  425. GC_log_printf("Needed max total of %ld back-edge structs\n",
  426. GC_n_back_edge_structs);
  427. }
  428. GC_apply_to_each_object(reset_back_edge);
  429. GC_deepest_obj = 0;
  430. }
  431. #endif /* MAKE_BACK_GRAPH */