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 #define SGEN_HAVE_CONCURRENT_MARK
49 #define MS_BLOCK_SIZE (16*1024)
50 #define MS_BLOCK_SIZE_SHIFT 14
51 #define MAJOR_SECTION_SIZE MS_BLOCK_SIZE
52 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
55 * Don't allocate single blocks, but alloc a contingent of this many
56 * blocks in one swoop. This must be a power of two.
58 #define MS_BLOCK_ALLOC_NUM 32
61 * Number of bytes before the first object in a block. At the start
62 * of a block is the MSBlockHeader, then opional padding, then come
63 * the objects, so this must be >= sizeof (MSBlockHeader).
65 #define MS_BLOCK_SKIP 16
67 #define MS_BLOCK_FREE (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
69 #define MS_NUM_MARK_WORDS ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
71 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
72 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
75 typedef struct _MSBlockInfo MSBlockInfo;
79 unsigned int pinned : 1;
80 unsigned int has_references : 1;
81 unsigned int has_pinned : 1; /* means cannot evacuate */
82 unsigned int is_to_space : 1;
83 unsigned int swept : 1;
86 MSBlockInfo *next_free;
87 size_t pin_queue_first_entry;
88 size_t pin_queue_last_entry;
89 #ifdef SGEN_HAVE_CONCURRENT_MARK
90 guint8 *cardtable_mod_union;
92 mword mark_words [MS_NUM_MARK_WORDS];
95 #define MS_BLOCK_FOR_BLOCK_INFO(b) ((b)->block)
97 #define MS_BLOCK_OBJ(b,i) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (b)->obj_size * (i))
98 #define MS_BLOCK_OBJ_FOR_SIZE(b,i,obj_size) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (obj_size) * (i))
99 #define MS_BLOCK_DATA_FOR_OBJ(o) ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
105 #define MS_BLOCK_FOR_OBJ(o) (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
107 /* object index will always be small */
108 #define MS_BLOCK_OBJ_INDEX(o,b) ((int)(((char*)(o) - (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP)) / (b)->obj_size))
110 //casting to int is fine since blocks are 32k
111 #define MS_CALC_MARK_BIT(w,b,o) do { \
112 int i = ((int)((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o)))) >> SGEN_ALLOC_ALIGN_BITS; \
113 if (sizeof (mword) == 4) { \
122 #define MS_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] & (ONE_P << (b)))
123 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (ONE_P << (b)))
125 #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))
127 #define MS_BLOCK_OBJ_SIZE_FACTOR (sqrt (2.0))
130 * This way we can lookup block object size indexes for sizes up to
131 * 256 bytes with a single load.
133 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES 32
135 static int *block_obj_sizes;
136 static int num_block_obj_sizes;
137 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
139 #define MS_BLOCK_FLAG_PINNED 1
140 #define MS_BLOCK_FLAG_REFS 2
142 #define MS_BLOCK_TYPE_MAX 4
144 static gboolean *evacuate_block_obj_sizes;
145 static float evacuation_threshold = 0.666f;
146 #ifdef SGEN_HAVE_CONCURRENT_MARK
147 static float concurrent_evacuation_threshold = 0.666f;
148 static gboolean want_evacuation = FALSE;
151 static gboolean lazy_sweep = TRUE;
152 static gboolean have_swept;
154 #ifdef SGEN_HAVE_CONCURRENT_MARK
155 static gboolean concurrent_mark;
158 #define BLOCK_IS_TAGGED_HAS_REFERENCES(bl) SGEN_POINTER_IS_TAGGED_1 ((bl))
159 #define BLOCK_TAG_HAS_REFERENCES(bl) SGEN_POINTER_TAG_1 ((bl))
160 #define BLOCK_UNTAG_HAS_REFERENCES(bl) SGEN_POINTER_UNTAG_1 ((bl))
162 #define BLOCK_TAG(bl) ((bl)->has_references ? BLOCK_TAG_HAS_REFERENCES ((bl)) : (bl))
164 /* all allocated blocks in the system */
165 static SgenPointerQueue allocated_blocks;
167 /* non-allocated block free-list */
168 static void *empty_blocks = NULL;
169 static size_t num_empty_blocks = 0;
171 #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]);
172 #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));
173 #define END_FOREACH_BLOCK } }
174 #define DELETE_BLOCK_IN_FOREACH() (allocated_blocks.data [__index] = NULL)
176 static size_t num_major_sections = 0;
177 /* one free block list for each block object size */
178 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
180 static long long stat_major_blocks_alloced = 0;
181 static long long stat_major_blocks_freed = 0;
182 static long long stat_major_blocks_lazy_swept = 0;
183 static long long stat_major_objects_evacuated = 0;
185 #if SIZEOF_VOID_P != 8
186 static long long stat_major_blocks_freed_ideal = 0;
187 static long long stat_major_blocks_freed_less_ideal = 0;
188 static long long stat_major_blocks_freed_individual = 0;
189 static long long stat_major_blocks_alloced_less_ideal = 0;
192 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
193 static long long num_major_objects_marked = 0;
194 #define INC_NUM_MAJOR_OBJECTS_MARKED() (++num_major_objects_marked)
196 #define INC_NUM_MAJOR_OBJECTS_MARKED()
200 sweep_block (MSBlockInfo *block, gboolean during_major_collection);
203 ms_find_block_obj_size_index (size_t size)
206 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);
207 for (i = 0; i < num_block_obj_sizes; ++i)
208 if (block_obj_sizes [i] >= size)
210 g_error ("no object of size %d\n", size);
213 #define FREE_BLOCKS_FROM(lists,p,r) (lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
214 #define FREE_BLOCKS(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
216 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
217 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
218 fast_block_obj_size_indexes [((s)+7)>>3] : \
219 ms_find_block_obj_size_index ((s)))
222 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
226 start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
228 start = sgen_alloc_os_memory (nursery_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
234 update_heap_boundaries_for_block (MSBlockInfo *block)
236 sgen_update_heap_boundaries ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), (mword)MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE);
240 ms_get_empty_block (void)
244 void *block, *empty, *next;
249 * We try allocating MS_BLOCK_ALLOC_NUM blocks first. If that's
250 * unsuccessful, we halve the number of blocks and try again, until we're at
251 * 1. If that doesn't work, either, we assert.
253 int alloc_num = MS_BLOCK_ALLOC_NUM;
255 p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * alloc_num, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE,
256 alloc_num == 1 ? "major heap section" : NULL);
262 for (i = 0; i < alloc_num; ++i) {
265 * We do the free list update one after the
266 * other so that other threads can use the new
267 * blocks as quickly as possible.
270 empty = empty_blocks;
271 *(void**)block = empty;
272 } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block, empty) != empty);
276 SGEN_ATOMIC_ADD_P (num_empty_blocks, alloc_num);
278 stat_major_blocks_alloced += alloc_num;
279 #if SIZEOF_VOID_P != 8
280 if (alloc_num != MS_BLOCK_ALLOC_NUM)
281 stat_major_blocks_alloced_less_ideal += alloc_num;
286 empty = empty_blocks;
290 next = *(void**)block;
291 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
293 SGEN_ATOMIC_ADD_P (num_empty_blocks, -1);
295 *(void**)block = NULL;
297 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
303 ms_free_block (void *block)
307 sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
308 memset (block, 0, MS_BLOCK_SIZE);
311 empty = empty_blocks;
312 *(void**)block = empty;
313 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
315 SGEN_ATOMIC_ADD_P (num_empty_blocks, 1);
318 //#define MARKSWEEP_CONSISTENCY_CHECK
320 #ifdef MARKSWEEP_CONSISTENCY_CHECK
322 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
326 for (; block; block = block->next_free) {
327 g_assert (block->obj_size == size);
328 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
330 /* blocks in the free lists must have at least
333 g_assert (block->free_list);
335 /* the block must be in the allocated_blocks array */
336 g_assert (sgen_pointer_queue_find (&allocated_blocks, BLOCK_TAG (block)) != (size_t)-1);
341 check_empty_blocks (void)
345 for (p = empty_blocks; p; p = *(void**)p)
347 g_assert (i == num_empty_blocks);
351 consistency_check (void)
356 /* check all blocks */
357 FOREACH_BLOCK (block) {
358 int count = MS_BLOCK_FREE / block->obj_size;
362 /* check block header */
363 g_assert (((MSBlockHeader*)block->block)->info == block);
365 /* count number of free slots */
366 for (i = 0; i < count; ++i) {
367 void **obj = (void**) MS_BLOCK_OBJ (block, i);
368 if (!MS_OBJ_ALLOCED (obj, block))
372 /* check free list */
373 for (free = block->free_list; free; free = (void**)*free) {
374 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
377 g_assert (num_free == 0);
379 /* check all mark words are zero */
381 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
382 g_assert (block->mark_words [i] == 0);
386 /* check free blocks */
387 for (i = 0; i < num_block_obj_sizes; ++i) {
389 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
390 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
393 check_empty_blocks ();
398 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
400 int size = block_obj_sizes [size_index];
401 int count = MS_BLOCK_FREE / size;
403 MSBlockHeader *header;
404 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
408 if (!sgen_memgov_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
411 info = sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
413 SGEN_ASSERT (9, count >= 2, "block with %d objects, it must hold at least 2", count);
415 info->obj_size = size;
416 info->obj_size_index = size_index;
417 info->pinned = pinned;
418 info->has_references = has_references;
419 info->has_pinned = pinned;
421 * Blocks that are to-space are not evacuated from. During an major collection
422 * blocks are allocated for two reasons: evacuating objects from the nursery and
423 * evacuating them from major blocks marked for evacuation. In both cases we don't
424 * want further evacuation.
426 info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD);
428 info->block = ms_get_empty_block ();
430 header = (MSBlockHeader*) info->block;
432 #ifdef SGEN_HAVE_CONCURRENT_MARK
433 info->cardtable_mod_union = NULL;
436 update_heap_boundaries_for_block (info);
438 /* build free list */
439 obj_start = MS_BLOCK_FOR_BLOCK_INFO (info) + MS_BLOCK_SKIP;
440 info->free_list = (void**)obj_start;
441 /* we're skipping the last one - it must be nulled */
442 for (i = 0; i < count - 1; ++i) {
443 char *next_obj_start = obj_start + size;
444 *(void**)obj_start = next_obj_start;
445 obj_start = next_obj_start;
448 *(void**)obj_start = NULL;
450 info->next_free = free_blocks [size_index];
451 free_blocks [size_index] = info;
453 sgen_pointer_queue_add (&allocated_blocks, BLOCK_TAG (info));
455 ++num_major_sections;
460 obj_is_from_pinned_alloc (char *ptr)
464 FOREACH_BLOCK (block) {
465 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE)
466 return block->pinned;
472 unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_index)
477 block = free_blocks [size_index];
478 SGEN_ASSERT (9, block, "no free block to unlink from free_blocks %p size_index %d", free_blocks, size_index);
480 if (G_UNLIKELY (!block->swept)) {
481 stat_major_blocks_lazy_swept ++;
482 sweep_block (block, FALSE);
485 obj = block->free_list;
486 SGEN_ASSERT (9, obj, "block %p in free list had no available object to alloc from", block);
488 block->free_list = *(void**)obj;
489 if (!block->free_list) {
490 free_blocks [size_index] = block->next_free;
491 block->next_free = NULL;
498 alloc_obj (MonoVTable *vtable, size_t size, gboolean pinned, gboolean has_references)
500 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
501 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
504 if (!free_blocks [size_index]) {
505 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
509 obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
511 *(MonoVTable**)obj = vtable;
517 major_alloc_object (MonoVTable *vtable, size_t size, gboolean has_references)
519 return alloc_obj (vtable, size, FALSE, has_references);
523 * We're not freeing the block if it's empty. We leave that work for
524 * the next major collection.
526 * This is just called from the domain clearing code, which runs in a
527 * single thread and has the GC lock, so we don't need an extra lock.
530 free_object (char *obj, size_t size, gboolean pinned)
532 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
536 sweep_block (block, FALSE);
537 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);
538 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p is already free", obj);
539 MS_CALC_MARK_BIT (word, bit, obj);
540 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p has mark bit set");
541 if (!block->free_list) {
542 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
543 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
544 SGEN_ASSERT (9, !block->next_free, "block %p doesn't have a free-list of object but belongs to a free-list of blocks");
545 block->next_free = free_blocks [size_index];
546 free_blocks [size_index] = block;
548 memset (obj, 0, size);
549 *(void**)obj = block->free_list;
550 block->free_list = (void**)obj;
554 major_free_non_pinned_object (char *obj, size_t size)
556 free_object (obj, size, FALSE);
559 /* size is a multiple of SGEN_ALLOC_ALIGN */
561 major_alloc_small_pinned_obj (MonoVTable *vtable, size_t size, gboolean has_references)
565 res = alloc_obj (vtable, size, TRUE, has_references);
566 /*If we failed to alloc memory, we better try releasing memory
567 *as pinned alloc is requested by the runtime.
570 sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure", TRUE);
571 res = alloc_obj (vtable, size, TRUE, has_references);
577 free_pinned_object (char *obj, size_t size)
579 free_object (obj, size, TRUE);
583 * size is already rounded up and we hold the GC lock.
586 major_alloc_degraded (MonoVTable *vtable, size_t size)
589 size_t old_num_sections;
591 old_num_sections = num_major_sections;
593 obj = alloc_obj (vtable, size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
594 if (G_LIKELY (obj)) {
595 HEAVY_STAT (++stat_objects_alloced_degraded);
596 HEAVY_STAT (stat_bytes_alloced_degraded += size);
597 g_assert (num_major_sections >= old_num_sections);
598 sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
604 * obj is some object. If it's not in the major heap (i.e. if it's in
605 * the nursery or LOS), return FALSE. Otherwise return whether it's
606 * been marked or copied.
609 major_is_object_live (char *obj)
615 if (sgen_ptr_in_nursery (obj))
618 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
621 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
624 /* now we know it's in a major block */
625 block = MS_BLOCK_FOR_OBJ (obj);
626 SGEN_ASSERT (9, !block->pinned, "block %p is pinned, BTW why is this bad?");
627 MS_CALC_MARK_BIT (word, bit, obj);
628 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
632 major_ptr_is_in_non_pinned_space (char *ptr, char **start)
636 FOREACH_BLOCK (block) {
637 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) {
638 int count = MS_BLOCK_FREE / block->obj_size;
642 for (i = 0; i <= count; ++i) {
643 if (ptr >= MS_BLOCK_OBJ (block, i) && ptr < MS_BLOCK_OBJ (block, i + 1)) {
644 *start = MS_BLOCK_OBJ (block, i);
648 return !block->pinned;
655 major_iterate_objects (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data)
657 gboolean sweep = flags & ITERATE_OBJECTS_SWEEP;
658 gboolean non_pinned = flags & ITERATE_OBJECTS_NON_PINNED;
659 gboolean pinned = flags & ITERATE_OBJECTS_PINNED;
662 FOREACH_BLOCK (block) {
663 int count = MS_BLOCK_FREE / block->obj_size;
666 if (block->pinned && !pinned)
668 if (!block->pinned && !non_pinned)
670 if (sweep && lazy_sweep) {
671 sweep_block (block, FALSE);
672 SGEN_ASSERT (0, block->swept, "Block must be swept after sweeping");
675 for (i = 0; i < count; ++i) {
676 void **obj = (void**) MS_BLOCK_OBJ (block, i);
679 MS_CALC_MARK_BIT (word, bit, obj);
680 if (!MS_MARK_BIT (block, word, bit))
683 if (MS_OBJ_ALLOCED (obj, block))
684 callback ((char*)obj, block->obj_size, data);
690 major_is_valid_object (char *object)
694 FOREACH_BLOCK (block) {
698 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > object) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= object))
701 idx = MS_BLOCK_OBJ_INDEX (object, block);
702 obj = (char*)MS_BLOCK_OBJ (block, idx);
705 return MS_OBJ_ALLOCED (obj, block);
713 major_describe_pointer (char *ptr)
717 FOREACH_BLOCK (block) {
725 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > ptr) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= ptr))
728 SGEN_LOG (0, "major-ptr (block %p sz %d pin %d ref %d)\n",
729 MS_BLOCK_FOR_BLOCK_INFO (block), block->obj_size, block->pinned, block->has_references);
731 idx = MS_BLOCK_OBJ_INDEX (ptr, block);
732 obj = (char*)MS_BLOCK_OBJ (block, idx);
733 live = MS_OBJ_ALLOCED (obj, block);
734 vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
736 MS_CALC_MARK_BIT (w, b, obj);
737 marked = MS_MARK_BIT (block, w, b);
742 SGEN_LOG (0, "object");
744 SGEN_LOG (0, "dead-object");
747 SGEN_LOG (0, "interior-ptr offset %td", ptr - obj);
749 SGEN_LOG (0, "dead-interior-ptr offset %td", ptr - obj);
752 SGEN_LOG (0, " marked %d)\n", marked ? 1 : 0);
761 major_check_scan_starts (void)
766 major_dump_heap (FILE *heap_dump_file)
769 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
770 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
773 for (i = 0; i < num_block_obj_sizes; ++i)
774 slots_available [i] = slots_used [i] = 0;
776 FOREACH_BLOCK (block) {
777 int index = ms_find_block_obj_size_index (block->obj_size);
778 int count = MS_BLOCK_FREE / block->obj_size;
780 slots_available [index] += count;
781 for (i = 0; i < count; ++i) {
782 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
783 ++slots_used [index];
787 fprintf (heap_dump_file, "<occupancies>\n");
788 for (i = 0; i < num_block_obj_sizes; ++i) {
789 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
790 block_obj_sizes [i], slots_available [i], slots_used [i]);
792 fprintf (heap_dump_file, "</occupancies>\n");
794 FOREACH_BLOCK (block) {
795 int count = MS_BLOCK_FREE / block->obj_size;
799 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
801 for (i = 0; i <= count; ++i) {
802 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
807 sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), MS_BLOCK_FOR_BLOCK_INFO (block));
813 fprintf (heap_dump_file, "</section>\n");
817 #define LOAD_VTABLE SGEN_LOAD_VTABLE
819 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,desc,block,queue) do { \
821 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
822 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
823 MS_SET_MARK_BIT ((block), __word, __bit); \
824 if ((block)->has_references) \
825 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
826 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
827 INC_NUM_MAJOR_OBJECTS_MARKED (); \
830 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,desc,block,queue) do { \
832 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
833 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
834 if (!MS_MARK_BIT ((block), __word, __bit)) { \
835 MS_SET_MARK_BIT ((block), __word, __bit); \
836 if ((block)->has_references) \
837 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
838 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
839 INC_NUM_MAJOR_OBJECTS_MARKED (); \
844 pin_major_object (char *obj, SgenGrayQueue *queue)
848 #ifdef SGEN_HAVE_CONCURRENT_MARK
850 g_assert_not_reached ();
853 block = MS_BLOCK_FOR_OBJ (obj);
854 block->has_pinned = TRUE;
855 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
858 #include "sgen-major-copy-object.h"
860 #ifdef SGEN_HAVE_CONCURRENT_MARK
862 major_copy_or_mark_object_concurrent (void **ptr, void *obj, SgenGrayQueue *queue)
864 SGEN_ASSERT (9, sgen_concurrent_collection_in_progress (), "Why are we scanning concurrently when there's no concurrent collection on?");
865 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!");
867 g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
869 if (!sgen_ptr_in_nursery (obj)) {
872 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
874 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE) {
875 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
876 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
878 if (sgen_los_object_is_pinned (obj))
882 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
883 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
884 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
888 sgen_los_pin_object (obj);
889 if (SGEN_OBJECT_HAS_REFERENCES (obj))
890 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_obj_get_descriptor (obj));
891 INC_NUM_MAJOR_OBJECTS_MARKED ();
898 major_copy_or_mark_object (void **ptr, void *obj, SgenGrayQueue *queue)
902 HEAVY_STAT (++stat_copy_object_called_major);
904 SGEN_ASSERT (9, !sgen_concurrent_collection_in_progress (), "Why are we scanning non-concurrently when there's a concurrent collection on?");
906 SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
907 SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
909 if (sgen_ptr_in_nursery (obj)) {
911 char *forwarded, *old_obj;
913 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
914 SGEN_UPDATE_REFERENCE (ptr, forwarded);
917 if (SGEN_OBJECT_IS_PINNED (obj))
920 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
921 if (sgen_nursery_is_to_space (obj))
924 HEAVY_STAT (++stat_objects_copied_major);
928 obj = copy_object_no_checks (obj, queue);
929 if (G_UNLIKELY (old_obj == obj)) {
930 /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
931 if (!sgen_ptr_in_nursery (obj)) {
933 block = MS_BLOCK_FOR_OBJ (obj);
934 size_index = block->obj_size_index;
935 evacuate_block_obj_sizes [size_index] = FALSE;
936 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
940 SGEN_UPDATE_REFERENCE (ptr, obj);
943 * FIXME: See comment for copy_object_no_checks(). If
944 * we have that, we can let the allocation function
945 * give us the block info, too, and we won't have to
948 * FIXME (2): We should rework this to avoid all those nursery checks.
951 * For the split nursery allocator the object might
952 * still be in the nursery despite having being
953 * promoted, in which case we can't mark it.
955 if (!sgen_ptr_in_nursery (obj)) {
956 block = MS_BLOCK_FOR_OBJ (obj);
957 MS_CALC_MARK_BIT (word, bit, obj);
958 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
959 MS_SET_MARK_BIT (block, word, bit);
960 binary_protocol_mark (obj, (gpointer)LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
967 * If we have don't have a fixed heap we cannot know
968 * whether an object is in the LOS or in the small
969 * object major heap without checking its size. To do
970 * that, however, we need to know that we actually
971 * have a valid object, not a forwarding pointer, so
972 * we have to do this check first.
974 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
975 SGEN_UPDATE_REFERENCE (ptr, forwarded);
979 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
981 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE) {
985 block = MS_BLOCK_FOR_OBJ (obj);
986 size_index = block->obj_size_index;
987 evacuate = evacuate_block_obj_sizes [size_index];
989 if (evacuate && !block->has_pinned) {
990 g_assert (!SGEN_OBJECT_IS_PINNED (obj));
991 if (block->is_to_space)
993 HEAVY_STAT (++stat_major_objects_evacuated);
996 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
999 if (sgen_los_object_is_pinned (obj))
1001 binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1003 #ifdef ENABLE_DTRACE
1004 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1005 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1006 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1010 sgen_los_pin_object (obj);
1011 if (SGEN_OBJECT_HAS_REFERENCES (obj))
1012 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_obj_get_descriptor (obj));
1018 major_copy_or_mark_object_canonical (void **ptr, SgenGrayQueue *queue)
1020 major_copy_or_mark_object (ptr, *ptr, queue);
1023 #ifdef SGEN_HAVE_CONCURRENT_MARK
1025 major_copy_or_mark_object_concurrent_canonical (void **ptr, SgenGrayQueue *queue)
1027 major_copy_or_mark_object_concurrent (ptr, *ptr, queue);
1032 major_get_and_reset_num_major_objects_marked (void)
1034 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
1035 long long num = num_major_objects_marked;
1036 num_major_objects_marked = 0;
1043 #include "sgen-major-scan-object.h"
1045 #ifdef SGEN_HAVE_CONCURRENT_MARK
1046 #define SCAN_FOR_CONCURRENT_MARK
1047 #include "sgen-major-scan-object.h"
1048 #undef SCAN_FOR_CONCURRENT_MARK
1052 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1054 void **entry, **end;
1055 int last_index = -1;
1057 if (block->pin_queue_first_entry == block->pin_queue_last_entry)
1060 block->has_pinned = TRUE;
1062 entry = sgen_pinning_get_entry (block->pin_queue_first_entry);
1063 end = sgen_pinning_get_entry (block->pin_queue_last_entry);
1065 for (; entry < end; ++entry) {
1066 int index = MS_BLOCK_OBJ_INDEX (*entry, block);
1068 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);
1069 if (index == last_index)
1071 obj = MS_BLOCK_OBJ (block, index);
1072 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (obj, sgen_obj_get_descriptor (obj), block, queue);
1078 sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
1082 for (obj_index = 0; obj_index < count; ++obj_index) {
1084 void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
1086 MS_CALC_MARK_BIT (word, bit, obj);
1087 if (MS_MARK_BIT (block, word, bit)) {
1088 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
1090 /* an unmarked object */
1091 if (MS_OBJ_ALLOCED (obj, block)) {
1093 * FIXME: Merge consecutive
1094 * slots for lower reporting
1095 * overhead. Maybe memset
1096 * will also benefit?
1098 binary_protocol_empty (obj, obj_size);
1099 MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
1100 memset (obj, 0, obj_size);
1102 *(void**)obj = block->free_list;
1103 block->free_list = obj;
1111 * Traverse BLOCK, freeing and zeroing unused objects.
1114 sweep_block (MSBlockInfo *block, gboolean during_major_collection)
1117 void *reversed = NULL;
1119 if (!during_major_collection)
1120 g_assert (!sgen_concurrent_collection_in_progress ());
1125 count = MS_BLOCK_FREE / block->obj_size;
1127 block->free_list = NULL;
1129 /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
1130 // FIXME: Add more sizes
1131 switch (block->obj_size) {
1133 sweep_block_for_size (block, count, 16);
1136 sweep_block_for_size (block, count, block->obj_size);
1140 /* reset mark bits */
1141 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1143 /* Reverse free list so that it's in address order */
1145 while (block->free_list) {
1146 void *next = *(void**)block->free_list;
1147 *(void**)block->free_list = reversed;
1148 reversed = block->free_list;
1149 block->free_list = next;
1151 block->free_list = reversed;
1162 if (sizeof (mword) == sizeof (unsigned long))
1163 count += __builtin_popcountl (d);
1165 count += __builtin_popcount (d);
1181 /* statistics for evacuation */
1182 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1183 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1184 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1186 #ifdef SGEN_HAVE_CONCURRENT_MARK
1187 mword total_evacuate_heap = 0;
1188 mword total_evacuate_saved = 0;
1191 for (i = 0; i < num_block_obj_sizes; ++i)
1192 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1194 /* clear all the free lists */
1195 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1196 MSBlockInfo **free_blocks = free_block_lists [i];
1198 for (j = 0; j < num_block_obj_sizes; ++j)
1199 free_blocks [j] = NULL;
1202 /* traverse all blocks, free and zero unmarked objects */
1203 FOREACH_BLOCK (block) {
1205 gboolean have_live = FALSE;
1206 gboolean has_pinned;
1207 gboolean have_free = FALSE;
1211 obj_size_index = block->obj_size_index;
1213 has_pinned = block->has_pinned;
1214 block->has_pinned = block->pinned;
1216 block->is_to_space = FALSE;
1219 count = MS_BLOCK_FREE / block->obj_size;
1221 #ifdef SGEN_HAVE_CONCURRENT_MARK
1222 if (block->cardtable_mod_union) {
1223 sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
1224 block->cardtable_mod_union = NULL;
1228 /* Count marked objects in the block */
1229 for (i = 0; i < MS_NUM_MARK_WORDS; ++i) {
1230 nused += bitcount (block->mark_words [i]);
1239 sweep_block (block, TRUE);
1243 ++num_blocks [obj_size_index];
1244 slots_used [obj_size_index] += nused;
1245 slots_available [obj_size_index] += count;
1249 * If there are free slots in the block, add
1250 * the block to the corresponding free list.
1253 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1254 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1255 block->next_free = free_blocks [index];
1256 free_blocks [index] = block;
1259 update_heap_boundaries_for_block (block);
1262 * Blocks without live objects are removed from the
1263 * block list and freed.
1265 DELETE_BLOCK_IN_FOREACH ();
1267 binary_protocol_empty (MS_BLOCK_OBJ (block, 0), (char*)MS_BLOCK_OBJ (block, count) - (char*)MS_BLOCK_OBJ (block, 0));
1268 ms_free_block (block->block);
1269 sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1271 --num_major_sections;
1273 } END_FOREACH_BLOCK;
1274 sgen_pointer_queue_remove_nulls (&allocated_blocks);
1276 for (i = 0; i < num_block_obj_sizes; ++i) {
1277 float usage = (float)slots_used [i] / (float)slots_available [i];
1278 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1279 evacuate_block_obj_sizes [i] = TRUE;
1281 g_print ("slot size %d - %d of %d used\n",
1282 block_obj_sizes [i], slots_used [i], slots_available [i]);
1285 evacuate_block_obj_sizes [i] = FALSE;
1287 #ifdef SGEN_HAVE_CONCURRENT_MARK
1289 mword total_bytes = block_obj_sizes [i] * slots_available [i];
1290 total_evacuate_heap += total_bytes;
1291 if (evacuate_block_obj_sizes [i])
1292 total_evacuate_saved += total_bytes - block_obj_sizes [i] * slots_used [i];
1297 #ifdef SGEN_HAVE_CONCURRENT_MARK
1298 want_evacuation = (float)total_evacuate_saved / (float)total_evacuate_heap > (1 - concurrent_evacuation_threshold);
1310 static int count_pinned_ref;
1311 static int count_pinned_nonref;
1312 static int count_nonpinned_ref;
1313 static int count_nonpinned_nonref;
1316 count_nonpinned_callback (char *obj, size_t size, void *data)
1318 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1320 if (vtable->klass->has_references)
1321 ++count_nonpinned_ref;
1323 ++count_nonpinned_nonref;
1327 count_pinned_callback (char *obj, size_t size, void *data)
1329 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1331 if (vtable->klass->has_references)
1334 ++count_pinned_nonref;
1337 static G_GNUC_UNUSED void
1338 count_ref_nonref_objs (void)
1342 count_pinned_ref = 0;
1343 count_pinned_nonref = 0;
1344 count_nonpinned_ref = 0;
1345 count_nonpinned_nonref = 0;
1347 major_iterate_objects (ITERATE_OBJECTS_SWEEP_NON_PINNED, count_nonpinned_callback, NULL);
1348 major_iterate_objects (ITERATE_OBJECTS_SWEEP_PINNED, count_pinned_callback, NULL);
1350 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1352 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1353 count_pinned_ref, count_nonpinned_ref,
1354 count_pinned_nonref, count_nonpinned_nonref,
1355 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1359 ms_calculate_block_obj_sizes (double factor, int *arr)
1361 double target_size = sizeof (MonoObject);
1366 int target_count = (int)ceil (MS_BLOCK_FREE / target_size);
1367 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1369 if (size != last_size) {
1371 arr [num_sizes] = size;
1376 target_size *= factor;
1377 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1382 /* only valid during minor collections */
1383 static mword old_num_major_sections;
1386 major_start_nursery_collection (void)
1388 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1389 consistency_check ();
1392 old_num_major_sections = num_major_sections;
1396 major_finish_nursery_collection (void)
1398 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1399 consistency_check ();
1401 sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1405 major_start_major_collection (void)
1409 /* clear the free lists */
1410 for (i = 0; i < num_block_obj_sizes; ++i) {
1411 if (!evacuate_block_obj_sizes [i])
1414 free_block_lists [0][i] = NULL;
1415 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1418 // Sweep all unswept blocks
1422 MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
1424 FOREACH_BLOCK (block) {
1425 sweep_block (block, TRUE);
1426 } END_FOREACH_BLOCK;
1428 MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
1433 major_finish_major_collection (void)
1437 #if SIZEOF_VOID_P != 8
1439 compare_pointers (const void *va, const void *vb) {
1440 char *a = *(char**)va, *b = *(char**)vb;
1450 major_have_computer_minor_collection_allowance (void)
1452 size_t section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1454 g_assert (have_swept);
1456 #if SIZEOF_VOID_P != 8
1458 int i, num_empty_blocks_orig, num_blocks, arr_length;
1460 void **empty_block_arr;
1461 void **rebuild_next;
1465 * sgen_free_os_memory () asserts in mono_vfree () because windows doesn't like freeing the middle of
1466 * a VirtualAlloc ()-ed block.
1471 if (num_empty_blocks <= section_reserve)
1473 SGEN_ASSERT (0, num_empty_blocks > 0, "section reserve can't be negative");
1475 num_empty_blocks_orig = num_empty_blocks;
1476 empty_block_arr = (void**)sgen_alloc_internal_dynamic (sizeof (void*) * num_empty_blocks_orig,
1477 INTERNAL_MEM_MS_BLOCK_INFO_SORT, FALSE);
1478 if (!empty_block_arr)
1482 for (block = empty_blocks; block; block = *(void**)block)
1483 empty_block_arr [i++] = block;
1484 SGEN_ASSERT (0, i == num_empty_blocks, "empty block count wrong");
1486 sgen_qsort (empty_block_arr, num_empty_blocks, sizeof (void*), compare_pointers);
1489 * We iterate over the free blocks, trying to find MS_BLOCK_ALLOC_NUM
1490 * contiguous ones. If we do, we free them. If that's not enough to get to
1491 * section_reserve, we halve the number of contiguous blocks we're looking
1492 * for and have another go, until we're done with looking for pairs of
1493 * blocks, at which point we give up and go to the fallback.
1495 arr_length = num_empty_blocks_orig;
1496 num_blocks = MS_BLOCK_ALLOC_NUM;
1497 while (num_empty_blocks > section_reserve && num_blocks > 1) {
1502 for (i = 0; i < arr_length; ++i) {
1504 void *block = empty_block_arr [i];
1505 SGEN_ASSERT (0, block, "we're not shifting correctly");
1507 empty_block_arr [dest] = block;
1509 * This is not strictly necessary, but we're
1512 empty_block_arr [i] = NULL;
1521 SGEN_ASSERT (0, first >= 0 && d > first, "algorithm is wrong");
1523 if ((char*)block != ((char*)empty_block_arr [d-1]) + MS_BLOCK_SIZE) {
1528 if (d + 1 - first == num_blocks) {
1530 * We found num_blocks contiguous blocks. Free them
1531 * and null their array entries. As an optimization
1532 * we could, instead of nulling the entries, shift
1533 * the following entries over to the left, while
1537 sgen_free_os_memory (empty_block_arr [first], MS_BLOCK_SIZE * num_blocks, SGEN_ALLOC_HEAP);
1538 for (j = first; j <= d; ++j)
1539 empty_block_arr [j] = NULL;
1543 num_empty_blocks -= num_blocks;
1545 stat_major_blocks_freed += num_blocks;
1546 if (num_blocks == MS_BLOCK_ALLOC_NUM)
1547 stat_major_blocks_freed_ideal += num_blocks;
1549 stat_major_blocks_freed_less_ideal += num_blocks;
1554 SGEN_ASSERT (0, dest <= i && dest <= arr_length, "array length is off");
1556 SGEN_ASSERT (0, arr_length == num_empty_blocks, "array length is off");
1561 /* rebuild empty_blocks free list */
1562 rebuild_next = (void**)&empty_blocks;
1563 for (i = 0; i < arr_length; ++i) {
1564 void *block = empty_block_arr [i];
1565 SGEN_ASSERT (0, block, "we're missing blocks");
1566 *rebuild_next = block;
1567 rebuild_next = (void**)block;
1569 *rebuild_next = NULL;
1572 sgen_free_internal_dynamic (empty_block_arr, sizeof (void*) * num_empty_blocks_orig, INTERNAL_MEM_MS_BLOCK_INFO_SORT);
1575 SGEN_ASSERT (0, num_empty_blocks >= 0, "we freed more blocks than we had in the first place?");
1579 * This is our threshold. If there's not more empty than used blocks, we won't
1580 * release uncontiguous blocks, in fear of fragmenting the address space.
1582 if (num_empty_blocks <= num_major_sections)
1586 while (num_empty_blocks > section_reserve) {
1587 void *next = *(void**)empty_blocks;
1588 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
1589 empty_blocks = next;
1591 * Needs not be atomic because this is running
1596 ++stat_major_blocks_freed;
1597 #if SIZEOF_VOID_P != 8
1598 ++stat_major_blocks_freed_individual;
1604 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1608 FOREACH_BLOCK (block) {
1609 sgen_find_optimized_pin_queue_area (MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SKIP, MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE,
1610 &block->pin_queue_first_entry, &block->pin_queue_last_entry);
1611 } END_FOREACH_BLOCK;
1615 major_pin_objects (SgenGrayQueue *queue)
1619 FOREACH_BLOCK (block) {
1620 mark_pinned_objects_in_block (block, queue);
1621 } END_FOREACH_BLOCK;
1625 major_init_to_space (void)
1630 major_report_pinned_memory_usage (void)
1632 g_assert_not_reached ();
1636 major_get_used_size (void)
1641 FOREACH_BLOCK (block) {
1642 int count = MS_BLOCK_FREE / block->obj_size;
1644 size += count * block->obj_size;
1645 for (iter = block->free_list; iter; iter = (void**)*iter)
1646 size -= block->obj_size;
1647 } END_FOREACH_BLOCK;
1653 get_num_major_sections (void)
1655 return num_major_sections;
1659 major_handle_gc_param (const char *opt)
1661 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1662 const char *arg = strchr (opt, '=') + 1;
1663 int percentage = atoi (arg);
1664 if (percentage < 0 || percentage > 100) {
1665 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1668 evacuation_threshold = (float)percentage / 100.0f;
1670 } else if (!strcmp (opt, "lazy-sweep")) {
1673 } else if (!strcmp (opt, "no-lazy-sweep")) {
1682 major_print_gc_param_usage (void)
1686 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1687 " (no-)lazy-sweep\n"
1692 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1695 gboolean has_references;
1697 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1699 callback ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE);
1700 } END_FOREACH_BLOCK;
1703 #ifdef HEAVY_STATISTICS
1704 extern long long marked_cards;
1705 extern long long scanned_cards;
1706 extern long long scanned_objects;
1707 extern long long remarked_cards;
1710 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1712 * MS blocks are 16K aligned.
1713 * Cardtables are 4K aligned, at least.
1714 * This means that the cardtable of a given block is 32 bytes aligned.
1717 initial_skip_card (guint8 *card_data)
1719 mword *cards = (mword*)card_data;
1722 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1728 if (i == CARD_WORDS_PER_BLOCK)
1729 return card_data + CARDS_PER_BLOCK;
1731 #if defined(__i386__) && defined(__GNUC__)
1732 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1733 #elif defined(__x86_64__) && defined(__GNUC__)
1734 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1735 #elif defined(__s390x__) && defined(__GNUC__)
1736 return card_data + i * 8 + (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
1738 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1740 return &card_data [i];
1747 static G_GNUC_UNUSED guint8*
1748 skip_card (guint8 *card_data, guint8 *card_data_end)
1750 while (card_data < card_data_end && !*card_data)
1755 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1756 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1757 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1760 major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
1763 gboolean has_references;
1764 ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
1766 #ifdef SGEN_HAVE_CONCURRENT_MARK
1767 if (!concurrent_mark)
1768 g_assert (!mod_union);
1770 g_assert (!mod_union);
1773 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1777 if (!has_references)
1780 block_obj_size = block->obj_size;
1781 block_start = MS_BLOCK_FOR_BLOCK_INFO (block);
1783 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1785 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1786 guint8 cards_data [CARDS_PER_BLOCK];
1788 char *obj, *end, *base;
1791 #ifdef SGEN_HAVE_CONCURRENT_MARK
1792 cards = block->cardtable_mod_union;
1794 * This happens when the nursery
1795 * collection that precedes finishing
1796 * the concurrent collection allocates
1803 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1804 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1805 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1808 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1813 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1814 end = block_start + MS_BLOCK_SIZE;
1815 base = sgen_card_table_align_pointer (obj);
1821 sweep_block (block, FALSE);
1823 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
1827 /* FIXME: do this more efficiently */
1829 MS_CALC_MARK_BIT (w, b, obj);
1830 if (!MS_MARK_BIT (block, w, b))
1834 card_offset = (obj - base) >> CARD_BITS;
1835 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, mod_union, queue);
1838 obj += block_obj_size;
1841 guint8 *card_data, *card_base;
1842 guint8 *card_data_end;
1845 * This is safe in face of card aliasing for the following reason:
1847 * Major blocks are 16k aligned, or 32 cards aligned.
1848 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1849 * sizes, they won't overflow the cardtable overlap modulus.
1852 #ifdef SGEN_HAVE_CONCURRENT_MARK
1853 card_data = card_base = block->cardtable_mod_union;
1855 * This happens when the nursery
1856 * collection that precedes finishing
1857 * the concurrent collection allocates
1863 g_assert_not_reached ();
1867 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1869 card_data_end = card_data + CARDS_PER_BLOCK;
1871 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)) {
1873 size_t idx = card_data - card_base;
1874 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1875 char *end = start + CARD_SIZE_IN_BYTES;
1876 char *first_obj, *obj;
1878 HEAVY_STAT (++scanned_cards);
1884 sweep_block (block, FALSE);
1886 HEAVY_STAT (++marked_cards);
1888 sgen_card_table_prepare_card_for_scanning (card_data);
1893 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1895 obj = first_obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1897 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
1901 /* FIXME: do this more efficiently */
1903 MS_CALC_MARK_BIT (w, b, obj);
1904 if (!MS_MARK_BIT (block, w, b))
1908 HEAVY_STAT (++scanned_objects);
1909 scan_func (obj, sgen_obj_get_descriptor (obj), queue);
1911 obj += block_obj_size;
1913 HEAVY_STAT (if (*card_data) ++remarked_cards);
1914 binary_protocol_card_scan (first_obj, obj - first_obj);
1917 } END_FOREACH_BLOCK;
1921 major_count_cards (long long *num_total_cards, long long *num_marked_cards)
1924 gboolean has_references;
1925 long long total_cards = 0;
1926 long long marked_cards = 0;
1928 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1929 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) MS_BLOCK_FOR_BLOCK_INFO (block));
1932 if (!has_references)
1935 total_cards += CARDS_PER_BLOCK;
1936 for (i = 0; i < CARDS_PER_BLOCK; ++i) {
1940 } END_FOREACH_BLOCK;
1942 *num_total_cards = total_cards;
1943 *num_marked_cards = marked_cards;
1946 #ifdef SGEN_HAVE_CONCURRENT_MARK
1948 update_cardtable_mod_union (void)
1952 FOREACH_BLOCK (block) {
1955 block->cardtable_mod_union = sgen_card_table_update_mod_union (block->cardtable_mod_union,
1956 MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE, &num_cards);
1958 SGEN_ASSERT (0, num_cards == CARDS_PER_BLOCK, "Number of cards calculation is wrong");
1959 } END_FOREACH_BLOCK;
1963 major_get_cardtable_mod_union_for_object (char *obj)
1965 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1966 return &block->cardtable_mod_union [(obj - (char*)sgen_card_table_align_pointer (MS_BLOCK_FOR_BLOCK_INFO (block))) >> CARD_BITS];
1971 alloc_free_block_lists (MSBlockInfo ***lists)
1974 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1975 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
1978 #undef pthread_create
1981 post_param_init (SgenMajorCollector *collector)
1983 collector->sweeps_lazily = lazy_sweep;
1986 #ifdef SGEN_HAVE_CONCURRENT_MARK
1988 sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurrent)
1989 #else // SGEN_HAVE_CONCURRENT_MARK
1990 #error unknown configuration
1991 #endif // SGEN_HAVE_CONCURRENT_MARK
1995 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1997 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
1998 block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
1999 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2001 evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2002 for (i = 0; i < num_block_obj_sizes; ++i)
2003 evacuate_block_obj_sizes [i] = FALSE;
2008 g_print ("block object sizes:\n");
2009 for (i = 0; i < num_block_obj_sizes; ++i)
2010 g_print ("%d\n", block_obj_sizes [i]);
2014 alloc_free_block_lists (free_block_lists);
2016 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2017 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2018 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2019 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2021 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2022 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2023 mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_lazy_swept);
2024 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2025 #if SIZEOF_VOID_P != 8
2026 mono_counters_register ("# major blocks freed ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_ideal);
2027 mono_counters_register ("# major blocks freed less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_less_ideal);
2028 mono_counters_register ("# major blocks freed individually", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_individual);
2029 mono_counters_register ("# major blocks allocated less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced_less_ideal);
2032 collector->section_size = MAJOR_SECTION_SIZE;
2034 #ifdef SGEN_HAVE_CONCURRENT_MARK
2035 concurrent_mark = is_concurrent;
2036 if (is_concurrent) {
2037 collector->is_concurrent = TRUE;
2038 collector->want_synchronous_collection = &want_evacuation;
2042 collector->is_concurrent = FALSE;
2043 collector->want_synchronous_collection = NULL;
2045 collector->get_and_reset_num_major_objects_marked = major_get_and_reset_num_major_objects_marked;
2046 collector->supports_cardtable = TRUE;
2048 collector->have_swept = &have_swept;
2050 collector->alloc_heap = major_alloc_heap;
2051 collector->is_object_live = major_is_object_live;
2052 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2053 collector->alloc_degraded = major_alloc_degraded;
2055 collector->alloc_object = major_alloc_object;
2056 collector->free_pinned_object = free_pinned_object;
2057 collector->iterate_objects = major_iterate_objects;
2058 collector->free_non_pinned_object = major_free_non_pinned_object;
2059 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2060 collector->pin_objects = major_pin_objects;
2061 collector->pin_major_object = pin_major_object;
2062 collector->scan_card_table = major_scan_card_table;
2063 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2064 #ifdef SGEN_HAVE_CONCURRENT_MARK
2065 if (is_concurrent) {
2066 collector->update_cardtable_mod_union = update_cardtable_mod_union;
2067 collector->get_cardtable_mod_union_for_object = major_get_cardtable_mod_union_for_object;
2070 collector->init_to_space = major_init_to_space;
2071 collector->sweep = major_sweep;
2072 collector->check_scan_starts = major_check_scan_starts;
2073 collector->dump_heap = major_dump_heap;
2074 collector->get_used_size = major_get_used_size;
2075 collector->start_nursery_collection = major_start_nursery_collection;
2076 collector->finish_nursery_collection = major_finish_nursery_collection;
2077 collector->start_major_collection = major_start_major_collection;
2078 collector->finish_major_collection = major_finish_major_collection;
2079 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2080 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2081 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2082 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2083 collector->get_num_major_sections = get_num_major_sections;
2084 collector->handle_gc_param = major_handle_gc_param;
2085 collector->print_gc_param_usage = major_print_gc_param_usage;
2086 collector->post_param_init = post_param_init;
2087 collector->is_valid_object = major_is_valid_object;
2088 collector->describe_pointer = major_describe_pointer;
2089 collector->count_cards = major_count_cards;
2091 collector->major_ops.copy_or_mark_object = major_copy_or_mark_object_canonical;
2092 collector->major_ops.scan_object = major_scan_object;
2093 #ifdef SGEN_HAVE_CONCURRENT_MARK
2094 if (is_concurrent) {
2095 collector->major_concurrent_ops.copy_or_mark_object = major_copy_or_mark_object_concurrent_canonical;
2096 collector->major_concurrent_ops.scan_object = major_scan_object_concurrent;
2097 collector->major_concurrent_ops.scan_vtype = major_scan_vtype_concurrent;
2101 /*cardtable requires major pages to be 8 cards aligned*/
2102 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
2105 #ifdef SGEN_HAVE_CONCURRENT_MARK
2107 sgen_marksweep_init (SgenMajorCollector *collector)
2109 sgen_marksweep_init_internal (collector, FALSE);
2113 sgen_marksweep_conc_init (SgenMajorCollector *collector)
2115 sgen_marksweep_init_internal (collector, TRUE);