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 #if defined(ARCH_MIN_MS_BLOCK_SIZE) && defined(ARCH_MIN_MS_BLOCK_SIZE_SHIFT)
49 #define MS_BLOCK_SIZE ARCH_MIN_MS_BLOCK_SIZE
50 #define MS_BLOCK_SIZE_SHIFT ARCH_MIN_MS_BLOCK_SIZE_SHIFT
52 #define MS_BLOCK_SIZE (16*1024)
53 #define MS_BLOCK_SIZE_SHIFT 14
55 #define MAJOR_SECTION_SIZE MS_BLOCK_SIZE
56 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
59 * Don't allocate single blocks, but alloc a contingent of this many
60 * blocks in one swoop. This must be a power of two.
62 #define MS_BLOCK_ALLOC_NUM 32
65 * Number of bytes before the first object in a block. At the start
66 * of a block is the MSBlockHeader, then opional padding, then come
67 * the objects, so this must be >= sizeof (MSBlockHeader).
69 #define MS_BLOCK_SKIP 16
71 #define MS_BLOCK_FREE (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
73 #define MS_NUM_MARK_WORDS ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
75 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
76 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
79 typedef struct _MSBlockInfo MSBlockInfo;
83 unsigned int pinned : 1;
84 unsigned int has_references : 1;
85 unsigned int has_pinned : 1; /* means cannot evacuate */
86 unsigned int is_to_space : 1;
87 unsigned int swept : 1;
90 MSBlockInfo *next_free;
91 size_t pin_queue_first_entry;
92 size_t pin_queue_last_entry;
93 #ifdef SGEN_HAVE_CONCURRENT_MARK
94 guint8 *cardtable_mod_union;
96 mword mark_words [MS_NUM_MARK_WORDS];
99 #define MS_BLOCK_FOR_BLOCK_INFO(b) ((b)->block)
101 #define MS_BLOCK_OBJ(b,i) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (b)->obj_size * (i))
102 #define MS_BLOCK_OBJ_FOR_SIZE(b,i,obj_size) (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP + (obj_size) * (i))
103 #define MS_BLOCK_DATA_FOR_OBJ(o) ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
109 #define MS_BLOCK_FOR_OBJ(o) (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
111 /* object index will always be small */
112 #define MS_BLOCK_OBJ_INDEX(o,b) ((int)(((char*)(o) - (MS_BLOCK_FOR_BLOCK_INFO(b) + MS_BLOCK_SKIP)) / (b)->obj_size))
114 //casting to int is fine since blocks are 32k
115 #define MS_CALC_MARK_BIT(w,b,o) do { \
116 int i = ((int)((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o)))) >> SGEN_ALLOC_ALIGN_BITS; \
117 if (sizeof (mword) == 4) { \
126 #define MS_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] & (ONE_P << (b)))
127 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (ONE_P << (b)))
128 #define MS_PAR_SET_MARK_BIT(was_marked,bl,w,b) do { \
129 mword __old = (bl)->mark_words [(w)]; \
130 mword __bitmask = ONE_P << (b); \
131 if (__old & __bitmask) { \
135 if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)], \
136 (gpointer)(__old | __bitmask), \
137 (gpointer)__old) == \
139 was_marked = FALSE; \
144 #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))
146 #define MS_BLOCK_OBJ_SIZE_FACTOR (sqrt (2.0))
149 * This way we can lookup block object size indexes for sizes up to
150 * 256 bytes with a single load.
152 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES 32
154 static int *block_obj_sizes;
155 static int num_block_obj_sizes;
156 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
158 #define MS_BLOCK_FLAG_PINNED 1
159 #define MS_BLOCK_FLAG_REFS 2
161 #define MS_BLOCK_TYPE_MAX 4
163 static gboolean *evacuate_block_obj_sizes;
164 static float evacuation_threshold = 0.666f;
165 #ifdef SGEN_HAVE_CONCURRENT_MARK
166 static float concurrent_evacuation_threshold = 0.666f;
167 static gboolean want_evacuation = FALSE;
170 static gboolean lazy_sweep = TRUE;
171 static gboolean have_swept;
173 #ifdef SGEN_HAVE_CONCURRENT_MARK
174 static gboolean concurrent_mark;
177 #define BLOCK_IS_TAGGED_HAS_REFERENCES(bl) SGEN_POINTER_IS_TAGGED_1 ((bl))
178 #define BLOCK_TAG_HAS_REFERENCES(bl) SGEN_POINTER_TAG_1 ((bl))
179 #define BLOCK_UNTAG_HAS_REFERENCES(bl) SGEN_POINTER_UNTAG_1 ((bl))
181 #define BLOCK_TAG(bl) ((bl)->has_references ? BLOCK_TAG_HAS_REFERENCES ((bl)) : (bl))
183 /* all allocated blocks in the system */
184 static SgenPointerQueue allocated_blocks;
186 /* non-allocated block free-list */
187 static void *empty_blocks = NULL;
188 static size_t num_empty_blocks = 0;
190 #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]);
191 #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));
192 #define END_FOREACH_BLOCK } }
193 #define DELETE_BLOCK_IN_FOREACH() (allocated_blocks.data [__index] = NULL)
195 static size_t num_major_sections = 0;
196 /* one free block list for each block object size */
197 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
199 static long long stat_major_blocks_alloced = 0;
200 static long long stat_major_blocks_freed = 0;
201 static long long stat_major_blocks_lazy_swept = 0;
202 static long long stat_major_objects_evacuated = 0;
204 #if SIZEOF_VOID_P != 8
205 static long long stat_major_blocks_freed_ideal = 0;
206 static long long stat_major_blocks_freed_less_ideal = 0;
207 static long long stat_major_blocks_freed_individual = 0;
208 static long long stat_major_blocks_alloced_less_ideal = 0;
211 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
212 static long long num_major_objects_marked = 0;
213 #define INC_NUM_MAJOR_OBJECTS_MARKED() (++num_major_objects_marked)
215 #define INC_NUM_MAJOR_OBJECTS_MARKED()
219 sweep_block (MSBlockInfo *block, gboolean during_major_collection);
222 ms_find_block_obj_size_index (size_t size)
225 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);
226 for (i = 0; i < num_block_obj_sizes; ++i)
227 if (block_obj_sizes [i] >= size)
229 g_error ("no object of size %d\n", size);
232 #define FREE_BLOCKS_FROM(lists,p,r) (lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
233 #define FREE_BLOCKS(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
235 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
236 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
237 fast_block_obj_size_indexes [((s)+7)>>3] : \
238 ms_find_block_obj_size_index ((s)))
241 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
245 start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
247 start = sgen_alloc_os_memory (nursery_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
253 update_heap_boundaries_for_block (MSBlockInfo *block)
255 sgen_update_heap_boundaries ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), (mword)MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE);
259 ms_get_empty_block (void)
263 void *block, *empty, *next;
268 * We try allocating MS_BLOCK_ALLOC_NUM blocks first. If that's
269 * unsuccessful, we halve the number of blocks and try again, until we're at
270 * 1. If that doesn't work, either, we assert.
272 int alloc_num = MS_BLOCK_ALLOC_NUM;
274 p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * alloc_num, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE,
275 alloc_num == 1 ? "major heap section" : NULL);
281 for (i = 0; i < alloc_num; ++i) {
284 * We do the free list update one after the
285 * other so that other threads can use the new
286 * blocks as quickly as possible.
289 empty = empty_blocks;
290 *(void**)block = empty;
291 } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block, empty) != empty);
295 SGEN_ATOMIC_ADD_P (num_empty_blocks, alloc_num);
297 stat_major_blocks_alloced += alloc_num;
298 #if SIZEOF_VOID_P != 8
299 if (alloc_num != MS_BLOCK_ALLOC_NUM)
300 stat_major_blocks_alloced_less_ideal += alloc_num;
305 empty = empty_blocks;
309 next = *(void**)block;
310 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
312 SGEN_ATOMIC_ADD_P (num_empty_blocks, -1);
314 *(void**)block = NULL;
316 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
322 ms_free_block (void *block)
326 sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
327 memset (block, 0, MS_BLOCK_SIZE);
330 empty = empty_blocks;
331 *(void**)block = empty;
332 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
334 SGEN_ATOMIC_ADD_P (num_empty_blocks, 1);
337 //#define MARKSWEEP_CONSISTENCY_CHECK
339 #ifdef MARKSWEEP_CONSISTENCY_CHECK
341 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
345 for (; block; block = block->next_free) {
346 g_assert (block->obj_size == size);
347 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
349 /* blocks in the free lists must have at least
352 g_assert (block->free_list);
354 /* the block must be in the allocated_blocks array */
355 g_assert (sgen_pointer_queue_find (&allocated_blocks, BLOCK_TAG (block)) != (size_t)-1);
360 check_empty_blocks (void)
364 for (p = empty_blocks; p; p = *(void**)p)
366 g_assert (i == num_empty_blocks);
370 consistency_check (void)
375 /* check all blocks */
376 FOREACH_BLOCK (block) {
377 int count = MS_BLOCK_FREE / block->obj_size;
381 /* check block header */
382 g_assert (((MSBlockHeader*)block->block)->info == block);
384 /* count number of free slots */
385 for (i = 0; i < count; ++i) {
386 void **obj = (void**) MS_BLOCK_OBJ (block, i);
387 if (!MS_OBJ_ALLOCED (obj, block))
391 /* check free list */
392 for (free = block->free_list; free; free = (void**)*free) {
393 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
396 g_assert (num_free == 0);
398 /* check all mark words are zero */
400 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
401 g_assert (block->mark_words [i] == 0);
405 /* check free blocks */
406 for (i = 0; i < num_block_obj_sizes; ++i) {
408 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
409 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
412 check_empty_blocks ();
417 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
419 int size = block_obj_sizes [size_index];
420 int count = MS_BLOCK_FREE / size;
422 MSBlockHeader *header;
423 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
427 if (!sgen_memgov_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
430 info = sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
432 SGEN_ASSERT (9, count >= 2, "block with %d objects, it must hold at least 2", count);
434 info->obj_size = size;
435 info->obj_size_index = size_index;
436 info->pinned = pinned;
437 info->has_references = has_references;
438 info->has_pinned = pinned;
440 * Blocks that are to-space are not evacuated from. During an major collection
441 * blocks are allocated for two reasons: evacuating objects from the nursery and
442 * evacuating them from major blocks marked for evacuation. In both cases we don't
443 * want further evacuation.
445 info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD);
447 info->block = ms_get_empty_block ();
449 header = (MSBlockHeader*) info->block;
451 #ifdef SGEN_HAVE_CONCURRENT_MARK
452 info->cardtable_mod_union = NULL;
455 update_heap_boundaries_for_block (info);
457 /* build free list */
458 obj_start = MS_BLOCK_FOR_BLOCK_INFO (info) + MS_BLOCK_SKIP;
459 info->free_list = (void**)obj_start;
460 /* we're skipping the last one - it must be nulled */
461 for (i = 0; i < count - 1; ++i) {
462 char *next_obj_start = obj_start + size;
463 *(void**)obj_start = next_obj_start;
464 obj_start = next_obj_start;
467 *(void**)obj_start = NULL;
469 info->next_free = free_blocks [size_index];
470 free_blocks [size_index] = info;
472 sgen_pointer_queue_add (&allocated_blocks, BLOCK_TAG (info));
474 ++num_major_sections;
479 obj_is_from_pinned_alloc (char *ptr)
483 FOREACH_BLOCK (block) {
484 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE)
485 return block->pinned;
491 unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_index)
496 block = free_blocks [size_index];
497 SGEN_ASSERT (9, block, "no free block to unlink from free_blocks %p size_index %d", free_blocks, size_index);
499 if (G_UNLIKELY (!block->swept)) {
500 stat_major_blocks_lazy_swept ++;
501 sweep_block (block, FALSE);
504 obj = block->free_list;
505 SGEN_ASSERT (9, obj, "block %p in free list had no available object to alloc from", block);
507 block->free_list = *(void**)obj;
508 if (!block->free_list) {
509 free_blocks [size_index] = block->next_free;
510 block->next_free = NULL;
517 alloc_obj (MonoVTable *vtable, size_t size, gboolean pinned, gboolean has_references)
519 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
520 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
523 if (!free_blocks [size_index]) {
524 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
528 obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
530 *(MonoVTable**)obj = vtable;
536 major_alloc_object (MonoVTable *vtable, size_t size, gboolean has_references)
538 return alloc_obj (vtable, size, FALSE, has_references);
542 * We're not freeing the block if it's empty. We leave that work for
543 * the next major collection.
545 * This is just called from the domain clearing code, which runs in a
546 * single thread and has the GC lock, so we don't need an extra lock.
549 free_object (char *obj, size_t size, gboolean pinned)
551 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
555 sweep_block (block, FALSE);
556 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);
557 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p is already free", obj);
558 MS_CALC_MARK_BIT (word, bit, obj);
559 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p has mark bit set");
560 if (!block->free_list) {
561 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
562 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
563 SGEN_ASSERT (9, !block->next_free, "block %p doesn't have a free-list of object but belongs to a free-list of blocks");
564 block->next_free = free_blocks [size_index];
565 free_blocks [size_index] = block;
567 memset (obj, 0, size);
568 *(void**)obj = block->free_list;
569 block->free_list = (void**)obj;
573 major_free_non_pinned_object (char *obj, size_t size)
575 free_object (obj, size, FALSE);
578 /* size is a multiple of SGEN_ALLOC_ALIGN */
580 major_alloc_small_pinned_obj (MonoVTable *vtable, size_t size, gboolean has_references)
584 res = alloc_obj (vtable, size, TRUE, has_references);
585 /*If we failed to alloc memory, we better try releasing memory
586 *as pinned alloc is requested by the runtime.
589 sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure", TRUE);
590 res = alloc_obj (vtable, size, TRUE, has_references);
596 free_pinned_object (char *obj, size_t size)
598 free_object (obj, size, TRUE);
602 * size is already rounded up and we hold the GC lock.
605 major_alloc_degraded (MonoVTable *vtable, size_t size)
608 size_t old_num_sections;
610 old_num_sections = num_major_sections;
612 obj = alloc_obj (vtable, size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
613 if (G_LIKELY (obj)) {
614 HEAVY_STAT (++stat_objects_alloced_degraded);
615 HEAVY_STAT (stat_bytes_alloced_degraded += size);
616 g_assert (num_major_sections >= old_num_sections);
617 sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
622 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj) FALSE
625 * obj is some object. If it's not in the major heap (i.e. if it's in
626 * the nursery or LOS), return FALSE. Otherwise return whether it's
627 * been marked or copied.
630 major_is_object_live (char *obj)
636 if (sgen_ptr_in_nursery (obj))
639 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
642 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
645 /* now we know it's in a major block */
646 block = MS_BLOCK_FOR_OBJ (obj);
647 SGEN_ASSERT (9, !block->pinned, "block %p is pinned, BTW why is this bad?");
648 MS_CALC_MARK_BIT (word, bit, obj);
649 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
653 major_ptr_is_in_non_pinned_space (char *ptr, char **start)
657 FOREACH_BLOCK (block) {
658 if (ptr >= MS_BLOCK_FOR_BLOCK_INFO (block) && ptr <= MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) {
659 int count = MS_BLOCK_FREE / block->obj_size;
663 for (i = 0; i <= count; ++i) {
664 if (ptr >= MS_BLOCK_OBJ (block, i) && ptr < MS_BLOCK_OBJ (block, i + 1)) {
665 *start = MS_BLOCK_OBJ (block, i);
669 return !block->pinned;
676 major_iterate_objects (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data)
678 gboolean sweep = flags & ITERATE_OBJECTS_SWEEP;
679 gboolean non_pinned = flags & ITERATE_OBJECTS_NON_PINNED;
680 gboolean pinned = flags & ITERATE_OBJECTS_PINNED;
683 FOREACH_BLOCK (block) {
684 int count = MS_BLOCK_FREE / block->obj_size;
687 if (block->pinned && !pinned)
689 if (!block->pinned && !non_pinned)
691 if (sweep && lazy_sweep) {
692 sweep_block (block, FALSE);
693 SGEN_ASSERT (0, block->swept, "Block must be swept after sweeping");
696 for (i = 0; i < count; ++i) {
697 void **obj = (void**) MS_BLOCK_OBJ (block, i);
700 MS_CALC_MARK_BIT (word, bit, obj);
701 if (!MS_MARK_BIT (block, word, bit))
704 if (MS_OBJ_ALLOCED (obj, block))
705 callback ((char*)obj, block->obj_size, data);
711 major_is_valid_object (char *object)
715 FOREACH_BLOCK (block) {
719 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > object) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= object))
722 idx = MS_BLOCK_OBJ_INDEX (object, block);
723 obj = (char*)MS_BLOCK_OBJ (block, idx);
726 return MS_OBJ_ALLOCED (obj, block);
734 major_describe_pointer (char *ptr)
738 FOREACH_BLOCK (block) {
746 if ((MS_BLOCK_FOR_BLOCK_INFO (block) > ptr) || ((MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE) <= ptr))
749 SGEN_LOG (0, "major-ptr (block %p sz %d pin %d ref %d)\n",
750 MS_BLOCK_FOR_BLOCK_INFO (block), block->obj_size, block->pinned, block->has_references);
752 idx = MS_BLOCK_OBJ_INDEX (ptr, block);
753 obj = (char*)MS_BLOCK_OBJ (block, idx);
754 live = MS_OBJ_ALLOCED (obj, block);
755 vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
757 MS_CALC_MARK_BIT (w, b, obj);
758 marked = MS_MARK_BIT (block, w, b);
763 SGEN_LOG (0, "object");
765 SGEN_LOG (0, "dead-object");
768 SGEN_LOG (0, "interior-ptr offset %td", ptr - obj);
770 SGEN_LOG (0, "dead-interior-ptr offset %td", ptr - obj);
773 SGEN_LOG (0, " marked %d)\n", marked ? 1 : 0);
782 major_check_scan_starts (void)
787 major_dump_heap (FILE *heap_dump_file)
790 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
791 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
794 for (i = 0; i < num_block_obj_sizes; ++i)
795 slots_available [i] = slots_used [i] = 0;
797 FOREACH_BLOCK (block) {
798 int index = ms_find_block_obj_size_index (block->obj_size);
799 int count = MS_BLOCK_FREE / block->obj_size;
801 slots_available [index] += count;
802 for (i = 0; i < count; ++i) {
803 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
804 ++slots_used [index];
808 fprintf (heap_dump_file, "<occupancies>\n");
809 for (i = 0; i < num_block_obj_sizes; ++i) {
810 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
811 block_obj_sizes [i], slots_available [i], slots_used [i]);
813 fprintf (heap_dump_file, "</occupancies>\n");
815 FOREACH_BLOCK (block) {
816 int count = MS_BLOCK_FREE / block->obj_size;
820 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
822 for (i = 0; i <= count; ++i) {
823 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
828 sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), MS_BLOCK_FOR_BLOCK_INFO (block));
834 fprintf (heap_dump_file, "</section>\n");
838 #define LOAD_VTABLE SGEN_LOAD_VTABLE
840 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,desc,block,queue) do { \
842 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
843 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
844 MS_SET_MARK_BIT ((block), __word, __bit); \
845 if ((block)->has_references) \
846 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
847 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
848 INC_NUM_MAJOR_OBJECTS_MARKED (); \
851 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,desc,block,queue) do { \
853 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
854 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
855 if (!MS_MARK_BIT ((block), __word, __bit)) { \
856 MS_SET_MARK_BIT ((block), __word, __bit); \
857 if ((block)->has_references) \
858 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
859 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
860 INC_NUM_MAJOR_OBJECTS_MARKED (); \
863 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,desc,block,queue) do { \
865 gboolean __was_marked; \
866 SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
867 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
868 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
869 if (!__was_marked) { \
870 if ((block)->has_references) \
871 GRAY_OBJECT_ENQUEUE ((queue), (obj), (desc)); \
872 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
873 INC_NUM_MAJOR_OBJECTS_MARKED (); \
878 pin_major_object (char *obj, SgenGrayQueue *queue)
882 #ifdef SGEN_HAVE_CONCURRENT_MARK
884 g_assert_not_reached ();
887 block = MS_BLOCK_FOR_OBJ (obj);
888 block->has_pinned = TRUE;
889 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
892 #include "sgen-major-copy-object.h"
894 #ifdef SGEN_HAVE_CONCURRENT_MARK
896 major_copy_or_mark_object_concurrent (void **ptr, void *obj, SgenGrayQueue *queue)
898 g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
900 if (!sgen_ptr_in_nursery (obj)) {
903 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
905 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE) {
906 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
907 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
909 if (sgen_los_object_is_pinned (obj))
913 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
914 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
915 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
919 sgen_los_pin_object (obj);
920 if (SGEN_OBJECT_HAS_REFERENCES (obj))
921 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_obj_get_descriptor (obj));
922 INC_NUM_MAJOR_OBJECTS_MARKED ();
929 major_copy_or_mark_object (void **ptr, void *obj, SgenGrayQueue *queue)
933 HEAVY_STAT (++stat_copy_object_called_major);
935 SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
936 SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
938 if (sgen_ptr_in_nursery (obj)) {
940 char *forwarded, *old_obj;
942 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
946 if (SGEN_OBJECT_IS_PINNED (obj))
949 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
950 if (sgen_nursery_is_to_space (obj))
953 HEAVY_STAT (++stat_objects_copied_major);
957 obj = copy_object_no_checks (obj, queue);
958 if (G_UNLIKELY (old_obj == obj)) {
959 /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
960 if (!sgen_ptr_in_nursery (obj)) {
962 block = MS_BLOCK_FOR_OBJ (obj);
963 size_index = block->obj_size_index;
964 evacuate_block_obj_sizes [size_index] = FALSE;
965 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
972 * FIXME: See comment for copy_object_no_checks(). If
973 * we have that, we can let the allocation function
974 * give us the block info, too, and we won't have to
977 * FIXME (2): We should rework this to avoid all those nursery checks.
980 * For the split nursery allocator the object might
981 * still be in the nursery despite having being
982 * promoted, in which case we can't mark it.
984 if (!sgen_ptr_in_nursery (obj)) {
985 block = MS_BLOCK_FOR_OBJ (obj);
986 MS_CALC_MARK_BIT (word, bit, obj);
987 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
988 MS_SET_MARK_BIT (block, word, bit);
989 binary_protocol_mark (obj, (gpointer)LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
996 * If we have don't have a fixed heap we cannot know
997 * whether an object is in the LOS or in the small
998 * object major heap without checking its size. To do
999 * that, however, we need to know that we actually
1000 * have a valid object, not a forwarding pointer, so
1001 * we have to do this check first.
1003 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1008 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1010 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE) {
1014 block = MS_BLOCK_FOR_OBJ (obj);
1015 size_index = block->obj_size_index;
1016 evacuate = evacuate_block_obj_sizes [size_index];
1018 if (evacuate && !block->has_pinned) {
1019 g_assert (!SGEN_OBJECT_IS_PINNED (obj));
1020 if (block->is_to_space)
1022 HEAVY_STAT (++stat_major_objects_evacuated);
1023 goto do_copy_object;
1025 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
1028 if (sgen_los_object_is_pinned (obj))
1030 binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1032 #ifdef ENABLE_DTRACE
1033 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1034 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
1035 MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
1039 sgen_los_pin_object (obj);
1040 if (SGEN_OBJECT_HAS_REFERENCES (obj))
1041 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_obj_get_descriptor (obj));
1047 major_copy_or_mark_object_canonical (void **ptr, SgenGrayQueue *queue)
1049 major_copy_or_mark_object (ptr, *ptr, queue);
1052 #ifdef SGEN_HAVE_CONCURRENT_MARK
1054 major_copy_or_mark_object_concurrent_canonical (void **ptr, SgenGrayQueue *queue)
1056 major_copy_or_mark_object_concurrent (ptr, *ptr, queue);
1061 major_get_and_reset_num_major_objects_marked (void)
1063 #ifdef SGEN_COUNT_NUMBER_OF_MAJOR_OBJECTS_MARKED
1064 long long num = num_major_objects_marked;
1065 num_major_objects_marked = 0;
1072 #include "sgen-major-scan-object.h"
1074 #ifdef SGEN_HAVE_CONCURRENT_MARK
1075 #define SCAN_FOR_CONCURRENT_MARK
1076 #include "sgen-major-scan-object.h"
1077 #undef SCAN_FOR_CONCURRENT_MARK
1081 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1083 void **entry, **end;
1084 int last_index = -1;
1086 if (block->pin_queue_first_entry == block->pin_queue_last_entry)
1089 block->has_pinned = TRUE;
1091 entry = sgen_pinning_get_entry (block->pin_queue_first_entry);
1092 end = sgen_pinning_get_entry (block->pin_queue_last_entry);
1094 for (; entry < end; ++entry) {
1095 int index = MS_BLOCK_OBJ_INDEX (*entry, block);
1097 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);
1098 if (index == last_index)
1100 obj = MS_BLOCK_OBJ (block, index);
1101 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (obj, sgen_obj_get_descriptor (obj), block, queue);
1107 sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
1111 for (obj_index = 0; obj_index < count; ++obj_index) {
1113 void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
1115 MS_CALC_MARK_BIT (word, bit, obj);
1116 if (MS_MARK_BIT (block, word, bit)) {
1117 SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
1119 /* an unmarked object */
1120 if (MS_OBJ_ALLOCED (obj, block)) {
1122 * FIXME: Merge consecutive
1123 * slots for lower reporting
1124 * overhead. Maybe memset
1125 * will also benefit?
1127 binary_protocol_empty (obj, obj_size);
1128 MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
1129 memset (obj, 0, obj_size);
1131 *(void**)obj = block->free_list;
1132 block->free_list = obj;
1140 * Traverse BLOCK, freeing and zeroing unused objects.
1143 sweep_block (MSBlockInfo *block, gboolean during_major_collection)
1146 void *reversed = NULL;
1148 if (!during_major_collection)
1149 g_assert (!sgen_concurrent_collection_in_progress ());
1154 count = MS_BLOCK_FREE / block->obj_size;
1156 block->free_list = NULL;
1158 /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
1159 // FIXME: Add more sizes
1160 switch (block->obj_size) {
1162 sweep_block_for_size (block, count, 16);
1165 sweep_block_for_size (block, count, block->obj_size);
1169 /* reset mark bits */
1170 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1172 /* Reverse free list so that it's in address order */
1174 while (block->free_list) {
1175 void *next = *(void**)block->free_list;
1176 *(void**)block->free_list = reversed;
1177 reversed = block->free_list;
1178 block->free_list = next;
1180 block->free_list = reversed;
1191 if (sizeof (mword) == sizeof (unsigned long))
1192 count += __builtin_popcountl (d);
1194 count += __builtin_popcount (d);
1210 /* statistics for evacuation */
1211 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1212 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1213 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1215 #ifdef SGEN_HAVE_CONCURRENT_MARK
1216 mword total_evacuate_heap = 0;
1217 mword total_evacuate_saved = 0;
1220 for (i = 0; i < num_block_obj_sizes; ++i)
1221 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1223 /* clear all the free lists */
1224 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1225 MSBlockInfo **free_blocks = free_block_lists [i];
1227 for (j = 0; j < num_block_obj_sizes; ++j)
1228 free_blocks [j] = NULL;
1231 /* traverse all blocks, free and zero unmarked objects */
1232 FOREACH_BLOCK (block) {
1234 gboolean have_live = FALSE;
1235 gboolean has_pinned;
1236 gboolean have_free = FALSE;
1240 obj_size_index = block->obj_size_index;
1242 has_pinned = block->has_pinned;
1243 block->has_pinned = block->pinned;
1245 block->is_to_space = FALSE;
1248 count = MS_BLOCK_FREE / block->obj_size;
1250 #ifdef SGEN_HAVE_CONCURRENT_MARK
1251 if (block->cardtable_mod_union) {
1252 sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
1253 block->cardtable_mod_union = NULL;
1257 /* Count marked objects in the block */
1258 for (i = 0; i < MS_NUM_MARK_WORDS; ++i) {
1259 nused += bitcount (block->mark_words [i]);
1268 sweep_block (block, TRUE);
1272 ++num_blocks [obj_size_index];
1273 slots_used [obj_size_index] += nused;
1274 slots_available [obj_size_index] += count;
1278 * If there are free slots in the block, add
1279 * the block to the corresponding free list.
1282 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1283 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1284 block->next_free = free_blocks [index];
1285 free_blocks [index] = block;
1288 update_heap_boundaries_for_block (block);
1291 * Blocks without live objects are removed from the
1292 * block list and freed.
1294 DELETE_BLOCK_IN_FOREACH ();
1296 binary_protocol_empty (MS_BLOCK_OBJ (block, 0), (char*)MS_BLOCK_OBJ (block, count) - (char*)MS_BLOCK_OBJ (block, 0));
1297 ms_free_block (block->block);
1298 sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1300 --num_major_sections;
1302 } END_FOREACH_BLOCK;
1303 sgen_pointer_queue_remove_nulls (&allocated_blocks);
1305 for (i = 0; i < num_block_obj_sizes; ++i) {
1306 float usage = (float)slots_used [i] / (float)slots_available [i];
1307 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1308 evacuate_block_obj_sizes [i] = TRUE;
1310 g_print ("slot size %d - %d of %d used\n",
1311 block_obj_sizes [i], slots_used [i], slots_available [i]);
1314 evacuate_block_obj_sizes [i] = FALSE;
1316 #ifdef SGEN_HAVE_CONCURRENT_MARK
1318 mword total_bytes = block_obj_sizes [i] * slots_available [i];
1319 total_evacuate_heap += total_bytes;
1320 if (evacuate_block_obj_sizes [i])
1321 total_evacuate_saved += total_bytes - block_obj_sizes [i] * slots_used [i];
1326 #ifdef SGEN_HAVE_CONCURRENT_MARK
1327 want_evacuation = (float)total_evacuate_saved / (float)total_evacuate_heap > (1 - concurrent_evacuation_threshold);
1339 static int count_pinned_ref;
1340 static int count_pinned_nonref;
1341 static int count_nonpinned_ref;
1342 static int count_nonpinned_nonref;
1345 count_nonpinned_callback (char *obj, size_t size, void *data)
1347 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1349 if (vtable->klass->has_references)
1350 ++count_nonpinned_ref;
1352 ++count_nonpinned_nonref;
1356 count_pinned_callback (char *obj, size_t size, void *data)
1358 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1360 if (vtable->klass->has_references)
1363 ++count_pinned_nonref;
1366 static G_GNUC_UNUSED void
1367 count_ref_nonref_objs (void)
1371 count_pinned_ref = 0;
1372 count_pinned_nonref = 0;
1373 count_nonpinned_ref = 0;
1374 count_nonpinned_nonref = 0;
1376 major_iterate_objects (ITERATE_OBJECTS_SWEEP_NON_PINNED, count_nonpinned_callback, NULL);
1377 major_iterate_objects (ITERATE_OBJECTS_SWEEP_PINNED, count_pinned_callback, NULL);
1379 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1381 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1382 count_pinned_ref, count_nonpinned_ref,
1383 count_pinned_nonref, count_nonpinned_nonref,
1384 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1388 ms_calculate_block_obj_sizes (double factor, int *arr)
1390 double target_size = sizeof (MonoObject);
1395 int target_count = (int)ceil (MS_BLOCK_FREE / target_size);
1396 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1398 if (size != last_size) {
1400 arr [num_sizes] = size;
1405 target_size *= factor;
1406 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1411 /* only valid during minor collections */
1412 static mword old_num_major_sections;
1415 major_start_nursery_collection (void)
1417 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1418 consistency_check ();
1421 old_num_major_sections = num_major_sections;
1425 major_finish_nursery_collection (void)
1427 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1428 consistency_check ();
1430 sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1434 major_start_major_collection (void)
1438 /* clear the free lists */
1439 for (i = 0; i < num_block_obj_sizes; ++i) {
1440 if (!evacuate_block_obj_sizes [i])
1443 free_block_lists [0][i] = NULL;
1444 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1447 // Sweep all unswept blocks
1451 MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
1453 FOREACH_BLOCK (block) {
1454 sweep_block (block, TRUE);
1455 } END_FOREACH_BLOCK;
1457 MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
1462 major_finish_major_collection (void)
1466 #if SIZEOF_VOID_P != 8
1468 compare_pointers (const void *va, const void *vb) {
1469 char *a = *(char**)va, *b = *(char**)vb;
1479 major_have_computer_minor_collection_allowance (void)
1481 size_t section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1483 g_assert (have_swept);
1485 #if SIZEOF_VOID_P != 8
1487 int i, num_empty_blocks_orig, num_blocks, arr_length;
1489 void **empty_block_arr;
1490 void **rebuild_next;
1494 * sgen_free_os_memory () asserts in mono_vfree () because windows doesn't like freeing the middle of
1495 * a VirtualAlloc ()-ed block.
1500 if (num_empty_blocks <= section_reserve)
1502 SGEN_ASSERT (0, num_empty_blocks > 0, "section reserve can't be negative");
1504 num_empty_blocks_orig = num_empty_blocks;
1505 empty_block_arr = (void**)sgen_alloc_internal_dynamic (sizeof (void*) * num_empty_blocks_orig,
1506 INTERNAL_MEM_MS_BLOCK_INFO_SORT, FALSE);
1507 if (!empty_block_arr)
1511 for (block = empty_blocks; block; block = *(void**)block)
1512 empty_block_arr [i++] = block;
1513 SGEN_ASSERT (0, i == num_empty_blocks, "empty block count wrong");
1515 sgen_qsort (empty_block_arr, num_empty_blocks, sizeof (void*), compare_pointers);
1518 * We iterate over the free blocks, trying to find MS_BLOCK_ALLOC_NUM
1519 * contiguous ones. If we do, we free them. If that's not enough to get to
1520 * section_reserve, we halve the number of contiguous blocks we're looking
1521 * for and have another go, until we're done with looking for pairs of
1522 * blocks, at which point we give up and go to the fallback.
1524 arr_length = num_empty_blocks_orig;
1525 num_blocks = MS_BLOCK_ALLOC_NUM;
1526 while (num_empty_blocks > section_reserve && num_blocks > 1) {
1531 for (i = 0; i < arr_length; ++i) {
1533 void *block = empty_block_arr [i];
1534 SGEN_ASSERT (0, block, "we're not shifting correctly");
1536 empty_block_arr [dest] = block;
1538 * This is not strictly necessary, but we're
1541 empty_block_arr [i] = NULL;
1550 SGEN_ASSERT (0, first >= 0 && d > first, "algorithm is wrong");
1552 if ((char*)block != ((char*)empty_block_arr [d-1]) + MS_BLOCK_SIZE) {
1557 if (d + 1 - first == num_blocks) {
1559 * We found num_blocks contiguous blocks. Free them
1560 * and null their array entries. As an optimization
1561 * we could, instead of nulling the entries, shift
1562 * the following entries over to the left, while
1566 sgen_free_os_memory (empty_block_arr [first], MS_BLOCK_SIZE * num_blocks, SGEN_ALLOC_HEAP);
1567 for (j = first; j <= d; ++j)
1568 empty_block_arr [j] = NULL;
1572 num_empty_blocks -= num_blocks;
1574 stat_major_blocks_freed += num_blocks;
1575 if (num_blocks == MS_BLOCK_ALLOC_NUM)
1576 stat_major_blocks_freed_ideal += num_blocks;
1578 stat_major_blocks_freed_less_ideal += num_blocks;
1583 SGEN_ASSERT (0, dest <= i && dest <= arr_length, "array length is off");
1585 SGEN_ASSERT (0, arr_length == num_empty_blocks, "array length is off");
1590 /* rebuild empty_blocks free list */
1591 rebuild_next = (void**)&empty_blocks;
1592 for (i = 0; i < arr_length; ++i) {
1593 void *block = empty_block_arr [i];
1594 SGEN_ASSERT (0, block, "we're missing blocks");
1595 *rebuild_next = block;
1596 rebuild_next = (void**)block;
1598 *rebuild_next = NULL;
1601 sgen_free_internal_dynamic (empty_block_arr, sizeof (void*) * num_empty_blocks_orig, INTERNAL_MEM_MS_BLOCK_INFO_SORT);
1604 SGEN_ASSERT (0, num_empty_blocks >= 0, "we freed more blocks than we had in the first place?");
1608 * This is our threshold. If there's not more empty than used blocks, we won't
1609 * release uncontiguous blocks, in fear of fragmenting the address space.
1611 if (num_empty_blocks <= num_major_sections)
1615 while (num_empty_blocks > section_reserve) {
1616 void *next = *(void**)empty_blocks;
1617 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
1618 empty_blocks = next;
1620 * Needs not be atomic because this is running
1625 ++stat_major_blocks_freed;
1626 #if SIZEOF_VOID_P != 8
1627 ++stat_major_blocks_freed_individual;
1633 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1637 FOREACH_BLOCK (block) {
1638 sgen_find_optimized_pin_queue_area (MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SKIP, MS_BLOCK_FOR_BLOCK_INFO (block) + MS_BLOCK_SIZE,
1639 &block->pin_queue_first_entry, &block->pin_queue_last_entry);
1640 } END_FOREACH_BLOCK;
1644 major_pin_objects (SgenGrayQueue *queue)
1648 FOREACH_BLOCK (block) {
1649 mark_pinned_objects_in_block (block, queue);
1650 } END_FOREACH_BLOCK;
1654 major_init_to_space (void)
1659 major_report_pinned_memory_usage (void)
1661 g_assert_not_reached ();
1665 major_get_used_size (void)
1670 FOREACH_BLOCK (block) {
1671 int count = MS_BLOCK_FREE / block->obj_size;
1673 size += count * block->obj_size;
1674 for (iter = block->free_list; iter; iter = (void**)*iter)
1675 size -= block->obj_size;
1676 } END_FOREACH_BLOCK;
1682 get_num_major_sections (void)
1684 return num_major_sections;
1688 major_handle_gc_param (const char *opt)
1690 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1691 const char *arg = strchr (opt, '=') + 1;
1692 int percentage = atoi (arg);
1693 if (percentage < 0 || percentage > 100) {
1694 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1697 evacuation_threshold = (float)percentage / 100.0f;
1699 } else if (!strcmp (opt, "lazy-sweep")) {
1702 } else if (!strcmp (opt, "no-lazy-sweep")) {
1711 major_print_gc_param_usage (void)
1715 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1716 " (no-)lazy-sweep\n"
1721 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1724 gboolean has_references;
1726 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1728 callback ((mword)MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE);
1729 } END_FOREACH_BLOCK;
1732 #ifdef HEAVY_STATISTICS
1733 extern long long marked_cards;
1734 extern long long scanned_cards;
1735 extern long long scanned_objects;
1736 extern long long remarked_cards;
1739 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1741 * MS blocks are 16K aligned.
1742 * Cardtables are 4K aligned, at least.
1743 * This means that the cardtable of a given block is 32 bytes aligned.
1746 initial_skip_card (guint8 *card_data)
1748 mword *cards = (mword*)card_data;
1751 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1757 if (i == CARD_WORDS_PER_BLOCK)
1758 return card_data + CARDS_PER_BLOCK;
1760 #if defined(__i386__) && defined(__GNUC__)
1761 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1762 #elif defined(__x86_64__) && defined(__GNUC__)
1763 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1764 #elif defined(__s390x__) && defined(__GNUC__)
1765 return card_data + i * 8 + (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
1767 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1769 return &card_data [i];
1776 static G_GNUC_UNUSED guint8*
1777 skip_card (guint8 *card_data, guint8 *card_data_end)
1779 while (card_data < card_data_end && !*card_data)
1784 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1785 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1786 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1789 major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
1792 gboolean has_references;
1793 ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
1795 #ifdef SGEN_HAVE_CONCURRENT_MARK
1796 if (!concurrent_mark)
1797 g_assert (!mod_union);
1799 g_assert (!mod_union);
1802 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1806 if (!has_references)
1809 block_obj_size = block->obj_size;
1810 block_start = MS_BLOCK_FOR_BLOCK_INFO (block);
1812 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1814 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1815 guint8 cards_data [CARDS_PER_BLOCK];
1817 char *obj, *end, *base;
1820 #ifdef SGEN_HAVE_CONCURRENT_MARK
1821 cards = block->cardtable_mod_union;
1823 * This happens when the nursery
1824 * collection that precedes finishing
1825 * the concurrent collection allocates
1832 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1833 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1834 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1837 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1842 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1843 end = block_start + MS_BLOCK_SIZE;
1844 base = sgen_card_table_align_pointer (obj);
1850 sweep_block (block, FALSE);
1852 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
1856 /* FIXME: do this more efficiently */
1858 MS_CALC_MARK_BIT (w, b, obj);
1859 if (!MS_MARK_BIT (block, w, b))
1863 card_offset = (obj - base) >> CARD_BITS;
1864 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, mod_union, queue);
1867 obj += block_obj_size;
1870 guint8 *card_data, *card_base;
1871 guint8 *card_data_end;
1874 * This is safe in face of card aliasing for the following reason:
1876 * Major blocks are 16k aligned, or 32 cards aligned.
1877 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1878 * sizes, they won't overflow the cardtable overlap modulus.
1881 #ifdef SGEN_HAVE_CONCURRENT_MARK
1882 card_data = card_base = block->cardtable_mod_union;
1884 * This happens when the nursery
1885 * collection that precedes finishing
1886 * the concurrent collection allocates
1892 g_assert_not_reached ();
1896 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1898 card_data_end = card_data + CARDS_PER_BLOCK;
1900 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)) {
1902 size_t idx = card_data - card_base;
1903 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1904 char *end = start + CARD_SIZE_IN_BYTES;
1905 char *first_obj, *obj;
1907 HEAVY_STAT (++scanned_cards);
1913 sweep_block (block, FALSE);
1915 HEAVY_STAT (++marked_cards);
1917 sgen_card_table_prepare_card_for_scanning (card_data);
1922 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1924 obj = first_obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1926 if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
1930 /* FIXME: do this more efficiently */
1932 MS_CALC_MARK_BIT (w, b, obj);
1933 if (!MS_MARK_BIT (block, w, b))
1937 HEAVY_STAT (++scanned_objects);
1938 scan_func (obj, sgen_obj_get_descriptor (obj), queue);
1940 obj += block_obj_size;
1942 HEAVY_STAT (if (*card_data) ++remarked_cards);
1943 binary_protocol_card_scan (first_obj, obj - first_obj);
1946 } END_FOREACH_BLOCK;
1950 major_count_cards (long long *num_total_cards, long long *num_marked_cards)
1953 gboolean has_references;
1954 long long total_cards = 0;
1955 long long marked_cards = 0;
1957 FOREACH_BLOCK_HAS_REFERENCES (block, has_references) {
1958 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) MS_BLOCK_FOR_BLOCK_INFO (block));
1961 if (!has_references)
1964 total_cards += CARDS_PER_BLOCK;
1965 for (i = 0; i < CARDS_PER_BLOCK; ++i) {
1969 } END_FOREACH_BLOCK;
1971 *num_total_cards = total_cards;
1972 *num_marked_cards = marked_cards;
1975 #ifdef SGEN_HAVE_CONCURRENT_MARK
1977 update_cardtable_mod_union (void)
1981 FOREACH_BLOCK (block) {
1984 block->cardtable_mod_union = sgen_card_table_update_mod_union (block->cardtable_mod_union,
1985 MS_BLOCK_FOR_BLOCK_INFO (block), MS_BLOCK_SIZE, &num_cards);
1987 SGEN_ASSERT (0, num_cards == CARDS_PER_BLOCK, "Number of cards calculation is wrong");
1988 } END_FOREACH_BLOCK;
1992 major_get_cardtable_mod_union_for_object (char *obj)
1994 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1995 return &block->cardtable_mod_union [(obj - (char*)sgen_card_table_align_pointer (MS_BLOCK_FOR_BLOCK_INFO (block))) >> CARD_BITS];
2000 alloc_free_block_lists (MSBlockInfo ***lists)
2003 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
2004 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2007 #undef pthread_create
2010 post_param_init (SgenMajorCollector *collector)
2012 collector->sweeps_lazily = lazy_sweep;
2015 #ifdef SGEN_HAVE_CONCURRENT_MARK
2017 sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurrent)
2018 #else // SGEN_HAVE_CONCURRENT_MARK
2019 #error unknown configuration
2020 #endif // SGEN_HAVE_CONCURRENT_MARK
2024 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
2026 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
2027 block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2028 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2030 evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2031 for (i = 0; i < num_block_obj_sizes; ++i)
2032 evacuate_block_obj_sizes [i] = FALSE;
2037 g_print ("block object sizes:\n");
2038 for (i = 0; i < num_block_obj_sizes; ++i)
2039 g_print ("%d\n", block_obj_sizes [i]);
2043 alloc_free_block_lists (free_block_lists);
2045 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2046 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2047 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2048 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2050 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2051 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2052 mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_lazy_swept);
2053 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2054 #if SIZEOF_VOID_P != 8
2055 mono_counters_register ("# major blocks freed ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_ideal);
2056 mono_counters_register ("# major blocks freed less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_less_ideal);
2057 mono_counters_register ("# major blocks freed individually", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed_individual);
2058 mono_counters_register ("# major blocks allocated less ideally", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced_less_ideal);
2061 collector->section_size = MAJOR_SECTION_SIZE;
2063 #ifdef SGEN_HAVE_CONCURRENT_MARK
2064 concurrent_mark = is_concurrent;
2065 if (is_concurrent) {
2066 collector->is_concurrent = TRUE;
2067 collector->want_synchronous_collection = &want_evacuation;
2071 collector->is_concurrent = FALSE;
2072 collector->want_synchronous_collection = NULL;
2074 collector->get_and_reset_num_major_objects_marked = major_get_and_reset_num_major_objects_marked;
2075 collector->supports_cardtable = TRUE;
2077 collector->have_swept = &have_swept;
2079 collector->alloc_heap = major_alloc_heap;
2080 collector->is_object_live = major_is_object_live;
2081 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2082 collector->alloc_degraded = major_alloc_degraded;
2084 collector->alloc_object = major_alloc_object;
2085 collector->free_pinned_object = free_pinned_object;
2086 collector->iterate_objects = major_iterate_objects;
2087 collector->free_non_pinned_object = major_free_non_pinned_object;
2088 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2089 collector->pin_objects = major_pin_objects;
2090 collector->pin_major_object = pin_major_object;
2091 collector->scan_card_table = major_scan_card_table;
2092 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2093 #ifdef SGEN_HAVE_CONCURRENT_MARK
2094 if (is_concurrent) {
2095 collector->update_cardtable_mod_union = update_cardtable_mod_union;
2096 collector->get_cardtable_mod_union_for_object = major_get_cardtable_mod_union_for_object;
2099 collector->init_to_space = major_init_to_space;
2100 collector->sweep = major_sweep;
2101 collector->check_scan_starts = major_check_scan_starts;
2102 collector->dump_heap = major_dump_heap;
2103 collector->get_used_size = major_get_used_size;
2104 collector->start_nursery_collection = major_start_nursery_collection;
2105 collector->finish_nursery_collection = major_finish_nursery_collection;
2106 collector->start_major_collection = major_start_major_collection;
2107 collector->finish_major_collection = major_finish_major_collection;
2108 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2109 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2110 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2111 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2112 collector->get_num_major_sections = get_num_major_sections;
2113 collector->handle_gc_param = major_handle_gc_param;
2114 collector->print_gc_param_usage = major_print_gc_param_usage;
2115 collector->post_param_init = post_param_init;
2116 collector->is_valid_object = major_is_valid_object;
2117 collector->describe_pointer = major_describe_pointer;
2118 collector->count_cards = major_count_cards;
2120 collector->major_ops.copy_or_mark_object = major_copy_or_mark_object_canonical;
2121 collector->major_ops.scan_object = major_scan_object;
2122 #ifdef SGEN_HAVE_CONCURRENT_MARK
2123 if (is_concurrent) {
2124 collector->major_concurrent_ops.copy_or_mark_object = major_copy_or_mark_object_concurrent_canonical;
2125 collector->major_concurrent_ops.scan_object = major_scan_object_concurrent;
2126 collector->major_concurrent_ops.scan_vtype = major_scan_vtype_concurrent;
2130 /*cardtable requires major pages to be 8 cards aligned*/
2131 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
2134 #ifdef SGEN_HAVE_CONCURRENT_MARK
2136 sgen_marksweep_init (SgenMajorCollector *collector)
2138 sgen_marksweep_init_internal (collector, FALSE);
2142 sgen_marksweep_conc_init (SgenMajorCollector *collector)
2144 sgen_marksweep_init_internal (collector, TRUE);