2009-02-04 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / boehm-gc.c
index 7de44839a7978ae1411d2c8213bd7f202f8d4058..a48428614f48a74f17602df54e3d87fd97feead2 100644 (file)
@@ -1,11 +1,13 @@
 /*
  * boehm-gc.c: GC implementation using either the installed or included Boehm GC.
  *
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include "config.h"
 #define GC_I_HIDE_POINTERS
-#include <mono/os/gc_wrapper.h>
+#include <mono/metadata/gc-internal.h>
 #include <mono/metadata/mono-gc.h>
 #include <mono/metadata/gc-internal.h>
 #include <mono/metadata/profiler-private.h>
@@ -13,6 +15,8 @@
 #include <mono/metadata/method-builder.h>
 #include <mono/metadata/opcodes.h>
 #include <mono/utils/mono-logger.h>
+#include <mono/utils/mono-time.h>
+#include <mono/utils/dtrace.h>
 
 #if HAVE_BOEHM_GC
 
@@ -89,8 +93,8 @@ mono_gc_base_init (void)
        }
 #endif
 
-       GC_init ();
        GC_no_dls = TRUE;
+       GC_init ();
        GC_oom_fn = mono_gc_out_of_memory;
        GC_set_warn_proc (mono_gc_warning);
        GC_finalize_on_demand = 1;
@@ -99,14 +103,25 @@ mono_gc_base_init (void)
 #ifdef HAVE_GC_GCJ_MALLOC
        GC_init_gcj_malloc (5, NULL);
 #endif
-
+       mono_gc_enable_events ();
        gc_initialized = TRUE;
 }
 
 void
 mono_gc_collect (int generation)
 {
+       MONO_PROBE_GC_BEGIN (generation);
+
+       mono_perfcounters->gc_induced++;
        GC_gcollect ();
+       
+       MONO_PROBE_GC_END (generation);
+#if defined(ENABLE_DTRACE) && defined(__sun__)
+       /* This works around a dtrace -G problem on Solaris.
+          Limit its actual use to when the probe is enabled. */
+       if (MONO_PROBE_GC_END_ENABLED ())
+               sleep(0);
+#endif
 }
 
 int
@@ -167,7 +182,9 @@ mono_gc_enable (void)
 gboolean
 mono_gc_is_gc_thread (void)
 {
-#ifdef USE_INCLUDED_LIBGC
+#if GC_VERSION_MAJOR >= 7
+       return TRUE;
+#elif defined(USE_INCLUDED_LIBGC)
        return GC_thread_is_registered ();
 #else
        return TRUE;
@@ -179,6 +196,25 @@ extern int GC_thread_register_foreign (void *base_addr);
 gboolean
 mono_gc_register_thread (void *baseptr)
 {
+#if GC_VERSION_MAJOR >= 7
+       struct GC_stack_base sb;
+       int res;
+
+       res = GC_get_stack_base (&sb);
+       if (res != GC_SUCCESS) {
+               sb.mem_base = baseptr;
+#ifdef __ia64__
+               /* Can't determine the register stack bounds */
+               g_error ("mono_gc_register_thread failed ().\n");
+#endif
+       }
+       res = GC_register_my_thread (&sb);
+       if ((res != GC_SUCCESS) && (res != GC_DUPLICATE)) {
+               g_warning ("GC_register_my_thread () failed.\n");
+               return FALSE;
+       }
+       return TRUE;
+#else
        if (mono_gc_is_gc_thread())
                return TRUE;
 #if defined(USE_INCLUDED_LIBGC) && !defined(PLATFORM_WIN32)
@@ -186,6 +222,7 @@ mono_gc_register_thread (void *baseptr)
 #else
        return FALSE;
 #endif
+#endif
 }
 
 gboolean
@@ -200,15 +237,34 @@ mono_object_is_alive (MonoObject* o)
 
 #ifdef USE_INCLUDED_LIBGC
 
+static gint64 gc_start_time;
+
 static void
 on_gc_notification (GCEventType event)
 {
+       if (event == MONO_GC_EVENT_START) {
+               mono_perfcounters->gc_collections0++;
+               mono_stats.major_gc_count ++;
+               gc_start_time = mono_100ns_ticks ();
+       } else if (event == MONO_GC_EVENT_END) {
+               guint64 heap_size = GC_get_heap_size ();
+               guint64 used_size = heap_size - GC_get_free_bytes ();
+               mono_perfcounters->gc_total_bytes = used_size;
+               mono_perfcounters->gc_committed_bytes = heap_size;
+               mono_perfcounters->gc_reserved_bytes = heap_size;
+               mono_perfcounters->gc_gen0size = heap_size;
+               mono_stats.major_gc_time_usecs += (mono_100ns_ticks () - gc_start_time) / 10;
+       }
        mono_profiler_gc_event ((MonoGCEvent) event, 0);
 }
  
 static void
 on_gc_heap_resize (size_t new_size)
 {
+       guint64 heap_size = GC_get_heap_size ();
+       mono_perfcounters->gc_committed_bytes = heap_size;
+       mono_perfcounters->gc_reserved_bytes = heap_size;
+       mono_perfcounters->gc_gen0size = heap_size;
        mono_profiler_gc_heap_resize (new_size);
 }
 
@@ -237,6 +293,16 @@ mono_gc_register_root (char *start, size_t size, void *descr)
        return TRUE;
 }
 
+void
+mono_gc_deregister_root (char* addr)
+{
+#ifndef PLATFORM_WIN32
+       /* FIXME: libgc doesn't define this work win32 for some reason */
+       /* FIXME: No size info */
+       GC_remove_roots (addr, addr + sizeof (gpointer) + 1);
+#endif
+}
+
 void
 mono_gc_weak_link_add (void **link_addr, MonoObject *obj)
 {
@@ -673,6 +739,12 @@ mono_gc_get_managed_allocator_by_type (int atype)
        return res;
 }
 
+guint32
+mono_gc_get_managed_allocator_types (void)
+{
+       return ATYPE_NUM;
+}
+
 #else
 
 MonoMethod*
@@ -693,6 +765,12 @@ mono_gc_get_managed_allocator_by_type (int atype)
        return NULL;
 }
 
+guint32
+mono_gc_get_managed_allocator_types (void)
+{
+       return 0;
+}
+
 #endif
 
 #endif /* no Boehm GC */