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)
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
17 * Copyright 2001-2003 Ximian, Inc
18 * Copyright 2003-2010 Novell, Inc.
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 #include "sgen-bridge.h"
49 #include "sgen-hash-table.h"
50 #include "sgen-qsort.h"
51 #include "tabledefs.h"
52 #include "utils/mono-logger-internal.h"
53 #include "utils/mono-time.h"
54 #include "utils/mono-compiler.h"
58 //#define TEST_NEW_XREFS
61 #if !defined(NEW_XREFS) || defined(TEST_NEW_XREFS)
66 #define XREFS new_xrefs
68 #define XREFS old_xrefs
73 int capacity; /* if negative, data points to another DynArray's data */
93 * FIXME: Optimizations:
95 * Don't allocate a scrs array for just one source. Most objects have
96 * just one source, so use the srcs pointer itself.
98 typedef struct _HashEntry {
103 guint32 is_visited : 1;
104 guint32 finishing_time : 31;
105 struct _HashEntry *forwarded_to;
118 } HashEntryWithAccounting;
120 typedef struct _SCC {
123 int num_bridge_entries;
126 * New and old xrefs are typically mutually exclusive. Only when TEST_NEW_XREFS is
127 * enabled we do both, and compare the results. This should only be done for
128 * debugging, obviously.
131 DynIntArray old_xrefs; /* these are incoming, not outgoing */
134 DynIntArray new_xrefs;
138 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);
140 static guint32 current_time;
142 static gboolean bridge_accounting_enabled = FALSE;
144 static SgenBridgeProcessor *bridge_processor;
152 dyn_array_init (DynArray *da)
160 dyn_array_uninit (DynArray *da, int elem_size)
162 if (da->capacity < 0) {
167 if (da->capacity == 0)
170 sgen_free_internal_dynamic (da->data, elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
175 dyn_array_empty (DynArray *da)
177 if (da->capacity < 0)
184 dyn_array_ensure_capacity (DynArray *da, int capacity, int elem_size)
186 int old_capacity = da->capacity;
189 g_assert (capacity > 0);
191 if (capacity <= old_capacity)
194 if (old_capacity <= 0)
196 while (capacity > da->capacity)
199 new_data = sgen_alloc_internal_dynamic (elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
200 memcpy (new_data, da->data, elem_size * da->size);
201 if (old_capacity > 0)
202 sgen_free_internal_dynamic (da->data, elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
207 dyn_array_is_copy (DynArray *da)
209 return da->capacity < 0;
213 dyn_array_ensure_independent (DynArray *da, int elem_size)
215 if (!dyn_array_is_copy (da))
217 dyn_array_ensure_capacity (da, da->size, elem_size);
218 g_assert (da->capacity > 0);
222 dyn_array_add (DynArray *da, int elem_size)
226 dyn_array_ensure_capacity (da, da->size + 1, elem_size);
228 p = da->data + da->size * elem_size;
234 dyn_array_copy (DynArray *dst, DynArray *src, int elem_size)
236 dyn_array_uninit (dst, elem_size);
241 dst->size = src->size;
243 dst->data = src->data;
248 dyn_array_int_init (DynIntArray *da)
250 dyn_array_init (&da->array);
254 dyn_array_int_uninit (DynIntArray *da)
256 dyn_array_uninit (&da->array, sizeof (int));
260 dyn_array_int_size (DynIntArray *da)
262 return da->array.size;
266 dyn_array_int_empty (DynIntArray *da)
268 dyn_array_empty (&da->array);
272 dyn_array_int_add (DynIntArray *da, int x)
274 int *p = dyn_array_add (&da->array, sizeof (int));
279 dyn_array_int_get (DynIntArray *da, int x)
281 return ((int*)da->array.data)[x];
285 dyn_array_int_set (DynIntArray *da, int idx, int val)
287 ((int*)da->array.data)[idx] = val;
291 dyn_array_int_ensure_capacity (DynIntArray *da, int capacity)
293 dyn_array_ensure_capacity (&da->array, capacity, sizeof (int));
297 dyn_array_int_ensure_independent (DynIntArray *da)
299 dyn_array_ensure_independent (&da->array, sizeof (int));
303 dyn_array_int_copy (DynIntArray *dst, DynIntArray *src)
305 dyn_array_copy (&dst->array, &src->array, sizeof (int));
309 dyn_array_int_is_copy (DynIntArray *da)
311 return dyn_array_is_copy (&da->array);
317 dyn_array_ptr_init (DynPtrArray *da)
319 dyn_array_init (&da->array);
323 dyn_array_ptr_uninit (DynPtrArray *da)
325 if (da->array.capacity == 1)
326 dyn_array_ptr_init (da);
328 dyn_array_uninit (&da->array, sizeof (void*));
332 dyn_array_ptr_size (DynPtrArray *da)
334 return da->array.size;
338 dyn_array_ptr_empty (DynPtrArray *da)
340 dyn_array_empty (&da->array);
344 dyn_array_ptr_get (DynPtrArray *da, int x)
346 if (da->array.capacity == 1) {
348 return da->array.data;
350 return ((void**)da->array.data)[x];
354 dyn_array_ptr_add (DynPtrArray *da, void *ptr)
358 if (da->array.capacity == 0) {
359 da->array.capacity = 1;
361 p = (void**)&da->array.data;
362 } else if (da->array.capacity == 1) {
363 void *ptr0 = da->array.data;
365 dyn_array_init (&da->array);
366 p0 = dyn_array_add (&da->array, sizeof (void*));
368 p = dyn_array_add (&da->array, sizeof (void*));
370 p = dyn_array_add (&da->array, sizeof (void*));
375 #define dyn_array_ptr_push dyn_array_ptr_add
378 dyn_array_ptr_pop (DynPtrArray *da)
380 int size = da->array.size;
383 if (da->array.capacity == 1) {
384 p = dyn_array_ptr_get (da, 0);
385 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 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;
432 dyn_array_int_contains (DynIntArray *da, int x)
435 for (i = 0; i < dyn_array_int_size (da); ++i)
436 if (dyn_array_int_get (da, i) == x)
442 enable_accounting (void)
444 SgenHashTable table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntryWithAccounting), mono_aligned_addr_hash, NULL);
445 bridge_accounting_enabled = TRUE;
449 static MonoGCBridgeObjectKind
450 class_kind (MonoClass *class)
452 MonoGCBridgeObjectKind res = bridge_callbacks.bridge_class_kind (class);
454 /* If it's a bridge, nothing we can do about it. */
455 if (res == GC_BRIDGE_TRANSPARENT_BRIDGE_CLASS || res == GC_BRIDGE_OPAQUE_BRIDGE_CLASS)
458 /* Non bridge classes with no pointers will never point to a bridge, so we can savely ignore them. */
459 if (!class->has_references) {
460 SGEN_LOG (6, "class %s is opaque\n", class->name);
461 return GC_BRIDGE_OPAQUE_CLASS;
464 /* Some arrays can be ignored */
465 if (class->rank == 1) {
466 MonoClass *elem_class = class->element_class;
468 /* FIXME the bridge check can be quite expensive, cache it at the class level. */
469 /* An array of a sealed type that is not a bridge will never get to a bridge */
470 if ((elem_class->flags & TYPE_ATTRIBUTE_SEALED) && !elem_class->has_references && !bridge_callbacks.bridge_class_kind (elem_class)) {
471 SGEN_LOG (6, "class %s is opaque\n", class->name);
472 return GC_BRIDGE_OPAQUE_CLASS;
476 return GC_BRIDGE_TRANSPARENT_CLASS;
480 get_hash_entry (MonoObject *obj, gboolean *existing)
482 HashEntry *entry = sgen_hash_table_lookup (&hash_table, obj);
493 memset (&new_entry, 0, sizeof (HashEntry));
495 dyn_array_ptr_init (&new_entry.srcs);
496 new_entry.v.dfs1.finishing_time = 0;
498 sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
500 return sgen_hash_table_lookup (&hash_table, obj);
504 add_source (HashEntry *entry, HashEntry *src)
506 dyn_array_ptr_add (&entry->srcs, src);
517 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
518 int entry_size = dyn_array_ptr_size (&entry->srcs);
519 total_srcs += entry_size;
520 if (entry_size > max_srcs)
521 max_srcs = entry_size;
522 dyn_array_ptr_uninit (&entry->srcs);
523 } SGEN_HASH_TABLE_FOREACH_END;
525 sgen_hash_table_clean (&hash_table);
527 dyn_array_int_uninit (&merge_array);
528 //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
532 register_bridge_object (MonoObject *obj)
534 HashEntry *entry = get_hash_entry (obj, NULL);
535 entry->is_bridge = TRUE;
540 register_finishing_time (HashEntry *entry, guint32 t)
542 g_assert (entry->v.dfs1.finishing_time == 0);
543 /* finishing_time has 31 bits, so it must be within signed int32 range. */
544 g_assert (t > 0 && t <= G_MAXINT32);
545 entry->v.dfs1.finishing_time = t;
548 static int ignored_objects;
551 is_opaque_object (MonoObject *obj)
553 if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT) == SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT) {
554 SGEN_LOG (6, "ignoring %s\n", obj->vtable->klass->name);
562 object_needs_expansion (MonoObject **objp)
564 MonoObject *obj = *objp;
565 MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
568 if (is_opaque_object (fwd))
570 return sgen_hash_table_lookup (&hash_table, fwd) != NULL;
572 if (is_opaque_object (obj))
574 if (!sgen_object_is_live (obj))
576 return sgen_hash_table_lookup (&hash_table, obj) != NULL;
580 follow_forward (HashEntry *entry)
582 while (entry->v.dfs1.forwarded_to) {
583 HashEntry *next = entry->v.dfs1.forwarded_to;
584 if (next->v.dfs1.forwarded_to)
585 entry->v.dfs1.forwarded_to = next->v.dfs1.forwarded_to;
591 static DynPtrArray registered_bridges;
592 static DynPtrArray dfs_stack;
594 static int dfs1_passes, dfs2_passes;
597 * DFS1 maintains a stack, where each two entries are effectively one entry. (FIXME:
598 * Optimize this via pointer tagging.) There are two different types of entries:
600 * entry, src: entry needs to be expanded via scanning, and linked to from src
601 * NULL, entry: entry has already been expanded and needs to be finished
605 #define HANDLE_PTR(ptr,obj) do { \
606 MonoObject *dst = (MonoObject*)*(ptr); \
607 if (dst && object_needs_expansion (&dst)) { \
609 dyn_array_ptr_push (&dfs_stack, obj_entry); \
610 dyn_array_ptr_push (&dfs_stack, follow_forward (get_hash_entry (dst, NULL))); \
615 dfs1 (HashEntry *obj_entry)
618 g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
620 dyn_array_ptr_push (&dfs_stack, NULL);
621 dyn_array_ptr_push (&dfs_stack, obj_entry);
628 obj_entry = dyn_array_ptr_pop (&dfs_stack);
630 /* obj_entry needs to be expanded */
631 src = dyn_array_ptr_pop (&dfs_stack);
633 g_assert (!src->v.dfs1.forwarded_to);
635 obj_entry = follow_forward (obj_entry);
638 g_assert (!obj_entry->v.dfs1.forwarded_to);
639 obj = sgen_hash_table_key_for_value_pointer (obj_entry);
642 if (!obj_entry->v.dfs1.is_visited) {
645 obj_entry->v.dfs1.is_visited = 1;
647 /* push the finishing entry on the stack */
648 dyn_array_ptr_push (&dfs_stack, obj_entry);
649 dyn_array_ptr_push (&dfs_stack, NULL);
651 #include "sgen-scan-object.h"
654 * We can remove non-bridge objects with a single outgoing
655 * link by forwarding links going to it.
657 * This is the first time we've encountered this object, so
658 * no links to it have yet been added. We'll keep it that
659 * way by setting the forward pointer, and instead of
660 * continuing processing this object, we start over with the
661 * object it points to.
663 if (!obj_entry->is_bridge && num_links == 1) {
664 HashEntry *dst_entry = dyn_array_ptr_pop (&dfs_stack);
665 HashEntry *obj_entry_again = dyn_array_ptr_pop (&dfs_stack);
666 g_assert (obj_entry_again == obj_entry);
667 g_assert (!dst_entry->v.dfs1.forwarded_to);
668 if (obj_entry != dst_entry) {
669 obj_entry->v.dfs1.forwarded_to = dst_entry;
670 obj_entry = dst_entry;
677 //g_print ("link %s -> %s\n", sgen_safe_name (src->obj), sgen_safe_name (obj));
678 g_assert (!obj_entry->v.dfs1.forwarded_to);
679 add_source (obj_entry, src);
681 //g_print ("starting with %s\n", sgen_safe_name (obj));
684 /* obj_entry needs to be finished */
686 obj_entry = dyn_array_ptr_pop (&dfs_stack);
688 //g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
689 register_finishing_time (obj_entry, ++current_time);
691 } while (dyn_array_ptr_size (&dfs_stack) > 0);
694 static DynSCCArray sccs;
695 static SCC *current_scc;
698 * At the end of bridge processing we need to end up with an (acyclyc) graph of bridge
699 * object SCCs, where the links between the nodes (each one an SCC) in that graph represent
700 * the presence of a direct or indirect link between those SCCs. An example:
705 * A -> B -> c -> e -> F
707 * A, B, D and F are SCCs that contain bridge objects, c and e don't contain bridge objects.
708 * The graph we need to produce from this is:
715 * Note that we don't need to produce an edge from A to F. It's sufficient that F is
716 * indirectly reachable from A.
718 * The old algorithm would create a set, for each SCC, of bridge SCCs that can reach it,
719 * directly or indirectly, by merging the ones sets for those that reach it directly. The
720 * sets it would build up are these:
729 * The merge operations on these sets turned out to be huge time sinks.
731 * The new algorithm proceeds in two passes: During DFS2, it only builds up the sets of SCCs
732 * that directly point to each SCC:
741 * This is the adjacency list for the SCC graph, in other words. In a separate step
742 * afterwards, it does a depth-first traversal of that graph, for each bridge node, to get
743 * to the final list. It uses a flag to avoid traversing any node twice.
746 scc_add_xref (SCC *src, SCC *dst)
748 g_assert (src != dst);
749 g_assert (src->index != dst->index);
753 * FIXME: Right now we don't even unique the direct ancestors, but just add to the
754 * list. Doing a containment check slows this algorithm down to almost the speed of
755 * the old one. Use the flag instead!
757 dyn_array_int_add (&dst->new_xrefs, src->index);
761 if (dyn_array_int_is_copy (&dst->old_xrefs)) {
763 dyn_array_int_ensure_independent (&dst->old_xrefs);
764 for (i = 0; i < dyn_array_int_size (&dst->old_xrefs); ++i) {
765 int j = dyn_array_int_get (&dst->old_xrefs, i);
766 SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
767 g_assert (!bridge_scc->flag);
768 bridge_scc->flag = TRUE;
772 if (src->num_bridge_entries) {
776 dyn_array_int_add (&dst->old_xrefs, src->index);
777 } else if (dyn_array_int_size (&dst->old_xrefs) == 0) {
778 dyn_array_int_copy (&dst->old_xrefs, &src->old_xrefs);
781 for (i = 0; i < dyn_array_int_size (&src->old_xrefs); ++i) {
782 int j = dyn_array_int_get (&src->old_xrefs, i);
783 SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
784 g_assert (bridge_scc->num_bridge_entries);
785 if (!bridge_scc->flag) {
786 bridge_scc->flag = TRUE;
787 dyn_array_int_add (&dst->old_xrefs, j);
795 scc_add_entry (SCC *scc, HashEntry *entry)
797 g_assert (entry->v.dfs2.scc_index < 0);
798 entry->v.dfs2.scc_index = scc->index;
799 if (entry->is_bridge)
800 ++scc->num_bridge_entries;
804 dfs2 (HashEntry *entry)
808 g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
810 dyn_array_ptr_push (&dfs_stack, entry);
813 entry = dyn_array_ptr_pop (&dfs_stack);
816 if (entry->v.dfs2.scc_index >= 0) {
817 if (entry->v.dfs2.scc_index != current_scc->index)
818 scc_add_xref (dyn_array_scc_get_ptr (&sccs, entry->v.dfs2.scc_index), current_scc);
822 scc_add_entry (current_scc, entry);
824 for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i)
825 dyn_array_ptr_push (&dfs_stack, dyn_array_ptr_get (&entry->srcs, i));
826 } while (dyn_array_ptr_size (&dfs_stack) > 0);
829 /* If xrefs is a copy then we haven't set a single flag. */
830 if (dyn_array_int_is_copy (¤t_scc->old_xrefs))
832 for (i = 0; i < dyn_array_int_size (¤t_scc->old_xrefs); ++i) {
833 int j = dyn_array_int_get (¤t_scc->old_xrefs, i);
834 SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
835 g_assert (bridge_scc->flag);
836 bridge_scc->flag = FALSE;
843 gather_xrefs (SCC *scc)
846 for (i = 0; i < dyn_array_int_size (&scc->new_xrefs); ++i) {
847 int index = dyn_array_int_get (&scc->new_xrefs, i);
848 SCC *src = dyn_array_scc_get_ptr (&sccs, index);
852 if (src->num_bridge_entries)
853 dyn_array_int_add (&merge_array, index);
860 reset_flags (SCC *scc)
863 for (i = 0; i < dyn_array_int_size (&scc->new_xrefs); ++i) {
864 int index = dyn_array_int_get (&scc->new_xrefs, i);
865 SCC *src = dyn_array_scc_get_ptr (&sccs, index);
869 if (!src->num_bridge_entries)
875 static char *dump_prefix = NULL;
880 static int counter = 0;
884 int prefix_len = strlen (dump_prefix);
885 char *filename = alloca(prefix_len + 64);
889 sprintf (filename, "%s.%d.gexf", dump_prefix, counter++);
890 file = fopen (filename, "w");
893 fprintf (stderr, "Warning: Could not open bridge dump file `%s` for writing: %s\n", filename, strerror (errno));
897 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");
899 fprintf (file, "<graph defaultedgetype=\"directed\">\n"
900 "<attributes class=\"node\">\n"
901 "<attribute id=\"0\" title=\"class\" type=\"string\"/>\n"
902 "<attribute id=\"1\" title=\"bridge\" type=\"boolean\"/>\n"
905 fprintf (file, "<nodes>\n");
906 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
907 MonoVTable *vt = (MonoVTable*) SGEN_LOAD_VTABLE (obj);
908 fprintf (file, "<node id=\"%p\"><attvalues><attvalue for=\"0\" value=\"%s.%s\"/><attvalue for=\"1\" value=\"%s\"/></attvalues></node>\n",
909 obj, vt->klass->name_space, vt->klass->name, entry->is_bridge ? "true" : "false");
910 } SGEN_HASH_TABLE_FOREACH_END;
911 fprintf (file, "</nodes>\n");
913 fprintf (file, "<edges>\n");
914 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
916 for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i) {
917 HashEntry *src = dyn_array_ptr_get (&entry->srcs, i);
918 fprintf (file, "<edge id=\"%d\" source=\"%p\" target=\"%p\"/>\n", edge_id++, sgen_hash_table_key_for_value_pointer (src), obj);
920 } SGEN_HASH_TABLE_FOREACH_END;
921 fprintf (file, "</edges>\n");
923 fprintf (file, "</graph></gexf>\n");
929 set_dump_prefix (const char *prefix)
931 dump_prefix = strdup (prefix);
935 compare_hash_entries (const HashEntry *e1, const HashEntry *e2)
937 /* We can cast to signed int here because finishing_time has only 31 bits. */
938 return (gint32)e2->v.dfs1.finishing_time - (gint32)e1->v.dfs1.finishing_time;
941 DEF_QSORT_INLINE(hash_entries, HashEntry*, compare_hash_entries)
943 static unsigned long step_1, step_2, step_3, step_4, step_5, step_6;
944 static int fist_pass_links, second_pass_links, sccs_links;
945 static int max_sccs_links = 0;
948 register_finalized_object (MonoObject *obj)
950 g_assert (sgen_need_bridge_processing ());
951 dyn_array_ptr_push (®istered_bridges, obj);
957 dyn_array_ptr_empty (®istered_bridges);
961 processing_stw_step (void)
967 SGEN_TV_DECLARE (atv);
968 SGEN_TV_DECLARE (btv);
970 if (!dyn_array_ptr_size (®istered_bridges))
973 SGEN_TV_GETTIME (btv);
977 dyn_array_ptr_init (&dfs_stack);
978 dyn_array_int_init (&merge_array);
982 First we insert all bridges into the hash table and then we do dfs1.
984 It must be done in 2 steps since the bridge arrays doesn't come in reverse topological order,
985 which means that we can have entry N pointing to entry N + 1.
987 If we dfs1 entry N before N + 1 is registered we'll not consider N + 1 for this bridge
988 pass and not create the required xref between the two.
990 bridge_count = dyn_array_ptr_size (®istered_bridges);
991 for (i = 0; i < bridge_count ; ++i)
992 register_bridge_object (dyn_array_ptr_get (®istered_bridges, i));
994 for (i = 0; i < bridge_count; ++i)
995 dfs1 (get_hash_entry (dyn_array_ptr_get (®istered_bridges, i), NULL));
997 /* Remove all forwarded objects. */
998 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
999 if (entry->v.dfs1.forwarded_to) {
1000 g_assert (dyn_array_ptr_size (&entry->srcs) == 0);
1001 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
1004 } SGEN_HASH_TABLE_FOREACH_END;
1006 SGEN_TV_GETTIME (atv);
1007 step_2 = SGEN_TV_ELAPSED (btv, atv);
1013 static int num_registered_bridges, hash_table_size;
1016 processing_build_callback_data (int generation)
1019 int num_sccs, num_xrefs;
1020 int max_entries, max_xrefs;
1024 HashEntry **all_entries;
1025 MonoGCBridgeSCC **api_sccs;
1026 MonoGCBridgeXRef *api_xrefs;
1027 SGEN_TV_DECLARE (atv);
1028 SGEN_TV_DECLARE (btv);
1030 g_assert (bridge_processor->num_sccs == 0 && bridge_processor->num_xrefs == 0);
1031 g_assert (!bridge_processor->api_sccs && !bridge_processor->api_xrefs);
1033 if (!dyn_array_ptr_size (®istered_bridges))
1036 g_assert (bridge_processing_in_progress);
1038 SGEN_TV_GETTIME (atv);
1040 /* alloc and fill array of all entries */
1042 all_entries = sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
1045 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
1046 g_assert (entry->v.dfs1.finishing_time > 0);
1047 all_entries [j++] = entry;
1048 fist_pass_links += dyn_array_ptr_size (&entry->srcs);
1049 } SGEN_HASH_TABLE_FOREACH_END;
1050 g_assert (j == hash_table.num_entries);
1051 hash_table_size = hash_table.num_entries;
1053 /* sort array according to decreasing finishing time */
1054 qsort_hash_entries (all_entries, hash_table.num_entries);
1056 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
1057 entry->v.dfs2.scc_index = -1;
1058 } SGEN_HASH_TABLE_FOREACH_END;
1060 SGEN_TV_GETTIME (btv);
1061 step_3 = SGEN_TV_ELAPSED (atv, btv);
1063 /* second DFS pass */
1065 dyn_array_scc_init (&sccs);
1066 for (i = 0; i < hash_table.num_entries; ++i) {
1067 HashEntry *entry = all_entries [i];
1068 if (entry->v.dfs2.scc_index < 0) {
1069 int index = dyn_array_scc_size (&sccs);
1070 current_scc = dyn_array_scc_add (&sccs);
1071 current_scc->index = index;
1072 current_scc->num_bridge_entries = 0;
1074 current_scc->flag = FALSE;
1075 dyn_array_int_init (¤t_scc->new_xrefs);
1078 dyn_array_int_init (¤t_scc->old_xrefs);
1080 current_scc->api_index = -1;
1086 * If a node has only one incoming edge, we just copy the source's
1087 * xrefs array, effectively removing the source from the graph.
1088 * This takes care of long linked lists.
1090 if (!current_scc->num_bridge_entries && dyn_array_int_size (¤t_scc->new_xrefs) == 1) {
1092 j = dyn_array_int_get (¤t_scc->new_xrefs, 0);
1093 src = dyn_array_scc_get_ptr (&sccs, j);
1094 if (src->num_bridge_entries)
1095 dyn_array_int_set (¤t_scc->new_xrefs, 0, j);
1097 dyn_array_int_copy (¤t_scc->new_xrefs, &src->new_xrefs);
1104 #ifdef TEST_NEW_XREFS
1105 for (j = 0; j < dyn_array_scc_size (&sccs); ++j) {
1106 SCC *scc = dyn_array_scc_get_ptr (&sccs, j);
1107 g_assert (!scc->flag);
1111 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1112 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1113 g_assert (scc->index == i);
1114 if (!scc->num_bridge_entries)
1117 dyn_array_int_empty (&merge_array);
1120 dyn_array_int_copy (&scc->new_xrefs, &merge_array);
1121 dyn_array_int_ensure_independent (&scc->new_xrefs);
1123 #ifdef TEST_NEW_XREFS
1124 for (j = 0; j < dyn_array_scc_size (&sccs); ++j) {
1125 SCC *scc = dyn_array_scc_get_ptr (&sccs, j);
1126 g_assert (!scc->flag);
1131 #ifdef TEST_NEW_XREFS
1132 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1133 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1134 g_assert (scc->index == i);
1135 if (!scc->num_bridge_entries)
1138 g_assert (dyn_array_int_size (&scc->new_xrefs) == dyn_array_int_size (&scc->old_xrefs));
1139 for (j = 0; j < dyn_array_int_size (&scc->new_xrefs); ++j)
1140 g_assert (dyn_array_int_contains (&scc->old_xrefs, dyn_array_int_get (&scc->new_xrefs, j)));
1146 * Compute the weight of each object. The weight of an object is its size plus the size of all
1147 * objects it points do. When the an object is pointed by multiple objects we distribute it's weight
1148 * equally among them. This distribution gives a rough estimate of the real impact of making the object
1151 * The reasoning for this model is that complex graphs with single roots will have a bridge with very high
1152 * value in comparison to others.
1154 * The all_entries array has all objects topologically sorted. To correctly propagate the weights it must be
1155 * done in reverse topological order - so we calculate the weight of the pointed-to objects before processing
1156 * pointer-from objects.
1158 * We log those objects in the opposite order for no particular reason. The other constrain is that it should use the same
1159 * direction as the other logging loop that records live/dead information.
1161 if (bridge_accounting_enabled) {
1162 for (i = hash_table.num_entries - 1; i >= 0; --i) {
1164 HashEntryWithAccounting *entry = (HashEntryWithAccounting*)all_entries [i];
1166 entry->weight += (double)sgen_safe_object_get_size (sgen_hash_table_key_for_value_pointer (entry));
1167 w = entry->weight / dyn_array_ptr_size (&entry->entry.srcs);
1168 for (j = 0; j < dyn_array_ptr_size (&entry->entry.srcs); ++j) {
1169 HashEntryWithAccounting *other = (HashEntryWithAccounting *)dyn_array_ptr_get (&entry->entry.srcs, j);
1173 for (i = 0; i < hash_table.num_entries; ++i) {
1174 HashEntryWithAccounting *entry = (HashEntryWithAccounting*)all_entries [i];
1175 if (entry->entry.is_bridge) {
1176 MonoObject *obj = sgen_hash_table_key_for_value_pointer (entry);
1177 MonoClass *klass = ((MonoVTable*)SGEN_LOAD_VTABLE (obj))->klass;
1178 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "OBJECT %s::%s (%p) weight %f", klass->name_space, klass->name, obj, entry->weight);
1183 sccs_size = dyn_array_scc_size (&sccs);
1185 for (i = 0; i < hash_table.num_entries; ++i) {
1186 HashEntry *entry = all_entries [i];
1187 second_pass_links += dyn_array_ptr_size (&entry->srcs);
1190 SGEN_TV_GETTIME (atv);
1191 step_4 = SGEN_TV_ELAPSED (btv, atv);
1193 //g_print ("%d sccs\n", sccs.size);
1195 dyn_array_ptr_uninit (&dfs_stack);
1197 /* init data for callback */
1200 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1201 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1202 g_assert (scc->index == i);
1203 if (scc->num_bridge_entries)
1205 sccs_links += dyn_array_int_size (&scc->XREFS);
1206 max_sccs_links = MAX (max_sccs_links, dyn_array_int_size (&scc->XREFS));
1209 api_sccs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
1212 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1213 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1214 if (!scc->num_bridge_entries)
1217 api_sccs [j] = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
1218 api_sccs [j]->is_alive = FALSE;
1219 api_sccs [j]->num_objs = scc->num_bridge_entries;
1220 scc->num_bridge_entries = 0;
1221 scc->api_index = j++;
1223 num_xrefs += dyn_array_int_size (&scc->XREFS);
1226 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
1227 if (entry->is_bridge) {
1228 SCC *scc = dyn_array_scc_get_ptr (&sccs, entry->v.dfs2.scc_index);
1229 api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = sgen_hash_table_key_for_value_pointer (entry);
1231 } SGEN_HASH_TABLE_FOREACH_END;
1233 api_xrefs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
1235 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1237 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1238 if (!scc->num_bridge_entries)
1240 for (k = 0; k < dyn_array_int_size (&scc->XREFS); ++k) {
1241 SCC *src_scc = dyn_array_scc_get_ptr (&sccs, dyn_array_int_get (&scc->XREFS, k));
1242 if (!src_scc->num_bridge_entries)
1244 api_xrefs [j].src_scc_index = src_scc->api_index;
1245 api_xrefs [j].dst_scc_index = scc->api_index;
1250 SGEN_TV_GETTIME (btv);
1251 step_5 = SGEN_TV_ELAPSED (atv, btv);
1256 max_entries = max_xrefs = 0;
1257 for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
1258 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
1259 if (scc->num_bridge_entries)
1261 if (scc->num_bridge_entries > max_entries)
1262 max_entries = scc->num_bridge_entries;
1263 if (dyn_array_int_size (&scc->XREFS) > max_xrefs)
1264 max_xrefs = dyn_array_int_size (&scc->XREFS);
1266 dyn_array_int_uninit (&scc->new_xrefs);
1269 dyn_array_int_uninit (&scc->old_xrefs);
1273 dyn_array_scc_uninit (&sccs);
1275 sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
1278 /* Empty the registered bridges array */
1279 num_registered_bridges = dyn_array_ptr_size (®istered_bridges);
1280 dyn_array_ptr_empty (®istered_bridges);
1282 SGEN_TV_GETTIME (atv);
1283 step_6 = SGEN_TV_ELAPSED (btv, atv);
1285 //g_print ("%d sccs containing bridges - %d max bridge objects - %d max xrefs\n", j, max_entries, max_xrefs);
1287 bridge_processor->num_sccs = num_sccs;
1288 bridge_processor->api_sccs = api_sccs;
1289 bridge_processor->num_xrefs = num_xrefs;
1290 bridge_processor->api_xrefs = api_xrefs;
1294 processing_after_callback (int generation)
1297 int num_sccs = bridge_processor->num_sccs;
1298 MonoGCBridgeSCC **api_sccs = bridge_processor->api_sccs;
1300 if (bridge_accounting_enabled) {
1301 for (i = 0; i < num_sccs; ++i) {
1302 for (j = 0; j < api_sccs [i]->num_objs; ++j)
1303 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC,
1304 "OBJECT %s (%p) SCC [%d] %s",
1305 sgen_safe_name (api_sccs [i]->objs [j]), api_sccs [i]->objs [j],
1307 api_sccs [i]->is_alive ? "ALIVE" : "DEAD");
1311 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",
1312 num_registered_bridges, hash_table_size, dyn_array_scc_size (&sccs),
1319 fist_pass_links, second_pass_links, sccs_links, max_sccs_links,
1320 dfs1_passes, dfs2_passes, ignored_objects);
1322 step_1 = 0; /* We must cleanup since this value is used as an accumulator. */
1323 fist_pass_links = second_pass_links = sccs_links = max_sccs_links = 0;
1324 dfs1_passes = dfs2_passes = ignored_objects = 0;
1328 describe_pointer (MonoObject *obj)
1333 for (i = 0; i < dyn_array_ptr_size (®istered_bridges); ++i) {
1334 if (obj == dyn_array_ptr_get (®istered_bridges, i)) {
1335 printf ("Pointer is a registered bridge object.\n");
1340 entry = sgen_hash_table_lookup (&hash_table, obj);
1344 printf ("Bridge hash table entry %p:\n", entry);
1345 printf (" is bridge: %d\n", (int)entry->is_bridge);
1346 printf (" is visited: %d\n", (int)entry->v.dfs1.is_visited);
1350 sgen_new_bridge_init (SgenBridgeProcessor *collector)
1352 collector->reset_data = reset_data;
1353 collector->processing_stw_step = processing_stw_step;
1354 collector->processing_build_callback_data = processing_build_callback_data;
1355 collector->processing_after_callback = processing_after_callback;
1356 collector->class_kind = class_kind;
1357 collector->register_finalized_object = register_finalized_object;
1358 collector->describe_pointer = describe_pointer;
1359 collector->enable_accounting = enable_accounting;
1360 collector->set_dump_prefix = set_dump_prefix;
1362 bridge_processor = collector;