2 * sgen-memory-governor.c: When to schedule collections based on
6 * Rodrigo Kumpera (rkumpera@novell.com)
8 * Copyright 2001-2003 Ximian, Inc
9 * Copyright 2003-2010 Novell, Inc.
10 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
11 * Copyright (C) 2012 Xamarin Inc
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
21 #include "mono/sgen/sgen-gc.h"
22 #include "mono/sgen/sgen-memory-governor.h"
23 #include "mono/sgen/sgen-thread-pool.h"
24 #include "mono/sgen/sgen-client.h"
26 #define MIN_MINOR_COLLECTION_ALLOWANCE ((mword)(DEFAULT_NURSERY_SIZE * default_allowance_nursery_size_ratio))
28 mword total_promoted_size = 0;
29 static mword total_promoted_size_start;
31 /*Heap limits and allocation knobs*/
32 static mword max_heap_size = ((mword)0)- ((mword)1);
33 static mword soft_heap_limit = ((mword)0) - ((mword)1);
35 static double default_allowance_nursery_size_ratio = SGEN_DEFAULT_ALLOWANCE_NURSERY_SIZE_RATIO;
36 static double save_target_ratio = SGEN_DEFAULT_SAVE_TARGET_RATIO;
39 static mword allocated_heap;
40 static mword total_alloc = 0;
41 static mword total_alloc_max = 0;
45 static gboolean debug_print_allowance = FALSE;
48 /* use this to tune when to do a major/minor collection */
49 static mword major_collection_trigger_size;
51 static mword major_pre_sweep_heap_size;
52 static mword major_start_heap_size;
54 static gboolean need_calculate_minor_collection_allowance;
56 /* The size of the LOS after the last major collection, after sweeping. */
57 static mword last_collection_los_memory_usage = 0;
59 static mword sgen_memgov_available_free_space (void);
62 /* GC trigger heuristics. */
65 sgen_memgov_calculate_minor_collection_allowance (void)
67 size_t new_major, new_heap_size, allowance_target, allowance;
70 if (!need_calculate_minor_collection_allowance)
73 SGEN_ASSERT (0, major_collector.have_swept (), "Can only calculate allowance if heap is swept");
75 new_major = major_collector.get_bytes_survived_last_sweep ();
76 new_heap_size = new_major + last_collection_los_memory_usage;
79 * We allow the heap to grow by one third its current size before we start the next
82 allowance_target = new_heap_size * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO;
84 allowance = MAX (allowance_target, MIN_MINOR_COLLECTION_ALLOWANCE);
87 * For the concurrent collector, we decrease the allowance relative to the memory
88 * growth during the M&S phase, survival rate of the collection and the allowance
91 decrease = (major_pre_sweep_heap_size - major_start_heap_size) * ((float)new_heap_size / major_pre_sweep_heap_size) * (SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO + 1);
92 if (decrease > allowance)
94 allowance -= decrease;
96 if (new_heap_size + allowance > soft_heap_limit) {
97 if (new_heap_size > soft_heap_limit)
98 allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
100 allowance = MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
103 /* FIXME: Why is this here? */
104 if (major_collector.free_swept_blocks)
105 major_collector.free_swept_blocks (allowance);
107 major_collection_trigger_size = new_heap_size + allowance;
109 need_calculate_minor_collection_allowance = FALSE;
111 if (debug_print_allowance) {
112 SGEN_LOG (0, "Surviving sweep: %ld bytes (%ld major, %ld LOS)", (long)new_heap_size, (long)new_major, (long)last_collection_los_memory_usage);
113 SGEN_LOG (0, "Allowance: %ld bytes", (long)allowance);
114 SGEN_LOG (0, "Trigger size: %ld bytes", (long)major_collection_trigger_size);
121 return major_collector.get_num_major_sections () * major_collector.section_size + los_memory_usage;
125 sgen_need_major_collection (mword space_needed)
129 if (sgen_concurrent_collection_in_progress ()) {
130 heap_size = get_heap_size ();
132 if (heap_size <= major_collection_trigger_size)
136 * The more the heap grows, the more we need to decrease the allowance above,
137 * in order to have similar trigger sizes as the synchronous collector.
138 * If the heap grows so much that we would need to have a negative allowance,
139 * we force the finishing of the collection, to avoid increased memory usage.
141 if ((heap_size - major_start_heap_size) > major_start_heap_size * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO)
146 /* FIXME: This is a cop-out. We should have some way of figuring this out. */
147 if (!major_collector.have_swept ())
150 if (space_needed > sgen_memgov_available_free_space ())
153 sgen_memgov_calculate_minor_collection_allowance ();
155 heap_size = get_heap_size ();
157 return heap_size > major_collection_trigger_size;
161 sgen_memgov_minor_collection_start (void)
163 total_promoted_size_start = total_promoted_size;
167 sgen_memgov_minor_collection_end (void)
172 sgen_memgov_major_pre_sweep (void)
174 if (sgen_concurrent_collection_in_progress ()) {
175 major_pre_sweep_heap_size = get_heap_size ();
177 /* We decrease the allowance only in the concurrent case */
178 major_pre_sweep_heap_size = major_start_heap_size;
183 sgen_memgov_major_post_sweep (void)
185 mword num_major_sections = major_collector.get_num_major_sections ();
187 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MAJOR_SWEEP: major %dK",
188 num_major_sections * major_collector.section_size / 1024);
192 sgen_memgov_major_collection_start (void)
194 need_calculate_minor_collection_allowance = TRUE;
195 major_start_heap_size = get_heap_size ();
197 if (debug_print_allowance) {
198 SGEN_LOG (0, "Starting collection with heap size %ld bytes", (long)major_start_heap_size);
203 sgen_memgov_major_collection_end (gboolean forced)
205 last_collection_los_memory_usage = los_memory_usage;
208 sgen_get_major_collector ()->finish_sweeping ();
209 sgen_memgov_calculate_minor_collection_allowance ();
214 sgen_memgov_collection_start (int generation)
219 sgen_memgov_collection_end (int generation, GGTimingInfo* info, int info_count)
222 for (i = 0; i < info_count; ++i) {
223 if (info[i].generation != -1)
224 sgen_client_log_timing (&info [i], total_promoted_size - total_promoted_size_start);
229 Global GC memory tracking.
230 This tracks the total usage of memory by the GC. This includes
231 managed and unmanaged memory.
235 prot_flags_for_activate (int activate)
237 unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
238 return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
242 sgen_assert_memory_alloc (void *ptr, size_t requested_size, const char *assert_description)
244 if (ptr || !assert_description)
246 fprintf (stderr, "Error: Garbage collector could not allocate %zu bytes of memory for %s.\n", requested_size, assert_description);
251 * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
252 * This must not require any lock.
255 sgen_alloc_os_memory (size_t size, SgenAllocFlags flags, const char *assert_description)
259 g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
261 ptr = mono_valloc (0, size, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
262 sgen_assert_memory_alloc (ptr, size, assert_description);
264 SGEN_ATOMIC_ADD_P (total_alloc, size);
265 total_alloc_max = MAX (total_alloc_max, total_alloc);
270 /* size must be a power of 2 */
272 sgen_alloc_os_memory_aligned (size_t size, mword alignment, SgenAllocFlags flags, const char *assert_description)
276 g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
278 ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
279 sgen_assert_memory_alloc (ptr, size, assert_description);
281 SGEN_ATOMIC_ADD_P (total_alloc, size);
282 total_alloc_max = MAX (total_alloc_max, total_alloc);
288 * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
291 sgen_free_os_memory (void *addr, size_t size, SgenAllocFlags flags)
293 g_assert (!(flags & ~SGEN_ALLOC_HEAP));
295 mono_vfree (addr, size);
296 SGEN_ATOMIC_ADD_P (total_alloc, -(gssize)size);
297 total_alloc_max = MAX (total_alloc_max, total_alloc);
301 sgen_gc_get_total_heap_allocation (void)
309 This limit the max size of the heap. It takes into account
310 only memory actively in use to hold heap objects and not
311 for other parts of the GC.
314 sgen_memgov_available_free_space (void)
316 return max_heap_size - MIN (allocated_heap, max_heap_size);
320 sgen_memgov_release_space (mword size, int space)
322 SGEN_ATOMIC_ADD_P (allocated_heap, -(gssize)size);
326 sgen_memgov_try_alloc_space (mword size, int space)
328 if (sgen_memgov_available_free_space () < size) {
329 SGEN_ASSERT (4, !sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()), "Memory shouldn't run out in worker thread");
333 SGEN_ATOMIC_ADD_P (allocated_heap, size);
334 sgen_client_total_allocated_heap_changed (allocated_heap);
339 sgen_memgov_init (size_t max_heap, size_t soft_limit, gboolean debug_allowance, double allowance_ratio, double save_target)
342 soft_heap_limit = soft_limit;
344 debug_print_allowance = debug_allowance;
345 major_collection_trigger_size = MIN_MINOR_COLLECTION_ALLOWANCE;
347 mono_counters_register ("Memgov alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_VARIABLE, &total_alloc);
348 mono_counters_register ("Memgov max alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_MONOTONIC, &total_alloc_max);
353 if (max_heap < soft_limit) {
354 sgen_env_var_error (MONO_GC_PARAMS_NAME, "Setting to minimum.", "`max-heap-size` must be at least as large as `soft-heap-limit`.");
355 max_heap = soft_limit;
358 if (max_heap < sgen_nursery_size * 4) {
359 sgen_env_var_error (MONO_GC_PARAMS_NAME, "Setting to minimum.", "`max-heap-size` must be at least 4 times as large as `nursery size`.");
360 max_heap = sgen_nursery_size * 4;
362 max_heap_size = max_heap - sgen_nursery_size;
365 default_allowance_nursery_size_ratio = allowance_ratio;
368 save_target_ratio = save_target;