2 * sgen-marksweep.c: The Mark & Sweep major collector.
5 * Mark Probst <mark.probst@gmail.com>
7 * Copyright 2009-2010 Novell, Inc.
8 * Copyright (C) 2012 Xamarin Inc
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License 2.0 as published by the Free Software Foundation;
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License 2.0 along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "utils/mono-counters.h"
32 #include "utils/mono-semaphore.h"
33 #include "utils/mono-time.h"
34 #include "metadata/object-internals.h"
35 #include "metadata/profiler-private.h"
37 #include "metadata/sgen-gc.h"
38 #include "metadata/sgen-protocol.h"
39 #include "metadata/sgen-cardtable.h"
40 #include "metadata/sgen-memory-governor.h"
41 #include "metadata/sgen-layout-stats.h"
42 #include "metadata/gc-internal.h"
43 #include "metadata/sgen-pointer-queue.h"
44 #include "metadata/sgen-pinning.h"
45 #include "metadata/sgen-workers.h"
47 #if defined(ARCH_MIN_MS_BLOCK_SIZE) && defined(ARCH_MIN_MS_BLOCK_SIZE_SHIFT)
48 #define MS_BLOCK_SIZE ARCH_MIN_MS_BLOCK_SIZE
49 #define MS_BLOCK_SIZE_SHIFT ARCH_MIN_MS_BLOCK_SIZE_SHIFT
51 #define MS_BLOCK_SIZE_SHIFT 14 /* INT FASTENABLE */
52 #define MS_BLOCK_SIZE (1 << MS_BLOCK_SIZE_SHIFT)
54 #define MAJOR_SECTION_SIZE MS_BLOCK_SIZE
55 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
58 * Don't allocate single blocks, but alloc a contingent of this many
59 * blocks in one swoop. This must be a power of two.
61 #define MS_BLOCK_ALLOC_NUM 32
64 * Number of bytes before the first object in a block. At the start
65 * of a block is the MSBlockHeader, then opional padding, then come
66 * the objects, so this must be >= sizeof (MSBlockHeader).
68 #define MS_BLOCK_SKIP ((sizeof (MSBlockHeader) + 15) & ~15)
70 #define MS_BLOCK_FREE (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
72 #define MS_NUM_MARK_WORDS ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
75 * Blocks progress from one state to the next:
77 * SWEPT The block is fully swept. It might or might not be in
80 * NOT_SWEPT The block might or might not contain live objects. If
81 * we're in between an initial collection pause and the
82 * finishing pause, the block might or might not be in a
85 * CHECKING The sweep thread is investigating the block to determine
86 * whether or not it contains live objects. The block is
89 * NEED_SWEEPING The block contains live objects but has not yet been
90 * swept. It also contains free slots. It is in a block
93 * SWEEPING The block is being swept. It might be in a free list.
100 BLOCK_STATE_NEED_SWEEPING,
104 typedef struct _MSBlockInfo MSBlockInfo;
105 struct _MSBlockInfo {
108 * FIXME: Do we even need this? It's only used during sweep and might be worth
109 * recalculating to save the space.
111 guint16 obj_size_index;
112 /* FIXME: reduce this */
113 volatile gint32 state;
114 unsigned int pinned : 1;
115 unsigned int has_references : 1;
116 unsigned int has_pinned : 1; /* means cannot evacuate */
117 unsigned int is_to_space : 1;
118 void ** volatile free_list;
119 MSBlockInfo * volatile next_free;
120 guint8 *cardtable_mod_union;
121 mword mark_words [MS_NUM_MARK_WORDS];
124 #define MS_BLOCK_FOR_BLOCK_INFO(b) ((char*)(b))
126 #define MS_BLOCK_OBJ(b,i) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (b)->obj_size * (i))
127 #define MS_BLOCK_OBJ_FOR_SIZE(b,i,obj_size) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (obj_size) * (i))
128 #define MS_BLOCK_DATA_FOR_OBJ(o) ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
134 #define MS_BLOCK_FOR_OBJ(o) (&((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
136 /* object index will always be small */
137 #define MS_BLOCK_OBJ_INDEX(o,b) ((int)(((char*)(o) - (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP)) / (b)->obj_size))
139 //casting to int is fine since blocks are 32k
140 #define MS_CALC_MARK_BIT(w,b,o) do { \
141 int i = ((int)((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o)))) >> SGEN_ALLOC_ALIGN_BITS; \
142 if (sizeof (mword) == 4) { \
151 #define MS_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] & (ONE_P << (b)))
152 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (ONE_P << (b)))
154 #define MS_OBJ_ALLOCED(o,b) (*(void**)(o) && (*(char**)(o) < MS_BLOCK_FOR_BLOCK_INFO (b) || *(char**)(o) >= MS_BLOCK_FOR_BLOCK_INFO (b) + MS_BLOCK_SIZE))
156 #define MS_BLOCK_OBJ_SIZE_FACTOR (pow (2.0, 1.0 / 3))
159 * This way we can lookup block object size indexes for sizes up to
160 * 256 bytes with a single load.
162 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES 32
164 static int *block_obj_sizes;
165 static int num_block_obj_sizes;
166 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
168 #define MS_BLOCK_FLAG_PINNED 1
169 #define MS_BLOCK_FLAG_REFS 2
171 #define MS_BLOCK_TYPE_MAX 4
173 static gboolean *evacuate_block_obj_sizes;
174 static float evacuation_threshold = 0.666f;
175 static float concurrent_evacuation_threshold = 0.666f;
176 static gboolean want_evacuation = FALSE;
178 static gboolean lazy_sweep = FALSE;
182 SWEEP_STATE_NEED_SWEEPING,
183 SWEEP_STATE_SWEEPING,
184 SWEEP_STATE_SWEEPING_AND_ITERATING,
185 SWEEP_STATE_COMPACTING
188 static volatile int sweep_state = SWEEP_STATE_SWEPT;
190 static gboolean concurrent_mark;
192 #define BLOCK_IS_TAGGED_HAS_REFERENCES(bl) SGEN_POINTER_IS_TAGGED_1 ((bl))
193 #define BLOCK_TAG_HAS_REFERENCES(bl) SGEN_POINTER_TAG_1 ((bl))
194 #define BLOCK_UNTAG_HAS_REFERENCES(bl) SGEN_POINTER_UNTAG_1 ((bl))
196 #define BLOCK_TAG(bl) ((bl)->has_references ? BLOCK_TAG_HAS_REFERENCES ((bl)) : (bl))
198 /* all allocated blocks in the system */
199 static SgenPointerQueue allocated_blocks;
200 static mono_mutex_t allocated_blocks_lock;
202 #define LOCK_ALLOCATED_BLOCKS mono_mutex_lock (&allocated_blocks_lock)
203 #define UNLOCK_ALLOCATED_BLOCKS mono_mutex_unlock (&allocated_blocks_lock)
205 /* non-allocated block free-list */
206 static void *empty_blocks = NULL;
207 static size_t num_empty_blocks = 0;
209 #define FOREACH_BLOCK(bl) { size_t __index; LOCK_ALLOCATED_BLOCKS; for (__index = 0; __index < allocated_blocks.next_slot; ++__index) { (bl) = BLOCK_UNTAG_HAS_REFERENCES (allocated_blocks.data [__index]);
210 #define FOREACH_BLOCK_HAS_REFERENCES(bl,hr) { size_t __index; LOCK_ALLOCATED_BLOCKS; for (__index = 0; __index < allocated_blocks.next_slot; ++__index) { (bl) = allocated_blocks.data [__index]; (hr) = BLOCK_IS_TAGGED_HAS_REFERENCES ((bl)); (bl) = BLOCK_UNTAG_HAS_REFERENCES ((bl));
211 #define END_FOREACH_BLOCK } UNLOCK_ALLOCATED_BLOCKS; }
213 #define FOREACH_BLOCK_NO_LOCK(bl) { size_t __index; for (__index = 0; __index < allocated_blocks.next_slot; ++__index) { (bl) = BLOCK_UNTAG_HAS_REFERENCES (allocated_blocks.data [__index]);
214 #define FOREACH_BLOCK_HAS_REFERENCES_NO_LOCK(bl,hr) { size_t __index; SGEN_ASSERT (0, sgen_is_world_stopped (), "Can't iterate blocks without lock when world is running."); for (__index = 0; __index < allocated_blocks.next_slot; ++__index) { (bl) = allocated_blocks.data [__index]; (hr) = BLOCK_IS_TAGGED_HAS_REFERENCES ((bl)); (bl) = BLOCK_UNTAG_HAS_REFERENCES ((bl));
215 #define END_FOREACH_BLOCK_NO_LOCK } }
217 static volatile size_t num_major_sections = 0;
219 * One free block list for each block object size. We add and remove blocks from these
220 * lists lock-free via CAS.
222 * Blocks accessed/removed from `free_block_lists`:
223 * from the mutator (with GC lock held)
224 * in nursery collections
225 * in non-concurrent major collections
226 * in the finishing pause of concurrent major collections (whole list is cleared)
228 * Blocks added to `free_block_lists`:
229 * in the sweeping thread
230 * during nursery collections
231 * from domain clearing (with the world stopped and no sweeping happening)
233 * The only item of those that doesn't require the GC lock is the sweep thread. The sweep
234 * thread only ever adds blocks to the free list, so the ABA problem can't occur.
236 static MSBlockInfo * volatile *free_block_lists [MS_BLOCK_TYPE_MAX];
238 static guint64 stat_major_blocks_alloced = 0;
239 static guint64 stat_major_blocks_freed = 0;
240 static guint64 stat_major_blocks_lazy_swept = 0;
241 static guint64 stat_major_objects_evacuated = 0;
243 #if SIZEOF_VOID_P != 8
244 static guint64 stat_major_blocks_freed_ideal = 0;
245 static guint64 stat_major_blocks_freed_less_ideal = 0;
246 static guint64 stat_major_blocks_freed_individual = 0;
247 static guint64 stat_major_blocks_alloced_less_ideal = 0;
250 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
251 static guint64 num_major_objects_marked = 0;
252 #define INC_NUM_MAJOR_OBJECTS_MARKED() (++num_major_objects_marked)
254 #define INC_NUM_MAJOR_OBJECTS_MARKED()
257 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
258 static mono_mutex_t scanned_objects_list_lock;
259 static SgenPointerQueue scanned_objects_list;
262 add_scanned_object (void *ptr)
264 if (!binary_protocol_is_enabled ())
267 mono_mutex_lock (&scanned_objects_list_lock);
268 sgen_pointer_queue_add (&scanned_objects_list, ptr);
269 mono_mutex_unlock (&scanned_objects_list_lock);
274 sweep_block (MSBlockInfo *block);
277 ms_find_block_obj_size_index (size_t size)
280 SGEN_ASSERT (9, size <= SGEN_MAX_SMALL_OBJ_SIZE, "size %d is bigger than max small object size %d", size, SGEN_MAX_SMALL_OBJ_SIZE);
281 for (i = 0; i < num_block_obj_sizes; ++i)
282 if (block_obj_sizes [i] >= size)
284 g_error ("no object of size %d\n", size);
287 #define FREE_BLOCKS_FROM(lists,p,r) (lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
288 #define FREE_BLOCKS(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
290 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
291 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
292 fast_block_obj_size_indexes [((s)+7)>>3] : \
293 ms_find_block_obj_size_index ((s)))
296 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
300 start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
302 start = sgen_alloc_os_memory (nursery_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
308 update_heap_boundaries_for_block (MSBlockInfo *block)
310 sgen_update_heap_boundaries ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), (mword)MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE);
317 ms_get_empty_block (void)
321 void *block, *empty, *next;
326 * We try allocating MS_BLOCK_ALLOC_NUM blocks first. If that's
327 * unsuccessful, we halve the number of blocks and try again, until we're at
328 * 1. If that doesn't work, either, we assert.
330 int alloc_num = MS_BLOCK_ALLOC_NUM;
332 p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * alloc_num, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE,
333 alloc_num == 1 ? "major heap section" : NULL);
339 for (i = 0; i < alloc_num; ++i) {
342 * We do the free list update one after the
343 * other so that other threads can use the new
344 * blocks as quickly as possible.
347 empty = empty_blocks;
348 *(void**)block = empty;
349 } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block, empty) != empty);
353 SGEN_ATOMIC_ADD_P (num_empty_blocks, alloc_num);
355 stat_major_blocks_alloced += alloc_num;
356 #if SIZEOF_VOID_P != 8
357 if (alloc_num != MS_BLOCK_ALLOC_NUM)
358 stat_major_blocks_alloced_less_ideal += alloc_num;
363 empty = empty_blocks;
367 next = *(void**)block;
368 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
370 SGEN_ATOMIC_ADD_P (num_empty_blocks, -1);
372 *(void**)block = NULL;
374 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
380 * This doesn't actually free a block immediately, but enqueues it into the `empty_blocks`
381 * list, where it will either be freed later on, or reused in nursery collections.
384 ms_free_block (void *block)
388 sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
389 memset (block, 0, MS_BLOCK_SIZE);
392 empty = empty_blocks;
393 *(void**)block = empty;
394 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
396 SGEN_ATOMIC_ADD_P (num_empty_blocks, 1);
398 binary_protocol_block_free (block, MS_BLOCK_SIZE);
402 sweep_in_progress (void)
404 int state = sweep_state;
405 return state == SWEEP_STATE_SWEEPING ||
406 state == SWEEP_STATE_SWEEPING_AND_ITERATING ||
407 state == SWEEP_STATE_COMPACTING;
410 //#define MARKSWEEP_CONSISTENCY_CHECK
412 #ifdef MARKSWEEP_CONSISTENCY_CHECK
414 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
416 SGEN_ASSERT (0, !sweep_in_progress (), "Can't examine allocated blocks during sweep");
417 for (; block; block = block->next_free) {
418 SGEN_ASSERT (0, block->state != BLOCK_STATE_CHECKING, "Can't have a block we're checking in a free list.");
419 g_assert (block->obj_size == size);
420 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
422 /* blocks in the free lists must have at least
424 g_assert (block->free_list);
426 /* the block must be in the allocated_blocks array */
427 g_assert (sgen_pointer_queue_find (&allocated_blocks, BLOCK_TAG (block)) != (size_t)-1);
432 check_empty_blocks (void)
436 for (p = empty_blocks; p; p = *(void**)p)
438 g_assert (i == num_empty_blocks);
442 consistency_check (void)
447 /* check all blocks */
448 SGEN_ASSERT (0, !sweep_in_progress (), "Can't examine allocated blocks during sweep");
449 FOREACH_BLOCK_NO_LOCK (block) {
450 int count = MS_BLOCK_FREE / block->obj_size;
454 /* count number of free slots */
455 for (i = 0; i < count; ++i) {
456 void **obj = (void**) MS_BLOCK_OBJ (block, i);
457 if (!MS_OBJ_ALLOCED (obj, block))
461 /* check free list */
462 for (free = block->free_list; free; free = (void**)*free) {
463 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
466 g_assert (num_free == 0);
468 /* check all mark words are zero */
469 if (!sgen_concurrent_collection_in_progress () && (block->state == BLOCK_STATE_SWEPT || block->state == BLOCK_STATE_MARKING)) {
470 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
471 g_assert (block->mark_words [i] == 0);
473 } END_FOREACH_BLOCK_NO_LOCK;
475 /* check free blocks */
476 for (i = 0; i < num_block_obj_sizes; ++i) {
478 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
479 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
482 check_empty_blocks ();
487 add_free_block (MSBlockInfo * volatile *free_blocks, int size_index, MSBlockInfo *block)
491 block->next_free = old = free_blocks [size_index];
492 } while (SGEN_CAS_PTR ((gpointer)&free_blocks [size_index], block, old) != old);
496 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
498 int size = block_obj_sizes [size_index];
499 int count = MS_BLOCK_FREE / size;
501 MSBlockInfo * volatile * free_blocks = FREE_BLOCKS (pinned, has_references);
505 if (!sgen_memgov_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
508 info = (MSBlockInfo*)ms_get_empty_block ();
510 SGEN_ASSERT (9, count >= 2, "block with %d objects, it must hold at least 2", count);
512 info->obj_size = size;
513 info->obj_size_index = size_index;
514 info->pinned = pinned;
515 info->has_references = has_references;
516 info->has_pinned = pinned;
518 * Blocks that are to-space are not evacuated from. During an major collection
519 * blocks are allocated for two reasons: evacuating objects from the nursery and
520 * evacuating them from major blocks marked for evacuation. In both cases we don't
521 * want further evacuation.
523 info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD);
524 info->state = (info->is_to_space || sgen_concurrent_collection_in_progress ()) ? BLOCK_STATE_MARKING : BLOCK_STATE_SWEPT;
525 if (sweep_in_progress ())
526 SGEN_ASSERT (0, info->state == BLOCK_STATE_SWEPT, "How do we add a new block to be swept while sweeping?");
527 info->cardtable_mod_union = NULL;
529 update_heap_boundaries_for_block (info);
531 binary_protocol_block_alloc (info, MS_BLOCK_SIZE);
533 /* build free list */
534 obj_start = MS_BLOCK_FOR_BLOCK_INFO (info) + MS_BLOCK_SKIP;
535 info->free_list = (void**)obj_start;
536 /* we're skipping the last one - it must be nulled */
537 for (i = 0; i < count - 1; ++i) {
538 char *next_obj_start = obj_start + size;
539 *(void**)obj_start = next_obj_start;
540 obj_start = next_obj_start;
543 *(void**)obj_start = NULL;
545 add_free_block (free_blocks, size_index, info);
547 LOCK_ALLOCATED_BLOCKS;
548 sgen_pointer_queue_add (&allocated_blocks, BLOCK_TAG (info));
549 UNLOCK_ALLOCATED_BLOCKS;
551 SGEN_ATOMIC_ADD_P (num_major_sections, 1);
556 obj_is_from_pinned_alloc (char *ptr)
560 SGEN_ASSERT (0, !sweep_in_progress (), "Can't examine allocated blocks during sweep");
561 FOREACH_BLOCK_NO_LOCK (block) {
562 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE)
563 return block->pinned;
564 } END_FOREACH_BLOCK_NO_LOCK;
569 ensure_can_access_block_free_list (MSBlockInfo *block)
573 switch (block->state) {
574 case BLOCK_STATE_SWEPT:
575 case BLOCK_STATE_MARKING:
577 case BLOCK_STATE_CHECKING:
578 SGEN_ASSERT (0, FALSE, "How did we get a block that's being checked from a free list?");
580 case BLOCK_STATE_NEED_SWEEPING:
581 stat_major_blocks_lazy_swept ++;
584 case BLOCK_STATE_SWEEPING:
585 /* FIXME: do this more elegantly */
589 SGEN_ASSERT (0, FALSE, "Illegal block state");
596 unlink_slot_from_free_list_uncontested (MSBlockInfo * volatile *free_blocks, int size_index)
598 MSBlockInfo *block, *next_free_block;
599 void *obj, *next_free_slot;
602 block = free_blocks [size_index];
603 SGEN_ASSERT (9, block, "no free block to unlink from free_blocks %p size_index %d", free_blocks, size_index);
605 ensure_can_access_block_free_list (block);
607 obj = block->free_list;
608 SGEN_ASSERT (0, obj, "block %p in free list had no available object to alloc from", block);
610 next_free_slot = *(void**)obj;
611 if (next_free_slot) {
612 block->free_list = next_free_slot;
616 next_free_block = block->next_free;
617 if (SGEN_CAS_PTR ((gpointer)&free_blocks [size_index], next_free_block, block) != block)
620 block->free_list = NULL;
621 block->next_free = NULL;
627 alloc_obj (MonoVTable *vtable, size_t size, gboolean pinned, gboolean has_references)
629 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
630 MSBlockInfo * volatile * free_blocks = FREE_BLOCKS (pinned, has_references);
633 if (!free_blocks [size_index]) {
634 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
638 obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
640 *(MonoVTable**)obj = vtable;
646 major_alloc_object (MonoVTable *vtable, size_t size, gboolean has_references)
648 return alloc_obj (vtable, size, FALSE, has_references);
652 * We're not freeing the block if it's empty. We leave that work for
653 * the next major collection.
655 * This is just called from the domain clearing code, which runs in a
656 * single thread and has the GC lock, so we don't need an extra lock.
659 free_object (char *obj, size_t size, gboolean pinned)
661 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
663 gboolean in_free_list;
665 SGEN_ASSERT (0, sweep_state == SWEEP_STATE_SWEPT, "Should have waited for sweep to free objects.");
667 ensure_can_access_block_free_list (block);
668 SGEN_ASSERT (9, (pinned && block->pinned) || (!pinned && !block->pinned), "free-object pinning mixup object %p pinned %d block %p pinned %d", obj, pinned, block, block->pinned);
669 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p is already free", obj);
670 MS_CALC_MARK_BIT (word, bit, obj);
671 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p has mark bit set");
673 memset (obj, 0, size);
675 in_free_list = !!block->free_list;
676 *(void**)obj = block->free_list;
677 block->free_list = (void**)obj;
680 MSBlockInfo * volatile *free_blocks = FREE_BLOCKS (pinned, block->has_references);
681 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
682 SGEN_ASSERT (9, !block->next_free, "block %p doesn't have a free-list of object but belongs to a free-list of blocks");
683 add_free_block (free_blocks, size_index, block);
688 major_free_non_pinned_object (char *obj, size_t size)
690 free_object (obj, size, FALSE);
693 /* size is a multiple of SGEN_ALLOC_ALIGN */
695 major_alloc_small_pinned_obj (MonoVTable *vtable, size_t size, gboolean has_references)
699 res = alloc_obj (vtable, size, TRUE, has_references);
700 /*If we failed to alloc memory, we better try releasing memory
701 *as pinned alloc is requested by the runtime.
704 sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure", TRUE);
705 res = alloc_obj (vtable, size, TRUE, has_references);
711 free_pinned_object (char *obj, size_t size)
713 free_object (obj, size, TRUE);
717 * size is already rounded up and we hold the GC lock.
720 major_alloc_degraded (MonoVTable *vtable, size_t size)
722 void *obj = alloc_obj (vtable, size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
723 if (G_LIKELY (obj)) {
724 HEAVY_STAT (++stat_objects_alloced_degraded);
725 HEAVY_STAT (stat_bytes_alloced_degraded += size);
731 * obj is some object. If it's not in the major heap (i.e. if it's in
732 * the nursery or LOS), return FALSE. Otherwise return whether it's
733 * been marked or copied.
736 major_is_object_live (char *obj)
742 if (sgen_ptr_in_nursery (obj))
745 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
748 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
751 /* now we know it's in a major block */
752 block = MS_BLOCK_FOR_OBJ (obj);
753 SGEN_ASSERT (9, !block->pinned, "block %p is pinned, BTW why is this bad?");
754 MS_CALC_MARK_BIT (word, bit, obj);
755 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
759 major_ptr_is_in_non_pinned_space (char *ptr, char **start)
763 SGEN_ASSERT (0, !sweep_in_progress (), "Can't examine allocated blocks during sweep");
764 FOREACH_BLOCK_NO_LOCK (block) {
765 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) {
766 int count = MS_BLOCK_FREE / block->obj_size;
770 for (i = 0; i <= count; ++i) {
771 if (ptr >= MS_BLOCK_OBJ (block, i) && ptr < MS_BLOCK_OBJ (block, i + 1)) {
772 *start = MS_BLOCK_OBJ (block, i);
776 return !block->pinned;
778 } END_FOREACH_BLOCK_NO_LOCK;
783 major_finish_sweeping (void)
785 if (!sweep_in_progress ())
788 while (sweep_in_progress ())
793 major_iterate_objects (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data)
795 gboolean sweep = flags & ITERATE_OBJECTS_SWEEP;
796 gboolean non_pinned = flags & ITERATE_OBJECTS_NON_PINNED;
797 gboolean pinned = flags & ITERATE_OBJECTS_PINNED;
800 major_finish_sweeping ();
801 FOREACH_BLOCK (block) {
802 int count = MS_BLOCK_FREE / block->obj_size;
805 if (block->pinned && !pinned)
807 if (!block->pinned && !non_pinned)
809 if (sweep && lazy_sweep) {
810 /* FIXME: We can't just call `sweep_block` willy-nilly. */
812 SGEN_ASSERT (0, block->state == BLOCK_STATE_SWEPT, "Block must be swept after sweeping");
815 for (i = 0; i < count; ++i) {
816 void **obj = (void**) MS_BLOCK_OBJ (block, i);
817 /* FIXME: This condition is probably incorrect. */
818 if (block->state != BLOCK_STATE_SWEPT && block->state != BLOCK_STATE_MARKING) {
820 MS_CALC_MARK_BIT (word, bit, obj);
821 if (!MS_MARK_BIT (block, word, bit))
824 if (MS_OBJ_ALLOCED (obj, block))
825 callback ((char*)obj, block->obj_size, data);
831 major_is_valid_object (char *object)
835 SGEN_ASSERT (0, !sweep_in_progress (), "Can't iterate blocks during sweep");
836 FOREACH_BLOCK_NO_LOCK (block) {
840 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > object) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= object))
843 idx = MS_BLOCK_OBJ_INDEX (object, block);
844 obj = (char*)MS_BLOCK_OBJ (block, idx);
847 return MS_OBJ_ALLOCED (obj, block);
848 } END_FOREACH_BLOCK_NO_LOCK;
855 major_describe_pointer (char *ptr)
859 FOREACH_BLOCK_NO_LOCK (block) {
867 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > ptr) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= ptr))
870 SGEN_LOG (0, "major-ptr (block %p sz %d pin %d ref %d)\n",
871 MS_BLOCK_FOR_BLOCK_INFO (block), block->obj_size, block->pinned, block->has_references);
873 idx = MS_BLOCK_OBJ_INDEX (ptr, block);
874 obj = (char*)MS_BLOCK_OBJ (block, idx);
875 live = MS_OBJ_ALLOCED (obj, block);
876 vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
878 MS_CALC_MARK_BIT (w, b, obj);
879 marked = MS_MARK_BIT (block, w, b);
884 SGEN_LOG (0, "object");
886 SGEN_LOG (0, "dead-object");
889 SGEN_LOG (0, "interior-ptr offset %td", ptr - obj);
891 SGEN_LOG (0, "dead-interior-ptr offset %td", ptr - obj);
894 SGEN_LOG (0, " marked %d)\n", marked ? 1 : 0);
897 } END_FOREACH_BLOCK_NO_LOCK;
903 major_check_scan_starts (void)
908 major_dump_heap (FILE *heap_dump_file)
911 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
912 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
915 for (i = 0; i < num_block_obj_sizes; ++i)
916 slots_available [i] = slots_used [i] = 0;
918 SGEN_ASSERT (0, !sweep_in_progress (), "Can't iterate blocks during sweep");
919 FOREACH_BLOCK (block) {
920 int index = ms_find_block_obj_size_index (block->obj_size);
921 int count = MS_BLOCK_FREE / block->obj_size;
923 slots_available [index] += count;
924 for (i = 0; i < count; ++i) {
925 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
926 ++slots_used [index];
930 fprintf (heap_dump_file, "<occupancies>\n");
931 for (i = 0; i < num_block_obj_sizes; ++i) {
932 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
933 block_obj_sizes [i], slots_available [i], slots_used [i]);
935 fprintf (heap_dump_file, "</occupancies>\n");
937 FOREACH_BLOCK (block) {
938 int count = MS_BLOCK_FREE / block->obj_size;
942 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
944 for (i = 0; i <= count; ++i) {
945 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
950 sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), MS_BLOCK_FOR_BLOCK_INFO (block));
956 fprintf (heap_dump_file, "</section>\n");
960 #define LOAD_VTABLE SGEN_LOAD_VTABLE
962 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,desc,block,queue) do { \
964 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
965 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
966 MS_SET_MARK_BIT ((block), __word, __bit); \
967 if (sgen_gc_descr_has_references (desc)) \
968 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
969 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
970 INC_NUM_MAJOR_OBJECTS_MARKED (); \
973 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,desc,block,queue) do { \
975 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
976 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
977 if (!MS_MARK_BIT ((block), __word, __bit)) { \
978 MS_SET_MARK_BIT ((block), __word, __bit); \
979 if (sgen_gc_descr_has_references (desc)) \
980 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
981 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
982 INC_NUM_MAJOR_OBJECTS_MARKED (); \
987 pin_major_object (char *obj, SgenGrayQueue *queue)
992 g_assert_not_reached ();
994 block = MS_BLOCK_FOR_OBJ (obj);
995 block->has_pinned = TRUE;
996 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
999 #include "sgen-major-copy-object.h"
1002 major_copy_or_mark_object_with_evacuation_concurrent (void **ptr, void *obj, SgenGrayQueue *queue)
1004 SGEN_ASSERT (9, sgen_concurrent_collection_in_progress (), "Why are we scanning concurrently when there's no concurrent collection on?");
1005 SGEN_ASSERT (9, !sgen_workers_are_working () || sgen_is_worker_thread (mono_native_thread_id_get ()), "We must not scan from two threads at the same time!");
1007 g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
1009 if (!sgen_ptr_in_nursery (obj)) {
1012 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1014 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE) {
1015 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1016 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
1018 if (sgen_los_object_is_pinned (obj))
1021 #ifdef ENABLE_DTRACE
1022 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1023 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1024 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1028 sgen_los_pin_object (obj);
1029 if (SGEN_OBJECT_HAS_REFERENCES (obj))
1030 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_obj_get_descriptor (obj));
1031 INC_NUM_MAJOR_OBJECTS_MARKED ();
1037 major_get_and_reset_num_major_objects_marked (void)
1039 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
1040 long long num = num_major_objects_marked;
1041 num_major_objects_marked = 0;
1048 #define PREFETCH_CARDS 1 /* BOOL FASTENABLE */
1050 #undef PREFETCH_CARDS
1053 /* gcc 4.2.1 from xcode4 crashes on sgen_card_table_get_card_address () when this is enabled */
1054 #if defined(PLATFORM_MACOSX)
1055 #define GCC_VERSION (__GNUC__ * 10000 \
1056 + __GNUC_MINOR__ * 100 \
1057 + __GNUC_PATCHLEVEL__)
1058 #if GCC_VERSION <= 40300
1059 #undef PREFETCH_CARDS
1063 #ifdef HEAVY_STATISTICS
1064 static guint64 stat_optimized_copy;
1065 static guint64 stat_optimized_copy_nursery;
1066 static guint64 stat_optimized_copy_nursery_forwarded;
1067 static guint64 stat_optimized_copy_nursery_pinned;
1068 static guint64 stat_optimized_copy_major;
1069 static guint64 stat_optimized_copy_major_small_fast;
1070 static guint64 stat_optimized_copy_major_small_slow;
1071 static guint64 stat_optimized_copy_major_large;
1072 static guint64 stat_optimized_copy_major_forwarded;
1073 static guint64 stat_optimized_copy_major_small_evacuate;
1074 static guint64 stat_optimized_major_scan;
1075 static guint64 stat_optimized_major_scan_no_refs;
1077 static guint64 stat_drain_prefetch_fills;
1078 static guint64 stat_drain_prefetch_fill_failures;
1079 static guint64 stat_drain_loops;
1082 static void major_scan_object_with_evacuation (char *start, mword desc, SgenGrayQueue *queue);
1084 #define COPY_OR_MARK_FUNCTION_NAME major_copy_or_mark_object_no_evacuation
1085 #define SCAN_OBJECT_FUNCTION_NAME major_scan_object_no_evacuation
1086 #define DRAIN_GRAY_STACK_FUNCTION_NAME drain_gray_stack_no_evacuation
1087 #include "sgen-marksweep-drain-gray-stack.h"
1089 #define COPY_OR_MARK_WITH_EVACUATION
1090 #define COPY_OR_MARK_FUNCTION_NAME major_copy_or_mark_object_with_evacuation
1091 #define SCAN_OBJECT_FUNCTION_NAME major_scan_object_with_evacuation
1092 #define DRAIN_GRAY_STACK_FUNCTION_NAME drain_gray_stack_with_evacuation
1093 #include "sgen-marksweep-drain-gray-stack.h"
1096 drain_gray_stack (ScanCopyContext ctx)
1098 gboolean evacuation = FALSE;
1100 for (i = 0; i < num_block_obj_sizes; ++i) {
1101 if (evacuate_block_obj_sizes [i]) {
1108 return drain_gray_stack_with_evacuation (ctx);
1110 return drain_gray_stack_no_evacuation (ctx);
1113 #include "sgen-marksweep-scan-object-concurrent.h"
1116 major_copy_or_mark_object_canonical (void **ptr, SgenGrayQueue *queue)
1118 major_copy_or_mark_object_with_evacuation (ptr, *ptr, queue);
1122 major_copy_or_mark_object_concurrent_canonical (void **ptr, SgenGrayQueue *queue)
1124 major_copy_or_mark_object_with_evacuation_concurrent (ptr, *ptr, queue);
1128 mark_pinned_objects_in_block (MSBlockInfo *block, size_t first_entry, size_t last_entry, SgenGrayQueue *queue)
1130 void **entry, **end;
1131 int last_index = -1;
1133 if (first_entry == last_entry)
1136 block->has_pinned = TRUE;
1138 entry = sgen_pinning_get_entry (first_entry);
1139 end = sgen_pinning_get_entry (last_entry);
1141 for (; entry < end; ++entry) {
1142 int index = MS_BLOCK_OBJ_INDEX (*entry, block);
1144 SGEN_ASSERT (9, index >= 0 && index < MS_BLOCK_FREE / block->obj_size, "invalid object %p index %d max-index %d", *entry, index, MS_BLOCK_FREE / block->obj_size);
1145 if (index == last_index)
1147 obj = MS_BLOCK_OBJ (block, index);
1148 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (obj, sgen_obj_get_descriptor (obj), block, queue);
1154 sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
1158 for (obj_index = 0; obj_index < count; ++obj_index) {
1160 void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
1162 MS_CALC_MARK_BIT (word, bit, obj);
1163 if (MS_MARK_BIT (block, word, bit)) {
1164 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
1166 /* an unmarked object */
1167 if (MS_OBJ_ALLOCED (obj, block)) {
1169 * FIXME: Merge consecutive
1170 * slots for lower reporting
1171 * overhead. Maybe memset
1172 * will also benefit?
1174 binary_protocol_empty (obj, obj_size);
1175 MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
1176 memset (obj, 0, obj_size);
1178 *(void**)obj = block->free_list;
1179 block->free_list = obj;
1185 try_set_block_state (MSBlockInfo *block, gint32 new_state, gint32 expected_state)
1187 gint32 old_state = SGEN_CAS (&block->state, new_state, expected_state);
1188 gboolean success = old_state == expected_state;
1190 binary_protocol_block_set_state (block, MS_BLOCK_SIZE, old_state, new_state);
1195 * FIXME: This only CASes to catch errors. It's not needed for correctness.
1198 set_block_state (MSBlockInfo *block, gint32 new_state, gint32 expected_state)
1200 gboolean success = try_set_block_state (block, new_state, expected_state);
1201 SGEN_ASSERT (0, success, "Couldn't set block state");
1202 SGEN_ASSERT (0, block->state == new_state, "Block state incorrect after set");
1208 * Traverse BLOCK, freeing and zeroing unused objects.
1211 sweep_block (MSBlockInfo *block)
1214 void *reversed = NULL;
1217 switch (block->state) {
1218 case BLOCK_STATE_SWEPT:
1220 case BLOCK_STATE_MARKING:
1221 case BLOCK_STATE_CHECKING:
1222 SGEN_ASSERT (0, FALSE, "How did we get to sweep a block that's being marked or being checked?");
1224 case BLOCK_STATE_SWEEPING:
1225 /* FIXME: Pick another block or whatever */
1228 case BLOCK_STATE_NEED_SWEEPING:
1229 if (!try_set_block_state (block, BLOCK_STATE_SWEEPING, BLOCK_STATE_NEED_SWEEPING))
1233 SGEN_ASSERT (0, FALSE, "Illegal block state");
1236 SGEN_ASSERT (0, block->state == BLOCK_STATE_SWEEPING, "How did we get here without setting state to sweeping?");
1238 count = MS_BLOCK_FREE / block->obj_size;
1240 block->free_list = NULL;
1242 /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
1243 // FIXME: Add more sizes
1244 switch (block->obj_size) {
1246 sweep_block_for_size (block, count, 16);
1249 sweep_block_for_size (block, count, block->obj_size);
1253 /* reset mark bits */
1254 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1256 /* Reverse free list so that it's in address order */
1258 while (block->free_list) {
1259 void *next = *(void**)block->free_list;
1260 *(void**)block->free_list = reversed;
1261 reversed = block->free_list;
1262 block->free_list = next;
1264 block->free_list = reversed;
1266 mono_memory_write_barrier ();
1268 set_block_state (block, BLOCK_STATE_SWEPT, BLOCK_STATE_SWEEPING);
1277 if (sizeof (mword) == sizeof (unsigned long))
1278 count += __builtin_popcountl (d);
1280 count += __builtin_popcount (d);
1290 /* statistics for evacuation */
1291 static size_t *sweep_slots_available;
1292 static size_t *sweep_slots_used;
1293 static size_t *sweep_num_blocks;
1295 static volatile size_t num_major_sections_before_sweep;
1296 static volatile size_t num_major_sections_freed_in_sweep;
1303 for (i = 0; i < num_block_obj_sizes; ++i)
1304 sweep_slots_available [i] = sweep_slots_used [i] = sweep_num_blocks [i] = 0;
1306 /* clear all the free lists */
1307 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1308 MSBlockInfo * volatile *free_blocks = free_block_lists [i];
1310 for (j = 0; j < num_block_obj_sizes; ++j)
1311 free_blocks [j] = NULL;
1315 static void sweep_finish (void);
1318 * LOCKING: The allocated blocks lock must be held when entering this function. `block`
1319 * must have been loaded from the array with the lock held. This function will unlock the
1322 * Returns whether the block is still there.
1325 ensure_block_is_checked_for_sweeping (MSBlockInfo *block, int block_index, gboolean *have_checked)
1328 gboolean have_live = FALSE;
1329 gboolean have_free = FALSE;
1331 int block_state = block->state;
1335 *have_checked = FALSE;
1337 if (!sweep_in_progress ()) {
1338 SGEN_ASSERT (0, block_state != BLOCK_STATE_SWEEPING && block_state != BLOCK_STATE_CHECKING, "Invalid block state.");
1340 SGEN_ASSERT (0, block_state != BLOCK_STATE_NEED_SWEEPING, "Invalid block state.");
1344 switch (block_state) {
1345 case BLOCK_STATE_SWEPT:
1346 case BLOCK_STATE_NEED_SWEEPING:
1347 case BLOCK_STATE_SWEEPING:
1348 UNLOCK_ALLOCATED_BLOCKS;
1350 case BLOCK_STATE_MARKING:
1351 if (sweep_in_progress ())
1353 UNLOCK_ALLOCATED_BLOCKS;
1355 case BLOCK_STATE_CHECKING: {
1356 MSBlockInfo *block_before = block;
1358 * FIXME: do this more elegantly.
1360 * Also, when we're called from the sweep thread, we don't actually have to
1361 * wait for it to finish, because the sweep thread doesn't use the block.
1362 * However, the sweep thread needs to know when all the blocks have been
1363 * checked (so it can set the global sweep state to SWEPT), so we'd have to
1364 * do some kind of accounting if we don't wait.
1366 UNLOCK_ALLOCATED_BLOCKS;
1368 LOCK_ALLOCATED_BLOCKS;
1369 block = BLOCK_UNTAG_HAS_REFERENCES (allocated_blocks.data [block_index]);
1371 UNLOCK_ALLOCATED_BLOCKS;
1374 SGEN_ASSERT (0, block == block_before, "How did the block get exchanged for a different one?");
1375 block_state = block->state;
1379 SGEN_ASSERT (0, FALSE, "Illegal block state");
1383 SGEN_ASSERT (0, block->state == BLOCK_STATE_MARKING, "When we sweep all blocks must start out marking.");
1384 set_block_state (block, BLOCK_STATE_CHECKING, BLOCK_STATE_MARKING);
1385 UNLOCK_ALLOCATED_BLOCKS;
1388 *have_checked = TRUE;
1390 block->has_pinned = block->pinned;
1392 block->is_to_space = FALSE;
1394 count = MS_BLOCK_FREE / block->obj_size;
1396 if (block->cardtable_mod_union) {
1397 sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
1398 block->cardtable_mod_union = NULL;
1401 /* Count marked objects in the block */
1402 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
1403 nused += bitcount (block->mark_words [i]);
1411 int obj_size_index = block->obj_size_index;
1412 gboolean has_pinned = block->has_pinned;
1414 set_block_state (block, BLOCK_STATE_NEED_SWEEPING, BLOCK_STATE_CHECKING);
1417 * FIXME: Go straight to SWEPT if there are no free slots. We need
1418 * to set the free slot list to NULL, though, and maybe update some
1422 sweep_block (block);
1425 ++sweep_num_blocks [obj_size_index];
1426 sweep_slots_used [obj_size_index] += nused;
1427 sweep_slots_available [obj_size_index] += count;
1431 * If there are free slots in the block, add
1432 * the block to the corresponding free list.
1435 MSBlockInfo * volatile *free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1436 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1439 SGEN_ASSERT (0, block->free_list, "How do we not have a free list when there are free slots?");
1441 add_free_block (free_blocks, index, block);
1444 /* FIXME: Do we need the heap boundaries while we do nursery collections? */
1445 update_heap_boundaries_for_block (block);
1450 * Blocks without live objects are removed from the
1451 * block list and freed.
1453 LOCK_ALLOCATED_BLOCKS;
1454 SGEN_ASSERT (0, block_index < allocated_blocks.next_slot, "How did the number of blocks shrink?");
1455 SGEN_ASSERT (0, BLOCK_UNTAG_HAS_REFERENCES (allocated_blocks.data [block_index]) == block, "How did the block move?");
1456 allocated_blocks.data [block_index] = NULL;
1457 UNLOCK_ALLOCATED_BLOCKS;
1459 binary_protocol_empty (MS_BLOCK_OBJ (block, 0), (char*)MS_BLOCK_OBJ (block, count) - (char*)MS_BLOCK_OBJ (block, 0));
1460 ms_free_block (block);
1462 SGEN_ATOMIC_ADD_P (num_major_sections, -1);
1469 try_set_sweep_state (int new, int expected)
1471 int old = SGEN_CAS (&sweep_state, new, expected);
1472 return old == expected;
1476 set_sweep_state (int new, int expected)
1478 gboolean success = try_set_sweep_state (new, expected);
1479 SGEN_ASSERT (0, success, "Could not set sweep state.");
1482 static mono_native_thread_return_t
1483 sweep_loop_thread_func (void *dummy)
1486 int num_blocks = num_major_sections_before_sweep;
1487 int small_id = mono_thread_info_register_small_id ();
1489 SGEN_ASSERT (0, sweep_in_progress (), "Sweep thread called with wrong state");
1490 SGEN_ASSERT (0, num_blocks <= allocated_blocks.next_slot, "How did we lose blocks?");
1493 * We traverse the block array from high to low. Nursery collections will have to
1494 * cooperate with the sweep thread to finish sweeping, and they will traverse from
1495 * low to high, to avoid constantly colliding on the same blocks.
1497 for (block_index = num_blocks - 1; block_index >= 0; --block_index) {
1499 gboolean have_checked;
1501 LOCK_ALLOCATED_BLOCKS;
1502 block = BLOCK_UNTAG_HAS_REFERENCES (allocated_blocks.data [block_index]);
1505 * The block might have been freed by another thread doing some checking
1509 UNLOCK_ALLOCATED_BLOCKS;
1510 ++num_major_sections_freed_in_sweep;
1514 if (block->state == BLOCK_STATE_SWEPT) {
1515 UNLOCK_ALLOCATED_BLOCKS;
1519 ensure_block_is_checked_for_sweeping (block, block_index, &have_checked);
1522 while (!try_set_sweep_state (SWEEP_STATE_COMPACTING, SWEEP_STATE_SWEEPING))
1525 LOCK_ALLOCATED_BLOCKS;
1526 for (block_index = num_blocks; block_index < allocated_blocks.next_slot; ++block_index) {
1527 MSBlockInfo *block = BLOCK_UNTAG_HAS_REFERENCES (allocated_blocks.data [block_index]);
1528 SGEN_ASSERT (0, block && block->state == BLOCK_STATE_SWEPT, "How did a new block to be swept get added while swept?");
1531 sgen_pointer_queue_remove_nulls (&allocated_blocks);
1532 UNLOCK_ALLOCATED_BLOCKS;
1536 mono_thread_small_id_free (small_id);
1544 mword total_evacuate_heap = 0;
1545 mword total_evacuate_saved = 0;
1548 for (i = 0; i < num_block_obj_sizes; ++i) {
1549 float usage = (float)sweep_slots_used [i] / (float)sweep_slots_available [i];
1550 if (sweep_num_blocks [i] > 5 && usage < evacuation_threshold) {
1551 evacuate_block_obj_sizes [i] = TRUE;
1553 g_print ("slot size %d - %d of %d used\n",
1554 block_obj_sizes [i], slots_used [i], slots_available [i]);
1557 evacuate_block_obj_sizes [i] = FALSE;
1560 mword total_bytes = block_obj_sizes [i] * sweep_slots_available [i];
1561 total_evacuate_heap += total_bytes;
1562 if (evacuate_block_obj_sizes [i])
1563 total_evacuate_saved += total_bytes - block_obj_sizes [i] * sweep_slots_used [i];
1567 want_evacuation = (float)total_evacuate_saved / (float)total_evacuate_heap > (1 - concurrent_evacuation_threshold);
1569 set_sweep_state (SWEEP_STATE_SWEPT, SWEEP_STATE_COMPACTING);
1572 static MonoNativeThreadId sweep_loop_thread;
1577 set_sweep_state (SWEEP_STATE_SWEEPING, SWEEP_STATE_NEED_SWEEPING);
1581 SGEN_ASSERT (0, num_major_sections == allocated_blocks.next_slot, "We don't know how many blocks we have?");
1583 num_major_sections_before_sweep = num_major_sections;
1584 num_major_sections_freed_in_sweep = 0;
1586 if (TRUE /*concurrent_mark*/) {
1588 * FIXME: We can't create a thread while the world is stopped because it
1589 * might deadlock. `finalizer-wait.exe` exposes this.
1591 mono_native_thread_create (&sweep_loop_thread, sweep_loop_thread_func, NULL);
1593 sweep_loop_thread_func (NULL);
1598 major_have_swept (void)
1600 return sweep_state == SWEEP_STATE_SWEPT;
1603 static int count_pinned_ref;
1604 static int count_pinned_nonref;
1605 static int count_nonpinned_ref;
1606 static int count_nonpinned_nonref;
1609 count_nonpinned_callback (char *obj, size_t size, void *data)
1611 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1613 if (vtable->klass->has_references)
1614 ++count_nonpinned_ref;
1616 ++count_nonpinned_nonref;
1620 count_pinned_callback (char *obj, size_t size, void *data)
1622 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1624 if (vtable->klass->has_references)
1627 ++count_pinned_nonref;
1630 static G_GNUC_UNUSED void
1631 count_ref_nonref_objs (void)
1635 count_pinned_ref = 0;
1636 count_pinned_nonref = 0;
1637 count_nonpinned_ref = 0;
1638 count_nonpinned_nonref = 0;
1640 major_iterate_objects (ITERATE_OBJECTS_SWEEP_NON_PINNED, count_nonpinned_callback, NULL);
1641 major_iterate_objects (ITERATE_OBJECTS_SWEEP_PINNED, count_pinned_callback, NULL);
1643 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1645 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1646 count_pinned_ref, count_nonpinned_ref,
1647 count_pinned_nonref, count_nonpinned_nonref,
1648 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1652 ms_calculate_block_obj_sizes (double factor, int *arr)
1659 * Have every possible slot size starting with the minimal
1660 * object size up to and including four times that size. Then
1661 * proceed by increasing geometrically with the given factor.
1664 for (int size = sizeof (MonoObject); size <= 4 * sizeof (MonoObject); size += SGEN_ALLOC_ALIGN) {
1666 arr [num_sizes] = size;
1670 target_size = (double)last_size;
1673 int target_count = (int)floor (MS_BLOCK_FREE / target_size);
1674 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1676 if (size != last_size) {
1678 arr [num_sizes] = size;
1683 target_size *= factor;
1684 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1689 /* only valid during minor collections */
1690 static mword old_num_major_sections;
1693 major_start_nursery_collection (void)
1695 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1696 consistency_check ();
1699 old_num_major_sections = num_major_sections;
1701 if (sweep_in_progress ())
1702 g_print ("sweeping during nursery collection\n");
1706 major_finish_nursery_collection (void)
1708 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1709 consistency_check ();
1714 major_start_major_collection (void)
1719 SGEN_ASSERT (0, sweep_state == SWEEP_STATE_SWEPT, "Major collection on unswept heap");
1722 * Clear the free lists for block sizes where we do evacuation. For those block
1723 * sizes we will have to allocate new blocks.
1725 for (i = 0; i < num_block_obj_sizes; ++i) {
1726 if (!evacuate_block_obj_sizes [i])
1729 free_block_lists [0][i] = NULL;
1730 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1733 // Sweep all unswept blocks
1735 MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
1737 FOREACH_BLOCK (block) {
1738 sweep_block (block);
1739 } END_FOREACH_BLOCK;
1741 MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
1744 /* FIXME: Just do one iteration over the blocks in this function. */
1745 FOREACH_BLOCK (block) {
1746 SGEN_ASSERT (0, block->state == BLOCK_STATE_SWEPT, "All blocks must be swept when we're pinning.");
1748 * FIXME: We don't need CAS here because there's still only one thread doing
1751 set_block_state (block, BLOCK_STATE_MARKING, BLOCK_STATE_SWEPT);
1752 } END_FOREACH_BLOCK;
1754 set_sweep_state (SWEEP_STATE_NEED_SWEEPING, SWEEP_STATE_SWEPT);
1758 major_finish_major_collection (ScannedObjectCounts *counts)
1760 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
1761 if (binary_protocol_is_enabled ()) {
1762 counts->num_scanned_objects = scanned_objects_list.next_slot;
1764 sgen_pointer_queue_sort_uniq (&scanned_objects_list);
1765 counts->num_unique_scanned_objects = scanned_objects_list.next_slot;
1767 sgen_pointer_queue_clear (&scanned_objects_list);
1772 #if SIZEOF_VOID_P != 8
1774 compare_pointers (const void *va, const void *vb) {
1775 char *a = *(char**)va, *b = *(char**)vb;
1785 * This is called with sweep completed and the world stopped.
1788 major_free_swept_blocks (void)
1790 size_t section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1792 SGEN_ASSERT (0, sweep_state == SWEEP_STATE_SWEPT, "Sweeping must have finished before freeing blocks");
1794 #if SIZEOF_VOID_P != 8
1796 int i, num_empty_blocks_orig, num_blocks, arr_length;
1798 void **empty_block_arr;
1799 void **rebuild_next;
1803 * sgen_free_os_memory () asserts in mono_vfree () because windows doesn't like freeing the middle of
1804 * a VirtualAlloc ()-ed block.
1809 if (num_empty_blocks <= section_reserve)
1811 SGEN_ASSERT (0, num_empty_blocks > 0, "section reserve can't be negative");
1813 num_empty_blocks_orig = num_empty_blocks;
1814 empty_block_arr = (void**)sgen_alloc_internal_dynamic (sizeof (void*) * num_empty_blocks_orig,
1815 INTERNAL_MEM_MS_BLOCK_INFO_SORT, FALSE);
1816 if (!empty_block_arr)
1820 for (block = empty_blocks; block; block = *(void**)block)
1821 empty_block_arr [i++] = block;
1822 SGEN_ASSERT (0, i == num_empty_blocks, "empty block count wrong");
1824 sgen_qsort (empty_block_arr, num_empty_blocks, sizeof (void*), compare_pointers);
1827 * We iterate over the free blocks, trying to find MS_BLOCK_ALLOC_NUM
1828 * contiguous ones. If we do, we free them. If that's not enough to get to
1829 * section_reserve, we halve the number of contiguous blocks we're looking
1830 * for and have another go, until we're done with looking for pairs of
1831 * blocks, at which point we give up and go to the fallback.
1833 arr_length = num_empty_blocks_orig;
1834 num_blocks = MS_BLOCK_ALLOC_NUM;
1835 while (num_empty_blocks > section_reserve && num_blocks > 1) {
1840 for (i = 0; i < arr_length; ++i) {
1842 void *block = empty_block_arr [i];
1843 SGEN_ASSERT (0, block, "we're not shifting correctly");
1845 empty_block_arr [dest] = block;
1847 * This is not strictly necessary, but we're
1850 empty_block_arr [i] = NULL;
1859 SGEN_ASSERT (0, first >= 0 && d > first, "algorithm is wrong");
1861 if ((char*)block != ((char*)empty_block_arr [d-1]) + MS_BLOCK_SIZE) {
1866 if (d + 1 - first == num_blocks) {
1868 * We found num_blocks contiguous blocks. Free them
1869 * and null their array entries. As an optimization
1870 * we could, instead of nulling the entries, shift
1871 * the following entries over to the left, while
1875 sgen_free_os_memory (empty_block_arr [first], MS_BLOCK_SIZE * num_blocks, SGEN_ALLOC_HEAP);
1876 for (j = first; j <= d; ++j)
1877 empty_block_arr [j] = NULL;
1881 num_empty_blocks -= num_blocks;
1883 stat_major_blocks_freed += num_blocks;
1884 if (num_blocks == MS_BLOCK_ALLOC_NUM)
1885 stat_major_blocks_freed_ideal += num_blocks;
1887 stat_major_blocks_freed_less_ideal += num_blocks;
1892 SGEN_ASSERT (0, dest <= i && dest <= arr_length, "array length is off");
1894 SGEN_ASSERT (0, arr_length == num_empty_blocks, "array length is off");
1899 /* rebuild empty_blocks free list */
1900 rebuild_next = (void**)&empty_blocks;
1901 for (i = 0; i < arr_length; ++i) {
1902 void *block = empty_block_arr [i];
1903 SGEN_ASSERT (0, block, "we're missing blocks");
1904 *rebuild_next = block;
1905 rebuild_next = (void**)block;
1907 *rebuild_next = NULL;
1910 sgen_free_internal_dynamic (empty_block_arr, sizeof (void*) * num_empty_blocks_orig, INTERNAL_MEM_MS_BLOCK_INFO_SORT);
1913 SGEN_ASSERT (0, num_empty_blocks >= 0, "we freed more blocks than we had in the first place?");
1917 * This is our threshold. If there's not more empty than used blocks, we won't
1918 * release uncontiguous blocks, in fear of fragmenting the address space.
1920 if (num_empty_blocks <= num_major_sections)
1924 while (num_empty_blocks > section_reserve) {
1925 void *next = *(void**)empty_blocks;
1926 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
1927 empty_blocks = next;
1929 * Needs not be atomic because this is running
1934 ++stat_major_blocks_freed;
1935 #if SIZEOF_VOID_P != 8
1936 ++stat_major_blocks_freed_individual;
1941 /* FIXME: Unify `major_find_pin_queue_start_ends` and `major_pin_objects`. */
1943 major_pin_objects (SgenGrayQueue *queue)
1947 SGEN_ASSERT (0, !sweep_in_progress (), "Cannot iterate blocks during sweep");
1948 FOREACH_BLOCK (block) {
1949 size_t first_entry, last_entry;
1950 SGEN_ASSERT (0, block->state == BLOCK_STATE_SWEPT || block->state == BLOCK_STATE_MARKING, "All blocks must be swept when we're pinning.");
1951 sgen_find_optimized_pin_queue_area (MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SKIP, MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE,
1952 &first_entry, &last_entry);
1953 mark_pinned_objects_in_block (block, first_entry, last_entry, queue);
1954 } END_FOREACH_BLOCK;
1958 major_init_to_space (void)
1963 major_report_pinned_memory_usage (void)
1965 g_assert_not_reached ();
1969 major_get_used_size (void)
1974 SGEN_ASSERT (0, !sweep_in_progress (), "Cannot iterate blocks during sweep");
1975 FOREACH_BLOCK (block) {
1976 int count = MS_BLOCK_FREE / block->obj_size;
1978 size += count * block->obj_size;
1979 for (iter = block->free_list; iter; iter = (void**)*iter)
1980 size -= block->obj_size;
1981 } END_FOREACH_BLOCK;
1987 get_num_major_sections (void)
1989 return num_major_sections;
1993 * Returns the number of major sections that were present when the last sweep was initiated,
1994 * and were not freed during the sweep. They are the basis for calculating the allowance.
1997 get_num_major_unswept_old_sections (void)
1999 SGEN_ASSERT (0, sweep_state == SWEEP_STATE_SWEPT, "Can only query unswept sections after sweep");
2000 return num_major_sections_before_sweep - num_major_sections_freed_in_sweep;
2004 major_handle_gc_param (const char *opt)
2006 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
2007 const char *arg = strchr (opt, '=') + 1;
2008 int percentage = atoi (arg);
2009 if (percentage < 0 || percentage > 100) {
2010 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
2013 evacuation_threshold = (float)percentage / 100.0f;
2015 } else if (!strcmp (opt, "lazy-sweep")) {
2018 } else if (!strcmp (opt, "no-lazy-sweep")) {
2027 major_print_gc_param_usage (void)
2031 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
2032 " (no-)lazy-sweep\n"
2037 * This callback is used to clear cards, move cards to the shadow table and do counting.
2040 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback, gboolean requires_sweep)
2043 gboolean has_references;
2046 major_finish_sweeping ();
2049 * FIXME: Don't take the lock for the whole allocated blocks array because we're
2050 * stopping the sweep thread.
2052 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
2054 callback ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE);
2055 } END_FOREACH_BLOCK;
2058 #ifdef HEAVY_STATISTICS
2059 extern guint64 marked_cards;
2060 extern guint64 scanned_cards;
2061 extern guint64 scanned_objects;
2062 extern guint64 remarked_cards;
2065 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
2067 * MS blocks are 16K aligned.
2068 * Cardtables are 4K aligned, at least.
2069 * This means that the cardtable of a given block is 32 bytes aligned.
2072 initial_skip_card (guint8 *card_data)
2074 mword *cards = (mword*)card_data;
2077 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
2083 if (i == CARD_WORDS_PER_BLOCK)
2084 return card_data + CARDS_PER_BLOCK;
2086 #if defined(__i386__) && defined(__GNUC__)
2087 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
2088 #elif defined(__x86_64__) && defined(__GNUC__)
2089 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
2090 #elif defined(__s390x__) && defined(__GNUC__)
2091 return card_data + i * 8 + (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
2093 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
2095 return &card_data [i];
2101 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
2102 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
2103 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
2106 card_offset (char *obj, char *base)
2108 return (obj - base) >> CARD_BITS;
2112 scan_card_table_for_block (MSBlockInfo *block, gboolean mod_union, ScanObjectFunc scan_func, SgenGrayQueue *queue)
2114 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
2115 guint8 cards_copy [CARDS_PER_BLOCK];
2117 gboolean small_objects;
2120 guint8 *card_data, *card_base;
2121 guint8 *card_data_end;
2122 char *scan_front = NULL;
2124 block_obj_size = block->obj_size;
2125 small_objects = block_obj_size < CARD_SIZE_IN_BYTES;
2127 block_start = MS_BLOCK_FOR_BLOCK_INFO (block);
2130 * This is safe in face of card aliasing for the following reason:
2132 * Major blocks are 16k aligned, or 32 cards aligned.
2133 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
2134 * sizes, they won't overflow the cardtable overlap modulus.
2137 card_data = card_base = block->cardtable_mod_union;
2139 * This happens when the nursery collection that precedes finishing
2140 * the concurrent collection allocates new major blocks.
2145 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
2146 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
2148 if (!sgen_card_table_get_card_data (cards_copy, (mword)block_start, CARDS_PER_BLOCK))
2150 card_data = card_base = cards_copy;
2153 card_data_end = card_data + CARDS_PER_BLOCK;
2155 card_data += MS_BLOCK_SKIP >> CARD_BITS;
2157 card_data = initial_skip_card (card_data);
2158 while (card_data < card_data_end) {
2159 size_t card_index, first_object_index;
2162 char *first_obj, *obj;
2164 HEAVY_STAT (++scanned_cards);
2171 card_index = card_data - card_base;
2172 start = (char*)(block_start + card_index * CARD_SIZE_IN_BYTES);
2173 end = start + CARD_SIZE_IN_BYTES;
2175 if (block->state != BLOCK_STATE_SWEPT && block->state != BLOCK_STATE_MARKING)
2176 sweep_block (block);
2178 HEAVY_STAT (++marked_cards);
2181 sgen_card_table_prepare_card_for_scanning (card_data);
2184 * If the card we're looking at starts at or in the block header, we
2185 * must start at the first object in the block, without calculating
2186 * the index of the object we're hypothetically starting at, because
2187 * it would be negative.
2189 if (card_index <= (MS_BLOCK_SKIP >> CARD_BITS))
2190 first_object_index = 0;
2192 first_object_index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
2194 obj = first_obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, first_object_index);
2196 binary_protocol_card_scan (first_obj, end - first_obj);
2199 if (obj < scan_front || !MS_OBJ_ALLOCED_FAST (obj, block_start))
2203 /* FIXME: do this more efficiently */
2205 MS_CALC_MARK_BIT (w, b, obj);
2206 if (!MS_MARK_BIT (block, w, b))
2210 if (small_objects) {
2211 HEAVY_STAT (++scanned_objects);
2212 scan_func (obj, sgen_obj_get_descriptor (obj), queue);
2214 size_t offset = card_offset (obj, block_start);
2215 sgen_cardtable_scan_object (obj, block_obj_size, card_base + offset, mod_union, queue);
2218 obj += block_obj_size;
2219 g_assert (scan_front <= obj);
2223 HEAVY_STAT (if (*card_data) ++remarked_cards);
2228 card_data = card_base + card_offset (obj, block_start);
2233 major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
2235 ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
2237 gboolean do_sweep_checking = sweep_state == SWEEP_STATE_SWEEPING;
2239 if (!concurrent_mark)
2240 g_assert (!mod_union);
2243 switch (sweep_state) {
2244 case SWEEP_STATE_SWEPT:
2245 case SWEEP_STATE_NEED_SWEEPING:
2247 case SWEEP_STATE_SWEEPING:
2248 if (try_set_sweep_state (SWEEP_STATE_SWEEPING_AND_ITERATING, SWEEP_STATE_SWEEPING))
2251 case SWEEP_STATE_SWEEPING_AND_ITERATING:
2252 SGEN_ASSERT (0, FALSE, "Is there another minor collection running?");
2254 case SWEEP_STATE_COMPACTING:
2258 SGEN_ASSERT (0, FALSE, "Invalid sweep state.");
2262 //major_finish_sweeping ();
2265 * We're running with the world stopped and the only other thread doing work is the
2266 * sweep thread, which doesn't add blocks to the array, so we can safely access
2267 * `next_slot` without locking.
2269 for (block_index = 0; block_index < allocated_blocks.next_slot; ++block_index) {
2270 //gboolean has_references;
2274 #ifdef PREFETCH_CARDS
2275 int prefetch_index = block_index + 6;
2276 if (prefetch_index < allocated_blocks.next_slot) {
2277 MSBlockInfo *prefetch_block = BLOCK_UNTAG_HAS_REFERENCES (allocated_blocks.data [prefetch_index]);
2278 guint8 *prefetch_cards = sgen_card_table_get_card_scan_address ((mword)MS_BLOCK_FOR_BLOCK_INFO (prefetch_block));
2279 PREFETCH_READ (prefetch_block);
2280 PREFETCH_WRITE (prefetch_cards);
2281 PREFETCH_WRITE (prefetch_cards + 32);
2285 if (do_sweep_checking) {
2286 LOCK_ALLOCATED_BLOCKS;
2287 tagged_block = allocated_blocks.data [block_index];
2288 block = BLOCK_UNTAG_HAS_REFERENCES (tagged_block);
2290 if (!block || !BLOCK_IS_TAGGED_HAS_REFERENCES (tagged_block)) {
2291 UNLOCK_ALLOCATED_BLOCKS;
2295 if (!ensure_block_is_checked_for_sweeping (block, block_index, NULL))
2298 tagged_block = allocated_blocks.data [block_index];
2299 block = BLOCK_UNTAG_HAS_REFERENCES (tagged_block);
2300 SGEN_ASSERT (0, block, "Why are there holes in the block array when we're not sweeping?");
2302 if (!BLOCK_IS_TAGGED_HAS_REFERENCES (tagged_block))
2306 scan_card_table_for_block (block, mod_union, scan_func, queue);
2309 if (sweep_state == SWEEP_STATE_SWEEPING_AND_ITERATING)
2310 set_sweep_state (SWEEP_STATE_SWEEPING, SWEEP_STATE_SWEEPING_AND_ITERATING);
2314 major_count_cards (long long *num_total_cards, long long *num_marked_cards)
2317 gboolean has_references;
2318 long long total_cards = 0;
2319 long long marked_cards = 0;
2321 if (sweep_in_progress ()) {
2322 *num_total_cards = -1;
2323 *num_marked_cards = -1;
2326 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
2327 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) MS_BLOCK_FOR_BLOCK_INFO (block));
2330 if (!has_references)
2333 total_cards += CARDS_PER_BLOCK;
2334 for (i = 0; i < CARDS_PER_BLOCK; ++i) {
2338 } END_FOREACH_BLOCK;
2340 *num_total_cards = total_cards;
2341 *num_marked_cards = marked_cards;
2345 update_cardtable_mod_union (void)
2349 SGEN_ASSERT (0, !sweep_in_progress (), "Cannot iterate blocks during sweep");
2350 FOREACH_BLOCK (block) {
2353 block->cardtable_mod_union = sgen_card_table_update_mod_union (block->cardtable_mod_union,
2354 MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE, &num_cards);
2356 SGEN_ASSERT (0, num_cards == CARDS_PER_BLOCK, "Number of cards calculation is wrong");
2357 } END_FOREACH_BLOCK;
2361 major_get_cardtable_mod_union_for_object (char *obj)
2363 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
2364 size_t offset = card_offset (obj, (char*)sgen_card_table_align_pointer (MS_BLOCK_FOR_BLOCK_INFO (block)));
2365 return &block->cardtable_mod_union [offset];
2368 #undef pthread_create
2371 post_param_init (SgenMajorCollector *collector)
2373 collector->sweeps_lazily = lazy_sweep;
2377 sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurrent)
2381 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
2383 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
2384 block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2385 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2387 evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2388 for (i = 0; i < num_block_obj_sizes; ++i)
2389 evacuate_block_obj_sizes [i] = FALSE;
2391 sweep_slots_available = sgen_alloc_internal_dynamic (sizeof (size_t) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2392 sweep_slots_used = sgen_alloc_internal_dynamic (sizeof (size_t) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2393 sweep_num_blocks = sgen_alloc_internal_dynamic (sizeof (size_t) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2398 g_print ("block object sizes:\n");
2399 for (i = 0; i < num_block_obj_sizes; ++i)
2400 g_print ("%d\n", block_obj_sizes [i]);
2404 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
2405 free_block_lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2407 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2408 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2409 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2410 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2412 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_alloced);
2413 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_freed);
2414 mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_lazy_swept);
2415 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_objects_evacuated);
2416 #if SIZEOF_VOID_P != 8
2417 mono_counters_register ("# major blocks freed ideally", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_freed_ideal);
2418 mono_counters_register ("# major blocks freed less ideally", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_freed_less_ideal);
2419 mono_counters_register ("# major blocks freed individually", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_freed_individual);
2420 mono_counters_register ("# major blocks allocated less ideally", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_alloced_less_ideal);
2423 collector->section_size = MAJOR_SECTION_SIZE;
2425 concurrent_mark = is_concurrent;
2426 if (is_concurrent) {
2427 collector->is_concurrent = TRUE;
2428 collector->want_synchronous_collection = &want_evacuation;
2430 collector->is_concurrent = FALSE;
2431 collector->want_synchronous_collection = NULL;
2433 collector->get_and_reset_num_major_objects_marked = major_get_and_reset_num_major_objects_marked;
2434 collector->supports_cardtable = TRUE;
2436 collector->alloc_heap = major_alloc_heap;
2437 collector->is_object_live = major_is_object_live;
2438 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2439 collector->alloc_degraded = major_alloc_degraded;
2441 collector->alloc_object = major_alloc_object;
2442 collector->free_pinned_object = free_pinned_object;
2443 collector->iterate_objects = major_iterate_objects;
2444 collector->free_non_pinned_object = major_free_non_pinned_object;
2445 collector->pin_objects = major_pin_objects;
2446 collector->pin_major_object = pin_major_object;
2447 collector->scan_card_table = major_scan_card_table;
2448 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2449 if (is_concurrent) {
2450 collector->update_cardtable_mod_union = update_cardtable_mod_union;
2451 collector->get_cardtable_mod_union_for_object = major_get_cardtable_mod_union_for_object;
2453 collector->init_to_space = major_init_to_space;
2454 collector->sweep = major_sweep;
2455 collector->have_swept = major_have_swept;
2456 collector->finish_sweeping = major_finish_sweeping;
2457 collector->free_swept_blocks = major_free_swept_blocks;
2458 collector->check_scan_starts = major_check_scan_starts;
2459 collector->dump_heap = major_dump_heap;
2460 collector->get_used_size = major_get_used_size;
2461 collector->start_nursery_collection = major_start_nursery_collection;
2462 collector->finish_nursery_collection = major_finish_nursery_collection;
2463 collector->start_major_collection = major_start_major_collection;
2464 collector->finish_major_collection = major_finish_major_collection;
2465 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2466 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2467 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2468 collector->get_num_major_sections = get_num_major_sections;
2469 collector->get_num_major_unswept_old_sections = get_num_major_unswept_old_sections;
2470 collector->handle_gc_param = major_handle_gc_param;
2471 collector->print_gc_param_usage = major_print_gc_param_usage;
2472 collector->post_param_init = post_param_init;
2473 collector->is_valid_object = major_is_valid_object;
2474 collector->describe_pointer = major_describe_pointer;
2475 collector->count_cards = major_count_cards;
2477 collector->major_ops.copy_or_mark_object = major_copy_or_mark_object_canonical;
2478 collector->major_ops.scan_object = major_scan_object_with_evacuation;
2479 if (is_concurrent) {
2480 collector->major_concurrent_ops.copy_or_mark_object = major_copy_or_mark_object_concurrent_canonical;
2481 collector->major_concurrent_ops.scan_object = major_scan_object_no_mark_concurrent;
2482 collector->major_concurrent_ops.scan_vtype = major_scan_vtype_concurrent;
2485 #if !defined (FIXED_HEAP) && !defined (SGEN_PARALLEL_MARK)
2486 /* FIXME: this will not work with evacuation or the split nursery. */
2488 collector->drain_gray_stack = drain_gray_stack;
2490 #ifdef HEAVY_STATISTICS
2491 mono_counters_register ("Optimized copy", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy);
2492 mono_counters_register ("Optimized copy nursery", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_nursery);
2493 mono_counters_register ("Optimized copy nursery forwarded", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_nursery_forwarded);
2494 mono_counters_register ("Optimized copy nursery pinned", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_nursery_pinned);
2495 mono_counters_register ("Optimized copy major", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_major);
2496 mono_counters_register ("Optimized copy major small fast", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_major_small_fast);
2497 mono_counters_register ("Optimized copy major small slow", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_major_small_slow);
2498 mono_counters_register ("Optimized copy major large", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_major_large);
2499 mono_counters_register ("Optimized major scan", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_major_scan);
2500 mono_counters_register ("Optimized major scan no refs", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_major_scan_no_refs);
2502 mono_counters_register ("Gray stack drain loops", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_drain_loops);
2503 mono_counters_register ("Gray stack prefetch fills", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_drain_prefetch_fills);
2504 mono_counters_register ("Gray stack prefetch failures", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_drain_prefetch_fill_failures);
2508 mono_mutex_init (&allocated_blocks_lock);
2510 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
2511 mono_mutex_init (&scanned_objects_list_lock);
2514 SGEN_ASSERT (0, SGEN_MAX_SMALL_OBJ_SIZE <= MS_BLOCK_FREE / 2, "MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2");
2516 /*cardtable requires major pages to be 8 cards aligned*/
2517 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
2521 sgen_marksweep_init (SgenMajorCollector *collector)
2523 sgen_marksweep_init_internal (collector, FALSE);
2527 sgen_marksweep_conc_init (SgenMajorCollector *collector)
2529 sgen_marksweep_init_internal (collector, TRUE);