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