787e3f555c0f73ce9d1df6d675f5c2812034d55c
[mono.git] / mono / sgen / sgen-marksweep-drain-gray-stack.h
1 /*
2  * sgen-marksweep-drain-gray-stack.h: The copy/mark and gray stack
3  *     draining functions of the M&S major collector.
4  *
5  * Copyright (C) 2014 Xamarin Inc
6  *
7  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8  */
9
10 /*
11  * COPY_OR_MARK_FUNCTION_NAME must be defined to be the function name of the copy/mark
12  * function.
13  *
14  * SCAN_OBJECT_FUNCTION_NAME must be defined to be the function name of the object scanning
15  * function.
16  *
17  * DRAIN_GRAY_STACK_FUNCTION_NAME must be defined to be the function name of the gray stack
18  * draining function.
19  *
20  * Define COPY_OR_MARK_WITH_EVACUATION to support evacuation.
21  */
22
23 /* Returns whether the object is still in the nursery. */
24 static inline MONO_ALWAYS_INLINE gboolean
25 COPY_OR_MARK_FUNCTION_NAME (GCObject **ptr, GCObject *obj, SgenGrayQueue *queue)
26 {
27         MSBlockInfo *block;
28
29 #ifdef HEAVY_STATISTICS
30         ++stat_optimized_copy;
31         {
32                 GCObject *forwarded;
33                 SgenDescriptor desc;
34                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
35                         desc = sgen_obj_get_descriptor_safe (forwarded);
36                 else
37                         desc = sgen_obj_get_descriptor_safe (obj);
38
39                 sgen_descriptor_count_copied_object (desc);
40         }
41 #endif
42
43         SGEN_ASSERT (9, obj, "null object from pointer %p", ptr);
44 #if !defined(COPY_OR_MARK_CONCURRENT) && !defined(COPY_OR_MARK_CONCURRENT_WITH_EVACUATION)
45         SGEN_ASSERT (9, current_collection_generation == GENERATION_OLD, "old gen parallel allocator called from a %d collection", current_collection_generation);
46 #endif
47
48         if (sgen_ptr_in_nursery (obj)) {
49 #if !defined(COPY_OR_MARK_CONCURRENT) && !defined(COPY_OR_MARK_CONCURRENT_WITH_EVACUATION)
50                 int word, bit;
51                 GCObject *forwarded, *old_obj;
52                 mword vtable_word = *(mword*)obj;
53
54                 HEAVY_STAT (++stat_optimized_copy_nursery);
55
56 #if SGEN_MAX_DEBUG_LEVEL >= 9
57                 if (sgen_nursery_is_to_space (obj))
58                         SGEN_ASSERT (9, !SGEN_VTABLE_IS_PINNED (vtable_word) && !SGEN_VTABLE_IS_FORWARDED (vtable_word), "To-space object can't be pinned or forwarded.");
59 #endif
60
61                 if (SGEN_VTABLE_IS_PINNED (vtable_word)) {
62                         SGEN_ASSERT (9, !SGEN_VTABLE_IS_FORWARDED (vtable_word), "Cannot be both pinned and forwarded.");
63                         HEAVY_STAT (++stat_optimized_copy_nursery_pinned);
64                         return TRUE;
65                 }
66                 if ((forwarded = (GCObject *)SGEN_VTABLE_IS_FORWARDED (vtable_word))) {
67                         HEAVY_STAT (++stat_optimized_copy_nursery_forwarded);
68                         SGEN_UPDATE_REFERENCE (ptr, forwarded);
69                         return sgen_ptr_in_nursery (forwarded);
70                 }
71
72                 /* An object in the nursery To Space has already been copied and grayed. Nothing to do. */
73                 if (sgen_nursery_is_to_space (obj))
74                         return TRUE;
75
76 #ifdef COPY_OR_MARK_WITH_EVACUATION
77         do_copy_object:
78 #endif
79                 old_obj = obj;
80                 obj = copy_object_no_checks (obj, queue);
81                 if (G_UNLIKELY (old_obj == obj)) {
82                         /*
83                          * If we fail to evacuate an object we just stop doing it for a
84                          * given block size as all other will surely fail too.
85                          */
86                         /* FIXME: test this case somehow. */
87                         if (!sgen_ptr_in_nursery (obj)) {
88                                 int size_index;
89                                 block = MS_BLOCK_FOR_OBJ (obj);
90                                 size_index = block->obj_size_index;
91                                 evacuate_block_obj_sizes [size_index] = FALSE;
92                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, sgen_obj_get_descriptor (obj), block, queue);
93                                 return FALSE;
94                         }
95                         return TRUE;
96                 }
97                 HEAVY_STAT (++stat_objects_copied_major);
98                 SGEN_UPDATE_REFERENCE (ptr, obj);
99
100                 if (sgen_ptr_in_nursery (obj))
101                         return TRUE;
102
103                 /*
104                  * FIXME: See comment for copy_object_no_checks().  If
105                  * we have that, we can let the allocation function
106                  * give us the block info, too, and we won't have to
107                  * re-fetch it.
108                  *
109                  * FIXME (2): We should rework this to avoid all those nursery checks.
110                  */
111                 /*
112                  * For the split nursery allocator the object might
113                  * still be in the nursery despite having being
114                  * promoted, in which case we can't mark it.
115                  */
116                 block = MS_BLOCK_FOR_OBJ (obj);
117                 MS_CALC_MARK_BIT (word, bit, obj);
118                 SGEN_ASSERT (9, !MS_MARK_BIT (block, word, bit), "object %p already marked", obj);
119                 MS_SET_MARK_BIT (block, word, bit);
120                 binary_protocol_mark (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size (obj));
121
122                 return FALSE;
123 #endif
124         } else {
125                 mword vtable_word = *(mword*)obj;
126                 SgenDescriptor desc;
127                 int type;
128
129                 HEAVY_STAT (++stat_optimized_copy_major);
130
131 #ifdef COPY_OR_MARK_WITH_EVACUATION
132                 {
133                         GCObject *forwarded;
134                         if ((forwarded = (GCObject *)SGEN_VTABLE_IS_FORWARDED (vtable_word))) {
135                                 HEAVY_STAT (++stat_optimized_copy_major_forwarded);
136                                 SGEN_UPDATE_REFERENCE (ptr, forwarded);
137                                 SGEN_ASSERT (9, !sgen_ptr_in_nursery (forwarded), "Cannot be forwarded to nursery.");
138                                 return FALSE;
139                         }
140                 }
141 #endif
142
143                 SGEN_ASSERT (9, !SGEN_VTABLE_IS_PINNED (vtable_word), "Pinned object in non-pinned block?");
144
145                 /* We untag the vtable for concurrent M&S, in case bridge is running and it tagged it */
146                 desc = sgen_vtable_get_descriptor ((GCVTable)SGEN_POINTER_UNTAG_VTABLE (vtable_word));
147                 type = desc & DESC_TYPE_MASK;
148
149                 if (sgen_safe_object_is_small (obj, type)) {
150 #ifdef HEAVY_STATISTICS
151                         if (type <= DESC_TYPE_MAX_SMALL_OBJ)
152                                 ++stat_optimized_copy_major_small_fast;
153                         else
154                                 ++stat_optimized_copy_major_small_slow;
155 #endif
156
157                         block = MS_BLOCK_FOR_OBJ (obj);
158
159 #ifdef COPY_OR_MARK_CONCURRENT_WITH_EVACUATION
160                         if (G_UNLIKELY (major_block_is_evacuating (block))) {
161                                 /*
162                                  * We don't copy within the concurrent phase. These objects will
163                                  * be handled below in the finishing pause, by scanning the mod-union
164                                  * card table.
165                                  */
166                                 return FALSE;
167                         }
168 #endif
169
170 #ifdef COPY_OR_MARK_WITH_EVACUATION
171                         if (major_block_is_evacuating (block)) {
172                                 HEAVY_STAT (++stat_optimized_copy_major_small_evacuate);
173                                 goto do_copy_object;
174                         }
175 #endif
176
177 #ifdef COPY_OR_MARK_PARALLEL
178                         MS_MARK_OBJECT_AND_ENQUEUE_PAR (obj, desc, block, queue);
179 #else
180                         MS_MARK_OBJECT_AND_ENQUEUE (obj, desc, block, queue);
181 #endif
182                 } else {
183                         HEAVY_STAT (++stat_optimized_copy_major_large);
184
185                         if (sgen_los_object_is_pinned (obj))
186                                 return FALSE;
187                         binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), sgen_safe_object_get_size (obj));
188
189                         sgen_los_pin_object (obj);
190                         if (SGEN_OBJECT_HAS_REFERENCES (obj))
191                                 GRAY_OBJECT_ENQUEUE (queue, obj, desc);
192                 }
193                 return FALSE;
194         }
195
196         return TRUE;
197 }
198
199 static void
200 SCAN_OBJECT_FUNCTION_NAME (GCObject *full_object, SgenDescriptor desc, SgenGrayQueue *queue)
201 {
202         char *start = (char*)full_object;
203
204 #ifdef HEAVY_STATISTICS
205         ++stat_optimized_major_scan;
206         if (!sgen_gc_descr_has_references (desc))
207                 ++stat_optimized_major_scan_no_refs;
208         sgen_descriptor_count_scanned_object (desc);
209 #endif
210 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
211         add_scanned_object (start);
212 #endif
213
214         /* Now scan the object. */
215
216 #undef HANDLE_PTR
217 #if defined(COPY_OR_MARK_CONCURRENT_WITH_EVACUATION)
218 #define HANDLE_PTR(ptr,obj)     do {                                    \
219                 GCObject *__old = *(ptr);                               \
220                 binary_protocol_scan_process_reference ((full_object), (ptr), __old); \
221                 if (__old && !sgen_ptr_in_nursery (__old)) {            \
222                         if (G_UNLIKELY (!sgen_ptr_in_nursery (ptr) &&   \
223                                         sgen_safe_object_is_small (__old, sgen_obj_get_descriptor (__old) & DESC_TYPE_MASK) && \
224                                         major_block_is_evacuating (MS_BLOCK_FOR_OBJ (__old)))) { \
225                                 mark_mod_union_card ((full_object), (void**)(ptr), __old); \
226                         } else {                                        \
227                                 PREFETCH_READ (__old);                  \
228                                 COPY_OR_MARK_FUNCTION_NAME ((ptr), __old, queue); \
229                         }                                               \
230                 } else {                                                \
231                         if (G_UNLIKELY (sgen_ptr_in_nursery (__old) && !sgen_ptr_in_nursery ((ptr)) && !sgen_cement_is_forced (__old))) \
232                                 mark_mod_union_card ((full_object), (void**)(ptr), __old); \
233                         }                                               \
234                 } while (0)
235 #elif defined(COPY_OR_MARK_CONCURRENT)
236 #define HANDLE_PTR(ptr,obj)     do {                                    \
237                 GCObject *__old = *(ptr);                               \
238                 binary_protocol_scan_process_reference ((full_object), (ptr), __old); \
239                 if (__old && !sgen_ptr_in_nursery (__old)) {            \
240                         PREFETCH_READ (__old);                  \
241                         COPY_OR_MARK_FUNCTION_NAME ((ptr), __old, queue); \
242                 } else {                                                \
243                         if (G_UNLIKELY (sgen_ptr_in_nursery (__old) && !sgen_ptr_in_nursery ((ptr)) && !sgen_cement_is_forced (__old))) \
244                                 mark_mod_union_card ((full_object), (void**)(ptr), __old); \
245                         }                                               \
246                 } while (0)
247 #else
248 #define HANDLE_PTR(ptr,obj)     do {                                    \
249                 GCObject *__old = *(ptr);                                       \
250                 binary_protocol_scan_process_reference ((full_object), (ptr), __old); \
251                 if (__old) {                                            \
252                         gboolean __still_in_nursery = COPY_OR_MARK_FUNCTION_NAME ((ptr), __old, queue); \
253                         if (G_UNLIKELY (__still_in_nursery && !sgen_ptr_in_nursery ((ptr)) && !SGEN_OBJECT_IS_CEMENTED (*(ptr)))) { \
254                                 GCObject *__copy = *(ptr);                      \
255                                 sgen_add_to_global_remset ((ptr), __copy); \
256                         }                                               \
257                 }                                                       \
258         } while (0)
259 #endif
260
261 #define SCAN_OBJECT_PROTOCOL
262 #include "sgen-scan-object.h"
263 }
264
265 #ifdef SCAN_VTYPE_FUNCTION_NAME 
266 static void
267 SCAN_VTYPE_FUNCTION_NAME (GCObject *full_object, char *start, SgenDescriptor desc, SgenGrayQueue *queue BINARY_PROTOCOL_ARG (size_t size))
268 {
269         SGEN_OBJECT_LAYOUT_STATISTICS_DECLARE_BITMAP;
270
271 #ifdef HEAVY_STATISTICS
272         /* FIXME: We're half scanning this object.  How do we account for that? */
273         //add_scanned_object (start);
274 #endif
275
276         /* The descriptors include info about the object header as well */
277         start -= SGEN_CLIENT_OBJECT_HEADER_SIZE;
278
279         /* We use the same HANDLE_PTR from the obj scan function */
280 #define SCAN_OBJECT_NOVTABLE
281 #define SCAN_OBJECT_PROTOCOL
282 #include "sgen-scan-object.h"
283
284         SGEN_OBJECT_LAYOUT_STATISTICS_COMMIT_BITMAP;
285 }
286 #endif
287
288 #ifdef SCAN_PTR_FIELD_FUNCTION_NAME
289 static void
290 SCAN_PTR_FIELD_FUNCTION_NAME (GCObject *full_object, GCObject **ptr, SgenGrayQueue *queue)
291 {
292         HANDLE_PTR (ptr, NULL);
293 }
294 #endif
295
296 static gboolean
297 DRAIN_GRAY_STACK_FUNCTION_NAME (SgenGrayQueue *queue)
298 {
299 #if defined(COPY_OR_MARK_CONCURRENT) || defined(COPY_OR_MARK_CONCURRENT_WITH_EVACUATION)
300         int i;
301         for (i = 0; i < 32; i++) {
302 #else
303         for (;;) {
304 #endif
305                 GCObject *obj;
306                 SgenDescriptor desc;
307
308                 HEAVY_STAT (++stat_drain_loops);
309
310                 GRAY_OBJECT_DEQUEUE (queue, &obj, &desc);
311                 if (!obj)
312                         return TRUE;
313
314                 SCAN_OBJECT_FUNCTION_NAME (obj, desc, queue);
315         }
316         return FALSE;
317 }
318
319 #undef COPY_OR_MARK_PARALLEL
320 #undef COPY_OR_MARK_FUNCTION_NAME
321 #undef COPY_OR_MARK_WITH_EVACUATION
322 #undef COPY_OR_MARK_CONCURRENT
323 #undef COPY_OR_MARK_CONCURRENT_WITH_EVACUATION
324 #undef SCAN_OBJECT_FUNCTION_NAME
325 #undef SCAN_VTYPE_FUNCTION_NAME
326 #undef SCAN_PTR_FIELD_FUNCTION_NAME
327 #undef DRAIN_GRAY_STACK_FUNCTION_NAME