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 // TODO: In non-generational mode, this should still be complete_marking_context() 54 _marking_context(ShenandoahHeap::heap()->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(CompiledMethod* method) const { 82 nmethod* const nm = method->as_nmethod(); 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(CompiledMethod* method) { 95 nmethod* const nm = method->as_nmethod(); 96 ShenandoahReentrantLock* const lock = ShenandoahNMethod::lock_for_nmethod(nm); 97 assert(lock != nullptr, "Not yet registered?"); 98 lock->lock(); 99 return true; 100 } 101 102 virtual void unlock(CompiledMethod* method) { 103 nmethod* const nm = method->as_nmethod(); 104 ShenandoahReentrantLock* const lock = ShenandoahNMethod::lock_for_nmethod(nm); 105 assert(lock != nullptr, "Not yet registered?"); 106 lock->unlock(); 107 } 108 109 virtual bool is_safe(CompiledMethod* method) { 110 if (SafepointSynchronize::is_at_safepoint() || method->is_unloading()) { 111 return true; 112 } 113 114 nmethod* const nm = method->as_nmethod(); 115 ShenandoahReentrantLock* const lock = ShenandoahNMethod::lock_for_nmethod(nm); 116 assert(lock != nullptr, "Not yet registered?"); 117 return lock->owned_by_self(); 118 } 119 }; 120 121 ShenandoahUnload::ShenandoahUnload() { 122 if (ClassUnloading) { 123 static ShenandoahIsUnloadingBehaviour is_unloading_behaviour; 124 IsUnloadingBehaviour::set_current(&is_unloading_behaviour); 125 126 static ShenandoahCompiledICProtectionBehaviour ic_protection_behaviour; 127 CompiledICProtectionBehaviour::set_current(&ic_protection_behaviour); 128 } 129 } 130 131 void ShenandoahUnload::prepare() { 132 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 133 assert(ClassUnloading, "Sanity"); 134 CodeCache::increment_unloading_cycle(); 135 DependencyContext::cleaning_start(); 136 } 137 138 void ShenandoahUnload::unload() { 139 ShenandoahHeap* heap = ShenandoahHeap::heap(); 140 assert(ClassUnloading, "Filtered by caller"); 141 assert(heap->is_concurrent_weak_root_in_progress(), "Filtered by caller"); 142 143 ClassUnloadingContext ctx(heap->workers()->active_workers(), 144 true /* unregister_nmethods_during_purge */, 145 true /* lock_codeblob_free_separately */); 146 147 // Unlink stale metadata and nmethods 148 { 149 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink); 150 151 SuspendibleThreadSetJoiner sts; 152 bool unloadingOccurred; 153 { 154 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_sd); 155 MutexLocker cldgMl(ClassLoaderDataGraph_lock); 156 unloadingOccurred = SystemDictionary::do_unloading(heap->gc_timer()); 157 } 158 159 { 160 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_weak_klass); 161 Klass::clean_weak_klass_links(unloadingOccurred); 162 } 163 164 { 165 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_code_roots); 166 ShenandoahCodeRoots::unlink(heap->workers(), unloadingOccurred); 167 } 168 169 DependencyContext::cleaning_end(); 170 } 171 172 // Make sure stale metadata and nmethods are no longer observable 173 { 174 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_rendezvous); 175 heap->rendezvous_threads(); 176 } 177 178 // Purge stale metadata and nmethods that were unlinked 179 { 180 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge); 181 182 { 183 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_coderoots); 184 SuspendibleThreadSetJoiner sts; 185 ShenandoahCodeRoots::purge(); 186 } 187 188 { 189 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_cldg); 190 ClassLoaderDataGraph::purge(false /* at_safepoint */); 191 } 192 193 { 194 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_ec); 195 CodeCache::purge_exception_caches(); 196 } 197 } 198 } 199 200 void ShenandoahUnload::finish() { 201 MetaspaceGC::compute_new_size(); 202 DEBUG_ONLY(MetaspaceUtils::verify();) 203 }