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/stringTable.hpp"
  28 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  29 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
  30 #include "gc/shenandoah/shenandoahTimingTracker.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "runtime/safepoint.hpp"
  33 
  34 template <typename IsAlive, typename KeepAlive>
  35 void ShenandoahWeakRoots::oops_do(IsAlive* is_alive, KeepAlive* keep_alive, uint worker_id) {
  36   if (!_claimed && Atomic::cmpxchg(true, &_claimed, false) == false) {
  37     WeakProcessor::weak_oops_do(is_alive, keep_alive);
  38   }
  39   StringTable::possibly_parallel_oops_do(&_par_state_string, keep_alive);
  40 }
  41 
  42 template <bool SINGLE_THREADED>
  43 ShenandoahClassLoaderDataRoots<SINGLE_THREADED>::ShenandoahClassLoaderDataRoots() {
  44   if (!SINGLE_THREADED) {
  45     ClassLoaderDataGraph::clear_claimed_marks();
  46   }
  47 }
  48 
  49 template <bool SINGLE_THREADED>
  50 void ShenandoahClassLoaderDataRoots<SINGLE_THREADED>::always_strong_cld_do(CLDClosure* clds, uint worker_id) {
  51   if (SINGLE_THREADED) {
  52     assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
  53     assert(Thread::current()->is_VM_thread(), "Single threaded CLDG iteration can only be done by VM thread");
  54     ClassLoaderDataGraph::always_strong_cld_do(clds);
  55   } else {
  56    ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  57    ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CLDGRoots, worker_id);
  58    ClassLoaderDataGraph::always_strong_cld_do(clds);
  59   }
  60 }
  61 
  62 template <bool SINGLE_THREADED>
  63 void ShenandoahClassLoaderDataRoots<SINGLE_THREADED>::cld_do(CLDClosure* clds, uint worker_id) {
  64   if (SINGLE_THREADED) {
  65     assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
  66     assert(Thread::current()->is_VM_thread(), "Single threaded CLDG iteration can only be done by VM thread");
  67     ClassLoaderDataGraph::cld_do(clds);
  68   } else {
  69     ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  70     ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CLDGRoots, worker_id);
  71     ClassLoaderDataGraph::cld_do(clds);
  72   }
  73 }
  74 
  75 template <typename ITR>
  76 ShenandoahCodeCacheRoots<ITR>::ShenandoahCodeCacheRoots() {
  77   nmethod::oops_do_marking_prologue();
  78 }
  79 
  80 template <typename ITR>
  81 void ShenandoahCodeCacheRoots<ITR>::code_blobs_do(CodeBlobClosure* blob_cl, uint worker_id) {
  82   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  83   ShenandoahWorkerTimingsTracker timer(worker_times, ShenandoahPhaseTimings::CodeCacheRoots, worker_id);
  84   _coderoots_iterator.possibly_parallel_blobs_do(blob_cl);
  85 }
  86 
  87 template <typename ITR>
  88 ShenandoahCodeCacheRoots<ITR>::~ShenandoahCodeCacheRoots() {
  89   nmethod::oops_do_marking_epilogue();
  90 }
  91 
  92 class ShenandoahParallelOopsDoThreadClosure : public ThreadClosure {
  93 private:
  94   OopClosure* _f;
  95   CodeBlobClosure* _cf;
  96   ThreadClosure* _thread_cl;
  97 public:
  98   ShenandoahParallelOopsDoThreadClosure(OopClosure* f, CodeBlobClosure* cf, ThreadClosure* thread_cl) :
  99     _f(f), _cf(cf), _thread_cl(thread_cl) {}
 100 
 101   void do_thread(Thread* t) {
 102     if (_thread_cl != NULL) {
 103       _thread_cl->do_thread(t);
 104     }
 105     t->oops_do(_f, _cf);
 106   }
 107 };
 108 
 109 template <typename ITR>
 110 ShenandoahRootScanner<ITR>::ShenandoahRootScanner(uint n_workers, ShenandoahPhaseTimings::Phase phase) :
 111   ShenandoahRootProcessor(phase),
 112   _thread_roots(n_workers > 1) {
 113 }
 114 
 115 template <typename ITR>
 116 void ShenandoahRootScanner<ITR>::roots_do(uint worker_id, OopClosure* oops) {
 117   CLDToOopClosure clds_cl(oops);
 118   MarkingCodeBlobClosure blobs_cl(oops, !CodeBlobToOopClosure::FixRelocations);
 119   roots_do(worker_id, oops, &clds_cl, &blobs_cl);
 120 }
 121 
 122 template <typename ITR>
 123 void ShenandoahRootScanner<ITR>::strong_roots_do(uint worker_id, OopClosure* oops) {
 124   CLDToOopClosure clds_cl(oops);
 125   MarkingCodeBlobClosure blobs_cl(oops, !CodeBlobToOopClosure::FixRelocations);
 126   strong_roots_do(worker_id, oops, &clds_cl, &blobs_cl);
 127 }
 128 
 129 template <typename ITR>
 130 void ShenandoahRootScanner<ITR>::roots_do(uint worker_id, OopClosure* oops, CLDClosure* clds, CodeBlobClosure* code, ThreadClosure *tc) {
 131   assert(!ShenandoahHeap::heap()->unload_classes() ||
 132           ShenandoahHeap::heap()->is_traversal_mode(),
 133           "No class unloading or traversal GC");
 134   ShenandoahParallelOopsDoThreadClosure tc_cl(oops, code, tc);
 135   ResourceMark rm;
 136 
 137   _serial_roots.oops_do(oops, worker_id);
 138   _jni_roots.oops_do(oops, worker_id);
 139 
 140   if (clds != NULL) {
 141     _cld_roots.cld_do(clds, worker_id);
 142   } else {
 143     assert(ShenandoahHeap::heap()->is_concurrent_traversal_in_progress(), "Only possible with traversal GC");
 144   }
 145 
 146   _thread_roots.threads_do(&tc_cl, worker_id);
 147 
 148   // With ShenandoahConcurrentScanCodeRoots, we avoid scanning the entire code cache here,
 149   // and instead do that in concurrent phase under the relevant lock. This saves init mark
 150   // pause time.
 151   if (code != NULL && !ShenandoahConcurrentScanCodeRoots) {
 152     _code_roots.code_blobs_do(code, worker_id);
 153   }
 154 }
 155 
 156 template <typename ITR>
 157 void ShenandoahRootScanner<ITR>::strong_roots_do(uint worker_id, OopClosure* oops, CLDClosure* clds, CodeBlobClosure* code, ThreadClosure* tc) {
 158   assert(ShenandoahHeap::heap()->unload_classes(), "Should be used during class unloading");
 159   ShenandoahParallelOopsDoThreadClosure tc_cl(oops, code, tc);
 160   ResourceMark rm;
 161 
 162   _serial_roots.oops_do(oops, worker_id);
 163   _jni_roots.oops_do(oops, worker_id);
 164   _cld_roots.always_strong_cld_do(clds, worker_id);
 165   _thread_roots.threads_do(&tc_cl, worker_id);
 166 }
 167 
 168 template <typename IsAlive, typename KeepAlive>
 169 void ShenandoahRootUpdater::roots_do(uint worker_id, IsAlive* is_alive, KeepAlive* keep_alive) {
 170   CodeBlobToOopClosure update_blobs(keep_alive, CodeBlobToOopClosure::FixRelocations);
 171   CLDToOopClosure clds(keep_alive);
 172 
 173   _serial_roots.oops_do(keep_alive, worker_id);
 174   _jni_roots.oops_do(keep_alive, worker_id);
 175 
 176   _thread_roots.oops_do(keep_alive, NULL, worker_id);
 177   _cld_roots.cld_do(&clds, worker_id);
 178 
 179   if(_update_code_cache) {
 180     _code_roots.code_blobs_do(&update_blobs, worker_id);
 181   }
 182 
 183   _weak_roots.oops_do<IsAlive, KeepAlive>(is_alive, keep_alive, worker_id);
 184   _dedup_roots.oops_do(is_alive, keep_alive, worker_id);
 185 }
 186 
 187 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_INLINE_HPP