2 * sgen-descriptor.c: GC descriptors describe object layout.
4 * Copyright 2001-2003 Ximian, Inc
5 * Copyright 2003-2010 Novell, Inc.
6 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
7 * Copyright (C) 2012 Xamarin Inc
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License 2.0 as published by the Free Software Foundation;
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License 2.0 along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #ifdef HAVE_SEMAPHORE_H
32 #include <semaphore.h>
45 #include "mono/sgen/sgen-gc.h"
46 #include "mono/sgen/gc-internal-agnostic.h"
48 #define MAX_USER_DESCRIPTORS 16
50 #define MAKE_ROOT_DESC(type,val) ((type) | ((val) << ROOT_DESC_TYPE_SHIFT))
51 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
54 static gsize* complex_descriptors = NULL;
55 static int complex_descriptors_size = 0;
56 static int complex_descriptors_next = 0;
57 static SgenUserRootMarkFunc user_descriptors [MAX_USER_DESCRIPTORS];
58 static int user_descriptors_next = 0;
59 static SgenDescriptor all_ref_root_descrs [32];
61 #ifdef HEAVY_STATISTICS
62 static guint64 stat_scanned_count_per_descriptor [DESC_TYPE_MAX];
63 static guint64 stat_copied_count_per_descriptor [DESC_TYPE_MAX];
67 alloc_complex_descriptor (gsize *bitmap, int numbits)
71 numbits = ALIGN_TO (numbits, GC_BITS_PER_WORD);
72 nwords = numbits / GC_BITS_PER_WORD + 1;
75 res = complex_descriptors_next;
76 /* linear search, so we don't have duplicates with domain load/unload
77 * this should not be performance critical or we'd have bigger issues
78 * (the number and size of complex descriptors should be small).
80 for (i = 0; i < complex_descriptors_next; ) {
81 if (complex_descriptors [i] == nwords) {
83 for (j = 0; j < nwords - 1; ++j) {
84 if (complex_descriptors [i + 1 + j] != bitmap [j]) {
94 i += (int)complex_descriptors [i];
96 if (complex_descriptors_next + nwords > complex_descriptors_size) {
97 int new_size = complex_descriptors_size * 2 + nwords;
98 complex_descriptors = (gsize *)g_realloc (complex_descriptors, new_size * sizeof (gsize));
99 complex_descriptors_size = new_size;
101 SGEN_LOG (6, "Complex descriptor %d, size: %d (total desc memory: %d)", res, nwords, complex_descriptors_size);
102 complex_descriptors_next += nwords;
103 complex_descriptors [res] = nwords;
104 for (i = 0; i < nwords - 1; ++i) {
105 complex_descriptors [res + 1 + i] = bitmap [i];
106 SGEN_LOG (6, "\tvalue: %p", (void*)complex_descriptors [res + 1 + i]);
113 sgen_get_complex_descriptor (SgenDescriptor desc)
115 return complex_descriptors + (desc >> LOW_TYPE_BITS);
119 * Descriptor builders.
122 mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size)
124 int first_set = -1, num_set = 0, last_set = -1, i;
125 SgenDescriptor desc = 0;
126 size_t stored_size = obj_size;
128 stored_size += SGEN_ALLOC_ALIGN - 1;
129 stored_size &= ~(SGEN_ALLOC_ALIGN - 1);
131 for (i = 0; i < numbits; ++i) {
132 if (bitmap [i / GC_BITS_PER_WORD] & ((gsize)1 << (i % GC_BITS_PER_WORD))) {
141 SGEN_LOG (6, "Ptrfree descriptor %p, size: %zd", (void*)desc, stored_size);
142 if (stored_size <= MAX_RUNLEN_OBJECT_SIZE && stored_size <= SGEN_MAX_SMALL_OBJ_SIZE)
143 return DESC_TYPE_SMALL_PTRFREE | stored_size;
144 return DESC_TYPE_COMPLEX_PTRFREE;
147 g_assert (!(stored_size & 0x7));
149 SGEN_ASSERT (5, stored_size == SGEN_ALIGN_UP (stored_size), "Size is not aligned");
151 /* we know the 2-word header is ptr-free */
152 if (last_set < BITMAP_NUM_BITS + OBJECT_HEADER_WORDS && stored_size <= SGEN_MAX_SMALL_OBJ_SIZE) {
153 desc = DESC_TYPE_BITMAP | ((*bitmap >> OBJECT_HEADER_WORDS) << LOW_TYPE_BITS);
154 SGEN_LOG (6, "Largebitmap descriptor %p, size: %zd, last set: %d", (void*)desc, stored_size, last_set);
158 if (stored_size <= MAX_RUNLEN_OBJECT_SIZE && stored_size <= SGEN_MAX_SMALL_OBJ_SIZE) {
159 /* check run-length encoding first: one byte offset, one byte number of pointers
160 * on 64 bit archs, we can have 3 runs, just one on 32.
161 * It may be better to use nibbles.
163 if (first_set < 256 && num_set < 256 && (first_set + num_set == last_set + 1)) {
164 desc = DESC_TYPE_RUN_LENGTH | stored_size | (first_set << 16) | (num_set << 24);
165 SGEN_LOG (6, "Runlen descriptor %p, size: %zd, first set: %d, num set: %d", (void*)desc, stored_size, first_set, num_set);
170 /* it's a complex object ... */
171 desc = DESC_TYPE_COMPLEX | (alloc_complex_descriptor (bitmap, last_set + 1) << LOW_TYPE_BITS);
175 /* If the array holds references, numbits == 1 and the first bit is set in elem_bitmap */
177 mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size)
179 int first_set = -1, num_set = 0, last_set = -1, i;
180 SgenDescriptor desc = DESC_TYPE_VECTOR | (vector ? VECTOR_KIND_SZARRAY : VECTOR_KIND_ARRAY);
181 for (i = 0; i < numbits; ++i) {
182 if (elem_bitmap [i / GC_BITS_PER_WORD] & ((gsize)1 << (i % GC_BITS_PER_WORD))) {
191 if (elem_size <= MAX_ELEMENT_SIZE)
192 return desc | VECTOR_SUBTYPE_PTRFREE | (elem_size << VECTOR_ELSIZE_SHIFT);
193 return DESC_TYPE_COMPLEX_PTRFREE;
196 if (elem_size <= MAX_ELEMENT_SIZE) {
197 desc |= elem_size << VECTOR_ELSIZE_SHIFT;
199 return desc | VECTOR_SUBTYPE_PTRFREE;
201 /* Note: we also handle structs with just ref fields */
202 if (num_set * sizeof (gpointer) == elem_size) {
203 return desc | VECTOR_SUBTYPE_REFS | ((gssize)(-1) << 16);
205 /* FIXME: try run-len first */
206 /* Note: we can't skip the object header here, because it's not present */
207 if (last_set < VECTOR_BITMAP_SIZE) {
208 return desc | VECTOR_SUBTYPE_BITMAP | (*elem_bitmap << 16);
211 /* it's am array of complex structs ... */
212 desc = DESC_TYPE_COMPLEX_ARR;
213 desc |= alloc_complex_descriptor (elem_bitmap, last_set + 1) << LOW_TYPE_BITS;
217 /* Return the bitmap encoded by a descriptor */
219 mono_gc_get_bitmap_for_descr (SgenDescriptor descr, int *numbits)
221 SgenDescriptor d = (SgenDescriptor)descr;
224 switch (d & DESC_TYPE_MASK) {
225 case DESC_TYPE_RUN_LENGTH: {
226 int first_set = (d >> 16) & 0xff;
227 int num_set = (d >> 24) & 0xff;
230 bitmap = g_new0 (gsize, (first_set + num_set + 7) / 8);
232 for (i = first_set; i < first_set + num_set; ++i)
233 bitmap [i / GC_BITS_PER_WORD] |= ((gsize)1 << (i % GC_BITS_PER_WORD));
235 *numbits = first_set + num_set;
240 case DESC_TYPE_BITMAP: {
241 gsize bmap = (d >> LOW_TYPE_BITS) << OBJECT_HEADER_WORDS;
243 bitmap = g_new0 (gsize, 1);
253 case DESC_TYPE_COMPLEX: {
254 gsize *bitmap_data = sgen_get_complex_descriptor (d);
255 int bwords = (int)(*bitmap_data) - 1;//Max scalar object size is 1Mb, which means up to 32k descriptor words
258 bitmap = g_new0 (gsize, bwords);
259 *numbits = bwords * GC_BITS_PER_WORD;
261 for (i = 0; i < bwords; ++i) {
262 bitmap [i] = bitmap_data [i + 1];
269 g_assert_not_reached ();
274 mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
277 return MAKE_ROOT_DESC (ROOT_DESC_BITMAP, 0);
278 } else if (numbits < ((sizeof (*bitmap) * 8) - ROOT_DESC_TYPE_SHIFT)) {
279 return MAKE_ROOT_DESC (ROOT_DESC_BITMAP, bitmap [0]);
281 SgenDescriptor complex = alloc_complex_descriptor (bitmap, numbits);
282 return MAKE_ROOT_DESC (ROOT_DESC_COMPLEX, complex);
287 mono_gc_make_root_descr_all_refs (int numbits)
290 SgenDescriptor descr;
291 int num_bytes = numbits / 8;
293 if (numbits < 32 && all_ref_root_descrs [numbits])
294 return all_ref_root_descrs [numbits];
296 gc_bitmap = (gsize *)g_malloc0 (ALIGN_TO (ALIGN_TO (numbits, 8) + 1, sizeof (gsize)));
297 memset (gc_bitmap, 0xff, num_bytes);
298 if (numbits < ((sizeof (*gc_bitmap) * 8) - ROOT_DESC_TYPE_SHIFT))
299 gc_bitmap[0] = GUINT64_TO_LE(gc_bitmap[0]);
300 else if (numbits && num_bytes % (sizeof (*gc_bitmap)))
301 gc_bitmap[num_bytes / 8] = GUINT64_TO_LE(gc_bitmap [num_bytes / 8]);
303 gc_bitmap [numbits / 8] = (1 << (numbits % 8)) - 1;
304 descr = mono_gc_make_descr_from_bitmap (gc_bitmap, numbits);
308 all_ref_root_descrs [numbits] = descr;
314 sgen_make_user_root_descriptor (SgenUserRootMarkFunc marker)
316 SgenDescriptor descr;
318 g_assert (user_descriptors_next < MAX_USER_DESCRIPTORS);
319 descr = MAKE_ROOT_DESC (ROOT_DESC_USER, (SgenDescriptor)user_descriptors_next);
320 user_descriptors [user_descriptors_next ++] = marker;
326 sgen_get_complex_descriptor_bitmap (SgenDescriptor desc)
328 return complex_descriptors + (desc >> ROOT_DESC_TYPE_SHIFT);
332 sgen_get_user_descriptor_func (SgenDescriptor desc)
334 return user_descriptors [desc >> ROOT_DESC_TYPE_SHIFT];
337 #ifdef HEAVY_STATISTICS
339 sgen_descriptor_count_scanned_object (SgenDescriptor desc)
341 int type = desc & DESC_TYPE_MASK;
342 SGEN_ASSERT (0, type, "Descriptor type can't be zero");
343 ++stat_scanned_count_per_descriptor [type - 1];
347 sgen_descriptor_count_copied_object (SgenDescriptor desc)
349 int type = desc & DESC_TYPE_MASK;
350 SGEN_ASSERT (0, type, "Descriptor type can't be zero");
351 ++stat_copied_count_per_descriptor [type - 1];
356 sgen_init_descriptors (void)
358 #ifdef HEAVY_STATISTICS
359 mono_counters_register ("# scanned RUN_LENGTH", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_RUN_LENGTH - 1]);
360 mono_counters_register ("# scanned SMALL_PTRFREE", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_SMALL_PTRFREE - 1]);
361 mono_counters_register ("# scanned COMPLEX", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_COMPLEX - 1]);
362 mono_counters_register ("# scanned VECTOR", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_VECTOR - 1]);
363 mono_counters_register ("# scanned BITMAP", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_BITMAP - 1]);
364 mono_counters_register ("# scanned COMPLEX_ARR", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_COMPLEX_ARR - 1]);
365 mono_counters_register ("# scanned COMPLEX_PTRFREE", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_COMPLEX_PTRFREE - 1]);
367 mono_counters_register ("# copied RUN_LENGTH", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_RUN_LENGTH - 1]);
368 mono_counters_register ("# copied SMALL_PTRFREE", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_SMALL_PTRFREE - 1]);
369 mono_counters_register ("# copied COMPLEX", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_COMPLEX - 1]);
370 mono_counters_register ("# copied VECTOR", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_VECTOR - 1]);
371 mono_counters_register ("# copied BITMAP", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_BITMAP - 1]);
372 mono_counters_register ("# copied COMPLEX_ARR", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_COMPLEX_ARR - 1]);
373 mono_counters_register ("# copied COMPLEX_PTRFREE", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_COMPLEX_PTRFREE - 1]);