1 /*
   2  * Copyright (c) 2018, 2020, 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/shenandoah/shenandoahBarrierSet.hpp"
  32 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  33 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  34 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  35 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  36 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  37 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  38 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  39 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
  40 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  41 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  42 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  43 #include "gc/shenandoah/shenandoahPhaseTimings.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/shenandoahTraversalGC.hpp"
  48 #include "gc/shenandoah/shenandoahUtils.hpp"
  49 #include "gc/shenandoah/shenandoahVerifier.hpp"
  50 
  51 #include "memory/iterator.hpp"
  52 #include "memory/metaspace.hpp"
  53 #include "memory/resourceArea.hpp"
  54 #include "memory/universe.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, ClassLoaderData::_claim_strong, 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, ClassLoaderData::_claim_strong, true);
 156     }
 157   }
 158 };
 159 
 160 class ShenandoahInitTraversalCollectionTask : public AbstractGangTask {
 161 private:
 162   ShenandoahCSetRootScanner* _rp;
 163   ShenandoahHeap* _heap;
 164 
 165 public:
 166   ShenandoahInitTraversalCollectionTask(ShenandoahCSetRootScanner* rp) :
 167     AbstractGangTask("Shenandoah Init Traversal Collection"),
 168     _rp(rp),
 169     _heap(ShenandoahHeap::heap()) {}
 170 
 171   void work(uint worker_id) {
 172     ShenandoahParallelWorkerSession worker_session(worker_id);
 173 
 174     ShenandoahObjToScanQueueSet* queues = _heap->traversal_gc()->task_queues();
 175     ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 176 
 177     bool process_refs = _heap->process_references();
 178     bool unload_classes = _heap->unload_classes();
 179     ReferenceProcessor* rp = NULL;
 180     if (process_refs) {
 181       rp = _heap->ref_processor();
 182     }
 183 
 184     // Step 1: Process ordinary GC roots.
 185     {
 186       ShenandoahTraversalRootsClosure roots_cl(q, rp);
 187       ShenandoahMarkCLDClosure cld_cl(&roots_cl);
 188       MarkingCodeBlobClosure code_cl(&roots_cl, CodeBlobToOopClosure::FixRelocations);
 189       if (unload_classes) {
 190         _rp->roots_do(worker_id, &roots_cl, NULL, &code_cl);
 191       } else {
 192         _rp->roots_do(worker_id, &roots_cl, &cld_cl, &code_cl);
 193       }
 194     }
 195   }
 196 };
 197 
 198 class ShenandoahConcurrentTraversalCollectionTask : public AbstractGangTask {
 199 private:
 200   ShenandoahTaskTerminator* _terminator;
 201   ShenandoahHeap* _heap;
 202 public:
 203   ShenandoahConcurrentTraversalCollectionTask(ShenandoahTaskTerminator* terminator) :
 204     AbstractGangTask("Shenandoah Concurrent Traversal Collection"),
 205     _terminator(terminator),
 206     _heap(ShenandoahHeap::heap()) {}
 207 
 208   void work(uint worker_id) {
 209     ShenandoahConcurrentWorkerSession worker_session(worker_id);
 210     ShenandoahSuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 211     ShenandoahTraversalGC* traversal_gc = _heap->traversal_gc();
 212 
 213     // Drain all outstanding work in queues.
 214     traversal_gc->main_loop(worker_id, _terminator, true);
 215   }
 216 };
 217 
 218 class ShenandoahFinalTraversalCollectionTask : public AbstractGangTask {
 219 private:
 220   ShenandoahAllRootScanner* _rp;
 221   ShenandoahTaskTerminator* _terminator;
 222   ShenandoahHeap* _heap;
 223 public:
 224   ShenandoahFinalTraversalCollectionTask(ShenandoahAllRootScanner* rp, ShenandoahTaskTerminator* terminator) :
 225     AbstractGangTask("Shenandoah Final Traversal Collection"),
 226     _rp(rp),
 227     _terminator(terminator),
 228     _heap(ShenandoahHeap::heap()) {}
 229 
 230   void work(uint worker_id) {
 231     ShenandoahParallelWorkerSession worker_session(worker_id);
 232 
 233     ShenandoahTraversalGC* traversal_gc = _heap->traversal_gc();
 234 
 235     ShenandoahObjToScanQueueSet* queues = traversal_gc->task_queues();
 236     ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 237 
 238     bool process_refs = _heap->process_references();
 239     bool unload_classes = _heap->unload_classes();
 240     ReferenceProcessor* rp = NULL;
 241     if (process_refs) {
 242       rp = _heap->ref_processor();
 243     }
 244 
 245     // Step 0: Drain outstanding SATB queues.
 246     // NOTE: we piggy-back draining of remaining thread SATB buffers on the final root scan below.
 247     ShenandoahTraversalSATBBufferClosure satb_cl(q);
 248     {
 249       // Process remaining finished SATB buffers.
 250       SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 251       while (satb_mq_set.apply_closure_to_completed_buffer(&satb_cl));
 252       // Process remaining threads SATB buffers below.
 253     }
 254 
 255     // Step 1: Process GC roots.
 256     // For oops in code roots, they are marked, evacuated, enqueued for further traversal,
 257     // and the references to the oops are updated during init pause. We only need to rescan
 258     // on stack code roots, in case of class unloading is enabled. Otherwise, code roots are
 259     // scanned during init traversal or degenerated GC will update them at the end.
 260     if (!_heap->is_degenerated_gc_in_progress()) {
 261       ShenandoahTraversalRootsClosure roots_cl(q, rp);
 262       ShenandoahTraversalSATBThreadsClosure tc(&satb_cl);
 263       if (unload_classes) {
 264         ShenandoahRemarkCLDClosure remark_cld_cl(&roots_cl);
 265         MarkingCodeBlobClosure code_cl(&roots_cl, CodeBlobToOopClosure::FixRelocations);
 266         _rp->strong_roots_do(worker_id, &roots_cl, &remark_cld_cl, &code_cl, &tc);
 267       } else {
 268         CLDToOopClosure cld_cl(&roots_cl, ClassLoaderData::_claim_strong);
 269         _rp->roots_do(worker_id, &roots_cl, &cld_cl, NULL, &tc);
 270       }
 271     } else {
 272       ShenandoahTraversalDegenClosure roots_cl(q, rp);
 273       ShenandoahTraversalSATBThreadsClosure tc(&satb_cl);
 274       if (unload_classes) {
 275         ShenandoahRemarkCLDClosure remark_cld_cl(&roots_cl);
 276         _rp->strong_roots_do(worker_id, &roots_cl, &remark_cld_cl, NULL, &tc);
 277       } else {
 278         CLDToOopClosure cld_cl(&roots_cl, ClassLoaderData::_claim_strong);
 279         _rp->roots_do(worker_id, &roots_cl, &cld_cl, NULL, &tc);
 280       }
 281     }
 282 
 283     {
 284       ShenandoahWorkerTimingsTracker timer(ShenandoahPhaseTimings::FinishQueues, worker_id);
 285 
 286       // Step 3: Finally drain all outstanding work in queues.
 287       traversal_gc->main_loop(worker_id, _terminator, false);
 288     }
 289 
 290   }
 291 };
 292 
 293 ShenandoahTraversalGC::ShenandoahTraversalGC(ShenandoahHeap* heap, size_t num_regions) :
 294   _heap(heap),
 295   _task_queues(new ShenandoahObjToScanQueueSet(heap->max_workers())),
 296   _traversal_set(ShenandoahHeapRegionSet()) {
 297 
 298   // Traversal does not support concurrent code root scanning
 299   FLAG_SET_DEFAULT(ShenandoahConcurrentScanCodeRoots, false);
 300 
 301   uint num_queues = heap->max_workers();
 302   for (uint i = 0; i < num_queues; ++i) {
 303     ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue();
 304     task_queue->initialize();
 305     _task_queues->register_queue(i, task_queue);
 306   }
 307 }
 308 
 309 ShenandoahTraversalGC::~ShenandoahTraversalGC() {
 310 }
 311 
 312 void ShenandoahTraversalGC::prepare_regions() {
 313   size_t num_regions = _heap->num_regions();
 314   ShenandoahMarkingContext* const ctx = _heap->marking_context();
 315   for (size_t i = 0; i < num_regions; i++) {
 316     ShenandoahHeapRegion* region = _heap->get_region(i);
 317     if (_heap->is_bitmap_slice_committed(region)) {
 318       if (_traversal_set.is_in(i)) {
 319         ctx->capture_top_at_mark_start(region);
 320         region->clear_live_data();
 321         assert(ctx->is_bitmap_clear_range(region->bottom(), region->end()), "bitmap for traversal regions must be cleared");
 322       } else {
 323         // Everything outside the traversal set is always considered live.
 324         ctx->reset_top_at_mark_start(region);
 325       }
 326     } else {
 327       // FreeSet may contain uncommitted empty regions, once they are recommitted,
 328       // their TAMS may have old values, so reset them here.
 329       ctx->reset_top_at_mark_start(region);
 330     }
 331   }
 332 }
 333 
 334 void ShenandoahTraversalGC::prepare() {
 335   {
 336     ShenandoahGCPhase phase(ShenandoahPhaseTimings::traversal_gc_make_parsable);
 337     _heap->make_parsable(true);
 338   }
 339 
 340   if (UseTLAB) {
 341     ShenandoahGCPhase phase(ShenandoahPhaseTimings::traversal_gc_resize_tlabs);
 342     _heap->resize_tlabs();
 343   }
 344 
 345   assert(_heap->marking_context()->is_bitmap_clear(), "need clean mark bitmap");
 346   assert(!_heap->marking_context()->is_complete(), "should not be complete");
 347 
 348   // About to choose the collection set, make sure we know which regions are pinned.
 349   {
 350     ShenandoahGCPhase phase_cleanup(ShenandoahPhaseTimings::traversal_gc_prepare_sync_pinned);
 351     _heap->sync_pinned_region_status();
 352   }
 353 
 354   ShenandoahCollectionSet* collection_set = _heap->collection_set();
 355   {
 356     ShenandoahHeapLocker lock(_heap->lock());
 357 
 358     collection_set->clear();
 359     assert(collection_set->count() == 0, "collection set not clear");
 360 
 361     // Find collection set
 362     _heap->heuristics()->choose_collection_set(collection_set);
 363     prepare_regions();
 364 
 365     // Rebuild free set
 366     _heap->free_set()->rebuild();
 367   }
 368 
 369   log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s, " SIZE_FORMAT "%s CSet, " SIZE_FORMAT " CSet regions",
 370                      byte_size_in_proper_unit(collection_set->garbage()),   proper_unit_for_byte_size(collection_set->garbage()),
 371                      byte_size_in_proper_unit(collection_set->live_data()), proper_unit_for_byte_size(collection_set->live_data()),
 372                      collection_set->count());
 373 }
 374 
 375 void ShenandoahTraversalGC::init_traversal_collection() {
 376   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "STW traversal GC");
 377 
 378   if (ShenandoahVerify) {
 379     _heap->verifier()->verify_before_traversal();
 380   }
 381 
 382   if (VerifyBeforeGC) {
 383     Universe::verify();
 384   }
 385 
 386   {
 387     ShenandoahGCPhase phase_prepare(ShenandoahPhaseTimings::traversal_gc_prepare);
 388     prepare();
 389   }
 390 
 391   _heap->set_concurrent_traversal_in_progress(true);
 392   _heap->set_has_forwarded_objects(true);
 393 
 394   bool process_refs = _heap->process_references();
 395   if (process_refs) {
 396     ReferenceProcessor* rp = _heap->ref_processor();
 397     rp->enable_discovery(true /*verify_no_refs*/);
 398     rp->setup_policy(_heap->soft_ref_policy()->should_clear_all_soft_refs());
 399   }
 400 
 401   {
 402     ShenandoahGCPhase phase_work(ShenandoahPhaseTimings::init_traversal_gc_work);
 403     assert(_task_queues->is_empty(), "queues must be empty before traversal GC");
 404     TASKQUEUE_STATS_ONLY(_task_queues->reset_taskqueue_stats());
 405 
 406 #if COMPILER2_OR_JVMCI
 407     DerivedPointerTable::clear();
 408 #endif
 409 
 410     {
 411       uint nworkers = _heap->workers()->active_workers();
 412       task_queues()->reserve(nworkers);
 413       ShenandoahCSetRootScanner rp(nworkers, ShenandoahPhaseTimings::init_traversal_gc_work);
 414       ShenandoahInitTraversalCollectionTask traversal_task(&rp);
 415       _heap->workers()->run_task(&traversal_task);
 416     }
 417 
 418 #if COMPILER2_OR_JVMCI
 419     DerivedPointerTable::update_pointers();
 420 #endif
 421   }
 422 
 423   if (ShenandoahPacing) {
 424     _heap->pacer()->setup_for_traversal();
 425   }
 426 }
 427 
 428 void ShenandoahTraversalGC::main_loop(uint w, ShenandoahTaskTerminator* t, bool sts_yield) {
 429   ShenandoahObjToScanQueue* q = task_queues()->queue(w);
 430 
 431   // Initialize live data.
 432   jushort* ld = _heap->get_liveness_cache(w);
 433 
 434   ReferenceProcessor* rp = NULL;
 435   if (_heap->process_references()) {
 436     rp = _heap->ref_processor();
 437   }
 438   {
 439     if (!_heap->is_degenerated_gc_in_progress()) {
 440       if (_heap->unload_classes()) {
 441         if (ShenandoahStringDedup::is_enabled()) {
 442           ShenandoahTraversalMetadataDedupClosure cl(q, rp);
 443           main_loop_work<ShenandoahTraversalMetadataDedupClosure>(&cl, ld, w, t, sts_yield);
 444         } else {
 445           ShenandoahTraversalMetadataClosure cl(q, rp);
 446           main_loop_work<ShenandoahTraversalMetadataClosure>(&cl, ld, w, t, sts_yield);
 447         }
 448       } else {
 449         if (ShenandoahStringDedup::is_enabled()) {
 450           ShenandoahTraversalDedupClosure cl(q, rp);
 451           main_loop_work<ShenandoahTraversalDedupClosure>(&cl, ld, w, t, sts_yield);
 452         } else {
 453           ShenandoahTraversalClosure cl(q, rp);
 454           main_loop_work<ShenandoahTraversalClosure>(&cl, ld, w, t, sts_yield);
 455         }
 456       }
 457     } else {
 458       if (_heap->unload_classes()) {
 459         if (ShenandoahStringDedup::is_enabled()) {
 460           ShenandoahTraversalMetadataDedupDegenClosure cl(q, rp);
 461           main_loop_work<ShenandoahTraversalMetadataDedupDegenClosure>(&cl, ld, w, t, sts_yield);
 462         } else {
 463           ShenandoahTraversalMetadataDegenClosure cl(q, rp);
 464           main_loop_work<ShenandoahTraversalMetadataDegenClosure>(&cl, ld, w, t, sts_yield);
 465         }
 466       } else {
 467         if (ShenandoahStringDedup::is_enabled()) {
 468           ShenandoahTraversalDedupDegenClosure cl(q, rp);
 469           main_loop_work<ShenandoahTraversalDedupDegenClosure>(&cl, ld, w, t, sts_yield);
 470         } else {
 471           ShenandoahTraversalDegenClosure cl(q, rp);
 472           main_loop_work<ShenandoahTraversalDegenClosure>(&cl, ld, w, t, sts_yield);
 473         }
 474       }
 475     }
 476   }
 477 
 478   _heap->flush_liveness_cache(w);
 479 }
 480 
 481 template <class T>
 482 void ShenandoahTraversalGC::main_loop_work(T* cl, jushort* live_data, uint worker_id, ShenandoahTaskTerminator* terminator, bool sts_yield) {
 483   ShenandoahObjToScanQueueSet* queues = task_queues();
 484   ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 485   ShenandoahConcurrentMark* conc_mark = _heap->concurrent_mark();
 486 
 487   uintx stride = ShenandoahMarkLoopStride;
 488 
 489   ShenandoahMarkTask task;
 490 
 491   // Process outstanding queues, if any.
 492   q = queues->claim_next();
 493   while (q != NULL) {
 494     if (_heap->check_cancelled_gc_and_yield(sts_yield)) {
 495       return;
 496     }
 497 
 498     for (uint i = 0; i < stride; i++) {
 499       if (q->pop(task)) {
 500         conc_mark->do_task<T>(q, cl, live_data, &task);
 501       } else {
 502         assert(q->is_empty(), "Must be empty");
 503         q = queues->claim_next();
 504         break;
 505       }
 506     }
 507   }
 508 
 509   if (check_and_handle_cancelled_gc(terminator, sts_yield)) return;
 510 
 511   // Normal loop.
 512   q = queues->queue(worker_id);
 513 
 514   ShenandoahTraversalSATBBufferClosure drain_satb(q);
 515   SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 516 
 517   while (true) {
 518     if (check_and_handle_cancelled_gc(terminator, sts_yield)) return;
 519 
 520     while (satb_mq_set.completed_buffers_num() > 0) {
 521       satb_mq_set.apply_closure_to_completed_buffer(&drain_satb);
 522     }
 523 
 524     uint work = 0;
 525     for (uint i = 0; i < stride; i++) {
 526       if (q->pop(task) ||
 527           queues->steal(worker_id, task)) {
 528         conc_mark->do_task<T>(q, cl, live_data, &task);
 529         work++;
 530       } else {
 531         break;
 532       }
 533     }
 534 
 535     if (work == 0) {
 536       // No more work, try to terminate
 537       ShenandoahSuspendibleThreadSetLeaver stsl(sts_yield && ShenandoahSuspendibleWorkers);
 538       ShenandoahTerminatorTerminator tt(_heap);
 539 
 540       if (terminator->offer_termination(&tt)) return;
 541     }
 542   }
 543 }
 544 
 545 bool ShenandoahTraversalGC::check_and_handle_cancelled_gc(ShenandoahTaskTerminator* terminator, bool sts_yield) {
 546   if (_heap->cancelled_gc()) {
 547     return true;
 548   }
 549   return false;
 550 }
 551 
 552 void ShenandoahTraversalGC::concurrent_traversal_collection() {
 553   ShenandoahGCPhase phase_work(ShenandoahPhaseTimings::conc_traversal);
 554   if (!_heap->cancelled_gc()) {
 555     uint nworkers = _heap->workers()->active_workers();
 556     task_queues()->reserve(nworkers);
 557 
 558     ShenandoahTaskTerminator terminator(nworkers, task_queues());
 559     ShenandoahConcurrentTraversalCollectionTask task(&terminator);
 560     _heap->workers()->run_task(&task);
 561   }
 562 
 563   if (!_heap->cancelled_gc() && ShenandoahPreclean && _heap->process_references()) {
 564     preclean_weak_refs();
 565   }
 566 }
 567 
 568 void ShenandoahTraversalGC::final_traversal_collection() {
 569   if (!_heap->cancelled_gc()) {
 570 #if COMPILER2_OR_JVMCI
 571     DerivedPointerTable::clear();
 572 #endif
 573     ShenandoahGCPhase phase_work(ShenandoahPhaseTimings::final_traversal_gc_work);
 574     uint nworkers = _heap->workers()->active_workers();
 575     task_queues()->reserve(nworkers);
 576 
 577     // Finish traversal
 578     ShenandoahAllRootScanner rp(nworkers, ShenandoahPhaseTimings::final_traversal_gc_work);
 579     ShenandoahTaskTerminator terminator(nworkers, task_queues());
 580     ShenandoahFinalTraversalCollectionTask task(&rp, &terminator);
 581     _heap->workers()->run_task(&task);
 582 #if COMPILER2_OR_JVMCI
 583     DerivedPointerTable::update_pointers();
 584 #endif
 585   }
 586 
 587   if (!_heap->cancelled_gc() && _heap->process_references()) {
 588     weak_refs_work();
 589   }
 590 
 591   if (!_heap->cancelled_gc()) {
 592     assert(_task_queues->is_empty(), "queues must be empty after traversal GC");
 593     TASKQUEUE_STATS_ONLY(_task_queues->print_taskqueue_stats());
 594     TASKQUEUE_STATS_ONLY(_task_queues->reset_taskqueue_stats());
 595 
 596     // No more marking expected
 597     _heap->set_concurrent_traversal_in_progress(false);
 598     _heap->mark_complete_marking_context();
 599 
 600     // A rare case, TLAB/GCLAB is initialized from an empty region without
 601     // any live data, the region can be trashed and may be uncommitted in later code,
 602     // that results the TLAB/GCLAB not usable. Retire them here.
 603     _heap->make_parsable(true);
 604 
 605     _heap->parallel_cleaning(false);
 606     fixup_roots();
 607 
 608     _heap->set_has_forwarded_objects(false);
 609 
 610     // Resize metaspace
 611     MetaspaceGC::compute_new_size();
 612 
 613     // Need to see that pinned region status is updated: newly pinned regions must not
 614     // be trashed. New unpinned regions should be trashed.
 615     {
 616       ShenandoahGCPhase phase_cleanup(ShenandoahPhaseTimings::traversal_gc_sync_pinned);
 617       _heap->sync_pinned_region_status();
 618     }
 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     assert(!_heap->cancelled_gc(), "must not be cancelled when getting out here");
 663 
 664     if (ShenandoahVerify) {
 665       _heap->verifier()->verify_after_traversal();
 666     }
 667 #ifdef ASSERT
 668     else {
 669       verify_roots_after_gc();
 670     }
 671 #endif
 672 
 673     if (VerifyAfterGC) {
 674       Universe::verify();
 675     }
 676   }
 677 }
 678 
 679 class ShenandoahVerifyAfterGC : public OopClosure {
 680 private:
 681   template <class T>
 682   void do_oop_work(T* p) {
 683     T o = RawAccess<>::oop_load(p);
 684     if (!CompressedOops::is_null(o)) {
 685       oop obj = CompressedOops::decode_not_null(o);
 686       shenandoah_assert_correct(p, obj);
 687       shenandoah_assert_not_in_cset_except(p, obj, ShenandoahHeap::heap()->cancelled_gc());
 688       shenandoah_assert_not_forwarded(p, obj);
 689     }
 690   }
 691 
 692 public:
 693   void do_oop(narrowOop* p) { do_oop_work(p); }
 694   void do_oop(oop* p)       { do_oop_work(p); }
 695 };
 696 
 697 void ShenandoahTraversalGC::verify_roots_after_gc() {
 698   ShenandoahRootVerifier verifier;
 699   ShenandoahVerifyAfterGC cl;
 700   verifier.oops_do(&cl);
 701 }
 702 
 703 class ShenandoahTraversalFixRootsClosure : public OopClosure {
 704 private:
 705   template <class T>
 706   inline void do_oop_work(T* p) {
 707     T o = RawAccess<>::oop_load(p);
 708     if (!CompressedOops::is_null(o)) {
 709       oop obj = CompressedOops::decode_not_null(o);
 710       oop forw = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 711       if (obj != forw) {
 712         RawAccess<IS_NOT_NULL>::oop_store(p, forw);
 713       }
 714     }
 715   }
 716 
 717 public:
 718   inline void do_oop(oop* p) { do_oop_work(p); }
 719   inline void do_oop(narrowOop* p) { do_oop_work(p); }
 720 };
 721 
 722 class ShenandoahTraversalFixRootsTask : public AbstractGangTask {
 723 private:
 724   ShenandoahRootUpdater* _rp;
 725 
 726 public:
 727   ShenandoahTraversalFixRootsTask(ShenandoahRootUpdater* rp) :
 728     AbstractGangTask("Shenandoah traversal fix roots"),
 729     _rp(rp) {
 730     assert(ShenandoahHeap::heap()->has_forwarded_objects(), "Must be");
 731   }
 732 
 733   void work(uint worker_id) {
 734     ShenandoahParallelWorkerSession worker_session(worker_id);
 735     ShenandoahTraversalFixRootsClosure cl;
 736     ShenandoahForwardedIsAliveClosure is_alive;
 737     _rp->roots_do(worker_id, &is_alive, &cl);
 738   }
 739 };
 740 
 741 void ShenandoahTraversalGC::fixup_roots() {
 742 #if COMPILER2_OR_JVMCI
 743   DerivedPointerTable::clear();
 744 #endif
 745   ShenandoahRootUpdater rp(_heap->workers()->active_workers(), ShenandoahPhaseTimings::final_traversal_update_roots);
 746   ShenandoahTraversalFixRootsTask update_roots_task(&rp);
 747   _heap->workers()->run_task(&update_roots_task);
 748 #if COMPILER2_OR_JVMCI
 749   DerivedPointerTable::update_pointers();
 750 #endif
 751 }
 752 
 753 void ShenandoahTraversalGC::reset() {
 754   _task_queues->clear();
 755 }
 756 
 757 ShenandoahObjToScanQueueSet* ShenandoahTraversalGC::task_queues() {
 758   return _task_queues;
 759 }
 760 
 761 class ShenandoahTraversalCancelledGCYieldClosure : public YieldClosure {
 762 private:
 763   ShenandoahHeap* const _heap;
 764 public:
 765   ShenandoahTraversalCancelledGCYieldClosure() : _heap(ShenandoahHeap::heap()) {};
 766   virtual bool should_return() { return _heap->cancelled_gc(); }
 767 };
 768 
 769 class ShenandoahTraversalPrecleanCompleteGCClosure : public VoidClosure {
 770 public:
 771   void do_void() {
 772     ShenandoahHeap* sh = ShenandoahHeap::heap();
 773     ShenandoahTraversalGC* traversal_gc = sh->traversal_gc();
 774     assert(sh->process_references(), "why else would we be here?");
 775     ShenandoahTaskTerminator terminator(1, traversal_gc->task_queues());
 776     shenandoah_assert_rp_isalive_installed();
 777     traversal_gc->main_loop((uint) 0, &terminator, true);
 778   }
 779 };
 780 
 781 class ShenandoahTraversalKeepAliveUpdateClosure : public OopClosure {
 782 private:
 783   ShenandoahObjToScanQueue* _queue;
 784   Thread* _thread;
 785   ShenandoahTraversalGC* _traversal_gc;
 786   ShenandoahMarkingContext* const _mark_context;
 787 
 788   template <class T>
 789   inline void do_oop_work(T* p) {
 790     _traversal_gc->process_oop<T, false /* string dedup */, false /* degen */, true /* atomic update */>(p, _thread, _queue, _mark_context);
 791   }
 792 
 793 public:
 794   ShenandoahTraversalKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) :
 795     _queue(q), _thread(Thread::current()),
 796     _traversal_gc(ShenandoahHeap::heap()->traversal_gc()),
 797     _mark_context(ShenandoahHeap::heap()->marking_context()) {}
 798 
 799   void do_oop(narrowOop* p) { do_oop_work(p); }
 800   void do_oop(oop* p)       { do_oop_work(p); }
 801 };
 802 
 803 class ShenandoahTraversalKeepAliveUpdateDegenClosure : public OopClosure {
 804 private:
 805   ShenandoahObjToScanQueue* _queue;
 806   Thread* _thread;
 807   ShenandoahTraversalGC* _traversal_gc;
 808   ShenandoahMarkingContext* const _mark_context;
 809 
 810   template <class T>
 811   inline void do_oop_work(T* p) {
 812     _traversal_gc->process_oop<T, false /* string dedup */, true /* degen */, false /* atomic update */>(p, _thread, _queue, _mark_context);
 813   }
 814 
 815 public:
 816   ShenandoahTraversalKeepAliveUpdateDegenClosure(ShenandoahObjToScanQueue* q) :
 817           _queue(q), _thread(Thread::current()),
 818           _traversal_gc(ShenandoahHeap::heap()->traversal_gc()),
 819           _mark_context(ShenandoahHeap::heap()->marking_context()) {}
 820 
 821   void do_oop(narrowOop* p) { do_oop_work(p); }
 822   void do_oop(oop* p)       { do_oop_work(p); }
 823 };
 824 
 825 class ShenandoahTraversalSingleThreadKeepAliveUpdateClosure : public OopClosure {
 826 private:
 827   ShenandoahObjToScanQueue* _queue;
 828   Thread* _thread;
 829   ShenandoahTraversalGC* _traversal_gc;
 830   ShenandoahMarkingContext* const _mark_context;
 831 
 832   template <class T>
 833   inline void do_oop_work(T* p) {
 834     _traversal_gc->process_oop<T, false /* string dedup */, false /* degen */, true /* atomic update */>(p, _thread, _queue, _mark_context);
 835   }
 836 
 837 public:
 838   ShenandoahTraversalSingleThreadKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) :
 839           _queue(q), _thread(Thread::current()),
 840           _traversal_gc(ShenandoahHeap::heap()->traversal_gc()),
 841           _mark_context(ShenandoahHeap::heap()->marking_context()) {}
 842 
 843   void do_oop(narrowOop* p) { do_oop_work(p); }
 844   void do_oop(oop* p)       { do_oop_work(p); }
 845 };
 846 
 847 class ShenandoahTraversalSingleThreadKeepAliveUpdateDegenClosure : public OopClosure {
 848 private:
 849   ShenandoahObjToScanQueue* _queue;
 850   Thread* _thread;
 851   ShenandoahTraversalGC* _traversal_gc;
 852   ShenandoahMarkingContext* const _mark_context;
 853 
 854   template <class T>
 855   inline void do_oop_work(T* p) {
 856     _traversal_gc->process_oop<T, false /* string dedup */, true /* degen */, false /* atomic update */>(p, _thread, _queue, _mark_context);
 857   }
 858 
 859 public:
 860   ShenandoahTraversalSingleThreadKeepAliveUpdateDegenClosure(ShenandoahObjToScanQueue* q) :
 861           _queue(q), _thread(Thread::current()),
 862           _traversal_gc(ShenandoahHeap::heap()->traversal_gc()),
 863           _mark_context(ShenandoahHeap::heap()->marking_context()) {}
 864 
 865   void do_oop(narrowOop* p) { do_oop_work(p); }
 866   void do_oop(oop* p)       { do_oop_work(p); }
 867 };
 868 
 869 class ShenandoahTraversalPrecleanTask : public AbstractGangTask {
 870 private:
 871   ReferenceProcessor* _rp;
 872 
 873 public:
 874   ShenandoahTraversalPrecleanTask(ReferenceProcessor* rp) :
 875           AbstractGangTask("Precleaning task"),
 876           _rp(rp) {}
 877 
 878   void work(uint worker_id) {
 879     assert(worker_id == 0, "The code below is single-threaded, only one worker is expected");
 880     ShenandoahParallelWorkerSession worker_session(worker_id);
 881     ShenandoahSuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 882 
 883     ShenandoahHeap* sh = ShenandoahHeap::heap();
 884 
 885     ShenandoahObjToScanQueue* q = sh->traversal_gc()->task_queues()->queue(worker_id);
 886 
 887     ShenandoahForwardedIsAliveClosure is_alive;
 888     ShenandoahTraversalCancelledGCYieldClosure yield;
 889     ShenandoahTraversalPrecleanCompleteGCClosure complete_gc;
 890     ShenandoahTraversalKeepAliveUpdateClosure keep_alive(q);
 891     ResourceMark rm;
 892     _rp->preclean_discovered_references(&is_alive, &keep_alive,
 893                                         &complete_gc, &yield,
 894                                         NULL);
 895   }
 896 };
 897 
 898 void ShenandoahTraversalGC::preclean_weak_refs() {
 899   // Pre-cleaning weak references before diving into STW makes sense at the
 900   // end of concurrent mark. This will filter out the references which referents
 901   // are alive. Note that ReferenceProcessor already filters out these on reference
 902   // discovery, and the bulk of work is done here. This phase processes leftovers
 903   // that missed the initial filtering, i.e. when referent was marked alive after
 904   // reference was discovered by RP.
 905 
 906   assert(_heap->process_references(), "sanity");
 907   assert(!_heap->is_degenerated_gc_in_progress(), "must be in concurrent non-degenerated phase");
 908 
 909   // Shortcut if no references were discovered to avoid winding up threads.
 910   ReferenceProcessor* rp = _heap->ref_processor();
 911   if (!rp->has_discovered_references()) {
 912     return;
 913   }
 914 
 915   ReferenceProcessorMTDiscoveryMutator fix_mt_discovery(rp, false);
 916 
 917   shenandoah_assert_rp_isalive_not_installed();
 918   ShenandoahForwardedIsAliveClosure is_alive;
 919   ReferenceProcessorIsAliveMutator fix_isalive(rp, &is_alive);
 920 
 921   assert(task_queues()->is_empty(), "Should be empty");
 922 
 923   // Execute precleaning in the worker thread: it will give us GCLABs, String dedup
 924   // queues and other goodies. When upstream ReferenceProcessor starts supporting
 925   // parallel precleans, we can extend this to more threads.
 926   ShenandoahPushWorkerScope scope(_heap->workers(), 1, /* check_workers = */ false);
 927 
 928   WorkGang* workers = _heap->workers();
 929   uint nworkers = workers->active_workers();
 930   assert(nworkers == 1, "This code uses only a single worker");
 931   task_queues()->reserve(nworkers);
 932 
 933   ShenandoahTraversalPrecleanTask task(rp);
 934   workers->run_task(&task);
 935 
 936   assert(_heap->cancelled_gc() || task_queues()->is_empty(), "Should be empty");
 937 }
 938 
 939 // Weak Reference Closures
 940 class ShenandoahTraversalDrainMarkingStackClosure: public VoidClosure {
 941   uint _worker_id;
 942   ShenandoahTaskTerminator* _terminator;
 943   bool _reset_terminator;
 944 
 945 public:
 946   ShenandoahTraversalDrainMarkingStackClosure(uint worker_id, ShenandoahTaskTerminator* t, bool reset_terminator = false):
 947     _worker_id(worker_id),
 948     _terminator(t),
 949     _reset_terminator(reset_terminator) {
 950   }
 951 
 952   void do_void() {
 953     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 954 
 955     ShenandoahHeap* sh = ShenandoahHeap::heap();
 956     ShenandoahTraversalGC* traversal_gc = sh->traversal_gc();
 957     assert(sh->process_references(), "why else would we be here?");
 958     shenandoah_assert_rp_isalive_installed();
 959 
 960     traversal_gc->main_loop(_worker_id, _terminator, false);
 961 
 962     if (_reset_terminator) {
 963       _terminator->reset_for_reuse();
 964     }
 965   }
 966 };
 967 
 968 class ShenandoahTraversalSingleThreadedDrainMarkingStackClosure: public VoidClosure {
 969   uint _worker_id;
 970   ShenandoahTaskTerminator* _terminator;
 971   bool _reset_terminator;
 972 
 973 public:
 974   ShenandoahTraversalSingleThreadedDrainMarkingStackClosure(uint worker_id, ShenandoahTaskTerminator* t, bool reset_terminator = false):
 975           _worker_id(worker_id),
 976           _terminator(t),
 977           _reset_terminator(reset_terminator) {
 978   }
 979 
 980   void do_void() {
 981     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 982 
 983     ShenandoahHeap* sh = ShenandoahHeap::heap();
 984     ShenandoahTraversalGC* traversal_gc = sh->traversal_gc();
 985     assert(sh->process_references(), "why else would we be here?");
 986     shenandoah_assert_rp_isalive_installed();
 987 
 988     traversal_gc->main_loop(_worker_id, _terminator, false);
 989 
 990     if (_reset_terminator) {
 991       _terminator->reset_for_reuse();
 992     }
 993   }
 994 };
 995 
 996 void ShenandoahTraversalGC::weak_refs_work() {
 997   assert(_heap->process_references(), "sanity");
 998 
 999   ShenandoahPhaseTimings::Phase phase_root = ShenandoahPhaseTimings::weakrefs;
1000 
1001   ShenandoahGCPhase phase(phase_root);
1002 
1003   ReferenceProcessor* rp = _heap->ref_processor();
1004 
1005   // NOTE: We cannot shortcut on has_discovered_references() here, because
1006   // we will miss marking JNI Weak refs then, see implementation in
1007   // ReferenceProcessor::process_discovered_references.
1008   weak_refs_work_doit();
1009 
1010   rp->verify_no_references_recorded();
1011   assert(!rp->discovery_enabled(), "Post condition");
1012 
1013 }
1014 
1015 class ShenandoahTraversalRefProcTaskProxy : public AbstractGangTask {
1016 private:
1017   AbstractRefProcTaskExecutor::ProcessTask& _proc_task;
1018   ShenandoahTaskTerminator* _terminator;
1019 
1020 public:
1021   ShenandoahTraversalRefProcTaskProxy(AbstractRefProcTaskExecutor::ProcessTask& proc_task,
1022                                       ShenandoahTaskTerminator* t) :
1023     AbstractGangTask("Process reference objects in parallel"),
1024     _proc_task(proc_task),
1025     _terminator(t) {
1026   }
1027 
1028   void work(uint worker_id) {
1029     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
1030     ShenandoahHeap* heap = ShenandoahHeap::heap();
1031     ShenandoahTraversalDrainMarkingStackClosure complete_gc(worker_id, _terminator);
1032 
1033     ShenandoahForwardedIsAliveClosure is_alive;
1034     if (!heap->is_degenerated_gc_in_progress()) {
1035       ShenandoahTraversalKeepAliveUpdateClosure keep_alive(heap->traversal_gc()->task_queues()->queue(worker_id));
1036       _proc_task.work(worker_id, is_alive, keep_alive, complete_gc);
1037     } else {
1038       ShenandoahTraversalKeepAliveUpdateDegenClosure keep_alive(heap->traversal_gc()->task_queues()->queue(worker_id));
1039       _proc_task.work(worker_id, is_alive, keep_alive, complete_gc);
1040     }
1041   }
1042 };
1043 
1044 class ShenandoahTraversalRefProcTaskExecutor : public AbstractRefProcTaskExecutor {
1045 private:
1046   WorkGang* _workers;
1047 
1048 public:
1049   ShenandoahTraversalRefProcTaskExecutor(WorkGang* workers) : _workers(workers) {}
1050 
1051   // Executes a task using worker threads.
1052   void execute(ProcessTask& task, uint ergo_workers) {
1053     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
1054 
1055     ShenandoahHeap* heap = ShenandoahHeap::heap();
1056     ShenandoahTraversalGC* traversal_gc = heap->traversal_gc();
1057     ShenandoahPushWorkerQueuesScope scope(_workers,
1058                                           traversal_gc->task_queues(),
1059                                           ergo_workers,
1060                                           /* do_check = */ false);
1061     uint nworkers = _workers->active_workers();
1062     traversal_gc->task_queues()->reserve(nworkers);
1063     ShenandoahTaskTerminator terminator(nworkers, traversal_gc->task_queues());
1064     ShenandoahTraversalRefProcTaskProxy proc_task_proxy(task, &terminator);
1065     _workers->run_task(&proc_task_proxy);
1066   }
1067 };
1068 
1069 void ShenandoahTraversalGC::weak_refs_work_doit() {
1070   ReferenceProcessor* rp = _heap->ref_processor();
1071 
1072   ShenandoahPhaseTimings::Phase phase_process = ShenandoahPhaseTimings::weakrefs_process;
1073 
1074   shenandoah_assert_rp_isalive_not_installed();
1075   ShenandoahForwardedIsAliveClosure is_alive;
1076   ReferenceProcessorIsAliveMutator fix_isalive(rp, &is_alive);
1077 
1078   WorkGang* workers = _heap->workers();
1079   uint nworkers = workers->active_workers();
1080 
1081   rp->setup_policy(_heap->soft_ref_policy()->should_clear_all_soft_refs());
1082   rp->set_active_mt_degree(nworkers);
1083 
1084   assert(task_queues()->is_empty(), "Should be empty");
1085 
1086   // complete_gc and keep_alive closures instantiated here are only needed for
1087   // single-threaded path in RP. They share the queue 0 for tracking work, which
1088   // simplifies implementation. Since RP may decide to call complete_gc several
1089   // times, we need to be able to reuse the terminator.
1090   uint serial_worker_id = 0;
1091   ShenandoahTaskTerminator terminator(1, task_queues());
1092   ShenandoahTraversalSingleThreadedDrainMarkingStackClosure complete_gc(serial_worker_id, &terminator, /* reset_terminator = */ true);
1093   ShenandoahPushWorkerQueuesScope scope(workers, task_queues(), 1, /* do_check = */ false);
1094 
1095   ShenandoahTraversalRefProcTaskExecutor executor(workers);
1096 
1097   ReferenceProcessorPhaseTimes pt(_heap->gc_timer(), rp->num_queues());
1098   if (!_heap->is_degenerated_gc_in_progress()) {
1099     ShenandoahTraversalSingleThreadKeepAliveUpdateClosure keep_alive(task_queues()->queue(serial_worker_id));
1100     rp->process_discovered_references(&is_alive, &keep_alive,
1101                                       &complete_gc, &executor,
1102                                       &pt);
1103   } else {
1104     ShenandoahTraversalSingleThreadKeepAliveUpdateDegenClosure keep_alive(task_queues()->queue(serial_worker_id));
1105     rp->process_discovered_references(&is_alive, &keep_alive,
1106                                       &complete_gc, &executor,
1107                                       &pt);
1108   }
1109 
1110   pt.print_all_references();
1111   assert(task_queues()->is_empty() || _heap->cancelled_gc(), "Should be empty");
1112 }