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