[sgen] DTrace probes for internal allocations.
[mono.git] / mono / metadata / sgen-internal.c
1 /*
2  * sgen-internal.c: Internal lock-free memory allocator.
3  *
4  * Copyright (C) 2012 Xamarin Inc
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License 2.0 as published by the Free Software Foundation;
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License 2.0 along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "config.h"
21
22 #ifdef HAVE_SGEN_GC
23
24 #include "utils/mono-counters.h"
25 #include "metadata/sgen-gc.h"
26 #include "utils/lock-free-alloc.h"
27 #include "metadata/sgen-memory-governor.h"
28
29 /* keep each size a multiple of ALLOC_ALIGN */
30 static const int allocator_sizes [] = {
31            8,   16,   24,   32,   40,   48,   64,   80,
32           96,  128,  160,  192,  224,  248,  320,  384,
33          448,  528,  584,  680,  816, 1088, 1360, 2040,
34         2336, 2728, 3272, 4088, 5456, 8184 };
35
36 #define NUM_ALLOCATORS  (sizeof (allocator_sizes) / sizeof (int))
37
38 static MonoLockFreeAllocSizeClass size_classes [NUM_ALLOCATORS];
39 static MonoLockFreeAllocator allocators [NUM_ALLOCATORS];
40
41 /*
42  * Find the allocator index for memory chunks that can contain @size
43  * objects.
44  */
45 static int
46 index_for_size (size_t size)
47 {
48         int slot;
49         /* do a binary search or lookup table later. */
50         for (slot = 0; slot < NUM_ALLOCATORS; ++slot) {
51                 if (allocator_sizes [slot] >= size)
52                         return slot;
53         }
54         g_assert_not_reached ();
55         return -1;
56 }
57
58 /*
59  * Allocator indexes for the fixed INTERNAL_MEM_XXX types.  -1 if that
60  * type is dynamic.
61  */
62 static int fixed_type_allocator_indexes [INTERNAL_MEM_MAX];
63
64 void
65 sgen_register_fixed_internal_mem_type (int type, size_t size)
66 {
67         int slot;
68
69         g_assert (type >= 0 && type < INTERNAL_MEM_MAX);
70
71         slot = index_for_size (size);
72         g_assert (slot >= 0);
73
74         if (fixed_type_allocator_indexes [type] == -1)
75                 fixed_type_allocator_indexes [type] = slot;
76         else
77                 g_assert (fixed_type_allocator_indexes [type] == slot);
78 }
79
80 static const char*
81 description_for_type (int type)
82 {
83         switch (type) {
84         case INTERNAL_MEM_PIN_QUEUE: return "pin-queue";
85         case INTERNAL_MEM_FRAGMENT: return "fragment";
86         case INTERNAL_MEM_SECTION: return "section";
87         case INTERNAL_MEM_SCAN_STARTS: return "scan-starts";
88         case INTERNAL_MEM_FIN_TABLE: return "fin-table";
89         case INTERNAL_MEM_FINALIZE_ENTRY: return "finalize-entry";
90         case INTERNAL_MEM_FINALIZE_READY_ENTRY: return "finalize-ready-entry";
91         case INTERNAL_MEM_DISLINK_TABLE: return "dislink-table";
92         case INTERNAL_MEM_DISLINK: return "dislink";
93         case INTERNAL_MEM_ROOTS_TABLE: return "roots-table";
94         case INTERNAL_MEM_ROOT_RECORD: return "root-record";
95         case INTERNAL_MEM_STATISTICS: return "statistics";
96         case INTERNAL_MEM_STAT_PINNED_CLASS: return "pinned-class";
97         case INTERNAL_MEM_STAT_REMSET_CLASS: return "remset-class";
98         case INTERNAL_MEM_REMSET: return "remset";
99         case INTERNAL_MEM_GRAY_QUEUE: return "gray-queue";
100         case INTERNAL_MEM_STORE_REMSET: return "store-remset";
101         case INTERNAL_MEM_MS_TABLES: return "marksweep-tables";
102         case INTERNAL_MEM_MS_BLOCK_INFO: return "marksweep-block-info";
103         case INTERNAL_MEM_EPHEMERON_LINK: return "ephemeron-link";
104         case INTERNAL_MEM_WORKER_DATA: return "worker-data";
105         case INTERNAL_MEM_WORKER_JOB_DATA: return "worker-job-data";
106         case INTERNAL_MEM_BRIDGE_DATA: return "bridge-data";
107         case INTERNAL_MEM_BRIDGE_HASH_TABLE: return "bridge-hash-table";
108         case INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY: return "bridge-hash-table-entry";
109         case INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE: return "bridge-alive-hash-table";
110         case INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY: return "bridge-alive-hash-table-entry";
111         case INTERNAL_MEM_JOB_QUEUE_ENTRY: return "job-queue-entry";
112         case INTERNAL_MEM_TOGGLEREF_DATA: return "toggleref-data";
113         case INTERNAL_MEM_CARDTABLE_MOD_UNION: return "cardtable-mod-union";
114         default:
115                 g_assert_not_reached ();
116         }
117 }
118
119 void*
120 sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure)
121 {
122         int index;
123         void *p;
124
125         if (size > allocator_sizes [NUM_ALLOCATORS - 1]) {
126                 p = sgen_alloc_os_memory (size, SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, NULL);
127                 if (!p)
128                         sgen_assert_memory_alloc (NULL, size, description_for_type (type));
129         } else {
130                 index = index_for_size (size);
131
132                 p = mono_lock_free_alloc (&allocators [index]);
133                 if (!p)
134                         sgen_assert_memory_alloc (NULL, size, description_for_type (type));
135                 memset (p, 0, size);
136         }
137
138         MONO_GC_INTERNAL_ALLOC (p, size, type);
139         return p;
140 }
141
142 void
143 sgen_free_internal_dynamic (void *addr, size_t size, int type)
144 {
145         if (!addr)
146                 return;
147
148         if (size > allocator_sizes [NUM_ALLOCATORS - 1])
149                 sgen_free_os_memory (addr, size, SGEN_ALLOC_INTERNAL);
150         else
151                 mono_lock_free_free (addr);
152
153         MONO_GC_INTERNAL_DEALLOC (addr, size, type);
154 }
155
156 void*
157 sgen_alloc_internal (int type)
158 {
159         int index = fixed_type_allocator_indexes [type];
160         int size = allocator_sizes [index];
161         void *p;
162         g_assert (index >= 0 && index < NUM_ALLOCATORS);
163         p = mono_lock_free_alloc (&allocators [index]);
164         memset (p, 0, size);
165
166         MONO_GC_INTERNAL_ALLOC (p, size, type);
167
168         return p;
169 }
170
171 void
172 sgen_free_internal (void *addr, int type)
173 {
174         int index;
175
176         if (!addr)
177                 return;
178
179         index = fixed_type_allocator_indexes [type];
180         g_assert (index >= 0 && index < NUM_ALLOCATORS);
181
182         mono_lock_free_free (addr);
183
184         if (MONO_GC_INTERNAL_DEALLOC_ENABLED ()) {
185                 int size = allocator_sizes [index];
186                 MONO_GC_INTERNAL_DEALLOC (addr, size, type);
187         }
188 }
189
190 void
191 sgen_dump_internal_mem_usage (FILE *heap_dump_file)
192 {
193         /*
194         int i;
195
196         fprintf (heap_dump_file, "<other-mem-usage type=\"large-internal\" size=\"%lld\"/>\n", large_internal_bytes_alloced);
197         fprintf (heap_dump_file, "<other-mem-usage type=\"pinned-chunks\" size=\"%lld\"/>\n", pinned_chunk_bytes_alloced);
198         for (i = 0; i < INTERNAL_MEM_MAX; ++i) {
199                 fprintf (heap_dump_file, "<other-mem-usage type=\"%s\" size=\"%ld\"/>\n",
200                                 description_for_type (i), unmanaged_allocator.small_internal_mem_bytes [i]);
201         }
202         */
203 }
204
205 void
206 sgen_report_internal_mem_usage (void)
207 {
208         /* FIXME: implement */
209         printf ("not implemented yet\n");
210 }
211
212 void
213 sgen_init_internal_allocator (void)
214 {
215         int i;
216
217         for (i = 0; i < INTERNAL_MEM_MAX; ++i)
218                 fixed_type_allocator_indexes [i] = -1;
219
220         for (i = 0; i < NUM_ALLOCATORS; ++i) {
221                 mono_lock_free_allocator_init_size_class (&size_classes [i], allocator_sizes [i]);
222                 mono_lock_free_allocator_init_allocator (&allocators [i], &size_classes [i]);
223         }
224 }
225
226 #endif