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