[sgen] Add scan/copy context for the simple parallel nursery
[mono.git] / mono / sgen / sgen-minor-copy-object.h
1 /**
2  * \file
3  * Copy functions for nursery collections.
4  *
5  * Copyright 2001-2003 Ximian, Inc
6  * Copyright 2003-2010 Novell, Inc.
7  * Copyright (C) 2012 Xamarin Inc
8  *
9  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10  */
11
12 #undef SERIAL_COPY_OBJECT
13 #undef SERIAL_COPY_OBJECT_FROM_OBJ
14
15 #if defined(SGEN_SIMPLE_NURSERY)
16
17 #ifdef SGEN_SIMPLE_PAR_NURSERY
18 /* Not supported with concurrent major yet */
19 #define SERIAL_COPY_OBJECT simple_par_nursery_copy_object
20 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_par_nursery_copy_object_from_obj
21 #else
22 #ifdef SGEN_CONCURRENT_MAJOR
23 #define SERIAL_COPY_OBJECT simple_nursery_serial_with_concurrent_major_copy_object
24 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_nursery_serial_with_concurrent_major_copy_object_from_obj
25 #else
26 #define SERIAL_COPY_OBJECT simple_nursery_serial_copy_object
27 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_nursery_serial_copy_object_from_obj
28 #endif
29 #endif
30
31 #elif defined (SGEN_SPLIT_NURSERY)
32
33 #ifdef SGEN_CONCURRENT_MAJOR
34 #define SERIAL_COPY_OBJECT split_nursery_serial_with_concurrent_major_copy_object
35 #define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_with_concurrent_major_copy_object_from_obj
36 #else
37 #define SERIAL_COPY_OBJECT split_nursery_serial_copy_object
38 #define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_copy_object_from_obj
39 #endif
40
41 #else
42 #error "No nursery configuration specified"
43 #endif
44
45
46 extern guint64 stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
47
48 /*
49  * This is how the copying happens from the nursery to the old generation.
50  * We assume that at this time all the pinned objects have been identified and
51  * marked as such.
52  * We run scan_object() for each pinned object so that each referenced
53  * objects if possible are copied. The new gray objects created can have
54  * scan_object() run on them right away, too.
55  * Then we run copy_object() for the precisely tracked roots. At this point
56  * all the roots are either gray or black. We run scan_object() on the gray
57  * objects until no more gray objects are created.
58  * At the end of the process we walk again the pinned list and we unmark
59  * the pinned flag. As we go we also create the list of free space for use
60  * in the next allocation runs.
61  *
62  * We need to remember objects from the old generation that point to the new one
63  * (or just addresses?).
64  *
65  * copy_object could be made into a macro once debugged (use inline for now).
66  */
67
68 static MONO_ALWAYS_INLINE void
69 SERIAL_COPY_OBJECT (GCObject **obj_slot, SgenGrayQueue *queue) 
70 {
71         GCObject *forwarded;
72         GCObject *copy;
73         GCObject *obj = *obj_slot;
74
75         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy from a %d generation collection", current_collection_generation);
76
77         HEAVY_STAT (++stat_copy_object_called_nursery);
78
79         if (!sgen_ptr_in_nursery (obj)) {
80                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
81                 return;
82         }
83
84         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
85
86         /*
87          * Before we can copy the object we must make sure that we are
88          * allowed to, i.e. that the object not pinned, not already
89          * forwarded or belongs to the nursery To Space.
90          */
91
92         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
93                 SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded),  "forwarded object %p has no gc descriptor", forwarded);
94                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
95                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
96                 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
97                 return;
98         }
99         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
100                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
101                 SGEN_LOG (9, " (pinned, no change)");
102                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
103                 return;
104         }
105
106 #ifndef SGEN_SIMPLE_NURSERY
107         if (sgen_nursery_is_to_space (obj)) {
108                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
109                 SGEN_LOG (9, " (tospace, no change)");
110                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
111                 return;
112         }
113 #endif
114
115         HEAVY_STAT (++stat_objects_copied_nursery);
116
117 #ifdef SGEN_SIMPLE_PAR_NURSERY
118         copy = copy_object_no_checks_par (obj, queue);
119 #else
120         copy = copy_object_no_checks (obj, queue);
121 #endif
122         SGEN_UPDATE_REFERENCE (obj_slot, copy);
123 }
124
125 /*
126  * SERIAL_COPY_OBJECT_FROM_OBJ:
127  *
128  *   Similar to SERIAL_COPY_OBJECT, but assumes that OBJ_SLOT is part of an object, so it handles global remsets as well.
129  */
130 static MONO_ALWAYS_INLINE void
131 SERIAL_COPY_OBJECT_FROM_OBJ (GCObject **obj_slot, SgenGrayQueue *queue)
132 {
133         GCObject *forwarded;
134         GCObject *obj = *obj_slot;
135         GCObject *copy;
136
137         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy-from-obj from a %d generation collection", current_collection_generation);
138
139         HEAVY_STAT (++stat_copy_object_called_nursery);
140
141         if (!sgen_ptr_in_nursery (obj)) {
142                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
143                 return;
144         }
145
146         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
147
148         /*
149          * Before we can copy the object we must make sure that we are
150          * allowed to, i.e. that the object not pinned, not already
151          * forwarded or belongs to the nursery To Space.
152          */
153
154         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
155                 SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded),  "forwarded object %p has no gc descriptor", forwarded);
156                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
157                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
158 #ifdef SGEN_CONCURRENT_MAJOR
159                 /* See comment on STORE_STORE_FENCE below. */
160                 STORE_STORE_FENCE;
161 #endif
162                 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
163 #ifndef SGEN_SIMPLE_NURSERY
164                 if (G_UNLIKELY (sgen_ptr_in_nursery (forwarded) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (forwarded)))
165                         sgen_add_to_global_remset (obj_slot, forwarded);
166 #endif
167                 return;
168         }
169         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
170                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
171                 SGEN_LOG (9, " (pinned, no change)");
172                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
173                 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
174                         sgen_add_to_global_remset (obj_slot, obj);
175                 return;
176         }
177
178 #ifndef SGEN_SIMPLE_NURSERY
179         if (sgen_nursery_is_to_space (obj)) {
180                 /* FIXME: all of these could just use `sgen_obj_get_descriptor_safe()` */
181                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
182                 SGEN_LOG (9, " (tospace, no change)");
183                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
184
185                 /*
186                  * FIXME:
187                  *
188                  * The card table scanning code sometimes clears cards
189                  * that have just been set for a global remset.  In
190                  * the split nursery the following situation can
191                  * occur:
192                  *
193                  * Let's say object A starts in card C but continues
194                  * into C+1.  Within A, at offset O there's a
195                  * reference to a new nursery object X.  A+O is in
196                  * card C+1.  Now card C is scanned, and as part of
197                  * it, object A.  The reference at A+O is processed by
198                  * copying X into nursery to-space at Y.  Since it's
199                  * still in the nursery, a global remset must be added
200                  * for A+O, so card C+1 is marked.  Now, however, card
201                  * C+1 is scanned, which means that it's cleared
202                  * first.  This wouldn't be terribly bad if reference
203                  * A+O were re-scanned and the global remset re-added,
204                  * but since the reference points to to-space, that
205                  * doesn't happen, and C+1 remains cleared: the remset
206                  * is lost.
207                  *
208                  * There's at least two ways to fix this.  The easy
209                  * one is to re-add the remset on the re-scan.  This
210                  * is that - the following two lines of code.
211                  *
212                  * The proper solution appears to be to first make a
213                  * copy of the cards before scanning a block, then to
214                  * clear all the cards and scan from the copy, so no
215                  * remsets will be overwritten.  Scanning objects at
216                  * most once would be the icing on the cake.
217                  */
218                 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
219                         sgen_add_to_global_remset (obj_slot, obj);
220
221                 return;
222         }
223 #endif
224
225         HEAVY_STAT (++stat_objects_copied_nursery);
226
227 #ifdef SGEN_SIMPLE_PAR_NURSERY
228         copy = copy_object_no_checks_par (obj, queue);
229 #else
230         copy = copy_object_no_checks (obj, queue);
231 #endif
232 #ifdef SGEN_CONCURRENT_MAJOR
233         /*
234          * If an object is evacuated to the major heap and a reference to it, from the major
235          * heap, updated, the concurrent major collector might follow that reference and
236          * scan the new major object.  To make sure the object contents are seen by the
237          * major collector we need this write barrier, so that the reference is seen after
238          * the object.
239          */
240         STORE_STORE_FENCE;
241 #endif
242         SGEN_UPDATE_REFERENCE (obj_slot, copy);
243 #ifndef SGEN_SIMPLE_NURSERY
244         if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
245                 sgen_add_to_global_remset (obj_slot, copy);
246 #else
247         /* copy_object_no_checks () can return obj on OOM */
248         if (G_UNLIKELY (obj == copy)) {
249                 if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
250                         sgen_add_to_global_remset (obj_slot, copy);
251         }
252 #endif
253 }