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