open Udiff src/hotspot/share/gc/shared/oopStorage.cpp (original) (raw)
rev 49824 : imported patch block_array rev 49826 : [mq]: active_array
@@ -26,24 +26,26 @@ #include "gc/shared/oopStorage.inline.hpp" #include "gc/shared/oopStorageParState.inline.hpp" #include "logging/log.hpp" #include "logging/logStream.hpp" #include "memory/allocation.inline.hpp" -#include "memory/resourceArea.hpp" #include "runtime/atomic.hpp" +#include "runtime/globals.hpp" #include "runtime/handles.inline.hpp" #include "runtime/mutex.hpp" #include "runtime/mutexLocker.hpp" #include "runtime/orderAccess.inline.hpp" #include "runtime/safepoint.hpp" #include "runtime/stubRoutines.hpp" +#include "runtime/thread.hpp" #include "utilities/align.hpp" #include "utilities/count_trailing_zeros.hpp" #include "utilities/debug.hpp" #include "utilities/globalDefinitions.hpp" #include "utilities/macros.hpp" #include "utilities/ostream.hpp" +#include "utilities/spinYield.hpp"
OopStorage::BlockEntry::BlockEntry() : _prev(NULL), _next(NULL) {}
OopStorage::BlockEntry::~BlockEntry() { assert(_prev == NULL, "deleting attached block");
@@ -106,10 +108,98 @@ _get_entry(*next_blk)._prev = prev_blk; _get_entry(*prev_blk)._next = next_blk; } }
+OopStorage::BlockArray::BlockArray(size_t size) :
- _size(size),
- _block_count(0),
- _refcount(0) +{}
- +OopStorage::BlockArray::~BlockArray() {
- assert(_refcount == 0, "precondition"); +}
- +OopStorage::BlockArray* OopStorage::BlockArray::create(size_t size) {
- size_t size_in_bytes = blocks_offset() + sizeof(Block*) * size;
- void* mem = NEW_C_HEAP_ARRAY_RETURN_NULL(char, size_in_bytes, mtGC);
- if (mem == NULL) return NULL;
- return new (mem) BlockArray(size); +}
- +void OopStorage::BlockArray::destroy(BlockArray* ba) {
- ba->~BlockArray();
- FREE_C_HEAP_ARRAY(char, ba); +}
- +size_t OopStorage::BlockArray::size() const {
- return _size; +}
- +size_t OopStorage::BlockArray::block_count() const {
- return _block_count; +}
- +size_t OopStorage::BlockArray::block_count_acquire() const {
- return OrderAccess::load_acquire(&_block_count); +}
- +void OopStorage::BlockArray::set_block_count(size_t value) {
- _block_count = value; +}
- +void OopStorage::BlockArray::increment_refcount() const {
- int new_value = Atomic::add(1, &_refcount);
- assert(new_value >= 1, "negative refcount %d", new_value - 1); +}
- +bool OopStorage::BlockArray::decrement_refcount() const {
- int new_value = Atomic::sub(1, &_refcount);
- assert(new_value >= 0, "negative refcount %d", new_value);
- return new_value == 0; +}
- +bool OopStorage::BlockArray::push(Block* block) {
- size_t index = _block_count;
- if (index < _size) {
- block->set_active_index(index);
- *block_ptr(index) = block;
- // Use a release_store to ensure all the setup is complete before
- // making the block visible.
- OrderAccess::release_store(&_block_count, index + 1);
- return true;
- } else {
- return false;
- } +}
- +void OopStorage::BlockArray::remove(Block* block) {
- assert(_block_count > 0, "array is empty");
- size_t index = block->active_index();
- assert(*block_ptr(index) == block, "block not present");
- size_t last_index = _block_count - 1;
- Block* last_block = *block_ptr(last_index);
- last_block->set_active_index(index);
- *block_ptr(index) = last_block;
- set_block_count(last_index); +}
- +void OopStorage::BlockArray::copy_from(const BlockArray* from) {
- assert(block_count() == 0, "array must be empty");
- size_t count = from->block_count();
- assert(count <= _size, "precondition");
- Block* const* from_ptr = from->block_ptr(0);
- Block** to_ptr = block_ptr(0);
- for (size_t i = 0; i < count; ++i) {
- Block* block = *from_ptr++;
- assert(block->active_index() == i, "invariant");
- *to_ptr++ = block;
- }
- set_block_count(count); +}
- // Blocks start with an array of BitsPerWord oop entries. That array // is divided into conceptual BytesPerWord sections of BitsPerByte // entries. Blocks are allocated aligned on section boundaries, for // the convenience of mapping from an entry to the containing block; // see block_for_ptr(). Aligning on section boundary rather than on
@@ -123,11 +213,11 @@ OopStorage::Block::Block(const OopStorage* owner, void* memory) : _data(), _allocated_bitmask(0), _owner(owner), _memory(memory),
- _active_entry(),
- _active_index(0), _allocate_entry(), _deferred_updates_next(NULL), _release_refcount(0) { STATIC_ASSERT(_data_pos == 0);
@@ -144,14 +234,10 @@ // might help catch bugs. Volatile to prevent dead-store elimination. const_cast<uintx volatile&>(_allocated_bitmask) = 0; const_cast<OopStorage* volatile&>(_owner) = NULL; } -const OopStorage::BlockEntry& OopStorage::Block::get_active_entry(const Block& block) { - return block._active_entry; -}
const OopStorage::BlockEntry& OopStorage::Block::get_allocate_entry(const Block& block) { return block._allocate_entry; }
size_t OopStorage::Block::allocation_size() {
@@ -202,10 +288,24 @@ bool OopStorage::Block::contains(const oop* ptr) const { const oop* base = get_pointer(0); return (base <= ptr) && (ptr < (base + ARRAY_SIZE(_data))); } +size_t OopStorage::Block::active_index() const { + return _active_index; +} + +void OopStorage::Block::set_active_index(size_t index) { + _active_index = index; +} + +size_t OopStorage::Block::active_index_safe(const Block* block) { + STATIC_ASSERT(sizeof(intptr_t) == sizeof(block->_active_index)); + assert(CanUseSafeFetchN(), "precondition"); + return SafeFetchN((intptr_t*)&block->_active_index, 0); +} + unsigned OopStorage::Block::get_index(const oop* ptr) const { assert(contains(ptr), PTR_FORMAT " not in block " PTR_FORMAT, p2i(ptr), p2i(this)); return static_cast(ptr - get_pointer(0)); }
@@ -244,11 +344,11 @@ FREE_C_HEAP_ARRAY(char, memory); }
// This can return a false positive if ptr is not contained by some // block. For some uses, it is a precondition that ptr is valid, -// e.g. contained in some block in owner's _active_list. Other uses +// e.g. contained in some block in owner's _active_array. Other uses // require additional validation of the result. OopStorage::Block* OopStorage::Block::block_for_ptr(const OopStorage* owner, const oop* ptr) { assert(CanUseSafeFetchN(), "precondition"); STATIC_ASSERT(_data_pos == 0);
@@ -278,16 +378,16 @@ // Allocation // // Allocation involves the _allocate_list, which contains a subset of the // blocks owned by a storage object. This is a doubly-linked list, linked // through dedicated fields in the blocks. Full blocks are removed from this -// list, though they are still present in the _active_list. Empty blocks are +// list, though they are still present in the _active_array. Empty blocks are // kept at the end of the _allocate_list, to make it easy for empty block // deletion to find them. // // allocate(), and delete_empty_blocks_concurrent() lock the -// _allocate_mutex while performing any list modifications. +// _allocate_mutex while performing any list and array modifications. // // allocate() and release() update a block's _allocated_bitmask using CAS // loops. This prevents loss of updates even though release() performs // its updates without any locking. //
@@ -297,11 +397,11 @@ // removed from the _allocate_list so it won't be considered by future // allocations until some entries in it are released. // // release() is performed lock-free. release() first looks up the block for // the entry, using address alignment to find the enclosing block (thereby -// avoiding iteration over the _active_list). Once the block has been +// avoiding iteration over the _active_array). Once the block has been // determined, its _allocated_bitmask needs to be updated, and its position in // the _allocate_list may need to be updated. There are two cases: // // (a) If the block is neither full nor would become empty with the release of // the entry, only its _allocated_bitmask needs to be updated. But if the CAS
@@ -338,29 +438,33 @@ while (_allocate_list.head() == NULL) { if (!reduce_deferred_updates()) { // Failed to make new block, no other thread made a block // available while the mutex was released, and didn't get // one from a deferred update either, so return failure.
log_info(oopstorage, ref)("%s: failed allocation", name());
log_info(oopstorage, ref)("%s: failed block allocation", name()); return NULL; } }
} else { // Add new block to storage. log_info(oopstorage, blocks)("%s: new block " PTR_FORMAT, name(), p2i(block));
// Add new block to the _active_array, growing if needed.
if (!_active_array->push(block)) {
if (expand_active_array()) {
guarantee(_active_array->push(block), "push failed after expansion");
} else {
log_info(oopstorage, blocks)("%s: failed active array expand", name());
Block::delete_block(*block);
return NULL;
}
} // Add to end of _allocate_list. The mutex release allowed // other threads to add blocks to the _allocate_list. We prefer // to allocate from non-empty blocks, to allow empty blocks to // be deleted. _allocate_list.push_back(*block);
// Add to front of _active_list, and then record as the head
// block, for concurrent iteration protocol.
_active_list.push_front(*block);
++_block_count;
// Ensure all setup of block is complete before making it visible.
} block = _allocate_list.head(); } // Allocate from first block. assert(block != NULL, "invariant");OrderAccess::release_store(&_active_head, block);
@@ -381,20 +485,122 @@ } log_info(oopstorage, ref)("%s: allocated " PTR_FORMAT, name(), p2i(result)); return result; }
+bool OopStorage::expand_active_array() {
assert_lock_strong(_allocate_mutex);
BlockArray* old_array = _active_array;
size_t new_size = 2 * old_array->size();
log_info(oopstorage, blocks)("%s: expand active array " SIZE_FORMAT,
name(), new_size);
BlockArray* new_array = BlockArray::create(new_size);
if (new_array == NULL) return false;
new_array->copy_from(old_array);
replace_active_array(new_array);
relinquish_block_array(old_array);
return true; +}
+OopStorage::ProtectActive::ProtectActive() : _enter(0), _exit() {}
+// Begin read-side critical section. +uint OopStorage::ProtectActive::read_enter() {
return Atomic::add(2u, &_enter); +}
+// End read-side critical section. +void OopStorage::ProtectActive::read_exit(uint enter_value) {
Atomic::add(2u, &_exit[enter_value & 1]); +}
+// Wait until all readers that entered the critical section before +// synchronization have exited that critical section. +void OopStorage::ProtectActive::write_synchronize() {
SpinYield spinner;
// Determine old and new exit counters, based on bit0 of the
// on-entry _enter counter.
uint value = OrderAccess::load_acquire(&_enter);
volatile uint* new_ptr = &_exit[(value + 1) & 1];
// Atomically change the in-use exit counter to the new counter, by
// adding 1 to the _enter counter (flipping bit0 between 0 and 1)
// and initializing the new exit counter to that enter value. Note:
// The new exit counter is not being used by read operations until
// this change succeeds.
uint old;
do {
old = value;
*new_ptr = ++value;
value = Atomic::cmpxchg(value, &_enter, old);
} while (old != value);
// Readers that entered the critical section before we changed the
// selected exit counter will use the old exit counter. Readers
// entering after the change will use the new exit counter. Wait
// for all the critical sections started before the change to
// complete, e.g. for the value of old_ptr to catch up with old.
volatile uint* old_ptr = &_exit[old & 1];
while (old != OrderAccess::load_acquire(old_ptr)) {
spinner.wait();
} +}
+void OopStorage::replace_active_array(BlockArray* new_array) {
// Caller has the old array that is the current value of _active_array.
new_array->increment_refcount();
OrderAccess::release_store(&_active_array, new_array);
// Wait for any readers that could read the old array from _active_array.
_protect_active.write_synchronize();
// All obtain critical sections that could see the old array have
// completed, having incremented the refcount of the old array. The
// caller can now safely relinquish the old array. +}
+OopStorage::BlockArray* OopStorage::obtain_active_array() const {
uint enter_value = _protect_active.read_enter();
// Safely (because protected) get the array and increment its
// refcount. Must relinquish the array when done using it.
BlockArray* result = OrderAccess::load_acquire(&_active_array);
result->increment_refcount();
_protect_active.read_exit(enter_value);
return result; +}
+void OopStorage::relinquish_block_array(BlockArray* array) const {
if (array->decrement_refcount()) {
assert(array != _active_array, "invariant");
BlockArray::destroy(array);
} +}
+class OopStorage::WithActiveArray : public StackObj {
const OopStorage* _storage;
BlockArray* _active_array;
+public:
WithActiveArray(const OopStorage* storage) :
_storage(storage),
_active_array(storage->obtain_active_array())
{}
~WithActiveArray() {
_storage->relinquish_block_array(_active_array);
}
BlockArray& active_array() const {
return *_active_array;
} +};
OopStorage::Block* OopStorage::find_block_or_null(const oop* ptr) const { assert(ptr != NULL, "precondition"); return Block::block_for_ptr(this, ptr); }
static void log_release_transitions(uintx releasing, uintx old_allocated, const OopStorage* owner, const void* block) {
- ResourceMark rm; Log(oopstorage, blocks) log; LogStream ls(log.debug()); if (is_full_bitmask(old_allocated)) { ls.print_cr("%s: block not full " PTR_FORMAT, owner->name(), p2i(block)); }
@@ -544,24 +750,25 @@ char* dup = NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtGC); strcpy(dup, name); return dup; }
+const size_t initial_active_array_size = 8; + OopStorage::OopStorage(const char* name, Mutex* allocate_mutex, Mutex* active_mutex) : _name(dup_name(name)),
- _active_list(&Block::get_active_entry),
- _active_array(BlockArray::create(initial_active_array_size)), _allocate_list(&Block::get_allocate_entry),
- _active_head(NULL), _deferred_updates(NULL), _allocate_mutex(allocate_mutex), _active_mutex(active_mutex), _allocation_count(0),
- _block_count(0), _concurrent_iteration_active(false) {
- _active_array->increment_refcount(); assert(_active_mutex->rank() < _allocate_mutex->rank(), "%s: active_mutex must have lower rank than allocate_mutex", _name); assert(_active_mutex->_safepoint_check_required != Mutex::_safepoint_check_always, "%s: active mutex requires safepoint check", _name); assert(_allocate_mutex->_safepoint_check_required != Mutex::_safepoint_check_always,
@@ -581,14 +788,17 @@ block->set_deferred_updates_next(NULL); } while ((block = _allocate_list.head()) != NULL) { _allocate_list.unlink(*block); }
- while ((block = _active_list.head()) != NULL) {
- _active_list.unlink(*block);
- bool unreferenced = _active_array->decrement_refcount();
- assert(unreferenced, "deleting storage while _active_array is referenced");
- for (size_t i = _active_array->block_count(); 0 < i; ) {
- block = _active_array->at(--i); Block::delete_block(*block); }
- BlockArray::destroy(_active_array); FREE_C_HEAP_ARRAY(char, _name); }
void OopStorage::delete_empty_blocks_safepoint() { assert_at_safepoint();
@@ -596,36 +806,33 @@ // blocks available for deletion. while (reduce_deferred_updates()) {} // Don't interfere with a concurrent iteration. if (_concurrent_iteration_active) return; // Delete empty (and otherwise deletable) blocks from end of _allocate_list.
- for (const Block* block = _allocate_list.ctail();
- for (Block* block = _allocate_list.tail(); (block != NULL) && block->is_deletable();
block = _allocate_list.ctail()) {
- _active_list.unlink(*block);
block = _allocate_list.tail()) {
- _active_array->remove(block); _allocate_list.unlink(*block); delete_empty_block(*block);
- --_block_count;
}
- // Update _active_head, in case current value was in deleted set.
- _active_head = _active_list.head(); }
void OopStorage::delete_empty_blocks_concurrent() { MutexLockerEx ml(_allocate_mutex, Mutex::_no_safepoint_check_flag); // Other threads could be adding to the empty block count while we // release the mutex across the block deletions. Set an upper bound // on how many blocks we'll try to release, so other threads can't // cause an unbounded stay in this function.
- size_t limit = _block_count;
size_t limit = block_count();
for (size_t i = 0; i < limit; ++i) { // Additional updates might become available while we dropped the // lock. But limit number processed to limit lock duration. reduce_deferred_updates();
- const Block* block = _allocate_list.ctail();
- Block* block = _allocate_list.tail(); if ((block == NULL) || !block->is_deletable()) { // No block to delete, so done. There could be more pending // deferred updates that could give us more work to do; deal with // that in some later call, to limit lock duration here. return;
@@ -633,16 +840,11 @@
{
MutexLockerEx aml(_active_mutex, Mutex::_no_safepoint_check_flag);
// Don't interfere with a concurrent iteration.
if (_concurrent_iteration_active) return;
// Remove block from _active_list, updating head if needed.
_active_list.unlink(*block);
--_block_count;
if (block == _active_head) {
_active_head = _active_list.head();
}
} // Remove block from _allocate_list and delete it. _allocate_list.unlink(*block); // Release mutex while deleting block. MutexUnlockerEx ul(_allocate_mutex, Mutex::_no_safepoint_check_flag);_active_array->remove(block);
@@ -651,57 +853,78 @@ }
OopStorage::EntryStatus OopStorage::allocation_status(const oop* ptr) const { const Block* block = find_block_or_null(ptr); if (block != NULL) {
- // Verify block is a real block. For now, simple linear search.
- // Do something more clever if this is a performance bottleneck.
- // Prevent block deletion and _active_array modification. MutexLockerEx ml(_allocate_mutex, Mutex::_no_safepoint_check_flag);
- for (const Block* check_block = _active_list.chead();
check_block != NULL;
check_block = _active_list.next(*check_block)) {
if (check_block == block) {
- // Block could be a false positive, so get index carefully.
- size_t index = Block::active_index_safe(block);
- if ((index < _active_array->block_count()) &&
(block == _active_array->at(index)) &&
}block->contains(ptr)) { if ((block->allocated_bitmask() & block->bitmask_for_entry(ptr)) != 0) { return ALLOCATED_ENTRY; } else { return UNALLOCATED_ENTRY; } }
- } return INVALID_ENTRY; }
size_t OopStorage::allocation_count() const { return _allocation_count; }
size_t OopStorage::block_count() const {
- return _block_count;
- WithActiveArray wab(this);
- return wab.active_array().block_count_acquire(); }
size_t OopStorage::total_memory_usage() const { size_t total_size = sizeof(OopStorage); total_size += strlen(name()) + 1;
- total_size += block_count() * Block::allocation_size();
- total_size += sizeof(BlockArray);
- WithActiveArray wab(this);
- const BlockArray& blocks = wab.active_array();
- total_size += blocks.block_count_acquire() * Block::allocation_size();
- total_size += blocks.size() * sizeof(Block*); return total_size; }
// Parallel iteration support
-static char* not_started_marker_dummy = NULL; -static void* const not_started_marker = ¬_started_marker_dummy; +uint OopStorage::BasicParState::default_estimated_thread_count(bool concurrent) {
- return concurrent ? ConcGCThreads : ParallelGCThreads; +}
- +OopStorage::BasicParState::IterationData::IterationData() :
- _segment_start(0),
- _segment_end(0),
- _processed(0) +{}
-OopStorage::BasicParState::BasicParState(OopStorage* storage, bool concurrent) : +OopStorage::BasicParState::BasicParState(const OopStorage* storage,
uint estimated_thread_count,
_storage(storage),bool concurrent) :
- _next_block(not_started_marker),
- _active_array(_storage->obtain_active_array()),
- _block_count(0), // initialized properly below
- _next_block(0),
- _estimated_thread_count(estimated_thread_count), _concurrent(concurrent) {
- assert(estimated_thread_count > 0, "estimated thread count must be positive"); update_iteration_state(true);
- // Get the block count after iteration state updated, so concurrent
- // empty block deletion is suppressed and can't reduce the count.
- _block_count = _active_array->block_count_acquire(); }
OopStorage::BasicParState::~BasicParState() {
- _storage->relinquish_block_array(_active_array); update_iteration_state(false); }
void OopStorage::BasicParState::update_iteration_state(bool value) { if (_concurrent) {
@@ -709,42 +932,62 @@ assert(_storage->_concurrent_iteration_active != value, "precondition"); _storage->_concurrent_iteration_active = value; } } -void OopStorage::BasicParState::ensure_iteration_started() { - if (!_concurrent) { - assert_at_safepoint(); - } - assert(!_concurrent || _storage->_concurrent_iteration_active, "invariant"); - // Ensure _next_block is not the not_started_marker, setting it to - // the _active_head to start the iteration if necessary. - if (OrderAccess::load_acquire(&_next_block) == not_started_marker) { - Atomic::cmpxchg(_storage->_active_head, &_next_block, not_started_marker); - } - assert(_next_block != not_started_marker, "postcondition"); -}
-OopStorage::Block* OopStorage::BasicParState::claim_next_block() {
- assert(_next_block != not_started_marker, "Iteration not started");
- void* next = _next_block;
- while (next != NULL) {
- void* new_next = _storage->_active_list.next(*static_cast<Block*>(next));
- void* fetched = Atomic::cmpxchg(new_next, &_next_block, next);
- if (fetched == next) break; // Claimed.
- next = fetched;
+bool OopStorage::BasicParState::claim_next_segment(IterationData* data) {
- data->note_processed_segment();
- size_t start = OrderAccess::load_acquire(&_next_block);
- if (start >= _block_count) {
- return finish_iteration(data); // No more blocks available.
- }
- // Try to claim several at a time, but not too many. We want to
- // avoid deciding there are many available and selecting a large
- // quantity, get delayed, and then end up claiming most or all of
- // the remaining largish amount of work, leaving nothing for other
- // threads to do. But too small a step can lead to contention
- // over _next_block, esp. when the work per block is small.
- size_t max_step = 10;
- size_t remaining = _block_count - start;
- size_t step = MIN2(max_step, 1 + (remaining / _estimated_thread_count));
- // Atomic::add with possible overshoot. This can perform better
- // than a CAS loop on some platforms when there is contention.
- // We can cope with the uncertainty by recomputing start/end from
- // the result of the add, and dealing with potential overshoot.
- size_t end = Atomic::add(step, &_next_block);
- // _next_block may have changed, so recompute start from result of add.
- start = end - step;
- // _next_block may have changed so much that end has overshot.
- end = MIN2(end, _block_count);
- // _next_block may have changed so much that even start has overshot.
- if (start < _block_count) {
- // Record claimed segment for iteration.
- data->set_segment_start(start);
- data->set_segment_end(end);
- return true; // Success.
- } else {
- // No more blocks to claim.
- return finish_iteration(data);
}
- return static_cast<Block*>(next); +}
- +bool OopStorage::BasicParState::finish_iteration(const IterationData* data) const {
- log_debug(oopstorage, blocks, stats)
("Parallel iteration on %s: blocks = " SIZE_FORMAT
", processed = " SIZE_FORMAT " (%2.f%%)",
_storage->name(), _block_count, data->processed(),
percent_of(data->processed(), _block_count));
- return false; }
const char* OopStorage::name() const { return _name; }
#ifndef PRODUCT
void OopStorage::print_on(outputStream* st) const { size_t allocations = _allocation_count;
- size_t blocks = _block_count;
size_t blocks = _active_array->block_count();
double data_size = section_size * section_count; double alloc_percentage = percent_of((double)allocations, blocks * data_size);
st->print("%s: " SIZE_FORMAT " entries in " SIZE_FORMAT " blocks (%.F%%), " SIZE_FORMAT " bytes",