/*
- * sgen-marksweep.c: Simple generational GC.
+ * sgen-marksweep.c: The Mark & Sweep major collector.
*
* Author:
* Mark Probst <mark.probst@gmail.com>
*
* Copyright 2009-2010 Novell, Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * Copyright (C) 2012 Xamarin Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License 2.0 as published by the Free Software Foundation;
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License 2.0 along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
#include "metadata/sgen-gc.h"
#include "metadata/sgen-protocol.h"
#include "metadata/sgen-cardtable.h"
+#include "metadata/sgen-memory-governor.h"
#include "metadata/gc-internal.h"
#define MS_BLOCK_SIZE (16*1024)
unsigned int has_references : 1;
unsigned int has_pinned : 1; /* means cannot evacuate */
unsigned int is_to_space : 1;
+ unsigned int swept : 1;
#ifdef FIXED_HEAP
unsigned int used : 1;
unsigned int zeroed : 1;
};
#ifdef FIXED_HEAP
-static int ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
+static mword ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
static char *ms_heap_start;
static char *ms_heap_end;
#endif
#define MS_BLOCK_OBJ(b,i) ((b)->block + MS_BLOCK_SKIP + (b)->obj_size * (i))
+#define MS_BLOCK_OBJ_FOR_SIZE(b,i,obj_size) ((b)->block + MS_BLOCK_SKIP + (obj_size) * (i))
#define MS_BLOCK_DATA_FOR_OBJ(o) ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
#ifdef FIXED_HEAP
static float evacuation_threshold = 0.666;
static gboolean concurrent_sweep = FALSE;
+static gboolean lazy_sweep = TRUE;
static gboolean have_swept;
/* all allocated blocks in the system */
static long long stat_major_blocks_alloced = 0;
static long long stat_major_blocks_freed = 0;
+static long long stat_major_blocks_lazy_swept = 0;
static long long stat_major_objects_evacuated = 0;
static long long stat_time_wait_for_sweep = 0;
-#ifdef SGEN_PARALLEL_MARK
-static long long stat_slots_allocated_in_vain = 0;
-#endif
static gboolean ms_sweep_in_progress = FALSE;
static MonoNativeThreadId ms_sweep_thread;
static MonoSemType ms_sweep_cmd_semaphore;
static MonoSemType ms_sweep_done_semaphore;
+static void
+sweep_block (MSBlockInfo *block);
+
static void
ms_signal_sweep_command (void)
{
ms_find_block_obj_size_index (int size)
{
int i;
- DEBUG (9, g_assert (size <= SGEN_MAX_SMALL_OBJ_SIZE));
+ SGEN_ASSERT (9, size <= SGEN_MAX_SMALL_OBJ_SIZE, "size %d is bigger than max small object size %d", size, SGEN_MAX_SMALL_OBJ_SIZE);
for (i = 0; i < num_block_obj_sizes; ++i)
if (block_obj_sizes [i] >= size)
return i;
char *nursery_start;
mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
mword alloc_size = nursery_size + major_heap_size;
- int i;
+ mword i;
g_assert (ms_heap_num_blocks > 0);
g_assert (nursery_size % MS_BLOCK_SIZE == 0);
if (nursery_align)
g_assert (nursery_align % MS_BLOCK_SIZE == 0);
- nursery_start = sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE);
+ nursery_start = sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "heap");
ms_heap_start = nursery_start + nursery_size;
ms_heap_end = ms_heap_start + major_heap_size;
- block_infos = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
+ block_infos = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO, TRUE);
for (i = 0; i < ms_heap_num_blocks; ++i) {
block_infos [i].block = ms_heap_start + i * MS_BLOCK_SIZE;
{
char *start;
if (nursery_align)
- start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
+ start = sgen_alloc_os_memory_aligned (nursery_size, nursery_align, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
else
- start = sgen_alloc_os_memory (nursery_size, TRUE);
+ start = sgen_alloc_os_memory (nursery_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "nursery");
return start;
}
empty_blocks = block;
block->used = FALSE;
block->zeroed = FALSE;
- sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
+ sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
}
#else
static void*
retry:
if (!empty_blocks) {
- p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
+ p = sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "major heap section");
for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
block = p;
{
void *empty;
- sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
+ sgen_memgov_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
memset (block, 0, MS_BLOCK_SIZE);
do {
/* blocks in the free lists must have at least
one free slot */
- g_assert (block->free_list);
+ if (block->swept)
+ g_assert (block->free_list);
#ifdef FIXED_HEAP
/* the block must not be in the empty_blocks list */
g_assert (num_free == 0);
/* check all mark words are zero */
- for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
- g_assert (block->mark_words [i] == 0);
+ if (block->swept) {
+ for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
+ g_assert (block->mark_words [i] == 0);
+ }
} END_FOREACH_BLOCK;
/* check free blocks */
char *obj_start;
int i;
- if (!sgen_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
+ if (!sgen_memgov_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
return FALSE;
#ifdef FIXED_HEAP
info = sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
#endif
- DEBUG (9, g_assert (count >= 2));
+ SGEN_ASSERT (9, count >= 2, "block with %d objects, it must hold at least 2", count);
info->obj_size = size;
info->obj_size_index = size_index;
info->has_references = has_references;
info->has_pinned = pinned;
info->is_to_space = (sgen_get_current_collection_generation () == GENERATION_OLD); /*FIXME WHY??? */
+ info->swept = 1;
#ifndef FIXED_HEAP
info->block = ms_get_empty_block ();
void *obj;
block = free_blocks [size_index];
- DEBUG (9, g_assert (block));
+ SGEN_ASSERT (9, block, "no free block to unlink from free_blocks %p size_index %d", free_blocks, size_index);
+
+ if (G_UNLIKELY (!block->swept)) {
+ stat_major_blocks_lazy_swept ++;
+ sweep_block (block);
+ }
obj = block->free_list;
- DEBUG (9, g_assert (obj));
+ SGEN_ASSERT (9, obj, "block %p in free list had no available object to alloc from", block);
block->free_list = *(void**)obj;
if (!block->free_list) {
MSBlockInfo *block;
void *obj;
- DEBUG (9, g_assert (!ms_sweep_in_progress));
- DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
+ SGEN_ASSERT (9, !ms_sweep_in_progress, "concurrent sweep in progress with concurrent allocation");
+ SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
if (free_blocks_local [size_index]) {
get_slot:
void *obj;
#ifdef SGEN_PARALLEL_MARK
- DEBUG (9, g_assert (current_collection_generation != GENERATION_OLD));
+ SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
+
#endif
- DEBUG (9, g_assert (!ms_sweep_in_progress));
+ SGEN_ASSERT (9, !ms_sweep_in_progress, "concurrent sweep in progress with concurrent allocation");
if (!free_blocks [size_index]) {
if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references)))
{
MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
int word, bit;
- DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
- DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
+
+ if (!block->swept)
+ sweep_block (block);
+ SGEN_ASSERT (9, (pinned && block->pinned) || (!pinned && !block->pinned), "free-object pinning mixup object %p pinned %d block %p pinned %d", obj, pinned, block, block->pinned);
+ SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p is already free", obj);
MS_CALC_MARK_BIT (word, bit, obj);
- DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
+ SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p has mark bit set");
if (!block->free_list) {
MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
- DEBUG (9, g_assert (!block->next_free));
+ SGEN_ASSERT (9, !block->next_free, "block %p doesn't have a free-list of object but belongs to a free-list of blocks");
block->next_free = free_blocks [size_index];
free_blocks [size_index] = block;
}
*as pinned alloc is requested by the runtime.
*/
if (!res) {
- sgen_collect_major_no_lock ("pinned alloc failure");
- res = alloc_obj (size, TRUE, has_references);
+ sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure");
+ res = alloc_obj (size, TRUE, has_references);
}
return res;
}
/* now we know it's in a major block */
block = MS_BLOCK_FOR_OBJ (obj);
- DEBUG (9, g_assert (!block->pinned));
+ SGEN_ASSERT (9, !block->pinned, "block %p is pinned, BTW why is this bad?");
MS_CALC_MARK_BIT (word, bit, obj);
return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
}
static gboolean
-major_ptr_is_in_non_pinned_space (char *ptr)
+major_ptr_is_in_non_pinned_space (char *ptr, char **start)
{
MSBlockInfo *block;
FOREACH_BLOCK (block) {
- if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE)
+ if (ptr >= block->block && ptr <= block->block + MS_BLOCK_SIZE) {
+ int count = MS_BLOCK_FREE / block->obj_size;
+ int i;
+
+ *start = NULL;
+ for (i = 0; i <= count; ++i) {
+ if (ptr >= MS_BLOCK_OBJ (block, i) && ptr < MS_BLOCK_OBJ (block, i + 1)) {
+ *start = MS_BLOCK_OBJ (block, i);
+ break;
+ }
+ }
return !block->pinned;
+ }
} END_FOREACH_BLOCK;
return FALSE;
}
continue;
if (!block->pinned && !non_pinned)
continue;
+ if (lazy_sweep)
+ sweep_block (block);
for (i = 0; i < count; ++i) {
void **obj = (void**) MS_BLOCK_OBJ (block, i);
} END_FOREACH_BLOCK;
}
+static gboolean
+major_is_valid_object (char *object)
+{
+ MSBlockInfo *block;
+
+ ms_wait_for_sweep_done ();
+ FOREACH_BLOCK (block) {
+ int idx;
+ char *obj;
+
+ if ((block->block > object) || ((block->block + MS_BLOCK_SIZE) <= object))
+ continue;
+
+ idx = MS_BLOCK_OBJ_INDEX (object, block);
+ obj = (char*)MS_BLOCK_OBJ (block, idx);
+ if (obj != object)
+ return FALSE;
+ return MS_OBJ_ALLOCED (obj, block);
+ } END_FOREACH_BLOCK;
+
+ return FALSE;
+}
+
+
+static gboolean
+major_describe_pointer (char *ptr)
+{
+ MSBlockInfo *block;
+
+ FOREACH_BLOCK (block) {
+ int idx;
+ char *obj;
+ gboolean live;
+ MonoVTable *vtable;
+
+ if ((block->block > ptr) || ((block->block + MS_BLOCK_SIZE) <= ptr))
+ continue;
+
+ SGEN_LOG (1, "major-ptr (block %p sz %d pin %d ref %d) ",
+ block->block, block->obj_size, block->pinned, block->has_references);
+
+ idx = MS_BLOCK_OBJ_INDEX (ptr, block);
+ obj = (char*)MS_BLOCK_OBJ (block, idx);
+ live = MS_OBJ_ALLOCED (obj, block);
+ vtable = live ? (MonoVTable*)SGEN_LOAD_VTABLE (obj) : NULL;
+
+ if (obj == ptr) {
+ if (live)
+ SGEN_LOG (1, "\t(object %s.%s)", vtable->klass->name_space, vtable->klass->name);
+ else
+ SGEN_LOG (1, "(dead-object)");
+ } else {
+ if (live)
+ SGEN_LOG (1, "(interior-ptr offset %td of %p %s.%s)",
+ ptr - obj,
+ obj, vtable->klass->name_space, vtable->klass->name);
+ else
+ SGEN_LOG (1, "(dead-interior-ptr to %td to %p)",
+ ptr - obj, obj);
+ }
+
+ return TRUE;
+ } END_FOREACH_BLOCK;
+
+ return FALSE;
+}
+
static void
major_check_scan_starts (void)
{
#define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
int __word, __bit; \
MS_CALC_MARK_BIT (__word, __bit, (obj)); \
- DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
+ SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
if (!MS_MARK_BIT ((block), __word, __bit)) { \
MS_SET_MARK_BIT ((block), __word, __bit); \
if ((block)->has_references) \
#define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do { \
int __word, __bit; \
gboolean __was_marked; \
- DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block)))); \
+ SGEN_ASSERT (9, MS_OBJ_ALLOCED ((obj), (block)), "object %p not allocated", obj); \
MS_CALC_MARK_BIT (__word, __bit, (obj)); \
MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
if (!__was_marked) { \
} while (0)
static void
-pin_major_object (void *obj, SgenGrayQueue *queue)
+pin_major_object (char *obj, SgenGrayQueue *queue)
{
MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
block->has_pinned = TRUE;
MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
}
-#ifdef SGEN_PARALLEL_MARK
-static void
-pin_or_update_par (void **ptr, void *obj, MonoVTable *vt, SgenGrayQueue *queue)
-{
- for (;;) {
- mword vtable_word;
- gboolean major_pinned = FALSE;
-
- if (sgen_ptr_in_nursery (obj)) {
- if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
- sgen_pin_object (obj, queue);
- break;
- }
- } else {
- pin_major_object (obj, queue);
- major_pinned = TRUE;
- }
-
- vtable_word = *(mword*)obj;
- /*someone else forwarded it, update the pointer and bail out*/
- if (vtable_word & SGEN_FORWARDED_BIT) {
- *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
- break;
- }
-
- /*someone pinned it, nothing to do.*/
- if (vtable_word & SGEN_PINNED_BIT || major_pinned)
- break;
- }
-}
-#endif
-
#include "sgen-major-copy-object.h"
#ifdef SGEN_PARALLEL_MARK
HEAVY_STAT (++stat_copy_object_called_major);
- DEBUG (9, g_assert (obj));
- DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
+ SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
+ SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
if (sgen_ptr_in_nursery (obj)) {
int word, bit;
if (vtable_word & SGEN_PINNED_BIT)
return;
+ /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
if (sgen_nursery_is_to_space (obj))
return;
objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
- destination = sgen_par_alloc_for_promotion (obj, objsize, has_references);
+ destination = sgen_minor_collector.par_alloc_for_promotion (obj, objsize, has_references);
if (G_UNLIKELY (!destination)) {
if (!sgen_ptr_in_nursery (obj)) {
int size_index;
evacuate_block_obj_sizes [size_index] = FALSE;
}
- pin_or_update_par (ptr, obj, vt, queue);
+ sgen_parallel_pin_or_update (ptr, obj, vt, queue);
sgen_set_pinned_from_failed_allocation (objsize);
return;
}
*
* FIXME (2): We should rework this to avoid all those nursery checks.
*/
- if (!sgen_ptr_in_nursery (obj)) { /*marking a nursery object is pretty stupid.*/
+ /*
+ * For the split nursery allocator the object
+ * might still be in the nursery despite
+ * having being promoted, in which case we
+ * can't mark it.
+ */
+ if (!sgen_ptr_in_nursery (obj)) {
block = MS_BLOCK_FOR_OBJ (obj);
MS_CALC_MARK_BIT (word, bit, obj);
- DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
+ SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
}
} else {
*ptr = obj;
- ++stat_slots_allocated_in_vain;
+ HEAVY_STAT (++stat_slots_allocated_in_vain);
}
} else {
#ifdef FIXED_HEAP
HEAVY_STAT (++stat_copy_object_called_major);
- DEBUG (9, g_assert (obj));
- DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
+ SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
+ SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
if (sgen_ptr_in_nursery (obj)) {
int word, bit;
if (SGEN_OBJECT_IS_PINNED (obj))
return;
+ /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
if (sgen_nursery_is_to_space (obj))
return;
*
* FIXME (2): We should rework this to avoid all those nursery checks.
*/
- if (!sgen_ptr_in_nursery (obj)) { /*marking a nursery object is pretty stupid.*/
+ /*
+ * For the split nursery allocator the object might
+ * still be in the nursery despite having being
+ * promoted, in which case we can't mark it.
+ */
+ if (!sgen_ptr_in_nursery (obj)) {
block = MS_BLOCK_FOR_OBJ (obj);
MS_CALC_MARK_BIT (word, bit, obj);
- DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
+ SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
MS_SET_MARK_BIT (block, word, bit);
}
} else {
if (SGEN_OBJECT_IS_PINNED (obj))
return;
binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size ((MonoObject*)obj));
+ if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
+ MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
+ MONO_GC_OBJ_PINNED ((mword)obj, sgen_safe_object_get_size (obj), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
+ }
SGEN_PIN_OBJECT (obj);
/* FIXME: only enqueue if object has references */
GRAY_OBJECT_ENQUEUE (queue, obj);
for (i = 0; i < block->pin_queue_num_entries; ++i) {
int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
- DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
+ SGEN_ASSERT (9, index >= 0 && index < MS_BLOCK_FREE / block->obj_size, "invalid object %p index %d max-index %d", block->pin_queue_start [i], index, MS_BLOCK_FREE / block->obj_size);
if (index == last_index)
continue;
MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
}
}
+static inline void
+sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
+{
+ int obj_index;
+
+ for (obj_index = 0; obj_index < count; ++obj_index) {
+ int word, bit;
+ void *obj = MS_BLOCK_OBJ_FOR_SIZE (block, obj_index, obj_size);
+
+ MS_CALC_MARK_BIT (word, bit, obj);
+ if (MS_MARK_BIT (block, word, bit)) {
+ SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
+ } else {
+ /* an unmarked object */
+ if (MS_OBJ_ALLOCED (obj, block)) {
+ /*
+ * FIXME: Merge consecutive
+ * slots for lower reporting
+ * overhead. Maybe memset
+ * will also benefit?
+ */
+ binary_protocol_empty (obj, obj_size);
+ MONO_GC_MAJOR_SWEPT ((mword)obj, obj_size);
+ memset (obj, 0, obj_size);
+ }
+ *(void**)obj = block->free_list;
+ block->free_list = obj;
+ }
+ }
+}
+
+/*
+ * sweep_block:
+ *
+ * Traverse BLOCK, freeing and zeroing unused objects.
+ */
+static void
+sweep_block (MSBlockInfo *block)
+{
+ int count;
+
+ if (block->swept)
+ return;
+
+ count = MS_BLOCK_FREE / block->obj_size;
+
+ block->free_list = NULL;
+
+ /* Use inline instances specialized to constant sizes, this allows the compiler to replace the memset calls with inline code */
+ // FIXME: Add more sizes
+ switch (block->obj_size) {
+ case 16:
+ sweep_block_for_size (block, count, 16);
+ break;
+ default:
+ sweep_block_for_size (block, count, block->obj_size);
+ break;
+ }
+
+ /* reset mark bits */
+ memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
+
+ /*
+ * FIXME: reverse free list so that it's in address
+ * order
+ */
+
+ block->swept = 1;
+}
+
+static inline int
+bitcount (mword d)
+{
+#if SIZEOF_VOID_P == 8
+ /* http://www.jjj.de/bitwizardry/bitwizardrypage.html */
+ d -= (d>>1) & 0x5555555555555555;
+ d = ((d>>2) & 0x3333333333333333) + (d & 0x3333333333333333);
+ d = ((d>>4) + d) & 0x0f0f0f0f0f0f0f0f;
+ d *= 0x0101010101010101;
+ return d >> 56;
+#else
+ /* http://aggregate.org/MAGIC/ */
+ d -= ((d >> 1) & 0x55555555);
+ d = (((d >> 2) & 0x33333333) + (d & 0x33333333));
+ d = (((d >> 4) + d) & 0x0f0f0f0f);
+ d += (d >> 8);
+ d += (d >> 16);
+ return (d & 0x0000003f);
+#endif
+}
+
static void
ms_sweep (void)
{
int count;
gboolean have_live = FALSE;
gboolean has_pinned;
- int obj_index;
+ gboolean have_free = FALSE;
int obj_size_index;
+ int nused = 0;
obj_size_index = block->obj_size_index;
block->has_pinned = block->pinned;
block->is_to_space = FALSE;
+ block->swept = 0;
count = MS_BLOCK_FREE / block->obj_size;
- block->free_list = NULL;
- for (obj_index = 0; obj_index < count; ++obj_index) {
- int word, bit;
- void *obj = MS_BLOCK_OBJ (block, obj_index);
-
- MS_CALC_MARK_BIT (word, bit, obj);
- if (MS_MARK_BIT (block, word, bit)) {
- DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
- have_live = TRUE;
- if (!has_pinned)
- ++slots_used [obj_size_index];
- } else {
- /* an unmarked object */
- if (MS_OBJ_ALLOCED (obj, block)) {
- binary_protocol_empty (obj, block->obj_size);
- memset (obj, 0, block->obj_size);
- }
- *(void**)obj = block->free_list;
- block->free_list = obj;
- }
+ /* Count marked objects in the block */
+ for (i = 0; i < MS_NUM_MARK_WORDS; ++i) {
+ nused += bitcount (block->mark_words [i]);
}
+ if (nused) {
+ have_live = TRUE;
+ }
+ if (nused < count)
+ have_free = TRUE;
- /* reset mark bits */
- memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
-
- /*
- * FIXME: reverse free list so that it's in address
- * order
- */
+ if (!lazy_sweep)
+ sweep_block (block);
if (have_live) {
if (!has_pinned) {
++num_blocks [obj_size_index];
+ slots_used [obj_size_index] += nused;
slots_available [obj_size_index] += count;
}
* If there are free slots in the block, add
* the block to the corresponding free list.
*/
- if (block->free_list) {
+ if (have_free) {
MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
block->next_free = free_blocks [index];
--num_major_sections;
}
}
-
for (i = 0; i < num_block_obj_sizes; ++i) {
float usage = (float)slots_used [i] / (float)slots_available [i];
if (num_blocks [i] > 5 && usage < evacuation_threshold) {
while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
if (errno != EINTR)
- g_error ("MONO_SEM_WAIT");
+ g_error ("MONO_SEM_WAIT FAILED with %d errno %d (%s)", result, errno, strerror (errno));
}
ms_sweep ();
++count_pinned_nonref;
}
-static void __attribute__ ((unused))
+static G_GNUC_UNUSED void
count_ref_nonref_objs (void)
{
int total;
free_block_lists [0][i] = NULL;
free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
}
+
+ // Sweep all unswept blocks
+ if (lazy_sweep) {
+ MSBlockInfo **iter;
+
+ iter = &all_blocks;
+ while (*iter) {
+ MSBlockInfo *block = *iter;
+
+ sweep_block (block);
+
+ iter = &block->next;
+ }
+ }
}
static void
while (num_empty_blocks > section_reserve) {
void *next = *(void**)empty_blocks;
- sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
+ sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE, SGEN_ALLOC_HEAP);
empty_blocks = next;
/*
* Needs not be atomic because this is running
continue;
#endif
+ if (!block->swept)
+ sweep_block (block);
+
obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
end = block_start + MS_BLOCK_SIZE;
base = sgen_card_table_align_pointer (obj);
if (!*card_data)
continue;
+ if (!block->swept)
+ sweep_block (block);
+
HEAVY_STAT (++marked_cards);
sgen_card_table_prepare_card_for_scanning (card_data);
{
int i;
for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
- lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
+ lists [i] = sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
}
#ifdef SGEN_PARALLEL_MARK
post_param_init (void)
{
if (concurrent_sweep) {
- if (mono_native_thread_create (&ms_sweep_thread, ms_sweep_thread_func, NULL)) {
+ if (!mono_native_thread_create (&ms_sweep_thread, ms_sweep_thread_func, NULL)) {
fprintf (stderr, "Error: Could not create sweep thread.\n");
exit (1);
}
#endif
num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
- block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
+ block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
- evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
+ evacuate_block_obj_sizes = sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES, TRUE);
for (i = 0; i < num_block_obj_sizes; ++i)
evacuate_block_obj_sizes [i] = FALSE;
mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
+ mono_counters_register ("# major blocks lazy swept", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_lazy_swept);
mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &stat_time_wait_for_sweep);
#ifdef SGEN_PARALLEL_MARK
- mono_counters_register ("Slots allocated in vain", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_slots_allocated_in_vain);
-
#ifndef HAVE_KW_THREAD
mono_native_tls_alloc (&workers_free_block_lists_key, NULL);
#endif
collector->free_non_pinned_object = major_free_non_pinned_object;
collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
collector->pin_objects = major_pin_objects;
+ collector->pin_major_object = pin_major_object;
#ifdef SGEN_HAVE_CARDTABLE
collector->scan_card_table = major_scan_card_table;
collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
collector->print_gc_param_usage = major_print_gc_param_usage;
collector->is_worker_thread = major_is_worker_thread;
collector->post_param_init = post_param_init;
+ collector->is_valid_object = major_is_valid_object;
+ collector->describe_pointer = major_describe_pointer;
- /* FIXME this macro mess */
collector->major_ops.copy_or_mark_object = major_copy_or_mark_object;
- FILL_COLLECTOR_COPY_OBJECT (collector);
- FILL_COLLECTOR_SCAN_OBJECT (collector);
+ collector->major_ops.scan_object = major_scan_object;
#ifdef SGEN_HAVE_CARDTABLE
/*cardtable requires major pages to be 8 cards aligned*/