1 /* 2 * Copyright (c) 2013, 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 25 #include "precompiled.hpp" 26 #include "gc/shenandoah/shenandoahAsserts.hpp" 27 #include "gc/shenandoah/shenandoahBarrierSet.hpp" 28 #include "gc/shenandoah/shenandoahBarrierSetClone.inline.hpp" 29 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp" 30 #include "gc/shenandoah/shenandoahBarrierSetNMethod.hpp" 31 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 32 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp" 33 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 34 #include "gc/shenandoah/shenandoahHeuristics.hpp" 35 #include "gc/shenandoah/shenandoahTraversalGC.hpp" 36 #include "memory/iterator.inline.hpp" 37 #include "runtime/interfaceSupport.inline.hpp" 38 #ifdef COMPILER1 39 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp" 40 #endif 41 #ifdef COMPILER2 42 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp" 43 #endif 44 45 class ShenandoahBarrierSetC1; 46 class ShenandoahBarrierSetC2; 47 48 static BarrierSetNMethod* make_barrier_set_nmethod(ShenandoahHeap* heap) { 49 // NMethod barriers are only used when concurrent nmethod unloading is enabled 50 if (!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) { 51 return NULL; 52 } 53 return new ShenandoahBarrierSetNMethod(heap); 54 } 55 56 ShenandoahBarrierSet::ShenandoahBarrierSet(ShenandoahHeap* heap) : 57 BarrierSet(make_barrier_set_assembler<ShenandoahBarrierSetAssembler>(), 58 make_barrier_set_c1<ShenandoahBarrierSetC1>(), 59 make_barrier_set_c2<ShenandoahBarrierSetC2>(), 60 make_barrier_set_nmethod(heap), 61 BarrierSet::FakeRtti(BarrierSet::ShenandoahBarrierSet)), 62 _heap(heap), 63 _satb_mark_queue_buffer_allocator("SATB Buffer Allocator", ShenandoahSATBBufferSize), 64 _satb_mark_queue_set(&_satb_mark_queue_buffer_allocator) 65 { 66 } 67 68 ShenandoahBarrierSetAssembler* ShenandoahBarrierSet::assembler() { 69 BarrierSetAssembler* const bsa = BarrierSet::barrier_set()->barrier_set_assembler(); 70 return reinterpret_cast<ShenandoahBarrierSetAssembler*>(bsa); 71 } 72 73 void ShenandoahBarrierSet::print_on(outputStream* st) const { 74 st->print("ShenandoahBarrierSet"); 75 } 76 77 bool ShenandoahBarrierSet::is_a(BarrierSet::Name bsn) { 78 return bsn == BarrierSet::ShenandoahBarrierSet; 79 } 80 81 bool ShenandoahBarrierSet::is_aligned(HeapWord* hw) { 82 return true; 83 } 84 85 bool ShenandoahBarrierSet::need_load_reference_barrier(DecoratorSet decorators, BasicType type) { 86 if (!ShenandoahLoadRefBarrier) return false; 87 // Only needed for references 88 return is_reference_type(type); 89 } 90 91 bool ShenandoahBarrierSet::use_load_reference_barrier_native(DecoratorSet decorators, BasicType type) { 92 assert(need_load_reference_barrier(decorators, type), "Should be subset of LRB"); 93 assert(is_reference_type(type), "Why we here?"); 94 // Native load reference barrier is only needed for concurrent root processing 95 if (!ShenandoahConcurrentRoots::can_do_concurrent_roots()) { 96 return false; 97 } 98 99 return (decorators & IN_NATIVE) != 0; 100 } 101 102 bool ShenandoahBarrierSet::need_keep_alive_barrier(DecoratorSet decorators,BasicType type) { 103 if (!ShenandoahSATBBarrier) return false; 104 // Only needed for references 105 if (!is_reference_type(type)) return false; 106 107 bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0; 108 bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0; 109 bool is_traversal_mode = ShenandoahHeap::heap()->is_traversal_mode(); 110 bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0; 111 return (on_weak_ref || unknown) && (keep_alive || is_traversal_mode); 112 } 113 114 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) { 115 if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) { 116 return load_reference_barrier_impl(obj); 117 } else { 118 return obj; 119 } 120 } 121 122 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) { 123 if (obj != NULL) { 124 return load_reference_barrier_not_null(obj); 125 } else { 126 return obj; 127 } 128 } 129 130 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, oop* load_addr) { 131 return load_reference_barrier_mutator_work(obj, load_addr); 132 } 133 134 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, narrowOop* load_addr) { 135 return load_reference_barrier_mutator_work(obj, load_addr); 136 } 137 138 template <class T> 139 oop ShenandoahBarrierSet::load_reference_barrier_mutator_work(oop obj, T* load_addr) { 140 assert(ShenandoahLoadRefBarrier, "should be enabled"); 141 shenandoah_assert_in_cset(load_addr, obj); 142 143 oop fwd = resolve_forwarded_not_null_mutator(obj); 144 if (obj == fwd) { 145 assert(_heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL), 146 "evac should be in progress"); 147 ShenandoahEvacOOMScope scope; 148 fwd = _heap->evacuate_object(obj, Thread::current()); 149 } 150 151 if (load_addr != NULL && fwd != obj) { 152 // Since we are here and we know the load address, update the reference. 153 ShenandoahHeap::cas_oop(fwd, load_addr, obj); 154 } 155 156 return fwd; 157 } 158 159 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) { 160 assert(ShenandoahLoadRefBarrier, "should be enabled"); 161 if (!CompressedOops::is_null(obj)) { 162 bool evac_in_progress = _heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL); 163 oop fwd = resolve_forwarded_not_null(obj); 164 if (evac_in_progress && 165 _heap->in_collection_set(obj) && 166 obj == fwd) { 167 Thread *t = Thread::current(); 168 ShenandoahEvacOOMScope oom_evac_scope; 169 return _heap->evacuate_object(obj, t); 170 } else { 171 return fwd; 172 } 173 } else { 174 return obj; 175 } 176 } 177 178 void ShenandoahBarrierSet::on_thread_create(Thread* thread) { 179 // Create thread local data 180 ShenandoahThreadLocalData::create(thread); 181 } 182 183 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) { 184 // Destroy thread local data 185 ShenandoahThreadLocalData::destroy(thread); 186 } 187 188 void ShenandoahBarrierSet::on_thread_attach(Thread *thread) { 189 assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(), 190 "We should not be at a safepoint"); 191 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread); 192 assert(!queue.is_active(), "SATB queue should not be active"); 193 assert( queue.is_empty(), "SATB queue should be empty"); 194 queue.set_active(_satb_mark_queue_set.is_active()); 195 if (thread->is_Java_thread()) { 196 ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state()); 197 ShenandoahThreadLocalData::initialize_gclab(thread); 198 } 199 } 200 201 void ShenandoahBarrierSet::on_thread_detach(Thread *thread) { 202 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread); 203 queue.flush(); 204 if (thread->is_Java_thread()) { 205 PLAB* gclab = ShenandoahThreadLocalData::gclab(thread); 206 if (gclab != NULL) { 207 gclab->retire(); 208 } 209 } 210 } 211 212 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, oop* load_addr) { 213 return load_reference_barrier_native_impl(obj, load_addr); 214 } 215 216 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, narrowOop* load_addr) { 217 return load_reference_barrier_native_impl(obj, load_addr); 218 } 219 220 template <class T> 221 oop ShenandoahBarrierSet::load_reference_barrier_native_impl(oop obj, T* load_addr) { 222 if (CompressedOops::is_null(obj)) { 223 return NULL; 224 } 225 226 ShenandoahMarkingContext* const marking_context = _heap->marking_context(); 227 if (_heap->is_concurrent_root_in_progress() && !marking_context->is_marked(obj)) { 228 Thread* thr = Thread::current(); 229 if (thr->is_Java_thread()) { 230 return NULL; 231 } else { 232 return obj; 233 } 234 } 235 236 oop fwd = load_reference_barrier_not_null(obj); 237 if (load_addr != NULL && fwd != obj) { 238 // Since we are here and we know the load address, update the reference. 239 ShenandoahHeap::cas_oop(fwd, load_addr, obj); 240 } 241 242 return fwd; 243 } 244 245 void ShenandoahBarrierSet::clone_barrier_runtime(oop src) { 246 if (_heap->has_forwarded_objects()) { 247 clone_barrier(src); 248 } 249 } 250