2 * sgen-bridge.c: Simple generational GC.
4 * Copyright 2011 Novell, Inc (http://www.novell.com)
5 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
6 * Copyright 2001-2003 Ximian, Inc
7 * Copyright 2003-2010 Novell, Inc.
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
19 #include "sgen/sgen-gc.h"
20 #include "sgen-bridge-internals.h"
21 #include "sgen/sgen-hash-table.h"
22 #include "sgen/sgen-qsort.h"
23 #include "sgen/sgen-client.h"
24 #include "tabledefs.h"
25 #include "utils/mono-logger-internals.h"
29 //#define TEST_NEW_XREFS
32 #if !defined(NEW_XREFS) || defined(TEST_NEW_XREFS)
37 #define XREFS new_xrefs
39 #define XREFS old_xrefs
42 #define OPTIMIZATION_COPY
43 #define OPTIMIZATION_FORWARD
44 #define OPTIMIZATION_SINGLETON_DYN_ARRAY
48 int capacity; /* if negative, data points to another DynArray's data */
68 * Bridge data for a single managed object
70 * FIXME: Optimizations:
72 * Don't allocate a srcs array for just one source. Most objects have
73 * just one source, so use the srcs pointer itself.
75 typedef struct _HashEntry {
80 guint32 is_visited : 1;
81 guint32 finishing_time : 31;
82 struct _HashEntry *forwarded_to;
85 // Index in sccs array of SCC this object was folded into
90 // "Source" managed objects pointing at this destination
97 } HashEntryWithAccounting;
99 // The graph of managed objects/HashEntries is reduced to a graph of strongly connected components
100 typedef struct _SCC {
104 // How many bridged objects does this SCC hold references to?
105 int num_bridge_entries;
110 * Index in global sccs array of SCCs holding pointers to this SCC
112 * New and old xrefs are typically mutually exclusive. Only when TEST_NEW_XREFS is
113 * enabled we do both, and compare the results. This should only be done for
114 * debugging, obviously.
117 DynIntArray old_xrefs; /* these are incoming, not outgoing */
120 DynIntArray new_xrefs;
124 // Maps managed objects to corresponding HashEntry stricts
125 static SgenHashTable hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntry), mono_aligned_addr_hash, NULL);
127 static guint32 current_time;
129 static gboolean bridge_accounting_enabled = FALSE;
131 static SgenBridgeProcessor *bridge_processor;
139 dyn_array_init (DynArray *da)
147 dyn_array_uninit (DynArray *da, int elem_size)
149 if (da->capacity < 0) {
154 if (da->capacity == 0)
157 sgen_free_internal_dynamic (da->data, elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
162 dyn_array_empty (DynArray *da)
164 if (da->capacity < 0)
171 dyn_array_ensure_capacity (DynArray *da, int capacity, int elem_size)
173 int old_capacity = da->capacity;
176 g_assert (capacity > 0);
178 if (capacity <= old_capacity)
181 if (old_capacity <= 0)
183 while (capacity > da->capacity)
186 new_data = (char *)sgen_alloc_internal_dynamic (elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
187 memcpy (new_data, da->data, elem_size * da->size);
188 if (old_capacity > 0)
189 sgen_free_internal_dynamic (da->data, elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
194 dyn_array_is_copy (DynArray *da)
196 return da->capacity < 0;
200 dyn_array_ensure_independent (DynArray *da, int elem_size)
202 if (!dyn_array_is_copy (da))
204 dyn_array_ensure_capacity (da, da->size, elem_size);
205 g_assert (da->capacity > 0);
209 dyn_array_add (DynArray *da, int elem_size)
213 dyn_array_ensure_capacity (da, da->size + 1, elem_size);
215 p = da->data + da->size * elem_size;
221 dyn_array_copy (DynArray *dst, DynArray *src, int elem_size)
223 dyn_array_uninit (dst, elem_size);
228 dst->size = src->size;
230 dst->data = src->data;
235 dyn_array_int_init (DynIntArray *da)
237 dyn_array_init (&da->array);
241 dyn_array_int_uninit (DynIntArray *da)
243 dyn_array_uninit (&da->array, sizeof (int));
247 dyn_array_int_size (DynIntArray *da)
249 return da->array.size;
254 dyn_array_int_empty (DynIntArray *da)
256 dyn_array_empty (&da->array);
261 dyn_array_int_add (DynIntArray *da, int x)
263 int *p = (int *)dyn_array_add (&da->array, sizeof (int));
268 dyn_array_int_get (DynIntArray *da, int x)
270 return ((int*)da->array.data)[x];
275 dyn_array_int_set (DynIntArray *da, int idx, int val)
277 ((int*)da->array.data)[idx] = val;
282 dyn_array_int_ensure_independent (DynIntArray *da)
284 dyn_array_ensure_independent (&da->array, sizeof (int));
288 dyn_array_int_copy (DynIntArray *dst, DynIntArray *src)
290 dyn_array_copy (&dst->array, &src->array, sizeof (int));
294 dyn_array_int_is_copy (DynIntArray *da)
296 return dyn_array_is_copy (&da->array);
302 dyn_array_ptr_init (DynPtrArray *da)
304 dyn_array_init (&da->array);
308 dyn_array_ptr_uninit (DynPtrArray *da)
310 #ifdef OPTIMIZATION_SINGLETON_DYN_ARRAY
311 if (da->array.capacity == 1)
312 dyn_array_ptr_init (da);
315 dyn_array_uninit (&da->array, sizeof (void*));
319 dyn_array_ptr_size (DynPtrArray *da)
321 return da->array.size;
325 dyn_array_ptr_empty (DynPtrArray *da)
327 #ifdef OPTIMIZATION_SINGLETON_DYN_ARRAY
328 if (da->array.capacity == 1)
329 dyn_array_ptr_init (da);
332 dyn_array_empty (&da->array);
336 dyn_array_ptr_get (DynPtrArray *da, int x)
338 #ifdef OPTIMIZATION_SINGLETON_DYN_ARRAY
339 if (da->array.capacity == 1) {
341 return da->array.data;
344 return ((void**)da->array.data)[x];
348 dyn_array_ptr_add (DynPtrArray *da, void *ptr)
352 #ifdef OPTIMIZATION_SINGLETON_DYN_ARRAY
353 if (da->array.capacity == 0) {
354 da->array.capacity = 1;
356 p = (void**)&da->array.data;
357 } else if (da->array.capacity == 1) {
358 void *ptr0 = da->array.data;
360 dyn_array_init (&da->array);
361 p0 = (void **)dyn_array_add (&da->array, sizeof (void*));
363 p = (void **)dyn_array_add (&da->array, sizeof (void*));
367 p = (void **)dyn_array_add (&da->array, sizeof (void*));
372 #define dyn_array_ptr_push dyn_array_ptr_add
375 dyn_array_ptr_pop (DynPtrArray *da)
377 int size = da->array.size;
380 #ifdef OPTIMIZATION_SINGLETON_DYN_ARRAY
381 if (da->array.capacity == 1) {
382 p = dyn_array_ptr_get (da, 0);
383 dyn_array_init (&da->array);
387 g_assert (da->array.capacity > 1);
388 dyn_array_ensure_independent (&da->array, sizeof (void*));
389 p = dyn_array_ptr_get (da, size - 1);
398 dyn_array_scc_init (DynSCCArray *da)
400 dyn_array_init (&da->array);
404 dyn_array_scc_uninit (DynSCCArray *da)
406 dyn_array_uninit (&da->array, sizeof (SCC));
410 dyn_array_scc_size (DynSCCArray *da)
412 return da->array.size;
416 dyn_array_scc_add (DynSCCArray *da)
418 return (SCC *)dyn_array_add (&da->array, sizeof (SCC));
422 dyn_array_scc_get_ptr (DynSCCArray *da, int x)
424 return &((SCC*)da->array.data)[x];
429 static DynIntArray merge_array;
433 dyn_array_int_contains (DynIntArray *da, int x)
436 for (i = 0; i < dyn_array_int_size (da); ++i)
437 if (dyn_array_int_get (da, i) == x)
444 enable_accounting (void)
446 SgenHashTable table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntryWithAccounting), mono_aligned_addr_hash, NULL);
447 bridge_accounting_enabled = TRUE;
451 static MonoGCBridgeObjectKind
452 class_kind (MonoClass *klass)
454 MonoGCBridgeObjectKind res = bridge_callbacks.bridge_class_kind (klass);
456 /* If it's a bridge, nothing we can do about it. */
457 if (res == GC_BRIDGE_TRANSPARENT_BRIDGE_CLASS || res == GC_BRIDGE_OPAQUE_BRIDGE_CLASS)
460 /* Non bridge classes with no pointers will never point to a bridge, so we can savely ignore them. */
461 if (!klass->has_references) {
462 SGEN_LOG (6, "class %s is opaque\n", klass->name);
463 return GC_BRIDGE_OPAQUE_CLASS;
466 /* Some arrays can be ignored */
467 if (klass->rank == 1) {
468 MonoClass *elem_class = klass->element_class;
470 /* FIXME the bridge check can be quite expensive, cache it at the class level. */
471 /* An array of a sealed type that is not a bridge will never get to a bridge */
472 if ((elem_class->flags & TYPE_ATTRIBUTE_SEALED) && !elem_class->has_references && !bridge_callbacks.bridge_class_kind (elem_class)) {
473 SGEN_LOG (6, "class %s is opaque\n", klass->name);
474 return GC_BRIDGE_OPAQUE_CLASS;
478 return GC_BRIDGE_TRANSPARENT_CLASS;
482 get_hash_entry (MonoObject *obj, gboolean *existing)
484 HashEntry *entry = (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
495 memset (&new_entry, 0, sizeof (HashEntry));
497 dyn_array_ptr_init (&new_entry.srcs);
498 new_entry.v.dfs1.finishing_time = 0;
500 sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
502 return (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
506 add_source (HashEntry *entry, HashEntry *src)
508 dyn_array_ptr_add (&entry->srcs, src);
514 MonoObject *obj G_GNUC_UNUSED;
519 SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
520 int entry_size = dyn_array_ptr_size (&entry->srcs);
521 total_srcs += entry_size;
522 if (entry_size > max_srcs)
523 max_srcs = entry_size;
524 dyn_array_ptr_uninit (&entry->srcs);
525 } SGEN_HASH_TABLE_FOREACH_END;
527 sgen_hash_table_clean (&hash_table);
529 dyn_array_int_uninit (&merge_array);
530 //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
534 register_bridge_object (MonoObject *obj)
536 HashEntry *entry = get_hash_entry (obj, NULL);
537 entry->is_bridge = TRUE;
542 register_finishing_time (HashEntry *entry, guint32 t)
544 g_assert (entry->v.dfs1.finishing_time == 0);
545 /* finishing_time has 31 bits, so it must be within signed int32 range. */
546 g_assert (t > 0 && t <= G_MAXINT32);
547 entry->v.dfs1.finishing_time = t;
550 static int ignored_objects;
553 is_opaque_object (MonoObject *obj)
555 if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT) == SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT) {
556 SGEN_LOG (6, "ignoring %s\n", obj->vtable->klass->name);
564 object_needs_expansion (MonoObject **objp)
566 MonoObject *obj = *objp;
567 MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
570 if (is_opaque_object (fwd))
572 return sgen_hash_table_lookup (&hash_table, fwd) != NULL;
574 if (is_opaque_object (obj))
576 if (!sgen_object_is_live (obj))
578 return sgen_hash_table_lookup (&hash_table, obj) != NULL;
582 follow_forward (HashEntry *entry)
584 #ifdef OPTIMIZATION_FORWARD
585 while (entry->v.dfs1.forwarded_to) {
586 HashEntry *next = entry->v.dfs1.forwarded_to;
587 if (next->v.dfs1.forwarded_to)
588 entry->v.dfs1.forwarded_to = next->v.dfs1.forwarded_to;
592 g_assert (!entry->v.dfs1.forwarded_to);
597 static DynPtrArray registered_bridges;
598 static DynPtrArray dfs_stack;
600 static int dfs1_passes, dfs2_passes;
603 * DFS1 maintains a stack, where each two entries are effectively one entry. (FIXME:
604 * Optimize this via pointer tagging.) There are two different types of entries:
606 * entry, src: entry needs to be expanded via scanning, and linked to from src
607 * NULL, entry: entry has already been expanded and needs to be finished
611 #define HANDLE_PTR(ptr,obj) do { \
612 GCObject *dst = (GCObject*)*(ptr); \
613 if (dst && object_needs_expansion (&dst)) { \
615 dyn_array_ptr_push (&dfs_stack, obj_entry); \
616 dyn_array_ptr_push (&dfs_stack, follow_forward (get_hash_entry (dst, NULL))); \
621 dfs1 (HashEntry *obj_entry)
624 g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
626 dyn_array_ptr_push (&dfs_stack, NULL);
627 dyn_array_ptr_push (&dfs_stack, obj_entry);
634 obj_entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
636 /* obj_entry needs to be expanded */
637 src = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
640 g_assert (!src->v.dfs1.forwarded_to);
642 obj_entry = follow_forward (obj_entry);
645 g_assert (!obj_entry->v.dfs1.forwarded_to);
646 obj = sgen_hash_table_key_for_value_pointer (obj_entry);
649 if (!obj_entry->v.dfs1.is_visited) {
651 mword desc = sgen_obj_get_descriptor_safe (obj);
653 obj_entry->v.dfs1.is_visited = 1;
655 /* push the finishing entry on the stack */
656 dyn_array_ptr_push (&dfs_stack, obj_entry);
657 dyn_array_ptr_push (&dfs_stack, NULL);
659 #include "sgen/sgen-scan-object.h"
662 * We can remove non-bridge objects with a single outgoing
663 * link by forwarding links going to it.
665 * This is the first time we've encountered this object, so
666 * no links to it have yet been added. We'll keep it that
667 * way by setting the forward pointer, and instead of
668 * continuing processing this object, we start over with the
669 * object it points to.
671 #ifdef OPTIMIZATION_FORWARD
672 if (!obj_entry->is_bridge && num_links == 1) {
673 HashEntry *dst_entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
674 HashEntry *obj_entry_again = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
675 g_assert (obj_entry_again == obj_entry);
676 g_assert (!dst_entry->v.dfs1.forwarded_to);
677 if (obj_entry != dst_entry) {
678 obj_entry->v.dfs1.forwarded_to = dst_entry;
679 obj_entry = dst_entry;
687 //g_print ("link %s -> %s\n", sgen_safe_name (src->obj), sgen_safe_name (obj));
688 g_assert (!obj_entry->v.dfs1.forwarded_to);
689 add_source (obj_entry, src);
691 //g_print ("starting with %s\n", sgen_safe_name (obj));
694 /* obj_entry needs to be finished */
696 obj_entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
698 //g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
699 register_finishing_time (obj_entry, ++current_time);
701 } while (dyn_array_ptr_size (&dfs_stack) > 0);
704 static DynSCCArray sccs;
705 static SCC *current_scc;
708 * At the end of bridge processing we need to end up with an (acyclyc) graph of bridge
709 * object SCCs, where the links between the nodes (each one an SCC) in that graph represent
710 * the presence of a direct or indirect link between those SCCs. An example:
715 * A -> B -> c -> e -> F
717 * A, B, D and F are SCCs that contain bridge objects, c and e don't contain bridge objects.
718 * The graph we need to produce from this is:
725 * Note that we don't need to produce an edge from A to F. It's sufficient that F is
726 * indirectly reachable from A.
728 * The old algorithm would create a set, for each SCC, of bridge SCCs that can reach it,
729 * directly or indirectly, by merging the ones sets for those that reach it directly. The
730 * sets it would build up are these:
739 * The merge operations on these sets turned out to be huge time sinks.
741 * The new algorithm proceeds in two passes: During DFS2, it only builds up the sets of SCCs
742 * that directly point to each SCC:
751 * This is the adjacency list for the SCC graph, in other words. In a separate step
752 * afterwards, it does a depth-first traversal of that graph, for each bridge node, to get
753 * to the final list. It uses a flag to avoid traversing any node twice.
756 scc_add_xref (SCC *src, SCC *dst)
758 g_assert (src != dst);
759 g_assert (src->index != dst->index);
763 * FIXME: Right now we don't even unique the direct ancestors, but just add to the
764 * list. Doing a containment check slows this algorithm down to almost the speed of
765 * the old one. Use the flag instead!
767 dyn_array_int_add (&dst->new_xrefs, src->index);
771 if (dyn_array_int_is_copy (&dst->old_xrefs)) {
773 dyn_array_int_ensure_independent (&dst->old_xrefs);
774 for (i = 0; i < dyn_array_int_size (&dst->old_xrefs); ++i) {
775 int j = dyn_array_int_get (&dst->old_xrefs, i);
776 SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
777 g_assert (!bridge_scc->flag);
778 bridge_scc->flag = TRUE;
782 if (src->num_bridge_entries) {
786 dyn_array_int_add (&dst->old_xrefs, src->index);
787 #ifdef OPTIMIZATION_COPY
788 } else if (dyn_array_int_size (&dst->old_xrefs) == 0) {
789 dyn_array_int_copy (&dst->old_xrefs, &src->old_xrefs);
793 for (i = 0; i < dyn_array_int_size (&src->old_xrefs); ++i) {
794 int j = dyn_array_int_get (&src->old_xrefs, i);
795 SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
796 g_assert (bridge_scc->num_bridge_entries);
797 if (!bridge_scc->flag) {
798 bridge_scc->flag = TRUE;
799 dyn_array_int_add (&dst->old_xrefs, j);
807 scc_add_entry (SCC *scc, HashEntry *entry)
809 g_assert (entry->v.dfs2.scc_index < 0);
810 entry->v.dfs2.scc_index = scc->index;
811 if (entry->is_bridge)
812 ++scc->num_bridge_entries;
816 dfs2 (HashEntry *entry)
820 g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
822 dyn_array_ptr_push (&dfs_stack, entry);
825 entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
828 if (entry->v.dfs2.scc_index >= 0) {
829 if (entry->v.dfs2.scc_index != current_scc->index)
830 scc_add_xref (dyn_array_scc_get_ptr (&sccs, entry->v.dfs2.scc_index), current_scc);
834 scc_add_entry (current_scc, entry);
836 for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i)
837 dyn_array_ptr_push (&dfs_stack, dyn_array_ptr_get (&entry->srcs, i));
838 } while (dyn_array_ptr_size (&dfs_stack) > 0);
841 /* If xrefs is a copy then we haven't set a single flag. */
842 if (dyn_array_int_is_copy (¤t_scc->old_xrefs))
844 for (i = 0; i < dyn_array_int_size (¤t_scc->old_xrefs); ++i) {
845 int j = dyn_array_int_get (¤t_scc->old_xrefs, i);
846 SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
847 g_assert (bridge_scc->flag);
848 bridge_scc->flag = FALSE;
855 gather_xrefs (SCC *scc)
858 for (i = 0; i < dyn_array_int_size (&scc->new_xrefs); ++i) {
859 int index = dyn_array_int_get (&scc->new_xrefs, i);
860 SCC *src = dyn_array_scc_get_ptr (&sccs, index);
864 if (src->num_bridge_entries)
865 dyn_array_int_add (&merge_array, index);
872 reset_flags (SCC *scc)
875 for (i = 0; i < dyn_array_int_size (&scc->new_xrefs); ++i) {
876 int index = dyn_array_int_get (&scc->new_xrefs, i);
877 SCC *src = dyn_array_scc_get_ptr (&sccs, index);
881 if (!src->num_bridge_entries)
887 static char *dump_prefix = NULL;
892 static int counter = 0;
896 size_t prefix_len = strlen (dump_prefix);
897 char *filename = (char *)alloca (prefix_len + 64);
901 sprintf (filename, "%s.%d.gexf", dump_prefix, counter++);
902 file = fopen (filename, "w");
905 fprintf (stderr, "Warning: Could not open bridge dump file `%s` for writing: %s\n", filename, strerror (errno));
909 fprintf (file, "<gexf xmlns=\"http://www.gexf.net/1.2draft\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd\" version=\"1.2\">\n");
911 fprintf (file, "<graph defaultedgetype=\"directed\">\n"
912 "<attributes class=\"node\">\n"
913 "<attribute id=\"0\" title=\"class\" type=\"string\"/>\n"
914 "<attribute id=\"1\" title=\"bridge\" type=\"boolean\"/>\n"
917 fprintf (file, "<nodes>\n");
918 SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
919 MonoVTable *vt = SGEN_LOAD_VTABLE (obj);
920 fprintf (file, "<node id=\"%p\"><attvalues><attvalue for=\"0\" value=\"%s.%s\"/><attvalue for=\"1\" value=\"%s\"/></attvalues></node>\n",
921 obj, vt->klass->name_space, vt->klass->name, entry->is_bridge ? "true" : "false");
922 } SGEN_HASH_TABLE_FOREACH_END;
923 fprintf (file, "</nodes>\n");
925 fprintf (file, "<edges>\n");
926 SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
928 for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i) {
929 HashEntry *src = (HashEntry *)dyn_array_ptr_get (&entry->srcs, i);
930 fprintf (file, "<edge id=\"%d\" source=\"%p\" target=\"%p\"/>\n", edge_id++, sgen_hash_table_key_for_value_pointer (src), obj);
932 } SGEN_HASH_TABLE_FOREACH_END;
933 fprintf (file, "</edges>\n");
935 fprintf (file, "</graph></gexf>\n");
941 set_dump_prefix (const char *prefix)
943 dump_prefix = strdup (prefix);
947 compare_hash_entries (const HashEntry *e1, const HashEntry *e2)
949 /* We can cast to signed int here because finishing_time has only 31 bits. */
950 return (gint32)e2->v.dfs1.finishing_time - (gint32)e1->v.dfs1.finishing_time;
953 DEF_QSORT_INLINE(hash_entries, HashEntry*, compare_hash_entries)
955 static gint64 step_1, step_2, step_3, step_4, step_5, step_6;
956 static int fist_pass_links, second_pass_links, sccs_links;
957 static int max_sccs_links = 0;
960 register_finalized_object (GCObject *obj)
962 g_assert (sgen_need_bridge_processing ());
963 dyn_array_ptr_push (®istered_bridges, obj);
969 dyn_array_ptr_empty (®istered_bridges);
973 processing_stw_step (void)
977 MonoObject *obj G_GNUC_UNUSED;
979 SGEN_TV_DECLARE (atv);
980 SGEN_TV_DECLARE (btv);
982 if (!dyn_array_ptr_size (®istered_bridges))
985 SGEN_TV_GETTIME (btv);
989 dyn_array_ptr_init (&dfs_stack);
990 dyn_array_int_init (&merge_array);
994 First we insert all bridges into the hash table and then we do dfs1.
996 It must be done in 2 steps since the bridge arrays doesn't come in reverse topological order,
997 which means that we can have entry N pointing to entry N + 1.
999 If we dfs1 entry N before N + 1 is registered we'll not consider N + 1 for this bridge
1000 pass and not create the required xref between the two.
1002 bridge_count = dyn_array_ptr_size (®istered_bridges);
1003 for (i = 0; i < bridge_count ; ++i)
1004 register_bridge_object ((MonoObject *)dyn_array_ptr_get (®istered_bridges, i));
1006 for (i = 0; i < bridge_count; ++i)
1007 dfs1 (get_hash_entry ((MonoObject *)dyn_array_ptr_get (®istered_bridges, i), NULL));
1009 /* Remove all forwarded objects. */
1010 SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
1011 if (entry->v.dfs1.forwarded_to) {
1012 g_assert (dyn_array_ptr_size (&entry->srcs) == 0);
1013 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
1016 } SGEN_HASH_TABLE_FOREACH_END;
1018 SGEN_TV_GETTIME (atv);
1019 step_2 = SGEN_TV_ELAPSED (btv, atv);
1025 static int num_registered_bridges, hash_table_size;
1028 processing_build_callback_data (int generation)
1031 int num_sccs, num_xrefs;
1032 int max_entries, max_xrefs;
1033 MonoObject *obj G_GNUC_UNUSED;
1035 HashEntry **all_entries;
1036 MonoGCBridgeSCC **api_sccs;
1037 MonoGCBridgeXRef *api_xrefs;
1038 SGEN_TV_DECLARE (atv);
1039 SGEN_TV_DECLARE (btv);
1041 g_assert (bridge_processor->num_sccs == 0 && bridge_processor->num_xrefs == 0);
1042 g_assert (!bridge_processor->api_sccs && !bridge_processor->api_xrefs);
1044 if (!dyn_array_ptr_size (®istered_bridges))
1047 g_assert (bridge_processing_in_progress);
1049 SGEN_TV_GETTIME (atv);
1051 /* alloc and fill array of all entries */
1053 all_entries = (HashEntry **)sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
1056 SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
1057 g_assert (entry->v.dfs1.finishing_time > 0);
1058 all_entries [j++] = entry;
1059 fist_pass_links += dyn_array_ptr_size (&entry->srcs);
1060 } SGEN_HASH_TABLE_FOREACH_END;
1061 g_assert (j == hash_table.num_entries);
1062 hash_table_size = hash_table.num_entries;
1064 /* sort array according to decreasing finishing time */
1065 qsort_hash_entries (all_entries, hash_table.num_entries);
1067 SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
1068 entry->v.dfs2.scc_index = -1;
1069 } SGEN_HASH_TABLE_FOREACH_END;
1071 SGEN_TV_GETTIME (btv);
1072 step_3 = SGEN_TV_ELAPSED (atv, btv);
1074 /* second DFS pass */
1076 dyn_array_scc_init (&sccs);
1077 for (i = 0; i < hash_table.num_entries; ++i) {
1078 HashEntry *entry = all_entries [i];
1079 if (entry->v.dfs2.scc_index < 0) {
1080 int index = dyn_array_scc_size (&sccs);
1081 current_scc = dyn_array_scc_add (&sccs);
1082 current_scc->index = index;
1083 current_scc->num_bridge_entries = 0;
1085 current_scc->flag = FALSE;
1086 dyn_array_int_init (¤t_scc->new_xrefs);
1089 dyn_array_int_init (¤t_scc->old_xrefs);
1091 current_scc->api_index = -1;
1097 * If a node has only one incoming edge, we just copy the source's
1098 * xrefs array, effectively removing the source from the graph.
1099 * This takes care of long linked lists.
1101 if (!current_scc->num_bridge_entries && dyn_array_int_size (¤t_scc->new_xrefs) == 1) {
1103 j = dyn_array_int_get (¤t_scc->new_xrefs, 0);
1104 src = dyn_array_scc_get_ptr (&sccs, j);
1105 if (src->num_bridge_entries)
1106 dyn_array_int_set (¤t_scc->new_xrefs, 0, j);
1108 dyn_array_int_copy (¤t_scc->new_xrefs, &src->new_xrefs);
1115 #ifdef TEST_NEW_XREFS
1116 for (j = 0; j < dyn_array_scc_size (&sccs); ++j) {
1117 SCC *scc = dyn_array_scc_get_ptr (&sccs, j);
1118 g_assert (!scc->flag);
1122 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1123 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1124 g_assert (scc->index == i);
1125 if (!scc->num_bridge_entries)
1128 dyn_array_int_empty (&merge_array);
1131 dyn_array_int_copy (&scc->new_xrefs, &merge_array);
1132 dyn_array_int_ensure_independent (&scc->new_xrefs);
1134 #ifdef TEST_NEW_XREFS
1135 for (j = 0; j < dyn_array_scc_size (&sccs); ++j) {
1136 SCC *scc = dyn_array_scc_get_ptr (&sccs, j);
1137 g_assert (!scc->flag);
1142 #ifdef TEST_NEW_XREFS
1143 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1144 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1145 g_assert (scc->index == i);
1146 if (!scc->num_bridge_entries)
1149 g_assert (dyn_array_int_size (&scc->new_xrefs) == dyn_array_int_size (&scc->old_xrefs));
1150 for (j = 0; j < dyn_array_int_size (&scc->new_xrefs); ++j)
1151 g_assert (dyn_array_int_contains (&scc->old_xrefs, dyn_array_int_get (&scc->new_xrefs, j)));
1157 * Compute the weight of each object. The weight of an object is its size plus the size of all
1158 * objects it points do. When the an object is pointed by multiple objects we distribute it's weight
1159 * equally among them. This distribution gives a rough estimate of the real impact of making the object
1162 * The reasoning for this model is that complex graphs with single roots will have a bridge with very high
1163 * value in comparison to others.
1165 * The all_entries array has all objects topologically sorted. To correctly propagate the weights it must be
1166 * done in reverse topological order - so we calculate the weight of the pointed-to objects before processing
1167 * pointer-from objects.
1169 * We log those objects in the opposite order for no particular reason. The other constrain is that it should use the same
1170 * direction as the other logging loop that records live/dead information.
1172 if (bridge_accounting_enabled) {
1173 for (i = hash_table.num_entries - 1; i >= 0; --i) {
1175 HashEntryWithAccounting *entry = (HashEntryWithAccounting*)all_entries [i];
1177 entry->weight += (double)sgen_safe_object_get_size (sgen_hash_table_key_for_value_pointer (entry));
1178 w = entry->weight / dyn_array_ptr_size (&entry->entry.srcs);
1179 for (j = 0; j < dyn_array_ptr_size (&entry->entry.srcs); ++j) {
1180 HashEntryWithAccounting *other = (HashEntryWithAccounting *)dyn_array_ptr_get (&entry->entry.srcs, j);
1184 for (i = 0; i < hash_table.num_entries; ++i) {
1185 HashEntryWithAccounting *entry = (HashEntryWithAccounting*)all_entries [i];
1186 if (entry->entry.is_bridge) {
1187 MonoObject *obj = sgen_hash_table_key_for_value_pointer (entry);
1188 MonoClass *klass = SGEN_LOAD_VTABLE (obj)->klass;
1189 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "OBJECT %s::%s (%p) weight %f", klass->name_space, klass->name, obj, entry->weight);
1194 for (i = 0; i < hash_table.num_entries; ++i) {
1195 HashEntry *entry = all_entries [i];
1196 second_pass_links += dyn_array_ptr_size (&entry->srcs);
1199 SGEN_TV_GETTIME (atv);
1200 step_4 = SGEN_TV_ELAPSED (btv, atv);
1202 //g_print ("%d sccs\n", sccs.size);
1204 dyn_array_ptr_uninit (&dfs_stack);
1206 /* init data for callback */
1209 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1210 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1211 g_assert (scc->index == i);
1212 if (scc->num_bridge_entries)
1214 sccs_links += dyn_array_int_size (&scc->XREFS);
1215 max_sccs_links = MAX (max_sccs_links, dyn_array_int_size (&scc->XREFS));
1218 api_sccs = (MonoGCBridgeSCC **)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
1221 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1222 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1223 if (!scc->num_bridge_entries)
1226 api_sccs [j] = (MonoGCBridgeSCC *)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
1227 api_sccs [j]->is_alive = FALSE;
1228 api_sccs [j]->num_objs = scc->num_bridge_entries;
1229 scc->num_bridge_entries = 0;
1230 scc->api_index = j++;
1232 num_xrefs += dyn_array_int_size (&scc->XREFS);
1235 SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
1236 if (entry->is_bridge) {
1237 SCC *scc = dyn_array_scc_get_ptr (&sccs, entry->v.dfs2.scc_index);
1238 api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = sgen_hash_table_key_for_value_pointer (entry);
1240 } SGEN_HASH_TABLE_FOREACH_END;
1242 api_xrefs = (MonoGCBridgeXRef *)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
1244 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1246 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1247 if (!scc->num_bridge_entries)
1249 for (k = 0; k < dyn_array_int_size (&scc->XREFS); ++k) {
1250 SCC *src_scc = dyn_array_scc_get_ptr (&sccs, dyn_array_int_get (&scc->XREFS, k));
1251 if (!src_scc->num_bridge_entries)
1253 api_xrefs [j].src_scc_index = src_scc->api_index;
1254 api_xrefs [j].dst_scc_index = scc->api_index;
1259 SGEN_TV_GETTIME (btv);
1260 step_5 = SGEN_TV_ELAPSED (atv, btv);
1265 max_entries = max_xrefs = 0;
1266 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1267 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1268 if (scc->num_bridge_entries)
1270 if (scc->num_bridge_entries > max_entries)
1271 max_entries = scc->num_bridge_entries;
1272 if (dyn_array_int_size (&scc->XREFS) > max_xrefs)
1273 max_xrefs = dyn_array_int_size (&scc->XREFS);
1275 dyn_array_int_uninit (&scc->new_xrefs);
1278 dyn_array_int_uninit (&scc->old_xrefs);
1282 dyn_array_scc_uninit (&sccs);
1284 sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
1287 /* Empty the registered bridges array */
1288 num_registered_bridges = dyn_array_ptr_size (®istered_bridges);
1289 dyn_array_ptr_empty (®istered_bridges);
1291 SGEN_TV_GETTIME (atv);
1292 step_6 = SGEN_TV_ELAPSED (btv, atv);
1294 //g_print ("%d sccs containing bridges - %d max bridge objects - %d max xrefs\n", j, max_entries, max_xrefs);
1296 bridge_processor->num_sccs = num_sccs;
1297 bridge_processor->api_sccs = api_sccs;
1298 bridge_processor->num_xrefs = num_xrefs;
1299 bridge_processor->api_xrefs = api_xrefs;
1303 processing_after_callback (int generation)
1306 int num_sccs = bridge_processor->num_sccs;
1307 MonoGCBridgeSCC **api_sccs = bridge_processor->api_sccs;
1309 if (bridge_accounting_enabled) {
1310 for (i = 0; i < num_sccs; ++i) {
1311 for (j = 0; j < api_sccs [i]->num_objs; ++j) {
1312 GCVTable vtable = SGEN_LOAD_VTABLE (api_sccs [i]->objs [j]);
1313 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC,
1314 "OBJECT %s (%p) SCC [%d] %s",
1315 sgen_client_vtable_get_namespace (vtable), sgen_client_vtable_get_name (vtable), api_sccs [i]->objs [j],
1317 api_sccs [i]->is_alive ? "ALIVE" : "DEAD");
1322 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_NEW_BRIDGE num-objects %d num_hash_entries %d sccs size %d init %.2fms df1 %.2fms sort %.2fms dfs2 %.2fms setup-cb %.2fms free-data %.2fms links %d/%d/%d/%d dfs passes %d/%d ignored %d",
1323 num_registered_bridges, hash_table_size, dyn_array_scc_size (&sccs),
1330 fist_pass_links, second_pass_links, sccs_links, max_sccs_links,
1331 dfs1_passes, dfs2_passes, ignored_objects);
1333 step_1 = 0; /* We must cleanup since this value is used as an accumulator. */
1334 fist_pass_links = second_pass_links = sccs_links = max_sccs_links = 0;
1335 dfs1_passes = dfs2_passes = ignored_objects = 0;
1339 describe_pointer (GCObject *obj)
1344 for (i = 0; i < dyn_array_ptr_size (®istered_bridges); ++i) {
1345 if (obj == dyn_array_ptr_get (®istered_bridges, i)) {
1346 printf ("Pointer is a registered bridge object.\n");
1351 entry = (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
1355 printf ("Bridge hash table entry %p:\n", entry);
1356 printf (" is bridge: %d\n", (int)entry->is_bridge);
1357 printf (" is visited: %d\n", (int)entry->v.dfs1.is_visited);
1361 sgen_new_bridge_init (SgenBridgeProcessor *collector)
1363 collector->reset_data = reset_data;
1364 collector->processing_stw_step = processing_stw_step;
1365 collector->processing_build_callback_data = processing_build_callback_data;
1366 collector->processing_after_callback = processing_after_callback;
1367 collector->class_kind = class_kind;
1368 collector->register_finalized_object = register_finalized_object;
1369 collector->describe_pointer = describe_pointer;
1370 collector->enable_accounting = enable_accounting;
1371 collector->set_dump_prefix = set_dump_prefix;
1373 bridge_processor = collector;