Merge pull request #637 from LogosBible/enetdown
[mono.git] / mono / metadata / sgen-bridge.c
index 1088e70da8b2e94cf405747a06562f828bbee895..40448b28696b9b1f117cae7a8ae86aa38491c07a 100644 (file)
@@ -45,6 +45,7 @@
 
 #include "sgen-gc.h"
 #include "sgen-bridge.h"
+#include "sgen-hash-table.h"
 #include "utils/mono-logger-internal.h"
 #include "utils/mono-time.h"
 
@@ -59,6 +60,8 @@ typedef struct {
 #define DYN_ARRAY_REF(da,i)    ((void*)((da)->data + (i) * (da)->elem_size))
 #define DYN_ARRAY_PTR_REF(da,i)        (((void**)(da)->data) [(i)])
 #define DYN_ARRAY_INT_REF(da,i)        (((int*)(da)->data) [(i)])
+#define DYN_ARRAY_PTR_STATIC_INITIALIZER { 0, sizeof (void*), 0, NULL }
+#define DYN_ARRAY_INT_STATIC_INITIALIZER { 0, sizeof (int), 0, NULL }
 
 static void
 dyn_array_init (DynArray *da, int elem_size)
@@ -87,7 +90,7 @@ dyn_array_uninit (DynArray *da)
        if (da->capacity <= 0)
                return;
 
-       mono_sgen_free_internal_dynamic (da->data, da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
+       sgen_free_internal_dynamic (da->data, da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
        da->data = NULL;
 }
 
@@ -105,9 +108,9 @@ dyn_array_ensure_capacity (DynArray *da, int capacity)
        while (capacity > da->capacity)
                da->capacity *= 2;
 
-       new_data = mono_sgen_alloc_internal_dynamic (da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
+       new_data = sgen_alloc_internal_dynamic (da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
        memcpy (new_data, da->data, da->elem_size * da->size);
-       mono_sgen_free_internal_dynamic (da->data, da->elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
+       sgen_free_internal_dynamic (da->data, da->elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
        da->data = new_data;
 }
 
@@ -171,16 +174,6 @@ dyn_array_int_contains (DynArray *da, int x)
        return FALSE;
 }
 
-static void G_GNUC_UNUSED
-dyn_array_append (DynArray *dst, DynArray *src)
-{
-       g_assert (dst->elem_size == src->elem_size);
-
-       dyn_array_ensure_capacity (dst, dst->size + src->size);
-       memcpy (DYN_ARRAY_REF (dst, dst->size), DYN_ARRAY_REF (src, 0), src->size * src->elem_size);
-       dst->size += src->size;
-}
-
 static DynArray merge_array;
 
 static void
@@ -278,38 +271,68 @@ typedef struct _SCC {
        DynArray xrefs;         /* these are incoming, not outgoing */
 } SCC;
 
-static SgenHashTable hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_DATA, INTERNAL_MEM_BRIDGE_DATA, sizeof (HashEntry), mono_aligned_addr_hash, NULL);
+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);
 
 static MonoGCBridgeCallbacks bridge_callbacks;
 
 static int current_time;
 
+gboolean bridge_processing_in_progress = FALSE;
+
+void
+mono_gc_wait_for_bridge_processing (void)
+{
+       if (!bridge_processing_in_progress)
+               return;
+
+       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_BRIDGE waiting for bridge processing to finish");
+
+       sgen_gc_lock ();
+       sgen_gc_unlock ();
+}
+
 void
 mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks)
 {
+       if (callbacks->bridge_version != SGEN_BRIDGE_VERSION)
+               g_error ("Invalid bridge callback version. Expected %d but got %d\n", SGEN_BRIDGE_VERSION, callbacks->bridge_version);
+
        bridge_callbacks = *callbacks;
 }
 
 gboolean
-mono_sgen_is_bridge_object (MonoObject *obj)
+sgen_is_bridge_object (MonoObject *obj)
 {
+       if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OBJECT) != SGEN_GC_BIT_BRIDGE_OBJECT)
+               return FALSE;
        return bridge_callbacks.is_bridge_object (obj);
 }
 
 gboolean
