1 /*
   2  * Copyright (c) 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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_INLINE_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_INLINE_HPP
  26 
  27 #include "classfile/classLoaderDataGraph.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "gc/shared/oopStorageParState.inline.hpp"
  31 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  32 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
  33 #include "gc/shenandoah/shenandoahTimingTracker.hpp"
  34 #include "gc/shenandoah/shenandoahUtils.hpp"
  35 #include "prims/resolvedMethodTable.hpp"
  36 #include "memory/resourceArea.hpp"
  37 
  38 template <bool CONCURRENT>
  39 inline ShenandoahWeakRoot<CONCURRENT>::ShenandoahWeakRoot(OopStorage* storage, ShenandoahPhaseTimings::GCParPhases phase) :
  40   _itr(storage), _phase(phase) {
  41 }
  42 
  43 template <bool CONCURRENT>
  44 template <typename Closure>
  45 inline void ShenandoahWeakRoot<CONCURRENT>::oops_do(Closure* cl, uint worker_id) {
  46   if (CONCURRENT) {
  47     _itr.oops_do(cl);
  48   } else {
  49     ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  50     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::ThreadRoots, worker_id);
  51     _itr.oops_do(cl);
  52   }
  53 }
  54 
  55 inline ShenandoahWeakRoot<false>::ShenandoahWeakRoot(OopStorage* storage, ShenandoahPhaseTimings::GCParPhases phase) :
  56   _itr(storage), _phase(phase) {
  57 }
  58 
  59 template <typename IsAliveClosure, typename KeepAliveClosure>
  60 void ShenandoahWeakRoot<false /* concurrent */>::weak_oops_do(IsAliveClosure* is_alive, KeepAliveClosure* keep_alive, uint worker_id) {
  61   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  62   ShenandoahWorkerTimingsTracker timer(worker_times, _phase, worker_id);
  63   _itr.weak_oops_do(is_alive, keep_alive);
  64 }
  65 
  66 template <bool CONCURRENT>
  67 ShenandoahWeakRoots<CONCURRENT>::ShenandoahWeakRoots() :
  68   _jni_roots(JNIHandles::weak_global_handles(), ShenandoahPhaseTimings::JNIWeakRoots),
  69   _string_table_roots(StringTable::weak_storage(), ShenandoahPhaseTimings::StringTableRoots),
  70   _resolved_method_table_roots(ResolvedMethodTable::weak_storage(), ShenandoahPhaseTimings::ResolvedMethodTableRoots),
  71   _vm_roots(SystemDictionary::vm_weak_oop_storage(), ShenandoahPhaseTimings::VMWeakRoots) {
  72 }
  73 
  74 template <bool CONCURRENT>
  75 template <typename Closure>
  76 void ShenandoahWeakRoots<CONCURRENT>::oops_do(Closure* cl, uint worker_id) {
  77   _jni_roots.oops_do(cl, worker_id);
  78   _string_table_roots.oops_do(cl, worker_id);
  79   _resolved_method_table_roots.oops_do(cl, worker_id);
  80   _vm_roots.oops_do(cl, worker_id);
  81 }
  82 
  83 inline ShenandoahWeakRoots<false /* concurrent */>::ShenandoahWeakRoots() :
  84   _jni_roots(JNIHandles::weak_global_handles(), ShenandoahPhaseTimings::JNIWeakRoots),
  85   _string_table_roots(StringTable::weak_storage(), ShenandoahPhaseTimings::StringTableRoots),
  86   _resolved_method_table_roots(ResolvedMethodTable::weak_storage(), ShenandoahPhaseTimings::ResolvedMethodTableRoots),
  87   _vm_roots(SystemDictionary::vm_weak_oop_storage(), ShenandoahPhaseTimings::VMWeakRoots) {
  88 }
  89 
  90 template <typename IsAliveClosure, typename KeepAliveClosure>
  91 void ShenandoahWeakRoots<false /* concurrent*/>::weak_oops_do(IsAliveClosure* is_alive, KeepAliveClosure* keep_alive, uint worker_id) {
  92   _jni_roots.weak_oops_do(is_alive, keep_alive, worker_id);
  93   _string_table_roots.weak_oops_do(is_alive, keep_alive, worker_id);
  94   _resolved_method_table_roots.weak_oops_do(is_alive, keep_alive, worker_id);
  95   _vm_roots.weak_oops_do(is_alive, keep_alive, worker_id);
  96 }
  97 
  98 template <typename Closure>
  99 void ShenandoahWeakRoots<false /* concurrent */>::oops_do(Closure* cl, uint worker_id) {
 100   AlwaysTrueClosure always_true;
 101   weak_oops_do<AlwaysTrueClosure, Closure>(&always_true, cl, worker_id);
 102 }
 103 
 104 template <bool CONCURRENT>
 105 ShenandoahJNIHandleRoots<CONCURRENT>::ShenandoahJNIHandleRoots() :
 106   _itr(JNIHandles::global_handles()) {
 107 }
 108 
 109 template <bool CONCURRENT>
 110 template <typename T>
 111 void ShenandoahJNIHandleRoots<CONCURRENT>::oops_do(T* cl, uint worker_id) {
 112   if (CONCURRENT) {
 113     _itr.oops_do(cl);
 114   } else {
 115     ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 116     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::JNIRoots, worker_id);
 117     _itr.oops_do(cl);
 118   }
 119 }
 120 
 121 template <typename ITR>
 122 ShenandoahCodeCacheRoots<ITR>::ShenandoahCodeCacheRoots() {
 123   nmethod::oops_do_marking_prologue();
 124 }
 125 
 126 template <typename ITR>
 127 void ShenandoahCodeCacheRoots<ITR>::code_blobs_do(CodeBlobClosure* blob_cl, uint worker_id) {
 128   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
 129   ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CodeCacheRoots, worker_id);
 130   _coderoots_iterator.possibly_parallel_blobs_do(blob_cl);
 131 }
 132 
 133 template <typename ITR>
 134 ShenandoahCodeCacheRoots<ITR>::~ShenandoahCodeCacheRoots() {
 135   nmethod::oops_do_marking_epilogue();
 136 }
 137 
 138 class ShenandoahParallelOopsDoThreadClosure : public ThreadClosure {
 139 private:
 140   OopClosure* _f;
 141   CodeBlobClosure* _cf;
 142   ThreadClosure* _thread_cl;
 143 public:
 144   ShenandoahParallelOopsDoThreadClosure(OopClosure* f, CodeBlobClosure* cf, ThreadClosure* thread_cl) :
 145     _f(f), _cf(cf), _thread_cl(thread_cl) {}
 146 
 147   void do_thread(Thread* t) {
 148     if (_thread_cl != NULL) {
 149       _thread_cl->do_thread(t);
 150     }
 151     t->oops_do(_f, _cf);
 152   }
 153 };
 154 
 155 template <typename ITR>
 156 ShenandoahRootScanner<ITR>::ShenandoahRootScanner(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 157   ShenandoahRootProcessor(phase),
 158   _thread_roots(n_workers > 1) {
 159 }
 160 
 161 template <typename ITR>
 162 void ShenandoahRootScanner<ITR>::roots_do(uint worker_id, OopClosure* oops) {
 163   CLDToOopClosure clds_cl(oops, ClassLoaderData::_claim_strong);
 164   MarkingCodeBlobClosure blobs_cl(oops, !CodeBlobToOopClosure::FixRelocations);
 165   roots_do(worker_id, oops, &clds_cl, &blobs_cl);
 166 }
 167 
 168 template <typename ITR>
 169 void ShenandoahRootScanner<ITR>::strong_roots_do(uint worker_id, OopClosure* oops) {
 170   CLDToOopClosure clds_cl(oops, ClassLoaderData::_claim_strong);
 171   MarkingCodeBlobClosure blobs_cl(oops, !CodeBlobToOopClosure::FixRelocations);
 172   strong_roots_do(worker_id, oops, &clds_cl, &blobs_cl);
 173 }
 174 
 175 template <typename ITR>
 176 void ShenandoahRootScanner<ITR>::roots_do(uint worker_id, OopClosure* oops, CLDClosure* clds, CodeBlobClosure* code, ThreadClosure *tc) {
 177   assert(!ShenandoahSafepoint::is_at_shenandoah_safepoint() ||
 178          !ShenandoahHeap::heap()->unload_classes() ||
 179           ShenandoahHeap::heap()->heuristics()->can_do_traversal_gc(),
 180           "Expect class unloading or traversal when Shenandoah cycle is running");
 181   ShenandoahParallelOopsDoThreadClosure tc_cl(oops, code, tc);
 182   ResourceMark rm;
 183 
 184   _serial_roots.oops_do(oops, worker_id);
 185   _jni_roots.oops_do(oops, worker_id);
 186   _cld_roots.clds_do(clds, clds, worker_id);
 187   _thread_roots.threads_do(&tc_cl, worker_id);
 188 
 189   // With ShenandoahConcurrentScanCodeRoots, we avoid scanning the entire code cache here,
 190   // and instead do that in concurrent phase under the relevant lock. This saves init mark
 191   // pause time.
 192   if (code != NULL && !ShenandoahConcurrentScanCodeRoots) {
 193     _code_roots.code_blobs_do(code, worker_id);
 194   }
 195 }
 196 
 197 template <typename ITR>
 198 void ShenandoahRootScanner<ITR>::roots_do_unchecked(OopClosure* oops) {
 199   CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
 200   MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations);
 201   ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
 202   ResourceMark rm;
 203 
 204   _serial_roots.oops_do(oops, 0);
 205   _jni_roots.oops_do(oops, 0);
 206   _cld_roots.clds_do(&clds, &clds, 0);
 207   _thread_roots.threads_do(&tc_cl, 0);
 208   _code_roots.code_blobs_do(&code, 0);
 209 }
 210 
 211 template <typename ITR>
 212 void ShenandoahRootScanner<ITR>::strong_roots_do_unchecked(OopClosure* oops) {
 213   CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
 214   MarkingCodeBlobClosure code(oops, !CodeBlobToOopClosure::FixRelocations);
 215   ShenandoahParallelOopsDoThreadClosure tc_cl(oops, &code, NULL);
 216   ResourceMark rm;
 217 
 218   _serial_roots.oops_do(oops, 0);
 219   _jni_roots.oops_do(oops, 0);
 220   _cld_roots.clds_do(&clds, NULL, 0);
 221   _thread_roots.threads_do(&tc_cl, 0);
 222 }
 223 
 224 template <typename ITR>
 225 void ShenandoahRootScanner<ITR>::strong_roots_do(uint worker_id, OopClosure* oops, CLDClosure* clds, CodeBlobClosure* code, ThreadClosure* tc) {
 226   assert(ShenandoahHeap::heap()->unload_classes(), "Should be used during class unloading");
 227   ShenandoahParallelOopsDoThreadClosure tc_cl(oops, code, tc);
 228   ResourceMark rm;
 229 
 230   _serial_roots.oops_do(oops, worker_id);
 231   _jni_roots.oops_do(oops, worker_id);
 232   _cld_roots.clds_do(clds, NULL, worker_id);
 233   _thread_roots.threads_do(&tc_cl, worker_id);
 234 }
 235 
 236 template <typename IsAlive, typename KeepAlive>
 237 void ShenandoahRootUpdater::roots_do(uint worker_id, IsAlive* is_alive, KeepAlive* keep_alive) {
 238   CodeBlobToOopClosure update_blobs(keep_alive, CodeBlobToOopClosure::FixRelocations);
 239   CLDToOopClosure clds(keep_alive, ClassLoaderData::_claim_strong);
 240   CLDToOopClosure* weak_clds = ShenandoahHeap::heap()->unload_classes() ? NULL : &clds;
 241 
 242   _serial_roots.oops_do(keep_alive, worker_id);
 243   _jni_roots.oops_do(keep_alive, worker_id);
 244 
 245   _thread_roots.oops_do(keep_alive, NULL, worker_id);
 246   _cld_roots.clds_do(&clds, weak_clds, worker_id);
 247 
 248   if(_update_code_cache) {
 249     _code_roots.code_blobs_do(&update_blobs, worker_id);
 250   }
 251 
 252   _serial_weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);
 253   _weak_roots.weak_oops_do(is_alive, keep_alive, worker_id);
 254   _dedup_roots.oops_do(is_alive, keep_alive, worker_id);
 255 }
 256 
 257 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_INLINE_HPP