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.
34 #include "utils/mono-counters.h"
35 #include "utils/mono-semaphore.h"
36 #include "utils/mono-time.h"
37 #include "metadata/object-internals.h"
38 #include "metadata/profiler-private.h"
40 #include "metadata/sgen-gc.h"
41 #include "metadata/sgen-protocol.h"
42 #include "metadata/sgen-cardtable.h"
43 #include "metadata/gc-internal.h"
45 #define MS_BLOCK_SIZE (16*1024)
46 #define MS_BLOCK_SIZE_SHIFT 14
47 #define MAJOR_SECTION_SIZE MS_BLOCK_SIZE
48 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
51 #define MS_DEFAULT_HEAP_NUM_BLOCKS (32 * 1024) /* 512 MB */
55 * Don't allocate single blocks, but alloc a contingent of this many
56 * blocks in one swoop.
58 #define MS_BLOCK_ALLOC_NUM 32
61 * Number of bytes before the first object in a block. At the start
62 * of a block is the MSBlockHeader, then opional padding, then come
63 * the objects, so this must be >= sizeof (MSBlockHeader).
66 #define MS_BLOCK_SKIP 0
68 #define MS_BLOCK_SKIP 16
71 #define MS_BLOCK_FREE (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
73 #define MS_NUM_MARK_WORDS ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
75 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
76 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
79 typedef struct _MSBlockInfo MSBlockInfo;
83 int pin_queue_num_entries;
84 unsigned int pinned : 1;
85 unsigned int has_references : 1;
86 unsigned int has_pinned : 1; /* means cannot evacuate */
87 unsigned int is_to_space : 1;
89 unsigned int used : 1;
90 unsigned int zeroed : 1;
95 MSBlockInfo *next_free;
96 void **pin_queue_start;
97 mword mark_words [MS_NUM_MARK_WORDS];
101 static int ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
103 #define ms_heap_start nursery_end
104 static char *ms_heap_end;
106 #define MS_PTR_IN_SMALL_MAJOR_HEAP(p) ((char*)(p) >= ms_heap_start && (char*)(p) < ms_heap_end)
108 /* array of all all block infos in the system */
109 static MSBlockInfo *block_infos;
112 #define MS_BLOCK_OBJ(b,i) ((b)->block + MS_BLOCK_SKIP + (b)->obj_size * (i))
113 #define MS_BLOCK_DATA_FOR_OBJ(o) ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
116 #define MS_BLOCK_FOR_OBJ(o) (&block_infos [(mword)((char*)(o) - ms_heap_start) >> MS_BLOCK_SIZE_SHIFT])
122 #define MS_BLOCK_FOR_OBJ(o) (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
125 #define MS_BLOCK_OBJ_INDEX(o,b) (((char*)(o) - ((b)->block + MS_BLOCK_SKIP)) / (b)->obj_size)
127 #define MS_CALC_MARK_BIT(w,b,o) do { \
128 int i = ((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o))) >> SGEN_ALLOC_ALIGN_BITS; \
129 if (sizeof (mword) == 4) { \
138 #define MS_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] & (1L << (b)))
139 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (1L << (b)))
140 #define MS_PAR_SET_MARK_BIT(was_marked,bl,w,b) do { \
141 mword __old = (bl)->mark_words [(w)]; \
142 mword __bitmask = 1L << (b); \
143 if (__old & __bitmask) { \
147 if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)], \
148 (gpointer)(__old | __bitmask), \
149 (gpointer)__old) == \
151 was_marked = FALSE; \
156 #define MS_OBJ_ALLOCED(o,b) (*(void**)(o) && (*(char**)(o) < (b)->block || *(char**)(o) >= (b)->block + MS_BLOCK_SIZE))
158 #define MS_BLOCK_OBJ_SIZE_FACTOR (sqrt (2.0))
161 * This way we can lookup block object size indexes for sizes up to
162 * 256 bytes with a single load.
164 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES 32
166 static int *block_obj_sizes;
167 static int num_block_obj_sizes;
168 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
170 #define MS_BLOCK_FLAG_PINNED 1
171 #define MS_BLOCK_FLAG_REFS 2
173 #define MS_BLOCK_TYPE_MAX 4
175 #ifdef SGEN_PARALLEL_MARK
176 static LOCK_DECLARE (ms_block_list_mutex);
177 #define LOCK_MS_BLOCK_LIST pthread_mutex_lock (&ms_block_list_mutex)
178 #define UNLOCK_MS_BLOCK_LIST pthread_mutex_unlock (&ms_block_list_mutex)
181 /* we get this at init */
182 static int nursery_bits;
183 static char *nursery_start;
184 static char *nursery_end;
186 static gboolean *evacuate_block_obj_sizes;
187 static float evacuation_threshold = 0.666;
189 static gboolean concurrent_sweep = FALSE;
190 static gboolean have_swept;
192 #define ptr_in_nursery(p) (SGEN_PTR_IN_NURSERY ((p), nursery_bits, nursery_start, nursery_end))
194 /* all allocated blocks in the system */
195 static MSBlockInfo *all_blocks;
198 /* non-allocated block free-list */
199 static MSBlockInfo *empty_blocks = NULL;
201 /* non-allocated block free-list */
202 static void *empty_blocks = NULL;
203 static int num_empty_blocks = 0;
206 #define FOREACH_BLOCK(bl) for ((bl) = all_blocks; (bl); (bl) = (bl)->next) {
207 #define END_FOREACH_BLOCK }
209 static int num_major_sections = 0;
210 /* one free block list for each block object size */
211 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
213 #ifdef SGEN_PARALLEL_MARK
214 #ifdef HAVE_KW_THREAD
215 static __thread MSBlockInfo ***workers_free_block_lists;
217 static pthread_key_t workers_free_block_lists_key;
221 static long long stat_major_blocks_alloced = 0;
222 static long long stat_major_blocks_freed = 0;
223 static long long stat_major_objects_evacuated = 0;
224 static long long stat_time_wait_for_sweep = 0;
225 #ifdef SGEN_PARALLEL_MARK
226 static long long stat_slots_allocated_in_vain = 0;
229 static gboolean ms_sweep_in_progress = FALSE;
230 static pthread_t ms_sweep_thread;
231 static MonoSemType ms_sweep_cmd_semaphore;
232 static MonoSemType ms_sweep_done_semaphore;
235 ms_signal_sweep_command (void)
237 if (!concurrent_sweep)
240 g_assert (!ms_sweep_in_progress);
241 ms_sweep_in_progress = TRUE;
242 MONO_SEM_POST (&ms_sweep_cmd_semaphore);
246 ms_signal_sweep_done (void)
248 if (!concurrent_sweep)
251 MONO_SEM_POST (&ms_sweep_done_semaphore);
255 ms_wait_for_sweep_done (void)
257 SGEN_TV_DECLARE (atv);
258 SGEN_TV_DECLARE (btv);
261 if (!concurrent_sweep)
264 if (!ms_sweep_in_progress)
267 SGEN_TV_GETTIME (atv);
268 while ((result = MONO_SEM_WAIT (&ms_sweep_done_semaphore)) != 0) {
270 g_error ("MONO_SEM_WAIT");
272 SGEN_TV_GETTIME (btv);
273 stat_time_wait_for_sweep += SGEN_TV_ELAPSED_MS (atv, btv);
275 g_assert (ms_sweep_in_progress);
276 ms_sweep_in_progress = FALSE;
280 ms_find_block_obj_size_index (int size)
283 DEBUG (9, g_assert (size <= SGEN_MAX_SMALL_OBJ_SIZE));
284 for (i = 0; i < num_block_obj_sizes; ++i)
285 if (block_obj_sizes [i] >= size)
287 g_assert_not_reached ();
290 #define FREE_BLOCKS_FROM(lists,p,r) (lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
291 #define FREE_BLOCKS(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
292 #ifdef SGEN_PARALLEL_MARK
293 #ifdef HAVE_KW_THREAD
294 #define FREE_BLOCKS_LOCAL(p,r) (FREE_BLOCKS_FROM (workers_free_block_lists, (p), (r)))
296 #define FREE_BLOCKS_LOCAL(p,r) (FREE_BLOCKS_FROM (((MSBlockInfo***)(pthread_getspecific (workers_free_block_lists_key))), (p), (r)))
299 //#define FREE_BLOCKS_LOCAL(p,r) (FREE_BLOCKS_FROM (free_block_lists, (p), (r)))
302 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
303 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
304 fast_block_obj_size_indexes [((s)+7)>>3] : \
305 ms_find_block_obj_size_index ((s)))
309 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
312 mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
313 mword alloc_size = nursery_size + major_heap_size;
316 g_assert (ms_heap_num_blocks > 0);
317 g_assert (nursery_size % MS_BLOCK_SIZE == 0);
319 g_assert (nursery_align % MS_BLOCK_SIZE == 0);
321 nursery_start = mono_sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE);
322 nursery_end = heap_start = nursery_start + nursery_size;
323 nursery_bits = the_nursery_bits;
325 ms_heap_end = heap_start + major_heap_size;
327 block_infos = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
329 for (i = 0; i < ms_heap_num_blocks; ++i) {
330 block_infos [i].block = heap_start + i * MS_BLOCK_SIZE;
331 if (i < ms_heap_num_blocks - 1)
332 block_infos [i].next_free = &block_infos [i + 1];
334 block_infos [i].next_free = NULL;
335 block_infos [i].zeroed = TRUE;
338 empty_blocks = &block_infos [0];
340 return nursery_start;
344 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
347 nursery_start = mono_sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
349 nursery_start = mono_sgen_alloc_os_memory (nursery_size, TRUE);
351 nursery_end = nursery_start + nursery_size;
352 nursery_bits = the_nursery_bits;
354 return nursery_start;
359 update_heap_boundaries_for_block (MSBlockInfo *block)
361 mono_sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
366 ms_get_empty_block (void)
370 g_assert (empty_blocks);
372 block = empty_blocks;
373 empty_blocks = empty_blocks->next_free;
378 memset (block->block, 0, MS_BLOCK_SIZE);
384 ms_free_block (MSBlockInfo *block)
386 block->next_free = empty_blocks;
387 empty_blocks = block;
389 block->zeroed = FALSE;
390 mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
394 ms_get_empty_block (void)
398 void *block, *empty, *next;
402 p = mono_sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
404 for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
407 * We do the free list update one after the
408 * other so that other threads can use the new
409 * blocks as quickly as possible.
412 empty = empty_blocks;
413 *(void**)block = empty;
414 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
418 SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
420 stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
424 empty = empty_blocks;
428 next = *(void**)block;
429 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
431 SGEN_ATOMIC_ADD (num_empty_blocks, -1);
433 *(void**)block = NULL;
435 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
441 ms_free_block (void *block)
445 mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
446 memset (block, 0, MS_BLOCK_SIZE);
449 empty = empty_blocks;
450 *(void**)block = empty;
451 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
453 SGEN_ATOMIC_ADD (num_empty_blocks, 1);
457 //#define MARKSWEEP_CONSISTENCY_CHECK
459 #ifdef MARKSWEEP_CONSISTENCY_CHECK
461 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
465 for (; block; block = block->next_free) {
466 g_assert (block->obj_size == size);
467 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
469 /* blocks in the free lists must have at least
471 g_assert (block->free_list);
474 /* the block must not be in the empty_blocks list */
475 for (b = empty_blocks; b; b = b->next_free)
476 g_assert (b != block);
478 /* the block must be in the all_blocks list */
479 for (b = all_blocks; b; b = b->next) {
483 g_assert (b == block);
488 check_empty_blocks (void)
493 for (p = empty_blocks; p; p = *(void**)p)
495 g_assert (i == num_empty_blocks);
500 consistency_check (void)
505 /* check all blocks */
506 FOREACH_BLOCK (block) {
507 int count = MS_BLOCK_FREE / block->obj_size;
512 /* check block header */
513 g_assert (((MSBlockHeader*)block->block)->info == block);
516 /* count number of free slots */
517 for (i = 0; i < count; ++i) {
518 void **obj = (void**) MS_BLOCK_OBJ (block, i);
519 if (!MS_OBJ_ALLOCED (obj, block))
523 /* check free list */
524 for (free = block->free_list; free; free = (void**)*free) {
525 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
528 g_assert (num_free == 0);
530 /* check all mark words are zero */
531 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
532 g_assert (block->mark_words [i] == 0);
535 /* check free blocks */
536 for (i = 0; i < num_block_obj_sizes; ++i) {
538 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
539 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
542 check_empty_blocks ();
547 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
549 int size = block_obj_sizes [size_index];
550 int count = MS_BLOCK_FREE / size;
552 #ifdef SGEN_PARALLEL_MARK
556 MSBlockHeader *header;
558 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
562 if (!mono_sgen_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
566 info = ms_get_empty_block ();
568 info = mono_sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
571 DEBUG (9, g_assert (count >= 2));
573 info->obj_size = size;
574 info->obj_size_index = size_index;
575 info->pinned = pinned;
576 info->has_references = has_references;
577 info->has_pinned = pinned;
578 info->is_to_space = (mono_sgen_get_current_collection_generation () == GENERATION_OLD);
580 info->block = ms_get_empty_block ();
582 header = (MSBlockHeader*) info->block;
586 update_heap_boundaries_for_block (info);
588 /* build free list */
589 obj_start = info->block + MS_BLOCK_SKIP;
590 info->free_list = (void**)obj_start;
591 /* we're skipping the last one - it must be nulled */
592 for (i = 0; i < count - 1; ++i) {
593 char *next_obj_start = obj_start + size;
594 *(void**)obj_start = next_obj_start;
595 obj_start = next_obj_start;
598 *(void**)obj_start = NULL;
600 #ifdef SGEN_PARALLEL_MARK
602 next = info->next_free = free_blocks [size_index];
603 } while (SGEN_CAS_PTR ((void**)&free_blocks [size_index], info, next) != next);
606 next = info->next = all_blocks;
607 } while (SGEN_CAS_PTR ((void**)&all_blocks, info, next) != next);
609 info->next_free = free_blocks [size_index];
610 free_blocks [size_index] = info;
612 info->next = all_blocks;
616 ++num_major_sections;
621 obj_is_from_pinned_alloc (char *ptr)
625 FOREACH_BLOCK (block) {
626 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
627 return block->pinned;
633 unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_index)
638 block = free_blocks [size_index];
639 DEBUG (9, g_assert (block));
641 obj = block->free_list;
642 DEBUG (9, g_assert (obj));
644 block->free_list = *(void**)obj;
645 if (!block->free_list) {
646 free_blocks [size_index] = block->next_free;
647 block->next_free = NULL;
653 #ifdef SGEN_PARALLEL_MARK
655 try_remove_block_from_free_list (MSBlockInfo *block, MSBlockInfo **free_blocks, int size_index)
658 * No more free slots in the block, so try to free the block.
659 * Don't try again if we don't succeed - another thread will
660 * already have done it.
662 MSBlockInfo *next_block = block->next_free;
663 if (SGEN_CAS_PTR ((void**)&free_blocks [size_index], next_block, block) == block) {
665 void *old = SGEN_CAS_PTR ((void**)&block->next_free, NULL, next_block);
666 g_assert (old == next_block);
668 block->next_free = NULL;
675 alloc_obj_par (int size, gboolean pinned, gboolean has_references)
677 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
678 MSBlockInfo **free_blocks_local = FREE_BLOCKS_LOCAL (pinned, has_references);
682 DEBUG (9, g_assert (!ms_sweep_in_progress));
683 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
685 if (free_blocks_local [size_index]) {
687 obj = unlink_slot_from_free_list_uncontested (free_blocks_local, size_index);
689 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
692 block = free_blocks [size_index];
694 if (!try_remove_block_from_free_list (block, free_blocks, size_index))
697 g_assert (block->next_free == NULL);
698 g_assert (block->free_list);
699 block->next_free = free_blocks_local [size_index];
700 free_blocks_local [size_index] = block;
707 success = ms_alloc_block (size_index, pinned, has_references);
708 UNLOCK_MS_BLOCK_LIST;
710 if (G_UNLIKELY (!success))
718 * FIXME: This should not be necessary because it'll be
719 * overwritten by the vtable immediately.
728 alloc_obj (int size, gboolean pinned, gboolean has_references)
730 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
731 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
734 #ifdef SGEN_PARALLEL_MARK
735 DEBUG (9, g_assert (current_collection_generation != GENERATION_OLD));
738 DEBUG (9, g_assert (!ms_sweep_in_progress));
740 if (!free_blocks [size_index]) {
741 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
745 obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
748 * FIXME: This should not be necessary because it'll be
749 * overwritten by the vtable immediately.
757 major_alloc_object (int size, gboolean has_references)
759 return alloc_obj (size, FALSE, has_references);
763 * We're not freeing the block if it's empty. We leave that work for
764 * the next major collection.
766 * This is just called from the domain clearing code, which runs in a
767 * single thread and has the GC lock, so we don't need an extra lock.
770 free_object (char *obj, size_t size, gboolean pinned)
772 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
774 DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
775 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
776 MS_CALC_MARK_BIT (word, bit, obj);
777 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
778 if (!block->free_list) {
779 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
780 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
781 DEBUG (9, g_assert (!block->next_free));
782 block->next_free = free_blocks [size_index];
783 free_blocks [size_index] = block;
785 memset (obj, 0, size);
786 *(void**)obj = block->free_list;
787 block->free_list = (void**)obj;
791 major_free_non_pinned_object (char *obj, size_t size)
793 free_object (obj, size, FALSE);
796 /* size is a multiple of SGEN_ALLOC_ALIGN */
798 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
802 ms_wait_for_sweep_done ();
804 res = alloc_obj (size, TRUE, has_references);
805 /*If we failed to alloc memory, we better try releasing memory
806 *as pinned alloc is requested by the runtime.
809 sgen_collect_major_no_lock ("pinned alloc failure");
810 res = alloc_obj (size, TRUE, has_references);
816 free_pinned_object (char *obj, size_t size)
818 free_object (obj, size, TRUE);
822 * size is already rounded up and we hold the GC lock.
825 major_alloc_degraded (MonoVTable *vtable, size_t size)
828 int old_num_sections;
830 ms_wait_for_sweep_done ();
832 old_num_sections = num_major_sections;
834 obj = alloc_obj (size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
835 if (G_LIKELY (obj)) {
836 *(MonoVTable**)obj = vtable;
837 HEAVY_STAT (++stat_objects_alloced_degraded);
838 HEAVY_STAT (stat_bytes_alloced_degraded += size);
839 g_assert (num_major_sections >= old_num_sections);
840 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
845 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj) FALSE
848 * obj is some object. If it's not in the major heap (i.e. if it's in
849 * the nursery or LOS), return FALSE. Otherwise return whether it's
850 * been marked or copied.
853 major_is_object_live (char *obj)
861 if (ptr_in_nursery (obj))
866 if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
869 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
872 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
876 /* now we know it's in a major block */
877 block = MS_BLOCK_FOR_OBJ (obj);
878 DEBUG (9, g_assert (!block->pinned));
879 MS_CALC_MARK_BIT (word, bit, obj);
880 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
884 major_ptr_is_in_non_pinned_space (char *ptr)
888 FOREACH_BLOCK (block) {
889 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
890 return !block->pinned;
896 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
900 ms_wait_for_sweep_done ();
902 FOREACH_BLOCK (block) {
903 int count = MS_BLOCK_FREE / block->obj_size;
906 if (block->pinned && !pinned)
908 if (!block->pinned && !non_pinned)
911 for (i = 0; i < count; ++i) {
912 void **obj = (void**) MS_BLOCK_OBJ (block, i);
913 if (MS_OBJ_ALLOCED (obj, block))
914 callback ((char*)obj, block->obj_size, data);
920 major_check_scan_starts (void)
925 major_dump_heap (FILE *heap_dump_file)
928 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
929 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
932 for (i = 0; i < num_block_obj_sizes; ++i)
933 slots_available [i] = slots_used [i] = 0;
935 FOREACH_BLOCK (block) {
936 int index = ms_find_block_obj_size_index (block->obj_size);
937 int count = MS_BLOCK_FREE / block->obj_size;
939 slots_available [index] += count;
940 for (i = 0; i < count; ++i) {
941 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
942 ++slots_used [index];
946 fprintf (heap_dump_file, "<occupancies>\n");
947 for (i = 0; i < num_block_obj_sizes; ++i) {
948 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
949 block_obj_sizes [i], slots_available [i], slots_used [i]);
951 fprintf (heap_dump_file, "</occupancies>\n");
953 FOREACH_BLOCK (block) {
954 int count = MS_BLOCK_FREE / block->obj_size;
958 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
960 for (i = 0; i <= count; ++i) {
961 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
966 mono_sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
972 fprintf (heap_dump_file, "</section>\n");
976 #define LOAD_VTABLE SGEN_LOAD_VTABLE
978 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do { \
980 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
981 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
982 MS_SET_MARK_BIT ((block), __word, __bit); \
983 if ((block)->has_references) \
984 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
985 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
988 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
990 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
991 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
992 if (!MS_MARK_BIT ((block), __word, __bit)) { \
993 MS_SET_MARK_BIT ((block), __word, __bit); \
994 if ((block)->has_references) \
995 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
996 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
999 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
1000 int __word, __bit; \
1001 gboolean __was_marked; \
1002 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
1003 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
1004 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
1005 if (!__was_marked) { \
1006 if ((block)->has_references) \
1007 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
1008 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
1012 #ifdef SGEN_PARALLEL_MARK
1014 pin_or_update_par (void **ptr, void *obj, MonoVTable *vt, SgenGrayQueue *queue)
1019 if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
1020 mono_sgen_pin_object (obj, queue);
1024 vtable_word = *(mword*)obj;
1025 /*someone else forwarded it, update the pointer and bail out*/
1026 if (vtable_word & SGEN_FORWARDED_BIT) {
1027 *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1031 /*someone pinned it, nothing to do.*/
1032 if (vtable_word & SGEN_PINNED_BIT)
1038 #include "sgen-major-copy-object.h"
1040 #ifdef SGEN_PARALLEL_MARK
1042 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1049 HEAVY_STAT (++stat_copy_object_called_major);
1051 DEBUG (9, g_assert (obj));
1052 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
1054 if (ptr_in_nursery (obj)) {
1056 gboolean has_references;
1058 mword vtable_word = *(mword*)obj;
1059 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1061 if (vtable_word & SGEN_FORWARDED_BIT) {
1066 if (vtable_word & SGEN_PINNED_BIT)
1069 HEAVY_STAT (++stat_objects_copied_major);
1072 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
1073 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
1075 destination = alloc_obj_par (objsize, FALSE, has_references);
1076 if (G_UNLIKELY (!destination)) {
1077 if (!ptr_in_nursery (obj)) {
1079 block = MS_BLOCK_FOR_OBJ (obj);
1080 size_index = block->obj_size_index;
1081 evacuate_block_obj_sizes [size_index] = FALSE;
1084 pin_or_update_par (ptr, obj, vt, queue);
1089 * We do this before the CAS because we want to make
1090 * sure that if another thread sees the destination
1091 * pointer the VTable is already in place. Not doing
1092 * this can crash binary protocols.
1094 *(MonoVTable**)destination = vt;
1096 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
1097 gboolean was_marked;
1099 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
1104 * FIXME: If we make major_alloc_object() give
1105 * us the block info, too, we won't have to
1108 block = MS_BLOCK_FOR_OBJ (obj);
1109 MS_CALC_MARK_BIT (word, bit, obj);
1110 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
1111 MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
1114 * FIXME: We have allocated destination, but
1115 * we cannot use it. Give it back to the
1118 *(void**)destination = NULL;
1120 vtable_word = *(mword*)obj;
1121 g_assert (vtable_word & SGEN_FORWARDED_BIT);
1123 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1127 ++stat_slots_allocated_in_vain;
1131 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1133 mword vtable_word = *(mword*)obj;
1134 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1136 /* see comment in the non-parallel version below */
1137 if (vtable_word & SGEN_FORWARDED_BIT) {
1141 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
1143 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1148 block = MS_BLOCK_FOR_OBJ (obj);
1149 size_index = block->obj_size_index;
1151 if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1152 if (block->is_to_space)
1157 mword vtable_word = *(mword*)obj;
1158 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1160 if (vtable_word & SGEN_FORWARDED_BIT) {
1167 HEAVY_STAT (++stat_major_objects_evacuated);
1168 goto do_copy_object;
1171 MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1174 mword vtable_word = *(mword*)obj;
1175 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1178 if (vtable_word & SGEN_PINNED_BIT)
1180 binary_protocol_pin (obj, vt, mono_sgen_safe_object_get_size ((MonoObject*)obj));
1181 if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
1182 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1183 GRAY_OBJECT_ENQUEUE (queue, obj);
1185 g_assert (SGEN_OBJECT_IS_PINNED (obj));
1192 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1197 HEAVY_STAT (++stat_copy_object_called_major);
1199 DEBUG (9, g_assert (obj));
1200 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
1202 if (ptr_in_nursery (obj)) {
1204 char *forwarded, *old_obj;
1206 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1210 if (SGEN_OBJECT_IS_PINNED (obj))
1213 HEAVY_STAT (++stat_objects_copied_major);
1217 obj = copy_object_no_checks (obj, queue);
1218 if (G_UNLIKELY (old_obj == obj)) {
1219 /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1220 if (!ptr_in_nursery (obj)) {
1222 block = MS_BLOCK_FOR_OBJ (obj);
1223 size_index = block->obj_size_index;
1224 evacuate_block_obj_sizes [size_index] = FALSE;
1225 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1232 * FIXME: See comment for copy_object_no_checks(). If
1233 * we have that, we can let the allocation function
1234 * give us the block info, too, and we won't have to
1237 block = MS_BLOCK_FOR_OBJ (obj);
1238 MS_CALC_MARK_BIT (word, bit, obj);
1239 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
1240 MS_SET_MARK_BIT (block, word, bit);
1244 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1249 * If we have don't have a fixed heap we cannot know
1250 * whether an object is in the LOS or in the small
1251 * object major heap without checking its size. To do
1252 * that, however, we need to know that we actually
1253 * have a valid object, not a forwarding pointer, so
1254 * we have to do this check first.
1256 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1261 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
1263 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1269 block = MS_BLOCK_FOR_OBJ (obj);
1270 size_index = block->obj_size_index;
1271 evacuate = evacuate_block_obj_sizes [size_index];
1275 * We could also check for !block->has_pinned
1276 * here, but it would only make an uncommon case
1277 * faster, namely objects that are in blocks
1278 * whose slot sizes are evacuated but which have
1281 if (evacuate && (forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1287 if (evacuate && !block->has_pinned) {
1288 if (block->is_to_space)
1290 HEAVY_STAT (++stat_major_objects_evacuated);
1291 goto do_copy_object;
1293 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1296 if (SGEN_OBJECT_IS_PINNED (obj))
1298 binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), mono_sgen_safe_object_get_size ((MonoObject*)obj));
1299 SGEN_PIN_OBJECT (obj);
1300 /* FIXME: only enqueue if object has references */
1301 GRAY_OBJECT_ENQUEUE (queue, obj);
1307 #include "sgen-major-scan-object.h"
1310 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1313 int last_index = -1;
1315 if (!block->pin_queue_num_entries)
1318 block->has_pinned = TRUE;
1320 for (i = 0; i < block->pin_queue_num_entries; ++i) {
1321 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1322 DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
1323 if (index == last_index)
1325 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1336 /* statistics for evacuation */
1337 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1338 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1339 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1341 for (i = 0; i < num_block_obj_sizes; ++i)
1342 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1344 /* clear all the free lists */
1345 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1346 MSBlockInfo **free_blocks = free_block_lists [i];
1348 for (j = 0; j < num_block_obj_sizes; ++j)
1349 free_blocks [j] = NULL;
1352 /* traverse all blocks, free and zero unmarked objects */
1355 MSBlockInfo *block = *iter;
1357 gboolean have_live = FALSE;
1358 gboolean has_pinned;
1362 obj_size_index = block->obj_size_index;
1364 has_pinned = block->has_pinned;
1365 block->has_pinned = block->pinned;
1367 block->is_to_space = FALSE;
1369 count = MS_BLOCK_FREE / block->obj_size;
1370 block->free_list = NULL;
1372 for (obj_index = 0; obj_index < count; ++obj_index) {
1374 void *obj = MS_BLOCK_OBJ (block, obj_index);
1376 MS_CALC_MARK_BIT (word, bit, obj);
1377 if (MS_MARK_BIT (block, word, bit)) {
1378 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
1381 ++slots_used [obj_size_index];
1383 /* an unmarked object */
1384 if (MS_OBJ_ALLOCED (obj, block)) {
1385 binary_protocol_empty (obj, block->obj_size);
1386 memset (obj, 0, block->obj_size);
1388 *(void**)obj = block->free_list;
1389 block->free_list = obj;
1393 /* reset mark bits */
1394 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1397 * FIXME: reverse free list so that it's in address
1403 ++num_blocks [obj_size_index];
1404 slots_available [obj_size_index] += count;
1407 iter = &block->next;
1410 * If there are free slots in the block, add
1411 * the block to the corresponding free list.
1413 if (block->free_list) {
1414 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1415 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1416 block->next_free = free_blocks [index];
1417 free_blocks [index] = block;
1420 update_heap_boundaries_for_block (block);
1423 * Blocks without live objects are removed from the
1424 * block list and freed.
1426 *iter = block->next;
1429 ms_free_block (block);
1431 ms_free_block (block->block);
1433 mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1436 --num_major_sections;
1440 for (i = 0; i < num_block_obj_sizes; ++i) {
1441 float usage = (float)slots_used [i] / (float)slots_available [i];
1442 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1443 evacuate_block_obj_sizes [i] = TRUE;
1445 g_print ("slot size %d - %d of %d used\n",
1446 block_obj_sizes [i], slots_used [i], slots_available [i]);
1449 evacuate_block_obj_sizes [i] = FALSE;
1457 ms_sweep_thread_func (void *dummy)
1459 g_assert (concurrent_sweep);
1464 while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
1466 g_error ("MONO_SEM_WAIT");
1471 ms_signal_sweep_done ();
1480 if (concurrent_sweep) {
1481 g_assert (ms_sweep_thread);
1482 ms_signal_sweep_command ();
1488 static int count_pinned_ref;
1489 static int count_pinned_nonref;
1490 static int count_nonpinned_ref;
1491 static int count_nonpinned_nonref;
1494 count_nonpinned_callback (char *obj, size_t size, void *data)
1496 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1498 if (vtable->klass->has_references)
1499 ++count_nonpinned_ref;
1501 ++count_nonpinned_nonref;
1505 count_pinned_callback (char *obj, size_t size, void *data)
1507 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1509 if (vtable->klass->has_references)
1512 ++count_pinned_nonref;
1515 static void __attribute__ ((unused))
1516 count_ref_nonref_objs (void)
1520 count_pinned_ref = 0;
1521 count_pinned_nonref = 0;
1522 count_nonpinned_ref = 0;
1523 count_nonpinned_nonref = 0;
1525 major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1526 major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1528 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1530 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1531 count_pinned_ref, count_nonpinned_ref,
1532 count_pinned_nonref, count_nonpinned_nonref,
1533 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1537 ms_calculate_block_obj_sizes (double factor, int *arr)
1539 double target_size = sizeof (MonoObject);
1544 int target_count = ceil (MS_BLOCK_FREE / target_size);
1545 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1547 if (size != last_size) {
1549 arr [num_sizes] = size;
1554 target_size *= factor;
1555 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1560 /* only valid during minor collections */
1561 static int old_num_major_sections;
1564 major_start_nursery_collection (void)
1566 ms_wait_for_sweep_done ();
1568 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1569 consistency_check ();
1572 old_num_major_sections = num_major_sections;
1576 major_finish_nursery_collection (void)
1578 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1579 consistency_check ();
1581 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1585 major_start_major_collection (void)
1589 ms_wait_for_sweep_done ();
1591 /* clear the free lists */
1592 for (i = 0; i < num_block_obj_sizes; ++i) {
1593 if (!evacuate_block_obj_sizes [i])
1596 free_block_lists [0][i] = NULL;
1597 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1602 major_finish_major_collection (void)
1607 major_have_computer_minor_collection_allowance (void)
1610 int section_reserve = mono_sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1612 g_assert (have_swept);
1613 ms_wait_for_sweep_done ();
1614 g_assert (!ms_sweep_in_progress);
1617 * FIXME: We don't free blocks on 32 bit platforms because it
1618 * can lead to address space fragmentation, since we're
1619 * allocating blocks in larger contingents.
1621 if (sizeof (mword) < 8)
1624 while (num_empty_blocks > section_reserve) {
1625 void *next = *(void**)empty_blocks;
1626 mono_sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
1627 empty_blocks = next;
1629 * Needs not be atomic because this is running
1634 ++stat_major_blocks_freed;
1640 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1644 FOREACH_BLOCK (block) {
1645 block->pin_queue_start = mono_sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
1646 &block->pin_queue_num_entries);
1647 } END_FOREACH_BLOCK;
1651 major_pin_objects (SgenGrayQueue *queue)
1655 FOREACH_BLOCK (block) {
1656 mark_pinned_objects_in_block (block, queue);
1657 } END_FOREACH_BLOCK;
1661 major_init_to_space (void)
1666 major_report_pinned_memory_usage (void)
1668 g_assert_not_reached ();
1672 major_get_used_size (void)
1677 FOREACH_BLOCK (block) {
1678 int count = MS_BLOCK_FREE / block->obj_size;
1680 size += count * block->obj_size;
1681 for (iter = block->free_list; iter; iter = (void**)*iter)
1682 size -= block->obj_size;
1683 } END_FOREACH_BLOCK;
1689 get_num_major_sections (void)
1691 return num_major_sections;
1695 major_handle_gc_param (const char *opt)
1698 if (g_str_has_prefix (opt, "major-heap-size=")) {
1699 const char *arg = strchr (opt, '=') + 1;
1701 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
1703 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1704 g_assert (ms_heap_num_blocks > 0);
1708 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1709 const char *arg = strchr (opt, '=') + 1;
1710 int percentage = atoi (arg);
1711 if (percentage < 0 || percentage > 100) {
1712 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1715 evacuation_threshold = (float)percentage / 100.0;
1717 } else if (!strcmp (opt, "concurrent-sweep")) {
1718 concurrent_sweep = TRUE;
1720 } else if (!strcmp (opt, "no-concurrent-sweep")) {
1721 concurrent_sweep = FALSE;
1729 major_print_gc_param_usage (void)
1734 " major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1736 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1737 " (no-)concurrent-sweep\n"
1741 #ifdef SGEN_HAVE_CARDTABLE
1743 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1747 FOREACH_BLOCK (block) {
1748 if (block->has_references)
1749 callback ((mword)block->block, MS_BLOCK_SIZE);
1750 } END_FOREACH_BLOCK;
1753 #ifdef HEAVY_STATISTICS
1754 extern long long marked_cards;
1755 extern long long scanned_cards;
1756 extern long long scanned_objects;
1760 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1762 * MS blocks are 16K aligned.
1763 * Cardtables are 4K aligned, at least.
1764 * This means that the cardtable of a given block is 32 bytes aligned.
1767 initial_skip_card (guint8 *card_data)
1769 mword *cards = (mword*)card_data;
1772 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1778 if (i == CARD_WORDS_PER_BLOCK)
1779 return card_data + CARDS_PER_BLOCK;
1781 #if defined(__i386__) && defined(__GNUC__)
1782 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1783 #elif defined(__x86_64__) && defined(__GNUC__)
1784 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1785 #elif defined(__s390x__) && defined(__GNUC__)
1786 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1788 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1790 return &card_data [i];
1797 static G_GNUC_UNUSED guint8*
1798 skip_card (guint8 *card_data, guint8 *card_data_end)
1800 while (card_data < card_data_end && !*card_data)
1805 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1806 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1807 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1810 major_scan_card_table (SgenGrayQueue *queue)
1814 FOREACH_BLOCK (block) {
1818 if (!block->has_references)
1821 block_obj_size = block->obj_size;
1822 block_start = block->block;
1824 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1826 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1827 guint8 cards_data [CARDS_PER_BLOCK];
1829 char *obj, *end, *base;
1831 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1832 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1833 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1836 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1840 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1841 end = block_start + MS_BLOCK_SIZE;
1842 base = sgen_card_table_align_pointer (obj);
1845 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1846 int card_offset = (obj - base) >> CARD_BITS;
1847 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
1849 obj += block_obj_size;
1852 guint8 *card_data, *card_base;
1853 guint8 *card_data_end;
1856 * This is safe in face of card aliasing for the following reason:
1858 * Major blocks are 16k aligned, or 32 cards aligned.
1859 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1860 * sizes, they won't overflow the cardtable overlap modulus.
1862 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1863 card_data_end = card_data + CARDS_PER_BLOCK;
1865 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)) {
1867 int idx = card_data - card_base;
1868 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1869 char *end = start + CARD_SIZE_IN_BYTES;
1872 HEAVY_STAT (++scanned_cards);
1877 HEAVY_STAT (++marked_cards);
1879 sgen_card_table_prepare_card_for_scanning (card_data);
1884 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1886 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1888 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1889 HEAVY_STAT (++scanned_objects);
1890 minor_scan_object (obj, queue);
1892 obj += block_obj_size;
1896 } END_FOREACH_BLOCK;
1901 major_is_worker_thread (pthread_t thread)
1903 if (concurrent_sweep)
1904 return thread == ms_sweep_thread;
1910 alloc_free_block_lists (MSBlockInfo ***lists)
1913 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1914 lists [i] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1917 #ifdef SGEN_PARALLEL_MARK
1919 major_alloc_worker_data (void)
1921 /* FIXME: free this when the workers come down */
1922 MSBlockInfo ***lists = malloc (sizeof (MSBlockInfo**) * MS_BLOCK_TYPE_MAX);
1923 alloc_free_block_lists (lists);
1928 major_init_worker_thread (void *data)
1930 MSBlockInfo ***lists = data;
1933 g_assert (lists && lists != free_block_lists);
1934 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1936 for (j = 0; j < num_block_obj_sizes; ++j)
1937 g_assert (!lists [i][j]);
1940 #ifdef HAVE_KW_THREAD
1941 workers_free_block_lists = data;
1943 pthread_setspecific (workers_free_block_lists_key, data);
1948 major_reset_worker_data (void *data)
1950 MSBlockInfo ***lists = data;
1952 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1954 for (j = 0; j < num_block_obj_sizes; ++j)
1955 lists [i][j] = NULL;
1960 #undef pthread_create
1963 post_param_init (void)
1965 if (concurrent_sweep) {
1966 if (pthread_create (&ms_sweep_thread, NULL, ms_sweep_thread_func, NULL)) {
1967 fprintf (stderr, "Error: Could not create sweep thread.\n");
1974 #ifdef SGEN_PARALLEL_MARK
1976 mono_sgen_marksweep_fixed_par_init
1978 mono_sgen_marksweep_par_init
1982 mono_sgen_marksweep_fixed_init
1984 mono_sgen_marksweep_init
1987 (SgenMajorCollector *collector)
1992 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1995 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
1996 block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1997 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
1999 evacuate_block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
2000 for (i = 0; i < num_block_obj_sizes; ++i)
2001 evacuate_block_obj_sizes [i] = FALSE;
2006 g_print ("block object sizes:\n");
2007 for (i = 0; i < num_block_obj_sizes; ++i)
2008 g_print ("%d\n", block_obj_sizes [i]);
2012 alloc_free_block_lists (free_block_lists);
2014 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
2015 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
2016 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
2017 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
2019 LOCK_INIT (ms_block_list_mutex);
2021 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
2022 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
2023 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
2024 mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_time_wait_for_sweep);
2025 #ifdef SGEN_PARALLEL_MARK
2026 mono_counters_register ("Slots allocated in vain", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_slots_allocated_in_vain);
2028 #ifndef HAVE_KW_THREAD
2029 pthread_key_create (&workers_free_block_lists_key, NULL);
2034 * FIXME: These are superfluous if concurrent sweep is
2035 * disabled. We might want to create them lazily.
2037 MONO_SEM_INIT (&ms_sweep_cmd_semaphore, 0);
2038 MONO_SEM_INIT (&ms_sweep_done_semaphore, 0);
2040 collector->section_size = MAJOR_SECTION_SIZE;
2041 #ifdef SGEN_PARALLEL_MARK
2042 collector->is_parallel = TRUE;
2043 collector->alloc_worker_data = major_alloc_worker_data;
2044 collector->init_worker_thread = major_init_worker_thread;
2045 collector->reset_worker_data = major_reset_worker_data;
2047 collector->is_parallel = FALSE;
2049 collector->supports_cardtable = TRUE;
2051 collector->have_swept = &have_swept;
2053 collector->alloc_heap = major_alloc_heap;
2054 collector->is_object_live = major_is_object_live;
2055 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
2056 collector->alloc_degraded = major_alloc_degraded;
2057 collector->copy_or_mark_object = major_copy_or_mark_object;
2058 collector->alloc_object = major_alloc_object;
2059 collector->free_pinned_object = free_pinned_object;
2060 collector->iterate_objects = major_iterate_objects;
2061 collector->free_non_pinned_object = major_free_non_pinned_object;
2062 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
2063 collector->pin_objects = major_pin_objects;
2064 #ifdef SGEN_HAVE_CARDTABLE
2065 collector->scan_card_table = major_scan_card_table;
2066 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
2068 collector->init_to_space = major_init_to_space;
2069 collector->sweep = major_sweep;
2070 collector->check_scan_starts = major_check_scan_starts;
2071 collector->dump_heap = major_dump_heap;
2072 collector->get_used_size = major_get_used_size;
2073 collector->start_nursery_collection = major_start_nursery_collection;
2074 collector->finish_nursery_collection = major_finish_nursery_collection;
2075 collector->start_major_collection = major_start_major_collection;
2076 collector->finish_major_collection = major_finish_major_collection;
2077 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
2078 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
2079 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
2080 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
2081 collector->get_num_major_sections = get_num_major_sections;
2082 collector->handle_gc_param = major_handle_gc_param;
2083 collector->print_gc_param_usage = major_print_gc_param_usage;
2084 collector->is_worker_thread = major_is_worker_thread;
2085 collector->post_param_init = post_param_init;
2087 FILL_COLLECTOR_COPY_OBJECT (collector);
2088 FILL_COLLECTOR_SCAN_OBJECT (collector);
2090 #ifdef SGEN_HAVE_CARDTABLE
2091 /*cardtable requires major pages to be 8 cards aligned*/
2092 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);