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