[mono-api-html] Fix rendering of nuint. (#10662)
[mono-project.git] / mono / sgen / sgen-nursery-allocator.c
blobaf4bcfd90ae3c207b9285226a629c919c21bc326
1 /**
2 * \file
3 * Nursery allocation code.
5 * Copyright 2009-2010 Novell, Inc.
6 * 2011 Rodrigo Kumpera
7 *
8 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
9 * Copyright (C) 2012 Xamarin Inc
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 * The young generation is divided into fragments. This is because
16 * we can hand one fragments to a thread for lock-less fast alloc and
17 * because the young generation ends up fragmented anyway by pinned objects.
18 * Once a collection is done, a list of fragments is created. When doing
19 * thread local alloc we use smallish nurseries so we allow new threads to
20 * allocate memory from gen0 without triggering a collection. Threads that
21 * are found to allocate lots of memory are given bigger fragments. This
22 * should make the finalizer thread use little nursery memory after a while.
23 * We should start assigning threads very small fragments: if there are many
24 * threads the nursery will be full of reserved space that the threads may not
25 * use at all, slowing down allocation speed.
26 * Thread local allocation is done from areas of memory Hotspot calls Thread Local
27 * Allocation Buffers (TLABs).
29 #include "config.h"
30 #ifdef HAVE_SGEN_GC
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #ifdef HAVE_PTHREAD_H
36 #include <pthread.h>
37 #endif
38 #ifdef HAVE_SEMAPHORE_H
39 #include <semaphore.h>
40 #endif
41 #include <stdio.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <assert.h>
45 #ifdef __MACH__
46 #undef _XOPEN_SOURCE
47 #endif
48 #ifdef __MACH__
49 #define _XOPEN_SOURCE
50 #endif
52 #include "mono/sgen/sgen-gc.h"
53 #include "mono/sgen/sgen-cardtable.h"
54 #include "mono/sgen/sgen-protocol.h"
55 #include "mono/sgen/sgen-memory-governor.h"
56 #include "mono/sgen/sgen-pinning.h"
57 #include "mono/sgen/sgen-client.h"
58 #include "mono/utils/mono-membar.h"
60 /* Enable it so nursery allocation diagnostic data is collected */
61 //#define NALLOC_DEBUG 1
63 /* The mutator allocs from here. */
64 static SgenFragmentAllocator mutator_allocator;
66 /* freeelist of fragment structures */
67 static SgenFragment *fragment_freelist = NULL;
69 char *sgen_nursery_start;
70 char *sgen_nursery_end;
72 /* good sizes are 512KB-1MB: larger ones increase a lot memzeroing time */
73 size_t sgen_nursery_size;
75 * Maximum size that we can resize the nursery to.
76 * If sgen_nursery_default_size == sgen_nursery_max_size then we are not
77 * dynamically resizing the nursery
79 size_t sgen_nursery_max_size;
80 size_t sgen_nursery_min_size;
81 /* The number of trailing 0 bits in sgen_nursery_max_size */
82 int sgen_nursery_bits;
85 char *sgen_space_bitmap;
86 size_t sgen_space_bitmap_size;
88 #ifdef HEAVY_STATISTICS
90 static mword stat_wasted_bytes_trailer = 0;
91 static mword stat_wasted_bytes_small_areas = 0;
92 static mword stat_wasted_bytes_discarded_fragments = 0;
93 static guint64 stat_nursery_alloc_requests = 0;
94 static guint64 stat_alloc_iterations = 0;
95 static guint64 stat_alloc_retries = 0;
97 static guint64 stat_nursery_alloc_range_requests = 0;
98 static guint64 stat_alloc_range_iterations = 0;
99 static guint64 stat_alloc_range_retries = 0;
101 #endif
103 /************************************Nursery allocation debugging *********************************************/
105 #ifdef NALLOC_DEBUG
107 enum {
108 FIXED_ALLOC = 1,
109 RANGE_ALLOC,
110 PINNING,
111 BLOCK_ZEROING,
112 CLEAR_NURSERY_FRAGS
115 typedef struct {
116 char *address;
117 size_t size;
118 int reason;
119 int seq;
120 MonoNativeThreadId tid;
121 } AllocRecord;
123 #define ALLOC_RECORD_COUNT 128000
126 static AllocRecord *alloc_records;
127 static volatile int next_record;
128 static volatile int alloc_count;
130 void dump_alloc_records (void);
131 void verify_alloc_records (void);
133 static const char*
134 get_reason_name (AllocRecord *rec)
136 switch (rec->reason) {
137 case FIXED_ALLOC: return "fixed-alloc";
138 case RANGE_ALLOC: return "range-alloc";
139 case PINNING: return "pinning";
140 case BLOCK_ZEROING: return "block-zeroing";
141 case CLEAR_NURSERY_FRAGS: return "clear-nursery-frag";
142 default: return "invalid";
146 static void
147 reset_alloc_records (void)
149 next_record = 0;
150 alloc_count = 0;
153 static void
154 add_alloc_record (char *addr, size_t size, int reason)
156 int idx = mono_atomic_inc_i32 (&next_record) - 1;
157 alloc_records [idx].address = addr;
158 alloc_records [idx].size = size;
159 alloc_records [idx].reason = reason;
160 alloc_records [idx].seq = idx;
161 alloc_records [idx].tid = mono_native_thread_id_get ();
164 static int
165 comp_alloc_record (const void *_a, const void *_b)
167 const AllocRecord *a = _a;
168 const AllocRecord *b = _b;
169 if (a->address == b->address)
170 return a->seq - b->seq;
171 return a->address - b->address;
174 #define rec_end(REC) ((REC)->address + (REC)->size)
176 void
177 dump_alloc_records (void)
179 int i;
180 sgen_qsort (alloc_records, next_record, sizeof (AllocRecord), comp_alloc_record);
182 printf ("------------------------------------DUMP RECORDS----------------------------\n");
183 for (i = 0; i < next_record; ++i) {
184 AllocRecord *rec = alloc_records + i;
185 printf ("obj [%p, %p] size %d reason %s seq %d tid %x\n", rec->address, rec_end (rec), (int)rec->size, get_reason_name (rec), rec->seq, (size_t)rec->tid);
189 void
190 verify_alloc_records (void)
192 int i;
193 int total = 0;
194 int holes = 0;
195 int max_hole = 0;
196 AllocRecord *prev = NULL;
198 sgen_qsort (alloc_records, next_record, sizeof (AllocRecord), comp_alloc_record);
199 printf ("------------------------------------DUMP RECORDS- %d %d---------------------------\n", next_record, alloc_count);
200 for (i = 0; i < next_record; ++i) {
201 AllocRecord *rec = alloc_records + i;
202 int hole_size = 0;
203 total += rec->size;
204 if (prev) {
205 if (rec_end (prev) > rec->address)
206 printf ("WE GOT OVERLAPPING objects %p and %p\n", prev->address, rec->address);
207 if ((rec->address - rec_end (prev)) >= 8)
208 ++holes;
209 hole_size = rec->address - rec_end (prev);
210 max_hole = MAX (max_hole, hole_size);
212 printf ("obj [%p, %p] size %d hole to prev %d reason %s seq %d tid %zx\n", rec->address, rec_end (rec), (int)rec->size, hole_size, get_reason_name (rec), rec->seq, (size_t)rec->tid);
213 prev = rec;
215 printf ("SUMMARY total alloc'd %d holes %d max_hole %d\n", total, holes, max_hole);
218 #endif
220 /*********************************************************************************/
223 static inline gpointer
224 mask (gpointer n, uintptr_t bit)
226 return (gpointer)(((uintptr_t)n) | bit);
229 static inline gpointer
230 unmask (gpointer p)
232 return (gpointer)((uintptr_t)p & ~(uintptr_t)0x3);
235 static inline uintptr_t
236 get_mark (gpointer n)
238 return (uintptr_t)n & 0x1;
241 /*MUST be called with world stopped*/
242 SgenFragment*
243 sgen_fragment_allocator_alloc (void)
245 SgenFragment *frag = fragment_freelist;
246 if (frag) {
247 fragment_freelist = frag->next_in_order;
248 frag->next = frag->next_in_order = NULL;
249 return frag;
251 frag = (SgenFragment *)sgen_alloc_internal (INTERNAL_MEM_FRAGMENT);
252 frag->next = frag->next_in_order = NULL;
253 return frag;
256 void
257 sgen_fragment_allocator_add (SgenFragmentAllocator *allocator, char *start, char *end)
259 SgenFragment *fragment;
261 fragment = sgen_fragment_allocator_alloc ();
262 fragment->fragment_start = start;
263 fragment->fragment_next = start;
264 fragment->fragment_end = end;
265 fragment->next_in_order = fragment->next = (SgenFragment *)unmask (allocator->region_head);
267 allocator->region_head = allocator->alloc_head = fragment;
268 g_assert (fragment->fragment_end > fragment->fragment_start);
271 void
272 sgen_fragment_allocator_release (SgenFragmentAllocator *allocator)
274 SgenFragment *last = allocator->region_head;
275 if (!last)
276 return;
278 /* Find the last fragment in insert order */
279 for (; last->next_in_order; last = last->next_in_order) ;
281 last->next_in_order = fragment_freelist;
282 fragment_freelist = allocator->region_head;
283 allocator->alloc_head = allocator->region_head = NULL;
286 static SgenFragment**
287 find_previous_pointer_fragment (SgenFragmentAllocator *allocator, SgenFragment *frag)
289 SgenFragment **prev;
290 SgenFragment *cur, *next;
291 #ifdef NALLOC_DEBUG
292 int count = 0;
293 #endif
295 try_again:
296 prev = &allocator->alloc_head;
297 #ifdef NALLOC_DEBUG
298 if (count++ > 5)
299 printf ("retry count for fppf is %d\n", count);
300 #endif
302 cur = (SgenFragment *)unmask (*prev);
304 while (1) {
305 if (cur == NULL)
306 return NULL;
307 next = cur->next;
310 * We need to make sure that we dereference prev below
311 * after reading cur->next above, so we need a read
312 * barrier.
314 mono_memory_read_barrier ();
316 if (*prev != cur)
317 goto try_again;
319 if (!get_mark (next)) {
320 if (cur == frag)
321 return prev;
322 prev = &cur->next;
323 } else {
324 next = (SgenFragment *)unmask (next);
325 if (mono_atomic_cas_ptr ((volatile gpointer*)prev, next, cur) != cur)
326 goto try_again;
327 /*we must make sure that the next from cur->next happens after*/
328 mono_memory_write_barrier ();
331 cur = (SgenFragment *)unmask (next);
333 return NULL;
336 static gboolean
337 claim_remaining_size (SgenFragment *frag, char *alloc_end)
339 /* All space used, nothing to claim. */
340 if (frag->fragment_end <= alloc_end)
341 return FALSE;
343 /* Try to alloc all the remaining space. */
344 return mono_atomic_cas_ptr ((volatile gpointer*)&frag->fragment_next, frag->fragment_end, alloc_end) == alloc_end;
347 static void*
348 par_alloc_from_fragment (SgenFragmentAllocator *allocator, SgenFragment *frag, size_t size)
350 char *p = frag->fragment_next;
351 char *end = p + size;
353 if (end > frag->fragment_end || end > (sgen_nursery_start + sgen_nursery_size))
354 return NULL;
356 /* p = frag->fragment_next must happen before */
357 mono_memory_barrier ();
359 if (mono_atomic_cas_ptr ((volatile gpointer*)&frag->fragment_next, end, p) != p)
360 return NULL;
362 if (frag->fragment_end - end < SGEN_MAX_NURSERY_WASTE) {
363 SgenFragment *next, **prev_ptr;
366 * Before we clean the remaining nursery, we must claim the remaining space
367 * as it could end up been used by the range allocator since it can end up
368 * allocating from this dying fragment as it doesn't respect SGEN_MAX_NURSERY_WASTE
369 * when doing second chance allocation.
371 if ((sgen_get_nursery_clear_policy () == CLEAR_AT_TLAB_CREATION || sgen_get_nursery_clear_policy () == CLEAR_AT_TLAB_CREATION_DEBUG) && claim_remaining_size (frag, end)) {
372 sgen_clear_range (end, frag->fragment_end);
373 HEAVY_STAT (stat_wasted_bytes_trailer += frag->fragment_end - end);
374 #ifdef NALLOC_DEBUG
375 add_alloc_record (end, frag->fragment_end - end, BLOCK_ZEROING);
376 #endif
379 prev_ptr = find_previous_pointer_fragment (allocator, frag);
381 /*Use Michaels linked list remove*/
383 /*prev_ptr will be null if the fragment was removed concurrently */
384 while (prev_ptr) {
385 next = frag->next;
387 /*already deleted*/
388 if (!get_mark (next)) {
389 /*frag->next read must happen before the first CAS*/
390 mono_memory_write_barrier ();
392 /*Fail if the next node is removed concurrently and its CAS wins */
393 if (mono_atomic_cas_ptr ((volatile gpointer*)&frag->next, mask (next, 1), next) != next) {
394 continue;
398 /* The second CAS must happen after the first CAS or frag->next. */
399 mono_memory_write_barrier ();
401 /* Fail if the previous node was deleted and its CAS wins */
402 if (mono_atomic_cas_ptr ((volatile gpointer*)prev_ptr, unmask (next), frag) != frag) {
403 prev_ptr = find_previous_pointer_fragment (allocator, frag);
404 continue;
406 break;
410 return p;
413 static void*
414 serial_alloc_from_fragment (SgenFragment **previous, SgenFragment *frag, size_t size)
416 char *p = frag->fragment_next;
417 char *end = p + size;
419 if (end > frag->fragment_end)
420 return NULL;
422 frag->fragment_next = end;
424 if (frag->fragment_end - end < SGEN_MAX_NURSERY_WASTE) {
425 *previous = frag->next;
427 /* Clear the remaining space, pinning depends on this. FIXME move this to use phony arrays */
428 memset (end, 0, frag->fragment_end - end);
430 *previous = frag->next;
433 return p;
436 void*
437 sgen_fragment_allocator_par_alloc (SgenFragmentAllocator *allocator, size_t size)
439 SgenFragment *frag;
441 #ifdef NALLOC_DEBUG
442 mono_atomic_inc_i32 (&alloc_count);
443 #endif
445 restart:
446 for (frag = (SgenFragment *)unmask (allocator->alloc_head); unmask (frag); frag = (SgenFragment *)unmask (frag->next)) {
447 size_t frag_size = frag->fragment_end - frag->fragment_next;
449 if (frag->fragment_next >= (sgen_nursery_start + sgen_nursery_size))
450 continue;
452 HEAVY_STAT (++stat_alloc_iterations);
454 if (size <= frag_size) {
455 void *p = par_alloc_from_fragment (allocator, frag, size);
456 if (!p) {
457 HEAVY_STAT (++stat_alloc_retries);
458 goto restart;
460 #ifdef NALLOC_DEBUG
461 add_alloc_record (p, size, FIXED_ALLOC);
462 #endif
463 return p;
466 return NULL;
469 void*
470 sgen_fragment_allocator_serial_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size)
472 SgenFragment *frag, **previous, *min_frag = NULL, **prev_min_frag = NULL;
473 size_t current_minimum = minimum_size;
475 #ifdef NALLOC_DEBUG
476 mono_atomic_inc_i32 (&alloc_count);
477 #endif
479 previous = &allocator->alloc_head;
481 for (frag = *previous; frag; frag = *previous) {
482 size_t frag_size = frag->fragment_end - frag->fragment_next;
484 HEAVY_STAT (++stat_alloc_range_iterations);
486 if (desired_size <= frag_size) {
487 void *p;
488 *out_alloc_size = desired_size;
490 p = serial_alloc_from_fragment (previous, frag, desired_size);
491 #ifdef NALLOC_DEBUG
492 add_alloc_record (p, desired_size, RANGE_ALLOC);
493 #endif
494 return p;
496 if (current_minimum <= frag_size) {
497 min_frag = frag;
498 prev_min_frag = previous;
499 current_minimum = frag_size;
501 previous = &frag->next;
504 if (min_frag) {
505 void *p;
506 size_t frag_size = min_frag->fragment_end - min_frag->fragment_next;
507 *out_alloc_size = frag_size;
509 p = serial_alloc_from_fragment (prev_min_frag, min_frag, frag_size);
511 #ifdef NALLOC_DEBUG
512 add_alloc_record (p, frag_size, RANGE_ALLOC);
513 #endif
514 return p;
517 return NULL;
520 void*
521 sgen_fragment_allocator_par_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size)
523 SgenFragment *frag, *min_frag;
524 size_t current_minimum;
526 restart:
527 min_frag = NULL;
528 current_minimum = minimum_size;
530 #ifdef NALLOC_DEBUG
531 mono_atomic_inc_i32 (&alloc_count);
532 #endif
534 for (frag = (SgenFragment *)unmask (allocator->alloc_head); frag; frag = (SgenFragment *)unmask (frag->next)) {
535 size_t frag_size = frag->fragment_end - frag->fragment_next;
537 if (frag->fragment_next >= (sgen_nursery_start + sgen_nursery_size))
538 continue;
540 HEAVY_STAT (++stat_alloc_range_iterations);
542 if (desired_size <= frag_size) {
543 void *p;
544 *out_alloc_size = desired_size;
546 p = par_alloc_from_fragment (allocator, frag, desired_size);
547 if (!p) {
548 HEAVY_STAT (++stat_alloc_range_retries);
549 goto restart;
551 #ifdef NALLOC_DEBUG
552 add_alloc_record (p, desired_size, RANGE_ALLOC);
553 #endif
554 return p;
556 if (current_minimum <= frag_size) {
557 min_frag = frag;
558 current_minimum = frag_size;
562 /* The second fragment_next read should be ordered in respect to the first code block */
563 mono_memory_barrier ();
565 if (min_frag) {
566 void *p;
567 size_t frag_size = min_frag->fragment_end - min_frag->fragment_next;
569 if (frag_size < minimum_size)
570 goto restart;
572 *out_alloc_size = frag_size;
574 mono_memory_barrier ();
575 p = par_alloc_from_fragment (allocator, min_frag, frag_size);
577 /*XXX restarting here is quite dubious given this is already second chance allocation. */
578 if (!p) {
579 HEAVY_STAT (++stat_alloc_retries);
580 goto restart;
582 #ifdef NALLOC_DEBUG
583 add_alloc_record (p, frag_size, RANGE_ALLOC);
584 #endif
585 return p;
588 return NULL;
591 void
592 sgen_clear_allocator_fragments (SgenFragmentAllocator *allocator)
594 SgenFragment *frag;
596 for (frag = (SgenFragment *)unmask (allocator->alloc_head); frag; frag = (SgenFragment *)unmask (frag->next)) {
597 SGEN_LOG (4, "Clear nursery frag %p-%p", frag->fragment_next, frag->fragment_end);
598 sgen_clear_range (frag->fragment_next, frag->fragment_end);
599 #ifdef NALLOC_DEBUG
600 add_alloc_record (frag->fragment_next, frag->fragment_end - frag->fragment_next, CLEAR_NURSERY_FRAGS);
601 #endif
605 /* Clear all remaining nursery fragments */
606 void
607 sgen_clear_nursery_fragments (void)
609 if (sgen_get_nursery_clear_policy () == CLEAR_AT_TLAB_CREATION || sgen_get_nursery_clear_policy () == CLEAR_AT_TLAB_CREATION_DEBUG) {
610 sgen_clear_allocator_fragments (&mutator_allocator);
611 sgen_minor_collector.clear_fragments ();
616 * Mark a given range of memory as invalid.
618 * This can be done either by zeroing memory or by placing
619 * a phony byte[] array. This keeps the heap forward walkable.
621 * This function ignores calls with a zero range, even if
622 * both start and end are NULL.
624 void
625 sgen_clear_range (char *start, char *end)
627 size_t size = end - start;
629 if ((start && !end) || (start > end))
630 g_error ("Invalid range [%p %p]", start, end);
632 if (sgen_client_array_fill_range (start, size)) {
633 sgen_set_nursery_scan_start (start);
634 SGEN_ASSERT (0, start + sgen_safe_object_get_size ((GCObject*)start) == end, "Array fill produced wrong size");
638 void
639 sgen_nursery_allocator_prepare_for_pinning (void)
641 sgen_clear_allocator_fragments (&mutator_allocator);
642 sgen_minor_collector.clear_fragments ();
645 static mword fragment_total = 0;
647 * We found a fragment of free memory in the nursery: memzero it and if
648 * it is big enough, add it to the list of fragments that can be used for
649 * allocation.
651 static void
652 add_nursery_frag (SgenFragmentAllocator *allocator, size_t frag_size, char* frag_start, char* frag_end)
654 SGEN_LOG (4, "Found empty fragment: %p-%p, size: %zd", frag_start, frag_end, frag_size);
655 sgen_binary_protocol_empty (frag_start, frag_size);
656 /* Not worth dealing with smaller fragments: need to tune */
657 if (frag_size >= SGEN_MAX_NURSERY_WASTE) {
658 /* memsetting just the first chunk start is bound to provide better cache locality */
659 if (sgen_get_nursery_clear_policy () == CLEAR_AT_GC)
660 memset (frag_start, 0, frag_size);
661 else if (sgen_get_nursery_clear_policy () == CLEAR_AT_TLAB_CREATION_DEBUG)
662 memset (frag_start, 0xff, frag_size);
664 #ifdef NALLOC_DEBUG
665 /* XXX convert this into a flight record entry
666 printf ("\tfragment [%p %p] size %zd\n", frag_start, frag_end, frag_size);
668 #endif
669 sgen_fragment_allocator_add (allocator, frag_start, frag_end);
670 fragment_total += frag_size;
671 } else {
672 /* Clear unused fragments, pinning depends on this */
673 sgen_clear_range (frag_start, frag_end);
674 HEAVY_STAT (stat_wasted_bytes_small_areas += frag_size);
678 static void
679 fragment_list_reverse (SgenFragmentAllocator *allocator)
681 SgenFragment *prev = NULL, *list = allocator->region_head;
682 while (list) {
683 SgenFragment *next = list->next;
684 list->next = prev;
685 list->next_in_order = prev;
686 prev = list;
687 list = next;
690 allocator->region_head = allocator->alloc_head = prev;
694 * We split fragments at the border of the current nursery limit. When we
695 * allocate from the nursery we only consider fragments that start in the
696 * current nursery section. We build fragments for the entire nursery in
697 * order to facilitate scanning it for objects (adding a nursery frag also
698 * marks a region in the nursery as being free)
700 static void
701 add_nursery_frag_checks (SgenFragmentAllocator *allocator, char *frag_start, char *frag_end)
703 char *nursery_limit = sgen_nursery_start + sgen_nursery_size;
705 if (frag_start < nursery_limit && frag_end > nursery_limit) {
706 add_nursery_frag (allocator, nursery_limit - frag_start, frag_start, nursery_limit);
707 add_nursery_frag (allocator, frag_end - nursery_limit, nursery_limit, frag_end);
708 } else {
709 add_nursery_frag (allocator, frag_end - frag_start, frag_start, frag_end);
713 mword
714 sgen_build_nursery_fragments (GCMemSection *nursery_section, SgenGrayQueue *unpin_queue)
716 char *frag_start, *frag_end;
717 size_t frag_size;
718 SgenFragment *frags_ranges;
719 void **pin_start, **pin_entry, **pin_end;
721 #ifdef NALLOC_DEBUG
722 reset_alloc_records ();
723 #endif
724 /*The mutator fragments are done. We no longer need them. */
725 sgen_fragment_allocator_release (&mutator_allocator);
727 frag_start = sgen_nursery_start;
728 fragment_total = 0;
730 /* The current nursery might give us a fragment list to exclude [start, next[*/
731 frags_ranges = sgen_minor_collector.build_fragments_get_exclude_head ();
733 /* clear scan starts */
734 memset (nursery_section->scan_starts, 0, nursery_section->num_scan_start * sizeof (gpointer));
736 pin_start = pin_entry = sgen_pinning_get_entry (nursery_section->pin_queue_first_entry);
737 pin_end = sgen_pinning_get_entry (nursery_section->pin_queue_last_entry);
739 while (pin_entry < pin_end || frags_ranges) {
740 char *addr0, *addr1;
741 size_t size;
743 addr0 = addr1 = sgen_nursery_end;
744 if (pin_entry < pin_end)
745 addr0 = (char *)*pin_entry;
746 if (frags_ranges)
747 addr1 = frags_ranges->fragment_start;
749 if (addr0 < addr1) {
750 if (unpin_queue)
751 GRAY_OBJECT_ENQUEUE_SERIAL (unpin_queue, (GCObject*)addr0, sgen_obj_get_descriptor_safe ((GCObject*)addr0));
752 else
753 SGEN_UNPIN_OBJECT (addr0);
754 size = SGEN_ALIGN_UP (sgen_safe_object_get_size ((GCObject*)addr0));
755 CANARIFY_SIZE (size);
756 sgen_set_nursery_scan_start (addr0);
757 frag_end = addr0;
758 ++pin_entry;
759 } else {
760 frag_end = addr1;
761 size = frags_ranges->fragment_next - addr1;
762 frags_ranges = frags_ranges->next_in_order;
765 frag_size = frag_end - frag_start;
767 if (size == 0)
768 continue;
770 g_assert (frag_size >= 0);
771 g_assert (size > 0);
772 if (frag_size && size)
773 add_nursery_frag_checks (&mutator_allocator, frag_start, frag_end);
775 frag_size = size;
776 #ifdef NALLOC_DEBUG
777 add_alloc_record (*pin_entry, frag_size, PINNING);
778 #endif
779 frag_start = frag_end + frag_size;
782 frag_end = sgen_nursery_end;
783 frag_size = frag_end - frag_start;
784 if (frag_size)
785 add_nursery_frag_checks (&mutator_allocator, frag_start, frag_end);
787 /* Now it's safe to release the fragments exclude list. */
788 sgen_minor_collector.build_fragments_release_exclude_head ();
790 /* First we reorder the fragment list to be in ascending address order. This makes H/W prefetchers happier. */
791 fragment_list_reverse (&mutator_allocator);
793 /*The collector might want to do something with the final nursery fragment list.*/
794 sgen_minor_collector.build_fragments_finish (&mutator_allocator);
796 if (!unmask (mutator_allocator.alloc_head)) {
797 SGEN_LOG (1, "Nursery fully pinned");
798 for (pin_entry = pin_start; pin_entry < pin_end; ++pin_entry) {
799 GCObject *p = (GCObject *)*pin_entry;
800 SGEN_LOG (3, "Bastard pinning obj %p (%s), size: %zd", p, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (p)), sgen_safe_object_get_size (p));
803 return fragment_total;
806 /*** Nursery memory allocation ***/
807 void
808 sgen_nursery_retire_region (void *address, ptrdiff_t size)
810 HEAVY_STAT (stat_wasted_bytes_discarded_fragments += size);
813 gboolean
814 sgen_can_alloc_size (size_t size)
816 SgenFragment *frag;
818 if (!SGEN_CAN_ALIGN_UP (size))
819 return FALSE;
821 size = SGEN_ALIGN_UP (size);
823 for (frag = (SgenFragment *)unmask (mutator_allocator.alloc_head); frag; frag = (SgenFragment *)unmask (frag->next)) {
824 if ((size_t)(frag->fragment_end - frag->fragment_next) >= size)
825 return TRUE;
827 return FALSE;
830 void*
831 sgen_nursery_alloc (size_t size)
833 SGEN_ASSERT (1, size >= (SGEN_CLIENT_MINIMUM_OBJECT_SIZE + CANARY_SIZE) && size <= (SGEN_MAX_SMALL_OBJ_SIZE + CANARY_SIZE), "Invalid nursery object size");
835 SGEN_LOG (4, "Searching nursery for size: %zd", size);
836 size = SGEN_ALIGN_UP (size);
838 HEAVY_STAT (++stat_nursery_alloc_requests);
840 return sgen_fragment_allocator_par_alloc (&mutator_allocator, size);
843 void*
844 sgen_nursery_alloc_range (size_t desired_size, size_t minimum_size, size_t *out_alloc_size)
846 SGEN_LOG (4, "Searching for byte range desired size: %zd minimum size %zd", desired_size, minimum_size);
848 HEAVY_STAT (++stat_nursery_alloc_range_requests);
850 return sgen_fragment_allocator_par_range_alloc (&mutator_allocator, desired_size, minimum_size, out_alloc_size);
853 /*** Initialization ***/
855 #ifdef HEAVY_STATISTICS
857 void
858 sgen_nursery_allocator_init_heavy_stats (void)
860 mono_counters_register ("bytes wasted trailer fragments", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES, &stat_wasted_bytes_trailer);
861 mono_counters_register ("bytes wasted small areas", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES, &stat_wasted_bytes_small_areas);
862 mono_counters_register ("bytes wasted discarded fragments", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES, &stat_wasted_bytes_discarded_fragments);
864 mono_counters_register ("# nursery alloc requests", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_nursery_alloc_requests);
865 mono_counters_register ("# nursery alloc iterations", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_alloc_iterations);
866 mono_counters_register ("# nursery alloc retries", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_alloc_retries);
868 mono_counters_register ("# nursery alloc range requests", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_nursery_alloc_range_requests);
869 mono_counters_register ("# nursery alloc range iterations", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_alloc_range_iterations);
870 mono_counters_register ("# nursery alloc range restries", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_alloc_range_retries);
873 #endif
875 void
876 sgen_init_nursery_allocator (void)
878 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_FRAGMENT, sizeof (SgenFragment));
879 #ifdef NALLOC_DEBUG
880 alloc_records = sgen_alloc_os_memory (sizeof (AllocRecord) * ALLOC_RECORD_COUNT, SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, "debugging memory");
881 #endif
884 void
885 sgen_nursery_alloc_prepare_for_minor (void)
887 sgen_minor_collector.prepare_to_space (sgen_space_bitmap, sgen_space_bitmap_size);
890 void
891 sgen_nursery_alloc_prepare_for_major (void)
893 sgen_minor_collector.prepare_to_space (sgen_space_bitmap, sgen_space_bitmap_size);
896 void
897 sgen_nursery_allocator_set_nursery_bounds (char *start, size_t min_size, size_t max_size)
899 sgen_nursery_start = start;
900 sgen_nursery_end = start + max_size;
902 sgen_nursery_size = min_size;
903 sgen_nursery_min_size = min_size;
904 sgen_nursery_max_size = max_size;
906 sgen_nursery_bits = 0;
907 while (ONE_P << (++ sgen_nursery_bits) != sgen_nursery_max_size)
911 * This will not divide evenly for tiny nurseries (<4kb), so we make sure to be on
912 * the right side of things and round up. We could just do a MIN(1,x) instead,
913 * since the nursery size must be a power of 2.
915 sgen_space_bitmap_size = (sgen_nursery_end - sgen_nursery_start + SGEN_TO_SPACE_GRANULE_IN_BYTES * 8 - 1) / (SGEN_TO_SPACE_GRANULE_IN_BYTES * 8);
916 sgen_space_bitmap = (char *)g_malloc0 (sgen_space_bitmap_size);
918 /* Setup the single first large fragment */
919 sgen_minor_collector.init_nursery (&mutator_allocator, sgen_nursery_start, sgen_nursery_end);
922 void
923 sgen_resize_nursery (gboolean need_shrink)
925 size_t major_size;
927 if (sgen_nursery_min_size == sgen_nursery_max_size)
928 return;
930 major_size = sgen_major_collector.get_num_major_sections () * sgen_major_collector.section_size + sgen_los_memory_usage;
932 * We attempt to use a larger nursery size, as long as it doesn't
933 * exceed a certain percentage of the major heap.
935 * FIXME
936 * Commit memory when expanding and release it when shrinking (which
937 * would only be possible if there aren't any pinned objects in the
938 * section).
940 if ((sgen_nursery_size * 2) < (major_size / SGEN_DEFAULT_ALLOWANCE_NURSERY_SIZE_RATIO) &&
941 (sgen_nursery_size * 2) <= sgen_nursery_max_size && !need_shrink) {
942 if ((sgen_nursery_section->end_data - sgen_nursery_section->data) == sgen_nursery_size)
943 sgen_nursery_section->end_data += sgen_nursery_size;
944 sgen_nursery_size *= 2;
945 } else if ((sgen_nursery_size > (major_size / SGEN_DEFAULT_ALLOWANCE_NURSERY_SIZE_RATIO) || need_shrink) &&
946 (sgen_nursery_size / 2) >= sgen_nursery_min_size) {
947 sgen_nursery_size /= 2;
952 #endif