Move the sgen_*_os_memory APIs to memgov.
authorRodrigo Kumpera <kumpera@gmail.com>
Mon, 2 Jul 2012 15:32:36 +0000 (12:32 -0300)
committerRodrigo Kumpera <kumpera@gmail.com>
Mon, 2 Jul 2012 20:53:08 +0000 (17:53 -0300)
mono/metadata/sgen-cardtable.c
mono/metadata/sgen-debug.c
mono/metadata/sgen-gc.c
mono/metadata/sgen-gc.h
mono/metadata/sgen-internal.c
mono/metadata/sgen-major-copying.c
mono/metadata/sgen-memory-governor.c
mono/metadata/sgen-memory-governor.h
mono/metadata/sgen-pinned-allocator.c

index e74623121de955c6255e4316b82b9c554f563543..84f699dc0dccb05545f5e92969180ab10c56e73a 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "metadata/sgen-gc.h"
 #include "metadata/sgen-cardtable.h"
+#include "metadata/sgen-memory-governor.h"
 #include "utils/mono-counters.h"
 #include "utils/mono-time.h"
 #include "utils/mono-memory-model.h"
index 66f62c89a786aa790677f3566dccbca5ef5f3326..9e193260d2284dc50aa63f7a8fe83e3ea8fb21eb 100644 (file)
@@ -36,6 +36,7 @@
 #include "metadata/sgen-cardtable.h"
 #include "metadata/sgen-ssb.h"
 #include "metadata/sgen-protocol.h"
+#include "metadata/sgen-memory-governor.h"
 
 #define LOAD_VTABLE    SGEN_LOAD_VTABLE
 
index 463b86e0f1a253df0b43da05d48e1ca134a3a44e..6fa780bd1663361267ff030f825800a2a050582e 100644 (file)
@@ -419,7 +419,6 @@ int degraded_mode = 0;
 
 static mword bytes_pinned_from_failed_allocation = 0;
 
-static mword total_alloc = 0;
 /* use this to tune when to do a major/minor collection */
 static mword memory_pressure = 0;
 static mword minor_collection_allowance;
@@ -1614,51 +1613,6 @@ sgen_update_heap_boundaries (mword low, mword high)
        } while (SGEN_CAS_PTR ((gpointer*)&highest_heap_address, (gpointer)high, (gpointer)old) != (gpointer)old);
 }
 
