1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. 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 #include "precompiled.hpp" 25 #include "classfile/classLoaderDataGraph.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "code/codeBehaviours.hpp" 28 #include "code/codeCache.hpp" 29 #include "code/dependencyContext.hpp" 30 #include "gc/shared/gcBehaviours.hpp" 31 #include "gc/shared/suspendibleThreadSet.hpp" 32 #include "gc/z/zLock.inline.hpp" 33 #include "gc/z/zNMethod.hpp" 34 #include "gc/z/zOopClosures.hpp" 35 #include "gc/z/zStat.hpp" 36 #include "gc/z/zUnload.hpp" 37 #include "oops/access.inline.hpp" 38 #include "runtime/mutexLocker.inline.hpp" 39 40 static const ZStatSubPhase ZSubPhaseConcurrentClassesUnload("Concurrent Classes Unload"); 41 42 class ZIsUnloadingOopClosure : public OopClosure { 43 private: 44 ZPhantomIsAliveObjectClosure _is_alive; 45 bool _is_unloading; 46 47 public: 48 ZIsUnloadingOopClosure() : 49 _is_alive(), 50 _is_unloading(false) {} 51 52 virtual void do_oop(oop* p) { 53 const oop o = RawAccess<>::oop_load(p); 54 if (o != NULL && !_is_alive.do_object_b(o)) { 55 _is_unloading = true; 56 } 57 } 58 59 virtual void do_oop(narrowOop* p) { 60 ShouldNotReachHere(); 61 } 62 63 bool is_unloading() const { 64 return _is_unloading; 65 } 66 }; 67 68 class ZIsUnloadingBehaviour : public IsUnloadingBehaviour { 69 public: 70 virtual bool is_unloading(CompiledMethod* method) const { 71 nmethod* const nm = method->as_nmethod(); 72 ZReentrantLock* const lock = ZNMethod::lock_for_nmethod(nm); 73 ZLocker<ZReentrantLock> locker(lock); 74 ZIsUnloadingOopClosure cl; 75 ZNMethod::nmethod_oops_do(nm, &cl); 76 return cl.is_unloading(); 77 } 78 }; 79 80 class ZCompiledICProtectionBehaviour : public CompiledICProtectionBehaviour { 81 public: 82 virtual bool lock(CompiledMethod* method) { 83 nmethod* const nm = method->as_nmethod(); 84 ZReentrantLock* const lock = ZNMethod::lock_for_nmethod(nm); 85 lock->lock(); 86 return true; 87 } 88 89 virtual void unlock(CompiledMethod* method) { 90 nmethod* const nm = method->as_nmethod(); 91 ZReentrantLock* const lock = ZNMethod::lock_for_nmethod(nm); 92 lock->unlock(); 93 } 94 95 virtual bool is_safe(CompiledMethod* method) { 96 if (SafepointSynchronize::is_at_safepoint()) { 97 return true; 98 } 99 100 nmethod* const nm = method->as_nmethod(); 101 ZReentrantLock* const lock = ZNMethod::lock_for_nmethod(nm); 102 return lock->is_owned(); 103 } 104 }; 105 106 ZUnload::ZUnload(ZWorkers* workers) : 107 _workers(workers) { 108 109 if (!ClassUnloading) { 110 return; 111 } 112 113 static ZIsUnloadingBehaviour is_unloading_behaviour; 114 IsUnloadingBehaviour::set_current(&is_unloading_behaviour); 115 116 static ZCompiledICProtectionBehaviour ic_protection_behaviour; 117 CompiledICProtectionBehaviour::set_current(&ic_protection_behaviour); 118 } 119 120 void ZUnload::prepare() { 121 if (!ClassUnloading) { 122 return; 123 } 124 125 CodeCache::increment_unloading_cycle(); 126 DependencyContext::cleaning_start(); 127 } 128 129 void ZUnload::unlink() { 130 SuspendibleThreadSetJoiner sts; 131 bool unloading_occurred; 132 133 { 134 MutexLocker ml(ClassLoaderDataGraph_lock); 135 unloading_occurred = SystemDictionary::do_unloading(ZStatPhase::timer()); 136 } 137 138 Klass::clean_weak_klass_links(unloading_occurred); 139 140 ZNMethod::unlink(_workers, unloading_occurred); 141 142 DependencyContext::cleaning_end(); 143 } 144 145 void ZUnload::purge() { 146 { 147 SuspendibleThreadSetJoiner sts; 148 ZNMethod::purge(_workers); 149 } 150 151 ClassLoaderDataGraph::purge(); 152 CodeCache::purge_exception_caches(); 153 } 154 155 class ZUnloadRendezvousClosure : public ThreadClosure { 156 public: 157 void do_thread(Thread* thread) {} 158 }; 159 160 void ZUnload::unload() { 161 if (!ClassUnloading) { 162 return; 163 } 164 165 ZStatTimer timer(ZSubPhaseConcurrentClassesUnload); 166 167 // Unlink stale metadata and nmethods 168 unlink(); 169 170 // Make sure stale metadata and nmethods are no longer observable 171 ZUnloadRendezvousClosure cl; 172 Handshake::execute(&cl); 173 174 // Purge stale metadata and nmethods that were unlinked 175 purge(); 176 } 177 178 void ZUnload::finish() { 179 // Resize and verify metaspace 180 MetaspaceGC::compute_new_size(); 181 MetaspaceUtils::verify_metrics(); 182 }