1 /* 2 * Copyright (c) 2019, 2020, 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/shenandoahClosures.inline.hpp" 35 #include "gc/shenandoah/shenandoahCodeRoots.hpp" 36 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp" 37 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 38 #include "gc/shenandoah/shenandoahNMethod.inline.hpp" 39 #include "gc/shenandoah/shenandoahLock.hpp" 40 #include "gc/shenandoah/shenandoahPhaseTimings.hpp" 41 #include "gc/shenandoah/shenandoahRootProcessor.hpp" 42 #include "gc/shenandoah/shenandoahUnload.hpp" 43 #include "gc/shenandoah/shenandoahVerifier.hpp" 44 #include "memory/iterator.hpp" 45 #include "memory/resourceArea.hpp" 46 #include "oops/access.inline.hpp" 47 48 class ShenandoahIsUnloadingOopClosure : public OopClosure { 49 private: 50 ShenandoahMarkingContext* const _marking_context; 51 bool _is_unloading; 52 53 public: 54 ShenandoahIsUnloadingOopClosure() : 55 _marking_context(ShenandoahHeap::heap()->complete_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 is_unloading(CompiledMethod* method) const { 83 nmethod* const nm = method->as_nmethod(); 84 assert(ShenandoahHeap::heap()->is_concurrent_weak_root_in_progress(), "Only for this phase"); 85 ShenandoahNMethod* data = ShenandoahNMethod::gc_data(nm); 86 ShenandoahReentrantLocker locker(data->lock()); 87 ShenandoahIsUnloadingOopClosure cl; 88 data->oops_do(&cl); 89 return cl.is_unloading(); 90 } 91 }; 92 93 class ShenandoahCompiledICProtectionBehaviour : public CompiledICProtectionBehaviour { 94 public: 95 virtual bool lock(CompiledMethod* method) { 96 nmethod* const nm = method->as_nmethod(); 97 ShenandoahReentrantLock* const lock = ShenandoahNMethod::lock_for_nmethod(nm); 98 assert(lock != NULL, "Not yet registered?"); 99 lock->lock(); 100 return true; 101 } 102 103 virtual void unlock(CompiledMethod* method) { 104 nmethod* const nm = method->as_nmethod(); 105 ShenandoahReentrantLock* const lock = ShenandoahNMethod::lock_for_nmethod(nm); 106 assert(lock != NULL, "Not yet registered?"); 107 lock->unlock(); 108 } 109 110 virtual bool is_safe(CompiledMethod* method) { 111 if (SafepointSynchronize::is_at_safepoint()) { 112 return true; 113 } 114 115 nmethod* const nm = method->as_nmethod(); 116 ShenandoahReentrantLock* const lock = ShenandoahNMethod::lock_for_nmethod(nm); 117 assert(lock != NULL, "Not yet registered?"); 118 return lock->owned_by_self(); 119 } 120 }; 121 122 ShenandoahUnload::ShenandoahUnload() { 123 if (ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) { 124 static ShenandoahIsUnloadingBehaviour is_unloading_behaviour; 125 IsUnloadingBehaviour::set_current(&is_unloading_behaviour); 126 127 static ShenandoahCompiledICProtectionBehaviour ic_protection_behaviour; 128 CompiledICProtectionBehaviour::set_current(&ic_protection_behaviour); 129 } 130 } 131 132 void ShenandoahUnload::prepare() { 133 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 134 assert(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), "Sanity"); 135 CodeCache::increment_unloading_cycle(); 136 DependencyContext::cleaning_start(); 137 } 138 139 void ShenandoahUnload::unload() { 140 ShenandoahHeap* heap = ShenandoahHeap::heap(); 141 assert(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), "Filtered by caller"); 142 assert(heap->is_concurrent_weak_root_in_progress(), "Filtered by caller"); 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 ShenandoahRendezvousClosure cl; 173 Handshake::execute(&cl); 174 } 175 176 // Purge stale metadata and nmethods that were unlinked 177 { 178 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge); 179 180 { 181 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_coderoots); 182 SuspendibleThreadSetJoiner sts; 183 ShenandoahCodeRoots::purge(heap->workers()); 184 } 185 186 { 187 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_cldg); 188 ClassLoaderDataGraph::purge(); 189 } 190 191 { 192 ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_ec); 193 CodeCache::purge_exception_caches(); 194 } 195 } 196 } 197 198 void ShenandoahUnload::finish() { 199 MetaspaceGC::compute_new_size(); 200 DEBUG_ONLY(MetaspaceUtils::verify(false);) 201 }