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