2 * sgen-marksweep.c: Simple generational GC.
5 * Mark Probst <mark.probst@gmail.com>
7 * Copyright 2009-2010 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 #include "utils/mono-counters.h"
37 #include "utils/mono-semaphore.h"
38 #include "utils/mono-time.h"
39 #include "metadata/object-internals.h"
40 #include "metadata/profiler-private.h"
42 #include "metadata/sgen-gc.h"
43 #include "metadata/sgen-protocol.h"
44 #include "metadata/sgen-cardtable.h"
45 #include "metadata/sgen-memory-governor.h"
46 #include "metadata/gc-internal.h"
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 #define MS_DEFAULT_HEAP_NUM_BLOCKS (32 * 1024) /* 512 MB */
58 * Don't allocate single blocks, but alloc a contingent of this many
59 * blocks in one swoop.
61 #define MS_BLOCK_ALLOC_NUM 32
64 * Number of bytes before the first object in a block. At the start
65 * of a block is the MSBlockHeader, then opional padding, then come
66 * the objects, so this must be >= sizeof (MSBlockHeader).
69 #define MS_BLOCK_SKIP 0
71 #define MS_BLOCK_SKIP 16
74 #define MS_BLOCK_FREE (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
76 #define MS_NUM_MARK_WORDS ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
78 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
79 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
82 typedef struct _MSBlockInfo MSBlockInfo;
86 int pin_queue_num_entries;
87 unsigned int pinned : 1;
88 unsigned int has_references : 1;
89 unsigned int has_pinned : 1; /* means cannot evacuate */
90 unsigned int is_to_space : 1;
92 unsigned int used : 1;
93 unsigned int zeroed : 1;
98 MSBlockInfo *next_free;
99 void **pin_queue_start;
100 mword mark_words [MS_NUM_MARK_WORDS];
104 static mword ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
106 static char *ms_heap_start;
107 static char *ms_heap_end;
109 #define MS_PTR_IN_SMALL_MAJOR_HEAP(p) ((char*)(p) >= ms_heap_start && (char*)(p) < ms_heap_end)
111 /* array of all all block infos in the system */
112 static MSBlockInfo *block_infos;
115 #define MS_BLOCK_OBJ(b,i) ((b)->block + MS_BLOCK_SKIP + (b)->obj_size * (i))
116 #define MS_BLOCK_DATA_FOR_OBJ(o) ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
119 #define MS_BLOCK_FOR_OBJ(o) (&block_infos [(mword)((char*)(o) - ms_heap_start) >> MS_BLOCK_SIZE_SHIFT])
125 #define MS_BLOCK_FOR_OBJ(o) (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
128 #define MS_BLOCK_OBJ_INDEX(o,b) (((char*)(o) - ((b)->block + MS_BLOCK_SKIP)) / (b)->obj_size)
130 #define MS_CALC_MARK_BIT(w,b,o) do { \
131 int i = ((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o))) >> SGEN_ALLOC_ALIGN_BITS; \
132 if (sizeof (mword) == 4) { \
141 #define MS_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] & (1L << (b)))
142 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (1L << (b)))
143 #define MS_PAR_SET_MARK_BIT(was_marked,bl,w,b) do { \
144 mword __old = (bl)->mark_words [(w)]; \
145 mword __bitmask = 1L << (b); \
146 if (__old & __bitmask) { \
150 if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)], \
151 (gpointer)(__old | __bitmask), \
152 (gpointer)__old) == \
154 was_marked = FALSE; \
159 #define MS_OBJ_ALLOCED(o,b) (*(void**)(o) && (*(char**)(o) < (b)->block || *(char**)(o) >= (b)->block + MS_BLOCK_SIZE))
161 #define MS_BLOCK_OBJ_SIZE_FACTOR (sqrt (2.0))
164 * This way we can lookup block object size indexes for sizes up to
165 * 256 bytes with a single load.
167 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES 32
169 static int *block_obj_sizes;
170 static int num_block_obj_sizes;
171 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
173 #define MS_BLOCK_FLAG_PINNED 1
174 #define MS_BLOCK_FLAG_REFS 2
176 #define MS_BLOCK_TYPE_MAX 4
178 #ifdef SGEN_PARALLEL_MARK
179 static LOCK_DECLARE (ms_block_list_mutex);
180 #define LOCK_MS_BLOCK_LIST mono_mutex_lock (&ms_block_list_mutex)
181 #define UNLOCK_MS_BLOCK_LIST mono_mutex_unlock (&ms_block_list_mutex)
184 static gboolean *evacuate_block_obj_sizes;
185 static float evacuation_threshold = 0.666;
187 static gboolean concurrent_sweep = FALSE;
188 static gboolean have_swept;
190 /* all allocated blocks in the system */
191 static MSBlockInfo *all_blocks;
194 /* non-allocated block free-list */
195 static MSBlockInfo *empty_blocks = NULL;
197 /* non-allocated block free-list */
198 static void *empty_blocks = NULL;
199 static int num_empty_blocks = 0;
202 #define FOREACH_BLOCK(bl) for ((bl) = all_blocks; (bl); (bl) = (bl)->next) {
203 #define END_FOREACH_BLOCK }
205 static int num_major_sections = 0;
206 /* one free block list for each block object size */
207 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
209 #ifdef SGEN_PARALLEL_MARK
210 #ifdef HAVE_KW_THREAD
211 static __thread MSBlockInfo ***workers_free_block_lists;
213 static MonoNativeTlsKey workers_free_block_lists_key;
217 static long long stat_major_blocks_alloced = 0;
218 static long long stat_major_blocks_freed = 0;
219 static long long stat_major_objects_evacuated = 0;
220 static long long stat_time_wait_for_sweep = 0;
222 static gboolean ms_sweep_in_progress = FALSE;
223 static MonoNativeThreadId ms_sweep_thread;
224 static MonoSemType ms_sweep_cmd_semaphore;
225 static MonoSemType ms_sweep_done_semaphore;
228 ms_signal_sweep_command (void)
230 if (!concurrent_sweep)
233 g_assert (!ms_sweep_in_progress);
234 ms_sweep_in_progress = TRUE;
235 MONO_SEM_POST (&ms_sweep_cmd_semaphore);
239 ms_signal_sweep_done (void)
241 if (!concurrent_sweep)
244 MONO_SEM_POST (&ms_sweep_done_semaphore);
248 ms_wait_for_sweep_done (void)
250 SGEN_TV_DECLARE (atv);
251 SGEN_TV_DECLARE (btv);
254 if (!concurrent_sweep)
257 if (!ms_sweep_in_progress)
260 SGEN_TV_GETTIME (atv);
261 while ((result = MONO_SEM_WAIT (&ms_sweep_done_semaphore)) != 0) {
263 g_error ("MONO_SEM_WAIT");
265 SGEN_TV_GETTIME (btv);
266 stat_time_wait_for_sweep += SGEN_TV_ELAPSED (atv, btv);
268 g_assert (ms_sweep_in_progress);
269 ms_sweep_in_progress = FALSE;
273 ms_find_block_obj_size_index (int size)
276 DEBUG (9, g_assert (size <= SGEN_MAX_SMALL_OBJ_SIZE));
277 for (i = 0; i < num_block_obj_sizes; ++i)
278 if (block_obj_sizes [i] >= size)
280 g_error ("no object of size %d\n", size);
283 #define FREE_BLOCKS_FROM(lists,p,r) (lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
284 #define FREE_BLOCKS(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
285 #ifdef SGEN_PARALLEL_MARK
286 #ifdef HAVE_KW_THREAD
287 #define FREE_BLOCKS_LOCAL(p,r) (FREE_BLOCKS_FROM (workers_free_block_lists, (p), (r)))
289 #define FREE_BLOCKS_LOCAL(p,r) (FREE_BLOCKS_FROM (((MSBlockInfo***)(mono_native_tls_get_value (workers_free_block_lists_key))), (p), (r)))
292 //#define FREE_BLOCKS_LOCAL(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
295 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
296 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
297 fast_block_obj_size_indexes [((s)+7)>>3] : \
298 ms_find_block_obj_size_index ((s)))
302 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
305 mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
306 mword alloc_size = nursery_size + major_heap_size;
309 g_assert (ms_heap_num_blocks > 0);
310 g_assert (nursery_size % MS_BLOCK_SIZE == 0);
312 g_assert (nursery_align % MS_BLOCK_SIZE == 0);
314 nursery_start = sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE, TRUE, "heap");
315 ms_heap_start = nursery_start + nursery_size;
316 ms_heap_end = ms_heap_start + major_heap_size;
318 block_infos = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO, TRUE);
320 for (i = 0; i < ms_heap_num_blocks; ++i) {
321 block_infos [i].block = ms_heap_start + i * MS_BLOCK_SIZE;
322 if (i < ms_heap_num_blocks - 1)
323 block_infos [i].next_free = &block_infos [i + 1];
325 block_infos [i].next_free = NULL;
326 block_infos [i].zeroed = TRUE;
329 empty_blocks = &block_infos [0];
331 return nursery_start;
335 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
339 start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE, TRUE, "nursery");
341 start = sgen_alloc_os_memory (nursery_size, TRUE, TRUE, "nursery");
348 update_heap_boundaries_for_block (MSBlockInfo *block)
350 sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
355 ms_get_empty_block (void)
359 g_assert (empty_blocks);
362 block = empty_blocks;
363 } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block->next_free, block) != block);
368 memset (block->block, 0, MS_BLOCK_SIZE);
374 ms_free_block (MSBlockInfo *block)
376 block->next_free = empty_blocks;
377 empty_blocks = block;
379 block->zeroed = FALSE;
380 sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
384 ms_get_empty_block (void)
388 void *block, *empty, *next;
392 p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE, TRUE, "major heap section");
394 for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
397 * We do the free list update one after the
398 * other so that other threads can use the new
399 * blocks as quickly as possible.
402 empty = empty_blocks;
403 *(void**)block = empty;
404 } while (SGEN_CAS_PTR ((gpointer*)&empty_blocks, block, empty) != empty);
408 SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
410 stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
414 empty = empty_blocks;
418 next = *(void**)block;
419 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
421 SGEN_ATOMIC_ADD (num_empty_blocks, -1);
423 *(void**)block = NULL;
425 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
431 ms_free_block (void *block)
435 sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
436 memset (block, 0, MS_BLOCK_SIZE);
439 empty = empty_blocks;
440 *(void**)block = empty;
441 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
443 SGEN_ATOMIC_ADD (num_empty_blocks, 1);
447 //#define MARKSWEEP_CONSISTENCY_CHECK
449 #ifdef MARKSWEEP_CONSISTENCY_CHECK
451 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
455 for (; block; block = block->next_free) {
456 g_assert (block->obj_size == size);
457 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
459 /* blocks in the free lists must have at least
461 g_assert (block->free_list);
464 /* the block must not be in the empty_blocks list */
465 for (b = empty_blocks; b; b = b->next_free)
466 g_assert (b != block);
468 /* the block must be in the all_blocks list */
469 for (b = all_blocks; b; b = b->next) {
473 g_assert (b == block);
478 check_empty_blocks (void)
483 for (p = empty_blocks; p; p = *(void**)p)
485 g_assert (i == num_empty_blocks);
490 consistency_check (void)
495 /* check all blocks */
496 FOREACH_BLOCK (block) {
497 int count = MS_BLOCK_FREE / block->obj_size;
502 /* check block header */
503 g_assert (((MSBlockHeader*)block->block)->info == block);
506 /* count number of free slots */
507 for (i = 0; i < count; ++i) {
508 void **obj = (void**) MS_BLOCK_OBJ (block, i);
509 if (!MS_OBJ_ALLOCED (obj, block))
513 /* check free list */
514 for (free = block->free_list; free; free = (void**)*free) {
515 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
518 g_assert (num_free == 0);
520 /* check all mark words are zero */
521 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
522 g_assert (block->mark_words [i] == 0);
525 /* check free blocks */
526 for (i = 0; i < num_block_obj_sizes; ++i) {
528 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
529 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
532 check_empty_blocks ();
537 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
539 int size = block_obj_sizes [size_index];
540 int count = MS_BLOCK_FREE / size;
542 #ifdef SGEN_PARALLEL_MARK
546 MSBlockHeader *header;
548 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
552 if (!sgen_memgov_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
556 info = ms_get_empty_block ();
558 info = sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
561 DEBUG (9, g_assert (count >= 2));
563 info->obj_size = size;
564 info->obj_size_index = size_index;
565 info->pinned = pinned;
566 info->has_references = has_references;
567 info->has_pinned = pinned;
568 info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD); /*FIXME WHY??? */
570 info->block = ms_get_empty_block ();
572 header = (MSBlockHeader*) info->block;
576 update_heap_boundaries_for_block (info);
578 /* build free list */
579 obj_start = info->block + MS_BLOCK_SKIP;
580 info->free_list = (void**)obj_start;
581 /* we're skipping the last one - it must be nulled */
582 for (i = 0; i < count - 1; ++i) {
583 char *next_obj_start = obj_start + size;
584 *(void**)obj_start = next_obj_start;
585 obj_start = next_obj_start;
588 *(void**)obj_start = NULL;
590 #ifdef SGEN_PARALLEL_MARK
592 next = info->next_free = free_blocks [size_index];
593 } while (SGEN_CAS_PTR ((void**)&free_blocks [size_index], info, next) != next);
596 next = info->next = all_blocks;
597 } while (SGEN_CAS_PTR ((void**)&all_blocks, info, next) != next);
599 info->next_free = free_blocks [size_index];
600 free_blocks [size_index] = info;
602 info->next = all_blocks;
606 ++num_major_sections;
611 obj_is_from_pinned_alloc (char *ptr)
615 FOREACH_BLOCK (block) {
616 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
617 return block->pinned;
623 unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_index)
628 block = free_blocks [size_index];
629 DEBUG (9, g_assert (block));
631 obj = block->free_list;
632 DEBUG (9, g_assert (obj));
634 block->free_list = *(void**)obj;
635 if (!block->free_list) {
636 free_blocks [size_index] = block->next_free;
637 block->next_free = NULL;
643 #ifdef SGEN_PARALLEL_MARK
645 try_remove_block_from_free_list (MSBlockInfo *block, MSBlockInfo **free_blocks, int size_index)
648 * No more free slots in the block, so try to free the block.
649 * Don't try again if we don't succeed - another thread will
650 * already have done it.
652 MSBlockInfo *next_block = block->next_free;
653 if (SGEN_CAS_PTR ((void**)&free_blocks [size_index], next_block, block) == block) {
655 void *old = SGEN_CAS_PTR ((void**)&block->next_free, NULL, next_block);
656 g_assert (old == next_block);
658 block->next_free = NULL;
665 alloc_obj_par (int size, gboolean pinned, gboolean has_references)
667 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
668 MSBlockInfo **free_blocks_local = FREE_BLOCKS_LOCAL (pinned, has_references);
672 DEBUG (9, g_assert (!ms_sweep_in_progress));
673 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
675 if (free_blocks_local [size_index]) {
677 obj = unlink_slot_from_free_list_uncontested (free_blocks_local, size_index);
679 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
682 block = free_blocks [size_index];
684 if (!try_remove_block_from_free_list (block, free_blocks, size_index))
687 g_assert (block->next_free == NULL);
688 g_assert (block->free_list);
689 block->next_free = free_blocks_local [size_index];
690 free_blocks_local [size_index] = block;
697 success = ms_alloc_block (size_index, pinned, has_references);
698 UNLOCK_MS_BLOCK_LIST;
700 if (G_UNLIKELY (!success))
708 * FIXME: This should not be necessary because it'll be
709 * overwritten by the vtable immediately.
717 major_par_alloc_object (int size, gboolean has_references)
719 return alloc_obj_par (size, FALSE, has_references);
724 alloc_obj (int size, gboolean pinned, gboolean has_references)
726 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
727 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
730 #ifdef SGEN_PARALLEL_MARK
731 DEBUG (9, g_assert (current_collection_generation != GENERATION_OLD));
734 DEBUG (9, g_assert (!ms_sweep_in_progress));
736 if (!free_blocks [size_index]) {
737 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
741 obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
744 * FIXME: This should not be necessary because it'll be
745 * overwritten by the vtable immediately.
753 major_alloc_object (int size, gboolean has_references)
755 return alloc_obj (size, FALSE, has_references);
759 * We're not freeing the block if it's empty. We leave that work for
760 * the next major collection.
762 * This is just called from the domain clearing code, which runs in a
763 * single thread and has the GC lock, so we don't need an extra lock.
766 free_object (char *obj, size_t size, gboolean pinned)
768 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
770 DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
771 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
772 MS_CALC_MARK_BIT (word, bit, obj);
773 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
774 if (!block->free_list) {
775 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
776 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
777 DEBUG (9, g_assert (!block->next_free));
778 block->next_free = free_blocks [size_index];
779 free_blocks [size_index] = block;
781 memset (obj, 0, size);
782 *(void**)obj = block->free_list;
783 block->free_list = (void**)obj;
787 major_free_non_pinned_object (char *obj, size_t size)
789 free_object (obj, size, FALSE);
792 /* size is a multiple of SGEN_ALLOC_ALIGN */
794 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
798 ms_wait_for_sweep_done ();
800 res = alloc_obj (size, TRUE, has_references);
801 /*If we failed to alloc memory, we better try releasing memory
802 *as pinned alloc is requested by the runtime.
805 sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure");
806 res = alloc_obj (size, TRUE, has_references);
812 free_pinned_object (char *obj, size_t size)
814 free_object (obj, size, TRUE);
818 * size is already rounded up and we hold the GC lock.
821 major_alloc_degraded (MonoVTable *vtable, size_t size)
824 int old_num_sections;
826 ms_wait_for_sweep_done ();
828 old_num_sections = num_major_sections;
830 obj = alloc_obj (size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
831 if (G_LIKELY (obj)) {
832 *(MonoVTable**)obj = vtable;
833 HEAVY_STAT (++stat_objects_alloced_degraded);
834 HEAVY_STAT (stat_bytes_alloced_degraded += size);
835 g_assert (num_major_sections >= old_num_sections);
836 sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
841 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj) FALSE
844 * obj is some object. If it's not in the major heap (i.e. if it's in
845 * the nursery or LOS), return FALSE. Otherwise return whether it's
846 * been marked or copied.
849 major_is_object_live (char *obj)
857 if (sgen_ptr_in_nursery (obj))
862 if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
865 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
868 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
872 /* now we know it's in a major block */
873 block = MS_BLOCK_FOR_OBJ (obj);
874 DEBUG (9, g_assert (!block->pinned));
875 MS_CALC_MARK_BIT (word, bit, obj);
876 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
880 major_ptr_is_in_non_pinned_space (char *ptr)
884 FOREACH_BLOCK (block) {
885 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
886 return !block->pinned;
892 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
896 ms_wait_for_sweep_done ();
898 FOREACH_BLOCK (block) {
899 int count = MS_BLOCK_FREE / block->obj_size;
902 if (block->pinned && !pinned)
904 if (!block->pinned && !non_pinned)
907 for (i = 0; i < count; ++i) {
908 void **obj = (void**) MS_BLOCK_OBJ (block, i);
909 if (MS_OBJ_ALLOCED (obj, block))
910 callback ((char*)obj, block->obj_size, data);
916 major_is_valid_object (char *object)
920 ms_wait_for_sweep_done ();
921 FOREACH_BLOCK (block) {
925 if ((block->block > object) || ((block->block + MS_BLOCK_SIZE) <= object))
928 idx = MS_BLOCK_OBJ_INDEX (object, block);
929 obj = (char*)MS_BLOCK_OBJ (block, idx);
932 return MS_OBJ_ALLOCED (obj, block);
940 major_describe_pointer (char *ptr)
944 FOREACH_BLOCK (block) {
950 if ((block->block > ptr) || ((block->block + MS_BLOCK_SIZE) <= ptr))
953 fprintf (gc_debug_file, "major-ptr (block %p sz %d pin %d ref %d) ",
954 block->block, block->obj_size, block->pinned, block->has_references);
956 idx = MS_BLOCK_OBJ_INDEX (ptr, block);
957 obj = (char*)MS_BLOCK_OBJ (block, idx);
958 live = MS_OBJ_ALLOCED (obj, block);
959 vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
963 fprintf (gc_debug_file, "(object %s.%s)", vtable->klass->name_space, vtable->klass->name);
965 fprintf (gc_debug_file, "(dead-object)");
968 fprintf (gc_debug_file, "(interior-ptr offset %td of %p %s.%s)",
970 obj, vtable->klass->name_space, vtable->klass->name);
972 fprintf (gc_debug_file, "(dead-interior-ptr to %td to %p)",
983 major_check_scan_starts (void)
988 major_dump_heap (FILE *heap_dump_file)
991 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
992 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
995 for (i = 0; i < num_block_obj_sizes; ++i)
996 slots_available [i] = slots_used [i] = 0;
998 FOREACH_BLOCK (block) {
999 int index = ms_find_block_obj_size_index (block->obj_size);
1000 int count = MS_BLOCK_FREE / block->obj_size;
1002 slots_available [index] += count;
1003 for (i = 0; i < count; ++i) {
1004 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
1005 ++slots_used [index];
1007 } END_FOREACH_BLOCK;
1009 fprintf (heap_dump_file, "<occupancies>\n");
1010 for (i = 0; i < num_block_obj_sizes; ++i) {
1011 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
1012 block_obj_sizes [i], slots_available [i], slots_used [i]);
1014 fprintf (heap_dump_file, "</occupancies>\n");
1016 FOREACH_BLOCK (block) {
1017 int count = MS_BLOCK_FREE / block->obj_size;
1021 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
1023 for (i = 0; i <= count; ++i) {
1024 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
1029 sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
1035 fprintf (heap_dump_file, "</section>\n");
1036 } END_FOREACH_BLOCK;
1039 #define LOAD_VTABLE SGEN_LOAD_VTABLE
1041 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do { \
1042 int __word, __bit; \
1043 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
1044 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
1045 MS_SET_MARK_BIT ((block), __word, __bit); \
1046 if ((block)->has_references) \
1047 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
1048 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1051 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
1052 int __word, __bit; \
1053 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
1054 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
1055 if (!MS_MARK_BIT ((block), __word, __bit)) { \
1056 MS_SET_MARK_BIT ((block), __word, __bit); \
1057 if ((block)->has_references) \
1058 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
1059 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1062 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
1063 int __word, __bit; \
1064 gboolean __was_marked; \
1065 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
1066 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
1067 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
1068 if (!__was_marked) { \
1069 if ((block)->has_references) \
1070 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
1071 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), sgen_safe_object_get_size ((MonoObject*)(obj))); \
1076 pin_major_object (char *obj, SgenGrayQueue *queue)
1078 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
1079 block->has_pinned = TRUE;
1080 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1083 #include "sgen-major-copy-object.h"
1085 #ifdef SGEN_PARALLEL_MARK
1087 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1094 HEAVY_STAT (++stat_copy_object_called_major);
1096 DEBUG (9, g_assert (obj));
1097 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
1099 if (sgen_ptr_in_nursery (obj)) {
1101 gboolean has_references;
1103 mword vtable_word = *(mword*)obj;
1104 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1106 if (vtable_word & SGEN_FORWARDED_BIT) {
1111 if (vtable_word & SGEN_PINNED_BIT)
1114 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
1115 if (sgen_nursery_is_to_space (obj))
1118 HEAVY_STAT (++stat_objects_copied_major);
1121 objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
1122 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
1124 destination = sgen_minor_collector.par_alloc_for_promotion (obj, objsize, has_references);
1125 if (G_UNLIKELY (!destination)) {
1126 if (!sgen_ptr_in_nursery (obj)) {
1128 block = MS_BLOCK_FOR_OBJ (obj);
1129 size_index = block->obj_size_index;
1130 evacuate_block_obj_sizes [size_index] = FALSE;
1133 sgen_parallel_pin_or_update (ptr, obj, vt, queue);
1134 sgen_set_pinned_from_failed_allocation (objsize);
1139 * We do this before the CAS because we want to make
1140 * sure that if another thread sees the destination
1141 * pointer the VTable is already in place. Not doing
1142 * this can crash binary protocols.
1144 *(MonoVTable**)destination = vt;
1146 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
1147 gboolean was_marked;
1149 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
1154 * FIXME: If we make major_alloc_object() give
1155 * us the block info, too, we won't have to
1158 * FIXME (2): We should rework this to avoid all those nursery checks.
1161 * For the split nursery allocator the object
1162 * might still be in the nursery despite
1163 * having being promoted, in which case we
1166 if (!sgen_ptr_in_nursery (obj)) {
1167 block = MS_BLOCK_FOR_OBJ (obj);
1168 MS_CALC_MARK_BIT (word, bit, obj);
1169 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
1170 MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
1174 * FIXME: We have allocated destination, but
1175 * we cannot use it. Give it back to the
1178 *(void**)destination = NULL;
1180 vtable_word = *(mword*)obj;
1181 g_assert (vtable_word & SGEN_FORWARDED_BIT);
1183 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1187 HEAVY_STAT (++stat_slots_allocated_in_vain);
1191 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1193 mword vtable_word = *(mword*)obj;
1194 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1196 /* see comment in the non-parallel version below */
1197 if (vtable_word & SGEN_FORWARDED_BIT) {
1201 objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
1203 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1208 block = MS_BLOCK_FOR_OBJ (obj);
1209 size_index = block->obj_size_index;
1211 if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1212 if (block->is_to_space)
1217 mword vtable_word = *(mword*)obj;
1218 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1220 if (vtable_word & SGEN_FORWARDED_BIT) {
1227 HEAVY_STAT (++stat_major_objects_evacuated);
1228 goto do_copy_object;
1231 MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1234 mword vtable_word = *(mword*)obj;
1235 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1238 if (vtable_word & SGEN_PINNED_BIT)
1240 binary_protocol_pin (obj, vt, sgen_safe_object_get_size ((MonoObject*)obj));
1241 if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
1242 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1243 GRAY_OBJECT_ENQUEUE (queue, obj);
1245 g_assert (SGEN_OBJECT_IS_PINNED (obj));
1252 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1257 HEAVY_STAT (++stat_copy_object_called_major);
1259 DEBUG (9, g_assert (obj));
1260 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
1262 if (sgen_ptr_in_nursery (obj)) {
1264 char *forwarded, *old_obj;
1266 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1270 if (SGEN_OBJECT_IS_PINNED (obj))
1273 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
1274 if (sgen_nursery_is_to_space (obj))
1277 HEAVY_STAT (++stat_objects_copied_major);
1281 obj = copy_object_no_checks (obj, queue);
1282 if (G_UNLIKELY (old_obj == obj)) {
1283 /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1284 if (!sgen_ptr_in_nursery (obj)) {
1286 block = MS_BLOCK_FOR_OBJ (obj);
1287 size_index = block->obj_size_index;
1288 evacuate_block_obj_sizes [size_index] = FALSE;
1289 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1296 * FIXME: See comment for copy_object_no_checks(). If
1297 * we have that, we can let the allocation function
1298 * give us the block info, too, and we won't have to
1301 * FIXME (2): We should rework this to avoid all those nursery checks.
1304 * For the split nursery allocator the object might
1305 * still be in the nursery despite having being
1306 * promoted, in which case we can't mark it.
1308 if (!sgen_ptr_in_nursery (obj)) {
1309 block = MS_BLOCK_FOR_OBJ (obj);
1310 MS_CALC_MARK_BIT (word, bit, obj);
1311 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
1312 MS_SET_MARK_BIT (block, word, bit);
1317 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1322 * If we have don't have a fixed heap we cannot know
1323 * whether an object is in the LOS or in the small
1324 * object major heap without checking its size. To do
1325 * that, however, we need to know that we actually
1326 * have a valid object, not a forwarding pointer, so
1327 * we have to do this check first.
1329 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1334 objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
1336 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1342 block = MS_BLOCK_FOR_OBJ (obj);
1343 size_index = block->obj_size_index;
1344 evacuate = evacuate_block_obj_sizes [size_index];
1348 * We could also check for !block->has_pinned
1349 * here, but it would only make an uncommon case
1350 * faster, namely objects that are in blocks
1351 * whose slot sizes are evacuated but which have
1354 if (evacuate && (forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1360 if (evacuate && !block->has_pinned) {
1361 g_assert (!SGEN_OBJECT_IS_PINNED (obj));
1362 if (block->is_to_space)
1364 HEAVY_STAT (++stat_major_objects_evacuated);
1365 goto do_copy_object;
1367 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1370 if (SGEN_OBJECT_IS_PINNED (obj))
1372 binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
1373 if (MONO_GC_OBJ_PINNED_ENABLED ()) {
1374 MONO_GC_OBJ_PINNED (obj, sgen_safe_object_get_size (obj), NULL, GENERATION_OLD);
1376 SGEN_PIN_OBJECT (obj);
1377 /* FIXME: only enqueue if object has references */
1378 GRAY_OBJECT_ENQUEUE (queue, obj);
1384 #include "sgen-major-scan-object.h"
1387 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1390 int last_index = -1;
1392 if (!block->pin_queue_num_entries)
1395 block->has_pinned = TRUE;
1397 for (i = 0; i < block->pin_queue_num_entries; ++i) {
1398 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1399 DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
1400 if (index == last_index)
1402 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1413 /* statistics for evacuation */
1414 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1415 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1416 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1418 for (i = 0; i < num_block_obj_sizes; ++i)
1419 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1421 /* clear all the free lists */
1422 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1423 MSBlockInfo **free_blocks = free_block_lists [i];
1425 for (j = 0; j < num_block_obj_sizes; ++j)
1426 free_blocks [j] = NULL;
1429 /* traverse all blocks, free and zero unmarked objects */
1432 MSBlockInfo *block = *iter;
1434 gboolean have_live = FALSE;
1435 gboolean has_pinned;
1439 obj_size_index = block->obj_size_index;
1441 has_pinned = block->has_pinned;
1442 block->has_pinned = block->pinned;
1444 block->is_to_space = FALSE;
1446 count = MS_BLOCK_FREE / block->obj_size;
1447 block->free_list = NULL;
1449 for (obj_index = 0; obj_index < count; ++obj_index) {
1451 void *obj = MS_BLOCK_OBJ (block, obj_index);
1453 MS_CALC_MARK_BIT (word, bit, obj);
1454 if (MS_MARK_BIT (block, word, bit)) {
1455 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
1458 ++slots_used [obj_size_index];
1460 /* an unmarked object */
1461 if (MS_OBJ_ALLOCED (obj, block)) {
1463 * FIXME: Merge consecutive
1464 * slots for lower reporting
1465 * overhead. Maybe memset
1466 * will also benefit?
1468 binary_protocol_empty (obj, block->obj_size);
1469 MONO_GC_MAJOR_SWEEPED (obj, block->obj_size);
1470 memset (obj, 0, block->obj_size);
1472 *(void**)obj = block->free_list;
1473 block->free_list = obj;
1477 /* reset mark bits */
1478 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1481 * FIXME: reverse free list so that it's in address
1487 ++num_blocks [obj_size_index];
1488 slots_available [obj_size_index] += count;
1491 iter = &block->next;
1494 * If there are free slots in the block, add
1495 * the block to the corresponding free list.
1497 if (block->free_list) {
1498 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1499 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1500 block->next_free = free_blocks [index];
1501 free_blocks [index] = block;
1504 update_heap_boundaries_for_block (block);
1507 * Blocks without live objects are removed from the
1508 * block list and freed.
1510 *iter = block->next;
1513 ms_free_block (block);
1515 ms_free_block (block->block);
1517 sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1520 --num_major_sections;
1524 for (i = 0; i < num_block_obj_sizes; ++i) {
1525 float usage = (float)slots_used [i] / (float)slots_available [i];
1526 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1527 evacuate_block_obj_sizes [i] = TRUE;
1529 g_print ("slot size %d - %d of %d used\n",
1530 block_obj_sizes [i], slots_used [i], slots_available [i]);
1533 evacuate_block_obj_sizes [i] = FALSE;
1540 static mono_native_thread_return_t
1541 ms_sweep_thread_func (void *dummy)
1543 g_assert (concurrent_sweep);
1548 while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
1550 g_error ("MONO_SEM_WAIT FAILED with %d errno %d (%s)", result, errno, strerror (errno));
1555 ms_signal_sweep_done ();
1564 if (concurrent_sweep) {
1565 g_assert (ms_sweep_thread);
1566 ms_signal_sweep_command ();
1572 static int count_pinned_ref;
1573 static int count_pinned_nonref;
1574 static int count_nonpinned_ref;
1575 static int count_nonpinned_nonref;
1578 count_nonpinned_callback (char *obj, size_t size, void *data)
1580 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1582 if (vtable->klass->has_references)
1583 ++count_nonpinned_ref;
1585 ++count_nonpinned_nonref;
1589 count_pinned_callback (char *obj, size_t size, void *data)
1591 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1593 if (vtable->klass->has_references)
1596 ++count_pinned_nonref;
1599 static void __attribute__ ((unused))
1600 count_ref_nonref_objs (void)
1604 count_pinned_ref = 0;
1605 count_pinned_nonref = 0;
1606 count_nonpinned_ref = 0;
1607 count_nonpinned_nonref = 0;
1609 major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1610 major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1612 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1614 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1615 count_pinned_ref, count_nonpinned_ref,
1616 count_pinned_nonref, count_nonpinned_nonref,
1617 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1621 ms_calculate_block_obj_sizes (double factor, int *arr)
1623 double target_size = sizeof (MonoObject);
1628 int target_count = ceil (MS_BLOCK_FREE / target_size);
1629 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1631 if (size != last_size) {
1633 arr [num_sizes] = size;
1638 target_size *= factor;
1639 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1644 /* only valid during minor collections */
1645 static int old_num_major_sections;
1648 major_start_nursery_collection (void)
1650 ms_wait_for_sweep_done ();
1652 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1653 consistency_check ();
1656 old_num_major_sections = num_major_sections;
1660 major_finish_nursery_collection (void)
1662 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1663 consistency_check ();
1665 sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1669 major_start_major_collection (void)
1673 ms_wait_for_sweep_done ();
1675 /* clear the free lists */
1676 for (i = 0; i < num_block_obj_sizes; ++i) {
1677 if (!evacuate_block_obj_sizes [i])
1680 free_block_lists [0][i] = NULL;
1681 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1686 major_finish_major_collection (void)
1691 major_have_computer_minor_collection_allowance (void)
1694 int section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1696 g_assert (have_swept);
1697 ms_wait_for_sweep_done ();
1698 g_assert (!ms_sweep_in_progress);
1701 * FIXME: We don't free blocks on 32 bit platforms because it
1702 * can lead to address space fragmentation, since we're
1703 * allocating blocks in larger contingents.
1705 if (sizeof (mword) < 8)
1708 while (num_empty_blocks > section_reserve) {
1709 void *next = *(void**)empty_blocks;
1710 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, TRUE);
1711 empty_blocks = next;
1713 * Needs not be atomic because this is running
1718 ++stat_major_blocks_freed;
1724 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1728 FOREACH_BLOCK (block) {
1729 block->pin_queue_start = sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
1730 &block->pin_queue_num_entries);
1731 } END_FOREACH_BLOCK;
1735 major_pin_objects (SgenGrayQueue *queue)
1739 FOREACH_BLOCK (block) {
1740 mark_pinned_objects_in_block (block, queue);
1741 } END_FOREACH_BLOCK;
1745 major_init_to_space (void)
1750 major_report_pinned_memory_usage (void)
1752 g_assert_not_reached ();
1756 major_get_used_size (void)
1761 FOREACH_BLOCK (block) {
1762 int count = MS_BLOCK_FREE / block->obj_size;
1764 size += count * block->obj_size;
1765 for (iter = block->free_list; iter; iter = (void**)*iter)
1766 size -= block->obj_size;
1767 } END_FOREACH_BLOCK;
1773 get_num_major_sections (void)
1775 return num_major_sections;
1779 major_handle_gc_param (const char *opt)
1782 if (g_str_has_prefix (opt, "major-heap-size=")) {
1783 const char *arg = strchr (opt, '=') + 1;
1785 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
1787 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1788 g_assert (ms_heap_num_blocks > 0);
1792 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1793 const char *arg = strchr (opt, '=') + 1;
1794 int percentage = atoi (arg);
1795 if (percentage < 0 || percentage > 100) {
1796 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1799 evacuation_threshold = (float)percentage / 100.0;
1801 } else if (!strcmp (opt, "concurrent-sweep")) {
1802 concurrent_sweep = TRUE;
1804 } else if (!strcmp (opt, "no-concurrent-sweep")) {
1805 concurrent_sweep = FALSE;
1813 major_print_gc_param_usage (void)
1818 " major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1820 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1821 " (no-)concurrent-sweep\n"
1825 #ifdef SGEN_HAVE_CARDTABLE
1827 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1831 FOREACH_BLOCK (block) {
1832 if (block->has_references)
1833 callback ((mword)block->block, MS_BLOCK_SIZE);
1834 } END_FOREACH_BLOCK;
1837 #ifdef HEAVY_STATISTICS
1838 extern long long marked_cards;
1839 extern long long scanned_cards;
1840 extern long long scanned_objects;
1841 extern long long remarked_cards;
1844 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1846 * MS blocks are 16K aligned.
1847 * Cardtables are 4K aligned, at least.
1848 * This means that the cardtable of a given block is 32 bytes aligned.
1851 initial_skip_card (guint8 *card_data)
1853 mword *cards = (mword*)card_data;
1856 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1862 if (i == CARD_WORDS_PER_BLOCK)
1863 return card_data + CARDS_PER_BLOCK;
1865 #if defined(__i386__) && defined(__GNUC__)
1866 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1867 #elif defined(__x86_64__) && defined(__GNUC__)
1868 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1869 #elif defined(__s390x__) && defined(__GNUC__)
1870 return card_data + i * 8 + (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
1872 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1874 return &card_data [i];
1881 static G_GNUC_UNUSED guint8*
1882 skip_card (guint8 *card_data, guint8 *card_data_end)
1884 while (card_data < card_data_end && !*card_data)
1889 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1890 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1891 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1894 major_scan_card_table (SgenGrayQueue *queue)
1897 ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
1899 FOREACH_BLOCK (block) {
1903 if (!block->has_references)
1906 block_obj_size = block->obj_size;
1907 block_start = block->block;
1909 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1911 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1912 guint8 cards_data [CARDS_PER_BLOCK];
1914 char *obj, *end, *base;
1916 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1917 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1918 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1921 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1925 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1926 end = block_start + MS_BLOCK_SIZE;
1927 base = sgen_card_table_align_pointer (obj);
1930 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1931 int card_offset = (obj - base) >> CARD_BITS;
1932 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
1934 obj += block_obj_size;
1937 guint8 *card_data, *card_base;
1938 guint8 *card_data_end;
1941 * This is safe in face of card aliasing for the following reason:
1943 * Major blocks are 16k aligned, or 32 cards aligned.
1944 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1945 * sizes, they won't overflow the cardtable overlap modulus.
1947 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1948 card_data_end = card_data + CARDS_PER_BLOCK;
1950 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)) {
1952 int idx = card_data - card_base;
1953 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1954 char *end = start + CARD_SIZE_IN_BYTES;
1957 HEAVY_STAT (++scanned_cards);
1962 HEAVY_STAT (++marked_cards);
1964 sgen_card_table_prepare_card_for_scanning (card_data);
1969 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1971 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1973 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1974 HEAVY_STAT (++scanned_objects);
1975 scan_func (obj, queue);
1977 obj += block_obj_size;
1979 HEAVY_STAT (if (*card_data) ++remarked_cards);
1982 } END_FOREACH_BLOCK;
1987 major_is_worker_thread (MonoNativeThreadId thread)
1989 if (concurrent_sweep)
1990 return thread == ms_sweep_thread;
1996 alloc_free_block_lists (MSBlockInfo ***lists)
1999 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
2000 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2003 #ifdef SGEN_PARALLEL_MARK
2005 major_alloc_worker_data (void)
2007 /* FIXME: free this when the workers come down */
2008 MSBlockInfo ***lists = malloc (sizeof (MSBlockInfo**) * MS_BLOCK_TYPE_MAX);
2009 alloc_free_block_lists (lists);
2014 major_init_worker_thread (void *data)
2016 MSBlockInfo ***lists = data;
2019 g_assert (lists && lists != free_block_lists);
2020 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2022 for (j = 0; j < num_block_obj_sizes; ++j)
2023 g_assert (!lists [i][j]);
2026 #ifdef HAVE_KW_THREAD
2027 workers_free_block_lists = data;
2029 mono_native_tls_set_value (workers_free_block_lists_key, data);
2034 major_reset_worker_data (void *data)
2036 MSBlockInfo ***lists = data;
2038 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2040 for (j = 0; j < num_block_obj_sizes; ++j)
2041 lists [i][j] = NULL;
2046 #undef pthread_create
2049 post_param_init (void)
2051 if (concurrent_sweep) {
2052 if (!mono_native_thread_create (&ms_sweep_thread, ms_sweep_thread_func, NULL)) {
2053 fprintf (stderr, "Error: Could not create sweep thread.\n");
2060 #ifdef SGEN_PARALLEL_MARK
2062 sgen_marksweep_fixed_par_init
2064 sgen_marksweep_par_init
2068 sgen_marksweep_fixed_init
2073 (SgenMajorCollector *collector)
2078 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
2081 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
2082 block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2083 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2085 evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
2086 for (i = 0; i < num_block_obj_sizes; ++i)
2087 evacuate_block_obj_sizes [i] = FALSE;
2092 g_print ("block object sizes:\n");
2093 for (i = 0; i < num_block_obj_sizes; ++i)
2094 g_print ("%d\n", block_obj_sizes [i]);
2098 alloc_free_block_lists (free_block_lists);
2100 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2101 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2102 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2103 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2105 #ifdef SGEN_PARALLEL_MARK
2106 LOCK_INIT (ms_block_list_mutex);
2109 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2110 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2111 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2112 mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &stat_time_wait_for_sweep);
2113 #ifdef SGEN_PARALLEL_MARK
2114 #ifndef HAVE_KW_THREAD
2115 mono_native_tls_alloc (&workers_free_block_lists_key, NULL);
2120 * FIXME: These are superfluous if concurrent sweep is
2121 * disabled. We might want to create them lazily.
2123 MONO_SEM_INIT (&ms_sweep_cmd_semaphore, 0);
2124 MONO_SEM_INIT (&ms_sweep_done_semaphore, 0);
2126 collector->section_size = MAJOR_SECTION_SIZE;
2127 #ifdef SGEN_PARALLEL_MARK
2128 collector->is_parallel = TRUE;
2129 collector->alloc_worker_data = major_alloc_worker_data;
2130 collector->init_worker_thread = major_init_worker_thread;
2131 collector->reset_worker_data = major_reset_worker_data;
2133 collector->is_parallel = FALSE;
2135 collector->supports_cardtable = TRUE;
2137 collector->have_swept = &have_swept;
2139 collector->alloc_heap = major_alloc_heap;
2140 collector->is_object_live = major_is_object_live;
2141 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2142 collector->alloc_degraded = major_alloc_degraded;
2144 collector->alloc_object = major_alloc_object;
2145 #ifdef SGEN_PARALLEL_MARK
2146 collector->par_alloc_object = major_par_alloc_object;
2148 collector->free_pinned_object = free_pinned_object;
2149 collector->iterate_objects = major_iterate_objects;
2150 collector->free_non_pinned_object = major_free_non_pinned_object;
2151 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2152 collector->pin_objects = major_pin_objects;
2153 collector->pin_major_object = pin_major_object;
2154 #ifdef SGEN_HAVE_CARDTABLE
2155 collector->scan_card_table = major_scan_card_table;
2156 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2158 collector->init_to_space = major_init_to_space;
2159 collector->sweep = major_sweep;
2160 collector->check_scan_starts = major_check_scan_starts;
2161 collector->dump_heap = major_dump_heap;
2162 collector->get_used_size = major_get_used_size;
2163 collector->start_nursery_collection = major_start_nursery_collection;
2164 collector->finish_nursery_collection = major_finish_nursery_collection;
2165 collector->start_major_collection = major_start_major_collection;
2166 collector->finish_major_collection = major_finish_major_collection;
2167 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2168 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2169 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2170 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2171 collector->get_num_major_sections = get_num_major_sections;
2172 collector->handle_gc_param = major_handle_gc_param;
2173 collector->print_gc_param_usage = major_print_gc_param_usage;
2174 collector->is_worker_thread = major_is_worker_thread;
2175 collector->post_param_init = post_param_init;
2176 collector->is_valid_object = major_is_valid_object;
2177 collector->describe_pointer = major_describe_pointer;
2179 collector->major_ops.copy_or_mark_object = major_copy_or_mark_object;
2180 collector->major_ops.scan_object = major_scan_object;
2182 #ifdef SGEN_HAVE_CARDTABLE
2183 /*cardtable requires major pages to be 8 cards aligned*/
2184 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);