1 /*
  2  * Copyright (c) 2019, 2021, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_INLINE_HPP
 27 
 28 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
 29 
 30 #include "classfile/classLoaderDataGraph.hpp"
 31 #include "gc/shared/oopStorageSetParState.inline.hpp"
 32 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
 33 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 34 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
 35 #include "gc/shenandoah/shenandoahUtils.hpp"
 36 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 37 #include "memory/resourceArea.hpp"
 38 #include "runtime/mutexLocker.hpp"
 39 #include "runtime/safepoint.hpp"
 40 
 41 template <bool CONCURRENT>
 42 ShenandoahVMWeakRoots<CONCURRENT>::ShenandoahVMWeakRoots(ShenandoahPhaseTimings::Phase phase) :
 43   _phase(phase) {
 44 }
 45 
 46 template <bool CONCURRENT>
 47 template <typename T>
 48 void ShenandoahVMWeakRoots<CONCURRENT>::oops_do(T* cl, uint worker_id) {
 49   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::VMWeakRoots, worker_id);
 50   _weak_roots.oops_do(cl);
 51 }
 52 
 53 template <bool CONCURRENT>
 54 template <typename IsAlive, typename KeepAlive>
 55 void ShenandoahVMWeakRoots<CONCURRENT>::weak_oops_do(IsAlive* is_alive, KeepAlive* keep_alive, uint worker_id) {
 56   ShenandoahCleanUpdateWeakOopsClosure<CONCURRENT, IsAlive, KeepAlive> cl(is_alive, keep_alive);
 57   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::VMWeakRoots, worker_id);
 58   _weak_roots.oops_do(&cl);
 59 }
 60 
 61 template <bool CONCURRENT>
 62 void ShenandoahVMWeakRoots<CONCURRENT>::report_num_dead() {
 63   _weak_roots.report_num_dead();
 64 }
 65 
 66 template <bool CONCURRENT>
 67 ShenandoahVMRoots<CONCURRENT>::ShenandoahVMRoots(ShenandoahPhaseTimings::Phase phase) :
 68   _phase(phase) {
 69 }
 70 
 71 template <bool CONCURRENT>
 72 template <typename T>
 73 void ShenandoahVMRoots<CONCURRENT>::oops_do(T* cl, uint worker_id) {
 74   ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::VMStrongRoots, worker_id);
 75   _strong_roots.oops_do(cl);
 76 }
 77 
 78 template <bool CONCURRENT>
 79 ShenandoahClassLoaderDataRoots<CONCURRENT>::ShenandoahClassLoaderDataRoots(ShenandoahPhaseTimings::Phase phase, uint n_workers, bool heap_iteration) :
 80   _semaphore(worker_count(n_workers)),
 81   _phase(phase) {
 82   if (heap_iteration) {
 83     ClassLoaderDataGraph::clear_claimed_marks(ClassLoaderData::_claim_other);
 84   } else {
 85     ClassLoaderDataGraph::clear_claimed_marks();
 86   }
 87 
 88   if (CONCURRENT) {
 89     ClassLoaderDataGraph_lock->lock();
 90   }
 91 
 92   // Non-concurrent mode only runs at safepoints
 93   assert(CONCURRENT || SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
 94 }
 95 
 96 template <bool CONCURRENT>
 97 ShenandoahClassLoaderDataRoots<CONCURRENT>::~ShenandoahClassLoaderDataRoots() {
 98   if (CONCURRENT) {
 99     ClassLoaderDataGraph_lock->unlock();
100   }
101 }
102 
103 template <bool CONCURRENT>
104 void ShenandoahClassLoaderDataRoots<CONCURRENT>::cld_do_impl(CldDo f, CLDClosure* clds, uint worker_id) {
105   if (CONCURRENT) {
106     if (_semaphore.try_acquire()) {
107       ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::CLDGRoots, worker_id);
108       f(clds);
109       _semaphore.claim_all();
110     }
111   } else {
112     ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::CLDGRoots, worker_id);
113     f(clds);
114   }
115 }
116 
117 template <bool CONCURRENT>
118 void ShenandoahClassLoaderDataRoots<CONCURRENT>::always_strong_cld_do(CLDClosure* clds, uint worker_id) {
119   cld_do_impl(&ClassLoaderDataGraph::always_strong_cld_do, clds, worker_id);
120 }
121 
122 template <bool CONCURRENT>
123 void ShenandoahClassLoaderDataRoots<CONCURRENT>::cld_do(CLDClosure* clds, uint worker_id) {
124   cld_do_impl(&ClassLoaderDataGraph::cld_do, clds, worker_id);
125 }
126 
127 class ShenandoahParallelOopsDoThreadClosure : public ThreadClosure {
128 private:
129   OopClosure* _f;
130   CodeBlobClosure* _cf;
131   ThreadClosure* _thread_cl;
132 public:
133   ShenandoahParallelOopsDoThreadClosure(OopClosure* f, CodeBlobClosure* cf, ThreadClosure* thread_cl) :
134     _f(f), _cf(cf), _thread_cl(thread_cl) {}
135 
136   void do_thread(Thread* t) {
137     if (_thread_cl != NULL) {
138       _thread_cl->do_thread(t);
139     }
140     t->oops_do(_f, _cf);
141   }
142 };
143 
144 // The rationale for selecting the roots to scan is as follows:
145 //   a. With unload_classes = true, we only want to scan the actual strong roots from the
146 //      code cache. This will allow us to identify the dead classes, unload them, *and*
147 //      invalidate the relevant code cache blobs. This could be only done together with
148 //      class unloading.
149 //   b. With unload_classes = false, we have to nominally retain all the references from code
150 //      cache, because there could be the case of embedded class/oop in the generated code,
151 //      which we will never visit during mark. Without code cache invalidation, as in (a),
152 //      we risk executing that code cache blob, and crashing.
153 template <typename T>
154 void ShenandoahSTWRootScanner::roots_do(T* oops, uint worker_id) {
155   MarkingCodeBlobClosure blobs_cl(oops, !CodeBlobToOopClosure::FixRelocations, true /*FIXME*/);
156   CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
157   ResourceMark rm;
158 
159   if (_unload_classes) {
160     _thread_roots.oops_do(oops, &blobs_cl, worker_id);
161     _cld_roots.always_strong_cld_do(&clds, worker_id);
162   } else {
163     _thread_roots.oops_do(oops, NULL, worker_id);
164     _code_roots.code_blobs_do(&blobs_cl, worker_id);
165     _cld_roots.cld_do(&clds, worker_id);
166   }
167 
168   _vm_roots.oops_do<T>(oops, worker_id);
169 }
170 
171 template <typename IsAlive, typename KeepAlive>
172 void ShenandoahRootUpdater::roots_do(uint worker_id, IsAlive* is_alive, KeepAlive* keep_alive) {
173   CodeBlobToOopClosure update_blobs(keep_alive, CodeBlobToOopClosure::FixRelocations);
174   ShenandoahCodeBlobAndDisarmClosure blobs_and_disarm_Cl(keep_alive);
175   CodeBlobToOopClosure* codes_cl = (ClassUnloading && ShenandoahNMethodBarrier) ?
176                                    static_cast<CodeBlobToOopClosure*>(&blobs_and_disarm_Cl) :
177                                    static_cast<CodeBlobToOopClosure*>(&update_blobs);
178 
179   CLDToOopClosure clds(keep_alive, ClassLoaderData::_claim_strong);
180 
181   // Process light-weight/limited parallel roots then
182   _vm_roots.oops_do(keep_alive, worker_id);
183   _weak_roots.weak_oops_do<IsAlive, KeepAlive>(is_alive, keep_alive, worker_id);
184   _cld_roots.cld_do(&clds, worker_id);
185 
186   // Process heavy-weight/fully parallel roots the last
187   _code_roots.code_blobs_do(codes_cl, worker_id);
188   _thread_roots.oops_do(keep_alive, NULL, worker_id);
189 }
190 
191 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_INLINE_HPP