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