-mono_sgen_need_bridge_processing (void)
+sgen_is_bridge_class (MonoClass *class)
+{
+       return bridge_callbacks.is_bridge_class (class);
+}
+
+gboolean
+sgen_need_bridge_processing (void)
 {
        return bridge_callbacks.cross_references != NULL;
 }
 
 static HashEntry*
-get_hash_entry (MonoObject *obj)
+get_hash_entry (MonoObject *obj, gboolean *existing)
 {
-       HashEntry *entry = mono_sgen_hash_table_lookup (&hash_table, obj);
+       HashEntry *entry = sgen_hash_table_lookup (&hash_table, obj);
        HashEntry new_entry;
 
-       if (entry)
+       if (entry) {
+               if (existing)
+                       *existing = TRUE;
                return entry;
+       }
+       if (existing)
+               *existing = FALSE;
 
        memset (&new_entry, 0, sizeof (HashEntry));
 
@@ -318,9 +341,9 @@ get_hash_entry (MonoObject *obj)
        new_entry.finishing_time = -1;
        new_entry.scc_index = -1;
 
-       mono_sgen_hash_table_replace (&hash_table, obj, &new_entry);
+       sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
 
-       return mono_sgen_hash_table_lookup (&hash_table, obj);
+       return sgen_hash_table_lookup (&hash_table, obj);
 }
 
 static void
@@ -344,18 +367,18 @@ free_data (void)
                dyn_array_uninit (&entry->srcs);
        } SGEN_HASH_TABLE_FOREACH_END;
 
-       mono_sgen_hash_table_clean (&hash_table);
+       sgen_hash_table_clean (&hash_table);
 
        dyn_array_uninit (&merge_array);
        //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
 }
 
-static void
+static HashEntry*
 register_bridge_object (MonoObject *obj)
 {
-       HashEntry *entry = get_hash_entry (obj);
-       g_assert (!entry->is_bridge);
+       HashEntry *entry = get_hash_entry (obj, NULL);
        entry->is_bridge = TRUE;
+       return entry;
 }
 
 static void
@@ -372,13 +395,14 @@ object_is_live (MonoObject **objp)
        MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
        if (fwd) {
                *objp = fwd;
-               return mono_sgen_hash_table_lookup (&hash_table, fwd) == NULL;
+               return sgen_hash_table_lookup (&hash_table, fwd) == NULL;
        }
-       if (!mono_sgen_object_is_live (obj))
+       if (!sgen_object_is_live (obj))
                return FALSE;
-       return mono_sgen_hash_table_lookup (&hash_table, obj) == NULL;
+       return sgen_hash_table_lookup (&hash_table, obj) == NULL;
 }
 
+static DynArray registered_bridges = DYN_ARRAY_PTR_STATIC_INITIALIZER;
 static DynArray dfs_stack;
 
 static int dsf1_passes, dsf2_passes;
@@ -389,7 +413,7 @@ static int dsf1_passes, dsf2_passes;
                MonoObject *dst = (MonoObject*)*(ptr);                  \
                if (dst && !object_is_live (&dst)) {                    \
                        dyn_array_ptr_push (&dfs_stack, obj_entry);     \
-                       dyn_array_ptr_push (&dfs_stack, get_hash_entry (dst)); \
+                       dyn_array_ptr_push (&dfs_stack, get_hash_entry (dst, NULL)); \
                }                                                       \
        } while (0)
 
@@ -414,10 +438,10 @@ dfs1 (HashEntry *obj_entry, HashEntry *src)
                        start = (char*)obj;
 
                        if (src) {
-                               //g_print ("link %s -> %s\n", mono_sgen_safe_name (src->obj), mono_sgen_safe_name (obj));
+                               //g_print ("link %s -> %s\n", sgen_safe_name (src->obj), sgen_safe_name (obj));
                                add_source (obj_entry, src);
                        } else {
-                               //g_print ("starting with %s\n", mono_sgen_safe_name (obj));
+                               //g_print ("starting with %s\n", sgen_safe_name (obj));
                        }
 
                        if (obj_entry->is_visited)
