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