Merge pull request #1668 from alexanderkyte/bug1856
[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
60 static guint64 los_marked_cards;
61 static guint64 large_objects;
62 static guint64 bloby_objects;
63 static guint64 los_array_cards;
64 static guint64 los_array_remsets;
65
66 #endif
67 static guint64 major_card_scan_time;
68 static guint64 los_card_scan_time;
69
70 static guint64 last_major_scan_time;
71 static guint64 last_los_scan_time;
72
73 static void sgen_card_tables_collect_stats (gboolean begin);
74
75 mword
76 sgen_card_table_number_of_cards_in_range (mword address, mword size)
77 {
78         mword end = address + MAX (1, size) - 1;
79         return (end >> CARD_BITS) - (address >> CARD_BITS) + 1;
80 }
81
82 static void
83 sgen_card_table_wbarrier_set_field (GCObject *obj, gpointer field_ptr, GCObject* value)
84 {
85         *(void**)field_ptr = value;
86         if (need_mod_union || sgen_ptr_in_nursery (value))
87                 sgen_card_table_mark_address ((mword)field_ptr);
88         sgen_dummy_use (value);
89 }
90
91 static void
92 sgen_card_table_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
93 {
94         gpointer *dest = dest_ptr;
95         gpointer *src = src_ptr;
96
97         /*overlapping that required backward copying*/
98         if (src < dest && (src + count) > dest) {
99                 gpointer *start = dest;
100                 dest += count - 1;
101                 src += count - 1;
102
103                 for (; dest >= start; --src, --dest) {
104                         gpointer value = *src;
105                         SGEN_UPDATE_REFERENCE_ALLOW_NULL (dest, value);
106                         if (need_mod_union || sgen_ptr_in_nursery (value))
107                                 sgen_card_table_mark_address ((mword)dest);
108                         sgen_dummy_use (value);
109                 }
110         } else {
111                 gpointer *end = dest + count;
112                 for (; dest < end; ++src, ++dest) {
113                         gpointer value = *src;
114                         SGEN_UPDATE_REFERENCE_ALLOW_NULL (dest, value);
115                         if (need_mod_union || sgen_ptr_in_nursery (value))
116                                 sgen_card_table_mark_address ((mword)dest);
117                         sgen_dummy_use (value);
118                 }
119         }       
120 }
121
122 static void
123 sgen_card_table_wbarrier_value_copy (gpointer dest, gpointer src, int count, size_t element_size)
124 {
125         size_t size = count * element_size;
126
127 #ifdef DISABLE_CRITICAL_REGION
128         LOCK_GC;
129 #else
130         TLAB_ACCESS_INIT;
131         ENTER_CRITICAL_REGION;
132 #endif
133         mono_gc_memmove_atomic (dest, src, size);
134         sgen_card_table_mark_range ((mword)dest, size);
135 #ifdef DISABLE_CRITICAL_REGION
136         UNLOCK_GC;
137 #else
138         EXIT_CRITICAL_REGION;
139 #endif
140 }
141
142 static void
143 sgen_card_table_wbarrier_object_copy (GCObject* obj, GCObject *src)
144 {
145         size_t size = sgen_client_par_object_get_size (SGEN_LOAD_VTABLE_UNCHECKED (obj), obj);
146
147 #ifdef DISABLE_CRITICAL_REGION
148         LOCK_GC;
149 #else
150         TLAB_ACCESS_INIT;
151         ENTER_CRITICAL_REGION;
152 #endif
153         mono_gc_memmove_aligned ((char*)obj + SGEN_CLIENT_OBJECT_HEADER_SIZE, (char*)src + SGEN_CLIENT_OBJECT_HEADER_SIZE,
154                         size - SGEN_CLIENT_OBJECT_HEADER_SIZE);
155         sgen_card_table_mark_range ((mword)obj, size);
156 #ifdef DISABLE_CRITICAL_REGION
157         UNLOCK_GC;
158 #else
159         EXIT_CRITICAL_REGION;
160 #endif  
161 }
162
163 static void
164 sgen_card_table_wbarrier_generic_nostore (gpointer ptr)
165 {
166         sgen_card_table_mark_address ((mword)ptr);      
167 }
168
169 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
170
171 guint8 *sgen_shadow_cardtable;
172
173 #define SGEN_CARDTABLE_END (sgen_cardtable + CARD_COUNT_IN_BYTES)
174
175 static gboolean
176 sgen_card_table_region_begin_scanning (mword start, mword size)
177 {
178         mword end = start + size;
179         /*XXX this can be improved to work on words and have a single loop induction var */
180         while (start < end) {
181                 if (sgen_card_table_card_begin_scanning (start))
182                         return TRUE;
183                 start += CARD_SIZE_IN_BYTES;
184         }
185         return FALSE;
186 }
187
188 #else
189
190 static gboolean
191 sgen_card_table_region_begin_scanning (mword start, mword size)
192 {
193         gboolean res = FALSE;
194         guint8 *card = sgen_card_table_get_card_address (start);
195         guint8 *end = card + sgen_card_table_number_of_cards_in_range (start, size);
196
197         /*XXX this can be improved to work on words and have a branchless body */
198         while (card != end) {
199                 if (*card++) {
200                         res = TRUE;
201                         break;
202                 }
203         }
204
205         memset (sgen_card_table_get_card_address (start), 0, size >> CARD_BITS);
206
207         return res;
208 }
209
210 #endif
211
212 /*FIXME this assumes that major blocks are multiple of 4K which is pretty reasonable */
213 gboolean
214 sgen_card_table_get_card_data (guint8 *data_dest, mword address, mword cards)
215 {
216         mword *start = (mword*)sgen_card_table_get_card_scan_address (address);
217         mword *dest = (mword*)data_dest;
218         mword *end = (mword*)(data_dest + cards);
219         mword mask = 0;
220
221         for (; dest < end; ++dest, ++start) {
222                 mword v = *start;
223                 *dest = v;
224                 mask |= v;
225
226 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
227                 *start = 0;
228 #endif
229         }
230
231         return mask != 0;
232 }
233
234 void*
235 sgen_card_table_align_pointer (void *ptr)
236 {
237         return (void*)((mword)ptr & ~(CARD_SIZE_IN_BYTES - 1));
238 }
239
240 void
241 sgen_card_table_mark_range (mword address, mword size)
242 {
243         mword num_cards = sgen_card_table_number_of_cards_in_range (address, size);
244         guint8 *start = sgen_card_table_get_card_address (address);
245
246 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
247         /*
248          * FIXME: There's a theoretical bug here, namely that the card table is allocated so
249          * far toward the end of the address space that start + num_cards overflows.
250          */
251         guint8 *end = start + num_cards;
252         SGEN_ASSERT (0, num_cards <= CARD_COUNT_IN_BYTES, "How did we get an object larger than the card table?");
253         if (end > SGEN_CARDTABLE_END) {
254                 memset (start, 1, SGEN_CARDTABLE_END - start);
255                 memset (sgen_cardtable, 1, end - sgen_cardtable);
256                 return;
257         }
258 #endif
259
260         memset (start, 1, num_cards);
261 }
262
263 static gboolean
264 sgen_card_table_is_range_marked (guint8 *cards, mword address, mword size)
265 {
266         guint8 *end = cards + sgen_card_table_number_of_cards_in_range (address, size);
267
268         /*This is safe since this function is only called by code that only passes continuous card blocks*/
269         while (cards != end) {
270                 if (*cards++)
271                         return TRUE;
272         }
273         return FALSE;
274
275 }
276
277 static void
278 sgen_card_table_record_pointer (gpointer address)
279 {
280         *sgen_card_table_get_card_address ((mword)address) = 1;
281 }
282
283 static gboolean
284 sgen_card_table_find_address (char *addr)
285 {
286         return sgen_card_table_address_is_marked ((mword)addr);
287 }
288
289 static gboolean
290 sgen_card_table_find_address_with_cards (char *cards_start, guint8 *cards, char *addr)
291 {
292         cards_start = sgen_card_table_align_pointer (cards_start);
293         return cards [(addr - cards_start) >> CARD_BITS];
294 }
295
296 static void
297 update_mod_union (guint8 *dest, guint8 *start_card, size_t num_cards)
298 {
299         int i;
300         for (i = 0; i < num_cards; ++i)
301                 dest [i] |= start_card [i];
302 }
303
304 guint8*
305 sgen_card_table_alloc_mod_union (char *obj, mword obj_size)
306 {
307         size_t num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj, obj_size);
308         guint8 *mod_union = sgen_alloc_internal_dynamic (num_cards, INTERNAL_MEM_CARDTABLE_MOD_UNION, TRUE);
309         memset (mod_union, 0, num_cards);
310         return mod_union;
311 }
312
313 void
314 sgen_card_table_free_mod_union (guint8 *mod_union, char *obj, mword obj_size)
315 {
316         size_t num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj, obj_size);
317         sgen_free_internal_dynamic (mod_union, num_cards, INTERNAL_MEM_CARDTABLE_MOD_UNION);
318 }
319
320 void
321 sgen_card_table_update_mod_union_from_cards (guint8 *dest, guint8 *start_card, size_t num_cards)
322 {
323         SGEN_ASSERT (0, dest, "Why don't we have a mod union?");
324         update_mod_union (dest, start_card, num_cards);
325 }
326
327 void
328 sgen_card_table_update_mod_union (guint8 *dest, char *obj, mword obj_size, size_t *out_num_cards)
329 {
330         guint8 *start_card = sgen_card_table_get_card_address ((mword)obj);
331 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
332         guint8 *end_card = sgen_card_table_get_card_address ((mword)obj + obj_size - 1) + 1;
333 #endif
334         size_t num_cards;
335
336 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
337         size_t rest;
338
339         rest = num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj, obj_size);
340
341         while (start_card + rest > SGEN_CARDTABLE_END) {
342                 size_t count = SGEN_CARDTABLE_END - start_card;
343                 sgen_card_table_update_mod_union_from_cards (dest, start_card, count);
344                 dest += count;
345                 rest -= count;
346                 start_card = sgen_cardtable;
347         }
348         num_cards = rest;
349 #else
350         num_cards = end_card - start_card;
351 #endif
352
353         sgen_card_table_update_mod_union_from_cards (dest, start_card, num_cards);
354
355         if (out_num_cards)
356                 *out_num_cards = num_cards;
357 }
358
359 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
360
361 static void
362 move_cards_to_shadow_table (mword start, mword size)
363 {
364         guint8 *from = sgen_card_table_get_card_address (start);
365         guint8 *to = sgen_card_table_get_shadow_card_address (start);
366         size_t bytes = sgen_card_table_number_of_cards_in_range (start, size);
367
368         if (bytes >= CARD_COUNT_IN_BYTES) {
369                 memcpy (sgen_shadow_cardtable, sgen_cardtable, CARD_COUNT_IN_BYTES);
370         } else if (to + bytes > SGEN_SHADOW_CARDTABLE_END) {
371                 size_t first_chunk = SGEN_SHADOW_CARDTABLE_END - to;
372                 size_t second_chunk = MIN (CARD_COUNT_IN_BYTES, bytes) - first_chunk;
373
374                 memcpy (to, from, first_chunk);
375                 memcpy (sgen_shadow_cardtable, sgen_cardtable, second_chunk);
376         } else {
377                 memcpy (to, from, bytes);
378         }
379 }
380
381 static void
382 clear_cards (mword start, mword size)
383 {
384         guint8 *addr = sgen_card_table_get_card_address (start);
385         size_t bytes = sgen_card_table_number_of_cards_in_range (start, size);
386
387         if (bytes >= CARD_COUNT_IN_BYTES) {
388                 memset (sgen_cardtable, 0, CARD_COUNT_IN_BYTES);
389         } else if (addr + bytes > SGEN_CARDTABLE_END) {
390                 size_t first_chunk = SGEN_CARDTABLE_END - addr;
391
392                 memset (addr, 0, first_chunk);
393                 memset (sgen_cardtable, 0, bytes - first_chunk);
394         } else {
395                 memset (addr, 0, bytes);
396         }
397 }
398
399
400 #else
401
402 static void
403 clear_cards (mword start, mword size)
404 {
405         memset (sgen_card_table_get_card_address (start), 0, sgen_card_table_number_of_cards_in_range (start, size));
406 }
407
408
409 #endif
410
411 static void
412 sgen_card_table_clear_cards (void)
413 {
414         /*XXX we could do this in 2 ways. using mincore or iterating over all sections/los objects */
415         sgen_major_collector_iterate_live_block_ranges (clear_cards);
416         sgen_los_iterate_live_block_ranges (clear_cards);
417 }
418
419 static void
420 sgen_card_table_finish_minor_collection (void)
421 {
422         sgen_card_tables_collect_stats (FALSE);
423 }
424
425 static void
426 sgen_card_table_scan_remsets (ScanCopyContext ctx)
427 {
428         SGEN_TV_DECLARE (atv);
429         SGEN_TV_DECLARE (btv);
430
431         sgen_card_tables_collect_stats (TRUE);
432
433 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
434         /*FIXME we should have a bit on each block/los object telling if the object have marked cards.*/
435         /*First we copy*/
436         sgen_major_collector_iterate_live_block_ranges (move_cards_to_shadow_table);
437         sgen_los_iterate_live_block_ranges (move_cards_to_shadow_table);
438
439         /*Then we clear*/
440         sgen_card_table_clear_cards ();
441 #endif
442         SGEN_TV_GETTIME (atv);
443         sgen_get_major_collector ()->scan_card_table (FALSE, ctx);
444         SGEN_TV_GETTIME (btv);
445         last_major_scan_time = SGEN_TV_ELAPSED (atv, btv); 
446         major_card_scan_time += last_major_scan_time;
447         sgen_los_scan_card_table (FALSE, ctx);
448         SGEN_TV_GETTIME (atv);
449         last_los_scan_time = SGEN_TV_ELAPSED (btv, atv);
450         los_card_scan_time += last_los_scan_time;
451 }
452
453 guint8*
454 sgen_get_card_table_configuration (int *shift_bits, gpointer *mask)
455 {
456 #ifndef MANAGED_WBARRIER
457         return NULL;
458 #else
459         if (!sgen_cardtable)
460                 return NULL;
461
462         *shift_bits = CARD_BITS;
463 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
464         *mask = (gpointer)CARD_MASK;
465 #else
466         *mask = NULL;
467 #endif
468
469         return sgen_cardtable;
470 #endif
471 }
472
473 #if 0
474 void
475 sgen_card_table_dump_obj_card (char *object, size_t size, void *dummy)
476 {
477         guint8 *start = sgen_card_table_get_card_scan_address (object);
478         guint8 *end = start + sgen_card_table_number_of_cards_in_range (object, size);
479         int cnt = 0;
480         printf ("--obj %p %d cards [%p %p]--", object, size, start, end);
481         for (; start < end; ++start) {
482                 if (cnt == 0)
483                         printf ("\n\t[%p] ", start);
484                 printf ("%x ", *start);
485                 ++cnt;
486                 if (cnt == 8)
487                         cnt = 0;
488         }
489         printf ("\n");
490 }
491 #endif
492
493 void
494 sgen_cardtable_scan_object (char *obj, mword block_obj_size, guint8 *cards, gboolean mod_union, ScanCopyContext ctx)
495 {
496         HEAVY_STAT (++large_objects);
497
498         if (sgen_client_cardtable_scan_object (obj, block_obj_size, cards, mod_union, ctx))
499                 return;
500
501         HEAVY_STAT (++bloby_objects);
502         if (cards) {
503                 if (sgen_card_table_is_range_marked (cards, (mword)obj, block_obj_size))
504                         ctx.ops->scan_object (obj, sgen_obj_get_descriptor (obj), ctx.queue);
505         } else if (sgen_card_table_region_begin_scanning ((mword)obj, block_obj_size)) {
506                 ctx.ops->scan_object (obj, sgen_obj_get_descriptor (obj), ctx.queue);
507         }
508
509         binary_protocol_card_scan (obj, sgen_safe_object_get_size ((GCObject*)obj));
510 }
511
512 #ifdef CARDTABLE_STATS
513
514 typedef struct {
515         int total, marked, remarked, gc_marked; 
516 } card_stats;
517
518 static card_stats major_stats, los_stats;
519 static card_stats *cur_stats;
520
521 static void
522 count_marked_cards (mword start, mword size)
523 {
524         mword end = start + size;
525         while (start <= end) {
526                 guint8 card = *sgen_card_table_get_card_address (start);
527                 ++cur_stats->total;
528                 if (card)
529                         ++cur_stats->marked;
530                 if (card == 2)
531                         ++cur_stats->gc_marked;
532                 start += CARD_SIZE_IN_BYTES;
533         }
534 }
535
536 static void
537 count_remarked_cards (mword start, mword size)
538 {
539         mword end = start + size;
540         while (start <= end) {
541                 if (sgen_card_table_address_is_marked (start)) {
542                         ++cur_stats->remarked;
543                         *sgen_card_table_get_card_address (start) = 2;
544                 }
545                 start += CARD_SIZE_IN_BYTES;
546         }
547 }
548
549 #endif
550
551 static void
552 sgen_card_tables_collect_stats (gboolean begin)
553 {
554 #ifdef CARDTABLE_STATS
555         if (begin) {
556                 memset (&major_stats, 0, sizeof (card_stats));
557                 memset (&los_stats, 0, sizeof (card_stats));
558                 cur_stats = &major_stats;
559                 sgen_major_collector_iterate_live_block_ranges (count_marked_cards);
560                 cur_stats = &los_stats;
561                 sgen_los_iterate_live_block_ranges (count_marked_cards);
562         } else {
563                 cur_stats = &major_stats;
564                 sgen_major_collector_iterate_live_block_ranges (count_remarked_cards);
565                 cur_stats = &los_stats;
566                 sgen_los_iterate_live_block_ranges (count_remarked_cards);
567                 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", 
568                         major_stats.total, major_stats.marked, major_stats.gc_marked, major_stats.remarked,
569                         los_stats.total, los_stats.marked, los_stats.gc_marked, los_stats.remarked,
570                         last_major_scan_time / 10000.0f, last_los_scan_time / 10000.0f);
571         }
572 #endif
573 }
574
575 void
576 sgen_card_table_init (SgenRememberedSet *remset)
577 {
578         sgen_cardtable = sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, "card table");
579
580 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
581         sgen_shadow_cardtable = sgen_alloc_os_memory (CARD_COUNT_IN_BYTES, SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, "shadow card table");
582 #endif
583
584 #ifdef HEAVY_STATISTICS
585         mono_counters_register ("marked cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &marked_cards);
586         mono_counters_register ("scanned cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &scanned_cards);
587         mono_counters_register ("remarked cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &remarked_cards);
588
589         mono_counters_register ("los marked cards", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &los_marked_cards);
590         mono_counters_register ("los array cards scanned ", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &los_array_cards);
591         mono_counters_register ("los array remsets", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &los_array_remsets);
592         mono_counters_register ("cardtable scanned objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &scanned_objects);
593         mono_counters_register ("cardtable large objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &large_objects);
594         mono_counters_register ("cardtable bloby objects", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &bloby_objects);
595 #endif
596         mono_counters_register ("cardtable major scan time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &major_card_scan_time);
597         mono_counters_register ("cardtable los scan time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &los_card_scan_time);
598
599
600         remset->wbarrier_set_field = sgen_card_table_wbarrier_set_field;
601         remset->wbarrier_arrayref_copy = sgen_card_table_wbarrier_arrayref_copy;
602         remset->wbarrier_value_copy = sgen_card_table_wbarrier_value_copy;
603         remset->wbarrier_object_copy = sgen_card_table_wbarrier_object_copy;
604         remset->wbarrier_generic_nostore = sgen_card_table_wbarrier_generic_nostore;
605         remset->record_pointer = sgen_card_table_record_pointer;
606
607         remset->scan_remsets = sgen_card_table_scan_remsets;
608
609         remset->finish_minor_collection = sgen_card_table_finish_minor_collection;
610         remset->clear_cards = sgen_card_table_clear_cards;
611
612         remset->find_address = sgen_card_table_find_address;
613         remset->find_address_with_cards = sgen_card_table_find_address_with_cards;
614
615         need_mod_union = sgen_get_major_collector ()->is_concurrent;
616 }
617
618 #endif /*HAVE_SGEN_GC*/