2 * sgen-internal.c: Internal lock-free memory allocator.
4 * Copyright (C) 2012 Xamarin Inc
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;
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.
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.
24 #include "utils/mono-counters.h"
25 #include "metadata/sgen-gc.h"
26 #include "utils/mono-mmap.h"
27 #include "utils/lock-free-alloc.h"
28 #include "metadata/sgen-memory-governor.h"
30 /* keep each size a multiple of ALLOC_ALIGN */
31 #if SIZEOF_VOID_P == 4
32 static const int allocator_sizes [] = {
33 8, 16, 24, 32, 40, 48, 64, 80,
34 96, 128, 160, 192, 224, 248, 296, 320,
35 384, 448, 504, 528, 584, 680, 816, 1088,
36 1360, 2044, 2336, 2728, 3272, 4092, 5456, 8188 };
38 static const int allocator_sizes [] = {
39 8, 16, 24, 32, 40, 48, 64, 80,
40 96, 128, 160, 192, 224, 248, 320, 328,
41 384, 448, 528, 584, 680, 816, 1016, 1088,
42 1360, 2040, 2336, 2728, 3272, 4088, 5456, 8184 };
45 #define NUM_ALLOCATORS (sizeof (allocator_sizes) / sizeof (int))
47 static int allocator_block_sizes [NUM_ALLOCATORS];
49 static MonoLockFreeAllocSizeClass size_classes [NUM_ALLOCATORS];
50 static MonoLockFreeAllocator allocators [NUM_ALLOCATORS];
52 #ifdef HEAVY_STATISTICS
53 static int allocator_sizes_stats [NUM_ALLOCATORS];
57 block_size (size_t slot_size)
59 static int pagesize = -1;
64 pagesize = mono_pagesize ();
66 for (size = pagesize; size < LOCK_FREE_ALLOC_SB_MAX_SIZE; size <<= 1) {
67 if (slot_size * 2 <= LOCK_FREE_ALLOC_SB_USABLE_SIZE (size))
70 return LOCK_FREE_ALLOC_SB_MAX_SIZE;
74 * Find the allocator index for memory chunks that can contain @size
78 index_for_size (size_t size)
81 /* do a binary search or lookup table later. */
82 for (slot = 0; slot < NUM_ALLOCATORS; ++slot) {
83 if (allocator_sizes [slot] >= size)
86 g_assert_not_reached ();
91 * Allocator indexes for the fixed INTERNAL_MEM_XXX types. -1 if that
94 static int fixed_type_allocator_indexes [INTERNAL_MEM_MAX];
97 sgen_register_fixed_internal_mem_type (int type, size_t size)
101 g_assert (type >= 0 && type < INTERNAL_MEM_MAX);
102 g_assert (size <= allocator_sizes [NUM_ALLOCATORS - 1]);
104 slot = index_for_size (size);
105 g_assert (slot >= 0);
107 if (fixed_type_allocator_indexes [type] == -1)
108 fixed_type_allocator_indexes [type] = slot;
110 g_assert (fixed_type_allocator_indexes [type] == slot);
114 description_for_type (int type)
117 case INTERNAL_MEM_PIN_QUEUE: return "pin-queue";
118 case INTERNAL_MEM_FRAGMENT: return "fragment";
119 case INTERNAL_MEM_SECTION: return "section";
120 case INTERNAL_MEM_SCAN_STARTS: return "scan-starts";
121 case INTERNAL_MEM_FIN_TABLE: return "fin-table";
122 case INTERNAL_MEM_FINALIZE_ENTRY: return "finalize-entry";
123 case INTERNAL_MEM_FINALIZE_READY_ENTRY: return "finalize-ready-entry";
124 case INTERNAL_MEM_DISLINK_TABLE: return "dislink-table";
125 case INTERNAL_MEM_DISLINK: return "dislink";
126 case INTERNAL_MEM_ROOTS_TABLE: return "roots-table";
127 case INTERNAL_MEM_ROOT_RECORD: return "root-record";
128 case INTERNAL_MEM_STATISTICS: return "statistics";
129 case INTERNAL_MEM_STAT_PINNED_CLASS: return "pinned-class";
130 case INTERNAL_MEM_STAT_REMSET_CLASS: return "remset-class";
131 case INTERNAL_MEM_GRAY_QUEUE: return "gray-queue";
132 case INTERNAL_MEM_MS_TABLES: return "marksweep-tables";
133 case INTERNAL_MEM_MS_BLOCK_INFO: return "marksweep-block-info";
134 case INTERNAL_MEM_MS_BLOCK_INFO_SORT: return "marksweep-block-info-sort";
135 case INTERNAL_MEM_EPHEMERON_LINK: return "ephemeron-link";
136 case INTERNAL_MEM_WORKER_DATA: return "worker-data";
137 case INTERNAL_MEM_WORKER_JOB_DATA: return "worker-job-data";
138 case INTERNAL_MEM_BRIDGE_DATA: return "bridge-data";
139 case INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE: return "old-bridge-hash-table";
140 case INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE_ENTRY: return "old-bridge-hash-table-entry";
141 case INTERNAL_MEM_BRIDGE_HASH_TABLE: return "bridge-hash-table";
142 case INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY: return "bridge-hash-table-entry";
143 case INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE: return "tarjan-bridge-hash-table";
144 case INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE_ENTRY: return "tarjan-bridge-hash-table-entry";
145 case INTERNAL_MEM_TARJAN_OBJ_BUCKET: return "tarjan-bridge-object-buckets";
146 case INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE: return "bridge-alive-hash-table";
147 case INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY: return "bridge-alive-hash-table-entry";
148 case INTERNAL_MEM_BRIDGE_DEBUG: return "bridge-debug";
149 case INTERNAL_MEM_JOB_QUEUE_ENTRY: return "job-queue-entry";
150 case INTERNAL_MEM_TOGGLEREF_DATA: return "toggleref-data";
151 case INTERNAL_MEM_CARDTABLE_MOD_UNION: return "cardtable-mod-union";
152 case INTERNAL_MEM_BINARY_PROTOCOL: return "binary-protocol";
153 case INTERNAL_MEM_TEMPORARY: return "temporary";
155 g_assert_not_reached ();
160 sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure)
165 if (size > allocator_sizes [NUM_ALLOCATORS - 1]) {
166 p = sgen_alloc_os_memory (size, SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, NULL);
168 sgen_assert_memory_alloc (NULL, size, description_for_type (type));
170 index = index_for_size (size);
172 #ifdef HEAVY_STATISTICS
173 ++ allocator_sizes_stats [index];
176 p = mono_lock_free_alloc (&allocators [index]);
178 sgen_assert_memory_alloc (NULL, size, description_for_type (type));
182 MONO_GC_INTERNAL_ALLOC ((mword)p, size, type);
187 sgen_free_internal_dynamic (void *addr, size_t size, int type)
192 if (size > allocator_sizes [NUM_ALLOCATORS - 1])
193 sgen_free_os_memory (addr, size, SGEN_ALLOC_INTERNAL);
195 mono_lock_free_free (addr, block_size (size));
197 MONO_GC_INTERNAL_DEALLOC ((mword)addr, size, type);
201 sgen_alloc_internal (int type)
206 index = fixed_type_allocator_indexes [type];
207 g_assert (index >= 0 && index < NUM_ALLOCATORS);
209 #ifdef HEAVY_STATISTICS
210 ++ allocator_sizes_stats [index];
213 size = allocator_sizes [index];
215 p = mono_lock_free_alloc (&allocators [index]);
218 MONO_GC_INTERNAL_ALLOC ((mword)p, size, type);
224 sgen_free_internal (void *addr, int type)
231 index = fixed_type_allocator_indexes [type];
232 g_assert (index >= 0 && index < NUM_ALLOCATORS);
234 mono_lock_free_free (addr, allocator_block_sizes [index]);
236 if (MONO_GC_INTERNAL_DEALLOC_ENABLED ()) {
237 int size G_GNUC_UNUSED = allocator_sizes [index];
238 MONO_GC_INTERNAL_DEALLOC ((mword)addr, size, type);
243 sgen_dump_internal_mem_usage (FILE *heap_dump_file)
248 fprintf (heap_dump_file, "<other-mem-usage type=\"large-internal\" size=\"%lld\"/>\n", large_internal_bytes_alloced);
249 fprintf (heap_dump_file, "<other-mem-usage type=\"pinned-chunks\" size=\"%lld\"/>\n", pinned_chunk_bytes_alloced);
250 for (i = 0; i < INTERNAL_MEM_MAX; ++i) {
251 fprintf (heap_dump_file, "<other-mem-usage type=\"%s\" size=\"%ld\"/>\n",
252 description_for_type (i), unmanaged_allocator.small_internal_mem_bytes [i]);
258 sgen_report_internal_mem_usage (void)
261 #ifdef HEAVY_STATISTICS
262 printf ("size -> # allocations\n");
263 for (i = 0; i < NUM_ALLOCATORS; ++i)
264 printf ("%d -> %d\n", allocator_sizes [i], allocator_sizes_stats [i]);
269 sgen_init_internal_allocator (void)
273 for (i = 0; i < INTERNAL_MEM_MAX; ++i)
274 fixed_type_allocator_indexes [i] = -1;
276 for (i = 0; i < NUM_ALLOCATORS; ++i) {
277 allocator_block_sizes [i] = block_size (allocator_sizes [i]);
278 mono_lock_free_allocator_init_size_class (&size_classes [i], allocator_sizes [i], allocator_block_sizes [i]);
279 mono_lock_free_allocator_init_allocator (&allocators [i], &size_classes [i]);
282 for (size = mono_pagesize (); size <= LOCK_FREE_ALLOC_SB_MAX_SIZE; size <<= 1) {
283 int max_size = LOCK_FREE_ALLOC_SB_USABLE_SIZE (size) / 2;
285 * we assert that allocator_sizes contains the biggest possible object size
286 * per block (4K => 4080 / 2 = 2040, 8k => 8176 / 2 = 4088, 16k => 16368 / 2 = 8184 on 64bits),
287 * so that we do not get different block sizes for sizes that should go to the same one
289 g_assert (allocator_sizes [index_for_size (max_size)] == max_size);