1 /*
  2  * Copyright (c) 2019, 2020, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. 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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_INLINE_HPP
 27 
 28 #include "gc/shenandoah/shenandoahClosures.hpp"
 29 
 30 #include "gc/shared/barrierSetNMethod.hpp"
 31 #include "gc/shenandoah/shenandoahAsserts.hpp"
 32 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
 33 #include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"
 34 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 35 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 36 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
 37 #include "memory/iterator.inline.hpp"
 38 #include "oops/compressedOops.inline.hpp"
 39 #include "runtime/atomic.hpp"
 40 #include "runtime/javaThread.hpp"
 41 
 42 ShenandoahFlushSATBHandshakeClosure::ShenandoahFlushSATBHandshakeClosure(SATBMarkQueueSet& qset) :
 43   HandshakeClosure("Shenandoah Flush SATB"),
 44   _qset(qset) {}
 45 
 46 void ShenandoahFlushSATBHandshakeClosure::do_thread(Thread* thread) {
 47   _qset.flush_queue(ShenandoahThreadLocalData::satb_mark_queue(thread));
 48 }
 49 
 50 ShenandoahForwardedIsAliveClosure::ShenandoahForwardedIsAliveClosure() :
 51   _mark_context(ShenandoahHeap::heap()->marking_context()) {
 52 }
 53 
 54 bool ShenandoahForwardedIsAliveClosure::do_object_b(oop obj) {
 55   if (CompressedOops::is_null(obj)) {
 56     return false;
 57   }
 58   obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 59   shenandoah_assert_not_forwarded_if(nullptr, obj, ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
 60   return _mark_context->is_marked_or_old(obj);
 61 }
 62 
 63 ShenandoahIsAliveClosure::ShenandoahIsAliveClosure() :
 64   _mark_context(ShenandoahHeap::heap()->marking_context()) {
 65 }
 66 
 67 bool ShenandoahIsAliveClosure::do_object_b(oop obj) {
 68   if (CompressedOops::is_null(obj)) {
 69     return false;
 70   }
 71   shenandoah_assert_not_forwarded(nullptr, obj);
 72   return _mark_context->is_marked_or_old(obj);
 73 }
 74 
 75 BoolObjectClosure* ShenandoahIsAliveSelector::is_alive_closure() {
 76   return ShenandoahHeap::heap()->has_forwarded_objects() ?
 77          reinterpret_cast<BoolObjectClosure*>(&_fwd_alive_cl) :
 78          reinterpret_cast<BoolObjectClosure*>(&_alive_cl);
 79 }
 80 
 81 void ShenandoahOopClosureBase::do_nmethod(nmethod* nm) {
 82   nm->run_nmethod_entry_barrier();
 83 }
 84 
 85 ShenandoahKeepAliveClosure::ShenandoahKeepAliveClosure() :
 86   _bs(ShenandoahBarrierSet::barrier_set()) {
 87 }
 88 
 89 void ShenandoahKeepAliveClosure::do_oop(oop* p) {
 90   do_oop_work(p);
 91 }
 92 
 93 void ShenandoahKeepAliveClosure::do_oop(narrowOop* p) {
 94   do_oop_work(p);
 95 }
 96 
 97 template <typename T>
 98 void ShenandoahKeepAliveClosure::do_oop_work(T* p) {
 99   assert(ShenandoahHeap::heap()->is_concurrent_mark_in_progress(), "Only for concurrent marking phase");
100   assert(ShenandoahHeap::heap()->is_concurrent_old_mark_in_progress() || !ShenandoahHeap::heap()->has_forwarded_objects(), "Not expected");
101 
102   T o = RawAccess<>::oop_load(p);
103   if (!CompressedOops::is_null(o)) {
104     oop obj = CompressedOops::decode_not_null(o);
105     _bs->enqueue(obj);
106   }
107 }
108 
109 ShenandoahUpdateRefsClosure::ShenandoahUpdateRefsClosure() :
110   _heap(ShenandoahHeap::heap()) {
111 }
112 
113 template <class T>
114 void ShenandoahUpdateRefsClosure::do_oop_work(T* p) {
115   _heap->update_with_forwarded(p);
116 }
117 
118 void ShenandoahUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
119 void ShenandoahUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
120 
121 template <bool concurrent, bool stable_thread>
122 ShenandoahEvacuateUpdateRootClosureBase<concurrent, stable_thread>::ShenandoahEvacuateUpdateRootClosureBase() :
123   _heap(ShenandoahHeap::heap()), _thread(stable_thread ? Thread::current() : nullptr) {
124 }
125 
126 template <bool concurrent, bool stable_thread>
127 void ShenandoahEvacuateUpdateRootClosureBase<concurrent, stable_thread>::do_oop(oop* p) {
128   if (concurrent) {
129     ShenandoahEvacOOMScope scope;
130     do_oop_work(p);
131   } else {
132     do_oop_work(p);
133   }
134 }
135 
136 template <bool concurrent, bool stable_thread>
137 void ShenandoahEvacuateUpdateRootClosureBase<concurrent, stable_thread>::do_oop(narrowOop* p) {
138   if (concurrent) {
139     ShenandoahEvacOOMScope scope;
140     do_oop_work(p);
141   } else {
142     do_oop_work(p);
143   }
144 }
145 
146 template <bool atomic, bool stable_thread>
147 template <class T>
148 void ShenandoahEvacuateUpdateRootClosureBase<atomic, stable_thread>::do_oop_work(T* p) {
149   assert(_heap->is_concurrent_weak_root_in_progress() ||
150          _heap->is_concurrent_strong_root_in_progress(),
151          "Only do this in root processing phase");
152 
153   T o = RawAccess<>::oop_load(p);
154   if (!CompressedOops::is_null(o)) {
155     oop obj = CompressedOops::decode_not_null(o);
156     if (_heap->in_collection_set(obj)) {
157       assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
158       shenandoah_assert_marked(p, obj);
159       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
160       if (resolved == obj) {
161         Thread* thr = stable_thread ? _thread : Thread::current();
162         assert(thr == Thread::current(), "Wrong thread");
163 
164         resolved = _heap->evacuate_object(obj, thr);
165       }
166       if (atomic) {
167         ShenandoahHeap::atomic_update_oop(resolved, p, o);
168       } else {
169         RawAccess<IS_NOT_NULL | MO_UNORDERED>::oop_store(p, resolved);
170       }
171     }
172   }
173 }
174 
175 template <bool CONCURRENT, typename IsAlive, typename KeepAlive>
176 ShenandoahCleanUpdateWeakOopsClosure<CONCURRENT, IsAlive, KeepAlive>::ShenandoahCleanUpdateWeakOopsClosure(IsAlive* is_alive, KeepAlive* keep_alive) :
177   _is_alive(is_alive), _keep_alive(keep_alive) {
178   if (!CONCURRENT) {
179     assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
180   }
181 }
182 
183 template <bool CONCURRENT, typename IsAlive, typename KeepAlive>
184 void ShenandoahCleanUpdateWeakOopsClosure<CONCURRENT, IsAlive, KeepAlive>::do_oop(oop* p) {
185   oop obj = RawAccess<>::oop_load(p);
186   if (!CompressedOops::is_null(obj)) {
187     if (_is_alive->do_object_b(obj)) {
188       _keep_alive->do_oop(p);
189     } else {
190       if (CONCURRENT) {
191         ShenandoahHeap::atomic_clear_oop(p, obj);
192       } else {
193         RawAccess<IS_NOT_NULL>::oop_store(p, oop());
194       }
195     }
196   }
197 }
198 
199 template <bool CONCURRENT, typename IsAlive, typename KeepAlive>
200 void ShenandoahCleanUpdateWeakOopsClosure<CONCURRENT, IsAlive, KeepAlive>::do_oop(narrowOop* p) {
201   ShouldNotReachHere();
202 }
203 
204 ShenandoahCodeBlobAndDisarmClosure::ShenandoahCodeBlobAndDisarmClosure(OopClosure* cl) :
205   CodeBlobToOopClosure(cl, true /* fix_relocations */),
206    _bs(BarrierSet::barrier_set()->barrier_set_nmethod()) {
207 }
208 
209 void ShenandoahCodeBlobAndDisarmClosure::do_code_blob(CodeBlob* cb) {
210   nmethod* const nm = cb->as_nmethod_or_null();
211   if (nm != nullptr) {
212     assert(!ShenandoahNMethod::gc_data(nm)->is_unregistered(), "Should not be here");
213     CodeBlobToOopClosure::do_code_blob(cb);
214     _bs->disarm(nm);
215   }
216 }
217 
218 #ifdef ASSERT
219 template <class T>
220 void ShenandoahAssertNotForwardedClosure::do_oop_work(T* p) {
221   T o = RawAccess<>::oop_load(p);
222   if (!CompressedOops::is_null(o)) {
223     oop obj = CompressedOops::decode_not_null(o);
224     shenandoah_assert_not_forwarded(p, obj);
225   }
226 }
227 
228 void ShenandoahAssertNotForwardedClosure::do_oop(narrowOop* p) { do_oop_work(p); }
229 void ShenandoahAssertNotForwardedClosure::do_oop(oop* p)       { do_oop_work(p); }
230 #endif
231 
232 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_INLINE_HPP