1 /*
2 * Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2017, 2022, Red Hat, Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "code/codeCache.hpp"
27 #include "code/nmethod.hpp"
28 #include "gc/shared/classUnloadingContext.hpp"
29 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
30 #include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"
31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
32 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
33 #include "gc/shenandoah/shenandoahUtils.hpp"
34 #include "memory/resourceArea.hpp"
35 #include "memory/universe.hpp"
36 #include "runtime/atomicAccess.hpp"
37 #include "utilities/powerOfTwo.hpp"
38
39
40 ShenandoahNMethodTable* ShenandoahCodeRoots::_nmethod_table;
41 int ShenandoahCodeRoots::_disarmed_value = 1;
42
43 bool ShenandoahCodeRoots::use_nmethod_barriers_for_mark() {
44 // Continuations need nmethod barriers for scanning stack chunk nmethods.
45 if (Continuations::enabled()) return true;
46
47 // Concurrent class unloading needs nmethod barriers.
48 // When a nmethod is about to be executed, we need to make sure that all its
49 // metadata are marked. The alternative is to remark thread roots at final mark
50 // pause, which would cause latency issues.
51 if (ShenandoahHeap::heap()->unload_classes()) return true;
52
53 // First pause: need to enable GC barriers for GC cycle.
54 if (ShenandoahGCStateCheckHotpatch) return true;
55
56 // Otherwise, we can go without nmethod barriers.
57 return false;
58 }
59
60 void ShenandoahCodeRoots::initialize() {
61 _nmethod_table = new ShenandoahNMethodTable();
62 }
63
64 void ShenandoahCodeRoots::register_nmethod(nmethod* nm) {
65 assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
66 _nmethod_table->register_nmethod(nm);
67 }
68
69 void ShenandoahCodeRoots::unregister_nmethod(nmethod* nm) {
70 assert_locked_or_safepoint(CodeCache_lock);
71 _nmethod_table->unregister_nmethod(nm);
72 }
73
74 void ShenandoahCodeRoots::arm_nmethods() {
75 if (ShenandoahGCStateCheckHotpatch) {
76 char gc_state = ShenandoahHeap::heap()->gc_state();
77 log_info(gc)("Arming nmethods with GC state: %d [%s%s%s%s%s%s%s]",
78 gc_state,
79 ((gc_state & ShenandoahHeap::HAS_FORWARDED) > 0) ? "HAS_FORWARDED " : "",
80 ((gc_state & ShenandoahHeap::MARKING) > 0) ? "MARKING " : "",
81 ((gc_state & ShenandoahHeap::EVACUATION) > 0) ? "EVACUATION " : "",
82 ((gc_state & ShenandoahHeap::UPDATE_REFS) > 0) ? "UPDATE_REFS " : "",
83 ((gc_state & ShenandoahHeap::WEAK_ROOTS) > 0) ? "WEAK_ROOTS " : "",
84 ((gc_state & ShenandoahHeap::YOUNG_MARKING) > 0) ? "YOUNG_MARKING " : "",
85 ((gc_state & ShenandoahHeap::OLD_MARKING) > 0) ? "OLD_MARKING " : ""
86 );
87 }
88 BarrierSet::barrier_set()->barrier_set_nmethod()->arm_all_nmethods();
89 }
90
91 void ShenandoahCodeRoots::arm_nmethods_for_mark() {
92 if (use_nmethod_barriers_for_mark()) {
93 arm_nmethods();
94 }
95 }
96
97 void ShenandoahCodeRoots::arm_nmethods_for_evac() {
98 arm_nmethods();
99 }
100
101 class ShenandoahDisarmNMethodClosure : public NMethodClosure {
102 private:
103 BarrierSetNMethod* const _bs;
104
105 public:
106 ShenandoahDisarmNMethodClosure() :
107 _bs(BarrierSet::barrier_set()->barrier_set_nmethod()) {
108 }
109
110 virtual void do_nmethod(nmethod* nm) {
111 _bs->disarm(nm);
112 }
113 };
114
115 class ShenandoahDisarmNMethodsTask : public WorkerTask {
116 private:
117 ShenandoahDisarmNMethodClosure _cl;
118 ShenandoahConcurrentNMethodIterator _iterator;
119
120 public:
121 ShenandoahDisarmNMethodsTask() :
122 WorkerTask("Shenandoah Disarm NMethods"),
123 _iterator(ShenandoahCodeRoots::table()) {
124 assert(SafepointSynchronize::is_at_safepoint(), "Only at a safepoint");
125 }
126
127 virtual void work(uint worker_id) {
128 ShenandoahParallelWorkerSession worker_session(worker_id);
129 _iterator.nmethods_do(&_cl);
130 }
131 };
132
133 void ShenandoahCodeRoots::disarm_nmethods() {
134 if (use_nmethod_barriers_for_mark()) {
135 ShenandoahDisarmNMethodsTask task;
136 ShenandoahHeap::heap()->workers()->run_task(&task);
137 }
138 }
139
140 class ShenandoahNMethodUnlinkClosure : public NMethodClosure {
141 private:
142 bool _unloading_occurred;
143 ShenandoahHeap* const _heap;
144 BarrierSetNMethod* const _bs;
145
146 public:
147 ShenandoahNMethodUnlinkClosure(bool unloading_occurred) :
148 _unloading_occurred(unloading_occurred),
149 _heap(ShenandoahHeap::heap()),
150 _bs(ShenandoahBarrierSet::barrier_set()->barrier_set_nmethod()) {}
151
152 virtual void do_nmethod(nmethod* nm) {
153 assert(_heap->is_concurrent_weak_root_in_progress(), "Only this phase");
154
155 ShenandoahNMethod* nm_data = ShenandoahNMethod::gc_data(nm);
156 assert(!nm_data->is_unregistered(), "Should not see unregistered entry");
157
158 if (nm->is_unloading()) {
159 ShenandoahNMethodLocker locker(nm_data->lock());
160 nm->unlink();
161 return;
162 }
163
164 {
165 ShenandoahNMethodLocker locker(nm_data->lock());
166
167 // Heal oops
168 if (_bs->is_armed(nm)) {
169 ShenandoahEvacOOMScope oom_evac_scope;
170 ShenandoahNMethod::heal_nmethod_metadata(nm_data);
171 // Must remain armed to complete remaining work in nmethod entry barrier
172 assert(_bs->is_armed(nm), "Should remain armed");
173 }
174 }
175
176 // Clear compiled ICs and exception caches
177 ShenandoahNMethodLocker locker(nm_data->ic_lock());
178 nm->unload_nmethod_caches(_unloading_occurred);
179 }
180 };
181
182 class ShenandoahUnlinkTask : public WorkerTask {
183 private:
184 ShenandoahNMethodUnlinkClosure _cl;
185 ShenandoahConcurrentNMethodIterator _iterator;
186
187 public:
188 ShenandoahUnlinkTask(bool unloading_occurred) :
189 WorkerTask("Shenandoah Unlink NMethods"),
190 _cl(unloading_occurred),
191 _iterator(ShenandoahCodeRoots::table()) {}
192
193 virtual void work(uint worker_id) {
194 _iterator.nmethods_do(&_cl);
195 }
196 };
197
198 void ShenandoahCodeRoots::unlink(WorkerThreads* workers, bool unloading_occurred) {
199 assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
200
201 ShenandoahUnlinkTask task(unloading_occurred);
202 workers->run_task(&task);
203 }
204
205 void ShenandoahCodeRoots::purge() {
206 assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
207
208 ClassUnloadingContext::context()->purge_and_free_nmethods();
209 }
210
211 ShenandoahCodeRootsIterator::ShenandoahCodeRootsIterator() :
212 _table_snapshot(nullptr) {
213 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
214 MutexLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
215 _table_snapshot = ShenandoahCodeRoots::table()->snapshot_for_iteration();
216 }
217
218 ShenandoahCodeRootsIterator::~ShenandoahCodeRootsIterator() {
219 MonitorLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
220 ShenandoahCodeRoots::table()->finish_iteration(_table_snapshot);
221 _table_snapshot = nullptr;
222 locker.notify_all();
223 }
224
225 void ShenandoahCodeRootsIterator::possibly_parallel_nmethods_do(NMethodClosure *f) {
226 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
227 assert(_table_snapshot != nullptr, "Sanity");
228 _table_snapshot->parallel_nmethods_do(f);
229 }