1 /* 2 * Copyright (c) 2019, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "classfile/classLoaderDataGraph.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "code/codeBehaviours.hpp" 29 #include "code/codeCache.hpp" 30 #include "code/dependencyContext.hpp" 31 #include "gc/shared/gcBehaviours.hpp" 32 #include "gc/shared/suspendibleThreadSet.hpp" 33 #include "gc/shenandoah/shenandoahClosures.inline.hpp" 34 #include "gc/shenandoah/shenandoahCodeRoots.hpp" 35 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp" 36 #include "gc/shenandoah/shenandoahNMethod.inline.hpp" 37 #include "gc/shenandoah/shenandoahLock.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/resourceArea.hpp" 43 #include "oops/access.inline.hpp" 44 45 class ShenandoahIsUnloadingOopClosure : public OopClosure { 46 private: 47 ShenandoahMarkingContext* _marking_context; 48 bool _is_unloading; 49 50 public: 51 ShenandoahIsUnloadingOopClosure() : 52 _marking_context(ShenandoahHeap::heap()->marking_context()), 53 _is_unloading(false) { 54 } 55 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_complete() && 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 is_unloading(CompiledMethod* method) const { 81 nmethod* const nm = method->as_nmethod(); 82 guarantee(ShenandoahHeap::heap()->is_evacuation_in_progress(), "Only 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 != NULL, "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 != NULL, "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 != NULL, "Not yet registered?"); 116 return lock->owned_by_self(); 117 } 118 }; 119 120 ShenandoahUnload::ShenandoahUnload() { 121 if (ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) { 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(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), "Sanity"); 133 CodeCache::increment_unloading_cycle(); 134 DependencyContext::cleaning_start(); 135 } 136 137 void ShenandoahUnload::unlink() { 138 SuspendibleThreadSetJoiner sts; 139 bool unloading_occurred; 140 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 141 { 142 MutexLocker cldg_ml(ClassLoaderDataGraph_lock); 143 unloading_occurred = SystemDictionary::do_unloading(heap->gc_timer()); 144 } 145 146 Klass::clean_weak_klass_links(unloading_occurred); 147 ShenandoahCodeRoots::unlink(ShenandoahHeap::heap()->workers(), unloading_occurred); 148 DependencyContext::cleaning_end(); 149 } 150 151 void ShenandoahUnload::purge() { 152 { 153 SuspendibleThreadSetJoiner sts; 154 ShenandoahCodeRoots::purge(ShenandoahHeap::heap()->workers()); 155 } 156 157 ClassLoaderDataGraph::purge(); 158 CodeCache::purge_exception_caches(); 159 } 160 161 class ShenandoahUnloadRendezvousClosure : public HandshakeClosure { 162 public: 163 ShenandoahUnloadRendezvousClosure() : HandshakeClosure("ShenandoahUnloadRendezvous") {} 164 void do_thread(Thread* thread) {} 165 }; 166 167 void ShenandoahUnload::unload() { 168 assert(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), "Why we here?"); 169 if (!ShenandoahHeap::heap()->is_evacuation_in_progress()) { 170 return; 171 } 172 173 // Unlink stale metadata and nmethods 174 unlink(); 175 176 // Make sure stale metadata and nmethods are no longer observable 177 ShenandoahUnloadRendezvousClosure cl; 178 Handshake::execute(&cl); 179 180 // Purge stale metadata and nmethods that were unlinked 181 purge(); 182 } 183 184 void ShenandoahUnload::finish() { 185 MetaspaceGC::compute_new_size(); 186 MetaspaceUtils::verify_metrics(); 187 }