1 /* 2 * Copyright (c) 2013, 2019, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "classfile/symbolTable.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "code/codeCache.hpp" 29 30 #include "gc/shared/weakProcessor.inline.hpp" 31 #include "gc/shared/gcTimer.hpp" 32 #include "gc/shared/referenceProcessor.hpp" 33 #include "gc/shared/referenceProcessorPhaseTimes.hpp" 34 35 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp" 36 #include "gc/shenandoah/shenandoahClosures.inline.hpp" 37 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp" 38 #include "gc/shenandoah/shenandoahMarkCompact.hpp" 39 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 40 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp" 41 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp" 42 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp" 43 #include "gc/shenandoah/shenandoahTimingTracker.hpp" 44 #include "gc/shenandoah/shenandoahUtils.hpp" 45 46 #include "memory/iterator.inline.hpp" 47 #include "memory/metaspace.hpp" 48 #include "memory/resourceArea.hpp" 49 #include "oops/oop.inline.hpp" 50 #include "runtime/handles.inline.hpp" 51 52 template<UpdateRefsMode UPDATE_REFS> 53 class ShenandoahInitMarkRootsClosure : public OopClosure { 54 private: 55 ShenandoahObjToScanQueue* _queue; 56 ShenandoahHeap* _heap; 57 ShenandoahMarkingContext* const _mark_context; 58 59 template <class T> 60 inline void do_oop_work(T* p) { 61 ShenandoahConcurrentMark::mark_through_ref<T, UPDATE_REFS, NO_DEDUP>(p, _heap, _queue, _mark_context); 62 } 63 64 public: 65 ShenandoahInitMarkRootsClosure(ShenandoahObjToScanQueue* q) : 66 _queue(q), 67 _heap(ShenandoahHeap::heap()), 68 _mark_context(_heap->marking_context()) {}; 69 70 void do_oop(narrowOop* p) { do_oop_work(p); } 71 void do_oop(oop* p) { do_oop_work(p); } 72 }; 73 74 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ReferenceProcessor* rp) : 75 MetadataVisitingOopIterateClosure(rp), 76 _queue(q), 77 _heap(ShenandoahHeap::heap()), 78 _mark_context(_heap->marking_context()) 79 { } 80 81 template<UpdateRefsMode UPDATE_REFS> 82 class ShenandoahInitMarkRootsTask : public AbstractGangTask { 83 private: 84 ShenandoahAllRootScanner* _rp; 85 bool _process_refs; 86 public: 87 ShenandoahInitMarkRootsTask(ShenandoahAllRootScanner* rp, bool process_refs) : 88 AbstractGangTask("Shenandoah init mark roots task"), 89 _rp(rp), 90 _process_refs(process_refs) { 91 } 92 93 void work(uint worker_id) { 94 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 95 ShenandoahParallelWorkerSession worker_session(worker_id); 96 97 ShenandoahHeap* heap = ShenandoahHeap::heap(); 98 ShenandoahObjToScanQueueSet* queues = heap->concurrent_mark()->task_queues(); 99 assert(queues->get_reserved() > worker_id, "Queue has not been reserved for worker id: %d", worker_id); 100 101 ShenandoahObjToScanQueue* q = queues->queue(worker_id); 102 103 ShenandoahInitMarkRootsClosure<UPDATE_REFS> mark_cl(q); 104 do_work(heap, &mark_cl, worker_id); 105 } 106 107 private: 108 void do_work(ShenandoahHeap* heap, OopClosure* oops, uint worker_id) { 109 // The rationale for selecting the roots to scan is as follows: 110 // a. With unload_classes = true, we only want to scan the actual strong roots from the 111 // code cache. This will allow us to identify the dead classes, unload them, *and* 112 // invalidate the relevant code cache blobs. This could be only done together with 113 // class unloading. 114 // b. With unload_classes = false, we have to nominally retain all the references from code 115 // cache, because there could be the case of embedded class/oop in the generated code, 116 // which we will never visit during mark. Without code cache invalidation, as in (a), 117 // we risk executing that code cache blob, and crashing. 118 if (heap->unload_classes()) { 119 _rp->strong_roots_do(worker_id, oops); 120 } else { 121 _rp->roots_do(worker_id, oops); 122 } 123 } 124 }; 125 126 class ShenandoahUpdateRootsTask : public AbstractGangTask { 127 private: 128 ShenandoahRootUpdater* _root_updater; 129 public: 130 ShenandoahUpdateRootsTask(ShenandoahRootUpdater* root_updater) : 131 AbstractGangTask("Shenandoah update roots task"), 132 _root_updater(root_updater) { 133 } 134 135 void work(uint worker_id) { 136 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 137 ShenandoahParallelWorkerSession worker_session(worker_id); 138 139 ShenandoahHeap* heap = ShenandoahHeap::heap(); 140 ShenandoahUpdateRefsClosure cl; 141 AlwaysTrueClosure always_true; 142 _root_updater->roots_do<AlwaysTrueClosure, ShenandoahUpdateRefsClosure>(worker_id, &always_true, &cl); 143 } 144 }; 145 146 class ShenandoahConcurrentMarkingTask : public AbstractGangTask { 147 private: 148 ShenandoahConcurrentMark* _cm; 149 ShenandoahTaskTerminator* _terminator; 150 151 public: 152 ShenandoahConcurrentMarkingTask(ShenandoahConcurrentMark* cm, ShenandoahTaskTerminator* terminator) : 153 AbstractGangTask("Root Region Scan"), _cm(cm), _terminator(terminator) { 154 } 155 156 void work(uint worker_id) { 157 ShenandoahHeap* heap = ShenandoahHeap::heap(); 158 ShenandoahConcurrentWorkerSession worker_session(worker_id); 159 ShenandoahSuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers); 160 ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id); 161 ReferenceProcessor* rp; 162 if (heap->process_references()) { 163 rp = heap->ref_processor(); 164 shenandoah_assert_rp_isalive_installed(); 165 } else { 166 rp = NULL; 167 } 168 169 _cm->concurrent_scan_code_roots(worker_id, rp); 170 _cm->mark_loop(worker_id, _terminator, rp, 171 true, // cancellable 172 ShenandoahStringDedup::is_enabled()); // perform string dedup 173 } 174 }; 175 176 class ShenandoahSATBThreadsClosure : public ThreadClosure { 177 private: 178 ShenandoahSATBBufferClosure* _satb_cl; 179 uintx _claim_token; 180 181 public: 182 ShenandoahSATBThreadsClosure(ShenandoahSATBBufferClosure* satb_cl) : 183 _satb_cl(satb_cl), 184 _claim_token(Threads::thread_claim_token()) {} 185 186 void do_thread(Thread* thread) { 187 if (thread->claim_threads_do(true, _claim_token)) { 188 ShenandoahThreadLocalData::satb_mark_queue(thread).apply_closure_and_empty(_satb_cl); 189 } 190 } 191 }; 192 193 class ShenandoahFinalMarkingTask : public AbstractGangTask { 194 private: 195 ShenandoahConcurrentMark* _cm; 196 ShenandoahTaskTerminator* _terminator; 197 bool _dedup_string; 198 199 public: 200 ShenandoahFinalMarkingTask(ShenandoahConcurrentMark* cm, ShenandoahTaskTerminator* terminator, bool dedup_string) : 201 AbstractGangTask("Shenandoah Final Marking"), _cm(cm), _terminator(terminator), _dedup_string(dedup_string) { 202 } 203 204 void work(uint worker_id) { 205 ShenandoahHeap* heap = ShenandoahHeap::heap(); 206 207 ShenandoahParallelWorkerSession worker_session(worker_id); 208 // First drain remaining SATB buffers. 209 // Notice that this is not strictly necessary for mark-compact. But since 210 // it requires a StrongRootsScope around the task, we need to claim the 211 // threads, and performance-wise it doesn't really matter. Adds about 1ms to 212 // full-gc. 213 { 214 ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id); 215 ShenandoahSATBBufferClosure cl(q); 216 SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set(); 217 while (satb_mq_set.apply_closure_to_completed_buffer(&cl)); 218 ShenandoahSATBThreadsClosure tc(&cl); 219 Threads::threads_do(&tc); 220 } 221 222 ReferenceProcessor* rp; 223 if (heap->process_references()) { 224 rp = heap->ref_processor(); 225 shenandoah_assert_rp_isalive_installed(); 226 } else { 227 rp = NULL; 228 } 229 230 if (heap->is_degenerated_gc_in_progress()) { 231 // Degenerated cycle may bypass concurrent cycle, so code roots might not be scanned, 232 // let's check here. 233 _cm->concurrent_scan_code_roots(worker_id, rp); 234 } 235 236 _cm->mark_loop(worker_id, _terminator, rp, 237 false, // not cancellable 238 _dedup_string); 239 240 assert(_cm->task_queues()->is_empty(), "Should be empty"); 241 } 242 }; 243 244 void ShenandoahConcurrentMark::mark_roots(ShenandoahPhaseTimings::Phase root_phase) { 245 assert(Thread::current()->is_VM_thread(), "can only do this in VMThread"); 246 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 247 248 ShenandoahHeap* heap = ShenandoahHeap::heap(); 249 250 ShenandoahGCPhase phase(root_phase); 251 252 WorkGang* workers = heap->workers(); 253 uint nworkers = workers->active_workers(); 254 255 assert(nworkers <= task_queues()->size(), "Just check"); 256 257 ShenandoahAllRootScanner root_proc(nworkers, root_phase); 258 TASKQUEUE_STATS_ONLY(task_queues()->reset_taskqueue_stats()); 259 task_queues()->reserve(nworkers); 260 261 if (heap->has_forwarded_objects()) { 262 ShenandoahInitMarkRootsTask<RESOLVE> mark_roots(&root_proc, _heap->process_references()); 263 workers->run_task(&mark_roots); 264 } else { 265 // No need to update references, which means the heap is stable. 266 // Can save time not walking through forwarding pointers. 267 ShenandoahInitMarkRootsTask<NONE> mark_roots(&root_proc, _heap->process_references()); 268 workers->run_task(&mark_roots); 269 } 270 271 if (ShenandoahConcurrentScanCodeRoots) { 272 clear_claim_codecache(); 273 } 274 } 275 276 void ShenandoahConcurrentMark::update_roots(ShenandoahPhaseTimings::Phase root_phase) { 277 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 278 279 bool update_code_cache = true; // initialize to safer value 280 switch (root_phase) { 281 case ShenandoahPhaseTimings::update_roots: 282 case ShenandoahPhaseTimings::final_update_refs_roots: 283 update_code_cache = false; 284 break; 285 case ShenandoahPhaseTimings::full_gc_roots: 286 case ShenandoahPhaseTimings::degen_gc_update_roots: 287 update_code_cache = true; 288 break; 289 default: 290 ShouldNotReachHere(); 291 } 292 293 ShenandoahGCPhase phase(root_phase); 294 295 #if COMPILER2_OR_JVMCI 296 DerivedPointerTable::clear(); 297 #endif 298 299 uint nworkers = _heap->workers()->active_workers(); 300 301 ShenandoahRootUpdater root_updater(nworkers, root_phase, update_code_cache); 302 ShenandoahUpdateRootsTask update_roots(&root_updater); 303 _heap->workers()->run_task(&update_roots); 304 305 #if COMPILER2_OR_JVMCI 306 DerivedPointerTable::update_pointers(); 307 #endif 308 } 309 310 class ShenandoahUpdateThreadRootsTask : public AbstractGangTask { 311 private: 312 ShenandoahThreadRoots _thread_roots; 313 ShenandoahPhaseTimings::Phase _phase; 314 public: 315 ShenandoahUpdateThreadRootsTask(bool is_par, ShenandoahPhaseTimings::Phase phase) : 316 AbstractGangTask("Shenandoah Update Thread Roots"), 317 _thread_roots(is_par), 318 _phase(phase) { 319 ShenandoahHeap::heap()->phase_timings()->record_workers_start(_phase); 320 } 321 322 ~ShenandoahUpdateThreadRootsTask() { 323 ShenandoahHeap::heap()->phase_timings()->record_workers_end(_phase); 324 } 325 void work(uint worker_id) { 326 ShenandoahUpdateRefsClosure cl; 327 _thread_roots.oops_do(&cl, NULL, worker_id); 328 } 329 }; 330 331 void ShenandoahConcurrentMark::update_thread_roots(ShenandoahPhaseTimings::Phase root_phase) { 332 WorkGang* workers = _heap->workers(); 333 bool is_par = workers->active_workers() > 1; 334 ShenandoahUpdateThreadRootsTask task(is_par, root_phase); 335 workers->run_task(&task); 336 } 337 338 void ShenandoahConcurrentMark::initialize(uint workers) { 339 _heap = ShenandoahHeap::heap(); 340 341 uint num_queues = MAX2(workers, 1U); 342 343 _task_queues = new ShenandoahObjToScanQueueSet((int) num_queues); 344 345 for (uint i = 0; i < num_queues; ++i) { 346 ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue(); 347 task_queue->initialize(); 348 _task_queues->register_queue(i, task_queue); 349 } 350 } 351 352 void ShenandoahConcurrentMark::concurrent_scan_code_roots(uint worker_id, ReferenceProcessor* rp) { 353 if (ShenandoahConcurrentScanCodeRoots && claim_codecache()) { 354 ShenandoahObjToScanQueue* q = task_queues()->queue(worker_id); 355 if (!_heap->unload_classes()) { 356 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 357 // TODO: We can not honor StringDeduplication here, due to lock ranking 358 // inversion. So, we may miss some deduplication candidates. 359 if (_heap->has_forwarded_objects()) { 360 ShenandoahMarkResolveRefsClosure cl(q, rp); 361 CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations); 362 CodeCache::blobs_do(&blobs); 363 } else { 364 ShenandoahMarkRefsClosure cl(q, rp); 365 CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations); 366 CodeCache::blobs_do(&blobs); 367 } 368 } 369 } 370 } 371 372 void ShenandoahConcurrentMark::mark_from_roots() { 373 WorkGang* workers = _heap->workers(); 374 uint nworkers = workers->active_workers(); 375 376 ShenandoahGCPhase conc_mark_phase(ShenandoahPhaseTimings::conc_mark); 377 378 if (_heap->process_references()) { 379 ReferenceProcessor* rp = _heap->ref_processor(); 380 rp->set_active_mt_degree(nworkers); 381 382 // enable ("weak") refs discovery 383 rp->enable_discovery(true /*verify_no_refs*/); 384 rp->setup_policy(_heap->soft_ref_policy()->should_clear_all_soft_refs()); 385 } 386 387 shenandoah_assert_rp_isalive_not_installed(); 388 ShenandoahIsAliveSelector is_alive; 389 ReferenceProcessorIsAliveMutator fix_isalive(_heap->ref_processor(), is_alive.is_alive_closure()); 390 391 task_queues()->reserve(nworkers); 392 393 { 394 ShenandoahTerminationTracker term(ShenandoahPhaseTimings::conc_termination); 395 ShenandoahTaskTerminator terminator(nworkers, task_queues()); 396 ShenandoahConcurrentMarkingTask task(this, &terminator); 397 workers->run_task(&task); 398 } 399 400 assert(task_queues()->is_empty() || _heap->cancelled_gc(), "Should be empty when not cancelled"); 401 } 402 403 void ShenandoahConcurrentMark::finish_mark_from_roots(bool full_gc) { 404 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 405 406 uint nworkers = _heap->workers()->active_workers(); 407 408 // Finally mark everything else we've got in our queues during the previous steps. 409 // It does two different things for concurrent vs. mark-compact GC: 410 // - For concurrent GC, it starts with empty task queues, drains the remaining 411 // SATB buffers, and then completes the marking closure. 412 // - For mark-compact GC, it starts out with the task queues seeded by initial 413 // root scan, and completes the closure, thus marking through all live objects 414 // The implementation is the same, so it's shared here. 415 { 416 ShenandoahGCPhase phase(full_gc ? 417 ShenandoahPhaseTimings::full_gc_mark_finish_queues : 418 ShenandoahPhaseTimings::finish_queues); 419 task_queues()->reserve(nworkers); 420 421 shenandoah_assert_rp_isalive_not_installed(); 422 ShenandoahIsAliveSelector is_alive; 423 ReferenceProcessorIsAliveMutator fix_isalive(_heap->ref_processor(), is_alive.is_alive_closure()); 424 425 ShenandoahTerminationTracker termination_tracker(full_gc ? 426 ShenandoahPhaseTimings::full_gc_mark_termination : 427 ShenandoahPhaseTimings::termination); 428 429 StrongRootsScope scope(nworkers); 430 ShenandoahTaskTerminator terminator(nworkers, task_queues()); 431 ShenandoahFinalMarkingTask task(this, &terminator, ShenandoahStringDedup::is_enabled()); 432 _heap->workers()->run_task(&task); 433 } 434 435 assert(task_queues()->is_empty(), "Should be empty"); 436 437 // When we're done marking everything, we process weak references. 438 if (_heap->process_references()) { 439 weak_refs_work(full_gc); 440 } 441 442 weak_roots_work(); 443 444 // And finally finish class unloading 445 if (_heap->unload_classes()) { 446 _heap->unload_classes_and_cleanup_tables(full_gc); 447 } else if (ShenandoahStringDedup::is_enabled()) { 448 ShenandoahIsAliveSelector alive; 449 BoolObjectClosure* is_alive = alive.is_alive_closure(); 450 ShenandoahStringDedup::unlink_or_oops_do(is_alive, NULL, false); 451 } 452 assert(task_queues()->is_empty(), "Should be empty"); 453 TASKQUEUE_STATS_ONLY(task_queues()->print_taskqueue_stats()); 454 TASKQUEUE_STATS_ONLY(task_queues()->reset_taskqueue_stats()); 455 456 // Resize Metaspace 457 MetaspaceGC::compute_new_size(); 458 } 459 460 // Weak Reference Closures 461 class ShenandoahCMDrainMarkingStackClosure: public VoidClosure { 462 uint _worker_id; 463 ShenandoahTaskTerminator* _terminator; 464 bool _reset_terminator; 465 466 public: 467 ShenandoahCMDrainMarkingStackClosure(uint worker_id, ShenandoahTaskTerminator* t, bool reset_terminator = false): 468 _worker_id(worker_id), 469 _terminator(t), 470 _reset_terminator(reset_terminator) { 471 } 472 473 void do_void() { 474 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 475 476 ShenandoahHeap* sh = ShenandoahHeap::heap(); 477 ShenandoahConcurrentMark* scm = sh->concurrent_mark(); 478 assert(sh->process_references(), "why else would we be here?"); 479 ReferenceProcessor* rp = sh->ref_processor(); 480 481 shenandoah_assert_rp_isalive_installed(); 482 483 scm->mark_loop(_worker_id, _terminator, rp, 484 false, // not cancellable 485 false); // do not do strdedup 486 487 if (_reset_terminator) { 488 _terminator->reset_for_reuse(); 489 } 490 } 491 }; 492 493 class ShenandoahCMKeepAliveClosure : public OopClosure { 494 private: 495 ShenandoahObjToScanQueue* _queue; 496 ShenandoahHeap* _heap; 497 ShenandoahMarkingContext* const _mark_context; 498 499 template <class T> 500 inline void do_oop_work(T* p) { 501 ShenandoahConcurrentMark::mark_through_ref<T, NONE, NO_DEDUP>(p, _heap, _queue, _mark_context); 502 } 503 504 public: 505 ShenandoahCMKeepAliveClosure(ShenandoahObjToScanQueue* q) : 506 _queue(q), 507 _heap(ShenandoahHeap::heap()), 508 _mark_context(_heap->marking_context()) {} 509 510 void do_oop(narrowOop* p) { do_oop_work(p); } 511 void do_oop(oop* p) { do_oop_work(p); } 512 }; 513 514 class ShenandoahCMKeepAliveUpdateClosure : public OopClosure { 515 private: 516 ShenandoahObjToScanQueue* _queue; 517 ShenandoahHeap* _heap; 518 ShenandoahMarkingContext* const _mark_context; 519 520 template <class T> 521 inline void do_oop_work(T* p) { 522 ShenandoahConcurrentMark::mark_through_ref<T, SIMPLE, NO_DEDUP>(p, _heap, _queue, _mark_context); 523 } 524 525 public: 526 ShenandoahCMKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) : 527 _queue(q), 528 _heap(ShenandoahHeap::heap()), 529 _mark_context(_heap->marking_context()) {} 530 531 void do_oop(narrowOop* p) { do_oop_work(p); } 532 void do_oop(oop* p) { do_oop_work(p); } 533 }; 534 535 class ShenandoahWeakUpdateClosure : public OopClosure { 536 private: 537 ShenandoahHeap* const _heap; 538 539 template <class T> 540 inline void do_oop_work(T* p) { 541 oop o = _heap->maybe_update_with_forwarded(p); 542 shenandoah_assert_marked_except(p, o, o == NULL); 543 } 544 545 public: 546 ShenandoahWeakUpdateClosure() : _heap(ShenandoahHeap::heap()) {} 547 548 void do_oop(narrowOop* p) { do_oop_work(p); } 549 void do_oop(oop* p) { do_oop_work(p); } 550 }; 551 552 class ShenandoahWeakAssertNotForwardedClosure : public OopClosure { 553 private: 554 template <class T> 555 inline void do_oop_work(T* p) { 556 #ifdef ASSERT 557 T o = RawAccess<>::oop_load(p); 558 if (!CompressedOops::is_null(o)) { 559 oop obj = CompressedOops::decode_not_null(o); 560 shenandoah_assert_not_forwarded(p, obj); 561 } 562 #endif 563 } 564 565 public: 566 ShenandoahWeakAssertNotForwardedClosure() {} 567 568 void do_oop(narrowOop* p) { do_oop_work(p); } 569 void do_oop(oop* p) { do_oop_work(p); } 570 }; 571 572 class ShenandoahRefProcTaskProxy : public AbstractGangTask { 573 private: 574 AbstractRefProcTaskExecutor::ProcessTask& _proc_task; 575 ShenandoahTaskTerminator* _terminator; 576 577 public: 578 ShenandoahRefProcTaskProxy(AbstractRefProcTaskExecutor::ProcessTask& proc_task, 579 ShenandoahTaskTerminator* t) : 580 AbstractGangTask("Process reference objects in parallel"), 581 _proc_task(proc_task), 582 _terminator(t) { 583 } 584 585 void work(uint worker_id) { 586 ResourceMark rm; 587 HandleMark hm; 588 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 589 ShenandoahHeap* heap = ShenandoahHeap::heap(); 590 ShenandoahCMDrainMarkingStackClosure complete_gc(worker_id, _terminator); 591 if (heap->has_forwarded_objects()) { 592 ShenandoahForwardedIsAliveClosure is_alive; 593 ShenandoahCMKeepAliveUpdateClosure keep_alive(heap->concurrent_mark()->get_queue(worker_id)); 594 _proc_task.work(worker_id, is_alive, keep_alive, complete_gc); 595 } else { 596 ShenandoahIsAliveClosure is_alive; 597 ShenandoahCMKeepAliveClosure keep_alive(heap->concurrent_mark()->get_queue(worker_id)); 598 _proc_task.work(worker_id, is_alive, keep_alive, complete_gc); 599 } 600 } 601 }; 602 603 class ShenandoahRefProcTaskExecutor : public AbstractRefProcTaskExecutor { 604 private: 605 WorkGang* _workers; 606 607 public: 608 ShenandoahRefProcTaskExecutor(WorkGang* workers) : 609 _workers(workers) { 610 } 611 612 // Executes a task using worker threads. 613 void execute(ProcessTask& task, uint ergo_workers) { 614 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 615 616 ShenandoahHeap* heap = ShenandoahHeap::heap(); 617 ShenandoahConcurrentMark* cm = heap->concurrent_mark(); 618 ShenandoahPushWorkerQueuesScope scope(_workers, cm->task_queues(), 619 ergo_workers, 620 /* do_check = */ false); 621 uint nworkers = _workers->active_workers(); 622 cm->task_queues()->reserve(nworkers); 623 ShenandoahTaskTerminator terminator(nworkers, cm->task_queues()); 624 ShenandoahRefProcTaskProxy proc_task_proxy(task, &terminator); 625 _workers->run_task(&proc_task_proxy); 626 } 627 }; 628 629 void ShenandoahConcurrentMark::weak_refs_work(bool full_gc) { 630 assert(_heap->process_references(), "sanity"); 631 632 ShenandoahPhaseTimings::Phase phase_root = 633 full_gc ? 634 ShenandoahPhaseTimings::full_gc_weakrefs : 635 ShenandoahPhaseTimings::weakrefs; 636 637 ShenandoahGCPhase phase(phase_root); 638 639 ReferenceProcessor* rp = _heap->ref_processor(); 640 641 // NOTE: We cannot shortcut on has_discovered_references() here, because 642 // we will miss marking JNI Weak refs then, see implementation in 643 // ReferenceProcessor::process_discovered_references. 644 weak_refs_work_doit(full_gc); 645 646 rp->verify_no_references_recorded(); 647 assert(!rp->discovery_enabled(), "Post condition"); 648 649 } 650 651 // Process leftover weak oops: update them, if needed or assert they do not 652 // need updating otherwise. 653 // Weak processor API requires us to visit the oops, even if we are not doing 654 // anything to them. 655 void ShenandoahConcurrentMark::weak_roots_work() { 656 WorkGang* workers = _heap->workers(); 657 OopClosure* keep_alive = &do_nothing_cl; 658 #ifdef ASSERT 659 ShenandoahWeakAssertNotForwardedClosure verify_cl; 660 keep_alive = &verify_cl; 661 #endif 662 ShenandoahIsAliveClosure is_alive; 663 WeakProcessor::weak_oops_do(workers, &is_alive, keep_alive, 1); 664 } 665 666 void ShenandoahConcurrentMark::weak_refs_work_doit(bool full_gc) { 667 ReferenceProcessor* rp = _heap->ref_processor(); 668 669 ShenandoahPhaseTimings::Phase phase_process = 670 full_gc ? 671 ShenandoahPhaseTimings::full_gc_weakrefs_process : 672 ShenandoahPhaseTimings::weakrefs_process; 673 674 ShenandoahPhaseTimings::Phase phase_process_termination = 675 full_gc ? 676 ShenandoahPhaseTimings::full_gc_weakrefs_termination : 677 ShenandoahPhaseTimings::weakrefs_termination; 678 679 shenandoah_assert_rp_isalive_not_installed(); 680 ShenandoahIsAliveSelector is_alive; 681 ReferenceProcessorIsAliveMutator fix_isalive(rp, is_alive.is_alive_closure()); 682 683 WorkGang* workers = _heap->workers(); 684 uint nworkers = workers->active_workers(); 685 686 rp->setup_policy(_heap->soft_ref_policy()->should_clear_all_soft_refs()); 687 rp->set_active_mt_degree(nworkers); 688 689 assert(task_queues()->is_empty(), "Should be empty"); 690 691 // complete_gc and keep_alive closures instantiated here are only needed for 692 // single-threaded path in RP. They share the queue 0 for tracking work, which 693 // simplifies implementation. Since RP may decide to call complete_gc several 694 // times, we need to be able to reuse the terminator. 695 uint serial_worker_id = 0; 696 ShenandoahTaskTerminator terminator(1, task_queues()); 697 ShenandoahCMDrainMarkingStackClosure complete_gc(serial_worker_id, &terminator, /* reset_terminator = */ true); 698 699 ShenandoahRefProcTaskExecutor executor(workers); 700 701 ReferenceProcessorPhaseTimes pt(_heap->gc_timer(), rp->num_queues()); 702 703 { 704 ShenandoahGCPhase phase(phase_process); 705 ShenandoahTerminationTracker phase_term(phase_process_termination); 706 707 if (_heap->has_forwarded_objects()) { 708 ShenandoahCMKeepAliveUpdateClosure keep_alive(get_queue(serial_worker_id)); 709 rp->process_discovered_references(is_alive.is_alive_closure(), &keep_alive, 710 &complete_gc, &executor, 711 &pt); 712 713 } else { 714 ShenandoahCMKeepAliveClosure keep_alive(get_queue(serial_worker_id)); 715 rp->process_discovered_references(is_alive.is_alive_closure(), &keep_alive, 716 &complete_gc, &executor, 717 &pt); 718 719 } 720 721 pt.print_all_references(); 722 723 assert(task_queues()->is_empty(), "Should be empty"); 724 } 725 } 726 727 class ShenandoahCancelledGCYieldClosure : public YieldClosure { 728 private: 729 ShenandoahHeap* const _heap; 730 public: 731 ShenandoahCancelledGCYieldClosure() : _heap(ShenandoahHeap::heap()) {}; 732 virtual bool should_return() { return _heap->cancelled_gc(); } 733 }; 734 735 class ShenandoahPrecleanCompleteGCClosure : public VoidClosure { 736 public: 737 void do_void() { 738 ShenandoahHeap* sh = ShenandoahHeap::heap(); 739 ShenandoahConcurrentMark* scm = sh->concurrent_mark(); 740 assert(sh->process_references(), "why else would we be here?"); 741 ShenandoahTaskTerminator terminator(1, scm->task_queues()); 742 743 ReferenceProcessor* rp = sh->ref_processor(); 744 shenandoah_assert_rp_isalive_installed(); 745 746 scm->mark_loop(0, &terminator, rp, 747 false, // not cancellable 748 false); // do not do strdedup 749 } 750 }; 751 752 class ShenandoahPrecleanKeepAliveUpdateClosure : public OopClosure { 753 private: 754 ShenandoahObjToScanQueue* _queue; 755 ShenandoahHeap* _heap; 756 ShenandoahMarkingContext* const _mark_context; 757 758 template <class T> 759 inline void do_oop_work(T* p) { 760 ShenandoahConcurrentMark::mark_through_ref<T, CONCURRENT, NO_DEDUP>(p, _heap, _queue, _mark_context); 761 } 762 763 public: 764 ShenandoahPrecleanKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) : 765 _queue(q), 766 _heap(ShenandoahHeap::heap()), 767 _mark_context(_heap->marking_context()) {} 768 769 void do_oop(narrowOop* p) { do_oop_work(p); } 770 void do_oop(oop* p) { do_oop_work(p); } 771 }; 772 773 class ShenandoahPrecleanTask : public AbstractGangTask { 774 private: 775 ReferenceProcessor* _rp; 776 777 public: 778 ShenandoahPrecleanTask(ReferenceProcessor* rp) : 779 AbstractGangTask("Precleaning task"), 780 _rp(rp) {} 781 782 void work(uint worker_id) { 783 assert(worker_id == 0, "The code below is single-threaded, only one worker is expected"); 784 ShenandoahParallelWorkerSession worker_session(worker_id); 785 786 ShenandoahHeap* sh = ShenandoahHeap::heap(); 787 788 ShenandoahObjToScanQueue* q = sh->concurrent_mark()->get_queue(worker_id); 789 790 ShenandoahCancelledGCYieldClosure yield; 791 ShenandoahPrecleanCompleteGCClosure complete_gc; 792 793 if (sh->has_forwarded_objects()) { 794 ShenandoahForwardedIsAliveClosure is_alive; 795 ShenandoahPrecleanKeepAliveUpdateClosure keep_alive(q); 796 ResourceMark rm; 797 _rp->preclean_discovered_references(&is_alive, &keep_alive, 798 &complete_gc, &yield, 799 NULL); 800 } else { 801 ShenandoahIsAliveClosure is_alive; 802 ShenandoahCMKeepAliveClosure keep_alive(q); 803 ResourceMark rm; 804 _rp->preclean_discovered_references(&is_alive, &keep_alive, 805 &complete_gc, &yield, 806 NULL); 807 } 808 } 809 }; 810 811 void ShenandoahConcurrentMark::preclean_weak_refs() { 812 // Pre-cleaning weak references before diving into STW makes sense at the 813 // end of concurrent mark. This will filter out the references which referents 814 // are alive. Note that ReferenceProcessor already filters out these on reference 815 // discovery, and the bulk of work is done here. This phase processes leftovers 816 // that missed the initial filtering, i.e. when referent was marked alive after 817 // reference was discovered by RP. 818 819 assert(_heap->process_references(), "sanity"); 820 821 // Shortcut if no references were discovered to avoid winding up threads. 822 ReferenceProcessor* rp = _heap->ref_processor(); 823 if (!rp->has_discovered_references()) { 824 return; 825 } 826 827 assert(task_queues()->is_empty(), "Should be empty"); 828 829 ReferenceProcessorMTDiscoveryMutator fix_mt_discovery(rp, false); 830 831 shenandoah_assert_rp_isalive_not_installed(); 832 ShenandoahIsAliveSelector is_alive; 833 ReferenceProcessorIsAliveMutator fix_isalive(rp, is_alive.is_alive_closure()); 834 835 // Execute precleaning in the worker thread: it will give us GCLABs, String dedup 836 // queues and other goodies. When upstream ReferenceProcessor starts supporting 837 // parallel precleans, we can extend this to more threads. 838 WorkGang* workers = _heap->workers(); 839 uint nworkers = workers->active_workers(); 840 assert(nworkers == 1, "This code uses only a single worker"); 841 task_queues()->reserve(nworkers); 842 843 ShenandoahPrecleanTask task(rp); 844 workers->run_task(&task); 845 846 assert(task_queues()->is_empty(), "Should be empty"); 847 } 848 849 void ShenandoahConcurrentMark::cancel() { 850 // Clean up marking stacks. 851 ShenandoahObjToScanQueueSet* queues = task_queues(); 852 queues->clear(); 853 854 // Cancel SATB buffers. 855 ShenandoahBarrierSet::satb_mark_queue_set().abandon_partial_marking(); 856 } 857 858 ShenandoahObjToScanQueue* ShenandoahConcurrentMark::get_queue(uint worker_id) { 859 assert(task_queues()->get_reserved() > worker_id, "No reserved queue for worker id: %d", worker_id); 860 return _task_queues->queue(worker_id); 861 } 862 863 template <bool CANCELLABLE> 864 void ShenandoahConcurrentMark::mark_loop_prework(uint w, ShenandoahTaskTerminator *t, ReferenceProcessor *rp, 865 bool strdedup) { 866 ShenandoahObjToScanQueue* q = get_queue(w); 867 868 jushort* ld = _heap->get_liveness_cache(w); 869 870 // TODO: We can clean up this if we figure out how to do templated oop closures that 871 // play nice with specialized_oop_iterators. 872 if (_heap->unload_classes()) { 873 if (_heap->has_forwarded_objects()) { 874 if (strdedup) { 875 ShenandoahMarkUpdateRefsMetadataDedupClosure cl(q, rp); 876 mark_loop_work<ShenandoahMarkUpdateRefsMetadataDedupClosure, CANCELLABLE>(&cl, ld, w, t); 877 } else { 878 ShenandoahMarkUpdateRefsMetadataClosure cl(q, rp); 879 mark_loop_work<ShenandoahMarkUpdateRefsMetadataClosure, CANCELLABLE>(&cl, ld, w, t); 880 } 881 } else { 882 if (strdedup) { 883 ShenandoahMarkRefsMetadataDedupClosure cl(q, rp); 884 mark_loop_work<ShenandoahMarkRefsMetadataDedupClosure, CANCELLABLE>(&cl, ld, w, t); 885 } else { 886 ShenandoahMarkRefsMetadataClosure cl(q, rp); 887 mark_loop_work<ShenandoahMarkRefsMetadataClosure, CANCELLABLE>(&cl, ld, w, t); 888 } 889 } 890 } else { 891 if (_heap->has_forwarded_objects()) { 892 if (strdedup) { 893 ShenandoahMarkUpdateRefsDedupClosure cl(q, rp); 894 mark_loop_work<ShenandoahMarkUpdateRefsDedupClosure, CANCELLABLE>(&cl, ld, w, t); 895 } else { 896 ShenandoahMarkUpdateRefsClosure cl(q, rp); 897 mark_loop_work<ShenandoahMarkUpdateRefsClosure, CANCELLABLE>(&cl, ld, w, t); 898 } 899 } else { 900 if (strdedup) { 901 ShenandoahMarkRefsDedupClosure cl(q, rp); 902 mark_loop_work<ShenandoahMarkRefsDedupClosure, CANCELLABLE>(&cl, ld, w, t); 903 } else { 904 ShenandoahMarkRefsClosure cl(q, rp); 905 mark_loop_work<ShenandoahMarkRefsClosure, CANCELLABLE>(&cl, ld, w, t); 906 } 907 } 908 } 909 910 _heap->flush_liveness_cache(w); 911 } 912 913 template <class T, bool CANCELLABLE> 914 void ShenandoahConcurrentMark::mark_loop_work(T* cl, jushort* live_data, uint worker_id, ShenandoahTaskTerminator *terminator) { 915 uintx stride = ShenandoahMarkLoopStride; 916 917 ShenandoahHeap* heap = ShenandoahHeap::heap(); 918 ShenandoahObjToScanQueueSet* queues = task_queues(); 919 ShenandoahObjToScanQueue* q; 920 ShenandoahMarkTask t; 921 922 /* 923 * Process outstanding queues, if any. 924 * 925 * There can be more queues than workers. To deal with the imbalance, we claim 926 * extra queues first. Since marking can push new tasks into the queue associated 927 * with this worker id, we come back to process this queue in the normal loop. 928 */ 929 assert(queues->get_reserved() == heap->workers()->active_workers(), 930 "Need to reserve proper number of queues: reserved: %u, active: %u", queues->get_reserved(), heap->workers()->active_workers()); 931 932 q = queues->claim_next(); 933 while (q != NULL) { 934 if (CANCELLABLE && heap->check_cancelled_gc_and_yield()) { 935 return; 936 } 937 938 for (uint i = 0; i < stride; i++) { 939 if (q->pop(t)) { 940 do_task<T>(q, cl, live_data, &t); 941 } else { 942 assert(q->is_empty(), "Must be empty"); 943 q = queues->claim_next(); 944 break; 945 } 946 } 947 } 948 q = get_queue(worker_id); 949 950 ShenandoahSATBBufferClosure drain_satb(q); 951 SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set(); 952 953 /* 954 * Normal marking loop: 955 */ 956 while (true) { 957 if (CANCELLABLE && heap->check_cancelled_gc_and_yield()) { 958 return; 959 } 960 961 while (satb_mq_set.completed_buffers_num() > 0) { 962 satb_mq_set.apply_closure_to_completed_buffer(&drain_satb); 963 } 964 965 uint work = 0; 966 for (uint i = 0; i < stride; i++) { 967 if (q->pop(t) || 968 queues->steal(worker_id, t)) { 969 do_task<T>(q, cl, live_data, &t); 970 work++; 971 } else { 972 break; 973 } 974 } 975 976 if (work == 0) { 977 // No work encountered in current stride, try to terminate. 978 // Need to leave the STS here otherwise it might block safepoints. 979 ShenandoahSuspendibleThreadSetLeaver stsl(CANCELLABLE && ShenandoahSuspendibleWorkers); 980 ShenandoahTerminationTimingsTracker term_tracker(worker_id); 981 ShenandoahTerminatorTerminator tt(heap); 982 if (terminator->offer_termination(&tt)) return; 983 } 984 } 985 } 986 987 bool ShenandoahConcurrentMark::claim_codecache() { 988 assert(ShenandoahConcurrentScanCodeRoots, "must not be called otherwise"); 989 return _claimed_codecache.try_set(); 990 } 991 992 void ShenandoahConcurrentMark::clear_claim_codecache() { 993 assert(ShenandoahConcurrentScanCodeRoots, "must not be called otherwise"); 994 _claimed_codecache.unset(); 995 }