-static unsigned long
-prot_flags_for_activate (int activate)
-{
-       unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
-       return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
-}
-
-/*
- * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
- * This must not require any lock.
- */
-void*
-sgen_alloc_os_memory (size_t size, int activate)
-{
-       void *ptr = mono_valloc (0, size, prot_flags_for_activate (activate));
-       if (ptr) {
-               /* FIXME: CAS */
-               total_alloc += size;
-       }
-       return ptr;
-}
-
-/* size must be a power of 2 */
-void*
-sgen_alloc_os_memory_aligned (mword size, mword alignment, gboolean activate)
-{
-       void *ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (activate));
-       if (ptr) {
-               /* FIXME: CAS */
-               total_alloc += size;
-       }
-       return ptr;
-}
-
-/*
- * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
- */
-void
-sgen_free_os_memory (void *addr, size_t size)
-{
-       mono_vfree (addr, size);
-       /* FIXME: CAS */
-       total_alloc -= size;
-}
-
 /*
  * Allocate and setup the data structures needed to be able to allocate objects
  * in the nursery. The nursery is stored in nursery_section.
@@ -1682,13 +1636,17 @@ alloc_nursery (void)
        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)total_alloc));
+       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;
@@ -3119,7 +3077,7 @@ minor_collect_or_expand_inner (size_t size)
                        major_gc_time = mono_100ns_ticks () - major_gc_time;
                        mono_profiler_gc_event (MONO_GC_EVENT_END, 1);
                }
-               DEBUG (2, fprintf (gc_debug_file, "Heap size: %lu, LOS size: %lu\n", (unsigned long)total_alloc, (unsigned long)los_memory_usage));
+               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));
                restart_world (0);
 
                total_gc_time = mono_100ns_ticks () - total_gc_time;
@@ -4523,12 +4481,6 @@ mono_gc_get_used_size (void)
        return tot;
 }
 
-int64_t
-mono_gc_get_heap_size (void)
-{
-       return total_alloc;
-}
-
 void
 mono_gc_disable (void)
 {
index 550f03fcd3a4c5766895dfbf920bb6518c5d2338..3d2f37bfab55cf27819570b0cc216af2b454cc2d 100644 (file)
@@ -377,10 +377,6 @@ enum {
 
 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
 
-void* sgen_alloc_os_memory (size_t size, int activate) MONO_INTERNAL;
-void* sgen_alloc_os_memory_aligned (mword size, mword alignment, gboolean activate) MONO_INTERNAL;
-void sgen_free_os_memory (void *addr, size_t size) MONO_INTERNAL;
-
 int sgen_thread_handshake (BOOL suspend) MONO_INTERNAL;
 gboolean sgen_suspend_thread (SgenThreadInfo *info) MONO_INTERNAL;
 gboolean sgen_resume_thread (SgenThreadInfo *info) MONO_INTERNAL;
@@ -794,6 +790,7 @@ void sgen_gc_lock (void) MONO_INTERNAL;
 void sgen_gc_unlock (void) MONO_INTERNAL;
 
 enum {
+       SPACE_NURSERY,
        SPACE_MAJOR,
        SPACE_LOS
 };
index c499236a2c5088a7334607ca20443048950542c6..eba4405c50068a5f3fb6044a15168ad386efb88c 100644 (file)
@@ -28,6 +28,7 @@
 #include "utils/mono-counters.h"
 #include "metadata/sgen-gc.h"
 #include "utils/lock-free-alloc.h"
+#include "metadata/sgen-memory-governor.h"
 
 /* keep each size a multiple of ALLOC_ALIGN */
 static const int allocator_sizes [] = {
index 7aef21559855e843254ded2ecfc5070b3a70047a..f97ae5fa9ca720d2700ed1f8b023a28a462a4b67 100644 (file)
@@ -57,6 +57,7 @@
 #include "metadata/mono-gc.h"
 #include "metadata/object-internals.h"
 #include "metadata/profiler-private.h"
+#include "metadata/sgen-memory-governor.h"
 
 #define MAJOR_SECTION_SIZE             SGEN_PINNED_CHUNK_SIZE
 #define BLOCK_FOR_OBJECT(o)            SGEN_PINNED_CHUNK_FOR_PTR ((o))
index 05f79b727cebce1568978a4319cbbddeb63dc06a..cd3f0f317bdcb8134a3456258da98625b108ef30 100644 (file)
 
 #include "metadata/sgen-gc.h"
 #include "metadata/sgen-memory-governor.h"
+#include "metadata/mono-gc.h"
+
 #include "utils/mono-counters.h"
+#include "utils/mono-mmap.h"
 
 #define MIN_MINOR_COLLECTION_ALLOWANCE ((mword)(DEFAULT_NURSERY_SIZE * SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO))
 
@@ -44,7 +47,68 @@ static mword max_heap_size = ((mword)0)- ((mword)1);
 static mword soft_heap_limit = ((mword)0) - ((mword)1);
 static mword allocated_heap;
 
+/*Memory usage tracking */
+static mword total_alloc = 0;
+
+/*
+Global GC memory tracking.
+This tracks the total usage of memory by the GC. This includes
+managed and unmanaged memory.
+*/
+
+static unsigned long
+prot_flags_for_activate (int activate)
+{
+       unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
+       return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
+}
+
+/*
+ * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
+ * This must not require any lock.
+ */
+void*
+sgen_alloc_os_memory (size_t size, int activate)
+{
+       void *ptr = mono_valloc (0, size, prot_flags_for_activate (activate));
+       if (ptr)
+               SGEN_ATOMIC_ADD_P (total_alloc, size);
+       return ptr;
+}
+
+/* size must be a power of 2 */
+void*
+sgen_alloc_os_memory_aligned (size_t size, mword alignment, gboolean activate)
+{
+       void *ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (activate));
+       if (ptr)
+               SGEN_ATOMIC_ADD_P (total_alloc, size);
+       return ptr;
+}
+
+/*
+ * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
+ */
+void
+sgen_free_os_memory (void *addr, size_t size)
+{
+       mono_vfree (addr, size);
+       SGEN_ATOMIC_ADD_P (total_alloc, -size);
+}
 
+int64_t
+mono_gc_get_heap_size (void)
+{
+       return total_alloc;
+}
+
+
+/*
+Heap Sizing limits.
+This limit the max size of the heap. It takes into account
+only memory actively in use to hold heap objects and not
+for other parts of the GC.
+ */
 mword
 sgen_memgov_available_free_space (void)
 {
index 72cc102d7d627d71677ed8c4f822230f9faff12f..1a63a98dcc603992a2e6b45110194a347cc296b7 100644 (file)
@@ -31,5 +31,10 @@ mword sgen_memgov_adjust_allowance (mword allowance_estimate, mword new_heap_siz
 mword sgen_memgov_min_allowance (void) MONO_INTERNAL;
 mword sgen_memgov_available_free_space (void) MONO_INTERNAL;
 
+/* OS memory allocation */
+void* sgen_alloc_os_memory (size_t size, int activate) MONO_INTERNAL;
+void* sgen_alloc_os_memory_aligned (size_t size, mword alignment, gboolean activate) MONO_INTERNAL;
+void sgen_free_os_memory (void *addr, size_t size) MONO_INTERNAL;
+
 #endif
 
index 5240cdee2b1d0f926ddab6dc104e65f12d74fe85..d35ec594ec456e932ad11e756ae99424149bfb32 100644 (file)
@@ -51,6 +51,7 @@
 
 #include "utils/mono-counters.h"
 #include "metadata/sgen-gc.h"
+#include "metadata/sgen-memory-governor.h"
 
 /* Pinned objects are allocated in the LOS space if bigger than half a page
  * or from freelists otherwise. We assume that pinned objects are relatively few