sgen-new-bridge.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /**
  2. * \file
  3. * Simple generational GC.
  4. *
  5. * Copyright 2011 Novell, Inc (http://www.novell.com)
  6. * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
  7. * Copyright 2001-2003 Ximian, Inc
  8. * Copyright 2003-2010 Novell, Inc.
  9. *
  10. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  11. */
  12. #include "config.h"
  13. #if defined (HAVE_SGEN_GC) && !defined (DISABLE_SGEN_GC_BRIDGE)
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #include "sgen/sgen-gc.h"
  17. #include "sgen-bridge-internals.h"
  18. #include "sgen/sgen-hash-table.h"
  19. #include "sgen/sgen-qsort.h"
  20. #include "sgen/sgen-client.h"
  21. #include "tabledefs.h"
  22. #include "utils/mono-logger-internals.h"
  23. #define OPTIMIZATION_COPY
  24. #define OPTIMIZATION_FORWARD
  25. #define OPTIMIZATION_SINGLETON_DYN_ARRAY
  26. #include "sgen-dynarray.h"
  27. //#define NEW_XREFS
  28. #ifdef NEW_XREFS
  29. //#define TEST_NEW_XREFS
  30. #endif
  31. #if !defined(NEW_XREFS) || defined(TEST_NEW_XREFS)
  32. #define OLD_XREFS
  33. #endif
  34. #ifdef NEW_XREFS
  35. #define XREFS new_xrefs
  36. #else
  37. #define XREFS old_xrefs
  38. #endif
  39. /*
  40. * Bridge data for a single managed object
  41. *
  42. * FIXME: Optimizations:
  43. *
  44. * Don't allocate a srcs array for just one source. Most objects have
  45. * just one source, so use the srcs pointer itself.
  46. */
  47. typedef struct _HashEntry {
  48. gboolean is_bridge;
  49. union {
  50. struct {
  51. guint32 is_visited : 1;
  52. guint32 finishing_time : 31;
  53. struct _HashEntry *forwarded_to;
  54. } dfs1;
  55. struct {
  56. // Index in sccs array of SCC this object was folded into
  57. int scc_index;
  58. } dfs2;
  59. } v;
  60. // "Source" managed objects pointing at this destination
  61. DynPtrArray srcs;
  62. } HashEntry;
  63. typedef struct {
  64. HashEntry entry;
  65. double weight;
  66. } HashEntryWithAccounting;
  67. // The graph of managed objects/HashEntries is reduced to a graph of strongly connected components
  68. typedef struct _SCC {
  69. int index;
  70. int api_index;
  71. // How many bridged objects does this SCC hold references to?
  72. int num_bridge_entries;
  73. gboolean flag;
  74. /*
  75. * Index in global sccs array of SCCs holding pointers to this SCC
  76. *
  77. * New and old xrefs are typically mutually exclusive. Only when TEST_NEW_XREFS is
  78. * enabled we do both, and compare the results. This should only be done for
  79. * debugging, obviously.
  80. */
  81. #ifdef OLD_XREFS
  82. DynIntArray old_xrefs; /* these are incoming, not outgoing */
  83. #endif
  84. #ifdef NEW_XREFS
  85. DynIntArray new_xrefs;
  86. #endif
  87. } SCC;
  88. static char *dump_prefix = NULL;
  89. // Maps managed objects to corresponding HashEntry stricts
  90. 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);
  91. static guint32 current_time;
  92. static gboolean bridge_accounting_enabled = FALSE;
  93. static SgenBridgeProcessor *bridge_processor;
  94. /* Core functions */
  95. /*SCC */
  96. static void
  97. dyn_array_scc_init (DynSCCArray *da)
  98. {
  99. dyn_array_init (&da->array);
  100. }
  101. static void
  102. dyn_array_scc_uninit (DynSCCArray *da)
  103. {
  104. dyn_array_uninit (&da->array, sizeof (SCC));
  105. }
  106. static int
  107. dyn_array_scc_size (DynSCCArray *da)
  108. {
  109. return da->array.size;
  110. }
  111. static SCC*
  112. dyn_array_scc_add (DynSCCArray *da)
  113. {
  114. return (SCC *)dyn_array_add (&da->array, sizeof (SCC));
  115. }
  116. static SCC*
  117. dyn_array_scc_get_ptr (DynSCCArray *da, int x)
  118. {
  119. return &((SCC*)da->array.data)[x];
  120. }
  121. /* Merge code*/
  122. static DynIntArray merge_array;
  123. #ifdef NEW_XREFS
  124. static gboolean
  125. dyn_array_int_contains (DynIntArray *da, int x)
  126. {
  127. int i;
  128. for (i = 0; i < dyn_array_int_size (da); ++i)
  129. if (dyn_array_int_get (da, i) == x)
  130. return TRUE;
  131. return FALSE;
  132. }
  133. #endif
  134. static void
  135. set_config (const SgenBridgeProcessorConfig *config)
  136. {
  137. if (config->accounting) {
  138. SgenHashTable table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntryWithAccounting), mono_aligned_addr_hash, NULL);
  139. bridge_accounting_enabled = TRUE;
  140. hash_table = table;
  141. }
  142. if (config->dump_prefix) {
  143. dump_prefix = strdup (config->dump_prefix);
  144. }
  145. }
  146. static MonoGCBridgeObjectKind
  147. class_kind (MonoClass *klass)
  148. {
  149. MonoGCBridgeObjectKind res = mono_bridge_callbacks.bridge_class_kind (klass);
  150. /* If it's a bridge, nothing we can do about it. */
  151. if (res == GC_BRIDGE_TRANSPARENT_BRIDGE_CLASS || res == GC_BRIDGE_OPAQUE_BRIDGE_CLASS)
  152. return res;
  153. /* Non bridge classes with no pointers will never point to a bridge, so we can savely ignore them. */
  154. if (!m_class_has_references (klass)) {
  155. SGEN_LOG (6, "class %s is opaque\n", m_class_get_name (klass));
  156. return GC_BRIDGE_OPAQUE_CLASS;
  157. }
  158. /* Some arrays can be ignored */
  159. if (m_class_get_rank (klass) == 1) {
  160. MonoClass *elem_class = m_class_get_element_class (klass);
  161. /* FIXME the bridge check can be quite expensive, cache it at the class level. */
  162. /* An array of a sealed type that is not a bridge will never get to a bridge */
  163. if ((mono_class_get_flags (elem_class) & TYPE_ATTRIBUTE_SEALED) && !m_class_has_references (elem_class) && !mono_bridge_callbacks.bridge_class_kind (elem_class)) {
  164. SGEN_LOG (6, "class %s is opaque\n", m_class_get_name (klass));
  165. return GC_BRIDGE_OPAQUE_CLASS;
  166. }
  167. }
  168. return GC_BRIDGE_TRANSPARENT_CLASS;
  169. }
  170. static HashEntry*
  171. get_hash_entry (MonoObject *obj, gboolean *existing)
  172. {
  173. HashEntry *entry = (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
  174. HashEntry new_entry;
  175. if (entry) {
  176. if (existing)
  177. *existing = TRUE;
  178. return entry;
  179. }
  180. if (existing)
  181. *existing = FALSE;
  182. memset (&new_entry, 0, sizeof (HashEntry));
  183. dyn_array_ptr_init (&new_entry.srcs);
  184. new_entry.v.dfs1.finishing_time = 0;
  185. sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
  186. return (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
  187. }
  188. static void
  189. add_source (HashEntry *entry, HashEntry *src)
  190. {
  191. dyn_array_ptr_add (&entry->srcs, src);
  192. }
  193. static void
  194. free_data (void)
  195. {
  196. MonoObject *obj G_GNUC_UNUSED;
  197. HashEntry *entry;
  198. int total_srcs = 0;
  199. int max_srcs = 0;
  200. SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
  201. int entry_size = dyn_array_ptr_size (&entry->srcs);
  202. total_srcs += entry_size;
  203. if (entry_size > max_srcs)
  204. max_srcs = entry_size;
  205. dyn_array_ptr_uninit (&entry->srcs);
  206. } SGEN_HASH_TABLE_FOREACH_END;
  207. sgen_hash_table_clean (&hash_table);
  208. dyn_array_int_uninit (&merge_array);
  209. //g_print ("total srcs %d - max %d\n", total_srcs, max_srcs);
  210. }
  211. static HashEntry*
  212. register_bridge_object (MonoObject *obj)
  213. {
  214. HashEntry *entry = get_hash_entry (obj, NULL);
  215. entry->is_bridge = TRUE;
  216. return entry;
  217. }
  218. static void
  219. register_finishing_time (HashEntry *entry, guint32 t)
  220. {
  221. g_assert (entry->v.dfs1.finishing_time == 0);
  222. /* finishing_time has 31 bits, so it must be within signed int32 range. */
  223. g_assert (t > 0 && t <= G_MAXINT32);
  224. entry->v.dfs1.finishing_time = t;
  225. }
  226. static int ignored_objects;
  227. static gboolean
  228. is_opaque_object (MonoObject *obj)
  229. {
  230. if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT) == SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT) {
  231. SGEN_LOG (6, "ignoring %s\n", m_class_get_name (mono_object_class (obj)));
  232. ++ignored_objects;
  233. return TRUE;
  234. }
  235. return FALSE;
  236. }
  237. static gboolean
  238. object_needs_expansion (MonoObject **objp)
  239. {
  240. MonoObject *obj = *objp;
  241. MonoObject *fwd = SGEN_OBJECT_IS_FORWARDED (obj);
  242. if (fwd) {
  243. *objp = fwd;
  244. if (is_opaque_object (fwd))
  245. return FALSE;
  246. return sgen_hash_table_lookup (&hash_table, fwd) != NULL;
  247. }
  248. if (is_opaque_object (obj))
  249. return FALSE;
  250. if (!sgen_object_is_live (obj))
  251. return TRUE;
  252. return sgen_hash_table_lookup (&hash_table, obj) != NULL;
  253. }
  254. static HashEntry*
  255. follow_forward (HashEntry *entry)
  256. {
  257. #ifdef OPTIMIZATION_FORWARD
  258. while (entry->v.dfs1.forwarded_to) {
  259. HashEntry *next = entry->v.dfs1.forwarded_to;
  260. if (next->v.dfs1.forwarded_to)
  261. entry->v.dfs1.forwarded_to = next->v.dfs1.forwarded_to;
  262. entry = next;
  263. }
  264. #else
  265. g_assert (!entry->v.dfs1.forwarded_to);
  266. #endif
  267. return entry;
  268. }
  269. static DynPtrArray registered_bridges;
  270. static DynPtrArray dfs_stack;
  271. static int dfs1_passes, dfs2_passes;
  272. /*
  273. * DFS1 maintains a stack, where each two entries are effectively one entry. (FIXME:
  274. * Optimize this via pointer tagging.) There are two different types of entries:
  275. *
  276. * entry, src: entry needs to be expanded via scanning, and linked to from src
  277. * NULL, entry: entry has already been expanded and needs to be finished
  278. */
  279. #undef HANDLE_PTR
  280. #define HANDLE_PTR(ptr,obj) do { \
  281. GCObject *dst = (GCObject*)*(ptr); \
  282. if (dst && object_needs_expansion (&dst)) { \
  283. ++num_links; \
  284. dyn_array_ptr_push (&dfs_stack, obj_entry); \
  285. dyn_array_ptr_push (&dfs_stack, follow_forward (get_hash_entry (dst, NULL))); \
  286. } \
  287. } while (0)
  288. static void
  289. dfs1 (HashEntry *obj_entry)
  290. {
  291. HashEntry *src;
  292. g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
  293. dyn_array_ptr_push (&dfs_stack, NULL);
  294. dyn_array_ptr_push (&dfs_stack, obj_entry);
  295. do {
  296. MonoObject *obj;
  297. char *start;
  298. ++dfs1_passes;
  299. obj_entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
  300. if (obj_entry) {
  301. /* obj_entry needs to be expanded */
  302. src = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
  303. if (src)
  304. g_assert (!src->v.dfs1.forwarded_to);
  305. obj_entry = follow_forward (obj_entry);
  306. again:
  307. g_assert (!obj_entry->v.dfs1.forwarded_to);
  308. obj = sgen_hash_table_key_for_value_pointer (obj_entry);
  309. start = (char*)obj;
  310. if (!obj_entry->v.dfs1.is_visited) {
  311. int num_links = 0;
  312. mword desc = sgen_obj_get_descriptor_safe (obj);
  313. obj_entry->v.dfs1.is_visited = 1;
  314. /* push the finishing entry on the stack */
  315. dyn_array_ptr_push (&dfs_stack, obj_entry);
  316. dyn_array_ptr_push (&dfs_stack, NULL);
  317. #include "sgen/sgen-scan-object.h"
  318. /*
  319. * We can remove non-bridge objects with a single outgoing
  320. * link by forwarding links going to it.
  321. *
  322. * This is the first time we've encountered this object, so
  323. * no links to it have yet been added. We'll keep it that
  324. * way by setting the forward pointer, and instead of
  325. * continuing processing this object, we start over with the
  326. * object it points to.
  327. */
  328. #ifdef OPTIMIZATION_FORWARD
  329. if (!obj_entry->is_bridge && num_links == 1) {
  330. HashEntry *dst_entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
  331. HashEntry *obj_entry_again = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
  332. g_assert (obj_entry_again == obj_entry);
  333. g_assert (!dst_entry->v.dfs1.forwarded_to);
  334. if (obj_entry != dst_entry) {
  335. obj_entry->v.dfs1.forwarded_to = dst_entry;
  336. obj_entry = dst_entry;
  337. }
  338. goto again;
  339. }
  340. #endif
  341. }
  342. if (src) {
  343. //g_print ("link %s -> %s\n", sgen_safe_name (src->obj), sgen_safe_name (obj));
  344. g_assert (!obj_entry->v.dfs1.forwarded_to);
  345. add_source (obj_entry, src);
  346. } else {
  347. //g_print ("starting with %s\n", sgen_safe_name (obj));
  348. }
  349. } else {
  350. /* obj_entry needs to be finished */
  351. obj_entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
  352. //g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
  353. register_finishing_time (obj_entry, ++current_time);
  354. }
  355. } while (dyn_array_ptr_size (&dfs_stack) > 0);
  356. }
  357. static DynSCCArray sccs;
  358. static SCC *current_scc;
  359. /*
  360. * At the end of bridge processing we need to end up with an (acyclyc) graph of bridge
  361. * object SCCs, where the links between the nodes (each one an SCC) in that graph represent
  362. * the presence of a direct or indirect link between those SCCs. An example:
  363. *
  364. * D
  365. * |
  366. * v
  367. * A -> B -> c -> e -> F
  368. *
  369. * A, B, D and F are SCCs that contain bridge objects, c and e don't contain bridge objects.
  370. * The graph we need to produce from this is:
  371. *
  372. * D
  373. * |
  374. * v
  375. * A -> B -> F
  376. *
  377. * Note that we don't need to produce an edge from A to F. It's sufficient that F is
  378. * indirectly reachable from A.
  379. *
  380. * The old algorithm would create a set, for each SCC, of bridge SCCs that can reach it,
  381. * directly or indirectly, by merging the ones sets for those that reach it directly. The
  382. * sets it would build up are these:
  383. *
  384. * A: {}
  385. * B: {A}
  386. * c: {B}
  387. * D: {}
  388. * e: {B,D}
  389. * F: {B,D}
  390. *
  391. * The merge operations on these sets turned out to be huge time sinks.
  392. *
  393. * The new algorithm proceeds in two passes: During DFS2, it only builds up the sets of SCCs
  394. * that directly point to each SCC:
  395. *
  396. * A: {}
  397. * B: {A}
  398. * c: {B}
  399. * D: {}
  400. * e: {c,D}
  401. * F: {e}
  402. *
  403. * This is the adjacency list for the SCC graph, in other words. In a separate step
  404. * afterwards, it does a depth-first traversal of that graph, for each bridge node, to get
  405. * to the final list. It uses a flag to avoid traversing any node twice.
  406. */
  407. static void
  408. scc_add_xref (SCC *src, SCC *dst)
  409. {
  410. g_assert (src != dst);
  411. g_assert (src->index != dst->index);
  412. #ifdef NEW_XREFS
  413. /*
  414. * FIXME: Right now we don't even unique the direct ancestors, but just add to the
  415. * list. Doing a containment check slows this algorithm down to almost the speed of
  416. * the old one. Use the flag instead!
  417. */
  418. dyn_array_int_add (&dst->new_xrefs, src->index);
  419. #endif
  420. #ifdef OLD_XREFS
  421. if (dyn_array_int_is_copy (&dst->old_xrefs)) {
  422. int i;
  423. dyn_array_int_ensure_independent (&dst->old_xrefs);
  424. for (i = 0; i < dyn_array_int_size (&dst->old_xrefs); ++i) {
  425. int j = dyn_array_int_get (&dst->old_xrefs, i);
  426. SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
  427. g_assert (!bridge_scc->flag);
  428. bridge_scc->flag = TRUE;
  429. }
  430. }
  431. if (src->num_bridge_entries) {
  432. if (src->flag)
  433. return;
  434. src->flag = TRUE;
  435. dyn_array_int_add (&dst->old_xrefs, src->index);
  436. #ifdef OPTIMIZATION_COPY
  437. } else if (dyn_array_int_size (&dst->old_xrefs) == 0) {
  438. dyn_array_int_copy (&dst->old_xrefs, &src->old_xrefs);
  439. #endif
  440. } else {
  441. int i;
  442. for (i = 0; i < dyn_array_int_size (&src->old_xrefs); ++i) {
  443. int j = dyn_array_int_get (&src->old_xrefs, i);
  444. SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
  445. g_assert (bridge_scc->num_bridge_entries);
  446. if (!bridge_scc->flag) {
  447. bridge_scc->flag = TRUE;
  448. dyn_array_int_add (&dst->old_xrefs, j);
  449. }
  450. }
  451. }
  452. #endif
  453. }
  454. static void
  455. scc_add_entry (SCC *scc, HashEntry *entry)
  456. {
  457. g_assert (entry->v.dfs2.scc_index < 0);
  458. entry->v.dfs2.scc_index = scc->index;
  459. if (entry->is_bridge)
  460. ++scc->num_bridge_entries;
  461. }
  462. static void
  463. dfs2 (HashEntry *entry)
  464. {
  465. int i;
  466. g_assert (dyn_array_ptr_size (&dfs_stack) == 0);
  467. dyn_array_ptr_push (&dfs_stack, entry);
  468. do {
  469. entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
  470. ++dfs2_passes;
  471. if (entry->v.dfs2.scc_index >= 0) {
  472. if (entry->v.dfs2.scc_index != current_scc->index)
  473. scc_add_xref (dyn_array_scc_get_ptr (&sccs, entry->v.dfs2.scc_index), current_scc);
  474. continue;
  475. }
  476. scc_add_entry (current_scc, entry);
  477. for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i)
  478. dyn_array_ptr_push (&dfs_stack, dyn_array_ptr_get (&entry->srcs, i));
  479. } while (dyn_array_ptr_size (&dfs_stack) > 0);
  480. #ifdef OLD_XREFS
  481. /* If xrefs is a copy then we haven't set a single flag. */
  482. if (dyn_array_int_is_copy (&current_scc->old_xrefs))
  483. return;
  484. for (i = 0; i < dyn_array_int_size (&current_scc->old_xrefs); ++i) {
  485. int j = dyn_array_int_get (&current_scc->old_xrefs, i);
  486. SCC *bridge_scc = dyn_array_scc_get_ptr (&sccs, j);
  487. g_assert (bridge_scc->flag);
  488. bridge_scc->flag = FALSE;
  489. }
  490. #endif
  491. }
  492. #ifdef NEW_XREFS
  493. static void
  494. gather_xrefs (SCC *scc)
  495. {
  496. int i;
  497. for (i = 0; i < dyn_array_int_size (&scc->new_xrefs); ++i) {
  498. int index = dyn_array_int_get (&scc->new_xrefs, i);
  499. SCC *src = dyn_array_scc_get_ptr (&sccs, index);
  500. if (src->flag)
  501. continue;
  502. src->flag = TRUE;
  503. if (src->num_bridge_entries)
  504. dyn_array_int_add (&merge_array, index);
  505. else
  506. gather_xrefs (src);
  507. }
  508. }
  509. static void
  510. reset_flags (SCC *scc)
  511. {
  512. int i;
  513. for (i = 0; i < dyn_array_int_size (&scc->new_xrefs); ++i) {
  514. int index = dyn_array_int_get (&scc->new_xrefs, i);
  515. SCC *src = dyn_array_scc_get_ptr (&sccs, index);
  516. if (!src->flag)
  517. continue;
  518. src->flag = FALSE;
  519. if (!src->num_bridge_entries)
  520. reset_flags (src);
  521. }
  522. }
  523. #endif
  524. static void
  525. dump_graph (void)
  526. {
  527. static int counter = 0;
  528. MonoObject *obj;
  529. HashEntry *entry;
  530. size_t prefix_len = strlen (dump_prefix);
  531. char *filename = g_newa (char, prefix_len + 64);
  532. FILE *file;
  533. int edge_id = 0;
  534. sprintf (filename, "%s.%d.gexf", dump_prefix, counter++);
  535. file = fopen (filename, "w");
  536. if (file == NULL) {
  537. fprintf (stderr, "Warning: Could not open bridge dump file `%s` for writing: %s\n", filename, strerror (errno));
  538. return;
  539. }
  540. 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");
  541. fprintf (file, "<graph defaultedgetype=\"directed\">\n"
  542. "<attributes class=\"node\">\n"
  543. "<attribute id=\"0\" title=\"class\" type=\"string\"/>\n"
  544. "<attribute id=\"1\" title=\"bridge\" type=\"boolean\"/>\n"
  545. "</attributes>\n");
  546. fprintf (file, "<nodes>\n");
  547. SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
  548. MonoVTable *vt = SGEN_LOAD_VTABLE (obj);
  549. fprintf (file, "<node id=\"%p\"><attvalues><attvalue for=\"0\" value=\"%s.%s\"/><attvalue for=\"1\" value=\"%s\"/></attvalues></node>\n",
  550. obj, m_class_get_name_space (vt->klass), m_class_get_name (vt->klass), entry->is_bridge ? "true" : "false");
  551. } SGEN_HASH_TABLE_FOREACH_END;
  552. fprintf (file, "</nodes>\n");
  553. fprintf (file, "<edges>\n");
  554. SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
  555. int i;
  556. for (i = 0; i < dyn_array_ptr_size (&entry->srcs); ++i) {
  557. HashEntry *src = (HashEntry *)dyn_array_ptr_get (&entry->srcs, i);
  558. fprintf (file, "<edge id=\"%d\" source=\"%p\" target=\"%p\"/>\n", edge_id++, sgen_hash_table_key_for_value_pointer (src), obj);
  559. }
  560. } SGEN_HASH_TABLE_FOREACH_END;
  561. fprintf (file, "</edges>\n");
  562. fprintf (file, "</graph></gexf>\n");
  563. fclose (file);
  564. }
  565. static int
  566. compare_hash_entries (const HashEntry *e1, const HashEntry *e2)
  567. {
  568. /* We can cast to signed int here because finishing_time has only 31 bits. */
  569. return (gint32)e2->v.dfs1.finishing_time - (gint32)e1->v.dfs1.finishing_time;
  570. }
  571. DEF_QSORT_INLINE(hash_entries, HashEntry*, compare_hash_entries)
  572. static gint64 step_1, step_2, step_3, step_4, step_5, step_6;
  573. static int fist_pass_links, second_pass_links, sccs_links;
  574. static int max_sccs_links = 0;
  575. static void
  576. register_finalized_object (GCObject *obj)
  577. {
  578. g_assert (sgen_need_bridge_processing ());
  579. dyn_array_ptr_push (&registered_bridges, obj);
  580. }
  581. static void
  582. reset_data (void)
  583. {
  584. dyn_array_ptr_empty (&registered_bridges);
  585. }
  586. static void
  587. processing_stw_step (void)
  588. {
  589. int i;
  590. int bridge_count;
  591. MonoObject *obj G_GNUC_UNUSED;
  592. HashEntry *entry;
  593. SGEN_TV_DECLARE (atv);
  594. SGEN_TV_DECLARE (btv);
  595. if (!dyn_array_ptr_size (&registered_bridges))
  596. return;
  597. SGEN_TV_GETTIME (btv);
  598. /* first DFS pass */
  599. dyn_array_ptr_init (&dfs_stack);
  600. dyn_array_int_init (&merge_array);
  601. current_time = 0;
  602. /*
  603. First we insert all bridges into the hash table and then we do dfs1.
  604. It must be done in 2 steps since the bridge arrays doesn't come in reverse topological order,
  605. which means that we can have entry N pointing to entry N + 1.
  606. If we dfs1 entry N before N + 1 is registered we'll not consider N + 1 for this bridge
  607. pass and not create the required xref between the two.
  608. */
  609. bridge_count = dyn_array_ptr_size (&registered_bridges);
  610. for (i = 0; i < bridge_count ; ++i)
  611. register_bridge_object ((MonoObject *)dyn_array_ptr_get (&registered_bridges, i));
  612. for (i = 0; i < bridge_count; ++i)
  613. dfs1 (get_hash_entry ((MonoObject *)dyn_array_ptr_get (&registered_bridges, i), NULL));
  614. /* Remove all forwarded objects. */
  615. SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
  616. if (entry->v.dfs1.forwarded_to) {
  617. g_assert (dyn_array_ptr_size (&entry->srcs) == 0);
  618. SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
  619. continue;
  620. }
  621. } SGEN_HASH_TABLE_FOREACH_END;
  622. SGEN_TV_GETTIME (atv);
  623. step_2 = SGEN_TV_ELAPSED (btv, atv);
  624. if (dump_prefix)
  625. dump_graph ();
  626. }
  627. static int num_registered_bridges, hash_table_size;
  628. static void
  629. processing_build_callback_data (int generation)
  630. {
  631. int i, j;
  632. int num_sccs, num_xrefs;
  633. int max_entries, max_xrefs;
  634. MonoObject *obj G_GNUC_UNUSED;
  635. HashEntry *entry;
  636. HashEntry **all_entries;
  637. MonoGCBridgeSCC **api_sccs;
  638. MonoGCBridgeXRef *api_xrefs;
  639. SGEN_TV_DECLARE (atv);
  640. SGEN_TV_DECLARE (btv);
  641. g_assert (bridge_processor->num_sccs == 0 && bridge_processor->num_xrefs == 0);
  642. g_assert (!bridge_processor->api_sccs && !bridge_processor->api_xrefs);
  643. if (!dyn_array_ptr_size (&registered_bridges))
  644. return;
  645. g_assert (mono_bridge_processing_in_progress);
  646. SGEN_TV_GETTIME (atv);
  647. /* alloc and fill array of all entries */
  648. all_entries = (HashEntry **)sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
  649. j = 0;
  650. SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
  651. g_assert (entry->v.dfs1.finishing_time > 0);
  652. all_entries [j++] = entry;
  653. fist_pass_links += dyn_array_ptr_size (&entry->srcs);
  654. } SGEN_HASH_TABLE_FOREACH_END;
  655. g_assert (j == hash_table.num_entries);
  656. hash_table_size = hash_table.num_entries;
  657. /* sort array according to decreasing finishing time */
  658. qsort_hash_entries (all_entries, hash_table.num_entries);
  659. SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
  660. entry->v.dfs2.scc_index = -1;
  661. } SGEN_HASH_TABLE_FOREACH_END;
  662. SGEN_TV_GETTIME (btv);
  663. step_3 = SGEN_TV_ELAPSED (atv, btv);
  664. /* second DFS pass */
  665. dyn_array_scc_init (&sccs);
  666. for (i = 0; i < hash_table.num_entries; ++i) {
  667. HashEntry *entry = all_entries [i];
  668. if (entry->v.dfs2.scc_index < 0) {
  669. int index = dyn_array_scc_size (&sccs);
  670. current_scc = dyn_array_scc_add (&sccs);
  671. current_scc->index = index;
  672. current_scc->num_bridge_entries = 0;
  673. #ifdef NEW_XREFS
  674. current_scc->flag = FALSE;
  675. dyn_array_int_init (&current_scc->new_xrefs);
  676. #endif
  677. #ifdef OLD_XREFS
  678. dyn_array_int_init (&current_scc->old_xrefs);
  679. #endif
  680. current_scc->api_index = -1;
  681. dfs2 (entry);
  682. #ifdef NEW_XREFS
  683. /*
  684. * If a node has only one incoming edge, we just copy the source's
  685. * xrefs array, effectively removing the source from the graph.
  686. * This takes care of long linked lists.
  687. */
  688. if (!current_scc->num_bridge_entries && dyn_array_int_size (&current_scc->new_xrefs) == 1) {
  689. SCC *src;
  690. j = dyn_array_int_get (&current_scc->new_xrefs, 0);
  691. src = dyn_array_scc_get_ptr (&sccs, j);
  692. if (src->num_bridge_entries)
  693. dyn_array_int_set (&current_scc->new_xrefs, 0, j);
  694. else
  695. dyn_array_int_copy (&current_scc->new_xrefs, &src->new_xrefs);
  696. }
  697. #endif
  698. }
  699. }
  700. #ifdef NEW_XREFS
  701. #ifdef TEST_NEW_XREFS
  702. for (j = 0; j < dyn_array_scc_size (&sccs); ++j) {
  703. SCC *scc = dyn_array_scc_get_ptr (&sccs, j);
  704. g_assert (!scc->flag);
  705. }
  706. #endif
  707. for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
  708. SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
  709. g_assert (scc->index == i);
  710. if (!scc->num_bridge_entries)
  711. continue;
  712. dyn_array_int_empty (&merge_array);
  713. gather_xrefs (scc);
  714. reset_flags (scc);
  715. dyn_array_int_copy (&scc->new_xrefs, &merge_array);
  716. dyn_array_int_ensure_independent (&scc->new_xrefs);
  717. #ifdef TEST_NEW_XREFS
  718. for (j = 0; j < dyn_array_scc_size (&sccs); ++j) {
  719. SCC *scc = dyn_array_scc_get_ptr (&sccs, j);
  720. g_assert (!scc->flag);
  721. }
  722. #endif
  723. }
  724. #ifdef TEST_NEW_XREFS
  725. for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
  726. SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
  727. g_assert (scc->index == i);
  728. if (!scc->num_bridge_entries)
  729. continue;
  730. g_assert (dyn_array_int_size (&scc->new_xrefs) == dyn_array_int_size (&scc->old_xrefs));
  731. for (j = 0; j < dyn_array_int_size (&scc->new_xrefs); ++j)
  732. g_assert (dyn_array_int_contains (&scc->old_xrefs, dyn_array_int_get (&scc->new_xrefs, j)));
  733. }
  734. #endif
  735. #endif
  736. /*
  737. * Compute the weight of each object. The weight of an object is its size plus the size of all
  738. * objects it points do. When the an object is pointed by multiple objects we distribute it's weight
  739. * equally among them. This distribution gives a rough estimate of the real impact of making the object
  740. * go away.
  741. *
  742. * The reasoning for this model is that complex graphs with single roots will have a bridge with very high
  743. * value in comparison to others.
  744. *
  745. * The all_entries array has all objects topologically sorted. To correctly propagate the weights it must be
  746. * done in reverse topological order - so we calculate the weight of the pointed-to objects before processing
  747. * pointer-from objects.
  748. *
  749. * We log those objects in the opposite order for no particular reason. The other constrain is that it should use the same
  750. * direction as the other logging loop that records live/dead information.
  751. */
  752. if (bridge_accounting_enabled) {
  753. for (i = hash_table.num_entries - 1; i >= 0; --i) {
  754. double w;
  755. HashEntryWithAccounting *entry = (HashEntryWithAccounting*)all_entries [i];
  756. entry->weight += (double)sgen_safe_object_get_size (sgen_hash_table_key_for_value_pointer (entry));
  757. w = entry->weight / dyn_array_ptr_size (&entry->entry.srcs);
  758. for (j = 0; j < dyn_array_ptr_size (&entry->entry.srcs); ++j) {
  759. HashEntryWithAccounting *other = (HashEntryWithAccounting *)dyn_array_ptr_get (&entry->entry.srcs, j);
  760. other->weight += w;
  761. }
  762. }
  763. for (i = 0; i < hash_table.num_entries; ++i) {
  764. HashEntryWithAccounting *entry = (HashEntryWithAccounting*)all_entries [i];
  765. if (entry->entry.is_bridge) {
  766. MonoObject *obj = sgen_hash_table_key_for_value_pointer (entry);
  767. MonoClass *klass = SGEN_LOAD_VTABLE (obj)->klass;
  768. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_GC, "OBJECT %s::%s (%p) weight %f", m_class_get_name_space (klass), m_class_get_name (klass), obj, entry->weight);
  769. }
  770. }
  771. }
  772. for (i = 0; i < hash_table.num_entries; ++i) {
  773. HashEntry *entry = all_entries [i];
  774. second_pass_links += dyn_array_ptr_size (&entry->srcs);
  775. }
  776. SGEN_TV_GETTIME (atv);
  777. step_4 = SGEN_TV_ELAPSED (btv, atv);
  778. //g_print ("%d sccs\n", sccs.size);
  779. dyn_array_ptr_uninit (&dfs_stack);
  780. /* init data for callback */
  781. num_sccs = 0;
  782. for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
  783. SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
  784. g_assert (scc->index == i);
  785. if (scc->num_bridge_entries)
  786. ++num_sccs;
  787. sccs_links += dyn_array_int_size (&scc->XREFS);
  788. max_sccs_links = MAX (max_sccs_links, dyn_array_int_size (&scc->XREFS));
  789. }
  790. api_sccs = (MonoGCBridgeSCC **)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
  791. num_xrefs = 0;
  792. j = 0;
  793. for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
  794. SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
  795. if (!scc->num_bridge_entries)
  796. continue;
  797. api_sccs [j] = (MonoGCBridgeSCC *)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
  798. api_sccs [j]->is_alive = FALSE;
  799. api_sccs [j]->num_objs = scc->num_bridge_entries;
  800. scc->num_bridge_entries = 0;
  801. scc->api_index = j++;
  802. num_xrefs += dyn_array_int_size (&scc->XREFS);
  803. }
  804. SGEN_HASH_TABLE_FOREACH (&hash_table, MonoObject *, obj, HashEntry *, entry) {
  805. if (entry->is_bridge) {
  806. SCC *scc = dyn_array_scc_get_ptr (&sccs, entry->v.dfs2.scc_index);
  807. api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = sgen_hash_table_key_for_value_pointer (entry);
  808. }
  809. } SGEN_HASH_TABLE_FOREACH_END;
  810. api_xrefs = (MonoGCBridgeXRef *)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
  811. j = 0;
  812. for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
  813. int k;
  814. SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
  815. if (!scc->num_bridge_entries)
  816. continue;
  817. for (k = 0; k < dyn_array_int_size (&scc->XREFS); ++k) {
  818. SCC *src_scc = dyn_array_scc_get_ptr (&sccs, dyn_array_int_get (&scc->XREFS, k));
  819. if (!src_scc->num_bridge_entries)
  820. continue;
  821. api_xrefs [j].src_scc_index = src_scc->api_index;
  822. api_xrefs [j].dst_scc_index = scc->api_index;
  823. ++j;
  824. }
  825. }
  826. SGEN_TV_GETTIME (btv);
  827. step_5 = SGEN_TV_ELAPSED (atv, btv);
  828. /* free data */
  829. j = 0;
  830. max_entries = max_xrefs = 0;
  831. for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
  832. SCC *scc = dyn_array_scc_get_ptr (&sccs, i);
  833. if (scc->num_bridge_entries)
  834. ++j;
  835. if (scc->num_bridge_entries > max_entries)
  836. max_entries = scc->num_bridge_entries;
  837. if (dyn_array_int_size (&scc->XREFS) > max_xrefs)
  838. max_xrefs = dyn_array_int_size (&scc->XREFS);
  839. #ifdef NEW_XREFS
  840. dyn_array_int_uninit (&scc->new_xrefs);
  841. #endif
  842. #ifdef OLD_XREFS
  843. dyn_array_int_uninit (&scc->old_xrefs);
  844. #endif
  845. }
  846. dyn_array_scc_uninit (&sccs);
  847. sgen_free_internal_dynamic (all_entries, sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA);
  848. free_data ();
  849. /* Empty the registered bridges array */
  850. num_registered_bridges = dyn_array_ptr_size (&registered_bridges);
  851. dyn_array_ptr_empty (&registered_bridges);
  852. SGEN_TV_GETTIME (atv);
  853. step_6 = SGEN_TV_ELAPSED (btv, atv);
  854. //g_print ("%d sccs containing bridges - %d max bridge objects - %d max xrefs\n", j, max_entries, max_xrefs);
  855. bridge_processor->num_sccs = num_sccs;
  856. bridge_processor->api_sccs = api_sccs;
  857. bridge_processor->num_xrefs = num_xrefs;
  858. bridge_processor->api_xrefs = api_xrefs;
  859. }
  860. static void
  861. processing_after_callback (int generation)
  862. {
  863. int i, j;
  864. int num_sccs = bridge_processor->num_sccs;
  865. MonoGCBridgeSCC **api_sccs = bridge_processor->api_sccs;
  866. if (bridge_accounting_enabled) {
  867. for (i = 0; i < num_sccs; ++i) {
  868. for (j = 0; j < api_sccs [i]->num_objs; ++j) {
  869. GCVTable vtable = SGEN_LOAD_VTABLE (api_sccs [i]->objs [j]);
  870. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_GC,
  871. "OBJECT %s.%s (%p) SCC [%d] %s",
  872. sgen_client_vtable_get_namespace (vtable), sgen_client_vtable_get_name (vtable), api_sccs [i]->objs [j],
  873. i,
  874. api_sccs [i]->is_alive ? "ALIVE" : "DEAD");
  875. }
  876. }
  877. }
  878. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_GC, "GC_NEW_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 ignored %d",
  879. num_registered_bridges, hash_table_size, dyn_array_scc_size (&sccs),
  880. step_1 / 10000.0f,
  881. step_2 / 10000.0f,
  882. step_3 / 10000.0f,
  883. step_4 / 10000.0f,
  884. step_5 / 10000.0f,
  885. step_6 / 10000.0f,
  886. fist_pass_links, second_pass_links, sccs_links, max_sccs_links,
  887. dfs1_passes, dfs2_passes, ignored_objects);
  888. step_1 = 0; /* We must cleanup since this value is used as an accumulator. */
  889. fist_pass_links = second_pass_links = sccs_links = max_sccs_links = 0;
  890. dfs1_passes = dfs2_passes = ignored_objects = 0;
  891. }
  892. static void
  893. describe_pointer (GCObject *obj)
  894. {
  895. HashEntry *entry;
  896. int i;
  897. for (i = 0; i < dyn_array_ptr_size (&registered_bridges); ++i) {
  898. if (obj == dyn_array_ptr_get (&registered_bridges, i)) {
  899. printf ("Pointer is a registered bridge object.\n");
  900. break;
  901. }
  902. }
  903. entry = (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
  904. if (!entry)
  905. return;
  906. printf ("Bridge hash table entry %p:\n", entry);
  907. printf (" is bridge: %d\n", (int)entry->is_bridge);
  908. printf (" is visited: %d\n", (int)entry->v.dfs1.is_visited);
  909. }
  910. void
  911. sgen_new_bridge_init (SgenBridgeProcessor *collector)
  912. {
  913. collector->reset_data = reset_data;
  914. collector->processing_stw_step = processing_stw_step;
  915. collector->processing_build_callback_data = processing_build_callback_data;
  916. collector->processing_after_callback = processing_after_callback;
  917. collector->class_kind = class_kind;
  918. collector->register_finalized_object = register_finalized_object;
  919. collector->describe_pointer = describe_pointer;
  920. collector->set_config = set_config;
  921. bridge_processor = collector;
  922. }
  923. #else
  924. #include <mono/utils/mono-compiler.h>
  925. MONO_EMPTY_SOURCE_FILE (sgen_new_bridge);
  926. #endif