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