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)
180 #define LOCK_MS_BLOCK_LIST
181 #define UNLOCK_MS_BLOCK_LIST
184 /* we get this at init */
185 static int nursery_bits;
186 static char *nursery_start;
187 static char *nursery_end;
189 static gboolean *evacuate_block_obj_sizes;
190 static float evacuation_threshold = 0.666;
192 static gboolean concurrent_sweep = FALSE;
193 static gboolean have_swept;
195 #define ptr_in_nursery(p) (SGEN_PTR_IN_NURSERY ((p), nursery_bits, nursery_start, nursery_end))
197 /* all allocated blocks in the system */
198 static MSBlockInfo *all_blocks;
201 /* non-allocated block free-list */
202 static MSBlockInfo *empty_blocks = NULL;
204 /* non-allocated block free-list */
205 static void *empty_blocks = NULL;
206 static int num_empty_blocks = 0;
209 #define FOREACH_BLOCK(bl) for ((bl) = all_blocks; (bl); (bl) = (bl)->next) {
210 #define END_FOREACH_BLOCK }
212 static int num_major_sections = 0;
213 /* one free block list for each block object size */
214 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
216 static long long stat_major_blocks_alloced = 0;
217 static long long stat_major_blocks_freed = 0;
218 static long long stat_major_objects_evacuated = 0;
219 static long long stat_time_wait_for_sweep = 0;
220 #ifdef SGEN_PARALLEL_MARK
221 static long long stat_slots_allocated_in_vain = 0;
224 static gboolean ms_sweep_in_progress = FALSE;
225 static pthread_t ms_sweep_thread;
226 static MonoSemType ms_sweep_cmd_semaphore;
227 static MonoSemType ms_sweep_done_semaphore;
230 ms_signal_sweep_command (void)
232 if (!concurrent_sweep)
235 g_assert (!ms_sweep_in_progress);
236 ms_sweep_in_progress = TRUE;
237 MONO_SEM_POST (&ms_sweep_cmd_semaphore);
241 ms_signal_sweep_done (void)
243 if (!concurrent_sweep)
246 MONO_SEM_POST (&ms_sweep_done_semaphore);
250 ms_wait_for_sweep_done (void)
252 SGEN_TV_DECLARE (atv);
253 SGEN_TV_DECLARE (btv);
256 if (!concurrent_sweep)
259 if (!ms_sweep_in_progress)
262 SGEN_TV_GETTIME (atv);
263 while ((result = MONO_SEM_WAIT (&ms_sweep_done_semaphore)) != 0) {
265 g_error ("MONO_SEM_WAIT");
267 SGEN_TV_GETTIME (btv);
268 stat_time_wait_for_sweep += SGEN_TV_ELAPSED_MS (atv, btv);
270 g_assert (ms_sweep_in_progress);
271 ms_sweep_in_progress = FALSE;
275 ms_find_block_obj_size_index (int size)
278 DEBUG (9, g_assert (size <= SGEN_MAX_SMALL_OBJ_SIZE));
279 for (i = 0; i < num_block_obj_sizes; ++i)
280 if (block_obj_sizes [i] >= size)
282 g_assert_not_reached ();
285 #define FREE_BLOCKS(p,r) (free_block_lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
287 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
288 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
289 fast_block_obj_size_indexes [((s)+7)>>3] : \
290 ms_find_block_obj_size_index ((s)))
294 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
297 mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
298 mword alloc_size = nursery_size + major_heap_size;
301 g_assert (ms_heap_num_blocks > 0);
302 g_assert (nursery_size % MS_BLOCK_SIZE == 0);
304 g_assert (nursery_align % MS_BLOCK_SIZE == 0);
306 nursery_start = mono_sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE);
307 nursery_end = heap_start = nursery_start + nursery_size;
308 nursery_bits = the_nursery_bits;
310 ms_heap_end = heap_start + major_heap_size;
312 block_infos = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
314 for (i = 0; i < ms_heap_num_blocks; ++i) {
315 block_infos [i].block = heap_start + i * MS_BLOCK_SIZE;
316 if (i < ms_heap_num_blocks - 1)
317 block_infos [i].next_free = &block_infos [i + 1];
319 block_infos [i].next_free = NULL;
320 block_infos [i].zeroed = TRUE;
323 empty_blocks = &block_infos [0];
325 return nursery_start;
329 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
332 nursery_start = mono_sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
334 nursery_start = mono_sgen_alloc_os_memory (nursery_size, TRUE);
336 nursery_end = nursery_start + nursery_size;
337 nursery_bits = the_nursery_bits;
339 return nursery_start;
344 update_heap_boundaries_for_block (MSBlockInfo *block)
346 mono_sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
351 ms_get_empty_block (void)
355 g_assert (empty_blocks);
357 block = empty_blocks;
358 empty_blocks = empty_blocks->next_free;
363 memset (block->block, 0, MS_BLOCK_SIZE);
369 ms_free_block (MSBlockInfo *block)
371 block->next_free = empty_blocks;
372 empty_blocks = block;
374 block->zeroed = FALSE;
375 mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
379 ms_get_empty_block (void)
383 void *block, *empty, *next;
387 p = mono_sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
389 for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
392 * We do the free list update one after the
393 * other so that other threads can use the new
394 * blocks as quickly as possible.
397 empty = empty_blocks;
398 *(void**)block = empty;
399 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
403 SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
405 stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
409 empty = empty_blocks;
413 next = *(void**)block;
414 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
416 SGEN_ATOMIC_ADD (num_empty_blocks, -1);
418 *(void**)block = NULL;
420 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
426 ms_free_block (void *block)
430 mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
431 memset (block, 0, MS_BLOCK_SIZE);
434 empty = empty_blocks;
435 *(void**)block = empty;
436 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
438 SGEN_ATOMIC_ADD (num_empty_blocks, 1);
442 //#define MARKSWEEP_CONSISTENCY_CHECK
444 #ifdef MARKSWEEP_CONSISTENCY_CHECK
446 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
450 for (; block; block = block->next_free) {
451 g_assert (block->obj_size == size);
452 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
454 /* blocks in the free lists must have at least
456 g_assert (block->free_list);
459 /* the block must not be in the empty_blocks list */
460 for (b = empty_blocks; b; b = b->next_free)
461 g_assert (b != block);
463 /* the block must be in the all_blocks list */
464 for (b = all_blocks; b; b = b->next) {
468 g_assert (b == block);
473 check_empty_blocks (void)
478 for (p = empty_blocks; p; p = *(void**)p)
480 g_assert (i == num_empty_blocks);
485 consistency_check (void)
490 /* check all blocks */
491 FOREACH_BLOCK (block) {
492 int count = MS_BLOCK_FREE / block->obj_size;
497 /* check block header */
498 g_assert (((MSBlockHeader*)block->block)->info == block);
501 /* count number of free slots */
502 for (i = 0; i < count; ++i) {
503 void **obj = (void**) MS_BLOCK_OBJ (block, i);
504 if (!MS_OBJ_ALLOCED (obj, block))
508 /* check free list */
509 for (free = block->free_list; free; free = (void**)*free) {
510 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
513 g_assert (num_free == 0);
515 /* check all mark words are zero */
516 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
517 g_assert (block->mark_words [i] == 0);
520 /* check free blocks */
521 for (i = 0; i < num_block_obj_sizes; ++i) {
523 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
524 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
527 check_empty_blocks ();
532 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
534 int size = block_obj_sizes [size_index];
535 int count = MS_BLOCK_FREE / size;
538 MSBlockHeader *header;
540 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
544 if (!mono_sgen_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
548 info = ms_get_empty_block ();
550 info = mono_sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
553 DEBUG (9, g_assert (count >= 2));
555 info->obj_size = size;
556 info->obj_size_index = size_index;
557 info->pinned = pinned;
558 info->has_references = has_references;
559 info->has_pinned = pinned;
560 info->is_to_space = (mono_sgen_get_current_collection_generation () == GENERATION_OLD);
562 info->block = ms_get_empty_block ();
564 header = (MSBlockHeader*) info->block;
568 update_heap_boundaries_for_block (info);
570 /* build free list */
571 obj_start = info->block + MS_BLOCK_SKIP;
572 info->free_list = (void**)obj_start;
573 /* we're skipping the last one - it must be nulled */
574 for (i = 0; i < count - 1; ++i) {
575 char *next_obj_start = obj_start + size;
576 *(void**)obj_start = next_obj_start;
577 obj_start = next_obj_start;
580 *(void**)obj_start = NULL;
582 info->next_free = free_blocks [size_index];
583 free_blocks [size_index] = info;
585 info->next = all_blocks;
588 ++num_major_sections;
593 obj_is_from_pinned_alloc (char *ptr)
597 FOREACH_BLOCK (block) {
598 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
599 return block->pinned;
605 alloc_obj (int size, gboolean pinned, gboolean has_references)
607 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
608 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
612 /* FIXME: try to do this without locking */
616 g_assert (!ms_sweep_in_progress);
618 if (!free_blocks [size_index]) {
619 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references))) {
620 UNLOCK_MS_BLOCK_LIST;
625 block = free_blocks [size_index];
626 DEBUG (9, g_assert (block));
628 obj = block->free_list;
629 DEBUG (9, g_assert (obj));
631 block->free_list = *(void**)obj;
632 if (!block->free_list) {
633 free_blocks [size_index] = block->next_free;
634 block->next_free = NULL;
637 UNLOCK_MS_BLOCK_LIST;
640 * FIXME: This should not be necessary because it'll be
641 * overwritten by the vtable immediately.
649 major_alloc_object (int size, gboolean has_references)
651 return alloc_obj (size, FALSE, has_references);
655 * We're not freeing the block if it's empty. We leave that work for
656 * the next major collection.
658 * This is just called from the domain clearing code, which runs in a
659 * single thread and has the GC lock, so we don't need an extra lock.
662 free_object (char *obj, size_t size, gboolean pinned)
664 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
666 DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
667 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
668 MS_CALC_MARK_BIT (word, bit, obj);
669 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
670 if (!block->free_list) {
671 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
672 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
673 DEBUG (9, g_assert (!block->next_free));
674 block->next_free = free_blocks [size_index];
675 free_blocks [size_index] = block;
677 memset (obj, 0, size);
678 *(void**)obj = block->free_list;
679 block->free_list = (void**)obj;
683 major_free_non_pinned_object (char *obj, size_t size)
685 free_object (obj, size, FALSE);
688 /* size is a multiple of SGEN_ALLOC_ALIGN */
690 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
694 ms_wait_for_sweep_done ();
696 res = alloc_obj (size, TRUE, has_references);
697 /*If we failed to alloc memory, we better try releasing memory
698 *as pinned alloc is requested by the runtime.
701 sgen_collect_major_no_lock ("pinned alloc failure");
702 res = alloc_obj (size, TRUE, has_references);
708 free_pinned_object (char *obj, size_t size)
710 free_object (obj, size, TRUE);
714 * size is already rounded up and we hold the GC lock.
717 major_alloc_degraded (MonoVTable *vtable, size_t size)
720 int old_num_sections;
722 ms_wait_for_sweep_done ();
724 old_num_sections = num_major_sections;
726 obj = alloc_obj (size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
727 if (G_LIKELY (obj)) {
728 *(MonoVTable**)obj = vtable;
729 HEAVY_STAT (++stat_objects_alloced_degraded);
730 HEAVY_STAT (stat_bytes_alloced_degraded += size);
731 g_assert (num_major_sections >= old_num_sections);
732 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
737 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj) FALSE
740 * obj is some object. If it's not in the major heap (i.e. if it's in
741 * the nursery or LOS), return FALSE. Otherwise return whether it's
742 * been marked or copied.
745 major_is_object_live (char *obj)
753 if (ptr_in_nursery (obj))
758 if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
761 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
764 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
768 /* now we know it's in a major block */
769 block = MS_BLOCK_FOR_OBJ (obj);
770 DEBUG (9, g_assert (!block->pinned));
771 MS_CALC_MARK_BIT (word, bit, obj);
772 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
776 major_ptr_is_in_non_pinned_space (char *ptr)
780 FOREACH_BLOCK (block) {
781 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
782 return !block->pinned;
788 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
792 ms_wait_for_sweep_done ();
794 FOREACH_BLOCK (block) {
795 int count = MS_BLOCK_FREE / block->obj_size;
798 if (block->pinned && !pinned)
800 if (!block->pinned && !non_pinned)
803 for (i = 0; i < count; ++i) {
804 void **obj = (void**) MS_BLOCK_OBJ (block, i);
805 if (MS_OBJ_ALLOCED (obj, block))
806 callback ((char*)obj, block->obj_size, data);
812 major_check_scan_starts (void)
817 major_dump_heap (FILE *heap_dump_file)
820 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
821 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
824 for (i = 0; i < num_block_obj_sizes; ++i)
825 slots_available [i] = slots_used [i] = 0;
827 FOREACH_BLOCK (block) {
828 int index = ms_find_block_obj_size_index (block->obj_size);
829 int count = MS_BLOCK_FREE / block->obj_size;
831 slots_available [index] += count;
832 for (i = 0; i < count; ++i) {
833 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
834 ++slots_used [index];
838 fprintf (heap_dump_file, "<occupancies>\n");
839 for (i = 0; i < num_block_obj_sizes; ++i) {
840 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
841 block_obj_sizes [i], slots_available [i], slots_used [i]);
843 fprintf (heap_dump_file, "</occupancies>\n");
845 FOREACH_BLOCK (block) {
846 int count = MS_BLOCK_FREE / block->obj_size;
850 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
852 for (i = 0; i <= count; ++i) {
853 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
858 mono_sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
864 fprintf (heap_dump_file, "</section>\n");
868 #define LOAD_VTABLE SGEN_LOAD_VTABLE
870 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do { \
872 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
873 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
874 MS_SET_MARK_BIT ((block), __word, __bit); \
875 if ((block)->has_references) \
876 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
877 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
880 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
882 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
883 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
884 if (!MS_MARK_BIT ((block), __word, __bit)) { \
885 MS_SET_MARK_BIT ((block), __word, __bit); \
886 if ((block)->has_references) \
887 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
888 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
891 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
893 gboolean __was_marked; \
894 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
895 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
896 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
897 if (!__was_marked) { \
898 if ((block)->has_references) \
899 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
900 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
904 #include "sgen-major-copy-object.h"
906 #ifdef SGEN_PARALLEL_MARK
908 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
915 HEAVY_STAT (++stat_copy_object_called_major);
917 DEBUG (9, g_assert (obj));
918 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
920 if (ptr_in_nursery (obj)) {
922 gboolean has_references;
924 mword vtable_word = *(mword*)obj;
925 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
927 if (vtable_word & SGEN_FORWARDED_BIT) {
932 if (vtable_word & SGEN_PINNED_BIT)
935 HEAVY_STAT (++stat_objects_copied_major);
938 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
939 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
941 destination = major_alloc_object (objsize, has_references);
942 if (G_UNLIKELY (!destination)) {
943 if (!ptr_in_nursery (obj)) {
945 block = MS_BLOCK_FOR_OBJ (obj);
946 size_index = block->obj_size_index;
947 evacuate_block_obj_sizes [size_index] = FALSE;
951 if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
952 mono_sgen_pin_object (obj, queue);
956 vtable_word = *(mword*)obj;
957 /*someone else forwarded it, update the pointer and bail out*/
958 if (vtable_word & SGEN_FORWARDED_BIT) {
959 *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
963 /*someone pinned it, nothing to do.*/
964 if (vtable_word & SGEN_PINNED_BIT)
970 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
973 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
978 * FIXME: If we make major_alloc_object() give
979 * us the block info, too, we won't have to
982 block = MS_BLOCK_FOR_OBJ (obj);
983 MS_CALC_MARK_BIT (word, bit, obj);
984 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
985 MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
988 * FIXME: We have allocated destination, but
989 * we cannot use it. Give it back to the
992 *(void**)destination = NULL;
994 vtable_word = *(mword*)obj;
995 g_assert (vtable_word & SGEN_FORWARDED_BIT);
997 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1001 ++stat_slots_allocated_in_vain;
1005 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1007 mword vtable_word = *(mword*)obj;
1008 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1010 /* see comment in the non-parallel version below */
1011 if (vtable_word & SGEN_FORWARDED_BIT) {
1015 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
1017 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1022 block = MS_BLOCK_FOR_OBJ (obj);
1023 size_index = block->obj_size_index;
1025 if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1026 if (block->is_to_space)
1031 mword vtable_word = *(mword*)obj;
1032 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1034 if (vtable_word & SGEN_FORWARDED_BIT) {
1041 HEAVY_STAT (++stat_major_objects_evacuated);
1042 goto do_copy_object;
1045 MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1048 mword vtable_word = *(mword*)obj;
1049 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1052 if (vtable_word & SGEN_PINNED_BIT)
1054 binary_protocol_pin (obj, vt, mono_sgen_safe_object_get_size ((MonoObject*)obj));
1055 if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
1056 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1057 GRAY_OBJECT_ENQUEUE (queue, obj);
1059 g_assert (SGEN_OBJECT_IS_PINNED (obj));
1066 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1071 HEAVY_STAT (++stat_copy_object_called_major);
1073 DEBUG (9, g_assert (obj));
1074 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
1076 if (ptr_in_nursery (obj)) {
1078 char *forwarded, *old_obj;
1080 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1084 if (SGEN_OBJECT_IS_PINNED (obj))
1087 HEAVY_STAT (++stat_objects_copied_major);
1091 obj = copy_object_no_checks (obj, queue);
1092 if (G_UNLIKELY (old_obj == obj)) {
1093 /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1094 if (!ptr_in_nursery (obj)) {
1096 block = MS_BLOCK_FOR_OBJ (obj);
1097 size_index = block->obj_size_index;
1098 evacuate_block_obj_sizes [size_index] = FALSE;
1099 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1106 * FIXME: See comment for copy_object_no_checks(). If
1107 * we have that, we can let the allocation function
1108 * give us the block info, too, and we won't have to
1111 block = MS_BLOCK_FOR_OBJ (obj);
1112 MS_CALC_MARK_BIT (word, bit, obj);
1113 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
1114 MS_SET_MARK_BIT (block, word, bit);
1118 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1123 * If we have don't have a fixed heap we cannot know
1124 * whether an object is in the LOS or in the small
1125 * object major heap without checking its size. To do
1126 * that, however, we need to know that we actually
1127 * have a valid object, not a forwarding pointer, so
1128 * we have to do this check first.
1130 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1135 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
1137 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1143 block = MS_BLOCK_FOR_OBJ (obj);
1144 size_index = block->obj_size_index;
1145 evacuate = evacuate_block_obj_sizes [size_index];
1149 * We could also check for !block->has_pinned
1150 * here, but it would only make an uncommon case
1151 * faster, namely objects that are in blocks
1152 * whose slot sizes are evacuated but which have
1155 if (evacuate && (forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1161 if (evacuate && !block->has_pinned) {
1162 if (block->is_to_space)
1164 HEAVY_STAT (++stat_major_objects_evacuated);
1165 goto do_copy_object;
1167 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1170 if (SGEN_OBJECT_IS_PINNED (obj))
1172 binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), mono_sgen_safe_object_get_size ((MonoObject*)obj));
1173 SGEN_PIN_OBJECT (obj);
1174 /* FIXME: only enqueue if object has references */
1175 GRAY_OBJECT_ENQUEUE (queue, obj);
1181 #include "sgen-major-scan-object.h"
1184 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1187 int last_index = -1;
1189 if (!block->pin_queue_num_entries)
1192 block->has_pinned = TRUE;
1194 for (i = 0; i < block->pin_queue_num_entries; ++i) {
1195 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1196 DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
1197 if (index == last_index)
1199 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1210 /* statistics for evacuation */
1211 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1212 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1213 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1215 for (i = 0; i < num_block_obj_sizes; ++i)
1216 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1218 /* clear all the free lists */
1219 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1220 MSBlockInfo **free_blocks = free_block_lists [i];
1222 for (j = 0; j < num_block_obj_sizes; ++j)
1223 free_blocks [j] = NULL;
1226 /* traverse all blocks, free and zero unmarked objects */
1229 MSBlockInfo *block = *iter;
1231 gboolean have_live = FALSE;
1232 gboolean has_pinned;
1236 obj_size_index = block->obj_size_index;
1238 has_pinned = block->has_pinned;
1239 block->has_pinned = block->pinned;
1241 block->is_to_space = FALSE;
1243 count = MS_BLOCK_FREE / block->obj_size;
1244 block->free_list = NULL;
1246 for (obj_index = 0; obj_index < count; ++obj_index) {
1248 void *obj = MS_BLOCK_OBJ (block, obj_index);
1250 MS_CALC_MARK_BIT (word, bit, obj);
1251 if (MS_MARK_BIT (block, word, bit)) {
1252 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
1255 ++slots_used [obj_size_index];
1257 /* an unmarked object */
1258 if (MS_OBJ_ALLOCED (obj, block)) {
1259 binary_protocol_empty (obj, block->obj_size);
1260 memset (obj, 0, block->obj_size);
1262 *(void**)obj = block->free_list;
1263 block->free_list = obj;
1267 /* reset mark bits */
1268 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1271 * FIXME: reverse free list so that it's in address
1277 ++num_blocks [obj_size_index];
1278 slots_available [obj_size_index] += count;
1281 iter = &block->next;
1284 * If there are free slots in the block, add
1285 * the block to the corresponding free list.
1287 if (block->free_list) {
1288 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1289 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1290 block->next_free = free_blocks [index];
1291 free_blocks [index] = block;
1294 update_heap_boundaries_for_block (block);
1297 * Blocks without live objects are removed from the
1298 * block list and freed.
1300 *iter = block->next;
1303 ms_free_block (block);
1305 ms_free_block (block->block);
1307 mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1310 --num_major_sections;
1314 for (i = 0; i < num_block_obj_sizes; ++i) {
1315 float usage = (float)slots_used [i] / (float)slots_available [i];
1316 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1317 evacuate_block_obj_sizes [i] = TRUE;
1319 g_print ("slot size %d - %d of %d used\n",
1320 block_obj_sizes [i], slots_used [i], slots_available [i]);
1323 evacuate_block_obj_sizes [i] = FALSE;
1331 ms_sweep_thread_func (void *dummy)
1333 g_assert (concurrent_sweep);
1338 while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
1340 g_error ("MONO_SEM_WAIT");
1345 ms_signal_sweep_done ();
1354 if (concurrent_sweep) {
1355 g_assert (ms_sweep_thread);
1356 ms_signal_sweep_command ();
1362 static int count_pinned_ref;
1363 static int count_pinned_nonref;
1364 static int count_nonpinned_ref;
1365 static int count_nonpinned_nonref;
1368 count_nonpinned_callback (char *obj, size_t size, void *data)
1370 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1372 if (vtable->klass->has_references)
1373 ++count_nonpinned_ref;
1375 ++count_nonpinned_nonref;
1379 count_pinned_callback (char *obj, size_t size, void *data)
1381 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1383 if (vtable->klass->has_references)
1386 ++count_pinned_nonref;
1389 static void __attribute__ ((unused))
1390 count_ref_nonref_objs (void)
1394 count_pinned_ref = 0;
1395 count_pinned_nonref = 0;
1396 count_nonpinned_ref = 0;
1397 count_nonpinned_nonref = 0;
1399 major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1400 major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1402 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1404 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1405 count_pinned_ref, count_nonpinned_ref,
1406 count_pinned_nonref, count_nonpinned_nonref,
1407 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1411 ms_calculate_block_obj_sizes (double factor, int *arr)
1413 double target_size = sizeof (MonoObject);
1418 int target_count = ceil (MS_BLOCK_FREE / target_size);
1419 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1421 if (size != last_size) {
1423 arr [num_sizes] = size;
1428 target_size *= factor;
1429 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1434 /* only valid during minor collections */
1435 static int old_num_major_sections;
1438 major_start_nursery_collection (void)
1440 ms_wait_for_sweep_done ();
1442 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1443 consistency_check ();
1446 old_num_major_sections = num_major_sections;
1450 major_finish_nursery_collection (void)
1452 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1453 consistency_check ();
1455 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1459 major_start_major_collection (void)
1463 ms_wait_for_sweep_done ();
1465 /* clear the free lists */
1466 for (i = 0; i < num_block_obj_sizes; ++i) {
1467 if (!evacuate_block_obj_sizes [i])
1470 free_block_lists [0][i] = NULL;
1471 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1476 major_finish_major_collection (void)
1481 major_have_computer_minor_collection_allowance (void)
1484 int section_reserve = mono_sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1486 g_assert (have_swept);
1487 ms_wait_for_sweep_done ();
1488 g_assert (!ms_sweep_in_progress);
1491 * FIXME: We don't free blocks on 32 bit platforms because it
1492 * can lead to address space fragmentation, since we're
1493 * allocating blocks in larger contingents.
1495 if (sizeof (mword) < 8)
1498 while (num_empty_blocks > section_reserve) {
1499 void *next = *(void**)empty_blocks;
1500 mono_sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
1501 empty_blocks = next;
1503 * Needs not be atomic because this is running
1508 ++stat_major_blocks_freed;
1514 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1518 FOREACH_BLOCK (block) {
1519 block->pin_queue_start = mono_sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
1520 &block->pin_queue_num_entries);
1521 } END_FOREACH_BLOCK;
1525 major_pin_objects (SgenGrayQueue *queue)
1529 FOREACH_BLOCK (block) {
1530 mark_pinned_objects_in_block (block, queue);
1531 } END_FOREACH_BLOCK;
1535 major_init_to_space (void)
1540 major_report_pinned_memory_usage (void)
1542 g_assert_not_reached ();
1546 major_get_used_size (void)
1551 FOREACH_BLOCK (block) {
1552 int count = MS_BLOCK_FREE / block->obj_size;
1554 size += count * block->obj_size;
1555 for (iter = block->free_list; iter; iter = (void**)*iter)
1556 size -= block->obj_size;
1557 } END_FOREACH_BLOCK;
1563 get_num_major_sections (void)
1565 return num_major_sections;
1569 major_handle_gc_param (const char *opt)
1572 if (g_str_has_prefix (opt, "major-heap-size=")) {
1573 const char *arg = strchr (opt, '=') + 1;
1575 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
1577 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1578 g_assert (ms_heap_num_blocks > 0);
1582 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1583 const char *arg = strchr (opt, '=') + 1;
1584 int percentage = atoi (arg);
1585 if (percentage < 0 || percentage > 100) {
1586 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1589 evacuation_threshold = (float)percentage / 100.0;
1591 } else if (!strcmp (opt, "concurrent-sweep")) {
1592 concurrent_sweep = TRUE;
1594 } else if (!strcmp (opt, "no-concurrent-sweep")) {
1595 concurrent_sweep = FALSE;
1603 major_print_gc_param_usage (void)
1608 " major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1610 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1611 " (no-)concurrent-sweep\n"
1615 #ifdef SGEN_HAVE_CARDTABLE
1617 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1621 FOREACH_BLOCK (block) {
1622 if (block->has_references)
1623 callback ((mword)block->block, MS_BLOCK_SIZE);
1624 } END_FOREACH_BLOCK;
1627 #ifdef HEAVY_STATISTICS
1628 extern long long marked_cards;
1629 extern long long scanned_cards;
1630 extern long long scanned_objects;
1634 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1636 * MS blocks are 16K aligned.
1637 * Cardtables are 4K aligned, at least.
1638 * This means that the cardtable of a given block is 32 bytes aligned.
1641 initial_skip_card (guint8 *card_data)
1643 mword *cards = (mword*)card_data;
1646 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1652 if (i == CARD_WORDS_PER_BLOCK)
1653 return card_data + CARDS_PER_BLOCK;
1655 #if defined(__i386__) && defined(__GNUC__)
1656 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1657 #elif defined(__x86_64__) && defined(__GNUC__)
1658 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1660 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1662 return &card_data [i];
1669 static G_GNUC_UNUSED guint8*
1670 skip_card (guint8 *card_data, guint8 *card_data_end)
1672 while (card_data < card_data_end && !*card_data)
1677 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1678 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1679 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1682 major_scan_card_table (SgenGrayQueue *queue)
1686 FOREACH_BLOCK (block) {
1690 if (!block->has_references)
1693 block_obj_size = block->obj_size;
1694 block_start = block->block;
1696 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1698 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1699 guint8 cards_data [CARDS_PER_BLOCK];
1701 char *obj, *end, *base;
1703 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1704 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1705 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1708 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1712 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1713 end = block_start + MS_BLOCK_SIZE;
1714 base = sgen_card_table_align_pointer (obj);
1717 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1718 int card_offset = (obj - base) >> CARD_BITS;
1719 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
1721 obj += block_obj_size;
1724 guint8 *card_data, *card_base;
1725 guint8 *card_data_end;
1728 * This is safe in face of card aliasing for the following reason:
1730 * Major blocks are 16k aligned, or 32 cards aligned.
1731 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1732 * sizes, they won't overflow the cardtable overlap modulus.
1734 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1735 card_data_end = card_data + CARDS_PER_BLOCK;
1737 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)) {
1739 int idx = card_data - card_base;
1740 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1741 char *end = start + CARD_SIZE_IN_BYTES;
1744 HEAVY_STAT (++scanned_cards);
1749 HEAVY_STAT (++marked_cards);
1751 sgen_card_table_prepare_card_for_scanning (card_data);
1756 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1758 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1760 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1761 HEAVY_STAT (++scanned_objects);
1762 minor_scan_object (obj, queue);
1764 obj += block_obj_size;
1768 } END_FOREACH_BLOCK;
1773 major_is_worker_thread (pthread_t thread)
1775 if (concurrent_sweep)
1776 return thread == ms_sweep_thread;
1781 #undef pthread_create
1784 post_param_init (void)
1786 if (concurrent_sweep) {
1787 if (pthread_create (&ms_sweep_thread, NULL, ms_sweep_thread_func, NULL)) {
1788 fprintf (stderr, "Error: Could not create sweep thread.\n");
1795 #ifdef SGEN_PARALLEL_MARK
1797 mono_sgen_marksweep_fixed_par_init
1799 mono_sgen_marksweep_par_init
1803 mono_sgen_marksweep_fixed_init
1805 mono_sgen_marksweep_init
1808 (SgenMajorCollector *collector)
1813 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1816 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
1817 block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1818 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
1820 evacuate_block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1821 for (i = 0; i < num_block_obj_sizes; ++i)
1822 evacuate_block_obj_sizes [i] = FALSE;
1827 g_print ("block object sizes:\n");
1828 for (i = 0; i < num_block_obj_sizes; ++i)
1829 g_print ("%d\n", block_obj_sizes [i]);
1833 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1834 free_block_lists [i] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1836 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
1837 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
1838 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
1839 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
1841 LOCK_INIT (ms_block_list_mutex);
1843 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
1844 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
1845 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
1846 mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_time_wait_for_sweep);
1847 #ifdef SGEN_PARALLEL_MARK
1848 mono_counters_register ("Slots allocated in vain", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_slots_allocated_in_vain);
1852 * FIXME: These are superfluous if concurrent sweep is
1853 * disabled. We might want to create them lazily.
1855 MONO_SEM_INIT (&ms_sweep_cmd_semaphore, 0);
1856 MONO_SEM_INIT (&ms_sweep_done_semaphore, 0);
1858 collector->section_size = MAJOR_SECTION_SIZE;
1859 #ifdef SGEN_PARALLEL_MARK
1860 collector->is_parallel = TRUE;
1862 collector->is_parallel = FALSE;
1864 collector->supports_cardtable = TRUE;
1866 collector->have_swept = &have_swept;
1868 collector->alloc_heap = major_alloc_heap;
1869 collector->is_object_live = major_is_object_live;
1870 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
1871 collector->alloc_degraded = major_alloc_degraded;
1872 collector->copy_or_mark_object = major_copy_or_mark_object;
1873 collector->alloc_object = major_alloc_object;
1874 collector->free_pinned_object = free_pinned_object;
1875 collector->iterate_objects = major_iterate_objects;
1876 collector->free_non_pinned_object = major_free_non_pinned_object;
1877 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
1878 collector->pin_objects = major_pin_objects;
1879 #ifdef SGEN_HAVE_CARDTABLE
1880 collector->scan_card_table = major_scan_card_table;
1881 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
1883 collector->init_to_space = major_init_to_space;
1884 collector->sweep = major_sweep;
1885 collector->check_scan_starts = major_check_scan_starts;
1886 collector->dump_heap = major_dump_heap;
1887 collector->get_used_size = major_get_used_size;
1888 collector->start_nursery_collection = major_start_nursery_collection;
1889 collector->finish_nursery_collection = major_finish_nursery_collection;
1890 collector->start_major_collection = major_start_major_collection;
1891 collector->finish_major_collection = major_finish_major_collection;
1892 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
1893 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
1894 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
1895 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
1896 collector->get_num_major_sections = get_num_major_sections;
1897 collector->handle_gc_param = major_handle_gc_param;
1898 collector->print_gc_param_usage = major_print_gc_param_usage;
1899 collector->is_worker_thread = major_is_worker_thread;
1900 collector->post_param_init = post_param_init;
1902 FILL_COLLECTOR_COPY_OBJECT (collector);
1903 FILL_COLLECTOR_SCAN_OBJECT (collector);
1905 #ifdef SGEN_HAVE_CARDTABLE
1906 /*cardtable requires major pages to be 8 cards aligned*/
1907 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);