< prev index next >

src/hotspot/share/gc/shared/oopStorage.cpp

Print this page




  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/os.hpp"
  39 #include "runtime/safepoint.hpp"
  40 #include "runtime/stubRoutines.hpp"
  41 #include "runtime/thread.hpp"
  42 #include "utilities/align.hpp"
  43 #include "utilities/count_trailing_zeros.hpp"
  44 #include "utilities/debug.hpp"
  45 #include "utilities/globalDefinitions.hpp"
  46 #include "utilities/macros.hpp"
  47 #include "utilities/ostream.hpp"
  48 
  49 OopStorage::AllocationListEntry::AllocationListEntry() : _prev(NULL), _next(NULL) {}
  50 
  51 OopStorage::AllocationListEntry::~AllocationListEntry() {
  52   assert(_prev == NULL, "deleting attached block");
  53   assert(_next == NULL, "deleting attached block");
  54 }
  55 
  56 OopStorage::AllocationList::AllocationList() : _head(NULL), _tail(NULL) {}
  57 
  58 OopStorage::AllocationList::~AllocationList() {


 191     *to_ptr++ = block;
 192   }
 193   _block_count = count;
 194 }
 195 
 196 // Blocks start with an array of BitsPerWord oop entries.  That array
 197 // is divided into conceptual BytesPerWord sections of BitsPerByte
 198 // entries.  Blocks are allocated aligned on section boundaries, for
 199 // the convenience of mapping from an entry to the containing block;
 200 // see block_for_ptr().  Aligning on section boundary rather than on
 201 // the full _data wastes a lot less space, but makes for a bit more
 202 // work in block_for_ptr().
 203 
 204 const unsigned section_size = BitsPerByte;
 205 const unsigned section_count = BytesPerWord;
 206 const unsigned block_alignment = sizeof(oop) * section_size;
 207 
 208 OopStorage::Block::Block(const OopStorage* owner, void* memory) :
 209   _data(),
 210   _allocated_bitmask(0),
 211   _owner_address(reinterpret_cast<intptr_t>(owner)),
 212   _memory(memory),
 213   _active_index(0),
 214   _allocation_list_entry(),
 215   _deferred_updates_next(NULL),
 216   _release_refcount(0)
 217 {
 218   STATIC_ASSERT(_data_pos == 0);
 219   STATIC_ASSERT(section_size * section_count == ARRAY_SIZE(_data));
 220   assert(offset_of(Block, _data) == _data_pos, "invariant");
 221   assert(owner != NULL, "NULL owner");
 222   assert(is_aligned(this, block_alignment), "misaligned block");
 223 }
 224 
 225 OopStorage::Block::~Block() {
 226   assert(_release_refcount == 0, "deleting block while releasing");
 227   assert(_deferred_updates_next == NULL, "deleting block with deferred update");
 228   // Clear fields used by block_for_ptr and entry validation, which
 229   // might help catch bugs.  Volatile to prevent dead-store elimination.
 230   const_cast<uintx volatile&>(_allocated_bitmask) = 0;
 231   const_cast<intptr_t volatile&>(_owner_address) = 0;
 232 }
 233 
 234 size_t OopStorage::Block::allocation_size() {
 235   // _data must be first member, so aligning Block aligns _data.
 236   STATIC_ASSERT(_data_pos == 0);
 237   return sizeof(Block) + block_alignment - sizeof(void*);
 238 }
 239 
 240 size_t OopStorage::Block::allocation_alignment_shift() {
 241   return exact_log2(block_alignment);
 242 }
 243 
 244 inline bool is_full_bitmask(uintx bitmask) { return ~bitmask == 0; }
 245 inline bool is_empty_bitmask(uintx bitmask) { return bitmask == 0; }
 246 
 247 bool OopStorage::Block::is_full() const {
 248   return is_full_bitmask(allocated_bitmask());
 249 }
 250 
 251 bool OopStorage::Block::is_empty() const {


 339 // block.  For some uses, it is a precondition that ptr is valid,
 340 // e.g. contained in some block in owner's _active_array.  Other uses
 341 // require additional validation of the result.
 342 OopStorage::Block*
 343 OopStorage::Block::block_for_ptr(const OopStorage* owner, const oop* ptr) {
 344   assert(CanUseSafeFetchN(), "precondition");
 345   STATIC_ASSERT(_data_pos == 0);
 346   // Const-ness of ptr is not related to const-ness of containing block.
 347   // Blocks are allocated section-aligned, so get the containing section.
 348   oop* section_start = align_down(const_cast<oop*>(ptr), block_alignment);
 349   // Start with a guess that the containing section is the last section,
 350   // so the block starts section_count-1 sections earlier.
 351   oop* section = section_start - (section_size * (section_count - 1));
 352   // Walk up through the potential block start positions, looking for
 353   // the owner in the expected location.  If we're below the actual block
 354   // start position, the value at the owner position will be some oop
 355   // (possibly NULL), which can never match the owner.
 356   intptr_t owner_addr = reinterpret_cast<intptr_t>(owner);
 357   for (unsigned i = 0; i < section_count; ++i, section += section_size) {
 358     Block* candidate = reinterpret_cast<Block*>(section);
 359     if (SafeFetchN(&candidate->_owner_address, 0) == owner_addr) {


 360       return candidate;
 361     }
 362   }
 363   return NULL;
 364 }
 365 
 366 //////////////////////////////////////////////////////////////////////////////
 367 // Allocation
 368 //
 369 // Allocation involves the _allocation_list, which contains a subset of the
 370 // blocks owned by a storage object.  This is a doubly-linked list, linked
 371 // through dedicated fields in the blocks.  Full blocks are removed from this
 372 // list, though they are still present in the _active_array.  Empty blocks are
 373 // kept at the end of the _allocation_list, to make it easy for empty block
 374 // deletion to find them.
 375 //
 376 // allocate(), and delete_empty_blocks() lock the
 377 // _allocation_mutex while performing any list and array modifications.
 378 //
 379 // allocate() and release() update a block's _allocated_bitmask using CAS


 396 // the _allocation_list may need to be updated.  There are two cases:
 397 //
 398 // (a) If the block is neither full nor would become empty with the release of
 399 // the entry, only its _allocated_bitmask needs to be updated.  But if the CAS
 400 // update fails, the applicable case may change for the retry.
 401 //
 402 // (b) Otherwise, the _allocation_list also needs to be modified.  This requires
 403 // locking the _allocation_mutex.  To keep the release() operation lock-free,
 404 // rather than updating the _allocation_list itself, it instead performs a
 405 // lock-free push of the block onto the _deferred_updates list.  Entries on
 406 // that list are processed by allocate() and delete_empty_blocks(), while
 407 // they already hold the necessary lock.  That processing makes the block's
 408 // list state consistent with its current _allocated_bitmask.  The block is
 409 // added to the _allocation_list if not already present and the bitmask is not
 410 // full.  The block is moved to the end of the _allocation_list if the bitmask
 411 // is empty, for ease of empty block deletion processing.
 412 
 413 oop* OopStorage::allocate() {
 414   MutexLocker ml(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 415 








 416   Block* block = block_for_allocation();
 417   if (block == NULL) return NULL; // Block allocation failed.
 418   assert(!block->is_full(), "invariant");
 419   if (block->is_empty()) {
 420     // Transitioning from empty to not empty.
 421     log_trace(oopstorage, blocks)("%s: block not empty " PTR_FORMAT, name(), p2i(block));
 422   }
 423   oop* result = block->allocate();
 424   assert(result != NULL, "allocation failed");
 425   assert(!block->is_empty(), "postcondition");
 426   Atomic::inc(&_allocation_count); // release updates outside lock.
 427   if (block->is_full()) {
 428     // Transitioning from not full to full.
 429     // Remove full blocks from consideration by future allocates.
 430     log_trace(oopstorage, blocks)("%s: block full " PTR_FORMAT, name(), p2i(block));
 431     _allocation_list.unlink(*block);
 432   }
 433   log_trace(oopstorage, ref)("%s: allocated " PTR_FORMAT, name(), p2i(result));
 434   return result;
 435 }
 436 
 437 bool OopStorage::try_add_block() {
 438   assert_lock_strong(_allocation_mutex);
 439   Block* block;
 440   {
 441     MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 442     block = Block::new_block(this);
 443   }
 444   if (block == NULL) return false;
 445 
 446   // Add new block to the _active_array, growing if needed.
 447   if (!_active_array->push(block)) {
 448     if (expand_active_array()) {
 449       guarantee(_active_array->push(block), "push failed after expansion");
 450     } else {
 451       log_debug(oopstorage, blocks)("%s: failed active array expand", name());
 452       Block::delete_block(*block);
 453       return false;
 454     }
 455   }
 456   // Add to end of _allocation_list.  The mutex release allowed other
 457   // threads to add blocks to the _allocation_list.  We prefer to
 458   // allocate from non-empty blocks, to allow empty blocks to be
 459   // deleted.  But we don't bother notifying about the empty block
 460   // because we're (probably) about to allocate an entry from it.
 461   _allocation_list.push_back(*block);
 462   log_debug(oopstorage, blocks)("%s: new block " PTR_FORMAT, name(), p2i(block));
 463   return true;
 464 }
 465 
 466 OopStorage::Block* OopStorage::block_for_allocation() {
 467   assert_lock_strong(_allocation_mutex);

 468   while (true) {
 469     // Use the first block in _allocation_list for the allocation.
 470     Block* block = _allocation_list.head();
 471     if (block != NULL) {
 472       return block;
 473     } else if (reduce_deferred_updates()) {
 474       // Might have added a block to the _allocation_list, so retry.

 475     } else if (try_add_block()) {
 476       // Successfully added a new block to the list, so retry.
 477       assert(_allocation_list.chead() != NULL, "invariant");
 478     } else if (_allocation_list.chead() != NULL) {
 479       // Trying to add a block failed, but some other thread added to the
 480       // list while we'd dropped the lock over the new block allocation.
 481     } else if (!reduce_deferred_updates()) { // Once more before failure.

 482       // Attempt to add a block failed, no other thread added a block,
 483       // and no deferred updated added a block, then allocation failed.
 484       log_info(oopstorage, blocks)("%s: failed block allocation", name());
 485       return NULL;
 486     }
 487   }
 488 }
 489 
 490 // Create a new, larger, active array with the same content as the
 491 // current array, and then replace, relinquishing the old array.
 492 // Return true if the array was successfully expanded, false to
 493 // indicate allocation failure.
 494 bool OopStorage::expand_active_array() {
 495   assert_lock_strong(_allocation_mutex);
 496   ActiveArray* old_array = _active_array;
 497   size_t new_size = 2 * old_array->size();
 498   log_debug(oopstorage, blocks)("%s: expand active array " SIZE_FORMAT,
 499                                 name(), new_size);
 500   ActiveArray* new_array = ActiveArray::create(new_size, AllocFailStrategy::RETURN_NULL);
 501   if (new_array == NULL) return false;
 502   new_array->copy_from(old_array);
 503   replace_active_array(new_array);
 504   relinquish_block_array(old_array);


 556   {}
 557 
 558   ~WithActiveArray() {
 559     _storage->relinquish_block_array(_active_array);
 560   }
 561 
 562   ActiveArray& active_array() const {
 563     return *_active_array;
 564   }
 565 };
 566 
 567 OopStorage::Block* OopStorage::find_block_or_null(const oop* ptr) const {
 568   assert(ptr != NULL, "precondition");
 569   return Block::block_for_ptr(this, ptr);
 570 }
 571 
 572 static void log_release_transitions(uintx releasing,
 573                                     uintx old_allocated,
 574                                     const OopStorage* owner,
 575                                     const void* block) {
 576   LogTarget(Trace, oopstorage, blocks) lt;
 577   if (lt.is_enabled()) {
 578     LogStream ls(lt);
 579     if (is_full_bitmask(old_allocated)) {
 580       ls.print_cr("%s: block not full " PTR_FORMAT, owner->name(), p2i(block));
 581     }
 582     if (releasing == old_allocated) {
 583       ls.print_cr("%s: block empty " PTR_FORMAT, owner->name(), p2i(block));
 584     }
 585   }
 586 }
 587 
 588 void OopStorage::Block::release_entries(uintx releasing, OopStorage* owner) {
 589   assert(releasing != 0, "preconditon");
 590   // Prevent empty block deletion when transitioning to empty.
 591   Atomic::inc(&_release_refcount);
 592 
 593   // Atomically update allocated bitmask.
 594   uintx old_allocated = _allocated_bitmask;
 595   while (true) {
 596     assert((releasing & ~old_allocated) == 0, "releasing unallocated entries");
 597     uintx new_value = old_allocated ^ releasing;
 598     uintx fetched = Atomic::cmpxchg(new_value, &_allocated_bitmask, old_allocated);
 599     if (fetched == old_allocated) break; // Successful update.
 600     old_allocated = fetched;             // Retry with updated bitmask.
 601   }
 602 
 603   // Now that the bitmask has been updated, if we have a state transition
 604   // (updated bitmask is empty or old bitmask was full), atomically push
 605   // this block onto the deferred updates list.  Some future call to
 606   // reduce_deferred_updates will make any needed changes related to this
 607   // block and _allocation_list.  This deferral avoids _allocation_list
 608   // updates and the associated locking here.
 609   if ((releasing == old_allocated) || is_full_bitmask(old_allocated)) {
 610     // Log transitions.  Both transitions are possible in a single update.
 611     log_release_transitions(releasing, old_allocated, owner, this);


 612     // Attempt to claim responsibility for adding this block to the deferred
 613     // list, by setting the link to non-NULL by self-looping.  If this fails,
 614     // then someone else has made such a claim and the deferred update has not
 615     // yet been processed and will include our change, so we don't need to do
 616     // anything further.
 617     if (Atomic::replace_if_null(this, &_deferred_updates_next)) {
 618       // Successfully claimed.  Push, with self-loop for end-of-list.
 619       Block* head = owner->_deferred_updates;
 620       while (true) {
 621         _deferred_updates_next = (head == NULL) ? this : head;
 622         Block* fetched = Atomic::cmpxchg(this, &owner->_deferred_updates, head);
 623         if (fetched == head) break; // Successful update.
 624         head = fetched;             // Retry with updated head.
 625       }
 626       // Only request cleanup for to-empty transitions, not for from-full.
 627       // There isn't any rush to process from-full transitions.  Allocation
 628       // will reduce deferrals before allocating new blocks, so may process
 629       // some.  And the service thread will drain the entire deferred list
 630       // if there are any pending to-empty transitions.
 631       if (releasing == old_allocated) {
 632         owner->record_needs_cleanup();
 633       }
 634       log_trace(oopstorage, blocks)("%s: deferred update " PTR_FORMAT,
 635                                     owner->name(), p2i(this));
 636     }
 637   }
 638   // Release hold on empty block deletion.
 639   Atomic::dec(&_release_refcount);
 640 }
 641 
 642 // Process one available deferred update.  Returns true if one was processed.
 643 bool OopStorage::reduce_deferred_updates() {
 644   assert_lock_strong(_allocation_mutex);
 645   // Atomically pop a block off the list, if any available.
 646   // No ABA issue because this is only called by one thread at a time.
 647   // The atomicity is wrto pushes by release().
 648   Block* block = OrderAccess::load_acquire(&_deferred_updates);
 649   while (true) {
 650     if (block == NULL) return false;
 651     // Try atomic pop of block from list.
 652     Block* tail = block->deferred_updates_next();
 653     if (block == tail) tail = NULL; // Handle self-loop end marker.
 654     Block* fetched = Atomic::cmpxchg(tail, &_deferred_updates, block);
 655     if (fetched == block) break; // Update successful.


 662   // the deferred update needed for its bitmask change.
 663   OrderAccess::fence();
 664   // Process popped block.
 665   uintx allocated = block->allocated_bitmask();
 666 
 667   // Make membership in list consistent with bitmask state.
 668   if ((_allocation_list.ctail() != NULL) &&
 669       ((_allocation_list.ctail() == block) ||
 670        (_allocation_list.next(*block) != NULL))) {
 671     // Block is in the _allocation_list.
 672     assert(!is_full_bitmask(allocated), "invariant");
 673   } else if (!is_full_bitmask(allocated)) {
 674     // Block is not in the _allocation_list, but now should be.
 675     _allocation_list.push_front(*block);
 676   } // Else block is full and not in list, which is correct.
 677 
 678   // Move empty block to end of list, for possible deletion.
 679   if (is_empty_bitmask(allocated)) {
 680     _allocation_list.unlink(*block);
 681     _allocation_list.push_back(*block);

 682   }
 683 
 684   log_trace(oopstorage, blocks)("%s: processed deferred update " PTR_FORMAT,
 685                                 name(), p2i(block));
 686   return true;              // Processed one pending update.
 687 }
 688 
 689 inline void check_release_entry(const oop* entry) {
 690   assert(entry != NULL, "Releasing NULL");
 691   assert(*entry == NULL, "Releasing uncleared entry: " PTR_FORMAT, p2i(entry));
 692 }
 693 
 694 void OopStorage::release(const oop* ptr) {
 695   check_release_entry(ptr);
 696   Block* block = find_block_or_null(ptr);
 697   assert(block != NULL, "%s: invalid release " PTR_FORMAT, name(), p2i(ptr));
 698   log_trace(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(ptr));
 699   block->release_entries(block->bitmask_for_entry(ptr), this);
 700   Atomic::dec(&_allocation_count);
 701 }
 702 
 703 void OopStorage::release(const oop* const* ptrs, size_t size) {
 704   size_t i = 0;


 711     uintx releasing = 0;
 712     for ( ; i < size; ++i) {
 713       const oop* entry = ptrs[i];
 714       check_release_entry(entry);
 715       // If entry not in block, finish block and resume outer loop with entry.
 716       if (!block->contains(entry)) break;
 717       // Add entry to releasing bitmap.
 718       log_trace(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(entry));
 719       uintx entry_bitmask = block->bitmask_for_entry(entry);
 720       assert((releasing & entry_bitmask) == 0,
 721              "Duplicate entry: " PTR_FORMAT, p2i(entry));
 722       releasing |= entry_bitmask;
 723       ++count;
 724     }
 725     // Release the contiguous entries that are in block.
 726     block->release_entries(releasing, this);
 727     Atomic::sub(count, &_allocation_count);
 728   }
 729 }
 730 











 731 const size_t initial_active_array_size = 8;
 732 
 733 OopStorage::OopStorage(const char* name,
 734                        Mutex* allocation_mutex,
 735                        Mutex* active_mutex) :
 736   _name(os::strdup(name)),
 737   _active_array(ActiveArray::create(initial_active_array_size)),
 738   _allocation_list(),
 739   _deferred_updates(NULL),
 740   _allocation_mutex(allocation_mutex),
 741   _active_mutex(active_mutex),
 742   _allocation_count(0),
 743   _concurrent_iteration_count(0),
 744   _needs_cleanup(false)
 745 {
 746   _active_array->increment_refcount();
 747   assert(_active_mutex->rank() < _allocation_mutex->rank(),
 748          "%s: active_mutex must have lower rank than allocation_mutex", _name);
 749   assert(Service_lock->rank() < _active_mutex->rank(),
 750          "%s: active_mutex must have higher rank than Service_lock", _name);
 751   assert(_active_mutex->_safepoint_check_required == Mutex::_safepoint_check_never,
 752          "%s: active mutex requires never safepoint check", _name);
 753   assert(_allocation_mutex->_safepoint_check_required == Mutex::_safepoint_check_never,
 754          "%s: allocation mutex requires never safepoint check", _name);
 755 }
 756 
 757 void OopStorage::delete_empty_block(const Block& block) {
 758   assert(block.is_empty(), "discarding non-empty block");
 759   log_debug(oopstorage, blocks)("%s: delete empty block " PTR_FORMAT, name(), p2i(&block));
 760   Block::delete_block(block);
 761 }
 762 
 763 OopStorage::~OopStorage() {
 764   Block* block;
 765   while ((block = _deferred_updates) != NULL) {
 766     _deferred_updates = block->deferred_updates_next();
 767     block->set_deferred_updates_next(NULL);
 768   }
 769   while ((block = _allocation_list.head()) != NULL) {
 770     _allocation_list.unlink(*block);
 771   }
 772   bool unreferenced = _active_array->decrement_refcount();
 773   assert(unreferenced, "deleting storage while _active_array is referenced");
 774   for (size_t i = _active_array->block_count(); 0 < i; ) {
 775     block = _active_array->at(--i);
 776     Block::delete_block(*block);
 777   }
 778   ActiveArray::destroy(_active_array);
 779   os::free(const_cast<char*>(_name));
 780 }
 781 
 782 // Managing service thread notifications.
 783 //
 784 // We don't want cleanup work to linger indefinitely, but we also don't want
 785 // to run the service thread too often.  We're also very limited in what we
 786 // can do in a release operation, where cleanup work is created.
 787 //
 788 // When a release operation changes a block's state to empty, it records the
 789 // need for cleanup in both the associated storage object and in the global
 790 // request state.  A safepoint cleanup task notifies the service thread when
 791 // there may be cleanup work for any storage object, based on the global
 792 // request state.  But that notification is deferred if the service thread
 793 // has run recently, and we also avoid duplicate notifications.  The service
 794 // thread updates the timestamp and resets the state flags on every iteration.
 795 
 796 // Global cleanup request state.
 797 static volatile bool needs_cleanup_requested = false;
 798 
 799 // Flag for avoiding duplicate notifications.
 800 static bool needs_cleanup_triggered = false;
 801 
 802 // Time after which a notification can be made.
 803 static jlong cleanup_trigger_permit_time = 0;
 804 
 805 // Minimum time since last service thread check before notification is
 806 // permitted.  The value of 500ms was an arbitrary choice; frequent, but not
 807 // too frequent.
 808 const jlong cleanup_trigger_defer_period = 500 * NANOSECS_PER_MILLISEC;
 809 
 810 void OopStorage::trigger_cleanup_if_needed() {
 811   MonitorLocker ml(Service_lock, Monitor::_no_safepoint_check_flag);
 812   if (Atomic::load(&needs_cleanup_requested) &&
 813       !needs_cleanup_triggered &&
 814       (os::javaTimeNanos() > cleanup_trigger_permit_time)) {
 815     needs_cleanup_triggered = true;
 816     ml.notify_all();
 817   }
 818 }
 819 
 820 bool OopStorage::has_cleanup_work_and_reset() {
 821   assert_lock_strong(Service_lock);
 822   cleanup_trigger_permit_time =
 823     os::javaTimeNanos() + cleanup_trigger_defer_period;
 824   needs_cleanup_triggered = false;
 825   // Set the request flag false and return its old value.
 826   // Needs to be atomic to avoid dropping a concurrent request.
 827   // Can't use Atomic::xchg, which may not support bool.
 828   return Atomic::cmpxchg(false, &needs_cleanup_requested, true);
 829 }
 830 
 831 // Record that cleanup is needed, without notifying the Service thread.
 832 // Used by release(), where we can't lock even Service_lock.
 833 void OopStorage::record_needs_cleanup() {
 834   // Set local flag first, else service thread could wake up and miss
 835   // the request.  This order may instead (rarely) unnecessarily notify.
 836   OrderAccess::release_store(&_needs_cleanup, true);
 837   OrderAccess::release_store_fence(&needs_cleanup_requested, true);
 838 }
 839 
 840 bool OopStorage::delete_empty_blocks() {
 841   // Service thread might have oopstorage work, but not for this object.
 842   // Check for deferred updates even though that's not a service thread
 843   // trigger; since we're here, we might as well process them.
 844   if (!OrderAccess::load_acquire(&_needs_cleanup) &&
 845       (OrderAccess::load_acquire(&_deferred_updates) == NULL)) {
 846     return false;
 847   }

 848 

 849   MutexLocker ml(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 850 
 851   // Clear the request before processing.
 852   OrderAccess::release_store_fence(&_needs_cleanup, false);

 853 
 854   // Other threads could be adding to the empty block count or the
 855   // deferred update list while we're working.  Set an upper bound on
 856   // how many updates we'll process and blocks we'll try to release,
 857   // so other threads can't cause an unbounded stay in this function.
 858   // We add a bit of slop because the reduce_deferred_updates clause
 859   // can cause blocks to be double counted.  If there are few blocks
 860   // and many of them are deferred and empty, we might hit the limit
 861   // and spin the caller without doing very much work.  Otherwise,
 862   // we don't normally hit the limit anyway, instead running out of
 863   // work to do.
 864   size_t limit = block_count() + 10;
 865 
 866   for (size_t i = 0; i < limit; ++i) {
 867     // Process deferred updates, which might make empty blocks available.
 868     // Continue checking once deletion starts, since additional updates
 869     // might become available while we're working.
 870     if (reduce_deferred_updates()) {
 871       // Be safepoint-polite while looping.
 872       MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
 873       ThreadBlockInVM tbiv(JavaThread::current());
 874     } else {
 875       Block* block = _allocation_list.tail();
 876       if ((block == NULL) || !block->is_empty()) {
 877         return false;
 878       } else if (!block->is_safe_to_delete()) {
 879         // Look for other work while waiting for block to be deletable.
 880         break;
 881       }
 882 
 883       // Try to delete the block.  First, try to remove from _active_array.
 884       {


 961   _storage(storage),
 962   _active_array(_storage->obtain_active_array()),
 963   _block_count(0),              // initialized properly below
 964   _next_block(0),
 965   _estimated_thread_count(estimated_thread_count),
 966   _concurrent(concurrent)
 967 {
 968   assert(estimated_thread_count > 0, "estimated thread count must be positive");
 969   update_concurrent_iteration_count(1);
 970   // Get the block count *after* iteration state updated, so concurrent
 971   // empty block deletion is suppressed and can't reduce the count.  But
 972   // ensure the count we use was written after the block with that count
 973   // was fully initialized; see ActiveArray::push.
 974   _block_count = _active_array->block_count_acquire();
 975 }
 976 
 977 OopStorage::BasicParState::~BasicParState() {
 978   _storage->relinquish_block_array(_active_array);
 979   update_concurrent_iteration_count(-1);
 980   if (_concurrent) {
 981     // We may have deferred some cleanup work.
 982     const_cast<OopStorage*>(_storage)->record_needs_cleanup();
 983   }
 984 }
 985 
 986 void OopStorage::BasicParState::update_concurrent_iteration_count(int value) {
 987   if (_concurrent) {
 988     MutexLocker ml(_storage->_active_mutex, Mutex::_no_safepoint_check_flag);
 989     _storage->_concurrent_iteration_count += value;
 990     assert(_storage->_concurrent_iteration_count >= 0, "invariant");
 991   }
 992 }
 993 
 994 bool OopStorage::BasicParState::claim_next_segment(IterationData* data) {
 995   data->_processed += data->_segment_end - data->_segment_start;
 996   size_t start = OrderAccess::load_acquire(&_next_block);
 997   if (start >= _block_count) {
 998     return finish_iteration(data); // No more blocks available.
 999   }
1000   // Try to claim several at a time, but not *too* many.  We want to
1001   // avoid deciding there are many available and selecting a large
1002   // quantity, get delayed, and then end up claiming most or all of




  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() {


 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 {


 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


 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);


 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.


 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;


 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       {


 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


< prev index next >