[sgen] Generational mono g hashtable
[mono.git] / mono / sgen / sgen-cardtable.c
index b6e9479f1479e6a7f65a4acbd2433b470ed9dee1..cb6c2c52a5f25db85cb19445585f8b3f8ebc85de 100644 (file)
@@ -9,18 +9,7 @@
  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
  * 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.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include "config.h"
@@ -119,19 +108,13 @@ sgen_card_table_wbarrier_value_copy (gpointer dest, gpointer src, int count, siz
 {
        size_t size = count * element_size;
 
-#ifdef DISABLE_CRITICAL_REGION
-       LOCK_GC;
-#else
        TLAB_ACCESS_INIT;
        ENTER_CRITICAL_REGION;
-#endif
+
        mono_gc_memmove_atomic (dest, src, size);
        sgen_card_table_mark_range ((mword)dest, size);
-#ifdef DISABLE_CRITICAL_REGION
-       UNLOCK_GC;
-#else
+
        EXIT_CRITICAL_REGION;
-#endif
 }
 
 static void
@@ -139,20 +122,14 @@ sgen_card_table_wbarrier_object_copy (GCObject* obj, GCObject *src)
 {
        size_t size = sgen_client_par_object_get_size (SGEN_LOAD_VTABLE_UNCHECKED (obj), obj);
 
-#ifdef DISABLE_CRITICAL_REGION
-       LOCK_GC;
-#else
        TLAB_ACCESS_INIT;
        ENTER_CRITICAL_REGION;
-#endif
+
        mono_gc_memmove_aligned ((char*)obj + SGEN_CLIENT_OBJECT_HEADER_SIZE, (char*)src + SGEN_CLIENT_OBJECT_HEADER_SIZE,
                        size - SGEN_CLIENT_OBJECT_HEADER_SIZE);
        sgen_card_table_mark_range ((mword)obj, size);
-#ifdef DISABLE_CRITICAL_REGION
-       UNLOCK_GC;
-#else
+
        EXIT_CRITICAL_REGION;
-#endif 
 }
 
 static void
@@ -354,6 +331,29 @@ sgen_card_table_update_mod_union (guint8 *dest, char *obj, mword obj_size, size_
                *out_num_cards = num_cards;
 }
 
+/* Preclean cards and saves the cards that need to be scanned afterwards in cards_preclean */
+void
+sgen_card_table_preclean_mod_union (guint8 *cards, guint8 *cards_preclean, size_t num_cards)
+{
+       size_t i;
+
+       memcpy (cards_preclean, cards, num_cards);
+       for (i = 0; i < num_cards; i++) {
+               if (cards_preclean [i]) {
+                       cards [i] = 0;
+               }
+       }
+       /*
+        * When precleaning we need to make sure the card cleaning
+        * takes place before the object is scanned. If we don't
+        * do this we could finish scanning the object and, before
+        * the cleaning of the card takes place, another thread
+        * could dirty the object, mark the mod_union card only for
+        * us to clean it back, without scanning the object again.
+        */
+       mono_memory_barrier ();
+}
+
 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
 
 static void
@@ -410,8 +410,9 @@ static void
 sgen_card_table_clear_cards (void)
 {
        /*XXX we could do this in 2 ways. using mincore or iterating over all sections/los objects */
-       sgen_major_collector_iterate_live_block_ranges (clear_cards);
+       sgen_major_collector_iterate_block_ranges (clear_cards);
        sgen_los_iterate_live_block_ranges (clear_cards);
+       sgen_wbroots_iterate_live_block_ranges (clear_cards);
 }
 
 static void
@@ -431,21 +432,24 @@ sgen_card_table_scan_remsets (ScanCopyContext ctx)
 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
        /*FIXME we should have a bit on each block/los object telling if the object have marked cards.*/
        /*First we copy*/
-       sgen_major_collector_iterate_live_block_ranges (move_cards_to_shadow_table);
+       sgen_major_collector_iterate_block_ranges (move_cards_to_shadow_table);
        sgen_los_iterate_live_block_ranges (move_cards_to_shadow_table);
+       sgen_wbroots_iterate_live_block_ranges (move_cards_to_shadow_table);
 
        /*Then we clear*/
        sgen_card_table_clear_cards ();
 #endif
        SGEN_TV_GETTIME (atv);
-       sgen_get_major_collector ()->scan_card_table (FALSE, ctx);
+       sgen_get_major_collector ()->scan_card_table (CARDTABLE_SCAN_GLOBAL, ctx, 0, 1);
        SGEN_TV_GETTIME (btv);
        last_major_scan_time = SGEN_TV_ELAPSED (atv, btv); 
        major_card_scan_time += last_major_scan_time;
-       sgen_los_scan_card_table (FALSE, ctx);
+       sgen_los_scan_card_table (CARDTABLE_SCAN_GLOBAL, ctx, 0, 1);
        SGEN_TV_GETTIME (atv);
        last_los_scan_time = SGEN_TV_ELAPSED (btv, atv);
        los_card_scan_time += last_los_scan_time;
+
+       sgen_wbroots_scan_card_table (ctx);
 }
 
 guint8*
