1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2019, 2021, 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 "precompiled.hpp"
27
28 #include "classfile/classLoaderDataGraph.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "code/codeBehaviours.hpp"
31 #include "code/codeCache.hpp"
32 #include "code/dependencyContext.hpp"
33 #include "gc/shared/gcBehaviours.hpp"
34 #include "gc/shared/classUnloadingContext.hpp"
35 #include "gc/shared/suspendibleThreadSet.hpp"
36 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
37 #include "gc/shenandoah/shenandoahLock.hpp"
38 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
39 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
40 #include "gc/shenandoah/shenandoahUnload.hpp"
41 #include "gc/shenandoah/shenandoahVerifier.hpp"
42 #include "memory/iterator.hpp"
43 #include "memory/metaspaceUtils.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "oops/access.inline.hpp"
46
47 class ShenandoahIsUnloadingOopClosure : public OopClosure {
48 private:
49 ShenandoahMarkingContext* const _marking_context;
50 bool _is_unloading;
51
52 public:
53 ShenandoahIsUnloadingOopClosure() :
54 _marking_context(ShenandoahHeap::heap()->complete_marking_context()),
55 _is_unloading(false) {
56 }
57
58 virtual void do_oop(oop* p) {
59 if (_is_unloading) {
60 return;
61 }
62
63 const oop o = RawAccess<>::oop_load(p);
64 if (!CompressedOops::is_null(o) &&
65 !_marking_context->is_marked(o)) {
66 _is_unloading = true;
67 }
68 }
69
70 virtual void do_oop(narrowOop* p) {
71 ShouldNotReachHere();
72 }
73
74 bool is_unloading() const {
75 return _is_unloading;
76 }
77 };
78
79 class ShenandoahIsUnloadingBehaviour : public IsUnloadingBehaviour {
80 public:
81 virtual bool has_dead_oop(nmethod* nm) const {
82 assert(ShenandoahHeap::heap()->is_concurrent_weak_root_in_progress(), "Only for this phase");
83 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
84 ShenandoahReentrantLocker locker(data->lock());
85 ShenandoahIsUnloadingOopClosure cl;
86 data->oops_do(&cl);
87 return cl.is_unloading();
88 }
89 };
90
91 class ShenandoahCompiledICProtectionBehaviour : public CompiledICProtectionBehaviour {
92 public:
93 virtual bool lock(nmethod* nm) {
94 ShenandoahReentrantLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
95 assert(lock != nullptr, "Not yet registered?");
96 lock->lock();
97 return true;
98 }
99
100 virtual void unlock(nmethod* nm) {
101 ShenandoahReentrantLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
102 assert(lock != nullptr, "Not yet registered?");
103 lock->unlock();
104 }
105
106 virtual bool is_safe(nmethod* nm) {
107 if (SafepointSynchronize::is_at_safepoint() || nm->is_unloading()) {
108 return true;
109 }
110
111 ShenandoahReentrantLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
112 assert(lock != nullptr, "Not yet registered?");
113 return lock->owned_by_self();
114 }
115 };
116
117 ShenandoahUnload::ShenandoahUnload() {
118 if (ClassUnloading) {
119 static ShenandoahIsUnloadingBehaviour is_unloading_behaviour;
120 IsUnloadingBehaviour::set_current(&is_unloading_behaviour);
121
122 static ShenandoahCompiledICProtectionBehaviour ic_protection_behaviour;
123 CompiledICProtectionBehaviour::set_current(&ic_protection_behaviour);
124 }
125 }
126
127 void ShenandoahUnload::prepare() {
128 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint");
129 assert(ClassUnloading, "Sanity");
130 CodeCache::increment_unloading_cycle();
131 DependencyContext::cleaning_start();
132 }
133
134 void ShenandoahUnload::unload() {
135 ShenandoahHeap* heap = ShenandoahHeap::heap();
136 assert(ClassUnloading, "Filtered by caller");
137 assert(heap->is_concurrent_weak_root_in_progress(), "Filtered by caller");
138
139 ClassUnloadingContext ctx(heap->workers()->active_workers(),
140 true /* unregister_nmethods_during_purge */,
141 true /* lock_nmethod_free_separately */);
142
143 // Unlink stale metadata and nmethods
144 {
145 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink);
146
147 SuspendibleThreadSetJoiner sts;
148 bool unloadingOccurred;
149 {
150 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_sd);
151 MutexLocker cldgMl(ClassLoaderDataGraph_lock);
152 unloadingOccurred = SystemDictionary::do_unloading(heap->gc_timer());
153 }
154
155 {
156 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_weak_klass);
157 Klass::clean_weak_klass_links(unloadingOccurred);
158 }
159
160 {
161 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_code_roots);
162 ShenandoahCodeRoots::unlink(heap->workers(), unloadingOccurred);
163 }
164
165 DependencyContext::cleaning_end();
166 }
167
168 // Make sure stale metadata and nmethods are no longer observable
169 {
170 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_rendezvous);
171 heap->rendezvous_threads();
172 }
173
174 // Purge stale metadata and nmethods that were unlinked
175 {
176 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge);
177
178 {
179 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_coderoots);
180 SuspendibleThreadSetJoiner sts;
181 ShenandoahCodeRoots::purge();
182 }
183
184 {
185 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_cldg);
186 ClassLoaderDataGraph::purge(false /* at_safepoint */);
187 }
188
189 {
190 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_ec);
191 CodeCache::purge_exception_caches();
192 }
193 }
194 }
195
196 void ShenandoahUnload::finish() {
197 MetaspaceGC::compute_new_size();
198 DEBUG_ONLY(MetaspaceUtils::verify();)
199 }
--- EOF ---