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 *ptr
)
594 FOREACH_BLOCK (block
) {
595 if (ptr
>= block
->block
&& ptr
<= block
->block
+ MS_BLOCK_SIZE
)
596 return block
->pinned
;
602 alloc_obj (int size
, gboolean pinned
, gboolean has_references
)
604 int size_index
= MS_BLOCK_OBJ_SIZE_INDEX (size
);
605 MSBlockInfo
**free_blocks
= FREE_BLOCKS (pinned
, has_references
);
609 /* FIXME: try to do this without locking */
613 g_assert (!ms_sweep_in_progress
);
615 if (!free_blocks
[size_index
]) {
616 if (G_UNLIKELY (!ms_alloc_block (size_index
, pinned
, has_references
))) {
617 UNLOCK_MS_BLOCK_LIST
;
622 block
= free_blocks
[size_index
];
623 DEBUG (9, g_assert (block
));
625 obj
= block
->free_list
;
626 DEBUG (9, g_assert (obj
));
628 block
->free_list
= *(void**)obj
;
629 if (!block
->free_list
) {
630 free_blocks
[size_index
] = block
->next_free
;
631 block
->next_free
= NULL
;
634 UNLOCK_MS_BLOCK_LIST
;
637 * FIXME: This should not be necessary because it'll be
638 * overwritten by the vtable immediately.
646 major_alloc_object (int size
, gboolean has_references
)
648 return alloc_obj (size
, FALSE
, has_references
);
652 * We're not freeing the block if it's empty. We leave that work for
653 * the next major collection.
655 * This is just called from the domain clearing code, which runs in a
656 * single thread and has the GC lock, so we don't need an extra lock.
659 free_object (char *obj
, size_t size
, gboolean pinned
)
661 MSBlockInfo
*block
= MS_BLOCK_FOR_OBJ (obj
);
663 DEBUG (9, g_assert ((pinned
&& block
->pinned
) || (!pinned
&& !block
->pinned
)));
664 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj
, block
)));
665 MS_CALC_MARK_BIT (word
, bit
, obj
);
666 DEBUG (9, g_assert (!MS_MARK_BIT (block
, word
, bit
)));
667 if (!block
->free_list
) {
668 MSBlockInfo
**free_blocks
= FREE_BLOCKS (pinned
, block
->has_references
);
669 int size_index
= MS_BLOCK_OBJ_SIZE_INDEX (size
);
670 DEBUG (9, g_assert (!block
->next_free
));
671 block
->next_free
= free_blocks
[size_index
];
672 free_blocks
[size_index
] = block
;
674 memset (obj
, 0, size
);
675 *(void**)obj
= block
->free_list
;
676 block
->free_list
= (void**)obj
;
680 major_free_non_pinned_object (char *obj
, size_t size
)
682 free_object (obj
, size
, FALSE
);
685 /* size is a multiple of SGEN_ALLOC_ALIGN */
687 major_alloc_small_pinned_obj (size_t size
, gboolean has_references
)
691 ms_wait_for_sweep_done ();
693 res
= alloc_obj (size
, TRUE
, has_references
);
694 /*If we failed to alloc memory, we better try releasing memory
695 *as pinned alloc is requested by the runtime.
698 sgen_collect_major_no_lock ("pinned alloc failure");
699 res
= alloc_obj (size
, TRUE
, has_references
);
705 free_pinned_object (char *obj
, size_t size
)
707 free_object (obj
, size
, TRUE
);
711 * size is already rounded up and we hold the GC lock.
714 major_alloc_degraded (MonoVTable
*vtable
, size_t size
)
717 int old_num_sections
;
719 ms_wait_for_sweep_done ();
721 old_num_sections
= num_major_sections
;
723 obj
= alloc_obj (size
, FALSE
, SGEN_VTABLE_HAS_REFERENCES (vtable
));
724 if (G_LIKELY (obj
)) {
725 *(MonoVTable
**)obj
= vtable
;
726 HEAVY_STAT (++stat_objects_alloced_degraded
);
727 HEAVY_STAT (stat_bytes_alloced_degraded
+= size
);
728 g_assert (num_major_sections
>= old_num_sections
);
729 mono_sgen_register_major_sections_alloced (num_major_sections
- old_num_sections
);
734 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj) FALSE
737 * obj is some object. If it's not in the major heap (i.e. if it's in
738 * the nursery or LOS), return FALSE. Otherwise return whether it's
739 * been marked or copied.
742 major_is_object_live (char *obj
)
750 if (ptr_in_nursery (obj
))
755 if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj
))
758 objsize
= SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject
*)obj
));
761 if (objsize
> SGEN_MAX_SMALL_OBJ_SIZE
)
765 /* now we know it's in a major block */
766 block
= MS_BLOCK_FOR_OBJ (obj
);
767 DEBUG (9, g_assert (!block
->pinned
));
768 MS_CALC_MARK_BIT (word
, bit
, obj
);
769 return MS_MARK_BIT (block
, word
, bit
) ? TRUE
: FALSE
;
773 major_ptr_is_in_non_pinned_space (char *ptr
)
777 FOREACH_BLOCK (block
) {
778 if (ptr
>= block
->block
&& ptr
<= block
->block
+ MS_BLOCK_SIZE
)
779 return !block
->pinned
;
785 major_iterate_objects (gboolean non_pinned
, gboolean pinned
, IterateObjectCallbackFunc callback
, void *data
)
789 ms_wait_for_sweep_done ();
791 FOREACH_BLOCK (block
) {
792 int count
= MS_BLOCK_FREE
/ block
->obj_size
;
795 if (block
->pinned
&& !pinned
)
797 if (!block
->pinned
&& !non_pinned
)
800 for (i
= 0; i
< count
; ++i
) {
801 void **obj
= (void**) MS_BLOCK_OBJ (block
, i
);
802 if (MS_OBJ_ALLOCED (obj
, block
))
803 callback ((char*)obj
, block
->obj_size
, data
);
809 major_is_valid_object (char *object
)
813 ms_wait_for_sweep_done ();
814 FOREACH_BLOCK (block
) {
818 if ((block
->block
> object
) || ((block
->block
+ MS_BLOCK_SIZE
) <= object
))
821 idx
= MS_BLOCK_OBJ_INDEX (object
, block
);
822 obj
= (char*)MS_BLOCK_OBJ (block
, idx
);
825 return MS_OBJ_ALLOCED (obj
, block
);
832 major_check_scan_starts (void)
837 major_dump_heap (FILE *heap_dump_file
)
840 int *slots_available
= alloca (sizeof (int) * num_block_obj_sizes
);
841 int *slots_used
= alloca (sizeof (int) * num_block_obj_sizes
);
844 for (i
= 0; i
< num_block_obj_sizes
; ++i
)
845 slots_available
[i
] = slots_used
[i
] = 0;
847 FOREACH_BLOCK (block
) {
848 int index
= ms_find_block_obj_size_index (block
->obj_size
);
849 int count
= MS_BLOCK_FREE
/ block
->obj_size
;
851 slots_available
[index
] += count
;
852 for (i
= 0; i
< count
; ++i
) {
853 if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block
, i
), block
))
854 ++slots_used
[index
];
858 fprintf (heap_dump_file
, "<occupancies>\n");
859 for (i
= 0; i
< num_block_obj_sizes
; ++i
) {
860 fprintf (heap_dump_file
, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
861 block_obj_sizes
[i
], slots_available
[i
], slots_used
[i
]);
863 fprintf (heap_dump_file
, "</occupancies>\n");
865 FOREACH_BLOCK (block
) {
866 int count
= MS_BLOCK_FREE
/ block
->obj_size
;
870 fprintf (heap_dump_file
, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE
);
872 for (i
= 0; i
<= count
; ++i
) {
873 if ((i
< count
) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block
, i
), block
)) {
878 mono_sgen_dump_occupied (MS_BLOCK_OBJ (block
, start
), MS_BLOCK_OBJ (block
, i
), block
->block
);
884 fprintf (heap_dump_file
, "</section>\n");
888 #define LOAD_VTABLE SGEN_LOAD_VTABLE
890 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do { \
892 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
893 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
894 MS_SET_MARK_BIT ((block), __word, __bit); \
895 if ((block)->has_references) \
896 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
897 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
900 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
902 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
903 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
904 if (!MS_MARK_BIT ((block), __word, __bit)) { \
905 MS_SET_MARK_BIT ((block), __word, __bit); \
906 if ((block)->has_references) \
907 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
908 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
911 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
913 gboolean __was_marked; \
914 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
915 MS_CALC_MARK_BIT (__word, __bit, (obj)); \
916 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
917 if (!__was_marked) { \
918 if ((block)->has_references) \
919 GRAY_OBJECT_ENQUEUE ((queue), (obj)); \
920 binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
924 #include "sgen-major-copy-object.h"
926 #ifdef SGEN_PARALLEL_MARK
928 major_copy_or_mark_object (void **ptr
, SgenGrayQueue
*queue
)
931 mword vtable_word
= *(mword
*)obj
;
932 MonoVTable
*vt
= (MonoVTable
*)(vtable_word
& ~SGEN_VTABLE_BITS_MASK
);
936 HEAVY_STAT (++stat_copy_object_called_major
);
938 DEBUG (9, g_assert (obj
));
939 DEBUG (9, g_assert (current_collection_generation
== GENERATION_OLD
));
941 if (vtable_word
& SGEN_FORWARDED_BIT
) {
946 if (ptr_in_nursery (obj
)) {
948 gboolean has_references
;
951 if (vtable_word
& SGEN_PINNED_BIT
)
954 HEAVY_STAT (++stat_objects_copied_major
);
957 objsize
= SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt
, (MonoObject
*)obj
));
958 has_references
= SGEN_VTABLE_HAS_REFERENCES (vt
);
960 destination
= major_alloc_object (objsize
, has_references
);
961 if (G_UNLIKELY (!destination
)) {
962 if (!ptr_in_nursery (obj
)) {
964 block
= MS_BLOCK_FOR_OBJ (obj
);
965 size_index
= block
->obj_size_index
;
966 evacuate_block_obj_sizes
[size_index
] = FALSE
;
970 if (SGEN_CAS_PTR (obj
, (void*)((mword
)vt
| SGEN_PINNED_BIT
), vt
) == vt
) {
971 mono_sgen_pin_object (obj
, queue
);
975 vtable_word
= *(mword
*)obj
;
976 /*someone else forwarded it, update the pointer and bail out*/
977 if (vtable_word
& SGEN_FORWARDED_BIT
) {
978 *ptr
= (void*)(vtable_word
& ~SGEN_VTABLE_BITS_MASK
);
982 /*someone pinned it, nothing to do.*/
983 if (vtable_word
& SGEN_PINNED_BIT
)
989 if (SGEN_CAS_PTR (obj
, (void*)((mword
)destination
| SGEN_FORWARDED_BIT
), vt
) == vt
) {
992 par_copy_object_no_checks (destination
, vt
, obj
, objsize
, has_references
? queue
: NULL
);
997 * FIXME: If we make major_alloc_object() give
998 * us the block info, too, we won't have to
1001 block
= MS_BLOCK_FOR_OBJ (obj
);
1002 MS_CALC_MARK_BIT (word
, bit
, obj
);
1003 DEBUG (9, g_assert (!MS_MARK_BIT (block
, word
, bit
)));
1004 MS_PAR_SET_MARK_BIT (was_marked
, block
, word
, bit
);
1007 * FIXME: We have allocated destination, but
1008 * we cannot use it. Give it back to the
1011 *(void**)destination
= NULL
;
1013 vtable_word
= *(mword
*)obj
;
1014 g_assert (vtable_word
& SGEN_FORWARDED_BIT
);
1016 obj
= (void*)(vtable_word
& ~SGEN_VTABLE_BITS_MASK
);
1022 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj
))
1024 objsize
= SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt
, (MonoObject
*)obj
));
1026 if (objsize
<= SGEN_MAX_SMALL_OBJ_SIZE
)
1031 block
= MS_BLOCK_FOR_OBJ (obj
);
1032 size_index
= block
->obj_size_index
;
1034 if (!block
->has_pinned
&& evacuate_block_obj_sizes
[size_index
]) {
1035 if (block
->is_to_space
)
1037 HEAVY_STAT (++stat_major_objects_evacuated
);
1038 goto do_copy_object
;
1040 MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj
, block
, queue
);
1043 if (vtable_word
& SGEN_PINNED_BIT
)
1045 binary_protocol_pin (obj
, vt
, mono_sgen_safe_object_get_size ((MonoObject
*)obj
));
1046 if (SGEN_CAS_PTR (obj
, (void*)(vtable_word
| SGEN_PINNED_BIT
), (void*)vtable_word
) == (void*)vtable_word
) {
1047 if (SGEN_VTABLE_HAS_REFERENCES (vt
))
1048 GRAY_OBJECT_ENQUEUE (queue
, obj
);
1050 g_assert (SGEN_OBJECT_IS_PINNED (obj
));
1057 major_copy_or_mark_object (void **ptr
, SgenGrayQueue
*queue
)
1062 HEAVY_STAT (++stat_copy_object_called_major
);
1064 DEBUG (9, g_assert (obj
));
1065 DEBUG (9, g_assert (current_collection_generation
== GENERATION_OLD
));
1067 if (ptr_in_nursery (obj
)) {
1069 char *forwarded
, *old_obj
;
1071 if ((forwarded
= SGEN_OBJECT_IS_FORWARDED (obj
))) {
1075 if (SGEN_OBJECT_IS_PINNED (obj
))
1078 HEAVY_STAT (++stat_objects_copied_major
);
1082 obj
= copy_object_no_checks (obj
, queue
);
1083 if (G_UNLIKELY (old_obj
== obj
)) {
1084 /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1085 if (!ptr_in_nursery (obj
)) {
1087 block
= MS_BLOCK_FOR_OBJ (obj
);
1088 size_index
= block
->obj_size_index
;
1089 evacuate_block_obj_sizes
[size_index
] = FALSE
;
1090 MS_MARK_OBJECT_AND_ENQUEUE (obj
, block
, queue
);
1097 * FIXME: See comment for copy_object_no_checks(). If
1098 * we have that, we can let the allocation function
1099 * give us the block info, too, and we won't have to
1102 block
= MS_BLOCK_FOR_OBJ (obj
);
1103 MS_CALC_MARK_BIT (word
, bit
, obj
);
1104 DEBUG (9, g_assert (!MS_MARK_BIT (block
, word
, bit
)));
1105 MS_SET_MARK_BIT (block
, word
, bit
);
1112 if ((forwarded
= SGEN_OBJECT_IS_FORWARDED (obj
))) {
1118 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj
))
1120 objsize
= SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject
*)obj
));
1122 if (objsize
<= SGEN_MAX_SMALL_OBJ_SIZE
)
1127 block
= MS_BLOCK_FOR_OBJ (obj
);
1128 size_index
= block
->obj_size_index
;
1130 if (!block
->has_pinned
&& evacuate_block_obj_sizes
[size_index
]) {
1131 if (block
->is_to_space
)
1133 HEAVY_STAT (++stat_major_objects_evacuated
);
1134 goto do_copy_object
;
1136 MS_MARK_OBJECT_AND_ENQUEUE (obj
, block
, queue
);
1139 if (SGEN_OBJECT_IS_PINNED (obj
))
1141 binary_protocol_pin (obj
, (gpointer
)SGEN_LOAD_VTABLE (obj
), mono_sgen_safe_object_get_size ((MonoObject
*)obj
));
1142 SGEN_PIN_OBJECT (obj
);
1143 /* FIXME: only enqueue if object has references */
1144 GRAY_OBJECT_ENQUEUE (queue
, obj
);
1150 #include "sgen-major-scan-object.h"
1153 mark_pinned_objects_in_block (MSBlockInfo
*block
, SgenGrayQueue
*queue
)
1156 int last_index
= -1;
1158 if (!block
->pin_queue_num_entries
)
1161 block
->has_pinned
= TRUE
;
1163 for (i
= 0; i
< block
->pin_queue_num_entries
; ++i
) {
1164 int index
= MS_BLOCK_OBJ_INDEX (block
->pin_queue_start
[i
], block
);
1165 DEBUG (9, g_assert (index
>= 0 && index
< MS_BLOCK_FREE
/ block
->obj_size
));
1166 if (index
== last_index
)
1168 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block
, index
), block
, queue
);
1179 /* statistics for evacuation */
1180 int *slots_available
= alloca (sizeof (int) * num_block_obj_sizes
);
1181 int *slots_used
= alloca (sizeof (int) * num_block_obj_sizes
);
1182 int *num_blocks
= alloca (sizeof (int) * num_block_obj_sizes
);
1184 for (i
= 0; i
< num_block_obj_sizes
; ++i
)
1185 slots_available
[i
] = slots_used
[i
] = num_blocks
[i
] = 0;
1187 /* clear all the free lists */
1188 for (i
= 0; i
< MS_BLOCK_TYPE_MAX
; ++i
) {
1189 MSBlockInfo
**free_blocks
= free_block_lists
[i
];
1191 for (j
= 0; j
< num_block_obj_sizes
; ++j
)
1192 free_blocks
[j
] = NULL
;
1195 /* traverse all blocks, free and zero unmarked objects */
1198 MSBlockInfo
*block
= *iter
;
1200 gboolean have_live
= FALSE
;
1201 gboolean has_pinned
;
1205 obj_size_index
= block
->obj_size_index
;
1207 has_pinned
= block
->has_pinned
;
1208 block
->has_pinned
= block
->pinned
;
1210 block
->is_to_space
= FALSE
;
1212 count
= MS_BLOCK_FREE
/ block
->obj_size
;
1213 block
->free_list
= NULL
;
1215 for (obj_index
= 0; obj_index
< count
; ++obj_index
) {
1217 void *obj
= MS_BLOCK_OBJ (block
, obj_index
);
1219 MS_CALC_MARK_BIT (word
, bit
, obj
);
1220 if (MS_MARK_BIT (block
, word
, bit
)) {
1221 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj
, block
)));
1224 ++slots_used
[obj_size_index
];
1226 /* an unmarked object */
1227 if (MS_OBJ_ALLOCED (obj
, block
)) {
1228 binary_protocol_empty (obj
, block
->obj_size
);
1229 memset (obj
, 0, block
->obj_size
);
1231 *(void**)obj
= block
->free_list
;
1232 block
->free_list
= obj
;
1236 /* reset mark bits */
1237 memset (block
->mark_words
, 0, sizeof (mword
) * MS_NUM_MARK_WORDS
);
1240 * FIXME: reverse free list so that it's in address
1246 ++num_blocks
[obj_size_index
];
1247 slots_available
[obj_size_index
] += count
;
1250 iter
= &block
->next
;
1253 * If there are free slots in the block, add
1254 * the block to the corresponding free list.
1256 if (block
->free_list
) {
1257 MSBlockInfo
**free_blocks
= FREE_BLOCKS (block
->pinned
, block
->has_references
);
1258 int index
= MS_BLOCK_OBJ_SIZE_INDEX (block
->obj_size
);
1259 block
->next_free
= free_blocks
[index
];
1260 free_blocks
[index
] = block
;
1263 update_heap_boundaries_for_block (block
);
1266 * Blocks without live objects are removed from the
1267 * block list and freed.
1269 *iter
= block
->next
;
1272 ms_free_block (block
);
1274 ms_free_block (block
->block
);
1276 mono_sgen_free_internal (block
, INTERNAL_MEM_MS_BLOCK_INFO
);
1279 --num_major_sections
;
1283 for (i
= 0; i
< num_block_obj_sizes
; ++i
) {
1284 float usage
= (float)slots_used
[i
] / (float)slots_available
[i
];
1285 if (num_blocks
[i
] > 5 && usage
< evacuation_threshold
) {
1286 evacuate_block_obj_sizes
[i
] = TRUE
;
1288 g_print ("slot size %d - %d of %d used\n",
1289 block_obj_sizes [i], slots_used [i], slots_available [i]);
1292 evacuate_block_obj_sizes
[i
] = FALSE
;
1300 ms_sweep_thread_func (void *dummy
)
1302 g_assert (concurrent_sweep
);
1307 while ((result
= MONO_SEM_WAIT (&ms_sweep_cmd_semaphore
)) != 0) {
1309 g_error ("MONO_SEM_WAIT FAILED with %d errno %d (%s)", result
, errno
, strerror (errno
));
1314 ms_signal_sweep_done ();
1323 if (concurrent_sweep
) {
1324 if (!ms_sweep_thread
)
1325 pthread_create (&ms_sweep_thread
, NULL
, ms_sweep_thread_func
, NULL
);
1327 ms_signal_sweep_command ();
1333 static int count_pinned_ref
;
1334 static int count_pinned_nonref
;
1335 static int count_nonpinned_ref
;
1336 static int count_nonpinned_nonref
;
1339 count_nonpinned_callback (char *obj
, size_t size
, void *data
)
1341 MonoVTable
*vtable
= (MonoVTable
*)LOAD_VTABLE (obj
);
1343 if (vtable
->klass
->has_references
)
1344 ++count_nonpinned_ref
;
1346 ++count_nonpinned_nonref
;
1350 count_pinned_callback (char *obj
, size_t size
, void *data
)
1352 MonoVTable
*vtable
= (MonoVTable
*)LOAD_VTABLE (obj
);
1354 if (vtable
->klass
->has_references
)
1357 ++count_pinned_nonref
;
1360 static void __attribute__ ((unused
))
1361 count_ref_nonref_objs (void)
1365 count_pinned_ref
= 0;
1366 count_pinned_nonref
= 0;
1367 count_nonpinned_ref
= 0;
1368 count_nonpinned_nonref
= 0;
1370 major_iterate_objects (TRUE
, FALSE
, count_nonpinned_callback
, NULL
);
1371 major_iterate_objects (FALSE
, TRUE
, count_pinned_callback
, NULL
);
1373 total
= count_pinned_nonref
+ count_nonpinned_nonref
+ count_pinned_ref
+ count_nonpinned_ref
;
1375 g_print ("ref: %d pinned %d non-pinned non-ref: %d pinned %d non-pinned -- %.1f\n",
1376 count_pinned_ref
, count_nonpinned_ref
,
1377 count_pinned_nonref
, count_nonpinned_nonref
,
1378 (count_pinned_nonref
+ count_nonpinned_nonref
) * 100.0 / total
);
1382 ms_calculate_block_obj_sizes (double factor
, int *arr
)
1384 double target_size
= sizeof (MonoObject
);
1389 int target_count
= ceil (MS_BLOCK_FREE
/ target_size
);
1390 int size
= MIN ((MS_BLOCK_FREE
/ target_count
) & ~(SGEN_ALLOC_ALIGN
- 1), SGEN_MAX_SMALL_OBJ_SIZE
);
1392 if (size
!= last_size
) {
1394 arr
[num_sizes
] = size
;
1399 target_size
*= factor
;
1400 } while (last_size
< SGEN_MAX_SMALL_OBJ_SIZE
);
1405 /* only valid during minor collections */
1406 static int old_num_major_sections
;
1409 major_start_nursery_collection (void)
1411 ms_wait_for_sweep_done ();
1413 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1414 consistency_check ();
1417 old_num_major_sections
= num_major_sections
;
1421 major_finish_nursery_collection (void)
1423 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1424 consistency_check ();
1426 mono_sgen_register_major_sections_alloced (num_major_sections
- old_num_major_sections
);
1430 major_start_major_collection (void)
1434 ms_wait_for_sweep_done ();
1436 /* clear the free lists */
1437 for (i
= 0; i
< num_block_obj_sizes
; ++i
) {
1438 if (!evacuate_block_obj_sizes
[i
])
1441 free_block_lists
[0][i
] = NULL
;
1442 free_block_lists
[MS_BLOCK_FLAG_REFS
][i
] = NULL
;
1447 major_finish_major_collection (void)
1452 major_have_computer_minor_collection_allowance (void)
1455 int section_reserve
= mono_sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE
;
1457 g_assert (have_swept
);
1458 ms_wait_for_sweep_done ();
1459 g_assert (!ms_sweep_in_progress
);
1462 * FIXME: We don't free blocks on 32 bit platforms because it
1463 * can lead to address space fragmentation, since we're
1464 * allocating blocks in larger contingents.
1466 if (sizeof (mword
) < 8)
1469 while (num_empty_blocks
> section_reserve
) {
1470 void *next
= *(void**)empty_blocks
;
1471 mono_sgen_free_os_memory (empty_blocks
, MS_BLOCK_SIZE
);
1472 empty_blocks
= next
;
1474 * Needs not be atomic because this is running
1479 ++stat_major_blocks_freed
;
1485 major_find_pin_queue_start_ends (SgenGrayQueue
*queue
)
1489 FOREACH_BLOCK (block
) {
1490 block
->pin_queue_start
= mono_sgen_find_optimized_pin_queue_area (block
->block
+ MS_BLOCK_SKIP
, block
->block
+ MS_BLOCK_SIZE
,
1491 &block
->pin_queue_num_entries
);
1492 } END_FOREACH_BLOCK
;
1496 major_pin_objects (SgenGrayQueue
*queue
)
1500 FOREACH_BLOCK (block
) {
1501 mark_pinned_objects_in_block (block
, queue
);
1502 } END_FOREACH_BLOCK
;
1506 major_init_to_space (void)
1511 major_report_pinned_memory_usage (void)
1513 g_assert_not_reached ();
1517 major_get_used_size (void)
1522 FOREACH_BLOCK (block
) {
1523 int count
= MS_BLOCK_FREE
/ block
->obj_size
;
1525 size
+= count
* block
->obj_size
;
1526 for (iter
= block
->free_list
; iter
; iter
= (void**)*iter
)
1527 size
-= block
->obj_size
;
1528 } END_FOREACH_BLOCK
;
1534 get_num_major_sections (void)
1536 return num_major_sections
;
1540 major_handle_gc_param (const char *opt
)
1543 if (g_str_has_prefix (opt
, "major-heap-size=")) {
1544 const char *arg
= strchr (opt
, '=') + 1;
1546 if (!mono_gc_parse_environment_string_extract_number (arg
, &size
))
1548 ms_heap_num_blocks
= (size
+ MS_BLOCK_SIZE
- 1) / MS_BLOCK_SIZE
;
1549 g_assert (ms_heap_num_blocks
> 0);
1553 if (g_str_has_prefix (opt
, "evacuation-threshold=")) {
1554 const char *arg
= strchr (opt
, '=') + 1;
1555 int percentage
= atoi (arg
);
1556 if (percentage
< 0 || percentage
> 100) {
1557 fprintf (stderr
, "evacuation-threshold must be an integer in the range 0-100.\n");
1560 evacuation_threshold
= (float)percentage
/ 100.0;
1562 } else if (!strcmp (opt
, "concurrent-sweep")) {
1563 concurrent_sweep
= TRUE
;
1565 } else if (!strcmp (opt
, "no-concurrent-sweep")) {
1566 concurrent_sweep
= FALSE
;
1574 major_print_gc_param_usage (void)
1579 " major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1581 " evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1582 " (no-)concurrent-sweep\n"
1586 #ifdef SGEN_HAVE_CARDTABLE
1588 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback
)
1592 FOREACH_BLOCK (block
) {
1593 if (block
->has_references
)
1594 callback ((mword
)block
->block
, MS_BLOCK_SIZE
);
1595 } END_FOREACH_BLOCK
;
1598 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1600 * MS blocks are 16K aligned.
1601 * Cardtables are 4K aligned, at least.
1602 * This means that the cardtable of a given block is 32 bytes aligned.
1605 initial_skip_card (guint8
*card_data
)
1607 mword
*cards
= (mword
*)card_data
;
1610 for (i
= 0; i
< CARD_WORDS_PER_BLOCK
; ++i
) {
1616 if (i
== CARD_WORDS_PER_BLOCK
)
1617 return card_data
+ CARDS_PER_BLOCK
;
1619 #if defined(__i386__) && defined(__GNUC__)
1620 return card_data
+ i
* 4 + (__builtin_ffs (card
) - 1) / 8;
1621 #elif defined(__x86_64__) && defined(__GNUC__)
1622 return card_data
+ i
* 8 + (__builtin_ffsll (card
) - 1) / 8;
1624 for (i
= i
* SIZEOF_VOID_P
; i
< CARDS_PER_BLOCK
; ++i
) {
1626 return &card_data
[i
];
1633 static G_GNUC_UNUSED guint8
*
1634 skip_card (guint8
*card_data
, guint8
*card_data_end
)
1636 while (card_data
< card_data_end
&& !*card_data
)
1641 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1642 #define MS_BLOCK_OBJ_FAST(b,os,i) ((b) + MS_BLOCK_SKIP + (os) * (i))
1643 #define MS_OBJ_ALLOCED_FAST(o,b) (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1646 major_scan_card_table (SgenGrayQueue
*queue
)
1650 FOREACH_BLOCK (block
) {
1654 if (!block
->has_references
)
1657 block_obj_size
= block
->obj_size
;
1658 block_start
= block
->block
;
1660 if (block_obj_size
>= CARD_SIZE_IN_BYTES
) {
1662 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1663 guint8 cards_data
[CARDS_PER_BLOCK
];
1665 char *obj
, *end
, *base
;
1667 /*We can avoid the extra copy since the remark cardtable was cleaned before */
1668 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1669 cards
= sgen_card_table_get_card_scan_address ((mword
)block_start
);
1672 if (!sgen_card_table_get_card_data (cards_data
, (mword
)block_start
, CARDS_PER_BLOCK
))
1676 obj
= (char*)MS_BLOCK_OBJ_FAST (block_start
, block_obj_size
, 0);
1677 end
= block_start
+ MS_BLOCK_SIZE
;
1678 base
= sgen_card_table_align_pointer (obj
);
1681 if (MS_OBJ_ALLOCED_FAST (obj
, block_start
)) {
1682 int card_offset
= (obj
- base
) >> CARD_BITS
;
1683 sgen_cardtable_scan_object (obj
, block_obj_size
, cards
+ card_offset
, queue
);
1685 obj
+= block_obj_size
;
1688 guint8
*card_data
, *card_base
;
1689 guint8
*card_data_end
;
1692 * This is safe in face of card aliasing for the following reason:
1694 * Major blocks are 16k aligned, or 32 cards aligned.
1695 * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1696 * sizes, they won't overflow the cardtable overlap modulus.
1698 card_data
= card_base
= sgen_card_table_get_card_scan_address ((mword
)block_start
);
1699 card_data_end
= card_data
+ CARDS_PER_BLOCK
;
1701 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)) {
1703 int idx
= card_data
- card_base
;
1704 char *start
= (char*)(block_start
+ idx
* CARD_SIZE_IN_BYTES
);
1705 char *end
= start
+ CARD_SIZE_IN_BYTES
;
1710 sgen_card_table_prepare_card_for_scanning (card_data
);
1715 index
= MS_BLOCK_OBJ_INDEX_FAST (start
, block_start
, block_obj_size
);
1717 obj
= (char*)MS_BLOCK_OBJ_FAST (block_start
, block_obj_size
, index
);
1719 if (MS_OBJ_ALLOCED_FAST (obj
, block_start
))
1720 minor_scan_object (obj
, queue
);
1721 obj
+= block_obj_size
;
1725 } END_FOREACH_BLOCK
;
1730 major_is_worker_thread (pthread_t thread
)
1732 if (concurrent_sweep
)
1733 return thread
== ms_sweep_thread
;
1738 #undef pthread_create
1741 #ifdef SGEN_PARALLEL_MARK
1743 mono_sgen_marksweep_fixed_par_init
1745 mono_sgen_marksweep_par_init
1749 mono_sgen_marksweep_fixed_init
1751 mono_sgen_marksweep_init
1754 (SgenMajorCollector
*collector
)
1759 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO
, sizeof (MSBlockInfo
));
1762 num_block_obj_sizes
= ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR
, NULL
);
1763 block_obj_sizes
= mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes
, INTERNAL_MEM_MS_TABLES
);
1764 ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR
, block_obj_sizes
);
1766 evacuate_block_obj_sizes
= mono_sgen_alloc_internal_dynamic (sizeof (gboolean
) * num_block_obj_sizes
, INTERNAL_MEM_MS_TABLES
);
1767 for (i
= 0; i
< num_block_obj_sizes
; ++i
)
1768 evacuate_block_obj_sizes
[i
] = FALSE
;
1773 g_print ("block object sizes:\n");
1774 for (i = 0; i < num_block_obj_sizes; ++i)
1775 g_print ("%d\n", block_obj_sizes [i]);
1779 for (i
= 0; i
< MS_BLOCK_TYPE_MAX
; ++i
)
1780 free_block_lists
[i
] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo
*) * num_block_obj_sizes
, INTERNAL_MEM_MS_TABLES
);
1782 for (i
= 0; i
< MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES
; ++i
)
1783 fast_block_obj_size_indexes
[i
] = ms_find_block_obj_size_index (i
* 8);
1784 for (i
= 0; i
< MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES
* 8; ++i
)
1785 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i
) == ms_find_block_obj_size_index (i
));
1787 LOCK_INIT (ms_block_list_mutex
);
1789 mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_major_blocks_alloced
);
1790 mono_counters_register ("# major blocks freed", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_major_blocks_freed
);
1791 mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_major_objects_evacuated
);
1792 mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC
| MONO_COUNTER_LONG
, &stat_time_wait_for_sweep
);
1795 * FIXME: These are superfluous if concurrent sweep is
1796 * disabled. We might want to create them lazily.
1798 MONO_SEM_INIT (&ms_sweep_cmd_semaphore
, 0);
1799 MONO_SEM_INIT (&ms_sweep_done_semaphore
, 0);
1801 collector
->section_size
= MAJOR_SECTION_SIZE
;
1802 #ifdef SGEN_PARALLEL_MARK
1803 collector
->is_parallel
= TRUE
;
1805 collector
->is_parallel
= FALSE
;
1807 collector
->supports_cardtable
= TRUE
;
1809 collector
->have_swept
= &have_swept
;
1811 collector
->alloc_heap
= major_alloc_heap
;
1812 collector
->is_object_live
= major_is_object_live
;
1813 collector
->alloc_small_pinned_obj
= major_alloc_small_pinned_obj
;
1814 collector
->alloc_degraded
= major_alloc_degraded
;
1815 collector
->copy_or_mark_object
= major_copy_or_mark_object
;
1816 collector
->alloc_object
= major_alloc_object
;
1817 collector
->free_pinned_object
= free_pinned_object
;
1818 collector
->iterate_objects
= major_iterate_objects
;
1819 collector
->free_non_pinned_object
= major_free_non_pinned_object
;
1820 collector
->find_pin_queue_start_ends
= major_find_pin_queue_start_ends
;
1821 collector
->pin_objects
= major_pin_objects
;
1822 #ifdef SGEN_HAVE_CARDTABLE
1823 collector
->scan_card_table
= major_scan_card_table
;
1824 collector
->iterate_live_block_ranges
= (void*)(void*) major_iterate_live_block_ranges
;
1826 collector
->init_to_space
= major_init_to_space
;
1827 collector
->sweep
= major_sweep
;
1828 collector
->check_scan_starts
= major_check_scan_starts
;
1829 collector
->dump_heap
= major_dump_heap
;
1830 collector
->get_used_size
= major_get_used_size
;
1831 collector
->start_nursery_collection
= major_start_nursery_collection
;
1832 collector
->finish_nursery_collection
= major_finish_nursery_collection
;
1833 collector
->start_major_collection
= major_start_major_collection
;
1834 collector
->finish_major_collection
= major_finish_major_collection
;
1835 collector
->have_computed_minor_collection_allowance
= major_have_computer_minor_collection_allowance
;
1836 collector
->ptr_is_in_non_pinned_space
= major_ptr_is_in_non_pinned_space
;
1837 collector
->obj_is_from_pinned_alloc
= obj_is_from_pinned_alloc
;
1838 collector
->report_pinned_memory_usage
= major_report_pinned_memory_usage
;
1839 collector
->get_num_major_sections
= get_num_major_sections
;
1840 collector
->handle_gc_param
= major_handle_gc_param
;
1841 collector
->print_gc_param_usage
= major_print_gc_param_usage
;
1842 collector
->is_worker_thread
= major_is_worker_thread
;
1843 collector
->is_valid_object
= major_is_valid_object
;
1845 FILL_COLLECTOR_COPY_OBJECT (collector
);
1846 FILL_COLLECTOR_SCAN_OBJECT (collector
);
1848 #ifdef SGEN_HAVE_CARDTABLE
1849 /*cardtable requires major pages to be 8 cards aligned*/
1850 g_assert ((MS_BLOCK_SIZE
% (8 * CARD_SIZE_IN_BYTES
)) == 0);