test_op_il_seq_point.sh now properly tests AOT compilation.
[mono.git] / mono / metadata / sgen-descriptor.c
1 /*
2  * sgen-descriptor.c: GC descriptors describe object layout.
3  *
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
8  *
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;
12  *
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.
17  *
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.
21  */
22 #include "config.h"
23 #ifdef HAVE_SGEN_GC
24
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_PTHREAD_H
29 #include <pthread.h>
30 #endif
31 #ifdef HAVE_SEMAPHORE_H
32 #include <semaphore.h>
33 #endif
34 #include <stdio.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <assert.h>
38 #ifdef __MACH__
39 #undef _XOPEN_SOURCE
40 #endif
41 #ifdef __MACH__
42 #define _XOPEN_SOURCE
43 #endif
44
45 #include "utils/mono-counters.h"
46 #include "metadata/sgen-gc.h"
47
48 #define MAX_USER_DESCRIPTORS 16
49
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))
52
53
54 static gsize* complex_descriptors = NULL;
55 static int complex_descriptors_size = 0;
56 static int complex_descriptors_next = 0;
57 static MonoGCRootMarkFunc user_descriptors [MAX_USER_DESCRIPTORS];
58 static int user_descriptors_next = 0;
59 static void *all_ref_root_descrs [32];
60
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];
64 #endif
65
66 static int
67 alloc_complex_descriptor (gsize *bitmap, int numbits)
68 {
69         int nwords, res, i;
70
71         numbits = ALIGN_TO (numbits, GC_BITS_PER_WORD);
72         nwords = numbits / GC_BITS_PER_WORD + 1;
73
74         sgen_gc_lock ();
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).
79          */
80         for (i = 0; i < complex_descriptors_next; ) {
81                 if (complex_descriptors [i] == nwords) {
82                         int j, found = TRUE;
83                         for (j = 0; j < nwords - 1; ++j) {
84                                 if (complex_descriptors [i + 1 + j] != bitmap [j]) {
85                                         found = FALSE;
86                                         break;
87                                 }
88                         }
89                         if (found) {
90                                 sgen_gc_unlock ();
91                                 return i;
92                         }
93                 }
94                 i += (int)complex_descriptors [i];
95         }
96         if (complex_descriptors_next + nwords > complex_descriptors_size) {
97                 int new_size = complex_descriptors_size * 2 + nwords;
98                 complex_descriptors = g_realloc (complex_descriptors, new_size * sizeof (gsize));
99                 complex_descriptors_size = new_size;
100         }
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]);
107         }
108         sgen_gc_unlock ();
109         return res;
110 }
111
112 gsize*
113 sgen_get_complex_descriptor (mword desc)
114 {
115         return complex_descriptors + (desc >> LOW_TYPE_BITS);
116 }
117
118 /*
119  * Descriptor builders.
120  */
121 void*
122 mono_gc_make_descr_for_string (gsize *bitmap, int numbits)
123 {
124         return (void*)SGEN_DESC_STRING;
125 }
126
127 void*
128 mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size)
129 {
130         int first_set = -1, num_set = 0, last_set = -1, i;
131         mword desc = 0;
132         size_t stored_size = obj_size;
133
134         stored_size += SGEN_ALLOC_ALIGN - 1;
135         stored_size &= ~(SGEN_ALLOC_ALIGN - 1);
136
137         for (i = 0; i < numbits; ++i) {
138                 if (bitmap [i / GC_BITS_PER_WORD] & ((gsize)1 << (i % GC_BITS_PER_WORD))) {
139                         if (first_set < 0)
140                                 first_set = i;
141                         last_set = i;
142                         num_set++;
143                 }
144         }
145
146         if (first_set < 0) {
147                 SGEN_LOG (6, "Ptrfree descriptor %p, size: %zd", (void*)desc, stored_size);
148                 if (stored_size <= MAX_RUNLEN_OBJECT_SIZE && stored_size <= SGEN_MAX_SMALL_OBJ_SIZE)
149                         return (void*)(DESC_TYPE_SMALL_PTRFREE | stored_size);
150                 return (void*)DESC_TYPE_COMPLEX_PTRFREE;
151         }
152
153         g_assert (!(stored_size & 0x7));
154
155         SGEN_ASSERT (5, stored_size == SGEN_ALIGN_UP (stored_size), "Size is not aligned");
156
157         /* we know the 2-word header is ptr-free */
158         if (last_set < BITMAP_NUM_BITS + OBJECT_HEADER_WORDS && stored_size <= SGEN_MAX_SMALL_OBJ_SIZE) {
159                 desc = DESC_TYPE_BITMAP | ((*bitmap >> OBJECT_HEADER_WORDS) << LOW_TYPE_BITS);
160                 SGEN_LOG (6, "Largebitmap descriptor %p, size: %zd, last set: %d", (void*)desc, stored_size, last_set);
161                 return (void*) desc;
162         }
163
164         if (stored_size <= MAX_RUNLEN_OBJECT_SIZE && stored_size <= SGEN_MAX_SMALL_OBJ_SIZE) {
165                 /* check run-length encoding first: one byte offset, one byte number of pointers
166                  * on 64 bit archs, we can have 3 runs, just one on 32.
167                  * It may be better to use nibbles.
168                  */
169                 if (first_set < 256 && num_set < 256 && (first_set + num_set == last_set + 1)) {
170                         desc = DESC_TYPE_RUN_LENGTH | stored_size | (first_set << 16) | (num_set << 24);
171                         SGEN_LOG (6, "Runlen descriptor %p, size: %zd, first set: %d, num set: %d", (void*)desc, stored_size, first_set, num_set);
172                         return (void*) desc;
173                 }
174         }
175
176         /* it's a complex object ... */
177         desc = DESC_TYPE_COMPLEX | (alloc_complex_descriptor (bitmap, last_set + 1) << LOW_TYPE_BITS);
178         return (void*) desc;
179 }
180
181 /* If the array holds references, numbits == 1 and the first bit is set in elem_bitmap */
182 void*
183 mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size)
184 {
185         int first_set = -1, num_set = 0, last_set = -1, i;
186         mword desc = DESC_TYPE_VECTOR | (vector ? VECTOR_KIND_SZARRAY : VECTOR_KIND_ARRAY);
187         for (i = 0; i < numbits; ++i) {
188                 if (elem_bitmap [i / GC_BITS_PER_WORD] & ((gsize)1 << (i % GC_BITS_PER_WORD))) {
189                         if (first_set < 0)
190                                 first_set = i;
191                         last_set = i;
192                         num_set++;
193                 }
194         }
195
196         if (first_set < 0) {
197                 if (elem_size <= MAX_ELEMENT_SIZE)
198                         return (void*)(desc | VECTOR_SUBTYPE_PTRFREE | (elem_size << VECTOR_ELSIZE_SHIFT));
199                 return (void*)DESC_TYPE_COMPLEX_PTRFREE;
200         }
201
202         if (elem_size <= MAX_ELEMENT_SIZE) {
203                 desc |= elem_size << VECTOR_ELSIZE_SHIFT;
204                 if (!num_set) {
205                         return (void*)(desc | VECTOR_SUBTYPE_PTRFREE);
206                 }
207                 /* Note: we also handle structs with just ref fields */
208                 if (num_set * sizeof (gpointer) == elem_size) {
209                         return (void*)(desc | VECTOR_SUBTYPE_REFS | ((gssize)(-1) << 16));
210                 }
211                 /* FIXME: try run-len first */
212                 /* Note: we can't skip the object header here, because it's not present */
213                 if (last_set < VECTOR_BITMAP_SIZE) {
214                         return (void*)(desc | VECTOR_SUBTYPE_BITMAP | (*elem_bitmap << 16));
215                 }
216         }
217         /* it's am array of complex structs ... */
218         desc = DESC_TYPE_COMPLEX_ARR;
219         desc |= alloc_complex_descriptor (elem_bitmap, last_set + 1) << LOW_TYPE_BITS;
220         return (void*) desc;
221 }
222
223 /* Return the bitmap encoded by a descriptor */
224 gsize*
225 mono_gc_get_bitmap_for_descr (void *descr, int *numbits)
226 {
227         mword d = (mword)descr;
228         gsize *bitmap;
229
230         switch (d & DESC_TYPE_MASK) {
231         case DESC_TYPE_RUN_LENGTH: {            
232                 int first_set = (d >> 16) & 0xff;
233                 int num_set = (d >> 24) & 0xff;
234                 int i;
235
236                 bitmap = g_new0 (gsize, (first_set + num_set + 7) / 8);
237
238                 for (i = first_set; i < first_set + num_set; ++i)
239                         bitmap [i / GC_BITS_PER_WORD] |= ((gsize)1 << (i % GC_BITS_PER_WORD));
240
241                 *numbits = first_set + num_set;
242
243                 return bitmap;
244         }
245
246         case DESC_TYPE_BITMAP: {
247                 gsize bmap = (d >> LOW_TYPE_BITS) << OBJECT_HEADER_WORDS;
248
249                 bitmap = g_new0 (gsize, 1);
250                 bitmap [0] = bmap;
251                 *numbits = 0;
252                 while (bmap) {
253                         (*numbits) ++;
254                         bmap >>= 1;
255                 }
256                 return bitmap;
257         }
258
259         case DESC_TYPE_COMPLEX: {
260                 gsize *bitmap_data = sgen_get_complex_descriptor (d);
261                 int bwords = (int)(*bitmap_data) - 1;//Max scalar object size is 1Mb, which means up to 32k descriptor words
262                 int i;
263
264                 bitmap = g_new0 (gsize, bwords);
265                 *numbits = bwords * GC_BITS_PER_WORD;
266
267                 for (i = 0; i < bwords; ++i) {
268                         bitmap [i] = bitmap_data [i + 1];
269                 }
270
271                 return bitmap;
272         }
273
274         default:
275                 g_assert_not_reached ();
276         }
277 }
278
279 void*
280 mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
281 {
282         if (numbits == 0) {
283                 return (void*)MAKE_ROOT_DESC (ROOT_DESC_BITMAP, 0);
284         } else if (numbits < ((sizeof (*bitmap) * 8) - ROOT_DESC_TYPE_SHIFT)) {
285                 return (void*)MAKE_ROOT_DESC (ROOT_DESC_BITMAP, bitmap [0]);
286         } else {
287                 mword complex = alloc_complex_descriptor (bitmap, numbits);
288                 return (void*)MAKE_ROOT_DESC (ROOT_DESC_COMPLEX, complex);
289         }
290 }
291
292 void*
293 mono_gc_make_root_descr_all_refs (int numbits)
294 {
295         gsize *gc_bitmap;
296         void *descr;
297         int num_bytes = numbits / 8;
298
299         if (numbits < 32 && all_ref_root_descrs [numbits])
300                 return all_ref_root_descrs [numbits];
301
302         gc_bitmap = g_malloc0 (ALIGN_TO (ALIGN_TO (numbits, 8) + 1, sizeof (gsize)));
303         memset (gc_bitmap, 0xff, num_bytes);
304         if (numbits < ((sizeof (*gc_bitmap) * 8) - ROOT_DESC_TYPE_SHIFT)) 
305                 gc_bitmap[0] = GUINT64_TO_LE(gc_bitmap[0]);
306         else if (numbits && num_bytes % (sizeof (*gc_bitmap)))
307                 gc_bitmap[num_bytes / 8] = GUINT64_TO_LE(gc_bitmap [num_bytes / 8]);
308         if (numbits % 8)
309                 gc_bitmap [numbits / 8] = (1 << (numbits % 8)) - 1;
310         descr = mono_gc_make_descr_from_bitmap (gc_bitmap, numbits);
311         g_free (gc_bitmap);
312
313         if (numbits < 32)
314                 all_ref_root_descrs [numbits] = descr;
315
316         return descr;
317 }
318
319 void*
320 mono_gc_make_root_descr_user (MonoGCRootMarkFunc marker)
321 {
322         void *descr;
323
324         g_assert (user_descriptors_next < MAX_USER_DESCRIPTORS);
325         descr = (void*)MAKE_ROOT_DESC (ROOT_DESC_USER, (mword)user_descriptors_next);
326         user_descriptors [user_descriptors_next ++] = marker;
327
328         return descr;
329 }
330
331 void*
332 sgen_get_complex_descriptor_bitmap (mword desc)
333 {
334         return complex_descriptors + (desc >> ROOT_DESC_TYPE_SHIFT);
335 }
336
337 MonoGCRootMarkFunc
338 sgen_get_user_descriptor_func (mword desc)
339 {
340         return user_descriptors [desc >> ROOT_DESC_TYPE_SHIFT];
341 }
342
343 #ifdef HEAVY_STATISTICS
344 void
345 sgen_descriptor_count_scanned_object (mword desc)
346 {
347         int type = desc & DESC_TYPE_MASK;
348         SGEN_ASSERT (0, type, "Descriptor type can't be zero");
349         ++stat_scanned_count_per_descriptor [type - 1];
350 }
351
352 void
353 sgen_descriptor_count_copied_object (mword desc)
354 {
355         int type = desc & DESC_TYPE_MASK;
356         SGEN_ASSERT (0, type, "Descriptor type can't be zero");
357         ++stat_copied_count_per_descriptor [type - 1];
358 }
359 #endif
360
361 void
362 sgen_init_descriptors (void)
363 {
364 #ifdef HEAVY_STATISTICS
365         mono_counters_register ("# scanned RUN_LENGTH", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_RUN_LENGTH - 1]);
366         mono_counters_register ("# scanned SMALL_PTRFREE", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_SMALL_PTRFREE - 1]);
367         mono_counters_register ("# scanned COMPLEX", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_COMPLEX - 1]);
368         mono_counters_register ("# scanned VECTOR", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_VECTOR - 1]);
369         mono_counters_register ("# scanned BITMAP", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_BITMAP - 1]);
370         mono_counters_register ("# scanned COMPLEX_ARR", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_COMPLEX_ARR - 1]);
371         mono_counters_register ("# scanned COMPLEX_PTRFREE", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_scanned_count_per_descriptor [DESC_TYPE_COMPLEX_PTRFREE - 1]);
372
373         mono_counters_register ("# copied RUN_LENGTH", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_RUN_LENGTH - 1]);
374         mono_counters_register ("# copied SMALL_PTRFREE", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_SMALL_PTRFREE - 1]);
375         mono_counters_register ("# copied COMPLEX", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_COMPLEX - 1]);
376         mono_counters_register ("# copied VECTOR", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_VECTOR - 1]);
377         mono_counters_register ("# copied BITMAP", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_BITMAP - 1]);
378         mono_counters_register ("# copied COMPLEX_ARR", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_COMPLEX_ARR - 1]);
379         mono_counters_register ("# copied COMPLEX_PTRFREE", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_copied_count_per_descriptor [DESC_TYPE_COMPLEX_PTRFREE - 1]);
380 #endif
381 }
382
383 #endif