/
sgen-new-bridge.c
1364 lines (1149 loc) · 35.6 KB
/
sgen-new-bridge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sgen-bridge.c: Simple generational GC.
*
* Copyright 2011 Novell, Inc (http://www.novell.com)
* Copyright 2011 Xamarin Inc (http://www.xamarin.com)
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program
* for any purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*
* Copyright 2001-2003 Ximian, Inc
* Copyright 2003-2010 Novell, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#ifdef HAVE_SGEN_GC
#include <stdlib.h>
#include <errno.h>
#include "sgen-gc.h"
#include "sgen-bridge.h"
#include "sgen-hash-table.h"
#include "sgen-qsort.h"
#include "tabledefs.h"
#include "utils/mono-logger-internal.h"
#include "utils/mono-time.h"
#include "utils/mono-compiler.h"
//#define NEW_XREFS
#ifdef NEW_XREFS
//#define TEST_NEW_XREFS
#endif
#if !defined(NEW_XREFS) || defined(TEST_NEW_XREFS)
#define OLD_XREFS
#endif
#ifdef NEW_XREFS
#define XREFS new_xrefs
#else
#define XREFS old_xrefs
#endif
typedef struct {
int size;
int capacity; /* if negative, data points to another DynArray's data */
char *data;
} DynArray;
/*Specializations*/
typedef struct {
DynArray array;
} DynIntArray;
typedef struct {
DynArray array;
} DynPtrArray;
typedef struct {
DynArray array;
} DynSCCArray;
/*
* FIXME: Optimizations:
*
* Don't allocate a scrs array for just one source. Most objects have
* just one source, so use the srcs pointer itself.
*/
typedef struct _HashEntry {
gboolean is_bridge;
union {
struct {
guint32 is_visited : 1;
guint32 finishing_time : 31;
struct _HashEntry *forwarded_to;
} dfs1;
struct {
int scc_index;
} dfs2;
} v;
DynPtrArray srcs;
} HashEntry;
typedef struct {
HashEntry entry;
double weight;
} HashEntryWithAccounting;
typedef struct _SCC {
int index;
int api_index;
int num_bridge_entries;
gboolean flag;
/*
* New and old xrefs are typically mutually exclusive. Only when TEST_NEW_XREFS is
* enabled we do both, and compare the results. This should only be done for
* debugging, obviously.
*/
#ifdef OLD_XREFS
DynIntArray old_xrefs; /* these are incoming, not outgoing */
#endif
#ifdef NEW_XREFS
DynIntArray new_xrefs;
#endif
} SCC;
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 guint32 current_time;
static gboolean bridge_accounting_enabled = FALSE;
static SgenBridgeProcessor *bridge_processor;
/* Core functions */
/* public */
/* private */
static void
dyn_array_init (DynArray *da)
{
da->size = 0;
da->capacity = 0;
da->data = NULL;
}
static void
dyn_array_uninit (DynArray *da, int elem_size)
{
if (da->capacity < 0) {
dyn_array_init (da);
return;
}
if (da->capacity == 0)
return;
sgen_free_internal_dynamic (da->data, elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA);
da->data = NULL;
}
static void
dyn_array_empty (DynArray *da)
{
if (da->capacity < 0)
dyn_array_init (da);
else
da->size = 0;
}
static void
dyn_array_ensure_capacity (DynArray *da, int capacity, int elem_size)
{
int old_capacity = da->capacity;
char *new_data;
g_assert (capacity > 0);
if (capacity <= old_capacity)
return;
if (old_capacity <= 0)
da->capacity = 2;
while (capacity > da->capacity)
da->capacity *= 2;
new_data = sgen_alloc_internal_dynamic (elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
memcpy (new_data, da->data, elem_size * da->size);
if (old_capacity > 0)
sgen_free_internal_dynamic (da->data, elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
da->data = new_data;
}
static gboolean
dyn_array_is_copy (DynArray *da)
{
return da->capacity < 0;
}
static void
dyn_array_ensure_independent (DynArray *da, int elem_size)
{
if (!dyn_array_is_copy (da))
return;
dyn_array_ensure_capacity (da, da->size, elem_size);
g_assert (da->capacity > 0);
}
static void*
dyn_array_add (DynArray *da, int elem_size)
{
void *p;
dyn_array_ensure_capacity (da, da->size + 1, elem_size);
p = da->data + da->size * elem_size;
++da->size;
return p;
}
static void
dyn_array_copy (DynArray *dst, DynArray *src, int elem_size)
{
dyn_array_uninit (dst, elem_size);
if (src->size == 0)
return;
dst->size = src->size;
dst->capacity = -1;
dst->data = src->data;
}
/* int */
static void
dyn_array_int_init (DynIntArray *da)
{
dyn_array_init (&da->array);
}
static void
dyn_array_int_uninit (DynIntArray *da)
{
dyn_array_uninit (&da->array, sizeof (int));
}
static int
dyn_array_int_size (DynIntArray *da)
{
return da->array.size;
}
static void
dyn_array_int_empty (DynIntArray *da)
{
dyn_array_empty (&da->array);
}
static void
dyn_array_int_add (DynIntArray *da, int x)
{
int *p = dyn_array_add (&da->array, sizeof (int));
*p = x;
}
static int
dyn_array_int_get (DynIntArray *da, int x)
{
return ((int*)da->array.data)[x];
}
static void
dyn_array_int_set (DynIntArray *da, int idx, int val)
{
((int*)da->array.data)[idx] = val;
}
static void
dyn_array_int_ensure_capacity (DynIntArray *da, int capacity)
{
dyn_array_ensure_capacity (&da->array, capacity, sizeof (int));
}
static void
dyn_array_int_ensure_independent (DynIntArray *da)
{
dyn_array_ensure_independent (&da->array, sizeof (int));
}
static void
dyn_array_int_copy (DynIntArray *dst, DynIntArray *src)
{
dyn_array_copy (&dst->array, &src->array, sizeof (int));
}
static gboolean
dyn_array_int_is_copy (DynIntArray *da)
{
return dyn_array_is_copy (&da->array);
}
/* ptr */
static void
dyn_array_ptr_init (DynPtrArray *da)
{
dyn_array_init (&da->array);
}
static void
dyn_array_ptr_uninit (DynPtrArray *da)
{
if (da->array.capacity == 1)
dyn_array_ptr_init (da);
else
dyn_array_uninit (&da->array, sizeof (void*));
}
static int
dyn_array_ptr_size (DynPtrArray *da)
{
return da->array.size;
}
static void
dyn_array_ptr_empty (DynPtrArray *da)
{
dyn_array_empty (&da->array);
}
static void*
dyn_array_ptr_get (DynPtrArray *da, int x)
{
if (da->array.capacity == 1) {
g_assert (x == 0);
return da->array.data;
}
return ((void**)da->array.data)[x];
}
static void
dyn_array_ptr_add (DynPtrArray *da, void *ptr)
{
void **p;
if (da->array.capacity == 0) {
da->array.capacity = 1;
da->array.size = 1;
p = (void**)&da->array.data;
} else if (da->array.capacity == 1) {
void *ptr0 = da->array.data;
void **p0;
dyn_array_init (&da->array);
p0 = dyn_array_add (&da->array, sizeof (void*));
*p0 = ptr0;
p = dyn_array_add (&da->array, sizeof (void*));
} else {
p = dyn_array_add (&da->array, sizeof (void*));
}
*p = ptr;
}
#define dyn_array_ptr_push dyn_array_ptr_add
static void*
dyn_array_ptr_pop (DynPtrArray *da)
{
int size = da->array.size;
void *p;
g_assert (size > 0);
if (da->array.capacity == 1) {
p = dyn_array_ptr_get (da, 0);
dyn_array_init (&da->array);
} else {
g_assert (da->array.capacity > 1);
dyn_array_ensure_independent (&da->array, sizeof (void*));
p = dyn_array_ptr_get (da, size - 1);
--da->array.size;
}
return p;
}
/*SCC */
static void
dyn_array_scc_init (DynSCCArray *da)
{
dyn_array_init (&da->array);
}
static void
dyn_array_scc_uninit (DynSCCArray *da)
{
dyn_array_uninit (&da->array, sizeof (SCC));
}
static int
dyn_array_scc_size (DynSCCArray *da)
{
return da->array.size;
}
static SCC*
dyn_array_scc_add (DynSCCArray *da)
{
return dyn_array_add (&da->array, sizeof (SCC));
}
static SCC*
dyn_array_scc_get_ptr (DynSCCArray *da, int x)
{
return &((SCC*)da->array.data)[x];
}
/* Merge code*/
static DynIntArray merge_array;
static gboolean
dyn_array_int_contains (DynIntArray *da, int x)
{
int i;
for (i = 0; i < dyn_array_int_size (da); ++i)
if (dyn_array_int_get (da, i) == x)
return TRUE;
return FALSE;
}
static void
enable_accounting (void)
{
bridge_accounting_enabled = TRUE;
hash_table = (SgenHashTable)SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntryWithAccounting), mono_aligned_addr_hash, NULL);
}
static MonoGCBridgeObjectKind
class_kind (MonoClass *class)
{
MonoGCBridgeObjectKind res = bridge_callbacks.bridge_class_kind (class);
/* If it's a bridge, nothing we can do about it. */
if (res == GC_BRIDGE_TRANSPARENT_BRIDGE_CLASS || res == GC_BRIDGE_OPAQUE_BRIDGE_CLASS)
return res;
/* Non bridge classes with no pointers will never point to a bridge, so we can savely ignore them. */
if (!class->has_references) {
SGEN_LOG (6, "class %s is opaque\n", class->name);
return GC_BRIDGE_OPAQUE_CLASS;
}
/* Some arrays can be ignored */
if (class->rank == 1) {
MonoClass *elem_class = class->element_class;
/* FIXME the bridge check can be quite expensive, cache it at the class level. */
/* An array of a sealed type that is not a bridge will never get to a bridge */
if ((elem_class->flags & TYPE_ATTRIBUTE_SEALED) && !elem_class->has_references && !bridge_callbacks.bridge_class_kind (elem_class)) {
SGEN_LOG (6, "class %s is opaque\n", class->name);
return GC_BRIDGE_OPAQUE_CLASS;
}
}
return GC_BRIDGE_TRANSPARENT_CLASS;
}
static HashEntry*
get_hash_entry (MonoObject *obj, gboolean *existing)
{
HashEntry *entry = sgen_hash_table_lookup (&hash_table, obj);
HashEntry new_entry;
if (entry) {
if (existing)
*existing = TRUE;
return entry;
}
if (existing)
*existing = FALSE;
memset (&new_entry, 0, sizeof (HashEntry));
dyn_array_ptr_init (&new_entry.srcs);
new_entry.v.dfs1.finishing_time = 0;
sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
return sgen_hash_table_lookup (&hash_table, obj);
}
static void
add_source (HashEntry *entry, HashEntry *src)
{
dyn_array_ptr_add (&entry->srcs, src);
}
static void
free_data (void)
{
MonoObject *obj;
HashEntry *entry;
int total_srcs = 0;
int max_srcs = 0;
SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
int entry_size = dyn_array_ptr_size (&entry->srcs);
total_srcs += entry_size;
if (entry_size > max_srcs)
max_srcs = entry_size;
dyn_array_ptr_uninit (&entry->srcs);
} SGEN_HASH_TABLE_FOREACH_END;
sgen_hash_table_clean (&hash_table);
dyn_array_int_uninit (&merge_array);
//g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
}
static HashEntry*
register_bridge_object (MonoObject *obj)
{
HashEntry *entry = get_hash_entry (obj, NULL);
entry->is_bridge = TRUE;
return entry;
}
static void
register_finishing_time (HashEntry *entry, guint32 t)
{
g_assert (entry->v.dfs1.finishing_time == 0);
/* finishing_time has 31 bits, so it must be within signed int32 range. */
g_assert (t > 0 && t <= G_MAXINT32);
entry->v.dfs1.finishing_time = t;
}
static int ignored_objects;
static gboolean
is_opaque_object (MonoObject *obj)
{
if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT) == SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT) {
SGEN_LOG (6, "ignoring %s\n", obj->vtable->klass->name);
++ignored_objects;
return TRUE;
}
return FALSE;
}
static gboolean
object_needs_expansion (MonoObject **objp)
{
MonoObject *obj = *objp;
MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
if (fwd) {
*objp = fwd;
if (is_opaque_object (fwd))
return FALSE;
return sgen_hash_table_lookup (&hash_table, fwd) != NULL;
}
if (is_opaque_object (obj))
return FALSE;
if (!sgen_object_is_live (obj))
return TRUE;
return sgen_hash_table_lookup (&hash_table, obj) != NULL;
}
static HashEntry*
follow_forward (HashEntry *entry)
{
while (entry->v.dfs1.forwarded_to) {
HashEntry *next = entry->v.dfs1.forwarded_to;
if (next->v.dfs1.forwarded_to)
entry->v.dfs1.forwarded_to = next->v.dfs1.forwarded_to;
entry = next;
}
return entry;
}
static DynPtrArray registered_bridges;
static DynPtrArray dfs_stack;
static int dfs1_passes, dfs2_passes;
/*
* DFS1 maintains a stack, where each two entries are effectively one entry. (FIXME:
* Optimize this via pointer tagging.) There are two different types of entries:
*
* entry, src: entry needs to be expanded via scanning, and linked to from src
* NULL, entry: entry has already been expanded and needs to be finished
*/
#undef HANDLE_PTR
#define HANDLE_PTR(ptr,obj) do { \
MonoObject *dst = (MonoObject*)*(ptr); \
if (dst && object_needs_expansion (&dst)) { \
++num_links; \
dyn_array_ptr_push (&dfs_stack, obj_entry); \
dyn_array_ptr_push (&dfs_stack, follow_forward (get_hash_entry (dst, NULL))); \
} \
} while (0)
static void
dfs1 (HashEntry *obj_entry)
{
HashEntry *src;
g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
dyn_array_ptr_push (&dfs_stack, NULL);
dyn_array_ptr_push (&dfs_stack, obj_entry);
do {
MonoObject *obj;
char *start;
++dfs1_passes;
obj_entry = dyn_array_ptr_pop (&dfs_stack);
if (obj_entry) {
/* obj_entry needs to be expanded */
src = dyn_array_ptr_pop (&dfs_stack);
if (src)
g_assert (!src->v.dfs1.forwarded_to);
obj_entry = follow_forward (obj_entry);
again:
g_assert (!obj_entry->v.dfs1.forwarded_to);
obj = sgen_hash_table_key_for_value_pointer (obj_entry);
start = (char*)obj;
if (!obj_entry->v.dfs1.is_visited) {
int num_links = 0;
obj_entry->v.dfs1.is_visited = 1;
/* push the finishing entry on the stack */
dyn_array_ptr_push (&dfs_stack, obj_entry);
dyn_array_ptr_push (&dfs_stack, NULL);
#include "sgen-scan-object.h"
/*
* We can remove non-bridge objects with a single outgoing
* link by forwarding links going to it.
*
* This is the first time we've encountered this object, so
* no links to it have yet been added. We'll keep it that
* way by setting the forward pointer, and instead of
* continuing processing this object, we start over with the
* object it points to.
*/
if (!obj_entry->is_bridge && num_links == 1) {
HashEntry *dst_entry = dyn_array_ptr_pop (&dfs_stack);
HashEntry *obj_entry_again = dyn_array_ptr_pop (&dfs_stack);
g_assert (obj_entry_again == obj_entry);
g_assert (!dst_entry->v.dfs1.forwarded_to);
if (obj_entry != dst_entry) {
obj_entry->v.dfs1.forwarded_to = dst_entry;
obj_entry = dst_entry;
}
goto again;
}
}
if (src) {
//g_print ("link %s -> %s\n", sgen_safe_name (src->obj), sgen_safe_name (obj));
g_assert (!obj_entry->v.dfs1.forwarded_to);
add_source (obj_entry, src);
} else {
//g_print ("starting with %s\n", sgen_safe_name (obj));
}
} else {
/* obj_entry needs to be finished */
obj_entry = dyn_array_ptr_pop (&dfs_stack);
//g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
register_finishing_time (obj_entry, ++current_time);
}
} while (dyn_array_ptr_size (&dfs_stack) > 0);
}
static DynSCCArray sccs;
static SCC *current_scc;
/*
* At the end of bridge processing we need to end up with an (acyclyc) graph of bridge
* object SCCs, where the links between the nodes (each one an SCC) in that graph represent
* the presence of a direct or indirect link between those SCCs. An example:
*
* D
* |
* v
* A -> B -> c -> e -> F
*
* A, B, D and F are SCCs that contain bridge objects, c and e don't contain bridge objects.
* The graph we need to produce from this is:
*
* D
* |
* v
* A -> B -> F
*
* Note that we don't need to produce an edge from A to F. It's sufficient that F is
* indirectly reachable from A.
*
* The old algorithm would create a set, for each SCC, of bridge SCCs that can reach it,
* directly or indirectly, by merging the ones sets for those that reach it directly. The
* sets it would build up are these:
*
* A: {}
* B: {A}
* c: {B}
* D: {}
* e: {B,D}
* F: {B,D}
*
* The merge operations on these sets turned out to be huge time sinks.
*
* The new algorithm proceeds in two passes: During DFS2, it only builds up the sets of SCCs
* that directly point to each SCC:
*
* A: {}
* B: {A}
* c: {B}
* D: {}
* e: {c,D}
* F: {e}
*
* This is the adjacency list for the SCC graph, in other words. In a separate step
* afterwards, it does a depth-first traversal of that graph, for each bridge node, to get
* to the final list. It uses a flag to avoid traversing any node twice.
*/
static void
scc_add_xref (SCC *src, SCC *dst)
{
g_assert (src != dst);
g_assert (src->index != dst->index);
#ifdef NEW_XREFS
/*
* FIXME: Right now we don't even unique the direct ancestors, but just add to the
* list. Doing a containment check slows this algorithm down to almost the speed of
* the old one. Use the flag instead!
*/
dyn_array_int_add (&dst->new_xrefs, src->index);
#endif
#ifdef OLD_XREFS
if (dyn_array_int_is_copy (&dst->old_xrefs)) {
int i;
dyn_array_int_ensure_independent (&dst->old_xrefs);
for (i = 0; i < dyn_array_int_size (&dst->old_xrefs); ++i) {
int j = dyn_array_int_get (&dst->old_xrefs, i);
SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
g_assert (!bridge_scc->flag);
bridge_scc->flag = TRUE;
}
}
if (src->num_bridge_entries) {
if (src->flag)
return;
src->flag = TRUE;
dyn_array_int_add (&dst->old_xrefs, src->index);
} else if (dyn_array_int_size (&dst->old_xrefs) == 0) {
dyn_array_int_copy (&dst->old_xrefs, &src->old_xrefs);
} else {
int i;
for (i = 0; i < dyn_array_int_size (&src->old_xrefs); ++i) {
int j = dyn_array_int_get (&src->old_xrefs, i);
SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
g_assert (bridge_scc->num_bridge_entries);
if (!bridge_scc->flag) {
bridge_scc->flag = TRUE;
dyn_array_int_add (&dst->old_xrefs, j);
}
}
}
#endif
}
static void
scc_add_entry (SCC *scc, HashEntry *entry)
{
g_assert (entry->v.dfs2.scc_index < 0);
entry->v.dfs2.scc_index = scc->index;
if (entry->is_bridge)
++scc->num_bridge_entries;
}
static void
dfs2 (HashEntry *entry)
{
int i;
g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
dyn_array_ptr_push (&dfs_stack, entry);
do {
entry = dyn_array_ptr_pop (&dfs_stack);
++dfs2_passes;
if (entry->v.dfs2.scc_index >= 0) {
if (entry->v.dfs2.scc_index != current_scc->index)
scc_add_xref (dyn_array_scc_get_ptr (&sccs, entry->v.dfs2.scc_index), current_scc);
continue;
}
scc_add_entry (current_scc, entry);
for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i)
dyn_array_ptr_push (&dfs_stack, dyn_array_ptr_get (&entry->srcs, i));
} while (dyn_array_ptr_size (&dfs_stack) > 0);
#ifdef OLD_XREFS
/* If xrefs is a copy then we haven't set a single flag. */
if (dyn_array_int_is_copy (¤t_scc->old_xrefs))
return;
for (i = 0; i < dyn_array_int_size (¤t_scc->old_xrefs); ++i) {
int j = dyn_array_int_get (¤t_scc->old_xrefs, i);
SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
g_assert (bridge_scc->flag);
bridge_scc->flag = FALSE;
}
#endif
}
#ifdef NEW_XREFS
static void
gather_xrefs (SCC *scc)
{
int i;
for (i = 0; i < dyn_array_int_size (&scc->new_xrefs); ++i) {
int index = dyn_array_int_get (&scc->new_xrefs, i);
SCC *src = dyn_array_scc_get_ptr (&sccs, index);
if (src->flag)
continue;
src->flag = TRUE;
if (src->num_bridge_entries)
dyn_array_int_add (&merge_array, index);
else
gather_xrefs (src);
}
}
static void
reset_flags (SCC *scc)
{
int i;
for (i = 0; i < dyn_array_int_size (&scc->new_xrefs); ++i) {
int index = dyn_array_int_get (&scc->new_xrefs, i);
SCC *src = dyn_array_scc_get_ptr (&sccs, index);
if (!src->flag)
continue;
src->flag = FALSE;
if (!src->num_bridge_entries)
reset_flags (src);
}
}
#endif
static char *dump_prefix = NULL;
static void
dump_graph (void)
{
static int counter = 0;
MonoObject *obj;
HashEntry *entry;
int prefix_len = strlen (dump_prefix);
char filename [prefix_len + 64];
FILE *file;
int edge_id = 0;
sprintf (filename, "%s.%d.gexf", dump_prefix, counter++);
file = fopen (filename, "w");
if (file == NULL) {
fprintf (stderr, "Warning: Could not open bridge dump file `%s` for writing: %s\n", filename, strerror (errno));
return;
}
fprintf (file, "<gexf xmlns=\"http://www.gexf.net/1.2draft\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd\" version=\"1.2\">\n");
fprintf (file, "<graph defaultedgetype=\"directed\">\n"
"<attributes class=\"node\">\n"
"<attribute id=\"0\" title=\"class\" type=\"string\"/>\n"
"<attribute id=\"1\" title=\"bridge\" type=\"boolean\"/>\n"
"</attributes>\n");
fprintf (file, "<nodes>\n");
SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
MonoVTable *vt = (MonoVTable*) SGEN_LOAD_VTABLE (obj);
fprintf (file, "<node id=\"%p\"><attvalues><attvalue for=\"0\" value=\"%s.%s\"/><attvalue for=\"1\" value=\"%s\"/></attvalues></node>\n",
obj, vt->klass->name_space, vt->klass->name, entry->is_bridge ? "true" : "false");
} SGEN_HASH_TABLE_FOREACH_END;
fprintf (file, "</nodes>\n");
fprintf (file, "<edges>\n");
SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
int i;
for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i) {
HashEntry *src = dyn_array_ptr_get (&entry->srcs, i);
fprintf (file, "<edge id=\"%d\" source=\"%p\" target=\"%p\"/>\n", edge_id++, sgen_hash_table_key_for_value_pointer (src), obj);
}
} SGEN_HASH_TABLE_FOREACH_END;
fprintf (file, "</edges>\n");
fprintf (file, "</graph></gexf>\n");
fclose (file);
}
static void
set_dump_prefix (const char *prefix)
{
dump_prefix = strdup (prefix);
}
static int
compare_hash_entries (const HashEntry *e1, const HashEntry *e2)
{
/* We can cast to signed int here because finishing_time has only 31 bits. */
return (gint32)e2->v.dfs1.finishing_time - (gint32)e1->v.dfs1.finishing_time;
}
DEF_QSORT_INLINE(hash_entries, HashEntry*, compare_hash_entries)
static unsigned long step_1, step_2, step_3, step_4, step_5, step_6;
static int fist_pass_links, second_pass_links, sccs_links;
static int max_sccs_links = 0;
static void
register_finalized_object (MonoObject *obj)
{
g_assert (sgen_need_bridge_processing ());
dyn_array_ptr_push (®istered_bridges, obj);
}
static void
reset_data (void)
{
dyn_array_ptr_empty (®istered_bridges);
}
static void
processing_stw_step (void)
{
int i;
int bridge_count;
MonoObject *obj;
HashEntry *entry;
SGEN_TV_DECLARE (atv);
SGEN_TV_DECLARE (btv);
if (!dyn_array_ptr_size (®istered_bridges))
return;
SGEN_TV_GETTIME (btv);
/* first DFS pass */
dyn_array_ptr_init (&dfs_stack);
dyn_array_int_init (&merge_array);
current_time = 0;
/*
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.
*/
bridge_count = dyn_array_ptr_size (®istered_bridges);
for (i = 0; i < bridge_count ; ++i)
register_bridge_object (dyn_array_ptr_get (®istered_bridges, i));
for (i = 0; i < bridge_count; ++i)
dfs1 (get_hash_entry (dyn_array_ptr_get (®istered_bridges, i), NULL));
/* Remove all forwarded objects. */
SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
if (entry->v.dfs1.forwarded_to) {
g_assert (dyn_array_ptr_size (&entry->srcs) == 0);
SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);