duke@435: /* sla@5237: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" sla@5237: #include "gc_implementation/shared/gcTimer.hpp" sla@5237: #include "gc_implementation/shared/gcTraceTime.hpp" stefank@2314: #include "gc_interface/collectedHeap.hpp" stefank@2314: #include "gc_interface/collectedHeap.inline.hpp" stefank@2314: #include "memory/referencePolicy.hpp" stefank@2314: #include "memory/referenceProcessor.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/java.hpp" stefank@2314: #include "runtime/jniHandles.hpp" duke@435: ysr@888: ReferencePolicy* ReferenceProcessor::_always_clear_soft_ref_policy = NULL; ysr@888: ReferencePolicy* ReferenceProcessor::_default_soft_ref_policy = NULL; ysr@3117: bool ReferenceProcessor::_pending_list_uses_discovered_field = false; johnc@3188: jlong ReferenceProcessor::_soft_ref_timestamp_clock = 0; ysr@888: duke@435: void referenceProcessor_init() { duke@435: ReferenceProcessor::init_statics(); duke@435: } duke@435: duke@435: void ReferenceProcessor::init_statics() { johnc@3339: // We need a monotonically non-deccreasing time in ms but johnc@3339: // os::javaTimeMillis() does not guarantee monotonicity. johnc@3339: jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC; johnc@3188: johnc@3188: // Initialize the soft ref timestamp clock. johnc@3188: _soft_ref_timestamp_clock = now; johnc@3188: // Also update the soft ref clock in j.l.r.SoftReference johnc@3188: java_lang_ref_SoftReference::set_clock(_soft_ref_timestamp_clock); duke@435: ysr@888: _always_clear_soft_ref_policy = new AlwaysClearPolicy(); ysr@888: _default_soft_ref_policy = new COMPILER2_PRESENT(LRUMaxHeapPolicy()) ysr@888: NOT_COMPILER2(LRUCurrentHeapPolicy()); ysr@888: if (_always_clear_soft_ref_policy == NULL || _default_soft_ref_policy == NULL) { ysr@888: vm_exit_during_initialization("Could not allocate reference policy object"); ysr@888: } duke@435: guarantee(RefDiscoveryPolicy == ReferenceBasedDiscovery || duke@435: RefDiscoveryPolicy == ReferentBasedDiscovery, duke@435: "Unrecongnized RefDiscoveryPolicy"); ysr@3117: _pending_list_uses_discovered_field = JDK_Version::current().pending_list_uses_discovered_field(); duke@435: } duke@435: johnc@3188: void ReferenceProcessor::enable_discovery(bool verify_disabled, bool check_no_refs) { johnc@3188: #ifdef ASSERT johnc@3188: // Verify that we're not currently discovering refs johnc@3188: assert(!verify_disabled || !_discovering_refs, "nested call?"); johnc@3188: johnc@3188: if (check_no_refs) { johnc@3188: // Verify that the discovered lists are empty johnc@3188: verify_no_references_recorded(); johnc@3188: } johnc@3188: #endif // ASSERT johnc@3188: johnc@3188: // Someone could have modified the value of the static johnc@3188: // field in the j.l.r.SoftReference class that holds the johnc@3188: // soft reference timestamp clock using reflection or johnc@3188: // Unsafe between GCs. Unconditionally update the static johnc@3188: // field in ReferenceProcessor here so that we use the new johnc@3188: // value during reference discovery. johnc@3188: johnc@3188: _soft_ref_timestamp_clock = java_lang_ref_SoftReference::clock(); johnc@3188: _discovering_refs = true; johnc@3188: } johnc@3188: duke@435: ReferenceProcessor::ReferenceProcessor(MemRegion span, ysr@2651: bool mt_processing, jmasa@3357: uint mt_processing_degree, ysr@2651: bool mt_discovery, jmasa@3357: uint mt_discovery_degree, coleenp@548: bool atomic_discovery, ysr@2651: BoolObjectClosure* is_alive_non_header, ysr@777: bool discovered_list_needs_barrier) : duke@435: _discovering_refs(false), duke@435: _enqueuing_is_done(false), ysr@2651: _is_alive_non_header(is_alive_non_header), ysr@777: _discovered_list_needs_barrier(discovered_list_needs_barrier), ysr@777: _bs(NULL), duke@435: _processing_is_mt(mt_processing), duke@435: _next_id(0) duke@435: { duke@435: _span = span; duke@435: _discovery_is_atomic = atomic_discovery; duke@435: _discovery_is_mt = mt_discovery; jmasa@3357: _num_q = MAX2(1U, mt_processing_degree); ysr@2651: _max_num_q = MAX2(_num_q, mt_discovery_degree); johnc@3210: _discovered_refs = NEW_C_HEAP_ARRAY(DiscoveredList, zgu@3900: _max_num_q * number_of_subclasses_of_ref(), mtGC); zgu@3900: johnc@3210: if (_discovered_refs == NULL) { duke@435: vm_exit_during_initialization("Could not allocated RefProc Array"); duke@435: } johnc@3210: _discoveredSoftRefs = &_discovered_refs[0]; jmasa@2188: _discoveredWeakRefs = &_discoveredSoftRefs[_max_num_q]; jmasa@2188: _discoveredFinalRefs = &_discoveredWeakRefs[_max_num_q]; jmasa@2188: _discoveredPhantomRefs = &_discoveredFinalRefs[_max_num_q]; johnc@3210: johnc@3210: // Initialize all entries to NULL jmasa@3357: for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { johnc@3210: _discovered_refs[i].set_head(NULL); johnc@3210: _discovered_refs[i].set_length(0); duke@435: } johnc@3210: ysr@3117: // If we do barriers, cache a copy of the barrier set. ysr@777: if (discovered_list_needs_barrier) { ysr@777: _bs = Universe::heap()->barrier_set(); ysr@777: } ysr@2651: setup_policy(false /* default soft ref policy */); duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void ReferenceProcessor::verify_no_references_recorded() { duke@435: guarantee(!_discovering_refs, "Discovering refs?"); jmasa@3357: for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { johnc@3210: guarantee(_discovered_refs[i].is_empty(), duke@435: "Found non-empty discovered list"); duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: void ReferenceProcessor::weak_oops_do(OopClosure* f) { jmasa@3357: for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { coleenp@548: if (UseCompressedOops) { johnc@3210: f->do_oop((narrowOop*)_discovered_refs[i].adr_head()); coleenp@548: } else { johnc@3210: f->do_oop((oop*)_discovered_refs[i].adr_head()); coleenp@548: } duke@435: } duke@435: } duke@435: coleenp@548: void ReferenceProcessor::update_soft_ref_master_clock() { duke@435: // Update (advance) the soft ref master clock field. This must be done duke@435: // after processing the soft ref list. johnc@3339: johnc@3339: // We need a monotonically non-deccreasing time in ms but johnc@3339: // os::javaTimeMillis() does not guarantee monotonicity. johnc@3339: jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC; johnc@3188: jlong soft_ref_clock = java_lang_ref_SoftReference::clock(); johnc@3188: assert(soft_ref_clock == _soft_ref_timestamp_clock, "soft ref clocks out of sync"); johnc@3188: duke@435: NOT_PRODUCT( johnc@3188: if (now < _soft_ref_timestamp_clock) { johnc@3188: warning("time warp: "INT64_FORMAT" to "INT64_FORMAT, johnc@3188: _soft_ref_timestamp_clock, now); duke@435: } duke@435: ) johnc@3339: // The values of now and _soft_ref_timestamp_clock are set using johnc@3339: // javaTimeNanos(), which is guaranteed to be monotonically johnc@3339: // non-decreasing provided the underlying platform provides such johnc@3339: // a time source (and it is bug free). johnc@3339: // In product mode, however, protect ourselves from non-monotonicty. johnc@3188: if (now > _soft_ref_timestamp_clock) { johnc@3188: _soft_ref_timestamp_clock = now; duke@435: java_lang_ref_SoftReference::set_clock(now); duke@435: } duke@435: // Else leave clock stalled at its old value until time progresses duke@435: // past clock value. duke@435: } duke@435: sla@5237: size_t ReferenceProcessor::total_count(DiscoveredList lists[]) { sla@5237: size_t total = 0; sla@5237: for (uint i = 0; i < _max_num_q; ++i) { sla@5237: total += lists[i].length(); sla@5237: } sla@5237: return total; sla@5237: } sla@5237: sla@5237: ReferenceProcessorStats ReferenceProcessor::process_discovered_references( duke@435: BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: VoidClosure* complete_gc, sla@5237: AbstractRefProcTaskExecutor* task_executor, sla@5237: GCTimer* gc_timer) { duke@435: NOT_PRODUCT(verify_ok_to_handle_reflists()); duke@435: duke@435: assert(!enqueuing_is_done(), "If here enqueuing should not be complete"); duke@435: // Stop treating discovered references specially. duke@435: disable_discovery(); duke@435: johnc@3188: // If discovery was concurrent, someone could have modified johnc@3188: // the value of the static field in the j.l.r.SoftReference johnc@3188: // class that holds the soft reference timestamp clock using johnc@3188: // reflection or Unsafe between when discovery was enabled and johnc@3188: // now. Unconditionally update the static field in ReferenceProcessor johnc@3188: // here so that we use the new value during processing of the johnc@3188: // discovered soft refs. johnc@3188: johnc@3188: _soft_ref_timestamp_clock = java_lang_ref_SoftReference::clock(); johnc@3188: duke@435: bool trace_time = PrintGCDetails && PrintReferenceGC; sla@5237: duke@435: // Soft references sla@5237: size_t soft_count = 0; duke@435: { sla@5237: GCTraceTime tt("SoftReference", trace_time, false, gc_timer); sla@5237: soft_count = sla@5237: process_discovered_reflist(_discoveredSoftRefs, _current_soft_ref_policy, true, sla@5237: is_alive, keep_alive, complete_gc, task_executor); duke@435: } duke@435: duke@435: update_soft_ref_master_clock(); duke@435: duke@435: // Weak references sla@5237: size_t weak_count = 0; duke@435: { sla@5237: GCTraceTime tt("WeakReference", trace_time, false, gc_timer); sla@5237: weak_count = sla@5237: process_discovered_reflist(_discoveredWeakRefs, NULL, true, sla@5237: is_alive, keep_alive, complete_gc, task_executor); duke@435: } duke@435: duke@435: // Final references sla@5237: size_t final_count = 0; duke@435: { sla@5237: GCTraceTime tt("FinalReference", trace_time, false, gc_timer); sla@5237: final_count = sla@5237: process_discovered_reflist(_discoveredFinalRefs, NULL, false, sla@5237: is_alive, keep_alive, complete_gc, task_executor); duke@435: } duke@435: duke@435: // Phantom references sla@5237: size_t phantom_count = 0; duke@435: { sla@5237: GCTraceTime tt("PhantomReference", trace_time, false, gc_timer); sla@5237: phantom_count = sla@5237: process_discovered_reflist(_discoveredPhantomRefs, NULL, false, sla@5237: is_alive, keep_alive, complete_gc, task_executor); duke@435: } duke@435: duke@435: // Weak global JNI references. It would make more sense (semantically) to duke@435: // traverse these simultaneously with the regular weak references above, but duke@435: // that is not how the JDK1.2 specification is. See #4126360. Native code can duke@435: // thus use JNI weak references to circumvent the phantom references and duke@435: // resurrect a "post-mortem" object. duke@435: { sla@5237: GCTraceTime tt("JNI Weak Reference", trace_time, false, gc_timer); duke@435: if (task_executor != NULL) { duke@435: task_executor->set_single_threaded_mode(); duke@435: } duke@435: process_phaseJNI(is_alive, keep_alive, complete_gc); duke@435: } sla@5237: sla@5237: return ReferenceProcessorStats(soft_count, weak_count, final_count, phantom_count); duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: // Calculate the number of jni handles. coleenp@548: uint ReferenceProcessor::count_jni_refs() { duke@435: class AlwaysAliveClosure: public BoolObjectClosure { duke@435: public: coleenp@548: virtual bool do_object_b(oop obj) { return true; } duke@435: }; duke@435: duke@435: class CountHandleClosure: public OopClosure { duke@435: private: duke@435: int _count; duke@435: public: duke@435: CountHandleClosure(): _count(0) {} coleenp@548: void do_oop(oop* unused) { _count++; } coleenp@548: void do_oop(narrowOop* unused) { ShouldNotReachHere(); } duke@435: int count() { return _count; } duke@435: }; duke@435: CountHandleClosure global_handle_count; duke@435: AlwaysAliveClosure always_alive; duke@435: JNIHandles::weak_oops_do(&always_alive, &global_handle_count); duke@435: return global_handle_count.count(); duke@435: } duke@435: #endif duke@435: duke@435: void ReferenceProcessor::process_phaseJNI(BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: VoidClosure* complete_gc) { duke@435: #ifndef PRODUCT duke@435: if (PrintGCDetails && PrintReferenceGC) { duke@435: unsigned int count = count_jni_refs(); duke@435: gclog_or_tty->print(", %u refs", count); duke@435: } duke@435: #endif duke@435: JNIHandles::weak_oops_do(is_alive, keep_alive); duke@435: complete_gc->do_void(); duke@435: } duke@435: coleenp@548: coleenp@548: template phh@1558: bool enqueue_discovered_ref_helper(ReferenceProcessor* ref, phh@1558: AbstractRefProcTaskExecutor* task_executor) { coleenp@548: duke@435: // Remember old value of pending references list coleenp@548: T* pending_list_addr = (T*)java_lang_ref_Reference::pending_list_addr(); coleenp@548: T old_pending_list_value = *pending_list_addr; duke@435: duke@435: // Enqueue references that are not made active again, and duke@435: // clear the decks for the next collection (cycle). coleenp@548: ref->enqueue_discovered_reflists((HeapWord*)pending_list_addr, task_executor); duke@435: // Do the oop-check on pending_list_addr missed in duke@435: // enqueue_discovered_reflist. We should probably duke@435: // do a raw oop_check so that future such idempotent duke@435: // oop_stores relying on the oop-check side-effect duke@435: // may be elided automatically and safely without duke@435: // affecting correctness. coleenp@548: oop_store(pending_list_addr, oopDesc::load_decode_heap_oop(pending_list_addr)); duke@435: duke@435: // Stop treating discovered references specially. coleenp@548: ref->disable_discovery(); duke@435: duke@435: // Return true if new pending references were added duke@435: return old_pending_list_value != *pending_list_addr; duke@435: } duke@435: coleenp@548: bool ReferenceProcessor::enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor) { coleenp@548: NOT_PRODUCT(verify_ok_to_handle_reflists()); coleenp@548: if (UseCompressedOops) { coleenp@548: return enqueue_discovered_ref_helper(this, task_executor); coleenp@548: } else { coleenp@548: return enqueue_discovered_ref_helper(this, task_executor); coleenp@548: } coleenp@548: } coleenp@548: duke@435: void ReferenceProcessor::enqueue_discovered_reflist(DiscoveredList& refs_list, coleenp@548: HeapWord* pending_list_addr) { duke@435: // Given a list of refs linked through the "discovered" field ysr@3117: // (java.lang.ref.Reference.discovered), self-loop their "next" field ysr@3117: // thus distinguishing them from active References, then ysr@3117: // prepend them to the pending list. ysr@3117: // BKWRD COMPATIBILITY NOTE: For older JDKs (prior to the fix for 4956777), ysr@3117: // the "next" field is used to chain the pending list, not the discovered ysr@3117: // field. ysr@3117: duke@435: if (TraceReferenceGC && PrintGCDetails) { duke@435: gclog_or_tty->print_cr("ReferenceProcessor::enqueue_discovered_reflist list " duke@435: INTPTR_FORMAT, (address)refs_list.head()); duke@435: } stefank@3115: stefank@3115: oop obj = NULL; ysr@3117: oop next_d = refs_list.head(); ysr@3117: if (pending_list_uses_discovered_field()) { // New behaviour ysr@3117: // Walk down the list, self-looping the next field ysr@3117: // so that the References are not considered active. ysr@3117: while (obj != next_d) { ysr@3117: obj = next_d; ysr@3117: assert(obj->is_instanceRef(), "should be reference object"); ysr@3117: next_d = java_lang_ref_Reference::discovered(obj); ysr@3117: if (TraceReferenceGC && PrintGCDetails) { ysr@3117: gclog_or_tty->print_cr(" obj " INTPTR_FORMAT "/next_d " INTPTR_FORMAT, hseigel@5784: (void *)obj, (void *)next_d); ysr@3117: } ysr@3117: assert(java_lang_ref_Reference::next(obj) == NULL, ysr@3117: "Reference not active; should not be discovered"); ysr@3117: // Self-loop next, so as to make Ref not active. ysr@3117: java_lang_ref_Reference::set_next(obj, obj); ysr@3117: if (next_d == obj) { // obj is last ysr@3117: // Swap refs_list into pendling_list_addr and ysr@3117: // set obj's discovered to what we read from pending_list_addr. ysr@3117: oop old = oopDesc::atomic_exchange_oop(refs_list.head(), pending_list_addr); ysr@3117: // Need oop_check on pending_list_addr above; ysr@3117: // see special oop-check code at the end of ysr@3117: // enqueue_discovered_reflists() further below. ysr@3117: java_lang_ref_Reference::set_discovered(obj, old); // old may be NULL ysr@3117: } duke@435: } ysr@3117: } else { // Old behaviour ysr@3117: // Walk down the list, copying the discovered field into ysr@3117: // the next field and clearing the discovered field. ysr@3117: while (obj != next_d) { ysr@3117: obj = next_d; ysr@3117: assert(obj->is_instanceRef(), "should be reference object"); ysr@3117: next_d = java_lang_ref_Reference::discovered(obj); ysr@3117: if (TraceReferenceGC && PrintGCDetails) { ysr@3117: gclog_or_tty->print_cr(" obj " INTPTR_FORMAT "/next_d " INTPTR_FORMAT, hseigel@5784: (void *)obj, (void *)next_d); ysr@3117: } ysr@3117: assert(java_lang_ref_Reference::next(obj) == NULL, ysr@3117: "The reference should not be enqueued"); ysr@3117: if (next_d == obj) { // obj is last ysr@3117: // Swap refs_list into pendling_list_addr and ysr@3117: // set obj's next to what we read from pending_list_addr. ysr@3117: oop old = oopDesc::atomic_exchange_oop(refs_list.head(), pending_list_addr); ysr@3117: // Need oop_check on pending_list_addr above; ysr@3117: // see special oop-check code at the end of ysr@3117: // enqueue_discovered_reflists() further below. ysr@3117: if (old == NULL) { ysr@3117: // obj should be made to point to itself, since ysr@3117: // pending list was empty. ysr@3117: java_lang_ref_Reference::set_next(obj, obj); ysr@3117: } else { ysr@3117: java_lang_ref_Reference::set_next(obj, old); ysr@3117: } duke@435: } else { ysr@3117: java_lang_ref_Reference::set_next(obj, next_d); duke@435: } ysr@3117: java_lang_ref_Reference::set_discovered(obj, (oop) NULL); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Parallel enqueue task duke@435: class RefProcEnqueueTask: public AbstractRefProcTaskExecutor::EnqueueTask { duke@435: public: duke@435: RefProcEnqueueTask(ReferenceProcessor& ref_processor, duke@435: DiscoveredList discovered_refs[], coleenp@548: HeapWord* pending_list_addr, duke@435: int n_queues) duke@435: : EnqueueTask(ref_processor, discovered_refs, stefank@3115: pending_list_addr, n_queues) duke@435: { } duke@435: coleenp@548: virtual void work(unsigned int work_id) { ysr@2651: assert(work_id < (unsigned int)_ref_processor.max_num_q(), "Index out-of-bounds"); duke@435: // Simplest first cut: static partitioning. duke@435: int index = work_id; jmasa@2188: // The increment on "index" must correspond to the maximum number of queues jmasa@2188: // (n_queues) with which that ReferenceProcessor was created. That jmasa@2188: // is because of the "clever" way the discovered references lists were ysr@2651: // allocated and are indexed into. ysr@2651: assert(_n_queues == (int) _ref_processor.max_num_q(), "Different number not expected"); jmasa@2188: for (int j = 0; johnc@3175: j < ReferenceProcessor::number_of_subclasses_of_ref(); jmasa@2188: j++, index += _n_queues) { duke@435: _ref_processor.enqueue_discovered_reflist( duke@435: _refs_lists[index], _pending_list_addr); stefank@3115: _refs_lists[index].set_head(NULL); duke@435: _refs_lists[index].set_length(0); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: // Enqueue references that are not made active again coleenp@548: void ReferenceProcessor::enqueue_discovered_reflists(HeapWord* pending_list_addr, duke@435: AbstractRefProcTaskExecutor* task_executor) { duke@435: if (_processing_is_mt && task_executor != NULL) { duke@435: // Parallel code johnc@3210: RefProcEnqueueTask tsk(*this, _discovered_refs, stefank@3115: pending_list_addr, _max_num_q); duke@435: task_executor->execute(tsk); duke@435: } else { duke@435: // Serial code: call the parent class's implementation jmasa@3357: for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { johnc@3210: enqueue_discovered_reflist(_discovered_refs[i], pending_list_addr); johnc@3210: _discovered_refs[i].set_head(NULL); johnc@3210: _discovered_refs[i].set_length(0); duke@435: } duke@435: } duke@435: } duke@435: johnc@3175: void DiscoveredListIterator::load_ptrs(DEBUG_ONLY(bool allow_null_referent)) { duke@435: _discovered_addr = java_lang_ref_Reference::discovered_addr(_ref); coleenp@548: oop discovered = java_lang_ref_Reference::discovered(_ref); coleenp@548: assert(_discovered_addr && discovered->is_oop_or_null(), duke@435: "discovered field is bad"); coleenp@548: _next = discovered; duke@435: _referent_addr = java_lang_ref_Reference::referent_addr(_ref); coleenp@548: _referent = java_lang_ref_Reference::referent(_ref); duke@435: assert(Universe::heap()->is_in_reserved_or_null(_referent), duke@435: "Wrong oop found in java.lang.Reference object"); duke@435: assert(allow_null_referent ? duke@435: _referent->is_oop_or_null() duke@435: : _referent->is_oop(), duke@435: "bad referent"); duke@435: } duke@435: johnc@3175: void DiscoveredListIterator::remove() { duke@435: assert(_ref->is_oop(), "Dropping a bad reference"); coleenp@548: oop_store_raw(_discovered_addr, NULL); stefank@3115: coleenp@548: // First _prev_next ref actually points into DiscoveredList (gross). stefank@3115: oop new_next; stefank@3115: if (_next == _ref) { stefank@3115: // At the end of the list, we should make _prev point to itself. stefank@3115: // If _ref is the first ref, then _prev_next will be in the DiscoveredList, stefank@3115: // and _prev will be NULL. stefank@3115: new_next = _prev; stefank@3115: } else { stefank@3115: new_next = _next; stefank@3115: } stefank@3115: coleenp@548: if (UseCompressedOops) { coleenp@548: // Remove Reference object from list. stefank@3115: oopDesc::encode_store_heap_oop((narrowOop*)_prev_next, new_next); coleenp@548: } else { coleenp@548: // Remove Reference object from list. stefank@3115: oopDesc::store_heap_oop((oop*)_prev_next, new_next); coleenp@548: } duke@435: NOT_PRODUCT(_removed++); ysr@887: _refs_list.dec_length(1); duke@435: } duke@435: johnc@3175: // Make the Reference object active again. johnc@3175: void DiscoveredListIterator::make_active() { johnc@3175: // For G1 we don't want to use set_next - it johnc@3175: // will dirty the card for the next field of johnc@3175: // the reference object and will fail johnc@3175: // CT verification. johnc@3175: if (UseG1GC) { johnc@3175: BarrierSet* bs = oopDesc::bs(); johnc@3175: HeapWord* next_addr = java_lang_ref_Reference::next_addr(_ref); johnc@3175: johnc@3175: if (UseCompressedOops) { johnc@3175: bs->write_ref_field_pre((narrowOop*)next_addr, NULL); johnc@3175: } else { johnc@3175: bs->write_ref_field_pre((oop*)next_addr, NULL); johnc@3175: } johnc@3175: java_lang_ref_Reference::set_next_raw(_ref, NULL); stefank@3115: } else { johnc@3175: java_lang_ref_Reference::set_next(_ref, NULL); stefank@3115: } johnc@3175: } johnc@3175: johnc@3175: void DiscoveredListIterator::clear_referent() { johnc@3175: oop_store_raw(_referent_addr, NULL); duke@435: } duke@435: duke@435: // NOTE: process_phase*() are largely similar, and at a high level duke@435: // merely iterate over the extant list applying a predicate to duke@435: // each of its elements and possibly removing that element from the duke@435: // list and applying some further closures to that element. duke@435: // We should consider the possibility of replacing these duke@435: // process_phase*() methods by abstracting them into duke@435: // a single general iterator invocation that receives appropriate duke@435: // closures that accomplish this work. duke@435: duke@435: // (SoftReferences only) Traverse the list and remove any SoftReferences whose duke@435: // referents are not alive, but that should be kept alive for policy reasons. duke@435: // Keep alive the transitive closure of all such referents. duke@435: void coleenp@548: ReferenceProcessor::process_phase1(DiscoveredList& refs_list, duke@435: ReferencePolicy* policy, duke@435: BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: VoidClosure* complete_gc) { duke@435: assert(policy != NULL, "Must have a non-NULL policy"); coleenp@548: DiscoveredListIterator iter(refs_list, keep_alive, is_alive); duke@435: // Decide which softly reachable refs should be kept alive. duke@435: while (iter.has_next()) { duke@435: iter.load_ptrs(DEBUG_ONLY(!discovery_is_atomic() /* allow_null_referent */)); duke@435: bool referent_is_dead = (iter.referent() != NULL) && !iter.is_referent_alive(); johnc@3188: if (referent_is_dead && johnc@3188: !policy->should_clear_reference(iter.obj(), _soft_ref_timestamp_clock)) { duke@435: if (TraceReferenceGC) { duke@435: gclog_or_tty->print_cr("Dropping reference (" INTPTR_FORMAT ": %s" ") by policy", hseigel@5784: (void *)iter.obj(), iter.obj()->klass()->internal_name()); duke@435: } ysr@887: // Remove Reference object from list ysr@887: iter.remove(); duke@435: // Make the Reference object active again duke@435: iter.make_active(); duke@435: // keep the referent around duke@435: iter.make_referent_alive(); ysr@887: iter.move_to_next(); duke@435: } else { duke@435: iter.next(); duke@435: } duke@435: } duke@435: // Close the reachable set duke@435: complete_gc->do_void(); duke@435: NOT_PRODUCT( duke@435: if (PrintGCDetails && TraceReferenceGC) { jmasa@2188: gclog_or_tty->print_cr(" Dropped %d dead Refs out of %d " ysr@3117: "discovered Refs by policy, from list " INTPTR_FORMAT, jmasa@2188: iter.removed(), iter.processed(), (address)refs_list.head()); duke@435: } duke@435: ) duke@435: } duke@435: duke@435: // Traverse the list and remove any Refs that are not active, or duke@435: // whose referents are either alive or NULL. duke@435: void coleenp@548: ReferenceProcessor::pp2_work(DiscoveredList& refs_list, duke@435: BoolObjectClosure* is_alive, coleenp@548: OopClosure* keep_alive) { duke@435: assert(discovery_is_atomic(), "Error"); coleenp@548: DiscoveredListIterator iter(refs_list, keep_alive, is_alive); duke@435: while (iter.has_next()) { duke@435: iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */)); coleenp@548: DEBUG_ONLY(oop next = java_lang_ref_Reference::next(iter.obj());) coleenp@548: assert(next == NULL, "Should not discover inactive Reference"); duke@435: if (iter.is_referent_alive()) { duke@435: if (TraceReferenceGC) { duke@435: gclog_or_tty->print_cr("Dropping strongly reachable reference (" INTPTR_FORMAT ": %s)", hseigel@5784: (void *)iter.obj(), iter.obj()->klass()->internal_name()); duke@435: } duke@435: // The referent is reachable after all. ysr@887: // Remove Reference object from list. ysr@887: iter.remove(); duke@435: // Update the referent pointer as necessary: Note that this duke@435: // should not entail any recursive marking because the duke@435: // referent must already have been traversed. duke@435: iter.make_referent_alive(); ysr@887: iter.move_to_next(); duke@435: } else { duke@435: iter.next(); duke@435: } duke@435: } duke@435: NOT_PRODUCT( ysr@2651: if (PrintGCDetails && TraceReferenceGC && (iter.processed() > 0)) { jmasa@2188: gclog_or_tty->print_cr(" Dropped %d active Refs out of %d " jmasa@2188: "Refs in discovered list " INTPTR_FORMAT, jmasa@2188: iter.removed(), iter.processed(), (address)refs_list.head()); duke@435: } duke@435: ) duke@435: } duke@435: duke@435: void coleenp@548: ReferenceProcessor::pp2_work_concurrent_discovery(DiscoveredList& refs_list, coleenp@548: BoolObjectClosure* is_alive, coleenp@548: OopClosure* keep_alive, coleenp@548: VoidClosure* complete_gc) { duke@435: assert(!discovery_is_atomic(), "Error"); coleenp@548: DiscoveredListIterator iter(refs_list, keep_alive, is_alive); duke@435: while (iter.has_next()) { duke@435: iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */)); coleenp@548: HeapWord* next_addr = java_lang_ref_Reference::next_addr(iter.obj()); coleenp@548: oop next = java_lang_ref_Reference::next(iter.obj()); duke@435: if ((iter.referent() == NULL || iter.is_referent_alive() || coleenp@548: next != NULL)) { coleenp@548: assert(next->is_oop_or_null(), "bad next field"); duke@435: // Remove Reference object from list duke@435: iter.remove(); duke@435: // Trace the cohorts duke@435: iter.make_referent_alive(); coleenp@548: if (UseCompressedOops) { coleenp@548: keep_alive->do_oop((narrowOop*)next_addr); coleenp@548: } else { coleenp@548: keep_alive->do_oop((oop*)next_addr); coleenp@548: } ysr@887: iter.move_to_next(); duke@435: } else { duke@435: iter.next(); duke@435: } duke@435: } duke@435: // Now close the newly reachable set duke@435: complete_gc->do_void(); duke@435: NOT_PRODUCT( ysr@2651: if (PrintGCDetails && TraceReferenceGC && (iter.processed() > 0)) { jmasa@2188: gclog_or_tty->print_cr(" Dropped %d active Refs out of %d " jmasa@2188: "Refs in discovered list " INTPTR_FORMAT, jmasa@2188: iter.removed(), iter.processed(), (address)refs_list.head()); duke@435: } duke@435: ) duke@435: } duke@435: duke@435: // Traverse the list and process the referents, by either coleenp@548: // clearing them or keeping them (and their reachable duke@435: // closure) alive. duke@435: void coleenp@548: ReferenceProcessor::process_phase3(DiscoveredList& refs_list, duke@435: bool clear_referent, duke@435: BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: VoidClosure* complete_gc) { jmasa@2188: ResourceMark rm; coleenp@548: DiscoveredListIterator iter(refs_list, keep_alive, is_alive); duke@435: while (iter.has_next()) { duke@435: iter.update_discovered(); duke@435: iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */)); duke@435: if (clear_referent) { duke@435: // NULL out referent pointer duke@435: iter.clear_referent(); duke@435: } else { duke@435: // keep the referent around duke@435: iter.make_referent_alive(); duke@435: } duke@435: if (TraceReferenceGC) { duke@435: gclog_or_tty->print_cr("Adding %sreference (" INTPTR_FORMAT ": %s) as pending", duke@435: clear_referent ? "cleared " : "", hseigel@5784: (void *)iter.obj(), iter.obj()->klass()->internal_name()); duke@435: } duke@435: assert(iter.obj()->is_oop(UseConcMarkSweepGC), "Adding a bad reference"); duke@435: iter.next(); duke@435: } stefank@3115: // Remember to update the next pointer of the last ref. duke@435: iter.update_discovered(); duke@435: // Close the reachable set duke@435: complete_gc->do_void(); duke@435: } duke@435: duke@435: void stefank@3115: ReferenceProcessor::clear_discovered_references(DiscoveredList& refs_list) { stefank@3115: oop obj = NULL; stefank@3115: oop next = refs_list.head(); stefank@3115: while (next != obj) { stefank@3115: obj = next; stefank@3115: next = java_lang_ref_Reference::discovered(obj); stefank@3115: java_lang_ref_Reference::set_discovered_raw(obj, NULL); stefank@3115: } stefank@3115: refs_list.set_head(NULL); stefank@3115: refs_list.set_length(0); stefank@3115: } stefank@3115: stefank@3115: void coleenp@548: ReferenceProcessor::abandon_partial_discovered_list(DiscoveredList& refs_list) { stefank@3115: clear_discovered_references(refs_list); duke@435: } duke@435: ysr@777: void ReferenceProcessor::abandon_partial_discovery() { ysr@777: // loop over the lists jmasa@3357: for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { jmasa@2188: if (TraceReferenceGC && PrintGCDetails && ((i % _max_num_q) == 0)) { johnc@3175: gclog_or_tty->print_cr("\nAbandoning %s discovered list", list_name(i)); ysr@777: } johnc@3210: abandon_partial_discovered_list(_discovered_refs[i]); duke@435: } duke@435: } duke@435: duke@435: class RefProcPhase1Task: public AbstractRefProcTaskExecutor::ProcessTask { duke@435: public: duke@435: RefProcPhase1Task(ReferenceProcessor& ref_processor, duke@435: DiscoveredList refs_lists[], duke@435: ReferencePolicy* policy, duke@435: bool marks_oops_alive) duke@435: : ProcessTask(ref_processor, refs_lists, marks_oops_alive), duke@435: _policy(policy) duke@435: { } duke@435: virtual void work(unsigned int i, BoolObjectClosure& is_alive, duke@435: OopClosure& keep_alive, duke@435: VoidClosure& complete_gc) duke@435: { jmasa@2188: Thread* thr = Thread::current(); jmasa@2188: int refs_list_index = ((WorkerThread*)thr)->id(); jmasa@2188: _ref_processor.process_phase1(_refs_lists[refs_list_index], _policy, duke@435: &is_alive, &keep_alive, &complete_gc); duke@435: } duke@435: private: duke@435: ReferencePolicy* _policy; duke@435: }; duke@435: duke@435: class RefProcPhase2Task: public AbstractRefProcTaskExecutor::ProcessTask { duke@435: public: duke@435: RefProcPhase2Task(ReferenceProcessor& ref_processor, duke@435: DiscoveredList refs_lists[], duke@435: bool marks_oops_alive) duke@435: : ProcessTask(ref_processor, refs_lists, marks_oops_alive) duke@435: { } duke@435: virtual void work(unsigned int i, BoolObjectClosure& is_alive, duke@435: OopClosure& keep_alive, duke@435: VoidClosure& complete_gc) duke@435: { duke@435: _ref_processor.process_phase2(_refs_lists[i], duke@435: &is_alive, &keep_alive, &complete_gc); duke@435: } duke@435: }; duke@435: duke@435: class RefProcPhase3Task: public AbstractRefProcTaskExecutor::ProcessTask { duke@435: public: duke@435: RefProcPhase3Task(ReferenceProcessor& ref_processor, duke@435: DiscoveredList refs_lists[], duke@435: bool clear_referent, duke@435: bool marks_oops_alive) duke@435: : ProcessTask(ref_processor, refs_lists, marks_oops_alive), duke@435: _clear_referent(clear_referent) duke@435: { } duke@435: virtual void work(unsigned int i, BoolObjectClosure& is_alive, duke@435: OopClosure& keep_alive, duke@435: VoidClosure& complete_gc) duke@435: { jmasa@2188: // Don't use "refs_list_index" calculated in this way because jmasa@2188: // balance_queues() has moved the Ref's into the first n queues. jmasa@2188: // Thread* thr = Thread::current(); jmasa@2188: // int refs_list_index = ((WorkerThread*)thr)->id(); jmasa@2188: // _ref_processor.process_phase3(_refs_lists[refs_list_index], _clear_referent, duke@435: _ref_processor.process_phase3(_refs_lists[i], _clear_referent, duke@435: &is_alive, &keep_alive, &complete_gc); duke@435: } duke@435: private: duke@435: bool _clear_referent; duke@435: }; duke@435: johnc@3175: void ReferenceProcessor::set_discovered(oop ref, oop value) { johnc@3175: if (_discovered_list_needs_barrier) { johnc@3175: java_lang_ref_Reference::set_discovered(ref, value); johnc@3175: } else { johnc@3175: java_lang_ref_Reference::set_discovered_raw(ref, value); johnc@3175: } johnc@3175: } johnc@3175: duke@435: // Balances reference queues. jmasa@2188: // Move entries from all queues[0, 1, ..., _max_num_q-1] to jmasa@2188: // queues[0, 1, ..., _num_q-1] because only the first _num_q jmasa@2188: // corresponding to the active workers will be processed. duke@435: void ReferenceProcessor::balance_queues(DiscoveredList ref_lists[]) duke@435: { duke@435: // calculate total length duke@435: size_t total_refs = 0; jmasa@2188: if (TraceReferenceGC && PrintGCDetails) { jmasa@2188: gclog_or_tty->print_cr("\nBalance ref_lists "); jmasa@2188: } jmasa@2188: jmasa@3357: for (uint i = 0; i < _max_num_q; ++i) { duke@435: total_refs += ref_lists[i].length(); jmasa@2188: if (TraceReferenceGC && PrintGCDetails) { jmasa@2188: gclog_or_tty->print("%d ", ref_lists[i].length()); jmasa@2188: } jmasa@2188: } jmasa@2188: if (TraceReferenceGC && PrintGCDetails) { jmasa@2188: gclog_or_tty->print_cr(" = %d", total_refs); duke@435: } duke@435: size_t avg_refs = total_refs / _num_q + 1; jmasa@3357: uint to_idx = 0; jmasa@3357: for (uint from_idx = 0; from_idx < _max_num_q; from_idx++) { jmasa@2188: bool move_all = false; jmasa@2188: if (from_idx >= _num_q) { jmasa@2188: move_all = ref_lists[from_idx].length() > 0; jmasa@2188: } jmasa@2188: while ((ref_lists[from_idx].length() > avg_refs) || jmasa@2188: move_all) { duke@435: assert(to_idx < _num_q, "Sanity Check!"); duke@435: if (ref_lists[to_idx].length() < avg_refs) { duke@435: // move superfluous refs jmasa@2188: size_t refs_to_move; jmasa@2188: // Move all the Ref's if the from queue will not be processed. jmasa@2188: if (move_all) { jmasa@2188: refs_to_move = MIN2(ref_lists[from_idx].length(), jmasa@2188: avg_refs - ref_lists[to_idx].length()); jmasa@2188: } else { jmasa@2188: refs_to_move = MIN2(ref_lists[from_idx].length() - avg_refs, jmasa@2188: avg_refs - ref_lists[to_idx].length()); jmasa@2188: } stefank@3115: stefank@3115: assert(refs_to_move > 0, "otherwise the code below will fail"); stefank@3115: duke@435: oop move_head = ref_lists[from_idx].head(); duke@435: oop move_tail = move_head; duke@435: oop new_head = move_head; duke@435: // find an element to split the list on duke@435: for (size_t j = 0; j < refs_to_move; ++j) { duke@435: move_tail = new_head; coleenp@548: new_head = java_lang_ref_Reference::discovered(new_head); duke@435: } stefank@3115: stefank@3115: // Add the chain to the to list. stefank@3115: if (ref_lists[to_idx].head() == NULL) { stefank@3115: // to list is empty. Make a loop at the end. johnc@3175: set_discovered(move_tail, move_tail); stefank@3115: } else { johnc@3175: set_discovered(move_tail, ref_lists[to_idx].head()); stefank@3115: } duke@435: ref_lists[to_idx].set_head(move_head); ysr@887: ref_lists[to_idx].inc_length(refs_to_move); stefank@3115: stefank@3115: // Remove the chain from the from list. stefank@3115: if (move_tail == new_head) { stefank@3115: // We found the end of the from list. stefank@3115: ref_lists[from_idx].set_head(NULL); stefank@3115: } else { stefank@3115: ref_lists[from_idx].set_head(new_head); stefank@3115: } ysr@887: ref_lists[from_idx].dec_length(refs_to_move); jmasa@2188: if (ref_lists[from_idx].length() == 0) { jmasa@2188: break; jmasa@2188: } duke@435: } else { jmasa@2188: to_idx = (to_idx + 1) % _num_q; duke@435: } duke@435: } duke@435: } jmasa@2188: #ifdef ASSERT jmasa@2188: size_t balanced_total_refs = 0; jmasa@3357: for (uint i = 0; i < _max_num_q; ++i) { jmasa@2188: balanced_total_refs += ref_lists[i].length(); jmasa@2188: if (TraceReferenceGC && PrintGCDetails) { jmasa@2188: gclog_or_tty->print("%d ", ref_lists[i].length()); jmasa@2188: } jmasa@2188: } jmasa@2188: if (TraceReferenceGC && PrintGCDetails) { jmasa@2188: gclog_or_tty->print_cr(" = %d", balanced_total_refs); jmasa@2188: gclog_or_tty->flush(); jmasa@2188: } jmasa@2188: assert(total_refs == balanced_total_refs, "Balancing was incomplete"); jmasa@2188: #endif jmasa@2188: } jmasa@2188: jmasa@2188: void ReferenceProcessor::balance_all_queues() { jmasa@2188: balance_queues(_discoveredSoftRefs); jmasa@2188: balance_queues(_discoveredWeakRefs); jmasa@2188: balance_queues(_discoveredFinalRefs); jmasa@2188: balance_queues(_discoveredPhantomRefs); duke@435: } duke@435: sla@5237: size_t duke@435: ReferenceProcessor::process_discovered_reflist( duke@435: DiscoveredList refs_lists[], duke@435: ReferencePolicy* policy, duke@435: bool clear_referent, duke@435: BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: VoidClosure* complete_gc, duke@435: AbstractRefProcTaskExecutor* task_executor) duke@435: { jmasa@2188: bool mt_processing = task_executor != NULL && _processing_is_mt; jmasa@2188: // If discovery used MT and a dynamic number of GC threads, then jmasa@2188: // the queues must be balanced for correctness if fewer than the jmasa@2188: // maximum number of queues were used. The number of queue used jmasa@2188: // during discovery may be different than the number to be used jmasa@2188: // for processing so don't depend of _num_q < _max_num_q as part jmasa@2188: // of the test. jmasa@2188: bool must_balance = _discovery_is_mt; jmasa@2188: jmasa@2188: if ((mt_processing && ParallelRefProcBalancingEnabled) || jmasa@2188: must_balance) { duke@435: balance_queues(refs_lists); duke@435: } sla@5237: sla@5237: size_t total_list_count = total_count(refs_lists); sla@5237: duke@435: if (PrintReferenceGC && PrintGCDetails) { sla@5237: gclog_or_tty->print(", %u refs", total_list_count); duke@435: } duke@435: duke@435: // Phase 1 (soft refs only): duke@435: // . Traverse the list and remove any SoftReferences whose duke@435: // referents are not alive, but that should be kept alive for duke@435: // policy reasons. Keep alive the transitive closure of all duke@435: // such referents. duke@435: if (policy != NULL) { jmasa@2188: if (mt_processing) { duke@435: RefProcPhase1Task phase1(*this, refs_lists, policy, true /*marks_oops_alive*/); duke@435: task_executor->execute(phase1); duke@435: } else { jmasa@3357: for (uint i = 0; i < _max_num_q; i++) { duke@435: process_phase1(refs_lists[i], policy, duke@435: is_alive, keep_alive, complete_gc); duke@435: } duke@435: } duke@435: } else { // policy == NULL duke@435: assert(refs_lists != _discoveredSoftRefs, duke@435: "Policy must be specified for soft references."); duke@435: } duke@435: duke@435: // Phase 2: duke@435: // . Traverse the list and remove any refs whose referents are alive. jmasa@2188: if (mt_processing) { duke@435: RefProcPhase2Task phase2(*this, refs_lists, !discovery_is_atomic() /*marks_oops_alive*/); duke@435: task_executor->execute(phase2); duke@435: } else { jmasa@3357: for (uint i = 0; i < _max_num_q; i++) { duke@435: process_phase2(refs_lists[i], is_alive, keep_alive, complete_gc); duke@435: } duke@435: } duke@435: duke@435: // Phase 3: duke@435: // . Traverse the list and process referents as appropriate. jmasa@2188: if (mt_processing) { duke@435: RefProcPhase3Task phase3(*this, refs_lists, clear_referent, true /*marks_oops_alive*/); duke@435: task_executor->execute(phase3); duke@435: } else { jmasa@3357: for (uint i = 0; i < _max_num_q; i++) { duke@435: process_phase3(refs_lists[i], clear_referent, duke@435: is_alive, keep_alive, complete_gc); duke@435: } duke@435: } sla@5237: sla@5237: return total_list_count; duke@435: } duke@435: duke@435: void ReferenceProcessor::clean_up_discovered_references() { duke@435: // loop over the lists jmasa@3357: for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { ysr@2651: if (TraceReferenceGC && PrintGCDetails && ((i % _max_num_q) == 0)) { duke@435: gclog_or_tty->print_cr( duke@435: "\nScrubbing %s discovered list of Null referents", duke@435: list_name(i)); duke@435: } johnc@3210: clean_up_discovered_reflist(_discovered_refs[i]); duke@435: } duke@435: } duke@435: duke@435: void ReferenceProcessor::clean_up_discovered_reflist(DiscoveredList& refs_list) { duke@435: assert(!discovery_is_atomic(), "Else why call this method?"); duke@435: DiscoveredListIterator iter(refs_list, NULL, NULL); duke@435: while (iter.has_next()) { duke@435: iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */)); coleenp@548: oop next = java_lang_ref_Reference::next(iter.obj()); coleenp@548: assert(next->is_oop_or_null(), "bad next field"); duke@435: // If referent has been cleared or Reference is not active, duke@435: // drop it. coleenp@548: if (iter.referent() == NULL || next != NULL) { duke@435: debug_only( duke@435: if (PrintGCDetails && TraceReferenceGC) { duke@435: gclog_or_tty->print_cr("clean_up_discovered_list: Dropping Reference: " duke@435: INTPTR_FORMAT " with next field: " INTPTR_FORMAT duke@435: " and referent: " INTPTR_FORMAT, hseigel@5784: (void *)iter.obj(), (void *)next, (void *)iter.referent()); duke@435: } duke@435: ) duke@435: // Remove Reference object from list duke@435: iter.remove(); ysr@887: iter.move_to_next(); duke@435: } else { duke@435: iter.next(); duke@435: } duke@435: } duke@435: NOT_PRODUCT( duke@435: if (PrintGCDetails && TraceReferenceGC) { duke@435: gclog_or_tty->print( duke@435: " Removed %d Refs with NULL referents out of %d discovered Refs", duke@435: iter.removed(), iter.processed()); duke@435: } duke@435: ) duke@435: } duke@435: duke@435: inline DiscoveredList* ReferenceProcessor::get_discovered_list(ReferenceType rt) { jmasa@3357: uint id = 0; duke@435: // Determine the queue index to use for this object. duke@435: if (_discovery_is_mt) { duke@435: // During a multi-threaded discovery phase, duke@435: // each thread saves to its "own" list. duke@435: Thread* thr = Thread::current(); johnc@2316: id = thr->as_Worker_thread()->id(); duke@435: } else { duke@435: // single-threaded discovery, we save in round-robin duke@435: // fashion to each of the lists. duke@435: if (_processing_is_mt) { duke@435: id = next_id(); duke@435: } duke@435: } jmasa@2188: assert(0 <= id && id < _max_num_q, "Id is out-of-bounds (call Freud?)"); duke@435: duke@435: // Get the discovered queue to which we will add duke@435: DiscoveredList* list = NULL; duke@435: switch (rt) { duke@435: case REF_OTHER: duke@435: // Unknown reference type, no special treatment duke@435: break; duke@435: case REF_SOFT: duke@435: list = &_discoveredSoftRefs[id]; duke@435: break; duke@435: case REF_WEAK: duke@435: list = &_discoveredWeakRefs[id]; duke@435: break; duke@435: case REF_FINAL: duke@435: list = &_discoveredFinalRefs[id]; duke@435: break; duke@435: case REF_PHANTOM: duke@435: list = &_discoveredPhantomRefs[id]; duke@435: break; duke@435: case REF_NONE: coleenp@4047: // we should not reach here if we are an InstanceRefKlass duke@435: default: duke@435: ShouldNotReachHere(); duke@435: } jmasa@2188: if (TraceReferenceGC && PrintGCDetails) { johnc@2316: gclog_or_tty->print_cr("Thread %d gets list " INTPTR_FORMAT, id, list); jmasa@2188: } duke@435: return list; duke@435: } duke@435: coleenp@548: inline void coleenp@548: ReferenceProcessor::add_to_discovered_list_mt(DiscoveredList& refs_list, coleenp@548: oop obj, coleenp@548: HeapWord* discovered_addr) { duke@435: assert(_discovery_is_mt, "!_discovery_is_mt should have been handled by caller"); duke@435: // First we must make sure this object is only enqueued once. CAS in a non null duke@435: // discovered_addr. ysr@777: oop current_head = refs_list.head(); stefank@3115: // The last ref must have its discovered field pointing to itself. stefank@3115: oop next_discovered = (current_head != NULL) ? current_head : obj; ysr@777: ysr@1280: // Note: In the case of G1, this specific pre-barrier is strictly ysr@777: // not necessary because the only case we are interested in ysr@1280: // here is when *discovered_addr is NULL (see the CAS further below), ysr@1280: // so this will expand to nothing. As a result, we have manually ysr@1280: // elided this out for G1, but left in the test for some future ysr@3117: // collector that might have need for a pre-barrier here, e.g.:- ysr@3117: // _bs->write_ref_field_pre((oop* or narrowOop*)discovered_addr, next_discovered); ysr@3117: assert(!_discovered_list_needs_barrier || UseG1GC, ysr@3117: "Need to check non-G1 collector: " ysr@3117: "may need a pre-write-barrier for CAS from NULL below"); stefank@3115: oop retest = oopDesc::atomic_compare_exchange_oop(next_discovered, discovered_addr, coleenp@548: NULL); duke@435: if (retest == NULL) { duke@435: // This thread just won the right to enqueue the object. ysr@3117: // We have separate lists for enqueueing, so no synchronization duke@435: // is necessary. coleenp@548: refs_list.set_head(obj); ysr@887: refs_list.inc_length(1); ysr@777: if (_discovered_list_needs_barrier) { stefank@3115: _bs->write_ref_field((void*)discovered_addr, next_discovered); ysr@777: } johnc@2316: johnc@2316: if (TraceReferenceGC) { ysr@3117: gclog_or_tty->print_cr("Discovered reference (mt) (" INTPTR_FORMAT ": %s)", hseigel@5784: (void *)obj, obj->klass()->internal_name()); johnc@2316: } duke@435: } else { duke@435: // If retest was non NULL, another thread beat us to it: duke@435: // The reference has already been discovered... duke@435: if (TraceReferenceGC) { ysr@3117: gclog_or_tty->print_cr("Already discovered reference (" INTPTR_FORMAT ": %s)", hseigel@5784: (void *)obj, obj->klass()->internal_name()); duke@435: } duke@435: } duke@435: } duke@435: ysr@2337: #ifndef PRODUCT ysr@2337: // Non-atomic (i.e. concurrent) discovery might allow us ysr@2337: // to observe j.l.References with NULL referents, being those ysr@2337: // cleared concurrently by mutators during (or after) discovery. ysr@2337: void ReferenceProcessor::verify_referent(oop obj) { ysr@2337: bool da = discovery_is_atomic(); ysr@2337: oop referent = java_lang_ref_Reference::referent(obj); ysr@2337: assert(da ? referent->is_oop() : referent->is_oop_or_null(), ysr@2337: err_msg("Bad referent " INTPTR_FORMAT " found in Reference " ysr@2337: INTPTR_FORMAT " during %satomic discovery ", hseigel@5784: (void *)referent, (void *)obj, da ? "" : "non-")); ysr@2337: } ysr@2337: #endif ysr@2337: duke@435: // We mention two of several possible choices here: duke@435: // #0: if the reference object is not in the "originating generation" duke@435: // (or part of the heap being collected, indicated by our "span" duke@435: // we don't treat it specially (i.e. we scan it as we would duke@435: // a normal oop, treating its references as strong references). ysr@3117: // This means that references can't be discovered unless their duke@435: // referent is also in the same span. This is the simplest, duke@435: // most "local" and most conservative approach, albeit one duke@435: // that may cause weak references to be enqueued least promptly. duke@435: // We call this choice the "ReferenceBasedDiscovery" policy. duke@435: // #1: the reference object may be in any generation (span), but if duke@435: // the referent is in the generation (span) being currently collected duke@435: // then we can discover the reference object, provided duke@435: // the object has not already been discovered by duke@435: // a different concurrently running collector (as may be the duke@435: // case, for instance, if the reference object is in CMS and duke@435: // the referent in DefNewGeneration), and provided the processing duke@435: // of this reference object by the current collector will duke@435: // appear atomic to every other collector in the system. duke@435: // (Thus, for instance, a concurrent collector may not duke@435: // discover references in other generations even if the duke@435: // referent is in its own generation). This policy may, duke@435: // in certain cases, enqueue references somewhat sooner than duke@435: // might Policy #0 above, but at marginally increased cost duke@435: // and complexity in processing these references. duke@435: // We call this choice the "RefeferentBasedDiscovery" policy. duke@435: bool ReferenceProcessor::discover_reference(oop obj, ReferenceType rt) { ysr@3117: // Make sure we are discovering refs (rather than processing discovered refs). duke@435: if (!_discovering_refs || !RegisterReferences) { duke@435: return false; duke@435: } ysr@3117: // We only discover active references. coleenp@548: oop next = java_lang_ref_Reference::next(obj); ysr@3117: if (next != NULL) { // Ref is no longer active duke@435: return false; duke@435: } duke@435: duke@435: HeapWord* obj_addr = (HeapWord*)obj; duke@435: if (RefDiscoveryPolicy == ReferenceBasedDiscovery && duke@435: !_span.contains(obj_addr)) { duke@435: // Reference is not in the originating generation; duke@435: // don't treat it specially (i.e. we want to scan it as a normal duke@435: // object with strong references). duke@435: return false; duke@435: } duke@435: ysr@3117: // We only discover references whose referents are not (yet) ysr@3117: // known to be strongly reachable. duke@435: if (is_alive_non_header() != NULL) { ysr@2337: verify_referent(obj); ysr@2337: if (is_alive_non_header()->do_object_b(java_lang_ref_Reference::referent(obj))) { duke@435: return false; // referent is reachable duke@435: } duke@435: } ysr@888: if (rt == REF_SOFT) { ysr@888: // For soft refs we can decide now if these are not ysr@888: // current candidates for clearing, in which case we ysr@888: // can mark through them now, rather than delaying that ysr@888: // to the reference-processing phase. Since all current ysr@888: // time-stamp policies advance the soft-ref clock only ysr@888: // at a major collection cycle, this is always currently ysr@888: // accurate. johnc@3188: if (!_current_soft_ref_policy->should_clear_reference(obj, _soft_ref_timestamp_clock)) { ysr@888: return false; ysr@888: } ysr@888: } duke@435: johnc@3175: ResourceMark rm; // Needed for tracing. johnc@3175: ysr@777: HeapWord* const discovered_addr = java_lang_ref_Reference::discovered_addr(obj); ysr@777: const oop discovered = java_lang_ref_Reference::discovered(obj); coleenp@548: assert(discovered->is_oop_or_null(), "bad discovered field"); coleenp@548: if (discovered != NULL) { duke@435: // The reference has already been discovered... duke@435: if (TraceReferenceGC) { ysr@3117: gclog_or_tty->print_cr("Already discovered reference (" INTPTR_FORMAT ": %s)", hseigel@5784: (void *)obj, obj->klass()->internal_name()); duke@435: } duke@435: if (RefDiscoveryPolicy == ReferentBasedDiscovery) { duke@435: // assumes that an object is not processed twice; duke@435: // if it's been already discovered it must be on another duke@435: // generation's discovered list; so we won't discover it. duke@435: return false; duke@435: } else { duke@435: assert(RefDiscoveryPolicy == ReferenceBasedDiscovery, duke@435: "Unrecognized policy"); duke@435: // Check assumption that an object is not potentially duke@435: // discovered twice except by concurrent collectors that potentially duke@435: // trace the same Reference object twice. johnc@2316: assert(UseConcMarkSweepGC || UseG1GC, johnc@2316: "Only possible with a concurrent marking collector"); duke@435: return true; duke@435: } duke@435: } duke@435: duke@435: if (RefDiscoveryPolicy == ReferentBasedDiscovery) { ysr@2337: verify_referent(obj); ysr@3117: // Discover if and only if EITHER: ysr@3117: // .. reference is in our span, OR ysr@3117: // .. we are an atomic collector and referent is in our span duke@435: if (_span.contains(obj_addr) || ysr@2337: (discovery_is_atomic() && ysr@2337: _span.contains(java_lang_ref_Reference::referent(obj)))) { duke@435: // should_enqueue = true; duke@435: } else { duke@435: return false; duke@435: } duke@435: } else { duke@435: assert(RefDiscoveryPolicy == ReferenceBasedDiscovery && duke@435: _span.contains(obj_addr), "code inconsistency"); duke@435: } duke@435: duke@435: // Get the right type of discovered queue head. duke@435: DiscoveredList* list = get_discovered_list(rt); duke@435: if (list == NULL) { duke@435: return false; // nothing special needs to be done duke@435: } duke@435: duke@435: if (_discovery_is_mt) { duke@435: add_to_discovered_list_mt(*list, obj, discovered_addr); duke@435: } else { ysr@777: // If "_discovered_list_needs_barrier", we do write barriers when ysr@777: // updating the discovered reference list. Otherwise, we do a raw store ysr@777: // here: the field will be visited later when processing the discovered ysr@777: // references. ysr@777: oop current_head = list->head(); stefank@3115: // The last ref must have its discovered field pointing to itself. stefank@3115: oop next_discovered = (current_head != NULL) ? current_head : obj; stefank@3115: ysr@777: // As in the case further above, since we are over-writing a NULL ysr@777: // pre-value, we can safely elide the pre-barrier here for the case of G1. ysr@3117: // e.g.:- _bs->write_ref_field_pre((oop* or narrowOop*)discovered_addr, next_discovered); ysr@777: assert(discovered == NULL, "control point invariant"); ysr@3117: assert(!_discovered_list_needs_barrier || UseG1GC, ysr@3117: "For non-G1 collector, may need a pre-write-barrier for CAS from NULL below"); stefank@3115: oop_store_raw(discovered_addr, next_discovered); ysr@777: if (_discovered_list_needs_barrier) { stefank@3115: _bs->write_ref_field((void*)discovered_addr, next_discovered); ysr@777: } duke@435: list->set_head(obj); ysr@887: list->inc_length(1); duke@435: johnc@2316: if (TraceReferenceGC) { ysr@3117: gclog_or_tty->print_cr("Discovered reference (" INTPTR_FORMAT ": %s)", hseigel@5784: (void *)obj, obj->klass()->internal_name()); duke@435: } duke@435: } ysr@3117: assert(obj->is_oop(), "Discovered a bad reference"); ysr@2337: verify_referent(obj); duke@435: return true; duke@435: } duke@435: duke@435: // Preclean the discovered references by removing those duke@435: // whose referents are alive, and by marking from those that duke@435: // are not active. These lists can be handled here duke@435: // in any order and, indeed, concurrently. duke@435: void ReferenceProcessor::preclean_discovered_references( duke@435: BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: VoidClosure* complete_gc, sla@5237: YieldClosure* yield, sla@5237: GCTimer* gc_timer) { duke@435: duke@435: NOT_PRODUCT(verify_ok_to_handle_reflists()); duke@435: duke@435: // Soft references duke@435: { sla@5237: GCTraceTime tt("Preclean SoftReferences", PrintGCDetails && PrintReferenceGC, sla@5237: false, gc_timer); jmasa@3357: for (uint i = 0; i < _max_num_q; i++) { ysr@887: if (yield->should_return()) { ysr@887: return; ysr@887: } duke@435: preclean_discovered_reflist(_discoveredSoftRefs[i], is_alive, duke@435: keep_alive, complete_gc, yield); duke@435: } duke@435: } duke@435: duke@435: // Weak references duke@435: { sla@5237: GCTraceTime tt("Preclean WeakReferences", PrintGCDetails && PrintReferenceGC, sla@5237: false, gc_timer); jmasa@3357: for (uint i = 0; i < _max_num_q; i++) { ysr@887: if (yield->should_return()) { ysr@887: return; ysr@887: } duke@435: preclean_discovered_reflist(_discoveredWeakRefs[i], is_alive, duke@435: keep_alive, complete_gc, yield); duke@435: } duke@435: } duke@435: duke@435: // Final references duke@435: { sla@5237: GCTraceTime tt("Preclean FinalReferences", PrintGCDetails && PrintReferenceGC, sla@5237: false, gc_timer); jmasa@3357: for (uint i = 0; i < _max_num_q; i++) { ysr@887: if (yield->should_return()) { ysr@887: return; ysr@887: } duke@435: preclean_discovered_reflist(_discoveredFinalRefs[i], is_alive, duke@435: keep_alive, complete_gc, yield); duke@435: } duke@435: } duke@435: duke@435: // Phantom references duke@435: { sla@5237: GCTraceTime tt("Preclean PhantomReferences", PrintGCDetails && PrintReferenceGC, sla@5237: false, gc_timer); jmasa@3357: for (uint i = 0; i < _max_num_q; i++) { ysr@887: if (yield->should_return()) { ysr@887: return; ysr@887: } duke@435: preclean_discovered_reflist(_discoveredPhantomRefs[i], is_alive, duke@435: keep_alive, complete_gc, yield); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Walk the given discovered ref list, and remove all reference objects duke@435: // whose referents are still alive, whose referents are NULL or which ysr@887: // are not active (have a non-NULL next field). NOTE: When we are ysr@887: // thus precleaning the ref lists (which happens single-threaded today), ysr@887: // we do not disable refs discovery to honour the correct semantics of ysr@887: // java.lang.Reference. As a result, we need to be careful below ysr@887: // that ref removal steps interleave safely with ref discovery steps ysr@887: // (in this thread). coleenp@548: void coleenp@548: ReferenceProcessor::preclean_discovered_reflist(DiscoveredList& refs_list, coleenp@548: BoolObjectClosure* is_alive, coleenp@548: OopClosure* keep_alive, coleenp@548: VoidClosure* complete_gc, coleenp@548: YieldClosure* yield) { duke@435: DiscoveredListIterator iter(refs_list, keep_alive, is_alive); duke@435: while (iter.has_next()) { duke@435: iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */)); coleenp@548: oop obj = iter.obj(); coleenp@548: oop next = java_lang_ref_Reference::next(obj); duke@435: if (iter.referent() == NULL || iter.is_referent_alive() || coleenp@548: next != NULL) { duke@435: // The referent has been cleared, or is alive, or the Reference is not duke@435: // active; we need to trace and mark its cohort. duke@435: if (TraceReferenceGC) { duke@435: gclog_or_tty->print_cr("Precleaning Reference (" INTPTR_FORMAT ": %s)", hseigel@5784: (void *)iter.obj(), iter.obj()->klass()->internal_name()); duke@435: } duke@435: // Remove Reference object from list duke@435: iter.remove(); duke@435: // Keep alive its cohort. duke@435: iter.make_referent_alive(); coleenp@548: if (UseCompressedOops) { coleenp@548: narrowOop* next_addr = (narrowOop*)java_lang_ref_Reference::next_addr(obj); coleenp@548: keep_alive->do_oop(next_addr); coleenp@548: } else { coleenp@548: oop* next_addr = (oop*)java_lang_ref_Reference::next_addr(obj); coleenp@548: keep_alive->do_oop(next_addr); coleenp@548: } ysr@887: iter.move_to_next(); duke@435: } else { duke@435: iter.next(); duke@435: } duke@435: } duke@435: // Close the reachable set duke@435: complete_gc->do_void(); duke@435: duke@435: NOT_PRODUCT( ysr@2651: if (PrintGCDetails && PrintReferenceGC && (iter.processed() > 0)) { jmasa@2188: gclog_or_tty->print_cr(" Dropped %d Refs out of %d " jmasa@2188: "Refs in discovered list " INTPTR_FORMAT, jmasa@2188: iter.removed(), iter.processed(), (address)refs_list.head()); duke@435: } duke@435: ) duke@435: } duke@435: jmasa@3357: const char* ReferenceProcessor::list_name(uint i) { johnc@3175: assert(i >= 0 && i <= _max_num_q * number_of_subclasses_of_ref(), johnc@3175: "Out of bounds index"); johnc@3175: jmasa@2188: int j = i / _max_num_q; duke@435: switch (j) { duke@435: case 0: return "SoftRef"; duke@435: case 1: return "WeakRef"; duke@435: case 2: return "FinalRef"; duke@435: case 3: return "PhantomRef"; duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void ReferenceProcessor::verify_ok_to_handle_reflists() { duke@435: // empty for now duke@435: } duke@435: #endif duke@435: duke@435: #ifndef PRODUCT duke@435: void ReferenceProcessor::clear_discovered_references() { duke@435: guarantee(!_discovering_refs, "Discovering refs?"); jmasa@3357: for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { johnc@3210: clear_discovered_references(_discovered_refs[i]); duke@435: } duke@435: } stefank@3115: duke@435: #endif // PRODUCT