1 /* 2 * Copyright (c) 2013, 2017, Red Hat, Inc. and/or its affiliates. 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 #include "classfile/stringTable.hpp" 26 #include "gc/shared/gcTimer.hpp" 27 #include "gc/shared/parallelCleaning.hpp" 28 #include "gc/shared/referenceProcessor.hpp" 29 #include "gc/shared/strongRootsScope.hpp" 30 #include "gc/shared/suspendibleThreadSet.hpp" 31 #include "gc/shenandoah/brooksPointer.hpp" 32 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp" 33 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 34 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp" 35 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp" 36 #include "gc/shenandoah/shenandoahMarkCompact.hpp" 37 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 38 #include "gc/shenandoah/shenandoahRootProcessor.hpp" 39 #include "gc/shenandoah/shenandoah_specialized_oop_closures.hpp" 40 #include "gc/shenandoah/shenandoahTaskqueue.hpp" 41 #include "gc/shenandoah/shenandoahUtils.hpp" 42 #include "gc/shared/weakProcessor.hpp" 43 #include "code/codeCache.hpp" 44 #include "classfile/symbolTable.hpp" 45 #include "classfile/systemDictionary.hpp" 46 #include "memory/iterator.inline.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "gc/shared/taskqueue.inline.hpp" 49 #include "logging/logStream.hpp" 50 51 template<UpdateRefsMode UPDATE_REFS> 52 class ShenandoahInitMarkRootsClosure : public OopClosure { 53 private: 54 ShenandoahObjToScanQueue* _queue; 55 ShenandoahHeap* _heap; 56 57 template <class T> 58 inline void do_oop_nv(T* p) { 59 ShenandoahConcurrentMark::mark_through_ref<T, UPDATE_REFS, false /* string dedup */>(p, _heap, _queue); 60 } 61 62 public: 63 ShenandoahInitMarkRootsClosure(ShenandoahObjToScanQueue* q) : 64 _queue(q), _heap(ShenandoahHeap::heap()) {}; 65 66 void do_oop(narrowOop* p) { do_oop_nv(p); } 67 void do_oop(oop* p) { do_oop_nv(p); } 68 }; 69 70 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ReferenceProcessor* rp) : 71 MetadataAwareOopClosure(rp), 72 _queue(q), 73 _dedup_queue(NULL), 74 _heap(ShenandoahHeap::heap()) 75 { } 76 77 78 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahStrDedupQueue* dq, ReferenceProcessor* rp) : 79 MetadataAwareOopClosure(rp), 80 _queue(q), 81 _dedup_queue(dq), 82 _heap(ShenandoahHeap::heap()) 83 { } 84 85 86 template<UpdateRefsMode UPDATE_REFS> 87 class ShenandoahInitMarkRootsTask : public AbstractGangTask { 88 private: 89 ShenandoahRootProcessor* _rp; 90 bool _process_refs; 91 public: 92 ShenandoahInitMarkRootsTask(ShenandoahRootProcessor* rp, bool process_refs) : 93 AbstractGangTask("Shenandoah init mark roots task"), 94 _rp(rp), 95 _process_refs(process_refs) { 96 } 97 98 void work(uint worker_id) { 99 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 100 101 ShenandoahHeap* heap = ShenandoahHeap::heap(); 102 ShenandoahObjToScanQueueSet* queues = heap->concurrentMark()->task_queues(); 103 assert(queues->get_reserved() > worker_id, "Queue has not been reserved for worker id: %d", worker_id); 104 105 ShenandoahObjToScanQueue* q = queues->queue(worker_id); 106 ShenandoahInitMarkRootsClosure<UPDATE_REFS> mark_cl(q); 107 CLDToOopClosure cldCl(&mark_cl); 108 MarkingCodeBlobClosure blobsCl(&mark_cl, ! CodeBlobToOopClosure::FixRelocations); 109 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 // c. With ShenandoahConcurrentScanCodeRoots, we avoid scanning the entire code cache here, 120 // and instead do that in concurrent phase under the relevant lock. This saves init mark 121 // pause time. 122 123 ResourceMark m; 124 if (heap->concurrentMark()->unload_classes()) { 125 _rp->process_strong_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, NULL, &blobsCl, NULL, worker_id); 126 } else { 127 if (ShenandoahConcurrentScanCodeRoots) { 128 CodeBlobClosure* code_blobs = NULL; 129 #ifdef ASSERT 130 ShenandoahAssertToSpaceClosure assert_to_space_oops; 131 CodeBlobToOopClosure assert_to_space(&assert_to_space_oops, !CodeBlobToOopClosure::FixRelocations); 132 // If conc code cache evac is disabled, code cache should have only to-space ptrs. 133 // Otherwise, it should have to-space ptrs only if mark does not update refs. 134 if (!ShenandoahConcurrentEvacCodeRoots && !heap->has_forwarded_objects()) { 135 code_blobs = &assert_to_space; 136 } 137 #endif 138 _rp->process_all_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, code_blobs, NULL, worker_id); 139 } else { 140 _rp->process_all_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, &blobsCl, NULL, worker_id); 141 } 142 } 143 } 144 }; 145 146 class ShenandoahUpdateRootsTask : public AbstractGangTask { 147 private: 148 ShenandoahRootProcessor* _rp; 149 const bool _update_code_cache; 150 public: 151 ShenandoahUpdateRootsTask(ShenandoahRootProcessor* rp, bool update_code_cache) : 152 AbstractGangTask("Shenandoah update roots task"), 153 _rp(rp), 154 _update_code_cache(update_code_cache) { 155 } 156 157 void work(uint worker_id) { 158 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 159 160 ShenandoahHeap* heap = ShenandoahHeap::heap(); 161 ShenandoahUpdateRefsClosure cl; 162 CLDToOopClosure cldCl(&cl); 163 164 CodeBlobClosure* code_blobs; 165 CodeBlobToOopClosure update_blobs(&cl, CodeBlobToOopClosure::FixRelocations); 166 #ifdef ASSERT 167 ShenandoahAssertToSpaceClosure assert_to_space_oops; 168 CodeBlobToOopClosure assert_to_space(&assert_to_space_oops, !CodeBlobToOopClosure::FixRelocations); 169 #endif 170 if (_update_code_cache) { 171 code_blobs = &update_blobs; 172 } else { 173 code_blobs = 174 DEBUG_ONLY(&assert_to_space) 175 NOT_DEBUG(NULL); 176 } 177 _rp->process_all_roots(&cl, &cl, &cldCl, code_blobs, NULL, worker_id); 178 } 179 }; 180 181 class ShenandoahConcurrentMarkingTask : public AbstractGangTask { 182 private: 183 ShenandoahConcurrentMark* _cm; 184 ParallelTaskTerminator* _terminator; 185 bool _update_refs; 186 187 public: 188 ShenandoahConcurrentMarkingTask(ShenandoahConcurrentMark* cm, ParallelTaskTerminator* terminator, bool update_refs) : 189 AbstractGangTask("Root Region Scan"), _cm(cm), _terminator(terminator), _update_refs(update_refs) { 190 } 191 192 193 void work(uint worker_id) { 194 SuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers); 195 ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id); 196 jushort* live_data = _cm->get_liveness(worker_id); 197 ReferenceProcessor* rp; 198 if (_cm->process_references()) { 199 rp = ShenandoahHeap::heap()->ref_processor(); 200 shenandoah_assert_rp_isalive_installed(); 201 } else { 202 rp = NULL; 203 } 204 205 _cm->concurrent_scan_code_roots(worker_id, rp, _update_refs); 206 _cm->mark_loop(worker_id, _terminator, rp, 207 true, // cancellable 208 true, // drain SATBs as we go 209 _cm->unload_classes(), 210 _update_refs, 211 ShenandoahStringDedup::is_enabled()); // perform string dedup 212 } 213 }; 214 215 class ShenandoahFinalMarkingTask : public AbstractGangTask { 216 private: 217 ShenandoahConcurrentMark* _cm; 218 ParallelTaskTerminator* _terminator; 219 bool _update_refs; 220 bool _unload_classes; 221 bool _dedup_string; 222 223 public: 224 ShenandoahFinalMarkingTask(ShenandoahConcurrentMark* cm, ParallelTaskTerminator* terminator, 225 bool update_refs, bool unload_classes, bool dedup_string) : 226 AbstractGangTask("Shenandoah Final Marking"), _cm(cm), _terminator(terminator), 227 _update_refs(update_refs), _unload_classes(unload_classes), _dedup_string(dedup_string) { 228 } 229 230 void work(uint worker_id) { 231 // First drain remaining SATB buffers. 232 // Notice that this is not strictly necessary for mark-compact. But since 233 // it requires a StrongRootsScope around the task, we need to claim the 234 // threads, and performance-wise it doesn't really matter. Adds about 1ms to 235 // full-gc. 236 _cm->drain_satb_buffers(worker_id, true); 237 238 ReferenceProcessor* rp; 239 if (_cm->process_references()) { 240 rp = ShenandoahHeap::heap()->ref_processor(); 241 shenandoah_assert_rp_isalive_installed(); 242 } else { 243 rp = NULL; 244 } 245 246 // Degenerated cycle may bypass concurrent cycle, so code roots might not be scanned, 247 // let's check here. 248 _cm->concurrent_scan_code_roots(worker_id, rp, _update_refs); 249 _cm->mark_loop(worker_id, _terminator, rp, 250 false, // not cancellable 251 false, // do not drain SATBs, already drained 252 _unload_classes, 253 _update_refs, 254 _dedup_string); 255 256 assert(_cm->task_queues()->is_empty(), "Should be empty"); 257 } 258 }; 259 260 void ShenandoahConcurrentMark::mark_roots(ShenandoahPhaseTimings::Phase root_phase) { 261 assert(Thread::current()->is_VM_thread(), "can only do this in VMThread"); 262 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 263 264 ShenandoahHeap* heap = ShenandoahHeap::heap(); 265 266 ShenandoahGCPhase phase(root_phase); 267 268 WorkGang* workers = heap->workers(); 269 uint nworkers = workers->active_workers(); 270 271 assert(nworkers <= task_queues()->size(), "Just check"); 272 273 ShenandoahRootProcessor root_proc(heap, nworkers, root_phase); 274 TASKQUEUE_STATS_ONLY(reset_taskqueue_stats()); 275 task_queues()->reserve(nworkers); 276 277 if (heap->has_forwarded_objects()) { 278 ShenandoahInitMarkRootsTask<RESOLVE> mark_roots(&root_proc, process_references()); 279 workers->run_task(&mark_roots); 280 } else { 281 // No need to update references, which means the heap is stable. 282 // Can save time not walking through forwarding pointers. 283 ShenandoahInitMarkRootsTask<NONE> mark_roots(&root_proc, process_references()); 284 workers->run_task(&mark_roots); 285 } 286 287 if (ShenandoahConcurrentScanCodeRoots) { 288 clear_claim_codecache(); 289 } 290 } 291 292 void ShenandoahConcurrentMark::init_mark_roots() { 293 assert(Thread::current()->is_VM_thread(), "can only do this in VMThread"); 294 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 295 296 mark_roots(ShenandoahPhaseTimings::scan_roots); 297 } 298 299 void ShenandoahConcurrentMark::update_roots(ShenandoahPhaseTimings::Phase root_phase) { 300 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 301 302 bool update_code_cache = true; // initialize to safer value 303 switch (root_phase) { 304 case ShenandoahPhaseTimings::update_roots: 305 case ShenandoahPhaseTimings::final_update_refs_roots: 306 // If code cache was evacuated concurrently, we need to update code cache roots. 307 update_code_cache = ShenandoahConcurrentEvacCodeRoots; 308 break; 309 case ShenandoahPhaseTimings::full_gc_roots: 310 update_code_cache = true; 311 break; 312 default: 313 ShouldNotReachHere(); 314 } 315 316 ShenandoahHeap* heap = ShenandoahHeap::heap(); 317 318 ShenandoahGCPhase phase(root_phase); 319 320 #if defined(COMPILER2) || INCLUDE_JVMCI 321 DerivedPointerTable::clear(); 322 #endif 323 324 uint nworkers = heap->workers()->active_workers(); 325 326 ShenandoahRootProcessor root_proc(heap, nworkers, root_phase); 327 ShenandoahUpdateRootsTask update_roots(&root_proc, update_code_cache); 328 heap->workers()->run_task(&update_roots); 329 330 #if defined(COMPILER2) || INCLUDE_JVMCI 331 DerivedPointerTable::update_pointers(); 332 #endif 333 } 334 335 void ShenandoahConcurrentMark::initialize(uint workers) { 336 _heap = ShenandoahHeap::heap(); 337 338 uint num_queues = MAX2(workers, 1U); 339 340 _task_queues = new ShenandoahObjToScanQueueSet((int) num_queues); 341 342 for (uint i = 0; i < num_queues; ++i) { 343 ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue(); 344 task_queue->initialize(); 345 _task_queues->register_queue(i, task_queue); 346 } 347 348 ShenandoahBarrierSet::satb_mark_queue_set().set_buffer_size(ShenandoahSATBBufferSize); 349 350 size_t num_regions = ShenandoahHeap::heap()->num_regions(); 351 _liveness_local = NEW_C_HEAP_ARRAY(jushort*, workers, mtGC); 352 for (uint worker = 0; worker < workers; worker++) { 353 _liveness_local[worker] = NEW_C_HEAP_ARRAY(jushort, num_regions, mtGC); 354 } 355 } 356 357 void ShenandoahConcurrentMark::concurrent_scan_code_roots(uint worker_id, ReferenceProcessor* rp, bool update_refs) { 358 if (ShenandoahConcurrentScanCodeRoots && claim_codecache()) { 359 ShenandoahObjToScanQueue* q = task_queues()->queue(worker_id); 360 if (!unload_classes()) { 361 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 362 if (update_refs) { 363 ShenandoahMarkResolveRefsClosure cl(q, rp); 364 CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations); 365 CodeCache::blobs_do(&blobs); 366 } else { 367 ShenandoahMarkRefsClosure cl(q, rp); 368 CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations); 369 CodeCache::blobs_do(&blobs); 370 } 371 } 372 } 373 } 374 375 void ShenandoahConcurrentMark::mark_from_roots() { 376 ShenandoahHeap* sh = ShenandoahHeap::heap(); 377 WorkGang* workers = sh->workers(); 378 uint nworkers = workers->active_workers(); 379 380 bool update_refs = sh->has_forwarded_objects(); 381 382 ShenandoahGCPhase conc_mark_phase(ShenandoahPhaseTimings::conc_mark); 383 384 if (process_references()) { 385 ReferenceProcessor* rp = sh->ref_processor(); 386 rp->set_active_mt_degree(nworkers); 387 388 // enable ("weak") refs discovery 389 rp->enable_discovery(true /*verify_no_refs*/); 390 rp->setup_policy(sh->is_full_gc_in_progress()); // snapshot the soft ref policy to be used in this cycle 391 } 392 393 shenandoah_assert_rp_isalive_not_installed(); 394 ReferenceProcessorIsAliveMutator fix_isalive(sh->ref_processor(), sh->is_alive_closure()); 395 396 task_queues()->reserve(nworkers); 397 398 if (UseShenandoahOWST) { 399 ShenandoahTaskTerminator terminator(nworkers, task_queues()); 400 ShenandoahConcurrentMarkingTask markingTask = ShenandoahConcurrentMarkingTask(this, &terminator, update_refs); 401 workers->run_task(&markingTask); 402 } else { 403 ParallelTaskTerminator terminator(nworkers, task_queues()); 404 ShenandoahConcurrentMarkingTask markingTask = ShenandoahConcurrentMarkingTask(this, &terminator, update_refs); 405 workers->run_task(&markingTask); 406 } 407 408 assert(task_queues()->is_empty() || sh->cancelled_gc(), "Should be empty when not cancelled"); 409 } 410 411 void ShenandoahConcurrentMark::finish_mark_from_roots() { 412 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 413 414 ShenandoahHeap* sh = ShenandoahHeap::heap(); 415 416 shared_finish_mark_from_roots(/* full_gc = */ false); 417 418 if (sh->has_forwarded_objects()) { 419 update_roots(ShenandoahPhaseTimings::update_roots); 420 } 421 422 TASKQUEUE_STATS_ONLY(print_taskqueue_stats()); 423 TASKQUEUE_STATS_ONLY(reset_taskqueue_stats()); 424 } 425 426 void ShenandoahConcurrentMark::shared_finish_mark_from_roots(bool full_gc) { 427 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 428 429 ShenandoahHeap* sh = ShenandoahHeap::heap(); 430 431 uint nworkers = sh->workers()->active_workers(); 432 433 // Finally mark everything else we've got in our queues during the previous steps. 434 // It does two different things for concurrent vs. mark-compact GC: 435 // - For concurrent GC, it starts with empty task queues, drains the remaining 436 // SATB buffers, and then completes the marking closure. 437 // - For mark-compact GC, it starts out with the task queues seeded by initial 438 // root scan, and completes the closure, thus marking through all live objects 439 // The implementation is the same, so it's shared here. 440 { 441 ShenandoahGCPhase phase(full_gc ? 442 ShenandoahPhaseTimings::full_gc_mark_finish_queues : 443 ShenandoahPhaseTimings::finish_queues); 444 task_queues()->reserve(nworkers); 445 446 shenandoah_assert_rp_isalive_not_installed(); 447 ReferenceProcessorIsAliveMutator fix_isalive(sh->ref_processor(), sh->is_alive_closure()); 448 449 StrongRootsScope scope(nworkers); 450 if (UseShenandoahOWST) { 451 ShenandoahTaskTerminator terminator(nworkers, task_queues()); 452 ShenandoahFinalMarkingTask task(this, &terminator, sh->has_forwarded_objects(), 453 unload_classes(), full_gc && ShenandoahStringDedup::is_enabled()); 454 sh->workers()->run_task(&task); 455 } else { 456 ParallelTaskTerminator terminator(nworkers, task_queues()); 457 ShenandoahFinalMarkingTask task(this, &terminator, sh->has_forwarded_objects(), 458 unload_classes(), full_gc && ShenandoahStringDedup::is_enabled()); 459 sh->workers()->run_task(&task); 460 } 461 } 462 463 assert(task_queues()->is_empty(), "Should be empty"); 464 465 // When we're done marking everything, we process weak references. 466 if (process_references()) { 467 weak_refs_work(full_gc); 468 } 469 470 // And finally finish class unloading 471 if (unload_classes()) { 472 sh->unload_classes_and_cleanup_tables(full_gc); 473 } 474 475 assert(task_queues()->is_empty(), "Should be empty"); 476 477 } 478 479 class ShenandoahSATBThreadsClosure : public ThreadClosure { 480 ShenandoahSATBBufferClosure* _satb_cl; 481 int _thread_parity; 482 483 public: 484 ShenandoahSATBThreadsClosure(ShenandoahSATBBufferClosure* satb_cl) : 485 _satb_cl(satb_cl), 486 _thread_parity(Threads::thread_claim_parity()) {} 487 488 void do_thread(Thread* thread) { 489 if (thread->is_Java_thread()) { 490 if (thread->claim_oops_do(true, _thread_parity)) { 491 JavaThread* jt = (JavaThread*)thread; 492 ShenandoahThreadLocalData::satb_mark_queue(jt).apply_closure_and_empty(_satb_cl); 493 } 494 } else if (thread->is_VM_thread()) { 495 if (thread->claim_oops_do(true, _thread_parity)) { 496 ShenandoahBarrierSet::satb_mark_queue_set().shared_satb_queue()->apply_closure_and_empty(_satb_cl); 497 } 498 } 499 } 500 }; 501 502 void ShenandoahConcurrentMark::drain_satb_buffers(uint worker_id, bool remark) { 503 ShenandoahObjToScanQueue* q = get_queue(worker_id); 504 ShenandoahSATBBufferClosure cl(q); 505 506 SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set(); 507 while (satb_mq_set.apply_closure_to_completed_buffer(&cl)); 508 509 if (remark) { 510 ShenandoahSATBThreadsClosure tc(&cl); 511 Threads::threads_do(&tc); 512 } 513 } 514 515 #if TASKQUEUE_STATS 516 void ShenandoahConcurrentMark::print_taskqueue_stats_hdr(outputStream* const st) { 517 st->print_raw_cr("GC Task Stats"); 518 st->print_raw("thr "); TaskQueueStats::print_header(1, st); st->cr(); 519 st->print_raw("--- "); TaskQueueStats::print_header(2, st); st->cr(); 520 } 521 522 void ShenandoahConcurrentMark::print_taskqueue_stats() const { 523 if (!log_develop_is_enabled(Trace, gc, task, stats)) { 524 return; 525 } 526 Log(gc, task, stats) log; 527 ResourceMark rm; 528 LogStream ls(log.trace()); 529 outputStream* st = &ls; 530 print_taskqueue_stats_hdr(st); 531 532 TaskQueueStats totals; 533 const uint n = _task_queues->size(); 534 for (uint i = 0; i < n; ++i) { 535 st->print(UINT32_FORMAT_W(3), i); 536 _task_queues->queue(i)->stats.print(st); 537 st->cr(); 538 totals += _task_queues->queue(i)->stats; 539 } 540 st->print("tot "); totals.print(st); st->cr(); 541 DEBUG_ONLY(totals.verify()); 542 543 } 544 545 void ShenandoahConcurrentMark::reset_taskqueue_stats() { 546 const uint n = task_queues()->size(); 547 for (uint i = 0; i < n; ++i) { 548 task_queues()->queue(i)->stats.reset(); 549 } 550 } 551 #endif // TASKQUEUE_STATS 552 553 // Weak Reference Closures 554 class ShenandoahCMDrainMarkingStackClosure: public VoidClosure { 555 uint _worker_id; 556 ParallelTaskTerminator* _terminator; 557 bool _reset_terminator; 558 559 public: 560 ShenandoahCMDrainMarkingStackClosure(uint worker_id, ParallelTaskTerminator* t, bool reset_terminator = false): 561 _worker_id(worker_id), 562 _terminator(t), 563 _reset_terminator(reset_terminator) { 564 } 565 566 void do_void() { 567 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 568 569 ShenandoahHeap* sh = ShenandoahHeap::heap(); 570 ShenandoahConcurrentMark* scm = sh->concurrentMark(); 571 assert(scm->process_references(), "why else would we be here?"); 572 ReferenceProcessor* rp = sh->ref_processor(); 573 574 shenandoah_assert_rp_isalive_installed(); 575 576 scm->mark_loop(_worker_id, _terminator, rp, 577 false, // not cancellable 578 false, // do not drain SATBs 579 scm->unload_classes(), 580 sh->has_forwarded_objects(), 581 false); // do not do strdedup 582 583 if (_reset_terminator) { 584 _terminator->reset_for_reuse(); 585 } 586 } 587 }; 588 589 590 class ShenandoahCMKeepAliveClosure : public OopClosure { 591 private: 592 ShenandoahObjToScanQueue* _queue; 593 ShenandoahHeap* _heap; 594 595 template <class T> 596 inline void do_oop_nv(T* p) { 597 ShenandoahConcurrentMark::mark_through_ref<T, NONE, false /* string dedup */>(p, _heap, _queue); 598 } 599 600 public: 601 ShenandoahCMKeepAliveClosure(ShenandoahObjToScanQueue* q) : 602 _queue(q), _heap(ShenandoahHeap::heap()) {} 603 604 void do_oop(narrowOop* p) { do_oop_nv(p); } 605 void do_oop(oop* p) { do_oop_nv(p); } 606 }; 607 608 class ShenandoahCMKeepAliveUpdateClosure : public OopClosure { 609 private: 610 ShenandoahObjToScanQueue* _queue; 611 ShenandoahHeap* _heap; 612 613 template <class T> 614 inline void do_oop_nv(T* p) { 615 ShenandoahConcurrentMark::mark_through_ref<T, SIMPLE, false /* string dedup */>(p, _heap, _queue); 616 } 617 618 public: 619 ShenandoahCMKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) : 620 _queue(q), _heap(ShenandoahHeap::heap()) {} 621 622 void do_oop(narrowOop* p) { do_oop_nv(p); } 623 void do_oop(oop* p) { do_oop_nv(p); } 624 }; 625 626 class ShenandoahRefProcTaskProxy : public AbstractGangTask { 627 628 private: 629 AbstractRefProcTaskExecutor::ProcessTask& _proc_task; 630 ParallelTaskTerminator* _terminator; 631 public: 632 633 ShenandoahRefProcTaskProxy(AbstractRefProcTaskExecutor::ProcessTask& proc_task, 634 ParallelTaskTerminator* t) : 635 AbstractGangTask("Process reference objects in parallel"), 636 _proc_task(proc_task), 637 _terminator(t) { 638 } 639 640 void work(uint worker_id) { 641 ResourceMark rm; 642 HandleMark hm; 643 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 644 ShenandoahHeap* heap = ShenandoahHeap::heap(); 645 ShenandoahCMDrainMarkingStackClosure complete_gc(worker_id, _terminator); 646 if (heap->has_forwarded_objects()) { 647 ShenandoahForwardedIsAliveClosure is_alive; 648 ShenandoahCMKeepAliveUpdateClosure keep_alive(heap->concurrentMark()->get_queue(worker_id)); 649 _proc_task.work(worker_id, is_alive, keep_alive, complete_gc); 650 } else { 651 ShenandoahIsAliveClosure is_alive; 652 ShenandoahCMKeepAliveClosure keep_alive(heap->concurrentMark()->get_queue(worker_id)); 653 _proc_task.work(worker_id, is_alive, keep_alive, complete_gc); 654 } 655 } 656 }; 657 658 class ShenandoahRefProcTaskExecutor : public AbstractRefProcTaskExecutor { 659 660 private: 661 WorkGang* _workers; 662 663 public: 664 665 ShenandoahRefProcTaskExecutor(WorkGang* workers) : 666 _workers(workers) { 667 } 668 669 // Executes a task using worker threads. 670 void execute(ProcessTask& task) { 671 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 672 673 // Shortcut execution if task is empty. 674 // This should be replaced with the generic ReferenceProcessor shortcut, 675 // see JDK-8181214, JDK-8043575, JDK-6938732. 676 if (task.is_empty()) { 677 return; 678 } 679 680 ShenandoahHeap* heap = ShenandoahHeap::heap(); 681 ShenandoahConcurrentMark* cm = heap->concurrentMark(); 682 uint nworkers = _workers->active_workers(); 683 cm->task_queues()->reserve(nworkers); 684 if (UseShenandoahOWST) { 685 ShenandoahTaskTerminator terminator(nworkers, cm->task_queues()); 686 ShenandoahRefProcTaskProxy proc_task_proxy(task, &terminator); 687 _workers->run_task(&proc_task_proxy); 688 } else { 689 ParallelTaskTerminator terminator(nworkers, cm->task_queues()); 690 ShenandoahRefProcTaskProxy proc_task_proxy(task, &terminator); 691 _workers->run_task(&proc_task_proxy); 692 } 693 } 694 }; 695 696 697 void ShenandoahConcurrentMark::weak_refs_work(bool full_gc) { 698 assert(process_references(), "sanity"); 699 700 ShenandoahHeap* sh = ShenandoahHeap::heap(); 701 702 ShenandoahPhaseTimings::Phase phase_root = 703 full_gc ? 704 ShenandoahPhaseTimings::full_gc_weakrefs : 705 ShenandoahPhaseTimings::weakrefs; 706 707 ShenandoahGCPhase phase(phase_root); 708 709 ReferenceProcessor* rp = sh->ref_processor(); 710 711 // NOTE: We cannot shortcut on has_discovered_references() here, because 712 // we will miss marking JNI Weak refs then, see implementation in 713 // ReferenceProcessor::process_discovered_references. 714 weak_refs_work_doit(full_gc); 715 716 rp->verify_no_references_recorded(); 717 assert(!rp->discovery_enabled(), "Post condition"); 718 719 } 720 721 void ShenandoahConcurrentMark::weak_refs_work_doit(bool full_gc) { 722 ShenandoahHeap* sh = ShenandoahHeap::heap(); 723 724 ReferenceProcessor* rp = sh->ref_processor(); 725 726 ShenandoahPhaseTimings::Phase phase_process = 727 full_gc ? 728 ShenandoahPhaseTimings::full_gc_weakrefs_process : 729 ShenandoahPhaseTimings::weakrefs_process; 730 731 shenandoah_assert_rp_isalive_not_installed(); 732 ReferenceProcessorIsAliveMutator fix_isalive(rp, sh->is_alive_closure()); 733 734 WorkGang* workers = sh->workers(); 735 uint nworkers = workers->active_workers(); 736 737 // Setup collector policy for softref cleaning. 738 bool clear_soft_refs = sh->soft_ref_policy()->use_should_clear_all_soft_refs(true /* bogus arg*/); 739 log_develop_debug(gc, ref)("clearing soft refs: %s", BOOL_TO_STR(clear_soft_refs)); 740 rp->setup_policy(clear_soft_refs); 741 rp->set_active_mt_degree(nworkers); 742 743 assert(task_queues()->is_empty(), "Should be empty"); 744 745 // complete_gc and keep_alive closures instantiated here are only needed for 746 // single-threaded path in RP. They share the queue 0 for tracking work, which 747 // simplifies implementation. Since RP may decide to call complete_gc several 748 // times, we need to be able to reuse the terminator. 749 uint serial_worker_id = 0; 750 ParallelTaskTerminator terminator(1, task_queues()); 751 ShenandoahCMDrainMarkingStackClosure complete_gc(serial_worker_id, &terminator, /* reset_terminator = */ true); 752 753 ShenandoahRefProcTaskExecutor executor(workers); 754 755 ReferenceProcessorPhaseTimes pt(sh->gc_timer(), rp->num_queues()); 756 757 { 758 ShenandoahGCPhase phase(phase_process); 759 760 if (sh->has_forwarded_objects()) { 761 ShenandoahForwardedIsAliveClosure is_alive; 762 ShenandoahCMKeepAliveUpdateClosure keep_alive(get_queue(serial_worker_id)); 763 rp->process_discovered_references(&is_alive, &keep_alive, 764 &complete_gc, &executor, 765 &pt); 766 767 WeakProcessor::weak_oops_do(&is_alive, &keep_alive); 768 } else { 769 ShenandoahIsAliveClosure is_alive; 770 ShenandoahCMKeepAliveClosure keep_alive(get_queue(serial_worker_id)); 771 rp->process_discovered_references(&is_alive, &keep_alive, 772 &complete_gc, &executor, 773 &pt); 774 775 WeakProcessor::weak_oops_do(&is_alive, &keep_alive); 776 } 777 pt.print_all_references(); 778 779 assert(task_queues()->is_empty(), "Should be empty"); 780 } 781 } 782 783 class ShenandoahCancelledGCYieldClosure : public YieldClosure { 784 private: 785 ShenandoahHeap* const _heap; 786 public: 787 ShenandoahCancelledGCYieldClosure() : _heap(ShenandoahHeap::heap()) {}; 788 virtual bool should_return() { return _heap->cancelled_gc(); } 789 }; 790 791 class ShenandoahPrecleanCompleteGCClosure : public VoidClosure { 792 public: 793 void do_void() { 794 ShenandoahHeap* sh = ShenandoahHeap::heap(); 795 ShenandoahConcurrentMark* scm = sh->concurrentMark(); 796 assert(scm->process_references(), "why else would we be here?"); 797 ParallelTaskTerminator terminator(1, scm->task_queues()); 798 799 ReferenceProcessor* rp = sh->ref_processor(); 800 shenandoah_assert_rp_isalive_installed(); 801 802 scm->mark_loop(0, &terminator, rp, 803 false, // not cancellable 804 true, // drain SATBs 805 scm->unload_classes(), 806 sh->has_forwarded_objects(), 807 false); // do not do strdedup 808 } 809 }; 810 811 class ShenandoahPrecleanKeepAliveUpdateClosure : public OopClosure { 812 private: 813 ShenandoahObjToScanQueue* _queue; 814 ShenandoahHeap* _heap; 815 816 template <class T> 817 inline void do_oop_nv(T* p) { 818 ShenandoahConcurrentMark::mark_through_ref<T, CONCURRENT, false /* string dedup */>(p, _heap, _queue); 819 } 820 821 public: 822 ShenandoahPrecleanKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) : 823 _queue(q), _heap(ShenandoahHeap::heap()) {} 824 825 void do_oop(narrowOop* p) { do_oop_nv(p); } 826 void do_oop(oop* p) { do_oop_nv(p); } 827 }; 828 829 void ShenandoahConcurrentMark::preclean_weak_refs() { 830 // Pre-cleaning weak references before diving into STW makes sense at the 831 // end of concurrent mark. This will filter out the references which referents 832 // are alive. Note that ReferenceProcessor already filters out these on reference 833 // discovery, and the bulk of work is done here. This phase processes leftovers 834 // that missed the initial filtering, i.e. when referent was marked alive after 835 // reference was discovered by RP. 836 837 assert(process_references(), "sanity"); 838 839 ShenandoahHeap* sh = ShenandoahHeap::heap(); 840 ReferenceProcessor* rp = sh->ref_processor(); 841 842 // Shortcut if no references were discovered to avoid winding up threads. 843 if (!rp->has_discovered_references()) { 844 return; 845 } 846 847 ReferenceProcessorMTDiscoveryMutator fix_mt_discovery(rp, false); 848 849 shenandoah_assert_rp_isalive_not_installed(); 850 ReferenceProcessorIsAliveMutator fix_isalive(rp, sh->is_alive_closure()); 851 852 // Interrupt on cancelled GC 853 ShenandoahCancelledGCYieldClosure yield; 854 855 assert(task_queues()->is_empty(), "Should be empty"); 856 857 ShenandoahPrecleanCompleteGCClosure complete_gc; 858 if (sh->has_forwarded_objects()) { 859 ShenandoahForwardedIsAliveClosure is_alive; 860 ShenandoahPrecleanKeepAliveUpdateClosure keep_alive(get_queue(0)); 861 ResourceMark rm; 862 rp->preclean_discovered_references(&is_alive, &keep_alive, 863 &complete_gc, &yield, 864 NULL); 865 } else { 866 ShenandoahIsAliveClosure is_alive; 867 ShenandoahCMKeepAliveClosure keep_alive(get_queue(0)); 868 ResourceMark rm; 869 rp->preclean_discovered_references(&is_alive, &keep_alive, 870 &complete_gc, &yield, 871 NULL); 872 } 873 874 assert(task_queues()->is_empty(), "Should be empty"); 875 } 876 877 void ShenandoahConcurrentMark::cancel() { 878 // Clean up marking stacks. 879 ShenandoahObjToScanQueueSet* queues = task_queues(); 880 queues->clear(); 881 882 // Cancel SATB buffers. 883 ShenandoahBarrierSet::satb_mark_queue_set().abandon_partial_marking(); 884 } 885 886 ShenandoahObjToScanQueue* ShenandoahConcurrentMark::get_queue(uint worker_id) { 887 assert(task_queues()->get_reserved() > worker_id, "No reserved queue for worker id: %d", worker_id); 888 return _task_queues->queue(worker_id); 889 } 890 891 void ShenandoahConcurrentMark::clear_queue(ShenandoahObjToScanQueue *q) { 892 q->set_empty(); 893 q->overflow_stack()->clear(); 894 q->clear_buffer(); 895 } 896 897 template <bool CANCELLABLE, bool DRAIN_SATB> 898 void ShenandoahConcurrentMark::mark_loop_prework(uint w, ParallelTaskTerminator *t, ReferenceProcessor *rp, 899 bool class_unload, bool update_refs, bool strdedup) { 900 ShenandoahObjToScanQueue* q = get_queue(w); 901 902 jushort* ld = get_liveness(w); 903 Copy::fill_to_bytes(ld, _heap->num_regions() * sizeof(jushort)); 904 905 // TODO: We can clean up this if we figure out how to do templated oop closures that 906 // play nice with specialized_oop_iterators. 907 if (class_unload) { 908 if (update_refs) { 909 if (strdedup) { 910 ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w); 911 ShenandoahMarkUpdateRefsMetadataDedupClosure cl(q, dq, rp); 912 mark_loop_work<ShenandoahMarkUpdateRefsMetadataDedupClosure, CANCELLABLE, DRAIN_SATB>(&cl, ld, w, t); 913 } else { 914 ShenandoahMarkUpdateRefsMetadataClosure cl(q, rp); 915 mark_loop_work<ShenandoahMarkUpdateRefsMetadataClosure, CANCELLABLE, DRAIN_SATB>(&cl, ld, w, t); 916 } 917 } else { 918 if (strdedup) { 919 ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w); 920 ShenandoahMarkRefsMetadataDedupClosure cl(q, dq, rp); 921 mark_loop_work<ShenandoahMarkRefsMetadataDedupClosure, CANCELLABLE, DRAIN_SATB>(&cl, ld, w, t); 922 } else { 923 ShenandoahMarkRefsMetadataClosure cl(q, rp); 924 mark_loop_work<ShenandoahMarkRefsMetadataClosure, CANCELLABLE, DRAIN_SATB>(&cl, ld, w, t); 925 } 926 } 927 } else { 928 if (update_refs) { 929 if (strdedup) { 930 ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w); 931 ShenandoahMarkUpdateRefsDedupClosure cl(q, dq, rp); 932 mark_loop_work<ShenandoahMarkUpdateRefsDedupClosure, CANCELLABLE, DRAIN_SATB>(&cl, ld, w, t); 933 } else { 934 ShenandoahMarkUpdateRefsClosure cl(q, rp); 935 mark_loop_work<ShenandoahMarkUpdateRefsClosure, CANCELLABLE, DRAIN_SATB>(&cl, ld, w, t); 936 } 937 } else { 938 if (strdedup) { 939 ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w); 940 ShenandoahMarkRefsDedupClosure cl(q, dq, rp); 941 mark_loop_work<ShenandoahMarkRefsDedupClosure, CANCELLABLE, DRAIN_SATB>(&cl, ld, w, t); 942 } else { 943 ShenandoahMarkRefsClosure cl(q, rp); 944 mark_loop_work<ShenandoahMarkRefsClosure, CANCELLABLE, DRAIN_SATB>(&cl, ld, w, t); 945 } 946 } 947 } 948 949 950 for (uint i = 0; i < _heap->num_regions(); i++) { 951 ShenandoahHeapRegion* r = _heap->get_region(i); 952 jushort live = ld[i]; 953 if (live > 0) { 954 r->increase_live_data_gc_words(live); 955 } 956 } 957 } 958 959 template <class T, bool CANCELLABLE, bool DRAIN_SATB> 960 void ShenandoahConcurrentMark::mark_loop_work(T* cl, jushort* live_data, uint worker_id, ParallelTaskTerminator *terminator) { 961 int seed = 17; 962 uintx stride = CANCELLABLE ? ShenandoahMarkLoopStride : 1; 963 964 ShenandoahHeap* heap = ShenandoahHeap::heap(); 965 ShenandoahObjToScanQueueSet* queues = task_queues(); 966 ShenandoahObjToScanQueue* q; 967 ShenandoahMarkTask t; 968 969 /* 970 * Process outstanding queues, if any. 971 * 972 * There can be more queues than workers. To deal with the imbalance, we claim 973 * extra queues first. Since marking can push new tasks into the queue associated 974 * with this worker id, we come back to process this queue in the normal loop. 975 */ 976 assert(queues->get_reserved() == heap->workers()->active_workers(), 977 "Need to reserve proper number of queues"); 978 979 q = queues->claim_next(); 980 while (q != NULL) { 981 if (CANCELLABLE && heap->check_cancelled_gc_and_yield()) { 982 ShenandoahCancelledTerminatorTerminator tt; 983 while (!terminator->offer_termination(&tt)); 984 return; 985 } 986 987 for (uint i = 0; i < stride; i++) { 988 if (try_queue(q, t)) { 989 do_task<T>(q, cl, live_data, &t); 990 } else { 991 assert(q->is_empty(), "Must be empty"); 992 q = queues->claim_next(); 993 break; 994 } 995 } 996 } 997 q = get_queue(worker_id); 998 999 ShenandoahSATBBufferClosure drain_satb(q); 1000 SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set(); 1001 1002 /* 1003 * Normal marking loop: 1004 */ 1005 while (true) { 1006 if (CANCELLABLE && heap->check_cancelled_gc_and_yield()) { 1007 ShenandoahCancelledTerminatorTerminator tt; 1008 while (!terminator->offer_termination(&tt)); 1009 return; 1010 } 1011 1012 if (DRAIN_SATB) { 1013 while (satb_mq_set.completed_buffers_num() > 0) { 1014 satb_mq_set.apply_closure_to_completed_buffer(&drain_satb); 1015 } 1016 } 1017 1018 uint work = 0; 1019 for (uint i = 0; i < stride; i++) { 1020 if (try_queue(q, t) || 1021 queues->steal(worker_id, &seed, t)) { 1022 do_task<T>(q, cl, live_data, &t); 1023 work++; 1024 } else { 1025 break; 1026 } 1027 } 1028 1029 if (work == 0) { 1030 // No work encountered in current stride, try to terminate. 1031 // Need to leave the STS here otherwise it might block safepoints. 1032 SuspendibleThreadSetLeaver stsl(CANCELLABLE && ShenandoahSuspendibleWorkers); 1033 if (terminator->offer_termination()) return; 1034 } 1035 } 1036 } 1037 1038 bool ShenandoahConcurrentMark::process_references() const { 1039 return _heap->process_references(); 1040 } 1041 1042 bool ShenandoahConcurrentMark::unload_classes() const { 1043 return _heap->unload_classes(); 1044 } 1045 1046 bool ShenandoahConcurrentMark::claim_codecache() { 1047 assert(ShenandoahConcurrentScanCodeRoots, "must not be called otherwise"); 1048 return _claimed_codecache.try_set(); 1049 } 1050 1051 void ShenandoahConcurrentMark::clear_claim_codecache() { 1052 assert(ShenandoahConcurrentScanCodeRoots, "must not be called otherwise"); 1053 _claimed_codecache.unset(); 1054 } 1055 1056 jushort* ShenandoahConcurrentMark::get_liveness(uint worker_id) { 1057 return _liveness_local[worker_id]; 1058 } 1059 1060 // Generate Shenandoah specialized oop_oop_iterate functions. 1061 SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_SHENANDOAH(ALL_KLASS_OOP_OOP_ITERATE_DEFN)