0 /*
1  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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 #include "precompiled.hpp"
24 #include "classfile/classLoaderDataGraph.hpp"
25 #include "classfile/systemDictionary.hpp"
26 #include "code/codeBehaviours.hpp"
27 #include "code/codeCache.hpp"
28 #include "code/dependencyContext.hpp"
29 #include "gc/shared/gcBehaviours.hpp"
30 #include "gc/shared/suspendibleThreadSet.hpp"
31 #include "gc/z/zLock.inline.hpp"
32 #include "gc/z/zNMethod.hpp"
33 #include "gc/z/zOopClosures.hpp"
34 #include "gc/z/zStat.hpp"
35 #include "gc/z/zUnload.hpp"
36 #include "oops/access.inline.hpp"
37 
38 static const ZStatSubPhase ZSubPhaseConcurrentClassesUnload("Concurrent Classes Unload");
39 
40 class ZIsUnloadingOopClosure : public OopClosure {
41 private:
42   ZPhantomIsAliveObjectClosure _is_alive;
43   bool                         _is_unloading;
44 
45 public:
46   ZIsUnloadingOopClosure() :
47       _is_alive(),
48       _is_unloading(false) {}
49 
50   virtual void do_oop(oop* p) {
51     const oop o = RawAccess<>::oop_load(p);
52     if (o != NULL && !_is_alive.do_object_b(o)) {
53       _is_unloading = true;
54     }
55   }
56 
57   virtual void do_oop(narrowOop* p) {
58     ShouldNotReachHere();
59   }
60 
61   bool is_unloading() const {
62     return _is_unloading;
63   }
64 };
65 
66 class ZIsUnloadingBehaviour : public IsUnloadingBehaviour {







67 public:
68   virtual bool is_unloading(CompiledMethod* method) const {
69     nmethod* const nm = method->as_nmethod();
70     ZReentrantLock* const lock = ZNMethod::lock_for_nmethod(nm);
71     ZLocker<ZReentrantLock> locker(lock);
72     ZIsUnloadingOopClosure cl;
73     nm->oops_do(&cl, true /* allow_zombie */);
74     return cl.is_unloading();


75   }
76 };
77 
78 class ZCompiledICProtectionBehaviour : public CompiledICProtectionBehaviour {
79 public:
80   virtual bool lock(CompiledMethod* method) {
81     nmethod* const nm = method->as_nmethod();
82     ZReentrantLock* const lock = ZNMethod::lock_for_nmethod(nm);
83     lock->lock();


84     return true;
85   }
86 
87   virtual void unlock(CompiledMethod* method) {
88     nmethod* const nm = method->as_nmethod();
89     ZReentrantLock* const lock = ZNMethod::lock_for_nmethod(nm);
90     lock->unlock();


91   }
92 
93   virtual bool is_safe(CompiledMethod* method) {
94     if (SafepointSynchronize::is_at_safepoint()) {
95       return true;
96     }
97 
98     nmethod* const nm = method->as_nmethod();
99     ZReentrantLock* const lock = ZNMethod::lock_for_nmethod(nm);
100     return lock->is_owned();
101   }
102 };
103 
104 ZUnload::ZUnload(ZWorkers* workers) :
105     _workers(workers) {
106 
107   if (!ClassUnloading) {
108     return;
109   }
110 
111   static ZIsUnloadingBehaviour is_unloading_behaviour;
112   IsUnloadingBehaviour::set_current(&is_unloading_behaviour);
113 
114   static ZCompiledICProtectionBehaviour ic_protection_behaviour;
115   CompiledICProtectionBehaviour::set_current(&ic_protection_behaviour);
116 }
117 
118 void ZUnload::prepare() {
119   if (!ClassUnloading) {
120     return;
121   }
122 
123   CodeCache::increment_unloading_cycle();
124   DependencyContext::cleaning_start();
125 }
126 
127 void ZUnload::unlink() {
128   SuspendibleThreadSetJoiner sts;
129   bool unloading_occurred;
130 
131   {
132     MutexLockerEx ml(ClassLoaderDataGraph_lock);
133     unloading_occurred = SystemDictionary::do_unloading(ZStatPhase::timer());
134   }
135 
136   Klass::clean_weak_klass_links(unloading_occurred);
137 
138   ZNMethod::unlink(_workers, unloading_occurred);
139 
140   DependencyContext::cleaning_end();
141 }
142 
143 void ZUnload::purge() {
144   {
145     SuspendibleThreadSetJoiner sts;
146     ZNMethod::purge(_workers);
147   }
148 
149   ClassLoaderDataGraph::purge();
150   CodeCache::purge_exception_caches();
151 }
152 
153 class ZUnloadRendezvousClosure : public ThreadClosure {
154 public:
155   void do_thread(Thread* thread) {}
156 };
157 
158 void ZUnload::unload() {
159   if (!ClassUnloading) {
160     return;
161   }
162 
163   ZStatTimer timer(ZSubPhaseConcurrentClassesUnload);
164 
165   // Unlink stale metadata and nmethods
166   unlink();
167 
168   // Make sure stale metadata and nmethods are no longer observable
169   ZUnloadRendezvousClosure cl;
170   Handshake::execute(&cl);
171 
172   // Purge stale metadata and nmethods that were unlinked
173   purge();
174 }
175 
176 void ZUnload::finish() {
177   // Resize and verify metaspace
178   MetaspaceGC::compute_new_size();
179   MetaspaceUtils::verify_metrics();
180 }
--- EOF ---