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.
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"
33 static SgenPointerQueue pin_queue;
34 static size_t last_num_pinned = 0;
36 #define PIN_HASH_SIZE 1024
37 static void *pin_hash_filter [PIN_HASH_SIZE];
40 sgen_init_pinning (void)
42 memset (pin_hash_filter, 0, sizeof (pin_hash_filter));
43 pin_queue.mem_type = INTERNAL_MEM_PIN_QUEUE;
47 sgen_finish_pinning (void)
49 last_num_pinned = pin_queue.next_slot;
50 sgen_pointer_queue_clear (&pin_queue);
54 sgen_pin_stage_ptr (void *ptr)
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)
61 pin_hash_filter [hash_idx] = ptr;
63 sgen_pointer_queue_add (&pin_queue, ptr);
67 sgen_find_optimized_pin_queue_area (void *start, void *end, size_t *first_out, size_t *last_out)
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");
78 sgen_pinning_get_entry (size_t index)
80 SGEN_ASSERT (0, index <= pin_queue.next_slot, "Pin queue entry out of range");
81 return &pin_queue.data [index];
85 sgen_find_section_pin_queue_start_end (GCMemSection *section)
87 SGEN_LOG (6, "Pinning from section %p (%p-%p)", section, section->data, section->end_data);
89 sgen_find_optimized_pin_queue_area (section->data, section->end_data,
90 §ion->pin_queue_first_entry, §ion->pin_queue_last_entry);
92 SGEN_LOG (6, "Found %zd pinning addresses in section %p",
93 section->pin_queue_last_entry - section->pin_queue_first_entry, section);
96 /*This will setup the given section for the while pin queue. */
98 sgen_pinning_setup_section (GCMemSection *section)
100 section->pin_queue_first_entry = 0;
101 section->pin_queue_last_entry = pin_queue.next_slot;
105 sgen_pinning_trim_queue_to_section (GCMemSection *section)
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;
112 * This is called when we've run out of memory during a major collection.
114 * After collecting potential pin entries and sorting the array, this is what it looks like:
116 * +--------------------+---------------------------------------------+--------------------+
117 * | major heap entries | nursery entries | major heap entries |
118 * +--------------------+---------------------------------------------+--------------------+
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.
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:
127 * +--------------------+-----------------+---------------------------+--------------------+
128 * | major heap entries | nursery entries | discarded nursery entries | major heap entries |
129 * +--------------------+-----------------+---------------------------+--------------------+
131 * When, due to being out of memory, we late pin more objects, the pin array looks like
134 * +--------------------+-----------------+---------------------------+--------------------+--------------+
135 * | major heap entries | nursery entries | discarded nursery entries | major heap entries | late entries |
136 * +--------------------+-----------------+---------------------------+--------------------+--------------+
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.
143 sgen_pin_queue_clear_discarded_entries (GCMemSection *section, size_t max_pin_slot)
145 void **start = sgen_pinning_get_entry (section->pin_queue_last_entry);
146 void **end = sgen_pinning_get_entry (max_pin_slot);
149 for (; start < end; ++start) {
151 if ((char*)addr < section->data || (char*)addr > section->end_data)
157 /* reduce the info in the pin queue, removing duplicate pointers and sorting them */
159 sgen_optimize_pin_queue (void)
161 sgen_pointer_queue_sort_uniq (&pin_queue);
165 sgen_get_pinned_count (void)
167 return pin_queue.next_slot;
171 sgen_dump_pin_queue (void)
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));
181 typedef struct _CementHashEntry CementHashEntry;
182 struct _CementHashEntry {
187 static CementHashEntry cement_hash [SGEN_CEMENT_HASH_SIZE];
189 static gboolean cement_enabled = TRUE;
192 sgen_cement_init (gboolean enabled)
194 cement_enabled = enabled;
198 sgen_cement_reset (void)
200 memset (cement_hash, 0, sizeof (cement_hash));
201 binary_protocol_cement_reset ();
205 sgen_cement_lookup (GCObject *obj)
207 guint hv = sgen_aligned_addr_hash (obj);
208 int i = SGEN_CEMENT_HASH (hv);
210 SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Looking up cementing for non-nursery objects makes no sense");
215 if (!cement_hash [i].obj)
217 if (cement_hash [i].obj != obj)
220 return cement_hash [i].count >= SGEN_CEMENT_THRESHOLD;
224 sgen_cement_lookup_or_register (GCObject *obj)
228 CementHashEntry *hash = cement_hash;
233 hv = sgen_aligned_addr_hash (obj);
234 i = SGEN_CEMENT_HASH (hv);
236 SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Can only cement pointers to nursery objects");
239 SGEN_ASSERT (5, !hash [i].count, "Cementing hash inconsistent");
241 } else if (hash [i].obj != obj) {
245 if (hash [i].count >= SGEN_CEMENT_THRESHOLD)
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);
254 binary_protocol_cement (obj, (gpointer)SGEN_LOAD_VTABLE (obj),
255 (int)sgen_safe_object_get_size (obj));
262 pin_from_hash (CementHashEntry *hash, gboolean has_been_reset)
265 for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
270 SGEN_ASSERT (5, hash [i].count >= SGEN_CEMENT_THRESHOLD, "Cementing hash inconsistent");
272 sgen_pin_stage_ptr (hash [i].obj);
273 binary_protocol_cement_stage (hash [i].obj);
274 /* FIXME: do pin stats if enabled */
276 SGEN_CEMENT_OBJECT (hash [i].obj);
281 sgen_pin_cemented_objects (void)
283 pin_from_hash (cement_hash, TRUE);
287 sgen_cement_clear_below_threshold (void)
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;
298 #endif /* HAVE_SGEN_GC */