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.inline.hpp"
  34 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  35 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  36 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  37 #include "jfr/jfr.hpp"
  38 #include "memory/iterator.hpp"
  39 #include "memory/resourceArea.hpp"
  40 #include "memory/universe.hpp"
  41 #include "runtime/thread.hpp"
  42 #include "services/management.hpp"
  43 
  44 ShenandoahSerialRoot::ShenandoahSerialRoot(ShenandoahSerialRoot::OopsDo oops_do,
  45   ShenandoahPhaseTimings::Phase phase, ShenandoahPhaseTimings::ParPhase par_phase) :
  46   _oops_do(oops_do), _phase(phase), _par_phase(par_phase) {
  47 }
  48 
  49 void ShenandoahSerialRoot::oops_do(OopClosure* cl, uint worker_id) {
  50   if (_claimed.try_set()) {
  51     ShenandoahWorkerTimingsTracker timer(_phase, _par_phase, worker_id);
  52     _oops_do(cl);
  53   }
  54 }
  55 
  56 // Overwrite the second argument for SD::oops_do, don't include vm global oop storage.
  57 static void system_dictionary_oops_do(OopClosure* cl) {
  58   SystemDictionary::oops_do(cl, false);
  59 }
  60 
  61 ShenandoahSerialRoots::ShenandoahSerialRoots(ShenandoahPhaseTimings::Phase phase) :
  62   _universe_root(&Universe::oops_do, phase, ShenandoahPhaseTimings::UniverseRoots),
  63   _object_synchronizer_root(&ObjectSynchronizer::oops_do, phase, ShenandoahPhaseTimings::ObjectSynchronizerRoots),
  64   _management_root(&Management::oops_do, phase, ShenandoahPhaseTimings::ManagementRoots),
  65   _system_dictionary_root(&system_dictionary_oops_do, phase, ShenandoahPhaseTimings::SystemDictionaryRoots),
  66   _jvmti_root(&JvmtiExport::oops_do, phase, ShenandoahPhaseTimings::JVMTIRoots) {
  67 }
  68 
  69 void ShenandoahSerialRoots::oops_do(OopClosure* cl, uint worker_id) {
  70   _universe_root.oops_do(cl, worker_id);
  71   _object_synchronizer_root.oops_do(cl, worker_id);
  72   _management_root.oops_do(cl, worker_id);
  73   _system_dictionary_root.oops_do(cl, worker_id);
  74   _jvmti_root.oops_do(cl, worker_id);
  75 }
  76 
  77 ShenandoahWeakSerialRoot::ShenandoahWeakSerialRoot(ShenandoahWeakSerialRoot::WeakOopsDo weak_oops_do,
  78   ShenandoahPhaseTimings::Phase phase, ShenandoahPhaseTimings::ParPhase par_phase) :
  79   _weak_oops_do(weak_oops_do), _phase(phase), _par_phase(par_phase) {
  80 }
  81 
  82 void ShenandoahWeakSerialRoot::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
  83   if (_claimed.try_set()) {
  84     ShenandoahWorkerTimingsTracker timer(_phase, _par_phase, worker_id);
  85     _weak_oops_do(is_alive, keep_alive);
  86   }
  87 }
  88 
  89 #if INCLUDE_JVMTI
  90 ShenandoahJVMTIWeakRoot::ShenandoahJVMTIWeakRoot(ShenandoahPhaseTimings::Phase phase) :
  91   ShenandoahWeakSerialRoot(&JvmtiExport::weak_oops_do, phase, ShenandoahPhaseTimings::JVMTIWeakRoots) {
  92 }
  93 #endif // INCLUDE_JVMTI
  94 
  95 #if INCLUDE_JFR
  96 ShenandoahJFRWeakRoot::ShenandoahJFRWeakRoot(ShenandoahPhaseTimings::Phase phase) :
  97   ShenandoahWeakSerialRoot(&Jfr::weak_oops_do, phase, ShenandoahPhaseTimings::JFRWeakRoots) {
  98 }
  99 #endif // INCLUDE_JFR
 100 
 101 void ShenandoahSerialWeakRoots::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
 102   JVMTI_ONLY(_jvmti_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);)
 103   JFR_ONLY(_jfr_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);)
 104 }
 105 
 106 void ShenandoahSerialWeakRoots::weak_oops_do(OopClosure* cl, uint worker_id) {
 107   AlwaysTrueClosure always_true;
 108   weak_oops_do(&always_true, cl, worker_id);
 109 }
 110 
 111 ShenandoahThreadRoots::ShenandoahThreadRoots(ShenandoahPhaseTimings::Phase phase, bool is_par) :
 112   _phase(phase), _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   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 118   ResourceMark rm;
 119   Threads::possibly_parallel_oops_do(_is_par, oops_cl, code_cl);
 120 }
 121 
 122 void ShenandoahThreadRoots::threads_do(ThreadClosure* tc, uint worker_id) {
 123   ShenandoahWorkerTimingsTracker timer(_phase, 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(ShenandoahPhaseTimings::Phase phase) : _phase(phase) {
 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(_phase, is_alive, keep_alive, worker_id);
 147   }
 148 }
 149 
 150 ShenandoahRootProcessor::ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase) :
 151   _heap(ShenandoahHeap::heap()),
 152   _phase(phase),
 153   _worker_phase(phase) {
 154   assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint");
 155 }
 156 
 157 ShenandoahRootEvacuator::ShenandoahRootEvacuator(uint n_workers,
 158                                                  ShenandoahPhaseTimings::Phase phase,
 159                                                  bool stw_roots_processing,
 160                                                  bool stw_class_unloading) :
 161   ShenandoahRootProcessor(phase),
 162   _serial_roots(phase),
 163   _vm_roots(phase),
 164   _cld_roots(phase),
 165   _thread_roots(phase, n_workers > 1),
 166   _serial_weak_roots(phase),
 167   _weak_roots(phase),
 168   _dedup_roots(phase),
 169   _code_roots(phase),
 170   _stw_roots_processing(stw_roots_processing),
 171   _stw_class_unloading(stw_class_unloading) {
 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 (_stw_roots_processing) {
 185     _vm_roots.oops_do<OopClosure>(oops, worker_id);
 186     _weak_roots.oops_do<OopClosure>(oops, worker_id);
 187   }
 188 
 189   if (_stw_class_unloading) {
 190     CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
 191     _cld_roots.cld_do(&clds, worker_id);
 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   _serial_roots(phase),
 204   _vm_roots(phase),
 205   _cld_roots(phase),
 206   _thread_roots(phase, n_workers > 1),
 207   _serial_weak_roots(phase),
 208   _weak_roots(phase),
 209   _dedup_roots(phase),
 210   _code_roots(phase) {
 211 }
 212 
 213 ShenandoahRootAdjuster::ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 214   ShenandoahRootProcessor(phase),
 215   _serial_roots(phase),
 216   _vm_roots(phase),
 217   _cld_roots(phase),
 218   _thread_roots(phase, n_workers > 1),
 219   _serial_weak_roots(phase),
 220   _weak_roots(phase),
 221   _dedup_roots(phase),
 222   _code_roots(phase) {
 223   assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only");
 224 }
 225 
 226 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) {
 227   CodeBlobToOopClosure code_blob_cl(oops, CodeBlobToOopClosure::FixRelocations);
 228   ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(oops);
 229   CodeBlobToOopClosure* adjust_code_closure = ShenandoahConcurrentRoots::can_do_concurrent_class_unloading() ?
 230                                               static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) :
 231                                               static_cast<CodeBlobToOopClosure*>(&code_blob_cl);
 232   CLDToOopClosure adjust_cld_closure(oops, ClassLoaderData::_claim_strong);
 233   AlwaysTrueClosure always_true;
 234 
 235   _serial_roots.oops_do(oops, worker_id);
 236   _vm_roots.oops_do(oops, worker_id);
 237 
 238   _thread_roots.oops_do(oops, NULL, worker_id);
 239   _cld_roots.cld_do(&adjust_cld_closure, worker_id);
 240   _code_roots.code_blobs_do(adjust_code_closure, worker_id);
 241 
 242   _serial_weak_roots.weak_oops_do(oops, worker_id);
 243   _weak_roots.oops_do<OopClosure>(oops, worker_id);
 244   _dedup_roots.oops_do(&always_true, oops, worker_id);
 245 }
 246 
 247 ShenandoahHeapIterationRootScanner::ShenandoahHeapIterationRootScanner() :
 248    ShenandoahRootProcessor(ShenandoahPhaseTimings::heap_iteration_roots),
 249    _serial_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 250    _thread_roots(ShenandoahPhaseTimings::heap_iteration_roots, false /*is par*/),
 251    _vm_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 252    _cld_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 253    _serial_weak_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 254    _weak_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 255    _dedup_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 256    _code_roots(ShenandoahPhaseTimings::heap_iteration_roots) {
 257  }
 258 
 259  void ShenandoahHeapIterationRootScanner::roots_do(OopClosure* oops) {
 260    assert(Thread::current()->is_VM_thread(), "Only by VM thread");
 261    // Must use _claim_none to avoid interfering with concurrent CLDG iteration
 262    CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
 263    MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations);
 264    ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
 265    AlwaysTrueClosure always_true;
 266    ResourceMark rm;
 267 
 268    _serial_roots.oops_do(oops, 0);
 269    _vm_roots.oops_do(oops, 0);
 270    _cld_roots.cld_do(&clds, 0);
 271    _thread_roots.threads_do(&tc_cl, 0);
 272    _code_roots.code_blobs_do(&code, 0);
 273 
 274    _serial_weak_roots.weak_oops_do(oops, 0);
 275    _weak_roots.oops_do<OopClosure>(oops, 0);
 276    _dedup_roots.oops_do(&always_true, oops, 0);
 277  }