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 class ShenandoahUnloadRendezvousClosure : public HandshakeClosure {
 140 public:
 141   ShenandoahUnloadRendezvousClosure() : HandshakeClosure("ShenandoahUnloadRendezvous") {}
 142   void do_thread(Thread* thread) {}
 143 };
 144 
 145 void ShenandoahUnload::unload() {
 146   assert(ShenandoahConcurrentRoots::can_do_concurrent_class_unloading(), "Why we here?");
 147 
 148   ShenandoahHeap* heap = ShenandoahHeap::heap();
 149 
 150   if (!heap->is_concurrent_weak_root_in_progress()) {
 151     return;
 152   }
 153 
 154   // Unlink stale metadata and nmethods
 155   {
 156     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink);
 157 
 158     SuspendibleThreadSetJoiner sts;
 159     bool unloadingOccurred;
 160     {
 161       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_sd);
 162       MutexLocker cldgMl(ClassLoaderDataGraph_lock);
 163       unloadingOccurred = SystemDictionary::do_unloading(heap->gc_timer());
 164     }
 165 
 166     {
 167       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_weak_klass);
 168       Klass::clean_weak_klass_links(unloadingOccurred);
 169     }
 170 
 171     {
 172       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_unlink_code_roots);
 173       ShenandoahCodeRoots::unlink(heap->workers(), unloadingOccurred);
 174     }
 175 
 176     DependencyContext::cleaning_end();
 177   }
 178 
 179   // Make sure stale metadata and nmethods are no longer observable
 180   {
 181     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_rendezvous);
 182     ShenandoahUnloadRendezvousClosure cl;
 183     Handshake::execute(&cl);
 184   }
 185 
 186   // Purge stale metadata and nmethods that were unlinked
 187   {
 188     ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge);
 189 
 190     {
 191       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_coderoots);
 192       SuspendibleThreadSetJoiner sts;
 193       ShenandoahCodeRoots::purge(heap->workers());
 194     }
 195 
 196     {
 197       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_cldg);
 198       ClassLoaderDataGraph::purge();
 199     }
 200 
 201     {
 202       ShenandoahTimingsTracker t(ShenandoahPhaseTimings::conc_class_unload_purge_ec);
 203       CodeCache::purge_exception_caches();
 204     }
 205   }
 206 }
 207 
 208 void ShenandoahUnload::finish() {
 209   MetaspaceGC::compute_new_size();
 210   MetaspaceUtils::verify_metrics();
 211 }