1 /*
   2  * Copyright (c) 2015, 2019, 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/shenandoahClosures.inline.hpp"
  31 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp"
  32 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  33 #include "gc/shenandoah/shenandoahHeap.hpp"
  34 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  35 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  36 #include "gc/shenandoah/shenandoahTimingTracker.hpp"
  37 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  38 #include "jfr/jfr.hpp"
  39 #include "memory/iterator.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "memory/universe.hpp"
  42 #include "runtime/thread.hpp"
  43 #include "services/management.hpp"
  44 
  45 ShenandoahSerialRoot::ShenandoahSerialRoot(ShenandoahSerialRoot::OopsDo oops_do, ShenandoahPhaseTimings::GCParPhases phase) :
  46   _oops_do(oops_do), _phase(phase) {
  47 }
  48 
  49 void ShenandoahSerialRoot::oops_do(OopClosure* cl, uint worker_id) {
  50   if (_claimed.try_set()) {
  51     ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  52     ShenandoahWorkerTimingsTracker timer(worker_times, _phase, worker_id);
  53     _oops_do(cl);
  54   }
  55 }
  56 
  57 // Overwrite the second argument for SD::oops_do, don't include vm global oop storage.
  58 static void system_dictionary_oops_do(OopClosure* cl) {
  59   SystemDictionary::oops_do(cl, false);
  60 }
  61 
  62 ShenandoahSerialRoots::ShenandoahSerialRoots() :
  63   _universe_root(&Universe::oops_do, ShenandoahPhaseTimings::UniverseRoots),
  64   _object_synchronizer_root(&ObjectSynchronizer::oops_do, ShenandoahPhaseTimings::ObjectSynchronizerRoots),
  65   _management_root(&Management::oops_do, ShenandoahPhaseTimings::ManagementRoots),
  66   _system_dictionary_root(&system_dictionary_oops_do, ShenandoahPhaseTimings::SystemDictionaryRoots),
  67   _jvmti_root(&JvmtiExport::oops_do, ShenandoahPhaseTimings::JVMTIRoots) {
  68 }
  69 
  70 void ShenandoahSerialRoots::oops_do(OopClosure* cl, uint worker_id) {
  71   _universe_root.oops_do(cl, worker_id);
  72   _object_synchronizer_root.oops_do(cl, worker_id);
  73   _management_root.oops_do(cl, worker_id);
  74   _system_dictionary_root.oops_do(cl, worker_id);
  75   _jvmti_root.oops_do(cl, worker_id);
  76 }
  77 
  78 ShenandoahWeakSerialRoot::ShenandoahWeakSerialRoot(ShenandoahWeakSerialRoot::WeakOopsDo weak_oops_do, ShenandoahPhaseTimings::GCParPhases phase) :
  79   _weak_oops_do(weak_oops_do), _phase(phase) {
  80 }
  81 
  82 void ShenandoahWeakSerialRoot::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
  83   if (_claimed.try_set()) {
  84     ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  85     ShenandoahWorkerTimingsTracker timer(worker_times, _phase, worker_id);
  86     _weak_oops_do(is_alive, keep_alive);
  87   }
  88 }
  89 
  90 #if INCLUDE_JVMTI
  91 ShenandoahJVMTIWeakRoot::ShenandoahJVMTIWeakRoot() :
  92   ShenandoahWeakSerialRoot(&JvmtiExport::weak_oops_do, ShenandoahPhaseTimings::JVMTIWeakRoots) {
  93 }
  94 #endif // INCLUDE_JVMTI
  95 
  96 #if INCLUDE_JFR
  97 ShenandoahJFRWeakRoot::ShenandoahJFRWeakRoot() :
  98   ShenandoahWeakSerialRoot(&Jfr::weak_oops_do, ShenandoahPhaseTimings::JFRWeakRoots) {
  99 }
 100 #endif // INCLUDE_JFR
 101 
 102 void ShenandoahSerialWeakRoots::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
 103   JVMTI_ONLY(_jvmti_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);)
 104   JFR_ONLY(_jfr_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);)
 105 }
 106 
 107 void ShenandoahSerialWeakRoots::weak_oops_do(OopClosure* cl, uint worker_id) {
 108   AlwaysTrueClosure always_true;
 109   weak_oops_do(&always_true, cl, worker_id);
 110 }
 111 
 112 ShenandoahThreadRoots::ShenandoahThreadRoots(bool is_par) : _is_par(is_par) {
 113   Threads::change_thread_claim_token();
 114 }
 115 
 116 void ShenandoahThreadRoots::oops_do(OopClosure* oops_cl, CodeBlobClosure* code_cl, uint worker_id) {
 117   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 118   ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 119   ResourceMark rm;
 120   Threads::possibly_parallel_oops_do(_is_par, oops_cl, code_cl);
 121 }
 122 
 123 void ShenandoahThreadRoots::threads_do(ThreadClosure* tc, uint worker_id) {
 124   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 125   ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 126   ResourceMark rm;
 127   Threads::possibly_parallel_threads_do(_is_par, tc);
 128 }
 129 
 130 ShenandoahThreadRoots::~ShenandoahThreadRoots() {
 131   Threads::assert_all_threads_claimed();
 132 }
 133 
 134 ShenandoahStringDedupRoots::ShenandoahStringDedupRoots() {
 135   if (ShenandoahStringDedup::is_enabled()) {
 136     StringDedup::gc_prologue(false);
 137   }
 138 }
 139 
 140 ShenandoahStringDedupRoots::~ShenandoahStringDedupRoots() {
 141   if (ShenandoahStringDedup::is_enabled()) {
 142     StringDedup::gc_epilogue();
 143   }
 144 }
 145 
 146 void ShenandoahStringDedupRoots::oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
 147   if (ShenandoahStringDedup::is_enabled()) {
 148     ShenandoahStringDedup::parallel_oops_do(is_alive, keep_alive, worker_id);
 149   }
 150 }
 151 
 152 ShenandoahRootProcessor::ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase) :
 153   _heap(ShenandoahHeap::heap()),
 154   _phase(phase) {
 155   assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint");
 156   _heap->phase_timings()->record_workers_start(_phase);
 157 }
 158 
 159 ShenandoahRootProcessor::~ShenandoahRootProcessor() {
 160   assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint");
 161   _heap->phase_timings()->record_workers_end(_phase);
 162 }
 163 
 164 ShenandoahRootEvacuator::ShenandoahRootEvacuator(uint n_workers,
 165                                                  ShenandoahPhaseTimings::Phase phase,
 166                                                  bool include_concurrent_roots,
 167                                                  bool include_concurrent_code_roots) :
 168   ShenandoahRootProcessor(phase),
 169   _thread_roots(n_workers > 1),
 170   _include_concurrent_roots(include_concurrent_roots),
 171   _include_concurrent_code_roots(include_concurrent_code_roots) {
 172 }
 173 
 174 void ShenandoahRootEvacuator::roots_do(uint worker_id, OopClosure* oops) {
 175   MarkingCodeBlobClosure blobsCl(oops, CodeBlobToOopClosure::FixRelocations);
 176   ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(oops);
 177   CodeBlobToOopClosure* codes_cl = ShenandoahConcurrentRoots::can_do_concurrent_class_unloading() ?
 178                                    static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) :
 179                                    static_cast<CodeBlobToOopClosure*>(&blobsCl);
 180   AlwaysTrueClosure always_true;
 181 
 182   _serial_roots.oops_do(oops, worker_id);
 183   _serial_weak_roots.weak_oops_do(oops, worker_id);
 184   if (_include_concurrent_roots) {
 185     CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
 186     _vm_roots.oops_do<OopClosure>(oops, worker_id);
 187     _cld_roots.cld_do(&clds, worker_id);
 188     _weak_roots.oops_do<OopClosure>(oops, worker_id);
 189   }
 190 
 191   if (_include_concurrent_code_roots) {
 192     _code_roots.code_blobs_do(codes_cl, worker_id);
 193     _thread_roots.oops_do(oops, NULL, worker_id);
 194   } else {
 195     _thread_roots.oops_do(oops, codes_cl, worker_id);
 196   }
 197 
 198   _dedup_roots.oops_do(&always_true, oops, worker_id);
 199 }
 200 
 201 ShenandoahRootUpdater::ShenandoahRootUpdater(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 202   ShenandoahRootProcessor(phase),
 203   _thread_roots(n_workers > 1) {
 204 }
 205 
 206 ShenandoahRootAdjuster::ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 207   ShenandoahRootProcessor(phase),
 208   _thread_roots(n_workers > 1) {
 209   assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only");
 210 }
 211 
 212 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) {
 213   CodeBlobToOopClosure code_blob_cl(oops, CodeBlobToOopClosure::FixRelocations);
 214   ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(oops);
 215   CodeBlobToOopClosure* adjust_code_closure = ShenandoahConcurrentRoots::can_do_concurrent_class_unloading() ?
 216                                               static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) :
 217                                               static_cast<CodeBlobToOopClosure*>(&code_blob_cl);
 218   CLDToOopClosure adjust_cld_closure(oops, ClassLoaderData::_claim_strong);
 219   AlwaysTrueClosure always_true;
 220 
 221   _serial_roots.oops_do(oops, worker_id);
 222   _vm_roots.oops_do(oops, worker_id);
 223 
 224   _thread_roots.oops_do(oops, NULL, worker_id);
 225   _cld_roots.cld_do(&adjust_cld_closure, worker_id);
 226   _code_roots.code_blobs_do(adjust_code_closure, worker_id);
 227 
 228   _serial_weak_roots.weak_oops_do(oops, worker_id);
 229   _weak_roots.oops_do<OopClosure>(oops, worker_id);
 230   _dedup_roots.oops_do(&always_true, oops, worker_id);
 231 }
 232 
 233  ShenandoahHeapIterationRootScanner::ShenandoahHeapIterationRootScanner() :
 234    ShenandoahRootProcessor(ShenandoahPhaseTimings::_num_phases),
 235    _thread_roots(false /*is par*/) {
 236  }
 237 
 238  void ShenandoahHeapIterationRootScanner::roots_do(OopClosure* oops) {
 239    assert(Thread::current()->is_VM_thread(), "Only by VM thread");
 240    // Must use _claim_none to avoid interfering with concurrent CLDG iteration
 241    CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
 242    MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations);
 243    ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
 244    ResourceMark rm;
 245 
 246    _serial_roots.oops_do(oops, 0);
 247    _vm_roots.oops_do(oops, 0);
 248    _cld_roots.cld_do(&clds, 0);
 249    _thread_roots.threads_do(&tc_cl, 0);
 250    _code_roots.code_blobs_do(&code, 0);
 251  }
 252 
 253  void ShenandoahHeapIterationRootScanner::strong_roots_do(OopClosure* oops) {
 254    assert(Thread::current()->is_VM_thread(), "Only by VM thread");
 255    // Must use _claim_none to avoid interfering with concurrent CLDG iteration
 256    CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
 257    MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations);
 258    ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
 259    ResourceMark rm;
 260 
 261    _serial_roots.oops_do(oops, 0);
 262    _vm_roots.oops_do(oops, 0);
 263    _cld_roots.always_strong_cld_do(&clds, 0);
 264    _thread_roots.threads_do(&tc_cl, 0);
 265  }