Merge pull request #1068 from esdrubal/bug18421
[mono.git] / mono / metadata / sgen-old-bridge.c
1 /*
2  * sgen-bridge.c: Simple generational GC.
3  *
4  * Copyright 2011 Novell, Inc (http://www.novell.com)
5  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
6  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
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.
15  *
16  *
17  * Copyright 2001-2003 Ximian, Inc
18  * Copyright 2003-2010 Novell, Inc.
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39
40 #include "config.h"
41
42 #ifdef HAVE_SGEN_GC
43
44 #include <stdlib.h>
45
46 #include "sgen-gc.h"
47 #include "sgen-bridge.h"
48 #include "sgen-hash-table.h"
49 #include "sgen-qsort.h"
50 #include "utils/mono-logger-internal.h"
51 #include "utils/mono-time.h"
52 #include "utils/mono-compiler.h"
53
54
55 typedef struct {
56         int size;
57         int capacity;
58         char *data;
59 } DynArray;
60
61 /*Specializations*/
62
63 typedef struct {
64         DynArray array;
65 } DynIntArray;
66
67 typedef struct {
68         DynArray array;
69 } DynPtrArray;
70
71 typedef struct {
72         DynArray array;
73 } DynSCCArray;
74
75
76 /*
77  * FIXME: Optimizations:
78  *
79  * Don't allocate a scrs array for just one source.  Most objects have
80  * just one source, so use the srcs pointer itself.
81  */
82 typedef struct _HashEntry {
83         MonoObject *obj;        /* This is a duplicate - it's already stored in the hash table */
84
85         gboolean is_bridge;
86         gboolean is_visited;
87
88         int finishing_time;
89
90         DynPtrArray srcs;
91
92         int scc_index;
93 } HashEntry;
94
95 typedef struct {
96         HashEntry entry;
97         double weight;
98 } HashEntryWithAccounting;
99
100 typedef struct _SCC {
101         int index;
102         int api_index;
103         int num_bridge_entries;
104         DynIntArray xrefs;              /* these are incoming, not outgoing */
105 } SCC;
106
107 static SgenHashTable hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE, INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntry), mono_aligned_addr_hash, NULL);
108
109 static int current_time;
110
111 static gboolean bridge_accounting_enabled = FALSE;
112
113 static SgenBridgeProcessor *bridge_processor;
114
115 /* Core functions */
116 /* public */
117
118 /* private */
119
120 static void
121 dyn_array_init (DynArray *da)
122 {
123         da->size = 0;
124         da->capacity = 0;
125         da->data = NULL;
126 }
127
128 static void
129 dyn_array_uninit (DynArray *da, int elem_size)
130 {
131         if (da->capacity <= 0)
132                 return;
133
134         sgen_free_internal_dynamic (da->data, elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
135         da->data = NULL;
136 }
137
138 static void
139 dyn_array_ensure_capacity (DynArray *da, int capacity, int elem_size)
140 {
141         int old_capacity = da->capacity;
142         char *new_data;
143
144         if (capacity <= old_capacity)
145                 return;
146
147         if (da->capacity == 0)
148                 da->capacity = 2;
149         while (capacity > da->capacity)
150                 da->capacity *= 2;
151
152         new_data = sgen_alloc_internal_dynamic (elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
153         memcpy (new_data, da->data, elem_size * da->size);
154         sgen_free_internal_dynamic (da->data, elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
155         da->data = new_data;
156 }
157
158 static void*
159 dyn_array_add (DynArray *da, int elem_size)
160 {
161         void *p;
162
163         dyn_array_ensure_capacity (da, da->size + 1, elem_size);
164
165         p = da->data + da->size * elem_size;
166         ++da->size;
167         return p;
168 }
169
170 /* int */
171 static void
172 dyn_array_int_init (DynIntArray *da)
173 {
174         dyn_array_init (&da->array);
175 }
176
177 static void
178 dyn_array_int_uninit (DynIntArray *da)
179 {
180         dyn_array_uninit (&da->array, sizeof (int));
181 }
182
183 static int
184 dyn_array_int_size (DynIntArray *da)
185 {
186         return da->array.size;
187 }
188
189 static void
190 dyn_array_int_set_size (DynIntArray *da, int size)
191 {
192         da->array.size = size;
193 }
194
195 static void
196 dyn_array_int_add (DynIntArray *da, int x)
197 {
198         int *p = dyn_array_add (&da->array, sizeof (int));
199         *p = x;
200 }
201
202 static int
203 dyn_array_int_get (DynIntArray *da, int x)
204 {
205         return ((int*)da->array.data)[x];
206 }
207
208 static void
209 dyn_array_int_set (DynIntArray *da, int idx, int val)
210 {
211         ((int*)da->array.data)[idx] = val;
212 }
213
214 static void
215 dyn_array_int_ensure_capacity (DynIntArray *da, int capacity)
216 {
217         dyn_array_ensure_capacity (&da->array, capacity, sizeof (int));
218 }
219
220 static void
221 dyn_array_int_set_all (DynIntArray *dst, DynIntArray *src)
222 {
223         dyn_array_int_ensure_capacity (dst, src->array.size);
224         memcpy (dst->array.data, src->array.data, src->array.size * sizeof (int));
225         dst->array.size = src->array.size;
226 }
227
228 /* ptr */
229
230 static void
231 dyn_array_ptr_init (DynPtrArray *da)
232 {
233         dyn_array_init (&da->array);
234 }
235
236 static void
237 dyn_array_ptr_uninit (DynPtrArray *da)
238 {
239         dyn_array_uninit (&da->array, sizeof (void*));
240 }
241
242 static int
243 dyn_array_ptr_size (DynPtrArray *da)
244 {
245         return da->array.size;
246 }
247
248 static void
249 dyn_array_ptr_set_size (DynPtrArray *da, int size)
250 {
251         da->array.size = size;
252 }
253
254 static void*
255 dyn_array_ptr_get (DynPtrArray *da, int x)
256 {
257         return ((void**)da->array.data)[x];
258 }
259
260 static void
261 dyn_array_ptr_add (DynPtrArray *da, void *ptr)
262 {
263         void **p = dyn_array_add (&da->array, sizeof (void*));
264         *p = ptr;
265 }
266
267 #define dyn_array_ptr_push dyn_array_ptr_add
268
269 static void*
270 dyn_array_ptr_pop (DynPtrArray *da)
271 {
272         void *p;
273         int size = da->array.size;
274         g_assert (size > 0);
275         p = dyn_array_ptr_get (da, size - 1);
276         --da->array.size;
277         return p;
278 }
279
280 /*SCC */
281
282 static void
283 dyn_array_scc_init (DynSCCArray *da)
284 {
285         dyn_array_init (&da->array);
286 }
287
288 static void
289 dyn_array_scc_uninit (DynSCCArray *da)
290 {
291         dyn_array_uninit (&da->array, sizeof (SCC));
292 }
293
294 static int
295 dyn_array_scc_size (DynSCCArray *da)
296 {
297         return da->array.size;
298 }
299
300 static SCC*
301 dyn_array_scc_add (DynSCCArray *da)
302 {
303         return dyn_array_add (&da->array, sizeof (SCC));
304 }
305
306 static SCC*
307 dyn_array_scc_get_ptr (DynSCCArray *da, int x)
308 {
309         return &((SCC*)da->array.data)[x];
310 }
311
312 /* Merge code*/
313
314 static DynIntArray merge_array;
315
316 static gboolean
317 dyn_array_int_contains (DynIntArray *da, int x)
318 {
319         int i;
320         for (i = 0; i < dyn_array_int_size (da); ++i)
321                 if (dyn_array_int_get (da, i) == x)
322                         return TRUE;
323         return FALSE;
324 }
325
326
327 static void
328 dyn_array_int_merge (DynIntArray *dst, DynIntArray *src)
329 {
330         int i, j;
331
332         dyn_array_int_ensure_capacity (&merge_array, dyn_array_int_size (dst) + dyn_array_int_size (src));
333         dyn_array_int_set_size (&merge_array, 0);
334
335         for (i = j = 0; i < dyn_array_int_size (dst) || j < dyn_array_int_size (src); ) {
336                 if (i < dyn_array_int_size (dst) && j < dyn_array_int_size (src)) {
337                         int a = dyn_array_int_get (dst, i); 
338                         int b = dyn_array_int_get (src, j); 
339                         if (a < b) {
340                                 dyn_array_int_add (&merge_array, a);
341                                 ++i;
342                         } else if (a == b) {
343                                 dyn_array_int_add (&merge_array, a);
344                                 ++i;
345                                 ++j;    
346                         } else {
347                                 dyn_array_int_add (&merge_array, b);
348                                 ++j;
349                         }
350                 } else if (i < dyn_array_int_size (dst)) {
351                         dyn_array_int_add (&merge_array, dyn_array_int_get (dst, i));
352                         ++i;
353                 } else {
354                         dyn_array_int_add (&merge_array, dyn_array_int_get (src, j));
355                         ++j;
356                 }
357         }
358
359         if (dyn_array_int_size (&merge_array) > dyn_array_int_size (dst)) {
360                 dyn_array_int_set_all (dst, &merge_array);
361         }
362 }
363
364 static void
365 dyn_array_int_merge_one (DynIntArray *array, int value)
366 {
367         int i;
368         int tmp;
369         int size = dyn_array_int_size (array);
370
371         for (i = 0; i < size; ++i) {
372                 if (dyn_array_int_get (array, i) == value)
373                         return;
374                 else if (dyn_array_int_get (array, i) > value)
375                         break;
376         }
377
378         dyn_array_int_ensure_capacity (array, size + 1);
379
380         if (i < size) {
381                 tmp = dyn_array_int_get (array, i);
382                 for (; i < size; ++i) {
383                         dyn_array_int_set (array, i, value);
384                         value = tmp;
385                         tmp = dyn_array_int_get (array, i + 1);
386                 }
387                 dyn_array_int_set (array, size, value);
388         } else {
389                 dyn_array_int_set (array, size, value);
390         }
391
392         dyn_array_int_set_size (array, size + 1);
393 }
394
395
396 static void
397 enable_accounting (void)
398 {
399         SgenHashTable table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntryWithAccounting), mono_aligned_addr_hash, NULL);
400         bridge_accounting_enabled = TRUE;
401         hash_table = table;
402 }
403
404 static MonoGCBridgeObjectKind
405 class_kind (MonoClass *class)
406 {
407         return bridge_callbacks.bridge_class_kind (class);
408 }
409
410 static HashEntry*
411 get_hash_entry (MonoObject *obj, gboolean *existing)
412 {
413         HashEntry *entry = sgen_hash_table_lookup (&hash_table, obj);
414         HashEntry new_entry;
415
416         if (entry) {
417                 if (existing)
418                         *existing = TRUE;
419                 return entry;
420         }
421         if (existing)
422                 *existing = FALSE;
423
424         memset (&new_entry, 0, sizeof (HashEntry));
425
426         new_entry.obj = obj;
427         dyn_array_ptr_init (&new_entry.srcs);
428         new_entry.finishing_time = -1;
429         new_entry.scc_index = -1;
430
431         sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
432
433         return sgen_hash_table_lookup (&hash_table, obj);
434 }
435
436 static void
437 add_source (HashEntry *entry, HashEntry *src)
438 {
439         dyn_array_ptr_add (&entry->srcs, src);
440 }
441
442 static void
443 free_data (void)
444 {
445         MonoObject *obj;
446         HashEntry *entry;
447         int total_srcs = 0;
448         int max_srcs = 0;
449
450         SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
451                 int entry_size = dyn_array_ptr_size (&entry->srcs);
452                 total_srcs += entry_size;
453                 if (entry_size > max_srcs)
454                         max_srcs = entry_size;
455                 dyn_array_ptr_uninit (&entry->srcs);
456         } SGEN_HASH_TABLE_FOREACH_END;
457
458         sgen_hash_table_clean (&hash_table);
459
460         dyn_array_int_uninit (&merge_array);
461         //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
462 }
463
464 static HashEntry*
465 register_bridge_object (MonoObject *obj)
466 {
467         HashEntry *entry = get_hash_entry (obj, NULL);
468         entry->is_bridge = TRUE;
469         return entry;
470 }
471
472 static void
473 register_finishing_time (HashEntry *entry, int t)
474 {
475         g_assert (entry->finishing_time < 0);
476         entry->finishing_time = t;
477 }
478
479 static gboolean
480 object_is_live (MonoObject **objp)
481 {
482         MonoObject *obj = *objp;
483         MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
484         if (fwd) {
485                 *objp = fwd;
486                 return sgen_hash_table_lookup (&hash_table, fwd) == NULL;
487         }
488         if (!sgen_object_is_live (obj))
489                 return FALSE;
490         return sgen_hash_table_lookup (&hash_table, obj) == NULL;
491 }
492
493 static DynPtrArray registered_bridges;
494 static DynPtrArray dfs_stack;
495
496 static int dfs1_passes, dfs2_passes;
497
498
499 #undef HANDLE_PTR
500 #define HANDLE_PTR(ptr,obj)     do {                                    \
501                 MonoObject *dst = (MonoObject*)*(ptr);                  \
502                 if (dst && !object_is_live (&dst)) {                    \
503                         dyn_array_ptr_push (&dfs_stack, obj_entry);     \
504                         dyn_array_ptr_push (&dfs_stack, get_hash_entry (dst, NULL)); \
505                 }                                                       \
506         } while (0)
507
508 static void
509 dfs1 (HashEntry *obj_entry)
510 {
511         HashEntry *src;
512         g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
513
514         dyn_array_ptr_push (&dfs_stack, NULL);
515         dyn_array_ptr_push (&dfs_stack, obj_entry);
516
517         do {
518                 MonoObject *obj;
519                 char *start;
520                 ++dfs1_passes;
521
522                 obj_entry = dyn_array_ptr_pop (&dfs_stack);
523                 if (obj_entry) {
524                         src = dyn_array_ptr_pop (&dfs_stack);
525
526                         obj = obj_entry->obj;
527                         start = (char*)obj;
528
529                         if (src) {
530                                 //g_print ("link %s -> %s\n", sgen_safe_name (src->obj), sgen_safe_name (obj));
531                                 add_source (obj_entry, src);
532                         } else {
533                                 //g_print ("starting with %s\n", sgen_safe_name (obj));
534                         }
535
536                         if (obj_entry->is_visited)
537                                 continue;
538
539                         obj_entry->is_visited = TRUE;
540
541                         dyn_array_ptr_push (&dfs_stack, obj_entry);
542                         /* NULL marks that the next entry is to be finished */
543                         dyn_array_ptr_push (&dfs_stack, NULL);
544
545 #include "sgen-scan-object.h"
546                 } else {
547                         obj_entry = dyn_array_ptr_pop (&dfs_stack);
548
549                         //g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
550                         register_finishing_time (obj_entry, current_time++);
551                 }
552         } while (dyn_array_ptr_size (&dfs_stack) > 0);
553 }
554
555 static void
556 scc_add_xref (SCC *src, SCC *dst)
557 {
558         g_assert (src != dst);
559         g_assert (src->index != dst->index);
560
561         if (dyn_array_int_contains (&dst->xrefs, src->index))
562                 return;
563         if (src->num_bridge_entries) {
564                 dyn_array_int_merge_one (&dst->xrefs, src->index);
565         } else {
566                 int i;
567                 dyn_array_int_merge (&dst->xrefs, &src->xrefs);
568                 for (i = 0; i < dyn_array_int_size (&dst->xrefs); ++i)
569                         g_assert (dyn_array_int_get (&dst->xrefs, i) != dst->index);
570         }
571 }
572
573 static void
574 scc_add_entry (SCC *scc, HashEntry *entry)
575 {
576         g_assert (entry->scc_index < 0);
577         entry->scc_index = scc->index;
578         if (entry->is_bridge)
579                 ++scc->num_bridge_entries;
580 }
581
582 static DynSCCArray sccs;
583 static SCC *current_scc;
584
585 static void
586 dfs2 (HashEntry *entry)
587 {
588         int i;
589
590         g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
591
592         dyn_array_ptr_push (&dfs_stack, entry);
593
594         do {
595                 entry = dyn_array_ptr_pop (&dfs_stack);
596                 ++dfs2_passes;
597
598                 if (entry->scc_index >= 0) {
599                         if (entry->scc_index != current_scc->index)
600                                 scc_add_xref (dyn_array_scc_get_ptr (&sccs, entry->scc_index), current_scc);
601                         continue;
602                 }
603
604                 scc_add_entry (current_scc, entry);
605
606                 for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i)
607                         dyn_array_ptr_push (&dfs_stack, dyn_array_ptr_get (&entry->srcs, i));
608         } while (dyn_array_ptr_size (&dfs_stack) > 0);
609 }
610
611 static int
612 compare_hash_entries (const HashEntry *e1, const HashEntry *e2)
613 {
614         return e2->finishing_time - e1->finishing_time;
615 }
616
617 DEF_QSORT_INLINE(hash_entries, HashEntry*, compare_hash_entries)
618
619 static unsigned long step_1, step_2, step_3, step_4, step_5, step_6;
620 static int fist_pass_links, second_pass_links, sccs_links;
621 static int max_sccs_links = 0;
622
623 static void
624 register_finalized_object (MonoObject *obj)
625 {
626         g_assert (sgen_need_bridge_processing ());
627         dyn_array_ptr_push (&registered_bridges, obj);
628 }
629
630 static void
631 reset_data (void)
632 {
633         dyn_array_ptr_set_size (&registered_bridges, 0);
634 }
635
636 static void
637 processing_stw_step (void)
638 {
639         int i;
640         int bridge_count;
641         SGEN_TV_DECLARE (atv);
642         SGEN_TV_DECLARE (btv);
643
644         if (!dyn_array_ptr_size (&registered_bridges))
645                 return;
646
647         SGEN_TV_GETTIME (btv);
648
649         /* first DFS pass */
650
651         dyn_array_ptr_init (&dfs_stack);
652         dyn_array_int_init (&merge_array);
653
654         current_time = 0;
655         /*
656         First we insert all bridges into the hash table and then we do dfs1.
657
658         It must be done in 2 steps since the bridge arrays doesn't come in reverse topological order,
659         which means that we can have entry N pointing to entry N + 1.
660
661         If we dfs1 entry N before N + 1 is registered we'll not consider N + 1 for this bridge
662         pass and not create the required xref between the two.
663         */
664         bridge_count = dyn_array_ptr_size (&registered_bridges);
665         for (i = 0; i < bridge_count ; ++i)
666                 register_bridge_object (dyn_array_ptr_get (&registered_bridges, i));
667
668         for (i = 0; i < bridge_count; ++i)
669                 dfs1 (get_hash_entry (dyn_array_ptr_get (&registered_bridges, i), NULL));
670
671         SGEN_TV_GETTIME (atv);
672         step_2 = SGEN_TV_ELAPSED (btv, atv);
673 }
674
675 static int num_registered_bridges, hash_table_size;
676
677 static void
678 processing_build_callback_data (int generation)
679 {
680         int i, j;
681         int num_sccs, num_xrefs;
682         int max_entries, max_xrefs;
683         int sccs_size;
684         MonoObject *obj;
685         HashEntry *entry;
686         HashEntry **all_entries;
687         MonoGCBridgeSCC **api_sccs;
688         MonoGCBridgeXRef *api_xrefs;
689         SGEN_TV_DECLARE (atv);
690         SGEN_TV_DECLARE (btv);
691
692         g_assert (bridge_processor->num_sccs == 0 && bridge_processor->num_xrefs == 0);
693         g_assert (!bridge_processor->api_sccs && !bridge_processor->api_xrefs);
694
695         if (!dyn_array_ptr_size (&registered_bridges))
696                 return;
697
698         g_assert (bridge_processing_in_progress);
699
700         SGEN_TV_GETTIME (atv);
701
702         /* alloc and fill array of all entries */
703
704         all_entries = sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
705
706         j = 0;
707         SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
708                 g_assert (entry->finishing_time >= 0);
709                 all_entries [j++] = entry;
710                 fist_pass_links += dyn_array_ptr_size (&entry->srcs);
711         } SGEN_HASH_TABLE_FOREACH_END;
712         g_assert (j == hash_table.num_entries);
713         hash_table_size = hash_table.num_entries;
714
715         /* sort array according to decreasing finishing time */
716         qsort_hash_entries (all_entries, hash_table.num_entries);
717
718         SGEN_TV_GETTIME (btv);
719         step_3 = SGEN_TV_ELAPSED (atv, btv);
720
721         /* second DFS pass */
722
723         dyn_array_scc_init (&sccs);
724         for (i = 0; i < hash_table.num_entries; ++i) {
725                 HashEntry *entry = all_entries [i];
726                 if (entry->scc_index < 0) {
727                         int index = dyn_array_scc_size (&sccs);
728                         current_scc = dyn_array_scc_add (&sccs);
729                         current_scc->index = index;
730                         current_scc->num_bridge_entries = 0;
731                         current_scc->api_index = -1;
732                         dyn_array_int_init (&current_scc->xrefs);
733
734                         dfs2 (entry);
735                 }
736         }
737
738         /*
739          * Compute the weight of each object. The weight of an object is its size plus the size of all
740          * objects it points do. When the an object is pointed by multiple objects we distribute it's weight
741          * equally among them. This distribution gives a rough estimate of the real impact of making the object
742          * go away.
743          *
744          * The reasoning for this model is that complex graphs with single roots will have a bridge with very high
745          * value in comparison to others.
746          *
747          * The all_entries array has all objects topologically sorted. To correctly propagate the weights it must be
748          * done in reverse topological order - so we calculate the weight of the pointed-to objects before processing
749          * pointer-from objects.
750          *
751          * We log those objects in the opposite order for no particular reason. The other constrain is that it should use the same
752          * direction as the other logging loop that records live/dead information.
753          */
754         if (bridge_accounting_enabled) {
755                 for (i = hash_table.num_entries - 1; i >= 0; --i) {
756                         double w;
757                         HashEntryWithAccounting *entry = (HashEntryWithAccounting*)all_entries [i];
758
759                         entry->weight += (double)sgen_safe_object_get_size (entry->entry.obj);
760                         w = entry->weight / dyn_array_ptr_size (&entry->entry.srcs);
761                         for (j = 0; j < dyn_array_ptr_size (&entry->entry.srcs); ++j) {
762                                 HashEntryWithAccounting *other = (HashEntryWithAccounting *)dyn_array_ptr_get (&entry->entry.srcs, j);
763                                 other->weight += w;
764                         }
765                 }
766                 for (i = 0; i < hash_table.num_entries; ++i) {
767                         HashEntryWithAccounting *entry = (HashEntryWithAccounting*)all_entries [i];
768                         if (entry->entry.is_bridge) {
769                                 MonoClass *klass = ((MonoVTable*)SGEN_LOAD_VTABLE (entry->entry.obj))->klass;
770                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "OBJECT %s::%s (%p) weight %f", klass->name_space, klass->name, entry->entry.obj, entry->weight);
771                         }
772                 }
773         }
774
775         sccs_size = dyn_array_scc_size (&sccs);
776
777         for (i = 0; i < hash_table.num_entries; ++i) {
778                 HashEntry *entry = all_entries [i];
779                 second_pass_links += dyn_array_ptr_size (&entry->srcs);
780         }
781
782         SGEN_TV_GETTIME (atv);
783         step_4 = SGEN_TV_ELAPSED (btv, atv);
784
785         //g_print ("%d sccs\n", sccs.size);
786
787         dyn_array_ptr_uninit (&dfs_stack);
788
789         /* init data for callback */
790
791         num_sccs = 0;
792         for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
793                 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
794                 g_assert (scc->index == i);
795                 if (scc->num_bridge_entries)
796                         ++num_sccs;
797                 sccs_links += dyn_array_int_size (&scc->xrefs);
798                 max_sccs_links = MAX (max_sccs_links, dyn_array_int_size (&scc->xrefs));
799         }
800
801         api_sccs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
802         num_xrefs = 0;
803         j = 0;
804         for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
805                 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
806                 if (!scc->num_bridge_entries)
807                         continue;
808
809                 api_sccs [j] = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
810                 api_sccs [j]->is_alive = FALSE;
811                 api_sccs [j]->num_objs = scc->num_bridge_entries;
812                 scc->num_bridge_entries = 0;
813                 scc->api_index = j++;
814
815                 num_xrefs += dyn_array_int_size (&scc->xrefs);
816         }
817
818         SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
819                 if (entry->is_bridge) {
820                         SCC *scc = dyn_array_scc_get_ptr (&sccs, entry->scc_index);
821                         api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = entry->obj;
822                 }
823         } SGEN_HASH_TABLE_FOREACH_END;
824
825         api_xrefs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
826         j = 0;
827         for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
828                 int k;
829                 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
830                 if (!scc->num_bridge_entries)
831                         continue;
832                 for (k = 0; k < dyn_array_int_size (&scc->xrefs); ++k) {
833                         SCC *src_scc = dyn_array_scc_get_ptr (&sccs, dyn_array_int_get (&scc->xrefs, k));
834                         if (!src_scc->num_bridge_entries)
835                                 continue;
836                         api_xrefs [j].src_scc_index = src_scc->api_index;
837                         api_xrefs [j].dst_scc_index = scc->api_index;
838                         ++j;
839                 }
840         }
841
842         SGEN_TV_GETTIME (btv);
843         step_5 = SGEN_TV_ELAPSED (atv, btv);
844
845         /* free data */
846
847         j = 0;
848         max_entries = max_xrefs = 0;
849         for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
850                 SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
851                 if (scc->num_bridge_entries)
852                         ++j;
853                 if (scc->num_bridge_entries > max_entries)
854                         max_entries = scc->num_bridge_entries;
855                 if (dyn_array_int_size (&scc->xrefs) > max_xrefs)
856                         max_xrefs = dyn_array_int_size (&scc->xrefs);
857                 dyn_array_int_uninit (&scc->xrefs);
858
859         }
860         dyn_array_scc_uninit (&sccs);
861
862         sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
863
864         free_data ();
865         /* Empty the registered bridges array */
866         num_registered_bridges = dyn_array_ptr_size (&registered_bridges);
867         dyn_array_ptr_set_size (&registered_bridges, 0);
868
869         SGEN_TV_GETTIME (atv);
870         step_6 = SGEN_TV_ELAPSED (btv, atv);
871
872         //g_print ("%d sccs containing bridges - %d max bridge objects - %d max xrefs\n", j, max_entries, max_xrefs);
873
874         bridge_processor->num_sccs = num_sccs;
875         bridge_processor->api_sccs = api_sccs;
876         bridge_processor->num_xrefs = num_xrefs;
877         bridge_processor->api_xrefs = api_xrefs;
878 }
879
880 static void
881 processing_after_callback (int generation)
882 {
883         int i, j;
884         int num_sccs = bridge_processor->num_sccs;
885         MonoGCBridgeSCC **api_sccs = bridge_processor->api_sccs;
886
887         if (bridge_accounting_enabled) {
888                 for (i = 0; i < num_sccs; ++i) {
889                         for (j = 0; j < api_sccs [i]->num_objs; ++j)
890                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC,
891                                         "OBJECT %s (%p) SCC [%d] %s",
892                                                 sgen_safe_name (api_sccs [i]->objs [j]), api_sccs [i]->objs [j],
893                                                 i,
894                                                 api_sccs [i]->is_alive  ? "ALIVE" : "DEAD");
895                 }
896         }
897
898         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_OLD_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",
899                 num_registered_bridges, hash_table_size, dyn_array_scc_size (&sccs),
900                 step_1 / 10000.0f,
901                 step_2 / 10000.0f,
902                 step_3 / 10000.0f,
903                 step_4 / 10000.0f,
904                 step_5 / 10000.0f,
905                 step_6 / 10000.0f,
906                 fist_pass_links, second_pass_links, sccs_links, max_sccs_links,
907                 dfs1_passes, dfs2_passes);
908
909         step_1 = 0; /* We must cleanup since this value is used as an accumulator. */
910         fist_pass_links = second_pass_links = sccs_links = max_sccs_links = 0;
911         dfs1_passes = dfs2_passes = 0;
912 }
913
914 static void
915 describe_pointer (MonoObject *obj)
916 {
917         HashEntry *entry;
918         int i;
919
920         for (i = 0; i < dyn_array_ptr_size (&registered_bridges); ++i) {
921                 if (obj == dyn_array_ptr_get (&registered_bridges, i)) {
922                         printf ("Pointer is a registered bridge object.\n");
923                         break;
924                 }
925         }
926
927         entry = sgen_hash_table_lookup (&hash_table, obj);
928         if (!entry)
929                 return;
930
931         printf ("Bridge hash table entry %p:\n", entry);
932         printf ("  is bridge: %d\n", (int)entry->is_bridge);
933         printf ("  is visited: %d\n", (int)entry->is_visited);
934 }
935
936 void
937 sgen_old_bridge_init (SgenBridgeProcessor *collector)
938 {
939         collector->reset_data = reset_data;
940         collector->processing_stw_step = processing_stw_step;
941         collector->processing_build_callback_data = processing_build_callback_data;
942         collector->processing_after_callback = processing_after_callback;
943         collector->class_kind = class_kind;
944         collector->register_finalized_object = register_finalized_object;
945         collector->describe_pointer = describe_pointer;
946         collector->enable_accounting = enable_accounting;
947
948         bridge_processor = collector;
949 }
950
951 #endif