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"
46 #define SGEN_HAVE_CONCURRENT_MARK
48 #define MS_BLOCK_SIZE (16*1024)
49 #define MS_BLOCK_SIZE_SHIFT 14
50 #define MAJOR_SECTION_SIZE MS_BLOCK_SIZE
51 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
54 * Don't allocate single blocks, but alloc a contingent of this many
55 * blocks in one swoop. This must be a power of two.
57 #define MS_BLOCK_ALLOC_NUM 32
60 * Number of bytes before the first object in a block. At the start
61 * of a block is the MSBlockHeader, then opional padding, then come
62 * the objects, so this must be >= sizeof (MSBlockHeader).
64 #define MS_BLOCK_SKIP 16
66 #define MS_BLOCK_FREE (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
68 #define MS_NUM_MARK_WORDS ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
70 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
71 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
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;
85 MSBlockInfo *next_free;
86 size_t pin_queue_first_entry;
87 size_t pin_queue_last_entry;
88 #ifdef SGEN_HAVE_CONCURRENT_MARK
89 guint8 *cardtable_mod_union;
91 mword mark_words [MS_NUM_MARK_WORDS];
94 #define MS_BLOCK_FOR_BLOCK_INFO(b) ((b)->block)
96 #define MS_BLOCK_OBJ(b,i) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (b)->obj_size * (i))
97 #define MS_BLOCK_OBJ_FOR_SIZE(b,i,obj_size) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (obj_size) * (i))
98 #define MS_BLOCK_DATA_FOR_OBJ(o) ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
104 #define MS_BLOCK_FOR_OBJ(o) (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
106 /* object index will always be small */
107 #define MS_BLOCK_OBJ_INDEX(o,b) ((int)(((char*)(o) - (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP)) / (b)->obj_size))
109 //casting to int is fine since blocks are 32k
110 #define MS_CALC_MARK_BIT(w,b,o) do { \
111 int i = ((int)((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o)))) >> SGEN_ALLOC_ALIGN_BITS; \
112 if (sizeof (mword) == 4) { \
121 #define MS_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] & (ONE_P << (b)))
122 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (ONE_P << (b)))
123 #define MS_PAR_SET_MARK_BIT(was_marked,bl,w,b) do { \
124 mword __old = (bl)->mark_words [(w)]; \
125 mword __bitmask = ONE_P << (b); \
126 if (__old & __bitmask) { \
130 if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)], \
131 (gpointer)(__old | __bitmask), \
132 (gpointer)__old) == \
134 was_marked = FALSE; \
139 #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))
141 #define MS_BLOCK_OBJ_SIZE_FACTOR (sqrt (2.0))
144 * This way we can lookup block object size indexes for sizes up to
145 * 256 bytes with a single load.
147 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES 32
149 static int *block_obj_sizes;
150 static int num_block_obj_sizes;
151 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
153 #define MS_BLOCK_FLAG_PINNED 1
154 #define MS_BLOCK_FLAG_REFS 2
156 #define MS_BLOCK_TYPE_MAX 4
158 static gboolean *evacuate_block_obj_sizes;
159 static float evacuation_threshold = 0.666f;
160 #ifdef SGEN_HAVE_CONCURRENT_MARK
161 static float concurrent_evacuation_threshold = 0.666f;
162 static gboolean want_evacuation = FALSE;
165 static gboolean lazy_sweep = TRUE;
166 static gboolean have_swept;
168 #ifdef SGEN_HAVE_CONCURRENT_MARK
169 static gboolean concurrent_mark;
172 #define BLOCK_IS_TAGGED_HAS_REFERENCES(bl) SGEN_POINTER_IS_TAGGED_1 ((bl))
173 #define BLOCK_TAG_HAS_REFERENCES(bl) SGEN_POINTER_TAG_1 ((bl))
174 #define BLOCK_UNTAG_HAS_REFERENCES(bl) SGEN_POINTER_UNTAG_1 ((bl))
176 #define BLOCK_TAG(bl) ((bl)->has_references ? BLOCK_TAG_HAS_REFERENCES ((bl)) : (bl))
178 /* all allocated blocks in the system */
179 static SgenPointerQueue allocated_blocks;
181 /* non-allocated block free-list */
182 static void *empty_blocks = NULL;
183 static size_t num_empty_blocks = 0;
185 #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]);
186 #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));
187 #define END_FOREACH_BLOCK } }
188 #define DELETE_BLOCK_IN_FOREACH() (allocated_blocks.data [__index] = NULL)
190 static size_t num_major_sections = 0;
191 /* one free block list for each block object size */
192 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
194 static long long stat_major_blocks_alloced = 0;
195 static long long stat_major_blocks_freed = 0;
196 static long long stat_major_blocks_lazy_swept = 0;
197 static long long stat_major_objects_evacuated = 0;
199 #if SIZEOF_VOID_P != 8
200 static long long stat_major_blocks_freed_ideal = 0;
201 static long long stat_major_blocks_freed_less_ideal = 0;
202 static long long stat_major_blocks_freed_individual = 0;
203 static long long stat_major_blocks_alloced_less_ideal = 0;
206 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
207 static long long num_major_objects_marked = 0;
208 #define INC_NUM_MAJOR_OBJECTS_MARKED() (++num_major_objects_marked)
210 #define INC_NUM_MAJOR_OBJECTS_MARKED()
214 sweep_block (MSBlockInfo *block, gboolean during_major_collection);
217 ms_find_block_obj_size_index (size_t size)
220 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);
221 for (i = 0; i < num_block_obj_sizes; ++i)
222 if (block_obj_sizes [i] >= size)
224 g_error ("no object of size %d\n", size);
227 #define FREE_BLOCKS_FROM(lists,p,r) (lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
228 #define FREE_BLOCKS(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
230 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
231 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
232 fast_block_obj_size_indexes [((s)+7)>>3] : \
233 ms_find_block_obj_size_index ((s)))
236 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
240 start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
242 start = sgen_alloc_os_memory (nursery_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
248 update_heap_boundaries_for_block (MSBlockInfo *block)
250 sgen_update_heap_boundaries ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), (mword)MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE);
254 ms_get_empty_block (void)
258 void *block, *empty, *next;
263 * We try allocating MS_BLOCK_ALLOC_NUM blocks first. If that's
264 * unsuccessful, we halve the number of blocks and try again, until we're at
265 * 1. If that doesn't work, either, we assert.
267 int alloc_num = MS_BLOCK_ALLOC_NUM;
269 p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * alloc_num, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE,
270 alloc_num == 1 ? "major heap section" : NULL);
276 for (i = 0; i < alloc_num; ++i) {
279 * We do the free list update one after the
280 * other so that other threads can use the new
281 * blocks as quickly as possible.
284 empty = empty_blocks;
285 *(void**)block = empty;
286 } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block, empty) != empty);
290 SGEN_ATOMIC_ADD_P (num_empty_blocks, alloc_num);
292 stat_major_blocks_alloced += alloc_num;
293 #if SIZEOF_VOID_P != 8
294 if (alloc_num != MS_BLOCK_ALLOC_NUM)
295 stat_major_blocks_alloced_less_ideal += alloc_num;
300 empty = empty_blocks;
304 next = *(void**)block;
305 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
307 SGEN_ATOMIC_ADD_P (num_empty_blocks, -1);
309 *(void**)block = NULL;
311 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
317 ms_free_block (void *block)
321 sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
322 memset (block, 0, MS_BLOCK_SIZE);
325 empty = empty_blocks;
326 *(void**)block = empty;
327 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
329 SGEN_ATOMIC_ADD_P (num_empty_blocks, 1);
332 //#define MARKSWEEP_CONSISTENCY_CHECK
334 #ifdef MARKSWEEP_CONSISTENCY_CHECK
336 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
340 for (; block; block = block->next_free) {
341 g_assert (block->obj_size == size);
342 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
344 /* blocks in the free lists must have at least
347 g_assert (block->free_list);
349 /* the block must be in the allocated_blocks array */
350 g_assert (sgen_pointer_queue_find (&allocated_blocks, BLOCK_TAG (block)) != (size_t)-1);
355 check_empty_blocks (void)
359 for (p = empty_blocks; p; p = *(void**)p)
361 g_assert (i == num_empty_blocks);
365 consistency_check (void)
370 /* check all blocks */
371 FOREACH_BLOCK (block) {
372 int count = MS_BLOCK_FREE / block->obj_size;
376 /* check block header */
377 g_assert (((MSBlockHeader*)block->block)->info == block);
379 /* count number of free slots */
380 for (i = 0; i < count; ++i) {
381 void **obj = (void**) MS_BLOCK_OBJ (block, i);
382 if (!MS_OBJ_ALLOCED (obj, block))
386 /* check free list */
387 for (free = block->free_list; free; free = (void**)*free) {
388 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
391 g_assert (num_free == 0);
393 /* check all mark words are zero */
395 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
396 g_assert (block->mark_words [i] == 0);
400 /* check free blocks */
401 for (i = 0; i < num_block_obj_sizes; ++i) {
403 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
404 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
407 check_empty_blocks ();
412 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
414 int size = block_obj_sizes [size_index];
415 int count = MS_BLOCK_FREE / size;
417 MSBlockHeader *header;
418 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
422 if (!sgen_memgov_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
425 info = sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
427 SGEN_ASSERT (9, count >= 2, "block with %d objects, it must hold at least 2", count);
429 info->obj_size = size;
430 info->obj_size_index = size_index;
431 info->pinned = pinned;
432 info->has_references = has_references;
433 info->has_pinned = pinned;
435 * Blocks that are to-space are not evacuated from. During an major collection
436 * blocks are allocated for two reasons: evacuating objects from the nursery and
437 * evacuating them from major blocks marked for evacuation. In both cases we don't
438 * want further evacuation.
440 info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD);
442 info->block = ms_get_empty_block ();
444 header = (MSBlockHeader*) info->block;
446 #ifdef SGEN_HAVE_CONCURRENT_MARK
447 info->cardtable_mod_union = NULL;
450 update_heap_boundaries_for_block (info);
452 /* build free list */
453 obj_start = MS_BLOCK_FOR_BLOCK_INFO (info) + MS_BLOCK_SKIP;
454 info->free_list = (void**)obj_start;
455 /* we're skipping the last one - it must be nulled */
456 for (i = 0; i < count - 1; ++i) {
457 char *next_obj_start = obj_start + size;
458 *(void**)obj_start = next_obj_start;
459 obj_start = next_obj_start;
462 *(void**)obj_start = NULL;
464 info->next_free = free_blocks [size_index];
465 free_blocks [size_index] = info;
467 sgen_pointer_queue_add (&allocated_blocks, BLOCK_TAG (info));
469 ++num_major_sections;
474 obj_is_from_pinned_alloc (char *ptr)
478 FOREACH_BLOCK (block) {
479 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE)
480 return block->pinned;
486 unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_index)
491 block = free_blocks [size_index];
492 SGEN_ASSERT (9, block, "no free block to unlink from free_blocks %p size_index %d", free_blocks, size_index);
494 if (G_UNLIKELY (!block->swept)) {
495 stat_major_blocks_lazy_swept ++;
496 sweep_block (block, FALSE);
499 obj = block->free_list;
500 SGEN_ASSERT (9, obj, "block %p in free list had no available object to alloc from", block);
502 block->free_list = *(void**)obj;
503 if (!block->free_list) {
504 free_blocks [size_index] = block->next_free;
505 block->next_free = NULL;
512 alloc_obj (MonoVTable *vtable, size_t size, gboolean pinned, gboolean has_references)
514 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
515 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
518 if (!free_blocks [size_index]) {
519 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
523 obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
525 *(MonoVTable**)obj = vtable;
531 major_alloc_object (MonoVTable *vtable, size_t size, gboolean has_references)
533 return alloc_obj (vtable, size, FALSE, has_references);
537 * We're not freeing the block if it's empty. We leave that work for
538 * the next major collection.
540 * This is just called from the domain clearing code, which runs in a
541 * single thread and has the GC lock, so we don't need an extra lock.
544 free_object (char *obj, size_t size, gboolean pinned)
546 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
550 sweep_block (block, FALSE);
551 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);
552 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p is already free", obj);
553 MS_CALC_MARK_BIT (word, bit, obj);
554 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p has mark bit set");
555 if (!block->free_list) {
556 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
557 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
558 SGEN_ASSERT (9, !block->next_free, "block %p doesn't have a free-list of object but belongs to a free-list of blocks");
559 block->next_free = free_blocks [size_index];
560 free_blocks [size_index] = block;
562 memset (obj, 0, size);
563 *(void**)obj = block->free_list;
564 block->free_list = (void**)obj;
568 major_free_non_pinned_object (char *obj, size_t size)
570 free_object (obj, size, FALSE);
573 /* size is a multiple of SGEN_ALLOC_ALIGN */
575 major_alloc_small_pinned_obj (MonoVTable *vtable, size_t size, gboolean has_references)
579 res = alloc_obj (vtable, size, TRUE, has_references);
580 /*If we failed to alloc memory, we better try releasing memory
581 *as pinned alloc is requested by the runtime.
584 sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure", TRUE);
585 res = alloc_obj (vtable, size, TRUE, has_references);
591 free_pinned_object (char *obj, size_t size)
593 free_object (obj, size, TRUE);
597 * size is already rounded up and we hold the GC lock.
600 major_alloc_degraded (MonoVTable *vtable, size_t size)
603 size_t old_num_sections;
605 old_num_sections = num_major_sections;
607 obj = alloc_obj (vtable, size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
608 if (G_LIKELY (obj)) {
609 HEAVY_STAT (++stat_objects_alloced_degraded);
610 HEAVY_STAT (stat_bytes_alloced_degraded += size);
611 g_assert (num_major_sections >= old_num_sections);
612 sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
617 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj) FALSE
620 * obj is some object. If it's not in the major heap (i.e. if it's in
621 * the nursery or LOS), return FALSE. Otherwise return whether it's
622 * been marked or copied.
625 major_is_object_live (char *obj)
631 if (sgen_ptr_in_nursery (obj))
634 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
637 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
640 /* now we know it's in a major block */
641 block = MS_BLOCK_FOR_OBJ (obj);
642 SGEN_ASSERT (9, !block->pinned, "block %p is pinned, BTW why is this bad?");
643 MS_CALC_MARK_BIT (word, bit, obj);
644 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
648 major_ptr_is_in_non_pinned_space (char *ptr, char **start)
652 FOREACH_BLOCK (block) {
653 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) {
654 int count = MS_BLOCK_FREE / block->obj_size;
658 for (i = 0; i <= count; ++i) {
659 if (ptr >= MS_BLOCK_OBJ (block, i) && ptr < MS_BLOCK_OBJ (block, i + 1)) {
660 *start = MS_BLOCK_OBJ (block, i);
664 return !block->pinned;
671 major_iterate_objects (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data)
673 gboolean sweep = flags & ITERATE_OBJECTS_SWEEP;
674 gboolean non_pinned = flags & ITERATE_OBJECTS_NON_PINNED;
675 gboolean pinned = flags & ITERATE_OBJECTS_PINNED;
678 FOREACH_BLOCK (block) {
679 int count = MS_BLOCK_FREE / block->obj_size;
682 if (block->pinned && !pinned)
684 if (!block->pinned && !non_pinned)
686 if (sweep && lazy_sweep) {
687 sweep_block (block, FALSE);
688 SGEN_ASSERT (0, block->swept, "Block must be swept after sweeping");
691 for (i = 0; i < count; ++i) {
692 void **obj = (void**) MS_BLOCK_OBJ (block, i);
695 MS_CALC_MARK_BIT (word, bit, obj);
696 if (!MS_MARK_BIT (block, word, bit))
699 if (MS_OBJ_ALLOCED (obj, block))
700 callback ((char*)obj, block->obj_size, data);
706 major_is_valid_object (char *object)
710 FOREACH_BLOCK (block) {
714 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > object) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= object))
717 idx = MS_BLOCK_OBJ_INDEX (object, block);
718 obj = (char*)MS_BLOCK_OBJ (block, idx);
721 return MS_OBJ_ALLOCED (obj, block);
729 major_describe_pointer (char *ptr)
733 FOREACH_BLOCK (block) {
741 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > ptr) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= ptr))
744 SGEN_LOG (0, "major-ptr (block %p sz %d pin %d ref %d)\n",
745 MS_BLOCK_FOR_BLOCK_INFO (block), block->obj_size, block->pinned, block->has_references);
747 idx = MS_BLOCK_OBJ_INDEX (ptr, block);
748 obj = (char*)MS_BLOCK_OBJ (block, idx);
749 live = MS_OBJ_ALLOCED (obj, block);
750 vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
752 MS_CALC_MARK_BIT (w, b, obj);
753 marked = MS_MARK_BIT (block, w, b);
758 SGEN_LOG (0, "object");
760 SGEN_LOG (0, "dead-object");
763 SGEN_LOG (0, "interior-ptr offset %td", ptr - obj);
765 SGEN_LOG (0, "dead-interior-ptr offset %td", ptr - obj);
768 SGEN_LOG (0, " marked %d)\n", marked ? 1 : 0);
777 major_check_scan_starts (void)
782 major_dump_heap (FILE *heap_dump_file)
785 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
786 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
789 for (i = 0; i < num_block_obj_sizes; ++i)
790 slots_available [i] = slots_used [i] = 0;
792 FOREACH_BLOCK (block) {
793 int index = ms_find_block_obj_size_index (block->obj_size);
794 int count = MS_BLOCK_FREE / block->obj_size;
796 slots_available [index] += count;
797 for (i = 0; i < count; ++i) {
798 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
799 ++slots_used [index];
803 fprintf (heap_dump_file, "<occupancies>\n");
804 for (i = 0; i < num_block_obj_sizes; ++i) {
805 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
806 block_obj_sizes [i], slots_available [i], slots_used [i]);
808 fprintf (heap_dump_file, "</occupancies>\n");
810 FOREACH_BLOCK (block) {
811 int count = MS_BLOCK_FREE / block->obj_size;
815 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
817 for (i = 0; i <= count; ++i) {
818 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
823 sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), MS_BLOCK_FOR_BLOCK_INFO (block));
829 fprintf (heap_dump_file, "</section>\n");
833 #define LOAD_VTABLE SGEN_LOAD_VTABLE
835 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,desc,block,queue) do { \
837 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
838 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
839 MS_SET_MARK_BIT ((block), __word, __bit); \
840 if ((block)->has_references) \
841 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
842 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
843 INC_NUM_MAJOR_OBJECTS_MARKED (); \
846 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,desc,block,queue) do { \
848 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
849 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
850 if (!MS_MARK_BIT ((block), __word, __bit)) { \
851 MS_SET_MARK_BIT ((block), __word, __bit); \
852 if ((block)->has_references) \
853 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
854 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
855 INC_NUM_MAJOR_OBJECTS_MARKED (); \
858 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,desc,block,queue) do { \
860 gboolean __was_marked; \
861 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
862 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
863 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
864 if (!__was_marked) { \
865 if ((block)->has_references) \
866 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
867 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
868 INC_NUM_MAJOR_OBJECTS_MARKED (); \
873 pin_major_object (char *obj, SgenGrayQueue *queue)
877 #ifdef SGEN_HAVE_CONCURRENT_MARK
879 g_assert_not_reached ();
882 block = MS_BLOCK_FOR_OBJ (obj);
883 block->has_pinned = TRUE;
884 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
887 #include "sgen-major-copy-object.h"
889 #ifdef SGEN_HAVE_CONCURRENT_MARK
891 major_copy_or_mark_object_concurrent (void **ptr, void *obj, SgenGrayQueue *queue)
893 g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
895 if (!sgen_ptr_in_nursery (obj)) {
898 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
900 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE) {
901 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
902 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
904 if (sgen_los_object_is_pinned (obj))
908 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
909 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
910 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
914 sgen_los_pin_object (obj);
915 if (SGEN_OBJECT_HAS_REFERENCES (obj))
916 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_obj_get_descriptor (obj));
917 INC_NUM_MAJOR_OBJECTS_MARKED ();
924 major_copy_or_mark_object (void **ptr, void *obj, SgenGrayQueue *queue)
928 HEAVY_STAT (++stat_copy_object_called_major);
930 SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
931 SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
933 if (sgen_ptr_in_nursery (obj)) {
935 char *forwarded, *old_obj;
937 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
941 if (SGEN_OBJECT_IS_PINNED (obj))
944 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
945 if (sgen_nursery_is_to_space (obj))
948 HEAVY_STAT (++stat_objects_copied_major);
952 obj = copy_object_no_checks (obj, queue);
953 if (G_UNLIKELY (old_obj == obj)) {
954 /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
955 if (!sgen_ptr_in_nursery (obj)) {
957 block = MS_BLOCK_FOR_OBJ (obj);
958 size_index = block->obj_size_index;
959 evacuate_block_obj_sizes [size_index] = FALSE;
960 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
967 * FIXME: See comment for copy_object_no_checks(). If
968 * we have that, we can let the allocation function
969 * give us the block info, too, and we won't have to
972 * FIXME (2): We should rework this to avoid all those nursery checks.
975 * For the split nursery allocator the object might
976 * still be in the nursery despite having being
977 * promoted, in which case we can't mark it.
979 if (!sgen_ptr_in_nursery (obj)) {
980 block = MS_BLOCK_FOR_OBJ (obj);
981 MS_CALC_MARK_BIT (word, bit, obj);
982 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
983 MS_SET_MARK_BIT (block, word, bit);
984 binary_protocol_mark (obj, (gpointer)LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
991 * If we have don't have a fixed heap we cannot know
992 * whether an object is in the LOS or in the small
993 * object major heap without checking its size. To do
994 * that, however, we need to know that we actually
995 * have a valid object, not a forwarding pointer, so
996 * we have to do this check first.
998 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1003 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1005 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE) {
1009 block = MS_BLOCK_FOR_OBJ (obj);
1010 size_index = block->obj_size_index;
1011 evacuate = evacuate_block_obj_sizes [size_index];
1013 if (evacuate && !block->has_pinned) {
1014 g_assert (!SGEN_OBJECT_IS_PINNED (obj));
1015 if (block->is_to_space)
1017 HEAVY_STAT (++stat_major_objects_evacuated);
1018 goto do_copy_object;
1020 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
1023 if (sgen_los_object_is_pinned (obj))
1025 binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1027 #ifdef ENABLE_DTRACE
1028 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1029 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1030 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1034 sgen_los_pin_object (obj);
1035 if (SGEN_OBJECT_HAS_REFERENCES (obj))
1036 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_obj_get_descriptor (obj));
1042 major_copy_or_mark_object_canonical (void **ptr, SgenGrayQueue *queue)
1044 major_copy_or_mark_object (ptr, *ptr, queue);
1047 #ifdef SGEN_HAVE_CONCURRENT_MARK
1049 major_copy_or_mark_object_concurrent_canonical (void **ptr, SgenGrayQueue *queue)
1051 major_copy_or_mark_object_concurrent (ptr, *ptr, queue);
1056 major_get_and_reset_num_major_objects_marked (void)
1058 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
1059 long long num = num_major_objects_marked;
1060 num_major_objects_marked = 0;
1067 #include "sgen-major-scan-object.h"
1069 #ifdef SGEN_HAVE_CONCURRENT_MARK
1070 #define SCAN_FOR_CONCURRENT_MARK
1071 #include "sgen-major-scan-object.h"
1072 #undef SCAN_FOR_CONCURRENT_MARK
1076 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1078 void **entry, **end;
1079 int last_index = -1;
1081 if (block->pin_queue_first_entry == block->pin_queue_last_entry)
1084 block->has_pinned = TRUE;
1086 entry = sgen_pinning_get_entry (block->pin_queue_first_entry);
1087 end = sgen_pinning_get_entry (block->pin_queue_last_entry);
1089 for (; entry < end; ++entry) {
1090 int index = MS_BLOCK_OBJ_INDEX (*entry, block);
1092 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);
1093 if (index == last_index)
1095 obj = MS_BLOCK_OBJ (block, index);
1096 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (obj, sgen_obj_get_descriptor (obj), block, queue);
1102 sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
1106 for (obj_index = 0; obj_index < count; ++obj_index) {
1108 void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
1110 MS_CALC_MARK_BIT (word, bit, obj);
1111 if (MS_MARK_BIT (block, word, bit)) {
1112 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
1114 /* an unmarked object */
1115 if (MS_OBJ_ALLOCED (obj, block)) {
1117 * FIXME: Merge consecutive
1118 * slots for lower reporting
1119 * overhead. Maybe memset
1120 * will also benefit?
1122 binary_protocol_empty (obj, obj_size);
1123 MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
1124 memset (obj, 0, obj_size);
1126 *(void**)obj = block->free_list;
1127 block->free_list = obj;
1135 * Traverse BLOCK, freeing and zeroing unused objects.
1138 sweep_block (MSBlockInfo *block, gboolean during_major_collection)
1141 void *reversed = NULL;
1143 if (!during_major_collection)
1144 g_assert (!sgen_concurrent_collection_in_progress ());
1149 count = MS_BLOCK_FREE / block->obj_size;
1151 block->free_list = NULL;
1153 /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
1154 // FIXME: Add more sizes
1155 switch (block->obj_size) {
1157 sweep_block_for_size (block, count, 16);
1160 sweep_block_for_size (block, count, block->obj_size);
1164 /* reset mark bits */
1165 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1167 /* Reverse free list so that it's in address order */
1169 while (block->free_list) {
1170 void *next = *(void**)block->free_list;
1171 *(void**)block->free_list = reversed;
1172 reversed = block->free_list;
1173 block->free_list = next;
1175 block->free_list = reversed;
1186 if (sizeof (mword) == sizeof (unsigned long))
1187 count += __builtin_popcountl (d);
1189 count += __builtin_popcount (d);
1205 /* statistics for evacuation */
1206 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1207 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1208 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1210 #ifdef SGEN_HAVE_CONCURRENT_MARK
1211 mword total_evacuate_heap = 0;
1212 mword total_evacuate_saved = 0;
1215 for (i = 0; i < num_block_obj_sizes; ++i)
1216 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1218 /* clear all the free lists */
1219 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1220 MSBlockInfo **free_blocks = free_block_lists [i];
1222 for (j = 0; j < num_block_obj_sizes; ++j)
1223 free_blocks [j] = NULL;
1226 /* traverse all blocks, free and zero unmarked objects */
1227 FOREACH_BLOCK (block) {
1229 gboolean have_live = FALSE;
1230 gboolean has_pinned;
1231 gboolean have_free = FALSE;
1235 obj_size_index = block->obj_size_index;
1237 has_pinned = block->has_pinned;
1238 block->has_pinned = block->pinned;
1240 block->is_to_space = FALSE;
1243 count = MS_BLOCK_FREE / block->obj_size;
1245 #ifdef SGEN_HAVE_CONCURRENT_MARK
1246 if (block->cardtable_mod_union) {
1247 sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
1248 block->cardtable_mod_union = NULL;
1252 /* Count marked objects in the block */
1253 for (i = 0; i < MS_NUM_MARK_WORDS; ++i) {
1254 nused += bitcount (block->mark_words [i]);
1263 sweep_block (block, TRUE);
1267 ++num_blocks [obj_size_index];
1268 slots_used [obj_size_index] += nused;
1269 slots_available [obj_size_index] += count;
1273 * If there are free slots in the block, add
1274 * the block to the corresponding free list.
1277 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1278 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1279 block->next_free = free_blocks [index];
1280 free_blocks [index] = block;
1283 update_heap_boundaries_for_block (block);
1286 * Blocks without live objects are removed from the
1287 * block list and freed.
1289 DELETE_BLOCK_IN_FOREACH ();
1291 binary_protocol_empty (MS_BLOCK_OBJ (block, 0), (char*)MS_BLOCK_OBJ (block, count) - (char*)MS_BLOCK_OBJ (block, 0));
1292 ms_free_block (block->block);
1293 sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1295 --num_major_sections;
1297 } END_FOREACH_BLOCK;
1298 sgen_pointer_queue_remove_nulls (&allocated_blocks);
1300 for (i = 0; i < num_block_obj_sizes; ++i) {
1301 float usage = (float)slots_used [i] / (float)slots_available [i];
1302 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1303 evacuate_block_obj_sizes [i] = TRUE;
1305 g_print ("slot size %d - %d of %d used\n",
1306 block_obj_sizes [i], slots_used [i], slots_available [i]);
1309 evacuate_block_obj_sizes [i] = FALSE;
1311 #ifdef SGEN_HAVE_CONCURRENT_MARK
1313 mword total_bytes = block_obj_sizes [i] * slots_available [i];
1314 total_evacuate_heap += total_bytes;
1315 if (evacuate_block_obj_sizes [i])
1316 total_evacuate_saved += total_bytes - block_obj_sizes [i] * slots_used [i];
1321 #ifdef SGEN_HAVE_CONCURRENT_MARK
1322 want_evacuation = (float)total_evacuate_saved / (float)total_evacuate_heap > (1 - concurrent_evacuation_threshold);
1334 static int count_pinned_ref;
1335 static int count_pinned_nonref;
1336 static int count_nonpinned_ref;
1337 static int count_nonpinned_nonref;
1340 count_nonpinned_callback (char *obj, size_t size, void *data)
1342 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1344 if (vtable->klass->has_references)
1345 ++count_nonpinned_ref;
1347 ++count_nonpinned_nonref;
1351 count_pinned_callback (char *obj, size_t size, void *data)
1353 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1355 if (vtable->klass->has_references)
1358 ++count_pinned_nonref;
1361 static G_GNUC_UNUSED void
1362 count_ref_nonref_objs (void)
1366 count_pinned_ref = 0;
1367 count_pinned_nonref = 0;
1368 count_nonpinned_ref = 0;
1369 count_nonpinned_nonref = 0;
1371 major_iterate_objects (ITERATE_OBJECTS_SWEEP_NON_PINNED, count_nonpinned_callback, NULL);
1372 major_iterate_objects (ITERATE_OBJECTS_SWEEP_PINNED, count_pinned_callback, NULL);
1374 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1376 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1377 count_pinned_ref, count_nonpinned_ref,
1378 count_pinned_nonref, count_nonpinned_nonref,
1379 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1383 ms_calculate_block_obj_sizes (double factor, int *arr)
1385 double target_size = sizeof (MonoObject);
1390 int target_count = (int)ceil (MS_BLOCK_FREE / target_size);
1391 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1393 if (size != last_size) {
1395 arr [num_sizes] = size;
1400 target_size *= factor;
1401 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1406 /* only valid during minor collections */
1407 static mword old_num_major_sections;
1410 major_start_nursery_collection (void)
1412 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1413 consistency_check ();
1416 old_num_major_sections = num_major_sections;
1420 major_finish_nursery_collection (void)
1422 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1423 consistency_check ();
1425 sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1429 major_start_major_collection (void)
1433 /* clear the free lists */
1434 for (i = 0; i < num_block_obj_sizes; ++i) {
1435 if (!evacuate_block_obj_sizes [i])
1438 free_block_lists [0][i] = NULL;
1439 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1442 // Sweep all unswept blocks
1446 MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
1448 FOREACH_BLOCK (block) {
1449 sweep_block (block, TRUE);
1450 } END_FOREACH_BLOCK;
1452 MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
1457 major_finish_major_collection (void)
1461 #if SIZEOF_VOID_P != 8
1463 compare_pointers (const void *va, const void *vb) {
1464 char *a = *(char**)va, *b = *(char**)vb;
1474 major_have_computer_minor_collection_allowance (void)
1476 size_t section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1478 g_assert (have_swept);
1480 #if SIZEOF_VOID_P != 8
1482 int i, num_empty_blocks_orig, num_blocks, arr_length;
1484 void **empty_block_arr;
1485 void **rebuild_next;
1489 * sgen_free_os_memory () asserts in mono_vfree () because windows doesn't like freeing the middle of
1490 * a VirtualAlloc ()-ed block.
1495 if (num_empty_blocks <= section_reserve)
1497 SGEN_ASSERT (0, num_empty_blocks > 0, "section reserve can't be negative");
1499 num_empty_blocks_orig = num_empty_blocks;
1500 empty_block_arr = (void**)sgen_alloc_internal_dynamic (sizeof (void*) * num_empty_blocks_orig,
1501 INTERNAL_MEM_MS_BLOCK_INFO_SORT, FALSE);
1502 if (!empty_block_arr)
1506 for (block = empty_blocks; block; block = *(void**)block)
1507 empty_block_arr [i++] = block;
1508 SGEN_ASSERT (0, i == num_empty_blocks, "empty block count wrong");
1510 sgen_qsort (empty_block_arr, num_empty_blocks, sizeof (void*), compare_pointers);
1513 * We iterate over the free blocks, trying to find MS_BLOCK_ALLOC_NUM
1514 * contiguous ones. If we do, we free them. If that's not enough to get to
1515 * section_reserve, we halve the number of contiguous blocks we're looking
1516 * for and have another go, until we're done with looking for pairs of
1517 * blocks, at which point we give up and go to the fallback.
1519 arr_length = num_empty_blocks_orig;
1520 num_blocks = MS_BLOCK_ALLOC_NUM;
1521 while (num_empty_blocks > section_reserve && num_blocks > 1) {
1526 for (i = 0; i < arr_length; ++i) {
1528 void *block = empty_block_arr [i];
1529 SGEN_ASSERT (0, block, "we're not shifting correctly");
1531 empty_block_arr [dest] = block;
1533 * This is not strictly necessary, but we're
1536 empty_block_arr [i] = NULL;
1545 SGEN_ASSERT (0, first >= 0 && d > first, "algorithm is wrong");
1547 if ((char*)block != ((char*)empty_block_arr [d-1]) + MS_BLOCK_SIZE) {
1552 if (d + 1 - first == num_blocks) {
1554 * We found num_blocks contiguous blocks. Free them
1555 * and null their array entries. As an optimization
1556 * we could, instead of nulling the entries, shift
1557 * the following entries over to the left, while
1561 sgen_free_os_memory (empty_block_arr [first], MS_BLOCK_SIZE * num_blocks, SGEN_ALLOC_HEAP);
1562 for (j = first; j <= d; ++j)
1563 empty_block_arr [j] = NULL;
1567 num_empty_blocks -= num_blocks;
1569 stat_major_blocks_freed += num_blocks;
1570 if (num_blocks == MS_BLOCK_ALLOC_NUM)
1571 stat_major_blocks_freed_ideal += num_blocks;
1573 stat_major_blocks_freed_less_ideal += num_blocks;
1578 SGEN_ASSERT (0, dest <= i && dest <= arr_length, "array length is off");
1580 SGEN_ASSERT (0, arr_length == num_empty_blocks, "array length is off");
1585 /* rebuild empty_blocks free list */
1586 rebuild_next = (void**)&empty_blocks;
1587 for (i = 0; i < arr_length; ++i) {
1588 void *block = empty_block_arr [i];
1589 SGEN_ASSERT (0, block, "we're missing blocks");
1590 *rebuild_next = block;
1591 rebuild_next = (void**)block;
1593 *rebuild_next = NULL;
1596 sgen_free_internal_dynamic (empty_block_arr, sizeof (void*) * num_empty_blocks_orig, INTERNAL_MEM_MS_BLOCK_INFO_SORT);
1599 SGEN_ASSERT (0, num_empty_blocks >= 0, "we freed more blocks than we had in the first place?");
1603 * This is our threshold. If there's not more empty than used blocks, we won't
1604 * release uncontiguous blocks, in fear of fragmenting the address space.
1606 if (num_empty_blocks <= num_major_sections)
1610 while (num_empty_blocks > section_reserve) {
1611 void *next = *(void**)empty_blocks;
1612 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
1613 empty_blocks = next;
1615 * Needs not be atomic because this is running
1620 ++stat_major_blocks_freed;
1621 #if SIZEOF_VOID_P != 8
1622 ++stat_major_blocks_freed_individual;
1628 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1632 FOREACH_BLOCK (block) {
1633 sgen_find_optimized_pin_queue_area (MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SKIP, MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE,
1634 &block->pin_queue_first_entry, &block->pin_queue_last_entry);
1635 } END_FOREACH_BLOCK;
1639 major_pin_objects (SgenGrayQueue *queue)
1643 FOREACH_BLOCK (block) {
1644 mark_pinned_objects_in_block (block, queue);
1645 } END_FOREACH_BLOCK;
1649 major_init_to_space (void)
1654 major_report_pinned_memory_usage (void)
1656 g_assert_not_reached ();
1660 major_get_used_size (void)
1665 FOREACH_BLOCK (block) {
1666 int count = MS_BLOCK_FREE / block->obj_size;
1668 size += count * block->obj_size;
1669 for (iter = block->free_list; iter; iter = (void**)*iter)
1670 size -= block->obj_size;
1671 } END_FOREACH_BLOCK;
1677 get_num_major_sections (void)
1679 return num_major_sections;
1683 major_handle_gc_param (const char *opt)
1685 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1686 const char *arg = strchr (opt, '=') + 1;
1687 int percentage = atoi (arg);
1688 if (percentage < 0 || percentage > 100) {
1689 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1692 evacuation_threshold = (float)percentage / 100.0f;
1694 } else if (!strcmp (opt, "lazy-sweep")) {
1697 } else if (!strcmp (opt, "no-lazy-sweep")) {
1706 major_print_gc_param_usage (void)
1710 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1711 " (no-)lazy-sweep\n"
1716 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1719 gboolean has_references;
1721 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1723 callback ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE);
1724 } END_FOREACH_BLOCK;
1727 #ifdef HEAVY_STATISTICS
1728 extern long long marked_cards;
1729 extern long long scanned_cards;
1730 extern long long scanned_objects;
1731 extern long long remarked_cards;
1734 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1736 * MS blocks are 16K aligned.
1737 * Cardtables are 4K aligned, at least.
1738 * This means that the cardtable of a given block is 32 bytes aligned.
1741 initial_skip_card (guint8 *card_data)
1743 mword *cards = (mword*)card_data;
1746 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1752 if (i == CARD_WORDS_PER_BLOCK)
1753 return card_data + CARDS_PER_BLOCK;
1755 #if defined(__i386__) && defined(__GNUC__)
1756 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1757 #elif defined(__x86_64__) && defined(__GNUC__)
1758 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1759 #elif defined(__s390x__) && defined(__GNUC__)
1760 return card_data + i * 8 + (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
1762 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1764 return &card_data [i];
1771 static G_GNUC_UNUSED guint8*
1772 skip_card (guint8 *card_data, guint8 *card_data_end)
1774 while (card_data < card_data_end && !*card_data)
1779 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1780 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1781 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1784 major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
1787 gboolean has_references;
1788 ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
1790 #ifdef SGEN_HAVE_CONCURRENT_MARK
1791 if (!concurrent_mark)
1792 g_assert (!mod_union);
1794 g_assert (!mod_union);
1797 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1801 if (!has_references)
1804 block_obj_size = block->obj_size;
1805 block_start = MS_BLOCK_FOR_BLOCK_INFO (block);
1807 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1809 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1810 guint8 cards_data [CARDS_PER_BLOCK];
1812 char *obj, *end, *base;
1815 #ifdef SGEN_HAVE_CONCURRENT_MARK
1816 cards = block->cardtable_mod_union;
1818 * This happens when the nursery
1819 * collection that precedes finishing
1820 * the concurrent collection allocates
1827 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1828 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1829 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1832 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1837 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1838 end = block_start + MS_BLOCK_SIZE;
1839 base = sgen_card_table_align_pointer (obj);
1845 sweep_block (block, FALSE);
1847 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
1851 /* FIXME: do this more efficiently */
1853 MS_CALC_MARK_BIT (w, b, obj);
1854 if (!MS_MARK_BIT (block, w, b))
1858 card_offset = (obj - base) >> CARD_BITS;
1859 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, mod_union, queue);
1862 obj += block_obj_size;
1865 guint8 *card_data, *card_base;
1866 guint8 *card_data_end;
1869 * This is safe in face of card aliasing for the following reason:
1871 * Major blocks are 16k aligned, or 32 cards aligned.
1872 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1873 * sizes, they won't overflow the cardtable overlap modulus.
1876 #ifdef SGEN_HAVE_CONCURRENT_MARK
1877 card_data = card_base = block->cardtable_mod_union;
1879 * This happens when the nursery
1880 * collection that precedes finishing
1881 * the concurrent collection allocates
1887 g_assert_not_reached ();
1891 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1893 card_data_end = card_data + CARDS_PER_BLOCK;
1895 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)) {
1897 size_t idx = card_data - card_base;
1898 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1899 char *end = start + CARD_SIZE_IN_BYTES;
1900 char *first_obj, *obj;
1902 HEAVY_STAT (++scanned_cards);
1908 sweep_block (block, FALSE);
1910 HEAVY_STAT (++marked_cards);
1912 sgen_card_table_prepare_card_for_scanning (card_data);
1917 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1919 obj = first_obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1921 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
1925 /* FIXME: do this more efficiently */
1927 MS_CALC_MARK_BIT (w, b, obj);
1928 if (!MS_MARK_BIT (block, w, b))
1932 HEAVY_STAT (++scanned_objects);
1933 scan_func (obj, sgen_obj_get_descriptor (obj), queue);
1935 obj += block_obj_size;
1937 HEAVY_STAT (if (*card_data) ++remarked_cards);
1938 binary_protocol_card_scan (first_obj, obj - first_obj);
1941 } END_FOREACH_BLOCK;
1945 major_count_cards (long long *num_total_cards, long long *num_marked_cards)
1948 gboolean has_references;
1949 long long total_cards = 0;
1950 long long marked_cards = 0;
1952 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1953 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) MS_BLOCK_FOR_BLOCK_INFO (block));
1956 if (!has_references)
1959 total_cards += CARDS_PER_BLOCK;
1960 for (i = 0; i < CARDS_PER_BLOCK; ++i) {
1964 } END_FOREACH_BLOCK;
1966 *num_total_cards = total_cards;
1967 *num_marked_cards = marked_cards;
1970 #ifdef SGEN_HAVE_CONCURRENT_MARK
1972 update_cardtable_mod_union (void)
1976 FOREACH_BLOCK (block) {
1979 block->cardtable_mod_union = sgen_card_table_update_mod_union (block->cardtable_mod_union,
1980 MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE, &num_cards);
1982 SGEN_ASSERT (0, num_cards == CARDS_PER_BLOCK, "Number of cards calculation is wrong");
1983 } END_FOREACH_BLOCK;
1987 major_get_cardtable_mod_union_for_object (char *obj)
1989 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1990 return &block->cardtable_mod_union [(obj - (char*)sgen_card_table_align_pointer (MS_BLOCK_FOR_BLOCK_INFO (block))) >> CARD_BITS];
1995 alloc_free_block_lists (MSBlockInfo ***lists)
1998 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1999 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2002 #undef pthread_create
2005 post_param_init (SgenMajorCollector *collector)
2007 collector->sweeps_lazily = lazy_sweep;
2010 #ifdef SGEN_HAVE_CONCURRENT_MARK
2012 sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurrent)
2013 #else // SGEN_HAVE_CONCURRENT_MARK
2014 #error unknown configuration
2015 #endif // SGEN_HAVE_CONCURRENT_MARK
2019 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
2021 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
2022 block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2023 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2025 evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2026 for (i = 0; i < num_block_obj_sizes; ++i)
2027 evacuate_block_obj_sizes [i] = FALSE;
2032 g_print ("block object sizes:\n");
2033 for (i = 0; i < num_block_obj_sizes; ++i)
2034 g_print ("%d\n", block_obj_sizes [i]);
2038 alloc_free_block_lists (free_block_lists);
2040 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2041 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2042 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2043 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2045 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2046 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2047 mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_lazy_swept);
2048 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2049 #if SIZEOF_VOID_P != 8
2050 mono_counters_register ("# major blocks freed ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_ideal);
2051 mono_counters_register ("# major blocks freed less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_less_ideal);
2052 mono_counters_register ("# major blocks freed individually", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_individual);
2053 mono_counters_register ("# major blocks allocated less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced_less_ideal);
2056 collector->section_size = MAJOR_SECTION_SIZE;
2058 #ifdef SGEN_HAVE_CONCURRENT_MARK
2059 concurrent_mark = is_concurrent;
2060 if (is_concurrent) {
2061 collector->is_concurrent = TRUE;
2062 collector->want_synchronous_collection = &want_evacuation;
2066 collector->is_concurrent = FALSE;
2067 collector->want_synchronous_collection = NULL;
2069 collector->get_and_reset_num_major_objects_marked = major_get_and_reset_num_major_objects_marked;
2070 collector->supports_cardtable = TRUE;
2072 collector->have_swept = &have_swept;
2074 collector->alloc_heap = major_alloc_heap;
2075 collector->is_object_live = major_is_object_live;
2076 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2077 collector->alloc_degraded = major_alloc_degraded;
2079 collector->alloc_object = major_alloc_object;
2080 collector->free_pinned_object = free_pinned_object;
2081 collector->iterate_objects = major_iterate_objects;
2082 collector->free_non_pinned_object = major_free_non_pinned_object;
2083 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2084 collector->pin_objects = major_pin_objects;
2085 collector->pin_major_object = pin_major_object;
2086 collector->scan_card_table = major_scan_card_table;
2087 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2088 #ifdef SGEN_HAVE_CONCURRENT_MARK
2089 if (is_concurrent) {
2090 collector->update_cardtable_mod_union = update_cardtable_mod_union;
2091 collector->get_cardtable_mod_union_for_object = major_get_cardtable_mod_union_for_object;
2094 collector->init_to_space = major_init_to_space;
2095 collector->sweep = major_sweep;
2096 collector->check_scan_starts = major_check_scan_starts;
2097 collector->dump_heap = major_dump_heap;
2098 collector->get_used_size = major_get_used_size;
2099 collector->start_nursery_collection = major_start_nursery_collection;
2100 collector->finish_nursery_collection = major_finish_nursery_collection;
2101 collector->start_major_collection = major_start_major_collection;
2102 collector->finish_major_collection = major_finish_major_collection;
2103 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2104 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2105 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2106 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2107 collector->get_num_major_sections = get_num_major_sections;
2108 collector->handle_gc_param = major_handle_gc_param;
2109 collector->print_gc_param_usage = major_print_gc_param_usage;
2110 collector->post_param_init = post_param_init;
2111 collector->is_valid_object = major_is_valid_object;
2112 collector->describe_pointer = major_describe_pointer;
2113 collector->count_cards = major_count_cards;
2115 collector->major_ops.copy_or_mark_object = major_copy_or_mark_object_canonical;
2116 collector->major_ops.scan_object = major_scan_object;
2117 #ifdef SGEN_HAVE_CONCURRENT_MARK
2118 if (is_concurrent) {
2119 collector->major_concurrent_ops.copy_or_mark_object = major_copy_or_mark_object_concurrent_canonical;
2120 collector->major_concurrent_ops.scan_object = major_scan_object_concurrent;
2121 collector->major_concurrent_ops.scan_vtype = major_scan_vtype_concurrent;
2125 /*cardtable requires major pages to be 8 cards aligned*/
2126 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
2129 #ifdef SGEN_HAVE_CONCURRENT_MARK
2131 sgen_marksweep_init (SgenMajorCollector *collector)
2133 sgen_marksweep_init_internal (collector, FALSE);
2137 sgen_marksweep_conc_init (SgenMajorCollector *collector)
2139 sgen_marksweep_init_internal (collector, TRUE);