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;
54 static gboolean debug_print_allowance = FALSE;
57 /* use this to tune when to do a major/minor collection */
58 static mword memory_pressure = 0;
59 static mword minor_collection_allowance;
60 static int minor_collection_sections_alloced = 0;
62 static int last_major_num_sections = 0;
63 static int last_los_memory_usage = 0;
65 static gboolean need_calculate_minor_collection_allowance;
67 static int last_collection_old_num_major_sections;
68 static mword last_collection_los_memory_usage = 0;
69 static mword last_collection_old_los_memory_usage;
70 static mword last_collection_los_memory_alloced;
72 static mword sgen_memgov_available_free_space (void);
76 double_to_mword_with_saturation (double value)
78 if (value >= (double)MWORD_MAX_VALUE)
79 return MWORD_MAX_VALUE;
83 /* GC trigger heuristics. */
86 sgen_memgov_try_calculate_minor_collection_allowance (gboolean overwrite)
88 int num_major_sections, num_major_sections_saved;
89 mword los_memory_saved, new_major, new_heap_size, save_target, allowance_target;
92 g_assert (need_calculate_minor_collection_allowance);
94 if (!need_calculate_minor_collection_allowance)
97 if (!*major_collector.have_swept) {
99 minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
103 num_major_sections = major_collector.get_num_major_sections ();
105 num_major_sections_saved = MAX (last_collection_old_num_major_sections - num_major_sections, 0);
106 los_memory_saved = MAX (last_collection_old_los_memory_usage - last_collection_los_memory_usage, 1);
108 new_major = num_major_sections * major_collector.section_size;
109 new_heap_size = new_major + last_collection_los_memory_usage;
111 save_target = (mword)((new_major + last_collection_los_memory_usage) * SGEN_DEFAULT_SAVE_TARGET_RATIO);
114 * We aim to allow the allocation of as many sections as is
115 * necessary to reclaim save_target sections in the next
116 * collection. We assume the collection pattern won't change.
117 * In the last cycle, we had num_major_sections_saved for
118 * minor_collection_sections_alloced. Assuming things won't
119 * change, this must be the same ratio as save_target for
120 * allowance_target, i.e.
122 * num_major_sections_saved save_target
123 * --------------------------------- == ----------------
124 * minor_collection_sections_alloced allowance_target
128 allowance_target = double_to_mword_with_saturation ((double)save_target * (double)(minor_collection_sections_alloced * major_collector.section_size + last_collection_los_memory_alloced) / (double)(num_major_sections_saved * major_collector.section_size + los_memory_saved));
130 minor_collection_allowance = MAX (MIN (allowance_target, num_major_sections * major_collector.section_size + los_memory_usage), MIN_MINOR_COLLECTION_ALLOWANCE);
132 if (new_heap_size + minor_collection_allowance > soft_heap_limit) {
133 if (new_heap_size > soft_heap_limit)
134 minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
136 minor_collection_allowance = MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
139 if (debug_print_allowance) {
140 mword old_major = last_collection_old_num_major_sections * major_collector.section_size;
142 SGEN_LOG (1, "Before collection: %td bytes (%td major, %td LOS)",
143 old_major + last_collection_old_los_memory_usage, old_major, last_collection_old_los_memory_usage);
144 SGEN_LOG (1, "After collection: %td bytes (%td major, %td LOS)",
145 new_heap_size, new_major, last_collection_los_memory_usage);
146 SGEN_LOG (1, "Allowance: %td bytes", minor_collection_allowance);
149 if (major_collector.have_computed_minor_collection_allowance)
150 major_collector.have_computed_minor_collection_allowance ();
152 need_calculate_minor_collection_allowance = FALSE;
157 sgen_need_major_collection (mword space_needed)
160 los_alloced = los_memory_usage - MIN (last_collection_los_memory_usage, los_memory_usage);
161 return (space_needed > sgen_memgov_available_free_space ()) ||
162 minor_collection_sections_alloced * major_collector.section_size + los_alloced > minor_collection_allowance;
166 sgen_memgov_minor_collection_start (void)
168 sgen_memgov_try_calculate_minor_collection_allowance (FALSE);
172 sgen_memgov_minor_collection_end (void)
177 sgen_memgov_major_collection_start (void)
179 last_collection_old_num_major_sections = sgen_get_major_collector ()->get_num_major_sections ();
182 * A domain could have been freed, resulting in
183 * los_memory_usage being less than last_collection_los_memory_usage.
185 last_collection_los_memory_alloced = los_memory_usage - MIN (last_collection_los_memory_usage, los_memory_usage);
186 last_collection_old_los_memory_usage = los_memory_usage;
188 need_calculate_minor_collection_allowance = TRUE;
192 sgen_memgov_major_collection_end (void)
194 sgen_memgov_try_calculate_minor_collection_allowance (TRUE);
196 minor_collection_sections_alloced = 0;
197 last_collection_los_memory_usage = los_memory_usage;
201 sgen_memgov_collection_start (int generation)
203 last_major_num_sections = major_collector.get_num_major_sections ();
204 last_los_memory_usage = los_memory_usage;
208 log_timming (GGTimingInfo *info)
210 //unsigned long stw_time, unsigned long bridge_time, gboolean is_overflow
211 int num_major_sections = major_collector.get_num_major_sections ();
212 char full_timing_buff [1024];
213 full_timing_buff [0] = '\0';
215 if (!info->is_overflow)
216 sprintf (full_timing_buff, "total %.2fms, bridge %.2f", info->stw_time / 1000.0f, (int)info->bridge_time / 1000.0f);
217 if (info->generation == GENERATION_OLD)
218 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MAJOR%s: (%s) pause %.2fms, %s major %dK/%dK los %dK/%dK",
219 info->is_overflow ? "_OVERFLOW" : "",
220 info->reason ? info->reason : "",
221 (int)info->total_time / 1000.0f,
223 major_collector.section_size * num_major_sections / 1024,
224 major_collector.section_size * last_major_num_sections / 1024,
225 los_memory_usage / 1024,
226 last_los_memory_usage / 1024);
228 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MINOR%s: (%s) pause %.2fms, %s promoted %dK major %dK los %dK",
229 info->is_overflow ? "_OVERFLOW" : "",
230 info->reason ? info->reason : "",
231 (int)info->total_time / 1000.0f,
233 (num_major_sections - last_major_num_sections) * major_collector.section_size / 1024,
234 major_collector.section_size * num_major_sections / 1024,
235 los_memory_usage / 1024);
239 sgen_memgov_collection_end (int generation, GGTimingInfo* info, int info_count)
242 for (i = 0; i < info_count; ++i) {
243 if (info[i].generation != -1)
244 log_timming (&info [i]);
249 sgen_register_major_sections_alloced (int num_sections)
251 minor_collection_sections_alloced += num_sections;
255 sgen_get_minor_collection_allowance (void)
257 return minor_collection_allowance;
260 /* Memory pressure API */
262 /* Negative value to remove */
264 mono_gc_add_memory_pressure (gint64 value)
266 /* FIXME: Use interlocked functions */
268 memory_pressure += value;
274 Global GC memory tracking.
275 This tracks the total usage of memory by the GC. This includes
276 managed and unmanaged memory.
280 prot_flags_for_activate (int activate)
282 unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
283 return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
287 sgen_assert_memory_alloc (void *ptr, size_t requested_size, const char *assert_description)
289 if (ptr || !assert_description)
291 fprintf (stderr, "Error: Garbage collector could not allocate %zu bytes of memory for %s.\n", requested_size, assert_description);
296 * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
297 * This must not require any lock.
300 sgen_alloc_os_memory (size_t size, SgenAllocFlags flags, const char *assert_description)
304 g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
306 ptr = mono_valloc (0, size, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
307 sgen_assert_memory_alloc (ptr, size, assert_description);
309 SGEN_ATOMIC_ADD_P (total_alloc, size);
310 if (flags & SGEN_ALLOC_HEAP)
311 MONO_GC_HEAP_ALLOC ((mword)ptr, size);
316 /* size must be a power of 2 */
318 sgen_alloc_os_memory_aligned (size_t size, mword alignment, SgenAllocFlags flags, const char *assert_description)
322 g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
324 ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
325 sgen_assert_memory_alloc (ptr, size, assert_description);
327 SGEN_ATOMIC_ADD_P (total_alloc, size);
328 if (flags & SGEN_ALLOC_HEAP)
329 MONO_GC_HEAP_ALLOC ((mword)ptr, size);
335 * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
338 sgen_free_os_memory (void *addr, size_t size, SgenAllocFlags flags)
340 g_assert (!(flags & ~SGEN_ALLOC_HEAP));
342 mono_vfree (addr, size);
343 SGEN_ATOMIC_ADD_P (total_alloc, -size);
344 if (flags & SGEN_ALLOC_HEAP)
345 MONO_GC_HEAP_FREE ((mword)addr, size);
349 mono_gc_get_heap_size (void)
357 This limit the max size of the heap. It takes into account
358 only memory actively in use to hold heap objects and not
359 for other parts of the GC.
362 sgen_memgov_available_free_space (void)
364 return max_heap_size - MIN (allocated_heap, max_heap_size);
368 sgen_memgov_release_space (mword size, int space)
370 SGEN_ATOMIC_ADD_P (allocated_heap, -size);
374 sgen_memgov_try_alloc_space (mword size, int space)
376 if (sgen_memgov_available_free_space () < size)
379 SGEN_ATOMIC_ADD_P (allocated_heap, size);
380 mono_runtime_resource_check_limit (MONO_RESOURCE_GC_HEAP, allocated_heap);
385 sgen_memgov_init (glong max_heap, glong soft_limit, gboolean debug_allowance, double allowance_ratio, double save_target)
388 soft_heap_limit = soft_limit;
390 debug_print_allowance = debug_allowance;
395 if (max_heap < soft_limit) {
396 fprintf (stderr, "max-heap-size must be at least as large as soft-heap-limit.\n");
400 if (max_heap < sgen_nursery_size * 4) {
401 fprintf (stderr, "max-heap-size must be at least 4 times larger than nursery size.\n");
404 max_heap_size = max_heap - sgen_nursery_size;
406 minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
409 default_allowance_nursery_size_ratio = allowance_ratio;
412 save_target_ratio = save_target;