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