[sgen] Move time stuff to client code.
[mono.git] / mono / metadata / sgen-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
52 MonoGCBridgeCallbacks bridge_callbacks;
53 static SgenBridgeProcessor bridge_processor;
54 static SgenBridgeProcessor compare_to_bridge_processor;
55
56 gboolean bridge_processing_in_progress = FALSE;
57
58 void
59 mono_gc_wait_for_bridge_processing (void)
60 {
61         if (!bridge_processing_in_progress)
62                 return;
63
64         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_BRIDGE waiting for bridge processing to finish");
65
66         sgen_gc_lock ();
67         sgen_gc_unlock ();
68 }
69
70
71 void
72 mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks)
73 {
74         if (callbacks->bridge_version != SGEN_BRIDGE_VERSION)
75                 g_error ("Invalid bridge callback version. Expected %d but got %d\n", SGEN_BRIDGE_VERSION, callbacks->bridge_version);
76
77         bridge_callbacks = *callbacks;
78
79         if (!bridge_processor.reset_data)
80                 sgen_old_bridge_init (&bridge_processor);
81 }
82
83 static gboolean
84 init_bridge_processor (SgenBridgeProcessor *processor, const char *name)
85 {
86         if (!strcmp ("old", name)) {
87                 memset (processor, 0, sizeof (SgenBridgeProcessor));
88                 sgen_old_bridge_init (processor);
89         } else if (!strcmp ("new", name)) {
90                 memset (processor, 0, sizeof (SgenBridgeProcessor));
91                 sgen_new_bridge_init (processor);
92         } else if (!strcmp ("tarjan", name)) {
93                 memset (processor, 0, sizeof (SgenBridgeProcessor));
94                 sgen_tarjan_bridge_init (processor);
95         } else {
96                 return FALSE;
97         }
98         return TRUE;
99 }
100
101 void
102 sgen_set_bridge_implementation (const char *name)
103 {
104         if (!init_bridge_processor (&bridge_processor, name))
105                 g_warning ("Invalid value for bridge implementation, valid values are: 'new' and 'old'.");
106 }
107
108 gboolean
109 sgen_is_bridge_object (GCObject *obj)
110 {
111         if ((obj->vtable->gc_bits & SGEN_GC_BIT_BRIDGE_OBJECT) != SGEN_GC_BIT_BRIDGE_OBJECT)
112                 return FALSE;
113         return bridge_callbacks.is_bridge_object (obj);
114 }
115
116 gboolean
117 sgen_need_bridge_processing (void)
118 {
119         return bridge_callbacks.cross_references != NULL;
120 }
121
122 static gboolean
123 compare_bridge_processors (void)
124 {
125         return compare_to_bridge_processor.reset_data != NULL;
126 }
127
128 /* Dispatch wrappers */
129 void
130 sgen_bridge_reset_data (void)
131 {
132         bridge_processor.reset_data ();
133         if (compare_bridge_processors ())
134                 compare_to_bridge_processor.reset_data ();
135 }
136
137 void
138 sgen_bridge_processing_stw_step (void)
139 {
140         /*
141          * bridge_processing_in_progress must be set with the world
142          * stopped.  If not there would be race conditions.
143          */
144         bridge_processing_in_progress = TRUE;
145
146         bridge_processor.processing_stw_step ();
147         if (compare_bridge_processors ())
148                 compare_to_bridge_processor.processing_stw_step ();
149 }
150
151 static mono_bool
152 is_bridge_object_alive (GCObject *obj, void *data)
153 {
154         SgenHashTable *table = data;
155         unsigned char *value = sgen_hash_table_lookup (table, obj);
156         if (!value)
157                 return TRUE;
158         return *value;
159 }
160
161 static void
162 null_weak_links_to_dead_objects (SgenBridgeProcessor *processor, int generation)
163 {
164         int i, j;
165         int num_sccs = processor->num_sccs;
166         MonoGCBridgeSCC **api_sccs = processor->api_sccs;
167         SgenHashTable alive_hash = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE, INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY, 1, mono_aligned_addr_hash, NULL);
168
169         for (i = 0; i < num_sccs; ++i) {
170                 unsigned char alive = api_sccs [i]->is_alive ? 1 : 0;
171                 for (j = 0; j < api_sccs [i]->num_objs; ++j) {
172                         /* Build hash table for nulling weak links. */
173                         sgen_hash_table_replace (&alive_hash, api_sccs [i]->objs [j], &alive, NULL);
174
175                         /* Release for finalization those objects we no longer care. */
176                         if (!api_sccs [i]->is_alive)
177                                 sgen_mark_bridge_object (api_sccs [i]->objs [j]);
178                 }
179         }
180
181         /* Null weak links to dead objects. */
182         sgen_null_links_with_predicate (GENERATION_NURSERY, is_bridge_object_alive, &alive_hash);
183         if (generation == GENERATION_OLD)
184                 sgen_null_links_with_predicate (GENERATION_OLD, is_bridge_object_alive, &alive_hash);
185
186         sgen_hash_table_clean (&alive_hash);
187 }
188
189 static void
190 free_callback_data (SgenBridgeProcessor *processor)
191 {
192         int i;
193         int num_sccs = processor->num_sccs;
194         int num_xrefs = processor->num_xrefs;
195         MonoGCBridgeSCC **api_sccs = processor->api_sccs;
196         MonoGCBridgeXRef *api_xrefs = processor->api_xrefs;
197
198         for (i = 0; i < num_sccs; ++i) {
199                 sgen_free_internal_dynamic (api_sccs [i],
200                                 sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * api_sccs [i]->num_objs,
201                                 INTERNAL_MEM_BRIDGE_DATA);
202         }
203         sgen_free_internal_dynamic (api_sccs, sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA);
204
205         sgen_free_internal_dynamic (api_xrefs, sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA);
206
207         processor->num_sccs = 0;
208         processor->api_sccs = NULL;
209         processor->num_xrefs = 0;
210         processor->api_xrefs = NULL;
211 }
212
213 void
214 sgen_bridge_processing_finish (int generation)
215 {
216         bridge_processor.processing_build_callback_data (generation);
217         if (compare_bridge_processors ())
218                 compare_to_bridge_processor.processing_build_callback_data (generation);
219
220         if (bridge_processor.num_sccs == 0) {
221                 g_assert (bridge_processor.num_xrefs == 0);
222                 goto after_callback;
223         }
224
225         bridge_callbacks.cross_references (bridge_processor.num_sccs, bridge_processor.api_sccs,
226                         bridge_processor.num_xrefs, bridge_processor.api_xrefs);
227
228         if (compare_bridge_processors ())
229                 sgen_compare_bridge_processor_results (&bridge_processor, &compare_to_bridge_processor);
230
231         null_weak_links_to_dead_objects (&bridge_processor, generation);
232
233         free_callback_data (&bridge_processor);
234         if (compare_bridge_processors ())
235                 free_callback_data (&compare_to_bridge_processor);
236
237  after_callback:
238         bridge_processor.processing_after_callback (generation);
239         if (compare_bridge_processors ())
240                 compare_to_bridge_processor.processing_after_callback (generation);
241
242         bridge_processing_in_progress = FALSE;
243 }
244
245 MonoGCBridgeObjectKind
246 sgen_bridge_class_kind (MonoClass *class)
247 {
248         return bridge_processor.class_kind (class);
249 }
250
251 void
252 sgen_bridge_register_finalized_object (GCObject *obj)
253 {
254         bridge_processor.register_finalized_object (obj);
255         if (compare_bridge_processors ())
256                 compare_to_bridge_processor.register_finalized_object (obj);
257 }
258
259 void
260 sgen_bridge_describe_pointer (GCObject *obj)
261 {
262         if (bridge_processor.describe_pointer)
263                 bridge_processor.describe_pointer (obj);
264 }
265
266 static void
267 set_dump_prefix (const char *prefix)
268 {
269         if (!bridge_processor.set_dump_prefix) {
270                 fprintf (stderr, "Warning: Bridge implementation does not support dumping - ignoring.\n");
271                 return;
272         }
273
274         bridge_processor.set_dump_prefix (prefix);
275 }
276
277 /* Test support code */
278 static const char *bridge_class;
279
280 static MonoGCBridgeObjectKind
281 bridge_test_bridge_class_kind (MonoClass *class)
282 {
283         if (!strcmp (bridge_class, class->name))
284                 return GC_BRIDGE_TRANSPARENT_BRIDGE_CLASS;
285         return GC_BRIDGE_TRANSPARENT_CLASS;
286 }
287
288 static gboolean
289 bridge_test_is_bridge_object (MonoObject *object)
290 {
291         return TRUE;
292 }
293
294 static void
295 bridge_test_cross_reference (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
296 {
297         int i;
298         for (i = 0; i < num_sccs; ++i) {
299                 int j;
300         //      g_print ("--- SCC %d\n", i);
301                 for (j = 0; j < sccs [i]->num_objs; ++j) {
302         //              g_print ("  %s\n", sgen_safe_name (sccs [i]->objs [j]));
303                         if (i & 1) /*retain half of the bridged objects */
304                                 sccs [i]->is_alive = TRUE;
305                 }
306         }
307         for (i = 0; i < num_xrefs; ++i) {
308                 g_assert (xrefs [i].src_scc_index >= 0 && xrefs [i].src_scc_index < num_sccs);
309                 g_assert (xrefs [i].dst_scc_index >= 0 && xrefs [i].dst_scc_index < num_sccs);
310         //      g_print ("%d -> %d\n", xrefs [i].src_scc_index, xrefs [i].dst_scc_index);
311         }
312 }
313
314 static MonoClassField *mono_bridge_test_field;
315
316 enum {
317         BRIDGE_DEAD,
318         BRIDGE_ROOT,
319         BRIDGE_SAME_SCC,
320         BRIDGE_XREF,
321 };
322
323 static gboolean
324 test_scc (MonoGCBridgeSCC *scc, int i)
325 {
326         int status = BRIDGE_DEAD;
327         mono_field_get_value (scc->objs [i], mono_bridge_test_field, &status);
328         return status > 0;
329 }
330
331 static void
332 mark_scc (MonoGCBridgeSCC *scc, int value)
333 {
334         int i;
335         for (i = 0; i < scc->num_objs; ++i) {
336                 if (!test_scc (scc, i)) {
337                         int status = value;
338                         mono_field_set_value (scc->objs [i], mono_bridge_test_field, &status);
339                 }
340         }
341 }
342
343 static void
344 bridge_test_cross_reference2 (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
345 {
346         int i;
347         gboolean modified;
348
349         if (!mono_bridge_test_field) {
350                 mono_bridge_test_field = mono_class_get_field_from_name (mono_object_get_class (sccs[0]->objs [0]), "__test");
351                 g_assert (mono_bridge_test_field);
352         }
353
354         /*We mark all objects in a scc with live objects as reachable by scc*/
355         for (i = 0; i < num_sccs; ++i) {
356                 int j;
357                 gboolean live = FALSE;
358                 for (j = 0; j < sccs [i]->num_objs; ++j) {
359                         if (test_scc (sccs [i], j)) {
360                                 live = TRUE;
361                                 break;
362                         }
363                 }
364                 if (!live)
365                         continue;
366                 for (j = 0; j < sccs [i]->num_objs; ++j) {
367                         if (!test_scc (sccs [i], j)) {
368                                 int status = BRIDGE_SAME_SCC;
369                                 mono_field_set_value (sccs [i]->objs [j], mono_bridge_test_field, &status);
370                         }
371                 }
372         }
373
374         /*Now we mark the transitive closure of reachable objects from the xrefs*/
375         modified = TRUE;
376         while (modified) {
377                 modified = FALSE;
378                 /* Mark all objects that are brought to life due to xrefs*/
379                 for (i = 0; i < num_xrefs; ++i) {
380                         MonoGCBridgeXRef ref = xrefs [i];
381                         if (test_scc (sccs [ref.src_scc_index], 0) && !test_scc (sccs [ref.dst_scc_index], 0)) {
382                                 modified = TRUE;
383                                 mark_scc (sccs [ref.dst_scc_index], BRIDGE_XREF);
384                         }
385                 }
386         }
387
388         /* keep everything in memory, all we want to do is test persistence */
389         for (i = 0; i < num_sccs; ++i)
390                 sccs [i]->is_alive = TRUE;
391 }
392
393 static void
394 register_test_bridge_callbacks (const char *bridge_class_name)
395 {
396         MonoGCBridgeCallbacks callbacks;
397         callbacks.bridge_version = SGEN_BRIDGE_VERSION;
398         callbacks.bridge_class_kind = bridge_test_bridge_class_kind;
399         callbacks.is_bridge_object = bridge_test_is_bridge_object;
400         callbacks.cross_references = bridge_class_name[0] == '2' ? bridge_test_cross_reference2 : bridge_test_cross_reference;
401         mono_gc_register_bridge_callbacks (&callbacks);
402         bridge_class = bridge_class_name + (bridge_class_name[0] == '2' ? 1 : 0);
403 }
404
405 gboolean
406 sgen_bridge_handle_gc_debug (const char *opt)
407 {
408         if (g_str_has_prefix (opt, "bridge=")) {
409                 opt = strchr (opt, '=') + 1;
410                 register_test_bridge_callbacks (g_strdup (opt));
411         } else if (!strcmp (opt, "enable-bridge-accounting")) {
412                 bridge_processor.enable_accounting ();
413         } else if (g_str_has_prefix (opt, "bridge-dump=")) {
414                 char *prefix = strchr (opt, '=') + 1;
415                 set_dump_prefix (prefix);
416         } else if (g_str_has_prefix (opt, "bridge-compare-to=")) {
417                 const char *name = strchr (opt, '=') + 1;
418                 if (init_bridge_processor (&compare_to_bridge_processor, name)) {
419                         if (compare_to_bridge_processor.reset_data == bridge_processor.reset_data) {
420                                 g_warning ("Cannot compare bridge implementation to itself - ignoring.");
421                                 memset (&compare_to_bridge_processor, 0, sizeof (SgenBridgeProcessor));
422                         }
423                 } else {
424                         g_warning ("Invalid bridge implementation to compare against - ignoring.");
425                 }
426         } else {
427                 return FALSE;
428         }
429         return TRUE;
430 }
431
432 void
433 sgen_bridge_print_gc_debug_usage (void)
434 {
435         fprintf (stderr, "  bridge=<class-name>\n");
436         fprintf (stderr, "  enable-bridge-accounting\n");
437         fprintf (stderr, "  bridge-dump=<filename-prefix>\n");
438         fprintf (stderr, "  bridge-compare-to=<implementation>\n");
439 }
440
441 #endif