Make sgen-los.c into a proper C module.
[mono.git] / mono / metadata / sgen-los.c
1 /*
2  * sgen-los.c: Simple generational GC.
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2005-2010 Novell, Inc (http://www.novell.com)
8  *
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  *
15  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
16  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
17  *
18  * Permission is hereby granted to use or copy this program
19  * for any purpose,  provided the above notices are retained on all copies.
20  * Permission to modify the code and to distribute modified code is granted,
21  * provided the above notices are retained, and a notice that the code was
22  * modified is included with the above copyright notice.
23  *
24  *
25  * Copyright 2001-2003 Ximian, Inc
26  * Copyright 2003-2010 Novell, Inc.
27  *
28  * Permission is hereby granted, free of charge, to any person obtaining
29  * a copy of this software and associated documentation files (the
30  * "Software"), to deal in the Software without restriction, including
31  * without limitation the rights to use, copy, modify, merge, publish,
32  * distribute, sublicense, and/or sell copies of the Software, and to
33  * permit persons to whom the Software is furnished to do so, subject to
34  * the following conditions:
35  *
36  * The above copyright notice and this permission notice shall be
37  * included in all copies or substantial portions of the Software.
38  *
39  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
42  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
43  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
44  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
45  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46  */
47
48 #ifdef HAVE_SGEN_GC
49
50 #include "metadata/sgen-gc.h"
51 #include "metadata/sgen-protocol.h"
52 #include "metadata/sgen-cardtable.h"
53 #include "utils/mono-mmap.h"
54
55 #define LOS_SECTION_SIZE        (1024 * 1024)
56
57 /*
58  * This shouldn't be much smaller or larger than MAX_SMALL_OBJ_SIZE.
59  * Must be at least sizeof (LOSSection).
60  */
61 #define LOS_CHUNK_SIZE          4096
62 #define LOS_CHUNK_BITS          12
63
64 /* Largest object that can be allocated in a section. */
65 #define LOS_SECTION_OBJECT_LIMIT        (LOS_SECTION_SIZE - LOS_CHUNK_SIZE - sizeof (LOSObject))
66 //#define LOS_SECTION_OBJECT_LIMIT      0
67 #define LOS_SECTION_NUM_CHUNKS          ((LOS_SECTION_SIZE >> LOS_CHUNK_BITS) - 1)
68
69 #define LOS_SECTION_FOR_OBJ(obj)        ((LOSSection*)((mword)(obj) & ~(mword)(LOS_SECTION_SIZE - 1)))
70 #define LOS_CHUNK_INDEX(obj,section)    (((char*)(obj) - (char*)(section)) >> LOS_CHUNK_BITS)
71
72 #define LOS_NUM_FAST_SIZES              32
73
74 typedef struct _LOSFreeChunks LOSFreeChunks;
75 struct _LOSFreeChunks {
76         LOSFreeChunks *next_size;
77         size_t size;
78 };
79
80 typedef struct _LOSSection LOSSection;
81 struct _LOSSection {
82         LOSSection *next;
83         int num_free_chunks;
84         unsigned char *free_chunk_map;
85 };
86
87 LOSObject *los_object_list = NULL;
88 mword los_memory_usage = 0;
89 mword last_los_memory_usage = 0;
90
91 static LOSSection *los_sections = NULL;
92 static LOSFreeChunks *los_fast_free_lists [LOS_NUM_FAST_SIZES]; /* 0 is for larger sizes */
93 static mword los_num_objects = 0;
94 static int los_num_sections = 0;
95 static mword next_los_collection = 2*1024*1024; /* 2 MB, need to tune */
96
97 //#define USE_MALLOC
98 //#define LOS_CONSISTENCY_CHECK
99 //#define LOS_DUMMY
100
101 #ifdef LOS_DUMMY
102 #define LOS_SEGMENT_SIZE        (4096 * 1024)
103
104 static char *los_segment = NULL;
105 static int los_segment_index = 0;
106 #endif
107
108 #ifdef LOS_CONSISTENCY_CHECK
109 static void
110 los_consistency_check (void)
111 {
112         LOSSection *section;
113         LOSObject *obj;
114         int i;
115         mword memory_usage = 0;
116
117         for (obj = los_object_list; obj; obj = obj->next) {
118                 char *end = obj->data + obj->size;
119                 int start_index, num_chunks;
120
121                 memory_usage += obj->size;
122
123                 if (obj->size > LOS_SECTION_OBJECT_LIMIT)
124                         continue;
125
126                 section = LOS_SECTION_FOR_OBJ (obj);
127
128                 g_assert (end <= (char*)section + LOS_SECTION_SIZE);
129
130                 start_index = LOS_CHUNK_INDEX (obj, section);
131                 num_chunks = (obj->size + sizeof (LOSObject) + LOS_CHUNK_SIZE - 1) >> LOS_CHUNK_BITS;
132                 for (i = start_index; i < start_index + num_chunks; ++i)
133                         g_assert (!section->free_chunk_map [i]);
134         }
135
136         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
137                 LOSFreeChunks *size_chunks;
138                 for (size_chunks = los_fast_free_lists [i]; size_chunks; size_chunks = size_chunks->next_size) {
139                         LOSSection *section = LOS_SECTION_FOR_OBJ (size_chunks);
140                         int j, num_chunks, start_index;
141
142                         if (i == 0)
143                                 g_assert (size_chunks->size >= LOS_NUM_FAST_SIZES * LOS_CHUNK_SIZE);
144                         else
145                                 g_assert (size_chunks->size == i * LOS_CHUNK_SIZE);
146
147                         num_chunks = size_chunks->size >> LOS_CHUNK_BITS;
148                         start_index = LOS_CHUNK_INDEX (size_chunks, section);
149                         for (j = start_index; j < start_index + num_chunks; ++j)
150                                 g_assert (section->free_chunk_map [j]);
151                 }
152         }
153
154         g_assert (los_memory_usage == memory_usage);
155 }
156 #endif
157
158 static void
159 add_free_chunk (LOSFreeChunks *free_chunks, size_t size)
160 {
161         int num_chunks = size >> LOS_CHUNK_BITS;
162
163         free_chunks->size = size;
164
165         if (num_chunks >= LOS_NUM_FAST_SIZES)
166                 num_chunks = 0;
167         free_chunks->next_size = los_fast_free_lists [num_chunks];
168         los_fast_free_lists [num_chunks] = free_chunks;
169 }
170
171 static LOSFreeChunks*
172 get_from_size_list (LOSFreeChunks **list, size_t size)
173 {
174         LOSFreeChunks *free_chunks = NULL;
175         LOSSection *section;
176         int num_chunks, i, start_index;
177
178         g_assert ((size & (LOS_CHUNK_SIZE - 1)) == 0);
179
180         while (*list) {
181                 free_chunks = *list;
182                 if (free_chunks->size >= size)
183                         break;
184                 list = &(*list)->next_size;
185         }
186
187         if (!*list)
188                 return NULL;
189
190         *list = free_chunks->next_size;
191
192         if (free_chunks->size > size)
193                 add_free_chunk ((LOSFreeChunks*)((char*)free_chunks + size), free_chunks->size - size);
194
195         num_chunks = size >> LOS_CHUNK_BITS;
196
197         section = LOS_SECTION_FOR_OBJ (free_chunks);
198
199         start_index = LOS_CHUNK_INDEX (free_chunks, section);
200         for (i = start_index; i < start_index + num_chunks; ++i) {
201                 g_assert (section->free_chunk_map [i]);
202                 section->free_chunk_map [i] = 0;
203         }
204
205         section->num_free_chunks -= size >> LOS_CHUNK_BITS;
206         g_assert (section->num_free_chunks >= 0);
207
208         return free_chunks;
209 }
210
211 static LOSObject*
212 get_los_section_memory (size_t size)
213 {
214         LOSSection *section;
215         LOSFreeChunks *free_chunks;
216         int num_chunks;
217
218         size += LOS_CHUNK_SIZE - 1;
219         size &= ~(LOS_CHUNK_SIZE - 1);
220
221         num_chunks = size >> LOS_CHUNK_BITS;
222
223         g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
224         g_assert (num_chunks > 0);
225
226  retry:
227         if (num_chunks >= LOS_NUM_FAST_SIZES) {
228                 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
229         } else {
230                 int i;
231                 for (i = num_chunks; i < LOS_NUM_FAST_SIZES; ++i) {
232                         free_chunks = get_from_size_list (&los_fast_free_lists [i], size);
233                         if (free_chunks)
234                                 break;
235                 }
236                 if (!free_chunks)
237                         free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
238         }
239
240         if (free_chunks)
241                 return (LOSObject*)free_chunks;
242
243         if (!mono_sgen_try_alloc_space (LOS_SECTION_SIZE, SPACE_LOS))
244                 return NULL;
245
246         section = mono_sgen_alloc_os_memory_aligned (LOS_SECTION_SIZE, LOS_SECTION_SIZE, TRUE);
247
248         free_chunks = (LOSFreeChunks*)((char*)section + LOS_CHUNK_SIZE);
249         free_chunks->size = LOS_SECTION_SIZE - LOS_CHUNK_SIZE;
250         free_chunks->next_size = los_fast_free_lists [0];
251         los_fast_free_lists [0] = free_chunks;
252
253         section->num_free_chunks = LOS_SECTION_NUM_CHUNKS;
254
255         section->free_chunk_map = (unsigned char*)section + sizeof (LOSSection);
256         g_assert (sizeof (LOSSection) + LOS_SECTION_NUM_CHUNKS + 1 <= LOS_CHUNK_SIZE);
257         section->free_chunk_map [0] = 0;
258         memset (section->free_chunk_map + 1, 1, LOS_SECTION_NUM_CHUNKS);
259
260         section->next = los_sections;
261         los_sections = section;
262
263         ++los_num_sections;
264
265         goto retry;
266 }
267
268 static void
269 free_los_section_memory (LOSObject *obj, size_t size)
270 {
271         LOSSection *section = LOS_SECTION_FOR_OBJ (obj);
272         int num_chunks, i, start_index;
273
274         size += LOS_CHUNK_SIZE - 1;
275         size &= ~(LOS_CHUNK_SIZE - 1);
276
277         num_chunks = size >> LOS_CHUNK_BITS;
278
279         g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
280         g_assert (num_chunks > 0);
281
282         section->num_free_chunks += num_chunks;
283         g_assert (section->num_free_chunks <= LOS_SECTION_NUM_CHUNKS);
284
285         /*
286          * We could free the LOS section here if it's empty, but we
287          * can't unless we also remove its free chunks from the fast
288          * free lists.  Instead, we do it in los_sweep().
289          */
290
291         start_index = LOS_CHUNK_INDEX (obj, section);
292         for (i = start_index; i < start_index + num_chunks; ++i) {
293                 g_assert (!section->free_chunk_map [i]);
294                 section->free_chunk_map [i] = 1;
295         }
296
297         add_free_chunk ((LOSFreeChunks*)obj, size);
298 }
299
300 static int pagesize;
301
302 void
303 mono_sgen_los_free_object (LOSObject *obj)
304 {
305 #ifndef LOS_DUMMY
306         size_t size = obj->size;
307         DEBUG (4, fprintf (gc_debug_file, "Freed large object %p, size %lu\n", obj->data, (unsigned long)obj->size));
308         binary_protocol_empty (obj->data, obj->size);
309
310         los_memory_usage -= size;
311         los_num_objects--;
312
313 #ifdef USE_MALLOC
314         free (obj);
315 #else
316         if (size > LOS_SECTION_OBJECT_LIMIT) {
317                 if (!pagesize)
318                         pagesize = mono_pagesize ();
319                 size += sizeof (LOSObject);
320                 size += pagesize - 1;
321                 size &= ~(pagesize - 1);
322                 mono_sgen_free_os_memory (obj, size);
323                 mono_sgen_release_space (size, SPACE_LOS);
324         } else {
325                 free_los_section_memory (obj, size + sizeof (LOSObject));
326 #ifdef LOS_CONSISTENCY_CHECKS
327                 los_consistency_check ();
328 #endif
329         }
330 #endif
331 #endif
332 }
333
334 /*
335  * Objects with size >= MAX_SMALL_SIZE are allocated in the large object space.
336  * They are currently kept track of with a linked list.
337  * They don't move, so there is no need to pin them during collection
338  * and we avoid the memcpy overhead.
339  */
340 void*
341 mono_sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size)
342 {
343         LOSObject *obj = NULL;
344         void **vtslot;
345
346         g_assert (size > SGEN_MAX_SMALL_OBJ_SIZE);
347
348 #ifdef LOS_DUMMY
349         if (!los_segment)
350                 los_segment = mono_sgen_alloc_os_memory (LOS_SEGMENT_SIZE, TRUE);
351         los_segment_index = ALIGN_UP (los_segment_index);
352
353         obj = (LOSObject*)(los_segment + los_segment_index);
354         los_segment_index += size + sizeof (LOSObject);
355         g_assert (los_segment_index <= LOS_SEGMENT_SIZE);
356 #else
357         if (mono_sgen_need_major_collection (size)) {
358                 DEBUG (4, fprintf (gc_debug_file, "Should trigger major collection: req size %zd (los already: %lu, limit: %lu)\n", size, (unsigned long)los_memory_usage, (unsigned long)next_los_collection));
359                 sgen_collect_major_no_lock ("LOS overflow");
360         }
361
362 #ifdef USE_MALLOC
363         obj = malloc (size + sizeof (LOSObject));
364         memset (obj, 0, size + sizeof (LOSObject));
365 #else
366         if (size > LOS_SECTION_OBJECT_LIMIT) {
367                 size_t alloc_size = size;
368                 if (!pagesize)
369                         pagesize = mono_pagesize ();
370                 alloc_size += sizeof (LOSObject);
371                 alloc_size += pagesize - 1;
372                 alloc_size &= ~(pagesize - 1);
373                 if (mono_sgen_try_alloc_space (alloc_size, SPACE_LOS)) {
374                         obj = mono_sgen_alloc_os_memory (alloc_size, TRUE);
375                         obj->huge_object = TRUE;
376                 }
377         } else {
378                 obj = get_los_section_memory (size + sizeof (LOSObject));
379                 if (obj)
380                         memset (obj, 0, size + sizeof (LOSObject));
381         }
382 #endif
383 #endif
384         if (!obj)
385                 return NULL;
386         g_assert (!((mword)obj->data & (SGEN_ALLOC_ALIGN - 1)));
387         obj->size = size;
388         vtslot = (void**)obj->data;
389         *vtslot = vtable;
390         mono_sgen_update_heap_boundaries ((mword)obj->data, (mword)obj->data + size);
391         obj->next = los_object_list;
392         los_object_list = obj;
393         los_memory_usage += size;
394         los_num_objects++;
395         DEBUG (4, fprintf (gc_debug_file, "Allocated large object %p, vtable: %p (%s), size: %zd\n", obj->data, vtable, vtable->klass->name, size));
396         binary_protocol_alloc (obj->data, vtable, size);
397
398 #ifdef LOS_CONSISTENCY_CHECK
399         los_consistency_check ();
400 #endif
401
402         return obj->data;
403 }
404
405 void
406 mono_sgen_los_sweep (void)
407 {
408         LOSSection *section, *prev;
409         int i;
410         int num_sections = 0;
411
412         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i)
413                 los_fast_free_lists [i] = NULL;
414
415         prev = NULL;
416         section = los_sections;
417         while (section) {
418                 if (section->num_free_chunks == LOS_SECTION_NUM_CHUNKS) {
419                         LOSSection *next = section->next;
420                         if (prev)
421                                 prev->next = next;
422                         else
423                                 los_sections = next;
424                         mono_sgen_free_os_memory (section, LOS_SECTION_SIZE);
425                         mono_sgen_release_space (LOS_SECTION_SIZE, SPACE_LOS);
426                         section = next;
427                         --los_num_sections;
428                         continue;
429                 }
430
431                 for (i = 0; i <= LOS_SECTION_NUM_CHUNKS; ++i) {
432                         if (section->free_chunk_map [i]) {
433                                 int j;
434                                 for (j = i + 1; j <= LOS_SECTION_NUM_CHUNKS && section->free_chunk_map [j]; ++j)
435                                         ;
436                                 add_free_chunk ((LOSFreeChunks*)((char*)section + (i << LOS_CHUNK_BITS)), (j - i) << LOS_CHUNK_BITS);
437                                 i = j - 1;
438                         }
439                 }
440
441                 prev = section;
442                 section = section->next;
443
444                 ++num_sections;
445         }
446
447 #ifdef LOS_CONSISTENCY_CHECK
448         los_consistency_check ();
449 #endif
450
451         /*
452         g_print ("LOS sections: %d  objects: %d  usage: %d\n", num_sections, los_num_objects, los_memory_usage);
453         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
454                 int num_chunks = 0;
455                 LOSFreeChunks *free_chunks;
456                 for (free_chunks = los_fast_free_lists [i]; free_chunks; free_chunks = free_chunks->next_size)
457                         ++num_chunks;
458                 g_print ("  %d: %d\n", i, num_chunks);
459         }
460         */
461
462         g_assert (los_num_sections == num_sections);
463 }
464
465 gboolean
466 mono_sgen_ptr_is_in_los (char *ptr, char **start)
467 {
468         LOSObject *obj;
469
470         *start = NULL;
471         for (obj = los_object_list; obj; obj = obj->next) {
472                 char *end = obj->data + obj->size;
473
474                 if (ptr >= obj->data && ptr < end) {
475                         *start = obj->data;
476                         return TRUE;
477                 }
478         }
479         return FALSE;
480 }
481
482 void
483 mono_sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data)
484 {
485         LOSObject *obj;
486
487         for (obj = los_object_list; obj; obj = obj->next)
488                 cb (obj->data, obj->size, user_data);
489 }
490
491 void
492 mono_sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
493 {
494         LOSObject *obj;
495         for (obj = los_object_list; obj; obj = obj->next) {
496                 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj->data);
497                 if (vt->klass->has_references)
498                         callback ((mword)obj->data, (mword)obj->size);
499         }
500 }
501
502 void
503 mono_sgen_los_scan_card_table (SgenGrayQueue *queue)
504 {
505         LOSObject *obj;
506
507         for (obj = los_object_list; obj; obj = obj->next) {
508                 sgen_cardtable_scan_object (obj->data, obj->size, NULL, queue);
509         }
510 }
511
512 #endif /* HAVE_SGEN_GC */