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 int 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);
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);
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);
341 start = sgen_alloc_os_memory (nursery_size, TRUE);
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);
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 SGEN_PIN_OBJECT (obj);
1374 /* FIXME: only enqueue if object has references */
1375 GRAY_OBJECT_ENQUEUE (queue, obj);
1381 #include "sgen-major-scan-object.h"
1384 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1387 int last_index = -1;
1389 if (!block->pin_queue_num_entries)
1392 block->has_pinned = TRUE;
1394 for (i = 0; i < block->pin_queue_num_entries; ++i) {
1395 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1396 DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
1397 if (index == last_index)
1399 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1410 /* statistics for evacuation */
1411 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1412 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1413 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1415 for (i = 0; i < num_block_obj_sizes; ++i)
1416 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1418 /* clear all the free lists */
1419 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1420 MSBlockInfo **free_blocks = free_block_lists [i];
1422 for (j = 0; j < num_block_obj_sizes; ++j)
1423 free_blocks [j] = NULL;
1426 /* traverse all blocks, free and zero unmarked objects */
1429 MSBlockInfo *block = *iter;
1431 gboolean have_live = FALSE;
1432 gboolean has_pinned;
1436 obj_size_index = block->obj_size_index;
1438 has_pinned = block->has_pinned;
1439 block->has_pinned = block->pinned;
1441 block->is_to_space = FALSE;
1443 count = MS_BLOCK_FREE / block->obj_size;
1444 block->free_list = NULL;
1446 for (obj_index = 0; obj_index < count; ++obj_index) {
1448 void *obj = MS_BLOCK_OBJ (block, obj_index);
1450 MS_CALC_MARK_BIT (word, bit, obj);
1451 if (MS_MARK_BIT (block, word, bit)) {
1452 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
1455 ++slots_used [obj_size_index];
1457 /* an unmarked object */
1458 if (MS_OBJ_ALLOCED (obj, block)) {
1459 binary_protocol_empty (obj, block->obj_size);
1460 memset (obj, 0, block->obj_size);
1462 *(void**)obj = block->free_list;
1463 block->free_list = obj;
1467 /* reset mark bits */
1468 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1471 * FIXME: reverse free list so that it's in address
1477 ++num_blocks [obj_size_index];
1478 slots_available [obj_size_index] += count;
1481 iter = &block->next;
1484 * If there are free slots in the block, add
1485 * the block to the corresponding free list.
1487 if (block->free_list) {
1488 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1489 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1490 block->next_free = free_blocks [index];
1491 free_blocks [index] = block;
1494 update_heap_boundaries_for_block (block);
1497 * Blocks without live objects are removed from the
1498 * block list and freed.
1500 *iter = block->next;
1503 ms_free_block (block);
1505 ms_free_block (block->block);
1507 sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1510 --num_major_sections;
1514 for (i = 0; i < num_block_obj_sizes; ++i) {
1515 float usage = (float)slots_used [i] / (float)slots_available [i];
1516 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1517 evacuate_block_obj_sizes [i] = TRUE;
1519 g_print ("slot size %d - %d of %d used\n",
1520 block_obj_sizes [i], slots_used [i], slots_available [i]);
1523 evacuate_block_obj_sizes [i] = FALSE;
1530 static mono_native_thread_return_t
1531 ms_sweep_thread_func (void *dummy)
1533 g_assert (concurrent_sweep);
1538 while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
1540 g_error ("MONO_SEM_WAIT FAILED with %d errno %d (%s)", result, errno, strerror (errno));
1545 ms_signal_sweep_done ();
1554 if (concurrent_sweep) {
1555 g_assert (ms_sweep_thread);
1556 ms_signal_sweep_command ();
1562 static int count_pinned_ref;
1563 static int count_pinned_nonref;
1564 static int count_nonpinned_ref;
1565 static int count_nonpinned_nonref;
1568 count_nonpinned_callback (char *obj, size_t size, void *data)
1570 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1572 if (vtable->klass->has_references)
1573 ++count_nonpinned_ref;
1575 ++count_nonpinned_nonref;
1579 count_pinned_callback (char *obj, size_t size, void *data)
1581 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1583 if (vtable->klass->has_references)
1586 ++count_pinned_nonref;
1589 static void __attribute__ ((unused))
1590 count_ref_nonref_objs (void)
1594 count_pinned_ref = 0;
1595 count_pinned_nonref = 0;
1596 count_nonpinned_ref = 0;
1597 count_nonpinned_nonref = 0;
1599 major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1600 major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1602 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1604 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1605 count_pinned_ref, count_nonpinned_ref,
1606 count_pinned_nonref, count_nonpinned_nonref,
1607 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1611 ms_calculate_block_obj_sizes (double factor, int *arr)
1613 double target_size = sizeof (MonoObject);
1618 int target_count = ceil (MS_BLOCK_FREE / target_size);
1619 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1621 if (size != last_size) {
1623 arr [num_sizes] = size;
1628 target_size *= factor;
1629 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1634 /* only valid during minor collections */
1635 static int old_num_major_sections;
1638 major_start_nursery_collection (void)
1640 ms_wait_for_sweep_done ();
1642 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1643 consistency_check ();
1646 old_num_major_sections = num_major_sections;
1650 major_finish_nursery_collection (void)
1652 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1653 consistency_check ();
1655 sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1659 major_start_major_collection (void)
1663 ms_wait_for_sweep_done ();
1665 /* clear the free lists */
1666 for (i = 0; i < num_block_obj_sizes; ++i) {
1667 if (!evacuate_block_obj_sizes [i])
1670 free_block_lists [0][i] = NULL;
1671 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1676 major_finish_major_collection (void)
1681 major_have_computer_minor_collection_allowance (void)
1684 int section_reserve = sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1686 g_assert (have_swept);
1687 ms_wait_for_sweep_done ();
1688 g_assert (!ms_sweep_in_progress);
1691 * FIXME: We don't free blocks on 32 bit platforms because it
1692 * can lead to address space fragmentation, since we're
1693 * allocating blocks in larger contingents.
1695 if (sizeof (mword) < 8)
1698 while (num_empty_blocks > section_reserve) {
1699 void *next = *(void**)empty_blocks;
1700 sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
1701 empty_blocks = next;
1703 * Needs not be atomic because this is running
1708 ++stat_major_blocks_freed;
1714 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1718 FOREACH_BLOCK (block) {
1719 block->pin_queue_start = sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
1720 &block->pin_queue_num_entries);
1721 } END_FOREACH_BLOCK;
1725 major_pin_objects (SgenGrayQueue *queue)
1729 FOREACH_BLOCK (block) {
1730 mark_pinned_objects_in_block (block, queue);
1731 } END_FOREACH_BLOCK;
1735 major_init_to_space (void)
1740 major_report_pinned_memory_usage (void)
1742 g_assert_not_reached ();
1746 major_get_used_size (void)
1751 FOREACH_BLOCK (block) {
1752 int count = MS_BLOCK_FREE / block->obj_size;
1754 size += count * block->obj_size;
1755 for (iter = block->free_list; iter; iter = (void**)*iter)
1756 size -= block->obj_size;
1757 } END_FOREACH_BLOCK;
1763 get_num_major_sections (void)
1765 return num_major_sections;
1769 major_handle_gc_param (const char *opt)
1772 if (g_str_has_prefix (opt, "major-heap-size=")) {
1773 const char *arg = strchr (opt, '=') + 1;
1775 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
1777 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1778 g_assert (ms_heap_num_blocks > 0);
1782 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1783 const char *arg = strchr (opt, '=') + 1;
1784 int percentage = atoi (arg);
1785 if (percentage < 0 || percentage > 100) {
1786 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1789 evacuation_threshold = (float)percentage / 100.0;
1791 } else if (!strcmp (opt, "concurrent-sweep")) {
1792 concurrent_sweep = TRUE;
1794 } else if (!strcmp (opt, "no-concurrent-sweep")) {
1795 concurrent_sweep = FALSE;
1803 major_print_gc_param_usage (void)
1808 " major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1810 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1811 " (no-)concurrent-sweep\n"
1815 #ifdef SGEN_HAVE_CARDTABLE
1817 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1821 FOREACH_BLOCK (block) {
1822 if (block->has_references)
1823 callback ((mword)block->block, MS_BLOCK_SIZE);
1824 } END_FOREACH_BLOCK;
1827 #ifdef HEAVY_STATISTICS
1828 extern long long marked_cards;
1829 extern long long scanned_cards;
1830 extern long long scanned_objects;
1831 extern long long remarked_cards;
1834 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1836 * MS blocks are 16K aligned.
1837 * Cardtables are 4K aligned, at least.
1838 * This means that the cardtable of a given block is 32 bytes aligned.
1841 initial_skip_card (guint8 *card_data)
1843 mword *cards = (mword*)card_data;
1846 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1852 if (i == CARD_WORDS_PER_BLOCK)
1853 return card_data + CARDS_PER_BLOCK;
1855 #if defined(__i386__) && defined(__GNUC__)
1856 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1857 #elif defined(__x86_64__) && defined(__GNUC__)
1858 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1859 #elif defined(__s390x__) && defined(__GNUC__)
1860 return card_data + i * 8 + (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
1862 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1864 return &card_data [i];
1871 static G_GNUC_UNUSED guint8*
1872 skip_card (guint8 *card_data, guint8 *card_data_end)
1874 while (card_data < card_data_end && !*card_data)
1879 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1880 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1881 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1884 major_scan_card_table (SgenGrayQueue *queue)
1887 ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
1889 FOREACH_BLOCK (block) {
1893 if (!block->has_references)
1896 block_obj_size = block->obj_size;
1897 block_start = block->block;
1899 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1901 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1902 guint8 cards_data [CARDS_PER_BLOCK];
1904 char *obj, *end, *base;
1906 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1907 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1908 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1911 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1915 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1916 end = block_start + MS_BLOCK_SIZE;
1917 base = sgen_card_table_align_pointer (obj);
1920 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1921 int card_offset = (obj - base) >> CARD_BITS;
1922 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
1924 obj += block_obj_size;
1927 guint8 *card_data, *card_base;
1928 guint8 *card_data_end;
1931 * This is safe in face of card aliasing for the following reason:
1933 * Major blocks are 16k aligned, or 32 cards aligned.
1934 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1935 * sizes, they won't overflow the cardtable overlap modulus.
1937 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1938 card_data_end = card_data + CARDS_PER_BLOCK;
1940 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)) {
1942 int idx = card_data - card_base;
1943 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1944 char *end = start + CARD_SIZE_IN_BYTES;
1947 HEAVY_STAT (++scanned_cards);
1952 HEAVY_STAT (++marked_cards);
1954 sgen_card_table_prepare_card_for_scanning (card_data);
1959 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1961 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1963 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1964 HEAVY_STAT (++scanned_objects);
1965 scan_func (obj, queue);
1967 obj += block_obj_size;
1969 HEAVY_STAT (if (*card_data) ++remarked_cards);
1972 } END_FOREACH_BLOCK;
1977 major_is_worker_thread (MonoNativeThreadId thread)
1979 if (concurrent_sweep)
1980 return thread == ms_sweep_thread;
1986 alloc_free_block_lists (MSBlockInfo ***lists)
1989 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1990 lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1993 #ifdef SGEN_PARALLEL_MARK
1995 major_alloc_worker_data (void)
1997 /* FIXME: free this when the workers come down */
1998 MSBlockInfo ***lists = malloc (sizeof (MSBlockInfo**) * MS_BLOCK_TYPE_MAX);
1999 alloc_free_block_lists (lists);
2004 major_init_worker_thread (void *data)
2006 MSBlockInfo ***lists = data;
2009 g_assert (lists && lists != free_block_lists);
2010 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2012 for (j = 0; j < num_block_obj_sizes; ++j)
2013 g_assert (!lists [i][j]);
2016 #ifdef HAVE_KW_THREAD
2017 workers_free_block_lists = data;
2019 mono_native_tls_set_value (workers_free_block_lists_key, data);
2024 major_reset_worker_data (void *data)
2026 MSBlockInfo ***lists = data;
2028 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
2030 for (j = 0; j < num_block_obj_sizes; ++j)
2031 lists [i][j] = NULL;
2036 #undef pthread_create
2039 post_param_init (void)
2041 if (concurrent_sweep) {
2042 if (!mono_native_thread_create (&ms_sweep_thread, ms_sweep_thread_func, NULL)) {
2043 fprintf (stderr, "Error: Could not create sweep thread.\n");
2050 #ifdef SGEN_PARALLEL_MARK
2052 sgen_marksweep_fixed_par_init
2054 sgen_marksweep_par_init
2058 sgen_marksweep_fixed_init
2063 (SgenMajorCollector *collector)
2068 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
2071 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
2072 block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
2073 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
2075 evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
2076 for (i = 0; i < num_block_obj_sizes; ++i)
2077 evacuate_block_obj_sizes [i] = FALSE;
2082 g_print ("block object sizes:\n");
2083 for (i = 0; i < num_block_obj_sizes; ++i)
2084 g_print ("%d\n", block_obj_sizes [i]);
2088 alloc_free_block_lists (free_block_lists);
2090 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2091 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2092 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2093 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2095 #ifdef SGEN_PARALLEL_MARK
2096 LOCK_INIT (ms_block_list_mutex);
2099 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2100 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2101 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2102 mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &stat_time_wait_for_sweep);
2103 #ifdef SGEN_PARALLEL_MARK
2104 #ifndef HAVE_KW_THREAD
2105 mono_native_tls_alloc (&workers_free_block_lists_key, NULL);
2110 * FIXME: These are superfluous if concurrent sweep is
2111 * disabled. We might want to create them lazily.
2113 MONO_SEM_INIT (&ms_sweep_cmd_semaphore, 0);
2114 MONO_SEM_INIT (&ms_sweep_done_semaphore, 0);
2116 collector->section_size = MAJOR_SECTION_SIZE;
2117 #ifdef SGEN_PARALLEL_MARK
2118 collector->is_parallel = TRUE;
2119 collector->alloc_worker_data = major_alloc_worker_data;
2120 collector->init_worker_thread = major_init_worker_thread;
2121 collector->reset_worker_data = major_reset_worker_data;
2123 collector->is_parallel = FALSE;
2125 collector->supports_cardtable = TRUE;
2127 collector->have_swept = &have_swept;
2129 collector->alloc_heap = major_alloc_heap;
2130 collector->is_object_live = major_is_object_live;
2131 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2132 collector->alloc_degraded = major_alloc_degraded;
2134 collector->alloc_object = major_alloc_object;
2135 #ifdef SGEN_PARALLEL_MARK
2136 collector->par_alloc_object = major_par_alloc_object;
2138 collector->free_pinned_object = free_pinned_object;
2139 collector->iterate_objects = major_iterate_objects;
2140 collector->free_non_pinned_object = major_free_non_pinned_object;
2141 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2142 collector->pin_objects = major_pin_objects;
2143 collector->pin_major_object = pin_major_object;
2144 #ifdef SGEN_HAVE_CARDTABLE
2145 collector->scan_card_table = major_scan_card_table;
2146 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2148 collector->init_to_space = major_init_to_space;
2149 collector->sweep = major_sweep;
2150 collector->check_scan_starts = major_check_scan_starts;
2151 collector->dump_heap = major_dump_heap;
2152 collector->get_used_size = major_get_used_size;
2153 collector->start_nursery_collection = major_start_nursery_collection;
2154 collector->finish_nursery_collection = major_finish_nursery_collection;
2155 collector->start_major_collection = major_start_major_collection;
2156 collector->finish_major_collection = major_finish_major_collection;
2157 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2158 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2159 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2160 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2161 collector->get_num_major_sections = get_num_major_sections;
2162 collector->handle_gc_param = major_handle_gc_param;
2163 collector->print_gc_param_usage = major_print_gc_param_usage;
2164 collector->is_worker_thread = major_is_worker_thread;
2165 collector->post_param_init = post_param_init;
2166 collector->is_valid_object = major_is_valid_object;
2167 collector->describe_pointer = major_describe_pointer;
2169 collector->major_ops.copy_or_mark_object = major_copy_or_mark_object;
2170 collector->major_ops.scan_object = major_scan_object;
2172 #ifdef SGEN_HAVE_CARDTABLE
2173 /*cardtable requires major pages to be 8 cards aligned*/
2174 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);