2 * sgen-gc.c: Simple generational GC.
5 * Paolo Molaro (lupus@ximian.com)
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * Copyright 2005-2011 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
11 * Thread start/stop adapted from Boehm's GC:
12 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
13 * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
14 * Copyright (c) 1998 by Fergus Henderson. All rights reserved.
15 * Copyright (c) 2000-2004 by Hewlett-Packard Company. All rights reserved.
17 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
18 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
20 * Permission is hereby granted to use or copy this program
21 * for any purpose, provided the above notices are retained on all copies.
22 * Permission to modify the code and to distribute modified code is granted,
23 * provided the above notices are retained, and a notice that the code was
24 * modified is included with the above copyright notice.
27 * Copyright 2001-2003 Ximian, Inc
28 * Copyright 2003-2010 Novell, Inc.
29 * Copyright 2011 Xamarin, Inc.
31 * Permission is hereby granted, free of charge, to any person obtaining
32 * a copy of this software and associated documentation files (the
33 * "Software"), to deal in the Software without restriction, including
34 * without limitation the rights to use, copy, modify, merge, publish,
35 * distribute, sublicense, and/or sell copies of the Software, and to
36 * permit persons to whom the Software is furnished to do so, subject to
37 * the following conditions:
39 * The above copyright notice and this permission notice shall be
40 * included in all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
45 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
46 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
47 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
48 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 * Important: allocation provides always zeroed memory, having to do
52 * a memset after allocation is deadly for performance.
53 * Memory usage at startup is currently as follows:
55 * 64 KB internal space
57 * We should provide a small memory config with half the sizes
59 * We currently try to make as few mono assumptions as possible:
60 * 1) 2-word header with no GC pointers in it (first vtable, second to store the
62 * 2) gc descriptor is the second word in the vtable (first word in the class)
63 * 3) 8 byte alignment is the minimum and enough (not true for special structures (SIMD), FIXME)
64 * 4) there is a function to get an object's size and the number of
65 * elements in an array.
66 * 5) we know the special way bounds are allocated for complex arrays
67 * 6) we know about proxies and how to treat them when domains are unloaded
69 * Always try to keep stack usage to a minimum: no recursive behaviour
70 * and no large stack allocs.
72 * General description.
73 * Objects are initially allocated in a nursery using a fast bump-pointer technique.
74 * When the nursery is full we start a nursery collection: this is performed with a
76 * When the old generation is full we start a copying GC of the old generation as well:
77 * this will be changed to mark&sweep with copying when fragmentation becomes to severe
78 * in the future. Maybe we'll even do both during the same collection like IMMIX.
80 * The things that complicate this description are:
81 * *) pinned objects: we can't move them so we need to keep track of them
82 * *) no precise info of the thread stacks and registers: we need to be able to
83 * quickly find the objects that may be referenced conservatively and pin them
84 * (this makes the first issues more important)
85 * *) large objects are too expensive to be dealt with using copying GC: we handle them
86 * with mark/sweep during major collections
87 * *) some objects need to not move even if they are small (interned strings, Type handles):
88 * we use mark/sweep for them, too: they are not allocated in the nursery, but inside
89 * PinnedChunks regions
95 *) we could have a function pointer in MonoClass to implement
96 customized write barriers for value types
98 *) investigate the stuff needed to advance a thread to a GC-safe
99 point (single-stepping, read from unmapped memory etc) and implement it.
100 This would enable us to inline allocations and write barriers, for example,
101 or at least parts of them, like the write barrier checks.
102 We may need this also for handling precise info on stacks, even simple things
103 as having uninitialized data on the stack and having to wait for the prolog
104 to zero it. Not an issue for the last frame that we scan conservatively.
105 We could always not trust the value in the slots anyway.
107 *) modify the jit to save info about references in stack locations:
108 this can be done just for locals as a start, so that at least
109 part of the stack is handled precisely.
111 *) test/fix endianess issues
113 *) Implement a card table as the write barrier instead of remembered
114 sets? Card tables are not easy to implement with our current
115 memory layout. We have several different kinds of major heap
116 objects: Small objects in regular blocks, small objects in pinned
117 chunks and LOS objects. If we just have a pointer we have no way
118 to tell which kind of object it points into, therefore we cannot
119 know where its card table is. The least we have to do to make
120 this happen is to get rid of write barriers for indirect stores.
123 *) Get rid of write barriers for indirect stores. We can do this by
124 telling the GC to wbarrier-register an object once we do an ldloca
125 or ldelema on it, and to unregister it once it's not used anymore
126 (it can only travel downwards on the stack). The problem with
127 unregistering is that it needs to happen eventually no matter
128 what, even if exceptions are thrown, the thread aborts, etc.
129 Rodrigo suggested that we could do only the registering part and
130 let the collector find out (pessimistically) when it's safe to
131 unregister, namely when the stack pointer of the thread that
132 registered the object is higher than it was when the registering
133 happened. This might make for a good first implementation to get
134 some data on performance.
136 *) Some sort of blacklist support? Blacklists is a concept from the
137 Boehm GC: if during a conservative scan we find pointers to an
138 area which we might use as heap, we mark that area as unusable, so
139 pointer retention by random pinning pointers is reduced.
141 *) experiment with max small object size (very small right now - 2kb,
142 because it's tied to the max freelist size)
144 *) add an option to mmap the whole heap in one chunk: it makes for many
145 simplifications in the checks (put the nursery at the top and just use a single
146 check for inclusion/exclusion): the issue this has is that on 32 bit systems it's
147 not flexible (too much of the address space may be used by default or we can't
148 increase the heap as needed) and we'd need a race-free mechanism to return memory
149 back to the system (mprotect(PROT_NONE) will still keep the memory allocated if it
150 was written to, munmap is needed, but the following mmap may not find the same segment
153 *) memzero the major fragments after restarting the world and optionally a smaller
156 *) investigate having fragment zeroing threads
158 *) separate locks for finalization and other minor stuff to reduce
161 *) try a different copying order to improve memory locality
163 *) a thread abort after a store but before the write barrier will
164 prevent the write barrier from executing
166 *) specialized dynamically generated markers/copiers
168 *) Dynamically adjust TLAB size to the number of threads. If we have
169 too many threads that do allocation, we might need smaller TLABs,
170 and we might get better performance with larger TLABs if we only
171 have a handful of threads. We could sum up the space left in all
172 assigned TLABs and if that's more than some percentage of the
173 nursery size, reduce the TLAB size.
175 *) Explore placing unreachable objects on unused nursery memory.
176 Instead of memset'ng a region to zero, place an int[] covering it.
177 A good place to start is add_nursery_frag. The tricky thing here is
178 placing those objects atomically outside of a collection.
180 *) Allocation should use asymmetric Dekker synchronization:
181 http://blogs.oracle.com/dave/resource/Asymmetric-Dekker-Synchronization.txt
182 This should help weak consistency archs.
190 #ifdef HAVE_PTHREAD_H
193 #ifdef HAVE_SEMAPHORE_H
194 #include <semaphore.h>
205 #define _XOPEN_SOURCE
208 #include "metadata/sgen-gc.h"
209 #include "metadata/metadata-internals.h"
210 #include "metadata/class-internals.h"
211 #include "metadata/gc-internal.h"
212 #include "metadata/object-internals.h"
213 #include "metadata/threads.h"
214 #include "metadata/sgen-cardtable.h"
215 #include "metadata/sgen-ssb.h"
216 #include "metadata/sgen-protocol.h"
217 #include "metadata/sgen-archdep.h"
218 #include "metadata/sgen-bridge.h"
219 #include "metadata/mono-gc.h"
220 #include "metadata/method-builder.h"
221 #include "metadata/profiler-private.h"
222 #include "metadata/monitor.h"
223 #include "metadata/threadpool-internals.h"
224 #include "metadata/mempool-internals.h"
225 #include "metadata/marshal.h"
226 #include "metadata/runtime.h"
227 #include "metadata/sgen-cardtable.h"
228 #include "metadata/sgen-pinning.h"
229 #include "metadata/sgen-workers.h"
230 #include "utils/mono-mmap.h"
231 #include "utils/mono-time.h"
232 #include "utils/mono-semaphore.h"
233 #include "utils/mono-counters.h"
234 #include "utils/mono-proclib.h"
235 #include "utils/mono-memory-model.h"
236 #include "utils/mono-logger-internal.h"
238 #include <mono/utils/mono-logger-internal.h>
239 #include <mono/utils/memcheck.h>
241 #if defined(__MACH__)
242 #include "utils/mach-support.h"
245 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
249 #include "mono/cil/opcode.def"
255 #undef pthread_create
257 #undef pthread_detach
260 * ######################################################################
261 * ######## Types and constants used by the GC.
262 * ######################################################################
265 /* 0 means not initialized, 1 is initialized, -1 means in progress */
266 static gint32 gc_initialized
= 0;
267 /* If set, do a minor collection before every X allocation */
268 guint32 collect_before_allocs
= 0;
269 /* If set, do a heap consistency check before each minor collection */
270 static gboolean consistency_check_at_minor_collection
= FALSE
;
271 /* If set, check that there are no references to the domain left at domain unload */
272 static gboolean xdomain_checks
= FALSE
;
273 /* If not null, dump the heap after each collection into this file */
274 static FILE *heap_dump_file
= NULL
;
275 /* If set, mark stacks conservatively, even if precise marking is possible */
276 static gboolean conservative_stack_mark
= FALSE
;
277 /* If set, do a plausibility check on the scan_starts before and after
279 static gboolean do_scan_starts_check
= FALSE
;
280 static gboolean nursery_collection_is_parallel
= FALSE
;
281 static gboolean disable_minor_collections
= FALSE
;
282 static gboolean disable_major_collections
= FALSE
;
283 gboolean do_pin_stats
= FALSE
;
284 static gboolean do_verify_nursery
= FALSE
;
285 static gboolean do_dump_nursery_content
= FALSE
;
287 #ifdef HEAVY_STATISTICS
288 static long long stat_objects_alloced
= 0;
289 static long long stat_bytes_alloced
= 0;
290 long long stat_objects_alloced_degraded
= 0;
291 long long stat_bytes_alloced_degraded
= 0;
292 static long long stat_bytes_alloced_los
= 0;
294 long long stat_copy_object_called_nursery
= 0;
295 long long stat_objects_copied_nursery
= 0;
296 long long stat_copy_object_called_major
= 0;
297 long long stat_objects_copied_major
= 0;
299 long long stat_scan_object_called_nursery
= 0;
300 long long stat_scan_object_called_major
= 0;
302 long long stat_nursery_copy_object_failed_from_space
= 0;
303 long long stat_nursery_copy_object_failed_forwarded
= 0;
304 long long stat_nursery_copy_object_failed_pinned
= 0;
306 static long long stat_store_remsets
= 0;
307 static long long stat_store_remsets_unique
= 0;
308 static long long stat_saved_remsets_1
= 0;
309 static long long stat_saved_remsets_2
= 0;
310 static long long stat_local_remsets_processed
= 0;
311 static long long stat_global_remsets_added
= 0;
312 static long long stat_global_remsets_readded
= 0;
313 static long long stat_global_remsets_processed
= 0;
314 static long long stat_global_remsets_discarded
= 0;
316 static int stat_wbarrier_set_field
= 0;
317 static int stat_wbarrier_set_arrayref
= 0;
318 static int stat_wbarrier_arrayref_copy
= 0;
319 static int stat_wbarrier_generic_store
= 0;
320 static int stat_wbarrier_generic_store_remset
= 0;
321 static int stat_wbarrier_set_root
= 0;
322 static int stat_wbarrier_value_copy
= 0;
323 static int stat_wbarrier_object_copy
= 0;
326 int stat_minor_gcs
= 0;
327 int stat_major_gcs
= 0;
329 static long long stat_pinned_objects
= 0;
331 static long long time_minor_pre_collection_fragment_clear
= 0;
332 static long long time_minor_pinning
= 0;
333 static long long time_minor_scan_remsets
= 0;
334 static long long time_minor_scan_card_table
= 0;
335 static long long time_minor_scan_pinned
= 0;
336 static long long time_minor_scan_registered_roots
= 0;
337 static long long time_minor_scan_thread_data
= 0;
338 static long long time_minor_finish_gray_stack
= 0;
339 static long long time_minor_fragment_creation
= 0;
341 static long long time_major_pre_collection_fragment_clear
= 0;
342 static long long time_major_pinning
= 0;
343 static long long time_major_scan_pinned
= 0;
344 static long long time_major_scan_registered_roots
= 0;
345 static long long time_major_scan_thread_data
= 0;
346 static long long time_major_scan_alloc_pinned
= 0;
347 static long long time_major_scan_finalized
= 0;
348 static long long time_major_scan_big_objects
= 0;
349 static long long time_major_finish_gray_stack
= 0;
350 static long long time_major_free_bigobjs
= 0;
351 static long long time_major_los_sweep
= 0;
352 static long long time_major_sweep
= 0;
353 static long long time_major_fragment_creation
= 0;
355 int gc_debug_level
= 0;
357 static gboolean debug_print_allowance
= FALSE
;
361 mono_gc_flush_info (void)
363 fflush (gc_debug_file);
368 * Define this to allow the user to change the nursery size by
369 * specifying its value in the MONO_GC_PARAMS environmental
370 * variable. See mono_gc_base_init for details.
372 #define USER_CONFIG 1
374 #define TV_DECLARE SGEN_TV_DECLARE
375 #define TV_GETTIME SGEN_TV_GETTIME
376 #define TV_ELAPSED SGEN_TV_ELAPSED
377 #define TV_ELAPSED_MS SGEN_TV_ELAPSED_MS
379 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
381 NurseryClearPolicy nursery_clear_policy
= CLEAR_AT_TLAB_CREATION
;
383 /* the runtime can register areas of memory as roots: we keep two lists of roots,
384 * a pinned root set for conservatively scanned roots and a normal one for
385 * precisely scanned roots (currently implemented as a single list).
387 typedef struct _RootRecord RootRecord
;
393 #ifdef HAVE_KW_THREAD
394 __thread RememberedSet
*remembered_set MONO_TLS_FAST
;
396 static MonoNativeTlsKey remembered_set_key
;
397 RememberedSet
*global_remset
;
398 RememberedSet
*freed_thread_remsets
;
399 GenericStoreRememberedSet
*generic_store_remsets
= NULL
;
401 /* FIXME: later choose a size that takes into account the RememberedSet struct
402 * and doesn't waste any alloc paddin space.
404 #define DEFAULT_REMSET_SIZE 1024
405 static RememberedSet
* alloc_remset (int size
, gpointer id
, gboolean global
);
407 #define object_is_forwarded SGEN_OBJECT_IS_FORWARDED
408 #define object_is_pinned SGEN_OBJECT_IS_PINNED
409 #define pin_object SGEN_PIN_OBJECT
410 #define unpin_object SGEN_UNPIN_OBJECT
412 #define ptr_in_nursery(p) (SGEN_PTR_IN_NURSERY ((p), DEFAULT_NURSERY_BITS, nursery_start, nursery_end))
414 #define LOAD_VTABLE SGEN_LOAD_VTABLE
417 safe_name (void* obj
)
419 MonoVTable
*vt
= (MonoVTable
*)LOAD_VTABLE (obj
);
420 return vt
->klass
->name
;
423 #define safe_object_get_size mono_sgen_safe_object_get_size
426 mono_sgen_safe_name (void* obj
)
428 return safe_name (obj
);
432 * ######################################################################
433 * ######## Global data.
434 * ######################################################################
436 LOCK_DECLARE (gc_mutex
);
437 static int gc_disabled
= 0;
439 gboolean use_cardtable
;
443 /* good sizes are 512KB-1MB: larger ones increase a lot memzeroing time */
444 #define DEFAULT_NURSERY_SIZE (default_nursery_size)
445 int default_nursery_size
= (1 << 22);
446 #ifdef SGEN_ALIGN_NURSERY
447 /* The number of trailing 0 bits in DEFAULT_NURSERY_SIZE */
448 #define DEFAULT_NURSERY_BITS (default_nursery_bits)
449 static int default_nursery_bits
= 22;
454 #define DEFAULT_NURSERY_SIZE (4*1024*1024)
455 #ifdef SGEN_ALIGN_NURSERY
456 #define DEFAULT_NURSERY_BITS 22
461 #ifndef SGEN_ALIGN_NURSERY
462 #define DEFAULT_NURSERY_BITS -1
465 #define MIN_MINOR_COLLECTION_ALLOWANCE (DEFAULT_NURSERY_SIZE * 4)
467 #define SCAN_START_SIZE SGEN_SCAN_START_SIZE
469 static mword pagesize
= 4096;
470 static mword nursery_size
;
471 int degraded_mode
= 0;
473 static mword bytes_pinned_from_failed_allocation
= 0;
475 static mword total_alloc
= 0;
476 /* use this to tune when to do a major/minor collection */
477 static mword memory_pressure
= 0;
478 static mword minor_collection_allowance
;
479 static int minor_collection_sections_alloced
= 0;
482 /* GC Logging stats */
483 static int last_major_num_sections
= 0;
484 static int last_los_memory_usage
= 0;
485 static gboolean major_collection_happened
= FALSE
;
487 GCMemSection
*nursery_section
= NULL
;
488 static mword lowest_heap_address
= ~(mword
)0;
489 static mword highest_heap_address
= 0;
491 static LOCK_DECLARE (interruption_mutex
);
492 static LOCK_DECLARE (pin_queue_mutex
);
494 #define LOCK_PIN_QUEUE mono_mutex_lock (&pin_queue_mutex)
495 #define UNLOCK_PIN_QUEUE mono_mutex_unlock (&pin_queue_mutex)
497 typedef struct _FinalizeReadyEntry FinalizeReadyEntry
;
498 struct _FinalizeReadyEntry
{
499 FinalizeReadyEntry
*next
;
503 typedef struct _EphemeronLinkNode EphemeronLinkNode
;
505 struct _EphemeronLinkNode
{
506 EphemeronLinkNode
*next
;
515 int current_collection_generation
= -1;
518 * The link pointer is hidden by negating each bit. We use the lowest
519 * bit of the link (before negation) to store whether it needs
520 * resurrection tracking.
522 #define HIDE_POINTER(p,t) ((gpointer)(~((gulong)(p)|((t)?1:0))))
523 #define REVEAL_POINTER(p) ((gpointer)((~(gulong)(p))&~3L))
525 /* objects that are ready to be finalized */
526 static FinalizeReadyEntry
*fin_ready_list
= NULL
;
527 static FinalizeReadyEntry
*critical_fin_list
= NULL
;
529 static EphemeronLinkNode
*ephemeron_list
;
531 static int num_ready_finalizers
= 0;
532 static int no_finalize
= 0;
535 ROOT_TYPE_NORMAL
= 0, /* "normal" roots */
536 ROOT_TYPE_PINNED
= 1, /* roots without a GC descriptor */
537 ROOT_TYPE_WBARRIER
= 2, /* roots with a write barrier */
541 /* registered roots: the key to the hash is the root start address */
543 * Different kinds of roots are kept separate to speed up pin_from_roots () for example.
545 static SgenHashTable roots_hash
[ROOT_TYPE_NUM
] = {
546 SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE
, INTERNAL_MEM_ROOT_RECORD
, sizeof (RootRecord
), mono_aligned_addr_hash
, NULL
),
547 SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE
, INTERNAL_MEM_ROOT_RECORD
, sizeof (RootRecord
), mono_aligned_addr_hash
, NULL
),
548 SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE
, INTERNAL_MEM_ROOT_RECORD
, sizeof (RootRecord
), mono_aligned_addr_hash
, NULL
)
550 static mword roots_size
= 0; /* amount of memory in the root set */
552 #define GC_ROOT_NUM 32
555 void *objects
[GC_ROOT_NUM
];
556 int root_types
[GC_ROOT_NUM
];
557 uintptr_t extra_info
[GC_ROOT_NUM
];
561 notify_gc_roots (GCRootReport
*report
)
565 mono_profiler_gc_roots (report
->count
, report
->objects
, report
->root_types
, report
->extra_info
);
570 add_profile_gc_root (GCRootReport
*report
, void *object
, int rtype
, uintptr_t extra_info
)
572 if (report
->count
== GC_ROOT_NUM
)
573 notify_gc_roots (report
);
574 report
->objects
[report
->count
] = object
;
575 report
->root_types
[report
->count
] = rtype
;
576 report
->extra_info
[report
->count
++] = (uintptr_t)((MonoVTable
*)LOAD_VTABLE (object
))->klass
;
580 * The current allocation cursors
581 * We allocate objects in the nursery.
582 * The nursery is the area between nursery_start and nursery_end.
583 * nursery_frag_real_end points to the end of the currently used nursery fragment.
584 * nursery_first_pinned_start points to the start of the first pinned object in the nursery
585 * nursery_last_pinned_end points to the end of the last pinned object in the nursery
586 * At the next allocation, the area of the nursery where objects can be present is
587 * between MIN(nursery_first_pinned_start, first_fragment_start) and
588 * MAX(nursery_last_pinned_end, nursery_frag_real_end)
590 static char *nursery_start
= NULL
;
591 static char *nursery_end
= NULL
;
592 static char *nursery_alloc_bound
= NULL
;
594 MonoNativeTlsKey thread_info_key
;
596 #ifdef HAVE_KW_THREAD
597 __thread SgenThreadInfo
*thread_info
;
598 __thread gpointer
*store_remset_buffer
;
599 __thread
long store_remset_buffer_index
;
600 __thread
char *stack_end
;
601 __thread
long *store_remset_buffer_index_addr
;
604 /* The size of a TLAB */
605 /* The bigger the value, the less often we have to go to the slow path to allocate a new
606 * one, but the more space is wasted by threads not allocating much memory.
608 * FIXME: Make this self-tuning for each thread.
610 guint32 tlab_size
= (1024 * 4);
612 #define MAX_SMALL_OBJ_SIZE SGEN_MAX_SMALL_OBJ_SIZE
614 /* Functions supplied by the runtime to be called by the GC */
615 static MonoGCCallbacks gc_callbacks
;
617 #define ALLOC_ALIGN SGEN_ALLOC_ALIGN
618 #define ALLOC_ALIGN_BITS SGEN_ALLOC_ALIGN_BITS
620 #define ALIGN_UP SGEN_ALIGN_UP
622 #define MOVED_OBJECTS_NUM 64
623 static void *moved_objects
[MOVED_OBJECTS_NUM
];
624 static int moved_objects_idx
= 0;
626 /* Vtable of the objects used to fill out nursery fragments before a collection */
627 static MonoVTable
*array_fill_vtable
;
629 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
630 MonoNativeThreadId main_gc_thread
= NULL
;
634 * ######################################################################
635 * ######## Heap size accounting
636 * ######################################################################
639 static mword max_heap_size
= ((mword
)0)- ((mword
)1);
640 static mword soft_heap_limit
= ((mword
)0) - ((mword
)1);
641 static mword allocated_heap
;
643 /*Object was pinned during the current collection*/
644 static mword objects_pinned
;
647 mono_sgen_release_space (mword size
, int space
)
649 allocated_heap
-= size
;
653 available_free_space (void)
655 return max_heap_size
- MIN (allocated_heap
, max_heap_size
);
659 mono_sgen_try_alloc_space (mword size
, int space
)
661 if (available_free_space () < size
)
664 allocated_heap
+= size
;
665 mono_runtime_resource_check_limit (MONO_RESOURCE_GC_HEAP
, allocated_heap
);
670 init_heap_size_limits (glong max_heap
, glong soft_limit
)
673 soft_heap_limit
= soft_limit
;
678 if (max_heap
< soft_limit
) {
679 fprintf (stderr
, "max-heap-size must be at least as large as soft-heap-limit.\n");
683 if (max_heap
< nursery_size
* 4) {
684 fprintf (stderr
, "max-heap-size must be at least 4 times larger than nursery size.\n");
687 max_heap_size
= max_heap
- nursery_size
;
691 * ######################################################################
692 * ######## Macros and function declarations.
693 * ######################################################################
697 align_pointer (void *ptr
)
699 mword p
= (mword
)ptr
;
700 p
+= sizeof (gpointer
) - 1;
701 p
&= ~ (sizeof (gpointer
) - 1);
705 typedef SgenGrayQueue GrayQueue
;
707 /* forward declarations */
708 static int stop_world (int generation
);
709 static int restart_world (int generation
);
710 static void scan_thread_data (void *start_nursery
, void *end_nursery
, gboolean precise
, GrayQueue
*queue
);
711 static void scan_from_registered_roots (CopyOrMarkObjectFunc copy_func
, char *addr_start
, char *addr_end
, int root_type
, GrayQueue
*queue
);
712 static void scan_finalizer_entries (CopyOrMarkObjectFunc copy_func
, FinalizeReadyEntry
*list
, GrayQueue
*queue
);
713 static void report_finalizer_roots (void);
714 static void report_registered_roots (void);
715 static void find_pinning_ref_from_thread (char *obj
, size_t size
);
716 static void update_current_thread_stack (void *start
);
717 static void collect_bridge_objects (CopyOrMarkObjectFunc copy_func
, char *start
, char *end
, int generation
, GrayQueue
*queue
);
718 static void finalize_in_range (CopyOrMarkObjectFunc copy_func
, char *start
, char *end
, int generation
, GrayQueue
*queue
);
719 static void process_fin_stage_entries (void);
720 static void null_link_in_range (CopyOrMarkObjectFunc copy_func
, char *start
, char *end
, int generation
, gboolean before_finalization
, GrayQueue
*queue
);
721 static void null_links_for_domain (MonoDomain
*domain
, int generation
);
722 static void process_dislink_stage_entries (void);
724 static void pin_from_roots (void *start_nursery
, void *end_nursery
, GrayQueue
*queue
);
725 static int pin_objects_from_addresses (GCMemSection
*section
, void **start
, void **end
, void *start_nursery
, void *end_nursery
, GrayQueue
*queue
);
726 static void finish_gray_stack (char *start_addr
, char *end_addr
, int generation
, GrayQueue
*queue
);
727 static gboolean
need_major_collection (mword space_needed
);
728 static void major_collection (const char *reason
);
730 static void mono_gc_register_disappearing_link (MonoObject
*obj
, void **link
, gboolean track
, gboolean in_gc
);
731 static gboolean
mono_gc_is_critical_method (MonoMethod
*method
);
733 void mono_gc_scan_for_specific_ref (MonoObject
*key
, gboolean precise
);
735 static void init_stats (void);
737 static int mark_ephemerons_in_range (CopyOrMarkObjectFunc copy_func
, char *start
, char *end
, GrayQueue
*queue
);
738 static void clear_unreachable_ephemerons (CopyOrMarkObjectFunc copy_func
, char *start
, char *end
, GrayQueue
*queue
);
739 static void null_ephemerons_for_domain (MonoDomain
*domain
);
741 SgenMajorCollector major_collector
;
742 static GrayQueue gray_queue
;
746 #define WORKERS_DISTRIBUTE_GRAY_QUEUE (mono_sgen_collection_is_parallel () ? mono_sgen_workers_get_distribute_gray_queue () : &gray_queue)
748 static SgenGrayQueue
*
749 mono_sgen_workers_get_job_gray_queue (WorkerData
*worker_data
)
751 return worker_data
? &worker_data
->private_gray_queue
: WORKERS_DISTRIBUTE_GRAY_QUEUE
;
755 is_xdomain_ref_allowed (gpointer
*ptr
, char *obj
, MonoDomain
*domain
)
757 MonoObject
*o
= (MonoObject
*)(obj
);
758 MonoObject
*ref
= (MonoObject
*)*(ptr
);
759 int offset
= (char*)(ptr
) - (char*)o
;
761 if (o
->vtable
->klass
== mono_defaults
.thread_class
&& offset
== G_STRUCT_OFFSET (MonoThread
, internal_thread
))
763 if (o
->vtable
->klass
== mono_defaults
.internal_thread_class
&& offset
== G_STRUCT_OFFSET (MonoInternalThread
, current_appcontext
))
765 if (mono_class_has_parent_fast (o
->vtable
->klass
, mono_defaults
.real_proxy_class
) &&
766 offset
== G_STRUCT_OFFSET (MonoRealProxy
, unwrapped_server
))
768 /* Thread.cached_culture_info */
769 if (!strcmp (ref
->vtable
->klass
->name_space
, "System.Globalization") &&
770 !strcmp (ref
->vtable
->klass
->name
, "CultureInfo") &&
771 !strcmp(o
->vtable
->klass
->name_space
, "System") &&
772 !strcmp(o
->vtable
->klass
->name
, "Object[]"))
775 * at System.IO.MemoryStream.InternalConstructor (byte[],int,int,bool,bool) [0x0004d] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.IO/MemoryStream.cs:121
776 * at System.IO.MemoryStream..ctor (byte[]) [0x00017] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.IO/MemoryStream.cs:81
777 * at (wrapper remoting-invoke-with-check) System.IO.MemoryStream..ctor (byte[]) <IL 0x00020, 0xffffffff>
778 * at System.Runtime.Remoting.Messaging.CADMethodCallMessage.GetArguments () [0x0000d] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Messaging/CADMessages.cs:327
779 * at System.Runtime.Remoting.Messaging.MethodCall..ctor (System.Runtime.Remoting.Messaging.CADMethodCallMessage) [0x00017] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Messaging/MethodCall.cs:87
780 * at System.AppDomain.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage,byte[]&,System.Runtime.Remoting.Messaging.CADMethodReturnMessage&) [0x00018] in /home/schani/Work/novell/trunk/mcs/class/corlib/System/AppDomain.cs:1213
781 * at (wrapper remoting-invoke-with-check) System.AppDomain.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage,byte[]&,System.Runtime.Remoting.Messaging.CADMethodReturnMessage&) <IL 0x0003d, 0xffffffff>
782 * at System.Runtime.Remoting.Channels.CrossAppDomainSink.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage) [0x00008] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Channels/CrossAppDomainChannel.cs:198
783 * at (wrapper runtime-invoke) object.runtime_invoke_CrossAppDomainSink/ProcessMessageRes_object_object (object,intptr,intptr,intptr) <IL 0x0004c, 0xffffffff>
785 if (!strcmp (ref
->vtable
->klass
->name_space
, "System") &&
786 !strcmp (ref
->vtable
->klass
->name
, "Byte[]") &&
787 !strcmp (o
->vtable
->klass
->name_space
, "System.IO") &&
788 !strcmp (o
->vtable
->klass
->name
, "MemoryStream"))
790 /* append_job() in threadpool.c */
791 if (!strcmp (ref
->vtable
->klass
->name_space
, "System.Runtime.Remoting.Messaging") &&
792 !strcmp (ref
->vtable
->klass
->name
, "AsyncResult") &&
793 !strcmp (o
->vtable
->klass
->name_space
, "System") &&
794 !strcmp (o
->vtable
->klass
->name
, "Object[]") &&
795 mono_thread_pool_is_queue_array ((MonoArray
*) o
))
801 check_reference_for_xdomain (gpointer
*ptr
, char *obj
, MonoDomain
*domain
)
803 MonoObject
*o
= (MonoObject
*)(obj
);
804 MonoObject
*ref
= (MonoObject
*)*(ptr
);
805 int offset
= (char*)(ptr
) - (char*)o
;
807 MonoClassField
*field
;
810 if (!ref
|| ref
->vtable
->domain
== domain
)
812 if (is_xdomain_ref_allowed (ptr
, obj
, domain
))
816 for (class = o
->vtable
->klass
; class; class = class->parent
) {
819 for (i
= 0; i
< class->field
.count
; ++i
) {
820 if (class->fields
[i
].offset
== offset
) {
821 field
= &class->fields
[i
];
829 if (ref
->vtable
->klass
== mono_defaults
.string_class
)
830 str
= mono_string_to_utf8 ((MonoString
*)ref
);
833 g_print ("xdomain reference in %p (%s.%s) at offset %d (%s) to %p (%s.%s) (%s) - pointed to by:\n",
834 o
, o
->vtable
->klass
->name_space
, o
->vtable
->klass
->name
,
835 offset
, field
? field
->name
: "",
836 ref
, ref
->vtable
->klass
->name_space
, ref
->vtable
->klass
->name
, str
? str
: "");
837 mono_gc_scan_for_specific_ref (o
, TRUE
);
843 #define HANDLE_PTR(ptr,obj) check_reference_for_xdomain ((ptr), (obj), domain)
846 scan_object_for_xdomain_refs (char *start
, mword size
, void *data
)
848 MonoDomain
*domain
= ((MonoObject
*)start
)->vtable
->domain
;
850 #include "sgen-scan-object.h"
853 static gboolean scan_object_for_specific_ref_precise
= TRUE
;
856 #define HANDLE_PTR(ptr,obj) do { \
857 if ((MonoObject*)*(ptr) == key) { \
858 g_print ("found ref to %p in object %p (%s) at offset %td\n", \
859 key, (obj), safe_name ((obj)), ((char*)(ptr) - (char*)(obj))); \
864 scan_object_for_specific_ref (char *start
, MonoObject
*key
)
868 if ((forwarded
= SGEN_OBJECT_IS_FORWARDED (start
)))
871 if (scan_object_for_specific_ref_precise
) {
872 #include "sgen-scan-object.h"
874 mword
*words
= (mword
*)start
;
875 size_t size
= safe_object_get_size ((MonoObject
*)start
);
877 for (i
= 0; i
< size
/ sizeof (mword
); ++i
) {
878 if (words
[i
] == (mword
)key
) {
879 g_print ("found possible ref to %p in object %p (%s) at offset %td\n",
880 key
, start
, safe_name (start
), i
* sizeof (mword
));
887 mono_sgen_scan_area_with_callback (char *start
, char *end
, IterateObjectCallbackFunc callback
, void *data
, gboolean allow_flags
)
889 while (start
< end
) {
893 if (!*(void**)start
) {
894 start
+= sizeof (void*); /* should be ALLOC_ALIGN, really */
899 if (!(obj
= SGEN_OBJECT_IS_FORWARDED (start
)))
905 size
= ALIGN_UP (safe_object_get_size ((MonoObject
*)obj
));
907 callback (obj
, size
, data
);
914 scan_object_for_specific_ref_callback (char *obj
, size_t size
, MonoObject
*key
)
916 scan_object_for_specific_ref (obj
, key
);
920 check_root_obj_specific_ref (RootRecord
*root
, MonoObject
*key
, MonoObject
*obj
)
924 g_print ("found ref to %p in root record %p\n", key
, root
);
927 static MonoObject
*check_key
= NULL
;
928 static RootRecord
*check_root
= NULL
;
931 check_root_obj_specific_ref_from_marker (void **obj
)
933 check_root_obj_specific_ref (check_root
, check_key
, *obj
);
937 scan_roots_for_specific_ref (MonoObject
*key
, int root_type
)
943 SGEN_HASH_TABLE_FOREACH (&roots_hash
[root_type
], start_root
, root
) {
944 mword desc
= root
->root_desc
;
948 switch (desc
& ROOT_DESC_TYPE_MASK
) {
949 case ROOT_DESC_BITMAP
:
950 desc
>>= ROOT_DESC_TYPE_SHIFT
;
953 check_root_obj_specific_ref (root
, key
, *start_root
);
958 case ROOT_DESC_COMPLEX
: {
959 gsize
*bitmap_data
= mono_sgen_get_complex_descriptor_bitmap (desc
);
960 int bwords
= (*bitmap_data
) - 1;
961 void **start_run
= start_root
;
963 while (bwords
-- > 0) {
964 gsize bmap
= *bitmap_data
++;
965 void **objptr
= start_run
;
968 check_root_obj_specific_ref (root
, key
, *objptr
);
972 start_run
+= GC_BITS_PER_WORD
;
976 case ROOT_DESC_USER
: {
977 MonoGCRootMarkFunc marker
= mono_sgen_get_user_descriptor_func (desc
);
978 marker (start_root
, check_root_obj_specific_ref_from_marker
);
981 case ROOT_DESC_RUN_LEN
:
982 g_assert_not_reached ();
984 g_assert_not_reached ();
986 } SGEN_HASH_TABLE_FOREACH_END
;
993 mono_gc_scan_for_specific_ref (MonoObject
*key
, gboolean precise
)
998 scan_object_for_specific_ref_precise
= precise
;
1000 mono_sgen_scan_area_with_callback (nursery_section
->data
, nursery_section
->end_data
,
1001 (IterateObjectCallbackFunc
)scan_object_for_specific_ref_callback
, key
, TRUE
);
1003 major_collector
.iterate_objects (TRUE
, TRUE
, (IterateObjectCallbackFunc
)scan_object_for_specific_ref_callback
, key
);
1005 mono_sgen_los_iterate_objects ((IterateObjectCallbackFunc
)scan_object_for_specific_ref_callback
, key
);
1007 scan_roots_for_specific_ref (key
, ROOT_TYPE_NORMAL
);
1008 scan_roots_for_specific_ref (key
, ROOT_TYPE_WBARRIER
);
1010 SGEN_HASH_TABLE_FOREACH (&roots_hash
[ROOT_TYPE_PINNED
], ptr
, root
) {
1011 while (ptr
< (void**)root
->end_root
) {
1012 check_root_obj_specific_ref (root
, *ptr
, key
);
1015 } SGEN_HASH_TABLE_FOREACH_END
;
1019 need_remove_object_for_domain (char *start
, MonoDomain
*domain
)
1021 if (mono_object_domain (start
) == domain
) {
1022 DEBUG (4, fprintf (gc_debug_file
, "Need to cleanup object %p\n", start
));
1023 binary_protocol_cleanup (start
, (gpointer
)LOAD_VTABLE (start
), safe_object_get_size ((MonoObject
*)start
));
1030 process_object_for_domain_clearing (char *start
, MonoDomain
*domain
)
1032 GCVTable
*vt
= (GCVTable
*)LOAD_VTABLE (start
);
1033 if (vt
->klass
== mono_defaults
.internal_thread_class
)
1034 g_assert (mono_object_domain (start
) == mono_get_root_domain ());
1035 /* The object could be a proxy for an object in the domain
1037 if (mono_class_has_parent_fast (vt
->klass
, mono_defaults
.real_proxy_class
)) {
1038 MonoObject
*server
= ((MonoRealProxy
*)start
)->unwrapped_server
;
1040 /* The server could already have been zeroed out, so
1041 we need to check for that, too. */
1042 if (server
&& (!LOAD_VTABLE (server
) || mono_object_domain (server
) == domain
)) {
1043 DEBUG (4, fprintf (gc_debug_file
, "Cleaning up remote pointer in %p to object %p\n",
1045 ((MonoRealProxy
*)start
)->unwrapped_server
= NULL
;
1050 static MonoDomain
*check_domain
= NULL
;
1053 check_obj_not_in_domain (void **o
)
1055 g_assert (((MonoObject
*)(*o
))->vtable
->domain
!= check_domain
);
1059 scan_for_registered_roots_in_domain (MonoDomain
*domain
, int root_type
)
1063 check_domain
= domain
;
1064 SGEN_HASH_TABLE_FOREACH (&roots_hash
[root_type
], start_root
, root
) {
1065 mword desc
= root
->root_desc
;
1067 /* The MonoDomain struct is allowed to hold
1068 references to objects in its own domain. */
1069 if (start_root
== (void**)domain
)
1072 switch (desc
& ROOT_DESC_TYPE_MASK
) {
1073 case ROOT_DESC_BITMAP
:
1074 desc
>>= ROOT_DESC_TYPE_SHIFT
;
1076 if ((desc
& 1) && *start_root
)
1077 check_obj_not_in_domain (*start_root
);
1082 case ROOT_DESC_COMPLEX
: {
1083 gsize
*bitmap_data
= mono_sgen_get_complex_descriptor_bitmap (desc
);
1084 int bwords
= (*bitmap_data
) - 1;
1085 void **start_run
= start_root
;
1087 while (bwords
-- > 0) {
1088 gsize bmap
= *bitmap_data
++;
1089 void **objptr
= start_run
;
1091 if ((bmap
& 1) && *objptr
)
1092 check_obj_not_in_domain (*objptr
);
1096 start_run
+= GC_BITS_PER_WORD
;
1100 case ROOT_DESC_USER
: {
1101 MonoGCRootMarkFunc marker
= mono_sgen_get_user_descriptor_func (desc
);
1102 marker (start_root
, check_obj_not_in_domain
);
1105 case ROOT_DESC_RUN_LEN
:
1106 g_assert_not_reached ();
1108 g_assert_not_reached ();
1110 } SGEN_HASH_TABLE_FOREACH_END
;
1112 check_domain
= NULL
;
1116 check_for_xdomain_refs (void)
1120 mono_sgen_scan_area_with_callback (nursery_section
->data
, nursery_section
->end_data
,
1121 (IterateObjectCallbackFunc
)scan_object_for_xdomain_refs
, NULL
, FALSE
);
1123 major_collector
.iterate_objects (TRUE
, TRUE
, (IterateObjectCallbackFunc
)scan_object_for_xdomain_refs
, NULL
);
1125 for (bigobj
= los_object_list
; bigobj
; bigobj
= bigobj
->next
)
1126 scan_object_for_xdomain_refs (bigobj
->data
, bigobj
->size
, NULL
);
1130 clear_domain_process_object (char *obj
, MonoDomain
*domain
)
1134 process_object_for_domain_clearing (obj
, domain
);
1135 remove
= need_remove_object_for_domain (obj
, domain
);
1137 if (remove
&& ((MonoObject
*)obj
)->synchronisation
) {
1138 void **dislink
= mono_monitor_get_object_monitor_weak_link ((MonoObject
*)obj
);
1140 mono_gc_register_disappearing_link (NULL
, dislink
, FALSE
, TRUE
);
1147 clear_domain_process_minor_object_callback (char *obj
, size_t size
, MonoDomain
*domain
)
1149 if (clear_domain_process_object (obj
, domain
))
1150 memset (obj
, 0, size
);
1154 clear_domain_process_major_object_callback (char *obj
, size_t size
, MonoDomain
*domain
)
1156 clear_domain_process_object (obj
, domain
);
1160 clear_domain_free_major_non_pinned_object_callback (char *obj
, size_t size
, MonoDomain
*domain
)
1162 if (need_remove_object_for_domain (obj
, domain
))
1163 major_collector
.free_non_pinned_object (obj
, size
);
1167 clear_domain_free_major_pinned_object_callback (char *obj
, size_t size
, MonoDomain
*domain
)
1169 if (need_remove_object_for_domain (obj
, domain
))
1170 major_collector
.free_pinned_object (obj
, size
);
1174 * When appdomains are unloaded we can easily remove objects that have finalizers,
1175 * but all the others could still be present in random places on the heap.
1176 * We need a sweep to get rid of them even though it's going to be costly
1178 * The reason we need to remove them is because we access the vtable and class
1179 * structures to know the object size and the reference bitmap: once the domain is
1180 * unloaded the point to random memory.
1183 mono_gc_clear_domain (MonoDomain
* domain
)
1185 LOSObject
*bigobj
, *prev
;
1190 process_fin_stage_entries ();
1191 process_dislink_stage_entries ();
1193 mono_sgen_clear_nursery_fragments ();
1195 if (xdomain_checks
&& domain
!= mono_get_root_domain ()) {
1196 scan_for_registered_roots_in_domain (domain
, ROOT_TYPE_NORMAL
);
1197 scan_for_registered_roots_in_domain (domain
, ROOT_TYPE_WBARRIER
);
1198 check_for_xdomain_refs ();
1201 mono_sgen_scan_area_with_callback (nursery_section
->data
, nursery_section
->end_data
,
1202 (IterateObjectCallbackFunc
)clear_domain_process_minor_object_callback
, domain
, FALSE
);
1204 /*Ephemerons and dislinks must be processed before LOS since they might end up pointing
1205 to memory returned to the OS.*/
1206 null_ephemerons_for_domain (domain
);
1208 for (i
= GENERATION_NURSERY
; i
< GENERATION_MAX
; ++i
)
1209 null_links_for_domain (domain
, i
);
1211 /* We need two passes over major and large objects because
1212 freeing such objects might give their memory back to the OS
1213 (in the case of large objects) or obliterate its vtable
1214 (pinned objects with major-copying or pinned and non-pinned
1215 objects with major-mark&sweep), but we might need to
1216 dereference a pointer from an object to another object if
1217 the first object is a proxy. */
1218 major_collector
.iterate_objects (TRUE
, TRUE
, (IterateObjectCallbackFunc
)clear_domain_process_major_object_callback
, domain
);
1219 for (bigobj
= los_object_list
; bigobj
; bigobj
= bigobj
->next
)
1220 clear_domain_process_object (bigobj
->data
, domain
);
1223 for (bigobj
= los_object_list
; bigobj
;) {
1224 if (need_remove_object_for_domain (bigobj
->data
, domain
)) {
1225 LOSObject
*to_free
= bigobj
;
1227 prev
->next
= bigobj
->next
;
1229 los_object_list
= bigobj
->next
;
1230 bigobj
= bigobj
->next
;
1231 DEBUG (4, fprintf (gc_debug_file
, "Freeing large object %p\n",
1233 mono_sgen_los_free_object (to_free
);
1237 bigobj
= bigobj
->next
;
1239 major_collector
.iterate_objects (TRUE
, FALSE
, (IterateObjectCallbackFunc
)clear_domain_free_major_non_pinned_object_callback
, domain
);
1240 major_collector
.iterate_objects (FALSE
, TRUE
, (IterateObjectCallbackFunc
)clear_domain_free_major_pinned_object_callback
, domain
);
1242 if (G_UNLIKELY (do_pin_stats
)) {
1243 if (domain
== mono_get_root_domain ())
1244 mono_sgen_pin_stats_print_class_stats ();
1251 * mono_sgen_add_to_global_remset:
1253 * The global remset contains locations which point into newspace after
1254 * a minor collection. This can happen if the objects they point to are pinned.
1256 * LOCKING: If called from a parallel collector, the global remset
1257 * lock must be held. For serial collectors that is not necessary.
1260 mono_sgen_add_to_global_remset (gpointer ptr
)
1263 mono_sgen_card_table_record_pointer (ptr
);
1265 mono_sgen_ssb_record_pointer (ptr
);
1269 * mono_sgen_drain_gray_stack:
1271 * Scan objects in the gray stack until the stack is empty. This should be called
1272 * frequently after each object is copied, to achieve better locality and cache
1276 mono_sgen_drain_gray_stack (GrayQueue
*queue
, int max_objs
)
1280 if (current_collection_generation
== GENERATION_NURSERY
) {
1281 ScanObjectFunc scan_func
= mono_sgen_get_minor_scan_object ();
1284 GRAY_OBJECT_DEQUEUE (queue
, obj
);
1287 DEBUG (9, fprintf (gc_debug_file
, "Precise gray object scan %p (%s)\n", obj
, safe_name (obj
)));
1288 scan_func (obj
, queue
);
1293 if (mono_sgen_collection_is_parallel () && mono_sgen_workers_is_distributed_queue (queue
))
1297 for (i
= 0; i
!= max_objs
; ++i
) {
1298 GRAY_OBJECT_DEQUEUE (queue
, obj
);
1301 DEBUG (9, fprintf (gc_debug_file
, "Precise gray object scan %p (%s)\n", obj
, safe_name (obj
)));
1302 major_collector
.major_scan_object (obj
, queue
);
1304 } while (max_objs
< 0);
1310 * Addresses from start to end are already sorted. This function finds
1311 * the object header for each address and pins the object. The
1312 * addresses must be inside the passed section. The (start of the)
1313 * address array is overwritten with the addresses of the actually
1314 * pinned objects. Return the number of pinned objects.
1317 pin_objects_from_addresses (GCMemSection
*section
, void **start
, void **end
, void *start_nursery
, void *end_nursery
, GrayQueue
*queue
)
1322 void *last_obj
= NULL
;
1323 size_t last_obj_size
= 0;
1326 void **definitely_pinned
= start
;
1328 mono_sgen_nursery_allocator_prepare_for_pinning ();
1330 while (start
< end
) {
1332 /* the range check should be reduntant */
1333 if (addr
!= last
&& addr
>= start_nursery
&& addr
< end_nursery
) {
1334 DEBUG (5, fprintf (gc_debug_file
, "Considering pinning addr %p\n", addr
));
1335 /* multiple pointers to the same object */
1336 if (addr
>= last_obj
&& (char*)addr
< (char*)last_obj
+ last_obj_size
) {
1340 idx
= ((char*)addr
- (char*)section
->data
) / SCAN_START_SIZE
;
1341 g_assert (idx
< section
->num_scan_start
);
1342 search_start
= (void*)section
->scan_starts
[idx
];
1343 if (!search_start
|| search_start
> addr
) {
1346 search_start
= section
->scan_starts
[idx
];
1347 if (search_start
&& search_start
<= addr
)
1350 if (!search_start
|| search_start
> addr
)
1351 search_start
= start_nursery
;
1353 if (search_start
< last_obj
)
1354 search_start
= (char*)last_obj
+ last_obj_size
;
1355 /* now addr should be in an object a short distance from search_start
1356 * Note that search_start must point to zeroed mem or point to an object.
1360 if (!*(void**)search_start
) {
1361 /* Consistency check */
1363 for (frag = nursery_fragments; frag; frag = frag->next) {
1364 if (search_start >= frag->fragment_start && search_start < frag->fragment_end)
1365 g_assert_not_reached ();
1369 search_start
= (void*)ALIGN_UP ((mword
)search_start
+ sizeof (gpointer
));
1372 last_obj
= search_start
;
1373 last_obj_size
= ALIGN_UP (safe_object_get_size ((MonoObject
*)search_start
));
1375 if (((MonoObject
*)last_obj
)->synchronisation
== GINT_TO_POINTER (-1)) {
1376 /* Marks the beginning of a nursery fragment, skip */
1378 DEBUG (8, fprintf (gc_debug_file
, "Pinned try match %p (%s), size %zd\n", last_obj
, safe_name (last_obj
), last_obj_size
));
1379 if (addr
>= search_start
&& (char*)addr
< (char*)last_obj
+ last_obj_size
) {
1380 DEBUG (4, fprintf (gc_debug_file
, "Pinned object %p, vtable %p (%s), count %d\n", search_start
, *(void**)search_start
, safe_name (search_start
), count
));
1381 binary_protocol_pin (search_start
, (gpointer
)LOAD_VTABLE (search_start
), safe_object_get_size (search_start
));
1382 pin_object (search_start
);
1383 GRAY_OBJECT_ENQUEUE (queue
, search_start
);
1384 if (G_UNLIKELY (do_pin_stats
))
1385 mono_sgen_pin_stats_register_object (search_start
, last_obj_size
);
1386 definitely_pinned
[count
] = search_start
;
1391 /* skip to the next object */
1392 search_start
= (void*)((char*)search_start
+ last_obj_size
);
1393 } while (search_start
<= addr
);
1394 /* we either pinned the correct object or we ignored the addr because
1395 * it points to unused zeroed memory.
1401 //printf ("effective pinned: %d (at the end: %d)\n", count, (char*)end_nursery - (char*)last);
1402 if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS
) {
1403 GCRootReport report
;
1405 for (idx
= 0; idx
< count
; ++idx
)
1406 add_profile_gc_root (&report
, definitely_pinned
[idx
], MONO_PROFILE_GC_ROOT_PINNING
| MONO_PROFILE_GC_ROOT_MISC
, 0);
1407 notify_gc_roots (&report
);
1409 stat_pinned_objects
+= count
;
1414 mono_sgen_pin_objects_in_section (GCMemSection
*section
, GrayQueue
*queue
)
1416 int num_entries
= section
->pin_queue_num_entries
;
1418 void **start
= section
->pin_queue_start
;
1420 reduced_to
= pin_objects_from_addresses (section
, start
, start
+ num_entries
,
1421 section
->data
, section
->next_data
, queue
);
1422 section
->pin_queue_num_entries
= reduced_to
;
1424 section
->pin_queue_start
= NULL
;
1430 mono_sgen_pin_object (void *object
, GrayQueue
*queue
)
1432 if (mono_sgen_collection_is_parallel ()) {
1434 /*object arrives pinned*/
1435 mono_sgen_pin_stage_ptr (object
);
1439 SGEN_PIN_OBJECT (object
);
1440 mono_sgen_pin_stage_ptr (object
);
1442 if (G_UNLIKELY (do_pin_stats
))
1443 mono_sgen_pin_stats_register_object (object
, safe_object_get_size (object
));
1445 GRAY_OBJECT_ENQUEUE (queue
, object
);
1446 binary_protocol_pin (object
, (gpointer
)LOAD_VTABLE (object
), safe_object_get_size (object
));
1449 /* Sort the addresses in array in increasing order.
1450 * Done using a by-the book heap sort. Which has decent and stable performance, is pretty cache efficient.
1453 mono_sgen_sort_addresses (void **array
, int size
)
1458 for (i
= 1; i
< size
; ++i
) {
1461 int parent
= (child
- 1) / 2;
1463 if (array
[parent
] >= array
[child
])
1466 tmp
= array
[parent
];
1467 array
[parent
] = array
[child
];
1468 array
[child
] = tmp
;
1474 for (i
= size
- 1; i
> 0; --i
) {
1477 array
[i
] = array
[0];
1483 while (root
* 2 + 1 <= end
) {
1484 int child
= root
* 2 + 1;
1486 if (child
< end
&& array
[child
] < array
[child
+ 1])
1488 if (array
[root
] >= array
[child
])
1492 array
[root
] = array
[child
];
1493 array
[child
] = tmp
;
1501 * Scan the memory between start and end and queue values which could be pointers
1502 * to the area between start_nursery and end_nursery for later consideration.
1503 * Typically used for thread stacks.
1506 conservatively_pin_objects_from (void **start
, void **end
, void *start_nursery
, void *end_nursery
, int pin_type
)
1509 while (start
< end
) {
1510 if (*start
>= start_nursery
&& *start
< end_nursery
) {
1512 * *start can point to the middle of an object
1513 * note: should we handle pointing at the end of an object?
1514 * pinning in C# code disallows pointing at the end of an object
1515 * but there is some small chance that an optimizing C compiler
1516 * may keep the only reference to an object by pointing
1517 * at the end of it. We ignore this small chance for now.
1518 * Pointers to the end of an object are indistinguishable
1519 * from pointers to the start of the next object in memory
1520 * so if we allow that we'd need to pin two objects...
1521 * We queue the pointer in an array, the
1522 * array will then be sorted and uniqued. This way
1523 * we can coalesce several pinning pointers and it should
1524 * be faster since we'd do a memory scan with increasing
1525 * addresses. Note: we can align the address to the allocation
1526 * alignment, so the unique process is more effective.
1528 mword addr
= (mword
)*start
;
1529 addr
&= ~(ALLOC_ALIGN
- 1);
1530 if (addr
>= (mword
)start_nursery
&& addr
< (mword
)end_nursery
)
1531 mono_sgen_pin_stage_ptr ((void*)addr
);
1532 if (G_UNLIKELY (do_pin_stats
)) {
1533 if (ptr_in_nursery (addr
))
1534 mono_sgen_pin_stats_register_address ((char*)addr
, pin_type
);
1536 DEBUG (6, if (count
) fprintf (gc_debug_file
, "Pinning address %p from %p\n", (void*)addr
, start
));
1541 DEBUG (7, if (count
) fprintf (gc_debug_file
, "found %d potential pinned heap pointers\n", count
));
1545 * Debugging function: find in the conservative roots where @obj is being pinned.
1547 static G_GNUC_UNUSED
void
1548 find_pinning_reference (char *obj
, size_t size
)
1552 char *endobj
= obj
+ size
;
1554 SGEN_HASH_TABLE_FOREACH (&roots_hash
[ROOT_TYPE_NORMAL
], start
, root
) {
1555 /* if desc is non-null it has precise info */
1556 if (!root
->root_desc
) {
1557 while (start
< (char**)root
->end_root
) {
1558 if (*start
>= obj
&& *start
< endobj
) {
1559 DEBUG (0, fprintf (gc_debug_file
, "Object %p referenced in pinned roots %p-%p\n", obj
, start
, root
->end_root
));
1564 } SGEN_HASH_TABLE_FOREACH_END
;
1566 find_pinning_ref_from_thread (obj
, size
);
1570 * The first thing we do in a collection is to identify pinned objects.
1571 * This function considers all the areas of memory that need to be
1572 * conservatively scanned.
1575 pin_from_roots (void *start_nursery
, void *end_nursery
, GrayQueue
*queue
)
1579 DEBUG (2, fprintf (gc_debug_file
, "Scanning pinned roots (%d bytes, %d/%d entries)\n", (int)roots_size
, roots_hash
[ROOT_TYPE_NORMAL
].num_entries
, roots_hash
[ROOT_TYPE_PINNED
].num_entries
));
1580 /* objects pinned from the API are inside these roots */
1581 SGEN_HASH_TABLE_FOREACH (&roots_hash
[ROOT_TYPE_PINNED
], start_root
, root
) {
1582 DEBUG (6, fprintf (gc_debug_file
, "Pinned roots %p-%p\n", start_root
, root
->end_root
));
1583 conservatively_pin_objects_from (start_root
, (void**)root
->end_root
, start_nursery
, end_nursery
, PIN_TYPE_OTHER
);
1584 } SGEN_HASH_TABLE_FOREACH_END
;
1585 /* now deal with the thread stacks
1586 * in the future we should be able to conservatively scan only:
1587 * *) the cpu registers
1588 * *) the unmanaged stack frames
1589 * *) the _last_ managed stack frame
1590 * *) pointers slots in managed frames
1592 scan_thread_data (start_nursery
, end_nursery
, FALSE
, queue
);
1596 CopyOrMarkObjectFunc func
;
1598 } UserCopyOrMarkData
;
1600 static MonoNativeTlsKey user_copy_or_mark_key
;
1603 init_user_copy_or_mark_key (void)
1605 mono_native_tls_alloc (&user_copy_or_mark_key
, NULL
);
1609 set_user_copy_or_mark_data (UserCopyOrMarkData
*data
)
1611 mono_native_tls_set_value (user_copy_or_mark_key
, data
);
1615 single_arg_user_copy_or_mark (void **obj
)
1617 UserCopyOrMarkData
*data
= mono_native_tls_get_value (user_copy_or_mark_key
);
1619 data
->func (obj
, data
->queue
);
1623 * The memory area from start_root to end_root contains pointers to objects.
1624 * Their position is precisely described by @desc (this means that the pointer
1625 * can be either NULL or the pointer to the start of an object).
1626 * This functions copies them to to_space updates them.
1628 * This function is not thread-safe!
1631 precisely_scan_objects_from (CopyOrMarkObjectFunc copy_func
, void** start_root
, void** end_root
, char* n_start
, char *n_end
, mword desc
, GrayQueue
*queue
)
1633 switch (desc
& ROOT_DESC_TYPE_MASK
) {
1634 case ROOT_DESC_BITMAP
:
1635 desc
>>= ROOT_DESC_TYPE_SHIFT
;
1637 if ((desc
& 1) && *start_root
) {
1638 copy_func (start_root
, queue
);
1639 DEBUG (9, fprintf (gc_debug_file
, "Overwrote root at %p with %p\n", start_root
, *start_root
));
1640 mono_sgen_drain_gray_stack (queue
, -1);
1646 case ROOT_DESC_COMPLEX
: {
1647 gsize
*bitmap_data
= mono_sgen_get_complex_descriptor_bitmap (desc
);
1648 int bwords
= (*bitmap_data
) - 1;
1649 void **start_run
= start_root
;
1651 while (bwords
-- > 0) {
1652 gsize bmap
= *bitmap_data
++;
1653 void **objptr
= start_run
;
1655 if ((bmap
& 1) && *objptr
) {
1656 copy_func (objptr
, queue
);
1657 DEBUG (9, fprintf (gc_debug_file
, "Overwrote root at %p with %p\n", objptr
, *objptr
));
1658 mono_sgen_drain_gray_stack (queue
, -1);
1663 start_run
+= GC_BITS_PER_WORD
;
1667 case ROOT_DESC_USER
: {
1668 UserCopyOrMarkData data
= { copy_func
, queue
};
1669 MonoGCRootMarkFunc marker
= mono_sgen_get_user_descriptor_func (desc
);
1670 set_user_copy_or_mark_data (&data
);
1671 marker (start_root
, single_arg_user_copy_or_mark
);
1672 set_user_copy_or_mark_data (NULL
);
1675 case ROOT_DESC_RUN_LEN
:
1676 g_assert_not_reached ();
1678 g_assert_not_reached ();
1683 reset_heap_boundaries (void)
1685 lowest_heap_address
= ~(mword
)0;
1686 highest_heap_address
= 0;
1690 mono_sgen_update_heap_boundaries (mword low
, mword high
)
1695 old
= lowest_heap_address
;
1698 } while (SGEN_CAS_PTR ((gpointer
*)&lowest_heap_address
, (gpointer
)low
, (gpointer
)old
) != (gpointer
)old
);
1701 old
= highest_heap_address
;
1704 } while (SGEN_CAS_PTR ((gpointer
*)&highest_heap_address
, (gpointer
)high
, (gpointer
)old
) != (gpointer
)old
);
1707 static unsigned long
1708 prot_flags_for_activate (int activate
)
1710 unsigned long prot_flags
= activate
? MONO_MMAP_READ
|MONO_MMAP_WRITE
: MONO_MMAP_NONE
;
1711 return prot_flags
| MONO_MMAP_PRIVATE
| MONO_MMAP_ANON
;
1715 * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
1716 * This must not require any lock.
1719 mono_sgen_alloc_os_memory (size_t size
, int activate
)
1721 void *ptr
= mono_valloc (0, size
, prot_flags_for_activate (activate
));
1724 total_alloc
+= size
;
1729 /* size must be a power of 2 */
1731 mono_sgen_alloc_os_memory_aligned (mword size
, mword alignment
, gboolean activate
)
1733 void *ptr
= mono_valloc_aligned (size
, alignment
, prot_flags_for_activate (activate
));
1736 total_alloc
+= size
;
1742 * Free the memory returned by mono_sgen_alloc_os_memory (), returning it to the OS.
1745 mono_sgen_free_os_memory (void *addr
, size_t size
)
1747 mono_vfree (addr
, size
);
1749 total_alloc
-= size
;
1753 * Allocate and setup the data structures needed to be able to allocate objects
1754 * in the nursery. The nursery is stored in nursery_section.
1757 alloc_nursery (void)
1759 GCMemSection
*section
;
1764 if (nursery_section
)
1766 DEBUG (2, fprintf (gc_debug_file
, "Allocating nursery size: %lu\n", (unsigned long)nursery_size
));
1767 /* later we will alloc a larger area for the nursery but only activate
1768 * what we need. The rest will be used as expansion if we have too many pinned
1769 * objects in the existing nursery.
1771 /* FIXME: handle OOM */
1772 section
= mono_sgen_alloc_internal (INTERNAL_MEM_SECTION
);
1774 g_assert (nursery_size
== DEFAULT_NURSERY_SIZE
);
1775 alloc_size
= nursery_size
;
1776 #ifdef SGEN_ALIGN_NURSERY
1777 data
= major_collector
.alloc_heap (alloc_size
, alloc_size
, DEFAULT_NURSERY_BITS
);
1779 data
= major_collector
.alloc_heap (alloc_size
, 0, DEFAULT_NURSERY_BITS
);
1781 nursery_start
= data
;
1782 nursery_end
= nursery_start
+ nursery_size
;
1783 mono_sgen_update_heap_boundaries ((mword
)nursery_start
, (mword
)nursery_end
);
1784 DEBUG (4, fprintf (gc_debug_file
, "Expanding nursery size (%p-%p): %lu, total: %lu\n", data
, data
+ alloc_size
, (unsigned long)nursery_size
, (unsigned long)total_alloc
));
1785 section
->data
= section
->next_data
= data
;
1786 section
->size
= alloc_size
;
1787 section
->end_data
= nursery_end
;
1788 scan_starts
= (alloc_size
+ SCAN_START_SIZE
- 1) / SCAN_START_SIZE
;
1789 section
->scan_starts
= mono_sgen_alloc_internal_dynamic (sizeof (char*) * scan_starts
, INTERNAL_MEM_SCAN_STARTS
);
1790 section
->num_scan_start
= scan_starts
;
1791 section
->block
.role
= MEMORY_ROLE_GEN0
;
1792 section
->block
.next
= NULL
;
1794 nursery_section
= section
;
1796 mono_sgen_nursery_allocator_set_nursery_bounds (nursery_start
, nursery_end
);
1800 mono_gc_get_nursery (int *shift_bits
, size_t *size
)
1802 *size
= nursery_size
;
1803 #ifdef SGEN_ALIGN_NURSERY
1804 *shift_bits
= DEFAULT_NURSERY_BITS
;
1808 return nursery_start
;
1812 mono_gc_set_current_thread_appdomain (MonoDomain
*domain
)
1814 SgenThreadInfo
*info
= mono_thread_info_current ();
1816 /* Could be called from sgen_thread_unregister () with a NULL info */
1819 info
->stopped_domain
= domain
;
1824 mono_gc_precise_stack_mark_enabled (void)
1826 return !conservative_stack_mark
;
1830 mono_gc_get_logfile (void)
1832 return mono_sgen_get_logfile ();
1836 report_finalizer_roots_list (FinalizeReadyEntry
*list
)
1838 GCRootReport report
;
1839 FinalizeReadyEntry
*fin
;
1842 for (fin
= list
; fin
; fin
= fin
->next
) {
1845 add_profile_gc_root (&report
, fin
->object
, MONO_PROFILE_GC_ROOT_FINALIZER
, 0);
1847 notify_gc_roots (&report
);
1851 report_finalizer_roots (void)
1853 report_finalizer_roots_list (fin_ready_list
);
1854 report_finalizer_roots_list (critical_fin_list
);
1857 static GCRootReport
*root_report
;
1860 single_arg_report_root (void **obj
)
1863 add_profile_gc_root (root_report
, *obj
, MONO_PROFILE_GC_ROOT_OTHER
, 0);
1867 precisely_report_roots_from (GCRootReport
*report
, void** start_root
, void** end_root
, mword desc
)
1869 switch (desc
& ROOT_DESC_TYPE_MASK
) {
1870 case ROOT_DESC_BITMAP
:
1871 desc
>>= ROOT_DESC_TYPE_SHIFT
;
1873 if ((desc
& 1) && *start_root
) {
1874 add_profile_gc_root (report
, *start_root
, MONO_PROFILE_GC_ROOT_OTHER
, 0);
1880 case ROOT_DESC_COMPLEX
: {
1881 gsize
*bitmap_data
= mono_sgen_get_complex_descriptor_bitmap (desc
);
1882 int bwords
= (*bitmap_data
) - 1;
1883 void **start_run
= start_root
;
1885 while (bwords
-- > 0) {
1886 gsize bmap
= *bitmap_data
++;
1887 void **objptr
= start_run
;
1889 if ((bmap
& 1) && *objptr
) {
1890 add_profile_gc_root (report
, *objptr
, MONO_PROFILE_GC_ROOT_OTHER
, 0);
1895 start_run
+= GC_BITS_PER_WORD
;
1899 case ROOT_DESC_USER
: {
1900 MonoGCRootMarkFunc marker
= mono_sgen_get_user_descriptor_func (desc
);
1901 root_report
= report
;
1902 marker (start_root
, single_arg_report_root
);
1905 case ROOT_DESC_RUN_LEN
:
1906 g_assert_not_reached ();
1908 g_assert_not_reached ();
1913 report_registered_roots_by_type (int root_type
)
1915 GCRootReport report
;
1919 SGEN_HASH_TABLE_FOREACH (&roots_hash
[root_type
], start_root
, root
) {
1920 DEBUG (6, fprintf (gc_debug_file
, "Precise root scan %p-%p (desc: %p)\n", start_root
, root
->end_root
, (void*)root
->root_desc
));
1921 precisely_report_roots_from (&report
, start_root
, (void**)root
->end_root
, root
->root_desc
);
1922 } SGEN_HASH_TABLE_FOREACH_END
;
1923 notify_gc_roots (&report
);
1927 report_registered_roots (void)
1929 report_registered_roots_by_type (ROOT_TYPE_NORMAL
);
1930 report_registered_roots_by_type (ROOT_TYPE_WBARRIER
);
1934 scan_finalizer_entries (CopyOrMarkObjectFunc copy_func
, FinalizeReadyEntry
*list
, GrayQueue
*queue
)
1936 FinalizeReadyEntry
*fin
;
1938 for (fin
= list
; fin
; fin
= fin
->next
) {
1941 DEBUG (5, fprintf (gc_debug_file
, "Scan of fin ready object: %p (%s)\n", fin
->object
, safe_name (fin
->object
)));
1942 copy_func (&fin
->object
, queue
);
1947 generation_name (int generation
)
1949 switch (generation
) {
1950 case GENERATION_NURSERY
: return "nursery";
1951 case GENERATION_OLD
: return "old";
1952 default: g_assert_not_reached ();
1958 stw_bridge_process (void)
1960 mono_sgen_bridge_processing_stw_step ();
1964 bridge_process (void)
1966 mono_sgen_bridge_processing_finish ();
1969 CopyOrMarkObjectFunc
1970 mono_sgen_get_copy_object (void)
1972 if (current_collection_generation
== GENERATION_NURSERY
) {
1973 if (mono_sgen_collection_is_parallel ())
1974 return major_collector
.copy_object
;
1976 return major_collector
.nopar_copy_object
;
1978 return major_collector
.copy_or_mark_object
;
1983 mono_sgen_get_minor_scan_object (void)
1985 g_assert (current_collection_generation
== GENERATION_NURSERY
);
1987 if (mono_sgen_collection_is_parallel ())
1988 return major_collector
.minor_scan_object
;
1990 return major_collector
.nopar_minor_scan_object
;
1994 mono_sgen_get_minor_scan_vtype (void)
1996 g_assert (current_collection_generation
== GENERATION_NURSERY
);
1998 if (mono_sgen_collection_is_parallel ())
1999 return major_collector
.minor_scan_vtype
;
2001 return major_collector
.nopar_minor_scan_vtype
;
2005 finish_gray_stack (char *start_addr
, char *end_addr
, int generation
, GrayQueue
*queue
)
2010 int done_with_ephemerons
, ephemeron_rounds
= 0;
2012 CopyOrMarkObjectFunc copy_func
= mono_sgen_get_copy_object ();
2015 * We copied all the reachable objects. Now it's the time to copy
2016 * the objects that were not referenced by the roots, but by the copied objects.
2017 * we built a stack of objects pointed to by gray_start: they are
2018 * additional roots and we may add more items as we go.
2019 * We loop until gray_start == gray_objects which means no more objects have
2020 * been added. Note this is iterative: no recursion is involved.
2021 * We need to walk the LO list as well in search of marked big objects
2022 * (use a flag since this is needed only on major collections). We need to loop
2023 * here as well, so keep a counter of marked LO (increasing it in copy_object).
2024 * To achieve better cache locality and cache usage, we drain the gray stack
2025 * frequently, after each object is copied, and just finish the work here.
2027 mono_sgen_drain_gray_stack (queue
, -1);
2029 DEBUG (2, fprintf (gc_debug_file
, "%s generation done\n", generation_name (generation
)));
2032 Reset bridge data, we might have lingering data from a previous collection if this is a major
2033 collection trigged by minor overflow.
2035 We must reset the gathered bridges since their original block might be evacuated due to major
2036 fragmentation in the meanwhile and the bridge code should not have to deal with that.
2038 mono_sgen_bridge_reset_data ();
2041 * Walk the ephemeron tables marking all values with reachable keys. This must be completely done
2042 * before processing finalizable objects or non-tracking weak hamdle to avoid finalizing/clearing
2043 * objects that are in fact reachable.
2045 done_with_ephemerons
= 0;
2047 done_with_ephemerons
= mark_ephemerons_in_range (copy_func
, start_addr
, end_addr
, queue
);
2048 mono_sgen_drain_gray_stack (queue
, -1);
2050 } while (!done_with_ephemerons
);
2052 mono_sgen_scan_togglerefs (copy_func
, start_addr
, end_addr
, queue
);
2053 if (generation
== GENERATION_OLD
)
2054 mono_sgen_scan_togglerefs (copy_func
, nursery_start
, nursery_end
, queue
);
2056 if (mono_sgen_need_bridge_processing ()) {
2057 collect_bridge_objects (copy_func
, start_addr
, end_addr
, generation
, queue
);
2058 if (generation
== GENERATION_OLD
)
2059 collect_bridge_objects (copy_func
, nursery_start
, nursery_end
, GENERATION_NURSERY
, queue
);
2060 mono_sgen_drain_gray_stack (queue
, -1);
2064 We must clear weak links that don't track resurrection before processing object ready for
2065 finalization so they can be cleared before that.
2067 null_link_in_range (copy_func
, start_addr
, end_addr
, generation
, TRUE
, queue
);
2068 if (generation
== GENERATION_OLD
)
2069 null_link_in_range (copy_func
, start_addr
, end_addr
, GENERATION_NURSERY
, TRUE
, queue
);
2072 /* walk the finalization queue and move also the objects that need to be
2073 * finalized: use the finalized objects as new roots so the objects they depend
2074 * on are also not reclaimed. As with the roots above, only objects in the nursery
2075 * are marked/copied.
2076 * We need a loop here, since objects ready for finalizers may reference other objects
2077 * that are fin-ready. Speedup with a flag?
2081 fin_ready
= num_ready_finalizers
;
2082 finalize_in_range (copy_func
, start_addr
, end_addr
, generation
, queue
);
2083 if (generation
== GENERATION_OLD
)
2084 finalize_in_range (copy_func
, nursery_start
, nursery_end
, GENERATION_NURSERY
, queue
);
2086 if (fin_ready
!= num_ready_finalizers
)
2089 /* drain the new stack that might have been created */
2090 DEBUG (6, fprintf (gc_debug_file
, "Precise scan of gray area post fin\n"));
2091 mono_sgen_drain_gray_stack (queue
, -1);
2092 } while (fin_ready
!= num_ready_finalizers
);
2094 if (mono_sgen_need_bridge_processing ())
2095 g_assert (num_loops
<= 1);
2098 * This must be done again after processing finalizable objects since CWL slots are cleared only after the key is finalized.
2100 done_with_ephemerons
= 0;
2102 done_with_ephemerons
= mark_ephemerons_in_range (copy_func
, start_addr
, end_addr
, queue
);
2103 mono_sgen_drain_gray_stack (queue
, -1);
2105 } while (!done_with_ephemerons
);
2108 * Clear ephemeron pairs with unreachable keys.
2109 * We pass the copy func so we can figure out if an array was promoted or not.
2111 clear_unreachable_ephemerons (copy_func
, start_addr
, end_addr
, queue
);
2114 DEBUG (2, fprintf (gc_debug_file
, "Finalize queue handling scan for %s generation: %d usecs %d ephemeron roundss\n", generation_name (generation
), TV_ELAPSED (atv
, btv
), ephemeron_rounds
));
2117 * handle disappearing links
2118 * Note we do this after checking the finalization queue because if an object
2119 * survives (at least long enough to be finalized) we don't clear the link.
2120 * This also deals with a possible issue with the monitor reclamation: with the Boehm
2121 * GC a finalized object my lose the monitor because it is cleared before the finalizer is
2124 g_assert (mono_sgen_gray_object_queue_is_empty (queue
));
2126 null_link_in_range (copy_func
, start_addr
, end_addr
, generation
, FALSE
, queue
);
2127 if (generation
== GENERATION_OLD
)
2128 null_link_in_range (copy_func
, start_addr
, end_addr
, GENERATION_NURSERY
, FALSE
, queue
);
2129 if (mono_sgen_gray_object_queue_is_empty (queue
))
2131 mono_sgen_drain_gray_stack (queue
, -1);
2134 g_assert (mono_sgen_gray_object_queue_is_empty (queue
));
2138 mono_sgen_check_section_scan_starts (GCMemSection
*section
)
2141 for (i
= 0; i
< section
->num_scan_start
; ++i
) {
2142 if (section
->scan_starts
[i
]) {
2143 guint size
= safe_object_get_size ((MonoObject
*) section
->scan_starts
[i
]);
2144 g_assert (size
>= sizeof (MonoObject
) && size
<= MAX_SMALL_OBJ_SIZE
);
2150 check_scan_starts (void)
2152 if (!do_scan_starts_check
)
2154 mono_sgen_check_section_scan_starts (nursery_section
);
2155 major_collector
.check_scan_starts ();
2159 scan_from_registered_roots (CopyOrMarkObjectFunc copy_func
, char *addr_start
, char *addr_end
, int root_type
, GrayQueue
*queue
)
2163 SGEN_HASH_TABLE_FOREACH (&roots_hash
[root_type
], start_root
, root
) {
2164 DEBUG (6, fprintf (gc_debug_file
, "Precise root scan %p-%p (desc: %p)\n", start_root
, root
->end_root
, (void*)root
->root_desc
));
2165 precisely_scan_objects_from (copy_func
, start_root
, (void**)root
->end_root
, addr_start
, addr_end
, root
->root_desc
, queue
);
2166 } SGEN_HASH_TABLE_FOREACH_END
;
2170 mono_sgen_dump_occupied (char *start
, char *end
, char *section_start
)
2172 fprintf (heap_dump_file
, "<occupied offset=\"%td\" size=\"%td\"/>\n", start
- section_start
, end
- start
);
2176 mono_sgen_dump_section (GCMemSection
*section
, const char *type
)
2178 char *start
= section
->data
;
2179 char *end
= section
->data
+ section
->size
;
2180 char *occ_start
= NULL
;
2182 char *old_start
= NULL
; /* just for debugging */
2184 fprintf (heap_dump_file
, "<section type=\"%s\" size=\"%lu\">\n", type
, (unsigned long)section
->size
);
2186 while (start
< end
) {
2190 if (!*(void**)start
) {
2192 mono_sgen_dump_occupied (occ_start
, start
, section
->data
);
2195 start
+= sizeof (void*); /* should be ALLOC_ALIGN, really */
2198 g_assert (start
< section
->next_data
);
2203 vt
= (GCVTable
*)LOAD_VTABLE (start
);
2206 size
= ALIGN_UP (safe_object_get_size ((MonoObject
*) start
));
2209 fprintf (heap_dump_file, "<object offset=\"%d\" class=\"%s.%s\" size=\"%d\"/>\n",
2210 start - section->data,
2211 vt->klass->name_space, vt->klass->name,
2219 mono_sgen_dump_occupied (occ_start
, start
, section
->data
);
2221 fprintf (heap_dump_file
, "</section>\n");
2225 dump_object (MonoObject
*obj
, gboolean dump_location
)
2227 static char class_name
[1024];
2229 MonoClass
*class = mono_object_class (obj
);
2233 * Python's XML parser is too stupid to parse angle brackets
2234 * in strings, so we just ignore them;
2237 while (class->name
[i
] && j
< sizeof (class_name
) - 1) {
2238 if (!strchr ("<>\"", class->name
[i
]))
2239 class_name
[j
++] = class->name
[i
];
2242 g_assert (j
< sizeof (class_name
));
2245 fprintf (heap_dump_file
, "<object class=\"%s.%s\" size=\"%d\"",
2246 class->name_space
, class_name
,
2247 safe_object_get_size (obj
));
2248 if (dump_location
) {
2249 const char *location
;
2250 if (ptr_in_nursery (obj
))
2251 location
= "nursery";
2252 else if (safe_object_get_size (obj
) <= MAX_SMALL_OBJ_SIZE
)
2256 fprintf (heap_dump_file
, " location=\"%s\"", location
);
2258 fprintf (heap_dump_file
, "/>\n");
2262 dump_heap (const char *type
, int num
, const char *reason
)
2267 fprintf (heap_dump_file
, "<collection type=\"%s\" num=\"%d\"", type
, num
);
2269 fprintf (heap_dump_file
, " reason=\"%s\"", reason
);
2270 fprintf (heap_dump_file
, ">\n");
2271 fprintf (heap_dump_file
, "<other-mem-usage type=\"mempools\" size=\"%ld\"/>\n", mono_mempool_get_bytes_allocated ());
2272 mono_sgen_dump_internal_mem_usage (heap_dump_file
);
2273 fprintf (heap_dump_file
, "<pinned type=\"stack\" bytes=\"%zu\"/>\n", mono_sgen_pin_stats_get_pinned_byte_count (PIN_TYPE_STACK
));
2274 /* fprintf (heap_dump_file, "<pinned type=\"static-data\" bytes=\"%d\"/>\n", pinned_byte_counts [PIN_TYPE_STATIC_DATA]); */
2275 fprintf (heap_dump_file
, "<pinned type=\"other\" bytes=\"%zu\"/>\n", mono_sgen_pin_stats_get_pinned_byte_count (PIN_TYPE_OTHER
));
2277 fprintf (heap_dump_file
, "<pinned-objects>\n");
2278 for (list
= mono_sgen_pin_stats_get_object_list (); list
; list
= list
->next
)
2279 dump_object (list
->obj
, TRUE
);
2280 fprintf (heap_dump_file
, "</pinned-objects>\n");
2282 mono_sgen_dump_section (nursery_section
, "nursery");
2284 major_collector
.dump_heap (heap_dump_file
);
2286 fprintf (heap_dump_file
, "<los>\n");
2287 for (bigobj
= los_object_list
; bigobj
; bigobj
= bigobj
->next
)
2288 dump_object ((MonoObject
*)bigobj
->data
, FALSE
);
2289 fprintf (heap_dump_file
, "</los>\n");
2291 fprintf (heap_dump_file
, "</collection>\n");
2295 mono_sgen_register_moved_object (void *obj
, void *destination
)
2297 g_assert (mono_profiler_events
& MONO_PROFILE_GC_MOVES
);
2299 /* FIXME: handle this for parallel collector */
2300 g_assert (!mono_sgen_collection_is_parallel ());
2302 if (moved_objects_idx
== MOVED_OBJECTS_NUM
) {
2303 mono_profiler_gc_moves (moved_objects
, moved_objects_idx
);
2304 moved_objects_idx
= 0;
2306 moved_objects
[moved_objects_idx
++] = obj
;
2307 moved_objects
[moved_objects_idx
++] = destination
;
2313 static gboolean inited
= FALSE
;
2318 mono_counters_register ("Minor fragment clear", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_pre_collection_fragment_clear
);
2319 mono_counters_register ("Minor pinning", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_pinning
);
2320 mono_counters_register ("Minor scan remsets", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_scan_remsets
);
2321 mono_counters_register ("Minor scan cardtables", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_scan_card_table
);
2322 mono_counters_register ("Minor scan pinned", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_scan_pinned
);
2323 mono_counters_register ("Minor scan registered roots", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_scan_registered_roots
);
2324 mono_counters_register ("Minor scan thread data", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_scan_thread_data
);
2325 mono_counters_register ("Minor finish gray stack", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_finish_gray_stack
);
2326 mono_counters_register ("Minor fragment creation", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_minor_fragment_creation
);
2328 mono_counters_register ("Major fragment clear", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_pre_collection_fragment_clear
);
2329 mono_counters_register ("Major pinning", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_pinning
);
2330 mono_counters_register ("Major scan pinned", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_scan_pinned
);
2331 mono_counters_register ("Major scan registered roots", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_scan_registered_roots
);
2332 mono_counters_register ("Major scan thread data", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_scan_thread_data
);
2333 mono_counters_register ("Major scan alloc_pinned", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_scan_alloc_pinned
);
2334 mono_counters_register ("Major scan finalized", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_scan_finalized
);
2335 mono_counters_register ("Major scan big objects", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_scan_big_objects
);
2336 mono_counters_register ("Major finish gray stack", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_finish_gray_stack
);
2337 mono_counters_register ("Major free big objects", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_free_bigobjs
);
2338 mono_counters_register ("Major LOS sweep", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_los_sweep
);
2339 mono_counters_register ("Major sweep", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_sweep
);
2340 mono_counters_register ("Major fragment creation", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &time_major_fragment_creation
);
2342 mono_counters_register ("Number of pinned objects", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_pinned_objects
);
2344 #ifdef HEAVY_STATISTICS
2345 mono_counters_register ("WBarrier set field", MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stat_wbarrier_set_field
);
2346 mono_counters_register ("WBarrier set arrayref", MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stat_wbarrier_set_arrayref
);
2347 mono_counters_register ("WBarrier arrayref copy", MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stat_wbarrier_arrayref_copy
);
2348 mono_counters_register ("WBarrier generic store called", MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stat_wbarrier_generic_store
);
2349 mono_counters_register ("WBarrier generic store stored", MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stat_wbarrier_generic_store_remset
);
2350 mono_counters_register ("WBarrier set root", MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stat_wbarrier_set_root
);
2351 mono_counters_register ("WBarrier value copy", MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stat_wbarrier_value_copy
);
2352 mono_counters_register ("WBarrier object copy", MONO_COUNTER_GC
| MONO_COUNTER_INT
, &stat_wbarrier_object_copy
);
2354 mono_counters_register ("# objects allocated", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_objects_alloced
);
2355 mono_counters_register ("bytes allocated", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_bytes_alloced
);
2356 mono_counters_register ("# objects allocated degraded", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_objects_alloced_degraded
);
2357 mono_counters_register ("bytes allocated degraded", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_bytes_alloced_degraded
);
2358 mono_counters_register ("bytes allocated in LOS", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_bytes_alloced_los
);
2360 mono_counters_register ("# copy_object() called (nursery)", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_copy_object_called_nursery
);
2361 mono_counters_register ("# objects copied (nursery)", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_objects_copied_nursery
);
2362 mono_counters_register ("# copy_object() called (major)", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_copy_object_called_major
);
2363 mono_counters_register ("# objects copied (major)", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_objects_copied_major
);
2365 mono_counters_register ("# scan_object() called (nursery)", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_scan_object_called_nursery
);
2366 mono_counters_register ("# scan_object() called (major)", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_scan_object_called_major
);
2368 mono_counters_register ("# nursery copy_object() failed from space", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_nursery_copy_object_failed_from_space
);
2369 mono_counters_register ("# nursery copy_object() failed forwarded", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_nursery_copy_object_failed_forwarded
);
2370 mono_counters_register ("# nursery copy_object() failed pinned", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_nursery_copy_object_failed_pinned
);
2372 mono_sgen_nursery_allocator_init_heavy_stats ();
2374 mono_counters_register ("Store remsets", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_store_remsets
);
2375 mono_counters_register ("Unique store remsets", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_store_remsets_unique
);
2376 mono_counters_register ("Saved remsets 1", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_saved_remsets_1
);
2377 mono_counters_register ("Saved remsets 2", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_saved_remsets_2
);
2378 mono_counters_register ("Non-global remsets processed", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_local_remsets_processed
);
2379 mono_counters_register ("Global remsets added", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_global_remsets_added
);
2380 mono_counters_register ("Global remsets re-added", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_global_remsets_readded
);
2381 mono_counters_register ("Global remsets processed", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_global_remsets_processed
);
2382 mono_counters_register ("Global remsets discarded", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_global_remsets_discarded
);
2388 static gboolean need_calculate_minor_collection_allowance
;
2390 static int last_collection_old_num_major_sections
;
2391 static mword last_collection_los_memory_usage
= 0;
2392 static mword last_collection_old_los_memory_usage
;
2393 static mword last_collection_los_memory_alloced
;
2396 reset_minor_collection_allowance (void)
2398 need_calculate_minor_collection_allowance
= TRUE
;
2402 try_calculate_minor_collection_allowance (gboolean overwrite
)
2404 int num_major_sections
, num_major_sections_saved
, save_target
, allowance_target
;
2405 mword los_memory_saved
, new_major
, new_heap_size
;
2408 g_assert (need_calculate_minor_collection_allowance
);
2410 if (!need_calculate_minor_collection_allowance
)
2413 if (!*major_collector
.have_swept
) {
2415 minor_collection_allowance
= MIN_MINOR_COLLECTION_ALLOWANCE
;
2419 num_major_sections
= major_collector
.get_num_major_sections ();
2421 num_major_sections_saved
= MAX (last_collection_old_num_major_sections
- num_major_sections
, 0);
2422 los_memory_saved
= MAX (last_collection_old_los_memory_usage
- last_collection_los_memory_usage
, 1);
2424 new_major
= num_major_sections
* major_collector
.section_size
;
2425 new_heap_size
= new_major
+ last_collection_los_memory_usage
;
2428 * FIXME: Why is save_target half the major memory plus half the
2429 * LOS memory saved? Shouldn't it be half the major memory
2430 * saved plus half the LOS memory saved? Or half the whole heap
2433 save_target
= (new_major
+ los_memory_saved
) / 2;
2436 * We aim to allow the allocation of as many sections as is
2437 * necessary to reclaim save_target sections in the next
2438 * collection. We assume the collection pattern won't change.
2439 * In the last cycle, we had num_major_sections_saved for
2440 * minor_collection_sections_alloced. Assuming things won't
2441 * change, this must be the same ratio as save_target for
2442 * allowance_target, i.e.
2444 * num_major_sections_saved save_target
2445 * --------------------------------- == ----------------
2446 * minor_collection_sections_alloced allowance_target
2450 allowance_target
= (mword
)((double)save_target
* (double)(minor_collection_sections_alloced
* major_collector
.section_size
+ last_collection_los_memory_alloced
) / (double)(num_major_sections_saved
* major_collector
.section_size
+ los_memory_saved
));
2452 minor_collection_allowance
= MAX (MIN (allowance_target
, num_major_sections
* major_collector
.section_size
+ los_memory_usage
), MIN_MINOR_COLLECTION_ALLOWANCE
);
2454 if (new_heap_size
+ minor_collection_allowance
> soft_heap_limit
) {
2455 if (new_heap_size
> soft_heap_limit
)
2456 minor_collection_allowance
= MIN_MINOR_COLLECTION_ALLOWANCE
;
2458 minor_collection_allowance
= MAX (soft_heap_limit
- new_heap_size
, MIN_MINOR_COLLECTION_ALLOWANCE
);
2461 if (debug_print_allowance
) {
2462 mword old_major
= last_collection_old_num_major_sections
* major_collector
.section_size
;
2464 fprintf (gc_debug_file
, "Before collection: %ld bytes (%ld major, %ld LOS)\n",
2465 old_major
+ last_collection_old_los_memory_usage
, old_major
, last_collection_old_los_memory_usage
);
2466 fprintf (gc_debug_file
, "After collection: %ld bytes (%ld major, %ld LOS)\n",
2467 new_heap_size
, new_major
, last_collection_los_memory_usage
);
2468 fprintf (gc_debug_file
, "Allowance: %ld bytes\n", minor_collection_allowance
);
2471 if (major_collector
.have_computed_minor_collection_allowance
)
2472 major_collector
.have_computed_minor_collection_allowance ();
2474 need_calculate_minor_collection_allowance
= FALSE
;
2478 need_major_collection (mword space_needed
)
2480 mword los_alloced
= los_memory_usage
- MIN (last_collection_los_memory_usage
, los_memory_usage
);
2481 return (space_needed
> available_free_space ()) ||
2482 minor_collection_sections_alloced
* major_collector
.section_size
+ los_alloced
> minor_collection_allowance
;
2486 mono_sgen_need_major_collection (mword space_needed
)
2488 return need_major_collection (space_needed
);
2492 reset_pinned_from_failed_allocation (void)
2494 bytes_pinned_from_failed_allocation
= 0;
2498 mono_sgen_set_pinned_from_failed_allocation (mword objsize
)
2500 bytes_pinned_from_failed_allocation
+= objsize
;
2504 mono_sgen_collection_is_parallel (void)
2506 switch (current_collection_generation
) {
2507 case GENERATION_NURSERY
:
2508 return nursery_collection_is_parallel
;
2509 case GENERATION_OLD
:
2510 return major_collector
.is_parallel
;
2512 g_assert_not_reached ();
2517 mono_sgen_nursery_collection_is_parallel (void)
2519 return nursery_collection_is_parallel
;
2526 } ScanFromRemsetsJobData
;
2529 job_scan_from_remsets (WorkerData
*worker_data
, void *job_data_untyped
)
2531 ScanFromRemsetsJobData
*job_data
= job_data_untyped
;
2533 mono_sgen_ssb_scan_from_remsets (job_data
->heap_start
, job_data
->heap_end
, mono_sgen_workers_get_job_gray_queue (worker_data
));
2538 CopyOrMarkObjectFunc func
;
2542 } ScanFromRegisteredRootsJobData
;
2545 job_scan_from_registered_roots (WorkerData
*worker_data
, void *job_data_untyped
)
2547 ScanFromRegisteredRootsJobData
*job_data
= job_data_untyped
;
2549 scan_from_registered_roots (job_data
->func
,
2550 job_data
->heap_start
, job_data
->heap_end
,
2551 job_data
->root_type
,
2552 mono_sgen_workers_get_job_gray_queue (worker_data
));
2559 } ScanThreadDataJobData
;
2562 job_scan_thread_data (WorkerData
*worker_data
, void *job_data_untyped
)
2564 ScanThreadDataJobData
*job_data
= job_data_untyped
;
2566 scan_thread_data (job_data
->heap_start
, job_data
->heap_end
, TRUE
,
2567 mono_sgen_workers_get_job_gray_queue (worker_data
));
2571 verify_scan_starts (char *start
, char *end
)
2575 for (i
= 0; i
< nursery_section
->num_scan_start
; ++i
) {
2576 char *addr
= nursery_section
->scan_starts
[i
];
2577 if (addr
> start
&& addr
< end
)
2578 fprintf (gc_debug_file
, "NFC-BAD SCAN START [%d] %p for obj [%p %p]\n", i
, addr
, start
, end
);
2583 verify_nursery (void)
2585 char *start
, *end
, *cur
, *hole_start
;
2587 if (!do_verify_nursery
)
2590 /*This cleans up unused fragments */
2591 mono_sgen_nursery_allocator_prepare_for_pinning ();
2593 hole_start
= start
= cur
= nursery_start
;
2599 if (!*(void**)cur
) {
2600 cur
+= sizeof (void*);
2604 if (object_is_forwarded (cur
))
2605 fprintf (gc_debug_file
, "FORWARDED OBJ %p\n", cur
);
2606 else if (object_is_pinned (cur
))
2607 fprintf (gc_debug_file
, "PINNED OBJ %p\n", cur
);
2609 ss
= safe_object_get_size ((MonoObject
*)cur
);
2610 size
= ALIGN_UP (safe_object_get_size ((MonoObject
*)cur
));
2611 verify_scan_starts (cur
, cur
+ size
);
2612 if (do_dump_nursery_content
) {
2613 if (cur
> hole_start
)
2614 fprintf (gc_debug_file
, "HOLE [%p %p %d]\n", hole_start
, cur
, (int)(cur
- hole_start
));
2615 fprintf (gc_debug_file
, "OBJ [%p %p %d %d %s %d]\n", cur
, cur
+ size
, (int)size
, (int)ss
, mono_sgen_safe_name ((MonoObject
*)cur
), (gpointer
)LOAD_VTABLE (cur
) == mono_sgen_get_array_fill_vtable ());
2620 fflush (gc_debug_file
);
2624 * Collect objects in the nursery. Returns whether to trigger a major
2628 collect_nursery (size_t requested_size
)
2630 gboolean needs_major
;
2631 size_t max_garbage_amount
;
2633 ScanFromRemsetsJobData sfrjd
;
2634 ScanFromRegisteredRootsJobData scrrjd_normal
, scrrjd_wbarrier
;
2635 ScanThreadDataJobData stdjd
;
2636 mword fragment_total
;
2637 TV_DECLARE (all_atv
);
2638 TV_DECLARE (all_btv
);
2642 if (disable_minor_collections
)
2647 mono_perfcounters
->gc_collections0
++;
2649 current_collection_generation
= GENERATION_NURSERY
;
2651 reset_pinned_from_failed_allocation ();
2653 binary_protocol_collection (GENERATION_NURSERY
);
2654 check_scan_starts ();
2658 nursery_next
= mono_sgen_nursery_alloc_get_upper_alloc_bound ();
2659 /* FIXME: optimize later to use the higher address where an object can be present */
2660 nursery_next
= MAX (nursery_next
, nursery_end
);
2662 nursery_alloc_bound
= nursery_next
;
2664 DEBUG (1, fprintf (gc_debug_file
, "Start nursery collection %d %p-%p, size: %d\n", stat_minor_gcs
, nursery_start
, nursery_next
, (int)(nursery_next
- nursery_start
)));
2665 max_garbage_amount
= nursery_next
- nursery_start
;
2666 g_assert (nursery_section
->size
>= max_garbage_amount
);
2668 /* world must be stopped already */
2669 TV_GETTIME (all_atv
);
2672 /* Pinning no longer depends on clearing all nursery fragments */
2673 mono_sgen_clear_current_nursery_fragment ();
2676 time_minor_pre_collection_fragment_clear
+= TV_ELAPSED_MS (atv
, btv
);
2679 check_for_xdomain_refs ();
2681 nursery_section
->next_data
= nursery_next
;
2683 major_collector
.start_nursery_collection ();
2685 try_calculate_minor_collection_allowance (FALSE
);
2687 mono_sgen_gray_object_queue_init (&gray_queue
);
2688 mono_sgen_workers_init_distribute_gray_queue ();
2691 mono_stats
.minor_gc_count
++;
2694 mono_sgen_ssb_prepare_for_minor_collection ();
2696 process_fin_stage_entries ();
2697 process_dislink_stage_entries ();
2699 /* pin from pinned handles */
2700 mono_sgen_init_pinning ();
2701 mono_profiler_gc_event (MONO_GC_EVENT_MARK_START
, 0);
2702 pin_from_roots (nursery_start
, nursery_next
, WORKERS_DISTRIBUTE_GRAY_QUEUE
);
2703 /* identify pinned objects */
2704 mono_sgen_optimize_pin_queue (0);
2705 mono_sgen_pinning_setup_section (nursery_section
);
2706 mono_sgen_pin_objects_in_section (nursery_section
, WORKERS_DISTRIBUTE_GRAY_QUEUE
);
2709 time_minor_pinning
+= TV_ELAPSED_MS (btv
, atv
);
2710 DEBUG (2, fprintf (gc_debug_file
, "Finding pinned pointers: %d in %d usecs\n", mono_sgen_get_pinned_count (), TV_ELAPSED (btv
, atv
)));
2711 DEBUG (4, fprintf (gc_debug_file
, "Start scan with %d pinned objects\n", mono_sgen_get_pinned_count ()));
2713 if (consistency_check_at_minor_collection
)
2714 mono_sgen_check_consistency ();
2716 mono_sgen_workers_start_all_workers ();
2719 * Walk all the roots and copy the young objects to the old
2720 * generation, starting from to_space.
2722 * The global remsets must be processed before the workers start
2723 * marking because they might add global remsets.
2725 mono_sgen_ssb_scan_from_global_remsets (nursery_start
, nursery_next
, WORKERS_DISTRIBUTE_GRAY_QUEUE
);
2727 mono_sgen_workers_start_marking ();
2729 sfrjd
.heap_start
= nursery_start
;
2730 sfrjd
.heap_end
= nursery_next
;
2731 mono_sgen_workers_enqueue_job (job_scan_from_remsets
, &sfrjd
);
2733 /* we don't have complete write barrier yet, so we scan all the old generation sections */
2735 time_minor_scan_remsets
+= TV_ELAPSED_MS (atv
, btv
);
2736 DEBUG (2, fprintf (gc_debug_file
, "Old generation scan: %d usecs\n", TV_ELAPSED (atv
, btv
)));
2738 if (use_cardtable
) {
2740 sgen_card_tables_collect_stats (TRUE
);
2741 sgen_scan_from_card_tables (nursery_start
, nursery_next
, WORKERS_DISTRIBUTE_GRAY_QUEUE
);
2743 time_minor_scan_card_table
+= TV_ELAPSED_MS (atv
, btv
);
2746 if (!mono_sgen_collection_is_parallel ())
2747 mono_sgen_drain_gray_stack (&gray_queue
, -1);
2749 if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS
)
2750 report_registered_roots ();
2751 if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS
)
2752 report_finalizer_roots ();
2754 time_minor_scan_pinned
+= TV_ELAPSED_MS (btv
, atv
);
2756 /* registered roots, this includes static fields */
2757 scrrjd_normal
.func
= mono_sgen_collection_is_parallel () ? major_collector
.copy_object
: major_collector
.nopar_copy_object
;
2758 scrrjd_normal
.heap_start
= nursery_start
;
2759 scrrjd_normal
.heap_end
= nursery_next
;
2760 scrrjd_normal
.root_type
= ROOT_TYPE_NORMAL
;
2761 mono_sgen_workers_enqueue_job (job_scan_from_registered_roots
, &scrrjd_normal
);
2763 scrrjd_wbarrier
.func
= mono_sgen_collection_is_parallel () ? major_collector
.copy_object
: major_collector
.nopar_copy_object
;
2764 scrrjd_wbarrier
.heap_start
= nursery_start
;
2765 scrrjd_wbarrier
.heap_end
= nursery_next
;
2766 scrrjd_wbarrier
.root_type
= ROOT_TYPE_WBARRIER
;
2767 mono_sgen_workers_enqueue_job (job_scan_from_registered_roots
, &scrrjd_wbarrier
);
2770 time_minor_scan_registered_roots
+= TV_ELAPSED_MS (atv
, btv
);
2773 stdjd
.heap_start
= nursery_start
;
2774 stdjd
.heap_end
= nursery_next
;
2775 mono_sgen_workers_enqueue_job (job_scan_thread_data
, &stdjd
);
2778 time_minor_scan_thread_data
+= TV_ELAPSED_MS (btv
, atv
);
2781 if (mono_sgen_collection_is_parallel ()) {
2782 while (!mono_sgen_gray_object_queue_is_empty (WORKERS_DISTRIBUTE_GRAY_QUEUE
)) {
2783 mono_sgen_workers_distribute_gray_queue_sections ();
2787 mono_sgen_workers_join ();
2789 if (mono_sgen_collection_is_parallel ())
2790 g_assert (mono_sgen_gray_object_queue_is_empty (&gray_queue
));
2792 finish_gray_stack (nursery_start
, nursery_next
, GENERATION_NURSERY
, &gray_queue
);
2794 time_minor_finish_gray_stack
+= TV_ELAPSED_MS (btv
, atv
);
2795 mono_profiler_gc_event (MONO_GC_EVENT_MARK_END
, 0);
2798 * The (single-threaded) finalization code might have done
2799 * some copying/marking so we can only reset the GC thread's
2800 * worker data here instead of earlier when we joined the
2803 mono_sgen_workers_reset_data ();
2805 if (objects_pinned
) {
2806 mono_sgen_optimize_pin_queue (0);
2807 mono_sgen_pinning_setup_section (nursery_section
);
2810 /* walk the pin_queue, build up the fragment list of free memory, unmark
2811 * pinned objects as we go, memzero() the empty fragments so they are ready for the
2814 mono_profiler_gc_event (MONO_GC_EVENT_RECLAIM_START
, 0);
2815 fragment_total
= mono_sgen_build_nursery_fragments (nursery_section
, nursery_section
->pin_queue_start
, nursery_section
->pin_queue_num_entries
);
2816 if (!fragment_total
)
2819 /* Clear TLABs for all threads */
2820 mono_sgen_clear_tlabs ();
2822 mono_profiler_gc_event (MONO_GC_EVENT_RECLAIM_END
, 0);
2824 time_minor_fragment_creation
+= TV_ELAPSED_MS (atv
, btv
);
2825 DEBUG (2, fprintf (gc_debug_file
, "Fragment creation: %d usecs, %lu bytes available\n", TV_ELAPSED (atv
, btv
), (unsigned long)fragment_total
));
2827 if (consistency_check_at_minor_collection
)
2828 mono_sgen_check_major_refs ();
2830 major_collector
.finish_nursery_collection ();
2832 TV_GETTIME (all_btv
);
2833 mono_stats
.minor_gc_time_usecs
+= TV_ELAPSED (all_atv
, all_btv
);
2836 dump_heap ("minor", stat_minor_gcs
- 1, NULL
);
2838 /* prepare the pin queue for the next collection */
2839 mono_sgen_finish_pinning ();
2840 if (fin_ready_list
|| critical_fin_list
) {
2841 DEBUG (4, fprintf (gc_debug_file
, "Finalizer-thread wakeup: ready %d\n", num_ready_finalizers
));
2842 mono_gc_finalize_notify ();
2844 mono_sgen_pin_stats_reset ();
2846 g_assert (mono_sgen_gray_object_queue_is_empty (&gray_queue
));
2849 sgen_card_tables_collect_stats (FALSE
);
2851 check_scan_starts ();
2853 binary_protocol_flush_buffers (FALSE
);
2855 /*objects are late pinned because of lack of memory, so a major is a good call*/
2856 needs_major
= need_major_collection (0) || objects_pinned
;
2857 current_collection_generation
= -1;
2864 mono_sgen_collect_nursery_no_lock (size_t requested_size
)
2866 gint64 gc_start_time
;
2868 mono_profiler_gc_event (MONO_GC_EVENT_START
, 0);
2869 gc_start_time
= mono_100ns_ticks ();
2872 collect_nursery (requested_size
);
2875 mono_trace_message (MONO_TRACE_GC
, "minor gc took %d usecs", (mono_100ns_ticks () - gc_start_time
) / 10);
2876 mono_profiler_gc_event (MONO_GC_EVENT_END
, 0);
2881 FinalizeReadyEntry
*list
;
2882 } ScanFinalizerEntriesJobData
;
2885 job_scan_finalizer_entries (WorkerData
*worker_data
, void *job_data_untyped
)
2887 ScanFinalizerEntriesJobData
*job_data
= job_data_untyped
;
2889 scan_finalizer_entries (major_collector
.copy_or_mark_object
,
2891 mono_sgen_workers_get_job_gray_queue (worker_data
));
2895 major_do_collection (const char *reason
)
2897 LOSObject
*bigobj
, *prevbo
;
2898 TV_DECLARE (all_atv
);
2899 TV_DECLARE (all_btv
);
2902 /* FIXME: only use these values for the precise scan
2903 * note that to_space pointers should be excluded anyway...
2905 char *heap_start
= NULL
;
2906 char *heap_end
= (char*)-1;
2907 int old_next_pin_slot
;
2908 ScanFromRegisteredRootsJobData scrrjd_normal
, scrrjd_wbarrier
;
2909 ScanThreadDataJobData stdjd
;
2910 ScanFinalizerEntriesJobData sfejd_fin_ready
, sfejd_critical_fin
;
2912 mono_perfcounters
->gc_collections1
++;
2914 reset_pinned_from_failed_allocation ();
2916 last_collection_old_num_major_sections
= major_collector
.get_num_major_sections ();
2919 * A domain could have been freed, resulting in
2920 * los_memory_usage being less than last_collection_los_memory_usage.
2922 last_collection_los_memory_alloced
= los_memory_usage
- MIN (last_collection_los_memory_usage
, los_memory_usage
);
2923 last_collection_old_los_memory_usage
= los_memory_usage
;
2926 //count_ref_nonref_objs ();
2927 //consistency_check ();
2929 binary_protocol_collection (GENERATION_OLD
);
2930 check_scan_starts ();
2931 mono_sgen_gray_object_queue_init (&gray_queue
);
2932 mono_sgen_workers_init_distribute_gray_queue ();
2935 DEBUG (1, fprintf (gc_debug_file
, "Start major collection %d\n", stat_major_gcs
));
2937 mono_stats
.major_gc_count
++;
2939 /* world must be stopped already */
2940 TV_GETTIME (all_atv
);
2943 /* Pinning depends on this */
2944 mono_sgen_clear_nursery_fragments ();
2947 time_major_pre_collection_fragment_clear
+= TV_ELAPSED_MS (atv
, btv
);
2949 nursery_section
->next_data
= nursery_end
;
2950 /* we should also coalesce scanning from sections close to each other
2951 * and deal with pointers outside of the sections later.
2954 if (major_collector
.start_major_collection
)
2955 major_collector
.start_major_collection ();
2957 *major_collector
.have_swept
= FALSE
;
2958 reset_minor_collection_allowance ();
2961 check_for_xdomain_refs ();
2963 /* Remsets are not useful for a major collection */
2965 mono_sgen_card_table_prepare_for_major_collection ();
2967 mono_sgen_ssb_prepare_for_major_collection ();
2969 process_fin_stage_entries ();
2970 process_dislink_stage_entries ();
2973 mono_sgen_init_pinning ();
2974 DEBUG (6, fprintf (gc_debug_file
, "Collecting pinned addresses\n"));
2975 pin_from_roots ((void*)lowest_heap_address
, (void*)highest_heap_address
, WORKERS_DISTRIBUTE_GRAY_QUEUE
);
2976 mono_sgen_optimize_pin_queue (0);
2979 * pin_queue now contains all candidate pointers, sorted and
2980 * uniqued. We must do two passes now to figure out which
2981 * objects are pinned.
2983 * The first is to find within the pin_queue the area for each
2984 * section. This requires that the pin_queue be sorted. We
2985 * also process the LOS objects and pinned chunks here.
2987 * The second, destructive, pass is to reduce the section
2988 * areas to pointers to the actually pinned objects.
2990 DEBUG (6, fprintf (gc_debug_file
, "Pinning from sections\n"));
2991 /* first pass for the sections */
2992 mono_sgen_find_section_pin_queue_start_end (nursery_section
);
2993 major_collector
.find_pin_queue_start_ends (WORKERS_DISTRIBUTE_GRAY_QUEUE
);
2994 /* identify possible pointers to the insize of large objects */
2995 DEBUG (6, fprintf (gc_debug_file
, "Pinning from large objects\n"));
2996 for (bigobj
= los_object_list
; bigobj
; bigobj
= bigobj
->next
) {
2998 gboolean profile_roots
= mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS
;
2999 GCRootReport report
;
3001 if (mono_sgen_find_optimized_pin_queue_area (bigobj
->data
, (char*)bigobj
->data
+ bigobj
->size
, &dummy
)) {
3002 binary_protocol_pin (bigobj
->data
, (gpointer
)LOAD_VTABLE (bigobj
->data
), safe_object_get_size (bigobj
->data
));
3003 pin_object (bigobj
->data
);
3004 /* FIXME: only enqueue if object has references */
3005 GRAY_OBJECT_ENQUEUE (WORKERS_DISTRIBUTE_GRAY_QUEUE
, bigobj
->data
);
3006 if (G_UNLIKELY (do_pin_stats
))
3007 mono_sgen_pin_stats_register_object ((char*) bigobj
->data
, safe_object_get_size ((MonoObject
*) bigobj
->data
));
3008 DEBUG (6, fprintf (gc_debug_file
, "Marked large object %p (%s) size: %lu from roots\n", bigobj
->data
, safe_name (bigobj
->data
), (unsigned long)bigobj
->size
));
3011 add_profile_gc_root (&report
, bigobj
->data
, MONO_PROFILE_GC_ROOT_PINNING
| MONO_PROFILE_GC_ROOT_MISC
, 0);
3014 notify_gc_roots (&report
);
3016 /* second pass for the sections */
3017 mono_sgen_pin_objects_in_section (nursery_section
, WORKERS_DISTRIBUTE_GRAY_QUEUE
);
3018 major_collector
.pin_objects (WORKERS_DISTRIBUTE_GRAY_QUEUE
);
3019 old_next_pin_slot
= mono_sgen_get_pinned_count ();
3022 time_major_pinning
+= TV_ELAPSED_MS (atv
, btv
);
3023 DEBUG (2, fprintf (gc_debug_file
, "Finding pinned pointers: %d in %d usecs\n", mono_sgen_get_pinned_count (), TV_ELAPSED (atv
, btv
)));
3024 DEBUG (4, fprintf (gc_debug_file
, "Start scan with %d pinned objects\n", mono_sgen_get_pinned_count ()));
3026 major_collector
.init_to_space ();
3028 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
3029 main_gc_thread
= mono_native_thread_self ();
3032 mono_sgen_workers_start_all_workers ();
3033 mono_sgen_workers_start_marking ();
3035 if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS
)
3036 report_registered_roots ();
3038 time_major_scan_pinned
+= TV_ELAPSED_MS (btv
, atv
);
3040 /* registered roots, this includes static fields */
3041 scrrjd_normal
.func
= major_collector
.copy_or_mark_object
;
3042 scrrjd_normal
.heap_start
= heap_start
;
3043 scrrjd_normal
.heap_end
= heap_end
;
3044 scrrjd_normal
.root_type
= ROOT_TYPE_NORMAL
;
3045 mono_sgen_workers_enqueue_job (job_scan_from_registered_roots
, &scrrjd_normal
);
3047 scrrjd_wbarrier
.func
= major_collector
.copy_or_mark_object
;
3048 scrrjd_wbarrier
.heap_start
= heap_start
;
3049 scrrjd_wbarrier
.heap_end
= heap_end
;
3050 scrrjd_wbarrier
.root_type
= ROOT_TYPE_WBARRIER
;
3051 mono_sgen_workers_enqueue_job (job_scan_from_registered_roots
, &scrrjd_wbarrier
);
3054 time_major_scan_registered_roots
+= TV_ELAPSED_MS (atv
, btv
);
3057 stdjd
.heap_start
= heap_start
;
3058 stdjd
.heap_end
= heap_end
;
3059 mono_sgen_workers_enqueue_job (job_scan_thread_data
, &stdjd
);
3062 time_major_scan_thread_data
+= TV_ELAPSED_MS (btv
, atv
);
3065 time_major_scan_alloc_pinned
+= TV_ELAPSED_MS (atv
, btv
);
3067 if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS
)
3068 report_finalizer_roots ();
3070 /* scan the list of objects ready for finalization */
3071 sfejd_fin_ready
.list
= fin_ready_list
;
3072 mono_sgen_workers_enqueue_job (job_scan_finalizer_entries
, &sfejd_fin_ready
);
3074 sfejd_critical_fin
.list
= critical_fin_list
;
3075 mono_sgen_workers_enqueue_job (job_scan_finalizer_entries
, &sfejd_critical_fin
);
3078 time_major_scan_finalized
+= TV_ELAPSED_MS (btv
, atv
);
3079 DEBUG (2, fprintf (gc_debug_file
, "Root scan: %d usecs\n", TV_ELAPSED (btv
, atv
)));
3082 time_major_scan_big_objects
+= TV_ELAPSED_MS (atv
, btv
);
3084 if (major_collector
.is_parallel
) {
3085 while (!mono_sgen_gray_object_queue_is_empty (WORKERS_DISTRIBUTE_GRAY_QUEUE
)) {
3086 mono_sgen_workers_distribute_gray_queue_sections ();
3090 mono_sgen_workers_join ();
3092 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
3093 main_gc_thread
= NULL
;
3096 if (major_collector
.is_parallel
)
3097 g_assert (mono_sgen_gray_object_queue_is_empty (&gray_queue
));
3099 /* all the objects in the heap */
3100 finish_gray_stack (heap_start
, heap_end
, GENERATION_OLD
, &gray_queue
);
3102 time_major_finish_gray_stack
+= TV_ELAPSED_MS (btv
, atv
);
3105 * The (single-threaded) finalization code might have done
3106 * some copying/marking so we can only reset the GC thread's
3107 * worker data here instead of earlier when we joined the
3110 mono_sgen_workers_reset_data ();
3112 if (objects_pinned
) {
3113 /*This is slow, but we just OOM'd*/
3114 mono_sgen_pin_queue_clear_discarded_entries (nursery_section
, old_next_pin_slot
);
3115 mono_sgen_optimize_pin_queue (0);
3116 mono_sgen_find_section_pin_queue_start_end (nursery_section
);
3120 reset_heap_boundaries ();
3121 mono_sgen_update_heap_boundaries ((mword
)nursery_start
, (mword
)nursery_end
);
3123 /* sweep the big objects list */
3125 for (bigobj
= los_object_list
; bigobj
;) {
3126 if (object_is_pinned (bigobj
->data
)) {
3127 unpin_object (bigobj
->data
);
3128 mono_sgen_update_heap_boundaries ((mword
)bigobj
->data
, (mword
)bigobj
->data
+ bigobj
->size
);
3131 /* not referenced anywhere, so we can free it */
3133 prevbo
->next
= bigobj
->next
;
3135 los_object_list
= bigobj
->next
;
3137 bigobj
= bigobj
->next
;
3138 mono_sgen_los_free_object (to_free
);
3142 bigobj
= bigobj
->next
;
3146 time_major_free_bigobjs
+= TV_ELAPSED_MS (atv
, btv
);
3148 mono_sgen_los_sweep ();
3151 time_major_los_sweep
+= TV_ELAPSED_MS (btv
, atv
);
3153 major_collector
.sweep ();
3156 time_major_sweep
+= TV_ELAPSED_MS (atv
, btv
);
3158 /* walk the pin_queue, build up the fragment list of free memory, unmark
3159 * pinned objects as we go, memzero() the empty fragments so they are ready for the
3162 if (!mono_sgen_build_nursery_fragments (nursery_section
, nursery_section
->pin_queue_start
, nursery_section
->pin_queue_num_entries
))
3165 /* Clear TLABs for all threads */
3166 mono_sgen_clear_tlabs ();
3169 time_major_fragment_creation
+= TV_ELAPSED_MS (btv
, atv
);
3171 TV_GETTIME (all_btv
);
3172 mono_stats
.major_gc_time_usecs
+= TV_ELAPSED (all_atv
, all_btv
);
3175 dump_heap ("major", stat_major_gcs
- 1, reason
);
3177 /* prepare the pin queue for the next collection */
3178 mono_sgen_finish_pinning ();
3180 if (fin_ready_list
|| critical_fin_list
) {
3181 DEBUG (4, fprintf (gc_debug_file
, "Finalizer-thread wakeup: ready %d\n", num_ready_finalizers
));
3182 mono_gc_finalize_notify ();
3184 mono_sgen_pin_stats_reset ();
3186 g_assert (mono_sgen_gray_object_queue_is_empty (&gray_queue
));
3188 try_calculate_minor_collection_allowance (TRUE
);
3190 minor_collection_sections_alloced
= 0;
3191 last_collection_los_memory_usage
= los_memory_usage
;
3193 major_collector
.finish_major_collection ();
3195 check_scan_starts ();
3197 binary_protocol_flush_buffers (FALSE
);
3199 //consistency_check ();
3201 return bytes_pinned_from_failed_allocation
> 0;
3205 major_collection (const char *reason
)
3207 gboolean need_minor_collection
;
3209 if (disable_major_collections
) {
3210 collect_nursery (0);
3214 major_collection_happened
= TRUE
;
3215 current_collection_generation
= GENERATION_OLD
;
3216 need_minor_collection
= major_do_collection (reason
);
3217 current_collection_generation
= -1;
3219 if (need_minor_collection
)
3220 collect_nursery (0);
3224 sgen_collect_major_no_lock (const char *reason
)
3226 gint64 gc_start_time
;
3228 mono_profiler_gc_event (MONO_GC_EVENT_START
, 1);
3229 gc_start_time
= mono_100ns_ticks ();
3231 major_collection (reason
);
3233 mono_trace_message (MONO_TRACE_GC
, "major gc took %d usecs", (mono_100ns_ticks () - gc_start_time
) / 10);
3234 mono_profiler_gc_event (MONO_GC_EVENT_END
, 1);
3238 * When deciding if it's better to collect or to expand, keep track
3239 * of how much garbage was reclaimed with the last collection: if it's too
3241 * This is called when we could not allocate a small object.
3243 static void __attribute__((noinline
))
3244 minor_collect_or_expand_inner (size_t size
)
3246 int do_minor_collection
= 1;
3248 g_assert (nursery_section
);
3249 if (do_minor_collection
) {
3250 gint64 total_gc_time
, major_gc_time
= 0;
3252 mono_profiler_gc_event (MONO_GC_EVENT_START
, 0);
3253 total_gc_time
= mono_100ns_ticks ();
3256 if (collect_nursery (size
)) {
3257 mono_profiler_gc_event (MONO_GC_EVENT_START
, 1);
3258 major_gc_time
= mono_100ns_ticks ();
3260 major_collection ("minor overflow");
3262 /* keep events symmetric */
3263 major_gc_time
= mono_100ns_ticks () - major_gc_time
;
3264 mono_profiler_gc_event (MONO_GC_EVENT_END
, 1);
3266 DEBUG (2, fprintf (gc_debug_file
, "Heap size: %lu, LOS size: %lu\n", (unsigned long)total_alloc
, (unsigned long)los_memory_usage
));
3269 total_gc_time
= mono_100ns_ticks () - total_gc_time
;
3271 mono_trace_message (MONO_TRACE_GC
, "overflow major gc took %d usecs minor gc took %d usecs", total_gc_time
/ 10, (total_gc_time
- major_gc_time
) / 10);
3273 mono_trace_message (MONO_TRACE_GC
, "minor gc took %d usecs", total_gc_time
/ 10);
3275 /* this also sets the proper pointers for the next allocation */
3276 if (!mono_sgen_can_alloc_size (size
)) {
3277 /* TypeBuilder and MonoMethod are killing mcs with fragmentation */
3278 DEBUG (1, fprintf (gc_debug_file
, "nursery collection didn't find enough room for %zd alloc (%d pinned)\n", size
, mono_sgen_get_pinned_count ()));
3279 mono_sgen_dump_pin_queue ();
3282 mono_profiler_gc_event (MONO_GC_EVENT_END
, 0);
3284 //report_internal_mem_usage ();
3288 mono_sgen_minor_collect_or_expand_inner (size_t size
)
3290 minor_collect_or_expand_inner (size
);
3294 * ######################################################################
3295 * ######## Memory allocation from the OS
3296 * ######################################################################
3297 * This section of code deals with getting memory from the OS and
3298 * allocating memory for GC-internal data structures.
3299 * Internal memory can be handled with a freelist for small objects.
3305 G_GNUC_UNUSED
static void
3306 report_internal_mem_usage (void)
3308 printf ("Internal memory usage:\n");
3309 mono_sgen_report_internal_mem_usage ();
3310 printf ("Pinned memory usage:\n");
3311 major_collector
.report_pinned_memory_usage ();
3315 * ######################################################################
3316 * ######## Finalization support
3317 * ######################################################################
3321 * this is valid for the nursery: if the object has been forwarded it means it's
3322 * still refrenced from a root. If it is pinned it's still alive as well.
3323 * Return TRUE if @obj is ready to be finalized.
3325 #define object_is_fin_ready(obj) (!object_is_pinned (obj) && !object_is_forwarded (obj))
3329 mono_sgen_gc_is_object_ready_for_finalization (void *object
)
3331 return !major_collector
.is_object_live (object
) && object_is_fin_ready (object
);
3335 has_critical_finalizer (MonoObject
*obj
)
3339 if (!mono_defaults
.critical_finalizer_object
)
3342 class = ((MonoVTable
*)LOAD_VTABLE (obj
))->klass
;
3344 return mono_class_has_parent_fast (class, mono_defaults
.critical_finalizer_object
);
3348 queue_finalization_entry (MonoObject
*obj
) {
3349 FinalizeReadyEntry
*entry
= mono_sgen_alloc_internal (INTERNAL_MEM_FINALIZE_READY_ENTRY
);
3350 entry
->object
= obj
;
3351 if (has_critical_finalizer (obj
)) {
3352 entry
->next
= critical_fin_list
;
3353 critical_fin_list
= entry
;
3355 entry
->next
= fin_ready_list
;
3356 fin_ready_list
= entry
;
3361 object_is_reachable (char *object
, char *start
, char *end
)
3363 /*This happens for non nursery objects during minor collections. We just treat all objects as alive.*/
3364 if (object
< start
|| object
>= end
)
3366 return !object_is_fin_ready (object
) || major_collector
.is_object_live (object
);
3369 #include "sgen-fin-weak-hash.c"
3372 mono_sgen_object_is_live (void *obj
)
3374 if (ptr_in_nursery (obj
))
3375 return object_is_pinned (obj
);
3376 if (current_collection_generation
== GENERATION_NURSERY
)
3378 return major_collector
.is_object_live (obj
);
3381 /* LOCKING: requires that the GC lock is held */
3383 null_ephemerons_for_domain (MonoDomain
*domain
)
3385 EphemeronLinkNode
*current
= ephemeron_list
, *prev
= NULL
;
3388 MonoObject
*object
= (MonoObject
*)current
->array
;
3390 if (object
&& !object
->vtable
) {
3391 EphemeronLinkNode
*tmp
= current
;
3394 prev
->next
= current
->next
;
3396 ephemeron_list
= current
->next
;
3398 current
= current
->next
;
3399 mono_sgen_free_internal (tmp
, INTERNAL_MEM_EPHEMERON_LINK
);
3402 current
= current
->next
;
3407 /* LOCKING: requires that the GC lock is held */
3409 clear_unreachable_ephemerons (CopyOrMarkObjectFunc copy_func
, char *start
, char *end
, GrayQueue
*queue
)
3411 int was_in_nursery
, was_promoted
;
3412 EphemeronLinkNode
*current
= ephemeron_list
, *prev
= NULL
;
3414 Ephemeron
*cur
, *array_end
;
3418 char *object
= current
->array
;
3420 if (!object_is_reachable (object
, start
, end
)) {
3421 EphemeronLinkNode
*tmp
= current
;
3423 DEBUG (5, fprintf (gc_debug_file
, "Dead Ephemeron array at %p\n", object
));
3426 prev
->next
= current
->next
;
3428 ephemeron_list
= current
->next
;
3430 current
= current
->next
;
3431 mono_sgen_free_internal (tmp
, INTERNAL_MEM_EPHEMERON_LINK
);
3436 was_in_nursery
= ptr_in_nursery (object
);
3437 copy_func ((void**)&object
, queue
);
3438 current
->array
= object
;
3440 /*The array was promoted, add global remsets for key/values left behind in nursery.*/
3441 was_promoted
= was_in_nursery
&& !ptr_in_nursery (object
);
3443 DEBUG (5, fprintf (gc_debug_file
, "Clearing unreachable entries for ephemeron array at %p\n", object
));
3445 array
= (MonoArray
*)object
;
3446 cur
= mono_array_addr (array
, Ephemeron
, 0);
3447 array_end
= cur
+ mono_array_length_fast (array
);
3448 tombstone
= (char*)((MonoVTable
*)LOAD_VTABLE (object
))->domain
->ephemeron_tombstone
;
3450 for (; cur
< array_end
; ++cur
) {
3451 char *key
= (char*)cur
->key
;
3453 if (!key
|| key
== tombstone
)
3456 DEBUG (5, fprintf (gc_debug_file
, "[%td] key %p (%s) value %p (%s)\n", cur
- mono_array_addr (array
, Ephemeron
, 0),
3457 key
, object_is_reachable (key
, start
, end
) ? "reachable" : "unreachable",
3458 cur
->value
, cur
->value
&& object_is_reachable (cur
->value
, start
, end
) ? "reachable" : "unreachable"));
3460 if (!object_is_reachable (key
, start
, end
)) {
3461 cur
->key
= tombstone
;
3467 if (ptr_in_nursery (key
)) {/*key was not promoted*/
3468 DEBUG (5, fprintf (gc_debug_file
, "\tAdded remset to key %p\n", key
));
3469 mono_sgen_add_to_global_remset (&cur
->key
);
3471 if (ptr_in_nursery (cur
->value
)) {/*value was not promoted*/
3472 DEBUG (5, fprintf (gc_debug_file
, "\tAdded remset to value %p\n", cur
->value
));
3473 mono_sgen_add_to_global_remset (&cur
->value
);
3478 current
= current
->next
;
3482 /* LOCKING: requires that the GC lock is held */
3484 mark_ephemerons_in_range (CopyOrMarkObjectFunc copy_func
, char *start
, char *end
, GrayQueue
*queue
)
3486 int nothing_marked
= 1;
3487 EphemeronLinkNode
*current
= ephemeron_list
;
3489 Ephemeron
*cur
, *array_end
;
3492 for (current
= ephemeron_list
; current
; current
= current
->next
) {
3493 char *object
= current
->array
;
3494 DEBUG (5, fprintf (gc_debug_file
, "Ephemeron array at %p\n", object
));
3497 For now we process all ephemerons during all collections.
3498 Ideally we should use remset information to partially scan those
3500 We already emit write barriers for Ephemeron fields, it's
3501 just that we don't process them.
3503 /*if (object < start || object >= end)
3506 /*It has to be alive*/
3507 if (!object_is_reachable (object
, start
, end
)) {
3508 DEBUG (5, fprintf (gc_debug_file
, "\tnot reachable\n"));
3512 copy_func ((void**)&object
, queue
);
3514 array
= (MonoArray
*)object
;
3515 cur
= mono_array_addr (array
, Ephemeron
, 0);
3516 array_end
= cur
+ mono_array_length_fast (array
);
3517 tombstone
= (char*)((MonoVTable
*)LOAD_VTABLE (object
))->domain
->ephemeron_tombstone
;
3519 for (; cur
< array_end
; ++cur
) {
3520 char *key
= cur
->key
;
3522 if (!key
|| key
== tombstone
)
3525 DEBUG (5, fprintf (gc_debug_file
, "[%td] key %p (%s) value %p (%s)\n", cur
- mono_array_addr (array
, Ephemeron
, 0),
3526 key
, object_is_reachable (key
, start
, end
) ? "reachable" : "unreachable",
3527 cur
->value
, cur
->value
&& object_is_reachable (cur
->value
, start
, end
) ? "reachable" : "unreachable"));
3529 if (object_is_reachable (key
, start
, end
)) {
3530 char *value
= cur
->value
;
3532 copy_func ((void**)&cur
->key
, queue
);
3534 if (!object_is_reachable (value
, start
, end
))
3536 copy_func ((void**)&cur
->value
, queue
);
3542 DEBUG (5, fprintf (gc_debug_file
, "Ephemeron run finished. Is it done %d\n", nothing_marked
));
3543 return nothing_marked
;
3547 mono_gc_invoke_finalizers (void)
3549 FinalizeReadyEntry
*entry
= NULL
;
3550 gboolean entry_is_critical
= FALSE
;
3553 /* FIXME: batch to reduce lock contention */
3554 while (fin_ready_list
|| critical_fin_list
) {
3558 FinalizeReadyEntry
**list
= entry_is_critical
? &critical_fin_list
: &fin_ready_list
;
3560 /* We have finalized entry in the last
3561 interation, now we need to remove it from
3564 *list
= entry
->next
;
3566 FinalizeReadyEntry
*e
= *list
;
3567 while (e
->next
!= entry
)
3569 e
->next
= entry
->next
;
3571 mono_sgen_free_internal (entry
, INTERNAL_MEM_FINALIZE_READY_ENTRY
);
3575 /* Now look for the first non-null entry. */
3576 for (entry
= fin_ready_list
; entry
&& !entry
->object
; entry
= entry
->next
)
3579 entry_is_critical
= FALSE
;
3581 entry_is_critical
= TRUE
;
3582 for (entry
= critical_fin_list
; entry
&& !entry
->object
; entry
= entry
->next
)
3587 g_assert (entry
->object
);
3588 num_ready_finalizers
--;
3589 obj
= entry
->object
;
3590 entry
->object
= NULL
;
3591 DEBUG (7, fprintf (gc_debug_file
, "Finalizing object %p (%s)\n", obj
, safe_name (obj
)));
3599 g_assert (entry
->object
== NULL
);
3601 /* the object is on the stack so it is pinned */
3602 /*g_print ("Calling finalizer for object: %p (%s)\n", entry->object, safe_name (entry->object));*/
3603 mono_gc_run_finalize (obj
, NULL
);
3610 mono_gc_pending_finalizers (void)
3612 return fin_ready_list
|| critical_fin_list
;
3615 /* Negative value to remove */
3617 mono_gc_add_memory_pressure (gint64 value
)
3619 /* FIXME: Use interlocked functions */
3621 memory_pressure
+= value
;
3626 mono_sgen_register_major_sections_alloced (int num_sections
)
3628 minor_collection_sections_alloced
+= num_sections
;
3632 mono_sgen_get_minor_collection_allowance (void)
3634 return minor_collection_allowance
;
3638 * ######################################################################
3639 * ######## registered roots support
3640 * ######################################################################
3644 * We do not coalesce roots.
3647 mono_gc_register_root_inner (char *start
, size_t size
, void *descr
, int root_type
)
3649 RootRecord new_root
;
3652 for (i
= 0; i
< ROOT_TYPE_NUM
; ++i
) {
3653 RootRecord
*root
= mono_sgen_hash_table_lookup (&roots_hash
[i
], start
);
3654 /* we allow changing the size and the descriptor (for thread statics etc) */
3656 size_t old_size
= root
->end_root
- start
;
3657 root
->end_root
= start
+ size
;
3658 g_assert (((root
->root_desc
!= 0) && (descr
!= NULL
)) ||
3659 ((root
->root_desc
== 0) && (descr
== NULL
)));
3660 root
->root_desc
= (mword
)descr
;
3662 roots_size
-= old_size
;
3668 new_root
.end_root
= start
+ size
;
3669 new_root
.root_desc
= (mword
)descr
;
3671 mono_sgen_hash_table_replace (&roots_hash
[root_type
], start
, &new_root
);
3674 DEBUG (3, fprintf (gc_debug_file
, "Added root for range: %p-%p, descr: %p (%d/%d bytes)\n", start
, new_root
.end_root
, descr
, (int)size
, (int)roots_size
));
3681 mono_gc_register_root (char *start
, size_t size
, void *descr
)
3683 return mono_gc_register_root_inner (start
, size
, descr
, descr
? ROOT_TYPE_NORMAL
: ROOT_TYPE_PINNED
);
3687 mono_gc_register_root_wbarrier (char *start
, size_t size
, void *descr
)
3689 return mono_gc_register_root_inner (start
, size
, descr
, ROOT_TYPE_WBARRIER
);
3693 mono_gc_deregister_root (char* addr
)
3699 for (root_type
= 0; root_type
< ROOT_TYPE_NUM
; ++root_type
) {
3700 if (mono_sgen_hash_table_remove (&roots_hash
[root_type
], addr
, &root
))
3701 roots_size
-= (root
.end_root
- addr
);
3707 * ######################################################################
3708 * ######## Thread handling (stop/start code)
3709 * ######################################################################
3712 unsigned int mono_sgen_global_stop_count
= 0;
3715 static MonoContext cur_thread_ctx
= {0};
3717 static mword cur_thread_regs
[ARCH_NUM_REGS
] = {0};
3721 update_current_thread_stack (void *start
)
3723 int stack_guard
= 0;
3724 #ifndef USE_MONO_CTX
3725 void *ptr
= cur_thread_regs
;
3727 SgenThreadInfo
*info
= mono_thread_info_current ();
3729 info
->stack_start
= align_pointer (&stack_guard
);
3730 g_assert (info
->stack_start
>= info
->stack_start_limit
&& info
->stack_start
< info
->stack_end
);
3732 MONO_CONTEXT_GET_CURRENT (cur_thread_ctx
);
3733 info
->monoctx
= &cur_thread_ctx
;
3735 ARCH_STORE_REGS (ptr
);
3736 info
->stopped_regs
= ptr
;
3738 if (gc_callbacks
.thread_suspend_func
)
3739 gc_callbacks
.thread_suspend_func (info
->runtime_data
, NULL
);
3743 mono_sgen_fill_thread_info_for_suspend (SgenThreadInfo
*info
)
3745 #ifdef HAVE_KW_THREAD
3746 /* update the remset info in the thread data structure */
3747 info
->remset
= remembered_set
;
3752 * Define this and use the "xdomain-checks" MONO_GC_DEBUG option to
3753 * have cross-domain checks in the write barrier.
3755 //#define XDOMAIN_CHECKS_IN_WBARRIER
3757 #ifndef SGEN_BINARY_PROTOCOL
3758 #ifndef HEAVY_STATISTICS
3759 #define MANAGED_ALLOCATION
3760 #ifndef XDOMAIN_CHECKS_IN_WBARRIER
3761 #define MANAGED_WBARRIER
3767 is_ip_in_managed_allocator (MonoDomain
*domain
, gpointer ip
);
3770 restart_threads_until_none_in_managed_allocator (void)
3772 SgenThreadInfo
*info
;
3773 int num_threads_died
= 0;
3774 int sleep_duration
= -1;
3777 int restart_count
= 0, restarted_count
= 0;
3778 /* restart all threads that stopped in the
3780 FOREACH_THREAD_SAFE (info
) {
3782 if (info
->skip
|| info
->gc_disabled
)
3784 if (!info
->thread_is_dying
&& (!info
->stack_start
|| info
->in_critical_region
||
3785 is_ip_in_managed_allocator (info
->stopped_domain
, info
->stopped_ip
))) {
3786 binary_protocol_thread_restart ((gpointer
)mono_thread_info_get_tid (info
));
3787 result
= mono_sgen_resume_thread (info
);
3794 /* we set the stopped_ip to
3795 NULL for threads which
3796 we're not restarting so
3797 that we can easily identify
3799 info
->stopped_ip
= NULL
;
3800 info
->stopped_domain
= NULL
;
3802 } END_FOREACH_THREAD_SAFE
3803 /* if no threads were restarted, we're done */
3804 if (restart_count
== 0)
3807 /* wait for the threads to signal their restart */
3808 mono_sgen_wait_for_suspend_ack (restart_count
);
3810 if (sleep_duration
< 0) {
3818 g_usleep (sleep_duration
);
3819 sleep_duration
+= 10;
3822 /* stop them again */
3823 FOREACH_THREAD (info
) {
3825 if (info
->skip
|| info
->stopped_ip
== NULL
)
3827 result
= mono_sgen_suspend_thread (info
);
3834 } END_FOREACH_THREAD
3835 /* some threads might have died */
3836 num_threads_died
+= restart_count
- restarted_count
;
3837 /* wait for the threads to signal their suspension
3839 mono_sgen_wait_for_suspend_ack (restart_count
);
3842 return num_threads_died
;
3846 acquire_gc_locks (void)
3849 mono_thread_info_suspend_lock ();
3853 release_gc_locks (void)
3855 mono_thread_info_suspend_unlock ();
3856 UNLOCK_INTERRUPTION
;
3859 static TV_DECLARE (stop_world_time
);
3860 static unsigned long max_pause_usec
= 0;
3862 /* LOCKING: assumes the GC lock is held */
3864 stop_world (int generation
)
3868 /*XXX this is the right stop, thought might not be the nicest place to put it*/
3869 mono_sgen_process_togglerefs ();
3871 mono_profiler_gc_event (MONO_GC_EVENT_PRE_STOP_WORLD
, generation
);
3872 acquire_gc_locks ();
3874 update_current_thread_stack (&count
);
3876 mono_sgen_global_stop_count
++;
3877 DEBUG (3, fprintf (gc_debug_file
, "stopping world n %d from %p %p\n", mono_sgen_global_stop_count
, mono_thread_info_current (), (gpointer
)mono_native_thread_id_get ()));
3878 TV_GETTIME (stop_world_time
);
3879 count
= mono_sgen_thread_handshake (TRUE
);
3880 count
-= restart_threads_until_none_in_managed_allocator ();
3881 g_assert (count
>= 0);
3882 DEBUG (3, fprintf (gc_debug_file
, "world stopped %d thread(s)\n", count
));
3883 mono_profiler_gc_event (MONO_GC_EVENT_POST_STOP_WORLD
, generation
);
3885 last_major_num_sections
= major_collector
.get_num_major_sections ();
3886 last_los_memory_usage
= los_memory_usage
;
3887 major_collection_happened
= FALSE
;
3891 /* LOCKING: assumes the GC lock is held */
3893 restart_world (int generation
)
3895 int count
, num_major_sections
;
3896 SgenThreadInfo
*info
;
3897 TV_DECLARE (end_sw
);
3898 TV_DECLARE (end_bridge
);
3899 unsigned long usec
, bridge_usec
;
3901 /* notify the profiler of the leftovers */
3902 if (G_UNLIKELY (mono_profiler_events
& MONO_PROFILE_GC_MOVES
)) {
3903 if (moved_objects_idx
) {
3904 mono_profiler_gc_moves (moved_objects
, moved_objects_idx
);
3905 moved_objects_idx
= 0;
3908 mono_profiler_gc_event (MONO_GC_EVENT_PRE_START_WORLD
, generation
);
3909 FOREACH_THREAD (info
) {
3910 info
->stack_start
= NULL
;
3912 info
->monoctx
= NULL
;
3914 info
->stopped_regs
= NULL
;
3916 } END_FOREACH_THREAD
3918 stw_bridge_process ();
3919 release_gc_locks ();
3921 count
= mono_sgen_thread_handshake (FALSE
);
3922 TV_GETTIME (end_sw
);
3923 usec
= TV_ELAPSED (stop_world_time
, end_sw
);
3924 max_pause_usec
= MAX (usec
, max_pause_usec
);
3925 DEBUG (2, fprintf (gc_debug_file
, "restarted %d thread(s) (pause time: %d usec, max: %d)\n", count
, (int)usec
, (int)max_pause_usec
));
3926 mono_profiler_gc_event (MONO_GC_EVENT_POST_START_WORLD
, generation
);
3930 TV_GETTIME (end_bridge
);
3931 bridge_usec
= TV_ELAPSED (end_sw
, end_bridge
);
3933 num_major_sections
= major_collector
.get_num_major_sections ();
3934 if (major_collection_happened
)
3935 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_GC
, "GC_MAJOR: %s pause %.2fms, bridge %.2fms major %dK/%dK los %dK/%dK",
3936 generation
? "" : "(minor overflow)",
3937 (int)usec
/ 1000.0f
, (int)bridge_usec
/ 1000.0f
,
3938 major_collector
.section_size
* num_major_sections
/ 1024,
3939 major_collector
.section_size
* last_major_num_sections
/ 1024,
3940 los_memory_usage
/ 1024,
3941 last_los_memory_usage
/ 1024);
3943 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_GC
, "GC_MINOR: pause %.2fms, bridge %.2fms promoted %dK major %dK los %dK",
3944 (int)usec
/ 1000.0f
, (int)bridge_usec
/ 1000.0f
,
3945 (num_major_sections
- last_major_num_sections
) * major_collector
.section_size
/ 1024,
3946 major_collector
.section_size
* num_major_sections
/ 1024,
3947 los_memory_usage
/ 1024);
3953 mono_sgen_get_current_collection_generation (void)
3955 return current_collection_generation
;
3959 mono_gc_set_gc_callbacks (MonoGCCallbacks
*callbacks
)
3961 gc_callbacks
= *callbacks
;
3965 mono_gc_get_gc_callbacks ()
3967 return &gc_callbacks
;
3970 /* Variables holding start/end nursery so it won't have to be passed at every call */
3971 static void *scan_area_arg_start
, *scan_area_arg_end
;
3974 mono_gc_conservatively_scan_area (void *start
, void *end
)
3976 conservatively_pin_objects_from (start
, end
, scan_area_arg_start
, scan_area_arg_end
, PIN_TYPE_STACK
);
3980 mono_gc_scan_object (void *obj
)
3982 UserCopyOrMarkData
*data
= mono_native_tls_get_value (user_copy_or_mark_key
);
3984 if (current_collection_generation
== GENERATION_NURSERY
) {
3985 if (mono_sgen_collection_is_parallel ())
3986 major_collector
.copy_object (&obj
, data
->queue
);
3988 major_collector
.nopar_copy_object (&obj
, data
->queue
);
3990 major_collector
.copy_or_mark_object (&obj
, data
->queue
);
3996 * Mark from thread stacks and registers.
3999 scan_thread_data (void *start_nursery
, void *end_nursery
, gboolean precise
, GrayQueue
*queue
)
4001 SgenThreadInfo
*info
;
4003 scan_area_arg_start
= start_nursery
;
4004 scan_area_arg_end
= end_nursery
;
4006 FOREACH_THREAD (info
) {
4008 DEBUG (3, fprintf (gc_debug_file
, "Skipping dead thread %p, range: %p-%p, size: %td\n", info
, info
->stack_start
, info
->stack_end
, (char*)info
->stack_end
- (char*)info
->stack_start
));
4011 if (info
->gc_disabled
) {
4012 DEBUG (3, fprintf (gc_debug_file
, "GC disabled for thread %p, range: %p-%p, size: %td\n", info
, info
->stack_start
, info
->stack_end
, (char*)info
->stack_end
- (char*)info
->stack_start
));
4015 DEBUG (3, fprintf (gc_debug_file
, "Scanning thread %p, range: %p-%p, size: %ld, pinned=%d\n", info
, info
->stack_start
, info
->stack_end
, (char*)info
->stack_end
- (char*)info
->stack_start
, mono_sgen_get_pinned_count ()));
4016 if (!info
->thread_is_dying
) {
4017 if (gc_callbacks
.thread_mark_func
&& !conservative_stack_mark
) {
4018 UserCopyOrMarkData data
= { NULL
, queue
};
4019 set_user_copy_or_mark_data (&data
);
4020 gc_callbacks
.thread_mark_func (info
->runtime_data
, info
->stack_start
, info
->stack_end
, precise
);
4021 set_user_copy_or_mark_data (NULL
);
4022 } else if (!precise
) {
4023 conservatively_pin_objects_from (info
->stack_start
, info
->stack_end
, start_nursery
, end_nursery
, PIN_TYPE_STACK
);
4028 if (!info
->thread_is_dying
&& !precise
)
4029 conservatively_pin_objects_from ((void**)info
->monoctx
, (void**)info
->monoctx
+ ARCH_NUM_REGS
,
4030 start_nursery
, end_nursery
, PIN_TYPE_STACK
);
4032 if (!info
->thread_is_dying
&& !precise
)
4033 conservatively_pin_objects_from (info
->stopped_regs
, info
->stopped_regs
+ ARCH_NUM_REGS
,
4034 start_nursery
, end_nursery
, PIN_TYPE_STACK
);
4036 } END_FOREACH_THREAD
4040 find_pinning_ref_from_thread (char *obj
, size_t size
)
4043 SgenThreadInfo
*info
;
4044 char *endobj
= obj
+ size
;
4046 FOREACH_THREAD (info
) {
4047 char **start
= (char**)info
->stack_start
;
4050 while (start
< (char**)info
->stack_end
) {
4051 if (*start
>= obj
&& *start
< endobj
) {
4052 DEBUG (0, fprintf (gc_debug_file
, "Object %p referenced in thread %p (id %p) at %p, stack: %p-%p\n", obj
, info
, (gpointer
)mono_thread_info_get_tid (info
), start
, info
->stack_start
, info
->stack_end
));
4057 for (j
= 0; j
< ARCH_NUM_REGS
; ++j
) {
4059 mword w
= ((mword
*)info
->monoctx
) [j
];
4061 mword w
= (mword
)info
->stopped_regs
[j
];
4064 if (w
>= (mword
)obj
&& w
< (mword
)obj
+ size
)
4065 DEBUG (0, fprintf (gc_debug_file
, "Object %p referenced in saved reg %d of thread %p (id %p)\n", obj
, j
, info
, (gpointer
)mono_thread_info_get_tid (info
)));
4066 } END_FOREACH_THREAD
4071 ptr_on_stack (void *ptr
)
4073 gpointer stack_start
= &stack_start
;
4074 SgenThreadInfo
*info
= mono_thread_info_current ();
4076 if (ptr
>= stack_start
&& ptr
< (gpointer
)info
->stack_end
)
4082 #ifdef HEAVY_STATISTICS
4084 collect_store_remsets (RememberedSet
*remset
, mword
*bumper
)
4086 mword
*p
= remset
->data
;
4091 while (p
< remset
->store_next
) {
4092 switch ((*p
) & REMSET_TYPE_MASK
) {
4093 case REMSET_LOCATION
:
4096 ++stat_saved_remsets_1
;
4098 if (*p
== last1
|| *p
== last2
) {
4099 ++stat_saved_remsets_2
;
4116 g_assert_not_reached ();
4126 RememberedSet
*remset
;
4128 SgenThreadInfo
*info
;
4130 mword
*addresses
, *bumper
, *p
, *r
;
4132 FOREACH_THREAD (info
) {
4133 for (remset
= info
->remset
; remset
; remset
= remset
->next
)
4134 size
+= remset
->store_next
- remset
->data
;
4135 } END_FOREACH_THREAD
4136 for (remset
= freed_thread_remsets
; remset
; remset
= remset
->next
)
4137 size
+= remset
->store_next
- remset
->data
;
4138 for (remset
= global_remset
; remset
; remset
= remset
->next
)
4139 size
+= remset
->store_next
- remset
->data
;
4141 bumper
= addresses
= mono_sgen_alloc_internal_dynamic (sizeof (mword
) * size
, INTERNAL_MEM_STATISTICS
);
4143 FOREACH_THREAD (info
) {
4144 for (remset
= info
->remset
; remset
; remset
= remset
->next
)
4145 bumper
= collect_store_remsets (remset
, bumper
);
4146 } END_FOREACH_THREAD
4147 for (remset
= global_remset
; remset
; remset
= remset
->next
)
4148 bumper
= collect_store_remsets (remset
, bumper
);
4149 for (remset
= freed_thread_remsets
; remset
; remset
= remset
->next
)
4150 bumper
= collect_store_remsets (remset
, bumper
);
4152 g_assert (bumper
<= addresses
+ size
);
4154 stat_store_remsets
+= bumper
- addresses
;
4156 mono_sgen_sort_addresses ((void**)addresses
, bumper
- addresses
);
4159 while (r
< bumper
) {
4165 stat_store_remsets_unique
+= p
- addresses
;
4167 mono_sgen_free_internal_dynamic (addresses
, sizeof (mword
) * size
, INTERNAL_MEM_STATISTICS
);
4172 sgen_thread_register (SgenThreadInfo
* info
, void *addr
)
4174 #ifndef HAVE_KW_THREAD
4175 SgenThreadInfo
*__thread_info__
= info
;
4179 #ifndef HAVE_KW_THREAD
4180 info
->tlab_start
= info
->tlab_next
= info
->tlab_temp_end
= info
->tlab_real_end
= NULL
;
4182 g_assert (!mono_native_tls_get_value (thread_info_key
));
4183 mono_native_tls_set_value (thread_info_key
, info
);
4188 #if !defined(__MACH__)
4189 info
->stop_count
= -1;
4193 info
->doing_handshake
= FALSE
;
4194 info
->thread_is_dying
= FALSE
;
4195 info
->stack_start
= NULL
;
4196 info
->store_remset_buffer_addr
= &STORE_REMSET_BUFFER
;
4197 info
->store_remset_buffer_index_addr
= &STORE_REMSET_BUFFER_INDEX
;
4198 info
->stopped_ip
= NULL
;
4199 info
->stopped_domain
= NULL
;
4201 info
->monoctx
= NULL
;
4203 info
->stopped_regs
= NULL
;
4206 mono_sgen_init_tlab_info (info
);
4208 binary_protocol_thread_register ((gpointer
)mono_thread_info_get_tid (info
));
4210 #ifdef HAVE_KW_THREAD
4211 store_remset_buffer_index_addr
= &store_remset_buffer_index
;
4214 #if defined(__MACH__)
4215 info
->mach_port
= mach_thread_self ();
4218 /* try to get it with attributes first */
4219 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
4223 pthread_attr_t attr
;
4224 pthread_getattr_np (pthread_self (), &attr
);
4225 pthread_attr_getstack (&attr
, &sstart
, &size
);
4226 info
->stack_start_limit
= sstart
;
4227 info
->stack_end
= (char*)sstart
+ size
;
4228 pthread_attr_destroy (&attr
);
4230 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
4231 info
->stack_end
= (char*)pthread_get_stackaddr_np (pthread_self ());
4232 info
->stack_start_limit
= (char*)info
->stack_end
- pthread_get_stacksize_np (pthread_self ());
4235 /* FIXME: we assume the stack grows down */
4236 gsize stack_bottom
= (gsize
)addr
;
4237 stack_bottom
+= 4095;
4238 stack_bottom
&= ~4095;
4239 info
->stack_end
= (char*)stack_bottom
;
4243 #ifdef HAVE_KW_THREAD
4244 stack_end
= info
->stack_end
;
4247 info
->remset
= alloc_remset (DEFAULT_REMSET_SIZE
, info
, FALSE
);
4248 mono_native_tls_set_value (remembered_set_key
, info
->remset
);
4249 #ifdef HAVE_KW_THREAD
4250 remembered_set
= info
->remset
;
4253 STORE_REMSET_BUFFER
= mono_sgen_alloc_internal (INTERNAL_MEM_STORE_REMSET
);
4254 STORE_REMSET_BUFFER_INDEX
= 0;
4256 DEBUG (3, fprintf (gc_debug_file
, "registered thread %p (%p) stack end %p\n", info
, (gpointer
)mono_thread_info_get_tid (info
), info
->stack_end
));
4258 if (gc_callbacks
.thread_attach_func
)
4259 info
->runtime_data
= gc_callbacks
.thread_attach_func ();
4266 mono_sgen_wbarrier_cleanup_thread (SgenThreadInfo
*p
)
4269 mono_sgen_ssb_cleanup_thread (p
);
4273 sgen_thread_unregister (SgenThreadInfo
*p
)
4275 /* If a delegate is passed to native code and invoked on a thread we dont
4276 * know about, the jit will register it with mono_jit_thread_attach, but
4277 * we have no way of knowing when that thread goes away. SGen has a TSD
4278 * so we assume that if the domain is still registered, we can detach
4281 if (mono_domain_get ())
4282 mono_thread_detach (mono_thread_current ());
4284 p
->thread_is_dying
= TRUE
;
4287 There is a race condition between a thread finishing executing and been removed
4288 from the GC thread set.
4289 This happens on posix systems when TLS data is been cleaned-up, libpthread will
4290 set the thread_info slot to NULL before calling the cleanup function. This
4291 opens a window in which the thread is registered but has a NULL TLS.
4293 The suspend signal handler needs TLS data to know where to store thread state
4294 data or otherwise it will simply ignore the thread.
4296 This solution works because the thread doing STW will wait until all threads been
4297 suspended handshake back, so there is no race between the doing_hankshake test
4298 and the suspend_thread call.
4300 This is not required on systems that do synchronous STW as those can deal with
4301 the above race at suspend time.
4303 FIXME: I believe we could avoid this by using mono_thread_info_lookup when
4304 mono_thread_info_current returns NULL. Or fix mono_thread_info_lookup to do so.
4306 #if (defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED) || !defined(HAVE_PTHREAD_KILL)
4309 while (!TRYLOCK_GC
) {
4310 if (!mono_sgen_park_current_thread_if_doing_handshake (p
))
4315 binary_protocol_thread_unregister ((gpointer
)mono_thread_info_get_tid (p
));
4316 DEBUG (3, fprintf (gc_debug_file
, "unregister thread %p (%p)\n", p
, (gpointer
)mono_thread_info_get_tid (p
)));
4318 #if defined(__MACH__)
4319 mach_port_deallocate (current_task (), p
->mach_port
);
4322 if (gc_callbacks
.thread_detach_func
) {
4323 gc_callbacks
.thread_detach_func (p
->runtime_data
);
4324 p
->runtime_data
= NULL
;
4326 mono_sgen_wbarrier_cleanup_thread (p
);
4328 mono_threads_unregister_current_thread (p
);
4334 sgen_thread_attach (SgenThreadInfo
*info
)
4337 /*this is odd, can we get attached before the gc is inited?*/
4341 if (gc_callbacks
.thread_attach_func
&& !info
->runtime_data
)
4342 info
->runtime_data
= gc_callbacks
.thread_attach_func ();
4345 mono_gc_register_thread (void *baseptr
)
4347 return mono_thread_info_attach (baseptr
) != NULL
;
4351 * mono_gc_set_stack_end:
4353 * Set the end of the current threads stack to STACK_END. The stack space between
4354 * STACK_END and the real end of the threads stack will not be scanned during collections.
4357 mono_gc_set_stack_end (void *stack_end
)
4359 SgenThreadInfo
*info
;
4362 info
= mono_thread_info_current ();
4364 g_assert (stack_end
< info
->stack_end
);
4365 info
->stack_end
= stack_end
;
4370 #if USE_PTHREAD_INTERCEPT
4374 mono_gc_pthread_create (pthread_t
*new_thread
, const pthread_attr_t
*attr
, void *(*start_routine
)(void *), void *arg
)
4376 return pthread_create (new_thread
, attr
, start_routine
, arg
);
4380 mono_gc_pthread_join (pthread_t thread
, void **retval
)
4382 return pthread_join (thread
, retval
);
4386 mono_gc_pthread_detach (pthread_t thread
)
4388 return pthread_detach (thread
);
4392 mono_gc_pthread_exit (void *retval
)
4394 pthread_exit (retval
);
4397 #endif /* USE_PTHREAD_INTERCEPT */
4400 * ######################################################################
4401 * ######## Write barriers
4402 * ######################################################################
4407 mono_sgen_alloc_remset (int size
, gpointer id
, gboolean global
)
4409 return alloc_remset (size
, id
, global
);
4412 static RememberedSet
*
4413 alloc_remset (int size
, gpointer id
, gboolean global
)
4415 RememberedSet
* res
= mono_sgen_alloc_internal_dynamic (sizeof (RememberedSet
) + (size
* sizeof (gpointer
)), INTERNAL_MEM_REMSET
);
4416 res
->store_next
= res
->data
;
4417 res
->end_set
= res
->data
+ size
;
4419 DEBUG (4, fprintf (gc_debug_file
, "Allocated%s remset size %d at %p for %p\n", global
? " global" : "", size
, res
->data
, id
));
4424 * Note: the write barriers first do the needed GC work and then do the actual store:
4425 * this way the value is visible to the conservative GC scan after the write barrier
4426 * itself. If a GC interrupts the barrier in the middle, value will be kept alive by
4427 * the conservative scan, otherwise by the remembered set scan.
4430 mono_gc_wbarrier_set_field (MonoObject
*obj
, gpointer field_ptr
, MonoObject
* value
)
4432 HEAVY_STAT (++stat_wbarrier_set_field
);
4433 if (ptr_in_nursery (field_ptr
)) {
4434 *(void**)field_ptr
= value
;
4437 DEBUG (8, fprintf (gc_debug_file
, "Adding remset at %p\n", field_ptr
));
4439 binary_protocol_wbarrier (field_ptr
, value
, value
->vtable
);
4442 mono_sgen_card_table_wbarrier_set_field (obj
, field_ptr
, value
);
4444 mono_sgen_ssb_wbarrier_set_field (obj
, field_ptr
, value
);
4448 mono_gc_wbarrier_set_arrayref (MonoArray
*arr
, gpointer slot_ptr
, MonoObject
* value
)
4450 HEAVY_STAT (++stat_wbarrier_set_arrayref
);
4451 if (ptr_in_nursery (slot_ptr
)) {
4452 *(void**)slot_ptr
= value
;
4455 DEBUG (8, fprintf (gc_debug_file
, "Adding remset at %p\n", slot_ptr
));
4457 binary_protocol_wbarrier (slot_ptr
, value
, value
->vtable
);
4460 mono_sgen_card_table_wbarrier_set_arrayref (arr
, slot_ptr
, value
);
4462 mono_sgen_ssb_wbarrier_set_arrayref (arr
, slot_ptr
, value
);
4466 mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr
, gpointer src_ptr
, int count
)
4468 HEAVY_STAT (++stat_wbarrier_arrayref_copy
);
4469 /*This check can be done without taking a lock since dest_ptr array is pinned*/
4470 if (ptr_in_nursery (dest_ptr
) || count
<= 0) {
4471 mono_gc_memmove (dest_ptr
, src_ptr
, count
* sizeof (gpointer
));
4475 #ifdef SGEN_BINARY_PROTOCOL
4478 for (i
= 0; i
< count
; ++i
) {
4479 gpointer dest
= (gpointer
*)dest_ptr
+ i
;
4480 gpointer obj
= *((gpointer
*)src_ptr
+ i
);
4482 binary_protocol_wbarrier (dest
, obj
, (gpointer
)LOAD_VTABLE (obj
));
4488 mono_sgen_card_table_wbarrier_arrayref_copy (dest_ptr
, src_ptr
, count
);
4490 mono_sgen_ssb_wbarrier_arrayref_copy (dest_ptr
, src_ptr
, count
);
4493 static char *found_obj
;
4496 find_object_for_ptr_callback (char *obj
, size_t size
, void *user_data
)
4498 char *ptr
= user_data
;
4500 if (ptr
>= obj
&& ptr
< obj
+ size
) {
4501 g_assert (!found_obj
);
4506 /* for use in the debugger */
4507 char* find_object_for_ptr (char *ptr
);
4509 find_object_for_ptr (char *ptr
)
4511 if (ptr
>= nursery_section
->data
&& ptr
< nursery_section
->end_data
) {
4513 mono_sgen_scan_area_with_callback (nursery_section
->data
, nursery_section
->end_data
,
4514 find_object_for_ptr_callback
, ptr
, TRUE
);
4520 mono_sgen_los_iterate_objects (find_object_for_ptr_callback
, ptr
);
4525 * Very inefficient, but this is debugging code, supposed to
4526 * be called from gdb, so we don't care.
4529 major_collector
.iterate_objects (TRUE
, TRUE
, find_object_for_ptr_callback
, ptr
);
4534 mono_gc_wbarrier_generic_nostore (gpointer ptr
)
4536 HEAVY_STAT (++stat_wbarrier_generic_store
);
4538 #ifdef XDOMAIN_CHECKS_IN_WBARRIER
4539 /* FIXME: ptr_in_heap must be called with the GC lock held */
4540 if (xdomain_checks
&& *(MonoObject
**)ptr
&& ptr_in_heap (ptr
)) {
4541 char *start
= find_object_for_ptr (ptr
);
4542 MonoObject
*value
= *(MonoObject
**)ptr
;
4546 MonoObject
*obj
= (MonoObject
*)start
;
4547 if (obj
->vtable
->domain
!= value
->vtable
->domain
)
4548 g_assert (is_xdomain_ref_allowed (ptr
, start
, obj
->vtable
->domain
));
4554 if (*(gpointer
*)ptr
)
4555 binary_protocol_wbarrier (ptr
, *(gpointer
*)ptr
, (gpointer
)LOAD_VTABLE (*(gpointer
*)ptr
));
4557 if (ptr_in_nursery (ptr
) || ptr_on_stack (ptr
) || !ptr_in_nursery (*(gpointer
*)ptr
)) {
4558 DEBUG (8, fprintf (gc_debug_file
, "Skipping remset at %p\n", ptr
));
4562 DEBUG (8, fprintf (gc_debug_file
, "Adding remset at %p\n", ptr
));
4565 mono_sgen_card_table_wbarrier_generic_nostore (ptr
);
4567 mono_sgen_ssb_wbarrier_generic_nostore (ptr
);
4571 mono_gc_wbarrier_generic_store (gpointer ptr
, MonoObject
* value
)
4573 DEBUG (8, fprintf (gc_debug_file
, "Wbarrier store at %p to %p (%s)\n", ptr
, value
, value
? safe_name (value
) : "null"));
4574 *(void**)ptr
= value
;
4575 if (ptr_in_nursery (value
))
4576 mono_gc_wbarrier_generic_nostore (ptr
);
4577 mono_sgen_dummy_use (value
);
4580 void mono_gc_wbarrier_value_copy_bitmap (gpointer _dest
, gpointer _src
, int size
, unsigned bitmap
)
4582 mword
*dest
= _dest
;
4587 mono_gc_wbarrier_generic_store (dest
, (MonoObject
*)*src
);
4592 size
-= SIZEOF_VOID_P
;
4597 #ifdef SGEN_BINARY_PROTOCOL
4599 #define HANDLE_PTR(ptr,obj) do { \
4600 gpointer o = *(gpointer*)(ptr); \
4602 gpointer d = ((char*)dest) + ((char*)(ptr) - (char*)(obj)); \
4603 binary_protocol_wbarrier (d, o, (gpointer) LOAD_VTABLE (o)); \
4608 scan_object_for_binary_protocol_copy_wbarrier (gpointer dest
, char *start
, mword desc
)
4610 #define SCAN_OBJECT_NOVTABLE
4611 #include "sgen-scan-object.h"
4616 mono_gc_wbarrier_value_copy (gpointer dest
, gpointer src
, int count
, MonoClass
*klass
)
4618 HEAVY_STAT (++stat_wbarrier_value_copy
);
4619 g_assert (klass
->valuetype
);
4621 DEBUG (8, fprintf (gc_debug_file
, "Adding value remset at %p, count %d, descr %p for class %s (%p)\n", dest
, count
, klass
->gc_descr
, klass
->name
, klass
));
4623 if (ptr_in_nursery (dest
) || ptr_on_stack (dest
) || !SGEN_CLASS_HAS_REFERENCES (klass
)) {
4624 size_t element_size
= mono_class_value_size (klass
, NULL
);
4625 size_t size
= count
* element_size
;
4626 mono_gc_memmove (dest
, src
, size
);
4630 #ifdef SGEN_BINARY_PROTOCOL
4633 for (i
= 0; i
< count
; ++i
) {
4634 scan_object_for_binary_protocol_copy_wbarrier ((char*)dest
+ i
* element_size
,
4635 (char*)src
+ i
* element_size
- sizeof (MonoObject
),
4636 (mword
) klass
->gc_descr
);
4642 mono_sgen_card_table_wbarrier_value_copy (dest
, src
, count
, klass
);
4644 mono_sgen_ssb_wbarrier_value_copy (dest
, src
, count
, klass
);
4648 * mono_gc_wbarrier_object_copy:
4650 * Write barrier to call when obj is the result of a clone or copy of an object.
4653 mono_gc_wbarrier_object_copy (MonoObject
* obj
, MonoObject
*src
)
4657 if (ptr_in_nursery (obj
) || ptr_on_stack (obj
)) {
4658 size
= mono_object_class (obj
)->instance_size
;
4659 mono_gc_memmove ((char*)obj
+ sizeof (MonoObject
), (char*)src
+ sizeof (MonoObject
),
4660 size
- sizeof (MonoObject
));
4664 #ifdef SGEN_BINARY_PROTOCOL
4665 scan_object_for_binary_protocol_copy_wbarrier (obj
, (char*)src
, (mword
) src
->vtable
->gc_descr
);
4669 mono_sgen_card_table_wbarrier_object_copy (obj
, src
);
4671 mono_sgen_ssb_wbarrier_object_copy (obj
, src
);
4675 * ######################################################################
4676 * ######## Other mono public interface functions.
4677 * ######################################################################
4680 #define REFS_SIZE 128
4683 MonoGCReferences callback
;
4687 MonoObject
*refs
[REFS_SIZE
];
4688 uintptr_t offsets
[REFS_SIZE
];
4692 #define HANDLE_PTR(ptr,obj) do { \
4694 if (hwi->count == REFS_SIZE) { \
4695 hwi->callback ((MonoObject*)start, mono_object_class (start), hwi->called? 0: size, hwi->count, hwi->refs, hwi->offsets, hwi->data); \
4699 hwi->offsets [hwi->count] = (char*)(ptr)-(char*)start; \
4700 hwi->refs [hwi->count++] = *(ptr); \
4705 collect_references (HeapWalkInfo
*hwi
, char *start
, size_t size
)
4707 #include "sgen-scan-object.h"
4711 walk_references (char *start
, size_t size
, void *data
)
4713 HeapWalkInfo
*hwi
= data
;
4716 collect_references (hwi
, start
, size
);
4717 if (hwi
->count
|| !hwi
->called
)
4718 hwi
->callback ((MonoObject
*)start
, mono_object_class (start
), hwi
->called
? 0: size
, hwi
->count
, hwi
->refs
, hwi
->offsets
, hwi
->data
);
4722 * mono_gc_walk_heap:
4723 * @flags: flags for future use
4724 * @callback: a function pointer called for each object in the heap
4725 * @data: a user data pointer that is passed to callback
4727 * This function can be used to iterate over all the live objects in the heap:
4728 * for each object, @callback is invoked, providing info about the object's
4729 * location in memory, its class, its size and the objects it references.
4730 * For each referenced object it's offset from the object address is
4731 * reported in the offsets array.
4732 * The object references may be buffered, so the callback may be invoked
4733 * multiple times for the same object: in all but the first call, the size
4734 * argument will be zero.
4735 * Note that this function can be only called in the #MONO_GC_EVENT_PRE_START_WORLD
4736 * profiler event handler.
4738 * Returns: a non-zero value if the GC doesn't support heap walking
4741 mono_gc_walk_heap (int flags
, MonoGCReferences callback
, void *data
)
4746 hwi
.callback
= callback
;
4749 mono_sgen_clear_nursery_fragments ();
4750 mono_sgen_scan_area_with_callback (nursery_section
->data
, nursery_section
->end_data
, walk_references
, &hwi
, FALSE
);
4752 major_collector
.iterate_objects (TRUE
, TRUE
, walk_references
, &hwi
);
4753 mono_sgen_los_iterate_objects (walk_references
, &hwi
);
4759 mono_gc_collect (int generation
)
4764 mono_profiler_gc_event (MONO_GC_EVENT_START
, generation
);
4765 stop_world (generation
);
4766 if (generation
== 0) {
4767 collect_nursery (0);
4769 major_collection ("user request");
4771 restart_world (generation
);
4772 mono_profiler_gc_event (MONO_GC_EVENT_END
, generation
);
4777 mono_gc_max_generation (void)
4783 mono_gc_collection_count (int generation
)
4785 if (generation
== 0)
4786 return stat_minor_gcs
;
4787 return stat_major_gcs
;
4791 mono_gc_get_used_size (void)
4795 tot
= los_memory_usage
;
4796 tot
+= nursery_section
->next_data
- nursery_section
->data
;
4797 tot
+= major_collector
.get_used_size ();
4798 /* FIXME: account for pinned objects */
4804 mono_gc_get_heap_size (void)
4810 mono_gc_disable (void)
4818 mono_gc_enable (void)
4826 mono_gc_get_los_limit (void)
4828 return MAX_SMALL_OBJ_SIZE
;
4832 mono_object_is_alive (MonoObject
* o
)
4838 mono_gc_get_generation (MonoObject
*obj
)
4840 if (ptr_in_nursery (obj
))
4846 mono_gc_enable_events (void)
4851 mono_gc_weak_link_add (void **link_addr
, MonoObject
*obj
, gboolean track
)
4853 mono_gc_register_disappearing_link (obj
, link_addr
, track
, FALSE
);
4857 mono_gc_weak_link_remove (void **link_addr
)
4859 mono_gc_register_disappearing_link (NULL
, link_addr
, FALSE
, FALSE
);
4863 mono_gc_weak_link_get (void **link_addr
)
4867 return (MonoObject
*) REVEAL_POINTER (*link_addr
);
4871 mono_gc_ephemeron_array_add (MonoObject
*obj
)
4873 EphemeronLinkNode
*node
;
4877 node
= mono_sgen_alloc_internal (INTERNAL_MEM_EPHEMERON_LINK
);
4882 node
->array
= (char*)obj
;
4883 node
->next
= ephemeron_list
;
4884 ephemeron_list
= node
;
4886 DEBUG (5, fprintf (gc_debug_file
, "Registered ephemeron array %p\n", obj
));
4893 mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func
, void *data
)
4897 result
= func (data
);
4898 UNLOCK_INTERRUPTION
;
4903 mono_gc_is_gc_thread (void)
4907 result
= mono_thread_info_current () != NULL
;
4913 is_critical_method (MonoMethod
*method
)
4915 return mono_runtime_is_critical_method (method
) || mono_gc_is_critical_method (method
);
4919 mono_gc_base_init (void)
4921 MonoThreadInfoCallbacks cb
;
4924 char *major_collector_opt
= NULL
;
4926 glong soft_limit
= 0;
4932 result
= InterlockedCompareExchange (&gc_initialized
, -1, 0);
4935 /* already inited */
4938 /* being inited by another thread */
4942 /* we will init it */
4945 g_assert_not_reached ();
4947 } while (result
!= 0);
4949 LOCK_INIT (gc_mutex
);
4951 pagesize
= mono_pagesize ();
4952 gc_debug_file
= stderr
;
4954 cb
.thread_register
= sgen_thread_register
;
4955 cb
.thread_unregister
= sgen_thread_unregister
;
4956 cb
.thread_attach
= sgen_thread_attach
;
4957 cb
.mono_method_is_critical
= (gpointer
)is_critical_method
;
4959 cb
.mono_gc_pthread_create
= (gpointer
)mono_gc_pthread_create
;
4962 mono_threads_init (&cb
, sizeof (SgenThreadInfo
));
4964 LOCK_INIT (interruption_mutex
);
4965 LOCK_INIT (pin_queue_mutex
);
4967 init_user_copy_or_mark_key ();
4969 if ((env
= getenv ("MONO_GC_PARAMS"))) {
4970 opts
= g_strsplit (env
, ",", -1);
4971 for (ptr
= opts
; *ptr
; ++ptr
) {
4973 if (g_str_has_prefix (opt
, "major=")) {
4974 opt
= strchr (opt
, '=') + 1;
4975 major_collector_opt
= g_strdup (opt
);
4983 mono_sgen_init_internal_allocator ();
4984 mono_sgen_init_nursery_allocator ();
4986 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_SECTION
, SGEN_SIZEOF_GC_MEM_SECTION
);
4987 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_FINALIZE_READY_ENTRY
, sizeof (FinalizeReadyEntry
));
4988 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_GRAY_QUEUE
, sizeof (GrayQueueSection
));
4989 g_assert (sizeof (GenericStoreRememberedSet
) == sizeof (gpointer
) * STORE_REMSET_BUFFER_SIZE
);
4990 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_STORE_REMSET
, sizeof (GenericStoreRememberedSet
));
4991 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_EPHEMERON_LINK
, sizeof (EphemeronLinkNode
));
4993 mono_native_tls_alloc (&remembered_set_key
, NULL
);
4995 #ifndef HAVE_KW_THREAD
4996 mono_native_tls_alloc (&thread_info_key
, NULL
);
5000 * This needs to happen before any internal allocations because
5001 * it inits the small id which is required for hazard pointer
5004 mono_sgen_os_init ();
5006 mono_thread_info_attach (&dummy
);
5008 if (!major_collector_opt
|| !strcmp (major_collector_opt
, "marksweep")) {
5009 mono_sgen_marksweep_init (&major_collector
);
5010 } else if (!major_collector_opt
|| !strcmp (major_collector_opt
, "marksweep-fixed")) {
5011 mono_sgen_marksweep_fixed_init (&major_collector
);
5012 } else if (!major_collector_opt
|| !strcmp (major_collector_opt
, "marksweep-par")) {
5013 mono_sgen_marksweep_par_init (&major_collector
);
5014 } else if (!major_collector_opt
|| !strcmp (major_collector_opt
, "marksweep-fixed-par")) {
5015 mono_sgen_marksweep_fixed_par_init (&major_collector
);
5016 } else if (!strcmp (major_collector_opt
, "copying")) {
5017 mono_sgen_copying_init (&major_collector
);
5019 fprintf (stderr
, "Unknown major collector `%s'.\n", major_collector_opt
);
5023 #ifdef SGEN_HAVE_CARDTABLE
5024 use_cardtable
= major_collector
.supports_cardtable
;
5026 use_cardtable
= FALSE
;
5029 num_workers
= mono_cpu_count ();
5030 g_assert (num_workers
> 0);
5031 if (num_workers
> 16)
5034 ///* Keep this the default for now */
5035 //conservative_stack_mark = TRUE;
5038 for (ptr
= opts
; *ptr
; ++ptr
) {
5040 if (g_str_has_prefix (opt
, "major="))
5042 if (g_str_has_prefix (opt
, "wbarrier=")) {
5043 opt
= strchr (opt
, '=') + 1;
5044 if (strcmp (opt
, "remset") == 0) {
5045 use_cardtable
= FALSE
;
5046 } else if (strcmp (opt
, "cardtable") == 0) {
5047 if (!use_cardtable
) {
5048 if (major_collector
.supports_cardtable
)
5049 fprintf (stderr
, "The cardtable write barrier is not supported on this platform.\n");
5051 fprintf (stderr
, "The major collector does not support the cardtable write barrier.\n");
5057 if (g_str_has_prefix (opt
, "max-heap-size=")) {
5058 opt
= strchr (opt
, '=') + 1;
5059 if (*opt
&& mono_gc_parse_environment_string_extract_number (opt
, &max_heap
)) {
5060 if ((max_heap
& (mono_pagesize () - 1))) {
5061 fprintf (stderr
, "max-heap-size size must be a multiple of %d.\n", mono_pagesize ());
5065 fprintf (stderr
, "max-heap-size must be an integer.\n");
5070 if (g_str_has_prefix (opt
, "soft-heap-limit=")) {
5071 opt
= strchr (opt
, '=') + 1;
5072 if (*opt
&& mono_gc_parse_environment_string_extract_number (opt
, &soft_limit
)) {
5073 if (soft_limit
<= 0) {
5074 fprintf (stderr
, "soft-heap-limit must be positive.\n");
5078 fprintf (stderr
, "soft-heap-limit must be an integer.\n");
5083 if (g_str_has_prefix (opt
, "workers=")) {
5086 if (!major_collector
.is_parallel
) {
5087 fprintf (stderr
, "The workers= option can only be used for parallel collectors.");
5090 opt
= strchr (opt
, '=') + 1;
5091 val
= strtol (opt
, &endptr
, 10);
5092 if (!*opt
|| *endptr
) {
5093 fprintf (stderr
, "Cannot parse the workers= option value.");
5096 if (val
<= 0 || val
> 16) {
5097 fprintf (stderr
, "The number of workers must be in the range 1 to 16.");
5100 num_workers
= (int)val
;
5103 if (g_str_has_prefix (opt
, "stack-mark=")) {
5104 opt
= strchr (opt
, '=') + 1;
5105 if (!strcmp (opt
, "precise")) {
5106 conservative_stack_mark
= FALSE
;
5107 } else if (!strcmp (opt
, "conservative")) {
5108 conservative_stack_mark
= TRUE
;
5110 fprintf (stderr
, "Invalid value '%s' for stack-mark= option, possible values are: 'precise', 'conservative'.\n", opt
);
5115 if (g_str_has_prefix (opt
, "bridge=")) {
5116 opt
= strchr (opt
, '=') + 1;
5117 mono_sgen_register_test_bridge_callbacks (g_strdup (opt
));
5121 if (g_str_has_prefix (opt
, "nursery-size=")) {
5123 opt
= strchr (opt
, '=') + 1;
5124 if (*opt
&& mono_gc_parse_environment_string_extract_number (opt
, &val
)) {
5125 default_nursery_size
= val
;
5126 #ifdef SGEN_ALIGN_NURSERY
5127 if ((val
& (val
- 1))) {
5128 fprintf (stderr
, "The nursery size must be a power of two.\n");
5132 if (val
< SGEN_MAX_NURSERY_WASTE
) {
5133 fprintf (stderr
, "The nursery size must be at least %d bytes.\n", SGEN_MAX_NURSERY_WASTE
);
5137 default_nursery_bits
= 0;
5138 while (1 << (++ default_nursery_bits
) != default_nursery_size
)
5142 fprintf (stderr
, "nursery-size must be an integer.\n");
5148 if (!(major_collector
.handle_gc_param
&& major_collector
.handle_gc_param (opt
))) {
5149 fprintf (stderr
, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
5150 fprintf (stderr
, " max-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
5151 fprintf (stderr
, " soft-heap-limit=n (where N is an integer, possibly with a k, m or a g suffix)\n");
5152 fprintf (stderr
, " nursery-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
5153 fprintf (stderr
, " major=COLLECTOR (where COLLECTOR is `marksweep', `marksweep-par' or `copying')\n");
5154 fprintf (stderr
, " wbarrier=WBARRIER (where WBARRIER is `remset' or `cardtable')\n");
5155 fprintf (stderr
, " stack-mark=MARK-METHOD (where MARK-METHOD is 'precise' or 'conservative')\n");
5156 if (major_collector
.print_gc_param_usage
)
5157 major_collector
.print_gc_param_usage ();
5164 if (major_collector
.is_parallel
)
5165 mono_sgen_workers_init (num_workers
);
5167 if (major_collector_opt
)
5168 g_free (major_collector_opt
);
5170 nursery_size
= DEFAULT_NURSERY_SIZE
;
5171 minor_collection_allowance
= MIN_MINOR_COLLECTION_ALLOWANCE
;
5172 init_heap_size_limits (max_heap
, soft_limit
);
5176 if ((env
= getenv ("MONO_GC_DEBUG"))) {
5177 opts
= g_strsplit (env
, ",", -1);
5178 for (ptr
= opts
; ptr
&& *ptr
; ptr
++) {
5180 if (opt
[0] >= '0' && opt
[0] <= '9') {
5181 gc_debug_level
= atoi (opt
);
5186 char *rf
= g_strdup_printf ("%s.%d", opt
, getpid ());
5187 gc_debug_file
= fopen (rf
, "wb");
5189 gc_debug_file
= stderr
;
5192 } else if (!strcmp (opt
, "print-allowance")) {
5193 debug_print_allowance
= TRUE
;
5194 } else if (!strcmp (opt
, "print-pinning")) {
5195 do_pin_stats
= TRUE
;
5196 } else if (!strcmp (opt
, "collect-before-allocs")) {
5197 collect_before_allocs
= 1;
5198 } else if (g_str_has_prefix (opt
, "collect-before-allocs=")) {
5199 char *arg
= strchr (opt
, '=') + 1;
5200 collect_before_allocs
= atoi (arg
);
5201 } else if (!strcmp (opt
, "check-at-minor-collections")) {
5202 consistency_check_at_minor_collection
= TRUE
;
5203 nursery_clear_policy
= CLEAR_AT_GC
;
5204 } else if (!strcmp (opt
, "xdomain-checks")) {
5205 xdomain_checks
= TRUE
;
5206 } else if (!strcmp (opt
, "clear-at-gc")) {
5207 nursery_clear_policy
= CLEAR_AT_GC
;
5208 } else if (!strcmp (opt
, "clear-nursery-at-gc")) {
5209 nursery_clear_policy
= CLEAR_AT_GC
;
5210 } else if (!strcmp (opt
, "check-scan-starts")) {
5211 do_scan_starts_check
= TRUE
;
5212 } else if (!strcmp (opt
, "verify-nursery-at-minor-gc")) {
5213 do_verify_nursery
= TRUE
;
5214 } else if (!strcmp (opt
, "dump-nursery-at-minor-gc")) {
5215 do_dump_nursery_content
= TRUE
;
5216 } else if (!strcmp (opt
, "disable-minor")) {
5217 disable_minor_collections
= TRUE
;
5218 } else if (!strcmp (opt
, "disable-major")) {
5219 disable_major_collections
= TRUE
;
5220 } else if (g_str_has_prefix (opt
, "heap-dump=")) {
5221 char *filename
= strchr (opt
, '=') + 1;
5222 nursery_clear_policy
= CLEAR_AT_GC
;
5223 heap_dump_file
= fopen (filename
, "w");
5224 if (heap_dump_file
) {
5225 fprintf (heap_dump_file
, "<sgen-dump>\n");
5226 do_pin_stats
= TRUE
;
5228 #ifdef SGEN_BINARY_PROTOCOL
5229 } else if (g_str_has_prefix (opt
, "binary-protocol=")) {
5230 char *filename
= strchr (opt
, '=') + 1;
5231 binary_protocol_init (filename
);
5233 fprintf (stderr
, "Warning: Cardtable write barriers will not be binary-protocolled.\n");
5236 fprintf (stderr
, "Invalid format for the MONO_GC_DEBUG env variable: '%s'\n", env
);
5237 fprintf (stderr
, "The format is: MONO_GC_DEBUG=[l[:filename]|<option>]+ where l is a debug level 0-9.\n");
5238 fprintf (stderr
, "Valid options are:\n");
5239 fprintf (stderr
, " collect-before-allocs[=<n>]\n");
5240 fprintf (stderr
, " check-at-minor-collections\n");
5241 fprintf (stderr
, " disable-minor\n");
5242 fprintf (stderr
, " disable-major\n");
5243 fprintf (stderr
, " xdomain-checks\n");
5244 fprintf (stderr
, " clear-at-gc\n");
5245 fprintf (stderr
, " print-allowance\n");
5246 fprintf (stderr
, " print-pinning\n");
5253 if (major_collector
.is_parallel
) {
5254 if (heap_dump_file
) {
5255 fprintf (stderr
, "Error: Cannot do heap dump with the parallel collector.\n");
5259 fprintf (stderr
, "Error: Cannot gather pinning statistics with the parallel collector.\n");
5264 if (major_collector
.post_param_init
)
5265 major_collector
.post_param_init ();
5267 global_remset
= alloc_remset (1024, NULL
, FALSE
);
5268 global_remset
->next
= NULL
;
5271 sgen_card_table_init ();
5273 mono_sgen_ssb_init ();
5279 mono_gc_get_gc_name (void)
5284 static MonoMethod
*write_barrier_method
;
5287 mono_gc_is_critical_method (MonoMethod
*method
)
5289 return (method
== write_barrier_method
|| mono_sgen_is_managed_allocator (method
));
5293 is_ip_in_managed_allocator (MonoDomain
*domain
, gpointer ip
)
5297 if (!mono_thread_internal_current ())
5298 /* Happens during thread attach */
5303 ji
= mono_jit_info_table_find (domain
, ip
);
5307 return mono_gc_is_critical_method (ji
->method
);
5311 emit_nursery_check (MonoMethodBuilder
*mb
, int *nursery_check_return_labels
)
5313 memset (nursery_check_return_labels
, 0, sizeof (int) * 3);
5314 #ifdef SGEN_ALIGN_NURSERY
5315 // if (ptr_in_nursery (ptr)) return;
5317 * Masking out the bits might be faster, but we would have to use 64 bit
5318 * immediates, which might be slower.
5320 mono_mb_emit_ldarg (mb
, 0);
5321 mono_mb_emit_icon (mb
, DEFAULT_NURSERY_BITS
);
5322 mono_mb_emit_byte (mb
, CEE_SHR_UN
);
5323 mono_mb_emit_icon (mb
, (mword
)nursery_start
>> DEFAULT_NURSERY_BITS
);
5324 nursery_check_return_labels
[0] = mono_mb_emit_branch (mb
, CEE_BEQ
);
5326 // if (!ptr_in_nursery (*ptr)) return;
5327 mono_mb_emit_ldarg (mb
, 0);
5328 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5329 mono_mb_emit_icon (mb
, DEFAULT_NURSERY_BITS
);
5330 mono_mb_emit_byte (mb
, CEE_SHR_UN
);
5331 mono_mb_emit_icon (mb
, (mword
)nursery_start
>> DEFAULT_NURSERY_BITS
);
5332 nursery_check_return_labels
[1] = mono_mb_emit_branch (mb
, CEE_BNE_UN
);
5334 int label_continue1
, label_continue2
;
5335 int dereferenced_var
;
5337 // if (ptr < (nursery_start)) goto continue;
5338 mono_mb_emit_ldarg (mb
, 0);
5339 mono_mb_emit_ptr (mb
, (gpointer
) nursery_start
);
5340 label_continue_1
= mono_mb_emit_branch (mb
, CEE_BLT
);
5342 // if (ptr >= nursery_end)) goto continue;
5343 mono_mb_emit_ldarg (mb
, 0);
5344 mono_mb_emit_ptr (mb
, (gpointer
) nursery_end
);
5345 label_continue_2
= mono_mb_emit_branch (mb
, CEE_BGE
);
5348 nursery_check_return_labels
[0] = mono_mb_emit_branch (mb
, CEE_BR
);
5351 mono_mb_patch_branch (mb
, label_continue_1
);
5352 mono_mb_patch_branch (mb
, label_continue_2
);
5354 // Dereference and store in local var
5355 dereferenced_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5356 mono_mb_emit_ldarg (mb
, 0);
5357 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5358 mono_mb_emit_stloc (mb
, dereferenced_var
);
5360 // if (*ptr < nursery_start) return;
5361 mono_mb_emit_ldloc (mb
, dereferenced_var
);
5362 mono_mb_emit_ptr (mb
, (gpointer
) nursery_start
);
5363 nursery_check_return_labels
[1] = mono_mb_emit_branch (mb
, CEE_BLT
);
5365 // if (*ptr >= nursery_end) return;
5366 mono_mb_emit_ldloc (mb
, dereferenced_var
);
5367 mono_mb_emit_ptr (mb
, (gpointer
) nursery_end
);
5368 nursery_check_return_labels
[2] = mono_mb_emit_branch (mb
, CEE_BGE
);
5373 mono_gc_get_write_barrier (void)
5376 MonoMethodBuilder
*mb
;
5377 MonoMethodSignature
*sig
;
5378 #ifdef MANAGED_WBARRIER
5379 int i
, nursery_check_labels
[3];
5380 int label_no_wb_3
, label_no_wb_4
, label_need_wb
, label_slow_path
;
5381 int buffer_var
, buffer_index_var
, dummy_var
;
5383 #ifdef HAVE_KW_THREAD
5384 int stack_end_offset
= -1, store_remset_buffer_offset
= -1;
5385 int store_remset_buffer_index_offset
= -1, store_remset_buffer_index_addr_offset
= -1;
5387 MONO_THREAD_VAR_OFFSET (stack_end
, stack_end_offset
);
5388 g_assert (stack_end_offset
!= -1);
5389 MONO_THREAD_VAR_OFFSET (store_remset_buffer
, store_remset_buffer_offset
);
5390 g_assert (store_remset_buffer_offset
!= -1);
5391 MONO_THREAD_VAR_OFFSET (store_remset_buffer_index
, store_remset_buffer_index_offset
);
5392 g_assert (store_remset_buffer_index_offset
!= -1);
5393 MONO_THREAD_VAR_OFFSET (store_remset_buffer_index_addr
, store_remset_buffer_index_addr_offset
);
5394 g_assert (store_remset_buffer_index_addr_offset
!= -1);
5398 // FIXME: Maybe create a separate version for ctors (the branch would be
5399 // correctly predicted more times)
5400 if (write_barrier_method
)
5401 return write_barrier_method
;
5403 /* Create the IL version of mono_gc_barrier_generic_store () */
5404 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
5405 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5406 sig
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
5408 mb
= mono_mb_new (mono_defaults
.object_class
, "wbarrier", MONO_WRAPPER_WRITE_BARRIER
);
5410 #ifdef MANAGED_WBARRIER
5411 if (use_cardtable
) {
5412 emit_nursery_check (mb
, nursery_check_labels
);
5414 addr = sgen_cardtable + ((address >> CARD_BITS) & CARD_MASK)
5418 LDC_PTR sgen_cardtable
5420 address >> CARD_BITS
5424 if (SGEN_HAVE_OVERLAPPING_CARDS) {
5425 LDC_PTR card_table_mask
5432 mono_mb_emit_ptr (mb
, sgen_cardtable
);
5433 mono_mb_emit_ldarg (mb
, 0);
5434 mono_mb_emit_icon (mb
, CARD_BITS
);
5435 mono_mb_emit_byte (mb
, CEE_SHR_UN
);
5436 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
5437 mono_mb_emit_ptr (mb
, (gpointer
)CARD_MASK
);
5438 mono_mb_emit_byte (mb
, CEE_AND
);
5440 mono_mb_emit_byte (mb
, CEE_ADD
);
5441 mono_mb_emit_icon (mb
, 1);
5442 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
5445 for (i
= 0; i
< 3; ++i
) {
5446 if (nursery_check_labels
[i
])
5447 mono_mb_patch_branch (mb
, nursery_check_labels
[i
]);
5449 mono_mb_emit_byte (mb
, CEE_RET
);
5450 } else if (mono_runtime_has_tls_get ()) {
5451 emit_nursery_check (mb
, nursery_check_labels
);
5453 // if (ptr >= stack_end) goto need_wb;
5454 mono_mb_emit_ldarg (mb
, 0);
5455 EMIT_TLS_ACCESS (mb
, stack_end
, stack_end_offset
);
5456 label_need_wb
= mono_mb_emit_branch (mb
, CEE_BGE_UN
);
5458 // if (ptr >= stack_start) return;
5459 dummy_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5460 mono_mb_emit_ldarg (mb
, 0);
5461 mono_mb_emit_ldloc_addr (mb
, dummy_var
);
5462 label_no_wb_3
= mono_mb_emit_branch (mb
, CEE_BGE_UN
);
5465 mono_mb_patch_branch (mb
, label_need_wb
);
5467 // buffer = STORE_REMSET_BUFFER;
5468 buffer_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5469 EMIT_TLS_ACCESS (mb
, store_remset_buffer
, store_remset_buffer_offset
);
5470 mono_mb_emit_stloc (mb
, buffer_var
);
5472 // buffer_index = STORE_REMSET_BUFFER_INDEX;
5473 buffer_index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5474 EMIT_TLS_ACCESS (mb
, store_remset_buffer_index
, store_remset_buffer_index_offset
);
5475 mono_mb_emit_stloc (mb
, buffer_index_var
);
5477 // if (buffer [buffer_index] == ptr) return;
5478 mono_mb_emit_ldloc (mb
, buffer_var
);
5479 mono_mb_emit_ldloc (mb
, buffer_index_var
);
5480 g_assert (sizeof (gpointer
) == 4 || sizeof (gpointer
) == 8);
5481 mono_mb_emit_icon (mb
, sizeof (gpointer
) == 4 ? 2 : 3);
5482 mono_mb_emit_byte (mb
, CEE_SHL
);
5483 mono_mb_emit_byte (mb
, CEE_ADD
);
5484 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5485 mono_mb_emit_ldarg (mb
, 0);
5486 label_no_wb_4
= mono_mb_emit_branch (mb
, CEE_BEQ
);
5489 mono_mb_emit_ldloc (mb
, buffer_index_var
);
5490 mono_mb_emit_icon (mb
, 1);
5491 mono_mb_emit_byte (mb
, CEE_ADD
);
5492 mono_mb_emit_stloc (mb
, buffer_index_var
);
5494 // if (buffer_index >= STORE_REMSET_BUFFER_SIZE) goto slow_path;
5495 mono_mb_emit_ldloc (mb
, buffer_index_var
);
5496 mono_mb_emit_icon (mb
, STORE_REMSET_BUFFER_SIZE
);
5497 label_slow_path
= mono_mb_emit_branch (mb
, CEE_BGE
);
5499 // buffer [buffer_index] = ptr;
5500 mono_mb_emit_ldloc (mb
, buffer_var
);
5501 mono_mb_emit_ldloc (mb
, buffer_index_var
);
5502 g_assert (sizeof (gpointer
) == 4 || sizeof (gpointer
) == 8);
5503 mono_mb_emit_icon (mb
, sizeof (gpointer
) == 4 ? 2 : 3);
5504 mono_mb_emit_byte (mb
, CEE_SHL
);
5505 mono_mb_emit_byte (mb
, CEE_ADD
);
5506 mono_mb_emit_ldarg (mb
, 0);
5507 mono_mb_emit_byte (mb
, CEE_STIND_I
);
5509 // STORE_REMSET_BUFFER_INDEX = buffer_index;
5510 EMIT_TLS_ACCESS (mb
, store_remset_buffer_index_addr
, store_remset_buffer_index_addr_offset
);
5511 mono_mb_emit_ldloc (mb
, buffer_index_var
);
5512 mono_mb_emit_byte (mb
, CEE_STIND_I
);
5515 for (i
= 0; i
< 3; ++i
) {
5516 if (nursery_check_labels
[i
])
5517 mono_mb_patch_branch (mb
, nursery_check_labels
[i
]);
5519 mono_mb_patch_branch (mb
, label_no_wb_3
);
5520 mono_mb_patch_branch (mb
, label_no_wb_4
);
5521 mono_mb_emit_byte (mb
, CEE_RET
);
5524 mono_mb_patch_branch (mb
, label_slow_path
);
5526 mono_mb_emit_ldarg (mb
, 0);
5527 mono_mb_emit_icall (mb
, mono_gc_wbarrier_generic_nostore
);
5528 mono_mb_emit_byte (mb
, CEE_RET
);
5532 mono_mb_emit_ldarg (mb
, 0);
5533 mono_mb_emit_icall (mb
, mono_gc_wbarrier_generic_nostore
);
5534 mono_mb_emit_byte (mb
, CEE_RET
);
5537 res
= mono_mb_create_method (mb
, sig
, 16);
5540 mono_loader_lock ();
5541 if (write_barrier_method
) {
5542 /* Already created */
5543 mono_free_method (res
);
5545 /* double-checked locking */
5546 mono_memory_barrier ();
5547 write_barrier_method
= res
;
5549 mono_loader_unlock ();
5551 return write_barrier_method
;
5555 mono_gc_get_description (void)
5557 return g_strdup ("sgen");
5561 mono_gc_set_desktop_mode (void)
5566 mono_gc_is_moving (void)
5572 mono_gc_is_disabled (void)
5578 mono_sgen_debug_printf (int level
, const char *format
, ...)
5582 if (level
> gc_debug_level
)
5585 va_start (ap
, format
);
5586 vfprintf (gc_debug_file
, format
, ap
);
5591 mono_sgen_get_logfile (void)
5593 return gc_debug_file
;
5597 BOOL APIENTRY
mono_gc_dllmain (HMODULE module_handle
, DWORD reason
, LPVOID reserved
)
5604 mono_sgen_get_nursery_clear_policy (void)
5606 return nursery_clear_policy
;
5610 mono_sgen_get_array_fill_vtable (void)
5612 if (!array_fill_vtable
) {
5613 static MonoClass klass
;
5614 static MonoVTable vtable
;
5617 MonoDomain
*domain
= mono_get_root_domain ();
5620 klass
.element_class
= mono_defaults
.byte_class
;
5622 klass
.instance_size
= sizeof (MonoArray
);
5623 klass
.sizes
.element_size
= 1;
5624 klass
.name
= "array_filler_type";
5626 vtable
.klass
= &klass
;
5628 vtable
.gc_descr
= mono_gc_make_descr_for_array (TRUE
, &bmap
, 0, 1);
5631 array_fill_vtable
= &vtable
;
5633 return array_fill_vtable
;
5637 mono_sgen_gc_lock (void)
5643 mono_sgen_gc_unlock (void)
5649 sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback
)
5651 major_collector
.iterate_live_block_ranges (callback
);
5655 sgen_major_collector_scan_card_table (SgenGrayQueue
*queue
)
5657 major_collector
.scan_card_table (queue
);
5661 mono_sgen_ptr_in_nursery (void *p
)
5663 return SGEN_PTR_IN_NURSERY ((p
), DEFAULT_NURSERY_BITS
, nursery_start
, nursery_end
);
5667 mono_sgen_get_major_collector (void)
5669 return &major_collector
;
5672 void mono_gc_set_skip_thread (gboolean skip
)
5674 SgenThreadInfo
*info
= mono_thread_info_current ();
5677 info
->gc_disabled
= skip
;
5681 #endif /* HAVE_SGEN_GC */