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