[sgen] More information in sweep and world restart DTrace probes.
[mono.git] / mono / metadata / sgen-marksweep.c
index a87251d6aa9741a79f37deb91328b738dd6c7c73..db3de651bb377f3cf0d9b15dafc4066ce343c10b 100644 (file)
@@ -1,29 +1,24 @@
 /*
- * 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"
@@ -98,6 +93,9 @@ struct _MSBlockInfo {
        void **free_list;
        MSBlockInfo *next_free;
        void **pin_queue_start;
+#ifdef SGEN_CONCURRENT_MARK
+       guint8 *cardtable_mod_union;
+#endif
        mword mark_words [MS_NUM_MARK_WORDS];
 };
 
@@ -114,6 +112,7 @@ static MSBlockInfo *block_infos;
 #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
@@ -279,7 +278,7 @@ static int
 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;
@@ -567,7 +566,7 @@ ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
        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;
@@ -582,6 +581,9 @@ ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
        header = (MSBlockHeader*) info->block;
        header->info = info;
 #endif
+#ifdef SGEN_CONCURRENT_MARK
+       info->cardtable_mod_union = NULL;
+#endif
 
        update_heap_boundaries_for_block (info);
 
@@ -636,7 +638,7 @@ unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_inde
        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 ++;
@@ -644,7 +646,7 @@ unlink_slot_from_free_list_uncontested (MSBlockInfo **free_blocks, int size_inde
        }
 
        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) {
@@ -677,15 +679,15 @@ try_remove_block_from_free_list (MSBlockInfo *block, MSBlockInfo **free_blocks,
 }
 
 static void*
-alloc_obj_par (int size, gboolean pinned, gboolean has_references)
+alloc_obj_par (MonoVTable *vtable, int size, gboolean pinned, gboolean has_references)
 {
        int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
        MSBlockInfo **free_blocks_local = FREE_BLOCKS_LOCAL (pinned, has_references);
        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:
@@ -719,34 +721,35 @@ alloc_obj_par (int size, gboolean pinned, gboolean has_references)
                }
        }
 
-       /*
-        * FIXME: This should not be necessary because it'll be
-        * overwritten by the vtable immediately.
-        */
-       *(void**)obj = NULL;
+       *(MonoVTable**)obj = vtable;
+
+#ifdef SGEN_CONCURRENT_MARK
+       g_assert_not_reached ();
+#endif
 
        return obj;
 }
 
 static void*
-major_par_alloc_object (int size, gboolean has_references)
+major_par_alloc_object (MonoVTable *vtable, int size, gboolean has_references)
 {
-       return alloc_obj_par (size, FALSE, has_references);
+       return alloc_obj_par (vtable, size, FALSE, has_references);
 }
 #endif
 
 static void*
-alloc_obj (int size, gboolean pinned, gboolean has_references)
+alloc_obj (MonoVTable *vtable, int size, gboolean pinned, gboolean has_references)
 {
        int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
        MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
        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)))
@@ -755,19 +758,25 @@ alloc_obj (int size, gboolean pinned, gboolean has_references)
 
        obj = unlink_slot_from_free_list_uncontested (free_blocks, size_index);
 
-       /*
-        * FIXME: This should not be necessary because it'll be
-        * overwritten by the vtable immediately.
-        */
-       *(void**)obj = NULL;
+       *(MonoVTable**)obj = vtable;
+
+#ifdef SGEN_CONCURRENT_MARK
+       if (obj && sgen_remember_major_object_for_concurrent_mark (obj)) {
+               MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
+               int word, bit;
+               MS_CALC_MARK_BIT (word, bit, obj);
+               MS_SET_MARK_BIT (block, word, bit);
+               binary_protocol_mark (obj, NULL, size);
+       }
+#endif
 
        return obj;
 }
 
 static void*
-major_alloc_object (int size, gboolean has_references)
+major_alloc_object (MonoVTable *vtable, int size, gboolean has_references)
 {
-       return alloc_obj (size, FALSE, has_references);
+       return alloc_obj (vtable, size, FALSE, has_references);
 }
 
 /*
@@ -782,14 +791,17 @@ free_object (char *obj, size_t size, gboolean pinned)
 {
        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;
        }
@@ -806,19 +818,19 @@ major_free_non_pinned_object (char *obj, size_t size)
 
 /* size is a multiple of SGEN_ALLOC_ALIGN */
 static void*