@@ -433,7 +457,7 @@ dfs1 (HashEntry *obj_entry, HashEntry *src)
                } else {
                        obj_entry = dyn_array_ptr_pop (&dfs_stack);
 
-                       //g_print ("finish %s\n", mono_sgen_safe_name (obj_entry->obj));
+                       //g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
                        register_finishing_time (obj_entry, current_time++);
                }
        } while (dfs_stack.size > 0);
@@ -508,37 +532,36 @@ static int fist_pass_links, second_pass_links, sccs_links;
 static int max_sccs_links = 0;
 
 void
-mono_sgen_bridge_processing_start (int num_objs, MonoObject **objs)
+sgen_bridge_register_finalized_object (MonoObject *obj)
+{
+       g_assert (sgen_need_bridge_processing ());
+       dyn_array_ptr_push (&registered_bridges, obj);
+}
+
+void
+sgen_bridge_reset_data (void)
+{
+       registered_bridges.size = 0;
+}
+
+void
+sgen_bridge_processing_stw_step (void)
 {
-       int j = 0;
        int i;
        SGEN_TV_DECLARE (atv);
        SGEN_TV_DECLARE (btv);
 
-       fist_pass_links = second_pass_links = sccs_links = 0;
-       dsf1_passes = dsf2_passes = 0;
-       SGEN_TV_GETTIME (atv);
-
-       g_assert (mono_sgen_need_bridge_processing ());
-
-       //g_print ("%d finalized objects\n", num_objs);
-
-       /* remove objects that are not bridge objects */
+       if (!registered_bridges.size)
+               return;
 
-       for (i = 0; i < num_objs; ++i) {
-               MonoObject *obj = objs [i];
-               //g_assert (!mono_sgen_object_is_live (obj));
-               if (bridge_callbacks.is_bridge_object (obj)) {
-                       register_bridge_object (obj);
-                       objs [j++] = obj;
-               }
-       }
-       num_objs = j;
+       /*
+        * bridge_processing_in_progress must be set with the world
+        * stopped.  If not there would be race conditions.
+        */
+       g_assert (!bridge_processing_in_progress);
+       bridge_processing_in_progress = TRUE;
 
        SGEN_TV_GETTIME (btv);
-       step_1 = SGEN_TV_ELAPSED (atv, btv);
-
-       //g_print ("%d bridge objects\n", num_objs);
 
        /* first DFS pass */
 
@@ -546,15 +569,37 @@ mono_sgen_bridge_processing_start (int num_objs, MonoObject **objs)
        dyn_array_int_init (&merge_array);
 
        current_time = 0;
-       for (i = 0; i < num_objs; ++i)
-               dfs1 (get_hash_entry (objs [i]), NULL);
+       /*
+       First we insert all bridges into the hash table and then we do dfs1.
+
+       It must be done in 2 steps since the bridge arrays doesn't come in reverse topological order,
+       which means that we can have entry N pointing to entry N + 1.
+
+       If we dfs1 entry N before N + 1 is registered we'll not consider N + 1 for this bridge
+       pass and not create the required xref between the two.
+       */
+       for (i = 0; i < registered_bridges.size; ++i)
+               register_bridge_object (DYN_ARRAY_PTR_REF (&registered_bridges, i));
+
+       for (i = 0; i < registered_bridges.size; ++i)
+               dfs1 (get_hash_entry (DYN_ARRAY_PTR_REF (&registered_bridges, i), NULL), NULL);
 
        SGEN_TV_GETTIME (atv);
        step_2 = SGEN_TV_ELAPSED (btv, atv);
 }
 
