Merge pull request #1266 from esdrubal/datetimenewformat
[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 }
41
42 void
43 sgen_finish_pinning (void)
44 {
45         last_num_pinned = pin_queue.next_slot;
46         sgen_pointer_queue_clear (&pin_queue);
47 }
48
49 void
50 sgen_pin_stage_ptr (void *ptr)
51 {
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)
55                 return;
56
57         pin_hash_filter [hash_idx] = ptr;
58
59         sgen_pointer_queue_add (&pin_queue, ptr);
60 }
61
62 void**
63 sgen_find_optimized_pin_queue_area (void *start, void *end, size_t *num)
64 {
65         size_t first, last;
66         first = sgen_pointer_queue_search (&pin_queue, start);
67         last = sgen_pointer_queue_search (&pin_queue, end);
68         *num = last - first;
69         if (first == last)
70                 return NULL;
71         return pin_queue.data + first;
72 }
73
74 void
75 sgen_find_section_pin_queue_start_end (GCMemSection *section)
76 {
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, &section->pin_queue_num_entries);
79         SGEN_LOG (6, "Found %zd pinning addresses in section %p", section->pin_queue_num_entries, section);
80 }
81
82 /*This will setup the given section for the while pin queue. */
83 void
84 sgen_pinning_setup_section (GCMemSection *section)
85 {
86         section->pin_queue_start = pin_queue.data;
87         section->pin_queue_num_entries = pin_queue.next_slot;
88 }
89
90 void
91 sgen_pinning_trim_queue_to_section (GCMemSection *section)
92 {
93         pin_queue.next_slot = section->pin_queue_num_entries;
94 }
95
96 void
97 sgen_pin_queue_clear_discarded_entries (GCMemSection *section, size_t max_pin_slot)
98 {
99         void **start = section->pin_queue_start + section->pin_queue_num_entries;
100         void **end = pin_queue.data + max_pin_slot;
101         void *addr;
102
103         if (!start)
104                 return;
105
106         for (; start < end; ++start) {
107                 addr = *start;
108                 if ((char*)addr < section->data || (char*)addr > section->end_data)
109                         break;
110                 *start = NULL;
111         }
112 }
113
114 /* reduce the info in the pin queue, removing duplicate pointers and sorting them */
115 void
116 sgen_optimize_pin_queue (void)
117 {
118         sgen_pointer_queue_sort_uniq (&pin_queue);
119 }
120
121 size_t
122 sgen_get_pinned_count (void)
123 {
124         return pin_queue.next_slot;
125 }
126
127 void
128 sgen_dump_pin_queue (void)
129 {
130         int i;
131
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));
135         }
136 }
137
138 typedef struct _CementHashEntry CementHashEntry;
139 struct _CementHashEntry {
140         char *obj;
141         unsigned int count;
142 };
143
144 static CementHashEntry cement_hash [SGEN_CEMENT_HASH_SIZE];
145 static CementHashEntry cement_hash_concurrent [SGEN_CEMENT_HASH_SIZE];
146
147 static gboolean cement_enabled = TRUE;
148 static gboolean cement_concurrent = FALSE;
149
150 void
151 sgen_cement_init (gboolean enabled)
152 {
153         cement_enabled = enabled;
154 }
155
156 void
157 sgen_cement_reset (void)
158 {
159         SGEN_ASSERT (1, !cement_concurrent, "Concurrent cementing cannot simply be reset");
160
161         memset (cement_hash, 0, sizeof (cement_hash));
162         binary_protocol_cement_reset ();
163 }
164
165 /*
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.
172  *
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.
181  */
182
183 void
184 sgen_cement_concurrent_start (void)
185 {
186         SGEN_ASSERT (1, !cement_concurrent, "Concurrent cementing has already been started");
187         cement_concurrent = TRUE;
188
189         memset (cement_hash_concurrent, 0, sizeof (cement_hash));
190 }
191
192 void
193 sgen_cement_concurrent_finish (void)
194 {
195         SGEN_ASSERT (1, cement_concurrent, "Concurrent cementing hasn't been started");
196         cement_concurrent = FALSE;
197
198         memcpy (cement_hash, cement_hash_concurrent, sizeof (cement_hash));
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;
226         gboolean concurrent_cementing = sgen_concurrent_collection_in_progress ();
227
228         if (!cement_enabled)
229                 return FALSE;
230
231         if (concurrent_cementing)
232                 SGEN_ASSERT (5, cement_concurrent, "Cementing wasn't inited with concurrent flag");
233
234         if (concurrent_cementing)
235                 hash = cement_hash_concurrent;
236         else
237                 hash = cement_hash;
238
239         hv = mono_aligned_addr_hash (obj);
240         i = SGEN_CEMENT_HASH (hv);
241
242         SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Can only cement pointers to nursery objects");
243
244         if (!hash [i].obj) {
245                 SGEN_ASSERT (5, !hash [i].count, "Cementing hash inconsistent");
246                 hash [i].obj = obj;
247         } else if (hash [i].obj != obj) {
248                 return FALSE;
249         }
250
251         if (hash [i].count >= SGEN_CEMENT_THRESHOLD)
252                 return TRUE;
253
254         ++hash [i].count;
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);
260                 }
261                 binary_protocol_cement (obj, (gpointer)SGEN_LOAD_VTABLE (obj),
262                                 (int)sgen_safe_object_get_size ((MonoObject*)obj));
263         }
264
265         return FALSE;
266 }
267
268 void
269 sgen_cement_iterate (IterateObjectCallbackFunc callback, void *callback_data)
270 {
271         int i;
272         for (i = 0; i < SGEN_CEMENT_HASH_SIZE; ++i) {
273                 if (!cement_hash [i].count)
274                         continue;
275
276                 SGEN_ASSERT (5, cement_hash [i].count >= SGEN_CEMENT_THRESHOLD, "Cementing hash inconsistent");
277
278                 callback (cement_hash [i].obj, 0, callback_data);
279         }
280 }
281
282 void
283 sgen_cement_clear_below_threshold (void)
284 {
285         int i;
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;
290                 }
291         }
292 }
293
294 #endif /* HAVE_SGEN_GC */