Merge pull request #2582 from ludovic-henry/fix-threadpool-starvation
[mono-project.git] / mono / sgen / sgen-memory-governor.c
blobce34f7f4a67a7d304ff038e674c01aa38651e0c5
1 /*
2 * sgen-memory-governor.c: When to schedule collections based on
3 * memory usage.
5 * Author:
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 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License 2.0 as published by the Free Software Foundation;
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License 2.0 along with this library; if not, write to the Free
24 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "config.h"
28 #ifdef HAVE_SGEN_GC
30 #include <stdlib.h>
32 #include "mono/sgen/sgen-gc.h"
33 #include "mono/sgen/sgen-memory-governor.h"
34 #include "mono/sgen/sgen-thread-pool.h"
35 #include "mono/sgen/sgen-client.h"
37 #define MIN_MINOR_COLLECTION_ALLOWANCE ((mword)(DEFAULT_NURSERY_SIZE * default_allowance_nursery_size_ratio))
39 /*Heap limits and allocation knobs*/
40 static mword max_heap_size = ((mword)0)- ((mword)1);
41 static mword soft_heap_limit = ((mword)0) - ((mword)1);
43 static double default_allowance_nursery_size_ratio = SGEN_DEFAULT_ALLOWANCE_NURSERY_SIZE_RATIO;
44 static double save_target_ratio = SGEN_DEFAULT_SAVE_TARGET_RATIO;
46 /**/
47 static mword allocated_heap;
48 static mword total_alloc = 0;
49 static mword total_alloc_max = 0;
51 /* GC triggers. */
53 static gboolean debug_print_allowance = FALSE;
56 /* use this to tune when to do a major/minor collection */
57 static mword major_collection_trigger_size;
59 static mword last_major_num_sections = 0;
60 static mword last_los_memory_usage = 0;
62 static gboolean need_calculate_minor_collection_allowance;
64 /* The size of the LOS after the last major collection, after sweeping. */
65 static mword last_collection_los_memory_usage = 0;
67 static mword sgen_memgov_available_free_space (void);
70 /* GC trigger heuristics. */
72 static void
73 sgen_memgov_calculate_minor_collection_allowance (void)
75 size_t new_major, new_heap_size, allowance_target, allowance;
77 if (!need_calculate_minor_collection_allowance)
78 return;
80 SGEN_ASSERT (0, major_collector.have_swept (), "Can only calculate allowance if heap is swept");
82 new_major = major_collector.get_bytes_survived_last_sweep ();
83 new_heap_size = new_major + last_collection_los_memory_usage;
86 * We allow the heap to grow by one third its current size before we start the next
87 * major collection.
89 allowance_target = new_heap_size * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO;
91 allowance = MAX (allowance_target, MIN_MINOR_COLLECTION_ALLOWANCE);
93 if (new_heap_size + allowance > soft_heap_limit) {
94 if (new_heap_size > soft_heap_limit)
95 allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
96 else
97 allowance = MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
100 /* FIXME: Why is this here? */
101 if (major_collector.free_swept_blocks)
102 major_collector.free_swept_blocks (allowance);
104 major_collection_trigger_size = new_heap_size + allowance;
106 need_calculate_minor_collection_allowance = FALSE;
108 if (debug_print_allowance) {
109 SGEN_LOG (0, "Surviving sweep: %ld bytes (%ld major, %ld LOS)", (long)new_heap_size, (long)new_major, (long)last_collection_los_memory_usage);
110 SGEN_LOG (0, "Allowance: %ld bytes", (long)allowance);
111 SGEN_LOG (0, "Trigger size: %ld bytes", (long)major_collection_trigger_size);
115 static inline size_t
116 get_heap_size (void)
118 return major_collector.get_num_major_sections () * major_collector.section_size + los_memory_usage;
121 gboolean
122 sgen_need_major_collection (mword space_needed)
124 size_t heap_size;
126 if (sgen_concurrent_collection_in_progress ()) {
127 heap_size = get_heap_size ();
129 if (heap_size <= major_collection_trigger_size)
130 return FALSE;
132 /* We allow the heap to grow an additional third of the allowance during a concurrent collection */
133 if ((heap_size - major_collection_trigger_size) >
134 (major_collection_trigger_size
135 * (SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO / (SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO + 1))
136 * SGEN_DEFAULT_CONCURRENT_HEAP_ALLOWANCE_RATIO)) {
137 return TRUE;
139 return FALSE;
142 /* FIXME: This is a cop-out. We should have some way of figuring this out. */
143 if (!major_collector.have_swept ())
144 return FALSE;
146 if (space_needed > sgen_memgov_available_free_space ())
147 return TRUE;
149 sgen_memgov_calculate_minor_collection_allowance ();
151 heap_size = get_heap_size ();
153 return heap_size > major_collection_trigger_size;
156 void
157 sgen_memgov_minor_collection_start (void)
161 void
162 sgen_memgov_minor_collection_end (void)
166 void
167 sgen_memgov_major_collection_start (void)
169 need_calculate_minor_collection_allowance = TRUE;
171 if (debug_print_allowance) {
172 SGEN_LOG (0, "Starting collection with heap size %ld bytes", (long)get_heap_size ());
176 void
177 sgen_memgov_major_collection_end (gboolean forced)
179 last_collection_los_memory_usage = los_memory_usage;
181 if (forced) {
182 sgen_get_major_collector ()->finish_sweeping ();
183 sgen_memgov_calculate_minor_collection_allowance ();
187 void
188 sgen_memgov_collection_start (int generation)
192 void
193 sgen_memgov_collection_end (int generation, GGTimingInfo* info, int info_count)
195 int i;
196 for (i = 0; i < info_count; ++i) {
197 if (info[i].generation != -1)
198 sgen_client_log_timing (&info [i], last_major_num_sections, last_los_memory_usage);
200 last_major_num_sections = major_collector.get_num_major_sections ();
204 Global GC memory tracking.
205 This tracks the total usage of memory by the GC. This includes
206 managed and unmanaged memory.
209 static unsigned long
210 prot_flags_for_activate (int activate)
212 unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
213 return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
216 void
217 sgen_assert_memory_alloc (void *ptr, size_t requested_size, const char *assert_description)
219 if (ptr || !assert_description)
220 return;
221 fprintf (stderr, "Error: Garbage collector could not allocate %zu bytes of memory for %s.\n", requested_size, assert_description);
222 exit (1);
226 * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
227 * This must not require any lock.
229 void*
230 sgen_alloc_os_memory (size_t size, SgenAllocFlags flags, const char *assert_description)
232 void *ptr;
234 g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
236 ptr = mono_valloc (0, size, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
237 sgen_assert_memory_alloc (ptr, size, assert_description);
238 if (ptr) {
239 SGEN_ATOMIC_ADD_P (total_alloc, size);
240 total_alloc_max = MAX (total_alloc_max, total_alloc);
242 return ptr;
245 /* size must be a power of 2 */
246 void*
247 sgen_alloc_os_memory_aligned (size_t size, mword alignment, SgenAllocFlags flags, const char *assert_description)
249 void *ptr;
251 g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
253 ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
254 sgen_assert_memory_alloc (ptr, size, assert_description);
255 if (ptr) {
256 SGEN_ATOMIC_ADD_P (total_alloc, size);
257 total_alloc_max = MAX (total_alloc_max, total_alloc);
259 return ptr;
263 * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
265 void
266 sgen_free_os_memory (void *addr, size_t size, SgenAllocFlags flags)
268 g_assert (!(flags & ~SGEN_ALLOC_HEAP));
270 mono_vfree (addr, size);
271 SGEN_ATOMIC_ADD_P (total_alloc, -(gssize)size);
272 total_alloc_max = MAX (total_alloc_max, total_alloc);
275 size_t
276 sgen_gc_get_total_heap_allocation (void)
278 return total_alloc;
283 Heap Sizing limits.
284 This limit the max size of the heap. It takes into account
285 only memory actively in use to hold heap objects and not
286 for other parts of the GC.
288 static mword
289 sgen_memgov_available_free_space (void)
291 return max_heap_size - MIN (allocated_heap, max_heap_size);
294 void
295 sgen_memgov_release_space (mword size, int space)
297 SGEN_ATOMIC_ADD_P (allocated_heap, -(gssize)size);
300 gboolean
301 sgen_memgov_try_alloc_space (mword size, int space)
303 if (sgen_memgov_available_free_space () < size) {
304 SGEN_ASSERT (4, !sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()), "Memory shouldn't run out in worker thread");
305 return FALSE;
308 SGEN_ATOMIC_ADD_P (allocated_heap, size);
309 sgen_client_total_allocated_heap_changed (allocated_heap);
310 return TRUE;
313 void
314 sgen_memgov_init (size_t max_heap, size_t soft_limit, gboolean debug_allowance, double allowance_ratio, double save_target)
316 if (soft_limit)
317 soft_heap_limit = soft_limit;
319 debug_print_allowance = debug_allowance;
320 major_collection_trigger_size = MIN_MINOR_COLLECTION_ALLOWANCE;
322 mono_counters_register ("Memgov alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_VARIABLE, &total_alloc);
323 mono_counters_register ("Memgov max alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_MONOTONIC, &total_alloc_max);
325 if (max_heap == 0)
326 return;
328 if (max_heap < soft_limit) {
329 sgen_env_var_error (MONO_GC_PARAMS_NAME, "Setting to minimum.", "`max-heap-size` must be at least as large as `soft-heap-limit`.");
330 max_heap = soft_limit;
333 if (max_heap < sgen_nursery_size * 4) {
334 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`.");
335 max_heap = sgen_nursery_size * 4;
337 max_heap_size = max_heap - sgen_nursery_size;
339 if (allowance_ratio)
340 default_allowance_nursery_size_ratio = allowance_ratio;
342 if (save_target)
343 save_target_ratio = save_target;
346 #endif