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;
221 static gboolean ms_sweep_in_progress = FALSE;
222 static pthread_t ms_sweep_thread;
223 static MonoSemType ms_sweep_cmd_semaphore;
224 static MonoSemType ms_sweep_done_semaphore;
227 ms_signal_sweep_command (void)
229 if (!concurrent_sweep)
232 g_assert (!ms_sweep_in_progress);
233 ms_sweep_in_progress = TRUE;
234 MONO_SEM_POST (&ms_sweep_cmd_semaphore);
238 ms_signal_sweep_done (void)
240 if (!concurrent_sweep)
243 MONO_SEM_POST (&ms_sweep_done_semaphore);
247 ms_wait_for_sweep_done (void)
249 SGEN_TV_DECLARE (atv);
250 SGEN_TV_DECLARE (btv);
253 if (!concurrent_sweep)
256 if (!ms_sweep_in_progress)
259 SGEN_TV_GETTIME (atv);
260 while ((result = MONO_SEM_WAIT (&ms_sweep_done_semaphore)) != 0) {
262 g_error ("MONO_SEM_WAIT");
264 SGEN_TV_GETTIME (btv);
265 stat_time_wait_for_sweep += SGEN_TV_ELAPSED_MS (atv, btv);
267 g_assert (ms_sweep_in_progress);
268 ms_sweep_in_progress = FALSE;
272 ms_find_block_obj_size_index (int size)
275 DEBUG (9, g_assert (size <= SGEN_MAX_SMALL_OBJ_SIZE));
276 for (i = 0; i < num_block_obj_sizes; ++i)
277 if (block_obj_sizes [i] >= size)
279 g_assert_not_reached ();
282 #define FREE_BLOCKS(p,r) (free_block_lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
284 #define MS_BLOCK_OBJ_SIZE_INDEX(s) \
285 (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ? \
286 fast_block_obj_size_indexes [((s)+7)>>3] : \
287 ms_find_block_obj_size_index ((s)))
291 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
294 mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
295 mword alloc_size = nursery_size + major_heap_size;
298 g_assert (ms_heap_num_blocks > 0);
299 g_assert (nursery_size % MS_BLOCK_SIZE == 0);
301 g_assert (nursery_align % MS_BLOCK_SIZE == 0);
303 nursery_start = mono_sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE);
304 nursery_end = heap_start = nursery_start + nursery_size;
305 nursery_bits = the_nursery_bits;
307 ms_heap_end = heap_start + major_heap_size;
309 block_infos = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
311 for (i = 0; i < ms_heap_num_blocks; ++i) {
312 block_infos [i].block = heap_start + i * MS_BLOCK_SIZE;
313 if (i < ms_heap_num_blocks - 1)
314 block_infos [i].next_free = &block_infos [i + 1];
316 block_infos [i].next_free = NULL;
317 block_infos [i].zeroed = TRUE;
320 empty_blocks = &block_infos [0];
322 return nursery_start;
326 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
329 nursery_start = mono_sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
331 nursery_start = mono_sgen_alloc_os_memory (nursery_size, TRUE);
333 nursery_end = nursery_start + nursery_size;
334 nursery_bits = the_nursery_bits;
336 return nursery_start;
341 update_heap_boundaries_for_block (MSBlockInfo *block)
343 mono_sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
348 ms_get_empty_block (void)
352 g_assert (empty_blocks);
354 block = empty_blocks;
355 empty_blocks = empty_blocks->next_free;
360 memset (block->block, 0, MS_BLOCK_SIZE);
366 ms_free_block (MSBlockInfo *block)
368 block->next_free = empty_blocks;
369 empty_blocks = block;
371 block->zeroed = FALSE;
372 mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
376 ms_get_empty_block (void)
380 void *block, *empty, *next;
384 p = mono_sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
386 for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
389 * We do the free list update one after the
390 * other so that other threads can use the new
391 * blocks as quickly as possible.
394 empty = empty_blocks;
395 *(void**)block = empty;
396 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
400 SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
402 stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
406 empty = empty_blocks;
410 next = *(void**)block;
411 } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
413 SGEN_ATOMIC_ADD (num_empty_blocks, -1);
415 *(void**)block = NULL;
417 g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
423 ms_free_block (void *block)
427 mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
428 memset (block, 0, MS_BLOCK_SIZE);
431 empty = empty_blocks;
432 *(void**)block = empty;
433 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
435 SGEN_ATOMIC_ADD (num_empty_blocks, 1);
439 //#define MARKSWEEP_CONSISTENCY_CHECK
441 #ifdef MARKSWEEP_CONSISTENCY_CHECK
443 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
447 for (; block; block = block->next_free) {
448 g_assert (block->obj_size == size);
449 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
451 /* blocks in the free lists must have at least
453 g_assert (block->free_list);
456 /* the block must not be in the empty_blocks list */
457 for (b = empty_blocks; b; b = b->next_free)
458 g_assert (b != block);
460 /* the block must be in the all_blocks list */
461 for (b = all_blocks; b; b = b->next) {
465 g_assert (b == block);
470 check_empty_blocks (void)
475 for (p = empty_blocks; p; p = *(void**)p)
477 g_assert (i == num_empty_blocks);
482 consistency_check (void)
487 /* check all blocks */
488 FOREACH_BLOCK (block) {
489 int count = MS_BLOCK_FREE / block->obj_size;
494 /* check block header */
495 g_assert (((MSBlockHeader*)block->block)->info == block);
498 /* count number of free slots */
499 for (i = 0; i < count; ++i) {
500 void **obj = (void**) MS_BLOCK_OBJ (block, i);
501 if (!MS_OBJ_ALLOCED (obj, block))
505 /* check free list */
506 for (free = block->free_list; free; free = (void**)*free) {
507 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
510 g_assert (num_free == 0);
512 /* check all mark words are zero */
513 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
514 g_assert (block->mark_words [i] == 0);
517 /* check free blocks */
518 for (i = 0; i < num_block_obj_sizes; ++i) {
520 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
521 check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
524 check_empty_blocks ();
529 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
531 int size = block_obj_sizes [size_index];
532 int count = MS_BLOCK_FREE / size;
535 MSBlockHeader *header;
537 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
541 if (!mono_sgen_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
545 info = ms_get_empty_block ();
547 info = mono_sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
550 DEBUG (9, g_assert (count >= 2));
552 info->obj_size = size;
553 info->obj_size_index = size_index;
554 info->pinned = pinned;
555 info->has_references = has_references;
556 info->has_pinned = pinned;
557 info->is_to_space = (mono_sgen_get_current_collection_generation () == GENERATION_OLD);
559 info->block = ms_get_empty_block ();
561 header = (MSBlockHeader*) info->block;
565 update_heap_boundaries_for_block (info);
567 /* build free list */
568 obj_start = info->block + MS_BLOCK_SKIP;
569 info->free_list = (void**)obj_start;
570 /* we're skipping the last one - it must be nulled */
571 for (i = 0; i < count - 1; ++i) {
572 char *next_obj_start = obj_start + size;
573 *(void**)obj_start = next_obj_start;
574 obj_start = next_obj_start;
577 *(void**)obj_start = NULL;
579 info->next_free = free_blocks [size_index];
580 free_blocks [size_index] = info;
582 info->next = all_blocks;
585 ++num_major_sections;
590 obj_is_from_pinned_alloc (char *obj)
592 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
593 return block->pinned;
597 alloc_obj (int size, gboolean pinned, gboolean has_references)
599 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
600 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
604 /* FIXME: try to do this without locking */
608 g_assert (!ms_sweep_in_progress);
610 if (!free_blocks [size_index]) {
611 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references))) {
612 UNLOCK_MS_BLOCK_LIST;
617 block = free_blocks [size_index];
618 DEBUG (9, g_assert (block));
620 obj = block->free_list;
621 DEBUG (9, g_assert (obj));
623 block->free_list = *(void**)obj;
624 if (!block->free_list) {
625 free_blocks [size_index] = block->next_free;
626 block->next_free = NULL;
629 UNLOCK_MS_BLOCK_LIST;
632 * FIXME: This should not be necessary because it'll be
633 * overwritten by the vtable immediately.
641 major_alloc_object (int size, gboolean has_references)
643 return alloc_obj (size, FALSE, has_references);
647 * We're not freeing the block if it's empty. We leave that work for
648 * the next major collection.
650 * This is just called from the domain clearing code, which runs in a
651 * single thread and has the GC lock, so we don't need an extra lock.
654 free_object (char *obj, size_t size, gboolean pinned)
656 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
658 DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
659 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
660 MS_CALC_MARK_BIT (word, bit, obj);
661 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
662 if (!block->free_list) {
663 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
664 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
665 DEBUG (9, g_assert (!block->next_free));
666 block->next_free = free_blocks [size_index];
667 free_blocks [size_index] = block;
669 memset (obj, 0, size);
670 *(void**)obj = block->free_list;
671 block->free_list = (void**)obj;
675 major_free_non_pinned_object (char *obj, size_t size)
677 free_object (obj, size, FALSE);
680 /* size is a multiple of SGEN_ALLOC_ALIGN */
682 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
686 ms_wait_for_sweep_done ();
688 res = alloc_obj (size, TRUE, has_references);
689 /*If we failed to alloc memory, we better try releasing memory
690 *as pinned alloc is requested by the runtime.
693 sgen_collect_major_no_lock ("pinned alloc failure");
694 res = alloc_obj (size, TRUE, has_references);
700 free_pinned_object (char *obj, size_t size)
702 free_object (obj, size, TRUE);
706 * size is already rounded up and we hold the GC lock.
709 major_alloc_degraded (MonoVTable *vtable, size_t size)
712 int old_num_sections;
714 ms_wait_for_sweep_done ();
716 old_num_sections = num_major_sections;
718 obj = alloc_obj (size, FALSE, vtable->klass->has_references);
719 if (G_LIKELY (obj)) {
720 *(MonoVTable**)obj = vtable;
721 HEAVY_STAT (++stat_objects_alloced_degraded);
722 HEAVY_STAT (stat_bytes_alloced_degraded += size);
723 g_assert (num_major_sections >= old_num_sections);
724 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
729 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj) FALSE
732 * obj is some object. If it's not in the major heap (i.e. if it's in
733 * the nursery or LOS), return FALSE. Otherwise return whether it's
734 * been marked or copied.
737 major_is_object_live (char *obj)
745 if (ptr_in_nursery (obj))
750 if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
753 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
756 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
760 /* now we know it's in a major block */
761 block = MS_BLOCK_FOR_OBJ (obj);
762 DEBUG (9, g_assert (!block->pinned));
763 MS_CALC_MARK_BIT (word, bit, obj);
764 return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
768 major_ptr_is_in_non_pinned_space (char *ptr)
772 FOREACH_BLOCK (block) {
773 if (ptr >= (char*)block && ptr <= (char*)block + MS_BLOCK_SIZE)
780 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
784 ms_wait_for_sweep_done ();
786 FOREACH_BLOCK (block) {
787 int count = MS_BLOCK_FREE / block->obj_size;
790 if (block->pinned && !pinned)
792 if (!block->pinned && !non_pinned)
795 for (i = 0; i < count; ++i) {
796 void **obj = (void**) MS_BLOCK_OBJ (block, i);
797 if (MS_OBJ_ALLOCED (obj, block))
798 callback ((char*)obj, block->obj_size, data);
804 major_check_scan_starts (void)
809 major_dump_heap (FILE *heap_dump_file)
812 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
813 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
816 for (i = 0; i < num_block_obj_sizes; ++i)
817 slots_available [i] = slots_used [i] = 0;
819 FOREACH_BLOCK (block) {
820 int index = ms_find_block_obj_size_index (block->obj_size);
821 int count = MS_BLOCK_FREE / block->obj_size;
823 slots_available [index] += count;
824 for (i = 0; i < count; ++i) {
825 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
826 ++slots_used [index];
830 fprintf (heap_dump_file, "<occupancies>\n");
831 for (i = 0; i < num_block_obj_sizes; ++i) {
832 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
833 block_obj_sizes [i], slots_available [i], slots_used [i]);
835 fprintf (heap_dump_file, "</occupancies>\n");
837 FOREACH_BLOCK (block) {
838 int count = MS_BLOCK_FREE / block->obj_size;
842 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
844 for (i = 0; i <= count; ++i) {
845 if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
850 mono_sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
856 fprintf (heap_dump_file, "</section>\n");
860 #define LOAD_VTABLE SGEN_LOAD_VTABLE
862 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do { \
864 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
865 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
866 MS_SET_MARK_BIT ((block), __word, __bit); \
867 if ((block)->has_references) \
868 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
869 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
872 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
874 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
875 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
876 if (!MS_MARK_BIT ((block), __word, __bit)) { \
877 MS_SET_MARK_BIT ((block), __word, __bit); \
878 if ((block)->has_references) \
879 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
880 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
883 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
885 gboolean __was_marked; \
886 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
887 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
888 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
889 if (!__was_marked) { \
890 if ((block)->has_references) \
891 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
892 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
896 #include "sgen-major-copy-object.h"
898 #ifdef SGEN_PARALLEL_MARK
900 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
903 mword vtable_word = *(mword*)obj;
904 MonoVTable *vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
908 HEAVY_STAT (++stat_copy_object_called_major);
910 DEBUG (9, g_assert (obj));
911 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
913 if (vtable_word & SGEN_FORWARDED_BIT) {
918 if (ptr_in_nursery (obj)) {
920 gboolean has_references;
923 if (vtable_word & SGEN_PINNED_BIT)
926 HEAVY_STAT (++stat_objects_copied_major);
929 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
930 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
932 destination = major_alloc_object (objsize, has_references);
933 if (G_UNLIKELY (!destination)) {
934 if (!ptr_in_nursery (obj)) {
936 block = MS_BLOCK_FOR_OBJ (obj);
937 size_index = block->obj_size_index;
938 evacuate_block_obj_sizes [size_index] = FALSE;
942 if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
943 mono_sgen_pin_object (obj, queue);
947 vtable_word = *(mword*)obj;
948 /*someone else forwarded it, update the pointer and bail out*/
949 if (vtable_word & SGEN_FORWARDED_BIT) {
950 *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
954 /*someone pinned it, nothing to do.*/
955 if (vtable_word & SGEN_PINNED_BIT)
961 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
964 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
969 * FIXME: If we make major_alloc_object() give
970 * us the block info, too, we won't have to
973 block = MS_BLOCK_FOR_OBJ (obj);
974 MS_CALC_MARK_BIT (word, bit, obj);
975 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
976 MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
979 * FIXME: We have allocated destination, but
980 * we cannot use it. Give it back to the
983 *(void**)destination = NULL;
985 vtable_word = *(mword*)obj;
986 g_assert (vtable_word & SGEN_FORWARDED_BIT);
988 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
994 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
996 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
998 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1003 block = MS_BLOCK_FOR_OBJ (obj);
1004 size_index = block->obj_size_index;
1006 if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1007 if (block->is_to_space)
1009 HEAVY_STAT (++stat_major_objects_evacuated);
1010 goto do_copy_object;
1012 MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1015 if (vtable_word & SGEN_PINNED_BIT)
1017 binary_protocol_pin (obj, vt, mono_sgen_safe_object_get_size ((MonoObject*)obj));
1018 if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
1019 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1020 GRAY_OBJECT_ENQUEUE (queue, obj);
1022 g_assert (SGEN_OBJECT_IS_PINNED (obj));
1029 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1034 HEAVY_STAT (++stat_copy_object_called_major);
1036 DEBUG (9, g_assert (obj));
1037 DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
1039 if (ptr_in_nursery (obj)) {
1041 char *forwarded, *old_obj;
1043 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1047 if (SGEN_OBJECT_IS_PINNED (obj))
1050 HEAVY_STAT (++stat_objects_copied_major);
1054 obj = copy_object_no_checks (obj, queue);
1055 if (G_UNLIKELY (old_obj == obj)) {
1056 /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1057 if (!ptr_in_nursery (obj)) {
1059 block = MS_BLOCK_FOR_OBJ (obj);
1060 size_index = block->obj_size_index;
1061 evacuate_block_obj_sizes [size_index] = FALSE;
1062 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1069 * FIXME: See comment for copy_object_no_checks(). If
1070 * we have that, we can let the allocation function
1071 * give us the block info, too, and we won't have to
1074 block = MS_BLOCK_FOR_OBJ (obj);
1075 MS_CALC_MARK_BIT (word, bit, obj);
1076 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
1077 MS_SET_MARK_BIT (block, word, bit);
1084 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1090 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1092 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
1094 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1099 block = MS_BLOCK_FOR_OBJ (obj);
1100 size_index = block->obj_size_index;
1102 if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1103 if (block->is_to_space)
1105 HEAVY_STAT (++stat_major_objects_evacuated);
1106 goto do_copy_object;
1108 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1111 if (SGEN_OBJECT_IS_PINNED (obj))
1113 binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), mono_sgen_safe_object_get_size ((MonoObject*)obj));
1114 SGEN_PIN_OBJECT (obj);
1115 /* FIXME: only enqueue if object has references */
1116 GRAY_OBJECT_ENQUEUE (queue, obj);
1122 #include "sgen-major-scan-object.h"
1125 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1128 int last_index = -1;
1130 if (!block->pin_queue_num_entries)
1133 block->has_pinned = TRUE;
1135 for (i = 0; i < block->pin_queue_num_entries; ++i) {
1136 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1137 DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
1138 if (index == last_index)
1140 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1151 /* statistics for evacuation */
1152 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1153 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1154 int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1156 for (i = 0; i < num_block_obj_sizes; ++i)
1157 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1159 /* clear all the free lists */
1160 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1161 MSBlockInfo **free_blocks = free_block_lists [i];
1163 for (j = 0; j < num_block_obj_sizes; ++j)
1164 free_blocks [j] = NULL;
1167 /* traverse all blocks, free and zero unmarked objects */
1170 MSBlockInfo *block = *iter;
1172 gboolean have_live = FALSE;
1173 gboolean has_pinned;
1177 obj_size_index = block->obj_size_index;
1179 has_pinned = block->has_pinned;
1180 block->has_pinned = block->pinned;
1182 block->is_to_space = FALSE;
1184 count = MS_BLOCK_FREE / block->obj_size;
1185 block->free_list = NULL;
1187 for (obj_index = 0; obj_index < count; ++obj_index) {
1189 void *obj = MS_BLOCK_OBJ (block, obj_index);
1191 MS_CALC_MARK_BIT (word, bit, obj);
1192 if (MS_MARK_BIT (block, word, bit)) {
1193 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
1196 ++slots_used [obj_size_index];
1198 /* an unmarked object */
1199 if (MS_OBJ_ALLOCED (obj, block)) {
1200 binary_protocol_empty (obj, block->obj_size);
1201 memset (obj, 0, block->obj_size);
1203 *(void**)obj = block->free_list;
1204 block->free_list = obj;
1208 /* reset mark bits */
1209 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1212 * FIXME: reverse free list so that it's in address
1218 ++num_blocks [obj_size_index];
1219 slots_available [obj_size_index] += count;
1222 iter = &block->next;
1225 * If there are free slots in the block, add
1226 * the block to the corresponding free list.
1228 if (block->free_list) {
1229 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1230 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1231 block->next_free = free_blocks [index];
1232 free_blocks [index] = block;
1235 update_heap_boundaries_for_block (block);
1238 * Blocks without live objects are removed from the
1239 * block list and freed.
1241 *iter = block->next;
1244 ms_free_block (block);
1246 ms_free_block (block->block);
1248 mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1251 --num_major_sections;
1255 for (i = 0; i < num_block_obj_sizes; ++i) {
1256 float usage = (float)slots_used [i] / (float)slots_available [i];
1257 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1258 evacuate_block_obj_sizes [i] = TRUE;
1260 g_print ("slot size %d - %d of %d used\n",
1261 block_obj_sizes [i], slots_used [i], slots_available [i]);
1264 evacuate_block_obj_sizes [i] = FALSE;
1272 ms_sweep_thread_func (void *dummy)
1274 g_assert (concurrent_sweep);
1279 while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
1281 g_error ("MONO_SEM_WAIT");
1286 ms_signal_sweep_done ();
1295 if (concurrent_sweep) {
1296 if (!ms_sweep_thread)
1297 pthread_create (&ms_sweep_thread, NULL, ms_sweep_thread_func, NULL);
1299 ms_signal_sweep_command ();
1305 static int count_pinned_ref;
1306 static int count_pinned_nonref;
1307 static int count_nonpinned_ref;
1308 static int count_nonpinned_nonref;
1311 count_nonpinned_callback (char *obj, size_t size, void *data)
1313 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1315 if (vtable->klass->has_references)
1316 ++count_nonpinned_ref;
1318 ++count_nonpinned_nonref;
1322 count_pinned_callback (char *obj, size_t size, void *data)
1324 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1326 if (vtable->klass->has_references)
1329 ++count_pinned_nonref;
1332 static void __attribute__ ((unused))
1333 count_ref_nonref_objs (void)
1337 count_pinned_ref = 0;
1338 count_pinned_nonref = 0;
1339 count_nonpinned_ref = 0;
1340 count_nonpinned_nonref = 0;
1342 major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1343 major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1345 total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1347 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1348 count_pinned_ref, count_nonpinned_ref,
1349 count_pinned_nonref, count_nonpinned_nonref,
1350 (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1354 ms_calculate_block_obj_sizes (double factor, int *arr)
1356 double target_size = sizeof (MonoObject);
1361 int target_count = ceil (MS_BLOCK_FREE / target_size);
1362 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1364 if (size != last_size) {
1366 arr [num_sizes] = size;
1371 target_size *= factor;
1372 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1377 /* only valid during minor collections */
1378 static int old_num_major_sections;
1381 major_start_nursery_collection (void)
1383 ms_wait_for_sweep_done ();
1385 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1386 consistency_check ();
1389 old_num_major_sections = num_major_sections;
1393 major_finish_nursery_collection (void)
1395 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1396 consistency_check ();
1398 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1402 major_start_major_collection (void)
1406 ms_wait_for_sweep_done ();
1408 /* clear the free lists */
1409 for (i = 0; i < num_block_obj_sizes; ++i) {
1410 if (!evacuate_block_obj_sizes [i])
1413 free_block_lists [0][i] = NULL;
1414 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1419 major_finish_major_collection (void)
1424 major_have_computer_minor_collection_allowance (void)
1427 int section_reserve = mono_sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1429 g_assert (have_swept);
1430 ms_wait_for_sweep_done ();
1431 g_assert (!ms_sweep_in_progress);
1434 * FIXME: We don't free blocks on 32 bit platforms because it
1435 * can lead to address space fragmentation, since we're
1436 * allocating blocks in larger contingents.
1438 if (sizeof (mword) < 8)
1441 while (num_empty_blocks > section_reserve) {
1442 void *next = *(void**)empty_blocks;
1443 mono_sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
1444 empty_blocks = next;
1446 * Needs not be atomic because this is running
1451 ++stat_major_blocks_freed;
1457 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1461 FOREACH_BLOCK (block) {
1462 block->pin_queue_start = mono_sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
1463 &block->pin_queue_num_entries);
1464 } END_FOREACH_BLOCK;
1468 major_pin_objects (SgenGrayQueue *queue)
1472 FOREACH_BLOCK (block) {
1473 mark_pinned_objects_in_block (block, queue);
1474 } END_FOREACH_BLOCK;
1478 major_init_to_space (void)
1483 major_report_pinned_memory_usage (void)
1485 g_assert_not_reached ();
1489 major_get_used_size (void)
1494 FOREACH_BLOCK (block) {
1495 int count = MS_BLOCK_FREE / block->obj_size;
1497 size += count * block->obj_size;
1498 for (iter = block->free_list; iter; iter = (void**)*iter)
1499 size -= block->obj_size;
1500 } END_FOREACH_BLOCK;
1506 get_num_major_sections (void)
1508 return num_major_sections;
1512 major_handle_gc_param (const char *opt)
1515 if (g_str_has_prefix (opt, "major-heap-size=")) {
1516 const char *arg = strchr (opt, '=') + 1;
1518 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
1520 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1521 g_assert (ms_heap_num_blocks > 0);
1525 if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1526 const char *arg = strchr (opt, '=') + 1;
1527 int percentage = atoi (arg);
1528 if (percentage < 0 || percentage > 100) {
1529 fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1532 evacuation_threshold = (float)percentage / 100.0;
1534 } else if (!strcmp (opt, "concurrent-sweep")) {
1535 concurrent_sweep = TRUE;
1537 } else if (!strcmp (opt, "no-concurrent-sweep")) {
1538 concurrent_sweep = FALSE;
1546 major_print_gc_param_usage (void)
1551 " major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1553 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1554 " (no-)concurrent-sweep\n"
1558 #ifdef SGEN_HAVE_CARDTABLE
1560 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1564 FOREACH_BLOCK (block) {
1565 if (block->has_references)
1566 callback ((mword)block->block, MS_BLOCK_SIZE);
1567 } END_FOREACH_BLOCK;
1570 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1572 * MS blocks are 16K aligned.
1573 * Cardtables are 4K aligned, at least.
1574 * This means that the cardtable of a given block is 32 bytes aligned.
1577 initial_skip_card (guint8 *card_data)
1579 mword *cards = (mword*)card_data;
1582 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1588 if (i == CARD_WORDS_PER_BLOCK)
1589 return card_data + CARDS_PER_BLOCK;
1591 #if defined(__i386__) && defined(__GNUC__)
1592 return card_data + i * 4 + (__builtin_ffs (card) - 1) / 8;
1593 #elif defined(__x86_64__) && defined(__GNUC__)
1594 return card_data + i * 8 + (__builtin_ffsll (card) - 1) / 8;
1596 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1598 return &card_data [i];
1605 static G_GNUC_UNUSED guint8*
1606 skip_card (guint8 *card_data, guint8 *card_data_end)
1608 while (card_data < card_data_end && !*card_data)
1613 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1614 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1615 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1618 major_scan_card_table (SgenGrayQueue *queue)
1622 FOREACH_BLOCK (block) {
1626 if (!block->has_references)
1629 block_obj_size = block->obj_size;
1630 block_start = block->block;
1632 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1634 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1635 guint8 cards_data [CARDS_PER_BLOCK];
1637 char *obj, *end, *base;
1639 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1640 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1641 cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1644 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1648 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1649 end = block_start + MS_BLOCK_SIZE;
1650 base = sgen_card_table_align_pointer (obj);
1653 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1654 int card_offset = (obj - base) >> CARD_BITS;
1655 sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
1657 obj += block_obj_size;
1660 guint8 *card_data, *card_base;
1661 guint8 *card_data_end;
1664 * This is safe in face of card aliasing for the following reason:
1666 * Major blocks are 16k aligned, or 32 cards aligned.
1667 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1668 * sizes, they won't overflow the cardtable overlap modulus.
1670 card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1671 card_data_end = card_data + CARDS_PER_BLOCK;
1673 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)) {
1675 int idx = card_data - card_base;
1676 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1677 char *end = start + CARD_SIZE_IN_BYTES;
1682 sgen_card_table_prepare_card_for_scanning (card_data);
1687 index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1689 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1691 if (MS_OBJ_ALLOCED_FAST (obj, block_start))
1692 minor_scan_object (obj, queue);
1693 obj += block_obj_size;
1697 } END_FOREACH_BLOCK;
1702 major_is_worker_thread (pthread_t thread)
1704 if (concurrent_sweep)
1705 return thread == ms_sweep_thread;
1710 #undef pthread_create
1713 #ifdef SGEN_PARALLEL_MARK
1715 mono_sgen_marksweep_fixed_par_init
1717 mono_sgen_marksweep_par_init
1721 mono_sgen_marksweep_fixed_init
1723 mono_sgen_marksweep_init
1726 (SgenMajorCollector *collector)
1731 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1734 num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
1735 block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1736 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
1738 evacuate_block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1739 for (i = 0; i < num_block_obj_sizes; ++i)
1740 evacuate_block_obj_sizes [i] = FALSE;
1745 g_print ("block object sizes:\n");
1746 for (i = 0; i < num_block_obj_sizes; ++i)
1747 g_print ("%d\n", block_obj_sizes [i]);
1751 for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1752 free_block_lists [i] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1754 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
1755 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
1756 for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
1757 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
1759 LOCK_INIT (ms_block_list_mutex);
1761 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
1762 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
1763 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
1764 mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_time_wait_for_sweep);
1767 * FIXME: These are superfluous if concurrent sweep is
1768 * disabled. We might want to create them lazily.
1770 MONO_SEM_INIT (&ms_sweep_cmd_semaphore, 0);
1771 MONO_SEM_INIT (&ms_sweep_done_semaphore, 0);
1773 collector->section_size = MAJOR_SECTION_SIZE;
1774 #ifdef SGEN_PARALLEL_MARK
1775 collector->is_parallel = TRUE;
1777 collector->is_parallel = FALSE;
1779 collector->supports_cardtable = TRUE;
1781 collector->have_swept = &have_swept;
1783 collector->alloc_heap = major_alloc_heap;
1784 collector->is_object_live = major_is_object_live;
1785 collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
1786 collector->alloc_degraded = major_alloc_degraded;
1787 collector->copy_or_mark_object = major_copy_or_mark_object;
1788 collector->alloc_object = major_alloc_object;
1789 collector->free_pinned_object = free_pinned_object;
1790 collector->iterate_objects = major_iterate_objects;
1791 collector->free_non_pinned_object = major_free_non_pinned_object;
1792 collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
1793 collector->pin_objects = major_pin_objects;
1794 #ifdef SGEN_HAVE_CARDTABLE
1795 collector->scan_card_table = major_scan_card_table;
1796 collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
1798 collector->init_to_space = major_init_to_space;
1799 collector->sweep = major_sweep;
1800 collector->check_scan_starts = major_check_scan_starts;
1801 collector->dump_heap = major_dump_heap;
1802 collector->get_used_size = major_get_used_size;
1803 collector->start_nursery_collection = major_start_nursery_collection;
1804 collector->finish_nursery_collection = major_finish_nursery_collection;
1805 collector->start_major_collection = major_start_major_collection;
1806 collector->finish_major_collection = major_finish_major_collection;
1807 collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
1808 collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
1809 collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
1810 collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
1811 collector->get_num_major_sections = get_num_major_sections;
1812 collector->handle_gc_param = major_handle_gc_param;
1813 collector->print_gc_param_usage = major_print_gc_param_usage;
1814 collector->is_worker_thread = major_is_worker_thread;
1816 FILL_COLLECTOR_COPY_OBJECT (collector);
1817 FILL_COLLECTOR_SCAN_OBJECT (collector);
1819 #ifdef SGEN_HAVE_CARDTABLE
1820 /*cardtable requires major pages to be 8 cards aligned*/
1821 g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);