backgraph.c 15 KB

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