@@ -488,12 +492,73 @@ sgen_card_table_dump_obj_card (GCObject *object, size_t size, void *dummy)
 }
 #endif
 
+/*
+ * Cardtable scanning
+ */
+
+#define MWORD_MASK (sizeof (mword) - 1)
+
+static inline int
+find_card_offset (mword card)
+{
+/*XXX Use assembly as this generates some pretty bad code */
+#if defined(__i386__) && defined(__GNUC__)
+       return  (__builtin_ffs (card) - 1) / 8;
+#elif defined(__x86_64__) && defined(__GNUC__)
+       return (__builtin_ffsll (card) - 1) / 8;
+#elif defined(__s390x__)
+       return (__builtin_ffsll (GUINT64_TO_LE(card)) - 1) / 8;
+#else
+       int i;
+       guint8 *ptr = (guint8 *) &card;
+       for (i = 0; i < sizeof (mword); ++i) {
+               if (ptr[i])
+                       return i;
+       }
+       return 0;
+#endif
+}
+
+guint8*
+sgen_find_next_card (guint8 *card_data, guint8 *end)
+{
+       mword *cards, *cards_end;
+       mword card;
+
+       while ((((mword)card_data) & MWORD_MASK) && card_data < end) {
+               if (*card_data)
+                       return card_data;
+               ++card_data;
+       }
+
+       if (card_data == end)
+               return end;
+
+       cards = (mword*)card_data;
+       cards_end = (mword*)((mword)end & ~MWORD_MASK);
+       while (cards < cards_end) {
+               card = *cards;
+               if (card)
+                       return (guint8*)cards + find_card_offset (card);
+               ++cards;
+       }
+
+       card_data = (guint8*)cards_end;
+       while (card_data < end) {
+               if (*card_data)
+                       return card_data;
+               ++card_data;
+       }
+
+       return end;
+}
+
 void
-sgen_cardtable_scan_object (GCObject *obj, mword block_obj_size, guint8 *cards, gboolean mod_union, ScanCopyContext ctx)
+sgen_cardtable_scan_object (GCObject *obj, mword block_obj_size, guint8 *cards, ScanCopyContext ctx)
 {
        HEAVY_STAT (++large_objects);
 
-       if (sgen_client_cardtable_scan_object (obj, block_obj_size, cards, mod_union, ctx))
+       if (sgen_client_cardtable_scan_object (obj, cards, ctx))
                return;
 
        HEAVY_STAT (++bloby_objects);
@@ -573,10 +638,10 @@ sgen_card_tables_collect_stats (gboolean begin)
 void
 sgen_card_table_init (SgenRememberedSet *remset)
 {
-       sgen_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "card table");
+       sgen_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "card table", MONO_MEM_ACCOUNT_SGEN_CARD_TABLE);
 
 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
-       sgen_shadow_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "shadow card table");
+       sgen_shadow_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "shadow card table", MONO_MEM_ACCOUNT_SGEN_SHADOW_CARD_TABLE);
 #endif
 
 #ifdef HEAVY_STATISTICS