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 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
348 move_cards_to_shadow_table (mword start, mword size)
350 guint8 *from = sgen_card_table_get_card_address (start);
351 guint8 *to = sgen_card_table_get_shadow_card_address (start);
352 size_t bytes = sgen_card_table_number_of_cards_in_range (start, size);
354 if (bytes >= CARD_COUNT_IN_BYTES) {
355 memcpy (sgen_shadow_cardtable, sgen_cardtable, CARD_COUNT_IN_BYTES);
356 } else if (to + bytes > SGEN_SHADOW_CARDTABLE_END) {
357 size_t first_chunk = SGEN_SHADOW_CARDTABLE_END - to;
358 size_t second_chunk = MIN (CARD_COUNT_IN_BYTES, bytes) - first_chunk;
360 memcpy (to, from, first_chunk);
361 memcpy (sgen_shadow_cardtable, sgen_cardtable, second_chunk);
363 memcpy (to, from, bytes);
368 clear_cards (mword start, mword size)
370 guint8 *addr = sgen_card_table_get_card_address (start);
371 size_t bytes = sgen_card_table_number_of_cards_in_range (start, size);
373 if (bytes >= CARD_COUNT_IN_BYTES) {
374 memset (sgen_cardtable, 0, CARD_COUNT_IN_BYTES);
375 } else if (addr + bytes > SGEN_CARDTABLE_END) {
376 size_t first_chunk = SGEN_CARDTABLE_END - addr;
378 memset (addr, 0, first_chunk);
379 memset (sgen_cardtable, 0, bytes - first_chunk);
381 memset (addr, 0, bytes);
389 clear_cards (mword start, mword size)
391 memset (sgen_card_table_get_card_address (start), 0, sgen_card_table_number_of_cards_in_range (start, size));
398 sgen_card_table_clear_cards (void)
400 /*XXX we could do this in 2 ways. using mincore or iterating over all sections/los objects */
401 sgen_major_collector_iterate_live_block_ranges (clear_cards);
402 sgen_los_iterate_live_block_ranges (clear_cards);
406 sgen_card_table_finish_minor_collection (void)
408 sgen_card_tables_collect_stats (FALSE);
412 sgen_card_table_scan_remsets (ScanCopyContext ctx)
414 SGEN_TV_DECLARE (atv);
415 SGEN_TV_DECLARE (btv);
417 sgen_card_tables_collect_stats (TRUE);
419 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
420 /*FIXME we should have a bit on each block/los object telling if the object have marked cards.*/
422 sgen_major_collector_iterate_live_block_ranges (move_cards_to_shadow_table);
423 sgen_los_iterate_live_block_ranges (move_cards_to_shadow_table);
426 sgen_card_table_clear_cards ();
428 SGEN_TV_GETTIME (atv);
429 sgen_get_major_collector ()->scan_card_table (FALSE, ctx);
430 SGEN_TV_GETTIME (btv);
431 last_major_scan_time = SGEN_TV_ELAPSED (atv, btv);
432 major_card_scan_time += last_major_scan_time;
433 sgen_los_scan_card_table (FALSE, ctx);
434 SGEN_TV_GETTIME (atv);
435 last_los_scan_time = SGEN_TV_ELAPSED (btv, atv);
436 los_card_scan_time += last_los_scan_time;
440 sgen_get_card_table_configuration (int *shift_bits, gpointer *mask)
442 #ifndef MANAGED_WBARRIER
448 *shift_bits = CARD_BITS;
449 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
450 *mask = (gpointer)CARD_MASK;
455 return sgen_cardtable;
461 sgen_card_table_dump_obj_card (GCObject *object, size_t size, void *dummy)
463 guint8 *start = sgen_card_table_get_card_scan_address (object);
464 guint8 *end = start + sgen_card_table_number_of_cards_in_range (object, size);
466 printf ("--obj %p %d cards [%p %p]--", object, size, start, end);
467 for (; start < end; ++start) {
469 printf ("\n\t[%p] ", start);
470 printf ("%x ", *start);
480 sgen_cardtable_scan_object (GCObject *obj, mword block_obj_size, guint8 *cards, gboolean mod_union, ScanCopyContext ctx)
482 HEAVY_STAT (++large_objects);
484 if (sgen_client_cardtable_scan_object (obj, block_obj_size, cards, mod_union, ctx))
487 HEAVY_STAT (++bloby_objects);
489 if (sgen_card_table_is_range_marked (cards, (mword)obj, block_obj_size))
490 ctx.ops->scan_object (obj, sgen_obj_get_descriptor (obj), ctx.queue);
491 } else if (sgen_card_table_region_begin_scanning ((mword)obj, block_obj_size)) {
492 ctx.ops->scan_object (obj, sgen_obj_get_descriptor (obj), ctx.queue);
495 binary_protocol_card_scan (obj, sgen_safe_object_get_size (obj));
498 #ifdef CARDTABLE_STATS
501 int total, marked, remarked, gc_marked;
504 static card_stats major_stats, los_stats;
505 static card_stats *cur_stats;
508 count_marked_cards (mword start, mword size)
510 mword end = start + size;
511 while (start <= end) {
512 guint8 card = *sgen_card_table_get_card_address (start);
517 ++cur_stats->gc_marked;
518 start += CARD_SIZE_IN_BYTES;
523 count_remarked_cards (mword start, mword size)
525 mword end = start + size;
526 while (start <= end) {
527 if (sgen_card_table_address_is_marked (start)) {
528 ++cur_stats->remarked;
529 *sgen_card_table_get_card_address (start) = 2;
531 start += CARD_SIZE_IN_BYTES;
538 sgen_card_tables_collect_stats (gboolean begin)
540 #ifdef CARDTABLE_STATS
542 memset (&major_stats, 0, sizeof (card_stats));
543 memset (&los_stats, 0, sizeof (card_stats));
544 cur_stats = &major_stats;
545 sgen_major_collector_iterate_live_block_ranges (count_marked_cards);
546 cur_stats = &los_stats;
547 sgen_los_iterate_live_block_ranges (count_marked_cards);
549 cur_stats = &major_stats;
550 sgen_major_collector_iterate_live_block_ranges (count_remarked_cards);
551 cur_stats = &los_stats;
552 sgen_los_iterate_live_block_ranges (count_remarked_cards);
553 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",
554 major_stats.total, major_stats.marked, major_stats.gc_marked, major_stats.remarked,
555 los_stats.total, los_stats.marked, los_stats.gc_marked, los_stats.remarked,
556 last_major_scan_time / 10000.0f, last_los_scan_time / 10000.0f);
562 sgen_card_table_init (SgenRememberedSet *remset)
564 sgen_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "card table");
566 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
567 sgen_shadow_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "shadow card table");
570 #ifdef HEAVY_STATISTICS
571 mono_counters_register ("marked cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &marked_cards);
572 mono_counters_register ("scanned cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &scanned_cards);
573 mono_counters_register ("remarked cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &remarked_cards);
575 mono_counters_register ("cardtable scanned objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &scanned_objects);
576 mono_counters_register ("cardtable large objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &large_objects);
577 mono_counters_register ("cardtable bloby objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &bloby_objects);
579 mono_counters_register ("cardtable major scan time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &major_card_scan_time);
580 mono_counters_register ("cardtable los scan time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &los_card_scan_time);
583 remset->wbarrier_set_field = sgen_card_table_wbarrier_set_field;
584 remset->wbarrier_arrayref_copy = sgen_card_table_wbarrier_arrayref_copy;
585 remset->wbarrier_value_copy = sgen_card_table_wbarrier_value_copy;
586 remset->wbarrier_object_copy = sgen_card_table_wbarrier_object_copy;
587 remset->wbarrier_generic_nostore = sgen_card_table_wbarrier_generic_nostore;
588 remset->record_pointer = sgen_card_table_record_pointer;
590 remset->scan_remsets = sgen_card_table_scan_remsets;
592 remset->finish_minor_collection = sgen_card_table_finish_minor_collection;
593 remset->clear_cards = sgen_card_table_clear_cards;
595 remset->find_address = sgen_card_table_find_address;
596 remset->find_address_with_cards = sgen_card_table_find_address_with_cards;
598 need_mod_union = sgen_get_major_collector ()->is_concurrent;
601 #endif /*HAVE_SGEN_GC*/