1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   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  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/oopStorage.inline.hpp"
  27 #include "gc/shared/oopStorageParState.inline.hpp"
  28 #include "logging/log.hpp"
  29 #include "logging/logStream.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "runtime/atomic.hpp"
  32 #include "runtime/globals.hpp"
  33 #include "runtime/handles.inline.hpp"
  34 #include "runtime/interfaceSupport.inline.hpp"
  35 #include "runtime/mutex.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/orderAccess.hpp"
  38 #include "runtime/safepoint.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "runtime/thread.hpp"
  41 #include "utilities/align.hpp"
  42 #include "utilities/count_trailing_zeros.hpp"
  43 #include "utilities/debug.hpp"
  44 #include "utilities/globalDefinitions.hpp"
  45 #include "utilities/macros.hpp"
  46 #include "utilities/ostream.hpp"
  47 
  48 OopStorage::AllocationListEntry::AllocationListEntry() : _prev(NULL), _next(NULL) {}
  49 
  50 OopStorage::AllocationListEntry::~AllocationListEntry() {
  51   assert(_prev == NULL, "deleting attached block");
  52   assert(_next == NULL, "deleting attached block");
  53 }
  54 
  55 OopStorage::AllocationList::AllocationList() : _head(NULL), _tail(NULL) {}
  56 
  57 OopStorage::AllocationList::~AllocationList() {
  58   // ~OopStorage() empties its lists before destroying them.
  59   assert(_head == NULL, "deleting non-empty block list");
  60   assert(_tail == NULL, "deleting non-empty block list");
  61 }
  62 
  63 void OopStorage::AllocationList::push_front(const Block& block) {
  64   const Block* old = _head;
  65   if (old == NULL) {
  66     assert(_tail == NULL, "invariant");
  67     _head = _tail = █
  68   } else {
  69     block.allocation_list_entry()._next = old;
  70     old->allocation_list_entry()._prev = █
  71     _head = █
  72   }
  73 }
  74 
  75 void OopStorage::AllocationList::push_back(const Block& block) {
  76   const Block* old = _tail;
  77   if (old == NULL) {
  78     assert(_head == NULL, "invariant");
  79     _head = _tail = █
  80   } else {
  81     old->allocation_list_entry()._next = █
  82     block.allocation_list_entry()._prev = old;
  83     _tail = █
  84   }
  85 }
  86 
  87 void OopStorage::AllocationList::unlink(const Block& block) {
  88   const AllocationListEntry& block_entry = block.allocation_list_entry();
  89   const Block* prev_blk = block_entry._prev;
  90   const Block* next_blk = block_entry._next;
  91   block_entry._prev = NULL;
  92   block_entry._next = NULL;
  93   if ((prev_blk == NULL) && (next_blk == NULL)) {
  94     assert(_head == &block, "invariant");
  95     assert(_tail == &block, "invariant");
  96     _head = _tail = NULL;
  97   } else if (prev_blk == NULL) {
  98     assert(_head == &block, "invariant");
  99     next_blk->allocation_list_entry()._prev = NULL;
 100     _head = next_blk;
 101   } else if (next_blk == NULL) {
 102     assert(_tail == &block, "invariant");
 103     prev_blk->allocation_list_entry()._next = NULL;
 104     _tail = prev_blk;
 105   } else {
 106     next_blk->allocation_list_entry()._prev = prev_blk;
 107     prev_blk->allocation_list_entry()._next = next_blk;
 108   }
 109 }
 110 
 111 OopStorage::ActiveArray::ActiveArray(size_t size) :
 112   _size(size),
 113   _block_count(0),
 114   _refcount(0)
 115 {}
 116 
 117 OopStorage::ActiveArray::~ActiveArray() {
 118   assert(_refcount == 0, "precondition");
 119 }
 120 
 121 OopStorage::ActiveArray* OopStorage::ActiveArray::create(size_t size, AllocFailType alloc_fail) {
 122   size_t size_in_bytes = blocks_offset() + sizeof(Block*) * size;
 123   void* mem = NEW_C_HEAP_ARRAY3(char, size_in_bytes, mtGC, CURRENT_PC, alloc_fail);
 124   if (mem == NULL) return NULL;
 125   return new (mem) ActiveArray(size);
 126 }
 127 
 128 void OopStorage::ActiveArray::destroy(ActiveArray* ba) {
 129   ba->~ActiveArray();
 130   FREE_C_HEAP_ARRAY(char, ba);
 131 }
 132 
 133 size_t OopStorage::ActiveArray::size() const {
 134   return _size;
 135 }
 136 
 137 size_t OopStorage::ActiveArray::block_count() const {
 138   return _block_count;
 139 }
 140 
 141 size_t OopStorage::ActiveArray::block_count_acquire() const {
 142   return OrderAccess::load_acquire(&_block_count);
 143 }
 144 
 145 void OopStorage::ActiveArray::increment_refcount() const {
 146   int new_value = Atomic::add(1, &_refcount);
 147   assert(new_value >= 1, "negative refcount %d", new_value - 1);
 148 }
 149 
 150 bool OopStorage::ActiveArray::decrement_refcount() const {
 151   int new_value = Atomic::sub(1, &_refcount);
 152   assert(new_value >= 0, "negative refcount %d", new_value);
 153   return new_value == 0;
 154 }
 155 
 156 bool OopStorage::ActiveArray::push(Block* block) {
 157   size_t index = _block_count;
 158   if (index < _size) {
 159     block->set_active_index(index);
 160     *block_ptr(index) = block;
 161     // Use a release_store to ensure all the setup is complete before
 162     // making the block visible.
 163     OrderAccess::release_store(&_block_count, index + 1);
 164     return true;
 165   } else {
 166     return false;
 167   }
 168 }
 169 
 170 void OopStorage::ActiveArray::remove(Block* block) {
 171   assert(_block_count > 0, "array is empty");
 172   size_t index = block->active_index();
 173   assert(*block_ptr(index) == block, "block not present");
 174   size_t last_index = _block_count - 1;
 175   Block* last_block = *block_ptr(last_index);
 176   last_block->set_active_index(index);
 177   *block_ptr(index) = last_block;
 178   _block_count = last_index;
 179 }
 180 
 181 void OopStorage::ActiveArray::copy_from(const ActiveArray* from) {
 182   assert(_block_count == 0, "array must be empty");
 183   size_t count = from->_block_count;
 184   assert(count <= _size, "precondition");
 185   Block* const* from_ptr = from->block_ptr(0);
 186   Block** to_ptr = block_ptr(0);
 187   for (size_t i = 0; i < count; ++i) {
 188     Block* block = *from_ptr++;
 189     assert(block->active_index() == i, "invariant");
 190     *to_ptr++ = block;
 191   }
 192   _block_count = count;
 193 }
 194 
 195 // Blocks start with an array of BitsPerWord oop entries.  That array
 196 // is divided into conceptual BytesPerWord sections of BitsPerByte
 197 // entries.  Blocks are allocated aligned on section boundaries, for
 198 // the convenience of mapping from an entry to the containing block;
 199 // see block_for_ptr().  Aligning on section boundary rather than on
 200 // the full _data wastes a lot less space, but makes for a bit more
 201 // work in block_for_ptr().
 202 
 203 const unsigned section_size = BitsPerByte;
 204 const unsigned section_count = BytesPerWord;
 205 const unsigned block_alignment = sizeof(oop) * section_size;
 206 
 207 OopStorage::Block::Block(const OopStorage* owner, void* memory) :
 208   _data(),
 209   _allocated_bitmask(0),
 210   _owner(owner),
 211   _memory(memory),
 212   _active_index(0),
 213   _allocation_list_entry(),
 214   _deferred_updates_next(NULL),
 215   _release_refcount(0)
 216 {
 217   STATIC_ASSERT(_data_pos == 0);
 218   STATIC_ASSERT(section_size * section_count == ARRAY_SIZE(_data));
 219   assert(offset_of(Block, _data) == _data_pos, "invariant");
 220   assert(owner != NULL, "NULL owner");
 221   assert(is_aligned(this, block_alignment), "misaligned block");
 222 }
 223 
 224 OopStorage::Block::~Block() {
 225   assert(_release_refcount == 0, "deleting block while releasing");
 226   assert(_deferred_updates_next == NULL, "deleting block with deferred update");
 227   // Clear fields used by block_for_ptr and entry validation, which
 228   // might help catch bugs.  Volatile to prevent dead-store elimination.
 229   const_cast<uintx volatile&>(_allocated_bitmask) = 0;
 230   const_cast<OopStorage* volatile&>(_owner) = NULL;
 231 }
 232 
 233 size_t OopStorage::Block::allocation_size() {
 234   // _data must be first member, so aligning Block aligns _data.
 235   STATIC_ASSERT(_data_pos == 0);
 236   return sizeof(Block) + block_alignment - sizeof(void*);
 237 }
 238 
 239 size_t OopStorage::Block::allocation_alignment_shift() {
 240   return exact_log2(block_alignment);
 241 }
 242 
 243 inline bool is_full_bitmask(uintx bitmask) { return ~bitmask == 0; }
 244 inline bool is_empty_bitmask(uintx bitmask) { return bitmask == 0; }
 245 
 246 bool OopStorage::Block::is_full() const {
 247   return is_full_bitmask(allocated_bitmask());
 248 }
 249 
 250 bool OopStorage::Block::is_empty() const {
 251   return is_empty_bitmask(allocated_bitmask());
 252 }
 253 
 254 uintx OopStorage::Block::bitmask_for_entry(const oop* ptr) const {
 255   return bitmask_for_index(get_index(ptr));
 256 }
 257 
 258 // An empty block is not yet deletable if either:
 259 // (1) There is a release() operation currently operating on it.
 260 // (2) It is in the deferred updates list.
 261 // For interaction with release(), these must follow the empty check,
 262 // and the order of these checks is important.
 263 bool OopStorage::Block::is_safe_to_delete() const {
 264   assert(is_empty(), "precondition");
 265   OrderAccess::loadload();
 266   return (OrderAccess::load_acquire(&_release_refcount) == 0) &&
 267          (OrderAccess::load_acquire(&_deferred_updates_next) == NULL);
 268 }
 269 
 270 OopStorage::Block* OopStorage::Block::deferred_updates_next() const {
 271   return _deferred_updates_next;
 272 }
 273 
 274 void OopStorage::Block::set_deferred_updates_next(Block* block) {
 275   _deferred_updates_next = block;
 276 }
 277 
 278 bool OopStorage::Block::contains(const oop* ptr) const {
 279   const oop* base = get_pointer(0);
 280   return (base <= ptr) && (ptr < (base + ARRAY_SIZE(_data)));
 281 }
 282 
 283 size_t OopStorage::Block::active_index() const {
 284   return _active_index;
 285 }
 286 
 287 void OopStorage::Block::set_active_index(size_t index) {
 288   _active_index = index;
 289 }
 290 
 291 size_t OopStorage::Block::active_index_safe(const Block* block) {
 292   STATIC_ASSERT(sizeof(intptr_t) == sizeof(block->_active_index));
 293   assert(CanUseSafeFetchN(), "precondition");
 294   return SafeFetchN((intptr_t*)&block->_active_index, 0);
 295 }
 296 
 297 unsigned OopStorage::Block::get_index(const oop* ptr) const {
 298   assert(contains(ptr), PTR_FORMAT " not in block " PTR_FORMAT, p2i(ptr), p2i(this));
 299   return static_cast<unsigned>(ptr - get_pointer(0));
 300 }
 301 
 302 oop* OopStorage::Block::allocate() {
 303   // Use CAS loop because release may change bitmask outside of lock.
 304   uintx allocated = allocated_bitmask();
 305   while (true) {
 306     assert(!is_full_bitmask(allocated), "attempt to allocate from full block");
 307     unsigned index = count_trailing_zeros(~allocated);
 308     uintx new_value = allocated | bitmask_for_index(index);
 309     uintx fetched = Atomic::cmpxchg(new_value, &_allocated_bitmask, allocated);
 310     if (fetched == allocated) {
 311       return get_pointer(index); // CAS succeeded; return entry for index.
 312     }
 313     allocated = fetched;       // CAS failed; retry with latest value.
 314   }
 315 }
 316 
 317 OopStorage::Block* OopStorage::Block::new_block(const OopStorage* owner) {
 318   // _data must be first member: aligning block => aligning _data.
 319   STATIC_ASSERT(_data_pos == 0);
 320   size_t size_needed = allocation_size();
 321   void* memory = NEW_C_HEAP_ARRAY_RETURN_NULL(char, size_needed, mtGC);
 322   if (memory == NULL) {
 323     return NULL;
 324   }
 325   void* block_mem = align_up(memory, block_alignment);
 326   assert(sizeof(Block) + pointer_delta(block_mem, memory, 1) <= size_needed,
 327          "allocated insufficient space for aligned block");
 328   return ::new (block_mem) Block(owner, memory);
 329 }
 330 
 331 void OopStorage::Block::delete_block(const Block& block) {
 332   void* memory = block._memory;
 333   block.Block::~Block();
 334   FREE_C_HEAP_ARRAY(char, memory);
 335 }
 336 
 337 // This can return a false positive if ptr is not contained by some
 338 // block.  For some uses, it is a precondition that ptr is valid,
 339 // e.g. contained in some block in owner's _active_array.  Other uses
 340 // require additional validation of the result.
 341 OopStorage::Block*
 342 OopStorage::Block::block_for_ptr(const OopStorage* owner, const oop* ptr) {
 343   assert(CanUseSafeFetchN(), "precondition");
 344   STATIC_ASSERT(_data_pos == 0);
 345   // Const-ness of ptr is not related to const-ness of containing block.
 346   // Blocks are allocated section-aligned, so get the containing section.
 347   oop* section_start = align_down(const_cast<oop*>(ptr), block_alignment);
 348   // Start with a guess that the containing section is the last section,
 349   // so the block starts section_count-1 sections earlier.
 350   oop* section = section_start - (section_size * (section_count - 1));
 351   // Walk up through the potential block start positions, looking for
 352   // the owner in the expected location.  If we're below the actual block
 353   // start position, the value at the owner position will be some oop
 354   // (possibly NULL), which can never match the owner.
 355   intptr_t owner_addr = reinterpret_cast<intptr_t>(owner);
 356   for (unsigned i = 0; i < section_count; ++i, section += section_size) {
 357     Block* candidate = reinterpret_cast<Block*>(section);
 358     intptr_t* candidate_owner_addr
 359       = reinterpret_cast<intptr_t*>(&candidate->_owner);
 360     if (SafeFetchN(candidate_owner_addr, 0) == owner_addr) {
 361       return candidate;
 362     }
 363   }
 364   return NULL;
 365 }
 366 
 367 //////////////////////////////////////////////////////////////////////////////
 368 // Allocation
 369 //
 370 // Allocation involves the _allocation_list, which contains a subset of the
 371 // blocks owned by a storage object.  This is a doubly-linked list, linked
 372 // through dedicated fields in the blocks.  Full blocks are removed from this
 373 // list, though they are still present in the _active_array.  Empty blocks are
 374 // kept at the end of the _allocation_list, to make it easy for empty block
 375 // deletion to find them.
 376 //
 377 // allocate(), and delete_empty_blocks() lock the
 378 // _allocation_mutex while performing any list and array modifications.
 379 //
 380 // allocate() and release() update a block's _allocated_bitmask using CAS
 381 // loops.  This prevents loss of updates even though release() performs
 382 // its updates without any locking.
 383 //
 384 // allocate() obtains the entry from the first block in the _allocation_list,
 385 // and updates that block's _allocated_bitmask to indicate the entry is in
 386 // use.  If this makes the block full (all entries in use), the block is
 387 // removed from the _allocation_list so it won't be considered by future
 388 // allocations until some entries in it are released.
 389 //
 390 // release() is performed lock-free. (Note: This means it can't notify the
 391 // service thread of pending cleanup work.  It must be lock-free because
 392 // it is called in all kinds of contexts where even quite low ranked locks
 393 // may be held.)  release() first looks up the block for
 394 // the entry, using address alignment to find the enclosing block (thereby
 395 // avoiding iteration over the _active_array).  Once the block has been
 396 // determined, its _allocated_bitmask needs to be updated, and its position in
 397 // the _allocation_list may need to be updated.  There are two cases:
 398 //
 399 // (a) If the block is neither full nor would become empty with the release of
 400 // the entry, only its _allocated_bitmask needs to be updated.  But if the CAS
 401 // update fails, the applicable case may change for the retry.
 402 //
 403 // (b) Otherwise, the _allocation_list also needs to be modified.  This requires
 404 // locking the _allocation_mutex.  To keep the release() operation lock-free,
 405 // rather than updating the _allocation_list itself, it instead performs a
 406 // lock-free push of the block onto the _deferred_updates list.  Entries on
 407 // that list are processed by allocate() and delete_empty_blocks(), while
 408 // they already hold the necessary lock.  That processing makes the block's
 409 // list state consistent with its current _allocated_bitmask.  The block is
 410 // added to the _allocation_list if not already present and the bitmask is not
 411 // full.  The block is moved to the end of the _allocation_list if the bitmask
 412 // is empty, for ease of empty block deletion processing.
 413 
 414 oop* OopStorage::allocate() {
 415   MutexLocker ml(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 416 
 417   // Note: Without this we might never perform cleanup.  As it is,
 418   // cleanup is only requested here, when completing a concurrent
 419   // iteration, or when someone entirely else wakes up the service
 420   // thread, which isn't ideal.  But we can't notify in release().
 421   if (reduce_deferred_updates()) {
 422     notify_needs_cleanup();
 423   }
 424 
 425   Block* block = block_for_allocation();
 426   if (block == NULL) return NULL; // Block allocation failed.
 427   assert(!block->is_full(), "invariant");
 428   if (block->is_empty()) {
 429     // Transitioning from empty to not empty.
 430     log_debug(oopstorage, blocks)("%s: block not empty " PTR_FORMAT, name(), p2i(block));
 431   }
 432   oop* result = block->allocate();
 433   assert(result != NULL, "allocation failed");
 434   assert(!block->is_empty(), "postcondition");
 435   Atomic::inc(&_allocation_count); // release updates outside lock.
 436   if (block->is_full()) {
 437     // Transitioning from not full to full.
 438     // Remove full blocks from consideration by future allocates.
 439     log_debug(oopstorage, blocks)("%s: block full " PTR_FORMAT, name(), p2i(block));
 440     _allocation_list.unlink(*block);
 441   }
 442   log_trace(oopstorage, ref)("%s: allocated " PTR_FORMAT, name(), p2i(result));
 443   return result;
 444 }
 445 
 446 bool OopStorage::try_add_block() {
 447   assert_lock_strong(_allocation_mutex);
 448   Block* block;
 449   {
 450     MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 451     block = Block::new_block(this);
 452   }
 453   if (block == NULL) return false;
 454 
 455   // Add new block to the _active_array, growing if needed.
 456   if (!_active_array->push(block)) {
 457     if (expand_active_array()) {
 458       guarantee(_active_array->push(block), "push failed after expansion");
 459     } else {
 460       log_debug(oopstorage, blocks)("%s: failed active array expand", name());
 461       Block::delete_block(*block);
 462       return false;
 463     }
 464   }
 465   // Add to end of _allocation_list.  The mutex release allowed other
 466   // threads to add blocks to the _allocation_list.  We prefer to
 467   // allocate from non-empty blocks, to allow empty blocks to be
 468   // deleted.  But we don't bother notifying about the empty block
 469   // because we're (probably) about to allocate an entry from it.
 470   _allocation_list.push_back(*block);
 471   log_debug(oopstorage, blocks)("%s: new block " PTR_FORMAT, name(), p2i(block));
 472   return true;
 473 }
 474 
 475 OopStorage::Block* OopStorage::block_for_allocation() {
 476   assert_lock_strong(_allocation_mutex);
 477 
 478   while (true) {
 479     // Use the first block in _allocation_list for the allocation.
 480     Block* block = _allocation_list.head();
 481     if (block != NULL) {
 482       return block;
 483     } else if (reduce_deferred_updates()) {
 484       MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 485       notify_needs_cleanup();
 486     } else if (try_add_block()) {
 487       block = _allocation_list.head();
 488       assert(block != NULL, "invariant");
 489       return block;
 490     } else if (reduce_deferred_updates()) { // Once more before failure.
 491       MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 492       notify_needs_cleanup();
 493     } else {
 494       // Attempt to add a block failed, no other thread added a block,
 495       // and no deferred updated added a block, then allocation failed.
 496       log_debug(oopstorage, blocks)("%s: failed block allocation", name());
 497       return NULL;
 498     }
 499   }
 500 }
 501 
 502 // Create a new, larger, active array with the same content as the
 503 // current array, and then replace, relinquishing the old array.
 504 // Return true if the array was successfully expanded, false to
 505 // indicate allocation failure.
 506 bool OopStorage::expand_active_array() {
 507   assert_lock_strong(_allocation_mutex);
 508   ActiveArray* old_array = _active_array;
 509   size_t new_size = 2 * old_array->size();
 510   log_debug(oopstorage, blocks)("%s: expand active array " SIZE_FORMAT,
 511                                 name(), new_size);
 512   ActiveArray* new_array = ActiveArray::create(new_size, AllocFailStrategy::RETURN_NULL);
 513   if (new_array == NULL) return false;
 514   new_array->copy_from(old_array);
 515   replace_active_array(new_array);
 516   relinquish_block_array(old_array);
 517   return true;
 518 }
 519 
 520 // Make new_array the _active_array.  Increments new_array's refcount
 521 // to account for the new reference.  The assignment is atomic wrto
 522 // obtain_active_array; once this function returns, it is safe for the
 523 // caller to relinquish the old array.
 524 void OopStorage::replace_active_array(ActiveArray* new_array) {
 525   // Caller has the old array that is the current value of _active_array.
 526   // Update new_array refcount to account for the new reference.
 527   new_array->increment_refcount();
 528   // Install new_array, ensuring its initialization is complete first.
 529   OrderAccess::release_store(&_active_array, new_array);
 530   // Wait for any readers that could read the old array from _active_array.
 531   // Can't use GlobalCounter here, because this is called from allocate(),
 532   // which may be called in the scope of a GlobalCounter critical section
 533   // when inserting a StringTable entry.
 534   _protect_active.synchronize();
 535   // All obtain critical sections that could see the old array have
 536   // completed, having incremented the refcount of the old array.  The
 537   // caller can now safely relinquish the old array.
 538 }
 539 
 540 // Atomically (wrto replace_active_array) get the active array and
 541 // increment its refcount.  This provides safe access to the array,
 542 // even if an allocate operation expands and replaces the value of
 543 // _active_array.  The caller must relinquish the array when done
 544 // using it.
 545 OopStorage::ActiveArray* OopStorage::obtain_active_array() const {
 546   SingleWriterSynchronizer::CriticalSection cs(&_protect_active);
 547   ActiveArray* result = OrderAccess::load_acquire(&_active_array);
 548   result->increment_refcount();
 549   return result;
 550 }
 551 
 552 // Decrement refcount of array and destroy if refcount is zero.
 553 void OopStorage::relinquish_block_array(ActiveArray* array) const {
 554   if (array->decrement_refcount()) {
 555     assert(array != _active_array, "invariant");
 556     ActiveArray::destroy(array);
 557   }
 558 }
 559 
 560 class OopStorage::WithActiveArray : public StackObj {
 561   const OopStorage* _storage;
 562   ActiveArray* _active_array;
 563 
 564 public:
 565   WithActiveArray(const OopStorage* storage) :
 566     _storage(storage),
 567     _active_array(storage->obtain_active_array())
 568   {}
 569 
 570   ~WithActiveArray() {
 571     _storage->relinquish_block_array(_active_array);
 572   }
 573 
 574   ActiveArray& active_array() const {
 575     return *_active_array;
 576   }
 577 };
 578 
 579 OopStorage::Block* OopStorage::find_block_or_null(const oop* ptr) const {
 580   assert(ptr != NULL, "precondition");
 581   return Block::block_for_ptr(this, ptr);
 582 }
 583 
 584 static void log_release_transitions(uintx releasing,
 585                                     uintx old_allocated,
 586                                     const OopStorage* owner,
 587                                     const void* block) {
 588   Log(oopstorage, blocks) log;
 589   LogStream ls(log.debug());
 590   if (is_full_bitmask(old_allocated)) {
 591     ls.print_cr("%s: block not full " PTR_FORMAT, owner->name(), p2i(block));
 592   }
 593   if (releasing == old_allocated) {
 594     ls.print_cr("%s: block empty " PTR_FORMAT, owner->name(), p2i(block));
 595   }
 596 }
 597 
 598 void OopStorage::Block::release_entries(uintx releasing, OopStorage* owner) {
 599   assert(releasing != 0, "preconditon");
 600   // Prevent empty block deletion when transitioning to empty.
 601   Atomic::inc(&_release_refcount);
 602 
 603   // Atomically update allocated bitmask.
 604   uintx old_allocated = _allocated_bitmask;
 605   while (true) {
 606     assert((releasing & ~old_allocated) == 0, "releasing unallocated entries");
 607     uintx new_value = old_allocated ^ releasing;
 608     uintx fetched = Atomic::cmpxchg(new_value, &_allocated_bitmask, old_allocated);
 609     if (fetched == old_allocated) break; // Successful update.
 610     old_allocated = fetched;             // Retry with updated bitmask.
 611   }
 612 
 613   // Now that the bitmask has been updated, if we have a state transition
 614   // (updated bitmask is empty or old bitmask was full), atomically push
 615   // this block onto the deferred updates list.  Some future call to
 616   // reduce_deferred_updates will make any needed changes related to this
 617   // block and _allocation_list.  This deferral avoids _allocation_list
 618   // updates and the associated locking here.
 619   if ((releasing == old_allocated) || is_full_bitmask(old_allocated)) {
 620     // Log transitions.  Both transitions are possible in a single update.
 621     if (log_is_enabled(Debug, oopstorage, blocks)) {
 622       log_release_transitions(releasing, old_allocated, _owner, this);
 623     }
 624     // Attempt to claim responsibility for adding this block to the deferred
 625     // list, by setting the link to non-NULL by self-looping.  If this fails,
 626     // then someone else has made such a claim and the deferred update has not
 627     // yet been processed and will include our change, so we don't need to do
 628     // anything further.
 629     if (Atomic::replace_if_null(this, &_deferred_updates_next)) {
 630       // Successfully claimed.  Push, with self-loop for end-of-list.
 631       Block* head = owner->_deferred_updates;
 632       while (true) {
 633         _deferred_updates_next = (head == NULL) ? this : head;
 634         Block* fetched = Atomic::cmpxchg(this, &owner->_deferred_updates, head);
 635         if (fetched == head) break; // Successful update.
 636         head = fetched;             // Retry with updated head.
 637       }
 638       owner->record_needs_cleanup();
 639       log_debug(oopstorage, blocks)("%s: deferred update " PTR_FORMAT,
 640                                     _owner->name(), p2i(this));
 641     }
 642   }
 643   // Release hold on empty block deletion.
 644   Atomic::dec(&_release_refcount);
 645 }
 646 
 647 // Process one available deferred update.  Returns true if one was processed.
 648 bool OopStorage::reduce_deferred_updates() {
 649   assert_lock_strong(_allocation_mutex);
 650   // Atomically pop a block off the list, if any available.
 651   // No ABA issue because this is only called by one thread at a time.
 652   // The atomicity is wrto pushes by release().
 653   Block* block = OrderAccess::load_acquire(&_deferred_updates);
 654   while (true) {
 655     if (block == NULL) return false;
 656     // Try atomic pop of block from list.
 657     Block* tail = block->deferred_updates_next();
 658     if (block == tail) tail = NULL; // Handle self-loop end marker.
 659     Block* fetched = Atomic::cmpxchg(tail, &_deferred_updates, block);
 660     if (fetched == block) break; // Update successful.
 661     block = fetched;             // Retry with updated block.
 662   }
 663   block->set_deferred_updates_next(NULL); // Clear tail after updating head.
 664   // Ensure bitmask read after pop is complete, including clearing tail, for
 665   // ordering with release().  Without this, we may be processing a stale
 666   // bitmask state here while blocking a release() operation from recording
 667   // the deferred update needed for its bitmask change.
 668   OrderAccess::fence();
 669   // Process popped block.
 670   uintx allocated = block->allocated_bitmask();
 671 
 672   // Make membership in list consistent with bitmask state.
 673   if ((_allocation_list.ctail() != NULL) &&
 674       ((_allocation_list.ctail() == block) ||
 675        (_allocation_list.next(*block) != NULL))) {
 676     // Block is in the _allocation_list.
 677     assert(!is_full_bitmask(allocated), "invariant");
 678   } else if (!is_full_bitmask(allocated)) {
 679     // Block is not in the _allocation_list, but now should be.
 680     _allocation_list.push_front(*block);
 681   } // Else block is full and not in list, which is correct.
 682 
 683   // Move empty block to end of list, for possible deletion.
 684   if (is_empty_bitmask(allocated)) {
 685     _allocation_list.unlink(*block);
 686     _allocation_list.push_back(*block);
 687     notify_needs_cleanup();
 688   }
 689 
 690   log_debug(oopstorage, blocks)("%s: processed deferred update " PTR_FORMAT,
 691                                 name(), p2i(block));
 692   return true;              // Processed one pending update.
 693 }
 694 
 695 inline void check_release_entry(const oop* entry) {
 696   assert(entry != NULL, "Releasing NULL");
 697   assert(*entry == NULL, "Releasing uncleared entry: " PTR_FORMAT, p2i(entry));
 698 }
 699 
 700 void OopStorage::release(const oop* ptr) {
 701   check_release_entry(ptr);
 702   Block* block = find_block_or_null(ptr);
 703   assert(block != NULL, "%s: invalid release " PTR_FORMAT, name(), p2i(ptr));
 704   log_trace(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(ptr));
 705   block->release_entries(block->bitmask_for_entry(ptr), this);
 706   Atomic::dec(&_allocation_count);
 707 }
 708 
 709 void OopStorage::release(const oop* const* ptrs, size_t size) {
 710   size_t i = 0;
 711   while (i < size) {
 712     check_release_entry(ptrs[i]);
 713     Block* block = find_block_or_null(ptrs[i]);
 714     assert(block != NULL, "%s: invalid release " PTR_FORMAT, name(), p2i(ptrs[i]));
 715     log_trace(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(ptrs[i]));
 716     size_t count = 0;
 717     uintx releasing = 0;
 718     for ( ; i < size; ++i) {
 719       const oop* entry = ptrs[i];
 720       check_release_entry(entry);
 721       // If entry not in block, finish block and resume outer loop with entry.
 722       if (!block->contains(entry)) break;
 723       // Add entry to releasing bitmap.
 724       log_trace(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(entry));
 725       uintx entry_bitmask = block->bitmask_for_entry(entry);
 726       assert((releasing & entry_bitmask) == 0,
 727              "Duplicate entry: " PTR_FORMAT, p2i(entry));
 728       releasing |= entry_bitmask;
 729       ++count;
 730     }
 731     // Release the contiguous entries that are in block.
 732     block->release_entries(releasing, this);
 733     Atomic::sub(count, &_allocation_count);
 734   }
 735 }
 736 
 737 const char* dup_name(const char* name) {
 738   char* dup = NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtGC);
 739   strcpy(dup, name);
 740   return dup;
 741 }
 742 
 743 // Possible values for OopStorage::_needs_cleanup.
 744 const uint needs_cleanup_none = 0;     // No cleanup needed.
 745 const uint needs_cleanup_marked = 1;   // Requested, but no notification made.
 746 const uint needs_cleanup_notified = 2; // Requested and Service thread notified.
 747 
 748 const size_t initial_active_array_size = 8;
 749 
 750 OopStorage::OopStorage(const char* name,
 751                        Mutex* allocation_mutex,
 752                        Mutex* active_mutex) :
 753   _name(dup_name(name)),
 754   _active_array(ActiveArray::create(initial_active_array_size)),
 755   _allocation_list(),
 756   _deferred_updates(NULL),
 757   _allocation_mutex(allocation_mutex),
 758   _active_mutex(active_mutex),
 759   _allocation_count(0),
 760   _concurrent_iteration_count(0),
 761   _needs_cleanup(needs_cleanup_none)
 762 {
 763   _active_array->increment_refcount();
 764   assert(_active_mutex->rank() < _allocation_mutex->rank(),
 765          "%s: active_mutex must have lower rank than allocation_mutex", _name);
 766   assert(Service_lock->rank() < _active_mutex->rank(),
 767          "%s: active_mutex must have higher rank than Service_lock", _name);
 768   assert(_active_mutex->_safepoint_check_required == Mutex::_safepoint_check_never,
 769          "%s: active mutex requires never safepoint check", _name);
 770   assert(_allocation_mutex->_safepoint_check_required == Mutex::_safepoint_check_never,
 771          "%s: allocation mutex requires never safepoint check", _name);
 772 }
 773 
 774 void OopStorage::delete_empty_block(const Block& block) {
 775   assert(block.is_empty(), "discarding non-empty block");
 776   log_debug(oopstorage, blocks)("%s: delete empty block " PTR_FORMAT, name(), p2i(&block));
 777   Block::delete_block(block);
 778 }
 779 
 780 OopStorage::~OopStorage() {
 781   Block* block;
 782   while ((block = _deferred_updates) != NULL) {
 783     _deferred_updates = block->deferred_updates_next();
 784     block->set_deferred_updates_next(NULL);
 785   }
 786   while ((block = _allocation_list.head()) != NULL) {
 787     _allocation_list.unlink(*block);
 788   }
 789   bool unreferenced = _active_array->decrement_refcount();
 790   assert(unreferenced, "deleting storage while _active_array is referenced");
 791   for (size_t i = _active_array->block_count(); 0 < i; ) {
 792     block = _active_array->at(--i);
 793     Block::delete_block(*block);
 794   }
 795   ActiveArray::destroy(_active_array);
 796   FREE_C_HEAP_ARRAY(char, _name);
 797 }
 798 
 799 // Called by service thread to check for pending work.
 800 bool OopStorage::needs_delete_empty_blocks() const {
 801   return Atomic::load(&_needs_cleanup) != needs_cleanup_none;
 802 }
 803 
 804 // Record that cleanup is needed, without notifying the Service thread.
 805 // Used by release(), where we can't lock even Service_lock.
 806 void OopStorage::record_needs_cleanup() {
 807   Atomic::cmpxchg(needs_cleanup_marked, &_needs_cleanup, needs_cleanup_none);
 808 }
 809 
 810 // Record that cleanup is needed, and notify the Service thread.
 811 void OopStorage::notify_needs_cleanup() {
 812   // Avoid re-notification if already notified.
 813   const uint notified = needs_cleanup_notified;
 814   if (Atomic::xchg(notified, &_needs_cleanup) != notified) {
 815     MonitorLocker ml(Service_lock, Monitor::_no_safepoint_check_flag);
 816     ml.notify_all();
 817   }
 818 }
 819 
 820 bool OopStorage::delete_empty_blocks() {
 821   MutexLocker ml(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 822 
 823   // Clear the request before processing.
 824   Atomic::store(needs_cleanup_none, &_needs_cleanup);
 825   OrderAccess::fence();
 826 
 827   // Other threads could be adding to the empty block count or the
 828   // deferred update list while we're working.  Set an upper bound on
 829   // how many updates we'll process and blocks we'll try to release,
 830   // so other threads can't cause an unbounded stay in this function.
 831   size_t limit = block_count();
 832   if (limit == 0) return false; // Empty storage; nothing at all to do.
 833 
 834   for (size_t i = 0; i < limit; ++i) {
 835     // Process deferred updates, which might make empty blocks available.
 836     // Continue checking once deletion starts, since additional updates
 837     // might become available while we're working.
 838     if (reduce_deferred_updates()) {
 839       // Be safepoint-polite while looping.
 840       MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 841       ThreadBlockInVM tbiv(JavaThread::current());
 842     } else {
 843       Block* block = _allocation_list.tail();
 844       if ((block == NULL) || !block->is_empty()) {
 845         return false;
 846       } else if (!block->is_safe_to_delete()) {
 847         // Look for other work while waiting for block to be deletable.
 848         break;
 849       }
 850 
 851       // Try to delete the block.  First, try to remove from _active_array.
 852       {
 853         MutexLocker aml(_active_mutex, Mutex::_no_safepoint_check_flag);
 854         // Don't interfere with an active concurrent iteration.
 855         // Instead, give up immediately.  There is more work to do,
 856         // but don't re-notify, to avoid useless spinning of the
 857         // service thread.  Instead, iteration completion notifies.
 858         if (_concurrent_iteration_count > 0) return true;
 859         _active_array->remove(block);
 860       }
 861       // Remove block from _allocation_list and delete it.
 862       _allocation_list.unlink(*block);
 863       // Be safepoint-polite while deleting and looping.
 864       MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 865       delete_empty_block(*block);
 866       ThreadBlockInVM tbiv(JavaThread::current());
 867     }
 868   }
 869   // Exceeded work limit or can't delete last block.  This will
 870   // cause the service thread to loop, giving other subtasks an
 871   // opportunity to run too.  There's no need for a notification,
 872   // because we are part of the service thread (unless gtesting).
 873   record_needs_cleanup();
 874   return true;
 875 }
 876 
 877 OopStorage::EntryStatus OopStorage::allocation_status(const oop* ptr) const {
 878   const Block* block = find_block_or_null(ptr);
 879   if (block != NULL) {
 880     // Prevent block deletion and _active_array modification.
 881     MutexLocker ml(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 882     // Block could be a false positive, so get index carefully.
 883     size_t index = Block::active_index_safe(block);
 884     if ((index < _active_array->block_count()) &&
 885         (block == _active_array->at(index)) &&
 886         block->contains(ptr)) {
 887       if ((block->allocated_bitmask() & block->bitmask_for_entry(ptr)) != 0) {
 888         return ALLOCATED_ENTRY;
 889       } else {
 890         return UNALLOCATED_ENTRY;
 891       }
 892     }
 893   }
 894   return INVALID_ENTRY;
 895 }
 896 
 897 size_t OopStorage::allocation_count() const {
 898   return _allocation_count;
 899 }
 900 
 901 size_t OopStorage::block_count() const {
 902   WithActiveArray wab(this);
 903   // Count access is racy, but don't care.
 904   return wab.active_array().block_count();
 905 }
 906 
 907 size_t OopStorage::total_memory_usage() const {
 908   size_t total_size = sizeof(OopStorage);
 909   total_size += strlen(name()) + 1;
 910   total_size += sizeof(ActiveArray);
 911   WithActiveArray wab(this);
 912   const ActiveArray& blocks = wab.active_array();
 913   // Count access is racy, but don't care.
 914   total_size += blocks.block_count() * Block::allocation_size();
 915   total_size += blocks.size() * sizeof(Block*);
 916   return total_size;
 917 }
 918 
 919 // Parallel iteration support
 920 
 921 uint OopStorage::BasicParState::default_estimated_thread_count(bool concurrent) {
 922   uint configured = concurrent ? ConcGCThreads : ParallelGCThreads;
 923   return MAX2(1u, configured);  // Never estimate zero threads.
 924 }
 925 
 926 OopStorage::BasicParState::BasicParState(const OopStorage* storage,
 927                                          uint estimated_thread_count,
 928                                          bool concurrent) :
 929   _storage(storage),
 930   _active_array(_storage->obtain_active_array()),
 931   _block_count(0),              // initialized properly below
 932   _next_block(0),
 933   _estimated_thread_count(estimated_thread_count),
 934   _concurrent(concurrent)
 935 {
 936   assert(estimated_thread_count > 0, "estimated thread count must be positive");
 937   update_concurrent_iteration_count(1);
 938   // Get the block count *after* iteration state updated, so concurrent
 939   // empty block deletion is suppressed and can't reduce the count.  But
 940   // ensure the count we use was written after the block with that count
 941   // was fully initialized; see ActiveArray::push.
 942   _block_count = _active_array->block_count_acquire();
 943 }
 944 
 945 OopStorage::BasicParState::~BasicParState() {
 946   _storage->relinquish_block_array(_active_array);
 947   update_concurrent_iteration_count(-1);
 948   if (_concurrent) {
 949     // We may have deferred some work.
 950     const_cast<OopStorage*>(_storage)->notify_needs_cleanup();
 951   }
 952 }
 953 
 954 void OopStorage::BasicParState::update_concurrent_iteration_count(int value) {
 955   if (_concurrent) {
 956     MutexLocker ml(_storage->_active_mutex, Mutex::_no_safepoint_check_flag);
 957     _storage->_concurrent_iteration_count += value;
 958     assert(_storage->_concurrent_iteration_count >= 0, "invariant");
 959   }
 960 }
 961 
 962 bool OopStorage::BasicParState::claim_next_segment(IterationData* data) {
 963   data->_processed += data->_segment_end - data->_segment_start;
 964   size_t start = OrderAccess::load_acquire(&_next_block);
 965   if (start >= _block_count) {
 966     return finish_iteration(data); // No more blocks available.
 967   }
 968   // Try to claim several at a time, but not *too* many.  We want to
 969   // avoid deciding there are many available and selecting a large
 970   // quantity, get delayed, and then end up claiming most or all of
 971   // the remaining largish amount of work, leaving nothing for other
 972   // threads to do.  But too small a step can lead to contention
 973   // over _next_block, esp. when the work per block is small.
 974   size_t max_step = 10;
 975   size_t remaining = _block_count - start;
 976   size_t step = MIN2(max_step, 1 + (remaining / _estimated_thread_count));
 977   // Atomic::add with possible overshoot.  This can perform better
 978   // than a CAS loop on some platforms when there is contention.
 979   // We can cope with the uncertainty by recomputing start/end from
 980   // the result of the add, and dealing with potential overshoot.
 981   size_t end = Atomic::add(step, &_next_block);
 982   // _next_block may have changed, so recompute start from result of add.
 983   start = end - step;
 984   // _next_block may have changed so much that end has overshot.
 985   end = MIN2(end, _block_count);
 986   // _next_block may have changed so much that even start has overshot.
 987   if (start < _block_count) {
 988     // Record claimed segment for iteration.
 989     data->_segment_start = start;
 990     data->_segment_end = end;
 991     return true;                // Success.
 992   } else {
 993     // No more blocks to claim.
 994     return finish_iteration(data);
 995   }
 996 }
 997 
 998 bool OopStorage::BasicParState::finish_iteration(const IterationData* data) const {
 999   log_info(oopstorage, blocks, stats)
1000           ("Parallel iteration on %s: blocks = " SIZE_FORMAT
1001            ", processed = " SIZE_FORMAT " (%2.f%%)",
1002            _storage->name(), _block_count, data->_processed,
1003            percent_of(data->_processed, _block_count));
1004   return false;
1005 }
1006 
1007 const char* OopStorage::name() const { return _name; }
1008 
1009 #ifndef PRODUCT
1010 
1011 void OopStorage::print_on(outputStream* st) const {
1012   size_t allocations = _allocation_count;
1013   size_t blocks = _active_array->block_count();
1014 
1015   double data_size = section_size * section_count;
1016   double alloc_percentage = percent_of((double)allocations, blocks * data_size);
1017 
1018   st->print("%s: " SIZE_FORMAT " entries in " SIZE_FORMAT " blocks (%.F%%), " SIZE_FORMAT " bytes",
1019             name(), allocations, blocks, alloc_percentage, total_memory_usage());
1020   if (_concurrent_iteration_count > 0) {
1021     st->print(", concurrent iteration active");
1022   }
1023 }
1024 
1025 #endif // !PRODUCT