Mercurial > hg > truffle
annotate src/share/vm/memory/heap.cpp @ 7461:561148896559
8005076: Creating a CDS archive with one alignment and running another causes a crash.
Summary: Save the alignment when writing the CDS and compare it when reading the CDS.
Reviewed-by: kvn, coleenp
author | hseigel |
---|---|
date | Tue, 08 Jan 2013 13:38:11 -0500 |
parents | b9a9ed0f8eeb |
children | e522a00b91aa cc32ccaaf47f |
rev | line source |
---|---|
0 | 1 /* |
6842
b9a9ed0f8eeb
7197424: update copyright year to match last edit in jdk8 hotspot repository
mikael
parents:
6197
diff
changeset
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1123
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1123
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1123
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "memory/heap.hpp" | |
27 #include "oops/oop.inline.hpp" | |
28 #include "runtime/os.hpp" | |
6197 | 29 #include "services/memTracker.hpp" |
0 | 30 |
31 size_t CodeHeap::header_size() { | |
32 return sizeof(HeapBlock); | |
33 } | |
34 | |
35 | |
36 // Implementation of Heap | |
37 | |
38 CodeHeap::CodeHeap() { | |
39 _number_of_committed_segments = 0; | |
40 _number_of_reserved_segments = 0; | |
41 _segment_size = 0; | |
42 _log2_segment_size = 0; | |
43 _next_segment = 0; | |
44 _freelist = NULL; | |
45 _free_segments = 0; | |
46 } | |
47 | |
48 | |
49 void CodeHeap::mark_segmap_as_free(size_t beg, size_t end) { | |
50 assert(0 <= beg && beg < _number_of_committed_segments, "interval begin out of bounds"); | |
51 assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds"); | |
52 // setup _segmap pointers for faster indexing | |
53 address p = (address)_segmap.low() + beg; | |
54 address q = (address)_segmap.low() + end; | |
55 // initialize interval | |
56 while (p < q) *p++ = 0xFF; | |
57 } | |
58 | |
59 | |
60 void CodeHeap::mark_segmap_as_used(size_t beg, size_t end) { | |
61 assert(0 <= beg && beg < _number_of_committed_segments, "interval begin out of bounds"); | |
62 assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds"); | |
63 // setup _segmap pointers for faster indexing | |
64 address p = (address)_segmap.low() + beg; | |
65 address q = (address)_segmap.low() + end; | |
66 // initialize interval | |
67 int i = 0; | |
68 while (p < q) { | |
69 *p++ = i++; | |
70 if (i == 0xFF) i = 1; | |
71 } | |
72 } | |
73 | |
74 | |
75 static size_t align_to_page_size(size_t size) { | |
76 const size_t alignment = (size_t)os::vm_page_size(); | |
77 assert(is_power_of_2(alignment), "no kidding ???"); | |
78 return (size + alignment - 1) & ~(alignment - 1); | |
79 } | |
80 | |
81 | |
82 static size_t align_to_allocation_size(size_t size) { | |
83 const size_t alignment = (size_t)os::vm_allocation_granularity(); | |
84 assert(is_power_of_2(alignment), "no kidding ???"); | |
85 return (size + alignment - 1) & ~(alignment - 1); | |
86 } | |
87 | |
88 | |
89 void CodeHeap::on_code_mapping(char* base, size_t size) { | |
90 #ifdef LINUX | |
91 extern void linux_wrap_code(char* base, size_t size); | |
92 linux_wrap_code(base, size); | |
93 #endif | |
94 } | |
95 | |
96 | |
97 bool CodeHeap::reserve(size_t reserved_size, size_t committed_size, | |
98 size_t segment_size) { | |
99 assert(reserved_size >= committed_size, "reserved < committed"); | |
100 assert(segment_size >= sizeof(FreeBlock), "segment size is too small"); | |
101 assert(is_power_of_2(segment_size), "segment_size must be a power of 2"); | |
102 | |
103 _segment_size = segment_size; | |
104 _log2_segment_size = exact_log2(segment_size); | |
105 | |
106 // Reserve and initialize space for _memory. | |
79
82db0859acbe
6642862: Code cache allocation fails with large pages after 6588638
jcoomes
parents:
0
diff
changeset
|
107 const size_t page_size = os::can_execute_large_page_memory() ? |
82db0859acbe
6642862: Code cache allocation fails with large pages after 6588638
jcoomes
parents:
0
diff
changeset
|
108 os::page_size_for_region(committed_size, reserved_size, 8) : |
82db0859acbe
6642862: Code cache allocation fails with large pages after 6588638
jcoomes
parents:
0
diff
changeset
|
109 os::vm_page_size(); |
0 | 110 const size_t granularity = os::vm_allocation_granularity(); |
111 const size_t r_align = MAX2(page_size, granularity); | |
112 const size_t r_size = align_size_up(reserved_size, r_align); | |
113 const size_t c_size = align_size_up(committed_size, page_size); | |
114 | |
115 const size_t rs_align = page_size == (size_t) os::vm_page_size() ? 0 : | |
116 MAX2(page_size, granularity); | |
656 | 117 ReservedCodeSpace rs(r_size, rs_align, rs_align > 0); |
0 | 118 os::trace_page_sizes("code heap", committed_size, reserved_size, page_size, |
119 rs.base(), rs.size()); | |
120 if (!_memory.initialize(rs, c_size)) { | |
121 return false; | |
122 } | |
123 | |
124 on_code_mapping(_memory.low(), _memory.committed_size()); | |
125 _number_of_committed_segments = number_of_segments(_memory.committed_size()); | |
126 _number_of_reserved_segments = number_of_segments(_memory.reserved_size()); | |
127 assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking"); | |
128 | |
129 // reserve space for _segmap | |
130 if (!_segmap.initialize(align_to_page_size(_number_of_reserved_segments), align_to_page_size(_number_of_committed_segments))) { | |
131 return false; | |
132 } | |
6197 | 133 |
134 MemTracker::record_virtual_memory_type((address)_segmap.low_boundary(), mtCode); | |
135 | |
0 | 136 assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "could not commit enough space for segment map"); |
137 assert(_segmap.reserved_size() >= (size_t) _number_of_reserved_segments , "could not reserve enough space for segment map"); | |
138 assert(_segmap.reserved_size() >= _segmap.committed_size() , "just checking"); | |
139 | |
140 // initialize remaining instance variables | |
141 clear(); | |
142 return true; | |
143 } | |
144 | |
145 | |
146 void CodeHeap::release() { | |
147 Unimplemented(); | |
148 } | |
149 | |
150 | |
151 bool CodeHeap::expand_by(size_t size) { | |
152 // expand _memory space | |
153 size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size(); | |
154 if (dm > 0) { | |
155 char* base = _memory.low() + _memory.committed_size(); | |
156 if (!_memory.expand_by(dm)) return false; | |
157 on_code_mapping(base, dm); | |
158 size_t i = _number_of_committed_segments; | |
159 _number_of_committed_segments = number_of_segments(_memory.committed_size()); | |
160 assert(_number_of_reserved_segments == number_of_segments(_memory.reserved_size()), "number of reserved segments should not change"); | |
161 assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking"); | |
162 // expand _segmap space | |
163 size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size(); | |
164 if (ds > 0) { | |
165 if (!_segmap.expand_by(ds)) return false; | |
166 } | |
167 assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking"); | |
168 // initialize additional segmap entries | |
169 mark_segmap_as_free(i, _number_of_committed_segments); | |
170 } | |
171 return true; | |
172 } | |
173 | |
174 | |
175 void CodeHeap::shrink_by(size_t size) { | |
176 Unimplemented(); | |
177 } | |
178 | |
179 | |
180 void CodeHeap::clear() { | |
181 _next_segment = 0; | |
182 mark_segmap_as_free(0, _number_of_committed_segments); | |
183 } | |
184 | |
185 | |
186 void* CodeHeap::allocate(size_t size) { | |
187 size_t length = number_of_segments(size + sizeof(HeapBlock)); | |
188 assert(length *_segment_size >= sizeof(FreeBlock), "not enough room for FreeList"); | |
189 | |
190 // First check if we can satify request from freelist | |
191 debug_only(verify()); | |
192 HeapBlock* block = search_freelist(length); | |
193 debug_only(if (VerifyCodeCacheOften) verify()); | |
194 if (block != NULL) { | |
195 assert(block->length() >= length && block->length() < length + CodeCacheMinBlockLength, "sanity check"); | |
196 assert(!block->free(), "must be marked free"); | |
197 #ifdef ASSERT | |
198 memset((void *)block->allocated_space(), badCodeHeapNewVal, size); | |
199 #endif | |
200 return block->allocated_space(); | |
201 } | |
202 | |
203 if (length < CodeCacheMinBlockLength) { | |
204 length = CodeCacheMinBlockLength; | |
205 } | |
206 if (_next_segment + length <= _number_of_committed_segments) { | |
207 mark_segmap_as_used(_next_segment, _next_segment + length); | |
208 HeapBlock* b = block_at(_next_segment); | |
209 b->initialize(length); | |
210 _next_segment += length; | |
211 #ifdef ASSERT | |
212 memset((void *)b->allocated_space(), badCodeHeapNewVal, size); | |
213 #endif | |
214 return b->allocated_space(); | |
215 } else { | |
216 return NULL; | |
217 } | |
218 } | |
219 | |
220 | |
221 void CodeHeap::deallocate(void* p) { | |
222 assert(p == find_start(p), "illegal deallocation"); | |
223 // Find start of HeapBlock | |
224 HeapBlock* b = (((HeapBlock *)p) - 1); | |
225 assert(b->allocated_space() == p, "sanity check"); | |
226 #ifdef ASSERT | |
227 memset((void *)b->allocated_space(), | |
228 badCodeHeapFreeVal, | |
229 size(b->length()) - sizeof(HeapBlock)); | |
230 #endif | |
231 add_to_freelist(b); | |
232 | |
233 debug_only(if (VerifyCodeCacheOften) verify()); | |
234 } | |
235 | |
236 | |
237 void* CodeHeap::find_start(void* p) const { | |
238 if (!contains(p)) { | |
239 return NULL; | |
240 } | |
241 size_t i = segment_for(p); | |
242 address b = (address)_segmap.low(); | |
243 if (b[i] == 0xFF) { | |
244 return NULL; | |
245 } | |
246 while (b[i] > 0) i -= (int)b[i]; | |
247 HeapBlock* h = block_at(i); | |
248 if (h->free()) { | |
249 return NULL; | |
250 } | |
251 return h->allocated_space(); | |
252 } | |
253 | |
254 | |
255 size_t CodeHeap::alignment_unit() const { | |
256 // this will be a power of two | |
257 return _segment_size; | |
258 } | |
259 | |
260 | |
261 size_t CodeHeap::alignment_offset() const { | |
262 // The lowest address in any allocated block will be | |
263 // equal to alignment_offset (mod alignment_unit). | |
264 return sizeof(HeapBlock) & (_segment_size - 1); | |
265 } | |
266 | |
267 // Finds the next free heapblock. If the current one is free, that it returned | |
268 void* CodeHeap::next_free(HeapBlock *b) const { | |
269 // Since free blocks are merged, there is max. on free block | |
270 // between two used ones | |
271 if (b != NULL && b->free()) b = next_block(b); | |
272 assert(b == NULL || !b->free(), "must be in use or at end of heap"); | |
273 return (b == NULL) ? NULL : b->allocated_space(); | |
274 } | |
275 | |
276 // Returns the first used HeapBlock | |
277 HeapBlock* CodeHeap::first_block() const { | |
278 if (_next_segment > 0) | |
279 return block_at(0); | |
280 return NULL; | |
281 } | |
282 | |
283 HeapBlock *CodeHeap::block_start(void *q) const { | |
284 HeapBlock* b = (HeapBlock*)find_start(q); | |
285 if (b == NULL) return NULL; | |
286 return b - 1; | |
287 } | |
288 | |
289 // Returns the next Heap block an offset into one | |
290 HeapBlock* CodeHeap::next_block(HeapBlock *b) const { | |
291 if (b == NULL) return NULL; | |
292 size_t i = segment_for(b) + b->length(); | |
293 if (i < _next_segment) | |
294 return block_at(i); | |
295 return NULL; | |
296 } | |
297 | |
298 | |
299 // Returns current capacity | |
300 size_t CodeHeap::capacity() const { | |
301 return _memory.committed_size(); | |
302 } | |
303 | |
304 size_t CodeHeap::max_capacity() const { | |
305 return _memory.reserved_size(); | |
306 } | |
307 | |
308 size_t CodeHeap::allocated_capacity() const { | |
309 // Start with the committed size in _memory; | |
310 size_t l = _memory.committed_size(); | |
311 | |
312 // Subtract the committed, but unused, segments | |
313 l -= size(_number_of_committed_segments - _next_segment); | |
314 | |
315 // Subtract the size of the freelist | |
316 l -= size(_free_segments); | |
317 | |
318 return l; | |
319 } | |
320 | |
2091
51bd2d261853
7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents:
1972
diff
changeset
|
321 size_t CodeHeap::largest_free_block() const { |
2353
1c0cf339481b
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents:
2091
diff
changeset
|
322 // First check unused space excluding free blocks. |
1c0cf339481b
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents:
2091
diff
changeset
|
323 size_t free_sz = size(_free_segments); |
1c0cf339481b
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents:
2091
diff
changeset
|
324 size_t unused = max_capacity() - allocated_capacity() - free_sz; |
1c0cf339481b
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents:
2091
diff
changeset
|
325 if (unused >= free_sz) |
1c0cf339481b
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents:
2091
diff
changeset
|
326 return unused; |
1c0cf339481b
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents:
2091
diff
changeset
|
327 |
1c0cf339481b
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents:
2091
diff
changeset
|
328 // Now check largest free block. |
2091
51bd2d261853
7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents:
1972
diff
changeset
|
329 size_t len = 0; |
51bd2d261853
7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents:
1972
diff
changeset
|
330 for (FreeBlock* b = _freelist; b != NULL; b = b->link()) { |
51bd2d261853
7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents:
1972
diff
changeset
|
331 if (b->length() > len) |
51bd2d261853
7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents:
1972
diff
changeset
|
332 len = b->length(); |
51bd2d261853
7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents:
1972
diff
changeset
|
333 } |
2353
1c0cf339481b
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents:
2091
diff
changeset
|
334 return MAX2(unused, size(len)); |
2091
51bd2d261853
7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents:
1972
diff
changeset
|
335 } |
51bd2d261853
7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents:
1972
diff
changeset
|
336 |
0 | 337 // Free list management |
338 | |
339 FreeBlock *CodeHeap::following_block(FreeBlock *b) { | |
340 return (FreeBlock*)(((address)b) + _segment_size * b->length()); | |
341 } | |
342 | |
343 // Inserts block b after a | |
344 void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) { | |
345 assert(a != NULL && b != NULL, "must be real pointers"); | |
346 | |
347 // Link b into the list after a | |
348 b->set_link(a->link()); | |
349 a->set_link(b); | |
350 | |
351 // See if we can merge blocks | |
352 merge_right(b); // Try to make b bigger | |
353 merge_right(a); // Try to make a include b | |
354 } | |
355 | |
356 // Try to merge this block with the following block | |
357 void CodeHeap::merge_right(FreeBlock *a) { | |
358 assert(a->free(), "must be a free block"); | |
359 if (following_block(a) == a->link()) { | |
360 assert(a->link() != NULL && a->link()->free(), "must be free too"); | |
361 // Update block a to include the following block | |
362 a->set_length(a->length() + a->link()->length()); | |
363 a->set_link(a->link()->link()); | |
364 // Update find_start map | |
365 size_t beg = segment_for(a); | |
366 mark_segmap_as_used(beg, beg + a->length()); | |
367 } | |
368 } | |
369 | |
370 void CodeHeap::add_to_freelist(HeapBlock *a) { | |
371 FreeBlock* b = (FreeBlock*)a; | |
372 assert(b != _freelist, "cannot be removed twice"); | |
373 | |
374 // Mark as free and update free space count | |
375 _free_segments += b->length(); | |
376 b->set_free(); | |
377 | |
378 // First element in list? | |
379 if (_freelist == NULL) { | |
380 _freelist = b; | |
381 b->set_link(NULL); | |
382 return; | |
383 } | |
384 | |
385 // Scan for right place to put into list. List | |
386 // is sorted by increasing addresseses | |
387 FreeBlock* prev = NULL; | |
388 FreeBlock* cur = _freelist; | |
389 while(cur != NULL && cur < b) { | |
390 assert(prev == NULL || prev < cur, "must be ordered"); | |
391 prev = cur; | |
392 cur = cur->link(); | |
393 } | |
394 | |
395 assert( (prev == NULL && b < _freelist) || | |
396 (prev < b && (cur == NULL || b < cur)), "list must be ordered"); | |
397 | |
398 if (prev == NULL) { | |
399 // Insert first in list | |
400 b->set_link(_freelist); | |
401 _freelist = b; | |
402 merge_right(_freelist); | |
403 } else { | |
404 insert_after(prev, b); | |
405 } | |
406 } | |
407 | |
408 // Search freelist for an entry on the list with the best fit | |
409 // Return NULL if no one was found | |
410 FreeBlock* CodeHeap::search_freelist(size_t length) { | |
411 FreeBlock *best_block = NULL; | |
412 FreeBlock *best_prev = NULL; | |
413 size_t best_length = 0; | |
414 | |
415 // Search for smallest block which is bigger than length | |
416 FreeBlock *prev = NULL; | |
417 FreeBlock *cur = _freelist; | |
418 while(cur != NULL) { | |
419 size_t l = cur->length(); | |
420 if (l >= length && (best_block == NULL || best_length > l)) { | |
421 // Remember best block, its previous element, and its length | |
422 best_block = cur; | |
423 best_prev = prev; | |
424 best_length = best_block->length(); | |
425 } | |
426 | |
427 // Next element in list | |
428 prev = cur; | |
429 cur = cur->link(); | |
430 } | |
431 | |
432 if (best_block == NULL) { | |
433 // None found | |
434 return NULL; | |
435 } | |
436 | |
437 assert((best_prev == NULL && _freelist == best_block ) || | |
438 (best_prev != NULL && best_prev->link() == best_block), "sanity check"); | |
439 | |
440 // Exact (or at least good enough) fit. Remove from list. | |
441 // Don't leave anything on the freelist smaller than CodeCacheMinBlockLength. | |
442 if (best_length < length + CodeCacheMinBlockLength) { | |
443 length = best_length; | |
444 if (best_prev == NULL) { | |
445 assert(_freelist == best_block, "sanity check"); | |
446 _freelist = _freelist->link(); | |
447 } else { | |
448 // Unmap element | |
449 best_prev->set_link(best_block->link()); | |
450 } | |
451 } else { | |
452 // Truncate block and return a pointer to the following block | |
453 best_block->set_length(best_length - length); | |
454 best_block = following_block(best_block); | |
455 // Set used bit and length on new block | |
456 size_t beg = segment_for(best_block); | |
457 mark_segmap_as_used(beg, beg + length); | |
458 best_block->set_length(length); | |
459 } | |
460 | |
461 best_block->set_used(); | |
462 _free_segments -= length; | |
463 return best_block; | |
464 } | |
465 | |
466 //---------------------------------------------------------------------------- | |
467 // Non-product code | |
468 | |
469 #ifndef PRODUCT | |
470 | |
471 void CodeHeap::print() { | |
472 tty->print_cr("The Heap"); | |
473 } | |
474 | |
475 #endif | |
476 | |
477 void CodeHeap::verify() { | |
478 // Count the number of blocks on the freelist, and the amount of space | |
479 // represented. | |
480 int count = 0; | |
481 size_t len = 0; | |
482 for(FreeBlock* b = _freelist; b != NULL; b = b->link()) { | |
483 len += b->length(); | |
484 count++; | |
485 } | |
486 | |
487 // Verify that freelist contains the right amount of free space | |
1123
167c2986d91b
6843629: Make current hotspot build part of jdk5 control build
phh
parents:
844
diff
changeset
|
488 // guarantee(len == _free_segments, "wrong freelist"); |
0 | 489 |
490 // Verify that the number of free blocks is not out of hand. | |
491 static int free_block_threshold = 10000; | |
492 if (count > free_block_threshold) { | |
493 warning("CodeHeap: # of free blocks > %d", free_block_threshold); | |
494 // Double the warning limit | |
495 free_block_threshold *= 2; | |
496 } | |
497 | |
498 // Verify that the freelist contains the same number of free blocks that is | |
499 // found on the full list. | |
500 for(HeapBlock *h = first_block(); h != NULL; h = next_block(h)) { | |
501 if (h->free()) count--; | |
502 } | |
1123
167c2986d91b
6843629: Make current hotspot build part of jdk5 control build
phh
parents:
844
diff
changeset
|
503 // guarantee(count == 0, "missing free blocks"); |
0 | 504 } |