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))
74 typedef struct _MSBlockInfo MSBlockInfo;
78 unsigned int pinned : 1;
79 unsigned int has_references : 1;
80 unsigned int has_pinned : 1; /* means cannot evacuate */
81 unsigned int is_to_space : 1;
82 unsigned int swept : 1;
84 MSBlockInfo *next_free;
85 size_t pin_queue_first_entry;
86 size_t pin_queue_last_entry;
87 guint8 *cardtable_mod_union;
88 mword mark_words [MS_NUM_MARK_WORDS];
91 #define MS_BLOCK_FOR_BLOCK_INFO(b) ((char*)(b))
93 #define MS_BLOCK_OBJ(b,i) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (b)->obj_size * (i))
94 #define MS_BLOCK_OBJ_FOR_SIZE(b,i,obj_size) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (obj_size) * (i))
95 #define MS_BLOCK_DATA_FOR_OBJ(o) ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
101 #define MS_BLOCK_FOR_OBJ(o) (&((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
103 /* object index will always be small */
104 #define MS_BLOCK_OBJ_INDEX(o,b) ((int)(((char*)(o) - (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP)) / (b)->obj_size))
106 //casting to int is fine since blocks are 32k
107 #define MS_CALC_MARK_BIT(w,b,o) do { \
108 int i = ((int)((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o)))) >> SGEN_ALLOC_ALIGN_BITS; \
109 if (sizeof (mword) == 4) { \
118 #define MS_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] & (ONE_P << (b)))
119 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (ONE_P << (b)))
121 #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))
123 #define MS_BLOCK_OBJ_SIZE_FACTOR (pow (2.0, 1.0 / 3))
126 * This way we can lookup block object size indexes for sizes up to
127 * 256 bytes with a single load.
129 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES 32
131 static int *block_obj_sizes;
132 static int num_block_obj_sizes;
133 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
135 #define MS_BLOCK_FLAG_PINNED 1
136 #define MS_BLOCK_FLAG_REFS 2
138 #define MS_BLOCK_TYPE_MAX 4
140 static gboolean *evacuate_block_obj_sizes;
141 static float evacuation_threshold = 0.666f;
142 static float concurrent_evacuation_threshold = 0.666f;
143 static gboolean want_evacuation = FALSE;
145 static gboolean lazy_sweep = TRUE;
146 static gboolean have_swept;
148 static gboolean concurrent_mark;
150 #define BLOCK_IS_TAGGED_HAS_REFERENCES(bl) SGEN_POINTER_IS_TAGGED_1 ((bl))
151 #define BLOCK_TAG_HAS_REFERENCES(bl) SGEN_POINTER_TAG_1 ((bl))
152 #define BLOCK_UNTAG_HAS_REFERENCES(bl) SGEN_POINTER_UNTAG_1 ((bl))
154 #define BLOCK_TAG(bl) ((bl)->has_references ? BLOCK_TAG_HAS_REFERENCES ((bl)) : (bl))
156 /* all allocated blocks in the system */
157 static SgenPointerQueue allocated_blocks;
159 /* non-allocated block free-list */
160 static void *empty_blocks = NULL;
161 static size_t num_empty_blocks = 0;
163 #define FOREACH_BLOCK(bl) { size_t __index; for (__index = 0; __index < allocated_blocks.next_slot; ++__index) { (bl) = BLOCK_UNTAG_HAS_REFERENCES (allocated_blocks.data [__index]);
164 #define FOREACH_BLOCK_HAS_REFERENCES(bl,hr) { size_t __index; 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));
165 #define END_FOREACH_BLOCK } }
166 #define DELETE_BLOCK_IN_FOREACH() (allocated_blocks.data [__index] = NULL)
168 static size_t num_major_sections = 0;
169 /* one free block list for each block object size */
170 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
172 static guint64 stat_major_blocks_alloced = 0;
173 static guint64 stat_major_blocks_freed = 0;
174 static guint64 stat_major_blocks_lazy_swept = 0;
175 static guint64 stat_major_objects_evacuated = 0;
177 #if SIZEOF_VOID_P != 8
178 static guint64 stat_major_blocks_freed_ideal = 0;
179 static guint64 stat_major_blocks_freed_less_ideal = 0;
180 static guint64 stat_major_blocks_freed_individual = 0;
181 static guint64 stat_major_blocks_alloced_less_ideal = 0;
184 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
185 static guint64 num_major_objects_marked = 0;
186 #define INC_NUM_MAJOR_OBJECTS_MARKED() (++num_major_objects_marked)
188 #define INC_NUM_MAJOR_OBJECTS_MARKED()
191 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
192 static mono_mutex_t scanned_objects_list_lock;
193 static SgenPointerQueue scanned_objects_list;
196 add_scanned_object (void *ptr)
198 if (!binary_protocol_is_enabled ())
201 mono_mutex_lock (&scanned_objects_list_lock);
202 sgen_pointer_queue_add (&scanned_objects_list, ptr);
203 mono_mutex_unlock (&scanned_objects_list_lock);
208 sweep_block (MSBlockInfo *block, gboolean during_major_collection);
211 ms_find_block_obj_size_index (size_t size)
214 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);
215 for (i = 0; i < num_block_obj_sizes; ++i)
216 if (block_obj_sizes [i] >= size)
218 g_error ("no object of size %d\n", size);
221 #define FREE_BLOCKS_FROM(lists,p,r) (lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
222 #define FREE_BLOCKS(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
224 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
225 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
226 fast_block_obj_size_indexes [((s)+7)>>3] : \
227 ms_find_block_obj_size_index ((s)))
230 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
234 start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
236 start = sgen_alloc_os_memory (nursery_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
242 update_heap_boundaries_for_block (MSBlockInfo *block)
244 sgen_update_heap_boundaries ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), (mword)MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE);
248 ms_get_empty_block (void)
252 void *block, *empty, *next;
257 * We try allocating MS_BLOCK_ALLOC_NUM blocks first. If that's
258 * unsuccessful, we halve the number of blocks and try again, until we're at
259 * 1. If that doesn't work, either, we assert.
261 int alloc_num = MS_BLOCK_ALLOC_NUM;
263 p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * alloc_num, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE,
264 alloc_num == 1 ? "major heap section" : NULL);
270 for (i = 0; i < alloc_num; ++i) {
273 * We do the free list update one after the
274 * other so that other threads can use the new
275 * blocks as quickly as possible.
278 empty = empty_blocks;
279 *(void**)block = empty;
280 } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block, empty) != empty);
284 SGEN_ATOMIC_ADD_P (num_empty_blocks, alloc_num);
286 stat_major_blocks_alloced += alloc_num;
287 #if SIZEOF_VOID_P != 8
288 if (alloc_num != MS_BLOCK_ALLOC_NUM)
289 stat_major_blocks_alloced_less_ideal += alloc_num;
294 empty = empty_blocks;
298 next = *(void**)block;
299 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
301 SGEN_ATOMIC_ADD_P (num_empty_blocks, -1);
303 *(void**)block = NULL;
305 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
311 ms_free_block (void *block)
315 sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
316 memset (block, 0, MS_BLOCK_SIZE);
319 empty = empty_blocks;
320 *(void**)block = empty;
321 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
323 SGEN_ATOMIC_ADD_P (num_empty_blocks, 1);
326 //#define MARKSWEEP_CONSISTENCY_CHECK
328 #ifdef MARKSWEEP_CONSISTENCY_CHECK
330 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
334 for (; block; block = block->next_free) {
335 g_assert (block->obj_size == size);
336 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
338 /* blocks in the free lists must have at least
341 g_assert (block->free_list);
343 /* the block must be in the allocated_blocks array */
344 g_assert (sgen_pointer_queue_find (&allocated_blocks, BLOCK_TAG (block)) != (size_t)-1);
349 check_empty_blocks (void)
353 for (p = empty_blocks; p; p = *(void**)p)
355 g_assert (i == num_empty_blocks);
359 consistency_check (void)
364 /* check all blocks */
365 FOREACH_BLOCK (block) {
366 int count = MS_BLOCK_FREE / block->obj_size;
370 /* check block header */
371 g_assert (((MSBlockHeader*)block->block)->info == block);
373 /* count number of free slots */
374 for (i = 0; i < count; ++i) {
375 void **obj = (void**) MS_BLOCK_OBJ (block, i);
376 if (!MS_OBJ_ALLOCED (obj, block))
380 /* check free list */
381 for (free = block->free_list; free; free = (void**)*free) {
382 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
385 g_assert (num_free == 0);
387 /* check all mark words are zero */
389 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
390 g_assert (block->mark_words [i] == 0);
394 /* check free blocks */
395 for (i = 0; i < num_block_obj_sizes; ++i) {
397 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
398 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
401 check_empty_blocks ();
406 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
408 int size = block_obj_sizes [size_index];
409 int count = MS_BLOCK_FREE / size;
411 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
415 if (!sgen_memgov_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
418 info = (MSBlockInfo*)ms_get_empty_block ();
420 SGEN_ASSERT (9, count >= 2, "block with %d objects, it must hold at least 2", count);
422 info->obj_size = size;
423 info->obj_size_index = size_index;
424 info->pinned = pinned;
425 info->has_references = has_references;
426 info->has_pinned = pinned;
428 * Blocks that are to-space are not evacuated from. During an major collection
429 * blocks are allocated for two reasons: evacuating objects from the nursery and
430 * evacuating them from major blocks marked for evacuation. In both cases we don't
431 * want further evacuation.
433 info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD);
435 info->cardtable_mod_union = NULL;
437 update_heap_boundaries_for_block (info);
439 /* build free list */
440 obj_start = MS_BLOCK_FOR_BLOCK_INFO (info) + MS_BLOCK_SKIP;
441 info->free_list = (void**)obj_start;
442 /* we're skipping the last one - it must be nulled */
443 for (i = 0; i < count - 1; ++i) {
444 char *next_obj_start = obj_start + size;
445 *(void**)obj_start = next_obj_start;
446 obj_start = next_obj_start;
449 *(void**)obj_start = NULL;
451 info->next_free = free_blocks [size_index];
452 free_blocks [size_index] = info;
454 sgen_pointer_queue_add (&allocated_blocks, BLOCK_TAG (info));
456 ++num_major_sections;
461 obj_is_from_pinned_alloc (char *ptr)
465 FOREACH_BLOCK (block) {
466 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE)
467 return block->pinned;
473 unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_index)
478 block = free_blocks [size_index];
479 SGEN_ASSERT (9, block, "no free block to unlink from free_blocks %p size_index %d", free_blocks, size_index);
481 if (G_UNLIKELY (!block->swept)) {
482 stat_major_blocks_lazy_swept ++;
483 sweep_block (block, FALSE);
486 obj = block->free_list;
487 SGEN_ASSERT (9, obj, "block %p in free list had no available object to alloc from", block);
489 block->free_list = *(void**)obj;
490 if (!block->free_list) {
491 free_blocks [size_index] = block->next_free;
492 block->next_free = NULL;
499 alloc_obj (MonoVTable *vtable, size_t size, gboolean pinned, gboolean has_references)
501 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
502 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
505 if (!free_blocks [size_index]) {
506 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
510 obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
512 *(MonoVTable**)obj = vtable;
518 major_alloc_object (MonoVTable *vtable, size_t size, gboolean has_references)
520 return alloc_obj (vtable, size, FALSE, has_references);
524 * We're not freeing the block if it's empty. We leave that work for
525 * the next major collection.
527 * This is just called from the domain clearing code, which runs in a
528 * single thread and has the GC lock, so we don't need an extra lock.
531 free_object (char *obj, size_t size, gboolean pinned)
533 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
537 sweep_block (block, FALSE);
538 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);
539 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p is already free", obj);
540 MS_CALC_MARK_BIT (word, bit, obj);
541 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p has mark bit set");
542 if (!block->free_list) {
543 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
544 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
545 SGEN_ASSERT (9, !block->next_free, "block %p doesn't have a free-list of object but belongs to a free-list of blocks");
546 block->next_free = free_blocks [size_index];
547 free_blocks [size_index] = block;
549 memset (obj, 0, size);
550 *(void**)obj = block->free_list;
551 block->free_list = (void**)obj;
555 major_free_non_pinned_object (char *obj, size_t size)
557 free_object (obj, size, FALSE);
560 /* size is a multiple of SGEN_ALLOC_ALIGN */
562 major_alloc_small_pinned_obj (MonoVTable *vtable, size_t size, gboolean has_references)
566 res = alloc_obj (vtable, size, TRUE, has_references);
567 /*If we failed to alloc memory, we better try releasing memory
568 *as pinned alloc is requested by the runtime.
571 sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure", TRUE);
572 res = alloc_obj (vtable, size, TRUE, has_references);
578 free_pinned_object (char *obj, size_t size)
580 free_object (obj, size, TRUE);
584 * size is already rounded up and we hold the GC lock.
587 major_alloc_degraded (MonoVTable *vtable, size_t size)
590 size_t old_num_sections;
592 old_num_sections = num_major_sections;
594 obj = alloc_obj (vtable, size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
595 if (G_LIKELY (obj)) {
596 HEAVY_STAT (++stat_objects_alloced_degraded);
597 HEAVY_STAT (stat_bytes_alloced_degraded += size);
598 g_assert (num_major_sections >= old_num_sections);
599 sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
605 * obj is some object. If it's not in the major heap (i.e. if it's in
606 * the nursery or LOS), return FALSE. Otherwise return whether it's
607 * been marked or copied.
610 major_is_object_live (char *obj)
616 if (sgen_ptr_in_nursery (obj))
619 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
622 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
625 /* now we know it's in a major block */
626 block = MS_BLOCK_FOR_OBJ (obj);
627 SGEN_ASSERT (9, !block->pinned, "block %p is pinned, BTW why is this bad?");
628 MS_CALC_MARK_BIT (word, bit, obj);
629 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
633 major_ptr_is_in_non_pinned_space (char *ptr, char **start)
637 FOREACH_BLOCK (block) {
638 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) {
639 int count = MS_BLOCK_FREE / block->obj_size;
643 for (i = 0; i <= count; ++i) {
644 if (ptr >= MS_BLOCK_OBJ (block, i) && ptr < MS_BLOCK_OBJ (block, i + 1)) {
645 *start = MS_BLOCK_OBJ (block, i);
649 return !block->pinned;
656 major_iterate_objects (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data)
658 gboolean sweep = flags & ITERATE_OBJECTS_SWEEP;
659 gboolean non_pinned = flags & ITERATE_OBJECTS_NON_PINNED;
660 gboolean pinned = flags & ITERATE_OBJECTS_PINNED;
663 FOREACH_BLOCK (block) {
664 int count = MS_BLOCK_FREE / block->obj_size;
667 if (block->pinned && !pinned)
669 if (!block->pinned && !non_pinned)
671 if (sweep && lazy_sweep) {
672 sweep_block (block, FALSE);
673 SGEN_ASSERT (0, block->swept, "Block must be swept after sweeping");
676 for (i = 0; i < count; ++i) {
677 void **obj = (void**) MS_BLOCK_OBJ (block, i);
680 MS_CALC_MARK_BIT (word, bit, obj);
681 if (!MS_MARK_BIT (block, word, bit))
684 if (MS_OBJ_ALLOCED (obj, block))
685 callback ((char*)obj, block->obj_size, data);
691 major_is_valid_object (char *object)
695 FOREACH_BLOCK (block) {
699 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > object) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= object))
702 idx = MS_BLOCK_OBJ_INDEX (object, block);
703 obj = (char*)MS_BLOCK_OBJ (block, idx);
706 return MS_OBJ_ALLOCED (obj, block);
714 major_describe_pointer (char *ptr)
718 FOREACH_BLOCK (block) {
726 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > ptr) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= ptr))
729 SGEN_LOG (0, "major-ptr (block %p sz %d pin %d ref %d)\n",
730 MS_BLOCK_FOR_BLOCK_INFO (block), block->obj_size, block->pinned, block->has_references);
732 idx = MS_BLOCK_OBJ_INDEX (ptr, block);
733 obj = (char*)MS_BLOCK_OBJ (block, idx);
734 live = MS_OBJ_ALLOCED (obj, block);
735 vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
737 MS_CALC_MARK_BIT (w, b, obj);
738 marked = MS_MARK_BIT (block, w, b);
743 SGEN_LOG (0, "object");
745 SGEN_LOG (0, "dead-object");
748 SGEN_LOG (0, "interior-ptr offset %td", ptr - obj);
750 SGEN_LOG (0, "dead-interior-ptr offset %td", ptr - obj);
753 SGEN_LOG (0, " marked %d)\n", marked ? 1 : 0);
762 major_check_scan_starts (void)
767 major_dump_heap (FILE *heap_dump_file)
770 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
771 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
774 for (i = 0; i < num_block_obj_sizes; ++i)
775 slots_available [i] = slots_used [i] = 0;
777 FOREACH_BLOCK (block) {
778 int index = ms_find_block_obj_size_index (block->obj_size);
779 int count = MS_BLOCK_FREE / block->obj_size;
781 slots_available [index] += count;
782 for (i = 0; i < count; ++i) {
783 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
784 ++slots_used [index];
788 fprintf (heap_dump_file, "<occupancies>\n");
789 for (i = 0; i < num_block_obj_sizes; ++i) {
790 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
791 block_obj_sizes [i], slots_available [i], slots_used [i]);
793 fprintf (heap_dump_file, "</occupancies>\n");
795 FOREACH_BLOCK (block) {
796 int count = MS_BLOCK_FREE / block->obj_size;
800 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
802 for (i = 0; i <= count; ++i) {
803 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
808 sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), MS_BLOCK_FOR_BLOCK_INFO (block));
814 fprintf (heap_dump_file, "</section>\n");
818 #define LOAD_VTABLE SGEN_LOAD_VTABLE
820 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,desc,block,queue) do { \
822 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
823 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
824 MS_SET_MARK_BIT ((block), __word, __bit); \
825 if (sgen_gc_descr_has_references (desc)) \
826 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
827 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
828 INC_NUM_MAJOR_OBJECTS_MARKED (); \
831 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,desc,block,queue) do { \
833 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
834 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
835 if (!MS_MARK_BIT ((block), __word, __bit)) { \
836 MS_SET_MARK_BIT ((block), __word, __bit); \
837 if (sgen_gc_descr_has_references (desc)) \
838 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
839 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
840 INC_NUM_MAJOR_OBJECTS_MARKED (); \
845 pin_major_object (char *obj, SgenGrayQueue *queue)
850 g_assert_not_reached ();
852 block = MS_BLOCK_FOR_OBJ (obj);
853 block->has_pinned = TRUE;
854 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
857 #include "sgen-major-copy-object.h"
860 major_copy_or_mark_object_with_evacuation_concurrent (void **ptr, void *obj, SgenGrayQueue *queue)
862 SGEN_ASSERT (9, sgen_concurrent_collection_in_progress (), "Why are we scanning concurrently when there's no concurrent collection on?");
863 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!");
865 g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
867 if (!sgen_ptr_in_nursery (obj)) {
870 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
872 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE) {
873 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
874 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
876 if (sgen_los_object_is_pinned (obj))
880 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
881 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
882 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
886 sgen_los_pin_object (obj);
887 if (SGEN_OBJECT_HAS_REFERENCES (obj))
888 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_obj_get_descriptor (obj));
889 INC_NUM_MAJOR_OBJECTS_MARKED ();
895 major_get_and_reset_num_major_objects_marked (void)
897 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
898 long long num = num_major_objects_marked;
899 num_major_objects_marked = 0;
906 #ifdef HEAVY_STATISTICS
907 static guint64 stat_optimized_copy;
908 static guint64 stat_optimized_copy_nursery;
909 static guint64 stat_optimized_copy_nursery_forwarded;
910 static guint64 stat_optimized_copy_nursery_pinned;
911 static guint64 stat_optimized_copy_major;
912 static guint64 stat_optimized_copy_major_small_fast;
913 static guint64 stat_optimized_copy_major_small_slow;
914 static guint64 stat_optimized_copy_major_large;
915 static guint64 stat_optimized_copy_major_forwarded;
916 static guint64 stat_optimized_copy_major_small_evacuate;
917 static guint64 stat_optimized_major_scan;
918 static guint64 stat_optimized_major_scan_no_refs;
920 static guint64 stat_drain_prefetch_fills;
921 static guint64 stat_drain_prefetch_fill_failures;
922 static guint64 stat_drain_loops;
925 static void major_scan_object_with_evacuation (char *start, mword desc, SgenGrayQueue *queue);
927 #define COPY_OR_MARK_FUNCTION_NAME major_copy_or_mark_object_no_evacuation
928 #define SCAN_OBJECT_FUNCTION_NAME major_scan_object_no_evacuation
929 #define DRAIN_GRAY_STACK_FUNCTION_NAME drain_gray_stack_no_evacuation
930 #include "sgen-marksweep-drain-gray-stack.h"
932 #define COPY_OR_MARK_WITH_EVACUATION
933 #define COPY_OR_MARK_FUNCTION_NAME major_copy_or_mark_object_with_evacuation
934 #define SCAN_OBJECT_FUNCTION_NAME major_scan_object_with_evacuation
935 #define DRAIN_GRAY_STACK_FUNCTION_NAME drain_gray_stack_with_evacuation
936 #include "sgen-marksweep-drain-gray-stack.h"
939 drain_gray_stack (ScanCopyContext ctx)
941 gboolean evacuation = FALSE;
943 for (i = 0; i < num_block_obj_sizes; ++i) {
944 if (evacuate_block_obj_sizes [i]) {
951 return drain_gray_stack_with_evacuation (ctx);
953 return drain_gray_stack_no_evacuation (ctx);
956 #include "sgen-marksweep-scan-object-concurrent.h"
959 major_copy_or_mark_object_canonical (void **ptr, SgenGrayQueue *queue)
961 major_copy_or_mark_object_with_evacuation (ptr, *ptr, queue);
965 major_copy_or_mark_object_concurrent_canonical (void **ptr, SgenGrayQueue *queue)
967 major_copy_or_mark_object_with_evacuation_concurrent (ptr, *ptr, queue);
971 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
976 if (block->pin_queue_first_entry == block->pin_queue_last_entry)
979 block->has_pinned = TRUE;
981 entry = sgen_pinning_get_entry (block->pin_queue_first_entry);
982 end = sgen_pinning_get_entry (block->pin_queue_last_entry);
984 for (; entry < end; ++entry) {
985 int index = MS_BLOCK_OBJ_INDEX (*entry, block);
987 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);
988 if (index == last_index)
990 obj = MS_BLOCK_OBJ (block, index);
991 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (obj, sgen_obj_get_descriptor (obj), block, queue);
997 sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
1001 for (obj_index = 0; obj_index < count; ++obj_index) {
1003 void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
1005 MS_CALC_MARK_BIT (word, bit, obj);
1006 if (MS_MARK_BIT (block, word, bit)) {
1007 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
1009 /* an unmarked object */
1010 if (MS_OBJ_ALLOCED (obj, block)) {
1012 * FIXME: Merge consecutive
1013 * slots for lower reporting
1014 * overhead. Maybe memset
1015 * will also benefit?
1017 binary_protocol_empty (obj, obj_size);
1018 MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
1019 memset (obj, 0, obj_size);
1021 *(void**)obj = block->free_list;
1022 block->free_list = obj;
1030 * Traverse BLOCK, freeing and zeroing unused objects.
1033 sweep_block (MSBlockInfo *block, gboolean during_major_collection)
1036 void *reversed = NULL;
1038 if (!during_major_collection)
1039 g_assert (!sgen_concurrent_collection_in_progress ());
1044 count = MS_BLOCK_FREE / block->obj_size;
1046 block->free_list = NULL;
1048 /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
1049 // FIXME: Add more sizes
1050 switch (block->obj_size) {
1052 sweep_block_for_size (block, count, 16);
1055 sweep_block_for_size (block, count, block->obj_size);
1059 /* reset mark bits */
1060 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1062 /* Reverse free list so that it's in address order */
1064 while (block->free_list) {
1065 void *next = *(void**)block->free_list;
1066 *(void**)block->free_list = reversed;
1067 reversed = block->free_list;
1068 block->free_list = next;
1070 block->free_list = reversed;
1081 if (sizeof (mword) == sizeof (unsigned long))
1082 count += __builtin_popcountl (d);
1084 count += __builtin_popcount (d);
1100 /* statistics for evacuation */
1101 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1102 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1103 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1105 mword total_evacuate_heap = 0;
1106 mword total_evacuate_saved = 0;
1108 for (i = 0; i < num_block_obj_sizes; ++i)
1109 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1111 /* clear all the free lists */
1112 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1113 MSBlockInfo **free_blocks = free_block_lists [i];
1115 for (j = 0; j < num_block_obj_sizes; ++j)
1116 free_blocks [j] = NULL;
1119 /* traverse all blocks, free and zero unmarked objects */
1120 FOREACH_BLOCK (block) {
1122 gboolean have_live = FALSE;
1123 gboolean has_pinned;
1124 gboolean have_free = FALSE;
1128 obj_size_index = block->obj_size_index;
1130 has_pinned = block->has_pinned;
1131 block->has_pinned = block->pinned;
1133 block->is_to_space = FALSE;
1136 count = MS_BLOCK_FREE / block->obj_size;
1138 if (block->cardtable_mod_union) {
1139 sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
1140 block->cardtable_mod_union = NULL;
1143 /* Count marked objects in the block */
1144 for (i = 0; i < MS_NUM_MARK_WORDS; ++i) {
1145 nused += bitcount (block->mark_words [i]);
1154 sweep_block (block, TRUE);
1158 ++num_blocks [obj_size_index];
1159 slots_used [obj_size_index] += nused;
1160 slots_available [obj_size_index] += count;
1164 * If there are free slots in the block, add
1165 * the block to the corresponding free list.
1168 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1169 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1170 block->next_free = free_blocks [index];
1171 free_blocks [index] = block;
1174 update_heap_boundaries_for_block (block);
1177 * Blocks without live objects are removed from the
1178 * block list and freed.
1180 DELETE_BLOCK_IN_FOREACH ();
1182 binary_protocol_empty (MS_BLOCK_OBJ (block, 0), (char*)MS_BLOCK_OBJ (block, count) - (char*)MS_BLOCK_OBJ (block, 0));
1183 ms_free_block (block);
1185 --num_major_sections;
1187 } END_FOREACH_BLOCK;
1188 sgen_pointer_queue_remove_nulls (&allocated_blocks);
1190 for (i = 0; i < num_block_obj_sizes; ++i) {
1191 float usage = (float)slots_used [i] / (float)slots_available [i];
1192 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1193 evacuate_block_obj_sizes [i] = TRUE;
1195 g_print ("slot size %d - %d of %d used\n",
1196 block_obj_sizes [i], slots_used [i], slots_available [i]);
1199 evacuate_block_obj_sizes [i] = FALSE;
1202 mword total_bytes = block_obj_sizes [i] * slots_available [i];
1203 total_evacuate_heap += total_bytes;
1204 if (evacuate_block_obj_sizes [i])
1205 total_evacuate_saved += total_bytes - block_obj_sizes [i] * slots_used [i];
1209 want_evacuation = (float)total_evacuate_saved / (float)total_evacuate_heap > (1 - concurrent_evacuation_threshold);
1220 static int count_pinned_ref;
1221 static int count_pinned_nonref;
1222 static int count_nonpinned_ref;
1223 static int count_nonpinned_nonref;
1226 count_nonpinned_callback (char *obj, size_t size, void *data)
1228 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1230 if (vtable->klass->has_references)
1231 ++count_nonpinned_ref;
1233 ++count_nonpinned_nonref;
1237 count_pinned_callback (char *obj, size_t size, void *data)
1239 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1241 if (vtable->klass->has_references)
1244 ++count_pinned_nonref;
1247 static G_GNUC_UNUSED void
1248 count_ref_nonref_objs (void)
1252 count_pinned_ref = 0;
1253 count_pinned_nonref = 0;
1254 count_nonpinned_ref = 0;
1255 count_nonpinned_nonref = 0;
1257 major_iterate_objects (ITERATE_OBJECTS_SWEEP_NON_PINNED, count_nonpinned_callback, NULL);
1258 major_iterate_objects (ITERATE_OBJECTS_SWEEP_PINNED, count_pinned_callback, NULL);
1260 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1262 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1263 count_pinned_ref, count_nonpinned_ref,
1264 count_pinned_nonref, count_nonpinned_nonref,
1265 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1269 ms_calculate_block_obj_sizes (double factor, int *arr)
1271 double target_size = sizeof (MonoObject);
1276 int target_count = (int)ceil (MS_BLOCK_FREE / target_size);
1277 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1279 if (size != last_size) {
1281 arr [num_sizes] = size;
1286 target_size *= factor;
1287 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1292 /* only valid during minor collections */
1293 static mword old_num_major_sections;
1296 major_start_nursery_collection (void)
1298 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1299 consistency_check ();
1302 old_num_major_sections = num_major_sections;
1306 major_finish_nursery_collection (void)
1308 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1309 consistency_check ();
1311 sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1315 major_start_major_collection (void)
1319 /* clear the free lists */
1320 for (i = 0; i < num_block_obj_sizes; ++i) {
1321 if (!evacuate_block_obj_sizes [i])
1324 free_block_lists [0][i] = NULL;
1325 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1328 // Sweep all unswept blocks
1332 MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
1334 FOREACH_BLOCK (block) {
1335 sweep_block (block, TRUE);
1336 } END_FOREACH_BLOCK;
1338 MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
1343 major_finish_major_collection (ScannedObjectCounts *counts)
1345 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
1346 if (binary_protocol_is_enabled ()) {
1347 counts->num_scanned_objects = scanned_objects_list.next_slot;
1349 sgen_pointer_queue_sort_uniq (&scanned_objects_list);
1350 counts->num_unique_scanned_objects = scanned_objects_list.next_slot;
1352 sgen_pointer_queue_clear (&scanned_objects_list);
1357 #if SIZEOF_VOID_P != 8
1359 compare_pointers (const void *va, const void *vb) {
1360 char *a = *(char**)va, *b = *(char**)vb;
1370 major_have_computer_minor_collection_allowance (void)
1372 size_t section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1374 g_assert (have_swept);
1376 #if SIZEOF_VOID_P != 8
1378 int i, num_empty_blocks_orig, num_blocks, arr_length;
1380 void **empty_block_arr;
1381 void **rebuild_next;
1385 * sgen_free_os_memory () asserts in mono_vfree () because windows doesn't like freeing the middle of
1386 * a VirtualAlloc ()-ed block.
1391 if (num_empty_blocks <= section_reserve)
1393 SGEN_ASSERT (0, num_empty_blocks > 0, "section reserve can't be negative");
1395 num_empty_blocks_orig = num_empty_blocks;
1396 empty_block_arr = (void**)sgen_alloc_internal_dynamic (sizeof (void*) * num_empty_blocks_orig,
1397 INTERNAL_MEM_MS_BLOCK_INFO_SORT, FALSE);
1398 if (!empty_block_arr)
1402 for (block = empty_blocks; block; block = *(void**)block)
1403 empty_block_arr [i++] = block;
1404 SGEN_ASSERT (0, i == num_empty_blocks, "empty block count wrong");
1406 sgen_qsort (empty_block_arr, num_empty_blocks, sizeof (void*), compare_pointers);
1409 * We iterate over the free blocks, trying to find MS_BLOCK_ALLOC_NUM
1410 * contiguous ones. If we do, we free them. If that's not enough to get to
1411 * section_reserve, we halve the number of contiguous blocks we're looking
1412 * for and have another go, until we're done with looking for pairs of
1413 * blocks, at which point we give up and go to the fallback.
1415 arr_length = num_empty_blocks_orig;
1416 num_blocks = MS_BLOCK_ALLOC_NUM;
1417 while (num_empty_blocks > section_reserve && num_blocks > 1) {
1422 for (i = 0; i < arr_length; ++i) {
1424 void *block = empty_block_arr [i];
1425 SGEN_ASSERT (0, block, "we're not shifting correctly");
1427 empty_block_arr [dest] = block;
1429 * This is not strictly necessary, but we're
1432 empty_block_arr [i] = NULL;
1441 SGEN_ASSERT (0, first >= 0 && d > first, "algorithm is wrong");
1443 if ((char*)block != ((char*)empty_block_arr [d-1]) + MS_BLOCK_SIZE) {
1448 if (d + 1 - first == num_blocks) {
1450 * We found num_blocks contiguous blocks. Free them
1451 * and null their array entries. As an optimization
1452 * we could, instead of nulling the entries, shift
1453 * the following entries over to the left, while
1457 sgen_free_os_memory (empty_block_arr [first], MS_BLOCK_SIZE * num_blocks, SGEN_ALLOC_HEAP);
1458 for (j = first; j <= d; ++j)
1459 empty_block_arr [j] = NULL;
1463 num_empty_blocks -= num_blocks;
1465 stat_major_blocks_freed += num_blocks;
1466 if (num_blocks == MS_BLOCK_ALLOC_NUM)
1467 stat_major_blocks_freed_ideal += num_blocks;
1469 stat_major_blocks_freed_less_ideal += num_blocks;
1474 SGEN_ASSERT (0, dest <= i && dest <= arr_length, "array length is off");
1476 SGEN_ASSERT (0, arr_length == num_empty_blocks, "array length is off");
1481 /* rebuild empty_blocks free list */
1482 rebuild_next = (void**)&empty_blocks;
1483 for (i = 0; i < arr_length; ++i) {
1484 void *block = empty_block_arr [i];
1485 SGEN_ASSERT (0, block, "we're missing blocks");
1486 *rebuild_next = block;
1487 rebuild_next = (void**)block;
1489 *rebuild_next = NULL;
1492 sgen_free_internal_dynamic (empty_block_arr, sizeof (void*) * num_empty_blocks_orig, INTERNAL_MEM_MS_BLOCK_INFO_SORT);
1495 SGEN_ASSERT (0, num_empty_blocks >= 0, "we freed more blocks than we had in the first place?");
1499 * This is our threshold. If there's not more empty than used blocks, we won't
1500 * release uncontiguous blocks, in fear of fragmenting the address space.
1502 if (num_empty_blocks <= num_major_sections)
1506 while (num_empty_blocks > section_reserve) {
1507 void *next = *(void**)empty_blocks;
1508 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
1509 empty_blocks = next;
1511 * Needs not be atomic because this is running
1516 ++stat_major_blocks_freed;
1517 #if SIZEOF_VOID_P != 8
1518 ++stat_major_blocks_freed_individual;
1524 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1528 FOREACH_BLOCK (block) {
1529 sgen_find_optimized_pin_queue_area (MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SKIP, MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE,
1530 &block->pin_queue_first_entry, &block->pin_queue_last_entry);
1531 } END_FOREACH_BLOCK;
1535 major_pin_objects (SgenGrayQueue *queue)
1539 FOREACH_BLOCK (block) {
1540 mark_pinned_objects_in_block (block, queue);
1541 } END_FOREACH_BLOCK;
1545 major_init_to_space (void)
1550 major_report_pinned_memory_usage (void)
1552 g_assert_not_reached ();
1556 major_get_used_size (void)
1561 FOREACH_BLOCK (block) {
1562 int count = MS_BLOCK_FREE / block->obj_size;
1564 size += count * block->obj_size;
1565 for (iter = block->free_list; iter; iter = (void**)*iter)
1566 size -= block->obj_size;
1567 } END_FOREACH_BLOCK;
1573 get_num_major_sections (void)
1575 return num_major_sections;
1579 major_handle_gc_param (const char *opt)
1581 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1582 const char *arg = strchr (opt, '=') + 1;
1583 int percentage = atoi (arg);
1584 if (percentage < 0 || percentage > 100) {
1585 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1588 evacuation_threshold = (float)percentage / 100.0f;
1590 } else if (!strcmp (opt, "lazy-sweep")) {
1593 } else if (!strcmp (opt, "no-lazy-sweep")) {
1602 major_print_gc_param_usage (void)
1606 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1607 " (no-)lazy-sweep\n"
1612 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1615 gboolean has_references;
1617 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1619 callback ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE);
1620 } END_FOREACH_BLOCK;
1623 #ifdef HEAVY_STATISTICS
1624 extern guint64 marked_cards;
1625 extern guint64 scanned_cards;
1626 extern guint64 scanned_objects;
1627 extern guint64 remarked_cards;
1630 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1632 * MS blocks are 16K aligned.
1633 * Cardtables are 4K aligned, at least.
1634 * This means that the cardtable of a given block is 32 bytes aligned.
1637 initial_skip_card (guint8 *card_data)
1639 mword *cards = (mword*)card_data;
1642 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1648 if (i == CARD_WORDS_PER_BLOCK)
1649 return card_data + CARDS_PER_BLOCK;
1651 #if defined(__i386__) && defined(__GNUC__)
1652 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1653 #elif defined(__x86_64__) && defined(__GNUC__)
1654 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1655 #elif defined(__s390x__) && defined(__GNUC__)
1656 return card_data + i * 8 + (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
1658 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1660 return &card_data [i];
1667 static G_GNUC_UNUSED guint8*
1668 skip_card (guint8 *card_data, guint8 *card_data_end)
1670 while (card_data < card_data_end && !*card_data)
1675 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1676 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1677 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1680 major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
1683 gboolean has_references;
1684 ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
1686 if (!concurrent_mark)
1687 g_assert (!mod_union);
1689 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1693 if (!has_references)
1696 block_obj_size = block->obj_size;
1697 block_start = MS_BLOCK_FOR_BLOCK_INFO (block);
1699 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1701 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1702 guint8 cards_data [CARDS_PER_BLOCK];
1704 char *obj, *end, *base;
1707 cards = block->cardtable_mod_union;
1709 * This happens when the nursery
1710 * collection that precedes finishing
1711 * the concurrent collection allocates
1717 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1718 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1719 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1722 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1727 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1728 end = block_start + MS_BLOCK_SIZE;
1729 base = sgen_card_table_align_pointer (obj);
1731 cards += MS_BLOCK_SKIP >> CARD_BITS;
1737 sweep_block (block, FALSE);
1739 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
1743 /* FIXME: do this more efficiently */
1745 MS_CALC_MARK_BIT (w, b, obj);
1746 if (!MS_MARK_BIT (block, w, b))
1750 card_offset = (obj - base) >> CARD_BITS;
1751 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, mod_union, queue);
1754 obj += block_obj_size;
1757 guint8 *card_data, *card_base;
1758 guint8 *card_data_end;
1761 * This is safe in face of card aliasing for the following reason:
1763 * Major blocks are 16k aligned, or 32 cards aligned.
1764 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1765 * sizes, they won't overflow the cardtable overlap modulus.
1768 card_data = card_base = block->cardtable_mod_union;
1770 * This happens when the nursery
1771 * collection that precedes finishing
1772 * the concurrent collection allocates
1778 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1780 card_data_end = card_data + CARDS_PER_BLOCK;
1782 card_data += MS_BLOCK_SKIP >> CARD_BITS;
1784 for (card_data = initial_skip_card (card_data); card_data < card_data_end; ++card_data) { //card_data = skip_card (card_data + 1, card_data_end)) {
1786 size_t idx = card_data - card_base;
1787 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1788 char *end = start + CARD_SIZE_IN_BYTES;
1789 char *first_obj, *obj;
1791 HEAVY_STAT (++scanned_cards);
1797 sweep_block (block, FALSE);
1799 HEAVY_STAT (++marked_cards);
1801 sgen_card_table_prepare_card_for_scanning (card_data);
1806 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1808 obj = first_obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1810 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
1814 /* FIXME: do this more efficiently */
1816 MS_CALC_MARK_BIT (w, b, obj);
1817 if (!MS_MARK_BIT (block, w, b))
1821 HEAVY_STAT (++scanned_objects);
1822 scan_func (obj, sgen_obj_get_descriptor (obj), queue);
1824 obj += block_obj_size;
1826 HEAVY_STAT (if (*card_data) ++remarked_cards);
1827 binary_protocol_card_scan (first_obj, obj - first_obj);
1830 } END_FOREACH_BLOCK;
1834 major_count_cards (long long *num_total_cards, long long *num_marked_cards)
1837 gboolean has_references;
1838 long long total_cards = 0;
1839 long long marked_cards = 0;
1841 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1842 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) MS_BLOCK_FOR_BLOCK_INFO (block));
1845 if (!has_references)
1848 total_cards += CARDS_PER_BLOCK;
1849 for (i = 0; i < CARDS_PER_BLOCK; ++i) {
1853 } END_FOREACH_BLOCK;
1855 *num_total_cards = total_cards;
1856 *num_marked_cards = marked_cards;
1860 update_cardtable_mod_union (void)
1864 FOREACH_BLOCK (block) {
1867 block->cardtable_mod_union = sgen_card_table_update_mod_union (block->cardtable_mod_union,
1868 MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE, &num_cards);
1870 SGEN_ASSERT (0, num_cards == CARDS_PER_BLOCK, "Number of cards calculation is wrong");
1871 } END_FOREACH_BLOCK;
1875 major_get_cardtable_mod_union_for_object (char *obj)
1877 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1878 return &block->cardtable_mod_union [(obj - (char*)sgen_card_table_align_pointer (MS_BLOCK_FOR_BLOCK_INFO (block))) >> CARD_BITS];
1882 alloc_free_block_lists (MSBlockInfo ***lists)
1885 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1886 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
1889 #undef pthread_create
1892 post_param_init (SgenMajorCollector *collector)
1894 collector->sweeps_lazily = lazy_sweep;
1898 sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurrent)
1902 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1904 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
1905 block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
1906 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
1908 evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
1909 for (i = 0; i < num_block_obj_sizes; ++i)
1910 evacuate_block_obj_sizes [i] = FALSE;
1915 g_print ("block object sizes:\n");
1916 for (i = 0; i < num_block_obj_sizes; ++i)
1917 g_print ("%d\n", block_obj_sizes [i]);
1921 alloc_free_block_lists (free_block_lists);
1923 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
1924 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
1925 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
1926 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
1928 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_alloced);
1929 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_freed);
1930 mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_lazy_swept);
1931 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_objects_evacuated);
1932 #if SIZEOF_VOID_P != 8
1933 mono_counters_register ("# major blocks freed ideally", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_freed_ideal);
1934 mono_counters_register ("# major blocks freed less ideally", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_freed_less_ideal);
1935 mono_counters_register ("# major blocks freed individually", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_freed_individual);
1936 mono_counters_register ("# major blocks allocated less ideally", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_major_blocks_alloced_less_ideal);
1939 collector->section_size = MAJOR_SECTION_SIZE;
1941 concurrent_mark = is_concurrent;
1942 if (is_concurrent) {
1943 collector->is_concurrent = TRUE;
1944 collector->want_synchronous_collection = &want_evacuation;
1946 collector->is_concurrent = FALSE;
1947 collector->want_synchronous_collection = NULL;
1949 collector->get_and_reset_num_major_objects_marked = major_get_and_reset_num_major_objects_marked;
1950 collector->supports_cardtable = TRUE;
1952 collector->have_swept = &have_swept;
1954 collector->alloc_heap = major_alloc_heap;
1955 collector->is_object_live = major_is_object_live;
1956 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
1957 collector->alloc_degraded = major_alloc_degraded;
1959 collector->alloc_object = major_alloc_object;
1960 collector->free_pinned_object = free_pinned_object;
1961 collector->iterate_objects = major_iterate_objects;
1962 collector->free_non_pinned_object = major_free_non_pinned_object;
1963 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
1964 collector->pin_objects = major_pin_objects;
1965 collector->pin_major_object = pin_major_object;
1966 collector->scan_card_table = major_scan_card_table;
1967 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
1968 if (is_concurrent) {
1969 collector->update_cardtable_mod_union = update_cardtable_mod_union;
1970 collector->get_cardtable_mod_union_for_object = major_get_cardtable_mod_union_for_object;
1972 collector->init_to_space = major_init_to_space;
1973 collector->sweep = major_sweep;
1974 collector->check_scan_starts = major_check_scan_starts;
1975 collector->dump_heap = major_dump_heap;
1976 collector->get_used_size = major_get_used_size;
1977 collector->start_nursery_collection = major_start_nursery_collection;
1978 collector->finish_nursery_collection = major_finish_nursery_collection;
1979 collector->start_major_collection = major_start_major_collection;
1980 collector->finish_major_collection = major_finish_major_collection;
1981 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
1982 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
1983 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
1984 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
1985 collector->get_num_major_sections = get_num_major_sections;
1986 collector->handle_gc_param = major_handle_gc_param;
1987 collector->print_gc_param_usage = major_print_gc_param_usage;
1988 collector->post_param_init = post_param_init;
1989 collector->is_valid_object = major_is_valid_object;
1990 collector->describe_pointer = major_describe_pointer;
1991 collector->count_cards = major_count_cards;
1993 collector->major_ops.copy_or_mark_object = major_copy_or_mark_object_canonical;
1994 collector->major_ops.scan_object = major_scan_object_with_evacuation;
1995 if (is_concurrent) {
1996 collector->major_concurrent_ops.copy_or_mark_object = major_copy_or_mark_object_concurrent_canonical;
1997 collector->major_concurrent_ops.scan_object = major_scan_object_no_mark_concurrent;
1998 collector->major_concurrent_ops.scan_vtype = major_scan_vtype_concurrent;
2001 #if !defined (FIXED_HEAP) && !defined (SGEN_PARALLEL_MARK)
2002 /* FIXME: this will not work with evacuation or the split nursery. */
2004 collector->drain_gray_stack = drain_gray_stack;
2006 #ifdef HEAVY_STATISTICS
2007 mono_counters_register ("Optimized copy", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy);
2008 mono_counters_register ("Optimized copy nursery", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_nursery);
2009 mono_counters_register ("Optimized copy nursery forwarded", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_nursery_forwarded);
2010 mono_counters_register ("Optimized copy nursery pinned", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_nursery_pinned);
2011 mono_counters_register ("Optimized copy major", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_major);
2012 mono_counters_register ("Optimized copy major small fast", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_major_small_fast);
2013 mono_counters_register ("Optimized copy major small slow", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_major_small_slow);
2014 mono_counters_register ("Optimized copy major large", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_copy_major_large);
2015 mono_counters_register ("Optimized major scan", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_major_scan);
2016 mono_counters_register ("Optimized major scan no refs", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_optimized_major_scan_no_refs);
2018 mono_counters_register ("Gray stack drain loops", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_drain_loops);
2019 mono_counters_register ("Gray stack prefetch fills", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_drain_prefetch_fills);
2020 mono_counters_register ("Gray stack prefetch failures", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_drain_prefetch_fill_failures);
2024 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
2025 mono_mutex_init (&scanned_objects_list_lock);
2028 SGEN_ASSERT (0, SGEN_MAX_SMALL_OBJ_SIZE <= MS_BLOCK_FREE / 2, "MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2");
2030 /*cardtable requires major pages to be 8 cards aligned*/
2031 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
2035 sgen_marksweep_init (SgenMajorCollector *collector)
2037 sgen_marksweep_init_internal (collector, FALSE);
2041 sgen_marksweep_conc_init (SgenMajorCollector *collector)
2043 sgen_marksweep_init_internal (collector, TRUE);