1 /*
   2  * Copyright (c) 2018, 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/mutex.hpp"
  35 #include "runtime/mutexLocker.hpp"
  36 #include "runtime/orderAccess.hpp"
  37 #include "runtime/safepoint.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 #include "runtime/thread.hpp"
  40 #include "utilities/align.hpp"
  41 #include "utilities/count_trailing_zeros.hpp"
  42 #include "utilities/debug.hpp"
  43 #include "utilities/globalDefinitions.hpp"
  44 #include "utilities/macros.hpp"
  45 #include "utilities/ostream.hpp"
  46 #include "utilities/spinYield.hpp"
  47 
  48 OopStorage::AllocateEntry::AllocateEntry() : _prev(NULL), _next(NULL) {}
  49 
  50 OopStorage::AllocateEntry::~AllocateEntry() {
  51   assert(_prev == NULL, "deleting attached block");
  52   assert(_next == NULL, "deleting attached block");
  53 }
  54 
  55 OopStorage::AllocateList::AllocateList() : _head(NULL), _tail(NULL) {}
  56 
  57 OopStorage::AllocateList::~AllocateList() {
  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::AllocateList::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.allocate_entry()._next = old;
  70     old->allocate_entry()._prev = █
  71     _head = █
  72   }
  73 }
  74 
  75 void OopStorage::AllocateList::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->allocate_entry()._next = █
  82     block.allocate_entry()._prev = old;
  83     _tail = █
  84   }
  85 }
  86 
  87 void OopStorage::AllocateList::unlink(const Block& block) {
  88   const AllocateEntry& block_entry = block.allocate_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->allocate_entry()._prev = NULL;
 100     _head = next_blk;
 101   } else if (next_blk == NULL) {
 102     assert(_tail == &block, "invariant");
 103     prev_blk->allocate_entry()._next = NULL;
 104     _tail = prev_blk;
 105   } else {
 106     next_blk->allocate_entry()._prev = prev_blk;
 107     prev_blk->allocate_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   _allocate_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 // A block is deletable if
 259 // (1) It is empty.
 260 // (2) There is not a release() operation currently operating on it.
 261 // (3) It is not in the deferred updates list.
 262 // The order of tests is important for proper interaction between release()
 263 // and concurrent deletion.
 264 bool OopStorage::Block::is_deletable() const {
 265   return (OrderAccess::load_acquire(&_allocated_bitmask) == 0) &&
 266          (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 _allocate_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 _allocate_list, to make it easy for empty block
 375 // deletion to find them.
 376 //
 377 // allocate(), and delete_empty_blocks_concurrent() lock the
 378 // _allocate_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 _allocate_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 _allocate_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. release() first looks up the block for
 391 // the entry, using address alignment to find the enclosing block (thereby
 392 // avoiding iteration over the _active_array).  Once the block has been
 393 // determined, its _allocated_bitmask needs to be updated, and its position in
 394 // the _allocate_list may need to be updated.  There are two cases:
 395 //
 396 // (a) If the block is neither full nor would become empty with the release of
 397 // the entry, only its _allocated_bitmask needs to be updated.  But if the CAS
 398 // update fails, the applicable case may change for the retry.
 399 //
 400 // (b) Otherwise, the _allocate_list also needs to be modified.  This requires
 401 // locking the _allocate_mutex.  To keep the release() operation lock-free,
 402 // rather than updating the _allocate_list itself, it instead performs a
 403 // lock-free push of the block onto the _deferred_updates list.  Entries on
 404 // that list are processed by allocate() and delete_empty_blocks_XXX(), while
 405 // they already hold the necessary lock.  That processing makes the block's
 406 // list state consistent with its current _allocated_bitmask.  The block is
 407 // added to the _allocate_list if not already present and the bitmask is not
 408 // full.  The block is moved to the end of the _allocated_list if the bitmask
 409 // is empty, for ease of empty block deletion processing.
 410 
 411 oop* OopStorage::allocate() {
 412   MutexLockerEx ml(_allocate_mutex, Mutex::_no_safepoint_check_flag);
 413   // Do some deferred update processing every time we allocate.
 414   // Continue processing deferred updates if _allocate_list is empty,
 415   // in the hope that we'll get a block from that, rather than
 416   // allocating a new block.
 417   while (reduce_deferred_updates() && (_allocate_list.head() == NULL)) {}
 418 
 419   // Use the first block in _allocate_list for the allocation.
 420   Block* block = _allocate_list.head();
 421   if (block == NULL) {
 422     // No available blocks; make a new one, and add to storage.
 423     {
 424       MutexUnlockerEx mul(_allocate_mutex, Mutex::_no_safepoint_check_flag);
 425       block = Block::new_block(this);
 426     }
 427     if (block == NULL) {
 428       while (_allocate_list.head() == NULL) {
 429         if (!reduce_deferred_updates()) {
 430           // Failed to make new block, no other thread made a block
 431           // available while the mutex was released, and didn't get
 432           // one from a deferred update either, so return failure.
 433           log_info(oopstorage, ref)("%s: failed block allocation", name());
 434           return NULL;
 435         }
 436       }
 437     } else {
 438       // Add new block to storage.
 439       log_info(oopstorage, blocks)("%s: new block " PTR_FORMAT, name(), p2i(block));
 440 
 441       // Add new block to the _active_array, growing if needed.
 442       if (!_active_array->push(block)) {
 443         if (expand_active_array()) {
 444           guarantee(_active_array->push(block), "push failed after expansion");
 445         } else {
 446           log_info(oopstorage, blocks)("%s: failed active array expand", name());
 447           Block::delete_block(*block);
 448           return NULL;
 449         }
 450       }
 451       // Add to end of _allocate_list.  The mutex release allowed
 452       // other threads to add blocks to the _allocate_list.  We prefer
 453       // to allocate from non-empty blocks, to allow empty blocks to
 454       // be deleted.
 455       _allocate_list.push_back(*block);
 456     }
 457     block = _allocate_list.head();
 458   }
 459   // Allocate from first block.
 460   assert(block != NULL, "invariant");
 461   assert(!block->is_full(), "invariant");
 462   if (block->is_empty()) {
 463     // Transitioning from empty to not empty.
 464     log_debug(oopstorage, blocks)("%s: block not empty " PTR_FORMAT, name(), p2i(block));
 465   }
 466   oop* result = block->allocate();
 467   assert(result != NULL, "allocation failed");
 468   assert(!block->is_empty(), "postcondition");
 469   Atomic::inc(&_allocation_count); // release updates outside lock.
 470   if (block->is_full()) {
 471     // Transitioning from not full to full.
 472     // Remove full blocks from consideration by future allocates.
 473     log_debug(oopstorage, blocks)("%s: block full " PTR_FORMAT, name(), p2i(block));
 474     _allocate_list.unlink(*block);
 475   }
 476   log_info(oopstorage, ref)("%s: allocated " PTR_FORMAT, name(), p2i(result));
 477   return result;
 478 }
 479 
 480 // Create a new, larger, active array with the same content as the
 481 // current array, and then replace, relinquishing the old array.
 482 // Return true if the array was successfully expanded, false to
 483 // indicate allocation failure.
 484 bool OopStorage::expand_active_array() {
 485   assert_lock_strong(_allocate_mutex);
 486   ActiveArray* old_array = _active_array;
 487   size_t new_size = 2 * old_array->size();
 488   log_info(oopstorage, blocks)("%s: expand active array " SIZE_FORMAT,
 489                                name(), new_size);
 490   ActiveArray* new_array = ActiveArray::create(new_size, AllocFailStrategy::RETURN_NULL);
 491   if (new_array == NULL) return false;
 492   new_array->copy_from(old_array);
 493   replace_active_array(new_array);
 494   relinquish_block_array(old_array);
 495   return true;
 496 }
 497 
 498 OopStorage::ProtectActive::ProtectActive() : _enter(0), _exit() {}
 499 
 500 // Begin read-side critical section.
 501 uint OopStorage::ProtectActive::read_enter() {
 502   return Atomic::add(2u, &_enter);
 503 }
 504 
 505 // End read-side critical section.
 506 void OopStorage::ProtectActive::read_exit(uint enter_value) {
 507   Atomic::add(2u, &_exit[enter_value & 1]);
 508 }
 509 
 510 // Wait until all readers that entered the critical section before
 511 // synchronization have exited that critical section.
 512 void OopStorage::ProtectActive::write_synchronize() {
 513   SpinYield spinner;
 514   // Determine old and new exit counters, based on bit0 of the
 515   // on-entry _enter counter.
 516   uint value = OrderAccess::load_acquire(&_enter);
 517   volatile uint* new_ptr = &_exit[(value + 1) & 1];
 518   // Atomically change the in-use exit counter to the new counter, by
 519   // adding 1 to the _enter counter (flipping bit0 between 0 and 1)
 520   // and initializing the new exit counter to that enter value.  Note:
 521   // The new exit counter is not being used by read operations until
 522   // this change succeeds.
 523   uint old;
 524   do {
 525     old = value;
 526     *new_ptr = ++value;
 527     value = Atomic::cmpxchg(value, &_enter, old);
 528   } while (old != value);
 529   // Readers that entered the critical section before we changed the
 530   // selected exit counter will use the old exit counter.  Readers
 531   // entering after the change will use the new exit counter.  Wait
 532   // for all the critical sections started before the change to
 533   // complete, e.g. for the value of old_ptr to catch up with old.
 534   volatile uint* old_ptr = &_exit[old & 1];
 535   while (old != OrderAccess::load_acquire(old_ptr)) {
 536     spinner.wait();
 537   }
 538 }
 539 
 540 // Make new_array the _active_array.  Increments new_array's refcount
 541 // to account for the new reference.  The assignment is atomic wrto
 542 // obtain_active_array; once this function returns, it is safe for the
 543 // caller to relinquish the old array.
 544 void OopStorage::replace_active_array(ActiveArray* new_array) {
 545   // Caller has the old array that is the current value of _active_array.
 546   // Update new_array refcount to account for the new reference.
 547   new_array->increment_refcount();
 548   // Install new_array, ensuring its initialization is complete first.
 549   OrderAccess::release_store(&_active_array, new_array);
 550   // Wait for any readers that could read the old array from _active_array.
 551   _protect_active.write_synchronize();
 552   // All obtain critical sections that could see the old array have
 553   // completed, having incremented the refcount of the old array.  The
 554   // caller can now safely relinquish the old array.
 555 }
 556 
 557 // Atomically (wrto replace_active_array) get the active array and
 558 // increment its refcount.  This provides safe access to the array,
 559 // even if an allocate operation expands and replaces the value of
 560 // _active_array.  The caller must relinquish the array when done
 561 // using it.
 562 OopStorage::ActiveArray* OopStorage::obtain_active_array() const {
 563   uint enter_value = _protect_active.read_enter();
 564   ActiveArray* result = OrderAccess::load_acquire(&_active_array);
 565   result->increment_refcount();
 566   _protect_active.read_exit(enter_value);
 567   return result;
 568 }
 569 
 570 // Decrement refcount of array and destroy if refcount is zero.
 571 void OopStorage::relinquish_block_array(ActiveArray* array) const {
 572   if (array->decrement_refcount()) {
 573     assert(array != _active_array, "invariant");
 574     ActiveArray::destroy(array);
 575   }
 576 }
 577 
 578 class OopStorage::WithActiveArray : public StackObj {
 579   const OopStorage* _storage;
 580   ActiveArray* _active_array;
 581 
 582 public:
 583   WithActiveArray(const OopStorage* storage) :
 584     _storage(storage),
 585     _active_array(storage->obtain_active_array())
 586   {}
 587 
 588   ~WithActiveArray() {
 589     _storage->relinquish_block_array(_active_array);
 590   }
 591 
 592   ActiveArray& active_array() const {
 593     return *_active_array;
 594   }
 595 };
 596 
 597 OopStorage::Block* OopStorage::find_block_or_null(const oop* ptr) const {
 598   assert(ptr != NULL, "precondition");
 599   return Block::block_for_ptr(this, ptr);
 600 }
 601 
 602 static void log_release_transitions(uintx releasing,
 603                                     uintx old_allocated,
 604                                     const OopStorage* owner,
 605                                     const void* block) {
 606   Log(oopstorage, blocks) log;
 607   LogStream ls(log.debug());
 608   if (is_full_bitmask(old_allocated)) {
 609     ls.print_cr("%s: block not full " PTR_FORMAT, owner->name(), p2i(block));
 610   }
 611   if (releasing == old_allocated) {
 612     ls.print_cr("%s: block empty " PTR_FORMAT, owner->name(), p2i(block));
 613   }
 614 }
 615 
 616 void OopStorage::Block::release_entries(uintx releasing, Block* volatile* deferred_list) {
 617   assert(releasing != 0, "preconditon");
 618   // Prevent empty block deletion when transitioning to empty.
 619   Atomic::inc(&_release_refcount);
 620 
 621   // Atomically update allocated bitmask.
 622   uintx old_allocated = _allocated_bitmask;
 623   while (true) {
 624     assert((releasing & ~old_allocated) == 0, "releasing unallocated entries");
 625     uintx new_value = old_allocated ^ releasing;
 626     uintx fetched = Atomic::cmpxchg(new_value, &_allocated_bitmask, old_allocated);
 627     if (fetched == old_allocated) break; // Successful update.
 628     old_allocated = fetched;             // Retry with updated bitmask.
 629   }
 630 
 631   // Now that the bitmask has been updated, if we have a state transition
 632   // (updated bitmask is empty or old bitmask was full), atomically push
 633   // this block onto the deferred updates list.  Some future call to
 634   // reduce_deferred_updates will make any needed changes related to this
 635   // block and _allocate_list.  This deferral avoids list updates and the
 636   // associated locking here.
 637   if ((releasing == old_allocated) || is_full_bitmask(old_allocated)) {
 638     // Log transitions.  Both transitions are possible in a single update.
 639     if (log_is_enabled(Debug, oopstorage, blocks)) {
 640       log_release_transitions(releasing, old_allocated, _owner, this);
 641     }
 642     // Attempt to claim responsibility for adding this block to the deferred
 643     // list, by setting the link to non-NULL by self-looping.  If this fails,
 644     // then someone else has made such a claim and the deferred update has not
 645     // yet been processed and will include our change, so we don't need to do
 646     // anything further.
 647     if (Atomic::replace_if_null(this, &_deferred_updates_next)) {
 648       // Successfully claimed.  Push, with self-loop for end-of-list.
 649       Block* head = *deferred_list;
 650       while (true) {
 651         _deferred_updates_next = (head == NULL) ? this : head;
 652         Block* fetched = Atomic::cmpxchg(this, deferred_list, head);
 653         if (fetched == head) break; // Successful update.
 654         head = fetched;             // Retry with updated head.
 655       }
 656       log_debug(oopstorage, blocks)("%s: deferred update " PTR_FORMAT,
 657                                     _owner->name(), p2i(this));
 658     }
 659   }
 660   // Release hold on empty block deletion.
 661   Atomic::dec(&_release_refcount);
 662 }
 663 
 664 // Process one available deferred update.  Returns true if one was processed.
 665 bool OopStorage::reduce_deferred_updates() {
 666   assert_locked_or_safepoint(_allocate_mutex);
 667   // Atomically pop a block off the list, if any available.
 668   // No ABA issue because this is only called by one thread at a time.
 669   // The atomicity is wrto pushes by release().
 670   Block* block = OrderAccess::load_acquire(&_deferred_updates);
 671   while (true) {
 672     if (block == NULL) return false;
 673     // Try atomic pop of block from list.
 674     Block* tail = block->deferred_updates_next();
 675     if (block == tail) tail = NULL; // Handle self-loop end marker.
 676     Block* fetched = Atomic::cmpxchg(tail, &_deferred_updates, block);
 677     if (fetched == block) break; // Update successful.
 678     block = fetched;             // Retry with updated block.
 679   }
 680   block->set_deferred_updates_next(NULL); // Clear tail after updating head.
 681   // Ensure bitmask read after pop is complete, including clearing tail, for
 682   // ordering with release().  Without this, we may be processing a stale
 683   // bitmask state here while blocking a release() operation from recording
 684   // the deferred update needed for its bitmask change.
 685   OrderAccess::storeload();
 686   // Process popped block.
 687   uintx allocated = block->allocated_bitmask();
 688 
 689   // Make membership in list consistent with bitmask state.
 690   if ((_allocate_list.ctail() != NULL) &&
 691       ((_allocate_list.ctail() == block) ||
 692        (_allocate_list.next(*block) != NULL))) {
 693     // Block is in the allocate list.
 694     assert(!is_full_bitmask(allocated), "invariant");
 695   } else if (!is_full_bitmask(allocated)) {
 696     // Block is not in the allocate list, but now should be.
 697     _allocate_list.push_front(*block);
 698   } // Else block is full and not in list, which is correct.
 699 
 700   // Move empty block to end of list, for possible deletion.
 701   if (is_empty_bitmask(allocated)) {
 702     _allocate_list.unlink(*block);
 703     _allocate_list.push_back(*block);
 704   }
 705 
 706   log_debug(oopstorage, blocks)("%s: processed deferred update " PTR_FORMAT,
 707                                 name(), p2i(block));
 708   return true;              // Processed one pending update.
 709 }
 710 
 711 inline void check_release_entry(const oop* entry) {
 712   assert(entry != NULL, "Releasing NULL");
 713   assert(*entry == NULL, "Releasing uncleared entry: " PTR_FORMAT, p2i(entry));
 714 }
 715 
 716 void OopStorage::release(const oop* ptr) {
 717   check_release_entry(ptr);
 718   Block* block = find_block_or_null(ptr);
 719   assert(block != NULL, "%s: invalid release " PTR_FORMAT, name(), p2i(ptr));
 720   log_info(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(ptr));
 721   block->release_entries(block->bitmask_for_entry(ptr), &_deferred_updates);
 722   Atomic::dec(&_allocation_count);
 723 }
 724 
 725 void OopStorage::release(const oop* const* ptrs, size_t size) {
 726   size_t i = 0;
 727   while (i < size) {
 728     check_release_entry(ptrs[i]);
 729     Block* block = find_block_or_null(ptrs[i]);
 730     assert(block != NULL, "%s: invalid release " PTR_FORMAT, name(), p2i(ptrs[i]));
 731     log_info(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(ptrs[i]));
 732     size_t count = 0;
 733     uintx releasing = 0;
 734     for ( ; i < size; ++i) {
 735       const oop* entry = ptrs[i];
 736       check_release_entry(entry);
 737       // If entry not in block, finish block and resume outer loop with entry.
 738       if (!block->contains(entry)) break;
 739       // Add entry to releasing bitmap.
 740       log_info(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(entry));
 741       uintx entry_bitmask = block->bitmask_for_entry(entry);
 742       assert((releasing & entry_bitmask) == 0,
 743              "Duplicate entry: " PTR_FORMAT, p2i(entry));
 744       releasing |= entry_bitmask;
 745       ++count;
 746     }
 747     // Release the contiguous entries that are in block.
 748     block->release_entries(releasing, &_deferred_updates);
 749     Atomic::sub(count, &_allocation_count);
 750   }
 751 }
 752 
 753 const char* dup_name(const char* name) {
 754   char* dup = NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtGC);
 755   strcpy(dup, name);
 756   return dup;
 757 }
 758 
 759 const size_t initial_active_array_size = 8;
 760 
 761 OopStorage::OopStorage(const char* name,
 762                        Mutex* allocate_mutex,
 763                        Mutex* active_mutex) :
 764   _name(dup_name(name)),
 765   _active_array(ActiveArray::create(initial_active_array_size)),
 766   _allocate_list(),
 767   _deferred_updates(NULL),
 768   _allocate_mutex(allocate_mutex),
 769   _active_mutex(active_mutex),
 770   _allocation_count(0),
 771   _concurrent_iteration_active(false)
 772 {
 773   _active_array->increment_refcount();
 774   assert(_active_mutex->rank() < _allocate_mutex->rank(),
 775          "%s: active_mutex must have lower rank than allocate_mutex", _name);
 776   assert(_active_mutex->_safepoint_check_required != Mutex::_safepoint_check_always,
 777          "%s: active mutex requires safepoint check", _name);
 778   assert(_allocate_mutex->_safepoint_check_required != Mutex::_safepoint_check_always,
 779          "%s: allocate mutex requires safepoint check", _name);
 780 }
 781 
 782 void OopStorage::delete_empty_block(const Block& block) {
 783   assert(block.is_empty(), "discarding non-empty block");
 784   log_info(oopstorage, blocks)("%s: delete empty block " PTR_FORMAT, name(), p2i(&block));
 785   Block::delete_block(block);
 786 }
 787 
 788 OopStorage::~OopStorage() {
 789   Block* block;
 790   while ((block = _deferred_updates) != NULL) {
 791     _deferred_updates = block->deferred_updates_next();
 792     block->set_deferred_updates_next(NULL);
 793   }
 794   while ((block = _allocate_list.head()) != NULL) {
 795     _allocate_list.unlink(*block);
 796   }
 797   bool unreferenced = _active_array->decrement_refcount();
 798   assert(unreferenced, "deleting storage while _active_array is referenced");
 799   for (size_t i = _active_array->block_count(); 0 < i; ) {
 800     block = _active_array->at(--i);
 801     Block::delete_block(*block);
 802   }
 803   ActiveArray::destroy(_active_array);
 804   FREE_C_HEAP_ARRAY(char, _name);
 805 }
 806 
 807 void OopStorage::delete_empty_blocks_safepoint() {
 808   assert_at_safepoint();
 809   // Process any pending release updates, which may make more empty
 810   // blocks available for deletion.
 811   while (reduce_deferred_updates()) {}
 812   // Don't interfere with a concurrent iteration.
 813   if (_concurrent_iteration_active) return;
 814   // Delete empty (and otherwise deletable) blocks from end of _allocate_list.
 815   for (Block* block = _allocate_list.tail();
 816        (block != NULL) && block->is_deletable();
 817        block = _allocate_list.tail()) {
 818     _active_array->remove(block);
 819     _allocate_list.unlink(*block);
 820     delete_empty_block(*block);
 821   }
 822 }
 823 
 824 void OopStorage::delete_empty_blocks_concurrent() {
 825   MutexLockerEx ml(_allocate_mutex, Mutex::_no_safepoint_check_flag);
 826   // Other threads could be adding to the empty block count while we
 827   // release the mutex across the block deletions.  Set an upper bound
 828   // on how many blocks we'll try to release, so other threads can't
 829   // cause an unbounded stay in this function.
 830   size_t limit = block_count();
 831 
 832   for (size_t i = 0; i < limit; ++i) {
 833     // Additional updates might become available while we dropped the
 834     // lock.  But limit number processed to limit lock duration.
 835     reduce_deferred_updates();
 836 
 837     Block* block = _allocate_list.tail();
 838     if ((block == NULL) || !block->is_deletable()) {
 839       // No block to delete, so done.  There could be more pending
 840       // deferred updates that could give us more work to do; deal with
 841       // that in some later call, to limit lock duration here.
 842       return;
 843     }
 844 
 845     {
 846       MutexLockerEx aml(_active_mutex, Mutex::_no_safepoint_check_flag);
 847       // Don't interfere with a concurrent iteration.
 848       if (_concurrent_iteration_active) return;
 849       _active_array->remove(block);
 850     }
 851     // Remove block from _allocate_list and delete it.
 852     _allocate_list.unlink(*block);
 853     // Release mutex while deleting block.
 854     MutexUnlockerEx ul(_allocate_mutex, Mutex::_no_safepoint_check_flag);
 855     delete_empty_block(*block);
 856   }
 857 }
 858 
 859 OopStorage::EntryStatus OopStorage::allocation_status(const oop* ptr) const {
 860   const Block* block = find_block_or_null(ptr);
 861   if (block != NULL) {
 862     // Prevent block deletion and _active_array modification.
 863     MutexLockerEx ml(_allocate_mutex, Mutex::_no_safepoint_check_flag);
 864     // Block could be a false positive, so get index carefully.
 865     size_t index = Block::active_index_safe(block);
 866     if ((index < _active_array->block_count()) &&
 867         (block == _active_array->at(index)) &&
 868         block->contains(ptr)) {
 869       if ((block->allocated_bitmask() & block->bitmask_for_entry(ptr)) != 0) {
 870         return ALLOCATED_ENTRY;
 871       } else {
 872         return UNALLOCATED_ENTRY;
 873       }
 874     }
 875   }
 876   return INVALID_ENTRY;
 877 }
 878 
 879 size_t OopStorage::allocation_count() const {
 880   return _allocation_count;
 881 }
 882 
 883 size_t OopStorage::block_count() const {
 884   WithActiveArray wab(this);
 885   // Count access is racy, but don't care.
 886   return wab.active_array().block_count();
 887 }
 888 
 889 size_t OopStorage::total_memory_usage() const {
 890   size_t total_size = sizeof(OopStorage);
 891   total_size += strlen(name()) + 1;
 892   total_size += sizeof(ActiveArray);
 893   WithActiveArray wab(this);
 894   const ActiveArray& blocks = wab.active_array();
 895   // Count access is racy, but don't care.
 896   total_size += blocks.block_count() * Block::allocation_size();
 897   total_size += blocks.size() * sizeof(Block*);
 898   return total_size;
 899 }
 900 
 901 // Parallel iteration support
 902 
 903 uint OopStorage::BasicParState::default_estimated_thread_count(bool concurrent) {
 904   uint configured = concurrent ? ConcGCThreads : ParallelGCThreads;
 905   return MAX2(1u, configured);  // Never estimate zero threads.
 906 }
 907 
 908 OopStorage::BasicParState::BasicParState(const OopStorage* storage,
 909                                          uint estimated_thread_count,
 910                                          bool concurrent) :
 911   _storage(storage),
 912   _active_array(_storage->obtain_active_array()),
 913   _block_count(0),              // initialized properly below
 914   _next_block(0),
 915   _estimated_thread_count(estimated_thread_count),
 916   _concurrent(concurrent)
 917 {
 918   assert(estimated_thread_count > 0, "estimated thread count must be positive");
 919   update_iteration_state(true);
 920   // Get the block count *after* iteration state updated, so concurrent
 921   // empty block deletion is suppressed and can't reduce the count.  But
 922   // ensure the count we use was written after the block with that count
 923   // was fully initialized; see ActiveArray::push.
 924   _block_count = _active_array->block_count_acquire();
 925 }
 926 
 927 OopStorage::BasicParState::~BasicParState() {
 928   _storage->relinquish_block_array(_active_array);
 929   update_iteration_state(false);
 930 }
 931 
 932 void OopStorage::BasicParState::update_iteration_state(bool value) {
 933   if (_concurrent) {
 934     MutexLockerEx ml(_storage->_active_mutex, Mutex::_no_safepoint_check_flag);
 935     assert(_storage->_concurrent_iteration_active != value, "precondition");
 936     _storage->_concurrent_iteration_active = value;
 937   }
 938 }
 939 
 940 bool OopStorage::BasicParState::claim_next_segment(IterationData* data) {
 941   data->_processed += data->_segment_end - data->_segment_start;
 942   size_t start = OrderAccess::load_acquire(&_next_block);
 943   if (start >= _block_count) {
 944     return finish_iteration(data); // No more blocks available.
 945   }
 946   // Try to claim several at a time, but not *too* many.  We want to
 947   // avoid deciding there are many available and selecting a large
 948   // quantity, get delayed, and then end up claiming most or all of
 949   // the remaining largish amount of work, leaving nothing for other
 950   // threads to do.  But too small a step can lead to contention
 951   // over _next_block, esp. when the work per block is small.
 952   size_t max_step = 10;
 953   size_t remaining = _block_count - start;
 954   size_t step = MIN2(max_step, 1 + (remaining / _estimated_thread_count));
 955   // Atomic::add with possible overshoot.  This can perform better
 956   // than a CAS loop on some platforms when there is contention.
 957   // We can cope with the uncertainty by recomputing start/end from
 958   // the result of the add, and dealing with potential overshoot.
 959   size_t end = Atomic::add(step, &_next_block);
 960   // _next_block may have changed, so recompute start from result of add.
 961   start = end - step;
 962   // _next_block may have changed so much that end has overshot.
 963   end = MIN2(end, _block_count);
 964   // _next_block may have changed so much that even start has overshot.
 965   if (start < _block_count) {
 966     // Record claimed segment for iteration.
 967     data->_segment_start = start;
 968     data->_segment_end = end;
 969     return true;                // Success.
 970   } else {
 971     // No more blocks to claim.
 972     return finish_iteration(data);
 973   }
 974 }
 975 
 976 bool OopStorage::BasicParState::finish_iteration(const IterationData* data) const {
 977   log_debug(oopstorage, blocks, stats)
 978            ("Parallel iteration on %s: blocks = " SIZE_FORMAT
 979             ", processed = " SIZE_FORMAT " (%2.f%%)",
 980             _storage->name(), _block_count, data->_processed,
 981             percent_of(data->_processed, _block_count));
 982   return false;
 983 }
 984 
 985 const char* OopStorage::name() const { return _name; }
 986 
 987 #ifndef PRODUCT
 988 
 989 void OopStorage::print_on(outputStream* st) const {
 990   size_t allocations = _allocation_count;
 991   size_t blocks = _active_array->block_count();
 992 
 993   double data_size = section_size * section_count;
 994   double alloc_percentage = percent_of((double)allocations, blocks * data_size);
 995 
 996   st->print("%s: " SIZE_FORMAT " entries in " SIZE_FORMAT " blocks (%.F%%), " SIZE_FORMAT " bytes",
 997             name(), allocations, blocks, alloc_percentage, total_memory_usage());
 998   if (_concurrent_iteration_active) {
 999     st->print(", concurrent iteration active");
1000   }
1001 }
1002 
1003 #endif // !PRODUCT