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.
18 #include "sgen/sgen-gc.h"
19 #include "sgen-bridge-internals.h"
20 #include "sgen/sgen-hash-table.h"
21 #include "sgen/sgen-qsort.h"
22 #include "utils/mono-logger-internals.h"
24 MonoGCBridgeCallbacks bridge_callbacks;
25 static SgenBridgeProcessor bridge_processor;
26 static SgenBridgeProcessor compare_to_bridge_processor;
28 volatile gboolean bridge_processing_in_progress = FALSE;
31 mono_gc_wait_for_bridge_processing (void)
33 if (!bridge_processing_in_progress)
36 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_BRIDGE waiting for bridge processing to finish");
44 mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks)
46 if (callbacks->bridge_version != SGEN_BRIDGE_VERSION)
47 g_error ("Invalid bridge callback version. Expected %d but got %d\n", SGEN_BRIDGE_VERSION, callbacks->bridge_version);
49 bridge_callbacks = *callbacks;
51 if (!bridge_processor.reset_data)
52 sgen_new_bridge_init (&bridge_processor);
56 init_bridge_processor (SgenBridgeProcessor *processor, const char *name)
58 if (!strcmp ("old", name)) {
59 memset (processor, 0, sizeof (SgenBridgeProcessor));
60 sgen_old_bridge_init (processor);
61 } else if (!strcmp ("new", name)) {
62 memset (processor, 0, sizeof (SgenBridgeProcessor));
63 sgen_new_bridge_init (processor);
64 } else if (!strcmp ("tarjan", name)) {
65 memset (processor, 0, sizeof (SgenBridgeProcessor));
66 sgen_tarjan_bridge_init (processor);
74 sgen_set_bridge_implementation (const char *name)
76 if (!init_bridge_processor (&bridge_processor, name))
77 g_warning ("Invalid value for bridge implementation, valid values are: 'new', 'old' and 'tarjan'.");
81 sgen_is_bridge_object (GCObject *obj)
83 if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OBJECT) != SGEN_GC_BIT_BRIDGE_OBJECT)
85 return bridge_callbacks.is_bridge_object (obj);
89 sgen_need_bridge_processing (void)
91 return bridge_callbacks.cross_references != NULL;
95 compare_bridge_processors (void)
97 return compare_to_bridge_processor.reset_data != NULL;
100 /* Dispatch wrappers */
102 sgen_bridge_reset_data (void)
104 bridge_processor.reset_data ();
105 if (compare_bridge_processors ())
106 compare_to_bridge_processor.reset_data ();
110 sgen_bridge_processing_stw_step (void)
113 * bridge_processing_in_progress must be set with the world
114 * stopped. If not there would be race conditions.
116 bridge_processing_in_progress = TRUE;
118 bridge_processor.processing_stw_step ();
119 if (compare_bridge_processors ())
120 compare_to_bridge_processor.processing_stw_step ();
124 is_bridge_object_dead (GCObject *obj, void *data)
126 SgenHashTable *table = (SgenHashTable *)data;
127 unsigned char *value = (unsigned char *)sgen_hash_table_lookup (table, obj);
134 null_weak_links_to_dead_objects (SgenBridgeProcessor *processor, int generation)
137 int num_sccs = processor->num_sccs;
138 MonoGCBridgeSCC **api_sccs = processor->api_sccs;
139 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);
141 for (i = 0; i < num_sccs; ++i) {
142 unsigned char alive = api_sccs [i]->is_alive ? 1 : 0;
143 for (j = 0; j < api_sccs [i]->num_objs; ++j) {
144 /* Build hash table for nulling weak links. */
145 sgen_hash_table_replace (&alive_hash, api_sccs [i]->objs [j], &alive, NULL);
147 /* Release for finalization those objects we no longer care. */
148 if (!api_sccs [i]->is_alive)
149 sgen_mark_bridge_object (api_sccs [i]->objs [j]);
153 /* Null weak links to dead objects. */
154 sgen_null_links_if (is_bridge_object_dead, &alive_hash, GENERATION_NURSERY, FALSE);
155 sgen_null_links_if (is_bridge_object_dead, &alive_hash, GENERATION_NURSERY, TRUE);
156 if (generation == GENERATION_OLD) {
157 sgen_null_links_if (is_bridge_object_dead, &alive_hash, GENERATION_OLD, FALSE);
158 sgen_null_links_if (is_bridge_object_dead, &alive_hash, GENERATION_OLD, TRUE);
161 sgen_hash_table_clean (&alive_hash);
165 free_callback_data (SgenBridgeProcessor *processor)
168 int num_sccs = processor->num_sccs;
169 int num_xrefs = processor->num_xrefs;
170 MonoGCBridgeSCC **api_sccs = processor->api_sccs;
171 MonoGCBridgeXRef *api_xrefs = processor->api_xrefs;
173 for (i = 0; i < num_sccs; ++i) {
174 sgen_free_internal_dynamic (api_sccs [i],
175 sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * api_sccs [i]->num_objs,
176 INTERNAL_MEM_BRIDGE_DATA);
178 sgen_free_internal_dynamic (api_sccs, sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
180 sgen_free_internal_dynamic (api_xrefs, sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
182 processor->num_sccs = 0;
183 processor->api_sccs = NULL;
184 processor->num_xrefs = 0;
185 processor->api_xrefs = NULL;
189 compare_xrefs (const void *a_ptr, const void *b_ptr)
191 const MonoGCBridgeXRef *a = (const MonoGCBridgeXRef *)a_ptr;
192 const MonoGCBridgeXRef *b = (const MonoGCBridgeXRef *)b_ptr;
194 if (a->src_scc_index < b->src_scc_index)
196 if (a->src_scc_index > b->src_scc_index)
199 if (a->dst_scc_index < b->dst_scc_index)
201 if (a->dst_scc_index > b->dst_scc_index)
209 dump_processor_state (SgenBridgeProcessor *p)
214 printf ("SCCS %d\n", p->num_sccs);
215 for (i = 0; i < p->num_sccs; ++i) {
217 MonoGCBridgeSCC *scc = p->api_sccs [i];
218 printf ("\tSCC %d:", i);
219 for (j = 0; j < scc->num_objs; ++j) {
220 MonoObject *obj = scc->objs [j];
226 printf ("XREFS %d\n", p->num_xrefs);
227 for (i = 0; i < p->num_xrefs; ++i)
228 printf ("\t%d -> %d\n", p->api_xrefs [i].src_scc_index, p->api_xrefs [i].dst_scc_index);
230 printf ("-------\n");
235 sgen_compare_bridge_processor_results (SgenBridgeProcessor *a, SgenBridgeProcessor *b)
238 SgenHashTable obj_to_a_scc = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_DEBUG, INTERNAL_MEM_BRIDGE_DEBUG, sizeof (int), mono_aligned_addr_hash, NULL);
239 SgenHashTable b_scc_to_a_scc = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_DEBUG, INTERNAL_MEM_BRIDGE_DEBUG, sizeof (int), g_direct_hash, NULL);
240 MonoGCBridgeXRef *a_xrefs, *b_xrefs;
241 size_t xrefs_alloc_size;
243 // dump_processor_state (a);
244 // dump_processor_state (b);
246 if (a->num_sccs != b->num_sccs)
247 g_error ("SCCS count expected %d but got %d", a->num_sccs, b->num_sccs);
248 if (a->num_xrefs != b->num_xrefs)
249 g_error ("SCCS count expected %d but got %d", a->num_xrefs, b->num_xrefs);
252 * First we build a hash of each object in `a` to its respective SCC index within
253 * `a`. Along the way we also assert that no object is more than one SCC.
255 for (i = 0; i < a->num_sccs; ++i) {
257 MonoGCBridgeSCC *scc = a->api_sccs [i];
259 g_assert (scc->num_objs > 0);
261 for (j = 0; j < scc->num_objs; ++j) {
262 GCObject *obj = scc->objs [j];
263 gboolean new_entry = sgen_hash_table_replace (&obj_to_a_scc, obj, &i, NULL);
264 g_assert (new_entry);
269 * Now we check whether each of the objects in `b` are in `a`, and whether the SCCs
270 * of `b` contain the same sets of objects as those of `a`.
272 * While we're doing this, build a hash table to map from `b` SCC indexes to `a` SCC
275 for (i = 0; i < b->num_sccs; ++i) {
276 MonoGCBridgeSCC *scc = b->api_sccs [i];
277 MonoGCBridgeSCC *a_scc;
278 int *a_scc_index_ptr;
283 g_assert (scc->num_objs > 0);
284 a_scc_index_ptr = (int *)sgen_hash_table_lookup (&obj_to_a_scc, scc->objs [0]);
285 g_assert (a_scc_index_ptr);
286 a_scc_index = *a_scc_index_ptr;
288 //g_print ("A SCC %d -> B SCC %d\n", a_scc_index, i);
290 a_scc = a->api_sccs [a_scc_index];
291 g_assert (a_scc->num_objs == scc->num_objs);
293 for (j = 1; j < scc->num_objs; ++j) {
294 a_scc_index_ptr = (int *)sgen_hash_table_lookup (&obj_to_a_scc, scc->objs [j]);
295 g_assert (a_scc_index_ptr);
296 g_assert (*a_scc_index_ptr == a_scc_index);
299 new_entry = sgen_hash_table_replace (&b_scc_to_a_scc, GINT_TO_POINTER (i), &a_scc_index, NULL);
300 g_assert (new_entry);
304 * Finally, check that we have the same xrefs. We do this by making copies of both
305 * xref arrays, and replacing the SCC indexes in the copy for `b` with the
306 * corresponding indexes in `a`. Then we sort both arrays and assert that they're
309 * At the same time, check that no xref is self-referential and that there are no
313 xrefs_alloc_size = a->num_xrefs * sizeof (MonoGCBridgeXRef);
314 a_xrefs = (MonoGCBridgeXRef *)sgen_alloc_internal_dynamic (xrefs_alloc_size, INTERNAL_MEM_BRIDGE_DEBUG, TRUE);
315 b_xrefs = (MonoGCBridgeXRef *)sgen_alloc_internal_dynamic (xrefs_alloc_size, INTERNAL_MEM_BRIDGE_DEBUG, TRUE);
317 memcpy (a_xrefs, a->api_xrefs, xrefs_alloc_size);
318 for (i = 0; i < b->num_xrefs; ++i) {
319 MonoGCBridgeXRef *xref = &b->api_xrefs [i];
322 g_assert (xref->src_scc_index != xref->dst_scc_index);
324 scc_index_ptr = (int *)sgen_hash_table_lookup (&b_scc_to_a_scc, GINT_TO_POINTER (xref->src_scc_index));
325 g_assert (scc_index_ptr);
326 b_xrefs [i].src_scc_index = *scc_index_ptr;
328 scc_index_ptr = (int *)sgen_hash_table_lookup (&b_scc_to_a_scc, GINT_TO_POINTER (xref->dst_scc_index));
329 g_assert (scc_index_ptr);
330 b_xrefs [i].dst_scc_index = *scc_index_ptr;
333 qsort (a_xrefs, a->num_xrefs, sizeof (MonoGCBridgeXRef), compare_xrefs);
334 qsort (b_xrefs, a->num_xrefs, sizeof (MonoGCBridgeXRef), compare_xrefs);
336 for (i = 0; i < a->num_xrefs; ++i) {
337 g_assert (a_xrefs [i].src_scc_index == b_xrefs [i].src_scc_index);
338 g_assert (a_xrefs [i].dst_scc_index == b_xrefs [i].dst_scc_index);
341 sgen_hash_table_clean (&obj_to_a_scc);
342 sgen_hash_table_clean (&b_scc_to_a_scc);
343 sgen_free_internal_dynamic (a_xrefs, xrefs_alloc_size, INTERNAL_MEM_BRIDGE_DEBUG);
344 sgen_free_internal_dynamic (b_xrefs, xrefs_alloc_size, INTERNAL_MEM_BRIDGE_DEBUG);
350 sgen_bridge_processing_finish (int generation)
352 bridge_processor.processing_build_callback_data (generation);
353 if (compare_bridge_processors ())
354 compare_to_bridge_processor.processing_build_callback_data (generation);
356 if (bridge_processor.num_sccs == 0) {
357 g_assert (bridge_processor.num_xrefs == 0);
361 bridge_callbacks.cross_references (bridge_processor.num_sccs, bridge_processor.api_sccs,
362 bridge_processor.num_xrefs, bridge_processor.api_xrefs);
364 if (compare_bridge_processors ())
365 sgen_compare_bridge_processor_results (&bridge_processor, &compare_to_bridge_processor);
367 null_weak_links_to_dead_objects (&bridge_processor, generation);
369 free_callback_data (&bridge_processor);
370 if (compare_bridge_processors ())
371 free_callback_data (&compare_to_bridge_processor);
374 bridge_processor.processing_after_callback (generation);
375 if (compare_bridge_processors ())
376 compare_to_bridge_processor.processing_after_callback (generation);
378 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_BRIDGE: Complete, was running for %.2fms", mono_time_since_last_stw () / 10000.0f);
380 bridge_processing_in_progress = FALSE;
383 MonoGCBridgeObjectKind
384 sgen_bridge_class_kind (MonoClass *klass)
386 return bridge_processor.class_kind (klass);
390 sgen_bridge_register_finalized_object (GCObject *obj)
392 bridge_processor.register_finalized_object (obj);
393 if (compare_bridge_processors ())
394 compare_to_bridge_processor.register_finalized_object (obj);
398 sgen_bridge_describe_pointer (GCObject *obj)
400 if (bridge_processor.describe_pointer)
401 bridge_processor.describe_pointer (obj);
405 set_dump_prefix (const char *prefix)
407 if (!bridge_processor.set_dump_prefix) {
408 fprintf (stderr, "Warning: Bridge implementation does not support dumping - ignoring.\n");
412 bridge_processor.set_dump_prefix (prefix);
415 /* Test support code */
416 static const char *bridge_class;
418 static MonoGCBridgeObjectKind
419 bridge_test_bridge_class_kind (MonoClass *klass)
421 if (!strcmp (bridge_class, klass->name))
422 return GC_BRIDGE_TRANSPARENT_BRIDGE_CLASS;
423 return GC_BRIDGE_TRANSPARENT_CLASS;
427 bridge_test_is_bridge_object (MonoObject *object)
433 bridge_test_cross_reference (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
436 for (i = 0; i < num_sccs; ++i) {
438 // g_print ("--- SCC %d\n", i);
439 for (j = 0; j < sccs [i]->num_objs; ++j) {
440 // g_print (" %s\n", sgen_safe_name (sccs [i]->objs [j]));
441 if (i & 1) /*retain half of the bridged objects */
442 sccs [i]->is_alive = TRUE;
445 for (i = 0; i < num_xrefs; ++i) {
446 g_assert (xrefs [i].src_scc_index >= 0 && xrefs [i].src_scc_index < num_sccs);
447 g_assert (xrefs [i].dst_scc_index >= 0 && xrefs [i].dst_scc_index < num_sccs);
448 // g_print ("%d -> %d\n", xrefs [i].src_scc_index, xrefs [i].dst_scc_index);
452 static MonoClassField *mono_bridge_test_field;
462 test_scc (MonoGCBridgeSCC *scc, int i)
464 int status = BRIDGE_DEAD;
465 mono_field_get_value (scc->objs [i], mono_bridge_test_field, &status);
470 mark_scc (MonoGCBridgeSCC *scc, int value)
473 for (i = 0; i < scc->num_objs; ++i) {
474 if (!test_scc (scc, i)) {
476 mono_field_set_value (scc->objs [i], mono_bridge_test_field, &status);
482 bridge_test_cross_reference2 (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
487 if (!mono_bridge_test_field) {
488 mono_bridge_test_field = mono_class_get_field_from_name (mono_object_get_class (sccs[0]->objs [0]), "__test");
489 g_assert (mono_bridge_test_field);
492 /*We mark all objects in a scc with live objects as reachable by scc*/
493 for (i = 0; i < num_sccs; ++i) {
495 gboolean live = FALSE;
496 for (j = 0; j < sccs [i]->num_objs; ++j) {
497 if (test_scc (sccs [i], j)) {
504 for (j = 0; j < sccs [i]->num_objs; ++j) {
505 if (!test_scc (sccs [i], j)) {
506 int status = BRIDGE_SAME_SCC;
507 mono_field_set_value (sccs [i]->objs [j], mono_bridge_test_field, &status);
512 /*Now we mark the transitive closure of reachable objects from the xrefs*/
516 /* Mark all objects that are brought to life due to xrefs*/
517 for (i = 0; i < num_xrefs; ++i) {
518 MonoGCBridgeXRef ref = xrefs [i];
519 if (test_scc (sccs [ref.src_scc_index], 0) && !test_scc (sccs [ref.dst_scc_index], 0)) {
521 mark_scc (sccs [ref.dst_scc_index], BRIDGE_XREF);
526 /* keep everything in memory, all we want to do is test persistence */
527 for (i = 0; i < num_sccs; ++i)
528 sccs [i]->is_alive = TRUE;
531 /* This bridge keeps all peers with __test > 0 */
533 bridge_test_positive_status (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
537 if (!mono_bridge_test_field) {
538 mono_bridge_test_field = mono_class_get_field_from_name (mono_object_get_class (sccs[0]->objs [0]), "__test");
539 g_assert (mono_bridge_test_field);
542 /*We mark all objects in a scc with live objects as reachable by scc*/
543 for (i = 0; i < num_sccs; ++i) {
545 for (j = 0; j < sccs [i]->num_objs; ++j) {
546 if (test_scc (sccs [i], j)) {
547 sccs [i]->is_alive = TRUE;
556 register_test_bridge_callbacks (const char *bridge_class_name)
558 MonoGCBridgeCallbacks callbacks;
559 callbacks.bridge_version = SGEN_BRIDGE_VERSION;
560 callbacks.bridge_class_kind = bridge_test_bridge_class_kind;
561 callbacks.is_bridge_object = bridge_test_is_bridge_object;
563 switch (bridge_class_name [0]) {
565 bridge_class = bridge_class_name + 1;
566 callbacks.cross_references = bridge_test_cross_reference2;
569 bridge_class = bridge_class_name + 1;
570 callbacks.cross_references = bridge_test_positive_status;
573 bridge_class = bridge_class_name;
574 callbacks.cross_references = bridge_test_cross_reference;
576 mono_gc_register_bridge_callbacks (&callbacks);
580 sgen_bridge_handle_gc_debug (const char *opt)
582 if (g_str_has_prefix (opt, "bridge=")) {
583 opt = strchr (opt, '=') + 1;
584 register_test_bridge_callbacks (g_strdup (opt));
585 } else if (!strcmp (opt, "enable-bridge-accounting")) {
586 bridge_processor.enable_accounting ();
587 } else if (g_str_has_prefix (opt, "bridge-dump=")) {
588 char *prefix = strchr (opt, '=') + 1;
589 set_dump_prefix (prefix);
590 } else if (g_str_has_prefix (opt, "bridge-compare-to=")) {
591 const char *name = strchr (opt, '=') + 1;
592 if (init_bridge_processor (&compare_to_bridge_processor, name)) {
593 if (compare_to_bridge_processor.reset_data == bridge_processor.reset_data) {
594 g_warning ("Cannot compare bridge implementation to itself - ignoring.");
595 memset (&compare_to_bridge_processor, 0, sizeof (SgenBridgeProcessor));
598 g_warning ("Invalid bridge implementation to compare against - ignoring.");
607 sgen_bridge_print_gc_debug_usage (void)
609 fprintf (stderr, " bridge=<class-name>\n");
610 fprintf (stderr, " enable-bridge-accounting\n");
611 fprintf (stderr, " bridge-dump=<filename-prefix>\n");
612 fprintf (stderr, " bridge-compare-to=<implementation>\n");