8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000

Double CodeCacheSegmentSize from 64 byte to 128 bytes if tiered compilation is enabled

Reviewed-by: kvn, twisti
This commit is contained in:
Albert Noll 2014-03-07 07:42:40 +01:00
parent 87b278c44c
commit e8bc971d19
6 changed files with 201 additions and 168 deletions

View file

@ -198,14 +198,12 @@ CodeBlob* CodeCache::allocate(int size, bool is_critical) {
} }
maxCodeCacheUsed = MAX2(maxCodeCacheUsed, ((address)_heap->high_boundary() - maxCodeCacheUsed = MAX2(maxCodeCacheUsed, ((address)_heap->high_boundary() -
(address)_heap->low_boundary()) - unallocated_capacity()); (address)_heap->low_boundary()) - unallocated_capacity());
verify_if_often();
print_trace("allocation", cb, size); print_trace("allocation", cb, size);
return cb; return cb;
} }
void CodeCache::free(CodeBlob* cb) { void CodeCache::free(CodeBlob* cb) {
assert_locked_or_safepoint(CodeCache_lock); assert_locked_or_safepoint(CodeCache_lock);
verify_if_often();
print_trace("free", cb); print_trace("free", cb);
if (cb->is_nmethod()) { if (cb->is_nmethod()) {
@ -221,7 +219,6 @@ void CodeCache::free(CodeBlob* cb) {
_heap->deallocate(cb); _heap->deallocate(cb);
verify_if_often();
assert(_number_of_blobs >= 0, "sanity check"); assert(_number_of_blobs >= 0, "sanity check");
} }
@ -244,12 +241,6 @@ void CodeCache::commit(CodeBlob* cb) {
} }
void CodeCache::flush() {
assert_locked_or_safepoint(CodeCache_lock);
Unimplemented();
}
// Iteration over CodeBlobs // Iteration over CodeBlobs
#define FOR_ALL_BLOBS(var) for (CodeBlob *var = first() ; var != NULL; var = next(var) ) #define FOR_ALL_BLOBS(var) for (CodeBlob *var = first() ; var != NULL; var = next(var) )
@ -269,7 +260,7 @@ bool CodeCache::contains(void *p) {
CodeBlob* CodeCache::find_blob(void* start) { CodeBlob* CodeCache::find_blob(void* start) {
CodeBlob* result = find_blob_unsafe(start); CodeBlob* result = find_blob_unsafe(start);
if (result == NULL) return NULL; if (result == NULL) return NULL;
// We could potientially look up non_entrant methods // We could potentially look up non_entrant methods
guarantee(!result->is_zombie() || result->is_locked_by_vm() || is_error_reported(), "unsafe access to zombie method"); guarantee(!result->is_zombie() || result->is_locked_by_vm() || is_error_reported(), "unsafe access to zombie method");
return result; return result;
} }
@ -741,17 +732,26 @@ void CodeCache::report_codemem_full() {
} }
} }
void CodeCache::print_memory_overhead() {
size_t wasted_bytes = 0;
CodeBlob *cb;
for (cb = first(); cb != NULL; cb = next(cb)) {
HeapBlock* heap_block = ((HeapBlock*)cb) - 1;
wasted_bytes += heap_block->length() * CodeCacheSegmentSize - cb->size();
}
// Print bytes that are allocated in the freelist
ttyLocker ttl;
tty->print_cr("Number of elements in freelist: %d", freelist_length());
tty->print_cr("Allocated in freelist: %dkB", bytes_allocated_in_freelist()/K);
tty->print_cr("Unused bytes in CodeBlobs: %dkB", (int)(wasted_bytes/K));
tty->print_cr("Segment map size: %dkB", allocated_segments()/K); // 1 byte per segment
}
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
// Non-product version // Non-product version
#ifndef PRODUCT #ifndef PRODUCT
void CodeCache::verify_if_often() {
if (VerifyCodeCacheOften) {
_heap->verify();
}
}
void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) { void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) {
if (PrintCodeCache2) { // Need to add a new flag if (PrintCodeCache2) { // Need to add a new flag
ResourceMark rm; ResourceMark rm;
@ -774,7 +774,7 @@ void CodeCache::print_internals() {
int nmethodUnloaded = 0; int nmethodUnloaded = 0;
int nmethodJava = 0; int nmethodJava = 0;
int nmethodNative = 0; int nmethodNative = 0;
int maxCodeSize = 0; int max_nm_size = 0;
ResourceMark rm; ResourceMark rm;
CodeBlob *cb; CodeBlob *cb;
@ -798,13 +798,11 @@ void CodeCache::print_internals() {
if(nm->is_not_entrant()) { nmethodNotEntrant++; } if(nm->is_not_entrant()) { nmethodNotEntrant++; }
if(nm->is_zombie()) { nmethodZombie++; } if(nm->is_zombie()) { nmethodZombie++; }
if(nm->is_unloaded()) { nmethodUnloaded++; } if(nm->is_unloaded()) { nmethodUnloaded++; }
if(nm->is_native_method()) { nmethodNative++; } if(nm->method() != NULL && nm->is_native_method()) { nmethodNative++; }
if(nm->method() != NULL && nm->is_java_method()) { if(nm->method() != NULL && nm->is_java_method()) {
nmethodJava++; nmethodJava++;
if (nm->insts_size() > maxCodeSize) { max_nm_size = MAX2(max_nm_size, nm->size());
maxCodeSize = nm->insts_size();
}
} }
} else if (cb->is_runtime_stub()) { } else if (cb->is_runtime_stub()) {
runtimeStubCount++; runtimeStubCount++;
@ -820,7 +818,7 @@ void CodeCache::print_internals() {
} }
int bucketSize = 512; int bucketSize = 512;
int bucketLimit = maxCodeSize / bucketSize + 1; int bucketLimit = max_nm_size / bucketSize + 1;
int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit, mtCode); int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit, mtCode);
memset(buckets, 0, sizeof(int) * bucketLimit); memset(buckets, 0, sizeof(int) * bucketLimit);
@ -828,10 +826,11 @@ void CodeCache::print_internals() {
if (cb->is_nmethod()) { if (cb->is_nmethod()) {
nmethod* nm = (nmethod*)cb; nmethod* nm = (nmethod*)cb;
if(nm->is_java_method()) { if(nm->is_java_method()) {
buckets[nm->insts_size() / bucketSize]++; buckets[nm->size() / bucketSize]++;
} }
} }
} }
tty->print_cr("Code Cache Entries (total of %d)",total); tty->print_cr("Code Cache Entries (total of %d)",total);
tty->print_cr("-------------------------------------------------"); tty->print_cr("-------------------------------------------------");
tty->print_cr("nmethods: %d",nmethodCount); tty->print_cr("nmethods: %d",nmethodCount);
@ -858,6 +857,7 @@ void CodeCache::print_internals() {
} }
FREE_C_HEAP_ARRAY(int, buckets, mtCode); FREE_C_HEAP_ARRAY(int, buckets, mtCode);
print_memory_overhead();
} }
#endif // !PRODUCT #endif // !PRODUCT

