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