a94af9ddf4494db961697faac801a9273bece46b
[mono.git] / mono / sgen / sgen-pinning.c
1 /*
2  * sgen-pinning.c: The pin queue.
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright (C) 2012 Xamarin Inc
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License 2.0 as published by the Free Software Foundation;
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License 2.0 along with this library; if not, write to the Free
19  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "config.h"
23 #ifdef HAVE_SGEN_GC
24
25 #include <string.h>
26
27 #include "mono/sgen/sgen-gc.h"
28 #include "mono/sgen/sgen-pinning.h"
29 #include "mono/sgen/sgen-protocol.h"
30 #include "mono/sgen/sgen-pointer-queue.h"
31 #include "mono/sgen/sgen-client.h"
32
33 static SgenPointerQueue pin_queue;
34 static size_t last_num_pinned = 0;
35
36 #define PIN_HASH_SIZE 1024
37 static void *pin_hash_filter [PIN_HASH_SIZE];
38
39 void
40 sgen_init_pinning (void)
41 {
42         memset (pin_hash_filter, 0, sizeof (pin_hash_filter));
43         pin_queue.mem_type = INTERNAL_MEM_PIN_QUEUE;
44 }
45
46 void
47 sgen_finish_pinning (void)
48 {
49         last_num_pinned = pin_queue.next_slot;
50         sgen_pointer_queue_clear (&pin_queue);
51 }
52
53 void
54 sgen_pin_stage_ptr (void *ptr)
55 {
56         /*very simple multiplicative hash function, tons better than simple and'ng */ 
57         int hash_idx = ((mword)ptr * 1737350767) & (PIN_HASH_SIZE - 1);
58         if (pin_hash_filter [hash_idx] == ptr)
59                 return;
60
61         pin_hash_filter [hash_idx] = ptr;
62
63         sgen_pointer_queue_add (&pin_queue, ptr);
64 }
65
66 gboolean
67 sgen_find_optimized_pin_queue_area (void *start, void *end, size_t *first_out, size_t *last_out)
68 {
69         size_t first = sgen_pointer_queue_search (&pin_queue, start);
70         size_t last = sgen_pointer_queue_search (&pin_queue, end);
71         SGEN_ASSERT (0, last == pin_queue.next_slot || pin_queue.data [last] >= end, "Pin queue search gone awry");
72         *first_out = first;
73         *last_out = last;
74         return first != last;
75 }
76
77 void**
78 sgen_pinning_get_entry (size_t index)
79 {
80         SGEN_ASSERT (0, index <= pin_queue.next_slot, "Pin queue entry out of range");
81         return &pin_queue.data [index];
82 }
83
84 void
85 sgen_find_section_pin_queue_start_end (GCMemSection *section)
86 {
87         SGEN_LOG (6, "Pinning from section %p (%p-%p)", section, section->data, section->end_data);
88
89         sgen_find_optimized_pin_queue_area (section->data, section->end_data,
90                         &section->pin_queue_first_entry, &section->pin_queue_last_entry);
91
92         SGEN_LOG (6, "Found %zd pinning addresses in section %p",
93                         section->pin_queue_last_entry - section->pin_queue_first_entry, section);
94 }
95
96 /*This will setup the given section for the while pin queue. */
97 void
98 sgen_pinning_setup_section (GCMemSection *section)
99 {
100         section->pin_queue_first_entry = 0;
101         section->pin_queue_last_entry = pin_queue.next_slot;
102 }
103
104 void
105 sgen_pinning_trim_queue_to_section (GCMemSection *section)
106 {
107         SGEN_ASSERT (0, section->pin_queue_first_entry == 0, "Pin queue trimming assumes the whole pin queue is used by the nursery");
108         pin_queue.next_slot = section->pin_queue_last_entry;
109 }
110
111 /*
112  * This is called when we've run out of memory during a major collection.
113  *
114  * After collecting potential pin entries and sorting the array, this is what it looks like:
115  *
116  * +--------------------+---------------------------------------------+--------------------+
117  * | major heap entries |               nursery entries               | major heap entries |
118  * +--------------------+---------------------------------------------+--------------------+
119  *
120  * Of course there might not be major heap entries before and/or after the nursery entries,
121  * depending on where the major heap sections are in the address space, and whether there
122  * were any potential pointers there.
123  *
124  * When we pin nursery objects, we compact the nursery part of the pin array, which leaves
125  * discarded entries after the ones that actually pointed to nursery objects:
126  *
127  * +--------------------+-----------------+---------------------------+--------------------+
128  * | major heap entries | nursery entries | discarded nursery entries | major heap entries |
129  * +--------------------+-----------------+---------------------------+--------------------+
130  *
131  * When, due to being out of memory, we late pin more objects, the pin array looks like
132  * this:
133  *
134  * +--------------------+-----------------+---------------------------+--------------------+--------------+
135  * | major heap entries | nursery entries | discarded nursery entries | major heap entries | late entries |
136  * +--------------------+-----------------+---------------------------+--------------------+--------------+
137  *
138  * This function gets rid of the discarded nursery entries by nulling them out.  Note that
139  * we can late pin objects not only in the nursery but also in the major heap, which happens
140  * when evacuation fails.
141  */
142 void
143 sgen_pin_queue_clear_discarded_entries (GCMemSection *section, size_t max_pin_slot)
144 {
145         void **start = sgen_pinning_get_entry (section->pin_queue_last_entry);
146         void **end = sgen_pinning_get_entry (max_pin_slot);
147         void *addr;
148
149         for (; start < end; ++start) {
150                 addr = *start;
151                 if ((char*)addr < section->data || (char*)addr > section->end_data)
152                         break;
153                 *start = NULL;
154         }
155 }
156
157 /* reduce the info in the pin queue, removing duplicate pointers and sorting them */
158 void
159 sgen_optimize_pin_queue (void)
160 {
161         sgen_pointer_queue_sort_uniq (&pin_queue);
162 }
163
164 size_t
165 sgen_get_pinned_count (void)
166 {
167         return pin_queue.next_slot;
168 }
169
170 void
171 sgen_dump_pin_queue (void)
172 {
173         int i;
174
175         for (i = 0; i < last_num_pinned; ++i) {
176                 GCObject *ptr = (GCObject *)pin_queue.data [i];
177                 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));
178         }
179 }
180
181 typedef struct _CementHashEntry CementHashEntry;
182 struct _CementHashEntry {
183         GCObject *obj;
184         unsigned int count;
185 };
186
187 static CementHashEntry cement_hash [SGEN_CEMENT_HASH_SIZE];
188
189 static gboolean cement_enabled = TRUE;
190
191 void
192 sgen_cement_init (gboolean enabled)
193 {
194         cement_enabled = enabled;
195 }
196
197 void
198 sgen_cement_reset (void)
199 {
200         memset (cement_hash, 0, sizeof (cement_hash));
201         binary_protocol_cement_reset ();
202 }
203
204 gboolean
205 sgen_cement_lookup (GCObject *obj)
206 {
207         guint hv = sgen_aligned_addr_hash (obj);
208         int i = SGEN_CEMENT_HASH (hv);
209
210         SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Looking up cementing for non-nursery objects makes no sense");
211
212         if (!cement_enabled)
213                 return FALSE;
214
215         if (!cement_hash [i].obj)
216                 return FALSE;
217         if (cement_hash [i].obj != obj)
218                 return FALSE;
219
220         return cement_hash [i].count >= SGEN_CEMENT_THRESHOLD;
221 }
222
223 gboolean
224 sgen_cement_lookup_or_register (GCObject *obj)
225 {
226         guint hv;
227         int i;
228         CementHashEntry *hash = cement_hash;
229
230         if (!cement_enabled)
231                 return FALSE;
232
233         hv = sgen_aligned_addr_hash (obj);
234         i = SGEN_CEMENT_HASH (hv);
235
236         SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Can only cement pointers to nursery objects");
237
238         if (!hash [i].obj) {
239                 SGEN_ASSERT (5, !hash [i].count, "Cementing hash inconsistent");
240                 hash [i].obj = obj;
241         } else if (hash [i].obj != obj) {
242                 return FALSE;
243         }
244
245         if (hash [i].count >= SGEN_CEMENT_THRESHOLD)
246                 return TRUE;
247
248         ++hash [i].count;
249         if (hash [i].count == SGEN_CEMENT_THRESHOLD) {
250                 SGEN_ASSERT (9, sgen_get_current_collection_generation () >= 0, "We can only cement objects when we're in a collection pause.");
251                 SGEN_ASSERT (9, SGEN_OBJECT_IS_PINNED (obj), "Can only cement pinned objects");
252                 SGEN_CEMENT_OBJECT (obj);
253
254                 binary_protocol_cement (obj, (gpointer)SGEN_LOAD_VTABLE (obj),
255                                 (int)sgen_safe_object_get_size (obj));
256         }
257
258         return FALSE;
259 }
260
261 static void
262 pin_from_hash (CementHashEntry *hash, gboolean has_been_reset)
263 {
264         int i;
265         for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
266                 if (!hash [i].count)
267                         continue;
268
269                 if (has_been_reset)
270                         SGEN_ASSERT (5, hash [i].count >= SGEN_CEMENT_THRESHOLD, "Cementing hash inconsistent");
271
272                 sgen_pin_stage_ptr (hash [i].obj);
273                 binary_protocol_cement_stage (hash [i].obj);
274                 /* FIXME: do pin stats if enabled */
275
276                 SGEN_CEMENT_OBJECT (hash [i].obj);
277         }
278 }
279
280 void
281 sgen_pin_cemented_objects (void)
282 {
283         pin_from_hash (cement_hash, TRUE);
284 }
285
286 void
287 sgen_cement_clear_below_threshold (void)
288 {
289         int i;
290         for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
291                 if (cement_hash [i].count < SGEN_CEMENT_THRESHOLD) {
292                         cement_hash [i].obj = NULL;
293                         cement_hash [i].count = 0;
294                 }
295         }
296 }
297
298 #endif /* HAVE_SGEN_GC */