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