+static mono_bool
+is_bridge_object_alive (MonoObject *obj, void *data)
+{
+       SgenHashTable *table = data;
+       unsigned char *value = sgen_hash_table_lookup (table, obj);
+       if (!value)
+               return TRUE;
+       return *value;
+}
+
 void
-mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs)
+sgen_bridge_processing_finish (int generation)
 {
        int i, j;
        int num_sccs, num_xrefs;
@@ -562,17 +607,24 @@ mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs)
        int hash_table_size, sccs_size;
        MonoObject *obj;
        HashEntry *entry;
+       int num_registered_bridges;
        HashEntry **all_entries;
        MonoGCBridgeSCC **api_sccs;
        MonoGCBridgeXRef *api_xrefs;
+       SgenHashTable alive_hash = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE, INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY, 1, mono_aligned_addr_hash, NULL);
        SGEN_TV_DECLARE (atv);
        SGEN_TV_DECLARE (btv);
 
+       if (!registered_bridges.size)
+               return;
+
+       g_assert (bridge_processing_in_progress);
+
        SGEN_TV_GETTIME (atv);
 
        /* alloc and fill array of all entries */
 
-       all_entries = mono_sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
+       all_entries = sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
 
        j = 0;
        SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
@@ -633,7 +685,7 @@ mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs)
                max_sccs_links = MAX (max_sccs_links, scc->xrefs.size);
        }
 
-       api_sccs = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
+       api_sccs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
        num_xrefs = 0;
        j = 0;
        for (i = 0; i < sccs.size; ++i) {
@@ -641,7 +693,8 @@ mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs)
                if (!scc->num_bridge_entries)
                        continue;
 
-               api_sccs [j] = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA);
+               api_sccs [j] = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
+               api_sccs [j]->is_alive = FALSE;
                api_sccs [j]->num_objs = scc->num_bridge_entries;
                scc->num_bridge_entries = 0;
                scc->api_index = j++;
@@ -656,7 +709,7 @@ mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs)
                }
        } SGEN_HASH_TABLE_FOREACH_END;
 
-       api_xrefs = mono_sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
+       api_xrefs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
        j = 0;
        for (i = 0; i < sccs.size; ++i) {
                int k;
@@ -693,9 +746,12 @@ mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs)
        }
        dyn_array_uninit (&sccs);
 
-       mono_sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
+       sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
 
        free_data ();
+       /* Empty the registered bridges array */
+       num_registered_bridges = registered_bridges.size;
+       registered_bridges.size = 0;
 
        SGEN_TV_GETTIME (atv);
        step_6 = SGEN_TV_ELAPSED (btv, atv);
@@ -706,33 +762,45 @@ mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs)
 
        bridge_callbacks.cross_references (num_sccs, api_sccs, num_xrefs, api_xrefs);
 
-/*Release for finalization those objects we no longer care. */
+       /* Release for finalization those objects we no longer care. */
        SGEN_TV_GETTIME (btv);
        step_7 = SGEN_TV_ELAPSED (atv, btv);
 
        for (i = 0; i < num_sccs; ++i) {
-               if (!api_sccs [i]->objs [0])
-                       continue;
-               for (j = 0; j < api_sccs [i]->num_objs; ++j)
-                       mono_sgen_mark_bridge_object (api_sccs [i]->objs [j]);
+               unsigned char alive = api_sccs [i]->is_alive ? 1 : 0;
+               for (j = 0; j < api_sccs [i]->num_objs; ++j) {
+                       /* Build hash table for nulling weak links. */
+                       sgen_hash_table_replace (&alive_hash, api_sccs [i]->objs [j], &alive, NULL);
+
+                       /* Release for finalization those objects we no longer care. */
+                       if (!api_sccs [i]->is_alive)
+                               sgen_mark_bridge_object (api_sccs [i]->objs [j]);
+               }
        }
 
