Thu, 26 Jun 2014 11:36:58 +0200
8047818: G1 HeapRegions can no longer be ContiguousSpaces
Summary: Change parent of G1OffsetTableContigSpace to CompactibleSpace, reimplement missing functionality
Reviewed-by: stefank, jmasa, tschatzl
duke@435 | 1 | /* |
drchase@6680 | 2 | * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "classfile/javaClasses.hpp" |
stefank@2314 | 27 | #include "classfile/systemDictionary.hpp" |
sla@5237 | 28 | #include "gc_implementation/shared/gcTimer.hpp" |
sla@5237 | 29 | #include "gc_implementation/shared/gcTraceTime.hpp" |
stefank@2314 | 30 | #include "gc_interface/collectedHeap.hpp" |
stefank@2314 | 31 | #include "gc_interface/collectedHeap.inline.hpp" |
stefank@2314 | 32 | #include "memory/referencePolicy.hpp" |
stefank@2314 | 33 | #include "memory/referenceProcessor.hpp" |
stefank@2314 | 34 | #include "oops/oop.inline.hpp" |
stefank@2314 | 35 | #include "runtime/java.hpp" |
stefank@2314 | 36 | #include "runtime/jniHandles.hpp" |
duke@435 | 37 | |
drchase@6680 | 38 | PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
drchase@6680 | 39 | |
ysr@888 | 40 | ReferencePolicy* ReferenceProcessor::_always_clear_soft_ref_policy = NULL; |
ysr@888 | 41 | ReferencePolicy* ReferenceProcessor::_default_soft_ref_policy = NULL; |
ysr@3117 | 42 | bool ReferenceProcessor::_pending_list_uses_discovered_field = false; |
johnc@3188 | 43 | jlong ReferenceProcessor::_soft_ref_timestamp_clock = 0; |
ysr@888 | 44 | |
duke@435 | 45 | void referenceProcessor_init() { |
duke@435 | 46 | ReferenceProcessor::init_statics(); |
duke@435 | 47 | } |
duke@435 | 48 | |
duke@435 | 49 | void ReferenceProcessor::init_statics() { |
johnc@3339 | 50 | // We need a monotonically non-deccreasing time in ms but |
johnc@3339 | 51 | // os::javaTimeMillis() does not guarantee monotonicity. |
johnc@3339 | 52 | jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC; |
johnc@3188 | 53 | |
johnc@3188 | 54 | // Initialize the soft ref timestamp clock. |
johnc@3188 | 55 | _soft_ref_timestamp_clock = now; |
johnc@3188 | 56 | // Also update the soft ref clock in j.l.r.SoftReference |
johnc@3188 | 57 | java_lang_ref_SoftReference::set_clock(_soft_ref_timestamp_clock); |
duke@435 | 58 | |
ysr@888 | 59 | _always_clear_soft_ref_policy = new AlwaysClearPolicy(); |
ysr@888 | 60 | _default_soft_ref_policy = new COMPILER2_PRESENT(LRUMaxHeapPolicy()) |
ysr@888 | 61 | NOT_COMPILER2(LRUCurrentHeapPolicy()); |
ysr@888 | 62 | if (_always_clear_soft_ref_policy == NULL || _default_soft_ref_policy == NULL) { |
ysr@888 | 63 | vm_exit_during_initialization("Could not allocate reference policy object"); |
ysr@888 | 64 | } |
duke@435 | 65 | guarantee(RefDiscoveryPolicy == ReferenceBasedDiscovery || |
duke@435 | 66 | RefDiscoveryPolicy == ReferentBasedDiscovery, |
duke@435 | 67 | "Unrecongnized RefDiscoveryPolicy"); |
ysr@3117 | 68 | _pending_list_uses_discovered_field = JDK_Version::current().pending_list_uses_discovered_field(); |
duke@435 | 69 | } |
duke@435 | 70 | |
johnc@3188 | 71 | void ReferenceProcessor::enable_discovery(bool verify_disabled, bool check_no_refs) { |
johnc@3188 | 72 | #ifdef ASSERT |
johnc@3188 | 73 | // Verify that we're not currently discovering refs |
johnc@3188 | 74 | assert(!verify_disabled || !_discovering_refs, "nested call?"); |
johnc@3188 | 75 | |
johnc@3188 | 76 | if (check_no_refs) { |
johnc@3188 | 77 | // Verify that the discovered lists are empty |
johnc@3188 | 78 | verify_no_references_recorded(); |
johnc@3188 | 79 | } |
johnc@3188 | 80 | #endif // ASSERT |
johnc@3188 | 81 | |
johnc@3188 | 82 | // Someone could have modified the value of the static |
johnc@3188 | 83 | // field in the j.l.r.SoftReference class that holds the |
johnc@3188 | 84 | // soft reference timestamp clock using reflection or |
johnc@3188 | 85 | // Unsafe between GCs. Unconditionally update the static |
johnc@3188 | 86 | // field in ReferenceProcessor here so that we use the new |
johnc@3188 | 87 | // value during reference discovery. |
johnc@3188 | 88 | |
johnc@3188 | 89 | _soft_ref_timestamp_clock = java_lang_ref_SoftReference::clock(); |
johnc@3188 | 90 | _discovering_refs = true; |
johnc@3188 | 91 | } |
johnc@3188 | 92 | |
duke@435 | 93 | ReferenceProcessor::ReferenceProcessor(MemRegion span, |
ysr@2651 | 94 | bool mt_processing, |
jmasa@3357 | 95 | uint mt_processing_degree, |
ysr@2651 | 96 | bool mt_discovery, |
jmasa@3357 | 97 | uint mt_discovery_degree, |
coleenp@548 | 98 | bool atomic_discovery, |
brutisso@6719 | 99 | BoolObjectClosure* is_alive_non_header) : |
duke@435 | 100 | _discovering_refs(false), |
duke@435 | 101 | _enqueuing_is_done(false), |
ysr@2651 | 102 | _is_alive_non_header(is_alive_non_header), |
duke@435 | 103 | _processing_is_mt(mt_processing), |
duke@435 | 104 | _next_id(0) |
duke@435 | 105 | { |
duke@435 | 106 | _span = span; |
duke@435 | 107 | _discovery_is_atomic = atomic_discovery; |
duke@435 | 108 | _discovery_is_mt = mt_discovery; |
jmasa@3357 | 109 | _num_q = MAX2(1U, mt_processing_degree); |
ysr@2651 | 110 | _max_num_q = MAX2(_num_q, mt_discovery_degree); |
johnc@3210 | 111 | _discovered_refs = NEW_C_HEAP_ARRAY(DiscoveredList, |
zgu@3900 | 112 | _max_num_q * number_of_subclasses_of_ref(), mtGC); |
zgu@3900 | 113 | |
johnc@3210 | 114 | if (_discovered_refs == NULL) { |
duke@435 | 115 | vm_exit_during_initialization("Could not allocated RefProc Array"); |
duke@435 | 116 | } |
johnc@3210 | 117 | _discoveredSoftRefs = &_discovered_refs[0]; |
jmasa@2188 | 118 | _discoveredWeakRefs = &_discoveredSoftRefs[_max_num_q]; |
jmasa@2188 | 119 | _discoveredFinalRefs = &_discoveredWeakRefs[_max_num_q]; |
jmasa@2188 | 120 | _discoveredPhantomRefs = &_discoveredFinalRefs[_max_num_q]; |
johnc@3210 | 121 | |
johnc@3210 | 122 | // Initialize all entries to NULL |
jmasa@3357 | 123 | for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { |
johnc@3210 | 124 | _discovered_refs[i].set_head(NULL); |
johnc@3210 | 125 | _discovered_refs[i].set_length(0); |
duke@435 | 126 | } |
johnc@3210 | 127 | |
ysr@2651 | 128 | setup_policy(false /* default soft ref policy */); |
duke@435 | 129 | } |
duke@435 | 130 | |
duke@435 | 131 | #ifndef PRODUCT |
duke@435 | 132 | void ReferenceProcessor::verify_no_references_recorded() { |
duke@435 | 133 | guarantee(!_discovering_refs, "Discovering refs?"); |
jmasa@3357 | 134 | for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { |
johnc@3210 | 135 | guarantee(_discovered_refs[i].is_empty(), |
duke@435 | 136 | "Found non-empty discovered list"); |
duke@435 | 137 | } |
duke@435 | 138 | } |
duke@435 | 139 | #endif |
duke@435 | 140 | |
duke@435 | 141 | void ReferenceProcessor::weak_oops_do(OopClosure* f) { |
jmasa@3357 | 142 | for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { |
coleenp@548 | 143 | if (UseCompressedOops) { |
johnc@3210 | 144 | f->do_oop((narrowOop*)_discovered_refs[i].adr_head()); |
coleenp@548 | 145 | } else { |
johnc@3210 | 146 | f->do_oop((oop*)_discovered_refs[i].adr_head()); |
coleenp@548 | 147 | } |
duke@435 | 148 | } |
duke@435 | 149 | } |
duke@435 | 150 | |
coleenp@548 | 151 | void ReferenceProcessor::update_soft_ref_master_clock() { |
duke@435 | 152 | // Update (advance) the soft ref master clock field. This must be done |
duke@435 | 153 | // after processing the soft ref list. |
johnc@3339 | 154 | |
johnc@3339 | 155 | // We need a monotonically non-deccreasing time in ms but |
johnc@3339 | 156 | // os::javaTimeMillis() does not guarantee monotonicity. |
johnc@3339 | 157 | jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC; |
johnc@3188 | 158 | jlong soft_ref_clock = java_lang_ref_SoftReference::clock(); |
johnc@3188 | 159 | assert(soft_ref_clock == _soft_ref_timestamp_clock, "soft ref clocks out of sync"); |
johnc@3188 | 160 | |
duke@435 | 161 | NOT_PRODUCT( |
johnc@3188 | 162 | if (now < _soft_ref_timestamp_clock) { |
johnc@3188 | 163 | warning("time warp: "INT64_FORMAT" to "INT64_FORMAT, |
johnc@3188 | 164 | _soft_ref_timestamp_clock, now); |
duke@435 | 165 | } |
duke@435 | 166 | ) |
johnc@3339 | 167 | // The values of now and _soft_ref_timestamp_clock are set using |
johnc@3339 | 168 | // javaTimeNanos(), which is guaranteed to be monotonically |
johnc@3339 | 169 | // non-decreasing provided the underlying platform provides such |
johnc@3339 | 170 | // a time source (and it is bug free). |
johnc@3339 | 171 | // In product mode, however, protect ourselves from non-monotonicty. |
johnc@3188 | 172 | if (now > _soft_ref_timestamp_clock) { |
johnc@3188 | 173 | _soft_ref_timestamp_clock = now; |
duke@435 | 174 | java_lang_ref_SoftReference::set_clock(now); |
duke@435 | 175 | } |
duke@435 | 176 | // Else leave clock stalled at its old value until time progresses |
duke@435 | 177 | // past clock value. |
duke@435 | 178 | } |
duke@435 | 179 | |
sla@5237 | 180 | size_t ReferenceProcessor::total_count(DiscoveredList lists[]) { |
sla@5237 | 181 | size_t total = 0; |
sla@5237 | 182 | for (uint i = 0; i < _max_num_q; ++i) { |
sla@5237 | 183 | total += lists[i].length(); |
sla@5237 | 184 | } |
sla@5237 | 185 | return total; |
sla@5237 | 186 | } |
sla@5237 | 187 | |
sla@5237 | 188 | ReferenceProcessorStats ReferenceProcessor::process_discovered_references( |
duke@435 | 189 | BoolObjectClosure* is_alive, |
duke@435 | 190 | OopClosure* keep_alive, |
duke@435 | 191 | VoidClosure* complete_gc, |
sla@5237 | 192 | AbstractRefProcTaskExecutor* task_executor, |
brutisso@6904 | 193 | GCTimer* gc_timer, |
brutisso@6904 | 194 | GCId gc_id) { |
duke@435 | 195 | NOT_PRODUCT(verify_ok_to_handle_reflists()); |
duke@435 | 196 | |
duke@435 | 197 | assert(!enqueuing_is_done(), "If here enqueuing should not be complete"); |
duke@435 | 198 | // Stop treating discovered references specially. |
duke@435 | 199 | disable_discovery(); |
duke@435 | 200 | |
johnc@3188 | 201 | // If discovery was concurrent, someone could have modified |
johnc@3188 | 202 | // the value of the static field in the j.l.r.SoftReference |
johnc@3188 | 203 | // class that holds the soft reference timestamp clock using |
johnc@3188 | 204 | // reflection or Unsafe between when discovery was enabled and |
johnc@3188 | 205 | // now. Unconditionally update the static field in ReferenceProcessor |
johnc@3188 | 206 | // here so that we use the new value during processing of the |
johnc@3188 | 207 | // discovered soft refs. |
johnc@3188 | 208 | |
johnc@3188 | 209 | _soft_ref_timestamp_clock = java_lang_ref_SoftReference::clock(); |
johnc@3188 | 210 | |
duke@435 | 211 | bool trace_time = PrintGCDetails && PrintReferenceGC; |
sla@5237 | 212 | |
duke@435 | 213 | // Soft references |
sla@5237 | 214 | size_t soft_count = 0; |
duke@435 | 215 | { |
brutisso@6904 | 216 | GCTraceTime tt("SoftReference", trace_time, false, gc_timer, gc_id); |
sla@5237 | 217 | soft_count = |
sla@5237 | 218 | process_discovered_reflist(_discoveredSoftRefs, _current_soft_ref_policy, true, |
sla@5237 | 219 | is_alive, keep_alive, complete_gc, task_executor); |
duke@435 | 220 | } |
duke@435 | 221 | |
duke@435 | 222 | update_soft_ref_master_clock(); |
duke@435 | 223 | |
duke@435 | 224 | // Weak references |
sla@5237 | 225 | size_t weak_count = 0; |
duke@435 | 226 | { |
brutisso@6904 | 227 | GCTraceTime tt("WeakReference", trace_time, false, gc_timer, gc_id); |
sla@5237 | 228 | weak_count = |
sla@5237 | 229 | process_discovered_reflist(_discoveredWeakRefs, NULL, true, |
sla@5237 | 230 | is_alive, keep_alive, complete_gc, task_executor); |
duke@435 | 231 | } |
duke@435 | 232 | |
duke@435 | 233 | // Final references |
sla@5237 | 234 | size_t final_count = 0; |
duke@435 | 235 | { |
brutisso@6904 | 236 | GCTraceTime tt("FinalReference", trace_time, false, gc_timer, gc_id); |
sla@5237 | 237 | final_count = |
sla@5237 | 238 | process_discovered_reflist(_discoveredFinalRefs, NULL, false, |
sla@5237 | 239 | is_alive, keep_alive, complete_gc, task_executor); |
duke@435 | 240 | } |
duke@435 | 241 | |
duke@435 | 242 | // Phantom references |
sla@5237 | 243 | size_t phantom_count = 0; |
duke@435 | 244 | { |
brutisso@6904 | 245 | GCTraceTime tt("PhantomReference", trace_time, false, gc_timer, gc_id); |
sla@5237 | 246 | phantom_count = |
sla@5237 | 247 | process_discovered_reflist(_discoveredPhantomRefs, NULL, false, |
sla@5237 | 248 | is_alive, keep_alive, complete_gc, task_executor); |
duke@435 | 249 | } |
duke@435 | 250 | |
duke@435 | 251 | // Weak global JNI references. It would make more sense (semantically) to |
duke@435 | 252 | // traverse these simultaneously with the regular weak references above, but |
duke@435 | 253 | // that is not how the JDK1.2 specification is. See #4126360. Native code can |
duke@435 | 254 | // thus use JNI weak references to circumvent the phantom references and |
duke@435 | 255 | // resurrect a "post-mortem" object. |
duke@435 | 256 | { |
brutisso@6904 | 257 | GCTraceTime tt("JNI Weak Reference", trace_time, false, gc_timer, gc_id); |
duke@435 | 258 | if (task_executor != NULL) { |
duke@435 | 259 | task_executor->set_single_threaded_mode(); |
duke@435 | 260 | } |
duke@435 | 261 | process_phaseJNI(is_alive, keep_alive, complete_gc); |
duke@435 | 262 | } |
sla@5237 | 263 | |
sla@5237 | 264 | return ReferenceProcessorStats(soft_count, weak_count, final_count, phantom_count); |
duke@435 | 265 | } |
duke@435 | 266 | |
duke@435 | 267 | #ifndef PRODUCT |
duke@435 | 268 | // Calculate the number of jni handles. |
coleenp@548 | 269 | uint ReferenceProcessor::count_jni_refs() { |
duke@435 | 270 | class AlwaysAliveClosure: public BoolObjectClosure { |
duke@435 | 271 | public: |
coleenp@548 | 272 | virtual bool do_object_b(oop obj) { return true; } |
duke@435 | 273 | }; |
duke@435 | 274 | |
duke@435 | 275 | class CountHandleClosure: public OopClosure { |
duke@435 | 276 | private: |
duke@435 | 277 | int _count; |
duke@435 | 278 | public: |
duke@435 | 279 | CountHandleClosure(): _count(0) {} |
coleenp@548 | 280 | void do_oop(oop* unused) { _count++; } |
coleenp@548 | 281 | void do_oop(narrowOop* unused) { ShouldNotReachHere(); } |
duke@435 | 282 | int count() { return _count; } |
duke@435 | 283 | }; |
duke@435 | 284 | CountHandleClosure global_handle_count; |
duke@435 | 285 | AlwaysAliveClosure always_alive; |
duke@435 | 286 | JNIHandles::weak_oops_do(&always_alive, &global_handle_count); |
duke@435 | 287 | return global_handle_count.count(); |
duke@435 | 288 | } |
duke@435 | 289 | #endif |
duke@435 | 290 | |
duke@435 | 291 | void ReferenceProcessor::process_phaseJNI(BoolObjectClosure* is_alive, |
duke@435 | 292 | OopClosure* keep_alive, |
duke@435 | 293 | VoidClosure* complete_gc) { |
duke@435 | 294 | #ifndef PRODUCT |
duke@435 | 295 | if (PrintGCDetails && PrintReferenceGC) { |
duke@435 | 296 | unsigned int count = count_jni_refs(); |
duke@435 | 297 | gclog_or_tty->print(", %u refs", count); |
duke@435 | 298 | } |
duke@435 | 299 | #endif |
duke@435 | 300 | JNIHandles::weak_oops_do(is_alive, keep_alive); |
duke@435 | 301 | complete_gc->do_void(); |
duke@435 | 302 | } |
duke@435 | 303 | |
coleenp@548 | 304 | |
coleenp@548 | 305 | template <class T> |
phh@1558 | 306 | bool enqueue_discovered_ref_helper(ReferenceProcessor* ref, |
phh@1558 | 307 | AbstractRefProcTaskExecutor* task_executor) { |
coleenp@548 | 308 | |
duke@435 | 309 | // Remember old value of pending references list |
coleenp@548 | 310 | T* pending_list_addr = (T*)java_lang_ref_Reference::pending_list_addr(); |
coleenp@548 | 311 | T old_pending_list_value = *pending_list_addr; |
duke@435 | 312 | |
duke@435 | 313 | // Enqueue references that are not made active again, and |
duke@435 | 314 | // clear the decks for the next collection (cycle). |
coleenp@548 | 315 | ref->enqueue_discovered_reflists((HeapWord*)pending_list_addr, task_executor); |
pliden@6395 | 316 | // Do the post-barrier on pending_list_addr missed in |
pliden@6395 | 317 | // enqueue_discovered_reflist. |
pliden@6395 | 318 | oopDesc::bs()->write_ref_field(pending_list_addr, oopDesc::load_decode_heap_oop(pending_list_addr)); |
duke@435 | 319 | |
duke@435 | 320 | // Stop treating discovered references specially. |
coleenp@548 | 321 | ref->disable_discovery(); |
duke@435 | 322 | |
duke@435 | 323 | // Return true if new pending references were added |
duke@435 | 324 | return old_pending_list_value != *pending_list_addr; |
duke@435 | 325 | } |
duke@435 | 326 | |
coleenp@548 | 327 | bool ReferenceProcessor::enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor) { |
coleenp@548 | 328 | NOT_PRODUCT(verify_ok_to_handle_reflists()); |
coleenp@548 | 329 | if (UseCompressedOops) { |
coleenp@548 | 330 | return enqueue_discovered_ref_helper<narrowOop>(this, task_executor); |
coleenp@548 | 331 | } else { |
coleenp@548 | 332 | return enqueue_discovered_ref_helper<oop>(this, task_executor); |
coleenp@548 | 333 | } |
coleenp@548 | 334 | } |
coleenp@548 | 335 | |
duke@435 | 336 | void ReferenceProcessor::enqueue_discovered_reflist(DiscoveredList& refs_list, |
coleenp@548 | 337 | HeapWord* pending_list_addr) { |
duke@435 | 338 | // Given a list of refs linked through the "discovered" field |
ysr@3117 | 339 | // (java.lang.ref.Reference.discovered), self-loop their "next" field |
ysr@3117 | 340 | // thus distinguishing them from active References, then |
ysr@3117 | 341 | // prepend them to the pending list. |
brutisso@6719 | 342 | // |
brutisso@6719 | 343 | // The Java threads will see the Reference objects linked together through |
brutisso@6719 | 344 | // the discovered field. Instead of trying to do the write barrier updates |
brutisso@6719 | 345 | // in all places in the reference processor where we manipulate the discovered |
brutisso@6719 | 346 | // field we make sure to do the barrier here where we anyway iterate through |
brutisso@6719 | 347 | // all linked Reference objects. Note that it is important to not dirty any |
brutisso@6719 | 348 | // cards during reference processing since this will cause card table |
brutisso@6719 | 349 | // verification to fail for G1. |
brutisso@6719 | 350 | // |
ysr@3117 | 351 | // BKWRD COMPATIBILITY NOTE: For older JDKs (prior to the fix for 4956777), |
ysr@3117 | 352 | // the "next" field is used to chain the pending list, not the discovered |
ysr@3117 | 353 | // field. |
duke@435 | 354 | if (TraceReferenceGC && PrintGCDetails) { |
duke@435 | 355 | gclog_or_tty->print_cr("ReferenceProcessor::enqueue_discovered_reflist list " |
duke@435 | 356 | INTPTR_FORMAT, (address)refs_list.head()); |
duke@435 | 357 | } |
stefank@3115 | 358 | |
stefank@3115 | 359 | oop obj = NULL; |
ysr@3117 | 360 | oop next_d = refs_list.head(); |
brutisso@6719 | 361 | if (pending_list_uses_discovered_field()) { // New behavior |
ysr@3117 | 362 | // Walk down the list, self-looping the next field |
ysr@3117 | 363 | // so that the References are not considered active. |
ysr@3117 | 364 | while (obj != next_d) { |
ysr@3117 | 365 | obj = next_d; |
ysr@3117 | 366 | assert(obj->is_instanceRef(), "should be reference object"); |
ysr@3117 | 367 | next_d = java_lang_ref_Reference::discovered(obj); |
ysr@3117 | 368 | if (TraceReferenceGC && PrintGCDetails) { |
ysr@3117 | 369 | gclog_or_tty->print_cr(" obj " INTPTR_FORMAT "/next_d " INTPTR_FORMAT, |
hseigel@5784 | 370 | (void *)obj, (void *)next_d); |
ysr@3117 | 371 | } |
ysr@3117 | 372 | assert(java_lang_ref_Reference::next(obj) == NULL, |
ysr@3117 | 373 | "Reference not active; should not be discovered"); |
ysr@3117 | 374 | // Self-loop next, so as to make Ref not active. |
pliden@6395 | 375 | java_lang_ref_Reference::set_next_raw(obj, obj); |
brutisso@6719 | 376 | if (next_d != obj) { |
brutisso@6719 | 377 | oopDesc::bs()->write_ref_field(java_lang_ref_Reference::discovered_addr(obj), next_d); |
brutisso@6719 | 378 | } else { |
brutisso@6719 | 379 | // This is the last object. |
brutisso@6719 | 380 | // Swap refs_list into pending_list_addr and |
ysr@3117 | 381 | // set obj's discovered to what we read from pending_list_addr. |
ysr@3117 | 382 | oop old = oopDesc::atomic_exchange_oop(refs_list.head(), pending_list_addr); |
brutisso@6719 | 383 | // Need post-barrier on pending_list_addr. See enqueue_discovered_ref_helper() above. |
pliden@6395 | 384 | java_lang_ref_Reference::set_discovered_raw(obj, old); // old may be NULL |
pliden@6395 | 385 | oopDesc::bs()->write_ref_field(java_lang_ref_Reference::discovered_addr(obj), old); |
ysr@3117 | 386 | } |
duke@435 | 387 | } |
ysr@3117 | 388 | } else { // Old behaviour |
ysr@3117 | 389 | // Walk down the list, copying the discovered field into |
ysr@3117 | 390 | // the next field and clearing the discovered field. |
ysr@3117 | 391 | while (obj != next_d) { |
ysr@3117 | 392 | obj = next_d; |
ysr@3117 | 393 | assert(obj->is_instanceRef(), "should be reference object"); |
ysr@3117 | 394 | next_d = java_lang_ref_Reference::discovered(obj); |
ysr@3117 | 395 | if (TraceReferenceGC && PrintGCDetails) { |
ysr@3117 | 396 | gclog_or_tty->print_cr(" obj " INTPTR_FORMAT "/next_d " INTPTR_FORMAT, |
hseigel@5784 | 397 | (void *)obj, (void *)next_d); |
ysr@3117 | 398 | } |
ysr@3117 | 399 | assert(java_lang_ref_Reference::next(obj) == NULL, |
ysr@3117 | 400 | "The reference should not be enqueued"); |
ysr@3117 | 401 | if (next_d == obj) { // obj is last |
ysr@3117 | 402 | // Swap refs_list into pendling_list_addr and |
ysr@3117 | 403 | // set obj's next to what we read from pending_list_addr. |
ysr@3117 | 404 | oop old = oopDesc::atomic_exchange_oop(refs_list.head(), pending_list_addr); |
ysr@3117 | 405 | // Need oop_check on pending_list_addr above; |
ysr@3117 | 406 | // see special oop-check code at the end of |
ysr@3117 | 407 | // enqueue_discovered_reflists() further below. |
ysr@3117 | 408 | if (old == NULL) { |
ysr@3117 | 409 | // obj should be made to point to itself, since |
ysr@3117 | 410 | // pending list was empty. |
ysr@3117 | 411 | java_lang_ref_Reference::set_next(obj, obj); |
ysr@3117 | 412 | } else { |
ysr@3117 | 413 | java_lang_ref_Reference::set_next(obj, old); |
ysr@3117 | 414 | } |
duke@435 | 415 | } else { |
ysr@3117 | 416 | java_lang_ref_Reference::set_next(obj, next_d); |
duke@435 | 417 | } |
ysr@3117 | 418 | java_lang_ref_Reference::set_discovered(obj, (oop) NULL); |
duke@435 | 419 | } |
duke@435 | 420 | } |
duke@435 | 421 | } |
duke@435 | 422 | |
duke@435 | 423 | // Parallel enqueue task |
duke@435 | 424 | class RefProcEnqueueTask: public AbstractRefProcTaskExecutor::EnqueueTask { |
duke@435 | 425 | public: |
duke@435 | 426 | RefProcEnqueueTask(ReferenceProcessor& ref_processor, |
duke@435 | 427 | DiscoveredList discovered_refs[], |
coleenp@548 | 428 | HeapWord* pending_list_addr, |
duke@435 | 429 | int n_queues) |
duke@435 | 430 | : EnqueueTask(ref_processor, discovered_refs, |
stefank@3115 | 431 | pending_list_addr, n_queues) |
duke@435 | 432 | { } |
duke@435 | 433 | |
coleenp@548 | 434 | virtual void work(unsigned int work_id) { |
ysr@2651 | 435 | assert(work_id < (unsigned int)_ref_processor.max_num_q(), "Index out-of-bounds"); |
duke@435 | 436 | // Simplest first cut: static partitioning. |
duke@435 | 437 | int index = work_id; |
jmasa@2188 | 438 | // The increment on "index" must correspond to the maximum number of queues |
jmasa@2188 | 439 | // (n_queues) with which that ReferenceProcessor was created. That |
jmasa@2188 | 440 | // is because of the "clever" way the discovered references lists were |
ysr@2651 | 441 | // allocated and are indexed into. |
ysr@2651 | 442 | assert(_n_queues == (int) _ref_processor.max_num_q(), "Different number not expected"); |
jmasa@2188 | 443 | for (int j = 0; |
johnc@3175 | 444 | j < ReferenceProcessor::number_of_subclasses_of_ref(); |
jmasa@2188 | 445 | j++, index += _n_queues) { |
duke@435 | 446 | _ref_processor.enqueue_discovered_reflist( |
duke@435 | 447 | _refs_lists[index], _pending_list_addr); |
stefank@3115 | 448 | _refs_lists[index].set_head(NULL); |
duke@435 | 449 | _refs_lists[index].set_length(0); |
duke@435 | 450 | } |
duke@435 | 451 | } |
duke@435 | 452 | }; |
duke@435 | 453 | |
duke@435 | 454 | // Enqueue references that are not made active again |
coleenp@548 | 455 | void ReferenceProcessor::enqueue_discovered_reflists(HeapWord* pending_list_addr, |
duke@435 | 456 | AbstractRefProcTaskExecutor* task_executor) { |
duke@435 | 457 | if (_processing_is_mt && task_executor != NULL) { |
duke@435 | 458 | // Parallel code |
johnc@3210 | 459 | RefProcEnqueueTask tsk(*this, _discovered_refs, |
stefank@3115 | 460 | pending_list_addr, _max_num_q); |
duke@435 | 461 | task_executor->execute(tsk); |
duke@435 | 462 | } else { |
duke@435 | 463 | // Serial code: call the parent class's implementation |
jmasa@3357 | 464 | for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { |
johnc@3210 | 465 | enqueue_discovered_reflist(_discovered_refs[i], pending_list_addr); |
johnc@3210 | 466 | _discovered_refs[i].set_head(NULL); |
johnc@3210 | 467 | _discovered_refs[i].set_length(0); |
duke@435 | 468 | } |
duke@435 | 469 | } |
duke@435 | 470 | } |
duke@435 | 471 | |
johnc@3175 | 472 | void DiscoveredListIterator::load_ptrs(DEBUG_ONLY(bool allow_null_referent)) { |
duke@435 | 473 | _discovered_addr = java_lang_ref_Reference::discovered_addr(_ref); |
coleenp@548 | 474 | oop discovered = java_lang_ref_Reference::discovered(_ref); |
coleenp@548 | 475 | assert(_discovered_addr && discovered->is_oop_or_null(), |
duke@435 | 476 | "discovered field is bad"); |
coleenp@548 | 477 | _next = discovered; |
duke@435 | 478 | _referent_addr = java_lang_ref_Reference::referent_addr(_ref); |
coleenp@548 | 479 | _referent = java_lang_ref_Reference::referent(_ref); |
duke@435 | 480 | assert(Universe::heap()->is_in_reserved_or_null(_referent), |
duke@435 | 481 | "Wrong oop found in java.lang.Reference object"); |
duke@435 | 482 | assert(allow_null_referent ? |
duke@435 | 483 | _referent->is_oop_or_null() |
duke@435 | 484 | : _referent->is_oop(), |
duke@435 | 485 | "bad referent"); |
duke@435 | 486 | } |
duke@435 | 487 | |
johnc@3175 | 488 | void DiscoveredListIterator::remove() { |
duke@435 | 489 | assert(_ref->is_oop(), "Dropping a bad reference"); |
coleenp@548 | 490 | oop_store_raw(_discovered_addr, NULL); |
stefank@3115 | 491 | |
coleenp@548 | 492 | // First _prev_next ref actually points into DiscoveredList (gross). |
stefank@3115 | 493 | oop new_next; |
stefank@3115 | 494 | if (_next == _ref) { |
stefank@3115 | 495 | // At the end of the list, we should make _prev point to itself. |
stefank@3115 | 496 | // If _ref is the first ref, then _prev_next will be in the DiscoveredList, |
stefank@3115 | 497 | // and _prev will be NULL. |
stefank@3115 | 498 | new_next = _prev; |
stefank@3115 | 499 | } else { |
stefank@3115 | 500 | new_next = _next; |
stefank@3115 | 501 | } |
pliden@6397 | 502 | // Remove Reference object from discovered list. Note that G1 does not need a |
pliden@6397 | 503 | // pre-barrier here because we know the Reference has already been found/marked, |
pliden@6397 | 504 | // that's how it ended up in the discovered list in the first place. |
pliden@6397 | 505 | oop_store_raw(_prev_next, new_next); |
duke@435 | 506 | NOT_PRODUCT(_removed++); |
ysr@887 | 507 | _refs_list.dec_length(1); |
duke@435 | 508 | } |
duke@435 | 509 | |
johnc@3175 | 510 | // Make the Reference object active again. |
johnc@3175 | 511 | void DiscoveredListIterator::make_active() { |
brutisso@6719 | 512 | // The pre barrier for G1 is probably just needed for the old |
brutisso@6719 | 513 | // reference processing behavior. Should we guard this with |
brutisso@6719 | 514 | // ReferenceProcessor::pending_list_uses_discovered_field() ? |
johnc@3175 | 515 | if (UseG1GC) { |
johnc@3175 | 516 | HeapWord* next_addr = java_lang_ref_Reference::next_addr(_ref); |
johnc@3175 | 517 | if (UseCompressedOops) { |
pliden@6395 | 518 | oopDesc::bs()->write_ref_field_pre((narrowOop*)next_addr, NULL); |
johnc@3175 | 519 | } else { |
pliden@6395 | 520 | oopDesc::bs()->write_ref_field_pre((oop*)next_addr, NULL); |
johnc@3175 | 521 | } |
stefank@3115 | 522 | } |
brutisso@6719 | 523 | java_lang_ref_Reference::set_next_raw(_ref, NULL); |
johnc@3175 | 524 | } |
johnc@3175 | 525 | |
johnc@3175 | 526 | void DiscoveredListIterator::clear_referent() { |
johnc@3175 | 527 | oop_store_raw(_referent_addr, NULL); |
duke@435 | 528 | } |
duke@435 | 529 | |
duke@435 | 530 | // NOTE: process_phase*() are largely similar, and at a high level |
duke@435 | 531 | // merely iterate over the extant list applying a predicate to |
duke@435 | 532 | // each of its elements and possibly removing that element from the |
duke@435 | 533 | // list and applying some further closures to that element. |
duke@435 | 534 | // We should consider the possibility of replacing these |
duke@435 | 535 | // process_phase*() methods by abstracting them into |
duke@435 | 536 | // a single general iterator invocation that receives appropriate |
duke@435 | 537 | // closures that accomplish this work. |
duke@435 | 538 | |
duke@435 | 539 | // (SoftReferences only) Traverse the list and remove any SoftReferences whose |
duke@435 | 540 | // referents are not alive, but that should be kept alive for policy reasons. |
duke@435 | 541 | // Keep alive the transitive closure of all such referents. |
duke@435 | 542 | void |
coleenp@548 | 543 | ReferenceProcessor::process_phase1(DiscoveredList& refs_list, |
duke@435 | 544 | ReferencePolicy* policy, |
duke@435 | 545 | BoolObjectClosure* is_alive, |
duke@435 | 546 | OopClosure* keep_alive, |
duke@435 | 547 | VoidClosure* complete_gc) { |
duke@435 | 548 | assert(policy != NULL, "Must have a non-NULL policy"); |
brutisso@6719 | 549 | DiscoveredListIterator iter(refs_list, keep_alive, is_alive); |
duke@435 | 550 | // Decide which softly reachable refs should be kept alive. |
duke@435 | 551 | while (iter.has_next()) { |
duke@435 | 552 | iter.load_ptrs(DEBUG_ONLY(!discovery_is_atomic() /* allow_null_referent */)); |
duke@435 | 553 | bool referent_is_dead = (iter.referent() != NULL) && !iter.is_referent_alive(); |
johnc@3188 | 554 | if (referent_is_dead && |
johnc@3188 | 555 | !policy->should_clear_reference(iter.obj(), _soft_ref_timestamp_clock)) { |
duke@435 | 556 | if (TraceReferenceGC) { |
duke@435 | 557 | gclog_or_tty->print_cr("Dropping reference (" INTPTR_FORMAT ": %s" ") by policy", |
hseigel@5784 | 558 | (void *)iter.obj(), iter.obj()->klass()->internal_name()); |
duke@435 | 559 | } |
ysr@887 | 560 | // Remove Reference object from list |
ysr@887 | 561 | iter.remove(); |
duke@435 | 562 | // Make the Reference object active again |
duke@435 | 563 | iter.make_active(); |
duke@435 | 564 | // keep the referent around |
duke@435 | 565 | iter.make_referent_alive(); |
ysr@887 | 566 | iter.move_to_next(); |
duke@435 | 567 | } else { |
duke@435 | 568 | iter.next(); |
duke@435 | 569 | } |
duke@435 | 570 | } |
duke@435 | 571 | // Close the reachable set |
duke@435 | 572 | complete_gc->do_void(); |
duke@435 | 573 | NOT_PRODUCT( |
duke@435 | 574 | if (PrintGCDetails && TraceReferenceGC) { |
jmasa@2188 | 575 | gclog_or_tty->print_cr(" Dropped %d dead Refs out of %d " |
ysr@3117 | 576 | "discovered Refs by policy, from list " INTPTR_FORMAT, |
jmasa@2188 | 577 | iter.removed(), iter.processed(), (address)refs_list.head()); |
duke@435 | 578 | } |
duke@435 | 579 | ) |
duke@435 | 580 | } |
duke@435 | 581 | |
duke@435 | 582 | // Traverse the list and remove any Refs that are not active, or |
duke@435 | 583 | // whose referents are either alive or NULL. |
duke@435 | 584 | void |
coleenp@548 | 585 | ReferenceProcessor::pp2_work(DiscoveredList& refs_list, |
duke@435 | 586 | BoolObjectClosure* is_alive, |
coleenp@548 | 587 | OopClosure* keep_alive) { |
duke@435 | 588 | assert(discovery_is_atomic(), "Error"); |
brutisso@6719 | 589 | DiscoveredListIterator iter(refs_list, keep_alive, is_alive); |
duke@435 | 590 | while (iter.has_next()) { |
duke@435 | 591 | iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */)); |
coleenp@548 | 592 | DEBUG_ONLY(oop next = java_lang_ref_Reference::next(iter.obj());) |
coleenp@548 | 593 | assert(next == NULL, "Should not discover inactive Reference"); |
duke@435 | 594 | if (iter.is_referent_alive()) { |
duke@435 | 595 | if (TraceReferenceGC) { |
duke@435 | 596 | gclog_or_tty->print_cr("Dropping strongly reachable reference (" INTPTR_FORMAT ": %s)", |
hseigel@5784 | 597 | (void *)iter.obj(), iter.obj()->klass()->internal_name()); |
duke@435 | 598 | } |
duke@435 | 599 | // The referent is reachable after all. |
ysr@887 | 600 | // Remove Reference object from list. |
ysr@887 | 601 | iter.remove(); |
duke@435 | 602 | // Update the referent pointer as necessary: Note that this |
duke@435 | 603 | // should not entail any recursive marking because the |
duke@435 | 604 | // referent must already have been traversed. |
duke@435 | 605 | iter.make_referent_alive(); |
ysr@887 | 606 | iter.move_to_next(); |
duke@435 | 607 | } else { |
duke@435 | 608 | iter.next(); |
duke@435 | 609 | } |
duke@435 | 610 | } |
duke@435 | 611 | NOT_PRODUCT( |
ysr@2651 | 612 | if (PrintGCDetails && TraceReferenceGC && (iter.processed() > 0)) { |
jmasa@2188 | 613 | gclog_or_tty->print_cr(" Dropped %d active Refs out of %d " |
jmasa@2188 | 614 | "Refs in discovered list " INTPTR_FORMAT, |
jmasa@2188 | 615 | iter.removed(), iter.processed(), (address)refs_list.head()); |
duke@435 | 616 | } |
duke@435 | 617 | ) |
duke@435 | 618 | } |
duke@435 | 619 | |
duke@435 | 620 | void |
coleenp@548 | 621 | ReferenceProcessor::pp2_work_concurrent_discovery(DiscoveredList& refs_list, |
coleenp@548 | 622 | BoolObjectClosure* is_alive, |
coleenp@548 | 623 | OopClosure* keep_alive, |
coleenp@548 | 624 | VoidClosure* complete_gc) { |
duke@435 | 625 | assert(!discovery_is_atomic(), "Error"); |
brutisso@6719 | 626 | DiscoveredListIterator iter(refs_list, keep_alive, is_alive); |
duke@435 | 627 | while (iter.has_next()) { |
duke@435 | 628 | iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */)); |
coleenp@548 | 629 | HeapWord* next_addr = java_lang_ref_Reference::next_addr(iter.obj()); |
coleenp@548 | 630 | oop next = java_lang_ref_Reference::next(iter.obj()); |
duke@435 | 631 | if ((iter.referent() == NULL || iter.is_referent_alive() || |
coleenp@548 | 632 | next != NULL)) { |
coleenp@548 | 633 | assert(next->is_oop_or_null(), "bad next field"); |
duke@435 | 634 | // Remove Reference object from list |
duke@435 | 635 | iter.remove(); |
duke@435 | 636 | // Trace the cohorts |
duke@435 | 637 | iter.make_referent_alive(); |
coleenp@548 | 638 | if (UseCompressedOops) { |
coleenp@548 | 639 | keep_alive->do_oop((narrowOop*)next_addr); |
coleenp@548 | 640 | } else { |
coleenp@548 | 641 | keep_alive->do_oop((oop*)next_addr); |
coleenp@548 | 642 | } |
ysr@887 | 643 | iter.move_to_next(); |
duke@435 | 644 | } else { |
duke@435 | 645 | iter.next(); |
duke@435 | 646 | } |
duke@435 | 647 | } |
duke@435 | 648 | // Now close the newly reachable set |
duke@435 | 649 | complete_gc->do_void(); |
duke@435 | 650 | NOT_PRODUCT( |
ysr@2651 | 651 | if (PrintGCDetails && TraceReferenceGC && (iter.processed() > 0)) { |
jmasa@2188 | 652 | gclog_or_tty->print_cr(" Dropped %d active Refs out of %d " |
jmasa@2188 | 653 | "Refs in discovered list " INTPTR_FORMAT, |
jmasa@2188 | 654 | iter.removed(), iter.processed(), (address)refs_list.head()); |
duke@435 | 655 | } |
duke@435 | 656 | ) |
duke@435 | 657 | } |
duke@435 | 658 | |
duke@435 | 659 | // Traverse the list and process the referents, by either |
coleenp@548 | 660 | // clearing them or keeping them (and their reachable |
duke@435 | 661 | // closure) alive. |
duke@435 | 662 | void |
coleenp@548 | 663 | ReferenceProcessor::process_phase3(DiscoveredList& refs_list, |
duke@435 | 664 | bool clear_referent, |
duke@435 | 665 | BoolObjectClosure* is_alive, |
duke@435 | 666 | OopClosure* keep_alive, |
duke@435 | 667 | VoidClosure* complete_gc) { |
jmasa@2188 | 668 | ResourceMark rm; |
brutisso@6719 | 669 | DiscoveredListIterator iter(refs_list, keep_alive, is_alive); |
duke@435 | 670 | while (iter.has_next()) { |
duke@435 | 671 | iter.update_discovered(); |
duke@435 | 672 | iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */)); |
duke@435 | 673 | if (clear_referent) { |
duke@435 | 674 | // NULL out referent pointer |
duke@435 | 675 | iter.clear_referent(); |
duke@435 | 676 | } else { |
duke@435 | 677 | // keep the referent around |
duke@435 | 678 | iter.make_referent_alive(); |
duke@435 | 679 | } |
duke@435 | 680 | if (TraceReferenceGC) { |
duke@435 | 681 | gclog_or_tty->print_cr("Adding %sreference (" INTPTR_FORMAT ": %s) as pending", |
duke@435 | 682 | clear_referent ? "cleared " : "", |
hseigel@5784 | 683 | (void *)iter.obj(), iter.obj()->klass()->internal_name()); |
duke@435 | 684 | } |
duke@435 | 685 | assert(iter.obj()->is_oop(UseConcMarkSweepGC), "Adding a bad reference"); |
duke@435 | 686 | iter.next(); |
duke@435 | 687 | } |
stefank@3115 | 688 | // Remember to update the next pointer of the last ref. |
duke@435 | 689 | iter.update_discovered(); |
duke@435 | 690 | // Close the reachable set |
duke@435 | 691 | complete_gc->do_void(); |
duke@435 | 692 | } |
duke@435 | 693 | |
duke@435 | 694 | void |
stefank@3115 | 695 | ReferenceProcessor::clear_discovered_references(DiscoveredList& refs_list) { |
stefank@3115 | 696 | oop obj = NULL; |
stefank@3115 | 697 | oop next = refs_list.head(); |
stefank@3115 | 698 | while (next != obj) { |
stefank@3115 | 699 | obj = next; |
stefank@3115 | 700 | next = java_lang_ref_Reference::discovered(obj); |
stefank@3115 | 701 | java_lang_ref_Reference::set_discovered_raw(obj, NULL); |
stefank@3115 | 702 | } |
stefank@3115 | 703 | refs_list.set_head(NULL); |
stefank@3115 | 704 | refs_list.set_length(0); |
stefank@3115 | 705 | } |
stefank@3115 | 706 | |
stefank@3115 | 707 | void |
coleenp@548 | 708 | ReferenceProcessor::abandon_partial_discovered_list(DiscoveredList& refs_list) { |
stefank@3115 | 709 | clear_discovered_references(refs_list); |
duke@435 | 710 | } |
duke@435 | 711 | |
ysr@777 | 712 | void ReferenceProcessor::abandon_partial_discovery() { |
ysr@777 | 713 | // loop over the lists |
jmasa@3357 | 714 | for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { |
jmasa@2188 | 715 | if (TraceReferenceGC && PrintGCDetails && ((i % _max_num_q) == 0)) { |
johnc@3175 | 716 | gclog_or_tty->print_cr("\nAbandoning %s discovered list", list_name(i)); |
ysr@777 | 717 | } |
johnc@3210 | 718 | abandon_partial_discovered_list(_discovered_refs[i]); |
duke@435 | 719 | } |
duke@435 | 720 | } |
duke@435 | 721 | |
duke@435 | 722 | class RefProcPhase1Task: public AbstractRefProcTaskExecutor::ProcessTask { |
duke@435 | 723 | public: |
duke@435 | 724 | RefProcPhase1Task(ReferenceProcessor& ref_processor, |
duke@435 | 725 | DiscoveredList refs_lists[], |
duke@435 | 726 | ReferencePolicy* policy, |
duke@435 | 727 | bool marks_oops_alive) |
duke@435 | 728 | : ProcessTask(ref_processor, refs_lists, marks_oops_alive), |
duke@435 | 729 | _policy(policy) |
duke@435 | 730 | { } |
duke@435 | 731 | virtual void work(unsigned int i, BoolObjectClosure& is_alive, |
duke@435 | 732 | OopClosure& keep_alive, |
duke@435 | 733 | VoidClosure& complete_gc) |
duke@435 | 734 | { |
jmasa@2188 | 735 | Thread* thr = Thread::current(); |
jmasa@2188 | 736 | int refs_list_index = ((WorkerThread*)thr)->id(); |
jmasa@2188 | 737 | _ref_processor.process_phase1(_refs_lists[refs_list_index], _policy, |
duke@435 | 738 | &is_alive, &keep_alive, &complete_gc); |
duke@435 | 739 | } |
duke@435 | 740 | private: |
duke@435 | 741 | ReferencePolicy* _policy; |
duke@435 | 742 | }; |
duke@435 | 743 | |
duke@435 | 744 | class RefProcPhase2Task: public AbstractRefProcTaskExecutor::ProcessTask { |
duke@435 | 745 | public: |
duke@435 | 746 | RefProcPhase2Task(ReferenceProcessor& ref_processor, |
duke@435 | 747 | DiscoveredList refs_lists[], |
duke@435 | 748 | bool marks_oops_alive) |
duke@435 | 749 | : ProcessTask(ref_processor, refs_lists, marks_oops_alive) |
duke@435 | 750 | { } |
duke@435 | 751 | virtual void work(unsigned int i, BoolObjectClosure& is_alive, |
duke@435 | 752 | OopClosure& keep_alive, |
duke@435 | 753 | VoidClosure& complete_gc) |
duke@435 | 754 | { |
duke@435 | 755 | _ref_processor.process_phase2(_refs_lists[i], |
duke@435 | 756 | &is_alive, &keep_alive, &complete_gc); |
duke@435 | 757 | } |
duke@435 | 758 | }; |
duke@435 | 759 | |
duke@435 | 760 | class RefProcPhase3Task: public AbstractRefProcTaskExecutor::ProcessTask { |
duke@435 | 761 | public: |
duke@435 | 762 | RefProcPhase3Task(ReferenceProcessor& ref_processor, |
duke@435 | 763 | DiscoveredList refs_lists[], |
duke@435 | 764 | bool clear_referent, |
duke@435 | 765 | bool marks_oops_alive) |
duke@435 | 766 | : ProcessTask(ref_processor, refs_lists, marks_oops_alive), |
duke@435 | 767 | _clear_referent(clear_referent) |
duke@435 | 768 | { } |
duke@435 | 769 | virtual void work(unsigned int i, BoolObjectClosure& is_alive, |
duke@435 | 770 | OopClosure& keep_alive, |
duke@435 | 771 | VoidClosure& complete_gc) |
duke@435 | 772 | { |
jmasa@2188 | 773 | // Don't use "refs_list_index" calculated in this way because |
jmasa@2188 | 774 | // balance_queues() has moved the Ref's into the first n queues. |
jmasa@2188 | 775 | // Thread* thr = Thread::current(); |
jmasa@2188 | 776 | // int refs_list_index = ((WorkerThread*)thr)->id(); |
jmasa@2188 | 777 | // _ref_processor.process_phase3(_refs_lists[refs_list_index], _clear_referent, |
duke@435 | 778 | _ref_processor.process_phase3(_refs_lists[i], _clear_referent, |
duke@435 | 779 | &is_alive, &keep_alive, &complete_gc); |
duke@435 | 780 | } |
duke@435 | 781 | private: |
duke@435 | 782 | bool _clear_referent; |
duke@435 | 783 | }; |
duke@435 | 784 | |
duke@435 | 785 | // Balances reference queues. |
jmasa@2188 | 786 | // Move entries from all queues[0, 1, ..., _max_num_q-1] to |
jmasa@2188 | 787 | // queues[0, 1, ..., _num_q-1] because only the first _num_q |
jmasa@2188 | 788 | // corresponding to the active workers will be processed. |
duke@435 | 789 | void ReferenceProcessor::balance_queues(DiscoveredList ref_lists[]) |
duke@435 | 790 | { |
duke@435 | 791 | // calculate total length |
duke@435 | 792 | size_t total_refs = 0; |
jmasa@2188 | 793 | if (TraceReferenceGC && PrintGCDetails) { |
jmasa@2188 | 794 | gclog_or_tty->print_cr("\nBalance ref_lists "); |
jmasa@2188 | 795 | } |
jmasa@2188 | 796 | |
jmasa@3357 | 797 | for (uint i = 0; i < _max_num_q; ++i) { |
duke@435 | 798 | total_refs += ref_lists[i].length(); |
jmasa@2188 | 799 | if (TraceReferenceGC && PrintGCDetails) { |
jmasa@2188 | 800 | gclog_or_tty->print("%d ", ref_lists[i].length()); |
jmasa@2188 | 801 | } |
jmasa@2188 | 802 | } |
jmasa@2188 | 803 | if (TraceReferenceGC && PrintGCDetails) { |
jmasa@2188 | 804 | gclog_or_tty->print_cr(" = %d", total_refs); |
duke@435 | 805 | } |
duke@435 | 806 | size_t avg_refs = total_refs / _num_q + 1; |
jmasa@3357 | 807 | uint to_idx = 0; |
jmasa@3357 | 808 | for (uint from_idx = 0; from_idx < _max_num_q; from_idx++) { |
jmasa@2188 | 809 | bool move_all = false; |
jmasa@2188 | 810 | if (from_idx >= _num_q) { |
jmasa@2188 | 811 | move_all = ref_lists[from_idx].length() > 0; |
jmasa@2188 | 812 | } |
jmasa@2188 | 813 | while ((ref_lists[from_idx].length() > avg_refs) || |
jmasa@2188 | 814 | move_all) { |
duke@435 | 815 | assert(to_idx < _num_q, "Sanity Check!"); |
duke@435 | 816 | if (ref_lists[to_idx].length() < avg_refs) { |
duke@435 | 817 | // move superfluous refs |
jmasa@2188 | 818 | size_t refs_to_move; |
jmasa@2188 | 819 | // Move all the Ref's if the from queue will not be processed. |
jmasa@2188 | 820 | if (move_all) { |
jmasa@2188 | 821 | refs_to_move = MIN2(ref_lists[from_idx].length(), |
jmasa@2188 | 822 | avg_refs - ref_lists[to_idx].length()); |
jmasa@2188 | 823 | } else { |
jmasa@2188 | 824 | refs_to_move = MIN2(ref_lists[from_idx].length() - avg_refs, |
jmasa@2188 | 825 | avg_refs - ref_lists[to_idx].length()); |
jmasa@2188 | 826 | } |
stefank@3115 | 827 | |
stefank@3115 | 828 | assert(refs_to_move > 0, "otherwise the code below will fail"); |
stefank@3115 | 829 | |
duke@435 | 830 | oop move_head = ref_lists[from_idx].head(); |
duke@435 | 831 | oop move_tail = move_head; |
duke@435 | 832 | oop new_head = move_head; |
duke@435 | 833 | // find an element to split the list on |
duke@435 | 834 | for (size_t j = 0; j < refs_to_move; ++j) { |
duke@435 | 835 | move_tail = new_head; |
coleenp@548 | 836 | new_head = java_lang_ref_Reference::discovered(new_head); |
duke@435 | 837 | } |
stefank@3115 | 838 | |
stefank@3115 | 839 | // Add the chain to the to list. |
stefank@3115 | 840 | if (ref_lists[to_idx].head() == NULL) { |
stefank@3115 | 841 | // to list is empty. Make a loop at the end. |
brutisso@6719 | 842 | java_lang_ref_Reference::set_discovered_raw(move_tail, move_tail); |
stefank@3115 | 843 | } else { |
brutisso@6719 | 844 | java_lang_ref_Reference::set_discovered_raw(move_tail, ref_lists[to_idx].head()); |
stefank@3115 | 845 | } |
duke@435 | 846 | ref_lists[to_idx].set_head(move_head); |
ysr@887 | 847 | ref_lists[to_idx].inc_length(refs_to_move); |
stefank@3115 | 848 | |
stefank@3115 | 849 | // Remove the chain from the from list. |
stefank@3115 | 850 | if (move_tail == new_head) { |
stefank@3115 | 851 | // We found the end of the from list. |
stefank@3115 | 852 | ref_lists[from_idx].set_head(NULL); |
stefank@3115 | 853 | } else { |
stefank@3115 | 854 | ref_lists[from_idx].set_head(new_head); |
stefank@3115 | 855 | } |
ysr@887 | 856 | ref_lists[from_idx].dec_length(refs_to_move); |
jmasa@2188 | 857 | if (ref_lists[from_idx].length() == 0) { |
jmasa@2188 | 858 | break; |
jmasa@2188 | 859 | } |
duke@435 | 860 | } else { |
jmasa@2188 | 861 | to_idx = (to_idx + 1) % _num_q; |
duke@435 | 862 | } |
duke@435 | 863 | } |
duke@435 | 864 | } |
jmasa@2188 | 865 | #ifdef ASSERT |
jmasa@2188 | 866 | size_t balanced_total_refs = 0; |
jmasa@3357 | 867 | for (uint i = 0; i < _max_num_q; ++i) { |
jmasa@2188 | 868 | balanced_total_refs += ref_lists[i].length(); |
jmasa@2188 | 869 | if (TraceReferenceGC && PrintGCDetails) { |
jmasa@2188 | 870 | gclog_or_tty->print("%d ", ref_lists[i].length()); |
jmasa@2188 | 871 | } |
jmasa@2188 | 872 | } |
jmasa@2188 | 873 | if (TraceReferenceGC && PrintGCDetails) { |
jmasa@2188 | 874 | gclog_or_tty->print_cr(" = %d", balanced_total_refs); |
jmasa@2188 | 875 | gclog_or_tty->flush(); |
jmasa@2188 | 876 | } |
jmasa@2188 | 877 | assert(total_refs == balanced_total_refs, "Balancing was incomplete"); |
jmasa@2188 | 878 | #endif |
jmasa@2188 | 879 | } |
jmasa@2188 | 880 | |
jmasa@2188 | 881 | void ReferenceProcessor::balance_all_queues() { |
jmasa@2188 | 882 | balance_queues(_discoveredSoftRefs); |
jmasa@2188 | 883 | balance_queues(_discoveredWeakRefs); |
jmasa@2188 | 884 | balance_queues(_discoveredFinalRefs); |
jmasa@2188 | 885 | balance_queues(_discoveredPhantomRefs); |
duke@435 | 886 | } |
duke@435 | 887 | |
sla@5237 | 888 | size_t |
duke@435 | 889 | ReferenceProcessor::process_discovered_reflist( |
duke@435 | 890 | DiscoveredList refs_lists[], |
duke@435 | 891 | ReferencePolicy* policy, |
duke@435 | 892 | bool clear_referent, |
duke@435 | 893 | BoolObjectClosure* is_alive, |
duke@435 | 894 | OopClosure* keep_alive, |
duke@435 | 895 | VoidClosure* complete_gc, |
duke@435 | 896 | AbstractRefProcTaskExecutor* task_executor) |
duke@435 | 897 | { |
jmasa@2188 | 898 | bool mt_processing = task_executor != NULL && _processing_is_mt; |
jmasa@2188 | 899 | // If discovery used MT and a dynamic number of GC threads, then |
jmasa@2188 | 900 | // the queues must be balanced for correctness if fewer than the |
jmasa@2188 | 901 | // maximum number of queues were used. The number of queue used |
jmasa@2188 | 902 | // during discovery may be different than the number to be used |
jmasa@2188 | 903 | // for processing so don't depend of _num_q < _max_num_q as part |
jmasa@2188 | 904 | // of the test. |
jmasa@2188 | 905 | bool must_balance = _discovery_is_mt; |
jmasa@2188 | 906 | |
jmasa@2188 | 907 | if ((mt_processing && ParallelRefProcBalancingEnabled) || |
jmasa@2188 | 908 | must_balance) { |
duke@435 | 909 | balance_queues(refs_lists); |
duke@435 | 910 | } |
sla@5237 | 911 | |
sla@5237 | 912 | size_t total_list_count = total_count(refs_lists); |
sla@5237 | 913 | |
duke@435 | 914 | if (PrintReferenceGC && PrintGCDetails) { |
sla@5237 | 915 | gclog_or_tty->print(", %u refs", total_list_count); |
duke@435 | 916 | } |
duke@435 | 917 | |
duke@435 | 918 | // Phase 1 (soft refs only): |
duke@435 | 919 | // . Traverse the list and remove any SoftReferences whose |
duke@435 | 920 | // referents are not alive, but that should be kept alive for |
duke@435 | 921 | // policy reasons. Keep alive the transitive closure of all |
duke@435 | 922 | // such referents. |
duke@435 | 923 | if (policy != NULL) { |
jmasa@2188 | 924 | if (mt_processing) { |
duke@435 | 925 | RefProcPhase1Task phase1(*this, refs_lists, policy, true /*marks_oops_alive*/); |
duke@435 | 926 | task_executor->execute(phase1); |
duke@435 | 927 | } else { |
jmasa@3357 | 928 | for (uint i = 0; i < _max_num_q; i++) { |
duke@435 | 929 | process_phase1(refs_lists[i], policy, |
duke@435 | 930 | is_alive, keep_alive, complete_gc); |
duke@435 | 931 | } |
duke@435 | 932 | } |
duke@435 | 933 | } else { // policy == NULL |
duke@435 | 934 | assert(refs_lists != _discoveredSoftRefs, |
duke@435 | 935 | "Policy must be specified for soft references."); |
duke@435 | 936 | } |
duke@435 | 937 | |
duke@435 | 938 | // Phase 2: |
duke@435 | 939 | // . Traverse the list and remove any refs whose referents are alive. |
jmasa@2188 | 940 | if (mt_processing) { |
duke@435 | 941 | RefProcPhase2Task phase2(*this, refs_lists, !discovery_is_atomic() /*marks_oops_alive*/); |
duke@435 | 942 | task_executor->execute(phase2); |
duke@435 | 943 | } else { |
jmasa@3357 | 944 | for (uint i = 0; i < _max_num_q; i++) { |
duke@435 | 945 | process_phase2(refs_lists[i], is_alive, keep_alive, complete_gc); |
duke@435 | 946 | } |
duke@435 | 947 | } |
duke@435 | 948 | |
duke@435 | 949 | // Phase 3: |
duke@435 | 950 | // . Traverse the list and process referents as appropriate. |
jmasa@2188 | 951 | if (mt_processing) { |
duke@435 | 952 | RefProcPhase3Task phase3(*this, refs_lists, clear_referent, true /*marks_oops_alive*/); |
duke@435 | 953 | task_executor->execute(phase3); |
duke@435 | 954 | } else { |
jmasa@3357 | 955 | for (uint i = 0; i < _max_num_q; i++) { |
duke@435 | 956 | process_phase3(refs_lists[i], clear_referent, |
duke@435 | 957 | is_alive, keep_alive, complete_gc); |
duke@435 | 958 | } |
duke@435 | 959 | } |
sla@5237 | 960 | |
sla@5237 | 961 | return total_list_count; |
duke@435 | 962 | } |
duke@435 | 963 | |
duke@435 | 964 | void ReferenceProcessor::clean_up_discovered_references() { |
duke@435 | 965 | // loop over the lists |
jmasa@3357 | 966 | for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { |
ysr@2651 | 967 | if (TraceReferenceGC && PrintGCDetails && ((i % _max_num_q) == 0)) { |
duke@435 | 968 | gclog_or_tty->print_cr( |
duke@435 | 969 | "\nScrubbing %s discovered list of Null referents", |
duke@435 | 970 | list_name(i)); |
duke@435 | 971 | } |
johnc@3210 | 972 | clean_up_discovered_reflist(_discovered_refs[i]); |
duke@435 | 973 | } |
duke@435 | 974 | } |
duke@435 | 975 | |
duke@435 | 976 | void ReferenceProcessor::clean_up_discovered_reflist(DiscoveredList& refs_list) { |
duke@435 | 977 | assert(!discovery_is_atomic(), "Else why call this method?"); |
brutisso@6719 | 978 | DiscoveredListIterator iter(refs_list, NULL, NULL); |
duke@435 | 979 | while (iter.has_next()) { |
duke@435 | 980 | iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */)); |
coleenp@548 | 981 | oop next = java_lang_ref_Reference::next(iter.obj()); |
coleenp@548 | 982 | assert(next->is_oop_or_null(), "bad next field"); |
duke@435 | 983 | // If referent has been cleared or Reference is not active, |
duke@435 | 984 | // drop it. |
coleenp@548 | 985 | if (iter.referent() == NULL || next != NULL) { |
duke@435 | 986 | debug_only( |
duke@435 | 987 | if (PrintGCDetails && TraceReferenceGC) { |
duke@435 | 988 | gclog_or_tty->print_cr("clean_up_discovered_list: Dropping Reference: " |
duke@435 | 989 | INTPTR_FORMAT " with next field: " INTPTR_FORMAT |
duke@435 | 990 | " and referent: " INTPTR_FORMAT, |
hseigel@5784 | 991 | (void *)iter.obj(), (void *)next, (void *)iter.referent()); |
duke@435 | 992 | } |
duke@435 | 993 | ) |
duke@435 | 994 | // Remove Reference object from list |
duke@435 | 995 | iter.remove(); |
ysr@887 | 996 | iter.move_to_next(); |
duke@435 | 997 | } else { |
duke@435 | 998 | iter.next(); |
duke@435 | 999 | } |
duke@435 | 1000 | } |
duke@435 | 1001 | NOT_PRODUCT( |
duke@435 | 1002 | if (PrintGCDetails && TraceReferenceGC) { |
duke@435 | 1003 | gclog_or_tty->print( |
duke@435 | 1004 | " Removed %d Refs with NULL referents out of %d discovered Refs", |
duke@435 | 1005 | iter.removed(), iter.processed()); |
duke@435 | 1006 | } |
duke@435 | 1007 | ) |
duke@435 | 1008 | } |
duke@435 | 1009 | |
duke@435 | 1010 | inline DiscoveredList* ReferenceProcessor::get_discovered_list(ReferenceType rt) { |
jmasa@3357 | 1011 | uint id = 0; |
duke@435 | 1012 | // Determine the queue index to use for this object. |
duke@435 | 1013 | if (_discovery_is_mt) { |
duke@435 | 1014 | // During a multi-threaded discovery phase, |
duke@435 | 1015 | // each thread saves to its "own" list. |
duke@435 | 1016 | Thread* thr = Thread::current(); |
johnc@2316 | 1017 | id = thr->as_Worker_thread()->id(); |
duke@435 | 1018 | } else { |
duke@435 | 1019 | // single-threaded discovery, we save in round-robin |
duke@435 | 1020 | // fashion to each of the lists. |
duke@435 | 1021 | if (_processing_is_mt) { |
duke@435 | 1022 | id = next_id(); |
duke@435 | 1023 | } |
duke@435 | 1024 | } |
jmasa@2188 | 1025 | assert(0 <= id && id < _max_num_q, "Id is out-of-bounds (call Freud?)"); |
duke@435 | 1026 | |
duke@435 | 1027 | // Get the discovered queue to which we will add |
duke@435 | 1028 | DiscoveredList* list = NULL; |
duke@435 | 1029 | switch (rt) { |
duke@435 | 1030 | case REF_OTHER: |
duke@435 | 1031 | // Unknown reference type, no special treatment |
duke@435 | 1032 | break; |
duke@435 | 1033 | case REF_SOFT: |
duke@435 | 1034 | list = &_discoveredSoftRefs[id]; |
duke@435 | 1035 | break; |
duke@435 | 1036 | case REF_WEAK: |
duke@435 | 1037 | list = &_discoveredWeakRefs[id]; |
duke@435 | 1038 | break; |
duke@435 | 1039 | case REF_FINAL: |
duke@435 | 1040 | list = &_discoveredFinalRefs[id]; |
duke@435 | 1041 | break; |
duke@435 | 1042 | case REF_PHANTOM: |
duke@435 | 1043 | list = &_discoveredPhantomRefs[id]; |
duke@435 | 1044 | break; |
duke@435 | 1045 | case REF_NONE: |
coleenp@4047 | 1046 | // we should not reach here if we are an InstanceRefKlass |
duke@435 | 1047 | default: |
duke@435 | 1048 | ShouldNotReachHere(); |
duke@435 | 1049 | } |
jmasa@2188 | 1050 | if (TraceReferenceGC && PrintGCDetails) { |
johnc@2316 | 1051 | gclog_or_tty->print_cr("Thread %d gets list " INTPTR_FORMAT, id, list); |
jmasa@2188 | 1052 | } |
duke@435 | 1053 | return list; |
duke@435 | 1054 | } |
duke@435 | 1055 | |
coleenp@548 | 1056 | inline void |
coleenp@548 | 1057 | ReferenceProcessor::add_to_discovered_list_mt(DiscoveredList& refs_list, |
coleenp@548 | 1058 | oop obj, |
coleenp@548 | 1059 | HeapWord* discovered_addr) { |
duke@435 | 1060 | assert(_discovery_is_mt, "!_discovery_is_mt should have been handled by caller"); |
duke@435 | 1061 | // First we must make sure this object is only enqueued once. CAS in a non null |
duke@435 | 1062 | // discovered_addr. |
ysr@777 | 1063 | oop current_head = refs_list.head(); |
stefank@3115 | 1064 | // The last ref must have its discovered field pointing to itself. |
stefank@3115 | 1065 | oop next_discovered = (current_head != NULL) ? current_head : obj; |
ysr@777 | 1066 | |
stefank@3115 | 1067 | oop retest = oopDesc::atomic_compare_exchange_oop(next_discovered, discovered_addr, |
coleenp@548 | 1068 | NULL); |
duke@435 | 1069 | if (retest == NULL) { |
duke@435 | 1070 | // This thread just won the right to enqueue the object. |
ysr@3117 | 1071 | // We have separate lists for enqueueing, so no synchronization |
duke@435 | 1072 | // is necessary. |
coleenp@548 | 1073 | refs_list.set_head(obj); |
ysr@887 | 1074 | refs_list.inc_length(1); |
johnc@2316 | 1075 | |
johnc@2316 | 1076 | if (TraceReferenceGC) { |
ysr@3117 | 1077 | gclog_or_tty->print_cr("Discovered reference (mt) (" INTPTR_FORMAT ": %s)", |
hseigel@5784 | 1078 | (void *)obj, obj->klass()->internal_name()); |
johnc@2316 | 1079 | } |
duke@435 | 1080 | } else { |
duke@435 | 1081 | // If retest was non NULL, another thread beat us to it: |
duke@435 | 1082 | // The reference has already been discovered... |
duke@435 | 1083 | if (TraceReferenceGC) { |
ysr@3117 | 1084 | gclog_or_tty->print_cr("Already discovered reference (" INTPTR_FORMAT ": %s)", |
hseigel@5784 | 1085 | (void *)obj, obj->klass()->internal_name()); |
duke@435 | 1086 | } |
duke@435 | 1087 | } |
duke@435 | 1088 | } |
duke@435 | 1089 | |
ysr@2337 | 1090 | #ifndef PRODUCT |
ysr@2337 | 1091 | // Non-atomic (i.e. concurrent) discovery might allow us |
ysr@2337 | 1092 | // to observe j.l.References with NULL referents, being those |
ysr@2337 | 1093 | // cleared concurrently by mutators during (or after) discovery. |
ysr@2337 | 1094 | void ReferenceProcessor::verify_referent(oop obj) { |
ysr@2337 | 1095 | bool da = discovery_is_atomic(); |
ysr@2337 | 1096 | oop referent = java_lang_ref_Reference::referent(obj); |
ysr@2337 | 1097 | assert(da ? referent->is_oop() : referent->is_oop_or_null(), |
ysr@2337 | 1098 | err_msg("Bad referent " INTPTR_FORMAT " found in Reference " |
ysr@2337 | 1099 | INTPTR_FORMAT " during %satomic discovery ", |
hseigel@5784 | 1100 | (void *)referent, (void *)obj, da ? "" : "non-")); |
ysr@2337 | 1101 | } |
ysr@2337 | 1102 | #endif |
ysr@2337 | 1103 | |
duke@435 | 1104 | // We mention two of several possible choices here: |
duke@435 | 1105 | // #0: if the reference object is not in the "originating generation" |
duke@435 | 1106 | // (or part of the heap being collected, indicated by our "span" |
duke@435 | 1107 | // we don't treat it specially (i.e. we scan it as we would |
duke@435 | 1108 | // a normal oop, treating its references as strong references). |
ysr@3117 | 1109 | // This means that references can't be discovered unless their |
duke@435 | 1110 | // referent is also in the same span. This is the simplest, |
duke@435 | 1111 | // most "local" and most conservative approach, albeit one |
duke@435 | 1112 | // that may cause weak references to be enqueued least promptly. |
duke@435 | 1113 | // We call this choice the "ReferenceBasedDiscovery" policy. |
duke@435 | 1114 | // #1: the reference object may be in any generation (span), but if |
duke@435 | 1115 | // the referent is in the generation (span) being currently collected |
duke@435 | 1116 | // then we can discover the reference object, provided |
duke@435 | 1117 | // the object has not already been discovered by |
duke@435 | 1118 | // a different concurrently running collector (as may be the |
duke@435 | 1119 | // case, for instance, if the reference object is in CMS and |
duke@435 | 1120 | // the referent in DefNewGeneration), and provided the processing |
duke@435 | 1121 | // of this reference object by the current collector will |
duke@435 | 1122 | // appear atomic to every other collector in the system. |
duke@435 | 1123 | // (Thus, for instance, a concurrent collector may not |
duke@435 | 1124 | // discover references in other generations even if the |
duke@435 | 1125 | // referent is in its own generation). This policy may, |
duke@435 | 1126 | // in certain cases, enqueue references somewhat sooner than |
duke@435 | 1127 | // might Policy #0 above, but at marginally increased cost |
duke@435 | 1128 | // and complexity in processing these references. |
duke@435 | 1129 | // We call this choice the "RefeferentBasedDiscovery" policy. |
duke@435 | 1130 | bool ReferenceProcessor::discover_reference(oop obj, ReferenceType rt) { |
ysr@3117 | 1131 | // Make sure we are discovering refs (rather than processing discovered refs). |
duke@435 | 1132 | if (!_discovering_refs || !RegisterReferences) { |
duke@435 | 1133 | return false; |
duke@435 | 1134 | } |
ysr@3117 | 1135 | // We only discover active references. |
coleenp@548 | 1136 | oop next = java_lang_ref_Reference::next(obj); |
ysr@3117 | 1137 | if (next != NULL) { // Ref is no longer active |
duke@435 | 1138 | return false; |
duke@435 | 1139 | } |
duke@435 | 1140 | |
duke@435 | 1141 | HeapWord* obj_addr = (HeapWord*)obj; |
duke@435 | 1142 | if (RefDiscoveryPolicy == ReferenceBasedDiscovery && |
duke@435 | 1143 | !_span.contains(obj_addr)) { |
duke@435 | 1144 | // Reference is not in the originating generation; |
duke@435 | 1145 | // don't treat it specially (i.e. we want to scan it as a normal |
duke@435 | 1146 | // object with strong references). |
duke@435 | 1147 | return false; |
duke@435 | 1148 | } |
duke@435 | 1149 | |
ysr@3117 | 1150 | // We only discover references whose referents are not (yet) |
ysr@3117 | 1151 | // known to be strongly reachable. |
duke@435 | 1152 | if (is_alive_non_header() != NULL) { |
ysr@2337 | 1153 | verify_referent(obj); |
ysr@2337 | 1154 | if (is_alive_non_header()->do_object_b(java_lang_ref_Reference::referent(obj))) { |
duke@435 | 1155 | return false; // referent is reachable |
duke@435 | 1156 | } |
duke@435 | 1157 | } |
ysr@888 | 1158 | if (rt == REF_SOFT) { |
ysr@888 | 1159 | // For soft refs we can decide now if these are not |
ysr@888 | 1160 | // current candidates for clearing, in which case we |
ysr@888 | 1161 | // can mark through them now, rather than delaying that |
ysr@888 | 1162 | // to the reference-processing phase. Since all current |
ysr@888 | 1163 | // time-stamp policies advance the soft-ref clock only |
ysr@888 | 1164 | // at a major collection cycle, this is always currently |
ysr@888 | 1165 | // accurate. |
johnc@3188 | 1166 | if (!_current_soft_ref_policy->should_clear_reference(obj, _soft_ref_timestamp_clock)) { |
ysr@888 | 1167 | return false; |
ysr@888 | 1168 | } |
ysr@888 | 1169 | } |
duke@435 | 1170 | |
johnc@3175 | 1171 | ResourceMark rm; // Needed for tracing. |
johnc@3175 | 1172 | |
ysr@777 | 1173 | HeapWord* const discovered_addr = java_lang_ref_Reference::discovered_addr(obj); |
ysr@777 | 1174 | const oop discovered = java_lang_ref_Reference::discovered(obj); |
coleenp@548 | 1175 | assert(discovered->is_oop_or_null(), "bad discovered field"); |
coleenp@548 | 1176 | if (discovered != NULL) { |
duke@435 | 1177 | // The reference has already been discovered... |
duke@435 | 1178 | if (TraceReferenceGC) { |
ysr@3117 | 1179 | gclog_or_tty->print_cr("Already discovered reference (" INTPTR_FORMAT ": %s)", |
hseigel@5784 | 1180 | (void *)obj, obj->klass()->internal_name()); |
duke@435 | 1181 | } |
duke@435 | 1182 | if (RefDiscoveryPolicy == ReferentBasedDiscovery) { |
duke@435 | 1183 | // assumes that an object is not processed twice; |
duke@435 | 1184 | // if it's been already discovered it must be on another |
duke@435 | 1185 | // generation's discovered list; so we won't discover it. |
duke@435 | 1186 | return false; |
duke@435 | 1187 | } else { |
duke@435 | 1188 | assert(RefDiscoveryPolicy == ReferenceBasedDiscovery, |
duke@435 | 1189 | "Unrecognized policy"); |
duke@435 | 1190 | // Check assumption that an object is not potentially |
duke@435 | 1191 | // discovered twice except by concurrent collectors that potentially |
duke@435 | 1192 | // trace the same Reference object twice. |
johnc@2316 | 1193 | assert(UseConcMarkSweepGC || UseG1GC, |
johnc@2316 | 1194 | "Only possible with a concurrent marking collector"); |
duke@435 | 1195 | return true; |
duke@435 | 1196 | } |
duke@435 | 1197 | } |
duke@435 | 1198 | |
duke@435 | 1199 | if (RefDiscoveryPolicy == ReferentBasedDiscovery) { |
ysr@2337 | 1200 | verify_referent(obj); |
ysr@3117 | 1201 | // Discover if and only if EITHER: |
ysr@3117 | 1202 | // .. reference is in our span, OR |
ysr@3117 | 1203 | // .. we are an atomic collector and referent is in our span |
duke@435 | 1204 | if (_span.contains(obj_addr) || |
ysr@2337 | 1205 | (discovery_is_atomic() && |
ysr@2337 | 1206 | _span.contains(java_lang_ref_Reference::referent(obj)))) { |
duke@435 | 1207 | // should_enqueue = true; |
duke@435 | 1208 | } else { |
duke@435 | 1209 | return false; |
duke@435 | 1210 | } |
duke@435 | 1211 | } else { |
duke@435 | 1212 | assert(RefDiscoveryPolicy == ReferenceBasedDiscovery && |
duke@435 | 1213 | _span.contains(obj_addr), "code inconsistency"); |
duke@435 | 1214 | } |
duke@435 | 1215 | |
duke@435 | 1216 | // Get the right type of discovered queue head. |
duke@435 | 1217 | DiscoveredList* list = get_discovered_list(rt); |
duke@435 | 1218 | if (list == NULL) { |
duke@435 | 1219 | return false; // nothing special needs to be done |
duke@435 | 1220 | } |
duke@435 | 1221 | |
duke@435 | 1222 | if (_discovery_is_mt) { |
duke@435 | 1223 | add_to_discovered_list_mt(*list, obj, discovered_addr); |
duke@435 | 1224 | } else { |
brutisso@6719 | 1225 | // We do a raw store here: the field will be visited later when processing |
brutisso@6719 | 1226 | // the discovered references. |
ysr@777 | 1227 | oop current_head = list->head(); |
stefank@3115 | 1228 | // The last ref must have its discovered field pointing to itself. |
stefank@3115 | 1229 | oop next_discovered = (current_head != NULL) ? current_head : obj; |
stefank@3115 | 1230 | |
ysr@777 | 1231 | assert(discovered == NULL, "control point invariant"); |
stefank@3115 | 1232 | oop_store_raw(discovered_addr, next_discovered); |
duke@435 | 1233 | list->set_head(obj); |
ysr@887 | 1234 | list->inc_length(1); |
duke@435 | 1235 | |
johnc@2316 | 1236 | if (TraceReferenceGC) { |
ysr@3117 | 1237 | gclog_or_tty->print_cr("Discovered reference (" INTPTR_FORMAT ": %s)", |
hseigel@5784 | 1238 | (void *)obj, obj->klass()->internal_name()); |
duke@435 | 1239 | } |
duke@435 | 1240 | } |
ysr@3117 | 1241 | assert(obj->is_oop(), "Discovered a bad reference"); |
ysr@2337 | 1242 | verify_referent(obj); |
duke@435 | 1243 | return true; |
duke@435 | 1244 | } |
duke@435 | 1245 | |
duke@435 | 1246 | // Preclean the discovered references by removing those |
duke@435 | 1247 | // whose referents are alive, and by marking from those that |
duke@435 | 1248 | // are not active. These lists can be handled here |
duke@435 | 1249 | // in any order and, indeed, concurrently. |
duke@435 | 1250 | void ReferenceProcessor::preclean_discovered_references( |
duke@435 | 1251 | BoolObjectClosure* is_alive, |
duke@435 | 1252 | OopClosure* keep_alive, |
duke@435 | 1253 | VoidClosure* complete_gc, |
sla@5237 | 1254 | YieldClosure* yield, |
brutisso@6904 | 1255 | GCTimer* gc_timer, |
brutisso@6904 | 1256 | GCId gc_id) { |
duke@435 | 1257 | |
duke@435 | 1258 | NOT_PRODUCT(verify_ok_to_handle_reflists()); |
duke@435 | 1259 | |
duke@435 | 1260 | // Soft references |
duke@435 | 1261 | { |
sla@5237 | 1262 | GCTraceTime tt("Preclean SoftReferences", PrintGCDetails && PrintReferenceGC, |
brutisso@6904 | 1263 | false, gc_timer, gc_id); |
jmasa@3357 | 1264 | for (uint i = 0; i < _max_num_q; i++) { |
ysr@887 | 1265 | if (yield->should_return()) { |
ysr@887 | 1266 | return; |
ysr@887 | 1267 | } |
duke@435 | 1268 | preclean_discovered_reflist(_discoveredSoftRefs[i], is_alive, |
duke@435 | 1269 | keep_alive, complete_gc, yield); |
duke@435 | 1270 | } |
duke@435 | 1271 | } |
duke@435 | 1272 | |
duke@435 | 1273 | // Weak references |
duke@435 | 1274 | { |
sla@5237 | 1275 | GCTraceTime tt("Preclean WeakReferences", PrintGCDetails && PrintReferenceGC, |
brutisso@6904 | 1276 | false, gc_timer, gc_id); |
jmasa@3357 | 1277 | for (uint i = 0; i < _max_num_q; i++) { |
ysr@887 | 1278 | if (yield->should_return()) { |
ysr@887 | 1279 | return; |
ysr@887 | 1280 | } |
duke@435 | 1281 | preclean_discovered_reflist(_discoveredWeakRefs[i], is_alive, |
duke@435 | 1282 | keep_alive, complete_gc, yield); |
duke@435 | 1283 | } |
duke@435 | 1284 | } |
duke@435 | 1285 | |
duke@435 | 1286 | // Final references |
duke@435 | 1287 | { |
sla@5237 | 1288 | GCTraceTime tt("Preclean FinalReferences", PrintGCDetails && PrintReferenceGC, |
brutisso@6904 | 1289 | false, gc_timer, gc_id); |
jmasa@3357 | 1290 | for (uint i = 0; i < _max_num_q; i++) { |
ysr@887 | 1291 | if (yield->should_return()) { |
ysr@887 | 1292 | return; |
ysr@887 | 1293 | } |
duke@435 | 1294 | preclean_discovered_reflist(_discoveredFinalRefs[i], is_alive, |
duke@435 | 1295 | keep_alive, complete_gc, yield); |
duke@435 | 1296 | } |
duke@435 | 1297 | } |
duke@435 | 1298 | |
duke@435 | 1299 | // Phantom references |
duke@435 | 1300 | { |
sla@5237 | 1301 | GCTraceTime tt("Preclean PhantomReferences", PrintGCDetails && PrintReferenceGC, |
brutisso@6904 | 1302 | false, gc_timer, gc_id); |
jmasa@3357 | 1303 | for (uint i = 0; i < _max_num_q; i++) { |
ysr@887 | 1304 | if (yield->should_return()) { |
ysr@887 | 1305 | return; |
ysr@887 | 1306 | } |
duke@435 | 1307 | preclean_discovered_reflist(_discoveredPhantomRefs[i], is_alive, |
duke@435 | 1308 | keep_alive, complete_gc, yield); |
duke@435 | 1309 | } |
duke@435 | 1310 | } |
duke@435 | 1311 | } |
duke@435 | 1312 | |
duke@435 | 1313 | // Walk the given discovered ref list, and remove all reference objects |
duke@435 | 1314 | // whose referents are still alive, whose referents are NULL or which |
ysr@887 | 1315 | // are not active (have a non-NULL next field). NOTE: When we are |
ysr@887 | 1316 | // thus precleaning the ref lists (which happens single-threaded today), |
ysr@887 | 1317 | // we do not disable refs discovery to honour the correct semantics of |
ysr@887 | 1318 | // java.lang.Reference. As a result, we need to be careful below |
ysr@887 | 1319 | // that ref removal steps interleave safely with ref discovery steps |
ysr@887 | 1320 | // (in this thread). |
coleenp@548 | 1321 | void |
coleenp@548 | 1322 | ReferenceProcessor::preclean_discovered_reflist(DiscoveredList& refs_list, |
coleenp@548 | 1323 | BoolObjectClosure* is_alive, |
coleenp@548 | 1324 | OopClosure* keep_alive, |
coleenp@548 | 1325 | VoidClosure* complete_gc, |
coleenp@548 | 1326 | YieldClosure* yield) { |
brutisso@6719 | 1327 | DiscoveredListIterator iter(refs_list, keep_alive, is_alive); |
duke@435 | 1328 | while (iter.has_next()) { |
duke@435 | 1329 | iter.load_ptrs(DEBUG_ONLY(true /* allow_null_referent */)); |
coleenp@548 | 1330 | oop obj = iter.obj(); |
coleenp@548 | 1331 | oop next = java_lang_ref_Reference::next(obj); |
duke@435 | 1332 | if (iter.referent() == NULL || iter.is_referent_alive() || |
coleenp@548 | 1333 | next != NULL) { |
duke@435 | 1334 | // The referent has been cleared, or is alive, or the Reference is not |
duke@435 | 1335 | // active; we need to trace and mark its cohort. |
duke@435 | 1336 | if (TraceReferenceGC) { |
duke@435 | 1337 | gclog_or_tty->print_cr("Precleaning Reference (" INTPTR_FORMAT ": %s)", |
hseigel@5784 | 1338 | (void *)iter.obj(), iter.obj()->klass()->internal_name()); |
duke@435 | 1339 | } |
duke@435 | 1340 | // Remove Reference object from list |
duke@435 | 1341 | iter.remove(); |
duke@435 | 1342 | // Keep alive its cohort. |
duke@435 | 1343 | iter.make_referent_alive(); |
coleenp@548 | 1344 | if (UseCompressedOops) { |
coleenp@548 | 1345 | narrowOop* next_addr = (narrowOop*)java_lang_ref_Reference::next_addr(obj); |
coleenp@548 | 1346 | keep_alive->do_oop(next_addr); |
coleenp@548 | 1347 | } else { |
coleenp@548 | 1348 | oop* next_addr = (oop*)java_lang_ref_Reference::next_addr(obj); |
coleenp@548 | 1349 | keep_alive->do_oop(next_addr); |
coleenp@548 | 1350 | } |
ysr@887 | 1351 | iter.move_to_next(); |
duke@435 | 1352 | } else { |
duke@435 | 1353 | iter.next(); |
duke@435 | 1354 | } |
duke@435 | 1355 | } |
duke@435 | 1356 | // Close the reachable set |
duke@435 | 1357 | complete_gc->do_void(); |
duke@435 | 1358 | |
duke@435 | 1359 | NOT_PRODUCT( |
ysr@2651 | 1360 | if (PrintGCDetails && PrintReferenceGC && (iter.processed() > 0)) { |
jmasa@2188 | 1361 | gclog_or_tty->print_cr(" Dropped %d Refs out of %d " |
jmasa@2188 | 1362 | "Refs in discovered list " INTPTR_FORMAT, |
jmasa@2188 | 1363 | iter.removed(), iter.processed(), (address)refs_list.head()); |
duke@435 | 1364 | } |
duke@435 | 1365 | ) |
duke@435 | 1366 | } |
duke@435 | 1367 | |
jmasa@3357 | 1368 | const char* ReferenceProcessor::list_name(uint i) { |
johnc@3175 | 1369 | assert(i >= 0 && i <= _max_num_q * number_of_subclasses_of_ref(), |
johnc@3175 | 1370 | "Out of bounds index"); |
johnc@3175 | 1371 | |
jmasa@2188 | 1372 | int j = i / _max_num_q; |
duke@435 | 1373 | switch (j) { |
duke@435 | 1374 | case 0: return "SoftRef"; |
duke@435 | 1375 | case 1: return "WeakRef"; |
duke@435 | 1376 | case 2: return "FinalRef"; |
duke@435 | 1377 | case 3: return "PhantomRef"; |
duke@435 | 1378 | } |
duke@435 | 1379 | ShouldNotReachHere(); |
duke@435 | 1380 | return NULL; |
duke@435 | 1381 | } |
duke@435 | 1382 | |
duke@435 | 1383 | #ifndef PRODUCT |
duke@435 | 1384 | void ReferenceProcessor::verify_ok_to_handle_reflists() { |
duke@435 | 1385 | // empty for now |
duke@435 | 1386 | } |
duke@435 | 1387 | #endif |
duke@435 | 1388 | |
duke@435 | 1389 | #ifndef PRODUCT |
duke@435 | 1390 | void ReferenceProcessor::clear_discovered_references() { |
duke@435 | 1391 | guarantee(!_discovering_refs, "Discovering refs?"); |
jmasa@3357 | 1392 | for (uint i = 0; i < _max_num_q * number_of_subclasses_of_ref(); i++) { |
johnc@3210 | 1393 | clear_discovered_references(_discovered_refs[i]); |
duke@435 | 1394 | } |
duke@435 | 1395 | } |
stefank@3115 | 1396 | |
duke@435 | 1397 | #endif // PRODUCT |