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/shenandoahRootProcessor.inline.hpp"
  32 #include "gc/shenandoah/shenandoahHeap.hpp"
  33 #include "gc/shenandoah/shenandoahHeuristics.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/allocation.inline.hpp"
  40 #include "memory/iterator.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "memory/universe.hpp"
  43 #include "runtime/thread.hpp"
  44 #include "services/management.hpp"
  45 
  46 ShenandoahSerialRoot::ShenandoahSerialRoot(ShenandoahSerialRoot::OopsDo oops_do, ShenandoahPhaseTimings::GCParPhases phase) :
  47   _claimed(false), _oops_do(oops_do), _phase(phase) {
  48 }
  49 
  50 void ShenandoahSerialRoot::oops_do(OopClosure* cl, uint worker_id) {
  51   if (!_claimed && Atomic::cmpxchg(true, &_claimed, false) == false) {
  52     ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  53     ShenandoahWorkerTimingsTracker timer(worker_times, _phase, worker_id);
  54     _oops_do(cl);
  55   }
  56 }
  57 
  58 ShenandoahSerialRoots::ShenandoahSerialRoots() :
  59   _universe_root(&Universe::oops_do, ShenandoahPhaseTimings::UniverseRoots),
  60   _object_synchronizer_root(&ObjectSynchronizer::oops_do, ShenandoahPhaseTimings::ObjectSynchronizerRoots),
  61   _management_root(&Management::oops_do, ShenandoahPhaseTimings::ManagementRoots),
  62   _system_dictionary_root(&SystemDictionary::oops_do, ShenandoahPhaseTimings::SystemDictionaryRoots),
  63   _jvmti_root(&JvmtiExport::oops_do, ShenandoahPhaseTimings::JVMTIRoots) {
  64 }
  65 
  66 void ShenandoahSerialRoots::oops_do(OopClosure* cl, uint worker_id) {
  67   _universe_root.oops_do(cl, worker_id);
  68   _object_synchronizer_root.oops_do(cl, worker_id);
  69   _management_root.oops_do(cl, worker_id);
  70   _system_dictionary_root.oops_do(cl, worker_id);
  71   _jvmti_root.oops_do(cl, worker_id);
  72 }
  73 
  74 ShenandoahWeakSerialRoot::ShenandoahWeakSerialRoot(ShenandoahWeakSerialRoot::WeakOopsDo weak_oops_do, ShenandoahPhaseTimings::GCParPhases phase) :
  75   _claimed(false), _weak_oops_do(weak_oops_do), _phase(phase) {
  76 }
  77 
  78 void ShenandoahWeakSerialRoot::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
  79   if (!_claimed && Atomic::cmpxchg(true, &_claimed, false) == false) {
  80     ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  81     ShenandoahWorkerTimingsTracker timer(worker_times, _phase, worker_id);
  82     _weak_oops_do(is_alive, keep_alive);
  83   }
  84 }
  85 
  86 #if INCLUDE_JVMTI
  87 ShenandoahJVMTIWeakRoot::ShenandoahJVMTIWeakRoot() :
  88   ShenandoahWeakSerialRoot(&JvmtiExport::weak_oops_do, ShenandoahPhaseTimings::JVMTIWeakRoots) {
  89 }
  90 #endif // INCLUDE_JVMTI
  91 
  92 #if INCLUDE_JFR
  93 ShenandoahJFRWeakRoot::ShenandoahJFRWeakRoot() :
  94   ShenandoahWeakSerialRoot(&Jfr::weak_oops_do, ShenandoahPhaseTimings::JFRWeakRoots) {
  95 }
  96 #endif // INCLUDE_JFR
  97 
  98 void ShenandoahSerialWeakRoots::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
  99   JVMTI_ONLY(_jvmti_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);)
 100   JFR_ONLY(_jfr_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);)
 101 }
 102 
 103 void ShenandoahSerialWeakRoots::weak_oops_do(OopClosure* cl, uint worker_id) {
 104   AlwaysTrueClosure always_true;
 105   weak_oops_do(&always_true, cl, worker_id);
 106 }
 107 
 108 ShenandoahThreadRoots::ShenandoahThreadRoots(bool is_par) : _is_par(is_par) {
 109   Threads::change_thread_claim_token();
 110 }
 111 
 112 void ShenandoahThreadRoots::oops_do(OopClosure* oops_cl, CodeBlobClosure* code_cl, uint worker_id) {
 113   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 114   ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 115   ResourceMark rm;
 116   Threads::possibly_parallel_oops_do(_is_par, oops_cl, code_cl);
 117 }
 118 
 119 void ShenandoahThreadRoots::threads_do(ThreadClosure* tc, uint worker_id) {
 120   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 121   ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id);
 122   ResourceMark rm;
 123   Threads::possibly_parallel_threads_do(_is_par, tc);
 124 }
 125 
 126 ShenandoahThreadRoots::~ShenandoahThreadRoots() {
 127   Threads::assert_all_threads_claimed();
 128 }
 129 
 130 ShenandoahStringDedupRoots::ShenandoahStringDedupRoots() {
 131   if (ShenandoahStringDedup::is_enabled()) {
 132     StringDedup::gc_prologue(false);
 133   }
 134 }
 135 
 136 ShenandoahStringDedupRoots::~ShenandoahStringDedupRoots() {
 137   if (ShenandoahStringDedup::is_enabled()) {
 138     StringDedup::gc_epilogue();
 139   }
 140 }
 141 
 142 void ShenandoahStringDedupRoots::oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
 143   if (ShenandoahStringDedup::is_enabled()) {
 144     ShenandoahStringDedup::parallel_oops_do(is_alive, keep_alive, worker_id);
 145   }
 146 }
 147 
 148 ShenandoahClassLoaderDataRoots::ShenandoahClassLoaderDataRoots() {
 149   ClassLoaderDataGraph::clear_claimed_marks();
 150 }
 151 
 152 void ShenandoahClassLoaderDataRoots::clds_do(CLDClosure* strong_clds, CLDClosure* weak_clds, uint worker_id) {
 153   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 154   ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CLDGRoots, worker_id);
 155   ClassLoaderDataGraph::roots_cld_do(strong_clds, weak_clds);
 156 }
 157 
 158 ShenandoahRootProcessor::ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase) :
 159   _heap(ShenandoahHeap::heap()),
 160   _phase(phase) {
 161   assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint");
 162   _heap->phase_timings()->record_workers_start(_phase);
 163 }
 164 
 165 ShenandoahRootProcessor::~ShenandoahRootProcessor() {
 166   assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint");
 167   _heap->phase_timings()->record_workers_end(_phase);
 168 }
 169 
 170 ShenandoahRootEvacuator::ShenandoahRootEvacuator(uint n_workers, ShenandoahPhaseTimings::Phase phase, bool include_concurrent_roots) :
 171   ShenandoahRootProcessor(phase),
 172   _thread_roots(n_workers > 1),
 173   _include_concurrent_roots(include_concurrent_roots) {
 174 }
 175 
 176 void ShenandoahRootEvacuator::roots_do(uint worker_id, OopClosure* oops) {
 177   MarkingCodeBlobClosure blobsCl(oops, CodeBlobToOopClosure::FixRelocations);
 178   CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
 179   AlwaysTrueClosure always_true;
 180 
 181   _serial_roots.oops_do(oops, worker_id);
 182   _serial_weak_roots.weak_oops_do(oops, worker_id);
 183   if (_include_concurrent_roots) {
 184     _jni_roots.oops_do<OopClosure>(oops, worker_id);
 185     _weak_roots.oops_do<OopClosure>(oops, worker_id);
 186   }
 187 
 188   _thread_roots.oops_do(oops, NULL, worker_id);
 189   _cld_roots.clds_do(&clds, &clds, worker_id);
 190   _code_roots.code_blobs_do(&blobsCl, worker_id);
 191 
 192   _dedup_roots.oops_do(&always_true, oops, worker_id);
 193 }
 194 
 195 ShenandoahRootUpdater::ShenandoahRootUpdater(uint n_workers, ShenandoahPhaseTimings::Phase phase, bool update_code_cache) :
 196   ShenandoahRootProcessor(phase),
 197   _thread_roots(n_workers > 1),
 198   _update_code_cache(update_code_cache) {
 199 }
 200 
 201 ShenandoahRootAdjuster::ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 202   ShenandoahRootProcessor(phase),
 203   _thread_roots(n_workers > 1) {
 204   assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only");
 205 }
 206 
 207 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) {
 208   CodeBlobToOopClosure adjust_code_closure(oops, CodeBlobToOopClosure::FixRelocations);
 209   CLDToOopClosure adjust_cld_closure(oops, ClassLoaderData::_claim_strong);
 210   AlwaysTrueClosure always_true;
 211 
 212   _serial_roots.oops_do(oops, worker_id);
 213   _jni_roots.oops_do(oops, worker_id);
 214 
 215   _thread_roots.oops_do(oops, NULL, worker_id);
 216   _cld_roots.clds_do(&adjust_cld_closure, NULL, worker_id);
 217   _code_roots.code_blobs_do(&adjust_code_closure, worker_id);
 218 
 219   _serial_weak_roots.weak_oops_do(oops, worker_id);
 220   _weak_roots.oops_do<OopClosure>(oops, worker_id);
 221   _dedup_roots.oops_do(&always_true, oops, worker_id);
 222 }