Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
5373 lines (4565 sloc) 163 KB
/*
* sgen-gc.c: Simple generational GC.
*
* Author:
* Paolo Molaro (lupus@ximian.com)
* Rodrigo Kumpera (kumpera@gmail.com)
*
* Copyright 2005-2011 Novell, Inc (http://www.novell.com)
* Copyright 2011 Xamarin Inc (http://www.xamarin.com)
*
* Thread start/stop adapted from Boehm's GC:
* Copyright (c) 1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996 by Silicon Graphics. All rights reserved.
* Copyright (c) 1998 by Fergus Henderson. All rights reserved.
* Copyright (c) 2000-2004 by Hewlett-Packard Company. All rights reserved.
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program
* for any purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*
* Copyright 2001-2003 Ximian, Inc
* Copyright 2003-2010 Novell, Inc.
* Copyright 2011 Xamarin, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* Important: allocation provides always zeroed memory, having to do
* a memset after allocation is deadly for performance.
* Memory usage at startup is currently as follows:
* 64 KB pinned space
* 64 KB internal space
* size of nursery
* We should provide a small memory config with half the sizes
*
* We currently try to make as few mono assumptions as possible:
* 1) 2-word header with no GC pointers in it (first vtable, second to store the
* forwarding ptr)
* 2) gc descriptor is the second word in the vtable (first word in the class)
* 3) 8 byte alignment is the minimum and enough (not true for special structures (SIMD), FIXME)
* 4) there is a function to get an object's size and the number of
* elements in an array.
* 5) we know the special way bounds are allocated for complex arrays
* 6) we know about proxies and how to treat them when domains are unloaded
*
* Always try to keep stack usage to a minimum: no recursive behaviour
* and no large stack allocs.
*
* General description.
* Objects are initially allocated in a nursery using a fast bump-pointer technique.
* When the nursery is full we start a nursery collection: this is performed with a
* copying GC.
* When the old generation is full we start a copying GC of the old generation as well:
* this will be changed to mark&sweep with copying when fragmentation becomes to severe
* in the future. Maybe we'll even do both during the same collection like IMMIX.
*
* The things that complicate this description are:
* *) pinned objects: we can't move them so we need to keep track of them
* *) no precise info of the thread stacks and registers: we need to be able to
* quickly find the objects that may be referenced conservatively and pin them
* (this makes the first issues more important)
* *) large objects are too expensive to be dealt with using copying GC: we handle them
* with mark/sweep during major collections
* *) some objects need to not move even if they are small (interned strings, Type handles):
* we use mark/sweep for them, too: they are not allocated in the nursery, but inside
* PinnedChunks regions
*/
/*
* TODO:
*) we could have a function pointer in MonoClass to implement
customized write barriers for value types
*) investigate the stuff needed to advance a thread to a GC-safe
point (single-stepping, read from unmapped memory etc) and implement it.
This would enable us to inline allocations and write barriers, for example,
or at least parts of them, like the write barrier checks.
We may need this also for handling precise info on stacks, even simple things
as having uninitialized data on the stack and having to wait for the prolog
to zero it. Not an issue for the last frame that we scan conservatively.
We could always not trust the value in the slots anyway.
*) modify the jit to save info about references in stack locations:
this can be done just for locals as a start, so that at least
part of the stack is handled precisely.
*) test/fix endianess issues
*) Implement a card table as the write barrier instead of remembered
sets? Card tables are not easy to implement with our current
memory layout. We have several different kinds of major heap
objects: Small objects in regular blocks, small objects in pinned
chunks and LOS objects. If we just have a pointer we have no way
to tell which kind of object it points into, therefore we cannot
know where its card table is. The least we have to do to make
this happen is to get rid of write barriers for indirect stores.
(See next item)
*) Get rid of write barriers for indirect stores. We can do this by
telling the GC to wbarrier-register an object once we do an ldloca
or ldelema on it, and to unregister it once it's not used anymore
(it can only travel downwards on the stack). The problem with
unregistering is that it needs to happen eventually no matter
what, even if exceptions are thrown, the thread aborts, etc.
Rodrigo suggested that we could do only the registering part and
let the collector find out (pessimistically) when it's safe to
unregister, namely when the stack pointer of the thread that
registered the object is higher than it was when the registering
happened. This might make for a good first implementation to get
some data on performance.
*) Some sort of blacklist support? Blacklists is a concept from the
Boehm GC: if during a conservative scan we find pointers to an
area which we might use as heap, we mark that area as unusable, so
pointer retention by random pinning pointers is reduced.
*) experiment with max small object size (very small right now - 2kb,
because it's tied to the max freelist size)
*) add an option to mmap the whole heap in one chunk: it makes for many
simplifications in the checks (put the nursery at the top and just use a single
check for inclusion/exclusion): the issue this has is that on 32 bit systems it's
not flexible (too much of the address space may be used by default or we can't
increase the heap as needed) and we'd need a race-free mechanism to return memory
back to the system (mprotect(PROT_NONE) will still keep the memory allocated if it
was written to, munmap is needed, but the following mmap may not find the same segment
free...)
*) memzero the major fragments after restarting the world and optionally a smaller
chunk at a time
*) investigate having fragment zeroing threads
*) separate locks for finalization and other minor stuff to reduce
lock contention
*) try a different copying order to improve memory locality
*) a thread abort after a store but before the write barrier will
prevent the write barrier from executing
*) specialized dynamically generated markers/copiers
*) Dynamically adjust TLAB size to the number of threads. If we have
too many threads that do allocation, we might need smaller TLABs,
and we might get better performance with larger TLABs if we only
have a handful of threads. We could sum up the space left in all
assigned TLABs and if that's more than some percentage of the
nursery size, reduce the TLAB size.
*) Explore placing unreachable objects on unused nursery memory.
Instead of memset'ng a region to zero, place an int[] covering it.
A good place to start is add_nursery_frag. The tricky thing here is
placing those objects atomically outside of a collection.
*) Allocation should use asymmetric Dekker synchronization:
http://blogs.oracle.com/dave/resource/Asymmetric-Dekker-Synchronization.txt
This should help weak consistency archs.
*/
#include "config.h"
#ifdef HAVE_SGEN_GC
#ifdef __MACH__
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#define _DARWIN_C_SOURCE
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#ifdef HAVE_SEMAPHORE_H
#include <semaphore.h>
#endif
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <assert.h>
#include "metadata/sgen-gc.h"
#include "metadata/metadata-internals.h"
#include "metadata/class-internals.h"
#include "metadata/gc-internal.h"
#include "metadata/object-internals.h"
#include "metadata/threads.h"
#include "metadata/sgen-cardtable.h"
#include "metadata/sgen-ssb.h"
#include "metadata/sgen-protocol.h"
#include "metadata/sgen-archdep.h"
#include "metadata/sgen-bridge.h"
#include "metadata/sgen-memory-governor.h"
#include "metadata/sgen-hash-table.h"
#include "metadata/mono-gc.h"
#include "metadata/method-builder.h"
#include "metadata/profiler-private.h"
#include "metadata/monitor.h"
#include "metadata/threadpool-internals.h"
#include "metadata/mempool-internals.h"
#include "metadata/marshal.h"
#include "metadata/runtime.h"
#include "metadata/sgen-cardtable.h"
#include "metadata/sgen-pinning.h"
#include "metadata/sgen-workers.h"
#include "utils/mono-mmap.h"
#include "utils/mono-time.h"
#include "utils/mono-semaphore.h"
#include "utils/mono-counters.h"
#include "utils/mono-proclib.h"
#include "utils/mono-memory-model.h"
#include "utils/mono-logger-internal.h"
#include "utils/dtrace.h"
#include <mono/utils/mono-logger-internal.h>
#include <mono/utils/memcheck.h>
#if defined(__MACH__)
#include "utils/mach-support.h"
#endif
#define OPDEF(a,b,c,d,e,f,g,h,i,j) \
a = i,
enum {
#include "mono/cil/opcode.def"
CEE_LAST
};
#undef OPDEF
#undef pthread_create
#undef pthread_join
#undef pthread_detach
/*
* ######################################################################
* ######## Types and constants used by the GC.
* ######################################################################
*/
/* 0 means not initialized, 1 is initialized, -1 means in progress */
static int gc_initialized = 0;
/* If set, check if we need to do something every X allocations */
gboolean has_per_allocation_action;
/* If set, do a heap check every X allocation */
guint32 verify_before_allocs = 0;
/* If set, do a minor collection before every X allocation */
guint32 collect_before_allocs = 0;
/* If set, do a whole heap check before each collection */
static gboolean whole_heap_check_before_collection = FALSE;
/* If set, do a heap consistency check before each minor collection */
static gboolean consistency_check_at_minor_collection = FALSE;
/* If set, check that there are no references to the domain left at domain unload */
static gboolean xdomain_checks = FALSE;
/* If not null, dump the heap after each collection into this file */
static FILE *heap_dump_file = NULL;
/* If set, mark stacks conservatively, even if precise marking is possible */
static gboolean conservative_stack_mark = FALSE;
/* If set, do a plausibility check on the scan_starts before and after
each collection */
static gboolean do_scan_starts_check = FALSE;
static gboolean nursery_collection_is_parallel = FALSE;
static gboolean disable_minor_collections = FALSE;
static gboolean disable_major_collections = FALSE;
gboolean do_pin_stats = FALSE;
static gboolean do_verify_nursery = FALSE;
static gboolean do_dump_nursery_content = FALSE;
#ifdef HEAVY_STATISTICS
long long stat_objects_alloced_degraded = 0;
long long stat_bytes_alloced_degraded = 0;
long long stat_copy_object_called_nursery = 0;
long long stat_objects_copied_nursery = 0;
long long stat_copy_object_called_major = 0;
long long stat_objects_copied_major = 0;
long long stat_scan_object_called_nursery = 0;
long long stat_scan_object_called_major = 0;
long long stat_slots_allocated_in_vain;
long long stat_nursery_copy_object_failed_from_space = 0;
long long stat_nursery_copy_object_failed_forwarded = 0;
long long stat_nursery_copy_object_failed_pinned = 0;
long long stat_nursery_copy_object_failed_to_space = 0;
static int stat_wbarrier_set_field = 0;
static int stat_wbarrier_set_arrayref = 0;
static int stat_wbarrier_arrayref_copy = 0;
static int stat_wbarrier_generic_store = 0;
static int stat_wbarrier_set_root = 0;
static int stat_wbarrier_value_copy = 0;
static int stat_wbarrier_object_copy = 0;
#endif
int stat_minor_gcs = 0;
int stat_major_gcs = 0;
static long long stat_pinned_objects = 0;
static long long time_minor_pre_collection_fragment_clear = 0;
static long long time_minor_pinning = 0;
static long long time_minor_scan_remsets = 0;
static long long time_minor_scan_pinned = 0;
static long long time_minor_scan_registered_roots = 0;
static long long time_minor_scan_thread_data = 0;
static long long time_minor_finish_gray_stack = 0;
static long long time_minor_fragment_creation = 0;
static long long time_major_pre_collection_fragment_clear = 0;
static long long time_major_pinning = 0;
static long long time_major_scan_pinned = 0;
static long long time_major_scan_registered_roots = 0;
static long long time_major_scan_thread_data = 0;
static long long time_major_scan_alloc_pinned = 0;
static long long time_major_scan_finalized = 0;
static long long time_major_scan_big_objects = 0;
static long long time_major_finish_gray_stack = 0;
static long long time_major_free_bigobjs = 0;
static long long time_major_los_sweep = 0;
static long long time_major_sweep = 0;
static long long time_major_fragment_creation = 0;
int gc_debug_level = 0;
FILE* gc_debug_file;
/*
void
mono_gc_flush_info (void)
{
fflush (gc_debug_file);
}
*/
#define TV_DECLARE SGEN_TV_DECLARE
#define TV_GETTIME SGEN_TV_GETTIME
#define TV_ELAPSED SGEN_TV_ELAPSED
#define TV_ELAPSED_MS SGEN_TV_ELAPSED_MS
#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
NurseryClearPolicy nursery_clear_policy = CLEAR_AT_TLAB_CREATION;
/* the runtime can register areas of memory as roots: we keep two lists of roots,
* a pinned root set for conservatively scanned roots and a normal one for
* precisely scanned roots (currently implemented as a single list).
*/
typedef struct _RootRecord RootRecord;
struct _RootRecord {
char *end_root;
mword root_desc;
};
#define object_is_forwarded SGEN_OBJECT_IS_FORWARDED
#define object_is_pinned SGEN_OBJECT_IS_PINNED
#define pin_object SGEN_PIN_OBJECT
#define unpin_object SGEN_UNPIN_OBJECT
#define ptr_in_nursery sgen_ptr_in_nursery
#define LOAD_VTABLE SGEN_LOAD_VTABLE
static const char*
safe_name (void* obj)
{
MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (obj);
return vt->klass->name;
}
#define safe_object_get_size sgen_safe_object_get_size
const char*
sgen_safe_name (void* obj)
{
return safe_name (obj);
}
/*
* ######################################################################
* ######## Global data.
* ######################################################################
*/
LOCK_DECLARE (gc_mutex);
static int gc_disabled = 0;
static gboolean use_cardtable;
#define SCAN_START_SIZE SGEN_SCAN_START_SIZE
static mword pagesize = 4096;
int degraded_mode = 0;
static mword bytes_pinned_from_failed_allocation = 0;
GCMemSection *nursery_section = NULL;
static mword lowest_heap_address = ~(mword)0;
static mword highest_heap_address = 0;
static LOCK_DECLARE (interruption_mutex);
static LOCK_DECLARE (pin_queue_mutex);
#define LOCK_PIN_QUEUE mono_mutex_lock (&pin_queue_mutex)
#define UNLOCK_PIN_QUEUE mono_mutex_unlock (&pin_queue_mutex)
typedef struct _FinalizeReadyEntry FinalizeReadyEntry;
struct _FinalizeReadyEntry {
FinalizeReadyEntry *next;
void *object;
};
typedef struct _EphemeronLinkNode EphemeronLinkNode;
struct _EphemeronLinkNode {
EphemeronLinkNode *next;
char *array;
};
typedef struct {
void *key;
void *value;
} Ephemeron;
int current_collection_generation = -1;
/*
* The link pointer is hidden by negating each bit. We use the lowest
* bit of the link (before negation) to store whether it needs
* resurrection tracking.
*/
#define HIDE_POINTER(p,t) ((gpointer)(~((gulong)(p)|((t)?1:0))))
#define REVEAL_POINTER(p) ((gpointer)((~(gulong)(p))&~3L))
/* objects that are ready to be finalized */
static FinalizeReadyEntry *fin_ready_list = NULL;
static FinalizeReadyEntry *critical_fin_list = NULL;
static EphemeronLinkNode *ephemeron_list;
static int num_ready_finalizers = 0;
static int no_finalize = 0;
enum {
ROOT_TYPE_NORMAL = 0, /* "normal" roots */
ROOT_TYPE_PINNED = 1, /* roots without a GC descriptor */
ROOT_TYPE_WBARRIER = 2, /* roots with a write barrier */
ROOT_TYPE_NUM
};
/* registered roots: the key to the hash is the root start address */
/*
* Different kinds of roots are kept separate to speed up pin_from_roots () for example.
*/
static SgenHashTable roots_hash [ROOT_TYPE_NUM] = {
SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL),
SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL),
SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL)
};
static mword roots_size = 0; /* amount of memory in the root set */
#define GC_ROOT_NUM 32
typedef struct {
int count;
void *objects [GC_ROOT_NUM];
int root_types [GC_ROOT_NUM];
uintptr_t extra_info [GC_ROOT_NUM];
} GCRootReport;
static void
notify_gc_roots (GCRootReport *report)
{
if (!report->count)
return;
mono_profiler_gc_roots (report->count, report->objects, report->root_types, report->extra_info);
report->count = 0;
}
static void
add_profile_gc_root (GCRootReport *report, void *object, int rtype, uintptr_t extra_info)
{
if (report->count == GC_ROOT_NUM)
notify_gc_roots (report);
report->objects [report->count] = object;
report->root_types [report->count] = rtype;
report->extra_info [report->count++] = (uintptr_t)((MonoVTable*)LOAD_VTABLE (object))->klass;
}
MonoNativeTlsKey thread_info_key;
#ifdef HAVE_KW_THREAD
__thread SgenThreadInfo *thread_info;
__thread gpointer *store_remset_buffer;
__thread long store_remset_buffer_index;
__thread char *stack_end;
__thread long *store_remset_buffer_index_addr;
#endif
/* The size of a TLAB */
/* The bigger the value, the less often we have to go to the slow path to allocate a new
* one, but the more space is wasted by threads not allocating much memory.
* FIXME: Tune this.
* FIXME: Make this self-tuning for each thread.
*/
guint32 tlab_size = (1024 * 4);
#define MAX_SMALL_OBJ_SIZE SGEN_MAX_SMALL_OBJ_SIZE
/* Functions supplied by the runtime to be called by the GC */
static MonoGCCallbacks gc_callbacks;
#define ALLOC_ALIGN SGEN_ALLOC_ALIGN
#define ALLOC_ALIGN_BITS SGEN_ALLOC_ALIGN_BITS
#define ALIGN_UP SGEN_ALIGN_UP
#define MOVED_OBJECTS_NUM 64
static void *moved_objects [MOVED_OBJECTS_NUM];
static int moved_objects_idx = 0;
/* Vtable of the objects used to fill out nursery fragments before a collection */
static MonoVTable *array_fill_vtable;
#ifdef SGEN_DEBUG_INTERNAL_ALLOC
MonoNativeThreadId main_gc_thread = NULL;
#endif
/*Object was pinned during the current collection*/
static mword objects_pinned;
/*
* ######################################################################
* ######## Macros and function declarations.
* ######################################################################
*/
inline static void*
align_pointer (void *ptr)
{
mword p = (mword)ptr;
p += sizeof (gpointer) - 1;
p &= ~ (sizeof (gpointer) - 1);
return (void*)p;
}
typedef SgenGrayQueue GrayQueue;
/* forward declarations */
static int stop_world (int generation);
static int restart_world (int generation, GGTimingInfo *timing);
static void scan_thread_data (void *start_nursery, void *end_nursery, gboolean precise, GrayQueue *queue);
static void scan_from_registered_roots (CopyOrMarkObjectFunc copy_func, char *addr_start, char *addr_end, int root_type, GrayQueue *queue);
static void scan_finalizer_entries (CopyOrMarkObjectFunc copy_func, FinalizeReadyEntry *list, GrayQueue *queue);
static void report_finalizer_roots (void);
static void report_registered_roots (void);
static void find_pinning_ref_from_thread (char *obj, size_t size);
static void update_current_thread_stack (void *start);
static void collect_bridge_objects (CopyOrMarkObjectFunc copy_func, char *start, char *end, int generation, GrayQueue *queue);
static void finalize_in_range (CopyOrMarkObjectFunc copy_func, char *start, char *end, int generation, GrayQueue *queue);
static void process_fin_stage_entries (void);
static void null_link_in_range (CopyOrMarkObjectFunc copy_func, char *start, char *end, int generation, gboolean before_finalization, GrayQueue *queue);
static void null_links_for_domain (MonoDomain *domain, int generation);
static void remove_finalizers_for_domain (MonoDomain *domain, int generation);
static void process_dislink_stage_entries (void);
static void pin_from_roots (void *start_nursery, void *end_nursery, GrayQueue *queue);
static int pin_objects_from_addresses (GCMemSection *section, void **start, void **end, void *start_nursery, void *end_nursery, GrayQueue *queue);
static void finish_gray_stack (char *start_addr, char *end_addr, int generation, GrayQueue *queue);
static void mono_gc_register_disappearing_link (MonoObject *obj, void **link, gboolean track, gboolean in_gc);
static gboolean mono_gc_is_critical_method (MonoMethod *method);
void mono_gc_scan_for_specific_ref (MonoObject *key, gboolean precise);
static void init_stats (void);
static int mark_ephemerons_in_range (CopyOrMarkObjectFunc copy_func, char *start, char *end, GrayQueue *queue);
static void clear_unreachable_ephemerons (CopyOrMarkObjectFunc copy_func, char *start, char *end, GrayQueue *queue);
static void null_ephemerons_for_domain (MonoDomain *domain);
SgenObjectOperations current_object_ops;
SgenMajorCollector major_collector;
SgenMinorCollector sgen_minor_collector;
static GrayQueue gray_queue;
static SgenRemeberedSet remset;
#define WORKERS_DISTRIBUTE_GRAY_QUEUE (sgen_collection_is_parallel () ? sgen_workers_get_distribute_gray_queue () : &gray_queue)
static SgenGrayQueue*
sgen_workers_get_job_gray_queue (WorkerData *worker_data)
{
return worker_data ? &worker_data->private_gray_queue : WORKERS_DISTRIBUTE_GRAY_QUEUE;
}
static gboolean
is_xdomain_ref_allowed (gpointer *ptr, char *obj, MonoDomain *domain)
{
MonoObject *o = (MonoObject*)(obj);
MonoObject *ref = (MonoObject*)*(ptr);
int offset = (char*)(ptr) - (char*)o;
if (o->vtable->klass == mono_defaults.thread_class && offset == G_STRUCT_OFFSET (MonoThread, internal_thread))
return TRUE;
if (o->vtable->klass == mono_defaults.internal_thread_class && offset == G_STRUCT_OFFSET (MonoInternalThread, current_appcontext))
return TRUE;
if (mono_class_has_parent_fast (o->vtable->klass, mono_defaults.real_proxy_class) &&
offset == G_STRUCT_OFFSET (MonoRealProxy, unwrapped_server))
return TRUE;
/* Thread.cached_culture_info */
if (!strcmp (ref->vtable->klass->name_space, "System.Globalization") &&
!strcmp (ref->vtable->klass->name, "CultureInfo") &&
!strcmp(o->vtable->klass->name_space, "System") &&
!strcmp(o->vtable->klass->name, "Object[]"))
return TRUE;
/*
* at System.IO.MemoryStream.InternalConstructor (byte[],int,int,bool,bool) [0x0004d] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.IO/MemoryStream.cs:121
* at System.IO.MemoryStream..ctor (byte[]) [0x00017] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.IO/MemoryStream.cs:81
* at (wrapper remoting-invoke-with-check) System.IO.MemoryStream..ctor (byte[]) <IL 0x00020, 0xffffffff>
* at System.Runtime.Remoting.Messaging.CADMethodCallMessage.GetArguments () [0x0000d] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Messaging/CADMessages.cs:327
* at System.Runtime.Remoting.Messaging.MethodCall..ctor (System.Runtime.Remoting.Messaging.CADMethodCallMessage) [0x00017] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Messaging/MethodCall.cs:87
* at System.AppDomain.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage,byte[]&,System.Runtime.Remoting.Messaging.CADMethodReturnMessage&) [0x00018] in /home/schani/Work/novell/trunk/mcs/class/corlib/System/AppDomain.cs:1213
* at (wrapper remoting-invoke-with-check) System.AppDomain.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage,byte[]&,System.Runtime.Remoting.Messaging.CADMethodReturnMessage&) <IL 0x0003d, 0xffffffff>
* at System.Runtime.Remoting.Channels.CrossAppDomainSink.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage) [0x00008] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Channels/CrossAppDomainChannel.cs:198
* at (wrapper runtime-invoke) object.runtime_invoke_CrossAppDomainSink/ProcessMessageRes_object_object (object,intptr,intptr,intptr) <IL 0x0004c, 0xffffffff>
*/
if (!strcmp (ref->vtable->klass->name_space, "System") &&
!strcmp (ref->vtable->klass->name, "Byte[]") &&
!strcmp (o->vtable->klass->name_space, "System.IO") &&
!strcmp (o->vtable->klass->name, "MemoryStream"))
return TRUE;
/* append_job() in threadpool.c */
if (!strcmp (ref->vtable->klass->name_space, "System.Runtime.Remoting.Messaging") &&
!strcmp (ref->vtable->klass->name, "AsyncResult") &&
!strcmp (o->vtable->klass->name_space, "System") &&
!strcmp (o->vtable->klass->name, "Object[]") &&
mono_thread_pool_is_queue_array ((MonoArray*) o))
return TRUE;
return FALSE;
}
static void
check_reference_for_xdomain (gpointer *ptr, char *obj, MonoDomain *domain)
{
MonoObject *o = (MonoObject*)(obj);
MonoObject *ref = (MonoObject*)*(ptr);
int offset = (char*)(ptr) - (char*)o;
MonoClass *class;
MonoClassField *field;
char *str;
if (!ref || ref->vtable->domain == domain)
return;
if (is_xdomain_ref_allowed (ptr, obj, domain))
return;
field = NULL;
for (class = o->vtable->klass; class; class = class->parent) {
int i;
for (i = 0; i < class->field.count; ++i) {
if (class->fields[i].offset == offset) {
field = &class->fields[i];
break;
}
}
if (field)
break;
}
if (ref->vtable->klass == mono_defaults.string_class)
str = mono_string_to_utf8 ((MonoString*)ref);
else
str = NULL;
g_print ("xdomain reference in %p (%s.%s) at offset %d (%s) to %p (%s.%s) (%s) - pointed to by:\n",
o, o->vtable->klass->name_space, o->vtable->klass->name,
offset, field ? field->name : "",
ref, ref->vtable->klass->name_space, ref->vtable->klass->name, str ? str : "");
mono_gc_scan_for_specific_ref (o, TRUE);
if (str)
g_free (str);
}
#undef HANDLE_PTR
#define HANDLE_PTR(ptr,obj) check_reference_for_xdomain ((ptr), (obj), domain)
static void
scan_object_for_xdomain_refs (char *start, mword size, void *data)
{
MonoDomain *domain = ((MonoObject*)start)->vtable->domain;
#include "sgen-scan-object.h"
}
static gboolean scan_object_for_specific_ref_precise = TRUE;
#undef HANDLE_PTR
#define HANDLE_PTR(ptr,obj) do { \
if ((MonoObject*)*(ptr) == key) { \
g_print ("found ref to %p in object %p (%s) at offset %td\n", \
key, (obj), safe_name ((obj)), ((char*)(ptr) - (char*)(obj))); \
} \
} while (0)
static void
scan_object_for_specific_ref (char *start, MonoObject *key)
{
char *forwarded;
if ((forwarded = SGEN_OBJECT_IS_FORWARDED (start)))
start = forwarded;
if (scan_object_for_specific_ref_precise) {
#include "sgen-scan-object.h"
} else {
mword *words = (mword*)start;
size_t size = safe_object_get_size ((MonoObject*)start);
int i;
for (i = 0; i < size / sizeof (mword); ++i) {
if (words [i] == (mword)key) {
g_print ("found possible ref to %p in object %p (%s) at offset %td\n",
key, start, safe_name (start), i * sizeof (mword));
}
}
}
}
void
sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags)
{
while (start < end) {
size_t size;
char *obj;
if (!*(void**)start) {
start += sizeof (void*); /* should be ALLOC_ALIGN, really */
continue;
}
if (allow_flags) {
if (!(obj = SGEN_OBJECT_IS_FORWARDED (start)))
obj = start;
} else {
obj = start;
}
size = ALIGN_UP (safe_object_get_size ((MonoObject*)obj));
if ((MonoVTable*)SGEN_LOAD_VTABLE (obj) != array_fill_vtable)
callback (obj, size, data);
start += size;
}
}
static void
scan_object_for_specific_ref_callback (char *obj, size_t size, MonoObject *key)
{
scan_object_for_specific_ref (obj, key);
}
static void
check_root_obj_specific_ref (RootRecord *root, MonoObject *key, MonoObject *obj)
{
if (key != obj)
return;
g_print ("found ref to %p in root record %p\n", key, root);
}
static MonoObject *check_key = NULL;
static RootRecord *check_root = NULL;
static void
check_root_obj_specific_ref_from_marker (void **obj)
{
check_root_obj_specific_ref (check_root, check_key, *obj);
}
static void
scan_roots_for_specific_ref (MonoObject *key, int root_type)
{
void **start_root;
RootRecord *root;
check_key = key;
SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
mword desc = root->root_desc;
check_root = root;
switch (desc & ROOT_DESC_TYPE_MASK) {
case ROOT_DESC_BITMAP:
desc >>= ROOT_DESC_TYPE_SHIFT;
while (desc) {
if (desc & 1)
check_root_obj_specific_ref (root, key, *start_root);
desc >>= 1;
start_root++;
}
return;
case ROOT_DESC_COMPLEX: {
gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
int bwords = (*bitmap_data) - 1;
void **start_run = start_root;
bitmap_data++;
while (bwords-- > 0) {
gsize bmap = *bitmap_data++;
void **objptr = start_run;
while (bmap) {
if (bmap & 1)
check_root_obj_specific_ref (root, key, *objptr);
bmap >>= 1;
++objptr;
}
start_run += GC_BITS_PER_WORD;
}
break;
}
case ROOT_DESC_USER: {
MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
marker (start_root, check_root_obj_specific_ref_from_marker);
break;
}
case ROOT_DESC_RUN_LEN:
g_assert_not_reached ();
default:
g_assert_not_reached ();
}
} SGEN_HASH_TABLE_FOREACH_END;
check_key = NULL;
check_root = NULL;
}
void
mono_gc_scan_for_specific_ref (MonoObject *key, gboolean precise)
{
void **ptr;
RootRecord *root;
scan_object_for_specific_ref_precise = precise;
sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
(IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key, TRUE);
major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key);
sgen_los_iterate_objects ((IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key);
scan_roots_for_specific_ref (key, ROOT_TYPE_NORMAL);
scan_roots_for_specific_ref (key, ROOT_TYPE_WBARRIER);
SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_PINNED], ptr, root) {
while (ptr < (void**)root->end_root) {
check_root_obj_specific_ref (root, *ptr, key);
++ptr;
}
} SGEN_HASH_TABLE_FOREACH_END;
}
static gboolean
need_remove_object_for_domain (char *start, MonoDomain *domain)
{
if (mono_object_domain (start) == domain) {
DEBUG (4, fprintf (gc_debug_file, "Need to cleanup object %p\n", start));
binary_protocol_cleanup (start, (gpointer)LOAD_VTABLE (start), safe_object_get_size ((MonoObject*)start));
return TRUE;
}
return FALSE;
}
static void
process_object_for_domain_clearing (char *start, MonoDomain *domain)
{
GCVTable *vt = (GCVTable*)LOAD_VTABLE (start);
if (vt->klass == mono_defaults.internal_thread_class)
g_assert (mono_object_domain (start) == mono_get_root_domain ());
/* The object could be a proxy for an object in the domain
we're deleting. */
if (mono_class_has_parent_fast (vt->klass, mono_defaults.real_proxy_class)) {
MonoObject *server = ((MonoRealProxy*)start)->unwrapped_server;
/* The server could already have been zeroed out, so
we need to check for that, too. */
if (server && (!LOAD_VTABLE (server) || mono_object_domain (server) == domain)) {
DEBUG (4, fprintf (gc_debug_file, "Cleaning up remote pointer in %p to object %p\n",
start, server));
((MonoRealProxy*)start)->unwrapped_server = NULL;
}
}
}
static MonoDomain *check_domain = NULL;
static void
check_obj_not_in_domain (void **o)
{
g_assert (((MonoObject*)(*o))->vtable->domain != check_domain);
}
static void
scan_for_registered_roots_in_domain (MonoDomain *domain, int root_type)
{
void **start_root;
RootRecord *root;
check_domain = domain;
SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
mword desc = root->root_desc;
/* The MonoDomain struct is allowed to hold
references to objects in its own domain. */
if (start_root == (void**)domain)
continue;
switch (desc & ROOT_DESC_TYPE_MASK) {
case ROOT_DESC_BITMAP:
desc >>= ROOT_DESC_TYPE_SHIFT;
while (desc) {
if ((desc & 1) && *start_root)
check_obj_not_in_domain (*start_root);
desc >>= 1;
start_root++;
}
break;
case ROOT_DESC_COMPLEX: {
gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
int bwords = (*bitmap_data) - 1;
void **start_run = start_root;
bitmap_data++;
while (bwords-- > 0) {
gsize bmap = *bitmap_data++;
void **objptr = start_run;
while (bmap) {
if ((bmap & 1) && *objptr)
check_obj_not_in_domain (*objptr);
bmap >>= 1;
++objptr;
}
start_run += GC_BITS_PER_WORD;
}
break;
}
case ROOT_DESC_USER: {
MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
marker (start_root, check_obj_not_in_domain);
break;
}
case ROOT_DESC_RUN_LEN:
g_assert_not_reached ();
default:
g_assert_not_reached ();
}
} SGEN_HASH_TABLE_FOREACH_END;
check_domain = NULL;
}
static void
check_for_xdomain_refs (void)
{
LOSObject *bigobj;
sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
(IterateObjectCallbackFunc)scan_object_for_xdomain_refs, NULL, FALSE);
major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)scan_object_for_xdomain_refs, NULL);
for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
scan_object_for_xdomain_refs (bigobj->data, bigobj->size, NULL);
}
static gboolean
clear_domain_process_object (char *obj, MonoDomain *domain)
{
gboolean remove;
process_object_for_domain_clearing (obj, domain);
remove = need_remove_object_for_domain (obj, domain);
if (remove && ((MonoObject*)obj)->synchronisation) {
void **dislink = mono_monitor_get_object_monitor_weak_link ((MonoObject*)obj);
if (dislink)
mono_gc_register_disappearing_link (NULL, dislink, FALSE, TRUE);
}
return remove;
}
static void
clear_domain_process_minor_object_callback (char *obj, size_t size, MonoDomain *domain)
{
if (clear_domain_process_object (obj, domain))
memset (obj, 0, size);
}
static void
clear_domain_process_major_object_callback (char *obj, size_t size, MonoDomain *domain)
{
clear_domain_process_object (obj, domain);
}
static void
clear_domain_free_major_non_pinned_object_callback (char *obj, size_t size, MonoDomain *domain)
{
if (need_remove_object_for_domain (obj, domain))
major_collector.free_non_pinned_object (obj, size);
}
static void
clear_domain_free_major_pinned_object_callback (char *obj, size_t size, MonoDomain *domain)
{
if (need_remove_object_for_domain (obj, domain))
major_collector.free_pinned_object (obj, size);
}
/*
* When appdomains are unloaded we can easily remove objects that have finalizers,
* but all the others could still be present in random places on the heap.
* We need a sweep to get rid of them even though it's going to be costly
* with big heaps.
* The reason we need to remove them is because we access the vtable and class
* structures to know the object size and the reference bitmap: once the domain is
* unloaded the point to random memory.
*/
void
mono_gc_clear_domain (MonoDomain * domain)
{
LOSObject *bigobj, *prev;
int i;
LOCK_GC;
process_fin_stage_entries ();
process_dislink_stage_entries ();
sgen_clear_nursery_fragments ();
if (xdomain_checks && domain != mono_get_root_domain ()) {
scan_for_registered_roots_in_domain (domain, ROOT_TYPE_NORMAL);
scan_for_registered_roots_in_domain (domain, ROOT_TYPE_WBARRIER);
check_for_xdomain_refs ();
}
/*Ephemerons and dislinks must be processed before LOS since they might end up pointing
to memory returned to the OS.*/
null_ephemerons_for_domain (domain);
for (i = GENERATION_NURSERY; i < GENERATION_MAX; ++i)
null_links_for_domain (domain, i);
for (i = GENERATION_NURSERY; i < GENERATION_MAX; ++i)
remove_finalizers_for_domain (domain, i);
sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
(IterateObjectCallbackFunc)clear_domain_process_minor_object_callback, domain, FALSE);
/* We need two passes over major and large objects because
freeing such objects might give their memory back to the OS
(in the case of large objects) or obliterate its vtable
(pinned objects with major-copying or pinned and non-pinned
objects with major-mark&sweep), but we might need to
dereference a pointer from an object to another object if
the first object is a proxy. */
major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)clear_domain_process_major_object_callback, domain);
for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
clear_domain_process_object (bigobj->data, domain);
prev = NULL;
for (bigobj = los_object_list; bigobj;) {
if (need_remove_object_for_domain (bigobj->data, domain)) {
LOSObject *to_free = bigobj;
if (prev)
prev->next = bigobj->next;
else
los_object_list = bigobj->next;
bigobj = bigobj->next;
DEBUG (4, fprintf (gc_debug_file, "Freeing large object %p\n",
bigobj->data));
sgen_los_free_object (to_free);
continue;
}
prev = bigobj;
bigobj = bigobj->next;
}
major_collector.iterate_objects (TRUE, FALSE, (IterateObjectCallbackFunc)clear_domain_free_major_non_pinned_object_callback, domain);
major_collector.iterate_objects (FALSE, TRUE, (IterateObjectCallbackFunc)clear_domain_free_major_pinned_object_callback, domain);
if (G_UNLIKELY (do_pin_stats)) {
if (domain == mono_get_root_domain ())
sgen_pin_stats_print_class_stats ();
}
UNLOCK_GC;
}
/*
* sgen_add_to_global_remset:
*
* The global remset contains locations which point into newspace after
* a minor collection. This can happen if the objects they point to are pinned.
*
* LOCKING: If called from a parallel collector, the global remset
* lock must be held. For serial collectors that is not necessary.
*/
void
sgen_add_to_global_remset (gpointer ptr)
{
remset.record_pointer (ptr);
}
/*
* sgen_drain_gray_stack:
*
* Scan objects in the gray stack until the stack is empty. This should be called
* frequently after each object is copied, to achieve better locality and cache
* usage.
*/
gboolean
sgen_drain_gray_stack (GrayQueue *queue, int max_objs)
{
char *obj;
ScanObjectFunc scan_func = current_object_ops.scan_object;
if (max_objs == -1) {
for (;;) {
GRAY_OBJECT_DEQUEUE (queue, obj);
if (!obj)
return TRUE;
DEBUG (9, fprintf (gc_debug_file, "Precise gray object scan %p (%s)\n", obj, safe_name (obj)));
scan_func (obj, queue);
}
} else {
int i;
do {
for (i = 0; i != max_objs; ++i) {
GRAY_OBJECT_DEQUEUE (queue, obj);
if (!obj)
return TRUE;
DEBUG (9, fprintf (gc_debug_file, "Precise gray object scan %p (%s)\n", obj, safe_name (obj)));
scan_func (obj, queue);
}
} while (max_objs < 0);
return FALSE;
}
}
/*
* Addresses from start to end are already sorted. This function finds
* the object header for each address and pins the object. The
* addresses must be inside the passed section. The (start of the)
* address array is overwritten with the addresses of the actually
* pinned objects. Return the number of pinned objects.
*/
static int
pin_objects_from_addresses (GCMemSection *section, void **start, void **end, void *start_nursery, void *end_nursery, GrayQueue *queue)
{
void *last = NULL;
int count = 0;
void *search_start;
void *last_obj = NULL;
size_t last_obj_size = 0;
void *addr;
int idx;
void **definitely_pinned = start;
sgen_nursery_allocator_prepare_for_pinning ();
while (start < end) {
addr = *start;
/* the range check should be reduntant */
if (addr != last && addr >= start_nursery && addr < end_nursery) {
DEBUG (5, fprintf (gc_debug_file, "Considering pinning addr %p\n", addr));
/* multiple pointers to the same object */
if (addr >= last_obj && (char*)addr < (char*)last_obj + last_obj_size) {
start++;
continue;
}
idx = ((char*)addr - (char*)section->data) / SCAN_START_SIZE;
g_assert (idx < section->num_scan_start);
search_start = (void*)section->scan_starts [idx];
if (!search_start || search_start > addr) {
while (idx) {
--idx;
search_start = section->scan_starts [idx];
if (search_start && search_start <= addr)
break;
}
if (!search_start || search_start > addr)
search_start = start_nursery;
}
if (search_start < last_obj)
search_start = (char*)last_obj + last_obj_size;
/* now addr should be in an object a short distance from search_start
* Note that search_start must point to zeroed mem or point to an object.
*/
do {
if (!*(void**)search_start) {
/* Consistency check */
/*
for (frag = nursery_fragments; frag; frag = frag->next) {
if (search_start >= frag->fragment_start && search_start < frag->fragment_end)
g_assert_not_reached ();
}
*/
search_start = (void*)ALIGN_UP ((mword)search_start + sizeof (gpointer));
continue;
}
last_obj = search_start;
last_obj_size = ALIGN_UP (safe_object_get_size ((MonoObject*)search_start));
if (((MonoObject*)last_obj)->synchronisation == GINT_TO_POINTER (-1)) {
/* Marks the beginning of a nursery fragment, skip */
} else {
DEBUG (8, fprintf (gc_debug_file, "Pinned try match %p (%s), size %zd\n", last_obj, safe_name (last_obj), last_obj_size));
if (addr >= search_start && (char*)addr < (char*)last_obj + last_obj_size) {
DEBUG (4, fprintf (gc_debug_file, "Pinned object %p, vtable %p (%s), count %d\n", search_start, *(void**)search_start, safe_name (search_start), count));
binary_protocol_pin (search_start, (gpointer)LOAD_VTABLE (search_start), safe_object_get_size (search_start));
if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
int gen = sgen_ptr_in_nursery (search_start) ? GENERATION_NURSERY : GENERATION_OLD;
MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (search_start);
MONO_GC_OBJ_PINNED ((mword)search_start, sgen_safe_object_get_size (search_start), vt->klass->name_space, vt->klass->name, gen);
}
pin_object (search_start);
GRAY_OBJECT_ENQUEUE (queue, search_start);
if (G_UNLIKELY (do_pin_stats))
sgen_pin_stats_register_object (search_start, last_obj_size);
definitely_pinned [count] = search_start;
count++;
break;
}
}
/* skip to the next object */
search_start = (void*)((char*)search_start + last_obj_size);
} while (search_start <= addr);
/* we either pinned the correct object or we ignored the addr because
* it points to unused zeroed memory.
*/
last = addr;
}
start++;
}
//printf ("effective pinned: %d (at the end: %d)\n", count, (char*)end_nursery - (char*)last);
if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS) {
GCRootReport report;
report.count = 0;
for (idx = 0; idx < count; ++idx)
add_profile_gc_root (&report, definitely_pinned [idx], MONO_PROFILE_GC_ROOT_PINNING | MONO_PROFILE_GC_ROOT_MISC, 0);
notify_gc_roots (&report);
}
stat_pinned_objects += count;
return count;
}
void
sgen_pin_objects_in_section (GCMemSection *section, GrayQueue *queue)
{
int num_entries = section->pin_queue_num_entries;
if (num_entries) {
void **start = section->pin_queue_start;
int reduced_to;
reduced_to = pin_objects_from_addresses (section, start, start + num_entries,
section->data, section->next_data, queue);
section->pin_queue_num_entries = reduced_to;
if (!reduced_to)
section->pin_queue_start = NULL;
}
}
void
sgen_pin_object (void *object, GrayQueue *queue)
{
if (sgen_collection_is_parallel ()) {
LOCK_PIN_QUEUE;
/*object arrives pinned*/
sgen_pin_stage_ptr (object);
++objects_pinned ;
UNLOCK_PIN_QUEUE;
} else {
SGEN_PIN_OBJECT (object);
sgen_pin_stage_ptr (object);
++objects_pinned;
if (G_UNLIKELY (do_pin_stats))
sgen_pin_stats_register_object (object, safe_object_get_size (object));
}
GRAY_OBJECT_ENQUEUE (queue, object);
binary_protocol_pin (object, (gpointer)LOAD_VTABLE (object), safe_object_get_size (object));
if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
int gen = sgen_ptr_in_nursery (object) ? GENERATION_NURSERY : GENERATION_OLD;
MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (object);
MONO_GC_OBJ_PINNED ((mword)object, sgen_safe_object_get_size (object), vt->klass->name_space, vt->klass->name, gen);
}
}
void
sgen_parallel_pin_or_update (void **ptr, void *obj, MonoVTable *vt, SgenGrayQueue *queue)
{
for (;;) {
mword vtable_word;
gboolean major_pinned = FALSE;
if (sgen_ptr_in_nursery (obj)) {
if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
sgen_pin_object (obj, queue);
break;
}
} else {
major_collector.pin_major_object (obj, queue);
major_pinned = TRUE;
}
vtable_word = *(mword*)obj;
/*someone else forwarded it, update the pointer and bail out*/
if (vtable_word & SGEN_FORWARDED_BIT) {
*ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
break;
}
/*someone pinned it, nothing to do.*/
if (vtable_word & SGEN_PINNED_BIT || major_pinned)
break;
}
}
/* Sort the addresses in array in increasing order.
* Done using a by-the book heap sort. Which has decent and stable performance, is pretty cache efficient.
*/
void
sgen_sort_addresses (void **array, int size)
{
int i;
void *tmp;
for (i = 1; i < size; ++i) {
int child = i;
while (child > 0) {
int parent = (child - 1) / 2;
if (array [parent] >= array [child])
break;
tmp = array [parent];
array [parent] = array [child];
array [child] = tmp;
child = parent;
}
}
for (i = size - 1; i > 0; --i) {
int end, root;
tmp = array [i];
array [i] = array [0];
array [0] = tmp;
end = i - 1;
root = 0;
while (root * 2 + 1 <= end) {
int child = root * 2 + 1;
if (child < end && array [child] < array [child + 1])
++child;
if (array [root] >= array [child])
break;
tmp = array [root];
array [root] = array [child];
array [child] = tmp;
root = child;
}
}
}
/*
* Scan the memory between start and end and queue values which could be pointers
* to the area between start_nursery and end_nursery for later consideration.
* Typically used for thread stacks.
*/
static void
conservatively_pin_objects_from (void **start, void **end, void *start_nursery, void *end_nursery, int pin_type)
{
int count = 0;
while (start < end) {
if (*start >= start_nursery && *start < end_nursery) {
/*
* *start can point to the middle of an object
* note: should we handle pointing at the end of an object?
* pinning in C# code disallows pointing at the end of an object
* but there is some small chance that an optimizing C compiler
* may keep the only reference to an object by pointing
* at the end of it. We ignore this small chance for now.
* Pointers to the end of an object are indistinguishable
* from pointers to the start of the next object in memory
* so if we allow that we'd need to pin two objects...
* We queue the pointer in an array, the
* array will then be sorted and uniqued. This way
* we can coalesce several pinning pointers and it should
* be faster since we'd do a memory scan with increasing
* addresses. Note: we can align the address to the allocation
* alignment, so the unique process is more effective.
*/
mword addr = (mword)*start;
addr &= ~(ALLOC_ALIGN - 1);
if (addr >= (mword)start_nursery && addr < (mword)end_nursery)
sgen_pin_stage_ptr ((void*)addr);
if (G_UNLIKELY (do_pin_stats)) {
if (ptr_in_nursery ((void*)addr))
sgen_pin_stats_register_address ((char*)addr, pin_type);
}
DEBUG (6, if (count) fprintf (gc_debug_file, "Pinning address %p from %p\n", (void*)addr, start));
count++;
}
start++;
}
DEBUG (7, if (count) fprintf (gc_debug_file, "found %d potential pinned heap pointers\n", count));
}
/*
* Debugging function: find in the conservative roots where @obj is being pinned.
*/
static G_GNUC_UNUSED void
find_pinning_reference (char *obj, size_t size)
{
char **start;
RootRecord *root;
char *endobj = obj + size;
SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_NORMAL], start, root) {
/* if desc is non-null it has precise info */
if (!root->root_desc) {
while (start < (char**)root->end_root) {
if (*start >= obj && *start < endobj) {
DEBUG (0, fprintf (gc_debug_file, "Object %p referenced in pinned roots %p-%p\n", obj, start, root->end_root));
}
start++;
}
}
} SGEN_HASH_TABLE_FOREACH_END;
find_pinning_ref_from_thread (obj, size);
}
/*
* The first thing we do in a collection is to identify pinned objects.
* This function considers all the areas of memory that need to be
* conservatively scanned.
*/
static void
pin_from_roots (void *start_nursery, void *end_nursery, GrayQueue *queue)
{
void **start_root;
RootRecord *root;
DEBUG (2, fprintf (gc_debug_file, "Scanning pinned roots (%d bytes, %d/%d entries)\n", (int)roots_size, roots_hash [ROOT_TYPE_NORMAL].num_entries, roots_hash [ROOT_TYPE_PINNED].num_entries));
/* objects pinned from the API are inside these roots */
SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_PINNED], start_root, root) {
DEBUG (6, fprintf (gc_debug_file, "Pinned roots %p-%p\n", start_root, root->end_root));
conservatively_pin_objects_from (start_root, (void**)root->end_root, start_nursery, end_nursery, PIN_TYPE_OTHER);
} SGEN_HASH_TABLE_FOREACH_END;
/* now deal with the thread stacks
* in the future we should be able to conservatively scan only:
* *) the cpu registers
* *) the unmanaged stack frames
* *) the _last_ managed stack frame
* *) pointers slots in managed frames
*/
scan_thread_data (start_nursery, end_nursery, FALSE, queue);
}
typedef struct {
CopyOrMarkObjectFunc func;
GrayQueue *queue;
} UserCopyOrMarkData;
static MonoNativeTlsKey user_copy_or_mark_key;
static void
init_user_copy_or_mark_key (void)
{
mono_native_tls_alloc (&user_copy_or_mark_key, NULL);
}
static void
set_user_copy_or_mark_data (UserCopyOrMarkData *data)
{
mono_native_tls_set_value (user_copy_or_mark_key, data);
}
static void
single_arg_user_copy_or_mark (void **obj)
{
UserCopyOrMarkData *data = mono_native_tls_get_value (user_copy_or_mark_key);
data->func (obj, data->queue);
}
/*
* The memory area from start_root to end_root contains pointers to objects.
* Their position is precisely described by @desc (this means that the pointer
* can be either NULL or the pointer to the start of an object).
* This functions copies them to to_space updates them.
*
* This function is not thread-safe!
*/
static void
precisely_scan_objects_from (CopyOrMarkObjectFunc copy_func, void** start_root, void** end_root, char* n_start, char *n_end, mword desc, GrayQueue *queue)
{
switch (desc & ROOT_DESC_TYPE_MASK) {
case ROOT_DESC_BITMAP:
desc >>= ROOT_DESC_TYPE_SHIFT;
while (desc) {
if ((desc & 1) && *start_root) {
copy_func (start_root, queue);
DEBUG (9, fprintf (gc_debug_file, "Overwrote root at %p with %p\n", start_root, *start_root));
sgen_drain_gray_stack (queue, -1);
}
desc >>= 1;
start_root++;
}
return;
case ROOT_DESC_COMPLEX: {
gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
int bwords = (*bitmap_data) - 1;
void **start_run = start_root;
bitmap_data++;
while (bwords-- > 0) {
gsize bmap = *bitmap_data++;
void **objptr = start_run;
while (bmap) {
if ((bmap & 1) && *objptr) {
copy_func (objptr, queue);
DEBUG (9, fprintf (gc_debug_file, "Overwrote root at %p with %p\n", objptr, *objptr));
sgen_drain_gray_stack (queue, -1);
}
bmap >>= 1;
++objptr;
}
start_run += GC_BITS_PER_WORD;
}
break;
}
case ROOT_DESC_USER: {
UserCopyOrMarkData data = { copy_func, queue };
MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
set_user_copy_or_mark_data (&data);
marker (start_root, single_arg_user_copy_or_mark);
set_user_copy_or_mark_data (NULL);
break;
}
case ROOT_DESC_RUN_LEN:
g_assert_not_reached ();
default:
g_assert_not_reached ();
}
}
static void
reset_heap_boundaries (void)
{
lowest_heap_address = ~(mword)0;
highest_heap_address = 0;
}
void
sgen_update_heap_boundaries (mword low, mword high)
{
mword old;
do {
old = lowest_heap_address;
if (low >= old)
break;
} while (SGEN_CAS_PTR ((gpointer*)&lowest_heap_address, (gpointer)low, (gpointer)old) != (gpointer)old);
do {
old = highest_heap_address;
if (high <= old)
break;
} while (SGEN_CAS_PTR ((gpointer*)&highest_heap_address, (gpointer)high, (gpointer)old) != (gpointer)old);
}
/*
* Allocate and setup the data structures needed to be able to allocate objects
* in the nursery. The nursery is stored in nursery_section.
*/
static void
alloc_nursery (void)
{
GCMemSection *section;
char *data;
int scan_starts;
int alloc_size;
if (nursery_section)
return;
DEBUG (2, fprintf (gc_debug_file, "Allocating nursery size: %lu\n", (unsigned long)sgen_nursery_size));
/* later we will alloc a larger area for the nursery but only activate
* what we need. The rest will be used as expansion if we have too many pinned
* objects in the existing nursery.
*/
/* FIXME: handle OOM */
section = sgen_alloc_internal (INTERNAL_MEM_SECTION);
alloc_size = sgen_nursery_size;
/* If there isn't enough space even for the nursery we should simply abort. */
g_assert (sgen_memgov_try_alloc_space (alloc_size, SPACE_NURSERY));
#ifdef SGEN_ALIGN_NURSERY
data = major_collector.alloc_heap (alloc_size, alloc_size, DEFAULT_NURSERY_BITS);
#else
data = major_collector.alloc_heap (alloc_size, 0, DEFAULT_NURSERY_BITS);
#endif
sgen_update_heap_boundaries ((mword)data, (mword)(data + sgen_nursery_size));
DEBUG (4, fprintf (gc_debug_file, "Expanding nursery size (%p-%p): %lu, total: %lu\n", data, data + alloc_size, (unsigned long)sgen_nursery_size, (unsigned long)mono_gc_get_heap_size ()));
section->data = section->next_data = data;
section->size = alloc_size;
section->end_data = data + sgen_nursery_size;
scan_starts = (alloc_size + SCAN_START_SIZE - 1) / SCAN_START_SIZE;
section->scan_starts = sgen_alloc_internal_dynamic (sizeof (char*) * scan_starts, INTERNAL_MEM_SCAN_STARTS, TRUE);
section->num_scan_start = scan_starts;
section->block.role = MEMORY_ROLE_GEN0;
section->block.next = NULL;
nursery_section = section;
sgen_nursery_allocator_set_nursery_bounds (data, data + sgen_nursery_size);
}
void*
mono_gc_get_nursery (int *shift_bits, size_t *size)
{
*size = sgen_nursery_size;
#ifdef SGEN_ALIGN_NURSERY
*shift_bits = DEFAULT_NURSERY_BITS;
#else
*shift_bits = -1;
#endif
return sgen_get_nursery_start ();
}
void
mono_gc_set_current_thread_appdomain (MonoDomain *domain)
{
SgenThreadInfo *info = mono_thread_info_current ();
/* Could be called from sgen_thread_unregister () with a NULL info */
if (domain) {
g_assert (info);
info->stopped_domain = domain;
}
}
gboolean
mono_gc_precise_stack_mark_enabled (void)
{
return !conservative_stack_mark;
}
FILE *
mono_gc_get_logfile (void)
{
return sgen_get_logfile ();
}
static void
report_finalizer_roots_list (FinalizeReadyEntry *list)
{
GCRootReport report;
FinalizeReadyEntry *fin;
report.count = 0;
for (fin = list; fin; fin = fin->next) {
if (!fin->object)
continue;
add_profile_gc_root (&report, fin->object, MONO_PROFILE_GC_ROOT_FINALIZER, 0);
}
notify_gc_roots (&report);
}
static void
report_finalizer_roots (void)
{
report_finalizer_roots_list (fin_ready_list);
report_finalizer_roots_list (critical_fin_list);
}
static GCRootReport *root_report;
static void
single_arg_report_root (void **obj)
{
if (*obj)
add_profile_gc_root (root_report, *obj, MONO_PROFILE_GC_ROOT_OTHER, 0);
}
static void
precisely_report_roots_from (GCRootReport *report, void** start_root, void** end_root, mword desc)
{
switch (desc & ROOT_DESC_TYPE_MASK) {
case ROOT_DESC_BITMAP:
desc >>= ROOT_DESC_TYPE_SHIFT;
while (desc) {
if ((desc & 1) && *start_root) {
add_profile_gc_root (report, *start_root, MONO_PROFILE_GC_ROOT_OTHER, 0);
}
desc >>= 1;
start_root++;
}
return;
case ROOT_DESC_COMPLEX: {
gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
int bwords = (*bitmap_data) - 1;
void **start_run = start_root;
bitmap_data++;
while (bwords-- > 0) {
gsize bmap = *bitmap_data++;
void **objptr = start_run;
while (bmap) {
if ((bmap & 1) && *objptr) {
add_profile_gc_root (report, *objptr, MONO_PROFILE_GC_ROOT_OTHER, 0);
}
bmap >>= 1;
++objptr;
}
start_run += GC_BITS_PER_WORD;
}
break;
}
case ROOT_DESC_USER: {
MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
root_report = report;
marker (start_root, single_arg_report_root);
break;
}
case ROOT_DESC_RUN_LEN:
g_assert_not_reached ();
default:
g_assert_not_reached ();
}
}
static void
report_registered_roots_by_type (int root_type)
{
GCRootReport report;
void **start_root;
RootRecord *root;
report.count = 0;
SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
DEBUG (6, fprintf (gc_debug_file, "Precise root scan %p-%p (desc: %p)\n", start_root, root->end_root, (void*)root->root_desc));
precisely_report_roots_from (&report, start_root, (void**)root->end_root, root->root_desc);
} SGEN_HASH_TABLE_FOREACH_END;
notify_gc_roots (&report);
}
static void
report_registered_roots (void)
{
report_registered_roots_by_type (ROOT_TYPE_NORMAL);
report_registered_roots_by_type (ROOT_TYPE_WBARRIER);
}
static void
scan_finalizer_entries (CopyOrMarkObjectFunc copy_func, FinalizeReadyEntry *list, GrayQueue *queue)
{
FinalizeReadyEntry *fin;
for (fin = list; fin; fin = fin->next) {
if (!fin->object)
continue;
DEBUG (5, fprintf (gc_debug_file, "Scan of fin ready object: %p (%s)\n", fin->object, safe_name (fin->object)));
copy_func (&fin->object, queue);
}
}
static const char*
generation_name (int generation)
{
switch (generation) {
case GENERATION_NURSERY: return "nursery";
case GENERATION_OLD: return "old";
default: g_assert_not_reached ();
}
}
static void
stw_bridge_process (void)
{
sgen_bridge_processing_stw_step ();
}
static void
bridge_process (int generation)
{
sgen_bridge_processing_finish (generation);
}
SgenObjectOperations *
sgen_get_current_object_ops (void){
return &current_object_ops;
}
static void
finish_gray_stack (char *start_addr, char *end_addr, int generation, GrayQueue *queue)
{
TV_DECLARE (atv);
TV_DECLARE (btv);
int done_with_ephemerons, ephemeron_rounds = 0;
CopyOrMarkObjectFunc copy_func = current_object_ops.copy_or_mark_object;
/*
* We copied all the reachable objects. Now it's the time to copy
* the objects that were not referenced by the roots, but by the copied objects.
* we built a stack of objects pointed to by gray_start: they are
* additional roots and we may add more items as we go.
* We loop until gray_start == gray_objects which means no more objects have
* been added. Note this is iterative: no recursion is involved.
* We need to walk the LO list as well in search of marked big objects
* (use a flag since this is needed only on major collections). We need to loop
* here as well, so keep a counter of marked LO (increasing it in copy_object).
* To achieve better cache locality and cache usage, we drain the gray stack
* frequently, after each object is copied, and just finish the work here.
*/
sgen_drain_gray_stack (queue, -1);
TV_GETTIME (atv);
DEBUG (2, fprintf (gc_debug_file, "%s generation done\n", generation_name (generation)));
/*
Reset bridge data, we might have lingering data from a previous collection if this is a major
collection trigged by minor overflow.
We must reset the gathered bridges since their original block might be evacuated due to major
fragmentation in the meanwhile and the bridge code should not have to deal with that.
*/
sgen_bridge_reset_data ();
/*
* Walk the ephemeron tables marking all values with reachable keys. This must be completely done
* before processing finalizable objects or non-tracking weak hamdle to avoid finalizing/clearing
* objects that are in fact reachable.
*/
done_with_ephemerons = 0;
do {
done_with_ephemerons = mark_ephemerons_in_range (copy_func, start_addr, end_addr, queue);
sgen_drain_gray_stack (queue, -1);
++ephemeron_rounds;
} while (!done_with_ephemerons);
sgen_scan_togglerefs (copy_func, start_addr, end_addr, queue);
if (generation == GENERATION_OLD)
sgen_scan_togglerefs (copy_func, sgen_get_nursery_start (), sgen_get_nursery_end (), queue);
if (sgen_need_bridge_processing ()) {
collect_bridge_objects (copy_func, start_addr, end_addr, generation, queue);
if (generation == GENERATION_OLD)
collect_bridge_objects (copy_func, sgen_get_nursery_start (), sgen_get_nursery_end (), GENERATION_NURSERY, queue);
}
/*
Make sure we drain the gray stack before processing disappearing links and finalizers.
If we don't make sure it is empty we might wrongly see a live object as dead.
*/
sgen_drain_gray_stack (queue, -1);
/*
We must clear weak links that don't track resurrection before processing object ready for
finalization so they can be cleared before that.
*/
null_link_in_range (copy_func, start_addr, end_addr, generation, TRUE, queue);
if (generation == GENERATION_OLD)
null_link_in_range (copy_func, start_addr, end_addr, GENERATION_NURSERY, TRUE, queue);
/* walk the finalization queue and move also the objects that need to be
* finalized: use the finalized objects as new roots so the objects they depend
* on are also not reclaimed. As with the roots above, only objects in the nursery
* are marked/copied.
*/
finalize_in_range (copy_func, start_addr, end_addr, generation, queue);
if (generation == GENERATION_OLD)
finalize_in_range (copy_func, sgen_get_nursery_start (), sgen_get_nursery_end (), GENERATION_NURSERY, queue);
/* drain the new stack that might have been created */
DEBUG (6, fprintf (gc_debug_file, "Precise scan of gray area post fin\n"));
sgen_drain_gray_stack (queue, -1);
/*
* This must be done again after processing finalizable objects since CWL slots are cleared only after the key is finalized.
*/
done_with_ephemerons = 0;
do {
done_with_ephemerons = mark_ephemerons_in_range (copy_func, start_addr, end_addr, queue);
sgen_drain_gray_stack (queue, -1);
++ephemeron_rounds;
} while (!done_with_ephemerons);
/*
* Clear ephemeron pairs with unreachable keys.
* We pass the copy func so we can figure out if an array was promoted or not.
*/
clear_unreachable_ephemerons (copy_func, start_addr, end_addr, queue);
TV_GETTIME (btv);
DEBUG (2, fprintf (gc_debug_file, "Finalize queue handling scan for %s generation: %d usecs %d ephemeron roundss\n", generation_name (generation), TV_ELAPSED (atv, btv), ephemeron_rounds));
/*
* handle disappearing links
* Note we do this after checking the finalization queue because if an object
* survives (at least long enough to be finalized) we don't clear the link.
* This also deals with a possible issue with the monitor reclamation: with the Boehm
* GC a finalized object my lose the monitor because it is cleared before the finalizer is
* called.
*/
g_assert (sgen_gray_object_queue_is_empty (queue));
for (;;) {
null_link_in_range (copy_func, start_addr, end_addr, generation, FALSE, queue);
if (generation == GENERATION_OLD)
null_link_in_range (copy_func, start_addr, end_addr, GENERATION_NURSERY, FALSE, queue);
if (sgen_gray_object_queue_is_empty (queue))
break;
sgen_drain_gray_stack (queue, -1);
}
g_assert (sgen_gray_object_queue_is_empty (queue));
}
void
sgen_check_section_scan_starts (GCMemSection *section)
{
int i;
for (i = 0; i < section->num_scan_start; ++i) {
if (section->scan_starts [i]) {
guint size = safe_object_get_size ((MonoObject*) section->scan_starts [i]);
g_assert (size >= sizeof (MonoObject) && size <= MAX_SMALL_OBJ_SIZE);
}
}
}
static void
check_scan_starts (void)
{
if (!do_scan_starts_check)
return;
sgen_check_section_scan_starts (nursery_section);
major_collector.check_scan_starts ();
}
static void
scan_from_registered_roots (CopyOrMarkObjectFunc copy_func, char *addr_start, char *addr_end, int root_type, GrayQueue *queue)
{
void **start_root;
RootRecord *root;
SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
DEBUG (6, fprintf (gc_debug_file, "Precise root scan %p-%p (desc: %p)\n", start_root, root->end_root, (void*)root->root_desc));
precisely_scan_objects_from (copy_func, start_root, (void**)root->end_root, addr_start, addr_end, root->root_desc, queue);
} SGEN_HASH_TABLE_FOREACH_END;
}
void
sgen_dump_occupied (char *start, char *end, char *section_start)
{
fprintf (heap_dump_file, "<occupied offset=\"%td\" size=\"%td\"/>\n", start - section_start, end - start);
}
void
sgen_dump_section (GCMemSection *section, const char *type)
{
char *start = section->data;
char *end = section->data + section->size;
char *occ_start = NULL;
GCVTable *vt;
char *old_start = NULL; /* just for debugging */
fprintf (heap_dump_file, "<section type=\"%s\" size=\"%lu\">\n", type, (unsigned long)section->size);
while (start < end) {
guint size;
MonoClass *class;
if (!*(void**)start) {
if (occ_start) {
sgen_dump_occupied (occ_start, start, section->data);
occ_start = NULL;
}
start += sizeof (void*); /* should be ALLOC_ALIGN, really */
continue;
}
g_assert (start < section->next_data);
if (!occ_start)
occ_start = start;
vt = (GCVTable*)LOAD_VTABLE (start);
class = vt->klass;
size = ALIGN_UP (safe_object_get_size ((MonoObject*) start));
/*
fprintf (heap_dump_file, "<object offset=\"%d\" class=\"%s.%s\" size=\"%d\"/>\n",
start - section->data,
vt->klass->name_space, vt->klass->name,
size);
*/
old_start = start;
start += size;
}
if (occ_start)
sgen_dump_occupied (occ_start, start, section->data);
fprintf (heap_dump_file, "</section>\n");
}
static void
dump_object (MonoObject *obj, gboolean dump_location)
{
static char class_name [1024];
MonoClass *class = mono_object_class (obj);
int i, j;
/*
* Python's XML parser is too stupid to parse angle brackets
* in strings, so we just ignore them;
*/
i = j = 0;
while (class->name [i] && j < sizeof (class_name) - 1) {
if (!strchr ("<>\"", class->name [i]))
class_name [j++] = class->name [i];
++i;
}
g_assert (j < sizeof (class_name));
class_name [j] = 0;
fprintf (heap_dump_file, "<object class=\"%s.%s\" size=\"%d\"",
class->name_space, class_name,
safe_object_get_size (obj));
if (dump_location) {
const char *location;
if (ptr_in_nursery (obj))
location = "nursery";
else if (safe_object_get_size (obj) <= MAX_SMALL_OBJ_SIZE)
location = "major";
else
location = "LOS";
fprintf (heap_dump_file, " location=\"%s\"", location);
}
fprintf (heap_dump_file, "/>\n");
}
static void
dump_heap (const char *type, int num, const char *reason)
{
ObjectList *list;
LOSObject *bigobj;
fprintf (heap_dump_file, "<collection type=\"%s\" num=\"%d\"", type, num);
if (reason)
fprintf (heap_dump_file, " reason=\"%s\"", reason);
fprintf (heap_dump_file, ">\n");
fprintf (heap_dump_file, "<other-mem-usage type=\"mempools\" size=\"%ld\"/>\n", mono_mempool_get_bytes_allocated ());
sgen_dump_internal_mem_usage (heap_dump_file);
fprintf (heap_dump_file, "<pinned type=\"stack\" bytes=\"%zu\"/>\n", sgen_pin_stats_get_pinned_byte_count (PIN_TYPE_STACK));
/* fprintf (heap_dump_file, "<pinned type=\"static-data\" bytes=\"%d\"/>\n", pinned_byte_counts [PIN_TYPE_STATIC_DATA]); */
fprintf (heap_dump_file, "<pinned type=\"other\" bytes=\"%zu\"/>\n", sgen_pin_stats_get_pinned_byte_count (PIN_TYPE_OTHER));
fprintf (heap_dump_file, "<pinned-objects>\n");
for (list = sgen_pin_stats_get_object_list (); list; list = list->next)
dump_object (list->obj, TRUE);
fprintf (heap_dump_file, "</pinned-objects>\n");
sgen_dump_section (nursery_section, "nursery");
major_collector.dump_heap (heap_dump_file);
fprintf (heap_dump_file, "<los>\n");
for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
dump_object ((MonoObject*)bigobj->data, FALSE);
fprintf (heap_dump_file, "</los>\n");
fprintf (heap_dump_file, "</collection>\n");
}
void
sgen_register_moved_object (void *obj, void *destination)
{
g_assert (mono_profiler_events & MONO_PROFILE_GC_MOVES);
/* FIXME: handle this for parallel collector */
g_assert (!sgen_collection_is_parallel ());
if (moved_objects_idx == MOVED_OBJECTS_NUM) {
mono_profiler_gc_moves (moved_objects, moved_objects_idx);
moved_objects_idx = 0;
}
moved_objects [moved_objects_idx++] = obj;
moved_objects [moved_objects_idx++] = destination;
}
static void
init_stats (void)
{
static gboolean inited = FALSE;
if (inited)
return;
mono_counters_register ("Minor fragment clear", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_pre_collection_fragment_clear);
mono_counters_register ("Minor pinning", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_pinning);
mono_counters_register ("Minor scan remembered set", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_remsets);
mono_counters_register ("Minor scan pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_pinned);
mono_counters_register ("Minor scan registered roots", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_registered_roots);
mono_counters_register ("Minor scan thread data", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_thread_data);
mono_counters_register ("Minor finish gray stack", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_finish_gray_stack);
mono_counters_register ("Minor fragment creation", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_fragment_creation);
mono_counters_register ("Major fragment clear", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_pre_collection_fragment_clear);
mono_counters_register ("Major pinning", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_pinning);
mono_counters_register ("Major scan pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_pinned);
mono_counters_register ("Major scan registered roots", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_registered_roots);
mono_counters_register ("Major scan thread data", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_thread_data);
mono_counters_register ("Major scan alloc_pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_alloc_pinned);
mono_counters_register ("Major scan finalized", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_finalized);
mono_counters_register ("Major scan big objects", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_big_objects);
mono_counters_register ("Major finish gray stack", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_finish_gray_stack);
mono_counters_register ("Major free big objects", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_free_bigobjs);
mono_counters_register ("Major LOS sweep", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_los_sweep);
mono_counters_register ("Major sweep", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_sweep);
mono_counters_register ("Major fragment creation", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_fragment_creation);
mono_counters_register ("Number of pinned objects", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_pinned_objects);
#ifdef HEAVY_STATISTICS
mono_counters_register ("WBarrier set field", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_field);
mono_counters_register ("WBarrier set arrayref", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_arrayref);
mono_counters_register ("WBarrier arrayref copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_arrayref_copy);
mono_counters_register ("WBarrier generic store called", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_generic_store);
mono_counters_register ("WBarrier set root", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_root);
mono_counters_register ("WBarrier value copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_value_copy);
mono_counters_register ("WBarrier object copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_object_copy);
mono_counters_register ("# objects allocated degraded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_alloced_degraded);
mono_counters_register ("bytes allocated degraded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced_degraded);
mono_counters_register ("# copy_object() called (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_copy_object_called_nursery);
mono_counters_register ("# objects copied (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_copied_nursery);
mono_counters_register ("# copy_object() called (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_copy_object_called_major);
mono_counters_register ("# objects copied (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_copied_major);
mono_counters_register ("# scan_object() called (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_scan_object_called_nursery);
mono_counters_register ("# scan_object() called (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_scan_object_called_major);
mono_counters_register ("Slots allocated in vain", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_slots_allocated_in_vain);
mono_counters_register ("# nursery copy_object() failed from space", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_from_space);
mono_counters_register ("# nursery copy_object() failed forwarded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_forwarded);
mono_counters_register ("# nursery copy_object() failed pinned", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_pinned);
mono_counters_register ("# nursery copy_object() failed to space", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_to_space);
sgen_nursery_allocator_init_heavy_stats ();
sgen_alloc_init_heavy_stats ();
#endif
inited = TRUE;
}
static void
reset_pinned_from_failed_allocation (void)
{
bytes_pinned_from_failed_allocation = 0;
}
void
sgen_set_pinned_from_failed_allocation (mword objsize)
{
bytes_pinned_from_failed_allocation += objsize;
}
gboolean
sgen_collection_is_parallel (void)
{
switch (current_collection_generation) {
case GENERATION_NURSERY:
return nursery_collection_is_parallel;
case GENERATION_OLD:
return major_collector.is_parallel;
default:
g_error ("Invalid current generation %d", current_collection_generation);
}
}
typedef struct
{
char *heap_start;
char *heap_end;
} FinishRememberedSetScanJobData;
static void
job_finish_remembered_set_scan (WorkerData *worker_data, void *job_data_untyped)
{
FinishRememberedSetScanJobData *job_data = job_data_untyped;
remset.finish_scan_remsets (job_data->heap_start, job_data->heap_end, sgen_workers_get_job_gray_queue (worker_data));
}
typedef struct
{
CopyOrMarkObjectFunc func;
char *heap_start;
char *heap_end;
int root_type;
} ScanFromRegisteredRootsJobData;
static void
job_scan_from_registered_roots (WorkerData *worker_data, void *job_data_untyped)
{
ScanFromRegisteredRootsJobData *job_data = job_data_untyped;
scan_from_registered_roots (job_data->func,
job_data->heap_start, job_data->heap_end,
job_data->root_type,
sgen_workers_get_job_gray_queue (worker_data));
}
typedef struct
{
char *heap_start;
char *heap_end;
} ScanThreadDataJobData;
static void
job_scan_thread_data (WorkerData *worker_data, void *job_data_untyped)
{
ScanThreadDataJobData *job_data = job_data_untyped;
scan_thread_data (job_data->heap_start, job_data->heap_end, TRUE,
sgen_workers_get_job_gray_queue (worker_data));
}
typedef struct
{
FinalizeReadyEntry *list;
} ScanFinalizerEntriesJobData;
static void
job_scan_finalizer_entries (WorkerData *worker_data, void *job_data_untyped)
{
ScanFinalizerEntriesJobData *job_data = job_data_untyped;
scan_finalizer_entries (current_object_ops.copy_or_mark_object,
job_data->list,
sgen_workers_get_job_gray_queue (worker_data));
}
static void
verify_scan_starts (char *start, char *end)
{
int i;
for (i = 0; i < nursery_section->num_scan_start; ++i) {
char *addr = nursery_section->scan_starts [i];
if (addr > start && addr < end)
fprintf (gc_debug_file, "NFC-BAD SCAN START [%d] %p for obj [%p %p]\n", i, addr, start, end);
}
}
static void
verify_nursery (void)
{
char *start, *end, *cur, *hole_start;
if (!do_verify_nursery)
return;
/*This cleans up unused fragments */
sgen_nursery_allocator_prepare_for_pinning ();
hole_start = start = cur = sgen_get_nursery_start ();
end = sgen_get_nursery_end ();
while (cur < end) {
size_t ss, size;
if (!*(void**)cur) {
cur += sizeof (void*);
continue;
}
if (object_is_forwarded (cur))
fprintf (gc_debug_file, "FORWARDED OBJ %p\n", cur);
else if (object_is_pinned (cur))
fprintf (gc_debug_file, "PINNED OBJ %p\n", cur);
ss = safe_object_get_size ((MonoObject*)cur);
size = ALIGN_UP (safe_object_get_size ((MonoObject*)cur));
verify_scan_starts (cur, cur + size);
if (do_dump_nursery_content) {
if (cur > hole_start)
fprintf (gc_debug_file, "HOLE [%p %p %d]\n", hole_start, cur, (int)(cur - hole_start));
fprintf (gc_debug_file, "OBJ [%p %p %d %d %s %d]\n", cur, cur + size, (int)size, (int)ss, sgen_safe_name ((MonoObject*)cur), (gpointer)LOAD_VTABLE (cur) == sgen_get_array_fill_vtable ());
}
cur += size;
hole_start = cur;
}
fflush (gc_debug_file);
}
/*
* Collect objects in the nursery. Returns whether to trigger a major
* collection.
*/
static gboolean
collect_nursery (void)
{
gboolean needs_major;
size_t max_garbage_amount;
char *nursery_next;
FinishRememberedSetScanJobData frssjd;
ScanFromRegisteredRootsJobData scrrjd_normal, scrrjd_wbarrier;
ScanFinalizerEntriesJobData sfejd_fin_ready, sfejd_critical_fin;
ScanThreadDataJobData stdjd;
mword fragment_total;
TV_DECLARE (all_atv);
TV_DECLARE (all_btv);
TV_DECLARE (atv);
TV_DECLARE (btv);
if (disable_minor_collections)
return TRUE;
MONO_GC_BEGIN (GENERATION_NURSERY);
verify_nursery ();
mono_perfcounters->gc_collections0++;
current_collection_generation = GENERATION_NURSERY;
if (sgen_collection_is_parallel ())
current_object_ops = sgen_minor_collector.parallel_ops;
else
current_object_ops = sgen_minor_collector.serial_ops;
reset_pinned_from_failed_allocation ();
binary_protocol_collection (GENERATION_NURSERY);
check_scan_starts ();
sgen_nursery_alloc_prepare_for_minor ();
degraded_mode = 0;
objects_pinned = 0;
nursery_next = sgen_nursery_alloc_get_upper_alloc_bound ();
/* FIXME: optimize later to use the higher address where an object can be present */
nursery_next = MAX (nursery_next, sgen_get_nursery_end ());
DEBUG (1, fprintf (gc_debug_file, "Start nursery collection %d %p-%p, size: %d\n", stat_minor_gcs, sgen_get_nursery_start (), nursery_next, (int)(nursery_next - sgen_get_nursery_start ())));
max_garbage_amount = nursery_next - sgen_get_nursery_start ();
g_assert (nursery_section->size >= max_garbage_amount);
/* world must be stopped already */
TV_GETTIME (all_atv);
atv = all_atv;
TV_GETTIME (btv);
time_minor_pre_collection_fragment_clear += TV_ELAPSED (atv, btv);
if (xdomain_checks) {
sgen_clear_nursery_fragments ();
check_for_xdomain_refs ();
}
nursery_section->next_data = nursery_next;
major_collector.start_nursery_collection ();
sgen_memgov_minor_collection_start ();
sgen_gray_object_queue_init (&gray_queue);
sgen_workers_init_distribute_gray_queue ();
stat_minor_gcs++;
gc_stats.minor_gc_count ++;
if (remset.prepare_for_minor_collection)
remset.prepare_for_minor_collection ();
process_fin_stage_entries ();
process_dislink_stage_entries ();
/* pin from pinned handles */
sgen_init_pinning ();
mono_profiler_gc_event (MONO_GC_EVENT_MARK_START, 0);
pin_from_roots (sgen_get_nursery_start (), nursery_next, WORKERS_DISTRIBUTE_GRAY_QUEUE);
/* identify pinned objects */
sgen_optimize_pin_queue (0);
sgen_pinning_setup_section (nursery_section);
sgen_pin_objects_in_section (nursery_section, WORKERS_DISTRIBUTE_GRAY_QUEUE);
sgen_pinning_trim_queue_to_section (nursery_section);
TV_GETTIME (atv);
time_minor_pinning += TV_ELAPSED (btv, atv);
DEBUG (2, fprintf (gc_debug_file, "Finding pinned pointers: %d in %d usecs\n", sgen_get_pinned_count (), TV_ELAPSED (btv, atv)));
DEBUG (4, fprintf (gc_debug_file, "Start scan with %d pinned objects\n", sgen_get_pinned_count ()));
if (whole_heap_check_before_collection)
sgen_check_whole_heap ();
if (consistency_check_at_minor_collection)
sgen_check_consistency ();
sgen_workers_start_all_workers ();
/*
* Perform the sequential part of remembered set scanning.
* This usually involves scanning global information that might later be produced by evacuation.
*/
if (remset.begin_scan_remsets)
remset.begin_scan_remsets (sgen_get_nursery_start (), nursery_next, WORKERS_DISTRIBUTE_GRAY_QUEUE);
sgen_workers_start_marking ();
frssjd.heap_start = sgen_get_nursery_start ();
frssjd.heap_end = nursery_next;
sgen_workers_enqueue_job (job_finish_remembered_set_scan, &frssjd);
/* we don't have complete write barrier yet, so we scan all the old generation sections */
TV_GETTIME (btv);
time_minor_scan_remsets += TV_ELAPSED (atv, btv);
DEBUG (2, fprintf (gc_debug_file, "Old generation scan: %d usecs\n", TV_ELAPSED (atv, btv)));
if (!sgen_collection_is_parallel ())
sgen_drain_gray_stack (&gray_queue, -1);
if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
report_registered_roots ();
if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
report_finalizer_roots ();
TV_GETTIME (atv);
time_minor_scan_pinned += TV_ELAPSED (btv, atv);
/* registered roots, this includes static fields */
scrrjd_normal.func = current_object_ops.copy_or_mark_object;
scrrjd_normal.heap_start = sgen_get_nursery_start ();
scrrjd_normal.heap_end = nursery_next;
scrrjd_normal.root_type = ROOT_TYPE_NORMAL;
sgen_workers_enqueue_job (job_scan_from_registered_roots, &scrrjd_normal);
scrrjd_wbarrier.func = current_object_ops.copy_or_mark_object;
scrrjd_wbarrier.heap_start = sgen_get_nursery_start ();
scrrjd_wbarrier.heap_end = nursery_next;
scrrjd_wbarrier.root_type = ROOT_TYPE_WBARRIER;
sgen_workers_enqueue_job (job_scan_from_registered_roots, &scrrjd_wbarrier);
TV_GETTIME (btv);
time_minor_scan_registered_roots += TV_ELAPSED (atv, btv);
/* thread data */
stdjd.heap_start = sgen_get_nursery_start ();
stdjd.heap_end = nursery_next;
sgen_workers_enqueue_job (job_scan_thread_data, &stdjd);
TV_GETTIME (atv);
time_minor_scan_thread_data += TV_ELAPSED (btv, atv);
btv = atv;
if (sgen_collection_is_parallel ()) {
while (!sgen_gray_object_queue_is_empty (WORKERS_DISTRIBUTE_GRAY_QUEUE)) {
sgen_workers_distribute_gray_queue_sections ();
g_usleep (1000);
}
}
sgen_workers_join ();
if (sgen_collection_is_parallel ())
g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
/* Scan the list of objects ready for finalization. If */
sfejd_fin_ready.list = fin_ready_list;
sgen_workers_enqueue_job (job_scan_finalizer_entries, &sfejd_fin_ready);
sfejd_critical_fin.list = critical_fin_list;
sgen_workers_enqueue_job (job_scan_finalizer_entries, &sfejd_critical_fin);
finish_gray_stack (sgen_get_nursery_start (), nursery_next, GENERATION_NURSERY, &gray_queue);
TV_GETTIME (atv);
time_minor_finish_gray_stack += TV_ELAPSED (btv, atv);
mono_profiler_gc_event (MONO_GC_EVENT_MARK_END, 0);
/*
* The (single-threaded) finalization code might have done
* some copying/marking so we can only reset the GC thread's
* worker data here instead of earlier when we joined the
* workers.
*/
sgen_workers_reset_data ();
if (objects_pinned) {
sgen_optimize_pin_queue (0);
sgen_pinning_setup_section (nursery_section);
}
/* walk the pin_queue, build up the fragment list of free memory, unmark
* pinned objects as we go, memzero() the empty fragments so they are ready for the
* next allocations.
*/
mono_profiler_gc_event (MONO_GC_EVENT_RECLAIM_START, 0);
fragment_total = sgen_build_nursery_fragments (nursery_section, nursery_section->pin_queue_start, nursery_section->pin_queue_num_entries);
if (!fragment_total)
degraded_mode = 1;
/* Clear TLABs for all threads */
sgen_clear_tlabs ();
mono_profiler_gc_event (MONO_GC_EVENT_RECLAIM_END, 0);
TV_GETTIME (btv);
time_minor_fragment_creation += TV_ELAPSED (atv, btv);
DEBUG (2, fprintf (gc_debug_file, "Fragment creation: %d usecs, %lu bytes available\n", TV_ELAPSED (atv, btv), (unsigned long)fragment_total));
if (consistency_check_at_minor_collection)
sgen_check_major_refs ();
major_collector.finish_nursery_collection ();
TV_GETTIME (all_btv);
gc_stats.minor_gc_time_usecs += TV_ELAPSED (all_atv, all_btv);
if (heap_dump_file)
dump_heap ("minor", stat_minor_gcs - 1, NULL);
/* prepare the pin queue for the next collection */
sgen_finish_pinning ();
if (fin_ready_list || critical_fin_list) {
DEBUG (4, fprintf (gc_debug_file, "Finalizer-thread wakeup: ready %d\n", num_ready_finalizers));
mono_gc_finalize_notify ();
}
sgen_pin_stats_reset ();
g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
if (remset.finish_minor_collection)
remset.finish_minor_collection ();
check_scan_starts ();
binary_protocol_flush_buffers (FALSE);
sgen_memgov_minor_collection_end ();
/*objects are late pinned because of lack of memory, so a major is a good call*/
needs_major = objects_pinned > 0;
current_collection_generation = -1;
objects_pinned = 0;
MONO_GC_END (GENERATION_NURSERY);
return needs_major;
}
static gboolean
major_do_collection (const char *reason)
{
LOSObject *bigobj, *prevbo;
TV_DECLARE (all_atv);
TV_DECLARE (all_btv);
TV_DECLARE (atv);
TV_DECLARE (btv);
/* FIXME: only use these values for the precise scan
* note that to_space pointers should be excluded anyway...
*/
char *heap_start = NULL;
char *heap_end = (char*)-1;
int old_next_pin_slot;
ScanFromRegisteredRootsJobData scrrjd_normal, scrrjd_wbarrier;
ScanThreadDataJobData stdjd;
ScanFinalizerEntriesJobData sfejd_fin_ready, sfejd_critical_fin;
MONO_GC_BEGIN (GENERATION_OLD);
current_collection_generation = GENERATION_OLD;
mono_perfcounters->gc_collections1++;
current_object_ops = major_collector.major_ops;
reset_pinned_from_failed_allocation ();
sgen_memgov_major_collection_start ();
//count_ref_nonref_objs ();
//consistency_check ();
binary_protocol_collection (GENERATION_OLD);
check_scan_starts ();
sgen_gray_object_queue_init (&gray_queue);
sgen_workers_init_distribute_gray_queue ();
sgen_nursery_alloc_prepare_for_major ();
degraded_mode = 0;
DEBUG (1, fprintf (gc_debug_file, "Start major collection %d\n", stat_major_gcs));
stat_major_gcs++;
gc_stats.major_gc_count ++;
/* world must be stopped already */
TV_GETTIME (all_atv);
atv = all_atv;
/* Pinning depends on this */
sgen_clear_nursery_fragments ();
if (whole_heap_check_before_collection)
sgen_check_whole_heap ();
TV_GETTIME (btv);
time_major_pre_collection_fragment_clear += TV_ELAPSED (atv, btv);
nursery_section->next_data = sgen_get_nursery_end ();
/* we should also coalesce scanning from sections close to each other
* and deal with pointers outside of the sections later.
*/
if (major_collector.start_major_collection)
major_collector.start_major_collection ();
objects_pinned = 0;
*major_collector.have_swept = FALSE;
if (xdomain_checks) {
sgen_clear_nursery_fragments ();
check_for_xdomain_refs ();
}
/* Remsets are not useful for a major collection */
remset.prepare_for_major_collection ();
process_fin_stage_entries ();
process_dislink_stage_entries ();
TV_GETTIME (atv);
sgen_init_pinning ();
DEBUG (6, fprintf (gc_debug_file, "Collecting pinned addresses\n"));
pin_from_roots ((void*)lowest_heap_address, (void*)highest_heap_address, WORKERS_DISTRIBUTE_GRAY_QUEUE);
sgen_optimize_pin_queue (0);
/*
* pin_queue now contains all candidate pointers, sorted and
* uniqued. We must do two passes now to figure out which
* objects are pinned.
*
* The first is to find within the pin_queue the area for each
* section. This requires that the pin_queue be sorted. We
* also process the LOS objects and pinned chunks here.
*
* The second, destructive, pass is to reduce the section
* areas to pointers to the actually pinned objects.
*/
DEBUG (6, fprintf (gc_debug_file, "Pinning from sections\n"));
/* first pass for the sections */
sgen_find_section_pin_queue_start_end (nursery_section);
major_collector.find_pin_queue_start_ends (WORKERS_DISTRIBUTE_GRAY_QUEUE);
/* identify possible pointers to the insize of large objects */
DEBUG (6, fprintf (gc_debug_file, "Pinning from large objects\n"));
for (bigobj = los_object_list; bigobj; bigobj = bigobj->next) {
int dummy;
gboolean profile_roots = mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS;
GCRootReport report;
report.count = 0;
if (sgen_find_optimized_pin_queue_area (bigobj->data, (char*)bigobj->data + bigobj->size, &dummy)) {
binary_protocol_pin (bigobj->data, (gpointer)LOAD_VTABLE (bigobj->data), safe_object_get_size (((MonoObject*)(bigobj->data))));
if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (bigobj->data);
MONO_GC_OBJ_PINNED ((mword)bigobj->data, sgen_safe_object_get_size ((MonoObject*)bigobj->data), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
}
pin_object (bigobj->data);
/* FIXME: only enqueue if object has references */
GRAY_OBJECT_ENQUEUE (WORKERS_DISTRIBUTE_GRAY_QUEUE, bigobj->data);
if (G_UNLIKELY (do_pin_stats))
sgen_pin_stats_register_object ((char*) bigobj->data, safe_object_get_size ((MonoObject*) bigobj->data));
DEBUG (6, fprintf (gc_debug_file, "Marked large object %p (%s) size: %lu from roots\n", bigobj->data, safe_name (bigobj->data), (unsigned long)bigobj->size));
if (profile_roots)
add_profile_gc_root (&report, bigobj->data, MONO_PROFILE_GC_ROOT_PINNING | MONO_PROFILE_GC_ROOT_MISC, 0);
}
if (profile_roots)
notify_gc_roots (&report);
}
/* second pass for the sections */
sgen_pin_objects_in_section (nursery_section, WORKERS_DISTRIBUTE_GRAY_QUEUE);
major_collector.pin_objects (WORKERS_DISTRIBUTE_GRAY_QUEUE);
old_next_pin_slot = sgen_get_pinned_count ();
TV_GETTIME (btv);
time_major_pinning += TV_ELAPSED (atv, btv);
DEBUG (2, fprintf (gc_debug_file, "Finding pinned pointers: %d in %d usecs\n", sgen_get_pinned_count (), TV_ELAPSED (atv, btv)));
DEBUG (4, fprintf (gc_debug_file, "Start scan with %d pinned objects\n", sgen_get_pinned_count ()));
major_collector.init_to_space ();
#ifdef SGEN_DEBUG_INTERNAL_ALLOC
main_gc_thread = mono_native_thread_self ();
#endif
sgen_workers_start_all_workers ();
sgen_workers_start_marking ();
if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
report_registered_roots ();
TV_GETTIME (atv);
time_major_scan_pinned += TV_ELAPSED (btv, atv);
/* registered roots, this includes static fields */
scrrjd_normal.func = current_object_ops.copy_or_mark_object;
scrrjd_normal.heap_start = heap_start;
scrrjd_normal.heap_end = heap_end;
scrrjd_normal.root_type = ROOT_TYPE_NORMAL;
sgen_workers_enqueue_job (job_scan_from_registered_roots, &scrrjd_normal);
scrrjd_wbarrier.func = current_object_ops.copy_or_mark_object;
scrrjd_wbarrier.heap_start = heap_start;
scrrjd_wbarrier.heap_end = heap_end;
scrrjd_wbarrier.root_type = ROOT_TYPE_WBARRIER;
sgen_workers_enqueue_job (job_scan_from_registered_roots, &scrrjd_wbarrier);
TV_GETTIME (btv);
time_major_scan_registered_roots += TV_ELAPSED (atv, btv);
/* Threads */
stdjd.heap_start = heap_start;
stdjd.heap_end = heap_end;
sgen_workers_enqueue_job (job_scan_thread_data, &stdjd);
TV_GETTIME (atv);
time_major_scan_thread_data += TV_ELAPSED (btv, atv);
TV_GETTIME (btv);
time_major_scan_alloc_pinned += TV_ELAPSED (atv, btv);
if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
report_finalizer_roots ();
/* scan the list of objects ready for finalization */
sfejd_fin_ready.list = fin_ready_list;
sgen_workers_enqueue_job (job_scan_finalizer_entries, &sfejd_fin_ready);
sfejd_critical_fin.list = critical_fin_list;
sgen_workers_enqueue_job (job_scan_finalizer_entries, &sfejd_critical_fin);
TV_GETTIME (atv);
time_major_scan_finalized += TV_ELAPSED (btv, atv);
DEBUG (2, fprintf (gc_debug_file, "Root scan: %d usecs\n", TV_ELAPSED (btv, atv)));
TV_GETTIME (btv);
time_major_scan_big_objects += TV_ELAPSED (atv, btv);
if (major_collector.is_parallel) {
while (!sgen_gray_object_queue_is_empty (WORKERS_DISTRIBUTE_GRAY_QUEUE)) {
sgen_workers_distribute_gray_queue_sections ();
g_usleep (1000);
}
}
sgen_workers_join ();
#ifdef SGEN_DEBUG_INTERNAL_ALLOC
main_gc_thread = NULL;
#endif
if (major_collector.is_parallel)
g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
/* all the objects in the heap */
finish_gray_stack (heap_start, heap_end, GENERATION_OLD, &gray_queue);
TV_GETTIME (atv);
time_major_finish_gray_stack += TV_ELAPSED (btv, atv);
/*
* The (single-threaded) finalization code might have done
* some copying/marking so we can only reset the GC thread's
* worker data here instead of earlier when we joined the
* workers.
*/
sgen_workers_reset_data ();
if (objects_pinned) {
/*This is slow, but we just OOM'd*/
sgen_pin_queue_clear_discarded_entries (nursery_section, old_next_pin_slot);
sgen_optimize_pin_queue (0);
sgen_find_section_pin_queue_start_end (nursery_section);
objects_pinned = 0;
}
reset_heap_boundaries ();
sgen_update_heap_boundaries ((mword)sgen_get_nursery_start (), (mword)sgen_get_nursery_end ());
/* sweep the big objects list */
prevbo = NULL;
for (bigobj = los_object_list; bigobj;) {
if (object_is_pinned (bigobj->data)) {
unpin_object (bigobj->data);
sgen_update_heap_boundaries ((mword)bigobj->data, (mword)bigobj->data + bigobj->size);
} else {
LOSObject *to_free;
/* not referenced anywhere, so we can free it */
if (prevbo)
prevbo->next = bigobj->next;
else
los_object_list = bigobj->next;
to_free = bigobj;
bigobj = bigobj->next;
sgen_los_free_object (to_free);
continue;
}
prevbo = bigobj;
bigobj = bigobj->next;
}
TV_GETTIME (btv);
time_major_free_bigobjs += TV_ELAPSED (atv, btv);
sgen_los_sweep ();
TV_GETTIME (atv);
time_major_los_sweep += TV_ELAPSED (btv, atv);
major_collector.sweep ();
TV_GETTIME (btv);
time_major_sweep += TV_ELAPSED (atv, btv);
/* walk the pin_queue, build up the fragment list of free memory, unmark
* pinned objects as we go, memzero() the empty fragments so they are ready for the
* next allocations.
*/
if (!sgen_build_nursery_fragments (nursery_section, nursery_section->pin_queue_start, nursery_section->pin_queue_num_entries))
degraded_mode = 1;
/* Clear TLABs for all threads */
sgen_clear_tlabs ();
TV_GETTIME (atv);
time_major_fragment_creation += TV_ELAPSED (btv, atv);
TV_GETTIME (all_btv);
gc_stats.major_gc_time_usecs += TV_ELAPSED (all_atv, all_btv);
if (heap_dump_file)
dump_heap ("major", stat_major_gcs - 1, reason);
/* prepare the pin queue for the next collection */
sgen_finish_pinning ();
if (fin_ready_list || critical_fin_list) {
DEBUG (4, fprintf (gc_debug_file, "Finalizer-thread wakeup: ready %d\n", num_ready_finalizers));
mono_gc_finalize_notify ();
}
sgen_pin_stats_reset ();
g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
sgen_memgov_major_collection_end ();
current_collection_generation = -1;
major_collector.finish_major_collection ();
check_scan_starts ();
binary_protocol_flush_buffers (FALSE);
//consistency_check ();
MONO_GC_END (GENERATION_OLD);
return bytes_pinned_from_failed_allocation > 0;
}
static gboolean major_do_collection (const char *reason);
/*
* Ensure an allocation request for @size will succeed by freeing enough memory.
*
* LOCKING: The GC lock MUST be held.
*/
void
sgen_ensure_free_space (size_t size)
{
int generation_to_collect = -1;
const char *reason = NULL;
if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
if (sgen_need_major_collection (size)) {
reason = "LOS overflow";
generation_to_collect = GENERATION_OLD;
}
} else {
if (degraded_mode) {
if (sgen_need_major_collection (size)) {
reason = "Degraded mode overflow";
generation_to_collect = GENERATION_OLD;
}
} else if (sgen_need_major_collection (size)) {
reason = "Minor allowance";
generation_to_collect = GENERATION_OLD;
} else {
generation_to_collect = GENERATION_NURSERY;
reason = "Nursery full";
}
}
if (generation_to_collect == -1)
return;
sgen_perform_collection (size, generation_to_collect, reason);
}
void
sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason)
{
TV_DECLARE (gc_end);
GGTimingInfo infos [2];
int overflow_generation_to_collect = -1;
const char *overflow_reason = NULL;
memset (infos, 0, sizeof (infos));
mono_profiler_gc_event (MONO_GC_EVENT_START, generation_to_collect);
infos [0].generation = generation_to_collect;
infos [0].reason = reason;
infos [0].is_overflow = FALSE;
TV_GETTIME (infos [0].total_time);
infos [1].generation = -1;
stop_world (generation_to_collect);
//FIXME extract overflow reason
if (generation_to_collect == GENERATION_NURSERY) {
if (collect_nursery ()) {
overflow_generation_to_collect = GENERATION_OLD;
overflow_reason = "Minor overflow";
}
} else {
if (major_do_collection (reason)) {
overflow_generation_to_collect = GENERATION_NURSERY;
overflow_reason = "Excessive pinning";
}
}
TV_GETTIME (gc_end);
infos [0].total_time = SGEN_TV_ELAPSED (infos [0].total_time, gc_end);
if (overflow_generation_to_collect != -1) {
mono_profiler_gc_event (MONO_GC_EVENT_START, overflow_generation_to_collect);
infos [1].generation = overflow_generation_to_collect;
infos [1].reason = overflow_reason;
infos [1].is_overflow = TRUE;
infos [1].total_time = gc_end;
if (overflow_generation_to_collect == GENERATION_NURSERY)
collect_nursery ();
else
major_do_collection (overflow_reason);
TV_GETTIME (gc_end);
infos [1].total_time = SGEN_TV_ELAPSED (infos [1].total_time, gc_end);
/* keep events symmetric */
mono_profiler_gc_event (MONO_GC_EVENT_END, overflow_generation_to_collect);
}
DEBUG (2, fprintf (gc_debug_file, "Heap size: %lu, LOS size: %lu\n", (unsigned long)mono_gc_get_heap_size (), (unsigned long)los_memory_usage));
/* this also sets the proper pointers for the next allocation */
if (generation_to_collect == GENERATION_NURSERY && !sgen_can_alloc_size (requested_size)) {
/* TypeBuilder and MonoMethod are killing mcs with fragmentation */
DEBUG (1, fprintf (gc_debug_file, "nursery collection didn't find enough room for %zd alloc (%d pinned)\n", requested_size, sgen_get_pinned_count ()));
sgen_dump_pin_queue ();
degraded_mode = 1;
}
restart_world (generation_to_collect, infos);
mono_profiler_gc_event (MONO_GC_EVENT_END, generation_to_collect);
}
/*
* ######################################################################
* ######## Memory allocation from the OS
* ######################################################################
* This section of code deals with getting memory from the OS and
* allocating memory for GC-internal data structures.
* Internal memory can be handled with a freelist for small objects.
*/
/*
* Debug reporting.
*/
G_GNUC_UNUSED static void
report_internal_mem_usage (void)
{
printf ("Internal memory usage:\n");
sgen_report_internal_mem_usage ();
printf ("Pinned memory usage:\n");
major_collector.report_pinned_memory_usage ();
}
/*
* ######################################################################
* ######## Finalization support
* ######################################################################
*/
/*
* If the object has been forwarded it means it's still referenced from a root.
* If it is pinned it's still alive as well.
* A LOS object is only alive if we have pinned it.
* Return TRUE if @obj is ready to be finalized.
*/
static inline gboolean
sgen_is_object_alive (void *object)
{
if (ptr_in_nursery (object))
return sgen_nursery_is_object_alive (object);
/* Oldgen objects can be pinned and forwarded too */
if (SGEN_OBJECT_IS_PINNED (object) || SGEN_OBJECT_IS_FORWARDED (object))
return TRUE;
return major_collector.is_object_live (object);
}
gboolean
sgen_gc_is_object_ready_for_finalization (void *object)
{
return !sgen_is_object_alive (object);
}
static gboolean
has_critical_finalizer (MonoObject *obj)
{
MonoClass *class;
if (!mono_defaults.critical_finalizer_object)
return FALSE;
class = ((MonoVTable*)LOAD_VTABLE (obj))->klass;
return mono_class_has_parent_fast (class, mono_defaults.critical_finalizer_object);
}
static void
queue_finalization_entry (MonoObject *obj) {
FinalizeReadyEntry *entry = sgen_alloc_internal (INTERNAL_MEM_FINALIZE_READY_ENTRY);
entry->object = obj;
if (has_critical_finalizer (obj)) {
entry->next = critical_fin_list;
critical_fin_list = entry;
} else {
entry->next = fin_ready_list;
fin_ready_list = entry;
}
}
static inline int
object_is_reachable (char *object, char *start, char *end)
{
/*This happens for non nursery objects during minor collections. We just treat all objects as alive.*/
if (object < start || object >= end)
return TRUE;
return sgen_is_object_alive (object);
}
#include "sgen-fin-weak-hash.c"
gboolean
sgen_object_is_live (void *obj)
{
if (ptr_in_nursery (obj))
return object_is_pinned (obj);
/* FIXME This is semantically wrong! All tenured object are considered alive during a nursery collection. */
if (current_collection_generation == GENERATION_NURSERY)
return FALSE;
return major_collector.is_object_live (obj);
}
/* LOCKING: requires that the GC lock is held */
static void
null_ephemerons_for_domain (MonoDomain *domain)
{
EphemeronLinkNode *current = ephemeron_list, *prev = NULL;
while (current) {
MonoObject *object = (MonoObject*)current->array;
if (object && !object->vtable) {
EphemeronLinkNode *tmp = current;
if (prev)
prev->next = current->next;
else
ephemeron_list = current->next;
current = current->next;
sgen_free_internal (tmp, INTERNAL_MEM_EPHEMERON_LINK);
} else {
prev = current;
current = current->next;
}
}
}
/* LOCKING: requires that the GC lock is held */
static void
clear_unreachable_ephemerons (CopyOrMarkObjectFunc copy_func, char *start, char *end, GrayQueue *queue)
{
int was_in_nursery, was_promoted;
EphemeronLinkNode *current = ephemeron_list, *prev = NULL;
MonoArray *array;
Ephemeron *cur, *array_end;
char *tombstone;
while (current) {
char *object = current->array;
if (!object_is_reachable (object, start, end)) {
EphemeronLinkNode *tmp = current;
DEBUG (5, fprintf (gc_debug_file, "Dead Ephemeron array at %p\n", object));
if (prev)
prev->next = current->next;
else
ephemeron_list = current->next;
current = current->next;
sgen_free_internal (tmp, INTERNAL_MEM_EPHEMERON_LINK);
continue;
}
was_in_nursery = ptr_in_nursery (object);
copy_func ((void**)&object, queue);
current->array = object;
/*The array was promoted, add global remsets for key/values left behind in nursery.*/
was_promoted = was_in_nursery && !ptr_in_nursery (object);
DEBUG (5, fprintf (gc_debug_file, "Clearing unreachable entries for ephemeron array at %p\n", object));
array = (MonoArray*)object;
cur = mono_array_addr (array, Ephemeron, 0);
array_end = cur + mono_array_length_fast (array);
tombstone = (char*)((MonoVTable*)LOAD_VTABLE (object))->domain->ephemeron_tombstone;
for (; cur < array_end; ++cur) {
char *key = (char*)cur->key;
if (!key || key == tombstone)
continue;
DEBUG (5, fprintf (gc_debug_file, "[%td] key %p (%s) value %p (%s)\n", cur - mono_array_addr (array, Ephemeron, 0),
key, object_is_reachable (key, start, end) ? "reachable" : "unreachable",
cur->value, cur->value && object_is_reachable (cur->value, start, end) ? "reachable" : "unreachable"));
if (!object_is_reachable (key, start, end)) {
cur->key = tombstone;
cur->value = NULL;
continue;
}
if (was_promoted) {
if (ptr_in_nursery (key)) {/*key was not promoted*/
DEBUG (5, fprintf (gc_debug_file, "\tAdded remset to key %p\n", key));
sgen_add_to_global_remset (&cur->key);
}
if (ptr_in_nursery (cur->value)) {/*value was not promoted*/
DEBUG (5, fprintf (gc_debug_file, "\tAdded remset to value %p\n", cur->value));
sgen_add_to_global_remset (&cur->value);
}
}
}
prev = current;
current = current->next;
}
}
/* LOCKING: requires that the GC lock is held */
static int
mark_ephemerons_in_range (CopyOrMarkObjectFunc copy_func, char *start, char *end, GrayQueue *queue)
{
int nothing_marked = 1;
EphemeronLinkNode *current = ephemeron_list;
MonoArray *array;
Ephemeron *cur, *array_end;
char *tombstone;