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     // TODO: In non-generational mode, this should still be complete_marking_context()
 55     _marking_context(ShenandoahHeap::heap()->marking_context()),
 56     _is_unloading(false) {
 57   }
 58 
 59   virtual void do_oop(oop* p) {
 60     if (_is_unloading) {
 61       return;
 62     }
 63 
 64     const oop o = RawAccess<>::oop_load(p);
 65     if (!CompressedOops::is_null(o) &&
 66         !_marking_context->is_marked(o)) {
 67       _is_unloading = true;
 68     }
 69   }
 70 
 71   virtual void do_oop(narrowOop* p) {
 72     ShouldNotReachHere();
 73   }
 74 
 75   bool is_unloading() const {
 76     return _is_unloading;
 77   }
 78 };
 79 
 80 class ShenandoahIsUnloadingBehaviour : public IsUnloadingBehaviour {
 81 public:
 82   virtual bool has_dead_oop(nmethod* nm) const {
 83     assert(ShenandoahHeap::heap()->is_concurrent_weak_root_in_progress(), "Only for this phase");
 84     ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm);
 85     ShenandoahReentrantLocker locker(data->lock());
 86     ShenandoahIsUnloadingOopClosure cl;
 87     data->oops_do(&cl);
 88     return  cl.is_unloading();
 89   }
 90 };
 91 
 92 class ShenandoahCompiledICProtectionBehaviour : public CompiledICProtectionBehaviour {
 93 public:
 94   virtual bool lock(nmethod* nm) {
 95     ShenandoahReentrantLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
 96     assert(lock != nullptr, "Not yet registered?");
 97     lock->lock();
 98     return true;
 99   }
100 
101   virtual void unlock(nmethod* nm) {
102     ShenandoahReentrantLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
103     assert(lock != nullptr, "Not yet registered?");
104     lock->unlock();
105   }
106 
107   virtual bool is_safe(nmethod* nm) {
108     if (SafepointSynchronize::is_at_safepoint() || nm->is_unloading()) {
109       return true;
110     }
111 
112     ShenandoahReentrantLock* const lock = ShenandoahNMethod::ic_lock_for_nmethod(nm);
113     assert(lock != nullptr, "Not yet registered?");
114     return lock->owned_by_self();
115   }
116 };
117 
118 ShenandoahUnload::ShenandoahUnload() {
119   if (ClassUnloading) {
120     static ShenandoahIsUnloadingBehaviour is_unloading_behaviour;
121     IsUnloadingBehaviour::set_current(&is_unloading_behaviour);
122 
123     static ShenandoahCompiledICProtectionBehaviour ic_protection_behaviour;
124     CompiledICProtectionBehaviour::set_current(&ic_protection_behaviour);
125   }
126 }
127 
128 void ShenandoahUnload::prepare() {
129   assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint");
130   assert(ClassUnloading, "Sanity");
131   CodeCache::increment_unloading_cycle();
132   DependencyContext::cleaning_start();
133 }
134 
135 void ShenandoahUnload::unload() {
136   ShenandoahHeap* heap = ShenandoahHeap::heap();
137   assert(ClassUnloading, "Filtered by caller");
138   assert(heap->is_concurrent_weak_root_in_progress(), "Filtered by caller");
139 
140   ClassUnloadingContext ctx(heap->workers()->active_workers(),
141                             true /* unregister_nmethods_during_purge */,
142                             true /* lock_nmethod_free_separately */);
143 
144   // Unlink stale metadata and nmethods
145   {
146     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink);
147 
148     SuspendibleThreadSetJoiner sts;
149     bool unloadingOccurred;
150     {
151       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_sd);
152       MutexLocker cldgMl(ClassLoaderDataGraph_lock);
153       unloadingOccurred = SystemDictionary::do_unloading(heap->gc_timer());
154     }
155 
156     {
157       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_weak_klass);
158       Klass::clean_weak_klass_links(unloadingOccurred);
159     }
160 
161     {
162       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_code_roots);
163       ShenandoahCodeRoots::unlink(heap->workers(), unloadingOccurred);
164     }
165 
166     DependencyContext::cleaning_end();
167   }
168 
169   // Make sure stale metadata and nmethods are no longer observable
170   {
171     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_rendezvous);
172     heap->rendezvous_threads();
173   }
174 
175   // Purge stale metadata and nmethods that were unlinked
176   {
177     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge);
178 
179     {
180       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_coderoots);
181       SuspendibleThreadSetJoiner sts;
182       ShenandoahCodeRoots::purge();
183     }
184 
185     {
186       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_cldg);
187       ClassLoaderDataGraph::purge(false /* at_safepoint */);
188     }
189 
190     {
191       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_ec);
192       CodeCache::purge_exception_caches();
193     }
194   }
195 }
196 
197 void ShenandoahUnload::finish() {
198   MetaspaceGC::compute_new_size();
199   DEBUG_ONLY(MetaspaceUtils::verify();)
200 }
--- EOF ---