1 /*
  2  * Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2017, 2022, Red Hat, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "code/codeCache.hpp"
 27 #include "code/nmethod.hpp"
 28 #include "gc/shared/classUnloadingContext.hpp"
 29 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 31 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
 32 #include "gc/shenandoah/shenandoahUtils.hpp"
 33 #include "memory/resourceArea.hpp"
 34 #include "memory/universe.hpp"
 35 #include "runtime/atomicAccess.hpp"
 36 #include "utilities/events.hpp"
 37 #include "utilities/powerOfTwo.hpp"
 38 
 39 
 40 ShenandoahNMethodTable* ShenandoahCodeRoots::_nmethod_table;
 41 int ShenandoahCodeRoots::_disarmed_value = 1;
 42 
 43 void ShenandoahCodeRoots::initialize() {
 44   _nmethod_table = new ShenandoahNMethodTable();
 45 }
 46 
 47 void ShenandoahCodeRoots::register_nmethod(nmethod* nm) {
 48   assert(CodeCache_lock->owned_by_self(), "Must have CodeCache_lock held");
 49   _nmethod_table->register_nmethod(nm);
 50 }
 51 
 52 void ShenandoahCodeRoots::unregister_nmethod(nmethod* nm) {
 53   assert_locked_or_safepoint(CodeCache_lock);
 54   _nmethod_table->unregister_nmethod(nm);
 55 }
 56 
 57 void ShenandoahCodeRoots::arm_nmethods() {
 58   char gc_state = ShenandoahHeap::heap()->gc_state();
 59   Events::log(Thread::current(), "Arming nmethods, GC state: %d [%s%s%s%s%s%s%s]",
 60       gc_state,
 61       ((gc_state & ShenandoahHeap::HAS_FORWARDED) > 0) ? "HAS_FORWARDED "  : "",
 62       ((gc_state & ShenandoahHeap::MARKING) > 0)       ? "MARKING "        : "",
 63       ((gc_state & ShenandoahHeap::EVACUATION) > 0)    ? "EVACUATION "     : "",
 64       ((gc_state & ShenandoahHeap::UPDATE_REFS) > 0)   ? "UPDATE_REFS "    : "",
 65       ((gc_state & ShenandoahHeap::WEAK_ROOTS) > 0)    ? "WEAK_ROOTS "     : "",
 66       ((gc_state & ShenandoahHeap::YOUNG_MARKING) > 0) ? "YOUNG_MARKING "  : "",
 67       ((gc_state & ShenandoahHeap::OLD_MARKING) > 0)   ? "OLD_MARKING "    : ""
 68   );
 69   log_info(gc)("Arming nmethods with GC state: %d [%s%s%s%s%s%s%s]",
 70        gc_state,
 71        ((gc_state & ShenandoahHeap::HAS_FORWARDED) > 0) ? "HAS_FORWARDED "  : "",
 72        ((gc_state & ShenandoahHeap::MARKING) > 0)       ? "MARKING "        : "",
 73        ((gc_state & ShenandoahHeap::EVACUATION) > 0)    ? "EVACUATION "     : "",
 74        ((gc_state & ShenandoahHeap::UPDATE_REFS) > 0)   ? "UPDATE_REFS "    : "",
 75        ((gc_state & ShenandoahHeap::WEAK_ROOTS) > 0)    ? "WEAK_ROOTS "     : "",
 76        ((gc_state & ShenandoahHeap::YOUNG_MARKING) > 0) ? "YOUNG_MARKING "  : "",
 77        ((gc_state & ShenandoahHeap::OLD_MARKING) > 0)   ? "OLD_MARKING "    : ""
 78   );
 79   BarrierSet::barrier_set()->barrier_set_nmethod()->arm_all_nmethods();
 80 }
 81 
 82 class ShenandoahDisarmNMethodClosure : public NMethodClosure {
 83 public:
 84   virtual void do_nmethod(nmethod* nm) {
 85     ShenandoahNMethod::disarm_nmethod(nm);
 86   }
 87 };
 88 
 89 class ShenandoahDisarmNMethodsTask : public WorkerTask {
 90 private:
 91   ShenandoahDisarmNMethodClosure      _cl;
 92   ShenandoahConcurrentNMethodIterator _iterator;
 93 
 94 public:
 95   ShenandoahDisarmNMethodsTask() :
 96     WorkerTask("Shenandoah Disarm NMethods"),
 97     _iterator(ShenandoahCodeRoots::table()) {}
 98 
 99   virtual void work(uint worker_id) {
100     _iterator.nmethods_do(&_cl);
101   }
102 };
103 
104 void ShenandoahCodeRoots::disarm_nmethods() {
105   ShenandoahDisarmNMethodsTask task;
106   ShenandoahHeap::heap()->workers()->run_task(&task);
107 }
108 
109 #ifdef ASSERT
110 class ShenandoahCheckNMethodClosure : public NMethodClosure {
111   bool const _armed;
112 public:
113   ShenandoahCheckNMethodClosure(int armed) : _armed(armed) {}
114   virtual void do_nmethod(nmethod* nm) {
115     ShenandoahNMethod::assert_barriers(nm, _armed);
116   }
117 };
118 
119 class ShenandoahCheckNMethodsTask : public WorkerTask {
120 private:
121   ShenandoahCheckNMethodClosure      _cl;
122   ShenandoahConcurrentNMethodIterator _iterator;
123 
124 public:
125   ShenandoahCheckNMethodsTask() :
126     WorkerTask("Shenandoah Check NMethods"),
127     _cl(!ShenandoahHeap::heap()->is_idle()),
128     _iterator(ShenandoahCodeRoots::table()) {}
129 
130   virtual void work(uint worker_id) {
131     _iterator.nmethods_do(&_cl);
132   }
133 };
134 
135 void ShenandoahCodeRoots::check_barriers() {
136   ShenandoahCheckNMethodsTask task;
137   ShenandoahHeap::heap()->workers()->run_task(&task);
138 }
139 #endif
140 
141 class ShenandoahNMethodUnlinkClosure : public NMethodClosure {
142 private:
143   bool                      _unloading_occurred;
144   ShenandoahHeap* const     _heap;
145   BarrierSetNMethod* const  _bs;
146 
147 public:
148   ShenandoahNMethodUnlinkClosure(bool unloading_occurred) :
149       _unloading_occurred(unloading_occurred),
150       _heap(ShenandoahHeap::heap()),
151       _bs(ShenandoahBarrierSet::barrier_set()->barrier_set_nmethod()) {}
152 
153   virtual void do_nmethod(nmethod* nm) {
154     assert(_heap->is_concurrent_weak_root_in_progress(), "Only this phase");
155 
156     ShenandoahNMethod* nm_data = ShenandoahNMethod::gc_data(nm);
157     assert(!nm_data->is_unregistered(), "Should not see unregistered entry");
158 
159     if (nm->is_unloading()) {
160       ShenandoahNMethodLocker locker(nm_data->lock());
161       nm->unlink();
162       return;
163     }
164 
165     {
166       ShenandoahNMethodLocker locker(nm_data->lock());
167 
168       // Heal oops
169       if (_bs->is_armed(nm)) {
170         ShenandoahNMethod::heal_nmethod_metadata(nm_data);
171         // Must remain armed to complete remaining work in nmethod entry barrier
172         assert(_bs->is_armed(nm), "Should remain armed");
173       }
174     }
175 
176     // Clear compiled ICs and exception caches
177     ShenandoahNMethodLocker locker(nm_data->ic_lock());
178     nm->unload_nmethod_caches(_unloading_occurred);
179   }
180 };
181 
182 class ShenandoahUnlinkTask : public WorkerTask {
183 private:
184   ShenandoahNMethodUnlinkClosure      _cl;
185   ShenandoahConcurrentNMethodIterator _iterator;
186 
187 public:
188   ShenandoahUnlinkTask(bool unloading_occurred) :
189     WorkerTask("Shenandoah Unlink NMethods"),
190     _cl(unloading_occurred),
191     _iterator(ShenandoahCodeRoots::table()) {}
192 
193   virtual void work(uint worker_id) {
194     _iterator.nmethods_do(&_cl);
195   }
196 };
197 
198 void ShenandoahCodeRoots::unlink(WorkerThreads* workers, bool unloading_occurred) {
199   assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
200 
201   ShenandoahUnlinkTask task(unloading_occurred);
202   workers->run_task(&task);
203 }
204 
205 void ShenandoahCodeRoots::purge() {
206   assert(ShenandoahHeap::heap()->unload_classes(), "Only when running concurrent class unloading");
207 
208   ClassUnloadingContext::context()->purge_and_free_nmethods();
209 }
210 
211 ShenandoahCodeRootsIterator::ShenandoahCodeRootsIterator() :
212         _table_snapshot(nullptr) {
213   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
214   MutexLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
215   _table_snapshot = ShenandoahCodeRoots::table()->snapshot_for_iteration();
216 }
217 
218 ShenandoahCodeRootsIterator::~ShenandoahCodeRootsIterator() {
219   MonitorLocker locker(CodeCache_lock, Mutex::_no_safepoint_check_flag);
220   ShenandoahCodeRoots::table()->finish_iteration(_table_snapshot);
221   _table_snapshot = nullptr;
222   locker.notify_all();
223 }
224 
225 void ShenandoahCodeRootsIterator::possibly_parallel_nmethods_do(NMethodClosure *f) {
226   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint");
227   assert(_table_snapshot != nullptr, "Sanity");
228   _table_snapshot->parallel_nmethods_do(f);
229 }