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   // Process serial-claiming roots first
 183   _serial_roots.oops_do(oops, worker_id);
 184   _serial_weak_roots.weak_oops_do(oops, worker_id);
 185 
 186   // Process light-weight/limited parallel roots then
 187   if (_stw_roots_processing) {
 188     _vm_roots.oops_do<OopClosure>(oops, worker_id);
 189     _weak_roots.oops_do<OopClosure>(oops, worker_id);
 190   }
 191   _dedup_roots.oops_do(&always_true, oops, worker_id);
 192 
 193   // Process heavy-weight/fully parallel roots the last
 194   if (_stw_class_unloading) {
 195     CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
 196     _cld_roots.cld_do(&clds, worker_id);
 197     _code_roots.code_blobs_do(codes_cl, worker_id);
 198     _thread_roots.oops_do(oops, NULL, worker_id);
 199   } else {
 200     _thread_roots.oops_do(oops, codes_cl, worker_id);
 201   }
 202 }
 203 
 204 ShenandoahRootUpdater::ShenandoahRootUpdater(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 205   ShenandoahRootProcessor(phase),
 206   _serial_roots(phase),
 207   _vm_roots(phase),
 208   _cld_roots(phase),
 209   _thread_roots(phase, n_workers > 1),
 210   _serial_weak_roots(phase),
 211   _weak_roots(phase),
 212   _dedup_roots(phase),
 213   _code_roots(phase) {
 214 }
 215 
 216 ShenandoahRootAdjuster::ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 217   ShenandoahRootProcessor(phase),
 218   _serial_roots(phase),
 219   _vm_roots(phase),
 220   _cld_roots(phase),
 221   _thread_roots(phase, n_workers > 1),
 222   _serial_weak_roots(phase),
 223   _weak_roots(phase),
 224   _dedup_roots(phase),
 225   _code_roots(phase) {
 226   assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only");
 227 }
 228 
 229 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) {
 230   CodeBlobToOopClosure code_blob_cl(oops, CodeBlobToOopClosure::FixRelocations);
 231   ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(oops);
 232   CodeBlobToOopClosure* adjust_code_closure = ShenandoahConcurrentRoots::can_do_concurrent_class_unloading() ?
 233                                               static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) :
 234                                               static_cast<CodeBlobToOopClosure*>(&code_blob_cl);
 235   CLDToOopClosure adjust_cld_closure(oops, ClassLoaderData::_claim_strong);
 236   AlwaysTrueClosure always_true;
 237 
 238   // Process serial-claiming roots first
 239   _serial_roots.oops_do(oops, worker_id);
 240   _serial_weak_roots.weak_oops_do(oops, worker_id);
 241 
 242   // Process light-weight/limited parallel roots then
 243   _vm_roots.oops_do(oops, worker_id);
 244   _weak_roots.oops_do<OopClosure>(oops, worker_id);
 245   _dedup_roots.oops_do(&always_true, oops, worker_id);
 246 
 247   // Process heavy-weight/fully parallel roots the last
 248   _cld_roots.cld_do(&adjust_cld_closure, worker_id);
 249   _code_roots.code_blobs_do(adjust_code_closure, worker_id);
 250   _thread_roots.oops_do(oops, NULL, worker_id);
 251 }
 252 
 253 ShenandoahHeapIterationRootScanner::ShenandoahHeapIterationRootScanner() :
 254    ShenandoahRootProcessor(ShenandoahPhaseTimings::heap_iteration_roots),
 255    _serial_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 256    _thread_roots(ShenandoahPhaseTimings::heap_iteration_roots, false /*is par*/),
 257    _vm_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 258    _cld_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 259    _serial_weak_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 260    _weak_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 261    _dedup_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 262    _code_roots(ShenandoahPhaseTimings::heap_iteration_roots) {
 263  }
 264 
 265  void ShenandoahHeapIterationRootScanner::roots_do(OopClosure* oops) {
 266    assert(Thread::current()->is_VM_thread(), "Only by VM thread");
 267    // Must use _claim_none to avoid interfering with concurrent CLDG iteration
 268    CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
 269    MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations);
 270    ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
 271    AlwaysTrueClosure always_true;
 272 
 273    ResourceMark rm;
 274 
 275    // Process serial-claiming roots first
 276    _serial_roots.oops_do(oops, 0);
 277    _serial_weak_roots.weak_oops_do(oops, 0);
 278 
 279    // Process light-weight/limited parallel roots then
 280    _vm_roots.oops_do(oops, 0);
 281    _weak_roots.oops_do<OopClosure>(oops, 0);
 282    _dedup_roots.oops_do(&always_true, oops, 0);
 283 
 284    // Process heavy-weight/fully parallel roots the last
 285    _cld_roots.cld_do(&clds, 0);
 286    _code_roots.code_blobs_do(&code, 0);
 287    _thread_roots.threads_do(&tc_cl, 0);
 288  }