[sgen] Increase parallelization of minors
[mono.git] / mono / sgen / sgen-pinning.c
1 /**
2  * \file
3  * The pin queue.
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 #include "config.h"
13 #ifdef HAVE_SGEN_GC
14
15 #include <string.h>
16
17 #include "mono/sgen/sgen-gc.h"
18 #include "mono/sgen/sgen-pinning.h"
19 #include "mono/sgen/sgen-protocol.h"
20 #include "mono/sgen/sgen-pointer-queue.h"
21 #include "mono/sgen/sgen-client.h"
22
23 static SgenPointerQueue pin_queue;
24 static size_t last_num_pinned = 0;
25 /*
26  * While we hold the pin_queue_mutex, all objects in pin_queue_objs will
27  * stay pinned, which means they can't move, therefore they can be scanned.
28  */
29 static SgenPointerQueue pin_queue_objs;
30 static mono_mutex_t pin_queue_mutex;
31
32 #define PIN_HASH_SIZE 1024
33 static void *pin_hash_filter [PIN_HASH_SIZE];
34
35 void
36 sgen_pinning_init (void)
37 {
38         mono_os_mutex_init (&pin_queue_mutex);
39 }
40
41 void
42 sgen_init_pinning (void)
43 {
44         mono_os_mutex_lock (&pin_queue_mutex);
45         memset (pin_hash_filter, 0, sizeof (pin_hash_filter));
46         pin_queue.mem_type = INTERNAL_MEM_PIN_QUEUE;
47         sgen_pointer_queue_clear (&pin_queue_objs);
48 }
49
50 void
51 sgen_finish_pinning (void)
52 {
53         last_num_pinned = pin_queue.next_slot;
54         sgen_pointer_queue_clear (&pin_queue);
55         mono_os_mutex_unlock (&pin_queue_mutex);
56 }
57
58 void
59 sgen_pinning_register_pinned_in_nursery (GCObject *obj)
60 {
61         sgen_pointer_queue_add (&pin_queue_objs, obj);
62 }
63
64 void
65 sgen_scan_pin_queue_objects (ScanCopyContext ctx)
66 {
67         int i;
68         ScanObjectFunc scan_func = ctx.ops->scan_object;
69
70         mono_os_mutex_lock (&pin_queue_mutex);
71         for (i = 0; i < pin_queue_objs.next_slot; ++i) {
72                 GCObject *obj = (GCObject *)pin_queue_objs.data [i];
73                 scan_func (obj, sgen_obj_get_descriptor_safe (obj), ctx.queue);
74         }
75         mono_os_mutex_unlock (&pin_queue_mutex);
76 }
77
78 void
79 sgen_pin_stage_ptr (void *ptr)
80 {
81         /*very simple multiplicative hash function, tons better than simple and'ng */ 
82         int hash_idx = ((mword)ptr * 1737350767) & (PIN_HASH_SIZE - 1);
83         if (pin_hash_filter [hash_idx] == ptr)
84                 return;
85
86         pin_hash_filter [hash_idx] = ptr;
87
88         sgen_pointer_queue_add (&pin_queue, ptr);
89 }
90
91 gboolean
92 sgen_find_optimized_pin_queue_area (void *start, void *end, size_t *first_out, size_t *last_out)
93 {
94         size_t first = sgen_pointer_queue_search (&pin_queue, start);
95         size_t last = sgen_pointer_queue_search (&pin_queue, end);
96         SGEN_ASSERT (0, last == pin_queue.next_slot || pin_queue.data [last] >= end, "Pin queue search gone awry");
97         *first_out = first;
98         *last_out = last;
99         return first != last;
100 }
101
102 void**
103 sgen_pinning_get_entry (size_t index)
104 {
105         SGEN_ASSERT (0, index <= pin_queue.next_slot, "Pin queue entry out of range");
106         return &pin_queue.data [index];
107 }
108
109 void
110 sgen_find_section_pin_queue_start_end (GCMemSection *section)
111 {
112         SGEN_LOG (6, "Pinning from section %p (%p-%p)", section, section->data, section->end_data);
113
114         sgen_find_optimized_pin_queue_area (section->data, section->end_data,
115                         &section->pin_queue_first_entry, &section->pin_queue_last_entry);
116
117         SGEN_LOG (6, "Found %zd pinning addresses in section %p",
118                         section->pin_queue_last_entry - section->pin_queue_first_entry, section);
119 }
120
121 /*This will setup the given section for the while pin queue. */
122 void
123 sgen_pinning_setup_section (GCMemSection *section)
124 {
125         section->pin_queue_first_entry = 0;
126         section->pin_queue_last_entry = pin_queue.next_slot;
127 }
128
129 void
130 sgen_pinning_trim_queue_to_section (GCMemSection *section)
131 {
132         SGEN_ASSERT (0, section->pin_queue_first_entry == 0, "Pin queue trimming assumes the whole pin queue is used by the nursery");
133         pin_queue.next_slot = section->pin_queue_last_entry;
134 }
135
136 /*
137  * This is called when we've run out of memory during a major collection.
138  *
139  * After collecting potential pin entries and sorting the array, this is what it looks like:
140  *
141  * +--------------------+---------------------------------------------+--------------------+
142  * | major heap entries |               nursery entries               | major heap entries |
143  * +--------------------+---------------------------------------------+--------------------+
144  *
145  * Of course there might not be major heap entries before and/or after the nursery entries,
146  * depending on where the major heap sections are in the address space, and whether there
147  * were any potential pointers there.
148  *
149  * When we pin nursery objects, we compact the nursery part of the pin array, which leaves
150  * discarded entries after the ones that actually pointed to nursery objects:
151  *
152  * +--------------------+-----------------+---------------------------+--------------------+
153  * | major heap entries | nursery entries | discarded nursery entries | major heap entries |
154  * +--------------------+-----------------+---------------------------+--------------------+
155  *
156  * When, due to being out of memory, we late pin more objects, the pin array looks like
157  * this:
158  *
159  * +--------------------+-----------------+---------------------------+--------------------+--------------+
160  * | major heap entries | nursery entries | discarded nursery entries | major heap entries | late entries |
161  * +--------------------+-----------------+---------------------------+--------------------+--------------+
162  *
163  * This function gets rid of the discarded nursery entries by nulling them out.  Note that
164  * we can late pin objects not only in the nursery but also in the major heap, which happens
165  * when evacuation fails.
166  */
167 void
168 sgen_pin_queue_clear_discarded_entries (GCMemSection *section, size_t max_pin_slot)
169 {
170         void **start = sgen_pinning_get_entry (section->pin_queue_last_entry);
171         void **end = sgen_pinning_get_entry (max_pin_slot);
172         void *addr;
173
174         for (; start < end; ++start) {
175                 addr = *start;
176                 if ((char*)addr < section->data || (char*)addr > section->end_data)
177                         break;
178                 *start = NULL;
179         }
180 }
181
182 /* reduce the info in the pin queue, removing duplicate pointers and sorting them */
183 void
184 sgen_optimize_pin_queue (void)
185 {
186         sgen_pointer_queue_sort_uniq (&pin_queue);
187 }
188
189 size_t
190 sgen_get_pinned_count (void)
191 {
192         return pin_queue.next_slot;
193 }
194
195 void
196 sgen_dump_pin_queue (void)
197 {
198         int i;
199
200         for (i = 0; i < last_num_pinned; ++i) {
201                 GCObject *ptr = (GCObject *)pin_queue.data [i];
202                 SGEN_LOG (3, "Bastard pinning obj %p (%s), size: %zd", ptr, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (ptr)), sgen_safe_object_get_size (ptr));
203         }
204 }
205
206 typedef struct _CementHashEntry CementHashEntry;
207 struct _CementHashEntry {
208         GCObject *obj;
209         unsigned int count;
210         gboolean forced; /* if it should stay cemented after the finishing pause */
211 };
212
213 static CementHashEntry cement_hash [SGEN_CEMENT_HASH_SIZE];
214
215 static gboolean cement_enabled = TRUE;
216
217 void
218 sgen_cement_init (gboolean enabled)
219 {
220         cement_enabled = enabled;
221 }
222
223 void
224 sgen_cement_reset (void)
225 {
226         int i;
227         for (i = 0; i < SGEN_CEMENT_HASH_SIZE; i++) {
228                 if (cement_hash [i].forced) {
229                         cement_hash [i].forced = FALSE;
230                 } else {
231                         cement_hash [i].obj = NULL;
232                         cement_hash [i].count = 0;
233                 }
234         }
235         binary_protocol_cement_reset ();
236 }
237
238
239 /*
240  * The pin_queue should be full and sorted, without entries from the cemented
241  * objects. We traverse the cement hash and check if each object is pinned in
242  * the pin_queue (the pin_queue contains entries between obj and obj+obj_len)
243  */
244 void
245 sgen_cement_force_pinned (void)
246 {
247         int i;
248
249         if (!cement_enabled)
250                 return;
251
252         for (i = 0; i < SGEN_CEMENT_HASH_SIZE; i++) {
253                 GCObject *obj = cement_hash [i].obj;
254                 size_t index;
255                 if (!obj)
256                         continue;
257                 if (cement_hash [i].count < SGEN_CEMENT_THRESHOLD)
258                         continue;
259                 SGEN_ASSERT (0, !cement_hash [i].forced, "Why do we have a forced cemented object before forcing ?");
260
261                 /* Returns the index of the target or of the first element greater than it */
262                 index = sgen_pointer_queue_search (&pin_queue, obj);
263                 if (index == pin_queue.next_slot)
264                         continue;
265                 SGEN_ASSERT (0, pin_queue.data [index] >= (gpointer)obj, "Binary search should return a pointer greater than the search target");
266                 if (pin_queue.data [index] < (gpointer)((char*)obj + sgen_safe_object_get_size (obj)))
267                         cement_hash [i].forced = TRUE;
268         }
269 }
270
271 gboolean
272 sgen_cement_is_forced (GCObject *obj)
273 {
274         guint hv = sgen_aligned_addr_hash (obj);
275         int i = SGEN_CEMENT_HASH (hv);
276
277         SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Looking up cementing for non-nursery objects makes no sense");
278
279         if (!cement_enabled)
280                 return FALSE;
281
282         if (!cement_hash [i].obj)
283                 return FALSE;
284         if (cement_hash [i].obj != obj)
285                 return FALSE;
286
287         return cement_hash [i].forced;
288 }
289
290 gboolean
291 sgen_cement_lookup (GCObject *obj)
292 {
293         guint hv = sgen_aligned_addr_hash (obj);
294         int i = SGEN_CEMENT_HASH (hv);
295
296         SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Looking up cementing for non-nursery objects makes no sense");
297
298         if (!cement_enabled)
299                 return FALSE;
300
301         if (!cement_hash [i].obj)
302                 return FALSE;
303         if (cement_hash [i].obj != obj)
304                 return FALSE;
305
306         return cement_hash [i].count >= SGEN_CEMENT_THRESHOLD;
307 }
308
309 gboolean
310 sgen_cement_lookup_or_register (GCObject *obj)
311 {
312         guint hv;
313         int i;
314         CementHashEntry *hash = cement_hash;
315
316         if (!cement_enabled)
317                 return FALSE;
318
319         hv = sgen_aligned_addr_hash (obj);
320         i = SGEN_CEMENT_HASH (hv);
321
322         SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Can only cement pointers to nursery objects");
323
324         if (!hash [i].obj) {
325                 GCObject *old_obj;
326                 old_obj = InterlockedCompareExchangePointer ((gpointer*)&hash [i].obj, obj, NULL);
327                 /* Check if the slot was occupied by some other object */
328                 if (old_obj != NULL && old_obj != obj)
329                         return FALSE;
330         } else if (hash [i].obj != obj) {
331                 return FALSE;
332         }
333
334         if (hash [i].count >= SGEN_CEMENT_THRESHOLD)
335                 return TRUE;
336
337         if (InterlockedIncrement ((gint32*)&hash [i].count) == SGEN_CEMENT_THRESHOLD) {
338                 SGEN_ASSERT (9, sgen_get_current_collection_generation () >= 0, "We can only cement objects when we're in a collection pause.");
339                 SGEN_ASSERT (9, SGEN_OBJECT_IS_PINNED (obj), "Can only cement pinned objects");
340                 SGEN_CEMENT_OBJECT (obj);
341
342                 binary_protocol_cement (obj, (gpointer)SGEN_LOAD_VTABLE (obj),
343                                 (int)sgen_safe_object_get_size (obj));
344         }
345
346         return FALSE;
347 }
348
349 static void
350 pin_from_hash (CementHashEntry *hash, gboolean has_been_reset)
351 {
352         int i;
353         for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
354                 if (!hash [i].count)
355                         continue;
356
357                 if (has_been_reset)
358                         SGEN_ASSERT (5, hash [i].count >= SGEN_CEMENT_THRESHOLD, "Cementing hash inconsistent");
359
360                 sgen_pin_stage_ptr (hash [i].obj);
361                 binary_protocol_cement_stage (hash [i].obj);
362                 /* FIXME: do pin stats if enabled */
363
364                 SGEN_CEMENT_OBJECT (hash [i].obj);
365         }
366 }
367
368 void
369 sgen_pin_cemented_objects (void)
370 {
371         pin_from_hash (cement_hash, TRUE);
372 }
373
374 void
375 sgen_cement_clear_below_threshold (void)
376 {
377         int i;
378         for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
379                 if (cement_hash [i].count < SGEN_CEMENT_THRESHOLD) {
380                         cement_hash [i].obj = NULL;
381                         cement_hash [i].count = 0;
382                 }
383         }
384 }
385
386 #endif /* HAVE_SGEN_GC */