[interp] Fix resuming from finally block (#8195)
[mono-project.git] / mono / sgen / sgen-split-nursery.c
blob28c7e58981a9bc1609b17495cd5edc652f5b2be6
1 /**
2 * \file
3 * 3-space based nursery collector.
5 * Author:
6 * Rodrigo Kumpera Kumpera <kumpera@gmail.com>
8 * Copyright 2001-2003 Ximian, Inc
9 * Copyright 2003-2010 Novell, Inc.
10 * Copyright 2011-2012 Xamarin Inc (http://www.xamarin.com)
11 * Copyright (C) 2012 Xamarin Inc
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "config.h"
17 #ifdef HAVE_SGEN_GC
19 #ifndef DISABLE_SGEN_SPLIT_NURSERY
21 #include <string.h>
22 #include <stdlib.h>
24 #include "mono/sgen/sgen-gc.h"
25 #include "mono/sgen/sgen-protocol.h"
26 #include "mono/sgen/sgen-layout-stats.h"
27 #include "mono/sgen/sgen-client.h"
28 #include "mono/utils/mono-memory-model.h"
31 The nursery is logically divided into 3 spaces: Allocator space and two Survivor spaces.
33 Objects are born (allocated by the mutator) in the Allocator Space.
35 The Survivor spaces are divided in a copying collector style From and To spaces.
36 The hole of each space switch on each collection.
38 On each collection we process objects from the nursery this way:
39 Objects from the Allocator Space are evacuated into the To Space.
40 Objects from the Survivor From Space are evacuated into the old generation.
43 The nursery is physically divided in two parts, set by the promotion barrier.
45 The Allocator Space takes the botton part of the nursery.
47 The Survivor spaces are intermingled in the top part of the nursery. It's done
48 this way since the required size for the To Space depends on the survivor rate
49 of objects from the Allocator Space.
51 During a collection when the object scan function see a nursery object it must
52 determine if the object needs to be evacuated or left in place. Originally, this
53 check was done by checking if a forwarding pointer is installed, but now an object
54 can be in the To Space, it won't have a forwarding pointer and it must be left in place.
56 In order to solve that we classify nursery memory been either in the From Space or in
57 the To Space. Since the Allocator Space has the same behavior as the Survivor From Space
58 they are unified for this purpoise - a bit confusing at first.
60 This from/to classification is done on a larger granule than object to make the check efficient
61 and, due to that, we must make sure that all fragemnts used to allocate memory from the To Space
62 are naturally aligned in both ends to that granule to avoid wronly classifying a From Space object.
64 TODO:
65 -The promotion barrier is statically defined to 50% of the nursery, it should be dinamically adjusted based
66 on survival rates;
67 -We apply the same promotion policy to all objects, finalizable ones should age longer in the nursery;
68 -We apply the same promotion policy to all stages of a collection, maybe we should promote more aggressively
69 objects from non-stack roots, specially those found in the remembered set;
70 -Fix our major collection trigger to happen before we do a minor GC and collect the nursery only once.
71 -Make the serial fragment allocator fast path inlineable
72 -Make aging threshold be based on survival rates and survivor occupancy;
73 -Change promotion barrier to be size and not address based;
74 -Pre allocate memory for young ages to make sure that on overflow only the older suffer;
75 -Get rid of par_alloc_buffer_refill_mutex so to the parallel collection of the nursery doesn't suck;
78 /*FIXME Move this to a separate header. */
79 #define _toi(ptr) ((size_t)ptr)
80 #define make_ptr_mask(bits) ((1 << bits) - 1)
81 #define align_down(ptr, bits) ((void*)(_toi(ptr) & ~make_ptr_mask (bits)))
82 #define align_up(ptr, bits) ((void*) ((_toi(ptr) + make_ptr_mask (bits)) & ~make_ptr_mask (bits)))
85 Even though the effective max age is 255, aging that much doesn't make sense.
86 It might even make sense to use nimbles for age recording.
88 #define MAX_AGE 15
91 * Each age has its allocation buffer. Whenever an object is to be
92 * aged we try to fit it into its new age's allocation buffer. If
93 * that is not possible we get new space from the fragment allocator
94 * and set the allocation buffer to that space (minus the space
95 * required for the object).
98 typedef struct {
99 char *next;
100 char *end;
101 } AgeAllocationBuffer;
103 /* Limits the ammount of memory the mutator can have. */
104 static char *promotion_barrier;
107 Promotion age and alloc ratio are the two nursery knobs to control
108 how much effort we want to spend on young objects.
110 Allocation ratio should be the inverse of the expected survivor rate.
111 The more objects surviver, the smaller the alloc ratio much be so we can
112 age all objects.
114 Promote age depends on how much effort we want to spend aging objects before
115 we promote them to the old generation. If addional ages don't somewhat improve
116 mortality, it's better avoid as they increase the cost of minor collections.
122 If we're evacuating an object with this age or more, promote it.
123 Age is the number of surviving collections of an object.
125 static int promote_age = 2;
128 Initial ratio of allocation and survivor spaces.
129 This should be read as the fraction of the whole nursery dedicated
130 for the allocator space.
132 static float alloc_ratio = 60.f/100.f;
135 static char *region_age;
136 static size_t region_age_size;
137 static AgeAllocationBuffer age_alloc_buffers [MAX_AGE];
139 /* The collector allocs from here. */
140 static SgenFragmentAllocator collector_allocator;
142 static inline int
143 get_object_age (GCObject *object)
145 size_t idx = ((char*)object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
146 return region_age [idx];
149 static void
150 set_age_in_range (char *start, char *end, int age)
152 char *region_start;
153 size_t region_idx, length;
154 region_idx = (start - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
155 region_start = &region_age [region_idx];
156 length = (end - start) >> SGEN_TO_SPACE_GRANULE_BITS;
157 memset (region_start, age, length);
160 static inline void
161 mark_bit (char *space_bitmap, char *pos)
163 size_t idx = (pos - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
164 size_t byte = idx / 8;
165 int bit = idx & 0x7;
167 g_assert (byte < sgen_space_bitmap_size);
168 space_bitmap [byte] |= 1 << bit;
171 static void
172 mark_bits_in_range (char *space_bitmap, char *start, char *end)
174 start = (char *)align_down (start, SGEN_TO_SPACE_GRANULE_BITS);
175 end = (char *)align_up (end, SGEN_TO_SPACE_GRANULE_BITS);
177 for (;start < end; start += SGEN_TO_SPACE_GRANULE_IN_BYTES)
178 mark_bit (space_bitmap, start);
182 * This splits the fragments at the point of the promotion barrier.
183 * Two allocator are actually involved here: The mutator allocator and
184 * the collector allocator. This function is called with the
185 * collector, but it's a copy of the mutator allocator and contains
186 * all the fragments in the nursery. The fragments below the
187 * promotion barrier are left with the mutator allocator and the ones
188 * above are put into the collector allocator.
190 static void
191 fragment_list_split (SgenFragmentAllocator *allocator)
193 SgenFragment *prev = NULL, *list = allocator->region_head;
195 while (list) {
196 if (list->fragment_end > promotion_barrier) {
197 if (list->fragment_start < promotion_barrier) {
198 SgenFragment *res = sgen_fragment_allocator_alloc ();
200 res->fragment_start = promotion_barrier;
201 res->fragment_next = promotion_barrier;
202 res->fragment_end = list->fragment_end;
203 res->next = list->next;
204 res->next_in_order = list->next_in_order;
205 g_assert (res->fragment_end > res->fragment_start);
207 list->fragment_end = promotion_barrier;
208 list->next = list->next_in_order = NULL;
209 set_age_in_range (list->fragment_start, list->fragment_end, 0);
211 allocator->region_head = allocator->alloc_head = res;
212 return;
213 } else {
214 if (prev)
215 prev->next = prev->next_in_order = NULL;
216 allocator->region_head = allocator->alloc_head = list;
217 return;
220 set_age_in_range (list->fragment_start, list->fragment_end, 0);
221 prev = list;
222 list = list->next;
224 allocator->region_head = allocator->alloc_head = NULL;
227 /******************************************Minor Collector API ************************************************/
229 #define AGE_ALLOC_BUFFER_MIN_SIZE SGEN_TO_SPACE_GRANULE_IN_BYTES
230 #define AGE_ALLOC_BUFFER_DESIRED_SIZE (SGEN_TO_SPACE_GRANULE_IN_BYTES * 8)
232 static char*
233 alloc_for_promotion_slow_path (int age, size_t objsize)
235 char *p;
236 size_t allocated_size;
237 size_t aligned_objsize = (size_t)align_up (objsize, SGEN_TO_SPACE_GRANULE_BITS);
239 p = (char *)sgen_fragment_allocator_serial_range_alloc (
240 &collector_allocator,
241 MAX (aligned_objsize, AGE_ALLOC_BUFFER_DESIRED_SIZE),
242 MAX (aligned_objsize, AGE_ALLOC_BUFFER_MIN_SIZE),
243 &allocated_size);
244 if (p) {
245 set_age_in_range (p, p + allocated_size, age);
246 sgen_clear_range (age_alloc_buffers [age].next, age_alloc_buffers [age].end);
247 age_alloc_buffers [age].next = p + objsize;
248 age_alloc_buffers [age].end = p + allocated_size;
250 return p;
253 static inline GCObject*
254 alloc_for_promotion (GCVTable vtable, GCObject *obj, size_t objsize, gboolean has_references)
256 char *p = NULL;
257 int age;
259 age = get_object_age (obj);
260 if (age >= promote_age) {
261 sgen_total_promoted_size += objsize;
262 return sgen_major_collector.alloc_object (vtable, objsize, has_references);
265 /* Promote! */
266 ++age;
268 p = age_alloc_buffers [age].next;
269 if (G_LIKELY (p + objsize <= age_alloc_buffers [age].end)) {
270 age_alloc_buffers [age].next += objsize;
271 } else {
272 p = alloc_for_promotion_slow_path (age, objsize);
273 if (!p) {
274 sgen_total_promoted_size += objsize;
275 return sgen_major_collector.alloc_object (vtable, objsize, has_references);
279 /* FIXME: assumes object layout */
280 *(GCVTable*)p = vtable;
282 return (GCObject*)p;
285 static GCObject*
286 minor_alloc_for_promotion (GCVTable vtable, GCObject *obj, size_t objsize, gboolean has_references)
289 We only need to check for a non-nursery object if we're doing a major collection.
291 if (!sgen_ptr_in_nursery (obj))
292 return sgen_major_collector.alloc_object (vtable, objsize, has_references);
294 return alloc_for_promotion (vtable, obj, objsize, has_references);
297 static SgenFragment*
298 build_fragments_get_exclude_head (void)
300 int i;
301 for (i = 0; i < MAX_AGE; ++i) {
302 /*If we OOM'd on the last collection ->end might be null while ->next not.*/
303 if (age_alloc_buffers [i].end)
304 sgen_clear_range (age_alloc_buffers [i].next, age_alloc_buffers [i].end);
307 return collector_allocator.region_head;
310 static void
311 build_fragments_release_exclude_head (void)
313 sgen_fragment_allocator_release (&collector_allocator);
316 static void
317 build_fragments_finish (SgenFragmentAllocator *allocator)
319 /* We split the fragment list based on the promotion barrier. */
320 collector_allocator = *allocator;
321 fragment_list_split (&collector_allocator);
324 static void
325 prepare_to_space (char *to_space_bitmap, size_t space_bitmap_size)
327 SgenFragment **previous, *frag;
329 memset (to_space_bitmap, 0, space_bitmap_size);
330 memset (age_alloc_buffers, 0, sizeof (age_alloc_buffers));
332 previous = &collector_allocator.alloc_head;
334 for (frag = *previous; frag; frag = *previous) {
335 char *start = (char *)align_up (frag->fragment_next, SGEN_TO_SPACE_GRANULE_BITS);
336 char *end = (char *)align_down (frag->fragment_end, SGEN_TO_SPACE_GRANULE_BITS);
338 /* Fragment is too small to be usable. */
339 if ((end - start) < SGEN_MAX_NURSERY_WASTE) {
340 sgen_clear_range (frag->fragment_next, frag->fragment_end);
341 frag->fragment_next = frag->fragment_end = frag->fragment_start;
342 *previous = frag->next;
343 continue;
347 We need to insert 3 phony objects so the fragments build step can correctly
348 walk the nursery.
351 /* Clean the fragment range. */
352 sgen_clear_range (start, end);
353 /* We need a phony object in between the original fragment start and the effective one. */
354 if (start != frag->fragment_next)
355 sgen_clear_range (frag->fragment_next, start);
356 /* We need an phony object in between the new fragment end and the original fragment end. */
357 if (end != frag->fragment_end)
358 sgen_clear_range (end, frag->fragment_end);
360 frag->fragment_start = frag->fragment_next = start;
361 frag->fragment_end = end;
362 mark_bits_in_range (to_space_bitmap, start, end);
363 previous = &frag->next;
367 static void
368 clear_fragments (void)
370 sgen_clear_allocator_fragments (&collector_allocator);
373 static void
374 init_nursery (SgenFragmentAllocator *allocator, char *start, char *end)
376 int alloc_quote = (int)((end - start) * alloc_ratio);
377 promotion_barrier = (char *)align_down (start + alloc_quote, 3);
378 sgen_fragment_allocator_add (allocator, start, promotion_barrier);
379 sgen_fragment_allocator_add (&collector_allocator, promotion_barrier, end);
381 region_age_size = (end - start) >> SGEN_TO_SPACE_GRANULE_BITS;
382 region_age = (char *)g_malloc0 (region_age_size);
385 static gboolean
386 handle_gc_param (const char *opt)
388 if (g_str_has_prefix (opt, "alloc-ratio=")) {
389 const char *arg = strchr (opt, '=') + 1;
390 int percentage = atoi (arg);
391 if (percentage < 1 || percentage > 100) {
392 fprintf (stderr, "alloc-ratio must be an integer in the range 1-100.\n");
393 exit (1);
395 alloc_ratio = (float)percentage / 100.0f;
396 return TRUE;
399 if (g_str_has_prefix (opt, "promotion-age=")) {
400 const char *arg = strchr (opt, '=') + 1;
401 promote_age = atoi (arg);
402 if (promote_age < 1 || promote_age >= MAX_AGE) {
403 fprintf (stderr, "promotion-age must be an integer in the range 1-%d.\n", MAX_AGE - 1);
404 exit (1);
406 return TRUE;
408 return FALSE;
411 static void
412 print_gc_param_usage (void)
414 fprintf (stderr,
416 " alloc-ratio=P (where P is a percentage, an integer in 1-100)\n"
417 " promotion-age=P (where P is a number, an integer in 1-%d)\n",
418 MAX_AGE - 1
422 /******************************************Copy/Scan functins ************************************************/
424 #define collector_pin_object(obj, queue) sgen_pin_object (obj, queue);
425 #define COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION alloc_for_promotion
427 #include "sgen-copy-object.h"
429 #define SGEN_SPLIT_NURSERY
431 #include "sgen-minor-copy-object.h"
432 #include "sgen-minor-scan-object.h"
434 static void
435 fill_serial_ops (SgenObjectOperations *ops)
437 ops->copy_or_mark_object = SERIAL_COPY_OBJECT;
438 FILL_MINOR_COLLECTOR_SCAN_OBJECT (ops);
441 #define SGEN_CONCURRENT_MAJOR
443 #include "sgen-minor-copy-object.h"
444 #include "sgen-minor-scan-object.h"
446 static void
447 fill_serial_with_concurrent_major_ops (SgenObjectOperations *ops)
449 ops->copy_or_mark_object = SERIAL_COPY_OBJECT;
450 FILL_MINOR_COLLECTOR_SCAN_OBJECT (ops);
453 void
454 sgen_split_nursery_init (SgenMinorCollector *collector)
456 collector->is_split = TRUE;
457 collector->is_parallel = FALSE;
459 collector->alloc_for_promotion = minor_alloc_for_promotion;
461 collector->prepare_to_space = prepare_to_space;
462 collector->clear_fragments = clear_fragments;
463 collector->build_fragments_get_exclude_head = build_fragments_get_exclude_head;
464 collector->build_fragments_release_exclude_head = build_fragments_release_exclude_head;
465 collector->build_fragments_finish = build_fragments_finish;
466 collector->init_nursery = init_nursery;
467 collector->handle_gc_param = handle_gc_param;
468 collector->print_gc_param_usage = print_gc_param_usage;
470 fill_serial_ops (&collector->serial_ops);
471 fill_serial_with_concurrent_major_ops (&collector->serial_ops_with_concurrent_major);
474 #endif //#ifndef DISABLE_SGEN_SPLIT_NURSERY
476 #endif