2 * sgen-pinned-allocator.c: Allocator for small pinned objects.
3 * Only used in the copying major collector.
6 * Paolo Molaro (lupus@ximian.com)
8 * Copyright 2005-2010 Novell, Inc (http://www.novell.com)
10 * Thread start/stop adapted from Boehm's GC:
11 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
12 * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
13 * Copyright (c) 1998 by Fergus Henderson. All rights reserved.
14 * Copyright (c) 2000-2004 by Hewlett-Packard Company. All rights reserved.
15 * Copyright 2001-2003 Ximian, Inc
16 * Copyright 2003-2010 Novell, Inc.
17 * Copyright (C) 2012 Xamarin Inc
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Library General Public
21 * License 2.0 as published by the Free Software Foundation;
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Library General Public License for more details.
28 * You should have received a copy of the GNU Library General Public
29 * License 2.0 along with this library; if not, write to the Free
30 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #include "utils/mono-counters.h"
38 #include "metadata/sgen-gc.h"
39 #include "metadata/sgen-memory-governor.h"
41 /* Pinned objects are allocated in the LOS space if bigger than half a page
42 * or from freelists otherwise. We assume that pinned objects are relatively few
43 * and they have a slow dying speed (like interned strings, thread objects).
44 * As such they will be collected only at major collections.
45 * free lists are not global: when we need memory we allocate a PinnedChunk.
46 * Each pinned chunk is made of several pages, the first of wich is used
47 * internally for bookeeping (here think of a page as 4KB). The bookeeping
48 * includes the freelists vectors and info about the object size of each page
49 * in the pinned chunk. So, when needed, a free page is found in a pinned chunk,
50 * a size is assigned to it, the page is divided in the proper chunks and each
51 * chunk is added to the freelist. To not waste space, the remaining space in the
52 * first page is used as objects of size 16 or 32 (need to measure which are more
54 * We use this same structure to allocate memory used internally by the GC, so
55 * we never use malloc/free if we need to alloc during collection: the world is stopped
56 * and malloc/free will deadlock.
57 * When we want to iterate over pinned objects, we just scan a page at a time
58 * linearly according to the size of objects in the page: the next pointer used to link
59 * the items in the freelist uses the same word as the vtable. Since we keep freelists
60 * for each pinned chunk, if the word points outside the pinned chunk it means
62 * We could avoid this expensive scanning in creative ways. We could have a policy
63 * of putting in the pinned space only objects we know about that have no struct fields
64 * with references and we can easily use a even expensive write barrier for them,
65 * since pointer writes on such objects should be rare.
66 * The best compromise is to just alloc interned strings and System.MonoType in them.
67 * It would be nice to allocate MonoThread in it, too: must check that we properly
68 * use write barriers so we don't have to do any expensive scanning of the whole pinned
69 * chunk list during minor collections. We can avoid it now because we alloc in it only
70 * reference-free objects.
72 struct _SgenPinnedChunk {
75 SgenPinnedAllocator *allocator;
76 int *page_sizes; /* a 0 means the page is still unused */
78 SgenPinnedChunk *free_list_nexts [SGEN_PINNED_FREELIST_NUM_SLOTS];
80 void *data [1]; /* page sizes and free lists are stored here */
83 #define PINNED_FIRST_SLOT_SIZE (sizeof (gpointer) * 4)
84 #define MAX_FREELIST_SIZE 8192
86 /* This is a fixed value used for pinned chunks, not the system pagesize */
87 #define FREELIST_PAGESIZE (16*1024)
89 /* keep each size a multiple of ALLOC_ALIGN */
90 /* on 64 bit systems 8 is likely completely unused. */
91 static const int freelist_sizes [] = {
92 8, 16, 24, 32, 40, 48, 64, 80,
93 96, 128, 160, 192, 224, 256, 320, 384,
94 448, 512, 584, 680, 816, 1024, 1360, 2048,
95 2336, 2728, 3272, 4096, 5456, 8192 };
97 #define LARGE_PINNED_MEM_HEADER_MAGIC 0x7d289f3a
99 /* FIXME: Do we even need these anymore? Large objects are always
100 allocated in the LOS. */
101 typedef struct _LargePinnedMemHeader LargePinnedMemHeader;
102 struct _LargePinnedMemHeader {
108 static long long pinned_chunk_bytes_alloced = 0;
109 static long long large_pinned_bytes_alloced = 0;
111 #ifdef HEAVY_STATISTICS
112 static long long stat_pinned_alloc = 0;
119 report_pinned_chunk (SgenPinnedChunk *chunk, int seq) {
121 int i, free_pages, num_free, free_mem;
123 for (i = 0; i < chunk->num_pages; ++i) {
124 if (!chunk->page_sizes [i])
127 printf ("Pinned chunk %d at %p, size: %d, pages: %d, free: %d\n", seq, chunk, chunk->num_pages * FREELIST_PAGESIZE, chunk->num_pages, free_pages);
128 free_mem = FREELIST_PAGESIZE * free_pages;
129 for (i = 0; i < SGEN_PINNED_FREELIST_NUM_SLOTS; ++i) {
130 if (!chunk->free_list [i])
133 p = chunk->free_list [i];
138 printf ("\tfree list of size %d, %d items\n", freelist_sizes [i], num_free);
139 free_mem += freelist_sizes [i] * num_free;
141 printf ("\tfree memory in chunk: %d\n", free_mem);
148 sgen_report_pinned_mem_usage (SgenPinnedAllocator *alc)
150 SgenPinnedChunk *chunk;
152 for (chunk = alc->chunk_list; chunk; chunk = chunk->block.next)
153 report_pinned_chunk (chunk, i++);
157 * Find the slot number in the freelist for memory chunks that
158 * can contain @size objects.
161 slot_for_size (size_t size)
164 /* do a binary search or lookup table later. */
165 for (slot = 0; slot < SGEN_PINNED_FREELIST_NUM_SLOTS; ++slot) {
166 if (freelist_sizes [slot] >= size)
169 g_assert_not_reached ();
174 * Build a free list for @size memory chunks from the memory area between
175 * start_page and end_page.
178 build_freelist (SgenPinnedAllocator *alc, SgenPinnedChunk *chunk, int slot, int size, char *start_page, char *end_page)
182 /*g_print ("building freelist for slot %d, size %d in %p\n", slot, size, chunk);*/
183 p = (void**)start_page;
184 end = (void**)(end_page - size);
185 g_assert (!chunk->free_list [slot]);
186 chunk->free_list [slot] = p;
187 while ((char*)p + size <= (char*)end) {
189 *p = (void*)((char*)p + size);
193 /*g_print ("%d items created, max: %d\n", count, (end_page - start_page) / size);*/
195 g_assert (!chunk->free_list_nexts [slot]);
196 chunk->free_list_nexts [slot] = alc->free_lists [slot];
197 alc->free_lists [slot] = chunk;
200 static SgenPinnedChunk*
201 alloc_pinned_chunk (SgenPinnedAllocator *alc)
203 SgenPinnedChunk *chunk;
205 int size = SGEN_PINNED_CHUNK_SIZE;
207 chunk = sgen_alloc_os_memory_aligned (size, size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "pinned chunk");
208 chunk->block.role = MEMORY_ROLE_PINNED;
210 sgen_update_heap_boundaries ((mword)chunk, ((mword)chunk + size));
212 pinned_chunk_bytes_alloced += size;
214 /* setup the bookeeping fields */
215 chunk->num_pages = size / FREELIST_PAGESIZE;
216 offset = G_STRUCT_OFFSET (SgenPinnedChunk, data);
217 chunk->page_sizes = (void*)((char*)chunk + offset);
218 offset += sizeof (int) * chunk->num_pages;
219 offset = SGEN_ALIGN_UP (offset);
220 chunk->free_list = (void*)((char*)chunk + offset);
221 offset += sizeof (void*) * SGEN_PINNED_FREELIST_NUM_SLOTS;
222 offset = SGEN_ALIGN_UP (offset);
223 chunk->start_data = (void*)((char*)chunk + offset);
225 /* allocate the first page to the freelist */
226 chunk->page_sizes [0] = PINNED_FIRST_SLOT_SIZE;
227 build_freelist (alc, chunk, slot_for_size (PINNED_FIRST_SLOT_SIZE), PINNED_FIRST_SLOT_SIZE,
228 chunk->start_data, ((char*)chunk + FREELIST_PAGESIZE));
229 SGEN_LOG (4, "Allocated pinned chunk %p, size: %d", chunk, size);
231 chunk->block.next = alc->chunk_list;
232 alc->chunk_list = chunk;
234 chunk->allocator = alc;
239 /* Must be called with an empty freelist for the given slot. */
241 populate_chunk_page (SgenPinnedAllocator *alc, SgenPinnedChunk *chunk, int slot)
243 int size = freelist_sizes [slot];
245 g_assert (!chunk->free_list [slot]);
246 g_assert (!chunk->free_list_nexts [slot]);
247 for (i = 0; i < chunk->num_pages; ++i) {
248 if (chunk->page_sizes [i])
250 chunk->page_sizes [i] = size;
251 build_freelist (alc, chunk, slot, size, (char*)chunk + FREELIST_PAGESIZE * i, (char*)chunk + FREELIST_PAGESIZE * (i + 1));
258 alloc_from_slot (SgenPinnedAllocator *alc, int slot)
260 SgenPinnedChunk *pchunk;
261 size_t size = freelist_sizes [slot];
263 if (alc->delayed_free_lists [slot]) {
266 p = alc->delayed_free_lists [slot];
267 } while (SGEN_CAS_PTR (&alc->delayed_free_lists [slot], *p, p) != p);
273 pchunk = alc->free_lists [slot];
275 void **p = pchunk->free_list [slot];
281 pchunk->free_list [slot] = next;
284 alc->free_lists [slot] = pchunk->free_list_nexts [slot];
285 pchunk->free_list_nexts [slot] = NULL;
292 for (pchunk = alc->chunk_list; pchunk; pchunk = pchunk->block.next) {
293 if (populate_chunk_page (alc, pchunk, slot))
297 pchunk = alloc_pinned_chunk (alc);
298 /* FIXME: handle OOM */
299 if (pchunk->free_list [slot])
301 if (!populate_chunk_page (alc, pchunk, slot))
302 g_assert_not_reached ();
306 /* used for the GC-internal data structures */
308 sgen_alloc_pinned (SgenPinnedAllocator *alc, size_t size)
313 HEAVY_STAT (++stat_pinned_alloc);
315 if (size > freelist_sizes [SGEN_PINNED_FREELIST_NUM_SLOTS - 1]) {
316 LargePinnedMemHeader *mh;
318 size += sizeof (LargePinnedMemHeader);
319 mh = sgen_alloc_os_memory (size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, "large pinned object");
320 mh->magic = LARGE_PINNED_MEM_HEADER_MAGIC;
322 /* FIXME: do a CAS here */
323 large_pinned_bytes_alloced += size;
327 slot = slot_for_size (size);
328 g_assert (size <= freelist_sizes [slot]);
329 res = alloc_from_slot (alc, slot);
335 free_from_slot (SgenPinnedAllocator *alc, void *addr, int slot)
337 SgenPinnedChunk *pchunk = (SgenPinnedChunk*)SGEN_PINNED_CHUNK_FOR_PTR (addr);
341 g_assert (addr >= (void*)pchunk && (char*)addr < (char*)pchunk + pchunk->num_pages * FREELIST_PAGESIZE);
343 next = pchunk->free_list [slot];
345 pchunk->free_list [slot] = p;
348 g_assert (!pchunk->free_list_nexts [slot]);
349 pchunk->free_list_nexts [slot] = alc->free_lists [slot];
350 alc->free_lists [slot] = pchunk;
355 sgen_free_pinned (SgenPinnedAllocator *alc, void *addr, size_t size)
357 LargePinnedMemHeader *mh;
362 if (size <= freelist_sizes [SGEN_PINNED_FREELIST_NUM_SLOTS - 1]) {
363 int slot = slot_for_size (size);
364 free_from_slot (alc, addr, slot);
368 mh = (LargePinnedMemHeader*)((char*)addr - G_STRUCT_OFFSET (LargePinnedMemHeader, data));
369 g_assert (mh->magic == LARGE_PINNED_MEM_HEADER_MAGIC);
370 g_assert (mh->size == size + sizeof (LargePinnedMemHeader));
371 /* FIXME: do a CAS */
372 large_pinned_bytes_alloced -= mh->size;
373 sgen_free_os_memory (mh, mh->size, SGEN_ALLOC_HEAP);
377 sgen_init_pinned_allocator (void)
379 g_assert (SGEN_PINNED_FREELIST_NUM_SLOTS == sizeof (freelist_sizes) / sizeof (freelist_sizes [0]));
381 #ifdef HEAVY_STATISTICS
382 mono_counters_register ("Pinned allocs", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_pinned_alloc);
387 sgen_pinned_scan_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data)
389 SgenPinnedChunk *chunk;
394 for (chunk = alc->chunk_list; chunk; chunk = chunk->block.next) {
395 end_chunk = (char*)chunk + chunk->num_pages * FREELIST_PAGESIZE;
396 SGEN_LOG (6, "Scanning pinned chunk %p (range: %p-%p)", chunk, chunk->start_data, end_chunk);
397 for (i = 0; i < chunk->num_pages; ++i) {
398 obj_size = chunk->page_sizes [i];
401 p = i? (char*)chunk + i * FREELIST_PAGESIZE: chunk->start_data;
402 endp = i? p + FREELIST_PAGESIZE: (char*)chunk + FREELIST_PAGESIZE;
403 SGEN_LOG (6, "Page %d (size: %d, range: %p-%p)", i, obj_size, p, endp);
404 while (p + obj_size <= endp) {
406 /* if the first word (the vtable) is outside the chunk we have an object */
407 if (*ptr && (*ptr < (void*)chunk || *ptr >= end_chunk))
408 callback ((char*)ptr, obj_size, callback_data);
416 sgen_pinned_update_heap_boundaries (SgenPinnedAllocator *alc)
418 SgenPinnedChunk *chunk;
419 for (chunk = alc->chunk_list; chunk; chunk = chunk->block.next) {
420 char *end_chunk = (char*)chunk + chunk->num_pages * FREELIST_PAGESIZE;
421 sgen_update_heap_boundaries ((mword)chunk, (mword)end_chunk);
426 * the array of pointers from @start to @end contains conservative
427 * pointers to objects inside @chunk: mark each referenced object
431 mark_pinned_from_addresses (SgenPinnedChunk *chunk, void **start, void **end, IterateObjectCallbackFunc callback, void *callback_data)
433 for (; start < end; start++) {
435 int offset = (char*)addr - (char*)chunk;
436 int page = offset / FREELIST_PAGESIZE;
437 int obj_offset = page == 0? offset - ((char*)chunk->start_data - (char*)chunk): offset % FREELIST_PAGESIZE;
438 int slot_size = chunk->page_sizes [page];
440 /* the page is not allocated */
443 /* would be faster if we restrict the sizes to power of two,
444 * but that's a waste of memory: need to measure. it could reduce
445 * fragmentation since there are less pages needed, if for example
446 * someone interns strings of each size we end up with one page per
447 * interned string (still this is just ~40 KB): with more fine-grained sizes
448 * this increases the number of used pages.
451 obj_offset /= slot_size;
452 obj_offset *= slot_size;
453 addr = (char*)chunk->start_data + obj_offset;
455 obj_offset /= slot_size;
456 obj_offset *= slot_size;
457 addr = (char*)chunk + page * FREELIST_PAGESIZE + obj_offset;
460 /* if the vtable is inside the chunk it's on the freelist, so skip */
461 /* FIXME: is it possible that we're pinning objects more than once here? */
462 if (*ptr && (*ptr < (void*)chunk->start_data || *ptr > (void*)((char*)chunk + chunk->num_pages * FREELIST_PAGESIZE)))
463 callback (addr, slot_size, callback_data);
468 sgen_pinned_scan_pinned_objects (SgenPinnedAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data)
470 SgenPinnedChunk *chunk;
472 /* look for pinned addresses for pinned-alloc objects */
473 SGEN_LOG (6, "Pinning from pinned-alloc objects");
474 for (chunk = alc->chunk_list; chunk; chunk = chunk->block.next) {
476 void **pinned = sgen_find_optimized_pin_queue_area (chunk->start_data,
477 (char*)chunk + chunk->num_pages * FREELIST_PAGESIZE, &num_pinned);
479 mark_pinned_from_addresses (chunk, pinned, pinned + num_pinned, callback, callback_data);