1 /*
   2  * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "classfile/classLoaderData.hpp"
  27 #include "classfile/classLoaderDataGraph.hpp"
  28 #include "gc/shared/referenceProcessor.hpp"
  29 #include "gc/shared/referenceProcessorPhaseTimes.hpp"
  30 #include "gc/shared/workgroup.hpp"
  31 #include "gc/shared/weakProcessor.inline.hpp"
  32 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  33 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  34 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  35 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  36 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  37 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  38 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  39 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  40 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
  41 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  42 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  43 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  44 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  45 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  46 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
  47 #include "gc/shenandoah/shenandoahTimingTracker.hpp"
  48 #include "gc/shenandoah/shenandoahTraversalGC.hpp"
  49 #include "gc/shenandoah/shenandoahUtils.hpp"
  50 #include "gc/shenandoah/shenandoahVerifier.hpp"
  51 
  52 #include "memory/iterator.hpp"
  53 #include "memory/metaspace.hpp"
  54 #include "memory/resourceArea.hpp"
  55 
  56 /**
  57  * NOTE: We are using the SATB buffer in thread.hpp and satbMarkQueue.hpp, however, it is not an SATB algorithm.
  58  * We're using the buffer as generic oop buffer to enqueue new values in concurrent oop stores, IOW, the algorithm
  59  * is incremental-update-based.
  60  *
  61  * NOTE on interaction with TAMS: we want to avoid traversing new objects for
  62  * several reasons:
  63  * - We will not reclaim them in this cycle anyway, because they are not in the
  64  *   cset
  65  * - It makes up for the bulk of work during final-pause
  66  * - It also shortens the concurrent cycle because we don't need to
  67  *   pointlessly traverse through newly allocated objects.
  68  * - As a nice side-effect, it solves the I-U termination problem (mutators
  69  *   cannot outrun the GC by allocating like crazy)
  70  * - It is an easy way to achieve MWF. What MWF does is to also enqueue the
  71  *   target object of stores if it's new. Treating new objects live implicitely
  72  *   achieves the same, but without extra barriers. I think the effect of
  73  *   shortened final-pause (mentioned above) is the main advantage of MWF. In
  74  *   particular, we will not see the head of a completely new long linked list
  75  *   in final-pause and end up traversing huge chunks of the heap there.
  76  * - We don't need to see/update the fields of new objects either, because they
  77  *   are either still null, or anything that's been stored into them has been
  78  *   evacuated+enqueued before (and will thus be treated later).
  79  *
  80  * We achieve this by setting TAMS for each region, and everything allocated
  81  * beyond TAMS will be 'implicitely marked'.
  82  *
  83  * Gotchas:
  84  * - While we want new objects to be implicitely marked, we don't want to count
  85  *   them alive. Otherwise the next cycle wouldn't pick them up and consider
  86  *   them for cset. This means that we need to protect such regions from
  87  *   getting accidentally thrashed at the end of traversal cycle. This is why I
  88  *   keep track of alloc-regions and check is_alloc_region() in the trashing
  89  *   code.
  90  * - We *need* to traverse through evacuated objects. Those objects are
  91  *   pre-existing, and any references in them point to interesting objects that
  92  *   we need to see. We also want to count them as live, because we just
  93  *   determined that they are alive :-) I achieve this by upping TAMS
  94  *   concurrently for every gclab/gc-shared alloc before publishing the
  95  *   evacuated object. This way, the GC threads will not consider such objects
  96  *   implictely marked, and traverse through them as normal.
  97  */
  98 class ShenandoahTraversalSATBBufferClosure : public SATBBufferClosure {
  99 private:
 100   ShenandoahObjToScanQueue* _queue;
 101   ShenandoahTraversalGC* _traversal_gc;
 102   ShenandoahHeap* const _heap;
 103 
 104 public:
 105   ShenandoahTraversalSATBBufferClosure(ShenandoahObjToScanQueue* q) :
 106     _queue(q),
 107     _heap(ShenandoahHeap::heap())
 108  { }
 109 
 110   void do_buffer(void** buffer, size_t size) {
 111     for (size_t i = 0; i < size; ++i) {
 112       oop* p = (oop*) &buffer[i];
 113       oop obj = RawAccess<>::oop_load(p);
 114       shenandoah_assert_not_forwarded(p, obj);
 115       if (_heap->marking_context()->mark(obj)) {
 116         _queue->push(ShenandoahMarkTask(obj));
 117       }
 118     }
 119   }
 120 };
 121 
 122 class ShenandoahTraversalSATBThreadsClosure : public ThreadClosure {
 123 private:
 124   ShenandoahTraversalSATBBufferClosure* _satb_cl;
 125 
 126 public:
 127   ShenandoahTraversalSATBThreadsClosure(ShenandoahTraversalSATBBufferClosure* satb_cl) :
 128     _satb_cl(satb_cl) {}
 129 
 130   void do_thread(Thread* thread) {
 131     ShenandoahThreadLocalData::satb_mark_queue(thread).apply_closure_and_empty(_satb_cl);
 132   }
 133 };
 134 
 135 // Like CLDToOopClosure, but clears has_modified_oops, so that we can record modified CLDs during traversal
 136 // and remark them later during final-traversal.
 137 class ShenandoahMarkCLDClosure : public CLDClosure {
 138 private:
 139   OopClosure* _cl;
 140 public:
 141   ShenandoahMarkCLDClosure(OopClosure* cl) : _cl(cl) {}
 142   void do_cld(ClassLoaderData* cld) {
 143     cld->oops_do(_cl, true, true);
 144   }
 145 };
 146 
 147 // Like CLDToOopClosure, but only process modified CLDs
 148 class ShenandoahRemarkCLDClosure : public CLDClosure {
 149 private:
 150   OopClosure* _cl;
 151 public:
 152   ShenandoahRemarkCLDClosure(OopClosure* cl) : _cl(cl) {}
 153   void do_cld(ClassLoaderData* cld) {
 154     if (cld->has_modified_oops()) {
 155       cld->oops_do(_cl, true, true);
 156     }
 157   }
 158 };
 159 
 160 class ShenandoahInitTraversalCollectionTask : public AbstractGangTask {
 161 private:
 162   ShenandoahRootProcessor* _rp;
 163   ShenandoahHeap* _heap;
 164   ShenandoahCsetCodeRootsIterator* _cset_coderoots;
 165 public:
 166   ShenandoahInitTraversalCollectionTask(ShenandoahRootProcessor* rp, ShenandoahCsetCodeRootsIterator* cset_coderoots) :
 167     AbstractGangTask("Shenandoah Init Traversal Collection"),
 168     _rp(rp),
 169     _heap(ShenandoahHeap::heap()),
 170     _cset_coderoots(cset_coderoots) {}
 171 
 172   void work(uint worker_id) {
 173     ShenandoahParallelWorkerSession worker_session(worker_id);
 174 
 175     ShenandoahEvacOOMScope oom_evac_scope;
 176     ShenandoahObjToScanQueueSet* queues = _heap->traversal_gc()->task_queues();
 177     ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 178 
 179     bool process_refs = _heap->process_references();
 180     bool unload_classes = _heap->unload_classes();
 181     ReferenceProcessor* rp = NULL;
 182     if (process_refs) {
 183       rp = _heap->ref_processor();
 184     }
 185 
 186     // Step 1: Process ordinary GC roots.
 187     {
 188       ShenandoahTraversalClosure roots_cl(q, rp);
 189       ShenandoahMarkCLDClosure cld_cl(&roots_cl);
 190       MarkingCodeBlobClosure code_cl(&roots_cl, CodeBlobToOopClosure::FixRelocations);
 191       if (unload_classes) {
 192         _rp->process_strong_roots(&roots_cl, &cld_cl, NULL, NULL, worker_id);
 193         // Need to pre-evac code roots here. Otherwise we might see from-space constants.
 194         ShenandoahWorkerTimings* worker_times = _heap->phase_timings()->worker_times();
 195         ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CodeCacheRoots, worker_id);
 196         _cset_coderoots->possibly_parallel_blobs_do(&code_cl);
 197       } else {
 198         _rp->process_all_roots(&roots_cl, &cld_cl, &code_cl, NULL, worker_id);
 199       }
 200       if (ShenandoahStringDedup::is_enabled()) {
 201         AlwaysTrueClosure is_alive;
 202         ShenandoahStringDedup::parallel_oops_do(&is_alive, &roots_cl, worker_id);
 203       }
 204     }
 205   }
 206 };
 207 
 208 class ShenandoahConcurrentTraversalCollectionTask : public AbstractGangTask {
 209 private:
 210   ShenandoahTaskTerminator* _terminator;
 211   ShenandoahHeap* _heap;
 212 public:
 213   ShenandoahConcurrentTraversalCollectionTask(ShenandoahTaskTerminator* terminator) :
 214     AbstractGangTask("Shenandoah Concurrent Traversal Collection"),
 215     _terminator(terminator),
 216     _heap(ShenandoahHeap::heap()) {}
 217 
 218   void work(uint worker_id) {
 219     ShenandoahConcurrentWorkerSession worker_session(worker_id);
 220     ShenandoahSuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 221     ShenandoahEvacOOMScope oom_evac_scope;
 222     ShenandoahTraversalGC* traversal_gc = _heap->traversal_gc();
 223 
 224     // Drain all outstanding work in queues.
 225     traversal_gc->main_loop(worker_id, _terminator, true);
 226   }
 227 };
 228 
 229 class ShenandoahFinalTraversalCollectionTask : public AbstractGangTask {
 230 private:
 231   ShenandoahRootProcessor* _rp;
 232   ShenandoahTaskTerminator* _terminator;
 233   ShenandoahHeap* _heap;
 234 public:
 235   ShenandoahFinalTraversalCollectionTask(ShenandoahRootProcessor* rp, ShenandoahTaskTerminator* terminator) :
 236     AbstractGangTask("Shenandoah Final Traversal Collection"),
 237     _rp(rp),
 238     _terminator(terminator),
 239     _heap(ShenandoahHeap::heap()) {}
 240 
 241   void work(uint worker_id) {
 242     ShenandoahParallelWorkerSession worker_session(worker_id);
 243 
 244     ShenandoahEvacOOMScope oom_evac_scope;
 245     ShenandoahTraversalGC* traversal_gc = _heap->traversal_gc();
 246 
 247     ShenandoahObjToScanQueueSet* queues = traversal_gc->task_queues();
 248     ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 249 
 250     bool process_refs = _heap->process_references();
 251     bool unload_classes = _heap->unload_classes();
 252     ReferenceProcessor* rp = NULL;
 253     if (process_refs) {
 254       rp = _heap->ref_processor();
 255     }
 256 
 257     // Step 0: Drain outstanding SATB queues.
 258     // NOTE: we piggy-back draining of remaining thread SATB buffers on the final root scan below.
 259     ShenandoahTraversalSATBBufferClosure satb_cl(q);
 260     {
 261       // Process remaining finished SATB buffers.
 262       SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 263       while (satb_mq_set.apply_closure_to_completed_buffer(&satb_cl));
 264       // Process remaining threads SATB buffers below.
 265     }
 266 
 267     // Step 1: Process GC roots.
 268     // For oops in code roots, they are marked, evacuated, enqueued for further traversal,
 269     // and the references to the oops are updated during init pause. New nmethods are handled
 270     // in similar way during nmethod-register process. Therefore, we don't need to rescan code
 271     // roots here.
 272     if (!_heap->is_degenerated_gc_in_progress()) {
 273       ShenandoahTraversalClosure roots_cl(q, rp);
 274       CLDToOopClosure cld_cl(&roots_cl, ClassLoaderData::_claim_strong);
 275       ShenandoahTraversalSATBThreadsClosure tc(&satb_cl);
 276       if (unload_classes) {
 277         ShenandoahRemarkCLDClosure remark_cld_cl(&roots_cl);
 278         _rp->process_strong_roots(&roots_cl, &remark_cld_cl, NULL, &tc, worker_id);
 279       } else {
 280         _rp->process_all_roots(&roots_cl, &cld_cl, NULL, &tc, worker_id);
 281       }
 282     } else {
 283       ShenandoahTraversalDegenClosure roots_cl(q, rp);
 284       CLDToOopClosure cld_cl(&roots_cl, ClassLoaderData::_claim_strong);
 285       ShenandoahTraversalSATBThreadsClosure tc(&satb_cl);
 286       if (unload_classes) {
 287         ShenandoahRemarkCLDClosure remark_cld_cl(&roots_cl);
 288         _rp->process_strong_roots(&roots_cl, &remark_cld_cl, NULL, &tc, worker_id);
 289       } else {
 290         _rp->process_all_roots(&roots_cl, &cld_cl, NULL, &tc, worker_id);
 291       }
 292     }
 293 
 294     {
 295       ShenandoahWorkerTimings *worker_times = _heap->phase_timings()->worker_times();
 296       ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::FinishQueues, worker_id);
 297 
 298       // Step 3: Finally drain all outstanding work in queues.
 299       traversal_gc->main_loop(worker_id, _terminator, false);
 300     }
 301 
 302   }
 303 };
 304 
 305 ShenandoahTraversalGC::ShenandoahTraversalGC(ShenandoahHeap* heap, size_t num_regions) :
 306   _heap(heap),
 307   _task_queues(new ShenandoahObjToScanQueueSet(heap->max_workers())),
 308   _traversal_set(ShenandoahHeapRegionSet()) {
 309 
 310   uint num_queues = heap->max_workers();
 311   for (uint i = 0; i < num_queues; ++i) {
 312     ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue();
 313     task_queue->initialize();
 314     _task_queues->register_queue(i, task_queue);
 315   }
 316 }
 317 
 318 ShenandoahTraversalGC::~ShenandoahTraversalGC() {
 319 }
 320 
 321 void ShenandoahTraversalGC::prepare_regions() {
 322   size_t num_regions = _heap->num_regions();
 323   ShenandoahMarkingContext* const ctx = _heap->marking_context();
 324   for (size_t i = 0; i < num_regions; i++) {
 325     ShenandoahHeapRegion* region = _heap->get_region(i);
 326     if (_heap->is_bitmap_slice_committed(region)) {
 327       if (_traversal_set.is_in(i)) {
 328         ctx->capture_top_at_mark_start(region);
 329         region->clear_live_data();
 330         assert(ctx->is_bitmap_clear_range(region->bottom(), region->end()), "bitmap for traversal regions must be cleared");
 331       } else {
 332         // Everything outside the traversal set is always considered live.
 333         ctx->reset_top_at_mark_start(region);
 334       }
 335     } else {
 336       // FreeSet may contain uncommitted empty regions, once they are recommitted,
 337       // their TAMS may have old values, so reset them here.
 338       ctx->reset_top_at_mark_start(region);
 339     }
 340   }
 341 }
 342 
 343 void ShenandoahTraversalGC::prepare() {
 344   _heap->collection_set()->clear();
 345   assert(_heap->collection_set()->count() == 0, "collection set not clear");
 346 
 347   {
 348     ShenandoahGCPhase phase(ShenandoahPhaseTimings::traversal_gc_make_parsable);
 349     _heap->make_parsable(true);
 350   }
 351 
 352   if (UseTLAB) {
 353     ShenandoahGCPhase phase(ShenandoahPhaseTimings::traversal_gc_resize_tlabs);
 354     _heap->resize_tlabs();
 355   }
 356 
 357   assert(_heap->marking_context()->is_bitmap_clear(), "need clean mark bitmap");
 358   assert(!_heap->marking_context()->is_complete(), "should not be complete");
 359 
 360   ShenandoahFreeSet* free_set = _heap->free_set();
 361   ShenandoahCollectionSet* collection_set = _heap->collection_set();
 362 
 363   // Find collection set
 364   _heap->heuristics()->choose_collection_set(collection_set);
 365   prepare_regions();
 366 
 367   // Rebuild free set
 368   free_set->rebuild();
 369 
 370   log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "M, " SIZE_FORMAT "M CSet, " SIZE_FORMAT " CSet regions",
 371                      collection_set->garbage() / M, collection_set->live_data() / M, collection_set->count());
 372 }
 373 
 374 void ShenandoahTraversalGC::init_traversal_collection() {
 375   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "STW traversal GC");
 376 
 377   if (ShenandoahVerify) {
 378     _heap->verifier()->verify_before_traversal();
 379   }
 380 
 381   if (VerifyBeforeGC) {
 382     Universe::verify();
 383   }
 384 
 385   {
 386     ShenandoahGCPhase phase_prepare(ShenandoahPhaseTimings::traversal_gc_prepare);
 387     ShenandoahHeapLocker lock(_heap->lock());
 388     prepare();
 389   }
 390 
 391   _heap->set_concurrent_traversal_in_progress(true);
 392 
 393   bool process_refs = _heap->process_references();
 394   if (process_refs) {
 395     ReferenceProcessor* rp = _heap->ref_processor();
 396     rp->enable_discovery(true /*verify_no_refs*/);
 397     rp->setup_policy(_heap->soft_ref_policy()->should_clear_all_soft_refs());
 398   }
 399 
 400   {
 401     ShenandoahGCPhase phase_work(ShenandoahPhaseTimings::init_traversal_gc_work);
 402     assert(_task_queues->is_empty(), "queues must be empty before traversal GC");
 403     TASKQUEUE_STATS_ONLY(_task_queues->reset_taskqueue_stats());
 404 
 405 #if defined(COMPILER2) || INCLUDE_JVMCI
 406     DerivedPointerTable::clear();
 407 #endif
 408 
 409     {
 410       uint nworkers = _heap->workers()->active_workers();
 411       task_queues()->reserve(nworkers);
 412       ShenandoahRootProcessor rp(_heap, nworkers, ShenandoahPhaseTimings::init_traversal_gc_work);
 413 
 414       ShenandoahCsetCodeRootsIterator cset_coderoots = ShenandoahCodeRoots::cset_iterator();
 415 
 416       ShenandoahInitTraversalCollectionTask traversal_task(&rp, &cset_coderoots);
 417       _heap->workers()->run_task(&traversal_task);
 418     }
 419 
 420 #if defined(COMPILER2) || INCLUDE_JVMCI
 421     DerivedPointerTable::update_pointers();
 422 #endif
 423   }
 424 
 425   if (ShenandoahPacing) {
 426     _heap->pacer()->setup_for_traversal();
 427   }
 428 }
 429 
 430 void ShenandoahTraversalGC::main_loop(uint w, ShenandoahTaskTerminator* t, bool sts_yield) {
 431   ShenandoahObjToScanQueue* q = task_queues()->queue(w);
 432 
 433   // Initialize live data.
 434   jushort* ld = _heap->get_liveness_cache(w);
 435 
 436   ReferenceProcessor* rp = NULL;
 437   if (_heap->process_references()) {
 438     rp = _heap->ref_processor();
 439   }
 440   {
 441     if (!_heap->is_degenerated_gc_in_progress()) {
 442       if (_heap->unload_classes()) {
 443         if (ShenandoahStringDedup::is_enabled()) {
 444           ShenandoahTraversalMetadataDedupClosure cl(q, rp);
 445           main_loop_work<ShenandoahTraversalMetadataDedupClosure>(&cl, ld, w, t, sts_yield);
 446         } else {
 447           ShenandoahTraversalMetadataClosure cl(q, rp);
 448           main_loop_work<ShenandoahTraversalMetadataClosure>(&cl, ld, w, t, sts_yield);
 449         }
 450       } else {
 451         if (ShenandoahStringDedup::is_enabled()) {
 452           ShenandoahTraversalDedupClosure cl(q, rp);
 453           main_loop_work<ShenandoahTraversalDedupClosure>(&cl, ld, w, t, sts_yield);
 454         } else {
 455           ShenandoahTraversalClosure cl(q, rp);
 456           main_loop_work<ShenandoahTraversalClosure>(&cl, ld, w, t, sts_yield);
 457         }
 458       }
 459     } else {
 460       if (_heap->unload_classes()) {
 461         if (ShenandoahStringDedup::is_enabled()) {
 462           ShenandoahTraversalMetadataDedupDegenClosure cl(q, rp);
 463           main_loop_work<ShenandoahTraversalMetadataDedupDegenClosure>(&cl, ld, w, t, sts_yield);
 464         } else {
 465           ShenandoahTraversalMetadataDegenClosure cl(q, rp);
 466           main_loop_work<ShenandoahTraversalMetadataDegenClosure>(&cl, ld, w, t, sts_yield);
 467         }
 468       } else {
 469         if (ShenandoahStringDedup::is_enabled()) {
 470           ShenandoahTraversalDedupDegenClosure cl(q, rp);
 471           main_loop_work<ShenandoahTraversalDedupDegenClosure>(&cl, ld, w, t, sts_yield);
 472         } else {
 473           ShenandoahTraversalDegenClosure cl(q, rp);
 474           main_loop_work<ShenandoahTraversalDegenClosure>(&cl, ld, w, t, sts_yield);
 475         }
 476       }
 477     }
 478   }
 479 
 480   _heap->flush_liveness_cache(w);
 481 }
 482 
 483 template <class T>
 484 void ShenandoahTraversalGC::main_loop_work(T* cl, jushort* live_data, uint worker_id, ShenandoahTaskTerminator* terminator, bool sts_yield) {
 485   ShenandoahObjToScanQueueSet* queues = task_queues();
 486   ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 487   ShenandoahConcurrentMark* conc_mark = _heap->concurrent_mark();
 488 
 489   uintx stride = ShenandoahMarkLoopStride;
 490 
 491   ShenandoahMarkTask task;
 492 
 493   // Process outstanding queues, if any.
 494   q = queues->claim_next();
 495   while (q != NULL) {
 496     if (_heap->check_cancelled_gc_and_yield(sts_yield)) {
 497       return;
 498     }
 499 
 500     for (uint i = 0; i < stride; i++) {
 501       if (q->pop(task)) {
 502         conc_mark->do_task<T>(q, cl, live_data, &task);
 503       } else {
 504         assert(q->is_empty(), "Must be empty");
 505         q = queues->claim_next();
 506         break;
 507       }
 508     }
 509   }
 510 
 511   if (check_and_handle_cancelled_gc(terminator, sts_yield)) return;
 512 
 513   // Normal loop.
 514   q = queues->queue(worker_id);
 515 
 516   ShenandoahTraversalSATBBufferClosure drain_satb(q);
 517   SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 518 
 519   while (true) {
 520     if (check_and_handle_cancelled_gc(terminator, sts_yield)) return;
 521 
 522     while (satb_mq_set.completed_buffers_num() > 0) {
 523       satb_mq_set.apply_closure_to_completed_buffer(&drain_satb);
 524     }
 525 
 526     uint work = 0;
 527     for (uint i = 0; i < stride; i++) {
 528       if (q->pop(task) ||
 529           queues->steal(worker_id, task)) {
 530         conc_mark->do_task<T>(q, cl, live_data, &task);
 531         work++;
 532       } else {
 533         break;
 534       }
 535     }
 536 
 537     if (work == 0) {
 538       // No more work, try to terminate
 539       ShenandoahEvacOOMScopeLeaver oom_scope_leaver;
 540       ShenandoahSuspendibleThreadSetLeaver stsl(sts_yield && ShenandoahSuspendibleWorkers);
 541       ShenandoahTerminationTimingsTracker term_tracker(worker_id);
 542       ShenandoahTerminatorTerminator tt(_heap);
 543 
 544       if (terminator->offer_termination(&tt)) return;
 545     }
 546   }
 547 }
 548 
 549 bool ShenandoahTraversalGC::check_and_handle_cancelled_gc(ShenandoahTaskTerminator* terminator, bool sts_yield) {
 550   if (_heap->cancelled_gc()) {
 551     return true;
 552   }
 553   return false;
 554 }
 555 
 556 void ShenandoahTraversalGC::concurrent_traversal_collection() {
 557   ClassLoaderDataGraph::clear_claimed_marks();
 558 
 559   ShenandoahGCPhase phase_work(ShenandoahPhaseTimings::conc_traversal);
 560   if (!_heap->cancelled_gc()) {
 561     uint nworkers = _heap->workers()->active_workers();
 562     task_queues()->reserve(nworkers);
 563     ShenandoahTerminationTracker tracker(ShenandoahPhaseTimings::conc_traversal_termination);
 564 
 565     ShenandoahTaskTerminator terminator(nworkers, task_queues());
 566     ShenandoahConcurrentTraversalCollectionTask task(&terminator);
 567     _heap->workers()->run_task(&task);
 568   }
 569 
 570   if (!_heap->cancelled_gc() && ShenandoahPreclean && _heap->process_references()) {
 571     preclean_weak_refs();
 572   }
 573 }
 574 
 575 void ShenandoahTraversalGC::final_traversal_collection() {
 576   _heap->make_parsable(true);
 577 
 578   if (!_heap->cancelled_gc()) {
 579 #if defined(COMPILER2) || INCLUDE_JVMCI
 580     DerivedPointerTable::clear();
 581 #endif
 582     ShenandoahGCPhase phase_work(ShenandoahPhaseTimings::final_traversal_gc_work);
 583     uint nworkers = _heap->workers()->active_workers();
 584     task_queues()->reserve(nworkers);
 585 
 586     // Finish traversal
 587     ShenandoahRootProcessor rp(_heap, nworkers, ShenandoahPhaseTimings::final_traversal_gc_work);
 588     ShenandoahTerminationTracker term(ShenandoahPhaseTimings::final_traversal_gc_termination);
 589 
 590     ShenandoahTaskTerminator terminator(nworkers, task_queues());
 591     ShenandoahFinalTraversalCollectionTask task(&rp, &terminator);
 592     _heap->workers()->run_task(&task);
 593 #if defined(COMPILER2) || INCLUDE_JVMCI
 594     DerivedPointerTable::update_pointers();
 595 #endif
 596   }
 597 
 598   if (!_heap->cancelled_gc() && _heap->process_references()) {
 599     weak_refs_work();
 600   }
 601 
 602   if (!_heap->cancelled_gc()) {
 603     fixup_roots();
 604     if (_heap->unload_classes()) {
 605       _heap->unload_classes_and_cleanup_tables(false);
 606     }
 607   }
 608 
 609   if (!_heap->cancelled_gc()) {
 610     assert(_task_queues->is_empty(), "queues must be empty after traversal GC");
 611     TASKQUEUE_STATS_ONLY(_task_queues->print_taskqueue_stats());
 612     TASKQUEUE_STATS_ONLY(_task_queues->reset_taskqueue_stats());
 613 
 614     // No more marking expected
 615     _heap->mark_complete_marking_context();
 616 
 617     // Resize metaspace
 618     MetaspaceGC::compute_new_size();
 619 
 620     // Still good? We can now trash the cset, and make final verification
 621     {
 622       ShenandoahGCPhase phase_cleanup(ShenandoahPhaseTimings::traversal_gc_cleanup);
 623       ShenandoahHeapLocker lock(_heap->lock());
 624 
 625       // Trash everything
 626       // Clear immediate garbage regions.
 627       size_t num_regions = _heap->num_regions();
 628 
 629       ShenandoahHeapRegionSet* traversal_regions = traversal_set();
 630       ShenandoahFreeSet* free_regions = _heap->free_set();
 631       ShenandoahMarkingContext* const ctx = _heap->marking_context();
 632       free_regions->clear();
 633       for (size_t i = 0; i < num_regions; i++) {
 634         ShenandoahHeapRegion* r = _heap->get_region(i);
 635         bool not_allocated = ctx->top_at_mark_start(r) == r->top();
 636 
 637         bool candidate = traversal_regions->is_in(r) && !r->has_live() && not_allocated;
 638         if (r->is_humongous_start() && candidate) {
 639           // Trash humongous.
 640           HeapWord* humongous_obj = r->bottom();
 641           assert(!ctx->is_marked(oop(humongous_obj)), "must not be marked");
 642           r->make_trash_immediate();
 643           while (i + 1 < num_regions && _heap->get_region(i + 1)->is_humongous_continuation()) {
 644             i++;
 645             r = _heap->get_region(i);
 646             assert(r->is_humongous_continuation(), "must be humongous continuation");
 647             r->make_trash_immediate();
 648           }
 649         } else if (!r->is_empty() && candidate) {
 650           // Trash regular.
 651           assert(!r->is_humongous(), "handled above");
 652           assert(!r->is_trash(), "must not already be trashed");
 653           r->make_trash_immediate();
 654         }
 655       }
 656       _heap->collection_set()->clear();
 657       _heap->free_set()->rebuild();
 658       reset();
 659     }
 660 
 661     assert(_task_queues->is_empty(), "queues must be empty after traversal GC");
 662     _heap->set_concurrent_traversal_in_progress(false);
 663     assert(!_heap->cancelled_gc(), "must not be cancelled when getting out here");
 664 
 665     if (ShenandoahVerify) {
 666       _heap->verifier()->verify_after_traversal();
 667     }
 668 
 669     if (VerifyAfterGC) {
 670       Universe::verify();
 671     }
 672   }
 673 }
 674 
 675 class ShenandoahTraversalFixRootsClosure : public OopClosure {
 676 private:
 677   template <class T>
 678   inline void do_oop_work(T* p) {
 679     T o = RawAccess<>::oop_load(p);
 680     if (!CompressedOops::is_null(o)) {
 681       oop obj = CompressedOops::decode_not_null(o);
 682       oop forw = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 683       if (!oopDesc::equals_raw(obj, forw)) {
 684         RawAccess<IS_NOT_NULL>::oop_store(p, forw);
 685       }
 686     }
 687   }
 688 
 689 public:
 690   inline void do_oop(oop* p) { do_oop_work(p); }
 691   inline void do_oop(narrowOop* p) { do_oop_work(p); }
 692 };
 693 
 694 class ShenandoahTraversalFixRootsTask : public AbstractGangTask {
 695 private:
 696   ShenandoahRootProcessor* _rp;
 697 
 698 public:
 699   ShenandoahTraversalFixRootsTask(ShenandoahRootProcessor* rp) :
 700     AbstractGangTask("Shenandoah traversal fix roots"),
 701     _rp(rp) {
 702     assert(ShenandoahHeap::heap()->has_forwarded_objects(), "Must be");
 703   }
 704 
 705   void work(uint worker_id) {
 706     ShenandoahParallelWorkerSession worker_session(worker_id);
 707     ShenandoahTraversalFixRootsClosure cl;
 708     MarkingCodeBlobClosure blobsCl(&cl, CodeBlobToOopClosure::FixRelocations);
 709     CLDToOopClosure cldCl(&cl, ClassLoaderData::_claim_strong);
 710     _rp->update_all_roots<ShenandoahForwardedIsAliveClosure>(&cl, &cldCl, &blobsCl, NULL, worker_id);
 711   }
 712 };
 713 
 714 void ShenandoahTraversalGC::fixup_roots() {
 715 #if defined(COMPILER2) || INCLUDE_JVMCI
 716   DerivedPointerTable::clear();
 717 #endif
 718   ShenandoahRootProcessor rp(_heap, _heap->workers()->active_workers(), ShenandoahPhaseTimings::final_traversal_update_roots);
 719   ShenandoahTraversalFixRootsTask update_roots_task(&rp);
 720   _heap->workers()->run_task(&update_roots_task);
 721 #if defined(COMPILER2) || INCLUDE_JVMCI
 722   DerivedPointerTable::update_pointers();
 723 #endif
 724 }
 725 
 726 void ShenandoahTraversalGC::reset() {
 727   _task_queues->clear();
 728 }
 729 
 730 ShenandoahObjToScanQueueSet* ShenandoahTraversalGC::task_queues() {
 731   return _task_queues;
 732 }
 733 
 734 class ShenandoahTraversalCancelledGCYieldClosure : public YieldClosure {
 735 private:
 736   ShenandoahHeap* const _heap;
 737 public:
 738   ShenandoahTraversalCancelledGCYieldClosure() : _heap(ShenandoahHeap::heap()) {};
 739   virtual bool should_return() { return _heap->cancelled_gc(); }
 740 };
 741 
 742 class ShenandoahTraversalPrecleanCompleteGCClosure : public VoidClosure {
 743 public:
 744   void do_void() {
 745     ShenandoahHeap* sh = ShenandoahHeap::heap();
 746     ShenandoahTraversalGC* traversal_gc = sh->traversal_gc();
 747     assert(sh->process_references(), "why else would we be here?");
 748     ShenandoahTaskTerminator terminator(1, traversal_gc->task_queues());
 749     shenandoah_assert_rp_isalive_installed();
 750     traversal_gc->main_loop((uint) 0, &terminator, true);
 751   }
 752 };
 753 
 754 class ShenandoahTraversalKeepAliveUpdateClosure : public OopClosure {
 755 private:
 756   ShenandoahObjToScanQueue* _queue;
 757   Thread* _thread;
 758   ShenandoahTraversalGC* _traversal_gc;
 759   ShenandoahMarkingContext* const _mark_context;
 760 
 761   template <class T>
 762   inline void do_oop_work(T* p) {
 763     _traversal_gc->process_oop<T, false /* string dedup */, false /* degen */>(p, _thread, _queue, _mark_context);
 764   }
 765 
 766 public:
 767   ShenandoahTraversalKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) :
 768     _queue(q), _thread(Thread::current()),
 769     _traversal_gc(ShenandoahHeap::heap()->traversal_gc()),
 770     _mark_context(ShenandoahHeap::heap()->marking_context()) {}
 771 
 772   void do_oop(narrowOop* p) { do_oop_work(p); }
 773   void do_oop(oop* p)       { do_oop_work(p); }
 774 };
 775 
 776 class ShenandoahTraversalKeepAliveUpdateDegenClosure : public OopClosure {
 777 private:
 778   ShenandoahObjToScanQueue* _queue;
 779   Thread* _thread;
 780   ShenandoahTraversalGC* _traversal_gc;
 781   ShenandoahMarkingContext* const _mark_context;
 782 
 783   template <class T>
 784   inline void do_oop_work(T* p) {
 785     _traversal_gc->process_oop<T, false /* string dedup */, true /* degen */>(p, _thread, _queue, _mark_context);
 786   }
 787 
 788 public:
 789   ShenandoahTraversalKeepAliveUpdateDegenClosure(ShenandoahObjToScanQueue* q) :
 790           _queue(q), _thread(Thread::current()),
 791           _traversal_gc(ShenandoahHeap::heap()->traversal_gc()),
 792           _mark_context(ShenandoahHeap::heap()->marking_context()) {}
 793 
 794   void do_oop(narrowOop* p) { do_oop_work(p); }
 795   void do_oop(oop* p)       { do_oop_work(p); }
 796 };
 797 
 798 class ShenandoahTraversalSingleThreadKeepAliveUpdateClosure : public OopClosure {
 799 private:
 800   ShenandoahObjToScanQueue* _queue;
 801   Thread* _thread;
 802   ShenandoahTraversalGC* _traversal_gc;
 803   ShenandoahMarkingContext* const _mark_context;
 804 
 805   template <class T>
 806   inline void do_oop_work(T* p) {
 807     ShenandoahEvacOOMScope evac_scope;
 808     _traversal_gc->process_oop<T, false /* string dedup */, false /* degen */>(p, _thread, _queue, _mark_context);
 809   }
 810 
 811 public:
 812   ShenandoahTraversalSingleThreadKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) :
 813           _queue(q), _thread(Thread::current()),
 814           _traversal_gc(ShenandoahHeap::heap()->traversal_gc()),
 815           _mark_context(ShenandoahHeap::heap()->marking_context()) {}
 816 
 817   void do_oop(narrowOop* p) { do_oop_work(p); }
 818   void do_oop(oop* p)       { do_oop_work(p); }
 819 };
 820 
 821 class ShenandoahTraversalSingleThreadKeepAliveUpdateDegenClosure : public OopClosure {
 822 private:
 823   ShenandoahObjToScanQueue* _queue;
 824   Thread* _thread;
 825   ShenandoahTraversalGC* _traversal_gc;
 826   ShenandoahMarkingContext* const _mark_context;
 827 
 828   template <class T>
 829   inline void do_oop_work(T* p) {
 830     ShenandoahEvacOOMScope evac_scope;
 831     _traversal_gc->process_oop<T, false /* string dedup */, true /* degen */>(p, _thread, _queue, _mark_context);
 832   }
 833 
 834 public:
 835   ShenandoahTraversalSingleThreadKeepAliveUpdateDegenClosure(ShenandoahObjToScanQueue* q) :
 836           _queue(q), _thread(Thread::current()),
 837           _traversal_gc(ShenandoahHeap::heap()->traversal_gc()),
 838           _mark_context(ShenandoahHeap::heap()->marking_context()) {}
 839 
 840   void do_oop(narrowOop* p) { do_oop_work(p); }
 841   void do_oop(oop* p)       { do_oop_work(p); }
 842 };
 843 
 844 class ShenandoahTraversalPrecleanTask : public AbstractGangTask {
 845 private:
 846   ReferenceProcessor* _rp;
 847 
 848 public:
 849   ShenandoahTraversalPrecleanTask(ReferenceProcessor* rp) :
 850           AbstractGangTask("Precleaning task"),
 851           _rp(rp) {}
 852 
 853   void work(uint worker_id) {
 854     assert(worker_id == 0, "The code below is single-threaded, only one worker is expected");
 855     ShenandoahParallelWorkerSession worker_session(worker_id);
 856     ShenandoahSuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 857     ShenandoahEvacOOMScope oom_evac_scope;
 858 
 859     ShenandoahHeap* sh = ShenandoahHeap::heap();
 860 
 861     ShenandoahObjToScanQueue* q = sh->traversal_gc()->task_queues()->queue(worker_id);
 862 
 863     ShenandoahForwardedIsAliveClosure is_alive;
 864     ShenandoahTraversalCancelledGCYieldClosure yield;
 865     ShenandoahTraversalPrecleanCompleteGCClosure complete_gc;
 866     ShenandoahTraversalKeepAliveUpdateClosure keep_alive(q);
 867     ResourceMark rm;
 868     _rp->preclean_discovered_references(&is_alive, &keep_alive,
 869                                         &complete_gc, &yield,
 870                                         NULL);
 871   }
 872 };
 873 
 874 void ShenandoahTraversalGC::preclean_weak_refs() {
 875   // Pre-cleaning weak references before diving into STW makes sense at the
 876   // end of concurrent mark. This will filter out the references which referents
 877   // are alive. Note that ReferenceProcessor already filters out these on reference
 878   // discovery, and the bulk of work is done here. This phase processes leftovers
 879   // that missed the initial filtering, i.e. when referent was marked alive after
 880   // reference was discovered by RP.
 881 
 882   assert(_heap->process_references(), "sanity");
 883   assert(!_heap->is_degenerated_gc_in_progress(), "must be in concurrent non-degenerated phase");
 884 
 885   // Shortcut if no references were discovered to avoid winding up threads.
 886   ReferenceProcessor* rp = _heap->ref_processor();
 887   if (!rp->has_discovered_references()) {
 888     return;
 889   }
 890 
 891   ReferenceProcessorMTDiscoveryMutator fix_mt_discovery(rp, false);
 892 
 893   shenandoah_assert_rp_isalive_not_installed();
 894   ShenandoahForwardedIsAliveClosure is_alive;
 895   ReferenceProcessorIsAliveMutator fix_isalive(rp, &is_alive);
 896 
 897   assert(task_queues()->is_empty(), "Should be empty");
 898 
 899   // Execute precleaning in the worker thread: it will give us GCLABs, String dedup
 900   // queues and other goodies. When upstream ReferenceProcessor starts supporting
 901   // parallel precleans, we can extend this to more threads.
 902   ShenandoahPushWorkerScope scope(_heap->workers(), 1, /* check_workers = */ false);
 903 
 904   WorkGang* workers = _heap->workers();
 905   uint nworkers = workers->active_workers();
 906   assert(nworkers == 1, "This code uses only a single worker");
 907   task_queues()->reserve(nworkers);
 908 
 909   ShenandoahTraversalPrecleanTask task(rp);
 910   workers->run_task(&task);
 911 
 912   assert(_heap->cancelled_gc() || task_queues()->is_empty(), "Should be empty");
 913 }
 914 
 915 // Weak Reference Closures
 916 class ShenandoahTraversalDrainMarkingStackClosure: public VoidClosure {
 917   uint _worker_id;
 918   ShenandoahTaskTerminator* _terminator;
 919   bool _reset_terminator;
 920 
 921 public:
 922   ShenandoahTraversalDrainMarkingStackClosure(uint worker_id, ShenandoahTaskTerminator* t, bool reset_terminator = false):
 923     _worker_id(worker_id),
 924     _terminator(t),
 925     _reset_terminator(reset_terminator) {
 926   }
 927 
 928   void do_void() {
 929     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 930 
 931     ShenandoahHeap* sh = ShenandoahHeap::heap();
 932     ShenandoahTraversalGC* traversal_gc = sh->traversal_gc();
 933     assert(sh->process_references(), "why else would we be here?");
 934     shenandoah_assert_rp_isalive_installed();
 935 
 936     traversal_gc->main_loop(_worker_id, _terminator, false);
 937 
 938     if (_reset_terminator) {
 939       _terminator->reset_for_reuse();
 940     }
 941   }
 942 };
 943 
 944 class ShenandoahTraversalSingleThreadedDrainMarkingStackClosure: public VoidClosure {
 945   uint _worker_id;
 946   ShenandoahTaskTerminator* _terminator;
 947   bool _reset_terminator;
 948 
 949 public:
 950   ShenandoahTraversalSingleThreadedDrainMarkingStackClosure(uint worker_id, ShenandoahTaskTerminator* t, bool reset_terminator = false):
 951           _worker_id(worker_id),
 952           _terminator(t),
 953           _reset_terminator(reset_terminator) {
 954   }
 955 
 956   void do_void() {
 957     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 958 
 959     ShenandoahHeap* sh = ShenandoahHeap::heap();
 960     ShenandoahTraversalGC* traversal_gc = sh->traversal_gc();
 961     assert(sh->process_references(), "why else would we be here?");
 962     shenandoah_assert_rp_isalive_installed();
 963 
 964     ShenandoahEvacOOMScope evac_scope;
 965     traversal_gc->main_loop(_worker_id, _terminator, false);
 966 
 967     if (_reset_terminator) {
 968       _terminator->reset_for_reuse();
 969     }
 970   }
 971 };
 972 
 973 void ShenandoahTraversalGC::weak_refs_work() {
 974   assert(_heap->process_references(), "sanity");
 975 
 976   ShenandoahPhaseTimings::Phase phase_root = ShenandoahPhaseTimings::weakrefs;
 977 
 978   ShenandoahGCPhase phase(phase_root);
 979 
 980   ReferenceProcessor* rp = _heap->ref_processor();
 981 
 982   // NOTE: We cannot shortcut on has_discovered_references() here, because
 983   // we will miss marking JNI Weak refs then, see implementation in
 984   // ReferenceProcessor::process_discovered_references.
 985   weak_refs_work_doit();
 986 
 987   rp->verify_no_references_recorded();
 988   assert(!rp->discovery_enabled(), "Post condition");
 989 
 990 }
 991 
 992 class ShenandoahTraversalRefProcTaskProxy : public AbstractGangTask {
 993 private:
 994   AbstractRefProcTaskExecutor::ProcessTask& _proc_task;
 995   ShenandoahTaskTerminator* _terminator;
 996 
 997 public:
 998   ShenandoahTraversalRefProcTaskProxy(AbstractRefProcTaskExecutor::ProcessTask& proc_task,
 999                                       ShenandoahTaskTerminator* t) :
1000     AbstractGangTask("Process reference objects in parallel"),
1001     _proc_task(proc_task),
1002     _terminator(t) {
1003   }
1004 
1005   void work(uint worker_id) {
1006     ShenandoahEvacOOMScope oom_evac_scope;
1007     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
1008     ShenandoahHeap* heap = ShenandoahHeap::heap();
1009     ShenandoahTraversalDrainMarkingStackClosure complete_gc(worker_id, _terminator);
1010 
1011     ShenandoahForwardedIsAliveClosure is_alive;
1012     if (!heap->is_degenerated_gc_in_progress()) {
1013       ShenandoahTraversalKeepAliveUpdateClosure keep_alive(heap->traversal_gc()->task_queues()->queue(worker_id));
1014       _proc_task.work(worker_id, is_alive, keep_alive, complete_gc);
1015     } else {
1016       ShenandoahTraversalKeepAliveUpdateDegenClosure keep_alive(heap->traversal_gc()->task_queues()->queue(worker_id));
1017       _proc_task.work(worker_id, is_alive, keep_alive, complete_gc);
1018     }
1019   }
1020 };
1021 
1022 class ShenandoahTraversalRefProcTaskExecutor : public AbstractRefProcTaskExecutor {
1023 private:
1024   WorkGang* _workers;
1025 
1026 public:
1027   ShenandoahTraversalRefProcTaskExecutor(WorkGang* workers) : _workers(workers) {}
1028 
1029   // Executes a task using worker threads.
1030   void execute(ProcessTask& task, uint ergo_workers) {
1031     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
1032 
1033     ShenandoahHeap* heap = ShenandoahHeap::heap();
1034     ShenandoahTraversalGC* traversal_gc = heap->traversal_gc();
1035     ShenandoahPushWorkerQueuesScope scope(_workers,
1036                                           traversal_gc->task_queues(),
1037                                           ergo_workers,
1038                                           /* do_check = */ false);
1039     uint nworkers = _workers->active_workers();
1040     traversal_gc->task_queues()->reserve(nworkers);
1041     ShenandoahTaskTerminator terminator(nworkers, traversal_gc->task_queues());
1042     ShenandoahTraversalRefProcTaskProxy proc_task_proxy(task, &terminator);
1043     _workers->run_task(&proc_task_proxy);
1044   }
1045 };
1046 
1047 void ShenandoahTraversalGC::weak_refs_work_doit() {
1048   ReferenceProcessor* rp = _heap->ref_processor();
1049 
1050   ShenandoahPhaseTimings::Phase phase_process = ShenandoahPhaseTimings::weakrefs_process;
1051 
1052   shenandoah_assert_rp_isalive_not_installed();
1053   ShenandoahForwardedIsAliveClosure is_alive;
1054   ReferenceProcessorIsAliveMutator fix_isalive(rp, &is_alive);
1055 
1056   WorkGang* workers = _heap->workers();
1057   uint nworkers = workers->active_workers();
1058 
1059   rp->setup_policy(_heap->soft_ref_policy()->should_clear_all_soft_refs());
1060   rp->set_active_mt_degree(nworkers);
1061 
1062   assert(task_queues()->is_empty(), "Should be empty");
1063 
1064   // complete_gc and keep_alive closures instantiated here are only needed for
1065   // single-threaded path in RP. They share the queue 0 for tracking work, which
1066   // simplifies implementation. Since RP may decide to call complete_gc several
1067   // times, we need to be able to reuse the terminator.
1068   uint serial_worker_id = 0;
1069   ShenandoahTaskTerminator terminator(1, task_queues());
1070   ShenandoahTraversalSingleThreadedDrainMarkingStackClosure complete_gc(serial_worker_id, &terminator, /* reset_terminator = */ true);
1071   ShenandoahPushWorkerQueuesScope scope(workers, task_queues(), 1, /* do_check = */ false);
1072 
1073   ShenandoahTraversalRefProcTaskExecutor executor(workers);
1074 
1075   ReferenceProcessorPhaseTimes pt(_heap->gc_timer(), rp->num_queues());
1076   if (!_heap->is_degenerated_gc_in_progress()) {
1077     ShenandoahTraversalSingleThreadKeepAliveUpdateClosure keep_alive(task_queues()->queue(serial_worker_id));
1078     rp->process_discovered_references(&is_alive, &keep_alive,
1079                                       &complete_gc, &executor,
1080                                       &pt);
1081   } else {
1082     ShenandoahTraversalSingleThreadKeepAliveUpdateDegenClosure keep_alive(task_queues()->queue(serial_worker_id));
1083     rp->process_discovered_references(&is_alive, &keep_alive,
1084                                       &complete_gc, &executor,
1085                                       &pt);
1086   }
1087 
1088   pt.print_all_references();
1089   assert(task_queues()->is_empty() || _heap->cancelled_gc(), "Should be empty");
1090 }