Add new whole heap verifier that can catch a whole lot more problems than previous...
[mono-project.git] / mono / metadata / sgen-marksweep.c
blob659f030e3be224cbe9fd792a65697c3c72224ff1
1 /*
2 * sgen-marksweep.c: Simple generational GC.
4 * Author:
5 * Mark Probst <mark.probst@gmail.com>
7 * Copyright 2009-2010 Novell, Inc.
8 *
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.
29 #ifdef HAVE_SGEN_GC
31 #include <math.h>
32 #include <errno.h>
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)
50 #ifdef FIXED_HEAP
51 #define MS_DEFAULT_HEAP_NUM_BLOCKS (32 * 1024) /* 512 MB */
52 #endif
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).
65 #ifdef FIXED_HEAP
66 #define MS_BLOCK_SKIP 0
67 #else
68 #define MS_BLOCK_SKIP 16
69 #endif
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
77 #endif
79 typedef struct _MSBlockInfo MSBlockInfo;
80 struct _MSBlockInfo {
81 int obj_size;
82 int obj_size_index;
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;
88 #ifdef FIXED_HEAP
89 unsigned int used : 1;
90 unsigned int zeroed : 1;
91 #endif
92 MSBlockInfo *next;
93 char *block;
94 void **free_list;
95 MSBlockInfo *next_free;
96 void **pin_queue_start;
97 mword mark_words [MS_NUM_MARK_WORDS];
100 #ifdef FIXED_HEAP
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;
110 #endif
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)))
115 #ifdef FIXED_HEAP
116 #define MS_BLOCK_FOR_OBJ(o) (&block_infos [(mword)((char*)(o) - ms_heap_start) >> MS_BLOCK_SIZE_SHIFT])
117 #else
118 typedef struct {
119 MSBlockInfo *info;
120 } MSBlockHeader;
122 #define MS_BLOCK_FOR_OBJ(o) (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
123 #endif
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) { \
130 (w) = i >> 5; \
131 (b) = i & 31; \
132 } else { \
133 (w) = i >> 6; \
134 (b) = i & 63; \
136 } while (0)
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) { \
144 was_marked = TRUE; \
145 break; \
147 if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)], \
148 (gpointer)(__old | __bitmask), \
149 (gpointer)__old) == \
150 (gpointer)__old) { \
151 was_marked = FALSE; \
152 break; \
154 } while (1)
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)
179 #else
180 #define LOCK_MS_BLOCK_LIST
181 #define UNLOCK_MS_BLOCK_LIST
182 #endif
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;
200 #ifdef FIXED_HEAP
201 /* non-allocated block free-list */
202 static MSBlockInfo *empty_blocks = NULL;
203 #else
204 /* non-allocated block free-list */
205 static void *empty_blocks = NULL;
206 static int num_empty_blocks = 0;
207 #endif
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;
226 static void
227 ms_signal_sweep_command (void)
229 if (!concurrent_sweep)
230 return;
232 g_assert (!ms_sweep_in_progress);
233 ms_sweep_in_progress = TRUE;
234 MONO_SEM_POST (&ms_sweep_cmd_semaphore);
237 static void
238 ms_signal_sweep_done (void)
240 if (!concurrent_sweep)
241 return;
243 MONO_SEM_POST (&ms_sweep_done_semaphore);
246 static void
247 ms_wait_for_sweep_done (void)
249 SGEN_TV_DECLARE (atv);
250 SGEN_TV_DECLARE (btv);
251 int result;
253 if (!concurrent_sweep)
254 return;
256 if (!ms_sweep_in_progress)
257 return;
259 SGEN_TV_GETTIME (atv);
260 while ((result = MONO_SEM_WAIT (&ms_sweep_done_semaphore)) != 0) {
261 if (errno != EINTR)
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;
271 static int
272 ms_find_block_obj_size_index (int size)
274 int i;
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)
278 return i;
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)))
289 #ifdef FIXED_HEAP
290 static void*
291 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
293 char *heap_start;
294 mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
295 mword alloc_size = nursery_size + major_heap_size;
296 int i;
298 g_assert (ms_heap_num_blocks > 0);
299 g_assert (nursery_size % MS_BLOCK_SIZE == 0);
300 if (nursery_align)
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];
315 else
316 block_infos [i].next_free = NULL;
317 block_infos [i].zeroed = TRUE;
320 empty_blocks = &block_infos [0];
322 return nursery_start;
324 #else
325 static void*
326 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
328 if (nursery_align)
329 nursery_start = mono_sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
330 else
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;
338 #endif
340 static void
341 update_heap_boundaries_for_block (MSBlockInfo *block)
343 mono_sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
346 #ifdef FIXED_HEAP
347 static MSBlockInfo*
348 ms_get_empty_block (void)
350 MSBlockInfo *block;
352 g_assert (empty_blocks);
354 block = empty_blocks;
355 empty_blocks = empty_blocks->next_free;
357 block->used = TRUE;
359 if (!block->zeroed)
360 memset (block->block, 0, MS_BLOCK_SIZE);
362 return block;
365 static void
366 ms_free_block (MSBlockInfo *block)
368 block->next_free = empty_blocks;
369 empty_blocks = block;
370 block->used = FALSE;
371 block->zeroed = FALSE;
372 mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
374 #else
375 static void*
376 ms_get_empty_block (void)
378 char *p;
379 int i;
380 void *block, *empty, *next;
382 retry:
383 if (!empty_blocks) {
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) {
387 block = p;
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.
393 do {
394 empty = empty_blocks;
395 *(void**)block = empty;
396 } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
397 p += MS_BLOCK_SIZE;
400 SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
402 stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
405 do {
406 empty = empty_blocks;
407 if (!empty)
408 goto retry;
409 block = empty;
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)));
419 return block;
422 static void
423 ms_free_block (void *block)
425 void *empty;
427 mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
428 memset (block, 0, MS_BLOCK_SIZE);
430 do {
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);
437 #endif
439 //#define MARKSWEEP_CONSISTENCY_CHECK
441 #ifdef MARKSWEEP_CONSISTENCY_CHECK
442 static void
443 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
445 MSBlockInfo *b;
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
452 one free slot */
453 g_assert (block->free_list);
455 #ifdef FIXED_HEAP
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);
459 #endif
460 /* the block must be in the all_blocks list */
461 for (b = all_blocks; b; b = b->next) {
462 if (b == block)
463 break;
465 g_assert (b == block);
469 static void
470 check_empty_blocks (void)
472 #ifndef FIXED_HEAP
473 void *p;
474 int i = 0;
475 for (p = empty_blocks; p; p = *(void**)p)
476 ++i;
477 g_assert (i == num_empty_blocks);
478 #endif
481 static void
482 consistency_check (void)
484 MSBlockInfo *block;
485 int i;
487 /* check all blocks */
488 FOREACH_BLOCK (block) {
489 int count = MS_BLOCK_FREE / block->obj_size;
490 int num_free = 0;
491 void **free;
493 #ifndef FIXED_HEAP
494 /* check block header */
495 g_assert (((MSBlockHeader*)block->block)->info == block);
496 #endif
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))
502 ++num_free;
505 /* check free list */
506 for (free = block->free_list; free; free = (void**)*free) {
507 g_assert (MS_BLOCK_FOR_OBJ (free) == block);
508 --num_free;
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);
515 } END_FOREACH_BLOCK;
517 /* check free blocks */
518 for (i = 0; i < num_block_obj_sizes; ++i) {
519 int j;
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 ();
526 #endif
528 static gboolean
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;
533 MSBlockInfo *info;
534 #ifndef FIXED_HEAP
535 MSBlockHeader *header;
536 #endif
537 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
538 char *obj_start;
539 int i;
541 if (!mono_sgen_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
542 return FALSE;
544 #ifdef FIXED_HEAP
545 info = ms_get_empty_block ();
546 #else
547 info = mono_sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
548 #endif
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);
558 #ifndef FIXED_HEAP
559 info->block = ms_get_empty_block ();
561 header = (MSBlockHeader*) info->block;
562 header->info = info;
563 #endif
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;
576 /* the last one */
577 *(void**)obj_start = NULL;
579 info->next_free = free_blocks [size_index];
580 free_blocks [size_index] = info;
582 info->next = all_blocks;
583 all_blocks = info;
585 ++num_major_sections;
586 return TRUE;
589 static gboolean
590 obj_is_from_pinned_alloc (char *ptr)
592 MSBlockInfo *block;
594 FOREACH_BLOCK (block) {
595 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
596 return block->pinned;
597 } END_FOREACH_BLOCK;
598 return FALSE;
601 static void*
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);
606 MSBlockInfo *block;
607 void *obj;
609 /* FIXME: try to do this without locking */
611 LOCK_MS_BLOCK_LIST;
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;
618 return NULL;
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.
640 *(void**)obj = NULL;
642 return obj;
645 static void*
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.
658 static void
659 free_object (char *obj, size_t size, gboolean pinned)
661 MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
662 int word, bit;
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;
679 static void
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 */
686 static void*
687 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
689 void *res;
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.
697 if (!res) {
698 sgen_collect_major_no_lock ("pinned alloc failure");
699 res = alloc_obj (size, TRUE, has_references);
701 return res;
704 static void
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.
713 static void*
714 major_alloc_degraded (MonoVTable *vtable, size_t size)
716 void *obj;
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);
731 return obj;
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.
741 static gboolean
742 major_is_object_live (char *obj)
744 MSBlockInfo *block;
745 int word, bit;
746 #ifndef FIXED_HEAP
747 mword objsize;
748 #endif
750 if (ptr_in_nursery (obj))
751 return FALSE;
753 #ifdef FIXED_HEAP
754 /* LOS */
755 if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
756 return FALSE;
757 #else
758 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
760 /* LOS */
761 if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
762 return FALSE;
763 #endif
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;
772 static gboolean
773 major_ptr_is_in_non_pinned_space (char *ptr)
775 MSBlockInfo *block;
777 FOREACH_BLOCK (block) {
778 if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
779 return !block->pinned;
780 } END_FOREACH_BLOCK;
781 return FALSE;
784 static void
785 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
787 MSBlockInfo *block;
789 ms_wait_for_sweep_done ();
791 FOREACH_BLOCK (block) {
792 int count = MS_BLOCK_FREE / block->obj_size;
793 int i;
795 if (block->pinned && !pinned)
796 continue;
797 if (!block->pinned && !non_pinned)
798 continue;
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);
805 } END_FOREACH_BLOCK;
808 static gboolean
809 major_is_valid_object (char *object)
811 MSBlockInfo *block;
813 ms_wait_for_sweep_done ();
814 FOREACH_BLOCK (block) {
815 int idx;
816 char *obj;
818 if ((block->block > object) || ((block->block + MS_BLOCK_SIZE) <= object))
819 continue;
821 idx = MS_BLOCK_OBJ_INDEX (object, block);
822 obj = (char*)MS_BLOCK_OBJ (block, idx);
823 if (obj != object)
824 return FALSE;
825 return MS_OBJ_ALLOCED (obj, block);
826 } END_FOREACH_BLOCK;
828 return FALSE;
831 static void
832 major_check_scan_starts (void)
836 static void
837 major_dump_heap (FILE *heap_dump_file)
839 MSBlockInfo *block;
840 int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
841 int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
842 int i;
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];
856 } END_FOREACH_BLOCK;
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;
867 int i;
868 int start = -1;
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)) {
874 if (start < 0)
875 start = i;
876 } else {
877 if (start >= 0) {
878 mono_sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
879 start = -1;
884 fprintf (heap_dump_file, "</section>\n");
885 } END_FOREACH_BLOCK;
888 #define LOAD_VTABLE SGEN_LOAD_VTABLE
890 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do { \
891 int __word, __bit; \
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))); \
899 } while (0)
900 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
901 int __word, __bit; \
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))); \
910 } while (0)
911 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
912 int __word, __bit; \
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))); \
922 } while (0)
924 #include "sgen-major-copy-object.h"
926 #ifdef SGEN_PARALLEL_MARK
927 static void
928 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
930 void *obj = *ptr;
931 mword vtable_word = *(mword*)obj;
932 MonoVTable *vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
933 mword objsize;
934 MSBlockInfo *block;
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) {
942 *ptr = (void*)vt;
943 return;
946 if (ptr_in_nursery (obj)) {
947 int word, bit;
948 gboolean has_references;
949 void *destination;
951 if (vtable_word & SGEN_PINNED_BIT)
952 return;
954 HEAVY_STAT (++stat_objects_copied_major);
956 do_copy_object:
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)) {
963 int size_index;
964 block = MS_BLOCK_FOR_OBJ (obj);
965 size_index = block->obj_size_index;
966 evacuate_block_obj_sizes [size_index] = FALSE;
969 do {
970 if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
971 mono_sgen_pin_object (obj, queue);
972 break;
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);
979 break;
982 /*someone pinned it, nothing to do.*/
983 if (vtable_word & SGEN_PINNED_BIT)
984 break;
985 } while (TRUE);
986 return;
989 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
990 gboolean was_marked;
992 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
993 obj = destination;
994 *ptr = obj;
997 * FIXME: If we make major_alloc_object() give
998 * us the block info, too, we won't have to
999 * re-fetch it here.
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);
1005 } else {
1007 * FIXME: We have allocated destination, but
1008 * we cannot use it. Give it back to the
1009 * allocator.
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);
1018 *ptr = obj;
1020 } else {
1021 #ifdef FIXED_HEAP
1022 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1023 #else
1024 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
1026 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1027 #endif
1029 int size_index;
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)
1036 return;
1037 HEAVY_STAT (++stat_major_objects_evacuated);
1038 goto do_copy_object;
1039 } else {
1040 MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1042 } else {
1043 if (vtable_word & SGEN_PINNED_BIT)
1044 return;
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);
1049 } else {
1050 g_assert (SGEN_OBJECT_IS_PINNED (obj));
1055 #else
1056 static void
1057 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1059 void *obj = *ptr;
1060 MSBlockInfo *block;
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)) {
1068 int word, bit;
1069 char *forwarded, *old_obj;
1071 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1072 *ptr = forwarded;
1073 return;
1075 if (SGEN_OBJECT_IS_PINNED (obj))
1076 return;
1078 HEAVY_STAT (++stat_objects_copied_major);
1080 do_copy_object:
1081 old_obj = obj;
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)) {
1086 int size_index;
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);
1092 return;
1094 *ptr = obj;
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
1100 * re-fetch it.
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);
1106 } else {
1107 char *forwarded;
1108 #ifndef FIXED_HEAP
1109 mword objsize;
1110 #endif
1112 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1113 *ptr = forwarded;
1114 return;
1117 #ifdef FIXED_HEAP
1118 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1119 #else
1120 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
1122 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1123 #endif
1125 int size_index;
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)
1132 return;
1133 HEAVY_STAT (++stat_major_objects_evacuated);
1134 goto do_copy_object;
1135 } else {
1136 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1138 } else {
1139 if (SGEN_OBJECT_IS_PINNED (obj))
1140 return;
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);
1148 #endif
1150 #include "sgen-major-scan-object.h"
1152 static void
1153 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1155 int i;
1156 int last_index = -1;
1158 if (!block->pin_queue_num_entries)
1159 return;
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)
1167 continue;
1168 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1169 last_index = index;
1173 static void
1174 ms_sweep (void)
1176 int i;
1177 MSBlockInfo **iter;
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];
1190 int j;
1191 for (j = 0; j < num_block_obj_sizes; ++j)
1192 free_blocks [j] = NULL;
1195 /* traverse all blocks, free and zero unmarked objects */
1196 iter = &all_blocks;
1197 while (*iter) {
1198 MSBlockInfo *block = *iter;
1199 int count;
1200 gboolean have_live = FALSE;
1201 gboolean has_pinned;
1202 int obj_index;
1203 int obj_size_index;
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) {
1216 int word, bit;
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)));
1222 have_live = TRUE;
1223 if (!has_pinned)
1224 ++slots_used [obj_size_index];
1225 } else {
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
1241 * order
1244 if (have_live) {
1245 if (!has_pinned) {
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);
1264 } else {
1266 * Blocks without live objects are removed from the
1267 * block list and freed.
1269 *iter = block->next;
1271 #ifdef FIXED_HEAP
1272 ms_free_block (block);
1273 #else
1274 ms_free_block (block->block);
1276 mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1277 #endif
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]);
1291 } else {
1292 evacuate_block_obj_sizes [i] = FALSE;
1296 have_swept = TRUE;
1299 static void*
1300 ms_sweep_thread_func (void *dummy)
1302 g_assert (concurrent_sweep);
1304 for (;;) {
1305 int result;
1307 while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
1308 if (errno != EINTR)
1309 g_error ("MONO_SEM_WAIT FAILED with %d errno %d (%s)", result, errno, strerror (errno));
1312 ms_sweep ();
1314 ms_signal_sweep_done ();
1317 return NULL;
1320 static void
1321 major_sweep (void)
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 ();
1328 } else {
1329 ms_sweep ();
1333 static int count_pinned_ref;
1334 static int count_pinned_nonref;
1335 static int count_nonpinned_ref;
1336 static int count_nonpinned_nonref;
1338 static void
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;
1345 else
1346 ++count_nonpinned_nonref;
1349 static void
1350 count_pinned_callback (char *obj, size_t size, void *data)
1352 MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1354 if (vtable->klass->has_references)
1355 ++count_pinned_ref;
1356 else
1357 ++count_pinned_nonref;
1360 static void __attribute__ ((unused))
1361 count_ref_nonref_objs (void)
1363 int total;
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);
1381 static int
1382 ms_calculate_block_obj_sizes (double factor, int *arr)
1384 double target_size = sizeof (MonoObject);
1385 int num_sizes = 0;
1386 int last_size = 0;
1388 do {
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) {
1393 if (arr)
1394 arr [num_sizes] = size;
1395 ++num_sizes;
1396 last_size = size;
1399 target_size *= factor;
1400 } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1402 return num_sizes;
1405 /* only valid during minor collections */
1406 static int old_num_major_sections;
1408 static void
1409 major_start_nursery_collection (void)
1411 ms_wait_for_sweep_done ();
1413 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1414 consistency_check ();
1415 #endif
1417 old_num_major_sections = num_major_sections;
1420 static void
1421 major_finish_nursery_collection (void)
1423 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1424 consistency_check ();
1425 #endif
1426 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1429 static void
1430 major_start_major_collection (void)
1432 int i;
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])
1439 continue;
1441 free_block_lists [0][i] = NULL;
1442 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1446 static void
1447 major_finish_major_collection (void)
1451 static void
1452 major_have_computer_minor_collection_allowance (void)
1454 #ifndef FIXED_HEAP
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)
1467 return;
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
1475 * single-threaded.
1477 --num_empty_blocks;
1479 ++stat_major_blocks_freed;
1481 #endif
1484 static void
1485 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1487 MSBlockInfo *block;
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;
1495 static void
1496 major_pin_objects (SgenGrayQueue *queue)
1498 MSBlockInfo *block;
1500 FOREACH_BLOCK (block) {
1501 mark_pinned_objects_in_block (block, queue);
1502 } END_FOREACH_BLOCK;
1505 static void
1506 major_init_to_space (void)
1510 static void
1511 major_report_pinned_memory_usage (void)
1513 g_assert_not_reached ();
1516 static gint64
1517 major_get_used_size (void)
1519 gint64 size = 0;
1520 MSBlockInfo *block;
1522 FOREACH_BLOCK (block) {
1523 int count = MS_BLOCK_FREE / block->obj_size;
1524 void **iter;
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;
1530 return size;
1533 static int
1534 get_num_major_sections (void)
1536 return num_major_sections;
1539 static gboolean
1540 major_handle_gc_param (const char *opt)
1542 #ifdef FIXED_HEAP
1543 if (g_str_has_prefix (opt, "major-heap-size=")) {
1544 const char *arg = strchr (opt, '=') + 1;
1545 glong size;
1546 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
1547 return FALSE;
1548 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1549 g_assert (ms_heap_num_blocks > 0);
1550 return TRUE;
1551 } else
1552 #endif
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");
1558 exit (1);
1560 evacuation_threshold = (float)percentage / 100.0;
1561 return TRUE;
1562 } else if (!strcmp (opt, "concurrent-sweep")) {
1563 concurrent_sweep = TRUE;
1564 return TRUE;
1565 } else if (!strcmp (opt, "no-concurrent-sweep")) {
1566 concurrent_sweep = FALSE;
1567 return TRUE;
1570 return FALSE;
1573 static void
1574 major_print_gc_param_usage (void)
1576 fprintf (stderr,
1578 #ifdef FIXED_HEAP
1579 " major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1580 #endif
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
1587 static void
1588 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1590 MSBlockInfo *block;
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.
1604 static guint8*
1605 initial_skip_card (guint8 *card_data)
1607 mword *cards = (mword*)card_data;
1608 mword card;
1609 int i;
1610 for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1611 card = cards [i];
1612 if (card)
1613 break;
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;
1623 #else
1624 for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1625 if (card_data [i])
1626 return &card_data [i];
1628 return card_data;
1629 #endif
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)
1637 ++card_data;
1638 return 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))
1645 static void
1646 major_scan_card_table (SgenGrayQueue *queue)
1648 MSBlockInfo *block;
1650 FOREACH_BLOCK (block) {
1651 int block_obj_size;
1652 char *block_start;
1654 if (!block->has_references)
1655 continue;
1657 block_obj_size = block->obj_size;
1658 block_start = block->block;
1660 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1661 guint8 *cards;
1662 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1663 guint8 cards_data [CARDS_PER_BLOCK];
1664 #endif
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);
1670 #else
1671 cards = cards_data;
1672 if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1673 continue;
1674 #endif
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);
1680 while (obj < end) {
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;
1687 } else {
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)) {
1702 int index;
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;
1706 char *obj;
1708 if (!*card_data)
1709 continue;
1710 sgen_card_table_prepare_card_for_scanning (card_data);
1712 if (idx == 0)
1713 index = 0;
1714 else
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);
1718 while (obj < end) {
1719 if (MS_OBJ_ALLOCED_FAST (obj, block_start))
1720 minor_scan_object (obj, queue);
1721 obj += block_obj_size;
1725 } END_FOREACH_BLOCK;
1727 #endif
1729 static gboolean
1730 major_is_worker_thread (pthread_t thread)
1732 if (concurrent_sweep)
1733 return thread == ms_sweep_thread;
1734 else
1735 return FALSE;
1738 #undef pthread_create
1740 void
1741 #ifdef SGEN_PARALLEL_MARK
1742 #ifdef FIXED_HEAP
1743 mono_sgen_marksweep_fixed_par_init
1744 #else
1745 mono_sgen_marksweep_par_init
1746 #endif
1747 #else
1748 #ifdef FIXED_HEAP
1749 mono_sgen_marksweep_fixed_init
1750 #else
1751 mono_sgen_marksweep_init
1752 #endif
1753 #endif
1754 (SgenMajorCollector *collector)
1756 int i;
1758 #ifndef FIXED_HEAP
1759 mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1760 #endif
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;
1772 int i;
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;
1804 #else
1805 collector->is_parallel = FALSE;
1806 #endif
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;
1825 #endif
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);
1851 #endif
1854 #endif