1 /* 2 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. 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/g1/g1BarrierSet.inline.hpp" 27 #include "gc/g1/g1BarrierSetAssembler.hpp" 28 #include "gc/g1/g1CardTable.inline.hpp" 29 #include "gc/g1/g1CollectedHeap.inline.hpp" 30 #include "gc/g1/g1ThreadLocalData.hpp" 31 #include "gc/g1/heapRegion.hpp" 32 #include "gc/g1/satbMarkQueue.hpp" 33 #include "logging/log.hpp" 34 #include "oops/access.inline.hpp" 35 #include "oops/compressedOops.inline.hpp" 36 #include "oops/oop.inline.hpp" 37 #include "runtime/mutexLocker.hpp" 38 #include "runtime/thread.inline.hpp" 39 #include "utilities/macros.hpp" 40 41 SATBMarkQueueSet G1BarrierSet::_satb_mark_queue_set; 42 DirtyCardQueueSet G1BarrierSet::_dirty_card_queue_set; 43 44 G1BarrierSet::G1BarrierSet(G1CardTable* card_table) : 45 CardTableBarrierSet(make_barrier_set_assembler<G1BarrierSetAssembler>(), 46 card_table, 47 BarrierSet::FakeRtti(BarrierSet::G1BarrierSet)) {} 48 49 void G1BarrierSet::enqueue(oop pre_val) { 50 // Nulls should have been already filtered. 51 assert(oopDesc::is_oop(pre_val, true), "Error"); 52 53 if (!_satb_mark_queue_set.is_active()) return; 54 Thread* thr = Thread::current(); 55 if (thr->is_Java_thread()) { 56 G1ThreadLocalData::satb_mark_queue(thr).enqueue(pre_val); 57 } else { 58 MutexLockerEx x(Shared_SATB_Q_lock, Mutex::_no_safepoint_check_flag); 59 _satb_mark_queue_set.shared_satb_queue()->enqueue(pre_val); 60 } 61 } 62 63 void G1BarrierSet::write_ref_array_pre_oop_entry(oop* dst, size_t length) { 64 G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set()); 65 bs->write_ref_array_pre(dst, length, false); 66 } 67 68 void G1BarrierSet::write_ref_array_pre_narrow_oop_entry(narrowOop* dst, size_t length) { 69 G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set()); 70 bs->write_ref_array_pre(dst, length, false); 71 } 72 73 void G1BarrierSet::write_ref_array_post_entry(HeapWord* dst, size_t length) { 74 G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set()); 75 bs->G1BarrierSet::write_ref_array(dst, length); 76 } 77 78 template <class T> void 79 G1BarrierSet::write_ref_array_pre_work(T* dst, size_t count) { 80 if (!_satb_mark_queue_set.is_active()) return; 81 T* elem_ptr = dst; 82 for (size_t i = 0; i < count; i++, elem_ptr++) { 83 T heap_oop = RawAccess<>::oop_load(elem_ptr); 84 if (!CompressedOops::is_null(heap_oop)) { 85 enqueue(CompressedOops::decode_not_null(heap_oop)); 86 } 87 } 88 } 89 90 void G1BarrierSet::write_ref_array_pre(oop* dst, size_t count, bool dest_uninitialized) { 91 if (!dest_uninitialized) { 92 write_ref_array_pre_work(dst, count); 93 } 94 } 95 96 void G1BarrierSet::write_ref_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized) { 97 if (!dest_uninitialized) { 98 write_ref_array_pre_work(dst, count); 99 } 100 } 101 102 void G1BarrierSet::write_ref_field_post_slow(volatile jbyte* byte) { 103 // In the slow path, we know a card is not young 104 assert(*byte != G1CardTable::g1_young_card_val(), "slow path invoked without filtering"); 105 OrderAccess::storeload(); 106 if (*byte != G1CardTable::dirty_card_val()) { 107 *byte = G1CardTable::dirty_card_val(); 108 Thread* thr = Thread::current(); 109 if (thr->is_Java_thread()) { 110 G1ThreadLocalData::dirty_card_queue(thr).enqueue(byte); 111 } else { 112 MutexLockerEx x(Shared_DirtyCardQ_lock, 113 Mutex::_no_safepoint_check_flag); 114 _dirty_card_queue_set.shared_dirty_card_queue()->enqueue(byte); 115 } 116 } 117 } 118 119 void G1BarrierSet::invalidate(MemRegion mr) { 120 if (mr.is_empty()) { 121 return; 122 } 123 volatile jbyte* byte = _card_table->byte_for(mr.start()); 124 jbyte* last_byte = _card_table->byte_for(mr.last()); 125 Thread* thr = Thread::current(); 126 // skip all consecutive young cards 127 for (; byte <= last_byte && *byte == G1CardTable::g1_young_card_val(); byte++); 128 129 if (byte <= last_byte) { 130 OrderAccess::storeload(); 131 // Enqueue if necessary. 132 if (thr->is_Java_thread()) { 133 for (; byte <= last_byte; byte++) { 134 if (*byte == G1CardTable::g1_young_card_val()) { 135 continue; 136 } 137 if (*byte != G1CardTable::dirty_card_val()) { 138 *byte = G1CardTable::dirty_card_val(); 139 G1ThreadLocalData::dirty_card_queue(thr).enqueue(byte); 140 } 141 } 142 } else { 143 MutexLockerEx x(Shared_DirtyCardQ_lock, 144 Mutex::_no_safepoint_check_flag); 145 for (; byte <= last_byte; byte++) { 146 if (*byte == G1CardTable::g1_young_card_val()) { 147 continue; 148 } 149 if (*byte != G1CardTable::dirty_card_val()) { 150 *byte = G1CardTable::dirty_card_val(); 151 _dirty_card_queue_set.shared_dirty_card_queue()->enqueue(byte); 152 } 153 } 154 } 155 } 156 } 157 158 void G1BarrierSet::on_thread_create(Thread* thread) { 159 // Create thread local data 160 G1ThreadLocalData::create(thread); 161 } 162 163 void G1BarrierSet::on_thread_destroy(Thread* thread) { 164 // Destroy thread local data 165 G1ThreadLocalData::destroy(thread); 166 } 167 168 void G1BarrierSet::on_thread_attach(JavaThread* thread) { 169 // This method initializes the SATB and dirty card queues before a 170 // JavaThread is added to the Java thread list. Right now, we don't 171 // have to do anything to the dirty card queue (it should have been 172 // activated when the thread was created), but we have to activate 173 // the SATB queue if the thread is created while a marking cycle is 174 // in progress. The activation / de-activation of the SATB queues at 175 // the beginning / end of a marking cycle is done during safepoints 176 // so we have to make sure this method is called outside one to be 177 // able to safely read the active field of the SATB queue set. Right 178 // now, it is called just before the thread is added to the Java 179 // thread list in the Threads::add() method. That method is holding 180 // the Threads_lock which ensures we are outside a safepoint. We 181 // cannot do the obvious and set the active field of the SATB queue 182 // when the thread is created given that, in some cases, safepoints 183 // might happen between the JavaThread constructor being called and the 184 // thread being added to the Java thread list (an example of this is 185 // when the structure for the DestroyJavaVM thread is created). 186 assert(!SafepointSynchronize::is_at_safepoint(), "We should not be at a safepoint"); 187 assert(!G1ThreadLocalData::satb_mark_queue(thread).is_active(), "SATB queue should not be active"); 188 assert(G1ThreadLocalData::satb_mark_queue(thread).is_empty(), "SATB queue should be empty"); 189 assert(G1ThreadLocalData::dirty_card_queue(thread).is_active(), "Dirty card queue should be active"); 190 191 // If we are creating the thread during a marking cycle, we should 192 // set the active field of the SATB queue to true. 193 if (_satb_mark_queue_set.is_active()) { 194 G1ThreadLocalData::satb_mark_queue(thread).set_active(true); 195 } 196 } 197 198 void G1BarrierSet::on_thread_detach(JavaThread* thread) { 199 // Flush any deferred card marks, SATB buffers and dirty card queue buffers 200 CardTableBarrierSet::on_thread_detach(thread); 201 G1ThreadLocalData::satb_mark_queue(thread).flush(); 202 G1ThreadLocalData::dirty_card_queue(thread).flush(); 203 }