aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "memory/heap.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "services/memTracker.hpp" aoqi@0: aoqi@0: size_t CodeHeap::header_size() { aoqi@0: return sizeof(HeapBlock); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of Heap aoqi@0: aoqi@0: CodeHeap::CodeHeap() { aoqi@0: _number_of_committed_segments = 0; aoqi@0: _number_of_reserved_segments = 0; aoqi@0: _segment_size = 0; aoqi@0: _log2_segment_size = 0; aoqi@0: _next_segment = 0; aoqi@0: _freelist = NULL; aoqi@0: _freelist_segments = 0; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeHeap::mark_segmap_as_free(size_t beg, size_t end) { aoqi@0: assert(0 <= beg && beg < _number_of_committed_segments, "interval begin out of bounds"); aoqi@0: assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds"); aoqi@0: // setup _segmap pointers for faster indexing aoqi@0: address p = (address)_segmap.low() + beg; aoqi@0: address q = (address)_segmap.low() + end; aoqi@0: // initialize interval aoqi@0: while (p < q) *p++ = 0xFF; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeHeap::mark_segmap_as_used(size_t beg, size_t end) { aoqi@0: assert(0 <= beg && beg < _number_of_committed_segments, "interval begin out of bounds"); aoqi@0: assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds"); aoqi@0: // setup _segmap pointers for faster indexing aoqi@0: address p = (address)_segmap.low() + beg; aoqi@0: address q = (address)_segmap.low() + end; aoqi@0: // initialize interval aoqi@0: int i = 0; aoqi@0: while (p < q) { aoqi@0: *p++ = i++; aoqi@0: if (i == 0xFF) i = 1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static size_t align_to_page_size(size_t size) { aoqi@0: const size_t alignment = (size_t)os::vm_page_size(); aoqi@0: assert(is_power_of_2(alignment), "no kidding ???"); aoqi@0: return (size + alignment - 1) & ~(alignment - 1); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeHeap::on_code_mapping(char* base, size_t size) { aoqi@0: #ifdef LINUX aoqi@0: extern void linux_wrap_code(char* base, size_t size); aoqi@0: linux_wrap_code(base, size); aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool CodeHeap::reserve(size_t reserved_size, size_t committed_size, aoqi@0: size_t segment_size) { aoqi@0: assert(reserved_size >= committed_size, "reserved < committed"); aoqi@0: assert(segment_size >= sizeof(FreeBlock), "segment size is too small"); aoqi@0: assert(is_power_of_2(segment_size), "segment_size must be a power of 2"); aoqi@0: aoqi@0: _segment_size = segment_size; aoqi@0: _log2_segment_size = exact_log2(segment_size); aoqi@0: aoqi@0: // Reserve and initialize space for _memory. aoqi@0: const size_t page_size = os::can_execute_large_page_memory() ? aoqi@0: os::page_size_for_region(committed_size, reserved_size, 8) : aoqi@0: os::vm_page_size(); aoqi@0: const size_t granularity = os::vm_allocation_granularity(); aoqi@0: const size_t r_align = MAX2(page_size, granularity); aoqi@0: const size_t r_size = align_size_up(reserved_size, r_align); aoqi@0: const size_t c_size = align_size_up(committed_size, page_size); aoqi@0: aoqi@0: const size_t rs_align = page_size == (size_t) os::vm_page_size() ? 0 : aoqi@0: MAX2(page_size, granularity); aoqi@0: ReservedCodeSpace rs(r_size, rs_align, rs_align > 0); aoqi@0: os::trace_page_sizes("code heap", committed_size, reserved_size, page_size, aoqi@0: rs.base(), rs.size()); aoqi@0: if (!_memory.initialize(rs, c_size)) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: on_code_mapping(_memory.low(), _memory.committed_size()); aoqi@0: _number_of_committed_segments = size_to_segments(_memory.committed_size()); aoqi@0: _number_of_reserved_segments = size_to_segments(_memory.reserved_size()); aoqi@0: assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking"); aoqi@0: const size_t reserved_segments_alignment = MAX2((size_t)os::vm_page_size(), granularity); aoqi@0: const size_t reserved_segments_size = align_size_up(_number_of_reserved_segments, reserved_segments_alignment); aoqi@0: const size_t committed_segments_size = align_to_page_size(_number_of_committed_segments); aoqi@0: aoqi@0: // reserve space for _segmap aoqi@0: if (!_segmap.initialize(reserved_segments_size, committed_segments_size)) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: MemTracker::record_virtual_memory_type((address)_segmap.low_boundary(), mtCode); aoqi@0: aoqi@0: assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "could not commit enough space for segment map"); aoqi@0: assert(_segmap.reserved_size() >= (size_t) _number_of_reserved_segments , "could not reserve enough space for segment map"); aoqi@0: assert(_segmap.reserved_size() >= _segmap.committed_size() , "just checking"); aoqi@0: aoqi@0: // initialize remaining instance variables aoqi@0: clear(); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeHeap::release() { aoqi@0: Unimplemented(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool CodeHeap::expand_by(size_t size) { aoqi@0: // expand _memory space aoqi@0: size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size(); aoqi@0: if (dm > 0) { aoqi@0: char* base = _memory.low() + _memory.committed_size(); aoqi@0: if (!_memory.expand_by(dm)) return false; aoqi@0: on_code_mapping(base, dm); aoqi@0: size_t i = _number_of_committed_segments; aoqi@0: _number_of_committed_segments = size_to_segments(_memory.committed_size()); aoqi@0: assert(_number_of_reserved_segments == size_to_segments(_memory.reserved_size()), "number of reserved segments should not change"); aoqi@0: assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking"); aoqi@0: // expand _segmap space aoqi@0: size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size(); aoqi@0: if (ds > 0) { aoqi@0: if (!_segmap.expand_by(ds)) return false; aoqi@0: } aoqi@0: assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking"); aoqi@0: // initialize additional segmap entries aoqi@0: mark_segmap_as_free(i, _number_of_committed_segments); aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeHeap::shrink_by(size_t size) { aoqi@0: Unimplemented(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeHeap::clear() { aoqi@0: _next_segment = 0; aoqi@0: mark_segmap_as_free(0, _number_of_committed_segments); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void* CodeHeap::allocate(size_t instance_size, bool is_critical) { aoqi@0: size_t number_of_segments = size_to_segments(instance_size + sizeof(HeapBlock)); aoqi@0: assert(segments_to_size(number_of_segments) >= sizeof(FreeBlock), "not enough room for FreeList"); aoqi@0: aoqi@0: // First check if we can satify request from freelist aoqi@0: debug_only(verify()); aoqi@0: HeapBlock* block = search_freelist(number_of_segments, is_critical); aoqi@0: debug_only(if (VerifyCodeCacheOften) verify()); aoqi@0: if (block != NULL) { aoqi@0: assert(block->length() >= number_of_segments && block->length() < number_of_segments + CodeCacheMinBlockLength, "sanity check"); aoqi@0: assert(!block->free(), "must be marked free"); aoqi@0: #ifdef ASSERT aoqi@0: memset((void *)block->allocated_space(), badCodeHeapNewVal, instance_size); aoqi@0: #endif aoqi@0: return block->allocated_space(); aoqi@0: } aoqi@0: aoqi@0: // Ensure minimum size for allocation to the heap. aoqi@0: if (number_of_segments < CodeCacheMinBlockLength) { aoqi@0: number_of_segments = CodeCacheMinBlockLength; aoqi@0: } aoqi@0: aoqi@0: if (!is_critical) { aoqi@0: // Make sure the allocation fits in the unallocated heap without using aoqi@0: // the CodeCacheMimimumFreeSpace that is reserved for critical allocations. aoqi@0: if (segments_to_size(number_of_segments) > (heap_unallocated_capacity() - CodeCacheMinimumFreeSpace)) { aoqi@0: // Fail allocation aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (_next_segment + number_of_segments <= _number_of_committed_segments) { aoqi@0: mark_segmap_as_used(_next_segment, _next_segment + number_of_segments); aoqi@0: HeapBlock* b = block_at(_next_segment); aoqi@0: b->initialize(number_of_segments); aoqi@0: _next_segment += number_of_segments; aoqi@0: #ifdef ASSERT aoqi@0: memset((void *)b->allocated_space(), badCodeHeapNewVal, instance_size); aoqi@0: #endif aoqi@0: return b->allocated_space(); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void CodeHeap::deallocate(void* p) { aoqi@0: assert(p == find_start(p), "illegal deallocation"); aoqi@0: // Find start of HeapBlock aoqi@0: HeapBlock* b = (((HeapBlock *)p) - 1); aoqi@0: assert(b->allocated_space() == p, "sanity check"); aoqi@0: #ifdef ASSERT aoqi@0: memset((void *)b->allocated_space(), aoqi@0: badCodeHeapFreeVal, aoqi@0: segments_to_size(b->length()) - sizeof(HeapBlock)); aoqi@0: #endif aoqi@0: add_to_freelist(b); aoqi@0: aoqi@0: debug_only(if (VerifyCodeCacheOften) verify()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void* CodeHeap::find_start(void* p) const { aoqi@0: if (!contains(p)) { aoqi@0: return NULL; aoqi@0: } aoqi@0: size_t i = segment_for(p); aoqi@0: address b = (address)_segmap.low(); aoqi@0: if (b[i] == 0xFF) { aoqi@0: return NULL; aoqi@0: } aoqi@0: while (b[i] > 0) i -= (int)b[i]; aoqi@0: HeapBlock* h = block_at(i); aoqi@0: if (h->free()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: return h->allocated_space(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: size_t CodeHeap::alignment_unit() const { aoqi@0: // this will be a power of two aoqi@0: return _segment_size; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: size_t CodeHeap::alignment_offset() const { aoqi@0: // The lowest address in any allocated block will be aoqi@0: // equal to alignment_offset (mod alignment_unit). aoqi@0: return sizeof(HeapBlock) & (_segment_size - 1); aoqi@0: } aoqi@0: aoqi@0: // Finds the next free heapblock. If the current one is free, that it returned aoqi@0: void* CodeHeap::next_free(HeapBlock *b) const { aoqi@0: // Since free blocks are merged, there is max. on free block aoqi@0: // between two used ones aoqi@0: if (b != NULL && b->free()) b = next_block(b); aoqi@0: assert(b == NULL || !b->free(), "must be in use or at end of heap"); aoqi@0: return (b == NULL) ? NULL : b->allocated_space(); aoqi@0: } aoqi@0: aoqi@0: // Returns the first used HeapBlock aoqi@0: HeapBlock* CodeHeap::first_block() const { aoqi@0: if (_next_segment > 0) aoqi@0: return block_at(0); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: HeapBlock *CodeHeap::block_start(void *q) const { aoqi@0: HeapBlock* b = (HeapBlock*)find_start(q); aoqi@0: if (b == NULL) return NULL; aoqi@0: return b - 1; aoqi@0: } aoqi@0: aoqi@0: // Returns the next Heap block an offset into one aoqi@0: HeapBlock* CodeHeap::next_block(HeapBlock *b) const { aoqi@0: if (b == NULL) return NULL; aoqi@0: size_t i = segment_for(b) + b->length(); aoqi@0: if (i < _next_segment) aoqi@0: return block_at(i); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Returns current capacity aoqi@0: size_t CodeHeap::capacity() const { aoqi@0: return _memory.committed_size(); aoqi@0: } aoqi@0: aoqi@0: size_t CodeHeap::max_capacity() const { aoqi@0: return _memory.reserved_size(); aoqi@0: } aoqi@0: aoqi@0: size_t CodeHeap::allocated_capacity() const { aoqi@0: // size of used heap - size on freelist aoqi@0: return segments_to_size(_next_segment - _freelist_segments); aoqi@0: } aoqi@0: aoqi@0: // Returns size of the unallocated heap block aoqi@0: size_t CodeHeap::heap_unallocated_capacity() const { aoqi@0: // Total number of segments - number currently used aoqi@0: return segments_to_size(_number_of_reserved_segments - _next_segment); aoqi@0: } aoqi@0: aoqi@0: // Free list management aoqi@0: aoqi@0: FreeBlock *CodeHeap::following_block(FreeBlock *b) { aoqi@0: return (FreeBlock*)(((address)b) + _segment_size * b->length()); aoqi@0: } aoqi@0: aoqi@0: // Inserts block b after a aoqi@0: void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) { aoqi@0: assert(a != NULL && b != NULL, "must be real pointers"); aoqi@0: aoqi@0: // Link b into the list after a aoqi@0: b->set_link(a->link()); aoqi@0: a->set_link(b); aoqi@0: aoqi@0: // See if we can merge blocks aoqi@0: merge_right(b); // Try to make b bigger aoqi@0: merge_right(a); // Try to make a include b aoqi@0: } aoqi@0: aoqi@0: // Try to merge this block with the following block aoqi@0: void CodeHeap::merge_right(FreeBlock *a) { aoqi@0: assert(a->free(), "must be a free block"); aoqi@0: if (following_block(a) == a->link()) { aoqi@0: assert(a->link() != NULL && a->link()->free(), "must be free too"); aoqi@0: // Update block a to include the following block aoqi@0: a->set_length(a->length() + a->link()->length()); aoqi@0: a->set_link(a->link()->link()); aoqi@0: // Update find_start map aoqi@0: size_t beg = segment_for(a); aoqi@0: mark_segmap_as_used(beg, beg + a->length()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void CodeHeap::add_to_freelist(HeapBlock *a) { aoqi@0: FreeBlock* b = (FreeBlock*)a; aoqi@0: assert(b != _freelist, "cannot be removed twice"); aoqi@0: aoqi@0: // Mark as free and update free space count aoqi@0: _freelist_segments += b->length(); aoqi@0: b->set_free(); aoqi@0: aoqi@0: // First element in list? aoqi@0: if (_freelist == NULL) { aoqi@0: _freelist = b; aoqi@0: b->set_link(NULL); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Scan for right place to put into list. List aoqi@0: // is sorted by increasing addresseses aoqi@0: FreeBlock* prev = NULL; aoqi@0: FreeBlock* cur = _freelist; aoqi@0: while(cur != NULL && cur < b) { aoqi@0: assert(prev == NULL || prev < cur, "must be ordered"); aoqi@0: prev = cur; aoqi@0: cur = cur->link(); aoqi@0: } aoqi@0: aoqi@0: assert( (prev == NULL && b < _freelist) || aoqi@0: (prev < b && (cur == NULL || b < cur)), "list must be ordered"); aoqi@0: aoqi@0: if (prev == NULL) { aoqi@0: // Insert first in list aoqi@0: b->set_link(_freelist); aoqi@0: _freelist = b; aoqi@0: merge_right(_freelist); aoqi@0: } else { aoqi@0: insert_after(prev, b); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Search freelist for an entry on the list with the best fit aoqi@0: // Return NULL if no one was found aoqi@0: FreeBlock* CodeHeap::search_freelist(size_t length, bool is_critical) { aoqi@0: FreeBlock *best_block = NULL; aoqi@0: FreeBlock *best_prev = NULL; aoqi@0: size_t best_length = 0; aoqi@0: aoqi@0: // Search for smallest block which is bigger than length aoqi@0: FreeBlock *prev = NULL; aoqi@0: FreeBlock *cur = _freelist; aoqi@0: while(cur != NULL) { aoqi@0: size_t l = cur->length(); aoqi@0: if (l >= length && (best_block == NULL || best_length > l)) { aoqi@0: aoqi@0: // Non critical allocations are not allowed to use the last part of the code heap. aoqi@0: if (!is_critical) { aoqi@0: // Make sure the end of the allocation doesn't cross into the last part of the code heap aoqi@0: if (((size_t)cur + length) > ((size_t)high_boundary() - CodeCacheMinimumFreeSpace)) { aoqi@0: // the freelist is sorted by address - if one fails, all consecutive will also fail. aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Remember best block, its previous element, and its length aoqi@0: best_block = cur; aoqi@0: best_prev = prev; aoqi@0: best_length = best_block->length(); aoqi@0: } aoqi@0: aoqi@0: // Next element in list aoqi@0: prev = cur; aoqi@0: cur = cur->link(); aoqi@0: } aoqi@0: aoqi@0: if (best_block == NULL) { aoqi@0: // None found aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: assert((best_prev == NULL && _freelist == best_block ) || aoqi@0: (best_prev != NULL && best_prev->link() == best_block), "sanity check"); aoqi@0: aoqi@0: // Exact (or at least good enough) fit. Remove from list. aoqi@0: // Don't leave anything on the freelist smaller than CodeCacheMinBlockLength. aoqi@0: if (best_length < length + CodeCacheMinBlockLength) { aoqi@0: length = best_length; aoqi@0: if (best_prev == NULL) { aoqi@0: assert(_freelist == best_block, "sanity check"); aoqi@0: _freelist = _freelist->link(); aoqi@0: } else { aoqi@0: // Unmap element aoqi@0: best_prev->set_link(best_block->link()); aoqi@0: } aoqi@0: } else { aoqi@0: // Truncate block and return a pointer to the following block aoqi@0: best_block->set_length(best_length - length); aoqi@0: best_block = following_block(best_block); aoqi@0: // Set used bit and length on new block aoqi@0: size_t beg = segment_for(best_block); aoqi@0: mark_segmap_as_used(beg, beg + length); aoqi@0: best_block->set_length(length); aoqi@0: } aoqi@0: aoqi@0: best_block->set_used(); aoqi@0: _freelist_segments -= length; aoqi@0: return best_block; aoqi@0: } aoqi@0: aoqi@0: //---------------------------------------------------------------------------- aoqi@0: // Non-product code aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: void CodeHeap::print() { aoqi@0: tty->print_cr("The Heap"); aoqi@0: } aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: void CodeHeap::verify() { aoqi@0: // Count the number of blocks on the freelist, and the amount of space aoqi@0: // represented. aoqi@0: int count = 0; aoqi@0: size_t len = 0; aoqi@0: for(FreeBlock* b = _freelist; b != NULL; b = b->link()) { aoqi@0: len += b->length(); aoqi@0: count++; aoqi@0: } aoqi@0: aoqi@0: // Verify that freelist contains the right amount of free space aoqi@0: // guarantee(len == _freelist_segments, "wrong freelist"); aoqi@0: aoqi@0: // Verify that the number of free blocks is not out of hand. aoqi@0: static int free_block_threshold = 10000; aoqi@0: if (count > free_block_threshold) { aoqi@0: warning("CodeHeap: # of free blocks > %d", free_block_threshold); aoqi@0: // Double the warning limit aoqi@0: free_block_threshold *= 2; aoqi@0: } aoqi@0: aoqi@0: // Verify that the freelist contains the same number of free blocks that is aoqi@0: // found on the full list. aoqi@0: for(HeapBlock *h = first_block(); h != NULL; h = next_block(h)) { aoqi@0: if (h->free()) count--; aoqi@0: } aoqi@0: // guarantee(count == 0, "missing free blocks"); aoqi@0: }