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