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