-major_alloc_small_pinned_obj (size_t size, gboolean has_references)
+major_alloc_small_pinned_obj (MonoVTable *vtable, size_t size, gboolean has_references)
 {
        void *res;
 
        ms_wait_for_sweep_done ();
 
-       res = alloc_obj (size, TRUE, has_references);
+       res = alloc_obj (vtable, size, TRUE, has_references);
         /*If we failed to alloc memory, we better try releasing memory
          *as pinned alloc is requested by the runtime.
          */
         if (!res) {
-               sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure");
-               res = alloc_obj (size, TRUE, has_references);
+               sgen_perform_collection (0, GENERATION_OLD, "pinned alloc failure", TRUE);
+               res = alloc_obj (vtable, size, TRUE, has_references);
         }
         return res;
 }
@@ -842,9 +854,8 @@ major_alloc_degraded (MonoVTable *vtable, size_t size)
 
        old_num_sections = num_major_sections;
 
-       obj = alloc_obj (size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
+       obj = alloc_obj (vtable, size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable));
        if (G_LIKELY (obj)) {
-               *(MonoVTable**)obj = vtable;
                HEAVY_STAT (++stat_objects_alloced_degraded);
                HEAVY_STAT (stat_bytes_alloced_degraded += size);
                g_assert (num_major_sections >= old_num_sections);
@@ -886,19 +897,30 @@ major_is_object_live (char *obj)
 
        /* 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;
 }
@@ -918,6 +940,8 @@ major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallba
                        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);
@@ -965,7 +989,7 @@ major_describe_pointer (char *ptr)
                if ((block->block > ptr) || ((block->block + MS_BLOCK_SIZE) <= ptr))
                        continue;
 
-               fprintf (gc_debug_file, "major-ptr (block %p sz %d pin %d ref %d) ",
+               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);
@@ -975,16 +999,16 @@ major_describe_pointer (char *ptr)
                
                if (obj == ptr) {
                        if (live)
-                               fprintf (gc_debug_file, "(object %s.%s)", vtable->klass->name_space, vtable->klass->name);
+                               SGEN_LOG (1, "\t(object %s.%s)", vtable->klass->name_space, vtable->klass->name);
                        else
-                               fprintf (gc_debug_file, "(dead-object)");
+                               SGEN_LOG (1, "(dead-object)");
                } else {
                        if (live)
-                               fprintf (gc_debug_file, "(interior-ptr offset %td of %p %s.%s)",
+                               SGEN_LOG (1, "(interior-ptr offset %td of %p %s.%s)",
                                        ptr - obj,
                                        obj, vtable->klass->name_space, vtable->klass->name);
                        else
-                               fprintf (gc_debug_file, "(dead-interior-ptr to %td to %p)",
+                               SGEN_LOG (1, "(dead-interior-ptr to %td to %p)",
                                        ptr - obj, obj);
                }
 
@@ -1066,7 +1090,7 @@ major_dump_heap (FILE *heap_dump_file)
 #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)                    \
@@ -1077,7 +1101,7 @@ major_dump_heap (FILE *heap_dump_file)
 #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) {                                    \
@@ -1090,9 +1114,13 @@ major_dump_heap (FILE *heap_dump_file)
 static void
 pin_major_object (char *obj, SgenGrayQueue *queue)
 {
+#ifdef SGEN_CONCURRENT_MARK
+       g_assert_not_reached ();
+#else
        MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
        block->has_pinned = TRUE;
        MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
+#endif
 }
 
 #include "sgen-major-copy-object.h"
@@ -1108,8 +1136,8 @@ major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
 
        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;
@@ -1136,7 +1164,7 @@ major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
                objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
                has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
 
-               destination = sgen_minor_collector.par_alloc_for_promotion (obj, objsize, has_references);
+               destination = sgen_minor_collector.par_alloc_for_promotion (vt, obj, objsize, has_references);
                if (G_UNLIKELY (!destination)) {
                        if (!sgen_ptr_in_nursery (obj)) {
                                int size_index;
@@ -1150,14 +1178,6 @@ major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
                        return;
                }
 
-               /*
-                * We do this before the CAS because we want to make
-                * sure that if another thread sees the destination
-                * pointer the VTable is already in place.  Not doing
-                * this can crash binary protocols.
-                */
-               *(MonoVTable**)destination = vt;
-
                if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
                        gboolean was_marked;
 
@@ -1181,7 +1201,7 @@ major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
                        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 {
@@ -1245,20 +1265,55 @@ major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
 
                        MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
                } else {
-#ifdef FIXED_HEAP
-                       mword vtable_word = *(mword*)obj;
-                       vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
-#endif
+                       LOSObject *bigobj = sgen_los_header_for_object (obj);
+                       mword size_word = bigobj->size;
 
-                       if (vtable_word & SGEN_PINNED_BIT)
+                       if (size_word & 1)
                                return;
                        binary_protocol_pin (obj, vt, sgen_safe_object_get_size ((MonoObject*)obj));
-                       if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
+                       if (SGEN_CAS_PTR ((void*)&bigobj->size, (void*)(size_word | 1), (void*)size_word) == (void*)size_word) {
                                if (SGEN_VTABLE_HAS_REFERENCES (vt))
                                        GRAY_OBJECT_ENQUEUE (queue, obj);
                        } else {
-                               g_assert (SGEN_OBJECT_IS_PINNED (obj));
+                               g_assert (sgen_los_object_is_pinned (obj));
+                       }
+               }
+       }
+}
+#else
+#ifdef SGEN_CONCURRENT_MARK
+static void
+major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
+{
+       void *obj = *ptr;
+
+       g_assert (!SGEN_OBJECT_IS_FORWARDED (obj));
+
+       if (sgen_ptr_in_nursery (obj)) {
+               g_assert (!SGEN_OBJECT_IS_PINNED (obj));
+       } else {
+#ifdef FIXED_HEAP
+               if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
+#else
+               mword objsize;
+
+               objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)obj));
+
+               if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
+#endif
+               {
+                       MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
+                       MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
+               } else {
+                       if (sgen_los_object_is_pinned (obj))
+                               return;
+                       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_los_pin_object (obj);
+                       /* FIXME: only enqueue if object has references */
+                       GRAY_OBJECT_ENQUEUE (queue, obj);
                }
        }
 }
