#include "sgen-gc.h"
#include "sgen-bridge.h"
+#include "sgen-hash-table.h"
#include "utils/mono-logger-internal.h"
#include "utils/mono-time.h"
#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)
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;
}
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;
}
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
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));
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
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
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;
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)
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)
} 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);
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 (®istered_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 */
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 (®istered_bridges, i));
+
+ for (i = 0; i < registered_bridges.size; ++i)
+ dfs1 (get_hash_entry (DYN_ARRAY_PTR_REF (®istered_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;
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) {
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) {
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++;
}
} 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;
}
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);
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,
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;
}
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