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.
47 #include "sgen-bridge.h"
48 #include "utils/mono-logger-internal.h"
49 #include "utils/mono-time.h"
59 #define DYN_ARRAY_REF(da,i) ((void*)((da)->data + (i) * (da)->elem_size))
60 #define DYN_ARRAY_PTR_REF(da,i) (((void**)(da)->data) [(i)])
61 #define DYN_ARRAY_INT_REF(da,i) (((int*)(da)->data) [(i)])
62 #define DYN_ARRAY_PTR_STATIC_INITIALIZER { 0, sizeof (void*), 0, NULL }
63 #define DYN_ARRAY_INT_STATIC_INITIALIZER { 0, sizeof (int), 0, NULL }
66 dyn_array_init (DynArray *da, int elem_size)
69 da->elem_size = elem_size;
75 dyn_array_ptr_init (DynArray *da)
77 dyn_array_init (da, sizeof (void*));
81 dyn_array_int_init (DynArray *da)
83 dyn_array_init (da, sizeof (int));
87 dyn_array_uninit (DynArray *da)
89 if (da->capacity <= 0)
92 sgen_free_internal_dynamic (da->data, da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
97 dyn_array_ensure_capacity (DynArray *da, int capacity)
99 int old_capacity = da->capacity;
102 if (capacity <= old_capacity)
105 if (da->capacity == 0)
107 while (capacity > da->capacity)
110 new_data = sgen_alloc_internal_dynamic (da->elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
111 memcpy (new_data, da->data, da->elem_size * da->size);
112 sgen_free_internal_dynamic (da->data, da->elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
117 dyn_array_add (DynArray *da)
121 dyn_array_ensure_capacity (da, da->size + 1);
123 p = DYN_ARRAY_REF (da, da->size);
129 dyn_array_ptr_add (DynArray *da, void *ptr)
131 void **p = dyn_array_add (da);
135 #define dyn_array_ptr_push dyn_array_ptr_add
138 dyn_array_ptr_pop (DynArray *da)
141 g_assert (da->size > 0);
142 p = DYN_ARRAY_PTR_REF (da, da->size - 1);
148 dyn_array_int_add (DynArray *da, int x)
150 int *p = dyn_array_add (da);
156 dyn_array_ptr_contains (DynArray *da, void *ptr)
159 for (i = 0; i < da->size; ++i)
160 if (DYN_ARRAY_PTR_REF (da, i) == ptr)
167 dyn_array_int_contains (DynArray *da, int x)
170 for (i = 0; i < da->size; ++i)
171 if (DYN_ARRAY_INT_REF (da, i) == x)
176 static DynArray merge_array;
179 dyn_array_int_merge (DynArray *dst, DynArray *src)
183 dyn_array_ensure_capacity (&merge_array, dst->size + src->size);
184 merge_array.size = 0;
186 for (i = j = 0; i < dst->size || j < src->size; ) {
187 if (i < dst->size && j < src->size) {
188 int a = DYN_ARRAY_INT_REF (dst, i);
189 int b = DYN_ARRAY_INT_REF (src, j);
191 dyn_array_int_add (&merge_array, a);
194 dyn_array_int_add (&merge_array, a);
198 dyn_array_int_add (&merge_array, b);
201 } else if (i < dst->size) {
202 dyn_array_int_add (&merge_array, DYN_ARRAY_INT_REF (dst, i));
205 dyn_array_int_add (&merge_array, DYN_ARRAY_INT_REF (src, j));
210 if (merge_array.size > dst->size) {
211 dyn_array_ensure_capacity (dst, merge_array.size);
212 memcpy (DYN_ARRAY_REF (dst, 0), DYN_ARRAY_REF (&merge_array, 0), merge_array.size * merge_array.elem_size);
213 dst->size = merge_array.size;
218 dyn_array_int_merge_one (DynArray *array, int value)
222 int end = array->size;
224 for (i = 0; i < end; ++i) {
225 if (DYN_ARRAY_INT_REF (array, i) == value)
227 else if (DYN_ARRAY_INT_REF (array, i) > value)
231 dyn_array_ensure_capacity (array, array->size + 1);
234 tmp = DYN_ARRAY_INT_REF (array, i);
235 for (; i <= end; ++i) {
236 DYN_ARRAY_INT_REF (array, i) = value;
238 tmp = DYN_ARRAY_INT_REF (array, i + 1);
240 DYN_ARRAY_INT_REF (array, end + 1) = tmp;
242 DYN_ARRAY_INT_REF (array, end) = value;
248 * FIXME: Optimizations:
250 * Don't allocate a scrs array for just one source. Most objects have
251 * just one source, so use the srcs pointer itself.
253 typedef struct _HashEntry {
254 MonoObject *obj; /* This is a duplicate - it's already stored in the hash table */
266 typedef struct _SCC {
269 int num_bridge_entries;
270 DynArray xrefs; /* these are incoming, not outgoing */
273 static SgenHashTable hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_DATA, INTERNAL_MEM_BRIDGE_DATA, sizeof (HashEntry), mono_aligned_addr_hash, NULL);
275 static MonoGCBridgeCallbacks bridge_callbacks;
277 static int current_time;
280 mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks)
282 if (callbacks->bridge_version != SGEN_BRIDGE_VERSION)
283 g_error ("Invalid bridge callback version. Expected %d but got %d\n", SGEN_BRIDGE_VERSION, callbacks->bridge_version);
285 bridge_callbacks = *callbacks;
289 sgen_is_bridge_object (MonoObject *obj)
291 if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OBJECT) != SGEN_GC_BIT_BRIDGE_OBJECT)
293 return bridge_callbacks.is_bridge_object (obj);
297 sgen_is_bridge_class (MonoClass *class)
299 return bridge_callbacks.is_bridge_class (class);
303 sgen_need_bridge_processing (void)
305 return bridge_callbacks.cross_references != NULL;
309 get_hash_entry (MonoObject *obj, gboolean *existing)
311 HashEntry *entry = sgen_hash_table_lookup (&hash_table, obj);
322 memset (&new_entry, 0, sizeof (HashEntry));
325 dyn_array_ptr_init (&new_entry.srcs);
326 new_entry.finishing_time = -1;
327 new_entry.scc_index = -1;
329 sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
331 return sgen_hash_table_lookup (&hash_table, obj);
335 add_source (HashEntry *entry, HashEntry *src)
337 dyn_array_ptr_add (&entry->srcs, src);
348 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
349 total_srcs += entry->srcs.size;
350 if (entry->srcs.size > max_srcs)
351 max_srcs = entry->srcs.size;
352 dyn_array_uninit (&entry->srcs);
353 } SGEN_HASH_TABLE_FOREACH_END;
355 sgen_hash_table_clean (&hash_table);
357 dyn_array_uninit (&merge_array);
358 //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
362 register_bridge_object (MonoObject *obj)
364 HashEntry *entry = get_hash_entry (obj, NULL);
365 entry->is_bridge = TRUE;
370 register_finishing_time (HashEntry *entry, int t)
372 g_assert (entry->finishing_time < 0);
373 entry->finishing_time = t;
377 object_is_live (MonoObject **objp)
379 MonoObject *obj = *objp;
380 MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
383 return sgen_hash_table_lookup (&hash_table, fwd) == NULL;
385 if (!sgen_object_is_live (obj))
387 return sgen_hash_table_lookup (&hash_table, obj) == NULL;
390 static DynArray registered_bridges = DYN_ARRAY_PTR_STATIC_INITIALIZER;
391 static DynArray dfs_stack;
393 static int dsf1_passes, dsf2_passes;
397 #define HANDLE_PTR(ptr,obj) do { \
398 MonoObject *dst = (MonoObject*)*(ptr); \
399 if (dst && !object_is_live (&dst)) { \
400 dyn_array_ptr_push (&dfs_stack, obj_entry); \
401 dyn_array_ptr_push (&dfs_stack, get_hash_entry (dst, NULL)); \
406 dfs1 (HashEntry *obj_entry, HashEntry *src)
408 g_assert (dfs_stack.size == 0);
410 dyn_array_ptr_push (&dfs_stack, src);
411 dyn_array_ptr_push (&dfs_stack, obj_entry);
418 obj_entry = dyn_array_ptr_pop (&dfs_stack);
420 src = dyn_array_ptr_pop (&dfs_stack);
422 obj = obj_entry->obj;
426 //g_print ("link %s -> %s\n", sgen_safe_name (src->obj), sgen_safe_name (obj));
427 add_source (obj_entry, src);
429 //g_print ("starting with %s\n", sgen_safe_name (obj));
432 if (obj_entry->is_visited)
435 obj_entry->is_visited = TRUE;
437 dyn_array_ptr_push (&dfs_stack, obj_entry);
438 /* NULL marks that the next entry is to be finished */
439 dyn_array_ptr_push (&dfs_stack, NULL);
441 #include "sgen-scan-object.h"
443 obj_entry = dyn_array_ptr_pop (&dfs_stack);
445 //g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
446 register_finishing_time (obj_entry, current_time++);
448 } while (dfs_stack.size > 0);
452 scc_add_xref (SCC *src, SCC *dst)
454 g_assert (src != dst);
455 g_assert (src->index != dst->index);
457 if (dyn_array_int_contains (&dst->xrefs, src->index))
459 if (src->num_bridge_entries) {
460 dyn_array_int_merge_one (&dst->xrefs, src->index);
463 dyn_array_int_merge (&dst->xrefs, &src->xrefs);
464 for (i = 0; i < dst->xrefs.size; ++i)
465 g_assert (DYN_ARRAY_INT_REF (&dst->xrefs, i) != dst->index);
470 scc_add_entry (SCC *scc, HashEntry *entry)
472 g_assert (entry->scc_index < 0);
473 entry->scc_index = scc->index;
474 if (entry->is_bridge)
475 ++scc->num_bridge_entries;
478 static DynArray sccs;
479 static SCC *current_scc;
482 dfs2 (HashEntry *entry)
486 g_assert (dfs_stack.size == 0);
488 dyn_array_ptr_push (&dfs_stack, entry);
491 entry = dyn_array_ptr_pop (&dfs_stack);
494 if (entry->scc_index >= 0) {
495 if (entry->scc_index != current_scc->index)
496 scc_add_xref (DYN_ARRAY_REF (&sccs, entry->scc_index), current_scc);
500 scc_add_entry (current_scc, entry);
502 for (i = 0; i < entry->srcs.size; ++i)
503 dyn_array_ptr_push (&dfs_stack, DYN_ARRAY_PTR_REF (&entry->srcs, i));
504 } while (dfs_stack.size > 0);
508 compare_hash_entries (const void *ep1, const void *ep2)
510 HashEntry *e1 = *(HashEntry**)ep1;
511 HashEntry *e2 = *(HashEntry**)ep2;
512 return e2->finishing_time - e1->finishing_time;
515 static unsigned long step_1, step_2, step_3, step_4, step_5, step_6, step_7, step_8;
516 static int fist_pass_links, second_pass_links, sccs_links;
517 static int max_sccs_links = 0;
520 sgen_bridge_register_finalized_object (MonoObject *obj)
522 g_assert (sgen_need_bridge_processing ());
523 dyn_array_ptr_push (®istered_bridges, obj);
527 sgen_bridge_reset_data (void)
529 registered_bridges.size = 0;
533 sgen_bridge_processing_stw_step (void)
536 SGEN_TV_DECLARE (atv);
537 SGEN_TV_DECLARE (btv);
539 if (!registered_bridges.size)
542 SGEN_TV_GETTIME (btv);
546 dyn_array_ptr_init (&dfs_stack);
547 dyn_array_int_init (&merge_array);
550 for (i = 0; i < registered_bridges.size; ++i)
551 dfs1 (register_bridge_object (DYN_ARRAY_PTR_REF (®istered_bridges, i)), NULL);
553 SGEN_TV_GETTIME (atv);
554 step_2 = SGEN_TV_ELAPSED (btv, atv);
558 sgen_bridge_processing_finish (void)
561 int num_sccs, num_xrefs;
562 int max_entries, max_xrefs;
563 int hash_table_size, sccs_size;
566 int num_registered_bridges;
567 HashEntry **all_entries;
568 MonoGCBridgeSCC **api_sccs;
569 MonoGCBridgeXRef *api_xrefs;
570 SGEN_TV_DECLARE (atv);
571 SGEN_TV_DECLARE (btv);
573 if (!registered_bridges.size)
576 SGEN_TV_GETTIME (atv);
578 /* alloc and fill array of all entries */
580 all_entries = sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
583 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
584 g_assert (entry->finishing_time >= 0);
585 all_entries [j++] = entry;
586 fist_pass_links += entry->srcs.size;
587 } SGEN_HASH_TABLE_FOREACH_END;
588 g_assert (j == hash_table.num_entries);
589 hash_table_size = hash_table.num_entries;
591 /* sort array according to decreasing finishing time */
593 qsort (all_entries, hash_table.num_entries, sizeof (HashEntry*), compare_hash_entries);
595 SGEN_TV_GETTIME (btv);
596 step_3 = SGEN_TV_ELAPSED (atv, btv);
598 /* second DFS pass */
600 dyn_array_init (&sccs, sizeof (SCC));
601 for (i = 0; i < hash_table.num_entries; ++i) {
602 HashEntry *entry = all_entries [i];
603 if (entry->scc_index < 0) {
604 int index = sccs.size;
605 current_scc = dyn_array_add (&sccs);
606 current_scc->index = index;
607 current_scc->num_bridge_entries = 0;
608 current_scc->api_index = -1;
609 dyn_array_int_init (¤t_scc->xrefs);
615 sccs_size = sccs.size;
617 for (i = 0; i < hash_table.num_entries; ++i) {
618 HashEntry *entry = all_entries [i];
619 second_pass_links += entry->srcs.size;
622 SGEN_TV_GETTIME (atv);
623 step_4 = SGEN_TV_ELAPSED (btv, atv);
625 //g_print ("%d sccs\n", sccs.size);
627 dyn_array_uninit (&dfs_stack);
629 /* init data for callback */
632 for (i = 0; i < sccs.size; ++i) {
633 SCC *scc = DYN_ARRAY_REF (&sccs, i);
634 g_assert (scc->index == i);
635 if (scc->num_bridge_entries)
637 sccs_links += scc->xrefs.size;
638 max_sccs_links = MAX (max_sccs_links, scc->xrefs.size);
641 api_sccs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
644 for (i = 0; i < sccs.size; ++i) {
645 SCC *scc = DYN_ARRAY_REF (&sccs, i);
646 if (!scc->num_bridge_entries)
649 api_sccs [j] = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
650 api_sccs [j]->num_objs = scc->num_bridge_entries;
651 scc->num_bridge_entries = 0;
652 scc->api_index = j++;
654 num_xrefs += scc->xrefs.size;
657 SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
658 if (entry->is_bridge) {
659 SCC *scc = DYN_ARRAY_REF (&sccs, entry->scc_index);
660 api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = entry->obj;
662 } SGEN_HASH_TABLE_FOREACH_END;
664 api_xrefs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
666 for (i = 0; i < sccs.size; ++i) {
668 SCC *scc = DYN_ARRAY_REF (&sccs, i);
669 if (!scc->num_bridge_entries)
671 for (k = 0; k < scc->xrefs.size; ++k) {
672 SCC *src_scc = DYN_ARRAY_REF (&sccs, DYN_ARRAY_INT_REF (&scc->xrefs, k));
673 if (!src_scc->num_bridge_entries)
675 api_xrefs [j].src_scc_index = src_scc->api_index;
676 api_xrefs [j].dst_scc_index = scc->api_index;
681 SGEN_TV_GETTIME (btv);
682 step_5 = SGEN_TV_ELAPSED (atv, btv);
687 max_entries = max_xrefs = 0;
688 for (i = 0; i < sccs.size; ++i) {
689 SCC *scc = DYN_ARRAY_REF (&sccs, i);
690 if (scc->num_bridge_entries)
692 if (scc->num_bridge_entries > max_entries)
693 max_entries = scc->num_bridge_entries;
694 if (scc->xrefs.size > max_xrefs)
695 max_xrefs = scc->xrefs.size;
696 dyn_array_uninit (&scc->xrefs);
699 dyn_array_uninit (&sccs);
701 sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
704 /* Empty the registered bridges array */
705 num_registered_bridges = registered_bridges.size;
706 registered_bridges.size = 0;
708 SGEN_TV_GETTIME (atv);
709 step_6 = SGEN_TV_ELAPSED (btv, atv);
711 //g_print ("%d sccs containing bridges - %d max bridge objects - %d max xrefs\n", j, max_entries, max_xrefs);
715 bridge_callbacks.cross_references (num_sccs, api_sccs, num_xrefs, api_xrefs);
717 /*Release for finalization those objects we no longer care. */
718 SGEN_TV_GETTIME (btv);
719 step_7 = SGEN_TV_ELAPSED (atv, btv);
721 for (i = 0; i < num_sccs; ++i) {
722 if (!api_sccs [i]->objs [0])
724 for (j = 0; j < api_sccs [i]->num_objs; ++j)
725 sgen_mark_bridge_object (api_sccs [i]->objs [j]);
728 /* free callback data */
730 for (i = 0; i < num_sccs; ++i) {
731 sgen_free_internal_dynamic (api_sccs [i],
732 sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * api_sccs [i]->num_objs,
733 INTERNAL_MEM_BRIDGE_DATA);
735 sgen_free_internal_dynamic (api_sccs, sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
737 sgen_free_internal_dynamic (api_xrefs, sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
739 SGEN_TV_GETTIME (atv);
740 step_8 = SGEN_TV_ELAPSED (btv, atv);
742 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",
743 num_registered_bridges, hash_table_size, sccs.size,
752 fist_pass_links, second_pass_links, sccs_links, max_sccs_links,
753 dsf1_passes, dsf2_passes);
755 step_1 = 0; /* We must cleanup since this value is used as an accumulator. */
758 static const char *bridge_class;
761 bridge_test_is_bridge_class (MonoClass *class)
763 return !strcmp (bridge_class, class->name);
767 bridge_test_is_bridge_object (MonoObject *object)
773 bridge_test_cross_reference (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
776 for (i = 0; i < num_sccs; ++i) {
778 // g_print ("--- SCC %d\n", i);
779 for (j = 0; j < sccs [i]->num_objs; ++j) {
780 // g_print (" %s\n", sgen_safe_name (sccs [i]->objs [j]));
781 if (i & 1) /*retain half of the bridged objects */
782 sccs [i]->objs [0] = NULL;
785 for (i = 0; i < num_xrefs; ++i) {
786 g_assert (xrefs [i].src_scc_index >= 0 && xrefs [i].src_scc_index < num_sccs);
787 g_assert (xrefs [i].dst_scc_index >= 0 && xrefs [i].dst_scc_index < num_sccs);
788 // g_print ("%d -> %d\n", xrefs [i].src_scc_index, xrefs [i].dst_scc_index);
794 sgen_register_test_bridge_callbacks (const char *bridge_class_name)
796 MonoGCBridgeCallbacks callbacks;
797 callbacks.bridge_version = SGEN_BRIDGE_VERSION;
798 callbacks.is_bridge_class = bridge_test_is_bridge_class;
799 callbacks.is_bridge_object = bridge_test_is_bridge_object;
800 callbacks.cross_references = bridge_test_cross_reference;
801 mono_gc_register_bridge_callbacks (&callbacks);
802 bridge_class = bridge_class_name;