@@ -1271,8 +1326,8 @@ major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
 
        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;
@@ -1323,7 +1378,7 @@ major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
                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 {
@@ -1382,20 +1437,21 @@ major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
                                MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
                        }
                } else {
-                       if (SGEN_OBJECT_IS_PINNED (obj))
+                       if (sgen_los_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);
+                       sgen_los_pin_object (obj);
                        /* FIXME: only enqueue if object has references */
                        GRAY_OBJECT_ENQUEUE (queue, obj);
                }
        }
 }
 #endif
+#endif
 
 #include "sgen-major-scan-object.h"
 
@@ -1412,7 +1468,7 @@ mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
 
        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);
@@ -1420,34 +1476,18 @@ mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
        }
 }
 
-// FIXME: Consistency check, heap traversal
-/*
- * sweep_block:
- *
- *   Traverse BLOCK, freeing and zeroing unused objects.
- */
-static void
-sweep_block (MSBlockInfo *block)
+static inline void
+sweep_block_for_size (MSBlockInfo *block, int count, int obj_size)
 {
-       int count;
        int obj_index;
-       int obj_size_index;
-
-       if (block->swept)
-               return;
-
-       obj_size_index = block->obj_size_index;
-
-       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);
+               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)) {
-                       DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
+                       SGEN_ASSERT (9, MS_OBJ_ALLOCED (obj, block), "object %p not allocated", obj);
                } else {
                        /* an unmarked object */
                        if (MS_OBJ_ALLOCED (obj, block)) {
@@ -1457,14 +1497,43 @@ sweep_block (MSBlockInfo *block)
                                 * overhead.  Maybe memset
                                 * will also benefit?
                                 */
-                               binary_protocol_empty (obj, block->obj_size);
-                               MONO_GC_MAJOR_SWEPT ((mword)obj, block->obj_size);
-                               memset (obj, 0, block->obj_size);
+                               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);
@@ -1541,14 +1610,19 @@ ms_sweep (void)
 
                count = MS_BLOCK_FREE / block->obj_size;
 
+#ifdef SGEN_CONCURRENT_MARK
+               if (block->cardtable_mod_union) {
+                       sgen_free_internal_dynamic (block->cardtable_mod_union, CARDS_PER_BLOCK, INTERNAL_MEM_CARDTABLE_MOD_UNION);
+                       block->cardtable_mod_union = NULL;
+               }
+#endif
+
                /* 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 (!has_pinned)
-                               slots_used [obj_size_index] += nused;
                }
                if (nused < count)
                        have_free = TRUE;
@@ -1559,6 +1633,7 @@ ms_sweep (void)
                if (have_live) {
                        if (!has_pinned) {
                                ++num_blocks [obj_size_index];
+                               slots_used [obj_size_index] += nused;
                                slots_available [obj_size_index] += count;
                        }
 
@@ -1758,6 +1833,8 @@ major_start_major_collection (void)
        if (lazy_sweep) {
                MSBlockInfo **iter;
 
+               MONO_GC_SWEEP_BEGIN (GENERATION_OLD, TRUE);
+
                iter = &all_blocks;
                while (*iter) {
                        MSBlockInfo *block = *iter;
@@ -1766,6 +1843,8 @@ major_start_major_collection (void)
 
                        iter = &block->next;
                }
+
+               MONO_GC_SWEEP_END (GENERATION_OLD, TRUE);
        }
 }
 
@@ -1978,7 +2057,7 @@ skip_card (guint8 *card_data, guint8 *card_data_end)
 #define MS_OBJ_ALLOCED_FAST(o,b)               (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
 
 static void
-major_scan_card_table (SgenGrayQueue *queue)
+major_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
 {
        MSBlockInfo *block;
        ScanObjectFunc scan_func = sgen_get_current_object_ops ()->scan_object;
@@ -2000,24 +2079,49 @@ major_scan_card_table (SgenGrayQueue *queue)
 #endif
                        char *obj, *end, *base;
 
+                       if (mod_union) {
+#ifdef SGEN_CONCURRENT_MARK
+                               cards = block->cardtable_mod_union;
+                               g_assert (cards);
+#else
+                               g_assert_not_reached ();
+#endif
+                       } else {
                        /*We can avoid the extra copy since the remark cardtable was cleaned before */
 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
-                       cards = sgen_card_table_get_card_scan_address ((mword)block_start);
+                               cards = sgen_card_table_get_card_scan_address ((mword)block_start);
 #else
-                       cards = cards_data;
-                       if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
-                               continue;
+                               cards = cards_data;
+                               if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
+                                       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);
 
                        while (obj < end) {
-                               if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
-                                       int card_offset = (obj - base) >> CARD_BITS;
-                                       sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
+                               int card_offset;
+
+                               if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
+                                       goto next_large;
+
+                               if (mod_union) {
+                                       /* FIXME: do this more efficiently */
+                                       int w, b;
+                                       MS_CALC_MARK_BIT (w, b, obj);
+                                       if (!MS_MARK_BIT (block, w, b))
+                                               goto next_large;
                                }
+
+                               card_offset = (obj - base) >> CARD_BITS;
+                               sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
+
+                       next_large:
                                obj += block_obj_size;
                        }
                } else {
@@ -2031,7 +2135,23 @@ major_scan_card_table (SgenGrayQueue *queue)
                         * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
                         * sizes, they won't overflow the cardtable overlap modulus.
                         */
-                       card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
+                       if (mod_union) {
+#ifdef SGEN_CONCURRENT_MARK
+                               card_data = card_base = block->cardtable_mod_union;
+                               /*
+                                * This happens when the nursery
+                                * collection that precedes finishing
+                                * the concurrent collection allocates
+                                * new major blocks.
+                                */
+                               if (!card_data)
+                                       continue;
+#else
+                               g_assert_not_reached ();
+#endif
+                       } else {
+                               card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
+                       }
                        card_data_end = card_data + CARDS_PER_BLOCK;
 
                        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)) {
@@ -2046,6 +2166,9 @@ major_scan_card_table (SgenGrayQueue *queue)
                                if (!*card_data)
                                        continue;
 
+                               if (!block->swept)
+                                       sweep_block (block);
+
                                HEAVY_STAT (++marked_cards);
 
                                sgen_card_table_prepare_card_for_scanning (card_data);
@@ -2057,10 +2180,20 @@ major_scan_card_table (SgenGrayQueue *queue)
 
                                obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
                                while (obj < end) {
-                                       if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
-                                               HEAVY_STAT (++scanned_objects);
-                                               scan_func (obj, queue);
+                                       if (!MS_OBJ_ALLOCED_FAST (obj, block_start))
+                                               goto next_small;
+
+                                       if (mod_union) {
+                                               /* FIXME: do this more efficiently */
+                                               int w, b;
+                                               MS_CALC_MARK_BIT (w, b, obj);
+                                               if (!MS_MARK_BIT (block, w, b))
+                                                       goto next_small;
                                        }
+
+                                       HEAVY_STAT (++scanned_objects);
+                                       scan_func (obj, queue);
+                               next_small:
                                        obj += block_obj_size;
                                }
                                HEAVY_STAT (if (*card_data) ++remarked_cards);
@@ -2068,6 +2201,34 @@ major_scan_card_table (SgenGrayQueue *queue)
                }
        } END_FOREACH_BLOCK;
 }
