611ae5c118f4d66b7e1eb982fd4d32048e78a20b
[mono.git] / mono / sgen / sgen-memory-governor.c
1 /*
2  * sgen-memory-governor.c: When to schedule collections based on
3  * memory usage.
4  *
5  * Author:
6  *      Rodrigo Kumpera (rkumpera@novell.com)
7  *
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
12  *
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;
16  *
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.
21  *
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.
25  */
26
27 #include "config.h"
28 #ifdef HAVE_SGEN_GC
29
30 #include <stdlib.h>
31
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"
36
37 #define MIN_MINOR_COLLECTION_ALLOWANCE  ((mword)(DEFAULT_NURSERY_SIZE * default_allowance_nursery_size_ratio))
38
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);
42
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;
45
46 /**/
47 static mword allocated_heap;
48 static mword total_alloc = 0;
49 static mword total_alloc_max = 0;
50
51 /* GC triggers. */
52
53 static gboolean debug_print_allowance = FALSE;
54
55
56 /* use this to tune when to do a major/minor collection */
57 static mword major_collection_trigger_size;
58
59 static mword major_pre_sweep_heap_size;
60 static mword major_start_heap_size;
61
62 static mword last_major_num_sections = 0;
63 static mword last_los_memory_usage = 0;
64
65 static gboolean need_calculate_minor_collection_allowance;
66
67 /* The size of the LOS after the last major collection, after sweeping. */
68 static mword last_collection_los_memory_usage = 0;
69
70 static mword sgen_memgov_available_free_space (void);
71
72
73 /* GC trigger heuristics. */
74
75 static void
76 sgen_memgov_calculate_minor_collection_allowance (void)
77 {
78         size_t new_major, new_heap_size, allowance_target, allowance;
79         size_t decrease;
80
81         if (!need_calculate_minor_collection_allowance)
82                 return;
83
84         SGEN_ASSERT (0, major_collector.have_swept (), "Can only calculate allowance if heap is swept");
85
86         new_major = major_collector.get_bytes_survived_last_sweep ();
87         new_heap_size = new_major + last_collection_los_memory_usage;
88
89         /*
90          * We allow the heap to grow by one third its current size before we start the next
91          * major collection.
92          */
93         allowance_target = new_heap_size * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO;
94
95         allowance = MAX (allowance_target, MIN_MINOR_COLLECTION_ALLOWANCE);
96
97         /*
98          * For the concurrent collector, we decrease the allowance relative to the memory
99          * growth during the M&S phase, survival rate of the collection and the allowance
100          * ratio.
101          */
102         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);
103         if (decrease > allowance)
104                 decrease = allowance;
105         allowance -= decrease;
106
107         if (new_heap_size + allowance > soft_heap_limit) {
108                 if (new_heap_size > soft_heap_limit)
109                         allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
110                 else
111                         allowance = MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
112         }
113
114         /* FIXME: Why is this here? */
115         if (major_collector.free_swept_blocks)
116                 major_collector.free_swept_blocks (allowance);
117
118         major_collection_trigger_size = new_heap_size + allowance;
119
120         need_calculate_minor_collection_allowance = FALSE;
121
122         if (debug_print_allowance) {
123                 SGEN_LOG (0, "Surviving sweep: %ld bytes (%ld major, %ld LOS)", (long)new_heap_size, (long)new_major, (long)last_collection_los_memory_usage);
124                 SGEN_LOG (0, "Allowance: %ld bytes", (long)allowance);
125                 SGEN_LOG (0, "Trigger size: %ld bytes", (long)major_collection_trigger_size);
126         }
127 }
128
129 static inline size_t
130 get_heap_size (void)
131 {
132         return major_collector.get_num_major_sections () * major_collector.section_size + los_memory_usage;
133 }
134
135 gboolean
136 sgen_need_major_collection (mword space_needed)
137 {
138         size_t heap_size;
139
140         if (sgen_concurrent_collection_in_progress ()) {
141                 heap_size = get_heap_size ();
142
143                 if (heap_size <= major_collection_trigger_size)
144                         return FALSE; 
145
146                 /*
147                  * The more the heap grows, the more we need to decrease the allowance above,
148                  * in order to have similar trigger sizes as the synchronous collector.
149                  * If the heap grows so much that we would need to have a negative allowance,
150                  * we force the finishing of the collection, to avoid increased memory usage.
151                  */
152                 if ((heap_size - major_start_heap_size) > major_start_heap_size * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO)
153                         return TRUE;
154                 return FALSE;
155         }
156
157         /* FIXME: This is a cop-out.  We should have some way of figuring this out. */
158         if (!major_collector.have_swept ())
159                 return FALSE;
160
161         if (space_needed > sgen_memgov_available_free_space ())
162                 return TRUE;
163
164         sgen_memgov_calculate_minor_collection_allowance ();
165
166         heap_size = get_heap_size ();
167
168         return heap_size > major_collection_trigger_size;
169 }
170
171 void
172 sgen_memgov_minor_collection_start (void)
173 {
174 }
175
176 void
177 sgen_memgov_minor_collection_end (void)
178 {
179 }
180
181 void
182 sgen_memgov_major_pre_sweep (void)
183 {
184         if (sgen_concurrent_collection_in_progress ()) {
185                 major_pre_sweep_heap_size = get_heap_size ();
186         } else {
187                 /* We decrease the allowance only in the concurrent case */
188                 major_pre_sweep_heap_size = major_start_heap_size;
189         }
190 }
191
192 void
193 sgen_memgov_major_collection_start (void)
194 {
195         need_calculate_minor_collection_allowance = TRUE;
196         major_start_heap_size = get_heap_size ();
197
198         if (debug_print_allowance) {
199                 SGEN_LOG (0, "Starting collection with heap size %ld bytes", (long)major_start_heap_size);
200         }
201 }
202
203 void
204 sgen_memgov_major_collection_end (gboolean forced)
205 {
206         last_collection_los_memory_usage = los_memory_usage;
207
208         if (forced) {
209                 sgen_get_major_collector ()->finish_sweeping ();
210                 sgen_memgov_calculate_minor_collection_allowance ();
211         }
212 }
213
214 void
215 sgen_memgov_collection_start (int generation)
216 {
217 }
218
219 void
220 sgen_memgov_collection_end (int generation, GGTimingInfo* info, int info_count)
221 {
222         int i;
223         for (i = 0; i < info_count; ++i) {
224                 if (info[i].generation != -1)
225                         sgen_client_log_timing (&info [i], last_major_num_sections, last_los_memory_usage);
226         }
227         last_major_num_sections = major_collector.get_num_major_sections ();
228 }
229
230 /*
231 Global GC memory tracking.
232 This tracks the total usage of memory by the GC. This includes
233 managed and unmanaged memory.
234 */
235
236 static unsigned long
237 prot_flags_for_activate (int activate)
238 {
239         unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
240         return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
241 }
242
243 void
244 sgen_assert_memory_alloc (void *ptr, size_t requested_size, const char *assert_description)
245 {
246         if (ptr || !assert_description)
247                 return;
248         fprintf (stderr, "Error: Garbage collector could not allocate %zu bytes of memory for %s.\n", requested_size, assert_description);
249         exit (1);
250 }
251
252 /*
253  * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
254  * This must not require any lock.
255  */
256 void*
257 sgen_alloc_os_memory (size_t size, SgenAllocFlags flags, const char *assert_description)
258 {
259         void *ptr;
260
261         g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
262
263         ptr = mono_valloc (0, size, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
264         sgen_assert_memory_alloc (ptr, size, assert_description);
265         if (ptr) {
266                 SGEN_ATOMIC_ADD_P (total_alloc, size);
267                 total_alloc_max = MAX (total_alloc_max, total_alloc);
268         }
269         return ptr;
270 }
271
272 /* size must be a power of 2 */
273 void*
274 sgen_alloc_os_memory_aligned (size_t size, mword alignment, SgenAllocFlags flags, const char *assert_description)
275 {
276         void *ptr;
277
278         g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
279
280         ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
281         sgen_assert_memory_alloc (ptr, size, assert_description);
282         if (ptr) {
283                 SGEN_ATOMIC_ADD_P (total_alloc, size);
284                 total_alloc_max = MAX (total_alloc_max, total_alloc);
285         }
286         return ptr;
287 }
288
289 /*
290  * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
291  */
292 void
293 sgen_free_os_memory (void *addr, size_t size, SgenAllocFlags flags)
294 {
295         g_assert (!(flags & ~SGEN_ALLOC_HEAP));
296
297         mono_vfree (addr, size);
298         SGEN_ATOMIC_ADD_P (total_alloc, -(gssize)size);
299         total_alloc_max = MAX (total_alloc_max, total_alloc);
300 }
301
302 size_t
303 sgen_gc_get_total_heap_allocation (void)
304 {
305         return total_alloc;
306 }
307
308
309 /*
310 Heap Sizing limits.
311 This limit the max size of the heap. It takes into account
312 only memory actively in use to hold heap objects and not
313 for other parts of the GC.
314  */
315 static mword
316 sgen_memgov_available_free_space (void)
317 {
318         return max_heap_size - MIN (allocated_heap, max_heap_size);
319 }
320
321 void
322 sgen_memgov_release_space (mword size, int space)
323 {
324         SGEN_ATOMIC_ADD_P (allocated_heap, -(gssize)size);
325 }
326
327 gboolean
328 sgen_memgov_try_alloc_space (mword size, int space)
329 {
330         if (sgen_memgov_available_free_space () < size) {
331                 SGEN_ASSERT (4, !sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()), "Memory shouldn't run out in worker thread");
332                 return FALSE;
333         }
334
335         SGEN_ATOMIC_ADD_P (allocated_heap, size);
336         sgen_client_total_allocated_heap_changed (allocated_heap);
337         return TRUE;
338 }
339
340 void
341 sgen_memgov_init (size_t max_heap, size_t soft_limit, gboolean debug_allowance, double allowance_ratio, double save_target)
342 {
343         if (soft_limit)
344                 soft_heap_limit = soft_limit;
345
346         debug_print_allowance = debug_allowance;
347         major_collection_trigger_size = MIN_MINOR_COLLECTION_ALLOWANCE;
348
349         mono_counters_register ("Memgov alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_VARIABLE, &total_alloc);
350         mono_counters_register ("Memgov max alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_MONOTONIC, &total_alloc_max);
351
352         if (max_heap == 0)
353                 return;
354
355         if (max_heap < soft_limit) {
356                 sgen_env_var_error (MONO_GC_PARAMS_NAME, "Setting to minimum.", "`max-heap-size` must be at least as large as `soft-heap-limit`.");
357                 max_heap = soft_limit;
358         }
359
360         if (max_heap < sgen_nursery_size * 4) {
361                 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`.");
362                 max_heap = sgen_nursery_size * 4;
363         }
364         max_heap_size = max_heap - sgen_nursery_size;
365
366         if (allowance_ratio)
367                 default_allowance_nursery_size_ratio = allowance_ratio;
368
369         if (save_target)
370                 save_target_ratio = save_target;
371 }
372
373 #endif