+       /* Null weak links to dead objects. */
+       sgen_null_links_with_predicate (GENERATION_NURSERY, is_bridge_object_alive, &alive_hash);
+       if (generation == GENERATION_OLD)
+               sgen_null_links_with_predicate (GENERATION_OLD, is_bridge_object_alive, &alive_hash);
+
+       sgen_hash_table_clean (&alive_hash);
+
        /* free callback data */
 
        for (i = 0; i < num_sccs; ++i) {
-               mono_sgen_free_internal_dynamic (api_sccs [i],
+               sgen_free_internal_dynamic (api_sccs [i],
                                sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * api_sccs [i]->num_objs,
                                INTERNAL_MEM_BRIDGE_DATA);
        }
-       mono_sgen_free_internal_dynamic (api_sccs, sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
+       sgen_free_internal_dynamic (api_sccs, sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
 
-       mono_sgen_free_internal_dynamic (api_xrefs, sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
+       sgen_free_internal_dynamic (api_xrefs, sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
 
        SGEN_TV_GETTIME (atv);
        step_8 = SGEN_TV_ELAPSED (btv, atv);
 
        mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_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 user-cb %.2fms clenanup %.2fms links %d/%d/%d/%d dfs passes %d/%d",
-               num_objs, hash_table_size, sccs.size,
+               num_registered_bridges, hash_table_size, sccs.size,
                step_1 / 1000.0f,
                step_2 / 1000.0f,
                step_3 / 1000.0f,
@@ -744,10 +812,21 @@ mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs)
                fist_pass_links, second_pass_links, sccs_links, max_sccs_links,
                dsf1_passes, dsf2_passes);
 
+       step_1 = 0; /* We must cleanup since this value is used as an accumulator. */
+
+       bridge_processing_in_progress = FALSE;
 }
 
+static const char *bridge_class;
+
 static gboolean
-bridge_test_is_bridge_object (MonoObject *obj)
+bridge_test_is_bridge_class (MonoClass *class)
+{
+       return !strcmp (bridge_class, class->name);
+}
+
+static gboolean
+bridge_test_is_bridge_object (MonoObject *object)
 {
        return TRUE;
 }
@@ -758,25 +837,31 @@ bridge_test_cross_reference (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs
        int i;
        for (i = 0; i < num_sccs; ++i) {
                int j;
-               g_print ("--- SCC %d\n", i);
-               for (j = 0; j < sccs [i]->num_objs; ++j)
-                       g_print ("  %s\n", mono_sgen_safe_name (sccs [i]->objs [j]));
+       //      g_print ("--- SCC %d\n", i);
+               for (j = 0; j < sccs [i]->num_objs; ++j) {
+       //              g_print ("  %s\n", sgen_safe_name (sccs [i]->objs [j]));
+                       if (i & 1) /*retain half of the bridged objects */
+                               sccs [i]->objs [0] = NULL;
+               }
        }
        for (i = 0; i < num_xrefs; ++i) {
                g_assert (xrefs [i].src_scc_index >= 0 && xrefs [i].src_scc_index < num_sccs);
                g_assert (xrefs [i].dst_scc_index >= 0 && xrefs [i].dst_scc_index < num_sccs);
-               g_print ("%d -> %d\n", xrefs [i].src_scc_index, xrefs [i].dst_scc_index);
+       //      g_print ("%d -> %d\n", xrefs [i].src_scc_index, xrefs [i].dst_scc_index);
        }
 }
 
 
 void
-mono_sgen_register_test_bridge_callbacks (void)
+sgen_register_test_bridge_callbacks (const char *bridge_class_name)
 {
        MonoGCBridgeCallbacks callbacks;
+       callbacks.bridge_version = SGEN_BRIDGE_VERSION;
+       callbacks.is_bridge_class = bridge_test_is_bridge_class;
        callbacks.is_bridge_object = bridge_test_is_bridge_object;
        callbacks.cross_references = bridge_test_cross_reference;
        mono_gc_register_bridge_callbacks (&callbacks);
+       bridge_class = bridge_class_name;
 }
 
 #endif