+
+#ifdef SGEN_CONCURRENT_MARK
+static void
+update_cardtable_mod_union (void)
+{
+       MSBlockInfo *block;
+
+       FOREACH_BLOCK (block) {
+               guint8 *cards;
+               gboolean init = FALSE;
+
+               if (!block->cardtable_mod_union) {
+                       block->cardtable_mod_union = sgen_alloc_internal_dynamic (CARDS_PER_BLOCK,
+                                       INTERNAL_MEM_CARDTABLE_MOD_UNION, TRUE);
+                       init = TRUE;
+               }
+
+               cards = sgen_card_table_get_card_scan_address ((mword)block->block);
+               if (init) {
+                       memcpy (block->cardtable_mod_union, cards, CARDS_PER_BLOCK);
+               } else {
+                       int i;
+                       for (i = 0; i < CARDS_PER_BLOCK; ++i)
+                               block->cardtable_mod_union [i] |= cards [i];
+               }
+       } END_FOREACH_BLOCK;
+}
+#endif
 #endif
 
 static gboolean
@@ -2133,7 +2294,7 @@ major_reset_worker_data (void *data)
 #undef pthread_create
 
 static void
-post_param_init (void)
+post_param_init (SgenMajorCollector *collector)
 {
        if (concurrent_sweep) {
                if (!mono_native_thread_create (&ms_sweep_thread, ms_sweep_thread_func, NULL)) {
@@ -2141,9 +2302,14 @@ post_param_init (void)
                        exit (1);
                }
        }
+
+       collector->sweeps_lazily = lazy_sweep;
 }
 
 void
