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