2 * sgen-cardtable.c: Card table implementation for sgen
5 * Rodrigo Kumpera (rkumpera@novell.com)
7 * SGen is licensed under the terms of the MIT X11 license
9 * Copyright 2001-2003 Ximian, Inc
10 * Copyright 2003-2010 Novell, Inc.
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files (the
14 * "Software"), to deal in the Software without restriction, including
15 * without limitation the rights to use, copy, modify, merge, publish,
16 * distribute, sublicense, and/or sell copies of the Software, and to
17 * permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #ifdef SGEN_HAVE_CARDTABLE
34 //#define CARDTABLE_STATS
38 #include <sys/types.h>
40 guint8 *sgen_cardtable;
42 /*WARNING: This function returns the number of cards regardless of overflow in case of overlapping cards.*/
44 cards_in_range (mword address, mword size)
46 mword end = address + MAX (1, size) - 1;
47 return (end >> CARD_BITS) - (address >> CARD_BITS) + 1;
50 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
52 guint8 *sgen_shadow_cardtable;
54 #define SGEN_SHADOW_CARDTABLE_END (sgen_shadow_cardtable + CARD_COUNT_IN_BYTES)
55 #define SGEN_CARDTABLE_END (sgen_cardtable + CARD_COUNT_IN_BYTES)
58 sgen_card_table_region_begin_scanning (mword start, mword end)
60 /*XXX this can be improved to work on words and have a single loop induction var */
61 while (start <= end) {
62 if (sgen_card_table_card_begin_scanning (start))
64 start += CARD_SIZE_IN_BYTES;
72 sgen_card_table_region_begin_scanning (mword start, mword size)
75 guint8 *card = sgen_card_table_get_card_address (start);
76 guint8 *end = card + cards_in_range (start, size);
78 /*XXX this can be improved to work on words and have a branchless body */
86 memset (sgen_card_table_get_card_address (start), 0, size >> CARD_BITS);
93 /*FIXME this assumes that major blocks are multiple of 4K which is pretty reasonable */
95 sgen_card_table_get_card_data (guint8 *data_dest, mword address, mword cards)
97 mword *start = (mword*)sgen_card_table_get_card_scan_address (address);
98 mword *dest = (mword*)data_dest;
99 mword *end = (mword*)(data_dest + cards);
102 for (; dest < end; ++dest, ++start) {
107 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
116 sgen_card_table_address_is_marked (mword address)
118 return *sgen_card_table_get_card_address (address) != 0;
122 sgen_card_table_mark_address (mword address)
124 *sgen_card_table_get_card_address (address) = 1;
128 sgen_card_table_align_pointer (void *ptr)
130 return (void*)((mword)ptr & ~(CARD_SIZE_IN_BYTES - 1));
134 sgen_card_table_mark_range (mword address, mword size)
136 mword end = address + size;
138 sgen_card_table_mark_address (address);
139 address += CARD_SIZE_IN_BYTES;
140 } while (address < end);
144 sgen_card_table_is_range_marked (guint8 *cards, mword address, mword size)
146 guint8 *end = cards + cards_in_range (address, size);
148 /*This is safe since this function is only called by code that only passes continuous card blocks*/
149 while (cards != end) {
158 card_table_init (void)
160 sgen_cardtable = mono_sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, TRUE);
162 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
163 sgen_shadow_cardtable = mono_sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, TRUE);
168 void los_scan_card_table (GrayQueue *queue);
169 void los_iterate_live_block_ranges (sgen_cardtable_block_callback callback);
173 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
176 move_cards_to_shadow_table (mword start, mword size)
178 guint8 *from = sgen_card_table_get_card_address (start);
179 guint8 *to = sgen_card_table_get_shadow_card_address (start);
180 size_t bytes = cards_in_range (start, size);
182 if (to + bytes > SGEN_SHADOW_CARDTABLE_END) {
183 size_t first_chunk = SGEN_SHADOW_CARDTABLE_END - to;
184 size_t second_chunk = MIN (CARD_COUNT_IN_BYTES, bytes) - first_chunk;
186 memcpy (to, from, first_chunk);
187 memcpy (sgen_shadow_cardtable, sgen_cardtable, second_chunk);
189 memcpy (to, from, bytes);
194 clear_cards (mword start, mword size)
196 guint8 *addr = sgen_card_table_get_card_address (start);
197 size_t bytes = cards_in_range (start, size);
199 if (addr + bytes > SGEN_CARDTABLE_END) {
200 size_t first_chunk = SGEN_CARDTABLE_END - addr;
202 memset (addr, 0, first_chunk);
203 memset (sgen_cardtable, 0, bytes - first_chunk);
205 memset (addr, 0, bytes);
213 clear_cards (mword start, mword size)
215 memset (sgen_card_table_get_card_address (start), 0, cards_in_range (start, size));
222 card_table_clear (void)
224 /*XXX we could do this in 2 ways. using mincore or iterating over all sections/los objects */
226 major_collector.iterate_live_block_ranges (clear_cards);
227 los_iterate_live_block_ranges (clear_cards);
231 scan_from_card_tables (void *start_nursery, void *end_nursery, GrayQueue *queue)
234 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
235 /*FIXME we should have a bit on each block/los object telling if the object have marked cards.*/
237 major_collector.iterate_live_block_ranges (move_cards_to_shadow_table);
238 los_iterate_live_block_ranges (move_cards_to_shadow_table);
243 major_collector.scan_card_table (queue);
244 los_scan_card_table (queue);
249 mono_gc_get_card_table (int *shift_bits, gpointer *mask)
254 g_assert (sgen_cardtable);
255 *shift_bits = CARD_BITS;
256 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
257 *mask = (gpointer)CARD_MASK;
262 return sgen_cardtable;
267 collect_faulted_cards (void)
269 #define CARD_PAGES (CARD_COUNT_IN_BYTES / 4096)
271 unsigned char faulted [CARD_PAGES] = { 0 };
272 mincore (sgen_cardtable, CARD_COUNT_IN_BYTES, faulted);
274 for (i = 0; i < CARD_PAGES; ++i) {
279 printf ("TOTAL card pages %d faulted %d\n", CARD_PAGES, count);
284 sgen_cardtable_scan_object (char *obj, mword obj_size, guint8 *cards, SgenGrayQueue *queue)
286 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (obj);
287 MonoClass *klass = vt->klass;
289 if (!klass->has_references)
293 guint8 *card_data, *card_base;
294 guint8 *card_data_end;
295 char *obj_start = sgen_card_table_align_pointer (obj);
296 char *obj_end = obj + obj_size;
300 MonoArray *arr = (MonoArray*)obj;
301 mword desc = (mword)klass->element_class->gc_descr;
302 int elem_size = mono_array_element_size (klass);
304 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
305 guint8 *overflow_scan_end = NULL;
311 card_data = sgen_card_table_get_card_scan_address ((mword)obj);
313 card_base = card_data;
314 card_count = cards_in_range ((mword)obj, obj_size);
315 card_data_end = card_data + card_count;
318 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
319 /*Check for overflow and if so, setup to scan in two steps*/
320 if (!cards && card_data_end >= SGEN_SHADOW_CARDTABLE_END) {
321 overflow_scan_end = sgen_shadow_cardtable + (card_data_end - SGEN_SHADOW_CARDTABLE_END);
322 card_data_end = SGEN_SHADOW_CARDTABLE_END;
327 /*FIXME use card skipping code*/
328 for (; card_data < card_data_end; ++card_data) {
330 int idx = (card_data - card_base) + extra_idx;
331 char *start = (char*)(obj_start + idx * CARD_SIZE_IN_BYTES);
332 char *card_end = start + CARD_SIZE_IN_BYTES;
339 sgen_card_table_prepare_card_for_scanning (card_data);
341 card_end = MIN (card_end, obj_end);
343 if (start <= (char*)arr->vector)
346 index = ARRAY_OBJ_INDEX (start, obj, elem_size);
348 elem = (char*)mono_array_addr_with_size ((MonoArray*)obj, elem_size, index);
349 if (klass->element_class->valuetype) {
350 for (; elem < card_end; elem += elem_size)
351 major_collector.minor_scan_vtype (elem, desc, nursery_start, nursery_next, queue);
353 for (; elem < card_end; elem += SIZEOF_VOID_P) {
354 gpointer new, old = *(gpointer*)elem;
355 /*XXX it might be faster to do a nursery check here instead as it avoid a call*/
357 major_collector.copy_object ((void**)elem, queue);
358 new = *(gpointer*)elem;
359 if (G_UNLIKELY (ptr_in_nursery (new)))
360 mono_sgen_add_to_global_remset (elem);
366 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
367 if (overflow_scan_end) {
368 extra_idx = card_data - card_base;
369 card_base = card_data = sgen_shadow_cardtable;
370 card_data_end = overflow_scan_end;
371 overflow_scan_end = NULL;
378 if (sgen_card_table_is_range_marked (cards, (mword)obj, obj_size))
379 major_collector.minor_scan_object (obj, queue);
380 } else if (sgen_card_table_region_begin_scanning ((mword)obj, obj_size)) {
381 major_collector.minor_scan_object (obj, queue);
386 #ifdef CARDTABLE_STATS
388 static int total_cards, marked_cards, remarked_cards;
391 count_marked_cards (mword start, mword size)
393 mword end = start + size;
394 while (start <= end) {
396 if (sgen_card_table_address_is_marked (start))
398 start += CARD_SIZE_IN_BYTES;
403 count_remarked_cards (mword start, mword size)
405 mword end = start + size;
406 while (start <= end) {
407 if (sgen_card_table_address_is_marked (start))
409 start += CARD_SIZE_IN_BYTES;
416 card_tables_collect_stats (gboolean begin)
418 #ifdef CARDTABLE_STATS
420 total_cards = marked_cards = remarked_cards = 0;
421 major_collector.iterate_live_block_ranges (count_marked_cards);
422 los_iterate_live_block_ranges (count_marked_cards);
424 major_collector.iterate_live_block_ranges (count_marked_cards);
425 los_iterate_live_block_ranges (count_remarked_cards);
426 printf ("cards total %d marked %d remarked %d\n", total_cards, marked_cards, remarked_cards);
434 sgen_card_table_mark_address (mword address)
436 g_assert_not_reached ();
440 sgen_card_table_mark_range (mword address, mword size)
442 g_assert_not_reached ();
445 #define sgen_card_table_address_is_marked(p) FALSE
446 #define scan_from_card_tables(start,end,queue)
447 #define card_table_clear()
448 #define card_table_init()
449 #define card_tables_collect_stats(begin)
452 mono_gc_get_card_table (int *shift_bits, gpointer *mask)