+#ifdef SGEN_CONCURRENT_MARK
+sgen_marksweep_conc_init
+#else
 #ifdef SGEN_PARALLEL_MARK
 #ifdef FIXED_HEAP
 sgen_marksweep_fixed_par_init
@@ -2156,6 +2322,7 @@ sgen_marksweep_fixed_init
 #else
 sgen_marksweep_init
 #endif
+#endif
 #endif
        (SgenMajorCollector *collector)
 {
@@ -2219,6 +2386,11 @@ sgen_marksweep_init
        collector->reset_worker_data = major_reset_worker_data;
 #else
        collector->is_parallel = FALSE;
+#endif
+#ifdef SGEN_CONCURRENT_MARK
+       collector->is_concurrent = TRUE;
+#else
+       collector->is_concurrent = FALSE;
 #endif
        collector->supports_cardtable = TRUE;
 
@@ -2242,6 +2414,9 @@ sgen_marksweep_init
 #ifdef SGEN_HAVE_CARDTABLE
        collector->scan_card_table = major_scan_card_table;
        collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
+#ifdef SGEN_CONCURRENT_MARK
+       collector->update_cardtable_mod_union = update_cardtable_mod_union;
+#endif
 #endif
        collector->init_to_space = major_init_to_space;
        collector->sweep = major_sweep;
@@ -2266,6 +2441,9 @@ sgen_marksweep_init
 
        collector->major_ops.copy_or_mark_object = major_copy_or_mark_object;
        collector->major_ops.scan_object = major_scan_object;
+#ifdef SGEN_CONCURRENT_MARK
+       collector->major_ops.scan_vtype = major_scan_vtype;
+#endif
 
 #ifdef SGEN_HAVE_CARDTABLE
        /*cardtable requires major pages to be 8 cards aligned*/