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/heuristics/shenandoahHeuristics.hpp"
33 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
34 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
35 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
36 #include "gc/shenandoah/shenandoahUtils.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::VMWeaks, 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::VMWeaks, 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::VMStrongs, 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(ClassLoaderData::_claim_strong);
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::Classes, worker_id);
108 f(clds);
109 _semaphore.claim_all();
110 }
111 } else {
112 ShenandoahWorkerTimingsTracker timer(_phase, ShenandoahPhaseTimings::Classes, 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 NMethodClosure* _cf;
131 ThreadClosure* _thread_cl;
132 public:
133 ShenandoahParallelOopsDoThreadClosure(OopClosure* f, NMethodClosure* cf, ThreadClosure* thread_cl) :
134 _f(f), _cf(cf), _thread_cl(thread_cl) {}
135
136 void do_thread(Thread* t) {
137 if (_thread_cl != nullptr) {
138 _thread_cl->do_thread(t);
139 }
140 t->oops_do(_f, _cf);
141 }
142 };
143
144 class ShenandoahInvisibleRootsMarkClosure : public ThreadClosure {
145 public:
146 void do_thread(Thread* t) {
147 assert_at_safepoint();
148
149 HeapWord* invisible_root = ShenandoahThreadLocalData::get_invisible_root(t);
150 if (invisible_root == nullptr) {
151 return;
152 }
153 size_t invisible_root_word_size = ShenandoahThreadLocalData::get_invisible_root_word_size(t);
154
155 ShenandoahHeap* const heap = ShenandoahHeap::heap();
156 ShenandoahMarkingContext* const marking_context = heap->marking_context();
157 // Mark the invisible root if it is not marked.
158 if (!marking_context->is_marked(invisible_root)) {
159 bool was_upgraded = false;
160 if (!marking_context->mark_strong(cast_to_oop(invisible_root), was_upgraded)) {
161 return;
162 }
163
164 // Update region liveness data
165 ShenandoahHeapRegion* region = heap->heap_region_containing(invisible_root);
166 if (region->is_regular_or_regular_pinned()) {
167 assert(!ShenandoahHeapRegion::requires_humongous(invisible_root_word_size), "Must not be humongous.");
168 region->increase_live_data_alloc_words(invisible_root_word_size);
169 } else if (region->is_humongous_start()) {
170 DEBUG_ONLY(size_t total_live_words = 0;)
171 do {
172 size_t current = region->get_live_data_words();
173 size_t region_used_words = region->used() >> LogHeapWordSize;
174 DEBUG_ONLY(total_live_words += region_used_words;)
175 assert(current == 0 || current == region_used_words, "Must be");
176 if (current == 0) {
177 region->increase_live_data_alloc_words(region_used_words);
178 }
179 region = heap->get_region(region->index() + 1);
180 } while (region != nullptr && region->is_humongous_continuation());
181 assert(total_live_words == invisible_root_word_size, "Must be");
182 }
183 }
184 }
185 };
186
187 // The rationale for selecting the roots to scan is as follows:
188 // a. With unload_classes = true, we only want to scan the actual strong roots from the
189 // code cache. This will allow us to identify the dead classes, unload them, *and*
190 // invalidate the relevant code cache blobs. This could be only done together with
191 // class unloading.
192 // b. With unload_classes = false, we have to nominally retain all the references from code
193 // cache, because there could be the case of embedded class/oop in the generated code,
194 // which we will never visit during mark. Without code cache invalidation, as in (a),
195 // we risk executing that code cache blob, and crashing.
196 template <typename T>
197 void ShenandoahSTWRootScanner::roots_do(T* oops, uint worker_id) {
198 MarkingNMethodClosure nmethods_cl(oops);
199 CLDToOopClosure clds(oops, ClassLoaderData::_claim_strong);
200 ResourceMark rm;
201
202 if (_unload_classes) {
203 _thread_roots.oops_do(oops, &nmethods_cl, worker_id);
204 _cld_roots.always_strong_cld_do(&clds, worker_id);
205 } else {
206 _thread_roots.oops_do(oops, nullptr, worker_id);
207 _code_roots.nmethods_do(&nmethods_cl, worker_id);
208 _cld_roots.cld_do(&clds, worker_id);
209 }
210
211 _vm_roots.oops_do<T>(oops, worker_id);
212 }
213
214 template <typename IsAlive, typename KeepAlive>
215 void ShenandoahRootUpdater::roots_do(uint worker_id, IsAlive* is_alive, KeepAlive* keep_alive) {
216 NMethodToOopClosure update_nmethods(keep_alive, NMethodToOopClosure::FixRelocations);
217 ShenandoahNMethodAndDisarmClosure nmethods_and_disarm_Cl(keep_alive);
218 CLDToOopClosure clds(keep_alive, ClassLoaderData::_claim_strong);
219
220 // Process light-weight/limited parallel roots then
221 _vm_roots.oops_do(keep_alive, worker_id);
222 _weak_roots.weak_oops_do<IsAlive, KeepAlive>(is_alive, keep_alive, worker_id);
223 _cld_roots.cld_do(&clds, worker_id);
224
225 // Process heavy-weight/fully parallel roots the last
226 _code_roots.nmethods_do(&nmethods_and_disarm_Cl, worker_id);
227 _thread_roots.oops_do(keep_alive, nullptr, worker_id);
228 }
229
230 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_INLINE_HPP