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