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