1 /*
   2  * Copyright (c) 2015, 2018, 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/classLoaderData.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.inline.hpp"
  33 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  34 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  35 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  36 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  37 #include "gc/shared/weakProcessor.hpp"
  38 #include "memory/allocation.inline.hpp"
  39 #include "memory/iterator.hpp"
  40 #include "memory/resourceArea.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 ShenandoahSerialRoots::ShenandoahSerialRoots(ShenandoahPhaseTimings::Phase phase) :
  57   _universe_root(&ShenandoahSerialRoots::universe_oops_do, phase, ShenandoahPhaseTimings::UniverseRoots),
  58   _object_synchronizer_root(&ObjectSynchronizer::oops_do, phase, ShenandoahPhaseTimings::ObjectSynchronizerRoots),
  59   _management_root(&Management::oops_do, phase, ShenandoahPhaseTimings::ManagementRoots),
  60   _system_dictionary_root(&SystemDictionary::oops_do, phase, ShenandoahPhaseTimings::SystemDictionaryRoots),
  61   _jvmti_root(&JvmtiExport::oops_do, phase, ShenandoahPhaseTimings::JVMTIRoots) {
  62 }
  63 
  64 void ShenandoahSerialRoots::oops_do(OopClosure* cl, uint worker_id) {
  65   _universe_root.oops_do(cl, worker_id);
  66   _object_synchronizer_root.oops_do(cl, worker_id);
  67   _management_root.oops_do(cl, worker_id);
  68   _system_dictionary_root.oops_do(cl, worker_id);
  69   _jvmti_root.oops_do(cl, worker_id);
  70 }
  71 
  72 ShenandoahJNIHandleRoots::ShenandoahJNIHandleRoots(ShenandoahPhaseTimings::Phase phase) :
  73   ShenandoahSerialRoot(&JNIHandles::oops_do, phase, ShenandoahPhaseTimings::JNIRoots) {
  74 }
  75 
  76 ShenandoahThreadRoots::ShenandoahThreadRoots(ShenandoahPhaseTimings::Phase phase, bool is_par) :
  77   _phase(phase), _is_par(is_par) {
  78   Threads::change_thread_claim_parity();
  79 }
  80 
  81 void ShenandoahThreadRoots::oops_do(OopClosure* oops_cl, CodeBlobClosure* code_cl, uint worker_id) {
  82   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::ThreadRoots, worker_id);
  83   ResourceMark rm;
  84   Threads::possibly_parallel_oops_do(_is_par, oops_cl, code_cl);
  85 }
  86 
  87 void ShenandoahThreadRoots::threads_do(ThreadClosure* tc, uint worker_id) {
  88   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::ThreadRoots, worker_id);
  89   ResourceMark rm;
  90   Threads::possibly_parallel_threads_do(_is_par, tc);
  91 }
  92 
  93 ShenandoahThreadRoots::~ShenandoahThreadRoots() {
  94   Threads::assert_all_threads_claimed();
  95 }
  96 
  97 ShenandoahWeakRoots::ShenandoahWeakRoots(ShenandoahPhaseTimings::Phase phase, uint n_workers) :
  98   _phase(phase),
  99   _par_state_string(StringTable::weak_storage()),
 100   _claimed(false) {
 101 }
 102 
 103 ShenandoahWeakRoots::~ShenandoahWeakRoots() {
 104 }
 105 
 106 ShenandoahStringDedupRoots::ShenandoahStringDedupRoots(ShenandoahPhaseTimings::Phase phase) : _phase(phase) {
 107   if (ShenandoahStringDedup::is_enabled()) {
 108     StringDedup::gc_prologue(false);
 109   }
 110 }
 111 
 112 ShenandoahStringDedupRoots::~ShenandoahStringDedupRoots() {
 113   if (ShenandoahStringDedup::is_enabled()) {
 114     StringDedup::gc_epilogue();
 115   }
 116 }
 117 
 118 void ShenandoahStringDedupRoots::oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, uint worker_id) {
 119   if (ShenandoahStringDedup::is_enabled()) {
 120     ShenandoahStringDedup::parallel_oops_do(_phase, is_alive, keep_alive, worker_id);
 121   }
 122 }
 123 
 124 ShenandoahRootProcessor::ShenandoahRootProcessor(ShenandoahPhaseTimings::Phase phase) :
 125   _heap(ShenandoahHeap::heap()),
 126   _phase(phase),
 127   _worker_phase(phase) {
 128   assert(SafepointSynchronize::is_at_safepoint(), "Must at safepoint");
 129 }
 130 
 131 ShenandoahRootEvacuator::ShenandoahRootEvacuator(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 132   ShenandoahRootProcessor(phase),
 133   _serial_roots(phase),
 134   _jni_roots(phase),
 135   _cld_roots(phase, n_workers),
 136   _thread_roots(phase, n_workers > 1),
 137   _weak_roots(phase, n_workers),
 138   _dedup_roots(phase),
 139   _code_roots(phase) {
 140 }
 141 
 142 void ShenandoahRootEvacuator::roots_do(uint worker_id, OopClosure* oops) {
 143   MarkingCodeBlobClosure blobsCl(oops, CodeBlobToOopClosure::FixRelocations);
 144   CLDToOopClosure clds(oops);
 145 
 146   AlwaysTrueClosure always_true;
 147 
 148   // Process serial-claiming roots first
 149   _serial_roots.oops_do(oops, worker_id);
 150   _jni_roots.oops_do(oops, worker_id);
 151 
 152   // Process light-weight/limited parallel roots then
 153   _weak_roots.oops_do<AlwaysTrueClosure, OopClosure>(&always_true, oops, worker_id);
 154   _dedup_roots.oops_do(&always_true, oops, worker_id);
 155   _cld_roots.cld_do(&clds, worker_id);
 156 
 157   // Process heavy-weight/fully parallel roots the last
 158   _code_roots.code_blobs_do(&blobsCl, worker_id);
 159   _thread_roots.oops_do(oops, NULL, worker_id);
 160 }
 161 
 162 ShenandoahRootUpdater::ShenandoahRootUpdater(uint n_workers, ShenandoahPhaseTimings::Phase phase, bool update_code_cache) :
 163   ShenandoahRootProcessor(phase),
 164   _serial_roots(phase),
 165   _jni_roots(phase),
 166   _cld_roots(phase, n_workers),
 167   _thread_roots(phase, n_workers > 1),
 168   _weak_roots(phase, n_workers),
 169   _dedup_roots(phase),
 170   _code_roots(phase),
 171   _update_code_cache(update_code_cache) {
 172 }
 173 
 174 ShenandoahRootAdjuster::ShenandoahRootAdjuster(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 175   ShenandoahRootProcessor(phase),
 176   _serial_roots(phase),
 177   _jni_roots(phase),
 178   _cld_roots(phase, n_workers),
 179   _thread_roots(phase, n_workers > 1),
 180   _weak_roots(phase, n_workers),
 181   _dedup_roots(phase),
 182   _code_roots(phase) {
 183   assert(ShenandoahHeap::heap()->is_full_gc_in_progress(), "Full GC only");
 184 }
 185 
 186 void ShenandoahRootAdjuster::roots_do(uint worker_id, OopClosure* oops) {
 187   CodeBlobToOopClosure adjust_code_closure(oops, CodeBlobToOopClosure::FixRelocations);
 188   CLDToOopClosure adjust_cld_closure(oops);
 189   AlwaysTrueClosure always_true;
 190 
 191   // Process serial-claiming roots first
 192   _serial_roots.oops_do(oops, worker_id);
 193   _jni_roots.oops_do(oops, worker_id);
 194 
 195   // Process light-weight/limited parallel roots then
 196   _weak_roots.oops_do<AlwaysTrueClosure, OopClosure>(&always_true, oops, worker_id);
 197   _dedup_roots.oops_do(&always_true, oops, worker_id);
 198   _cld_roots.cld_do(&adjust_cld_closure, worker_id);
 199 
 200   // Process heavy-weight/fully parallel roots the last
 201   _code_roots.code_blobs_do(&adjust_code_closure, worker_id);
 202   _thread_roots.oops_do(oops, NULL, worker_id);
 203 }
 204 
 205 ShenandoahHeapIterationRootScanner::ShenandoahHeapIterationRootScanner() :
 206    ShenandoahRootProcessor(ShenandoahPhaseTimings::heap_iteration_roots),
 207    _serial_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 208    _thread_roots(ShenandoahPhaseTimings::heap_iteration_roots, false /*is par*/),
 209    _jni_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 210    _cld_roots(ShenandoahPhaseTimings::heap_iteration_roots, 1),
 211    _weak_roots(ShenandoahPhaseTimings::heap_iteration_roots, 1),
 212    _dedup_roots(ShenandoahPhaseTimings::heap_iteration_roots),
 213    _code_roots(ShenandoahPhaseTimings::heap_iteration_roots)
 214  { }
 215 
 216  void ShenandoahHeapIterationRootScanner::roots_do(OopClosure* oops) {
 217    assert(Thread::current()->is_VM_thread(), "Only by VM thread");
 218    // Must use _claim_none to avoid interfering with concurrent CLDG iteration
 219    CLDToOopClosure clds(oops, false);
 220    MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations);
 221    ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
 222    AlwaysTrueClosure always_true;
 223    ResourceMark rm;
 224 
 225    // Process serial-claiming roots first
 226    _serial_roots.oops_do(oops, 0);
 227    _jni_roots.oops_do(oops, 0);
 228 
 229    // Process light-weight/limited parallel roots then
 230    _weak_roots.oops_do<AlwaysTrueClosure, OopClosure>(&always_true, oops, 0);
 231    _dedup_roots.oops_do(&always_true, oops, 0);
 232    _cld_roots.cld_do(&clds, 0);
 233 
 234    // Process heavy-weight/fully parallel roots the last
 235    _code_roots.code_blobs_do(&code, 0);
 236    _thread_roots.threads_do(&tc_cl, 0);
 237 
 238  }