16f4ea6a7d9ae8c7066d33d793305b6e334d32af
[mono.git] / mono / sgen / sgen-cardtable.c
1 /*
2  * sgen-cardtable.c: Card table implementation for sgen
3  *
4  * Author:
5  *      Rodrigo Kumpera (rkumpera@novell.com)
6  *
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
11  *
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;
15  *
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.
20  *
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.
24  */
25
26 #include "config.h"
27 #ifdef HAVE_SGEN_GC
28
29 #include <string.h>
30
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"
39
40 //#define CARDTABLE_STATS
41
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45 #ifdef HAVE_SYS_MMAN_H
46 #include <sys/mman.h>
47 #endif
48 #include <sys/types.h>
49
50 guint8 *sgen_cardtable;
51
52 static gboolean need_mod_union;
53
54 #ifdef HEAVY_STATISTICS
55 guint64 marked_cards;
56 guint64 scanned_cards;
57 guint64 scanned_objects;
58 guint64 remarked_cards;
59 static guint64 large_objects;
60 static guint64 bloby_objects;
61 #endif
62 static guint64 major_card_scan_time;
63 static guint64 los_card_scan_time;
64
65 static guint64 last_major_scan_time;
66 static guint64 last_los_scan_time;
67
68 static void sgen_card_tables_collect_stats (gboolean begin);
69
70 mword
71 sgen_card_table_number_of_cards_in_range (mword address, mword size)
72 {
73         mword end = address + MAX (1, size) - 1;
74         return (end >> CARD_BITS) - (address >> CARD_BITS) + 1;
75 }
76
77 static void
78 sgen_card_table_wbarrier_set_field (GCObject *obj, gpointer field_ptr, GCObject* value)
79 {
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);
84 }
85
86 static void
87 sgen_card_table_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
88 {
89         gpointer *dest = (gpointer *)dest_ptr;
90         gpointer *src = (gpointer *)src_ptr;
91
92         /*overlapping that required backward copying*/
93         if (src < dest && (src + count) > dest) {
94                 gpointer *start = dest;
95                 dest += count - 1;
96                 src += count - 1;
97
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);
104                 }
105         } else {
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);
113                 }
114         }       
115 }
116
117 static void
118 sgen_card_table_wbarrier_value_copy (gpointer dest, gpointer src, int count, size_t element_size)
119 {
120         size_t size = count * element_size;
121
122         TLAB_ACCESS_INIT;
123         ENTER_CRITICAL_REGION;
124
125         mono_gc_memmove_atomic (dest, src, size);
126         sgen_card_table_mark_range ((mword)dest, size);
127
128         EXIT_CRITICAL_REGION;
129 }
130
131 static void
132 sgen_card_table_wbarrier_object_copy (GCObject* obj, GCObject *src)
133 {
134         size_t size = sgen_client_par_object_get_size (SGEN_LOAD_VTABLE_UNCHECKED (obj), obj);
135
136         TLAB_ACCESS_INIT;
137         ENTER_CRITICAL_REGION;
138
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);
142
143         EXIT_CRITICAL_REGION;
144 }
145
146 static void
147 sgen_card_table_wbarrier_generic_nostore (gpointer ptr)
148 {
149         sgen_card_table_mark_address ((mword)ptr);      
150 }
151
152 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
153
154 guint8 *sgen_shadow_cardtable;
155
156 #define SGEN_CARDTABLE_END (sgen_cardtable + CARD_COUNT_IN_BYTES)
157
158 static gboolean
159 sgen_card_table_region_begin_scanning (mword start, mword size)
160 {
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))
165                         return TRUE;
166                 start += CARD_SIZE_IN_BYTES;
167         }
168         return FALSE;
169 }
170
171 #else
172
173 static gboolean
174 sgen_card_table_region_begin_scanning (mword start, mword size)
175 {
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);
179
180         /*XXX this can be improved to work on words and have a branchless body */
181         while (card != end) {
182                 if (*card++) {
183                         res = TRUE;
184                         break;
185                 }
186         }
187
188         memset (sgen_card_table_get_card_address (start), 0, size >> CARD_BITS);
189
190         return res;
191 }
192
193 #endif
194
195 /*FIXME this assumes that major blocks are multiple of 4K which is pretty reasonable */
196 gboolean
197 sgen_card_table_get_card_data (guint8 *data_dest, mword address, mword cards)
198 {
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);
202         mword mask = 0;
203
204         for (; dest < end; ++dest, ++start) {
205                 mword v = *start;
206                 *dest = v;
207                 mask |= v;
208
209 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
210                 *start = 0;
211 #endif
212         }
213
214         return mask != 0;
215 }
216
217 void*
218 sgen_card_table_align_pointer (void *ptr)
219 {
220         return (void*)((mword)ptr & ~(CARD_SIZE_IN_BYTES - 1));
221 }
222
223 void
224 sgen_card_table_mark_range (mword address, mword size)
225 {
226         mword num_cards = sgen_card_table_number_of_cards_in_range (address, size);
227         guint8 *start = sgen_card_table_get_card_address (address);
228
229 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
230         /*
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.
233          */
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);
239                 return;
240         }
241 #endif
242
243         memset (start, 1, num_cards);
244 }
245
246 static gboolean
247 sgen_card_table_is_range_marked (guint8 *cards, mword address, mword size)
248 {
249         guint8 *end = cards + sgen_card_table_number_of_cards_in_range (address, size);
250
251         /*This is safe since this function is only called by code that only passes continuous card blocks*/
252         while (cards != end) {
253                 if (*cards++)
254                         return TRUE;
255         }
256         return FALSE;
257
258 }
259
260 static void
261 sgen_card_table_record_pointer (gpointer address)
262 {
263         *sgen_card_table_get_card_address ((mword)address) = 1;
264 }
265
266 static gboolean
267 sgen_card_table_find_address (char *addr)
268 {
269         return sgen_card_table_address_is_marked ((mword)addr);
270 }
271
272 static gboolean
273 sgen_card_table_find_address_with_cards (char *cards_start, guint8 *cards, char *addr)
274 {
275         cards_start = (char *)sgen_card_table_align_pointer (cards_start);
276         return cards [(addr - cards_start) >> CARD_BITS];
277 }
278
279 static void
280 update_mod_union (guint8 *dest, guint8 *start_card, size_t num_cards)
281 {
282         int i;
283         /* Marking from another thread can happen while we mark here */
284         for (i = 0; i < num_cards; ++i) {
285                 if (start_card [i])
286                         dest [i] = 1;
287         }
288 }
289
290 guint8*
291 sgen_card_table_alloc_mod_union (char *obj, mword obj_size)
292 {
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);
296         return mod_union;
297 }
298
299 void
300 sgen_card_table_free_mod_union (guint8 *mod_union, char *obj, mword obj_size)
301 {
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);
304 }
305
306 void
307 sgen_card_table_update_mod_union_from_cards (guint8 *dest, guint8 *start_card, size_t num_cards)
308 {
309         SGEN_ASSERT (0, dest, "Why don't we have a mod union?");
310         update_mod_union (dest, start_card, num_cards);
311 }
312
313 void
314 sgen_card_table_update_mod_union (guint8 *dest, char *obj, mword obj_size, size_t *out_num_cards)
315 {
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;
319 #endif
320         size_t num_cards;
321
322 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
323         size_t rest;
324
325         rest = num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj, obj_size);
326
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);
330                 dest += count;
331                 rest -= count;
332                 start_card = sgen_cardtable;
333         }
334         num_cards = rest;
335 #else
336         num_cards = end_card - start_card;
337 #endif
338
339         sgen_card_table_update_mod_union_from_cards (dest, start_card, num_cards);
340
341         if (out_num_cards)
342                 *out_num_cards = num_cards;
343 }
344
345 /* Preclean cards and saves the cards that need to be scanned afterwards in cards_preclean */
346 void
347 sgen_card_table_preclean_mod_union (guint8 *cards, guint8 *cards_preclean, size_t num_cards)
348 {
349         size_t i;
350
351         memcpy (cards_preclean, cards, num_cards);
352         for (i = 0; i < num_cards; i++) {
353                 if (cards_preclean [i]) {
354                         cards [i] = 0;
355                 }
356         }
357         /*
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.
364          */
365         mono_memory_barrier ();
366 }
367
368 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
369
370 static void
371 move_cards_to_shadow_table (mword start, mword size)
372 {
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);
376
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;
382
383                 memcpy (to, from, first_chunk);
384                 memcpy (sgen_shadow_cardtable, sgen_cardtable, second_chunk);
385         } else {
386                 memcpy (to, from, bytes);
387         }
388 }
389
390 static void
391 clear_cards (mword start, mword size)
392 {
393         guint8 *addr = sgen_card_table_get_card_address (start);
394         size_t bytes = sgen_card_table_number_of_cards_in_range (start, size);
395
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;
400
401                 memset (addr, 0, first_chunk);
402                 memset (sgen_cardtable, 0, bytes - first_chunk);
403         } else {
404                 memset (addr, 0, bytes);
405         }
406 }
407
408
409 #else
410
411 static void
412 clear_cards (mword start, mword size)
413 {
414         memset (sgen_card_table_get_card_address (start), 0, sgen_card_table_number_of_cards_in_range (start, size));
415 }
416
417
418 #endif
419
420 static void
421 sgen_card_table_clear_cards (void)
422 {
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);
426 }
427
428 static void
429 sgen_card_table_finish_minor_collection (void)
430 {
431         sgen_card_tables_collect_stats (FALSE);
432 }
433
434 static void
435 sgen_card_table_scan_remsets (ScanCopyContext ctx)
436 {
437         SGEN_TV_DECLARE (atv);
438         SGEN_TV_DECLARE (btv);
439
440         sgen_card_tables_collect_stats (TRUE);
441
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.*/
444         /*First we copy*/
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);
447
448         /*Then we clear*/
449         sgen_card_table_clear_cards ();
450 #endif
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;
460 }
461
462 guint8*
463 sgen_get_card_table_configuration (int *shift_bits, gpointer *mask)
464 {
465 #ifndef MANAGED_WBARRIER
466         return NULL;
467 #else
468         if (!sgen_cardtable)
469                 return NULL;
470
471         *shift_bits = CARD_BITS;
472 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
473         *mask = (gpointer)CARD_MASK;
474 #else
475         *mask = NULL;
476 #endif
477
478         return sgen_cardtable;
479 #endif
480 }
481
482 #if 0
483 void
484 sgen_card_table_dump_obj_card (GCObject *object, size_t size, void *dummy)
485 {
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);
488         int cnt = 0;
489         printf ("--obj %p %d cards [%p %p]--", object, size, start, end);
490         for (; start < end; ++start) {
491                 if (cnt == 0)
492                         printf ("\n\t[%p] ", start);
493                 printf ("%x ", *start);
494                 ++cnt;
495                 if (cnt == 8)
496                         cnt = 0;
497         }
498         printf ("\n");
499 }
500 #endif
501
502 void
503 sgen_cardtable_scan_object (GCObject *obj, mword block_obj_size, guint8 *cards, ScanCopyContext ctx)
504 {
505         HEAVY_STAT (++large_objects);
506
507         if (sgen_client_cardtable_scan_object (obj, block_obj_size, cards, ctx))
508                 return;
509
510         HEAVY_STAT (++bloby_objects);
511         if (cards) {
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);
516         }
517
518         binary_protocol_card_scan (obj, sgen_safe_object_get_size (obj));
519 }
520
521 #ifdef CARDTABLE_STATS
522
523 typedef struct {
524         int total, marked, remarked, gc_marked; 
525 } card_stats;
526
527 static card_stats major_stats, los_stats;
528 static card_stats *cur_stats;
529
530 static void
531 count_marked_cards (mword start, mword size)
532 {
533         mword end = start + size;
534         while (start <= end) {
535                 guint8 card = *sgen_card_table_get_card_address (start);
536                 ++cur_stats->total;
537                 if (card)
538                         ++cur_stats->marked;
539                 if (card == 2)
540                         ++cur_stats->gc_marked;
541                 start += CARD_SIZE_IN_BYTES;
542         }
543 }
544
545 static void
546 count_remarked_cards (mword start, mword size)
547 {
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;
553                 }
554                 start += CARD_SIZE_IN_BYTES;
555         }
556 }
557
558 #endif
559
560 static void
561 sgen_card_tables_collect_stats (gboolean begin)
562 {
563 #ifdef CARDTABLE_STATS
564         if (begin) {
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);
571         } else {
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);
580         }
581 #endif
582 }
583
584 void
585 sgen_card_table_init (SgenRememberedSet *remset)
586 {
587         sgen_cardtable = (guint8 *)sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "card table");
588
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");
591 #endif
592
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);
597
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);
601 #endif
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);
604
605
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;
612
613         remset->scan_remsets = sgen_card_table_scan_remsets;
614
615         remset->finish_minor_collection = sgen_card_table_finish_minor_collection;
616         remset->clear_cards = sgen_card_table_clear_cards;
617
618         remset->find_address = sgen_card_table_find_address;
619         remset->find_address_with_cards = sgen_card_table_find_address_with_cards;
620
621         need_mod_union = sgen_get_major_collector ()->is_concurrent;
622 }
623
624 #endif /*HAVE_SGEN_GC*/