1 /*
   2  * Copyright (c) 2015, 2018, 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/classLoaderDataGraph.hpp"
  27 #include "classfile/stringTable.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
  31 #include "gc/shenandoah/shenandoahHeap.hpp"
  32 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  33 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  34 #include "gc/shenandoah/shenandoahTimingTracker.hpp"
  35 #include "gc/shenandoah/shenandoahUtils.hpp"
  36 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  37 #include "gc/shared/weakProcessor.hpp"
  38 #include "memory/allocation.inline.hpp"
  39 #include "memory/iterator.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "runtime/thread.hpp"
  42 #include "services/management.hpp"
  43 
  44 ShenandoahRootProcessor::ShenandoahRootProcessor(ShenandoahHeap* heap, uint n_workers,
  45                                                  ShenandoahPhaseTimings::Phase phase) :
  46   _process_strong_tasks(new SubTasksDone(SHENANDOAH_RP_PS_NumElements)),
  47   _srs(n_workers),
  48   _par_state_string(StringTable::weak_storage()),
  49   _phase(phase),
  50   _coderoots_all_iterator(ShenandoahCodeRoots::iterator())
  51 {
  52   heap->phase_timings()->record_workers_start(_phase);
  53 
  54   if (ShenandoahStringDedup::is_enabled()) {
  55     StringDedup::gc_prologue(false);
  56   }
  57 }
  58 
  59 ShenandoahRootProcessor::~ShenandoahRootProcessor() {
  60   delete _process_strong_tasks;
  61   if (ShenandoahStringDedup::is_enabled()) {
  62     StringDedup::gc_epilogue();
  63   }
  64 
  65   ShenandoahHeap::heap()->phase_timings()->record_workers_end(_phase);
  66 }
  67 
  68 void ShenandoahRootProcessor::process_all_roots_slow(OopClosure* oops) {
  69   CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
  70   CodeBlobToOopClosure blobs(oops, !CodeBlobToOopClosure::FixRelocations);
  71 
  72   CodeCache::blobs_do(&blobs);
  73   ClassLoaderDataGraph::cld_do(&clds);
  74   Universe::oops_do(oops);
  75   Management::oops_do(oops);
  76   JvmtiExport::oops_do(oops);
  77   JNIHandles::oops_do(oops);
  78   WeakProcessor::oops_do(oops);
  79   ObjectSynchronizer::oops_do(oops);
  80   SystemDictionary::oops_do(oops);
  81 
  82   if (ShenandoahStringDedup::is_enabled()) {
  83     ShenandoahStringDedup::oops_do_slow(oops);
  84   }
  85 
  86   // Do thread roots the last. This allows verification code to find
  87   // any broken objects from those special roots first, not the accidental
  88   // dangling reference from the thread root.
  89   Threads::possibly_parallel_oops_do(false, oops, &blobs);
  90 }
  91 
  92 void ShenandoahRootProcessor::process_strong_roots(OopClosure* oops,
  93                                                    OopClosure* weak_oops,
  94                                                    CLDClosure* clds,
  95                                                    CLDClosure* weak_clds,
  96                                                    CodeBlobClosure* blobs,
  97                                                    ThreadClosure* thread_cl,
  98                                                    uint worker_id) {
  99 
 100   process_java_roots(oops, clds, weak_clds, blobs, thread_cl, worker_id);
 101   process_vm_roots(oops, NULL, weak_oops, worker_id);
 102 
 103   _process_strong_tasks->all_tasks_completed(n_workers());
 104 }
 105 
 106 void ShenandoahRootProcessor::process_all_roots(OopClosure* oops,
 107                                                 OopClosure* weak_oops,
 108                                                 CLDClosure* clds,
 109                                                 CodeBlobClosure* blobs,
 110                                                 ThreadClosure* thread_cl,
 111                                                 uint worker_id) {
 112 
 113   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 114   process_java_roots(oops, clds, clds, blobs, thread_cl, worker_id);
 115   process_vm_roots(oops, oops, weak_oops, worker_id);
 116 
 117   if (blobs != NULL) {
 118     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CodeCacheRoots, worker_id);
 119     _coderoots_all_iterator.possibly_parallel_blobs_do(blobs);
 120   }
 121 
 122   _process_strong_tasks->all_tasks_completed(n_workers());
 123 }
 124 
 125 class ShenandoahParallelOopsDoThreadClosure : public ThreadClosure {
 126 private:
 127   OopClosure* _f;
 128   CodeBlobClosure* _cf;
 129   ThreadClosure* _thread_cl;
 130 public:
 131   ShenandoahParallelOopsDoThreadClosure(OopClosure* f, CodeBlobClosure* cf, ThreadClosure* thread_cl) :
 132     _f(f), _cf(cf), _thread_cl(thread_cl) {}
 133 
 134   void do_thread(Thread* t) {
 135     if (_thread_cl != NULL) {
 136       _thread_cl->do_thread(t);
 137     }
 138     t->oops_do(_f, _cf);
 139   }
 140 };
 141 
 142 void ShenandoahRootProcessor::process_java_roots(OopClosure* strong_roots,
 143                                                  CLDClosure* strong_clds,
 144                                                  CLDClosure* weak_clds,
 145                                                  CodeBlobClosure* strong_code,
 146                                                  ThreadClosure* thread_cl,
 147                                                  uint worker_id)
 148 {
 149   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 150   // Iterating over the CLDG and the Threads are done early to allow us to
 151   // first process the strong CLDs and nmethods and then, after a barrier,
 152   // let the thread process the weak CLDs and nmethods.
 153   {
 154     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CLDGRoots, worker_id);
 155     _cld_iterator.root_cld_do(strong_clds, weak_clds);
 156   }
 157 
 158   {
 159     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 160     bool is_par = n_workers() > 1;
 161     ResourceMark rm;
 162     ShenandoahParallelOopsDoThreadClosure cl(strong_roots, strong_code, thread_cl);
 163     Threads::possibly_parallel_threads_do(is_par, &cl);
 164   }
 165 }
 166 
 167 void ShenandoahRootProcessor::process_vm_roots(OopClosure* strong_roots,
 168                                                OopClosure* weak_roots,
 169                                                OopClosure* jni_weak_roots,
 170                                                uint worker_id)
 171 {
 172   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 173   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_Universe_oops_do)) {
 174     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::UniverseRoots, worker_id);
 175     Universe::oops_do(strong_roots);
 176   }
 177 
 178   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_JNIHandles_oops_do)) {
 179     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::JNIRoots, worker_id);
 180     JNIHandles::oops_do(strong_roots);
 181   }
 182   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_Management_oops_do)) {
 183     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ManagementRoots, worker_id);
 184     Management::oops_do(strong_roots);
 185   }
 186   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_jvmti_oops_do)) {
 187     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::JVMTIRoots, worker_id);
 188     JvmtiExport::oops_do(strong_roots);
 189   }
 190   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_SystemDictionary_oops_do)) {
 191     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::SystemDictionaryRoots, worker_id);
 192     SystemDictionary::oops_do(strong_roots);
 193   }
 194   if (jni_weak_roots != NULL) {
 195     if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_JNIHandles_weak_oops_do)) {
 196       ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::JNIWeakRoots, worker_id);
 197       WeakProcessor::oops_do(jni_weak_roots);
 198     }
 199   }
 200 
 201   if (ShenandoahStringDedup::is_enabled() && weak_roots != NULL) {
 202     ShenandoahStringDedup::parallel_oops_do(weak_roots, worker_id);
 203   }
 204 
 205   {
 206     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ObjectSynchronizerRoots, worker_id);
 207     if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_ObjectSynchronizer_oops_do)) {
 208       ObjectSynchronizer::oops_do(strong_roots);
 209     }
 210   }
 211 }
 212 
 213 uint ShenandoahRootProcessor::n_workers() const {
 214   return _srs.n_threads();
 215 }
 216 
 217 ShenandoahRootEvacuator::ShenandoahRootEvacuator(ShenandoahHeap* heap, uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 218   _evacuation_tasks(new SubTasksDone(SHENANDOAH_EVAC_NumElements)),
 219   _srs(n_workers),
 220   _phase(phase),
 221   _coderoots_cset_iterator(ShenandoahCodeRoots::cset_iterator())
 222 {
 223   heap->phase_timings()->record_workers_start(_phase);
 224 }
 225 
 226 ShenandoahRootEvacuator::~ShenandoahRootEvacuator() {
 227   delete _evacuation_tasks;
 228   ShenandoahHeap::heap()->phase_timings()->record_workers_end(_phase);
 229 }
 230 
 231 void ShenandoahRootEvacuator::process_evacuate_roots(OopClosure* oops,
 232                                                      CodeBlobClosure* blobs,
 233                                                      uint worker_id) {
 234 
 235   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 236   {
 237     bool is_par = n_workers() > 1;
 238     ResourceMark rm;
 239     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 240 
 241     Threads::possibly_parallel_oops_do(is_par, oops, NULL);
 242   }
 243 
 244   if (blobs != NULL) {
 245     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CodeCacheRoots, worker_id);
 246     _coderoots_cset_iterator.possibly_parallel_blobs_do(blobs);
 247   }
 248 
 249   if (_evacuation_tasks->try_claim_task(SHENANDOAH_EVAC_jvmti_oops_do)) {
 250     ShenandoahForwardedIsAliveClosure is_alive;
 251     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::JVMTIRoots, worker_id);
 252     JvmtiExport::weak_oops_do(&is_alive, oops);
 253   }
 254 }
 255 
 256 uint ShenandoahRootEvacuator::n_workers() const {
 257   return _srs.n_threads();
 258 }
 259 
 260 // Implemenation of ParallelCLDRootIterator
 261 ParallelCLDRootIterator::ParallelCLDRootIterator() {
 262   assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint");
 263   ClassLoaderDataGraph::clear_claimed_marks();
 264 }
 265 
 266 void ParallelCLDRootIterator::root_cld_do(CLDClosure* strong, CLDClosure* weak) {
 267     ClassLoaderDataGraph::roots_cld_do(strong, weak);
 268 }