View file

@ -58,12 +58,13 @@ class CodeCache : AllStatic {
static bool _needs_cache_clean; static bool _needs_cache_clean;
static nmethod* _scavenge_root_nmethods; // linked via nm->scavenge_root_link() static nmethod* _scavenge_root_nmethods; // linked via nm->scavenge_root_link()
static void verify_if_often() PRODUCT_RETURN;
static void mark_scavenge_root_nmethods() PRODUCT_RETURN; static void mark_scavenge_root_nmethods() PRODUCT_RETURN;
static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN; static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN;
static int _codemem_full_count; static int _codemem_full_count;
static size_t bytes_allocated_in_freelist() { return _heap->allocated_in_freelist(); }
static int allocated_segments() { return _heap->allocated_segments(); }
static size_t freelist_length() { return _heap->freelist_length(); }
public: public:
@ -78,7 +79,6 @@ class CodeCache : AllStatic {
static int alignment_unit(); // guaranteed alignment of all CodeBlobs static int alignment_unit(); // guaranteed alignment of all CodeBlobs
static int alignment_offset(); // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header) static int alignment_offset(); // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header)
static void free(CodeBlob* cb); // frees a CodeBlob static void free(CodeBlob* cb); // frees a CodeBlob
static void flush(); // flushes all CodeBlobs
static bool contains(void *p); // returns whether p is included static bool contains(void *p); // returns whether p is included
static void blobs_do(void f(CodeBlob* cb)); // iterates over all CodeBlobs static void blobs_do(void f(CodeBlob* cb)); // iterates over all CodeBlobs
static void blobs_do(CodeBlobClosure* f); // iterates over all CodeBlobs static void blobs_do(CodeBlobClosure* f); // iterates over all CodeBlobs
@ -150,6 +150,7 @@ class CodeCache : AllStatic {
// Printing/debugging // Printing/debugging
static void print(); // prints summary static void print(); // prints summary
static void print_internals(); static void print_internals();
static void print_memory_overhead();
static void verify(); // verifies the code cache static void verify(); // verifies the code cache
static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN; static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN;
static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage

View file

@ -43,6 +43,7 @@ CodeHeap::CodeHeap() {
_next_segment = 0; _next_segment = 0;
_freelist = NULL; _freelist = NULL;
_freelist_segments = 0; _freelist_segments = 0;
_freelist_length = 0;
} }
@ -53,7 +54,7 @@ void CodeHeap::mark_segmap_as_free(size_t beg, size_t end) {
address p = (address)_segmap.low() + beg; address p = (address)_segmap.low() + beg;
address q = (address)_segmap.low() + end; address q = (address)_segmap.low() + end;
// initialize interval // initialize interval
while (p < q) *p++ = 0xFF; while (p < q) *p++ = free_sentinel;
} }
@ -67,7 +68,7 @@ void CodeHeap::mark_segmap_as_used(size_t beg, size_t end) {
int i = 0; int i = 0;
while (p < q) { while (p < q) {
*p++ = i++; *p++ = i++;
if (i == 0xFF) i = 1; if (i == free_sentinel) i = 1;
} }
} }
@ -139,11 +140,6 @@ bool CodeHeap::reserve(size_t reserved_size, size_t committed_size,
} }
void CodeHeap::release() {
Unimplemented();
}
bool CodeHeap::expand_by(size_t size) { bool CodeHeap::expand_by(size_t size) {
// expand _memory space // expand _memory space
size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size(); size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size();
@ -157,8 +153,8 @@ bool CodeHeap::expand_by(size_t size) {
assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking"); assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
// expand _segmap space // expand _segmap space
size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size(); size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size();
if (ds > 0) { if ((ds > 0) && !_segmap.expand_by(ds)) {
if (!_segmap.expand_by(ds)) return false; return false;
} }
assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking"); assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking");
// initialize additional segmap entries // initialize additional segmap entries
@ -167,12 +163,6 @@ bool CodeHeap::expand_by(size_t size) {
return true; return true;
} }
void CodeHeap::shrink_by(size_t size) {
Unimplemented();
}
void CodeHeap::clear() { void CodeHeap::clear() {
_next_segment = 0; _next_segment = 0;
mark_segmap_as_free(0, _number_of_committed_segments); mark_segmap_as_free(0, _number_of_committed_segments);
@ -180,26 +170,23 @@ void CodeHeap::clear() {
void* CodeHeap::allocate(size_t instance_size, bool is_critical) { void* CodeHeap::allocate(size_t instance_size, bool is_critical) {
size_t number_of_segments = size_to_segments(instance_size + sizeof(HeapBlock)); size_t number_of_segments = size_to_segments(instance_size + header_size());
assert(segments_to_size(number_of_segments) >= sizeof(FreeBlock), "not enough room for FreeList"); assert(segments_to_size(number_of_segments) >= sizeof(FreeBlock), "not enough room for FreeList");
// First check if we can satisfy request from freelist // First check if we can satisfy request from freelist
debug_only(verify()); NOT_PRODUCT(verify());
HeapBlock* block = search_freelist(number_of_segments, is_critical); HeapBlock* block = search_freelist(number_of_segments, is_critical);
debug_only(if (VerifyCodeCacheOften) verify()); NOT_PRODUCT(verify());
if (block != NULL) { if (block != NULL) {
assert(block->length() >= number_of_segments && block->length() < number_of_segments + CodeCacheMinBlockLength, "sanity check"); assert(block->length() >= number_of_segments && block->length() < number_of_segments + CodeCacheMinBlockLength, "sanity check");
assert(!block->free(), "must be marked free"); assert(!block->free(), "must be marked free");
#ifdef ASSERT DEBUG_ONLY(memset((void*)block->allocated_space(), badCodeHeapNewVal, instance_size));
memset((void *)block->allocated_space(), badCodeHeapNewVal, instance_size);
#endif
return block->allocated_space(); return block->allocated_space();
} }
// Ensure minimum size for allocation to the heap. // Ensure minimum size for allocation to the heap.
if (number_of_segments < CodeCacheMinBlockLength) { number_of_segments = MAX2((int)CodeCacheMinBlockLength, (int)number_of_segments);
number_of_segments = CodeCacheMinBlockLength;
}
if (!is_critical) { if (!is_critical) {
// Make sure the allocation fits in the unallocated heap without using // Make sure the allocation fits in the unallocated heap without using
@ -215,9 +202,7 @@ void* CodeHeap::allocate(size_t instance_size, bool is_critical) {
HeapBlock* b = block_at(_next_segment); HeapBlock* b = block_at(_next_segment);
b->initialize(number_of_segments); b->initialize(number_of_segments);
_next_segment += number_of_segments; _next_segment += number_of_segments;
#ifdef ASSERT DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapNewVal, instance_size));
memset((void *)b->allocated_space(), badCodeHeapNewVal, instance_size);
#endif
return b->allocated_space(); return b->allocated_space();
} else { } else {
return NULL; return NULL;
@ -230,28 +215,56 @@ void CodeHeap::deallocate(void* p) {
// Find start of HeapBlock // Find start of HeapBlock
HeapBlock* b = (((HeapBlock *)p) - 1); HeapBlock* b = (((HeapBlock *)p) - 1);
assert(b->allocated_space() == p, "sanity check"); assert(b->allocated_space() == p, "sanity check");
#ifdef ASSERT DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapFreeVal,
memset((void *)b->allocated_space(), segments_to_size(b->length()) - sizeof(HeapBlock)));
badCodeHeapFreeVal,
segments_to_size(b->length()) - sizeof(HeapBlock));
#endif
add_to_freelist(b); add_to_freelist(b);
NOT_PRODUCT(verify());
debug_only(if (VerifyCodeCacheOften) verify());
} }
/**
* Uses segment map to find the the start (header) of a nmethod. This works as follows:
* The memory of the code cache is divided into 'segments'. The size of a segment is
* determined by -XX:CodeCacheSegmentSize=XX. Allocation in the code cache can only
* happen at segment boundaries. A pointer in the code cache can be mapped to a segment
* by calling segment_for(addr). Each time memory is requested from the code cache,
* the segmap is updated accordingly. See the following example, which illustrates the
* state of code cache and the segment map: (seg -> segment, nm ->nmethod)
*
* code cache segmap
* ----------- ---------
* seg 1 | nm 1 | -> | 0 |
* seg 2 | nm 1 | -> | 1 |
* ... | nm 1 | -> | .. |
* seg m | nm 2 | -> | 0 |
* seg m+1 | nm 2 | -> | 1 |
* ... | nm 2 | -> | 2 |
* ... | nm 2 | -> | .. |
* ... | nm 2 | -> | 0xFE |
* seg m+n | nm 2 | -> | 1 |
* ... | nm 2 | -> | |
*
* A value of '0' in the segmap indicates that this segment contains the beginning of
* an nmethod. Let's walk through a simple example: If we want to find the start of
* an nmethod that falls into seg 2, we read the value of the segmap[2]. The value
* is an offset that points to the segment that contains the start of the nmethod.
* Another example: If we want to get the start of nm 2, and we happen to get a pointer
* that points to seg m+n, we first read seg[n+m], which returns '1'. So we have to
* do one more read of the segmap[m+n-1] to finally get the segment header.
*/
void* CodeHeap::find_start(void* p) const { void* CodeHeap::find_start(void* p) const {
if (!contains(p)) { if (!contains(p)) {
return NULL; return NULL;
} }
size_t i = segment_for(p); size_t seg_idx = segment_for(p);
address b = (address)_segmap.low(); address seg_map = (address)_segmap.low();
if (b[i] == 0xFF) { if (is_segment_unused(seg_map[seg_idx])) {
return NULL; return NULL;
} }
while (b[i] > 0) i -= (int)b[i]; while (seg_map[seg_idx] > 0) {
HeapBlock* h = block_at(i); seg_idx -= (int)seg_map[seg_idx];
}
HeapBlock* h = block_at(seg_idx);
if (h->free()) { if (h->free()) {
return NULL; return NULL;
} }
@ -312,6 +325,10 @@ size_t CodeHeap::max_capacity() const {
return _memory.reserved_size(); return _memory.reserved_size();
} }
int CodeHeap::allocated_segments() const {
return (int)_next_segment;
}
size_t CodeHeap::allocated_capacity() const { size_t CodeHeap::allocated_capacity() const {
// size of used heap - size on freelist // size of used heap - size on freelist
return segments_to_size(_next_segment - _freelist_segments); return segments_to_size(_next_segment - _freelist_segments);
@ -343,7 +360,7 @@ void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) {
} }
// Try to merge this block with the following block // Try to merge this block with the following block
void CodeHeap::merge_right(FreeBlock *a) { bool CodeHeap::merge_right(FreeBlock* a) {
assert(a->free(), "must be a free block"); assert(a->free(), "must be a free block");
if (following_block(a) == a->link()) { if (following_block(a) == a->link()) {
assert(a->link() != NULL && a->link()->free(), "must be free too"); assert(a->link() != NULL && a->link()->free(), "must be free too");
@ -353,13 +370,20 @@ void CodeHeap::merge_right(FreeBlock *a) {
// Update find_start map // Update find_start map
size_t beg = segment_for(a); size_t beg = segment_for(a);
mark_segmap_as_used(beg, beg + a->length()); mark_segmap_as_used(beg, beg + a->length());
_freelist_length--;
return true;
} }
return false;
} }
void CodeHeap::add_to_freelist(HeapBlock* a) { void CodeHeap::add_to_freelist(HeapBlock* a) {
FreeBlock* b = (FreeBlock*)a; FreeBlock* b = (FreeBlock*)a;
_freelist_length++;
assert(b != _freelist, "cannot be removed twice"); assert(b != _freelist, "cannot be removed twice");
// Mark as free and update free space count // Mark as free and update free space count
_freelist_segments += b->length(); _freelist_segments += b->length();
b->set_free(); b->set_free();
@ -371,95 +395,96 @@ void CodeHeap::add_to_freelist(HeapBlock *a) {
return; return;
} }
// Scan for right place to put into list. List // Since the freelist is ordered (smaller addresses -> larger addresses) and the
// is sorted by increasing addresses // element we want to insert into the freelist has a smaller address than the first
FreeBlock* prev = NULL; // element, we can simply add 'b' as the first element and we are done.
FreeBlock* cur = _freelist; if (b < _freelist) {
while(cur != NULL && cur < b) {
assert(prev == NULL || prev < cur, "must be ordered");
prev = cur;
cur = cur->link();
}
assert( (prev == NULL && b < _freelist) ||
(prev < b && (cur == NULL || b < cur)), "list must be ordered");
if (prev == NULL) {
// Insert first in list // Insert first in list
b->set_link(_freelist); b->set_link(_freelist);
_freelist = b; _freelist = b;
merge_right(_freelist); merge_right(_freelist);
} else { return;
}
// Scan for right place to put into list. List
// is sorted by increasing addresses
FreeBlock* prev = _freelist;
FreeBlock* cur = _freelist->link();
while(cur != NULL && cur < b) {
assert(prev < cur, "Freelist must be ordered");
prev = cur;
cur = cur->link();
}
assert((prev < b) && (cur == NULL || b < cur), "free-list must be ordered");
insert_after(prev, b); insert_after(prev, b);
} }
}
// Search freelist for an entry on the list with the best fit /**
// Return NULL if no one was found * Search freelist for an entry on the list with the best fit.
* @return NULL, if no one was found
*/
FreeBlock* CodeHeap::search_freelist(size_t length, bool is_critical) { FreeBlock* CodeHeap::search_freelist(size_t length, bool is_critical) {
FreeBlock *best_block = NULL; FreeBlock* found_block = NULL;
FreeBlock *best_prev = NULL; FreeBlock* found_prev = NULL;
size_t best_length = 0; size_t found_length = 0;
// Search for smallest block which is bigger than length
FreeBlock* prev = NULL; FreeBlock* prev = NULL;
FreeBlock* cur = _freelist; FreeBlock* cur = _freelist;
while(cur != NULL) { const size_t critical_boundary = (size_t)high_boundary() - CodeCacheMinimumFreeSpace;
size_t l = cur->length();
if (l >= length && (best_block == NULL || best_length > l)) {
// Search for first block that fits
while(cur != NULL) {
if (cur->length() >= length) {
// Non critical allocations are not allowed to use the last part of the code heap. // Non critical allocations are not allowed to use the last part of the code heap.
if (!is_critical) { // Make sure the end of the allocation doesn't cross into the last part of the code heap.
// Make sure the end of the allocation doesn't cross into the last part of the code heap if (!is_critical && (((size_t)cur + length) > critical_boundary)) {
if (((size_t)cur + length) > ((size_t)high_boundary() - CodeCacheMinimumFreeSpace)) { // The freelist is sorted by address - if one fails, all consecutive will also fail.
// the freelist is sorted by address - if one fails, all consecutive will also fail.
break; break;
} }
} // Remember block, its previous element, and its length
found_block = cur;
found_prev = prev;
found_length = found_block->length();
// Remember best block, its previous element, and its length break;
best_block = cur;
best_prev = prev;
best_length = best_block->length();
} }
// Next element in list // Next element in list
prev = cur; prev = cur;
cur = cur->link(); cur = cur->link();
} }
if (best_block == NULL) { if (found_block == NULL) {
// None found // None found
return NULL; return NULL;
} }
assert((best_prev == NULL && _freelist == best_block ) ||
(best_prev != NULL && best_prev->link() == best_block), "sanity check");
// Exact (or at least good enough) fit. Remove from list. // Exact (or at least good enough) fit. Remove from list.
// Don't leave anything on the freelist smaller than CodeCacheMinBlockLength. // Don't leave anything on the freelist smaller than CodeCacheMinBlockLength.
if (best_length < length + CodeCacheMinBlockLength) { if (found_length - length < CodeCacheMinBlockLength) {
length = best_length; _freelist_length--;
if (best_prev == NULL) { length = found_length;
assert(_freelist == best_block, "sanity check"); if (found_prev == NULL) {
assert(_freelist == found_block, "sanity check");
_freelist = _freelist->link(); _freelist = _freelist->link();
} else { } else {
assert((found_prev->link() == found_block), "sanity check");
// Unmap element // Unmap element
best_prev->set_link(best_block->link()); found_prev->set_link(found_block->link());
} }
} else { } else {
// Truncate block and return a pointer to the following block // Truncate block and return a pointer to the following block
best_block->set_length(best_length - length);
best_block = following_block(best_block);
// Set used bit and length on new block // Set used bit and length on new block
size_t beg = segment_for(best_block); found_block->set_length(found_length - length);
found_block = following_block(found_block);
size_t beg = segment_for(found_block);
mark_segmap_as_used(beg, beg + length); mark_segmap_as_used(beg, beg + length);
best_block->set_length(length); found_block->set_length(length);
} }
best_block->set_used(); found_block->set_used();
_freelist_segments -= length; _freelist_segments -= length;
return best_block; return found_block;
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -471,20 +496,25 @@ void CodeHeap::print() {
tty->print_cr("The Heap"); tty->print_cr("The Heap");
} }
#endif
void CodeHeap::verify() { void CodeHeap::verify() {
// Count the number of blocks on the freelist, and the amount of space if (VerifyCodeCache) {
// represented.
int count = 0;
size_t len = 0; size_t len = 0;
int count = 0;
for(FreeBlock* b = _freelist; b != NULL; b = b->link()) { for(FreeBlock* b = _freelist; b != NULL; b = b->link()) {
len += b->length(); len += b->length();
count++; count++;
// Check if we have merged all free blocks
assert(merge_right(b) == false, "Missed merging opportunity");
} }
// Verify that freelist contains the right amount of free space // Verify that freelist contains the right amount of free space
// guarantee(len == _freelist_segments, "wrong freelist"); assert(len == _freelist_segments, "wrong freelist");
for(HeapBlock* h = first_block(); h != NULL; h = next_block(h)) {
if (h->free()) count--;
}
// Verify that the freelist contains the same number of blocks
// than free blocks found on the full list.
assert(count == 0, "missing free blocks");
// Verify that the number of free blocks is not out of hand. // Verify that the number of free blocks is not out of hand.
static int free_block_threshold = 10000; static int free_block_threshold = 10000;
@ -493,11 +523,7 @@ void CodeHeap::verify() {
// Double the warning limit // Double the warning limit
free_block_threshold *= 2; free_block_threshold *= 2;
} }
}
}
// Verify that the freelist contains the same number of free blocks that is #endif
// found on the full list.
for(HeapBlock *h = first_block(); h != NULL; h = next_block(h)) {
if (h->free()) count--;
}
// guarantee(count == 0, "missing free blocks");
}

View file

@ -92,12 +92,16 @@ class CodeHeap : public CHeapObj<mtCode> {
FreeBlock* _freelist; FreeBlock* _freelist;
size_t _freelist_segments; // No. of segments in freelist size_t _freelist_segments; // No. of segments in freelist
int _freelist_length;
enum { free_sentinel = 0xFF };
// Helper functions // Helper functions
size_t size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; } size_t size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; }
size_t segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; } size_t segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; }
size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; } size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; }
bool is_segment_unused(int val) const { return val == free_sentinel; }
HeapBlock* block_at(size_t i) const { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); } HeapBlock* block_at(size_t i) const { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); }
void mark_segmap_as_free(size_t beg, size_t end); void mark_segmap_as_free(size_t beg, size_t end);
@ -106,7 +110,7 @@ class CodeHeap : public CHeapObj<mtCode> {
// Freelist management helpers // Freelist management helpers
FreeBlock* following_block(FreeBlock* b); FreeBlock* following_block(FreeBlock* b);
void insert_after(FreeBlock* a, FreeBlock* b); void insert_after(FreeBlock* a, FreeBlock* b);
void merge_right (FreeBlock* a); bool merge_right (FreeBlock* a);
// Toplevel freelist management // Toplevel freelist management
void add_to_freelist(HeapBlock* b); void add_to_freelist(HeapBlock* b);
@ -120,16 +124,14 @@ class CodeHeap : public CHeapObj<mtCode> {
// to perform additional actions on creation of executable code // to perform additional actions on creation of executable code
void on_code_mapping(char* base, size_t size); void on_code_mapping(char* base, size_t size);
void clear(); // clears all heap contents
public: public:
CodeHeap(); CodeHeap();
// Heap extents // Heap extents
bool reserve(size_t reserved_size, size_t committed_size, size_t segment_size); bool reserve(size_t reserved_size, size_t committed_size, size_t segment_size);
void release(); // releases all allocated memory
bool expand_by(size_t size); // expands committed memory by size bool expand_by(size_t size); // expands committed memory by size
void shrink_by(size_t size); // shrinks committed memory by size
void clear(); // clears all heap contents
// Memory allocation // Memory allocation
void* allocate (size_t size, bool is_critical); // allocates a block of size or returns NULL void* allocate (size_t size, bool is_critical); // allocates a block of size or returns NULL
@ -146,7 +148,8 @@ class CodeHeap : public CHeapObj<mtCode> {
size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit
static size_t header_size(); // returns the header size for each heap block static size_t header_size(); // returns the header size for each heap block
// Iteration size_t allocated_in_freelist() const { return _freelist_segments * CodeCacheSegmentSize; }
int freelist_length() const { return _freelist_length; } // number of elements in the freelist
// returns the first block or NULL // returns the first block or NULL
void* first() const { return next_free(first_block()); } void* first() const { return next_free(first_block()); }
@ -156,6 +159,7 @@ class CodeHeap : public CHeapObj<mtCode> {
// Statistics // Statistics
size_t capacity() const; size_t capacity() const;
size_t max_capacity() const; size_t max_capacity() const;
int allocated_segments() const;
size_t allocated_capacity() const; size_t allocated_capacity() const;
size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); } size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); }
@ -164,7 +168,7 @@ private:
public: public:
// Debugging // Debugging
void verify(); void verify() PRODUCT_RETURN;
void print() PRODUCT_RETURN; void print() PRODUCT_RETURN;
}; };

