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.inline.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   _weak_processor_task(n_workers)
  52 {
  53   heap->phase_timings()->record_workers_start(_phase);
  54 
  55   if (ShenandoahStringDedup::is_enabled()) {
  56     StringDedup::gc_prologue(false);
  57   }
  58 }
  59 
  60 ShenandoahRootProcessor::~ShenandoahRootProcessor() {
  61   delete _process_strong_tasks;
  62   if (ShenandoahStringDedup::is_enabled()) {
  63     StringDedup::gc_epilogue();
  64   }
  65 
  66   ShenandoahHeap::heap()->phase_timings()->record_workers_end(_phase);
  67 }
  68 
  69 void ShenandoahRootProcessor::process_all_roots_slow(OopClosure* oops) {
  70   CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
  71   CodeBlobToOopClosure blobs(oops, !CodeBlobToOopClosure::FixRelocations);
  72 
  73   CodeCache::blobs_do(&blobs);
  74   ClassLoaderDataGraph::cld_do(&clds);
  75   Universe::oops_do(oops);
  76   Management::oops_do(oops);
  77   JvmtiExport::oops_do(oops);
  78   JNIHandles::oops_do(oops);
  79   WeakProcessor::oops_do(oops);
  80   ObjectSynchronizer::oops_do(oops);
  81   SystemDictionary::oops_do(oops);
  82 
  83   if (ShenandoahStringDedup::is_enabled()) {
  84     ShenandoahStringDedup::oops_do_slow(oops);
  85   }
  86 
  87   // Do thread roots the last. This allows verification code to find
  88   // any broken objects from those special roots first, not the accidental
  89   // dangling reference from the thread root.
  90   Threads::possibly_parallel_oops_do(false, oops, &blobs);
  91 }
  92 
  93 void ShenandoahRootProcessor::process_strong_roots(OopClosure* oops,
  94                                                    OopClosure* weak_oops,
  95                                                    CLDClosure* clds,
  96                                                    CLDClosure* weak_clds,
  97                                                    CodeBlobClosure* blobs,
  98                                                    ThreadClosure* thread_cl,
  99                                                    uint worker_id) {
 100 
 101   process_java_roots(oops, clds, weak_clds, blobs, thread_cl, worker_id);
 102   process_vm_roots(oops, NULL, weak_oops, worker_id);
 103 
 104   _process_strong_tasks->all_tasks_completed(n_workers());
 105 }
 106 
 107 void ShenandoahRootProcessor::process_all_roots(OopClosure* oops,
 108                                                 OopClosure* weak_oops,
 109                                                 CLDClosure* clds,
 110                                                 CodeBlobClosure* blobs,
 111                                                 ThreadClosure* thread_cl,
 112                                                 uint worker_id) {
 113 
 114   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 115   process_java_roots(oops, clds, clds, blobs, thread_cl, worker_id);
 116   process_vm_roots(oops, oops, weak_oops, worker_id);
 117 
 118   if (blobs != NULL) {
 119     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CodeCacheRoots, worker_id);
 120     _coderoots_all_iterator.possibly_parallel_blobs_do(blobs);
 121   }
 122 
 123   _process_strong_tasks->all_tasks_completed(n_workers());
 124 }
 125 
 126 class ShenandoahParallelOopsDoThreadClosure : public ThreadClosure {
 127 private:
 128   OopClosure* _f;
 129   CodeBlobClosure* _cf;
 130   ThreadClosure* _thread_cl;
 131 public:
 132   ShenandoahParallelOopsDoThreadClosure(OopClosure* f, CodeBlobClosure* cf, ThreadClosure* thread_cl) :
 133     _f(f), _cf(cf), _thread_cl(thread_cl) {}
 134 
 135   void do_thread(Thread* t) {
 136     if (_thread_cl != NULL) {
 137       _thread_cl->do_thread(t);
 138     }
 139     t->oops_do(_f, _cf);
 140   }
 141 };
 142 
 143 void ShenandoahRootProcessor::process_java_roots(OopClosure* strong_roots,
 144                                                  CLDClosure* strong_clds,
 145                                                  CLDClosure* weak_clds,
 146                                                  CodeBlobClosure* strong_code,
 147                                                  ThreadClosure* thread_cl,
 148                                                  uint worker_id)
 149 {
 150   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 151   // Iterating over the CLDG and the Threads are done early to allow us to
 152   // first process the strong CLDs and nmethods and then, after a barrier,
 153   // let the thread process the weak CLDs and nmethods.
 154   {
 155     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CLDGRoots, worker_id);
 156     _cld_iterator.root_cld_do(strong_clds, weak_clds);
 157   }
 158 
 159   {
 160     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 161     bool is_par = n_workers() > 1;
 162     ResourceMark rm;
 163     ShenandoahParallelOopsDoThreadClosure cl(strong_roots, strong_code, thread_cl);
 164     Threads::possibly_parallel_threads_do(is_par, &cl);
 165   }
 166 }
 167 
 168 void ShenandoahRootProcessor::process_vm_roots(OopClosure* strong_roots,
 169                                                OopClosure* weak_roots,
 170                                                OopClosure* jni_weak_roots,
 171                                                uint worker_id)
 172 {
 173   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 174   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_Universe_oops_do)) {
 175     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::UniverseRoots, worker_id);
 176     Universe::oops_do(strong_roots);
 177   }
 178 
 179   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_JNIHandles_oops_do)) {
 180     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::JNIRoots, worker_id);
 181     JNIHandles::oops_do(strong_roots);
 182   }
 183   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_Management_oops_do)) {
 184     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ManagementRoots, worker_id);
 185     Management::oops_do(strong_roots);
 186   }
 187   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_jvmti_oops_do)) {
 188     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::JVMTIRoots, worker_id);
 189     JvmtiExport::oops_do(strong_roots);
 190   }
 191   if (_process_strong_tasks->try_claim_task(SHENANDOAH_RP_PS_SystemDictionary_oops_do)) {
 192     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::SystemDictionaryRoots, worker_id);
 193     SystemDictionary::oops_do(strong_roots);
 194   }
 195   if (jni_weak_roots != NULL) {
 196       ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::JNIWeakRoots, worker_id);
 197       AlwaysTrueClosure always_true;
 198       _weak_processor_task.work<AlwaysTrueClosure, OopClosure>(worker_id, &always_true, jni_weak_roots);
 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 }