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