2 * sgen-pinning.c: The pin queue.
4 * Copyright 2001-2003 Ximian, Inc
5 * Copyright 2003-2010 Novell, Inc.
6 * Copyright (C) 2012 Xamarin Inc
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;
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.
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.
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"
30 static SgenPointerQueue pin_queue;
31 static size_t last_num_pinned = 0;
33 #define PIN_HASH_SIZE 1024
34 static void *pin_hash_filter [PIN_HASH_SIZE];
37 sgen_init_pinning (void)
39 memset (pin_hash_filter, 0, sizeof (pin_hash_filter));
43 sgen_finish_pinning (void)
45 last_num_pinned = pin_queue.next_slot;
46 sgen_pointer_queue_clear (&pin_queue);
50 sgen_pin_stage_ptr (void *ptr)
52 /*very simple multiplicative hash function, tons better than simple and'ng */
53 int hash_idx = ((mword)ptr * 1737350767) & (PIN_HASH_SIZE - 1);
54 if (pin_hash_filter [hash_idx] == ptr)
57 pin_hash_filter [hash_idx] = ptr;
59 sgen_pointer_queue_add (&pin_queue, ptr);
63 sgen_find_optimized_pin_queue_area (void *start, void *end, size_t *num)
66 first = sgen_pointer_queue_search (&pin_queue, start);
67 last = sgen_pointer_queue_search (&pin_queue, end);
71 return pin_queue.data + first;
75 sgen_find_section_pin_queue_start_end (GCMemSection *section)
77 SGEN_LOG (6, "Pinning from section %p (%p-%p)", section, section->data, section->end_data);
78 section->pin_queue_start = sgen_find_optimized_pin_queue_area (section->data, section->end_data, §ion->pin_queue_num_entries);
79 SGEN_LOG (6, "Found %zd pinning addresses in section %p", section->pin_queue_num_entries, section);
82 /*This will setup the given section for the while pin queue. */
84 sgen_pinning_setup_section (GCMemSection *section)
86 section->pin_queue_start = pin_queue.data;
87 section->pin_queue_num_entries = pin_queue.next_slot;
91 sgen_pinning_trim_queue_to_section (GCMemSection *section)
93 pin_queue.next_slot = section->pin_queue_num_entries;
97 sgen_pin_queue_clear_discarded_entries (GCMemSection *section, size_t max_pin_slot)
99 void **start = section->pin_queue_start + section->pin_queue_num_entries;
100 void **end = pin_queue.data + max_pin_slot;
106 for (; start < end; ++start) {
108 if ((char*)addr < section->data || (char*)addr > section->end_data)
114 /* reduce the info in the pin queue, removing duplicate pointers and sorting them */
116 sgen_optimize_pin_queue (void)
118 sgen_pointer_queue_sort_uniq (&pin_queue);
122 sgen_get_pinned_count (void)
124 return pin_queue.next_slot;
128 sgen_dump_pin_queue (void)
132 for (i = 0; i < last_num_pinned; ++i) {
133 void *ptr = pin_queue.data [i];
134 SGEN_LOG (3, "Bastard pinning obj %p (%s), size: %zd", ptr, sgen_safe_name (ptr), sgen_safe_object_get_size (ptr));
138 typedef struct _CementHashEntry CementHashEntry;
139 struct _CementHashEntry {
144 static CementHashEntry cement_hash [SGEN_CEMENT_HASH_SIZE];
145 static CementHashEntry cement_hash_concurrent [SGEN_CEMENT_HASH_SIZE];
147 static gboolean cement_enabled = TRUE;
148 static gboolean cement_concurrent = FALSE;
151 sgen_cement_init (gboolean enabled)
153 cement_enabled = enabled;
157 sgen_cement_reset (void)
159 SGEN_ASSERT (1, !cement_concurrent, "Concurrent cementing cannot simply be reset");
161 memset (cement_hash, 0, sizeof (cement_hash));
162 binary_protocol_cement_reset ();
166 * The reason we cannot simply reset cementing at the start of a
167 * concurrent collection is that the nursery collections running
168 * concurrently must keep pinning the cemented objects, because we
169 * don't have the global remsets that point to them anymore - if the
170 * nursery collector moved the cemented objects, we'd have invalid
171 * pointers in the major heap.
173 * What we do instead is to reset cementing at the start of concurrent
174 * collections in such a way that nursery collections happening during
175 * the major collection still pin the formerly cemented objects. We
176 * have a shadow cementing table for that purpose. The nursery
177 * collections still work with the old cementing table, while the
178 * major collector builds up a new cementing table, adding global
179 * remsets whenever needed like usual. When the major collector
180 * finishes, the old cementing table is replaced by the new one.
184 sgen_cement_concurrent_start (void)
186 SGEN_ASSERT (1, !cement_concurrent, "Concurrent cementing has already been started");
187 cement_concurrent = TRUE;
189 memset (cement_hash_concurrent, 0, sizeof (cement_hash));
193 sgen_cement_concurrent_finish (void)
195 SGEN_ASSERT (1, cement_concurrent, "Concurrent cementing hasn't been started");
196 cement_concurrent = FALSE;
198 memcpy (cement_hash, cement_hash_concurrent, sizeof (cement_hash));
202 sgen_cement_lookup (char *obj)
204 guint hv = mono_aligned_addr_hash (obj);
205 int i = SGEN_CEMENT_HASH (hv);
207 SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Looking up cementing for non-nursery objects makes no sense");
212 if (!cement_hash [i].obj)
214 if (cement_hash [i].obj != obj)
217 return cement_hash [i].count >= SGEN_CEMENT_THRESHOLD;
221 sgen_cement_lookup_or_register (char *obj)
225 CementHashEntry *hash;
226 gboolean concurrent_cementing = sgen_concurrent_collection_in_progress ();
231 if (concurrent_cementing)
232 SGEN_ASSERT (5, cement_concurrent, "Cementing wasn't inited with concurrent flag");
234 if (concurrent_cementing)
235 hash = cement_hash_concurrent;
239 hv = mono_aligned_addr_hash (obj);
240 i = SGEN_CEMENT_HASH (hv);
242 SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Can only cement pointers to nursery objects");
245 SGEN_ASSERT (5, !hash [i].count, "Cementing hash inconsistent");
247 } else if (hash [i].obj != obj) {
251 if (hash [i].count >= SGEN_CEMENT_THRESHOLD)
255 if (hash [i].count == SGEN_CEMENT_THRESHOLD) {
256 if (G_UNLIKELY (MONO_GC_OBJ_CEMENTED_ENABLED())) {
257 MonoVTable *vt G_GNUC_UNUSED = (MonoVTable*)SGEN_LOAD_VTABLE (obj);
258 MONO_GC_OBJ_CEMENTED ((mword)obj, sgen_safe_object_get_size ((MonoObject*)obj),
259 vt->klass->name_space, vt->klass->name);
261 binary_protocol_cement (obj, (gpointer)SGEN_LOAD_VTABLE (obj),
262 (int)sgen_safe_object_get_size ((MonoObject*)obj));
269 sgen_cement_iterate (IterateObjectCallbackFunc callback, void *callback_data)
272 for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
273 if (!cement_hash [i].count)
276 SGEN_ASSERT (5, cement_hash [i].count >= SGEN_CEMENT_THRESHOLD, "Cementing hash inconsistent");
278 callback (cement_hash [i].obj, 0, callback_data);
283 sgen_cement_clear_below_threshold (void)
286 for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
287 if (cement_hash [i].count < SGEN_CEMENT_THRESHOLD) {
288 cement_hash [i].obj = NULL;
289 cement_hash [i].count = 0;
294 #endif /* HAVE_SGEN_GC */