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