Merge pull request #394 from directhex/master
[mono.git] / mono / metadata / sgen-memory-governor.c
1 /*
2  * sgen-cardtable.c: Card table implementation for sgen
3  *
4  * Author:
5  *      Rodrigo Kumpera (rkumpera@novell.com)
6  *
7  * SGen is licensed under the terms of the MIT X11 license
8  *
9  * Copyright 2001-2003 Ximian, Inc
10  * Copyright 2003-2010 Novell, Inc.
11  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
12  * 
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  * 
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  * 
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  */
32
33 #include "config.h"
34 #ifdef HAVE_SGEN_GC
35
36 #include "metadata/sgen-gc.h"
37 #include "metadata/sgen-memory-governor.h"
38 #include "metadata/mono-gc.h"
39
40 #include "utils/mono-counters.h"
41 #include "utils/mono-mmap.h"
42 #include "utils/mono-logger-internal.h"
43
44 #define MIN_MINOR_COLLECTION_ALLOWANCE  ((mword)(DEFAULT_NURSERY_SIZE * default_allowance_nursery_size_ratio))
45
46 /*Heap limits and allocation knobs*/
47 static mword max_heap_size = ((mword)0)- ((mword)1);
48 static mword soft_heap_limit = ((mword)0) - ((mword)1);
49
50 static double default_allowance_nursery_size_ratio = SGEN_DEFAULT_ALLOWANCE_NURSERY_SIZE_RATIO;
51 static double save_target_ratio = SGEN_DEFAULT_SAVE_TARGET_RATIO;
52
53 /**/
54 static mword allocated_heap;
55 static mword total_alloc = 0;
56
57 /* GC triggers. */
58
59 static gboolean debug_print_allowance = FALSE;
60
61
62 /* use this to tune when to do a major/minor collection */
63 static mword memory_pressure = 0;
64 static mword minor_collection_allowance;
65 static int minor_collection_sections_alloced = 0;
66
67 static int last_major_num_sections = 0;
68 static int last_los_memory_usage = 0;
69
70 static gboolean need_calculate_minor_collection_allowance;
71
72 static int last_collection_old_num_major_sections;
73 static mword last_collection_los_memory_usage = 0;
74 static mword last_collection_old_los_memory_usage;
75 static mword last_collection_los_memory_alloced;
76
77 static mword sgen_memgov_available_free_space (void);
78
79
80 /* GC trigger heuristics. */
81
82 static void
83 sgen_memgov_try_calculate_minor_collection_allowance (gboolean overwrite)
84 {
85         int num_major_sections, num_major_sections_saved;
86         mword los_memory_saved, new_major, new_heap_size, save_target, allowance_target;
87
88         if (overwrite)
89                 g_assert (need_calculate_minor_collection_allowance);
90
91         if (!need_calculate_minor_collection_allowance)
92                 return;
93
94         if (!*major_collector.have_swept) {
95                 if (overwrite)
96                         minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
97                 return;
98         }
99
100         num_major_sections = major_collector.get_num_major_sections ();
101
102         num_major_sections_saved = MAX (last_collection_old_num_major_sections - num_major_sections, 0);
103         los_memory_saved = MAX (last_collection_old_los_memory_usage - last_collection_los_memory_usage, 1);
104
105         new_major = num_major_sections * major_collector.section_size;
106         new_heap_size = new_major + last_collection_los_memory_usage;
107
108         save_target = (mword)((new_major + last_collection_los_memory_usage) * SGEN_DEFAULT_SAVE_TARGET_RATIO);
109
110         /*
111          * We aim to allow the allocation of as many sections as is
112          * necessary to reclaim save_target sections in the next
113          * collection.  We assume the collection pattern won't change.
114          * In the last cycle, we had num_major_sections_saved for
115          * minor_collection_sections_alloced.  Assuming things won't
116          * change, this must be the same ratio as save_target for
117          * allowance_target, i.e.
118          *
119          *    num_major_sections_saved            save_target
120          * --------------------------------- == ----------------
121          * minor_collection_sections_alloced    allowance_target
122          *
123          * hence:
124          */
125         allowance_target = (mword)((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));
126
127         minor_collection_allowance = MAX (MIN (allowance_target, num_major_sections * major_collector.section_size + los_memory_usage), MIN_MINOR_COLLECTION_ALLOWANCE);
128
129         if (new_heap_size + minor_collection_allowance > soft_heap_limit) {
130                 if (new_heap_size > soft_heap_limit)
131                         minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
132                 else
133                         minor_collection_allowance = MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
134         }
135
136         if (debug_print_allowance) {
137                 mword old_major = last_collection_old_num_major_sections * major_collector.section_size;
138
139                 fprintf (gc_debug_file, "Before collection: %td bytes (%td major, %td LOS)\n",
140                                 old_major + last_collection_old_los_memory_usage, old_major, last_collection_old_los_memory_usage);
141                 fprintf (gc_debug_file, "After collection: %td bytes (%td major, %td LOS)\n",
142                                 new_heap_size, new_major, last_collection_los_memory_usage);
143                 fprintf (gc_debug_file, "Allowance: %td bytes\n", minor_collection_allowance);
144         }
145
146         if (major_collector.have_computed_minor_collection_allowance)
147                 major_collector.have_computed_minor_collection_allowance ();
148
149         need_calculate_minor_collection_allowance = FALSE;
150 }
151
152
153 gboolean
154 sgen_need_major_collection (mword space_needed)
155 {
156         mword los_alloced = los_memory_usage - MIN (last_collection_los_memory_usage, los_memory_usage);
157         return (space_needed > sgen_memgov_available_free_space ()) ||
158                 minor_collection_sections_alloced * major_collector.section_size + los_alloced > minor_collection_allowance;
159 }
160
161 void
162 sgen_memgov_minor_collection_start (void)
163 {
164         sgen_memgov_try_calculate_minor_collection_allowance (FALSE);
165 }
166
167 void
168 sgen_memgov_minor_collection_end (void)
169 {
170 }
171
172 void
173 sgen_memgov_major_collection_start (void)
174 {
175         last_collection_old_num_major_sections = sgen_get_major_collector ()->get_num_major_sections ();
176
177         /*
178          * A domain could have been freed, resulting in
179          * los_memory_usage being less than last_collection_los_memory_usage.
180          */
181         last_collection_los_memory_alloced = los_memory_usage - MIN (last_collection_los_memory_usage, los_memory_usage);
182         last_collection_old_los_memory_usage = los_memory_usage;
183
184         need_calculate_minor_collection_allowance = TRUE;
185 }
186
187 void
188 sgen_memgov_major_collection_end (void)
189 {
190         sgen_memgov_try_calculate_minor_collection_allowance (TRUE);
191
192         minor_collection_sections_alloced = 0;
193         last_collection_los_memory_usage = los_memory_usage;
194 }
195
196 void
197 sgen_memgov_collection_start (int generation)
198 {
199         last_major_num_sections = major_collector.get_num_major_sections ();
200         last_los_memory_usage = los_memory_usage;
201 }
202
203 static void
204 log_timming (GGTimingInfo *info)
205 {
206         //unsigned long stw_time, unsigned long bridge_time, gboolean is_overflow
207         int num_major_sections = major_collector.get_num_major_sections ();
208         char full_timing_buff [1024];
209         full_timing_buff [0] = '\0';
210
211         if (!info->is_overflow)
212                 sprintf (full_timing_buff, "total %.2fms, bridge %.2f", info->stw_time / 1000.0f, (int)info->bridge_time / 1000.0f);
213         if (info->generation == GENERATION_OLD)
214                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MAJOR%s: (%s) pause %.2fms, %s major %dK/%dK los %dK/%dK",
215                         info->is_overflow ? "_OVERFLOW" : "",
216                         info->reason,
217                         (int)info->total_time / 1000.0f,
218                         full_timing_buff,
219                         major_collector.section_size * num_major_sections / 1024,
220                         major_collector.section_size * last_major_num_sections / 1024,
221                         los_memory_usage / 1024,
222                         last_los_memory_usage / 1024);
223         else
224                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MINOR%s: (%s) pause %.2fms, %s promoted %dK major %dK los %dK",
225                                 info->is_overflow ? "_OVERFLOW" : "",
226                         info->reason,
227                         (int)info->total_time / 1000.0f,
228                         full_timing_buff,
229                         (num_major_sections - last_major_num_sections) * major_collector.section_size / 1024,
230                         major_collector.section_size * num_major_sections / 1024,
231                         los_memory_usage / 1024);       
232 }
233
234 void
235 sgen_memgov_collection_end (int generation, GGTimingInfo* info, int info_count)
236 {
237         int i;
238         for (i = 0; i < info_count; ++i) {
239                 if (info->generation != -1)
240                         log_timming (&info [i]);
241         }
242 }
243
244 void
245 sgen_register_major_sections_alloced (int num_sections)
246 {
247         minor_collection_sections_alloced += num_sections;
248 }
249
250 mword
251 sgen_get_minor_collection_allowance (void)
252 {
253         return minor_collection_allowance;
254 }
255
256 /* Memory pressure API */
257
258 /* Negative value to remove */
259 void
260 mono_gc_add_memory_pressure (gint64 value)
261 {
262         /* FIXME: Use interlocked functions */
263         LOCK_GC;
264         memory_pressure += value;
265         UNLOCK_GC;
266 }
267
268
269 /*
270 Global GC memory tracking.
271 This tracks the total usage of memory by the GC. This includes
272 managed and unmanaged memory.
273 */
274
275 static unsigned long
276 prot_flags_for_activate (int activate)
277 {
278         unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
279         return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
280 }
281
282 /*
283  * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
284  * This must not require any lock.
285  */
286 void*
287 sgen_alloc_os_memory (size_t size, int activate)
288 {
289         void *ptr = mono_valloc (0, size, prot_flags_for_activate (activate));
290         if (ptr)
291                 SGEN_ATOMIC_ADD_P (total_alloc, size);
292         return ptr;
293 }
294
295 /* size must be a power of 2 */
296 void*
297 sgen_alloc_os_memory_aligned (size_t size, mword alignment, gboolean activate)
298 {
299         void *ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (activate));
300         if (ptr)
301                 SGEN_ATOMIC_ADD_P (total_alloc, size);
302         return ptr;
303 }
304
305 /*
306  * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
307  */
308 void
309 sgen_free_os_memory (void *addr, size_t size)
310 {
311         mono_vfree (addr, size);
312         SGEN_ATOMIC_ADD_P (total_alloc, -size);
313 }
314
315 int64_t
316 mono_gc_get_heap_size (void)
317 {
318         return total_alloc;
319 }
320
321
322 /*
323 Heap Sizing limits.
324 This limit the max size of the heap. It takes into account
325 only memory actively in use to hold heap objects and not
326 for other parts of the GC.
327  */
328 static mword
329 sgen_memgov_available_free_space (void)
330 {
331         return max_heap_size - MIN (allocated_heap, max_heap_size);
332 }
333
334 void
335 sgen_memgov_release_space (mword size, int space)
336 {
337         SGEN_ATOMIC_ADD_P (allocated_heap, -size);
338 }
339
340 gboolean
341 sgen_memgov_try_alloc_space (mword size, int space)
342 {
343         if (sgen_memgov_available_free_space () < size)
344                 return FALSE;
345
346         SGEN_ATOMIC_ADD_P (allocated_heap, size);
347         mono_runtime_resource_check_limit (MONO_RESOURCE_GC_HEAP, allocated_heap);
348         return TRUE;
349 }
350
351 void
352 sgen_memgov_init (glong max_heap, glong soft_limit, gboolean debug_allowance, double allowance_ratio, double save_target)
353 {
354         if (soft_limit)
355                 soft_heap_limit = soft_limit;
356
357         debug_print_allowance = debug_allowance;
358
359         if (max_heap == 0)
360                 return;
361
362         if (max_heap < soft_limit) {
363                 fprintf (stderr, "max-heap-size must be at least as large as soft-heap-limit.\n");
364                 exit (1);
365         }
366
367         if (max_heap < sgen_nursery_size * 4) {
368                 fprintf (stderr, "max-heap-size must be at least 4 times larger than nursery size.\n");
369                 exit (1);
370         }
371         max_heap_size = max_heap - sgen_nursery_size;
372
373         minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
374
375         if (allowance_ratio)
376                 default_allowance_nursery_size_ratio = allowance_ratio;
377
378         if (save_target)
379                 save_target_ratio = save_target;
380 }
381
382 #endif