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 * 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.
30 #include "metadata/sgen-gc.h"
31 #include "metadata/sgen-memory-governor.h"
32 #include "metadata/mono-gc.h"
34 #include "utils/mono-counters.h"
35 #include "utils/mono-mmap.h"
36 #include "utils/mono-logger-internal.h"
37 #include "utils/dtrace.h"
39 #define MIN_MINOR_COLLECTION_ALLOWANCE ((mword)(DEFAULT_NURSERY_SIZE * default_allowance_nursery_size_ratio))
41 /*Heap limits and allocation knobs*/
42 static mword max_heap_size = ((mword)0)- ((mword)1);
43 static mword soft_heap_limit = ((mword)0) - ((mword)1);
45 static double default_allowance_nursery_size_ratio = SGEN_DEFAULT_ALLOWANCE_NURSERY_SIZE_RATIO;
46 static double save_target_ratio = SGEN_DEFAULT_SAVE_TARGET_RATIO;
49 static mword allocated_heap;
50 static mword total_alloc = 0;
51 static mword total_alloc_max = 0;
55 static gboolean debug_print_allowance = FALSE;
58 /* use this to tune when to do a major/minor collection */
59 static mword memory_pressure = 0;
60 static mword minor_collection_allowance;
61 static mword minor_collection_sections_alloced = 0;
63 static mword last_major_num_sections = 0;
64 static mword last_los_memory_usage = 0;
66 static gboolean need_calculate_minor_collection_allowance;
68 static mword last_collection_old_num_major_sections;
69 static mword last_collection_los_memory_usage = 0;
70 static mword last_collection_old_los_memory_usage;
71 static mword last_collection_los_memory_alloced;
73 static mword sgen_memgov_available_free_space (void);
76 /* GC trigger heuristics. */
79 sgen_memgov_try_calculate_minor_collection_allowance (gboolean overwrite)
81 size_t num_major_sections;
82 mword new_major, new_heap_size, allowance_target;
85 g_assert (need_calculate_minor_collection_allowance);
87 if (!need_calculate_minor_collection_allowance)
90 if (!*major_collector.have_swept) {
92 minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
96 num_major_sections = major_collector.get_num_major_sections ();
98 new_major = num_major_sections * major_collector.section_size;
99 new_heap_size = new_major + last_collection_los_memory_usage;
102 * We allow the heap to grow by one third its current size before we start the next
105 allowance_target = new_heap_size / 3;
107 minor_collection_allowance = MAX (allowance_target, MIN_MINOR_COLLECTION_ALLOWANCE);
109 if (new_heap_size + minor_collection_allowance > soft_heap_limit) {
110 if (new_heap_size > soft_heap_limit)
111 minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
113 minor_collection_allowance = MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
116 if (debug_print_allowance) {
117 mword old_major = last_collection_old_num_major_sections * major_collector.section_size;
119 SGEN_LOG (1, "Before collection: %ld bytes (%ld major, %ld LOS)",
120 (long)(old_major + last_collection_old_los_memory_usage), (long)old_major, (long)last_collection_old_los_memory_usage);
121 SGEN_LOG (1, "After collection: %ld bytes (%ld major, %ld LOS)",
122 (long)new_heap_size, (long)new_major, (long)last_collection_los_memory_usage);
123 SGEN_LOG (1, "Allowance: %ld bytes", (long)minor_collection_allowance);
126 if (major_collector.have_computed_minor_collection_allowance)
127 major_collector.have_computed_minor_collection_allowance ();
129 need_calculate_minor_collection_allowance = FALSE;
134 sgen_need_major_collection (mword space_needed)
137 if (sgen_concurrent_collection_in_progress ())
139 los_alloced = los_memory_usage - MIN (last_collection_los_memory_usage, los_memory_usage);
140 return (space_needed > sgen_memgov_available_free_space ()) ||
141 minor_collection_sections_alloced * major_collector.section_size + los_alloced > minor_collection_allowance;
145 sgen_memgov_minor_collection_start (void)
147 sgen_memgov_try_calculate_minor_collection_allowance (FALSE);
151 sgen_memgov_minor_collection_end (void)
156 sgen_memgov_major_collection_start (void)
158 last_collection_old_num_major_sections = sgen_get_major_collector ()->get_num_major_sections ();
161 * A domain could have been freed, resulting in
162 * los_memory_usage being less than last_collection_los_memory_usage.
164 last_collection_los_memory_alloced = los_memory_usage - MIN (last_collection_los_memory_usage, los_memory_usage);
165 last_collection_old_los_memory_usage = los_memory_usage;
167 need_calculate_minor_collection_allowance = TRUE;
171 sgen_memgov_major_collection_end (void)
173 sgen_memgov_try_calculate_minor_collection_allowance (TRUE);
175 minor_collection_sections_alloced = 0;
176 last_collection_los_memory_usage = los_memory_usage;
180 sgen_memgov_collection_start (int generation)
182 last_major_num_sections = major_collector.get_num_major_sections ();
183 last_los_memory_usage = los_memory_usage;
187 log_timming (GGTimingInfo *info)
189 //unsigned long stw_time, unsigned long bridge_time, gboolean is_overflow
190 mword num_major_sections = major_collector.get_num_major_sections ();
191 char full_timing_buff [1024];
192 full_timing_buff [0] = '\0';
194 if (!info->is_overflow)
195 sprintf (full_timing_buff, "total %.2fms, bridge %.2fms", info->stw_time / 10000.0f, (int)info->bridge_time / 10000.0f);
196 if (info->generation == GENERATION_OLD)
197 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MAJOR%s: (%s) pause %.2fms, %s major %dK/%dK los %dK/%dK",
198 info->is_overflow ? "_OVERFLOW" : "",
199 info->reason ? info->reason : "",
200 (int)info->total_time / 10000.0f,
202 major_collector.section_size * num_major_sections / 1024,
203 major_collector.section_size * last_major_num_sections / 1024,
204 los_memory_usage / 1024,
205 last_los_memory_usage / 1024);
207 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MINOR%s: (%s) pause %.2fms, %s promoted %dK major %dK los %dK",
208 info->is_overflow ? "_OVERFLOW" : "",
209 info->reason ? info->reason : "",
210 (int)info->total_time / 10000.0f,
212 (num_major_sections - last_major_num_sections) * major_collector.section_size / 1024,
213 major_collector.section_size * num_major_sections / 1024,
214 los_memory_usage / 1024);
218 sgen_memgov_collection_end (int generation, GGTimingInfo* info, int info_count)
221 for (i = 0; i < info_count; ++i) {
222 if (info[i].generation != -1)
223 log_timming (&info [i]);
228 sgen_register_major_sections_alloced (size_t num_sections)
230 minor_collection_sections_alloced += num_sections;
234 sgen_get_minor_collection_allowance (void)
236 return minor_collection_allowance;
239 /* Memory pressure API */
241 /* Negative value to remove */
243 mono_gc_add_memory_pressure (gint64 value)
245 /* FIXME: Use interlocked functions */
247 memory_pressure += value;
253 Global GC memory tracking.
254 This tracks the total usage of memory by the GC. This includes
255 managed and unmanaged memory.
259 prot_flags_for_activate (int activate)
261 unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
262 return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
266 sgen_assert_memory_alloc (void *ptr, size_t requested_size, const char *assert_description)
268 if (ptr || !assert_description)
270 fprintf (stderr, "Error: Garbage collector could not allocate %zu bytes of memory for %s.\n", requested_size, assert_description);
275 * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
276 * This must not require any lock.
279 sgen_alloc_os_memory (size_t size, SgenAllocFlags flags, const char *assert_description)
283 g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
285 ptr = mono_valloc (0, size, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
286 sgen_assert_memory_alloc (ptr, size, assert_description);
288 SGEN_ATOMIC_ADD_P (total_alloc, size);
289 if (flags & SGEN_ALLOC_HEAP)
290 MONO_GC_HEAP_ALLOC ((mword)ptr, size);
291 total_alloc_max = MAX (total_alloc_max, total_alloc);
296 /* size must be a power of 2 */
298 sgen_alloc_os_memory_aligned (size_t size, mword alignment, SgenAllocFlags flags, const char *assert_description)
302 g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
304 ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
305 sgen_assert_memory_alloc (ptr, size, assert_description);
307 SGEN_ATOMIC_ADD_P (total_alloc, size);
308 if (flags & SGEN_ALLOC_HEAP)
309 MONO_GC_HEAP_ALLOC ((mword)ptr, size);
310 total_alloc_max = MAX (total_alloc_max, total_alloc);
316 * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
319 sgen_free_os_memory (void *addr, size_t size, SgenAllocFlags flags)
321 g_assert (!(flags & ~SGEN_ALLOC_HEAP));
323 mono_vfree (addr, size);
324 SGEN_ATOMIC_ADD_P (total_alloc, -(gssize)size);
325 if (flags & SGEN_ALLOC_HEAP)
326 MONO_GC_HEAP_FREE ((mword)addr, size);
327 total_alloc_max = MAX (total_alloc_max, total_alloc);
331 mono_gc_get_heap_size (void)
339 This limit the max size of the heap. It takes into account
340 only memory actively in use to hold heap objects and not
341 for other parts of the GC.
344 sgen_memgov_available_free_space (void)
346 return max_heap_size - MIN (allocated_heap, max_heap_size);
350 sgen_memgov_release_space (mword size, int space)
352 SGEN_ATOMIC_ADD_P (allocated_heap, -(gssize)size);
356 sgen_memgov_try_alloc_space (mword size, int space)
358 if (sgen_memgov_available_free_space () < size) {
359 SGEN_ASSERT (4, !sgen_is_worker_thread (mono_native_thread_id_get ()), "Memory shouldn't run out in worker thread");
363 SGEN_ATOMIC_ADD_P (allocated_heap, size);
364 mono_runtime_resource_check_limit (MONO_RESOURCE_GC_HEAP, allocated_heap);
369 sgen_memgov_init (size_t max_heap, size_t soft_limit, gboolean debug_allowance, double allowance_ratio, double save_target)
372 soft_heap_limit = soft_limit;
374 debug_print_allowance = debug_allowance;
375 minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
377 mono_counters_register ("Memgov alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_VARIABLE, &total_alloc);
378 mono_counters_register ("Memgov max alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_MONOTONIC, &total_alloc_max);
383 if (max_heap < soft_limit) {
384 sgen_env_var_error (MONO_GC_PARAMS_NAME, "Setting to minimum.", "`max-heap-size` must be at least as large as `soft-heap-limit`.");
385 max_heap = soft_limit;
388 if (max_heap < sgen_nursery_size * 4) {
389 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`.");
390 max_heap = sgen_nursery_size * 4;
392 max_heap_size = max_heap - sgen_nursery_size;
395 default_allowance_nursery_size_ratio = allowance_ratio;
398 save_target_ratio = save_target;
399 minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;