2 * sgen-cardtable.c: Card table implementation for sgen
5 * Rodrigo Kumpera (rkumpera@novell.com)
7 * Copyright 2001-2003 Ximian, Inc
8 * Copyright 2003-2010 Novell, Inc.
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10 * Copyright (C) 2012 Xamarin Inc
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License 2.0 as published by the Free Software Foundation;
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License 2.0 along with this library; if not, write to the Free
23 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "mono/sgen/sgen-gc.h"
32 #include "mono/sgen/sgen-cardtable.h"
33 #include "mono/sgen/sgen-memory-governor.h"
34 #include "mono/sgen/sgen-protocol.h"
35 #include "mono/sgen/sgen-layout-stats.h"
36 #include "mono/sgen/sgen-client.h"
37 #include "mono/sgen/gc-internal-agnostic.h"
38 #include "mono/utils/mono-memory-model.h"
40 //#define CARDTABLE_STATS
45 #ifdef HAVE_SYS_MMAN_H
48 #include <sys/types.h>
50 guint8 *sgen_cardtable;
52 static gboolean need_mod_union;
54 #ifdef HEAVY_STATISTICS
56 guint64 scanned_cards;
57 guint64 scanned_objects;
58 guint64 remarked_cards;
59 static guint64 large_objects;
60 static guint64 bloby_objects;
62 static guint64 major_card_scan_time;
63 static guint64 los_card_scan_time;
65 static guint64 last_major_scan_time;
66 static guint64 last_los_scan_time;
68 static void sgen_card_tables_collect_stats (gboolean begin);
71 sgen_card_table_number_of_cards_in_range (mword address, mword size)
73 mword end = address + MAX (1, size) - 1;
74 return (end >> CARD_BITS) - (address >> CARD_BITS) + 1;
78 sgen_card_table_wbarrier_set_field (GCObject *obj, gpointer field_ptr, GCObject* value)
80 *(void**)field_ptr = value;
81 if (need_mod_union || sgen_ptr_in_nursery (value))
82 sgen_card_table_mark_address ((mword)field_ptr);
83 sgen_dummy_use (value);
87 sgen_card_table_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
89 gpointer *dest = (gpointer *)dest_ptr;
90 gpointer *src = (gpointer *)src_ptr;
92 /*overlapping that required backward copying*/
93 if (src < dest && (src + count) > dest) {
94 gpointer *start = dest;
98 for (; dest >= start; --src, --dest) {
99 gpointer value = *src;
100 SGEN_UPDATE_REFERENCE_ALLOW_NULL (dest, value);
101 if (need_mod_union || sgen_ptr_in_nursery (value))
102 sgen_card_table_mark_address ((mword)dest);
103 sgen_dummy_use (value);
106 gpointer *end = dest + count;
107 for (; dest < end; ++src, ++dest) {
108 gpointer value = *src;
109 SGEN_UPDATE_REFERENCE_ALLOW_NULL (dest, value);
110 if (need_mod_union || sgen_ptr_in_nursery (value))
111 sgen_card_table_mark_address ((mword)dest);
112 sgen_dummy_use (value);
118 sgen_card_table_wbarrier_value_copy (gpointer dest, gpointer src, int count, size_t element_size)
120 size_t size = count * element_size;
123 ENTER_CRITICAL_REGION;
125 mono_gc_memmove_atomic (dest, src, size);
126 sgen_card_table_mark_range ((mword)dest, size);
128 EXIT_CRITICAL_REGION;
132 sgen_card_table_wbarrier_object_copy (GCObject* obj, GCObject *src)
134 size_t size = sgen_client_par_object_get_size (SGEN_LOAD_VTABLE_UNCHECKED (obj), obj);
137 ENTER_CRITICAL_REGION;
139 mono_gc_memmove_aligned ((char*)obj + SGEN_CLIENT_OBJECT_HEADER_SIZE, (char*)src + SGEN_CLIENT_OBJECT_HEADER_SIZE,
140 size - SGEN_CLIENT_OBJECT_HEADER_SIZE);
141 sgen_card_table_mark_range ((mword)obj, size);
143 EXIT_CRITICAL_REGION;
147 sgen_card_table_wbarrier_generic_nostore (gpointer ptr)
149 sgen_card_table_mark_address ((mword)ptr);
152 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
154 guint8 *sgen_shadow_cardtable;
156 #define SGEN_CARDTABLE_END (sgen_cardtable + CARD_COUNT_IN_BYTES)
159 sgen_card_table_region_begin_scanning (mword start, mword size)
161 mword end = start + size;
162 /*XXX this can be improved to work on words and have a single loop induction var */
163 while (start < end) {
164 if (sgen_card_table_card_begin_scanning (start))
166 start += CARD_SIZE_IN_BYTES;
174 sgen_card_table_region_begin_scanning (mword start, mword size)
176 gboolean res = FALSE;
177 guint8 *card = sgen_card_table_get_card_address (start);
178 guint8 *end = card + sgen_card_table_number_of_cards_in_range (start, size);
180 /*XXX this can be improved to work on words and have a branchless body */
181 while (card != end) {
188 memset (sgen_card_table_get_card_address (start), 0, size >> CARD_BITS);
195 /*FIXME this assumes that major blocks are multiple of 4K which is pretty reasonable */
197 sgen_card_table_get_card_data (guint8 *data_dest, mword address, mword cards)
199 mword *start = (mword*)sgen_card_table_get_card_scan_address (address);
200 mword *dest = (mword*)data_dest;
201 mword *end = (mword*)(data_dest + cards);
204 for (; dest < end; ++dest, ++start) {
209 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
218 sgen_card_table_align_pointer (void *ptr)
220 return (void*)((mword)ptr & ~(CARD_SIZE_IN_BYTES - 1));
224 sgen_card_table_mark_range (mword address, mword size)
226 mword num_cards = sgen_card_table_number_of_cards_in_range (address, size);
227 guint8 *start = sgen_card_table_get_card_address (address);
229 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
231 * FIXME: There's a theoretical bug here, namely that the card table is allocated so
232 * far toward the end of the address space that start + num_cards overflows.
234 guint8 *end = start + num_cards;
235 SGEN_ASSERT (0, num_cards <= CARD_COUNT_IN_BYTES, "How did we get an object larger than the card table?");
236 if (end > SGEN_CARDTABLE_END) {
237 memset (start, 1, SGEN_CARDTABLE_END - start);
238 memset (sgen_cardtable, 1, end - SGEN_CARDTABLE_END);
243 memset (start, 1, num_cards);
247 sgen_card_table_is_range_marked (guint8 *cards, mword address, mword size)
249 guint8 *end = cards + sgen_card_table_number_of_cards_in_range (address, size);
251 /*This is safe since this function is only called by code that only passes continuous card blocks*/
252 while (cards != end) {
261 sgen_card_table_record_pointer (gpointer address)
263 *sgen_card_table_get_card_address ((mword)address) = 1;
267 sgen_card_table_find_address (char *addr)
269 return sgen_card_table_address_is_marked ((mword)addr);
273 sgen_card_table_find_address_with_cards (char *cards_start, guint8 *cards, char *addr)
275 cards_start = (char *)sgen_card_table_align_pointer (cards_start);
276 return cards [(addr - cards_start) >> CARD_BITS];
280 update_mod_union (guint8 *dest, guint8 *start_card, size_t num_cards)
283 /* Marking from another thread can happen while we mark here */
284 for (i = 0; i < num_cards; ++i) {
291 sgen_card_table_alloc_mod_union (char *obj, mword obj_size)
293 size_t num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj, obj_size);
294 guint8 *mod_union = (guint8 *)sgen_alloc_internal_dynamic (num_cards, INTERNAL_MEM_CARDTABLE_MOD_UNION, TRUE);
295 memset (mod_union, 0, num_cards);
300 sgen_card_table_free_mod_union (guint8 *mod_union, char *obj, mword obj_size)
302 size_t num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj, obj_size);
303 sgen_free_internal_dynamic (mod_union, num_cards, INTERNAL_MEM_CARDTABLE_MOD_UNION);
307 sgen_card_table_update_mod_union_from_cards (guint8 *dest, guint8 *start_card, size_t num_cards)
309 SGEN_ASSERT (0, dest, "Why don't we have a mod union?");
310 update_mod_union (dest, start_card, num_cards);
314 sgen_card_table_update_mod_union (guint8 *dest, char *obj, mword obj_size, size_t *out_num_cards)
316 guint8 *start_card = sgen_card_table_get_card_address ((mword)obj);
317 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
318 guint8 *end_card = sgen_card_table_get_card_address ((mword)obj + obj_size - 1) + 1;
322 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
325 rest = num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj, obj_size);
327 while (start_card + rest > SGEN_CARDTABLE_END) {
328 size_t count = SGEN_CARDTABLE_END - start_card;
329 sgen_card_table_update_mod_union_from_cards (dest, start_card, count);
332 start_card = sgen_cardtable;
336 num_cards = end_card - start_card;
339 sgen_card_table_update_mod_union_from_cards (dest, start_card, num_cards);
342 *out_num_cards = num_cards;
345 /* Preclean cards and saves the cards that need to be scanned afterwards in cards_preclean */
347 sgen_card_table_preclean_mod_union (guint8 *cards, guint8 *cards_preclean, size_t num_cards)
351 memcpy (cards_preclean, cards, num_cards);
352 for (i = 0; i < num_cards; i++) {
353 if (cards_preclean [i]) {
358 * When precleaning we need to make sure the card cleaning
359 * takes place before the object is scanned. If we don't
360 * do this we could finish scanning the object and, before
361 * the cleaning of the card takes place, another thread
362 * could dirty the object, mark the mod_union card only for
363 * us to clean it back, without scanning the object again.
365 mono_memory_barrier ();
368 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
371 move_cards_to_shadow_table (mword start, mword size)
373 guint8 *from = sgen_card_table_get_card_address (start);
374 guint8 *to = sgen_card_table_get_shadow_card_address (start);
375 size_t bytes = sgen_card_table_number_of_cards_in_range (start, size);
377 if (bytes >= CARD_COUNT_IN_BYTES) {
378 memcpy (sgen_shadow_cardtable, sgen_cardtable, CARD_COUNT_IN_BYTES);
379 } else if (to + bytes > SGEN_SHADOW_CARDTABLE_END) {
380 size_t first_chunk = SGEN_SHADOW_CARDTABLE_END - to;
381 size_t second_chunk = MIN (CARD_COUNT_IN_BYTES, bytes) - first_chunk;
383 memcpy (to, from, first_chunk);
384 memcpy (sgen_shadow_cardtable, sgen_cardtable, second_chunk);
386 memcpy (to, from, bytes);
391 clear_cards (mword start, mword size)
393 guint8 *addr = sgen_card_table_get_card_address (start);
394 size_t bytes = sgen_card_table_number_of_cards_in_range (start, size);
396 if (bytes >= CARD_COUNT_IN_BYTES) {
397 memset (sgen_cardtable, 0, CARD_COUNT_IN_BYTES);
398 } else if (addr + bytes > SGEN_CARDTABLE_END) {
399 size_t first_chunk = SGEN_CARDTABLE_END - addr;
401 memset (addr, 0, first_chunk);
402 memset (sgen_cardtable, 0, bytes - first_chunk);
404 memset (addr, 0, bytes);
412 clear_cards (mword start, mword size)
414 memset (sgen_card_table_get_card_address (start), 0, sgen_card_table_number_of_cards_in_range (start, size));
421 sgen_card_table_clear_cards (void)
423 /*XXX we could do this in 2 ways. using mincore or iterating over all sections/los objects */
424 sgen_major_collector_iterate_live_block_ranges (clear_cards);
425 sgen_los_iterate_live_block_ranges (clear_cards);
429 sgen_card_table_finish_minor_collection (void)
431 sgen_card_tables_collect_stats (FALSE);
435 sgen_card_table_scan_remsets (ScanCopyContext ctx)
437 SGEN_TV_DECLARE (atv);
438 SGEN_TV_DECLARE (btv);
440 sgen_card_tables_collect_stats (TRUE);
442 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
443 /*FIXME we should have a bit on each block/los object telling if the object have marked cards.*/
445 sgen_major_collector_iterate_live_block_ranges (move_cards_to_shadow_table);
446 sgen_los_iterate_live_block_ranges (move_cards_to_shadow_table);
449 sgen_card_table_clear_cards ();
451 SGEN_TV_GETTIME (atv);
452 sgen_get_major_collector ()->scan_card_table (CARDTABLE_SCAN_GLOBAL, ctx);
453 SGEN_TV_GETTIME (btv);
454 last_major_scan_time = SGEN_TV_ELAPSED (atv, btv);
455 major_card_scan_time += last_major_scan_time;
456 sgen_los_scan_card_table (CARDTABLE_SCAN_GLOBAL, ctx);
457 SGEN_TV_GETTIME (atv);
458 last_los_scan_time = SGEN_TV_ELAPSED (btv, atv);
459 los_card_scan_time += last_los_scan_time;
463 sgen_get_card_table_configuration (int *shift_bits, gpointer *mask)
465 #ifndef MANAGED_WBARRIER
471 *shift_bits = CARD_BITS;
472 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
473 *mask = (gpointer)CARD_MASK;
478 return sgen_cardtable;
484 sgen_card_table_dump_obj_card (GCObject *object, size_t size, void *dummy)
486 guint8 *start = sgen_card_table_get_card_scan_address (object);
487 guint8 *end = start + sgen_card_table_number_of_cards_in_range (object, size);
489 printf ("--obj %p %d cards [%p %p]--", object, size, start, end);
490 for (; start < end; ++start) {
492 printf ("\n\t[%p] ", start);
493 printf ("%x ", *start);
503 sgen_cardtable_scan_object (GCObject *obj, mword block_obj_size, guint8 *cards, ScanCopyContext ctx)
505 HEAVY_STAT (++large_objects);
507 if (sgen_client_cardtable_scan_object (obj, block_obj_size, cards, ctx))
510 HEAVY_STAT (++bloby_objects);
512 if (sgen_card_table_is_range_marked (cards, (mword)obj, block_obj_size))
513 ctx.ops->scan_object (obj, sgen_obj_get_descriptor (obj), ctx.queue);
514 } else if (sgen_card_table_region_begin_scanning ((mword)obj, block_obj_size)) {
515 ctx.ops->scan_object (obj, sgen_obj_get_descriptor (obj), ctx.queue);
518 binary_protocol_card_scan (obj, sgen_safe_object_get_size (obj));
521 #ifdef CARDTABLE_STATS
524 int total, marked, remarked, gc_marked;
527 static card_stats major_stats, los_stats;
528 static card_stats *cur_stats;
531 count_marked_cards (mword start, mword size)
533 mword end = start + size;
534 while (start <= end) {
535 guint8 card = *sgen_card_table_get_card_address (start);
540 ++cur_stats->gc_marked;
541 start += CARD_SIZE_IN_BYTES;
546 count_remarked_cards (mword start, mword size)
548 mword end = start + size;
549 while (start <= end) {
550 if (sgen_card_table_address_is_marked (start)) {
551 ++cur_stats->remarked;
552 *sgen_card_table_get_card_address (start) = 2;
554 start += CARD_SIZE_IN_BYTES;
561 sgen_card_tables_collect_stats (gboolean begin)
563 #ifdef CARDTABLE_STATS
565 memset (&major_stats, 0, sizeof (card_stats));
566 memset (&los_stats, 0, sizeof (card_stats));
567 cur_stats = &major_stats;
568 sgen_major_collector_iterate_live_block_ranges (count_marked_cards);
569 cur_stats = &los_stats;
570 sgen_los_iterate_live_block_ranges (count_marked_cards);
572 cur_stats = &major_stats;
573 sgen_major_collector_iterate_live_block_ranges (count_remarked_cards);
574 cur_stats = &los_stats;
575 sgen_los_iterate_live_block_ranges (count_remarked_cards);
576 printf ("cards major (t %d m %d g %d r %d) los (t %d m %d g %d r %d) major_scan %.2fms los_scan %.2fms\n",
577 major_stats.total, major_stats.marked, major_stats.gc_marked, major_stats.remarked,
578 los_stats.total, los_stats.marked, los_stats.gc_marked, los_stats.remarked,
579 last_major_scan_time / 10000.0f, last_los_scan_time / 10000.0f);
585 sgen_card_table_init (SgenRememberedSet *remset)
587 sgen_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "card table");
589 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
590 sgen_shadow_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "shadow card table");
593 #ifdef HEAVY_STATISTICS
594 mono_counters_register ("marked cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &marked_cards);
595 mono_counters_register ("scanned cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &scanned_cards);
596 mono_counters_register ("remarked cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &remarked_cards);
598 mono_counters_register ("cardtable scanned objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &scanned_objects);
599 mono_counters_register ("cardtable large objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &large_objects);
600 mono_counters_register ("cardtable bloby objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &bloby_objects);
602 mono_counters_register ("cardtable major scan time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &major_card_scan_time);
603 mono_counters_register ("cardtable los scan time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &los_card_scan_time);
606 remset->wbarrier_set_field = sgen_card_table_wbarrier_set_field;
607 remset->wbarrier_arrayref_copy = sgen_card_table_wbarrier_arrayref_copy;
608 remset->wbarrier_value_copy = sgen_card_table_wbarrier_value_copy;
609 remset->wbarrier_object_copy = sgen_card_table_wbarrier_object_copy;
610 remset->wbarrier_generic_nostore = sgen_card_table_wbarrier_generic_nostore;
611 remset->record_pointer = sgen_card_table_record_pointer;
613 remset->scan_remsets = sgen_card_table_scan_remsets;
615 remset->finish_minor_collection = sgen_card_table_finish_minor_collection;
616 remset->clear_cards = sgen_card_table_clear_cards;
618 remset->find_address = sgen_card_table_find_address;
619 remset->find_address_with_cards = sgen_card_table_find_address_with_cards;
621 need_mod_union = sgen_get_major_collector ()->is_concurrent;
624 #endif /*HAVE_SGEN_GC*/