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 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "mono/sgen/sgen-gc.h"
17 #include "mono/sgen/sgen-pinning.h"
18 #include "mono/sgen/sgen-protocol.h"
19 #include "mono/sgen/sgen-pointer-queue.h"
20 #include "mono/sgen/sgen-client.h"
22 static SgenPointerQueue pin_queue;
23 static size_t last_num_pinned = 0;
25 #define PIN_HASH_SIZE 1024
26 static void *pin_hash_filter [PIN_HASH_SIZE];
29 sgen_init_pinning (void)
31 memset (pin_hash_filter, 0, sizeof (pin_hash_filter));
32 pin_queue.mem_type = INTERNAL_MEM_PIN_QUEUE;
36 sgen_finish_pinning (void)
38 last_num_pinned = pin_queue.next_slot;
39 sgen_pointer_queue_clear (&pin_queue);
43 sgen_pin_stage_ptr (void *ptr)
45 /*very simple multiplicative hash function, tons better than simple and'ng */
46 int hash_idx = ((mword)ptr * 1737350767) & (PIN_HASH_SIZE - 1);
47 if (pin_hash_filter [hash_idx] == ptr)
50 pin_hash_filter [hash_idx] = ptr;
52 sgen_pointer_queue_add (&pin_queue, ptr);
56 sgen_find_optimized_pin_queue_area (void *start, void *end, size_t *first_out, size_t *last_out)
58 size_t first = sgen_pointer_queue_search (&pin_queue, start);
59 size_t last = sgen_pointer_queue_search (&pin_queue, end);
60 SGEN_ASSERT (0, last == pin_queue.next_slot || pin_queue.data [last] >= end, "Pin queue search gone awry");
67 sgen_pinning_get_entry (size_t index)
69 SGEN_ASSERT (0, index <= pin_queue.next_slot, "Pin queue entry out of range");
70 return &pin_queue.data [index];
74 sgen_find_section_pin_queue_start_end (GCMemSection *section)
76 SGEN_LOG (6, "Pinning from section %p (%p-%p)", section, section->data, section->end_data);
78 sgen_find_optimized_pin_queue_area (section->data, section->end_data,
79 §ion->pin_queue_first_entry, §ion->pin_queue_last_entry);
81 SGEN_LOG (6, "Found %zd pinning addresses in section %p",
82 section->pin_queue_last_entry - section->pin_queue_first_entry, section);
85 /*This will setup the given section for the while pin queue. */
87 sgen_pinning_setup_section (GCMemSection *section)
89 section->pin_queue_first_entry = 0;
90 section->pin_queue_last_entry = pin_queue.next_slot;
94 sgen_pinning_trim_queue_to_section (GCMemSection *section)
96 SGEN_ASSERT (0, section->pin_queue_first_entry == 0, "Pin queue trimming assumes the whole pin queue is used by the nursery");
97 pin_queue.next_slot = section->pin_queue_last_entry;
101 * This is called when we've run out of memory during a major collection.
103 * After collecting potential pin entries and sorting the array, this is what it looks like:
105 * +--------------------+---------------------------------------------+--------------------+
106 * | major heap entries | nursery entries | major heap entries |
107 * +--------------------+---------------------------------------------+--------------------+
109 * Of course there might not be major heap entries before and/or after the nursery entries,
110 * depending on where the major heap sections are in the address space, and whether there
111 * were any potential pointers there.
113 * When we pin nursery objects, we compact the nursery part of the pin array, which leaves
114 * discarded entries after the ones that actually pointed to nursery objects:
116 * +--------------------+-----------------+---------------------------+--------------------+
117 * | major heap entries | nursery entries | discarded nursery entries | major heap entries |
118 * +--------------------+-----------------+---------------------------+--------------------+
120 * When, due to being out of memory, we late pin more objects, the pin array looks like
123 * +--------------------+-----------------+---------------------------+--------------------+--------------+
124 * | major heap entries | nursery entries | discarded nursery entries | major heap entries | late entries |
125 * +--------------------+-----------------+---------------------------+--------------------+--------------+
127 * This function gets rid of the discarded nursery entries by nulling them out. Note that
128 * we can late pin objects not only in the nursery but also in the major heap, which happens
129 * when evacuation fails.
132 sgen_pin_queue_clear_discarded_entries (GCMemSection *section, size_t max_pin_slot)
134 void **start = sgen_pinning_get_entry (section->pin_queue_last_entry);
135 void **end = sgen_pinning_get_entry (max_pin_slot);
138 for (; start < end; ++start) {
140 if ((char*)addr < section->data || (char*)addr > section->end_data)
146 /* reduce the info in the pin queue, removing duplicate pointers and sorting them */
148 sgen_optimize_pin_queue (void)
150 sgen_pointer_queue_sort_uniq (&pin_queue);
154 sgen_get_pinned_count (void)
156 return pin_queue.next_slot;
160 sgen_dump_pin_queue (void)
164 for (i = 0; i < last_num_pinned; ++i) {
165 GCObject *ptr = (GCObject *)pin_queue.data [i];
166 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));
170 typedef struct _CementHashEntry CementHashEntry;
171 struct _CementHashEntry {
176 static CementHashEntry cement_hash [SGEN_CEMENT_HASH_SIZE];
178 static gboolean cement_enabled = TRUE;
181 sgen_cement_init (gboolean enabled)
183 cement_enabled = enabled;
187 sgen_cement_reset (void)
189 memset (cement_hash, 0, sizeof (cement_hash));
190 binary_protocol_cement_reset ();
194 sgen_cement_lookup (GCObject *obj)
196 guint hv = sgen_aligned_addr_hash (obj);
197 int i = SGEN_CEMENT_HASH (hv);
199 SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Looking up cementing for non-nursery objects makes no sense");
204 if (!cement_hash [i].obj)
206 if (cement_hash [i].obj != obj)
209 return cement_hash [i].count >= SGEN_CEMENT_THRESHOLD;
213 sgen_cement_lookup_or_register (GCObject *obj)
217 CementHashEntry *hash = cement_hash;
222 hv = sgen_aligned_addr_hash (obj);
223 i = SGEN_CEMENT_HASH (hv);
225 SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Can only cement pointers to nursery objects");
228 SGEN_ASSERT (5, !hash [i].count, "Cementing hash inconsistent");
230 } else if (hash [i].obj != obj) {
234 if (hash [i].count >= SGEN_CEMENT_THRESHOLD)
238 if (hash [i].count == SGEN_CEMENT_THRESHOLD) {
239 SGEN_ASSERT (9, sgen_get_current_collection_generation () >= 0, "We can only cement objects when we're in a collection pause.");
240 SGEN_ASSERT (9, SGEN_OBJECT_IS_PINNED (obj), "Can only cement pinned objects");
241 SGEN_CEMENT_OBJECT (obj);
243 binary_protocol_cement (obj, (gpointer)SGEN_LOAD_VTABLE (obj),
244 (int)sgen_safe_object_get_size (obj));
251 pin_from_hash (CementHashEntry *hash, gboolean has_been_reset)
254 for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
259 SGEN_ASSERT (5, hash [i].count >= SGEN_CEMENT_THRESHOLD, "Cementing hash inconsistent");
261 sgen_pin_stage_ptr (hash [i].obj);
262 binary_protocol_cement_stage (hash [i].obj);
263 /* FIXME: do pin stats if enabled */
265 SGEN_CEMENT_OBJECT (hash [i].obj);
270 sgen_pin_cemented_objects (void)
272 pin_from_hash (cement_hash, TRUE);
276 sgen_cement_clear_below_threshold (void)
279 for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
280 if (cement_hash [i].count < SGEN_CEMENT_THRESHOLD) {
281 cement_hash [i].obj = NULL;
282 cement_hash [i].count = 0;
287 #endif /* HAVE_SGEN_GC */