2 * sgen-los.c: Large objects space.
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2005-2010 Novell, Inc (http://www.novell.com)
9 * Thread start/stop adapted from Boehm's GC:
10 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
11 * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
12 * Copyright (c) 1998 by Fergus Henderson. All rights reserved.
13 * Copyright (c) 2000-2004 by Hewlett-Packard Company. All rights reserved.
14 * Copyright 2001-2003 Ximian, Inc
15 * Copyright 2003-2010 Novell, Inc.
16 * Copyright (C) 2012 Xamarin Inc
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Library General Public
20 * License 2.0 as published by the Free Software Foundation;
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Library General Public License for more details.
27 * You should have received a copy of the GNU Library General Public
28 * License 2.0 along with this library; if not, write to the Free
29 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 #include "mono/sgen/sgen-gc.h"
39 #include "mono/sgen/sgen-protocol.h"
40 #include "mono/sgen/sgen-cardtable.h"
41 #include "mono/sgen/sgen-memory-governor.h"
42 #include "mono/sgen/sgen-client.h"
44 #define LOS_SECTION_SIZE (1024 * 1024)
47 * This shouldn't be much smaller or larger than MAX_SMALL_OBJ_SIZE.
48 * Must be at least sizeof (LOSSection).
50 #define LOS_CHUNK_SIZE 4096
51 #define LOS_CHUNK_BITS 12
53 /* Largest object that can be allocated in a section. */
54 #define LOS_SECTION_OBJECT_LIMIT (LOS_SECTION_SIZE - LOS_CHUNK_SIZE - sizeof (LOSObject))
55 //#define LOS_SECTION_OBJECT_LIMIT 0
56 #define LOS_SECTION_NUM_CHUNKS ((LOS_SECTION_SIZE >> LOS_CHUNK_BITS) - 1)
58 #define LOS_SECTION_FOR_OBJ(obj) ((LOSSection*)((mword)(obj) & ~(mword)(LOS_SECTION_SIZE - 1)))
59 #define LOS_CHUNK_INDEX(obj,section) (((char*)(obj) - (char*)(section)) >> LOS_CHUNK_BITS)
61 #define LOS_NUM_FAST_SIZES 32
63 typedef struct _LOSFreeChunks LOSFreeChunks;
64 struct _LOSFreeChunks {
65 LOSFreeChunks *next_size;
69 typedef struct _LOSSection LOSSection;
72 size_t num_free_chunks;
73 unsigned char *free_chunk_map;
76 /* We allow read only access on the list while sweep is not running */
77 LOSObject *los_object_list = NULL;
78 mword los_memory_usage = 0;
80 static LOSSection *los_sections = NULL;
81 static LOSFreeChunks *los_fast_free_lists [LOS_NUM_FAST_SIZES]; /* 0 is for larger sizes */
82 static mword los_num_objects = 0;
83 static int los_num_sections = 0;
86 //#define LOS_CONSISTENCY_CHECK
90 #define LOS_SEGMENT_SIZE (4096 * 1024)
92 static char *los_segment = NULL;
93 static int los_segment_index = 0;
97 sgen_los_object_size (LOSObject *obj)
99 return obj->size & ~1L;
102 #ifdef LOS_CONSISTENCY_CHECK
104 los_consistency_check (void)
109 mword memory_usage = 0;
111 for (obj = los_object_list; obj; obj = obj->next) {
112 mword obj_size = sgen_los_object_size (obj);
113 char *end = obj->data + obj_size;
114 int start_index, num_chunks;
116 memory_usage += obj_size;
118 if (obj_size > LOS_SECTION_OBJECT_LIMIT)
121 section = LOS_SECTION_FOR_OBJ (obj);
123 g_assert (end <= (char*)section + LOS_SECTION_SIZE);
125 start_index = LOS_CHUNK_INDEX (obj, section);
126 num_chunks = (obj_size + sizeof (LOSObject) + LOS_CHUNK_SIZE - 1) >> LOS_CHUNK_BITS;
127 for (i = start_index; i < start_index + num_chunks; ++i)
128 g_assert (!section->free_chunk_map [i]);
131 for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
132 LOSFreeChunks *size_chunks;
133 for (size_chunks = los_fast_free_lists [i]; size_chunks; size_chunks = size_chunks->next_size) {
134 LOSSection *section = LOS_SECTION_FOR_OBJ (size_chunks);
135 int j, num_chunks, start_index;
138 g_assert (size_chunks->size >= LOS_NUM_FAST_SIZES * LOS_CHUNK_SIZE);
140 g_assert (size_chunks->size == i * LOS_CHUNK_SIZE);
142 num_chunks = size_chunks->size >> LOS_CHUNK_BITS;
143 start_index = LOS_CHUNK_INDEX (size_chunks, section);
144 for (j = start_index; j < start_index + num_chunks; ++j)
145 g_assert (section->free_chunk_map [j]);
149 g_assert (los_memory_usage == memory_usage);
154 add_free_chunk (LOSFreeChunks *free_chunks, size_t size)
156 size_t num_chunks = size >> LOS_CHUNK_BITS;
158 free_chunks->size = size;
160 if (num_chunks >= LOS_NUM_FAST_SIZES)
162 free_chunks->next_size = los_fast_free_lists [num_chunks];
163 los_fast_free_lists [num_chunks] = free_chunks;
166 static LOSFreeChunks*
167 get_from_size_list (LOSFreeChunks **list, size_t size)
169 LOSFreeChunks *free_chunks = NULL;
171 size_t i, num_chunks, start_index;
174 g_assert ((size & (LOS_CHUNK_SIZE - 1)) == 0);
178 if (free_chunks->size >= size)
180 list = &(*list)->next_size;
186 *list = free_chunks->next_size;
188 if (free_chunks->size > size)
189 add_free_chunk ((LOSFreeChunks*)((char*)free_chunks + size), free_chunks->size - size);
191 num_chunks = size >> LOS_CHUNK_BITS;
193 section = LOS_SECTION_FOR_OBJ (free_chunks);
195 start_index = LOS_CHUNK_INDEX (free_chunks, section);
196 for (i = start_index; i < start_index + num_chunks; ++i) {
197 g_assert (section->free_chunk_map [i]);
198 section->free_chunk_map [i] = 0;
201 section->num_free_chunks -= size >> LOS_CHUNK_BITS;
202 g_assert (section->num_free_chunks >= 0);
208 randomize_los_object_start (gpointer addr, size_t obj_size, size_t alloced_size, size_t addr_alignment)
211 if (alloced_size != obj_size) {
213 * We want to get a random offset between 0 and (alloced_size - obj_size)
214 * We do a prime multiplication to avoid usage of functions which might not
215 * be thread/signal safe (like rand ()). We subtract 1 to avoid common
216 * power by 2 factors.
218 offset = SGEN_ALIGN_DOWN ((((size_t)addr - 1) * 2654435761u) % (alloced_size - obj_size));
220 SGEN_ASSERT (0, (alloced_size - obj_size) < addr_alignment, "Why are we wasting one entire chunk for a los object ?");
221 /* Randomize the location within the reserved chunks to improve cache performance */
222 return (LOSObject*)((guint8*)addr + offset);
227 get_los_section_memory (size_t size)
230 LOSFreeChunks *free_chunks;
232 size_t obj_size = size;
234 size = SGEN_ALIGN_UP_TO (size, LOS_CHUNK_SIZE);
236 num_chunks = size >> LOS_CHUNK_BITS;
238 g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
239 g_assert (num_chunks > 0);
242 if (num_chunks >= LOS_NUM_FAST_SIZES) {
243 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
246 for (i = num_chunks; i < LOS_NUM_FAST_SIZES; ++i) {
247 free_chunks = get_from_size_list (&los_fast_free_lists [i], size);
252 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
256 return randomize_los_object_start (free_chunks, obj_size, size, LOS_CHUNK_SIZE);
259 if (!sgen_memgov_try_alloc_space (LOS_SECTION_SIZE, SPACE_LOS))
262 section = (LOSSection *)sgen_alloc_os_memory_aligned (LOS_SECTION_SIZE, LOS_SECTION_SIZE, (SgenAllocFlags)(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE), NULL);
267 free_chunks = (LOSFreeChunks*)((char*)section + LOS_CHUNK_SIZE);
268 free_chunks->size = LOS_SECTION_SIZE - LOS_CHUNK_SIZE;
269 free_chunks->next_size = los_fast_free_lists [0];
270 los_fast_free_lists [0] = free_chunks;
272 section->num_free_chunks = LOS_SECTION_NUM_CHUNKS;
274 section->free_chunk_map = (unsigned char*)section + sizeof (LOSSection);
275 g_assert (sizeof (LOSSection) + LOS_SECTION_NUM_CHUNKS + 1 <= LOS_CHUNK_SIZE);
276 section->free_chunk_map [0] = 0;
277 memset (section->free_chunk_map + 1, 1, LOS_SECTION_NUM_CHUNKS);
279 section->next = los_sections;
280 los_sections = section;
288 free_los_section_memory (LOSObject *obj, size_t size)
290 LOSSection *section = LOS_SECTION_FOR_OBJ (obj);
291 size_t num_chunks, i, start_index;
293 size = SGEN_ALIGN_UP_TO (size, LOS_CHUNK_SIZE);
295 num_chunks = size >> LOS_CHUNK_BITS;
297 g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
298 g_assert (num_chunks > 0);
300 section->num_free_chunks += num_chunks;
301 g_assert (section->num_free_chunks <= LOS_SECTION_NUM_CHUNKS);
304 * We could free the LOS section here if it's empty, but we
305 * can't unless we also remove its free chunks from the fast
306 * free lists. Instead, we do it in los_sweep().
309 start_index = LOS_CHUNK_INDEX (obj, section);
310 for (i = start_index; i < start_index + num_chunks; ++i) {
311 g_assert (!section->free_chunk_map [i]);
312 section->free_chunk_map [i] = 1;
315 add_free_chunk ((LOSFreeChunks*)SGEN_ALIGN_DOWN_TO ((mword)obj, LOS_CHUNK_SIZE), size);
319 sgen_los_free_object (LOSObject *obj)
321 SGEN_ASSERT (0, !obj->cardtable_mod_union, "We should never free a LOS object with a mod-union table.");
324 mword size = sgen_los_object_size (obj);
325 SGEN_LOG (4, "Freed large object %p, size %lu", obj->data, (unsigned long)size);
326 binary_protocol_empty (obj->data, size);
328 los_memory_usage -= size;
334 if (size > LOS_SECTION_OBJECT_LIMIT) {
335 int pagesize = mono_pagesize ();
336 size += sizeof (LOSObject);
337 size = SGEN_ALIGN_UP_TO (size, pagesize);
338 sgen_free_os_memory ((gpointer)SGEN_ALIGN_DOWN_TO ((mword)obj, pagesize), size, SGEN_ALLOC_HEAP);
339 sgen_memgov_release_space (size, SPACE_LOS);
341 free_los_section_memory (obj, size + sizeof (LOSObject));
342 #ifdef LOS_CONSISTENCY_CHECKS
343 los_consistency_check ();
351 * Objects with size >= MAX_SMALL_SIZE are allocated in the large object space.
352 * They are currently kept track of with a linked list.
353 * They don't move, so there is no need to pin them during collection
354 * and we avoid the memcpy overhead.
357 sgen_los_alloc_large_inner (GCVTable vtable, size_t size)
359 LOSObject *obj = NULL;
362 g_assert (size > SGEN_MAX_SMALL_OBJ_SIZE);
363 g_assert ((size & 1) == 0);
366 * size + sizeof (LOSObject) <= SSIZE_MAX - (mono_pagesize () - 1)
370 * size <= SSIZE_MAX - (mono_pagesize () - 1) - sizeof (LOSObject)
372 if (size > SSIZE_MAX - (mono_pagesize () - 1) - sizeof (LOSObject))
377 los_segment = sgen_alloc_os_memory (LOS_SEGMENT_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
378 los_segment_index = ALIGN_UP (los_segment_index);
380 obj = (LOSObject*)(los_segment + los_segment_index);
381 los_segment_index += size + sizeof (LOSObject);
382 g_assert (los_segment_index <= LOS_SEGMENT_SIZE);
384 sgen_ensure_free_space (size, GENERATION_OLD);
387 obj = malloc (size + sizeof (LOSObject));
388 memset (obj, 0, size + sizeof (LOSObject));
390 if (size > LOS_SECTION_OBJECT_LIMIT) {
391 size_t obj_size = size + sizeof (LOSObject);
392 int pagesize = mono_pagesize ();
393 size_t alloc_size = SGEN_ALIGN_UP_TO (obj_size, pagesize);
394 if (sgen_memgov_try_alloc_space (alloc_size, SPACE_LOS)) {
395 obj = (LOSObject *)sgen_alloc_os_memory (alloc_size, (SgenAllocFlags)(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE), NULL);
397 obj = randomize_los_object_start (obj, obj_size, alloc_size, pagesize);
400 obj = get_los_section_memory (size + sizeof (LOSObject));
402 memset (obj, 0, size + sizeof (LOSObject));
408 g_assert (!((mword)obj->data & (SGEN_ALLOC_ALIGN - 1)));
410 vtslot = (void**)obj->data;
412 sgen_update_heap_boundaries ((mword)obj->data, (mword)obj->data + size);
413 obj->next = los_object_list;
415 * We need a memory barrier so we don't expose as head of the los object list
416 * a LOSObject that doesn't have its fields initialized.
418 mono_memory_write_barrier ();
419 los_object_list = obj;
420 los_memory_usage += size;
422 SGEN_LOG (4, "Allocated large object %p, vtable: %p (%s), size: %zd", obj->data, vtable, sgen_client_vtable_get_name (vtable), size);
423 binary_protocol_alloc (obj->data, vtable, size, sgen_client_get_provenance ());
425 #ifdef LOS_CONSISTENCY_CHECK
426 los_consistency_check ();
432 static void sgen_los_unpin_object (GCObject *data);
435 sgen_los_sweep (void)
437 LOSObject *bigobj, *prevbo;
438 LOSSection *section, *prev;
440 int num_sections = 0;
442 /* sweep the big objects list */
444 for (bigobj = los_object_list; bigobj;) {
445 SGEN_ASSERT (0, !SGEN_OBJECT_IS_PINNED (bigobj->data), "Who pinned a LOS object?");
447 if (bigobj->cardtable_mod_union) {
448 sgen_card_table_free_mod_union (bigobj->cardtable_mod_union, (char*)bigobj->data, sgen_los_object_size (bigobj));
449 bigobj->cardtable_mod_union = NULL;
452 if (sgen_los_object_is_pinned (bigobj->data)) {
453 sgen_los_unpin_object (bigobj->data);
454 sgen_update_heap_boundaries ((mword)bigobj->data, (mword)bigobj->data + sgen_los_object_size (bigobj));
457 /* not referenced anywhere, so we can free it */
459 prevbo->next = bigobj->next;
461 los_object_list = bigobj->next;
463 bigobj = bigobj->next;
464 sgen_los_free_object (to_free);
468 bigobj = bigobj->next;
471 /* Try to free memory */
472 for (i = 0; i < LOS_NUM_FAST_SIZES; ++i)
473 los_fast_free_lists [i] = NULL;
476 section = los_sections;
478 if (section->num_free_chunks == LOS_SECTION_NUM_CHUNKS) {
479 LOSSection *next = section->next;
484 sgen_free_os_memory (section, LOS_SECTION_SIZE, SGEN_ALLOC_HEAP);
485 sgen_memgov_release_space (LOS_SECTION_SIZE, SPACE_LOS);
491 for (i = 0; i <= LOS_SECTION_NUM_CHUNKS; ++i) {
492 if (section->free_chunk_map [i]) {
494 for (j = i + 1; j <= LOS_SECTION_NUM_CHUNKS && section->free_chunk_map [j]; ++j)
496 add_free_chunk ((LOSFreeChunks*)((char*)section + (i << LOS_CHUNK_BITS)), (j - i) << LOS_CHUNK_BITS);
502 section = section->next;
507 #ifdef LOS_CONSISTENCY_CHECK
508 los_consistency_check ();
512 g_print ("LOS sections: %d objects: %d usage: %d\n", num_sections, los_num_objects, los_memory_usage);
513 for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
515 LOSFreeChunks *free_chunks;
516 for (free_chunks = los_fast_free_lists [i]; free_chunks; free_chunks = free_chunks->next_size)
518 g_print (" %d: %d\n", i, num_chunks);
522 g_assert (los_num_sections == num_sections);
526 sgen_ptr_is_in_los (char *ptr, char **start)
531 for (obj = los_object_list; obj; obj = obj->next) {
532 char *end = (char*)obj->data + sgen_los_object_size (obj);
534 if (ptr >= (char*)obj->data && ptr < end) {
535 *start = (char*)obj->data;
543 sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data)
547 for (obj = los_object_list; obj; obj = obj->next)
548 cb (obj->data, sgen_los_object_size (obj), user_data);
552 sgen_los_is_valid_object (char *object)
556 for (obj = los_object_list; obj; obj = obj->next) {
557 if ((char*)obj->data == object)
564 mono_sgen_los_describe_pointer (char *ptr)
568 for (obj = los_object_list; obj; obj = obj->next) {
569 const char *los_kind;
573 if ((char*)obj->data > ptr || (char*)obj->data + sgen_los_object_size (obj) <= ptr)
576 size = sgen_los_object_size (obj);
577 pinned = sgen_los_object_is_pinned (obj->data);
579 if (size > LOS_SECTION_OBJECT_LIMIT)
580 los_kind = "huge-los-ptr";
582 los_kind = "los-ptr";
584 if ((char*)obj->data == ptr) {
585 SGEN_LOG (0, "%s (size %d pin %d)\n", los_kind, (int)size, pinned ? 1 : 0);
587 SGEN_LOG (0, "%s (interior-ptr offset %zd size %d pin %d)",
588 los_kind, ptr - (char*)obj->data, (int)size, pinned ? 1 : 0);
597 sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
600 for (obj = los_object_list; obj; obj = obj->next) {
601 GCVTable vt = SGEN_LOAD_VTABLE (obj->data);
602 if (SGEN_VTABLE_HAS_REFERENCES (vt))
603 callback ((mword)obj->data, sgen_los_object_size (obj));
608 get_cardtable_mod_union_for_object (LOSObject *obj)
610 mword size = sgen_los_object_size (obj);
611 guint8 *mod_union = obj->cardtable_mod_union;
615 mod_union = sgen_card_table_alloc_mod_union ((char*)obj->data, size);
616 other = (guint8 *)SGEN_CAS_PTR ((gpointer*)&obj->cardtable_mod_union, mod_union, NULL);
618 SGEN_ASSERT (0, obj->cardtable_mod_union == mod_union, "Why did CAS not replace?");
621 sgen_card_table_free_mod_union (mod_union, (char*)obj->data, size);
626 sgen_los_scan_card_table (CardTableScanType scan_type, ScanCopyContext ctx)
630 binary_protocol_los_card_table_scan_start (sgen_timestamp (), scan_type & CARDTABLE_SCAN_MOD_UNION);
631 for (obj = los_object_list; obj; obj = obj->next) {
635 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
638 if (scan_type & CARDTABLE_SCAN_MOD_UNION) {
639 if (!sgen_los_object_is_pinned (obj->data))
642 cards = get_cardtable_mod_union_for_object (obj);
644 if (scan_type == CARDTABLE_SCAN_MOD_UNION_PRECLEAN) {
645 guint8 *cards_preclean;
646 mword obj_size = sgen_los_object_size (obj);
647 num_cards = sgen_card_table_number_of_cards_in_range ((mword) obj->data, obj_size);
648 cards_preclean = (guint8 *)sgen_alloc_internal_dynamic (num_cards, INTERNAL_MEM_CARDTABLE_MOD_UNION, TRUE);
650 sgen_card_table_preclean_mod_union (cards, cards_preclean, num_cards);
652 cards = cards_preclean;
658 sgen_cardtable_scan_object (obj->data, sgen_los_object_size (obj), cards, ctx);
660 if (scan_type == CARDTABLE_SCAN_MOD_UNION_PRECLEAN)
661 sgen_free_internal_dynamic (cards, num_cards, INTERNAL_MEM_CARDTABLE_MOD_UNION);
663 binary_protocol_los_card_table_scan_end (sgen_timestamp (), scan_type & CARDTABLE_SCAN_MOD_UNION);
667 sgen_los_count_cards (long long *num_total_cards, long long *num_marked_cards)
670 long long total_cards = 0;
671 long long marked_cards = 0;
673 for (obj = los_object_list; obj; obj = obj->next) {
675 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) obj->data);
676 guint8 *cards_end = sgen_card_table_get_card_scan_address ((mword) obj->data + sgen_los_object_size (obj) - 1);
677 mword num_cards = (cards_end - cards) + 1;
679 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
682 total_cards += num_cards;
683 for (i = 0; i < num_cards; ++i) {
689 *num_total_cards = total_cards;
690 *num_marked_cards = marked_cards;
694 sgen_los_update_cardtable_mod_union (void)
698 for (obj = los_object_list; obj; obj = obj->next) {
699 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
701 sgen_card_table_update_mod_union (get_cardtable_mod_union_for_object (obj),
702 (char*)obj->data, sgen_los_object_size (obj), NULL);
707 sgen_los_header_for_object (GCObject *data)
710 return (LOSObject*)((char*)data - (int)(&(((LOSObject*)0)->data)));
712 return (LOSObject*)((char*)data - sizeof (LOSObject));
717 sgen_los_pin_object (GCObject *data)
719 LOSObject *obj = sgen_los_header_for_object (data);
720 obj->size = obj->size | 1;
721 binary_protocol_pin (data, (gpointer)SGEN_LOAD_VTABLE (data), sgen_safe_object_get_size (data));
725 sgen_los_unpin_object (GCObject *data)
727 LOSObject *obj = sgen_los_header_for_object (data);
728 obj->size = sgen_los_object_size (obj);
732 sgen_los_object_is_pinned (GCObject *data)
734 LOSObject *obj = sgen_los_header_for_object (data);
735 return obj->size & 1;
739 sgen_los_mark_mod_union_card (GCObject *mono_obj, void **ptr)
741 LOSObject *obj = sgen_los_header_for_object (mono_obj);
742 guint8 *mod_union = get_cardtable_mod_union_for_object (obj);
743 /* The LOSObject structure is not represented within the card space */
744 size_t offset = sgen_card_table_get_card_offset ((char*)ptr, (char*)sgen_card_table_align_pointer((char*)mono_obj));
745 SGEN_ASSERT (0, mod_union, "FIXME: optionally allocate the mod union if it's not here and CAS it in.");
746 mod_union [offset] = 1;
749 #endif /* HAVE_SGEN_GC */