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