< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahConcurrentMark.cpp

Print this page




  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/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "code/codeCache.hpp"
  29 
  30 #include "gc/shared/weakProcessor.inline.hpp"
  31 #include "gc/shared/gcTimer.hpp"
  32 #include "gc/shared/referenceProcessor.hpp"
  33 #include "gc/shared/referenceProcessorPhaseTimes.hpp"
  34 
  35 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
  36 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  37 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
  38 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
  39 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  40 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  41 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  42 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
  43 #include "gc/shenandoah/shenandoahTimingTracker.hpp"
  44 #include "gc/shenandoah/shenandoahUtils.hpp"
  45 
  46 #include "memory/iterator.inline.hpp"
  47 #include "memory/metaspace.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "runtime/handles.inline.hpp"
  51 
  52 template<UpdateRefsMode UPDATE_REFS>
  53 class ShenandoahInitMarkRootsClosure : public OopClosure {
  54 private:
  55   ShenandoahObjToScanQueue* _queue;
  56   ShenandoahHeap* _heap;


  64 public:
  65   ShenandoahInitMarkRootsClosure(ShenandoahObjToScanQueue* q) :
  66     _queue(q),
  67     _heap(ShenandoahHeap::heap()),
  68     _mark_context(_heap->marking_context()) {};
  69 
  70   void do_oop(narrowOop* p) { do_oop_work(p); }
  71   void do_oop(oop* p)       { do_oop_work(p); }
  72 };
  73 
  74 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ReferenceProcessor* rp) :
  75   MetadataVisitingOopIterateClosure(rp),
  76   _queue(q),
  77   _heap(ShenandoahHeap::heap()),
  78   _mark_context(_heap->marking_context())
  79 { }
  80 
  81 template<UpdateRefsMode UPDATE_REFS>
  82 class ShenandoahInitMarkRootsTask : public AbstractGangTask {
  83 private:
  84   ShenandoahAllRootScanner* _rp;
  85   bool _process_refs;
  86 public:
  87   ShenandoahInitMarkRootsTask(ShenandoahAllRootScanner* rp, bool process_refs) :
  88     AbstractGangTask("Shenandoah init mark roots task"),
  89     _rp(rp),
  90     _process_refs(process_refs) {
  91   }
  92 
  93   void work(uint worker_id) {
  94     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
  95     ShenandoahParallelWorkerSession worker_session(worker_id);
  96 
  97     ShenandoahHeap* heap = ShenandoahHeap::heap();
  98     ShenandoahObjToScanQueueSet* queues = heap->concurrent_mark()->task_queues();
  99     assert(queues->get_reserved() > worker_id, "Queue has not been reserved for worker id: %d", worker_id);
 100 
 101     ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 102 
 103     ShenandoahInitMarkRootsClosure<UPDATE_REFS> mark_cl(q);
 104     do_work(heap, &mark_cl, worker_id);
 105   }
 106 
 107 private:
 108   void do_work(ShenandoahHeap* heap, OopClosure* oops, uint worker_id) {
 109     // The rationale for selecting the roots to scan is as follows:
 110     //   a. With unload_classes = true, we only want to scan the actual strong roots from the
 111     //      code cache. This will allow us to identify the dead classes, unload them, *and*
 112     //      invalidate the relevant code cache blobs. This could be only done together with
 113     //      class unloading.
 114     //   b. With unload_classes = false, we have to nominally retain all the references from code
 115     //      cache, because there could be the case of embedded class/oop in the generated code,
 116     //      which we will never visit during mark. Without code cache invalidation, as in (a),
 117     //      we risk executing that code cache blob, and crashing.








 118     if (heap->unload_classes()) {
 119       _rp->strong_roots_do(worker_id, oops);
 120     } else {
 121       _rp->roots_do(worker_id, oops);














 122     }
 123   }
 124 };
 125 
 126 class ShenandoahUpdateRootsTask : public AbstractGangTask {
 127 private:
 128   ShenandoahRootUpdater*  _root_updater;

 129 public:
 130   ShenandoahUpdateRootsTask(ShenandoahRootUpdater* root_updater) :
 131     AbstractGangTask("Shenandoah update roots task"),
 132     _root_updater(root_updater) {

 133   }
 134 
 135   void work(uint worker_id) {
 136     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 137     ShenandoahParallelWorkerSession worker_session(worker_id);
 138 
 139     ShenandoahHeap* heap = ShenandoahHeap::heap();
 140     ShenandoahUpdateRefsClosure cl;
 141     AlwaysTrueClosure always_true;
 142     _root_updater->roots_do<AlwaysTrueClosure, ShenandoahUpdateRefsClosure>(worker_id, &always_true, &cl);














 143   }
 144 };
 145 
 146 class ShenandoahConcurrentMarkingTask : public AbstractGangTask {
 147 private:
 148   ShenandoahConcurrentMark* _cm;
 149   ShenandoahTaskTerminator* _terminator;
 150 
 151 public:
 152   ShenandoahConcurrentMarkingTask(ShenandoahConcurrentMark* cm, ShenandoahTaskTerminator* terminator) :
 153     AbstractGangTask("Root Region Scan"), _cm(cm), _terminator(terminator) {
 154   }
 155 
 156   void work(uint worker_id) {
 157     ShenandoahHeap* heap = ShenandoahHeap::heap();
 158     ShenandoahConcurrentWorkerSession worker_session(worker_id);
 159     ShenandoahSuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 160     ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id);
 161     ReferenceProcessor* rp;
 162     if (heap->process_references()) {


 210     // it requires a StrongRootsScope around the task, we need to claim the
 211     // threads, and performance-wise it doesn't really matter. Adds about 1ms to
 212     // full-gc.
 213     {
 214       ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id);
 215       ShenandoahSATBBufferClosure cl(q);
 216       SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 217       while (satb_mq_set.apply_closure_to_completed_buffer(&cl));
 218       ShenandoahSATBThreadsClosure tc(&cl);
 219       Threads::threads_do(&tc);
 220     }
 221 
 222     ReferenceProcessor* rp;
 223     if (heap->process_references()) {
 224       rp = heap->ref_processor();
 225       shenandoah_assert_rp_isalive_installed();
 226     } else {
 227       rp = NULL;
 228     }
 229 
 230     if (heap->is_degenerated_gc_in_progress()) {
 231       // Degenerated cycle may bypass concurrent cycle, so code roots might not be scanned,
 232       // let's check here.
 233       _cm->concurrent_scan_code_roots(worker_id, rp);
 234     }
 235 
 236     _cm->mark_loop(worker_id, _terminator, rp,
 237                    false, // not cancellable
 238                    _dedup_string);
 239 
 240     assert(_cm->task_queues()->is_empty(), "Should be empty");
 241   }
 242 };
 243 
 244 void ShenandoahConcurrentMark::mark_roots(ShenandoahPhaseTimings::Phase root_phase) {
 245   assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
 246   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 247 
 248   ShenandoahHeap* heap = ShenandoahHeap::heap();
 249 
 250   ShenandoahGCPhase phase(root_phase);
 251 
 252   WorkGang* workers = heap->workers();
 253   uint nworkers = workers->active_workers();
 254 
 255   assert(nworkers <= task_queues()->size(), "Just check");
 256 
 257   ShenandoahAllRootScanner root_proc(nworkers, root_phase);
 258   TASKQUEUE_STATS_ONLY(task_queues()->reset_taskqueue_stats());
 259   task_queues()->reserve(nworkers);
 260 
 261   if (heap->has_forwarded_objects()) {
 262     ShenandoahInitMarkRootsTask<RESOLVE> mark_roots(&root_proc, _heap->process_references());
 263     workers->run_task(&mark_roots);
 264   } else {
 265     // No need to update references, which means the heap is stable.
 266     // Can save time not walking through forwarding pointers.
 267     ShenandoahInitMarkRootsTask<NONE> mark_roots(&root_proc, _heap->process_references());
 268     workers->run_task(&mark_roots);
 269   }
 270 
 271   if (ShenandoahConcurrentScanCodeRoots) {
 272     clear_claim_codecache();
 273   }
 274 }
 275 
 276 void ShenandoahConcurrentMark::update_roots(ShenandoahPhaseTimings::Phase root_phase) {
 277   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");


 281     case ShenandoahPhaseTimings::update_roots:
 282     case ShenandoahPhaseTimings::final_update_refs_roots:
 283       update_code_cache = false;
 284       break;
 285     case ShenandoahPhaseTimings::full_gc_roots:
 286     case ShenandoahPhaseTimings::degen_gc_update_roots:
 287       update_code_cache = true;
 288       break;
 289     default:
 290       ShouldNotReachHere();
 291   }
 292 
 293   ShenandoahGCPhase phase(root_phase);
 294 
 295 #if defined(COMPILER2) || INCLUDE_JVMCI
 296   DerivedPointerTable::clear();
 297 #endif
 298 
 299   uint nworkers = _heap->workers()->active_workers();
 300 
 301   ShenandoahRootUpdater root_updater(nworkers, root_phase, update_code_cache);
 302   ShenandoahUpdateRootsTask update_roots(&root_updater);
 303   _heap->workers()->run_task(&update_roots);
 304 
 305 #if defined(COMPILER2) || INCLUDE_JVMCI
 306   DerivedPointerTable::update_pointers();
 307 #endif
 308 }
 309 
 310 class ShenandoahUpdateThreadRootsTask : public AbstractGangTask {
 311 private:
 312   ShenandoahThreadRoots           _thread_roots;
 313   ShenandoahPhaseTimings::Phase   _phase;
 314 public:
 315   ShenandoahUpdateThreadRootsTask(bool is_par, ShenandoahPhaseTimings::Phase phase) :
 316     AbstractGangTask("Shenandoah Update Thread Roots"),
 317     _thread_roots(is_par),
 318     _phase(phase) {
 319     ShenandoahHeap::heap()->phase_timings()->record_workers_start(_phase);
 320   }
 321 
 322   ~ShenandoahUpdateThreadRootsTask() {
 323     ShenandoahHeap::heap()->phase_timings()->record_workers_end(_phase);
 324   }
 325   void work(uint worker_id) {
 326     ShenandoahUpdateRefsClosure cl;
 327     _thread_roots.oops_do(&cl, NULL, worker_id);
 328   }
 329 };
 330 
 331 void ShenandoahConcurrentMark::update_thread_roots(ShenandoahPhaseTimings::Phase root_phase) {
 332   WorkGang* workers = _heap->workers();
 333   bool is_par = workers->active_workers() > 1;
 334   ShenandoahUpdateThreadRootsTask task(is_par, root_phase);
 335   workers->run_task(&task);
 336 }
 337 
 338 void ShenandoahConcurrentMark::initialize(uint workers) {
 339   _heap = ShenandoahHeap::heap();
 340 
 341   uint num_queues = MAX2(workers, 1U);
 342 
 343   _task_queues = new ShenandoahObjToScanQueueSet((int) num_queues);
 344 
 345   for (uint i = 0; i < num_queues; ++i) {
 346     ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue();
 347     task_queue->initialize();
 348     _task_queues->register_queue(i, task_queue);
 349   }
 350 }
 351 
 352 void ShenandoahConcurrentMark::concurrent_scan_code_roots(uint worker_id, ReferenceProcessor* rp) {
 353   if (ShenandoahConcurrentScanCodeRoots && claim_codecache()) {
 354     ShenandoahObjToScanQueue* q = task_queues()->queue(worker_id);
 355     if (!_heap->unload_classes()) {
 356       MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 357       // TODO: We can not honor StringDeduplication here, due to lock ranking
 358       // inversion. So, we may miss some deduplication candidates.
 359       if (_heap->has_forwarded_objects()) {
 360         ShenandoahMarkResolveRefsClosure cl(q, rp);
 361         CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations);
 362         CodeCache::blobs_do(&blobs);
 363       } else {
 364         ShenandoahMarkRefsClosure cl(q, rp);
 365         CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations);
 366         CodeCache::blobs_do(&blobs);
 367       }
 368     }
 369   }
 370 }
 371 
 372 void ShenandoahConcurrentMark::mark_from_roots() {
 373   WorkGang* workers = _heap->workers();
 374   uint nworkers = workers->active_workers();
 375 
 376   ShenandoahGCPhase conc_mark_phase(ShenandoahPhaseTimings::conc_mark);


 637   ShenandoahGCPhase phase(phase_root);
 638 
 639   ReferenceProcessor* rp = _heap->ref_processor();
 640 
 641   // NOTE: We cannot shortcut on has_discovered_references() here, because
 642   // we will miss marking JNI Weak refs then, see implementation in
 643   // ReferenceProcessor::process_discovered_references.
 644   weak_refs_work_doit(full_gc);
 645 
 646   rp->verify_no_references_recorded();
 647   assert(!rp->discovery_enabled(), "Post condition");
 648 
 649 }
 650 
 651 // Process leftover weak oops: update them, if needed or assert they do not
 652 // need updating otherwise.
 653 // Weak processor API requires us to visit the oops, even if we are not doing
 654 // anything to them.
 655 void ShenandoahConcurrentMark::weak_roots_work() {
 656   WorkGang* workers = _heap->workers();
 657   OopClosure* keep_alive = &do_nothing_cl;
 658 #ifdef ASSERT
 659   ShenandoahWeakAssertNotForwardedClosure verify_cl;
 660   keep_alive = &verify_cl;
 661 #endif
 662   ShenandoahIsAliveClosure is_alive;
 663   WeakProcessor::weak_oops_do(workers, &is_alive, keep_alive, 1);


 664 }
 665 
 666 void ShenandoahConcurrentMark::weak_refs_work_doit(bool full_gc) {
 667   ReferenceProcessor* rp = _heap->ref_processor();
 668 
 669   ShenandoahPhaseTimings::Phase phase_process =
 670           full_gc ?
 671           ShenandoahPhaseTimings::full_gc_weakrefs_process :
 672           ShenandoahPhaseTimings::weakrefs_process;
 673 
 674   ShenandoahPhaseTimings::Phase phase_process_termination =
 675           full_gc ?
 676           ShenandoahPhaseTimings::full_gc_weakrefs_termination :
 677           ShenandoahPhaseTimings::weakrefs_termination;
 678 
 679   shenandoah_assert_rp_isalive_not_installed();
 680   ShenandoahIsAliveSelector is_alive;
 681   ReferenceProcessorIsAliveMutator fix_isalive(rp, is_alive.is_alive_closure());
 682 
 683   WorkGang* workers = _heap->workers();




  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/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "code/codeCache.hpp"
  29 
  30 #include "gc/shared/weakProcessor.inline.hpp"
  31 #include "gc/shared/gcTimer.hpp"
  32 #include "gc/shared/referenceProcessor.hpp"
  33 #include "gc/shared/referenceProcessorPhaseTimes.hpp"
  34 
  35 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"

  36 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
  37 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
  38 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  39 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  40 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  41 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
  42 #include "gc/shenandoah/shenandoahTimingTracker.hpp"
  43 #include "gc/shenandoah/shenandoahUtils.hpp"
  44 
  45 #include "memory/iterator.inline.hpp"
  46 #include "memory/metaspace.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "oops/oop.inline.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 
  51 template<UpdateRefsMode UPDATE_REFS>
  52 class ShenandoahInitMarkRootsClosure : public OopClosure {
  53 private:
  54   ShenandoahObjToScanQueue* _queue;
  55   ShenandoahHeap* _heap;


  63 public:
  64   ShenandoahInitMarkRootsClosure(ShenandoahObjToScanQueue* q) :
  65     _queue(q),
  66     _heap(ShenandoahHeap::heap()),
  67     _mark_context(_heap->marking_context()) {};
  68 
  69   void do_oop(narrowOop* p) { do_oop_work(p); }
  70   void do_oop(oop* p)       { do_oop_work(p); }
  71 };
  72 
  73 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ReferenceProcessor* rp) :
  74   MetadataVisitingOopIterateClosure(rp),
  75   _queue(q),
  76   _heap(ShenandoahHeap::heap()),
  77   _mark_context(_heap->marking_context())
  78 { }
  79 
  80 template<UpdateRefsMode UPDATE_REFS>
  81 class ShenandoahInitMarkRootsTask : public AbstractGangTask {
  82 private:
  83   ShenandoahRootProcessor* _rp;
  84   bool _process_refs;
  85 public:
  86   ShenandoahInitMarkRootsTask(ShenandoahRootProcessor* rp, bool process_refs) :
  87     AbstractGangTask("Shenandoah init mark roots task"),
  88     _rp(rp),
  89     _process_refs(process_refs) {
  90   }
  91 
  92   void work(uint worker_id) {
  93     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
  94     ShenandoahParallelWorkerSession worker_session(worker_id);
  95 
  96     ShenandoahHeap* heap = ShenandoahHeap::heap();
  97     ShenandoahObjToScanQueueSet* queues = heap->concurrent_mark()->task_queues();
  98     assert(queues->get_reserved() > worker_id, "Queue has not been reserved for worker id: %d", worker_id);
  99 
 100     ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 101 
 102     ShenandoahInitMarkRootsClosure<UPDATE_REFS> mark_cl(q);
 103     do_work(heap, &mark_cl, worker_id);
 104   }
 105 
 106 private:
 107   void do_work(ShenandoahHeap* heap, OopClosure* oops, uint worker_id) {
 108     // The rationale for selecting the roots to scan is as follows:
 109     //   a. With unload_classes = true, we only want to scan the actual strong roots from the
 110     //      code cache. This will allow us to identify the dead classes, unload them, *and*
 111     //      invalidate the relevant code cache blobs. This could be only done together with
 112     //      class unloading.
 113     //   b. With unload_classes = false, we have to nominally retain all the references from code
 114     //      cache, because there could be the case of embedded class/oop in the generated code,
 115     //      which we will never visit during mark. Without code cache invalidation, as in (a),
 116     //      we risk executing that code cache blob, and crashing.
 117     //   c. With ShenandoahConcurrentScanCodeRoots, we avoid scanning the entire code cache here,
 118     //      and instead do that in concurrent phase under the relevant lock. This saves init mark
 119     //      pause time.
 120 
 121     CLDToOopClosure clds_cl(oops, ClassLoaderData::_claim_strong);
 122     MarkingCodeBlobClosure blobs_cl(oops, ! CodeBlobToOopClosure::FixRelocations);
 123 
 124     ResourceMark m;
 125     if (heap->unload_classes()) {
 126       _rp->process_strong_roots(oops, &clds_cl, &blobs_cl, NULL, worker_id);
 127     } else {
 128       if (ShenandoahConcurrentScanCodeRoots) {
 129         CodeBlobClosure* code_blobs = NULL;
 130 #ifdef ASSERT
 131         ShenandoahAssertToSpaceClosure assert_to_space_oops;
 132         CodeBlobToOopClosure assert_to_space(&assert_to_space_oops, !CodeBlobToOopClosure::FixRelocations);
 133         // If conc code cache evac is disabled, code cache should have only to-space ptrs.
 134         // Otherwise, it should have to-space ptrs only if mark does not update refs.
 135         if (!heap->has_forwarded_objects()) {
 136           code_blobs = &assert_to_space;
 137         }
 138 #endif
 139         _rp->process_all_roots(oops, &clds_cl, code_blobs, NULL, worker_id);
 140       } else {
 141         _rp->process_all_roots(oops, &clds_cl, &blobs_cl, NULL, worker_id);
 142       }
 143     }
 144   }
 145 };
 146 
 147 class ShenandoahUpdateRootsTask : public AbstractGangTask {
 148 private:
 149   ShenandoahRootProcessor* _rp;
 150   const bool _update_code_cache;
 151 public:
 152   ShenandoahUpdateRootsTask(ShenandoahRootProcessor* rp, bool update_code_cache) :
 153     AbstractGangTask("Shenandoah update roots task"),
 154     _rp(rp),
 155     _update_code_cache(update_code_cache) {
 156   }
 157 
 158   void work(uint worker_id) {
 159     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 160     ShenandoahParallelWorkerSession worker_session(worker_id);
 161 
 162     ShenandoahHeap* heap = ShenandoahHeap::heap();
 163     ShenandoahUpdateRefsClosure cl;
 164     CLDToOopClosure cldCl(&cl, ClassLoaderData::_claim_strong);
 165 
 166     CodeBlobClosure* code_blobs;
 167     CodeBlobToOopClosure update_blobs(&cl, CodeBlobToOopClosure::FixRelocations);
 168 #ifdef ASSERT
 169     ShenandoahAssertToSpaceClosure assert_to_space_oops;
 170     CodeBlobToOopClosure assert_to_space(&assert_to_space_oops, !CodeBlobToOopClosure::FixRelocations);
 171 #endif
 172     if (_update_code_cache) {
 173       code_blobs = &update_blobs;
 174     } else {
 175       code_blobs =
 176         DEBUG_ONLY(&assert_to_space)
 177         NOT_DEBUG(NULL);
 178     }
 179     _rp->update_all_roots<AlwaysTrueClosure>(&cl, &cldCl, code_blobs, NULL, worker_id);
 180   }
 181 };
 182 
 183 class ShenandoahConcurrentMarkingTask : public AbstractGangTask {
 184 private:
 185   ShenandoahConcurrentMark* _cm;
 186   ShenandoahTaskTerminator* _terminator;
 187 
 188 public:
 189   ShenandoahConcurrentMarkingTask(ShenandoahConcurrentMark* cm, ShenandoahTaskTerminator* terminator) :
 190     AbstractGangTask("Root Region Scan"), _cm(cm), _terminator(terminator) {
 191   }
 192 
 193   void work(uint worker_id) {
 194     ShenandoahHeap* heap = ShenandoahHeap::heap();
 195     ShenandoahConcurrentWorkerSession worker_session(worker_id);
 196     ShenandoahSuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 197     ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id);
 198     ReferenceProcessor* rp;
 199     if (heap->process_references()) {


 247     // it requires a StrongRootsScope around the task, we need to claim the
 248     // threads, and performance-wise it doesn't really matter. Adds about 1ms to
 249     // full-gc.
 250     {
 251       ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id);
 252       ShenandoahSATBBufferClosure cl(q);
 253       SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 254       while (satb_mq_set.apply_closure_to_completed_buffer(&cl));
 255       ShenandoahSATBThreadsClosure tc(&cl);
 256       Threads::threads_do(&tc);
 257     }
 258 
 259     ReferenceProcessor* rp;
 260     if (heap->process_references()) {
 261       rp = heap->ref_processor();
 262       shenandoah_assert_rp_isalive_installed();
 263     } else {
 264       rp = NULL;
 265     }
 266 
 267     // Degenerated cycle may bypass concurrent cycle, so code roots might not be scanned,
 268     // let's check here.
 269     _cm->concurrent_scan_code_roots(worker_id, rp);



 270     _cm->mark_loop(worker_id, _terminator, rp,
 271                    false, // not cancellable
 272                    _dedup_string);
 273 
 274     assert(_cm->task_queues()->is_empty(), "Should be empty");
 275   }
 276 };
 277 
 278 void ShenandoahConcurrentMark::mark_roots(ShenandoahPhaseTimings::Phase root_phase) {
 279   assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
 280   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 281 
 282   ShenandoahHeap* heap = ShenandoahHeap::heap();
 283 
 284   ShenandoahGCPhase phase(root_phase);
 285 
 286   WorkGang* workers = heap->workers();
 287   uint nworkers = workers->active_workers();
 288 
 289   assert(nworkers <= task_queues()->size(), "Just check");
 290 
 291   ShenandoahRootProcessor root_proc(heap, nworkers, root_phase);
 292   TASKQUEUE_STATS_ONLY(task_queues()->reset_taskqueue_stats());
 293   task_queues()->reserve(nworkers);
 294 
 295   if (heap->has_forwarded_objects()) {
 296     ShenandoahInitMarkRootsTask<RESOLVE> mark_roots(&root_proc, _heap->process_references());
 297     workers->run_task(&mark_roots);
 298   } else {
 299     // No need to update references, which means the heap is stable.
 300     // Can save time not walking through forwarding pointers.
 301     ShenandoahInitMarkRootsTask<NONE> mark_roots(&root_proc, _heap->process_references());
 302     workers->run_task(&mark_roots);
 303   }
 304 
 305   if (ShenandoahConcurrentScanCodeRoots) {
 306     clear_claim_codecache();
 307   }
 308 }
 309 
 310 void ShenandoahConcurrentMark::update_roots(ShenandoahPhaseTimings::Phase root_phase) {
 311   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");


 315     case ShenandoahPhaseTimings::update_roots:
 316     case ShenandoahPhaseTimings::final_update_refs_roots:
 317       update_code_cache = false;
 318       break;
 319     case ShenandoahPhaseTimings::full_gc_roots:
 320     case ShenandoahPhaseTimings::degen_gc_update_roots:
 321       update_code_cache = true;
 322       break;
 323     default:
 324       ShouldNotReachHere();
 325   }
 326 
 327   ShenandoahGCPhase phase(root_phase);
 328 
 329 #if defined(COMPILER2) || INCLUDE_JVMCI
 330   DerivedPointerTable::clear();
 331 #endif
 332 
 333   uint nworkers = _heap->workers()->active_workers();
 334 
 335   ShenandoahRootProcessor root_proc(_heap, nworkers, root_phase);
 336   ShenandoahUpdateRootsTask update_roots(&root_proc, update_code_cache);
 337   _heap->workers()->run_task(&update_roots);
 338 
 339 #if defined(COMPILER2) || INCLUDE_JVMCI
 340   DerivedPointerTable::update_pointers();
 341 #endif
 342 }
 343 




























 344 void ShenandoahConcurrentMark::initialize(uint workers) {
 345   _heap = ShenandoahHeap::heap();
 346 
 347   uint num_queues = MAX2(workers, 1U);
 348 
 349   _task_queues = new ShenandoahObjToScanQueueSet((int) num_queues);
 350 
 351   for (uint i = 0; i < num_queues; ++i) {
 352     ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue();
 353     task_queue->initialize();
 354     _task_queues->register_queue(i, task_queue);
 355   }
 356 }
 357 
 358 void ShenandoahConcurrentMark::concurrent_scan_code_roots(uint worker_id, ReferenceProcessor* rp) {
 359   if (ShenandoahConcurrentScanCodeRoots && claim_codecache()) {
 360     ShenandoahObjToScanQueue* q = task_queues()->queue(worker_id);
 361     if (!_heap->unload_classes()) {
 362       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 363       // TODO: We can not honor StringDeduplication here, due to lock ranking
 364       // inversion. So, we may miss some deduplication candidates.
 365       if (_heap->has_forwarded_objects()) {
 366         ShenandoahMarkResolveRefsClosure cl(q, rp);
 367         CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations);
 368         CodeCache::blobs_do(&blobs);
 369       } else {
 370         ShenandoahMarkRefsClosure cl(q, rp);
 371         CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations);
 372         CodeCache::blobs_do(&blobs);
 373       }
 374     }
 375   }
 376 }
 377 
 378 void ShenandoahConcurrentMark::mark_from_roots() {
 379   WorkGang* workers = _heap->workers();
 380   uint nworkers = workers->active_workers();
 381 
 382   ShenandoahGCPhase conc_mark_phase(ShenandoahPhaseTimings::conc_mark);


 643   ShenandoahGCPhase phase(phase_root);
 644 
 645   ReferenceProcessor* rp = _heap->ref_processor();
 646 
 647   // NOTE: We cannot shortcut on has_discovered_references() here, because
 648   // we will miss marking JNI Weak refs then, see implementation in
 649   // ReferenceProcessor::process_discovered_references.
 650   weak_refs_work_doit(full_gc);
 651 
 652   rp->verify_no_references_recorded();
 653   assert(!rp->discovery_enabled(), "Post condition");
 654 
 655 }
 656 
 657 // Process leftover weak oops: update them, if needed or assert they do not
 658 // need updating otherwise.
 659 // Weak processor API requires us to visit the oops, even if we are not doing
 660 // anything to them.
 661 void ShenandoahConcurrentMark::weak_roots_work() {
 662   WorkGang* workers = _heap->workers();
 663   ShenandoahIsAliveSelector is_alive;
 664 
 665   if (_heap->has_forwarded_objects()) {
 666     ShenandoahWeakUpdateClosure cl;
 667     WeakProcessor::weak_oops_do(workers, is_alive.is_alive_closure(), &cl, 1);
 668   } else {
 669     ShenandoahWeakAssertNotForwardedClosure cl;
 670     WeakProcessor::weak_oops_do(workers, is_alive.is_alive_closure(), &cl, 1);
 671   }
 672 }
 673 
 674 void ShenandoahConcurrentMark::weak_refs_work_doit(bool full_gc) {
 675   ReferenceProcessor* rp = _heap->ref_processor();
 676 
 677   ShenandoahPhaseTimings::Phase phase_process =
 678           full_gc ?
 679           ShenandoahPhaseTimings::full_gc_weakrefs_process :
 680           ShenandoahPhaseTimings::weakrefs_process;
 681 
 682   ShenandoahPhaseTimings::Phase phase_process_termination =
 683           full_gc ?
 684           ShenandoahPhaseTimings::full_gc_weakrefs_termination :
 685           ShenandoahPhaseTimings::weakrefs_termination;
 686 
 687   shenandoah_assert_rp_isalive_not_installed();
 688   ShenandoahIsAliveSelector is_alive;
 689   ReferenceProcessorIsAliveMutator fix_isalive(rp, is_alive.is_alive_closure());
 690 
 691   WorkGang* workers = _heap->workers();


< prev index next >