backgraph.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. #include "private/dbg_mlc.h"
  15. /*
  16. * This implements a full, though not well-tuned, representation of the
  17. * backwards points-to graph. This is used to test for non-GC-robust
  18. * data structures; the code is not used during normal garbage collection.
  19. *
  20. * One restriction is that we drop all back-edges from nodes with very
  21. * high in-degree, and simply add them add them to a list of such
  22. * nodes. They are then treated as permanent roots. Id this by itself
  23. * doesn't introduce a space leak, then such nodes can't contribute to
  24. * a growing space leak.
  25. */
  26. #ifdef MAKE_BACK_GRAPH
  27. #define MAX_IN 10 /* Maximum in-degree we handle directly */
  28. /* #include <unistd.h> */
  29. #if !defined(DBG_HDRS_ALL) || (ALIGNMENT != CPP_WORDSZ/8) /* || !defined(UNIX_LIKE) */
  30. # error The configuration does not 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. STATIC int GC_n_back_edge_structs = 0;
  70. /* 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. size_t bytes_to_get = ROUNDUP_PAGESIZE_IF_MMAP(MAX_BACK_EDGE_STRUCTS
  79. * sizeof(back_edges));
  80. back_edge_space = (back_edges *)GET_MEM(bytes_to_get);
  81. if (NULL == back_edge_space)
  82. ABORT("Insufficient memory for back edges");
  83. GC_add_to_our_memory((ptr_t)back_edge_space, bytes_to_get);
  84. }
  85. if (0 != avail_back_edges) {
  86. back_edges * result = avail_back_edges;
  87. avail_back_edges = result -> cont;
  88. result -> cont = 0;
  89. return result;
  90. }
  91. if (GC_n_back_edge_structs >= MAX_BACK_EDGE_STRUCTS - 1) {
  92. ABORT("Needed too much space for back edges: adjust "
  93. "MAX_BACK_EDGE_STRUCTS");
  94. }
  95. return back_edge_space + (GC_n_back_edge_structs++);
  96. }
  97. /* Deallocate p and its associated continuation structures. */
  98. static void deallocate_back_edges(back_edges *p)
  99. {
  100. back_edges *last = p;
  101. while (0 != last -> cont) last = last -> cont;
  102. last -> cont = avail_back_edges;
  103. avail_back_edges = p;
  104. }
  105. /* Table of objects that are currently on the depth-first search */
  106. /* stack. Only objects with in-degree one are in this table. */
  107. /* Other objects are identified using HEIGHT_IN_PROGRESS. */
  108. /* FIXME: This data structure NEEDS IMPROVEMENT. */
  109. #define INITIAL_IN_PROGRESS 10000
  110. static ptr_t * in_progress_space = 0;
  111. static size_t in_progress_size = 0;
  112. static size_t n_in_progress = 0;
  113. static void push_in_progress(ptr_t p)
  114. {
  115. if (n_in_progress >= in_progress_size) {
  116. ptr_t * new_in_progress_space;
  117. if (NULL == in_progress_space) {
  118. in_progress_size = ROUNDUP_PAGESIZE_IF_MMAP(INITIAL_IN_PROGRESS
  119. * sizeof(ptr_t))
  120. / sizeof(ptr_t);
  121. new_in_progress_space =
  122. (ptr_t *)GET_MEM(in_progress_size * sizeof(ptr_t));
  123. } else {
  124. in_progress_size *= 2;
  125. new_in_progress_space = (ptr_t *)
  126. GET_MEM(in_progress_size * sizeof(ptr_t));
  127. if (new_in_progress_space != NULL)
  128. BCOPY(in_progress_space, new_in_progress_space,
  129. n_in_progress * sizeof(ptr_t));
  130. }
  131. GC_add_to_our_memory((ptr_t)new_in_progress_space,
  132. in_progress_size * sizeof(ptr_t));
  133. # ifndef GWW_VDB
  134. GC_scratch_recycle_no_gww(in_progress_space,
  135. n_in_progress * sizeof(ptr_t));
  136. # elif defined(LINT2)
  137. /* TODO: implement GWW-aware recycling as in alloc_mark_stack */
  138. GC_noop1((word)in_progress_space);
  139. # endif
  140. in_progress_space = new_in_progress_space;
  141. }
  142. if (in_progress_space == 0)
  143. ABORT("MAKE_BACK_GRAPH: Out of in-progress space: "
  144. "Huge linear data structure?");
  145. in_progress_space[n_in_progress++] = p;
  146. }
  147. static GC_bool is_in_progress(ptr_t p)
  148. {
  149. size_t i;
  150. for (i = 0; i < n_in_progress; ++i) {
  151. if (in_progress_space[i] == p) return TRUE;
  152. }
  153. return FALSE;
  154. }
  155. GC_INLINE void pop_in_progress(ptr_t p GC_ATTR_UNUSED)
  156. {
  157. --n_in_progress;
  158. GC_ASSERT(in_progress_space[n_in_progress] == p);
  159. }
  160. #define GET_OH_BG_PTR(p) \
  161. (ptr_t)GC_REVEAL_POINTER(((oh *)(p)) -> oh_bg_ptr)
  162. #define SET_OH_BG_PTR(p,q) (((oh *)(p)) -> oh_bg_ptr = GC_HIDE_POINTER(q))
  163. /* Execute s once for each predecessor q of p in the points-to graph. */
  164. /* s should be a bracketed statement. We declare q. */
  165. #define FOR_EACH_PRED(q, p, s) \
  166. do { \
  167. ptr_t q = GET_OH_BG_PTR(p); \
  168. if (!((word)q & FLAG_MANY)) { \
  169. if (q && !((word)q & 1)) s \
  170. /* !((word)q & 1) checks for a misinterpreted freelist link */ \
  171. } else { \
  172. back_edges *orig_be_ = (back_edges *)((word)q & ~FLAG_MANY); \
  173. back_edges *be_ = orig_be_; \
  174. int local_; \
  175. word total_; \
  176. word n_edges_ = be_ -> n_edges; \
  177. for (total_ = 0, local_ = 0; total_ < n_edges_; ++local_, ++total_) { \
  178. if (local_ == MAX_IN) { \
  179. be_ = be_ -> cont; \
  180. local_ = 0; \
  181. } \
  182. q = be_ -> edges[local_]; s \
  183. } \
  184. } \
  185. } while (0)
  186. /* Ensure that p has a back_edges structure associated with it. */
  187. static void ensure_struct(ptr_t p)
  188. {
  189. ptr_t old_back_ptr = GET_OH_BG_PTR(p);
  190. if (!((word)old_back_ptr & FLAG_MANY)) {
  191. back_edges *be = new_back_edges();
  192. be -> flags = 0;
  193. if (0 == old_back_ptr) {
  194. be -> n_edges = 0;
  195. } else {
  196. be -> n_edges = 1;
  197. be -> edges[0] = old_back_ptr;
  198. }
  199. be -> height = HEIGHT_UNKNOWN;
  200. be -> height_gc_no = (unsigned short)(GC_gc_no - 1);
  201. GC_ASSERT((word)be >= (word)back_edge_space);
  202. SET_OH_BG_PTR(p, (word)be | FLAG_MANY);
  203. }
  204. }
  205. /* Add the (forward) edge from p to q to the backward graph. Both p */
  206. /* q are pointers to the object base, i.e. pointers to an oh. */
  207. static void add_edge(ptr_t p, ptr_t q)
  208. {
  209. ptr_t old_back_ptr = GET_OH_BG_PTR(q);
  210. back_edges * be, *be_cont;
  211. word i;
  212. GC_ASSERT(p == GC_base(p) && q == GC_base(q));
  213. if (!GC_HAS_DEBUG_INFO(q) || !GC_HAS_DEBUG_INFO(p)) {
  214. /* This is really a misinterpreted free list link, since we saw */
  215. /* a pointer to a free list. Don't overwrite it! */
  216. return;
  217. }
  218. if (0 == old_back_ptr) {
  219. static unsigned random_number = 13;
  220. # define GOT_LUCKY_NUMBER (((++random_number) & 0x7f) == 0)
  221. /* A not very random number we use to occasionally allocate a */
  222. /* back_edges structure even for a single backward edge. This */
  223. /* prevents us from repeatedly tracing back through very long */
  224. /* chains, since we will have some place to store height and */
  225. /* in_progress flags along the way. */
  226. SET_OH_BG_PTR(q, p);
  227. if (GOT_LUCKY_NUMBER) ensure_struct(q);
  228. return;
  229. }
  230. /* Check whether it was already in the list of predecessors. */
  231. FOR_EACH_PRED(pred, q, { if (p == pred) return; });
  232. ensure_struct(q);
  233. old_back_ptr = GET_OH_BG_PTR(q);
  234. be = (back_edges *)((word)old_back_ptr & ~FLAG_MANY);
  235. for (i = be -> n_edges, be_cont = be; i > MAX_IN; i -= MAX_IN)
  236. be_cont = be_cont -> cont;
  237. if (i == MAX_IN) {
  238. be_cont -> cont = new_back_edges();
  239. be_cont = be_cont -> cont;
  240. i = 0;
  241. }
  242. be_cont -> edges[i] = p;
  243. be -> n_edges++;
  244. # ifdef DEBUG_PRINT_BIG_N_EDGES
  245. if (GC_print_stats == VERBOSE && be -> n_edges == 100) {
  246. GC_err_printf("The following object has big in-degree:\n");
  247. GC_print_heap_obj(q);
  248. }
  249. # endif
  250. }
  251. typedef void (*per_object_func)(ptr_t p, size_t n_bytes, word gc_descr);
  252. static void per_object_helper(struct hblk *h, word fn)
  253. {
  254. hdr * hhdr = HDR(h);
  255. size_t sz = (size_t)hhdr->hb_sz;
  256. word descr = hhdr -> hb_descr;
  257. per_object_func f = (per_object_func)fn;
  258. size_t i = 0;
  259. do {
  260. f((ptr_t)(h -> hb_body + i), sz, descr);
  261. i += sz;
  262. } while (i + sz <= BYTES_TO_WORDS(HBLKSIZE));
  263. }
  264. GC_INLINE void GC_apply_to_each_object(per_object_func f)
  265. {
  266. GC_apply_to_all_blocks(per_object_helper, (word)f);
  267. }
  268. static void reset_back_edge(ptr_t p, size_t n_bytes GC_ATTR_UNUSED,
  269. word gc_descr GC_ATTR_UNUSED)
  270. {
  271. /* Skip any free list links, or dropped blocks */
  272. if (GC_HAS_DEBUG_INFO(p)) {
  273. ptr_t old_back_ptr = GET_OH_BG_PTR(p);
  274. if ((word)old_back_ptr & FLAG_MANY) {
  275. back_edges *be = (back_edges *)((word)old_back_ptr & ~FLAG_MANY);
  276. if (!(be -> flags & RETAIN)) {
  277. deallocate_back_edges(be);
  278. SET_OH_BG_PTR(p, 0);
  279. } else {
  280. GC_ASSERT(GC_is_marked(p));
  281. /* Back edges may point to objects that will not be retained. */
  282. /* Delete them for now, but remember the height. */
  283. /* Some will be added back at next GC. */
  284. be -> n_edges = 0;
  285. if (0 != be -> cont) {
  286. deallocate_back_edges(be -> cont);
  287. be -> cont = 0;
  288. }
  289. GC_ASSERT(GC_is_marked(p));
  290. /* We only retain things for one GC cycle at a time. */
  291. be -> flags &= ~RETAIN;
  292. }
  293. } else /* Simple back pointer */ {
  294. /* Clear to avoid dangling pointer. */
  295. SET_OH_BG_PTR(p, 0);
  296. }
  297. }
  298. }
  299. static void add_back_edges(ptr_t p, size_t n_bytes, word gc_descr)
  300. {
  301. word *currentp = (word *)(p + sizeof(oh));
  302. /* For now, fix up non-length descriptors conservatively. */
  303. if((gc_descr & GC_DS_TAGS) != GC_DS_LENGTH) {
  304. gc_descr = n_bytes;
  305. }
  306. while ((word)currentp < (word)(p + gc_descr)) {
  307. word current = *currentp++;
  308. FIXUP_POINTER(current);
  309. if (current >= (word)GC_least_plausible_heap_addr &&
  310. current <= (word)GC_greatest_plausible_heap_addr) {
  311. ptr_t target = (ptr_t)GC_base((void *)current);
  312. if (0 != target) {
  313. add_edge(p, target);
  314. }
  315. }
  316. }
  317. }
  318. /* Rebuild the representation of the backward reachability graph. */
  319. /* Does not examine mark bits. Can be called before GC. */
  320. GC_INNER void GC_build_back_graph(void)
  321. {
  322. GC_ASSERT(I_HOLD_LOCK());
  323. GC_apply_to_each_object(add_back_edges);
  324. }
  325. /* Return an approximation to the length of the longest simple path */
  326. /* through unreachable objects to p. We refer to this as the height */
  327. /* of p. */
  328. static word backwards_height(ptr_t p)
  329. {
  330. word result;
  331. ptr_t back_ptr = GET_OH_BG_PTR(p);
  332. back_edges *be;
  333. if (0 == back_ptr) return 1;
  334. if (!((word)back_ptr & FLAG_MANY)) {
  335. if (is_in_progress(p)) return 0; /* DFS back edge, i.e. we followed */
  336. /* an edge to an object already */
  337. /* on our stack: ignore */
  338. push_in_progress(p);
  339. result = backwards_height(back_ptr)+1;
  340. pop_in_progress(p);
  341. return result;
  342. }
  343. be = (back_edges *)((word)back_ptr & ~FLAG_MANY);
  344. if (be -> height >= 0 && be -> height_gc_no == (unsigned short)GC_gc_no)
  345. return be -> height;
  346. /* Ignore back edges in DFS */
  347. if (be -> height == HEIGHT_IN_PROGRESS) return 0;
  348. result = (be -> height > 0? be -> height : 1);
  349. be -> height = HEIGHT_IN_PROGRESS;
  350. FOR_EACH_PRED(q, p, {
  351. word this_height;
  352. if (GC_is_marked(q) && !(FLAG_MANY & (word)GET_OH_BG_PTR(p))) {
  353. GC_COND_LOG_PRINTF("Found bogus pointer from %p to %p\n",
  354. (void *)q, (void *)p);
  355. /* Reachable object "points to" unreachable one. */
  356. /* Could be caused by our lax treatment of GC descriptors. */
  357. this_height = 1;
  358. } else {
  359. this_height = backwards_height(q);
  360. }
  361. if (this_height >= result) result = this_height + 1;
  362. });
  363. be -> height = result;
  364. be -> height_gc_no = (unsigned short)GC_gc_no;
  365. return result;
  366. }
  367. STATIC word GC_max_height = 0;
  368. STATIC ptr_t GC_deepest_obj = NULL;
  369. /* Compute the maximum height of every unreachable predecessor p of a */
  370. /* reachable object. Arrange to save the heights of all such objects p */
  371. /* so that they can be used in calculating the height of objects in the */
  372. /* next GC. */
  373. /* Set GC_max_height to be the maximum height we encounter, and */
  374. /* GC_deepest_obj to be the corresponding object. */
  375. static void update_max_height(ptr_t p, size_t n_bytes GC_ATTR_UNUSED,
  376. word gc_descr GC_ATTR_UNUSED)
  377. {
  378. if (GC_is_marked(p) && GC_HAS_DEBUG_INFO(p)) {
  379. word p_height = 0;
  380. ptr_t p_deepest_obj = 0;
  381. ptr_t back_ptr;
  382. back_edges *be = 0;
  383. /* If we remembered a height last time, use it as a minimum. */
  384. /* It may have increased due to newly unreachable chains pointing */
  385. /* to p, but it can't have decreased. */
  386. back_ptr = GET_OH_BG_PTR(p);
  387. if (0 != back_ptr && ((word)back_ptr & FLAG_MANY)) {
  388. be = (back_edges *)((word)back_ptr & ~FLAG_MANY);
  389. if (be -> height != HEIGHT_UNKNOWN) p_height = be -> height;
  390. }
  391. FOR_EACH_PRED(q, p, {
  392. if (!GC_is_marked(q) && GC_HAS_DEBUG_INFO(q)) {
  393. word q_height;
  394. q_height = backwards_height(q);
  395. if (q_height > p_height) {
  396. p_height = q_height;
  397. p_deepest_obj = q;
  398. }
  399. }
  400. });
  401. if (p_height > 0) {
  402. /* Remember the height for next time. */
  403. if (be == 0) {
  404. ensure_struct(p);
  405. back_ptr = GET_OH_BG_PTR(p);
  406. be = (back_edges *)((word)back_ptr & ~FLAG_MANY);
  407. }
  408. be -> flags |= RETAIN;
  409. be -> height = p_height;
  410. be -> height_gc_no = (unsigned short)GC_gc_no;
  411. }
  412. if (p_height > GC_max_height) {
  413. GC_max_height = p_height;
  414. GC_deepest_obj = p_deepest_obj;
  415. }
  416. }
  417. }
  418. STATIC word GC_max_max_height = 0;
  419. GC_INNER void GC_traverse_back_graph(void)
  420. {
  421. GC_ASSERT(I_HOLD_LOCK());
  422. GC_max_height = 0;
  423. GC_apply_to_each_object(update_max_height);
  424. if (0 != GC_deepest_obj)
  425. GC_set_mark_bit(GC_deepest_obj); /* Keep it until we can print it. */
  426. }
  427. void GC_print_back_graph_stats(void)
  428. {
  429. GC_ASSERT(I_HOLD_LOCK());
  430. GC_printf("Maximum backwards height of reachable objects at GC %lu is %lu\n",
  431. (unsigned long) GC_gc_no, (unsigned long)GC_max_height);
  432. if (GC_max_height > GC_max_max_height) {
  433. ptr_t obj = GC_deepest_obj;
  434. GC_max_max_height = GC_max_height;
  435. UNLOCK();
  436. GC_err_printf(
  437. "The following unreachable object is last in a longest chain "
  438. "of unreachable objects:\n");
  439. GC_print_heap_obj(obj);
  440. LOCK();
  441. }
  442. GC_COND_LOG_PRINTF("Needed max total of %d back-edge structs\n",
  443. GC_n_back_edge_structs);
  444. GC_apply_to_each_object(reset_back_edge);
  445. GC_deepest_obj = 0;
  446. }
  447. #endif /* MAKE_BACK_GRAPH */