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 * 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 * 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 * 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. 1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2988N/A // We need a monotonically non-deccreasing time in ms but 2988N/A // os::javaTimeMillis() does not guarantee monotonicity. 2828N/A // Initialize the soft ref timestamp clock. 2828N/A // Also update the soft ref clock in j.l.r.SoftReference 0N/A "Unrecongnized RefDiscoveryPolicy");
2828N/A // Verify that we're not currently discovering refs 2828N/A // Verify that the discovered lists are empty 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. 2850N/A // Initialize all entries to NULL 2751N/A // If we do barriers, cache a copy of the barrier set. 0N/A "Found non-empty discovered list");
0N/A // Update (advance) the soft ref master clock field. This must be done 0N/A // after processing the soft ref list. 2988N/A // We need a monotonically non-deccreasing time in ms but 2988N/A // os::javaTimeMillis() does not guarantee monotonicity. 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. 0N/A // Else leave clock stalled at its old value until time progresses 0N/A // past clock value. 0N/A // Stop treating discovered references specially. 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 0N/A // Phantom references 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// Calculate the number of jni handles. 0N/A // Remember old value of pending references list 0N/A // Enqueue references that are not made active again, and 0N/A // clear the decks for the next collection (cycle). 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. 0N/A // Stop treating discovered references specially. 0N/A // Return true if new pending references were added 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 // Walk down the list, self-looping the next field 2751N/A // so that the References are not considered active. 2751N/A "Reference not active; should not be discovered");
2751N/A // Self-loop next, so as to make Ref not active. 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 // 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 // Walk down the list, copying the discovered field into 2751N/A // the next field and clearing the discovered field. 2751N/A "The reference should not be enqueued");
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 // 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 // obj should be made to point to itself, since 0N/A// Parallel enqueue task 0N/A // Simplest first cut: static partitioning. 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. 0N/A// Enqueue references that are not made active again 0N/A // Serial code: call the parent class's implementation 0N/A "discovered field is bad");
0N/A "Wrong oop found in java.lang.Reference object");
113N/A // First _prev_next ref actually points into DiscoveredList (gross). 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, 113N/A // Remove Reference object from list. 113N/A // Remove Reference object from list. 2815N/A// Make the Reference object active again. 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 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// (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/A // Decide which softly reachable refs should be kept alive. 452N/A // Remove Reference object from list 0N/A // Make the Reference object active again 0N/A // keep the referent around 0N/A // Close the reachable set 0N/A// Traverse the list and remove any Refs that are not active, or 0N/A// whose referents are either alive or NULL. 0N/A // The referent is reachable after all. 452N/A // Remove Reference object from list. 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 // Remove Reference object from list 0N/A // Trace the cohorts 0N/A // Now close the newly reachable set 0N/A// Traverse the list and process the referents, by either 113N/A// clearing them or keeping them (and their reachable 0N/A // NULL out referent pointer 0N/A // keep the referent around 2749N/A // Remember to update the next pointer of the last ref. 0N/A // Close the reachable set 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// 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/A // calculate total length 0N/A // move superfluous refs 1753N/A // Move all the Ref's if the from queue will not be processed. 0N/A // find an element to split the list on 2749N/A // Add the chain to the to list. 2749N/A // to list is empty. Make a loop at the end. 2749N/A // Remove the chain from the from list. 2749N/A // We found the end of the from list. 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 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 }
else {
// policy == NULL 0N/A "Policy must be specified for soft references.");
0N/A // . Traverse the list and remove any refs whose referents are alive. 0N/A // . Traverse the list and process referents as appropriate. 0N/A // loop over the lists 0N/A "\nScrubbing %s discovered list of Null referents",
0N/A // If referent has been cleared or Reference is not active, 0N/A // Remove Reference object from list 0N/A " Removed %d Refs with NULL referents out of %d discovered Refs",
0N/A // Determine the queue index to use for this object. 0N/A // During a multi-threaded discovery phase, 0N/A // each thread saves to its "own" list. 0N/A // single-threaded discovery, we save in round-robin 0N/A // fashion to each of the lists. 0N/A // Get the discovered queue to which we will add 0N/A // Unknown reference type, no special treatment 0N/A // we should not reach here if we are an instanceRefKlass 0N/A // First we must make sure this object is only enqueued once. CAS in a non null 2749N/A // The last ref must have its discovered field pointing to itself. 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 "Need to check non-G1 collector: " 2751N/A "may need a pre-write-barrier for CAS from NULL below");
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 // If retest was non NULL, another thread beat us to it: 0N/A // The reference has already been discovered... 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. 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. 2751N/A // Make sure we are discovering refs (rather than processing discovered refs). 2751N/A // We only discover active references. 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). 2751N/A // We only discover references whose referents are not (yet) 2751N/A // known to be strongly reachable. 0N/A return false;
// referent is reachable 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 0N/A // The reference has already been discovered... 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 "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 "Only possible with a concurrent marking collector");
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 // should_enqueue = true; 0N/A // Get the right type of discovered queue head. 0N/A return false;
// nothing special needs to be done 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 2749N/A // The last ref must have its discovered field pointing to itself. 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); 2751N/A "For non-G1 collector, may need a pre-write-barrier for CAS from NULL below");
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/A // Phantom references 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 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 // Remove Reference object from list 0N/A // Keep alive its cohort. 0N/A // Close the reachable set 0N/A case 0:
return "SoftRef";
0N/A case 1:
return "WeakRef";
0N/A case 2:
return "FinalRef";
0N/A case 3:
return "PhantomRef";