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 void ShenandoahRootUpdater::strong_roots_do(uint worker_id, OopClosure* oops_cl) {
 207   CodeBlobToOopClosure update_blobs(oops_cl, CodeBlobToOopClosure::FixRelocations);
 208   CLDToOopClosure clds(oops_cl, ClassLoaderData::_claim_strong);
 209 
 210   _serial_roots.oops_do(oops_cl, worker_id);
 211   _vm_roots.oops_do(oops_cl, worker_id);
 212 
 213   _thread_roots.oops_do(oops_cl, NULL, worker_id);
 214   _cld_roots.cld_do(&clds, worker_id);
 215   _code_roots.code_blobs_do(&update_blobs, worker_id);
 216 }
 217 
 218 ShenandoahRootAdjuster::ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 219   ShenandoahRootProcessor(phase),
 220   _thread_roots(n_workers > 1) {
 221   assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only");
 222 }
 223 
 224 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) {
 225   CodeBlobToOopClosure code_blob_cl(oops, CodeBlobToOopClosure::FixRelocations);
 226   ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(oops);
 227   CodeBlobToOopClosure* adjust_code_closure = ShenandoahConcurrentRoots::can_do_concurrent_class_unloading() ?
 228                                               static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) :
 229                                               static_cast<CodeBlobToOopClosure*>(&code_blob_cl);
 230   CLDToOopClosure adjust_cld_closure(oops, ClassLoaderData::_claim_strong);
 231   AlwaysTrueClosure always_true;
 232 
 233   _serial_roots.oops_do(oops, worker_id);
 234   _vm_roots.oops_do(oops, worker_id);
 235 
 236   _thread_roots.oops_do(oops, NULL, worker_id);
 237   _cld_roots.cld_do(&adjust_cld_closure, worker_id);
 238   _code_roots.code_blobs_do(adjust_code_closure, worker_id);
 239 
 240   _serial_weak_roots.weak_oops_do(oops, worker_id);
 241   _weak_roots.oops_do<OopClosure>(oops, worker_id);
 242   _dedup_roots.oops_do(&always_true, oops, worker_id);
 243 }
 244 
 245  ShenandoahHeapIterationRootScanner::ShenandoahHeapIterationRootScanner() :
 246    ShenandoahRootProcessor(ShenandoahPhaseTimings::_num_phases),
 247    _thread_roots(false /*is par*/) {
 248  }
 249 
 250  void ShenandoahHeapIterationRootScanner::roots_do(OopClosure* oops) {
 251    assert(Thread::current()->is_VM_thread(), "Only by VM thread");
 252    // Must use _claim_none to avoid interfering with concurrent CLDG iteration
 253    CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
 254    MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations);
 255    ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
 256    AlwaysTrueClosure always_true;
 257    ResourceMark rm;
 258 
 259    _serial_roots.oops_do(oops, 0);
 260    _vm_roots.oops_do(oops, 0);
 261    _cld_roots.cld_do(&clds, 0);
 262    _thread_roots.threads_do(&tc_cl, 0);
 263    _code_roots.code_blobs_do(&code, 0);
 264 
 265    _serial_weak_roots.weak_oops_do(oops, 0);
 266    _weak_roots.oops_do<OopClosure>(oops, 0);
 267    _dedup_roots.oops_do(&always_true, oops, 0);
 268  }