[sgen] Have an allocated block list for fixed-heap M&S, too.
[mono.git] / mono / metadata / sgen-marksweep.c
1 /*
2  * sgen-marksweep.c: Simple generational GC.
3  *
4  * Author:
5  *      Mark Probst <mark.probst@gmail.com>
6  *
7  * Copyright 2009-2010 Novell, Inc.
8  * 
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #ifdef HAVE_SGEN_GC
30
31 #include <math.h>
32 #include <errno.h>
33
34 #include "utils/mono-counters.h"
35 #include "utils/mono-semaphore.h"
36 #include "utils/mono-time.h"
37 #include "metadata/object-internals.h"
38 #include "metadata/profiler-private.h"
39
40 #include "metadata/sgen-gc.h"
41 #include "metadata/sgen-protocol.h"
42 #include "metadata/sgen-cardtable.h"
43 #include "metadata/gc-internal.h"
44
45 #define MS_BLOCK_SIZE   (16*1024)
46 #define MS_BLOCK_SIZE_SHIFT     14
47 #define MAJOR_SECTION_SIZE      MS_BLOCK_SIZE
48 #define CARDS_PER_BLOCK (MS_BLOCK_SIZE / CARD_SIZE_IN_BYTES)
49
50 #ifdef FIXED_HEAP
51 #define MS_DEFAULT_HEAP_NUM_BLOCKS      (32 * 1024) /* 512 MB */
52 #endif
53
54 /*
55  * Don't allocate single blocks, but alloc a contingent of this many
56  * blocks in one swoop.
57  */
58 #define MS_BLOCK_ALLOC_NUM      32
59
60 /*
61  * Number of bytes before the first object in a block.  At the start
62  * of a block is the MSBlockHeader, then opional padding, then come
63  * the objects, so this must be >= sizeof (MSBlockHeader).
64  */
65 #ifdef FIXED_HEAP
66 #define MS_BLOCK_SKIP   0
67 #else
68 #define MS_BLOCK_SKIP   16
69 #endif
70
71 #define MS_BLOCK_FREE   (MS_BLOCK_SIZE - MS_BLOCK_SKIP)
72
73 #define MS_NUM_MARK_WORDS       ((MS_BLOCK_SIZE / SGEN_ALLOC_ALIGN + sizeof (mword) * 8 - 1) / (sizeof (mword) * 8))
74
75 #if SGEN_MAX_SMALL_OBJ_SIZE > MS_BLOCK_FREE / 2
76 #error MAX_SMALL_OBJ_SIZE must be at most MS_BLOCK_FREE / 2
77 #endif
78
79 typedef struct _MSBlockInfo MSBlockInfo;
80 struct _MSBlockInfo {
81         int obj_size;
82         int obj_size_index;
83         int pin_queue_num_entries;
84         unsigned int pinned : 1;
85         unsigned int has_references : 1;
86         unsigned int has_pinned : 1;    /* means cannot evacuate */
87         unsigned int is_to_space : 1;
88 #ifdef FIXED_HEAP
89         unsigned int used : 1;
90         unsigned int zeroed : 1;
91 #endif
92         MSBlockInfo *next;
93         char *block;
94         void **free_list;
95         MSBlockInfo *next_free;
96         void **pin_queue_start;
97         mword mark_words [MS_NUM_MARK_WORDS];
98 };
99
100 #ifdef FIXED_HEAP
101 static int ms_heap_num_blocks = MS_DEFAULT_HEAP_NUM_BLOCKS;
102
103 #define ms_heap_start   nursery_end
104 static char *ms_heap_end;
105
106 #define MS_PTR_IN_SMALL_MAJOR_HEAP(p)   ((char*)(p) >= ms_heap_start && (char*)(p) < ms_heap_end)
107
108 /* array of all all block infos in the system */
109 static MSBlockInfo *block_infos;
110 #endif
111
112 #define MS_BLOCK_OBJ(b,i)               ((b)->block + MS_BLOCK_SKIP + (b)->obj_size * (i))
113 #define MS_BLOCK_DATA_FOR_OBJ(o)        ((char*)((mword)(o) & ~(mword)(MS_BLOCK_SIZE - 1)))
114
115 #ifdef FIXED_HEAP
116 #define MS_BLOCK_FOR_OBJ(o)             (&block_infos [(mword)((char*)(o) - ms_heap_start) >> MS_BLOCK_SIZE_SHIFT])
117 #else
118 typedef struct {
119         MSBlockInfo *info;
120 } MSBlockHeader;
121
122 #define MS_BLOCK_FOR_OBJ(o)             (((MSBlockHeader*)MS_BLOCK_DATA_FOR_OBJ ((o)))->info)
123 #endif
124
125 #define MS_BLOCK_OBJ_INDEX(o,b) (((char*)(o) - ((b)->block + MS_BLOCK_SKIP)) / (b)->obj_size)
126
127 #define MS_CALC_MARK_BIT(w,b,o)         do {                            \
128                 int i = ((char*)(o) - MS_BLOCK_DATA_FOR_OBJ ((o))) >> SGEN_ALLOC_ALIGN_BITS; \
129                 if (sizeof (mword) == 4) {                              \
130                         (w) = i >> 5;                                   \
131                         (b) = i & 31;                                   \
132                 } else {                                                \
133                         (w) = i >> 6;                                   \
134                         (b) = i & 63;                                   \
135                 }                                                       \
136         } while (0)
137
138 #define MS_MARK_BIT(bl,w,b)     ((bl)->mark_words [(w)] & (1L << (b)))
139 #define MS_SET_MARK_BIT(bl,w,b) ((bl)->mark_words [(w)] |= (1L << (b)))
140 #define MS_PAR_SET_MARK_BIT(was_marked,bl,w,b)  do {                    \
141                 mword __old = (bl)->mark_words [(w)];                   \
142                 mword __bitmask = 1L << (b);                            \
143                 if (__old & __bitmask) {                                \
144                         was_marked = TRUE;                              \
145                         break;                                          \
146                 }                                                       \
147                 if (SGEN_CAS_PTR ((gpointer*)&(bl)->mark_words [(w)],   \
148                                                 (gpointer)(__old | __bitmask), \
149                                                 (gpointer)__old) ==     \
150                                 (gpointer)__old) {                      \
151                         was_marked = FALSE;                             \
152                         break;                                          \
153                 }                                                       \
154         } while (1)
155
156 #define MS_OBJ_ALLOCED(o,b)     (*(void**)(o) && (*(char**)(o) < (b)->block || *(char**)(o) >= (b)->block + MS_BLOCK_SIZE))
157
158 #define MS_BLOCK_OBJ_SIZE_FACTOR        (sqrt (2.0))
159
160 /*
161  * This way we can lookup block object size indexes for sizes up to
162  * 256 bytes with a single load.
163  */
164 #define MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES      32
165
166 static int *block_obj_sizes;
167 static int num_block_obj_sizes;
168 static int fast_block_obj_size_indexes [MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES];
169
170 #define MS_BLOCK_FLAG_PINNED    1
171 #define MS_BLOCK_FLAG_REFS      2
172
173 #define MS_BLOCK_TYPE_MAX       4
174
175 #ifdef SGEN_PARALLEL_MARK
176 static LOCK_DECLARE (ms_block_list_mutex);
177 #define LOCK_MS_BLOCK_LIST pthread_mutex_lock (&ms_block_list_mutex)
178 #define UNLOCK_MS_BLOCK_LIST pthread_mutex_unlock (&ms_block_list_mutex)
179 #else
180 #define LOCK_MS_BLOCK_LIST
181 #define UNLOCK_MS_BLOCK_LIST
182 #endif
183
184 /* we get this at init */
185 static int nursery_bits;
186 static char *nursery_start;
187 static char *nursery_end;
188
189 static gboolean *evacuate_block_obj_sizes;
190 static float evacuation_threshold = 0.666;
191
192 static gboolean concurrent_sweep = FALSE;
193 static gboolean have_swept;
194
195 #define ptr_in_nursery(p)       (SGEN_PTR_IN_NURSERY ((p), nursery_bits, nursery_start, nursery_end))
196
197 /* all allocated blocks in the system */
198 static MSBlockInfo *all_blocks;
199
200 #ifdef FIXED_HEAP
201 /* non-allocated block free-list */
202 static MSBlockInfo *empty_blocks = NULL;
203 #else
204 /* non-allocated block free-list */
205 static void *empty_blocks = NULL;
206 static int num_empty_blocks = 0;
207 #endif
208
209 #define FOREACH_BLOCK(bl)       for ((bl) = all_blocks; (bl); (bl) = (bl)->next) {
210 #define END_FOREACH_BLOCK       }
211
212 static int num_major_sections = 0;
213 /* one free block list for each block object size */
214 static MSBlockInfo **free_block_lists [MS_BLOCK_TYPE_MAX];
215
216 static long long stat_major_blocks_alloced = 0;
217 static long long stat_major_blocks_freed = 0;
218 static long long stat_major_objects_evacuated = 0;
219 static long long stat_time_wait_for_sweep = 0;
220
221 static gboolean ms_sweep_in_progress = FALSE;
222 static pthread_t ms_sweep_thread;
223 static MonoSemType ms_sweep_cmd_semaphore;
224 static MonoSemType ms_sweep_done_semaphore;
225
226 static void
227 ms_signal_sweep_command (void)
228 {
229         if (!concurrent_sweep)
230                 return;
231
232         g_assert (!ms_sweep_in_progress);
233         ms_sweep_in_progress = TRUE;
234         MONO_SEM_POST (&ms_sweep_cmd_semaphore);
235 }
236
237 static void
238 ms_signal_sweep_done (void)
239 {
240         if (!concurrent_sweep)
241                 return;
242
243         MONO_SEM_POST (&ms_sweep_done_semaphore);
244 }
245
246 static void
247 ms_wait_for_sweep_done (void)
248 {
249         SGEN_TV_DECLARE (atv);
250         SGEN_TV_DECLARE (btv);
251         int result;
252
253         if (!concurrent_sweep)
254                 return;
255
256         if (!ms_sweep_in_progress)
257                 return;
258
259         SGEN_TV_GETTIME (atv);
260         while ((result = MONO_SEM_WAIT (&ms_sweep_done_semaphore)) != 0) {
261                 if (errno != EINTR)
262                         g_error ("MONO_SEM_WAIT");
263         }
264         SGEN_TV_GETTIME (btv);
265         stat_time_wait_for_sweep += SGEN_TV_ELAPSED_MS (atv, btv);
266
267         g_assert (ms_sweep_in_progress);
268         ms_sweep_in_progress = FALSE;
269 }
270
271 static int
272 ms_find_block_obj_size_index (int size)
273 {
274         int i;
275         DEBUG (9, g_assert (size <= SGEN_MAX_SMALL_OBJ_SIZE));
276         for (i = 0; i < num_block_obj_sizes; ++i)
277                 if (block_obj_sizes [i] >= size)
278                         return i;
279         g_assert_not_reached ();
280 }
281
282 #define FREE_BLOCKS(p,r) (free_block_lists [((p) ? MS_BLOCK_FLAG_PINNED : 0) | ((r) ? MS_BLOCK_FLAG_REFS : 0)])
283
284 #define MS_BLOCK_OBJ_SIZE_INDEX(s)                              \
285         (((s)+7)>>3 < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES ?      \
286          fast_block_obj_size_indexes [((s)+7)>>3] :             \
287          ms_find_block_obj_size_index ((s)))
288
289 #ifdef FIXED_HEAP
290 static void*
291 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
292 {
293         char *heap_start;
294         mword major_heap_size = ms_heap_num_blocks * MS_BLOCK_SIZE;
295         mword alloc_size = nursery_size + major_heap_size;
296         int i;
297
298         g_assert (ms_heap_num_blocks > 0);
299         g_assert (nursery_size % MS_BLOCK_SIZE == 0);
300         if (nursery_align)
301                 g_assert (nursery_align % MS_BLOCK_SIZE == 0);
302
303         nursery_start = mono_sgen_alloc_os_memory_aligned (alloc_size, nursery_align ? nursery_align : MS_BLOCK_SIZE, TRUE);
304         nursery_end = heap_start = nursery_start + nursery_size;
305         nursery_bits = the_nursery_bits;
306
307         ms_heap_end = heap_start + major_heap_size;
308
309         block_infos = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo) * ms_heap_num_blocks, INTERNAL_MEM_MS_BLOCK_INFO);
310
311         for (i = 0; i < ms_heap_num_blocks; ++i) {
312                 block_infos [i].block = heap_start + i * MS_BLOCK_SIZE;
313                 if (i < ms_heap_num_blocks - 1)
314                         block_infos [i].next_free = &block_infos [i + 1];
315                 else
316                         block_infos [i].next_free = NULL;
317                 block_infos [i].zeroed = TRUE;
318         }
319
320         empty_blocks = &block_infos [0];
321
322         return nursery_start;
323 }
324 #else
325 static void*
326 major_alloc_heap (mword nursery_size, mword nursery_align, int the_nursery_bits)
327 {
328         if (nursery_align)
329                 nursery_start = mono_sgen_alloc_os_memory_aligned (nursery_size, nursery_align, TRUE);
330         else
331                 nursery_start = mono_sgen_alloc_os_memory (nursery_size, TRUE);
332
333         nursery_end = nursery_start + nursery_size;
334         nursery_bits = the_nursery_bits;
335
336         return nursery_start;
337 }
338 #endif
339
340 static void
341 update_heap_boundaries_for_block (MSBlockInfo *block)
342 {
343         mono_sgen_update_heap_boundaries ((mword)block->block, (mword)block->block + MS_BLOCK_SIZE);
344 }
345
346 #ifdef FIXED_HEAP
347 static MSBlockInfo*
348 ms_get_empty_block (void)
349 {
350         MSBlockInfo *block;
351
352         g_assert (empty_blocks);
353
354         block = empty_blocks;
355         empty_blocks = empty_blocks->next_free;
356
357         block->used = TRUE;
358
359         if (!block->zeroed)
360                 memset (block->block, 0, MS_BLOCK_SIZE);
361
362         return block;
363 }
364
365 static void
366 ms_free_block (MSBlockInfo *block)
367 {
368         block->next_free = empty_blocks;
369         empty_blocks = block;
370         block->used = FALSE;
371         block->zeroed = FALSE;
372         mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
373 }
374 #else
375 static void*
376 ms_get_empty_block (void)
377 {
378         char *p;
379         int i;
380         void *block, *empty, *next;
381
382  retry:
383         if (!empty_blocks) {
384                 p = mono_sgen_alloc_os_memory_aligned (MS_BLOCK_SIZE * MS_BLOCK_ALLOC_NUM, MS_BLOCK_SIZE, TRUE);
385
386                 for (i = 0; i < MS_BLOCK_ALLOC_NUM; ++i) {
387                         block = p;
388                         /*
389                          * We do the free list update one after the
390                          * other so that other threads can use the new
391                          * blocks as quickly as possible.
392                          */
393                         do {
394                                 empty = empty_blocks;
395                                 *(void**)block = empty;
396                         } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
397                         p += MS_BLOCK_SIZE;
398                 }
399
400                 SGEN_ATOMIC_ADD (num_empty_blocks, MS_BLOCK_ALLOC_NUM);
401
402                 stat_major_blocks_alloced += MS_BLOCK_ALLOC_NUM;
403         }
404
405         do {
406                 empty = empty_blocks;
407                 if (!empty)
408                         goto retry;
409                 block = empty;
410                 next = *(void**)block;
411         } while (SGEN_CAS_PTR (&empty_blocks, next, empty) != empty);
412
413         SGEN_ATOMIC_ADD (num_empty_blocks, -1);
414
415         *(void**)block = NULL;
416
417         g_assert (!((mword)block & (MS_BLOCK_SIZE - 1)));
418
419         return block;
420 }
421
422 static void
423 ms_free_block (void *block)
424 {
425         void *empty;
426
427         mono_sgen_release_space (MS_BLOCK_SIZE, SPACE_MAJOR);
428         memset (block, 0, MS_BLOCK_SIZE);
429
430         do {
431                 empty = empty_blocks;
432                 *(void**)block = empty;
433         } while (SGEN_CAS_PTR (&empty_blocks, block, empty) != empty);
434
435         SGEN_ATOMIC_ADD (num_empty_blocks, 1);
436 }
437 #endif
438
439 //#define MARKSWEEP_CONSISTENCY_CHECK
440
441 #ifdef MARKSWEEP_CONSISTENCY_CHECK
442 static void
443 check_block_free_list (MSBlockInfo *block, int size, gboolean pinned)
444 {
445         MSBlockInfo *b;
446
447         for (; block; block = block->next_free) {
448                 g_assert (block->obj_size == size);
449                 g_assert ((pinned && block->pinned) || (!pinned && !block->pinned));
450
451                 /* blocks in the free lists must have at least
452                    one free slot */
453                 g_assert (block->free_list);
454
455 #ifdef FIXED_HEAP
456                 /* the block must not be in the empty_blocks list */
457                 for (b = empty_blocks; b; b = b->next_free)
458                         g_assert (b != block);
459 #endif
460                 /* the block must be in the all_blocks list */
461                 for (b = all_blocks; b; b = b->next) {
462                         if (b == block)
463                                 break;
464                 }
465                 g_assert (b == block);
466         }
467 }
468
469 static void
470 check_empty_blocks (void)
471 {
472 #ifndef FIXED_HEAP
473         void *p;
474         int i = 0;
475         for (p = empty_blocks; p; p = *(void**)p)
476                 ++i;
477         g_assert (i == num_empty_blocks);
478 #endif
479 }
480
481 static void
482 consistency_check (void)
483 {
484         MSBlockInfo *block;
485         int i;
486
487         /* check all blocks */
488         FOREACH_BLOCK (block) {
489                 int count = MS_BLOCK_FREE / block->obj_size;
490                 int num_free = 0;
491                 void **free;
492
493 #ifndef FIXED_HEAP
494                 /* check block header */
495                 g_assert (((MSBlockHeader*)block->block)->info == block);
496 #endif
497
498                 /* count number of free slots */
499                 for (i = 0; i < count; ++i) {
500                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
501                         if (!MS_OBJ_ALLOCED (obj, block))
502                                 ++num_free;
503                 }
504
505                 /* check free list */
506                 for (free = block->free_list; free; free = (void**)*free) {
507                         g_assert (MS_BLOCK_FOR_OBJ (free) == block);
508                         --num_free;
509                 }
510                 g_assert (num_free == 0);
511
512                 /* check all mark words are zero */
513                 for (i = 0; i < MS_NUM_MARK_WORDS; ++i)
514                         g_assert (block->mark_words [i] == 0);
515         } END_FOREACH_BLOCK;
516
517         /* check free blocks */
518         for (i = 0; i < num_block_obj_sizes; ++i) {
519                 int j;
520                 for (j = 0; j < MS_BLOCK_TYPE_MAX; ++j)
521                         check_block_free_list (free_block_lists [j][i], block_obj_sizes [i], j & MS_BLOCK_FLAG_PINNED);
522         }
523
524         check_empty_blocks ();
525 }
526 #endif
527
528 static gboolean
529 ms_alloc_block (int size_index, gboolean pinned, gboolean has_references)
530 {
531         int size = block_obj_sizes [size_index];
532         int count = MS_BLOCK_FREE / size;
533         MSBlockInfo *info;
534 #ifndef FIXED_HEAP
535         MSBlockHeader *header;
536 #endif
537         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
538         char *obj_start;
539         int i;
540
541         if (!mono_sgen_try_alloc_space (MS_BLOCK_SIZE, SPACE_MAJOR))
542                 return FALSE;
543
544 #ifdef FIXED_HEAP
545         info = ms_get_empty_block ();
546 #else
547         info = mono_sgen_alloc_internal (INTERNAL_MEM_MS_BLOCK_INFO);
548 #endif
549
550         DEBUG (9, g_assert (count >= 2));
551
552         info->obj_size = size;
553         info->obj_size_index = size_index;
554         info->pinned = pinned;
555         info->has_references = has_references;
556         info->has_pinned = pinned;
557         info->is_to_space = (mono_sgen_get_current_collection_generation () == GENERATION_OLD);
558 #ifndef FIXED_HEAP
559         info->block = ms_get_empty_block ();
560
561         header = (MSBlockHeader*) info->block;
562         header->info = info;
563 #endif
564
565         update_heap_boundaries_for_block (info);
566
567         /* build free list */
568         obj_start = info->block + MS_BLOCK_SKIP;
569         info->free_list = (void**)obj_start;
570         /* we're skipping the last one - it must be nulled */
571         for (i = 0; i < count - 1; ++i) {
572                 char *next_obj_start = obj_start + size;
573                 *(void**)obj_start = next_obj_start;
574                 obj_start = next_obj_start;
575         }
576         /* the last one */
577         *(void**)obj_start = NULL;
578
579         info->next_free = free_blocks [size_index];
580         free_blocks [size_index] = info;
581
582         info->next = all_blocks;
583         all_blocks = info;
584
585         ++num_major_sections;
586         return TRUE;
587 }
588
589 static gboolean
590 obj_is_from_pinned_alloc (char *obj)
591 {
592         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
593         return block->pinned;
594 }
595
596 static void*
597 alloc_obj (int size, gboolean pinned, gboolean has_references)
598 {
599         int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
600         MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references);
601         MSBlockInfo *block;
602         void *obj;
603
604         /* FIXME: try to do this without locking */
605
606         LOCK_MS_BLOCK_LIST;
607
608         g_assert (!ms_sweep_in_progress);
609
610         if (!free_blocks [size_index]) {
611                 if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references))) {
612                         UNLOCK_MS_BLOCK_LIST;
613                         return NULL;
614                 }
615         }
616
617         block = free_blocks [size_index];
618         DEBUG (9, g_assert (block));
619
620         obj = block->free_list;
621         DEBUG (9, g_assert (obj));
622
623         block->free_list = *(void**)obj;
624         if (!block->free_list) {
625                 free_blocks [size_index] = block->next_free;
626                 block->next_free = NULL;
627         }
628
629         UNLOCK_MS_BLOCK_LIST;
630
631         /*
632          * FIXME: This should not be necessary because it'll be
633          * overwritten by the vtable immediately.
634          */
635         *(void**)obj = NULL;
636
637         return obj;
638 }
639
640 static void*
641 major_alloc_object (int size, gboolean has_references)
642 {
643         return alloc_obj (size, FALSE, has_references);
644 }
645
646 /*
647  * We're not freeing the block if it's empty.  We leave that work for
648  * the next major collection.
649  *
650  * This is just called from the domain clearing code, which runs in a
651  * single thread and has the GC lock, so we don't need an extra lock.
652  */
653 static void
654 free_object (char *obj, size_t size, gboolean pinned)
655 {
656         MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj);
657         int word, bit;
658         DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned)));
659         DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
660         MS_CALC_MARK_BIT (word, bit, obj);
661         DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
662         if (!block->free_list) {
663                 MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references);
664                 int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size);
665                 DEBUG (9, g_assert (!block->next_free));
666                 block->next_free = free_blocks [size_index];
667                 free_blocks [size_index] = block;
668         }
669         memset (obj, 0, size);
670         *(void**)obj = block->free_list;
671         block->free_list = (void**)obj;
672 }
673
674 static void
675 major_free_non_pinned_object (char *obj, size_t size)
676 {
677         free_object (obj, size, FALSE);
678 }
679
680 /* size is a multiple of SGEN_ALLOC_ALIGN */
681 static void*
682 major_alloc_small_pinned_obj (size_t size, gboolean has_references)
683 {
684         void *res;
685
686         ms_wait_for_sweep_done ();
687
688         res = alloc_obj (size, TRUE, has_references);
689          /*If we failed to alloc memory, we better try releasing memory
690           *as pinned alloc is requested by the runtime.
691           */
692          if (!res) {
693                  sgen_collect_major_no_lock ("pinned alloc failure");
694                  res = alloc_obj (size, TRUE, has_references);
695          }
696          return res;
697 }
698
699 static void
700 free_pinned_object (char *obj, size_t size)
701 {
702         free_object (obj, size, TRUE);
703 }
704
705 /*
706  * size is already rounded up and we hold the GC lock.
707  */
708 static void*
709 major_alloc_degraded (MonoVTable *vtable, size_t size)
710 {
711         void *obj;
712         int old_num_sections;
713
714         ms_wait_for_sweep_done ();
715
716         old_num_sections = num_major_sections;
717
718         obj = alloc_obj (size, FALSE, vtable->klass->has_references);
719         if (G_LIKELY (obj)) {
720                 *(MonoVTable**)obj = vtable;
721                 HEAVY_STAT (++stat_objects_alloced_degraded);
722                 HEAVY_STAT (stat_bytes_alloced_degraded += size);
723                 g_assert (num_major_sections >= old_num_sections);
724                 mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections);
725         }
726         return obj;
727 }
728
729 #define MAJOR_OBJ_IS_IN_TO_SPACE(obj)   FALSE
730
731 /*
732  * obj is some object.  If it's not in the major heap (i.e. if it's in
733  * the nursery or LOS), return FALSE.  Otherwise return whether it's
734  * been marked or copied.
735  */
736 static gboolean
737 major_is_object_live (char *obj)
738 {
739         MSBlockInfo *block;
740         int word, bit;
741 #ifndef FIXED_HEAP
742         mword objsize;
743 #endif
744
745         if (ptr_in_nursery (obj))
746                 return FALSE;
747
748 #ifdef FIXED_HEAP
749         /* LOS */
750         if (!MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
751                 return FALSE;
752 #else
753         objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
754
755         /* LOS */
756         if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
757                 return FALSE;
758 #endif
759
760         /* now we know it's in a major block */
761         block = MS_BLOCK_FOR_OBJ (obj);
762         DEBUG (9, g_assert (!block->pinned));
763         MS_CALC_MARK_BIT (word, bit, obj);
764         return MS_MARK_BIT (block, word, bit) ? TRUE : FALSE;
765 }
766
767 static gboolean
768 major_ptr_is_in_non_pinned_space (char *ptr)
769 {
770         MSBlockInfo *block;
771
772         FOREACH_BLOCK (block) {
773                 if (ptr >= (char*)block && ptr <= (char*)block + MS_BLOCK_SIZE)
774                         return TRUE;
775         } END_FOREACH_BLOCK;
776         return FALSE;
777 }
778
779 static void
780 major_iterate_objects (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data)
781 {
782         MSBlockInfo *block;
783
784         ms_wait_for_sweep_done ();
785
786         FOREACH_BLOCK (block) {
787                 int count = MS_BLOCK_FREE / block->obj_size;
788                 int i;
789
790                 if (block->pinned && !pinned)
791                         continue;
792                 if (!block->pinned && !non_pinned)
793                         continue;
794
795                 for (i = 0; i < count; ++i) {
796                         void **obj = (void**) MS_BLOCK_OBJ (block, i);
797                         if (MS_OBJ_ALLOCED (obj, block))
798                                 callback ((char*)obj, block->obj_size, data);
799                 }
800         } END_FOREACH_BLOCK;
801 }
802
803 static void
804 major_check_scan_starts (void)
805 {
806 }
807
808 static void
809 major_dump_heap (FILE *heap_dump_file)
810 {
811         MSBlockInfo *block;
812         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
813         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
814         int i;
815
816         for (i = 0; i < num_block_obj_sizes; ++i)
817                 slots_available [i] = slots_used [i] = 0;
818
819         FOREACH_BLOCK (block) {
820                 int index = ms_find_block_obj_size_index (block->obj_size);
821                 int count = MS_BLOCK_FREE / block->obj_size;
822
823                 slots_available [index] += count;
824                 for (i = 0; i < count; ++i) {
825                         if (MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block))
826                                 ++slots_used [index];
827                 }
828         } END_FOREACH_BLOCK;
829
830         fprintf (heap_dump_file, "<occupancies>\n");
831         for (i = 0; i < num_block_obj_sizes; ++i) {
832                 fprintf (heap_dump_file, "<occupancy size=\"%d\" available=\"%d\" used=\"%d\" />\n",
833                                 block_obj_sizes [i], slots_available [i], slots_used [i]);
834         }
835         fprintf (heap_dump_file, "</occupancies>\n");
836
837         FOREACH_BLOCK (block) {
838                 int count = MS_BLOCK_FREE / block->obj_size;
839                 int i;
840                 int start = -1;
841
842                 fprintf (heap_dump_file, "<section type=\"%s\" size=\"%zu\">\n", "old", (size_t)MS_BLOCK_FREE);
843
844                 for (i = 0; i <= count; ++i) {
845                         if ((i < count) && MS_OBJ_ALLOCED (MS_BLOCK_OBJ (block, i), block)) {
846                                 if (start < 0)
847                                         start = i;
848                         } else {
849                                 if (start >= 0) {
850                                         mono_sgen_dump_occupied (MS_BLOCK_OBJ (block, start), MS_BLOCK_OBJ (block, i), block->block);
851                                         start = -1;
852                                 }
853                         }
854                 }
855
856                 fprintf (heap_dump_file, "</section>\n");
857         } END_FOREACH_BLOCK;
858 }
859
860 #define LOAD_VTABLE     SGEN_LOAD_VTABLE
861
862 #define MS_MARK_OBJECT_AND_ENQUEUE_CHECKED(obj,block,queue) do {        \
863                 int __word, __bit;                                      \
864                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
865                 if (!MS_MARK_BIT ((block), __word, __bit) && MS_OBJ_ALLOCED ((obj), (block))) { \
866                         MS_SET_MARK_BIT ((block), __word, __bit);       \
867                         if ((block)->has_references)                    \
868                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
869                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
870                 }                                                       \
871         } while (0)
872 #define MS_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {                \
873                 int __word, __bit;                                      \
874                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
875                 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block))));  \
876                 if (!MS_MARK_BIT ((block), __word, __bit)) {            \
877                         MS_SET_MARK_BIT ((block), __word, __bit);       \
878                         if ((block)->has_references)                    \
879                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
880                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
881                 }                                                       \
882         } while (0)
883 #define MS_PAR_MARK_OBJECT_AND_ENQUEUE(obj,block,queue) do {            \
884                 int __word, __bit;                                      \
885                 gboolean __was_marked;                                  \
886                 DEBUG (9, g_assert (MS_OBJ_ALLOCED ((obj), (block))));  \
887                 MS_CALC_MARK_BIT (__word, __bit, (obj));                \
888                 MS_PAR_SET_MARK_BIT (__was_marked, (block), __word, __bit); \
889                 if (!__was_marked) {                                    \
890                         if ((block)->has_references)                    \
891                                 GRAY_OBJECT_ENQUEUE ((queue), (obj));   \
892                         binary_protocol_mark ((obj), (gpointer)LOAD_VTABLE ((obj)), mono_sgen_safe_object_get_size ((MonoObject*)(obj))); \
893                 }                                                       \
894         } while (0)
895
896 #include "sgen-major-copy-object.h"
897
898 #ifdef SGEN_PARALLEL_MARK
899 static void
900 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
901 {
902         void *obj = *ptr;
903         mword vtable_word = *(mword*)obj;
904         MonoVTable *vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
905         mword objsize;
906         MSBlockInfo *block;
907
908         HEAVY_STAT (++stat_copy_object_called_major);
909
910         DEBUG (9, g_assert (obj));
911         DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
912
913         if (vtable_word & SGEN_FORWARDED_BIT) {
914                 *ptr = (void*)vt;
915                 return;
916         }
917
918         if (ptr_in_nursery (obj)) {
919                 int word, bit;
920                 gboolean has_references;
921                 void *destination;
922
923                 if (vtable_word & SGEN_PINNED_BIT)
924                         return;
925
926                 HEAVY_STAT (++stat_objects_copied_major);
927
928         do_copy_object:
929                 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
930                 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
931
932                 destination = major_alloc_object (objsize, has_references);
933                 if (G_UNLIKELY (!destination)) {
934                         if (!ptr_in_nursery (obj)) {
935                                 int size_index;
936                                 block = MS_BLOCK_FOR_OBJ (obj);
937                                 size_index = block->obj_size_index;
938                                 evacuate_block_obj_sizes [size_index] = FALSE;
939                         }
940
941                         do {
942                                 if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
943                                         mono_sgen_pin_object (obj, queue);
944                                         break;
945                                 }
946
947                                 vtable_word = *(mword*)obj;
948                                 /*someone else forwarded it, update the pointer and bail out*/
949                                 if (vtable_word & SGEN_FORWARDED_BIT) {
950                                         *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
951                                         break;
952                                 }
953
954                                 /*someone pinned it, nothing to do.*/
955                                 if (vtable_word & SGEN_PINNED_BIT)
956                                         break;
957                         } while (TRUE);
958                         return;
959                 }
960
961                 if (SGEN_CAS_PTR (obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
962                         gboolean was_marked;
963
964                         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
965                         obj = destination;
966                         *ptr = obj;
967
968                         /*
969                          * FIXME: If we make major_alloc_object() give
970                          * us the block info, too, we won't have to
971                          * re-fetch it here.
972                          */
973                         block = MS_BLOCK_FOR_OBJ (obj);
974                         MS_CALC_MARK_BIT (word, bit, obj);
975                         DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
976                         MS_PAR_SET_MARK_BIT (was_marked, block, word, bit);
977                 } else {
978                         /*
979                          * FIXME: We have allocated destination, but
980                          * we cannot use it.  Give it back to the
981                          * allocator.
982                          */
983                         *(void**)destination = NULL;
984
985                         vtable_word = *(mword*)obj;
986                         g_assert (vtable_word & SGEN_FORWARDED_BIT);
987
988                         obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
989
990                         *ptr = obj;
991                 }
992         } else {
993 #ifdef FIXED_HEAP
994                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
995 #else
996                 objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
997
998                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
999 #endif
1000                 {
1001                         int size_index;
1002
1003                         block = MS_BLOCK_FOR_OBJ (obj);
1004                         size_index = block->obj_size_index;
1005
1006                         if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1007                                 if (block->is_to_space)
1008                                         return;
1009                                 HEAVY_STAT (++stat_major_objects_evacuated);
1010                                 goto do_copy_object;
1011                         } else {
1012                                 MS_PAR_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1013                         }
1014                 } else {
1015                         if (vtable_word & SGEN_PINNED_BIT)
1016                                 return;
1017                         binary_protocol_pin (obj, vt, mono_sgen_safe_object_get_size ((MonoObject*)obj));
1018                         if (SGEN_CAS_PTR (obj, (void*)(vtable_word | SGEN_PINNED_BIT), (void*)vtable_word) == (void*)vtable_word) {
1019                                 if (SGEN_VTABLE_HAS_REFERENCES (vt))
1020                                         GRAY_OBJECT_ENQUEUE (queue, obj);
1021                         } else {
1022                                 g_assert (SGEN_OBJECT_IS_PINNED (obj));
1023                         }
1024                 }
1025         }
1026 }
1027 #else
1028 static void
1029 major_copy_or_mark_object (void **ptr, SgenGrayQueue *queue)
1030 {
1031         void *obj = *ptr;
1032         MSBlockInfo *block;
1033
1034         HEAVY_STAT (++stat_copy_object_called_major);
1035
1036         DEBUG (9, g_assert (obj));
1037         DEBUG (9, g_assert (current_collection_generation == GENERATION_OLD));
1038
1039         if (ptr_in_nursery (obj)) {
1040                 int word, bit;
1041                 char *forwarded, *old_obj;
1042
1043                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1044                         *ptr = forwarded;
1045                         return;
1046                 }
1047                 if (SGEN_OBJECT_IS_PINNED (obj))
1048                         return;
1049
1050                 HEAVY_STAT (++stat_objects_copied_major);
1051
1052         do_copy_object:
1053                 old_obj = obj;
1054                 obj = copy_object_no_checks (obj, queue);
1055                 if (G_UNLIKELY (old_obj == obj)) {
1056                         /*If we fail to evacuate an object we just stop doing it for a given block size as all other will surely fail too.*/
1057                         if (!ptr_in_nursery (obj)) {
1058                                 int size_index;
1059                                 block = MS_BLOCK_FOR_OBJ (obj);
1060                                 size_index = block->obj_size_index;
1061                                 evacuate_block_obj_sizes [size_index] = FALSE;
1062                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1063                         }
1064                         return;
1065                 }
1066                 *ptr = obj;
1067
1068                 /*
1069                  * FIXME: See comment for copy_object_no_checks().  If
1070                  * we have that, we can let the allocation function
1071                  * give us the block info, too, and we won't have to
1072                  * re-fetch it.
1073                  */
1074                 block = MS_BLOCK_FOR_OBJ (obj);
1075                 MS_CALC_MARK_BIT (word, bit, obj);
1076                 DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit)));
1077                 MS_SET_MARK_BIT (block, word, bit);
1078         } else {
1079                 char *forwarded;
1080 #ifndef FIXED_HEAP
1081                 mword objsize;
1082 #endif
1083
1084                 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
1085                         *ptr = forwarded;
1086                         return;
1087                 }
1088
1089 #ifdef FIXED_HEAP
1090                 if (MS_PTR_IN_SMALL_MAJOR_HEAP (obj))
1091 #else
1092                 objsize = SGEN_ALIGN_UP (mono_sgen_safe_object_get_size ((MonoObject*)obj));
1093
1094                 if (objsize <= SGEN_MAX_SMALL_OBJ_SIZE)
1095 #endif
1096                 {
1097                         int size_index;
1098
1099                         block = MS_BLOCK_FOR_OBJ (obj);
1100                         size_index = block->obj_size_index;
1101
1102                         if (!block->has_pinned && evacuate_block_obj_sizes [size_index]) {
1103                                 if (block->is_to_space)
1104                                         return;
1105                                 HEAVY_STAT (++stat_major_objects_evacuated);
1106                                 goto do_copy_object;
1107                         } else {
1108                                 MS_MARK_OBJECT_AND_ENQUEUE (obj, block, queue);
1109                         }
1110                 } else {
1111                         if (SGEN_OBJECT_IS_PINNED (obj))
1112                                 return;
1113                         binary_protocol_pin (obj, (gpointer)SGEN_LOAD_VTABLE (obj), mono_sgen_safe_object_get_size ((MonoObject*)obj));
1114                         SGEN_PIN_OBJECT (obj);
1115                         /* FIXME: only enqueue if object has references */
1116                         GRAY_OBJECT_ENQUEUE (queue, obj);
1117                 }
1118         }
1119 }
1120 #endif
1121
1122 #include "sgen-major-scan-object.h"
1123
1124 static void
1125 mark_pinned_objects_in_block (MSBlockInfo *block, SgenGrayQueue *queue)
1126 {
1127         int i;
1128         int last_index = -1;
1129
1130         if (!block->pin_queue_num_entries)
1131                 return;
1132
1133         block->has_pinned = TRUE;
1134
1135         for (i = 0; i < block->pin_queue_num_entries; ++i) {
1136                 int index = MS_BLOCK_OBJ_INDEX (block->pin_queue_start [i], block);
1137                 DEBUG (9, g_assert (index >= 0 && index < MS_BLOCK_FREE / block->obj_size));
1138                 if (index == last_index)
1139                         continue;
1140                 MS_MARK_OBJECT_AND_ENQUEUE_CHECKED (MS_BLOCK_OBJ (block, index), block, queue);
1141                 last_index = index;
1142         }
1143 }
1144
1145 static void
1146 ms_sweep (void)
1147 {
1148         int i;
1149         MSBlockInfo **iter;
1150
1151         /* statistics for evacuation */
1152         int *slots_available = alloca (sizeof (int) * num_block_obj_sizes);
1153         int *slots_used = alloca (sizeof (int) * num_block_obj_sizes);
1154         int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes);
1155
1156         for (i = 0; i < num_block_obj_sizes; ++i)
1157                 slots_available [i] = slots_used [i] = num_blocks [i] = 0;
1158
1159         /* clear all the free lists */
1160         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) {
1161                 MSBlockInfo **free_blocks = free_block_lists [i];
1162                 int j;
1163                 for (j = 0; j < num_block_obj_sizes; ++j)
1164                         free_blocks [j] = NULL;
1165         }
1166
1167         /* traverse all blocks, free and zero unmarked objects */
1168         iter = &all_blocks;
1169         while (*iter) {
1170                 MSBlockInfo *block = *iter;
1171                 int count;
1172                 gboolean have_live = FALSE;
1173                 gboolean has_pinned;
1174                 int obj_index;
1175                 int obj_size_index;
1176
1177                 obj_size_index = block->obj_size_index;
1178
1179                 has_pinned = block->has_pinned;
1180                 block->has_pinned = block->pinned;
1181
1182                 block->is_to_space = FALSE;
1183
1184                 count = MS_BLOCK_FREE / block->obj_size;
1185                 block->free_list = NULL;
1186
1187                 for (obj_index = 0; obj_index < count; ++obj_index) {
1188                         int word, bit;
1189                         void *obj = MS_BLOCK_OBJ (block, obj_index);
1190
1191                         MS_CALC_MARK_BIT (word, bit, obj);
1192                         if (MS_MARK_BIT (block, word, bit)) {
1193                                 DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block)));
1194                                 have_live = TRUE;
1195                                 if (!has_pinned)
1196                                         ++slots_used [obj_size_index];
1197                         } else {
1198                                 /* an unmarked object */
1199                                 if (MS_OBJ_ALLOCED (obj, block)) {
1200                                         binary_protocol_empty (obj, block->obj_size);
1201                                         memset (obj, 0, block->obj_size);
1202                                 }
1203                                 *(void**)obj = block->free_list;
1204                                 block->free_list = obj;
1205                         }
1206                 }
1207
1208                 /* reset mark bits */
1209                 memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS);
1210
1211                 /*
1212                  * FIXME: reverse free list so that it's in address
1213                  * order
1214                  */
1215
1216                 if (have_live) {
1217                         if (!has_pinned) {
1218                                 ++num_blocks [obj_size_index];
1219                                 slots_available [obj_size_index] += count;
1220                         }
1221
1222                         iter = &block->next;
1223
1224                         /*
1225                          * If there are free slots in the block, add
1226                          * the block to the corresponding free list.
1227                          */
1228                         if (block->free_list) {
1229                                 MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references);
1230                                 int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size);
1231                                 block->next_free = free_blocks [index];
1232                                 free_blocks [index] = block;
1233                         }
1234
1235                         update_heap_boundaries_for_block (block);
1236                 } else {
1237                         /*
1238                          * Blocks without live objects are removed from the
1239                          * block list and freed.
1240                          */
1241                         *iter = block->next;
1242
1243 #ifdef FIXED_HEAP
1244                         ms_free_block (block);
1245 #else
1246                         ms_free_block (block->block);
1247
1248                         mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO);
1249 #endif
1250
1251                         --num_major_sections;
1252                 }
1253         }
1254
1255         for (i = 0; i < num_block_obj_sizes; ++i) {
1256                 float usage = (float)slots_used [i] / (float)slots_available [i];
1257                 if (num_blocks [i] > 5 && usage < evacuation_threshold) {
1258                         evacuate_block_obj_sizes [i] = TRUE;
1259                         /*
1260                         g_print ("slot size %d - %d of %d used\n",
1261                                         block_obj_sizes [i], slots_used [i], slots_available [i]);
1262                         */
1263                 } else {
1264                         evacuate_block_obj_sizes [i] = FALSE;
1265                 }
1266         }
1267
1268         have_swept = TRUE;
1269 }
1270
1271 static void*
1272 ms_sweep_thread_func (void *dummy)
1273 {
1274         g_assert (concurrent_sweep);
1275
1276         for (;;) {
1277                 int result;
1278
1279                 while ((result = MONO_SEM_WAIT (&ms_sweep_cmd_semaphore)) != 0) {
1280                         if (errno != EINTR)
1281                                 g_error ("MONO_SEM_WAIT");
1282                 }
1283
1284                 ms_sweep ();
1285
1286                 ms_signal_sweep_done ();
1287         }
1288
1289         return NULL;
1290 }
1291
1292 static void
1293 major_sweep (void)
1294 {
1295         if (concurrent_sweep) {
1296                 if (!ms_sweep_thread)
1297                         pthread_create (&ms_sweep_thread, NULL, ms_sweep_thread_func, NULL);
1298
1299                 ms_signal_sweep_command ();
1300         } else {
1301                 ms_sweep ();
1302         }
1303 }
1304
1305 static int count_pinned_ref;
1306 static int count_pinned_nonref;
1307 static int count_nonpinned_ref;
1308 static int count_nonpinned_nonref;
1309
1310 static void
1311 count_nonpinned_callback (char *obj, size_t size, void *data)
1312 {
1313         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1314
1315         if (vtable->klass->has_references)
1316                 ++count_nonpinned_ref;
1317         else
1318                 ++count_nonpinned_nonref;
1319 }
1320
1321 static void
1322 count_pinned_callback (char *obj, size_t size, void *data)
1323 {
1324         MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj);
1325
1326         if (vtable->klass->has_references)
1327                 ++count_pinned_ref;
1328         else
1329                 ++count_pinned_nonref;
1330 }
1331
1332 static void __attribute__ ((unused))
1333 count_ref_nonref_objs (void)
1334 {
1335         int total;
1336
1337         count_pinned_ref = 0;
1338         count_pinned_nonref = 0;
1339         count_nonpinned_ref = 0;
1340         count_nonpinned_nonref = 0;
1341
1342         major_iterate_objects (TRUE, FALSE, count_nonpinned_callback, NULL);
1343         major_iterate_objects (FALSE, TRUE, count_pinned_callback, NULL);
1344
1345         total = count_pinned_nonref + count_nonpinned_nonref + count_pinned_ref + count_nonpinned_ref;
1346
1347         g_print ("ref: %d pinned %d non-pinned   non-ref: %d pinned %d non-pinned  --  %.1f\n",
1348                         count_pinned_ref, count_nonpinned_ref,
1349                         count_pinned_nonref, count_nonpinned_nonref,
1350                         (count_pinned_nonref + count_nonpinned_nonref) * 100.0 / total);
1351 }
1352
1353 static int
1354 ms_calculate_block_obj_sizes (double factor, int *arr)
1355 {
1356         double target_size = sizeof (MonoObject);
1357         int num_sizes = 0;
1358         int last_size = 0;
1359
1360         do {
1361                 int target_count = ceil (MS_BLOCK_FREE / target_size);
1362                 int size = MIN ((MS_BLOCK_FREE / target_count) & ~(SGEN_ALLOC_ALIGN - 1), SGEN_MAX_SMALL_OBJ_SIZE);
1363
1364                 if (size != last_size) {
1365                         if (arr)
1366                                 arr [num_sizes] = size;
1367                         ++num_sizes;
1368                         last_size = size;
1369                 }
1370
1371                 target_size *= factor;
1372         } while (last_size < SGEN_MAX_SMALL_OBJ_SIZE);
1373
1374         return num_sizes;
1375 }
1376
1377 /* only valid during minor collections */
1378 static int old_num_major_sections;
1379
1380 static void
1381 major_start_nursery_collection (void)
1382 {
1383         ms_wait_for_sweep_done ();
1384
1385 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1386         consistency_check ();
1387 #endif
1388
1389         old_num_major_sections = num_major_sections;
1390 }
1391
1392 static void
1393 major_finish_nursery_collection (void)
1394 {
1395 #ifdef MARKSWEEP_CONSISTENCY_CHECK
1396         consistency_check ();
1397 #endif
1398         mono_sgen_register_major_sections_alloced (num_major_sections - old_num_major_sections);
1399 }
1400
1401 static void
1402 major_start_major_collection (void)
1403 {
1404         int i;
1405
1406         ms_wait_for_sweep_done ();
1407
1408         /* clear the free lists */
1409         for (i = 0; i < num_block_obj_sizes; ++i) {
1410                 if (!evacuate_block_obj_sizes [i])
1411                         continue;
1412
1413                 free_block_lists [0][i] = NULL;
1414                 free_block_lists [MS_BLOCK_FLAG_REFS][i] = NULL;
1415         }
1416 }
1417
1418 static void
1419 major_finish_major_collection (void)
1420 {
1421 }
1422
1423 static void
1424 major_have_computer_minor_collection_allowance (void)
1425 {
1426 #ifndef FIXED_HEAP
1427         int section_reserve = mono_sgen_get_minor_collection_allowance () / MS_BLOCK_SIZE;
1428
1429         g_assert (have_swept);
1430         ms_wait_for_sweep_done ();
1431         g_assert (!ms_sweep_in_progress);
1432
1433         /*
1434          * FIXME: We don't free blocks on 32 bit platforms because it
1435          * can lead to address space fragmentation, since we're
1436          * allocating blocks in larger contingents.
1437          */
1438         if (sizeof (mword) < 8)
1439                 return;
1440
1441         while (num_empty_blocks > section_reserve) {
1442                 void *next = *(void**)empty_blocks;
1443                 mono_sgen_free_os_memory (empty_blocks, MS_BLOCK_SIZE);
1444                 empty_blocks = next;
1445                 /*
1446                  * Needs not be atomic because this is running
1447                  * single-threaded.
1448                  */
1449                 --num_empty_blocks;
1450
1451                 ++stat_major_blocks_freed;
1452         }
1453 #endif
1454 }
1455
1456 static void
1457 major_find_pin_queue_start_ends (SgenGrayQueue *queue)
1458 {
1459         MSBlockInfo *block;
1460
1461         FOREACH_BLOCK (block) {
1462                 block->pin_queue_start = mono_sgen_find_optimized_pin_queue_area (block->block + MS_BLOCK_SKIP, block->block + MS_BLOCK_SIZE,
1463                                 &block->pin_queue_num_entries);
1464         } END_FOREACH_BLOCK;
1465 }
1466
1467 static void
1468 major_pin_objects (SgenGrayQueue *queue)
1469 {
1470         MSBlockInfo *block;
1471
1472         FOREACH_BLOCK (block) {
1473                 mark_pinned_objects_in_block (block, queue);
1474         } END_FOREACH_BLOCK;
1475 }
1476
1477 static void
1478 major_init_to_space (void)
1479 {
1480 }
1481
1482 static void
1483 major_report_pinned_memory_usage (void)
1484 {
1485         g_assert_not_reached ();
1486 }
1487
1488 static gint64
1489 major_get_used_size (void)
1490 {
1491         gint64 size = 0;
1492         MSBlockInfo *block;
1493
1494         FOREACH_BLOCK (block) {
1495                 int count = MS_BLOCK_FREE / block->obj_size;
1496                 void **iter;
1497                 size += count * block->obj_size;
1498                 for (iter = block->free_list; iter; iter = (void**)*iter)
1499                         size -= block->obj_size;
1500         } END_FOREACH_BLOCK;
1501
1502         return size;
1503 }
1504
1505 static int
1506 get_num_major_sections (void)
1507 {
1508         return num_major_sections;
1509 }
1510
1511 static gboolean
1512 major_handle_gc_param (const char *opt)
1513 {
1514 #ifdef FIXED_HEAP
1515         if (g_str_has_prefix (opt, "major-heap-size=")) {
1516                 const char *arg = strchr (opt, '=') + 1;
1517                 glong size;
1518                 if (!mono_gc_parse_environment_string_extract_number (arg, &size))
1519                         return FALSE;
1520                 ms_heap_num_blocks = (size + MS_BLOCK_SIZE - 1) / MS_BLOCK_SIZE;
1521                 g_assert (ms_heap_num_blocks > 0);
1522                 return TRUE;
1523         } else
1524 #endif
1525         if (g_str_has_prefix (opt, "evacuation-threshold=")) {
1526                 const char *arg = strchr (opt, '=') + 1;
1527                 int percentage = atoi (arg);
1528                 if (percentage < 0 || percentage > 100) {
1529                         fprintf (stderr, "evacuation-threshold must be an integer in the range 0-100.\n");
1530                         exit (1);
1531                 }
1532                 evacuation_threshold = (float)percentage / 100.0;
1533                 return TRUE;
1534         } else if (!strcmp (opt, "concurrent-sweep")) {
1535                 concurrent_sweep = TRUE;
1536                 return TRUE;
1537         } else if (!strcmp (opt, "no-concurrent-sweep")) {
1538                 concurrent_sweep = FALSE;
1539                 return TRUE;
1540         }
1541
1542         return FALSE;
1543 }
1544
1545 static void
1546 major_print_gc_param_usage (void)
1547 {
1548         fprintf (stderr,
1549                         ""
1550 #ifdef FIXED_HEAP
1551                         "  major-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n"
1552 #endif
1553                         "  evacuation-threshold=P (where P is a percentage, an integer in 0-100)\n"
1554                         "  (no-)concurrent-sweep\n"
1555                         );
1556 }
1557
1558 #ifdef SGEN_HAVE_CARDTABLE
1559 static void
1560 major_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
1561 {
1562         MSBlockInfo *block;
1563
1564         FOREACH_BLOCK (block) {
1565                 if (block->has_references)
1566                         callback ((mword)block->block, MS_BLOCK_SIZE);
1567         } END_FOREACH_BLOCK;
1568 }
1569
1570 #define CARD_WORDS_PER_BLOCK (CARDS_PER_BLOCK / SIZEOF_VOID_P)
1571 /*
1572  * MS blocks are 16K aligned.
1573  * Cardtables are 4K aligned, at least.
1574  * This means that the cardtable of a given block is 32 bytes aligned.
1575  */
1576 static guint8*
1577 initial_skip_card (guint8 *card_data)
1578 {
1579         mword *cards = (mword*)card_data;
1580         mword card;
1581         int i;
1582         for (i = 0; i < CARD_WORDS_PER_BLOCK; ++i) {
1583                 card = cards [i];
1584                 if (card)
1585                         break;
1586         }
1587
1588         if (i == CARD_WORDS_PER_BLOCK)
1589                 return card_data + CARDS_PER_BLOCK;
1590
1591 #if defined(__i386__) && defined(__GNUC__)
1592         return card_data + i * 4 +  (__builtin_ffs (card) - 1) / 8;
1593 #elif defined(__x86_64__) && defined(__GNUC__)
1594         return card_data + i * 8 +  (__builtin_ffsll (card) - 1) / 8;
1595 #else
1596         for (i = i * SIZEOF_VOID_P; i < CARDS_PER_BLOCK; ++i) {
1597                 if (card_data [i])
1598                         return &card_data [i];
1599         }
1600         return card_data;
1601 #endif
1602 }
1603
1604
1605 static G_GNUC_UNUSED guint8*
1606 skip_card (guint8 *card_data, guint8 *card_data_end)
1607 {
1608         while (card_data < card_data_end && !*card_data)
1609                 ++card_data;
1610         return card_data;
1611 }
1612
1613 #define MS_BLOCK_OBJ_INDEX_FAST(o,b,os) (((char*)(o) - ((b) + MS_BLOCK_SKIP)) / (os))
1614 #define MS_BLOCK_OBJ_FAST(b,os,i)                       ((b) + MS_BLOCK_SKIP + (os) * (i))
1615 #define MS_OBJ_ALLOCED_FAST(o,b)                (*(void**)(o) && (*(char**)(o) < (b) || *(char**)(o) >= (b) + MS_BLOCK_SIZE))
1616
1617 static void
1618 major_scan_card_table (SgenGrayQueue *queue)
1619 {
1620         MSBlockInfo *block;
1621
1622         FOREACH_BLOCK (block) {
1623                 int block_obj_size;
1624                 char *block_start;
1625
1626                 if (!block->has_references)
1627                         continue;
1628
1629                 block_obj_size = block->obj_size;
1630                 block_start = block->block;
1631
1632                 if (block_obj_size >= CARD_SIZE_IN_BYTES) {
1633                         guint8 *cards;
1634 #ifndef SGEN_HAVE_OVERLAPPING_CARDS
1635                         guint8 cards_data [CARDS_PER_BLOCK];
1636 #endif
1637                         char *obj, *end, *base;
1638
1639                         /*We can avoid the extra copy since the remark cardtable was cleaned before */
1640 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
1641                         cards = sgen_card_table_get_card_scan_address ((mword)block_start);
1642 #else
1643                         cards = cards_data;
1644                         if (!sgen_card_table_get_card_data (cards_data, (mword)block_start, CARDS_PER_BLOCK))
1645                                 continue;
1646 #endif
1647
1648                         obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, 0);
1649                         end = block_start + MS_BLOCK_SIZE;
1650                         base = sgen_card_table_align_pointer (obj);
1651
1652                         while (obj < end) {
1653                                 if (MS_OBJ_ALLOCED_FAST (obj, block_start)) {
1654                                         int card_offset = (obj - base) >> CARD_BITS;
1655                                         sgen_cardtable_scan_object (obj, block_obj_size, cards + card_offset, queue);
1656                                 }
1657                                 obj += block_obj_size;
1658                         }
1659                 } else {
1660                         guint8 *card_data, *card_base;
1661                         guint8 *card_data_end;
1662
1663                         /*
1664                          * This is safe in face of card aliasing for the following reason:
1665                          *
1666                          * Major blocks are 16k aligned, or 32 cards aligned.
1667                          * Cards aliasing happens in powers of two, so as long as major blocks are aligned to their
1668                          * sizes, they won't overflow the cardtable overlap modulus.
1669                          */
1670                         card_data = card_base = sgen_card_table_get_card_scan_address ((mword)block_start);
1671                         card_data_end = card_data + CARDS_PER_BLOCK;
1672
1673                         for (card_data = initial_skip_card (card_data); card_data < card_data_end; ++card_data) { //card_data = skip_card (card_data + 1, card_data_end)) {
1674                                 int index;
1675                                 int idx = card_data - card_base;
1676                                 char *start = (char*)(block_start + idx * CARD_SIZE_IN_BYTES);
1677                                 char *end = start + CARD_SIZE_IN_BYTES;
1678                                 char *obj;
1679
1680                                 if (!*card_data)
1681                                         continue;
1682                                 sgen_card_table_prepare_card_for_scanning (card_data);
1683
1684                                 if (idx == 0)
1685                                         index = 0;
1686                                 else
1687                                         index = MS_BLOCK_OBJ_INDEX_FAST (start, block_start, block_obj_size);
1688
1689                                 obj = (char*)MS_BLOCK_OBJ_FAST (block_start, block_obj_size, index);
1690                                 while (obj < end) {
1691                                         if (MS_OBJ_ALLOCED_FAST (obj, block_start))
1692                                                 minor_scan_object (obj, queue);
1693                                         obj += block_obj_size;
1694                                 }
1695                         }
1696                 }
1697         } END_FOREACH_BLOCK;
1698 }
1699 #endif
1700
1701 static gboolean
1702 major_is_worker_thread (pthread_t thread)
1703 {
1704         if (concurrent_sweep)
1705                 return thread == ms_sweep_thread;
1706         else
1707                 return FALSE;
1708 }
1709
1710 #undef pthread_create
1711
1712 void
1713 #ifdef SGEN_PARALLEL_MARK
1714 #ifdef FIXED_HEAP
1715 mono_sgen_marksweep_fixed_par_init
1716 #else
1717 mono_sgen_marksweep_par_init
1718 #endif
1719 #else
1720 #ifdef FIXED_HEAP
1721 mono_sgen_marksweep_fixed_init
1722 #else
1723 mono_sgen_marksweep_init
1724 #endif
1725 #endif
1726         (SgenMajorCollector *collector)
1727 {
1728         int i;
1729
1730 #ifndef FIXED_HEAP
1731         mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo));
1732 #endif
1733
1734         num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL);
1735         block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1736         ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes);
1737
1738         evacuate_block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (gboolean) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1739         for (i = 0; i < num_block_obj_sizes; ++i)
1740                 evacuate_block_obj_sizes [i] = FALSE;
1741
1742         /*
1743         {
1744                 int i;
1745                 g_print ("block object sizes:\n");
1746                 for (i = 0; i < num_block_obj_sizes; ++i)
1747                         g_print ("%d\n", block_obj_sizes [i]);
1748         }
1749         */
1750
1751         for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i)
1752                 free_block_lists [i] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES);
1753
1754         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i)
1755                 fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8);
1756         for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i)
1757                 g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i));
1758
1759         LOCK_INIT (ms_block_list_mutex);
1760
1761         mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced);
1762         mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed);
1763         mono_counters_register ("# major objects evacuated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_objects_evacuated);
1764         mono_counters_register ("Wait for sweep time", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_time_wait_for_sweep);
1765
1766         /*
1767          * FIXME: These are superfluous if concurrent sweep is
1768          * disabled.  We might want to create them lazily.
1769          */
1770         MONO_SEM_INIT (&ms_sweep_cmd_semaphore, 0);
1771         MONO_SEM_INIT (&ms_sweep_done_semaphore, 0);
1772
1773         collector->section_size = MAJOR_SECTION_SIZE;
1774 #ifdef SGEN_PARALLEL_MARK
1775         collector->is_parallel = TRUE;
1776 #else
1777         collector->is_parallel = FALSE;
1778 #endif
1779         collector->supports_cardtable = TRUE;
1780
1781         collector->have_swept = &have_swept;
1782
1783         collector->alloc_heap = major_alloc_heap;
1784         collector->is_object_live = major_is_object_live;
1785         collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj;
1786         collector->alloc_degraded = major_alloc_degraded;
1787         collector->copy_or_mark_object = major_copy_or_mark_object;
1788         collector->alloc_object = major_alloc_object;
1789         collector->free_pinned_object = free_pinned_object;
1790         collector->iterate_objects = major_iterate_objects;
1791         collector->free_non_pinned_object = major_free_non_pinned_object;
1792         collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends;
1793         collector->pin_objects = major_pin_objects;
1794 #ifdef SGEN_HAVE_CARDTABLE
1795         collector->scan_card_table = major_scan_card_table;
1796         collector->iterate_live_block_ranges = (void*)(void*) major_iterate_live_block_ranges;
1797 #endif
1798         collector->init_to_space = major_init_to_space;
1799         collector->sweep = major_sweep;
1800         collector->check_scan_starts = major_check_scan_starts;
1801         collector->dump_heap = major_dump_heap;
1802         collector->get_used_size = major_get_used_size;
1803         collector->start_nursery_collection = major_start_nursery_collection;
1804         collector->finish_nursery_collection = major_finish_nursery_collection;
1805         collector->start_major_collection = major_start_major_collection;
1806         collector->finish_major_collection = major_finish_major_collection;
1807         collector->have_computed_minor_collection_allowance = major_have_computer_minor_collection_allowance;
1808         collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space;
1809         collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc;
1810         collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
1811         collector->get_num_major_sections = get_num_major_sections;
1812         collector->handle_gc_param = major_handle_gc_param;
1813         collector->print_gc_param_usage = major_print_gc_param_usage;
1814         collector->is_worker_thread = major_is_worker_thread;
1815
1816         FILL_COLLECTOR_COPY_OBJECT (collector);
1817         FILL_COLLECTOR_SCAN_OBJECT (collector);
1818
1819
1820         /*cardtable requires major pages to be 8 cards aligned*/
1821         g_assert ((MS_BLOCK_SIZE % (8 * CARD_SIZE_IN_BYTES)) == 0);
1822
1823 }
1824
1825 #endif