1 /*
2  * Copyright (c) 2013, 2019, Red Hat, Inc. All rights reserved.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  *
22  */
23 
24 #include "precompiled.hpp"
25 #include "gc/shenandoah/shenandoahAsserts.hpp"
26 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
27 #include "gc/shenandoah/shenandoahBarrierSetClone.inline.hpp"
28 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
29 #include "gc/shenandoah/shenandoahBarrierSetNMethod.hpp"
30 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
31 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp"
32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
33 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
34 #include "memory/iterator.inline.hpp"
35 #include "runtime/interfaceSupport.inline.hpp"
36 #ifdef COMPILER1
37 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
38 #endif
39 #ifdef COMPILER2
40 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
41 #endif
42 
43 class ShenandoahBarrierSetC1;
44 class ShenandoahBarrierSetC2;
45 
make_barrier_set_nmethod(ShenandoahHeap * heap)46 static BarrierSetNMethod* make_barrier_set_nmethod(ShenandoahHeap* heap) {
47   // NMethod barriers are only used when concurrent nmethod unloading is enabled
48   if (!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) {
49     return NULL;
50   }
51   return new ShenandoahBarrierSetNMethod(heap);
52 }
53 
ShenandoahBarrierSet(ShenandoahHeap * heap)54 ShenandoahBarrierSet::ShenandoahBarrierSet(ShenandoahHeap* heap) :
55   BarrierSet(make_barrier_set_assembler<ShenandoahBarrierSetAssembler>(),
56              make_barrier_set_c1<ShenandoahBarrierSetC1>(),
57              make_barrier_set_c2<ShenandoahBarrierSetC2>(),
58              make_barrier_set_nmethod(heap),
59              BarrierSet::FakeRtti(BarrierSet::ShenandoahBarrierSet)),
60   _heap(heap),
61   _satb_mark_queue_buffer_allocator("SATB Buffer Allocator", ShenandoahSATBBufferSize),
62   _satb_mark_queue_set(&_satb_mark_queue_buffer_allocator)
63 {
64 }
65 
assembler()66 ShenandoahBarrierSetAssembler* ShenandoahBarrierSet::assembler() {
67   BarrierSetAssembler* const bsa = BarrierSet::barrier_set()->barrier_set_assembler();
68   return reinterpret_cast<ShenandoahBarrierSetAssembler*>(bsa);
69 }
70 
print_on(outputStream * st) const71 void ShenandoahBarrierSet::print_on(outputStream* st) const {
72   st->print("ShenandoahBarrierSet");
73 }
74 
is_a(BarrierSet::Name bsn)75 bool ShenandoahBarrierSet::is_a(BarrierSet::Name bsn) {
76   return bsn == BarrierSet::ShenandoahBarrierSet;
77 }
78 
is_aligned(HeapWord * hw)79 bool ShenandoahBarrierSet::is_aligned(HeapWord* hw) {
80   return true;
81 }
82 
need_load_reference_barrier(DecoratorSet decorators,BasicType type)83 bool ShenandoahBarrierSet::need_load_reference_barrier(DecoratorSet decorators, BasicType type) {
84   if (!ShenandoahLoadRefBarrier) return false;
85   // Only needed for references
86   return is_reference_type(type);
87 }
88 
use_load_reference_barrier_native(DecoratorSet decorators,BasicType type)89 bool ShenandoahBarrierSet::use_load_reference_barrier_native(DecoratorSet decorators, BasicType type) {
90   assert(need_load_reference_barrier(decorators, type), "Should be subset of LRB");
91   assert(is_reference_type(type), "Why we here?");
92   // Native load reference barrier is only needed for concurrent root processing
93   if (!ShenandoahConcurrentRoots::can_do_concurrent_roots()) {
94     return false;
95   }
96 
97   return (decorators & IN_NATIVE) != 0;
98 }
99 
need_keep_alive_barrier(DecoratorSet decorators,BasicType type)100 bool ShenandoahBarrierSet::need_keep_alive_barrier(DecoratorSet decorators,BasicType type) {
101   if (!ShenandoahSATBBarrier) return false;
102   // Only needed for references
103   if (!is_reference_type(type)) return false;
104 
105   bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
106   bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0;
107   bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0;
108   return (on_weak_ref || unknown) && keep_alive;
109 }
110 
load_reference_barrier_not_null(oop obj)111 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) {
112   if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) {
113     return load_reference_barrier_impl(obj);
114   } else {
115     return obj;
116   }
117 }
118 
load_reference_barrier(oop obj)119 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
120   if (obj != NULL) {
121     return load_reference_barrier_not_null(obj);
122   } else {
123     return obj;
124   }
125 }
126 
load_reference_barrier_impl(oop obj)127 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) {
128   assert(ShenandoahLoadRefBarrier, "should be enabled");
129   if (!CompressedOops::is_null(obj)) {
130     bool evac_in_progress = _heap->is_evacuation_in_progress();
131     oop fwd = resolve_forwarded_not_null(obj);
132     if (evac_in_progress &&
133         _heap->in_collection_set(obj) &&
134         obj == fwd) {
135       Thread *t = Thread::current();
136       ShenandoahEvacOOMScope oom_evac_scope;
137       return _heap->evacuate_object(obj, t);
138     } else {
139       return fwd;
140     }
141   } else {
142     return obj;
143   }
144 }
145 
on_thread_create(Thread * thread)146 void ShenandoahBarrierSet::on_thread_create(Thread* thread) {
147   // Create thread local data
148   ShenandoahThreadLocalData::create(thread);
149 }
150 
on_thread_destroy(Thread * thread)151 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) {
152   // Destroy thread local data
153   ShenandoahThreadLocalData::destroy(thread);
154 }
155 
on_thread_attach(Thread * thread)156 void ShenandoahBarrierSet::on_thread_attach(Thread *thread) {
157   assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(),
158          "We should not be at a safepoint");
159   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
160   assert(!queue.is_active(), "SATB queue should not be active");
161   assert( queue.is_empty(),  "SATB queue should be empty");
162   queue.set_active(_satb_mark_queue_set.is_active());
163   if (thread->is_Java_thread()) {
164     ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state());
165     ShenandoahThreadLocalData::initialize_gclab(thread);
166     ShenandoahThreadLocalData::set_disarmed_value(thread, ShenandoahCodeRoots::disarmed_value());
167   }
168 }
169 
on_thread_detach(Thread * thread)170 void ShenandoahBarrierSet::on_thread_detach(Thread *thread) {
171   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
172   queue.flush();
173   if (thread->is_Java_thread()) {
174     PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
175     if (gclab != NULL) {
176       gclab->retire();
177     }
178   }
179 }
180 
load_reference_barrier_native(oop obj,oop * load_addr)181 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, oop* load_addr) {
182   return load_reference_barrier_native_impl(obj, load_addr);
183 }
184 
load_reference_barrier_native(oop obj,narrowOop * load_addr)185 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, narrowOop* load_addr) {
186   return load_reference_barrier_native_impl(obj, load_addr);
187 }
188 
189 template <class T>
load_reference_barrier_native_impl(oop obj,T * load_addr)190 oop ShenandoahBarrierSet::load_reference_barrier_native_impl(oop obj, T* load_addr) {
191   if (CompressedOops::is_null(obj)) {
192     return NULL;
193   }
194 
195   ShenandoahMarkingContext* const marking_context = _heap->marking_context();
196   if (_heap->is_concurrent_root_in_progress() && !marking_context->is_marked(obj)) {
197     Thread* thr = Thread::current();
198     if (thr->is_Java_thread()) {
199       return NULL;
200     } else {
201       return obj;
202     }
203   }
204 
205   oop fwd = load_reference_barrier_not_null(obj);
206   if (load_addr != NULL && fwd != obj) {
207     // Since we are here and we know the load address, update the reference.
208     ShenandoahHeap::cas_oop(fwd, load_addr, obj);
209   }
210 
211   return fwd;
212 }
213 
clone_barrier_runtime(oop src)214 void ShenandoahBarrierSet::clone_barrier_runtime(oop src) {
215   if (_heap->has_forwarded_objects() || (ShenandoahStoreValEnqueueBarrier && _heap->is_concurrent_mark_in_progress())) {
216     clone_barrier(src);
217   }
218 }
219 
220