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