View file

@ -2407,6 +2407,8 @@ bool Arguments::check_vm_args_consistency() {
status &= verify_interval(NmethodSweepFraction, 1, ReservedCodeCacheSize/K, "NmethodSweepFraction"); status &= verify_interval(NmethodSweepFraction, 1, ReservedCodeCacheSize/K, "NmethodSweepFraction");
status &= verify_interval(NmethodSweepActivity, 0, 2000, "NmethodSweepActivity"); status &= verify_interval(NmethodSweepActivity, 0, 2000, "NmethodSweepActivity");
status &= verify_interval(CodeCacheMinBlockLength, 1, 100, "CodeCacheMinBlockLength");
status &= verify_interval(CodeCacheSegmentSize, 1, 1024, "CodeCacheSegmentSize");
// TieredCompilation needs at least 2 compiler threads. // TieredCompilation needs at least 2 compiler threads.
const int num_min_compiler_threads = (TieredCompilation && (TieredStopAtLevel >= CompLevel_full_optimization)) ? 2 : 1; const int num_min_compiler_threads = (TieredCompilation && (TieredStopAtLevel >= CompLevel_full_optimization)) ? 2 : 1;

View file

@ -814,8 +814,8 @@ class CommandLineFlags {
product(bool, PrintOopAddress, false, \ product(bool, PrintOopAddress, false, \
"Always print the location of the oop") \ "Always print the location of the oop") \
\ \
notproduct(bool, VerifyCodeCacheOften, false, \ notproduct(bool, VerifyCodeCache, false, \
"Verify compiled-code cache often") \ "Verify code cache on memory allocation/deallocation") \
\ \
develop(bool, ZapDeadCompiledLocals, false, \ develop(bool, ZapDeadCompiledLocals, false, \
"Zap dead locals in compiler frames") \ "Zap dead locals in compiler frames") \
@ -3296,8 +3296,8 @@ class CommandLineFlags {
"disable this feature") \ "disable this feature") \
\ \
/* code cache parameters */ \ /* code cache parameters */ \
/* ppc64 has large code-entry alignment. */ \ /* ppc64/tiered compilation has large code-entry alignment. */ \
develop(uintx, CodeCacheSegmentSize, 64 PPC64_ONLY(+64), \ develop(uintx, CodeCacheSegmentSize, 64 PPC64_ONLY(+64) NOT_PPC64(TIERED_ONLY(+64)),\
"Code cache segment size (in bytes) - smallest unit of " \ "Code cache segment size (in bytes) - smallest unit of " \
"allocation") \ "allocation") \
\ \