< prev index next >

src/hotspot/share/gc/g1/g1BarrierSet.cpp

Print this page




  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/heapRegion.hpp"
  31 #include "gc/g1/satbMarkQueue.hpp"
  32 #include "logging/log.hpp"
  33 #include "oops/access.inline.hpp"
  34 #include "oops/compressedOops.inline.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/thread.inline.hpp"
  38 #include "utilities/macros.hpp"
  39 



  40 G1BarrierSet::G1BarrierSet(G1CardTable* card_table) :
  41   CardTableBarrierSet(make_barrier_set_assembler<G1BarrierSetAssembler>(),
  42                       card_table,
  43                       BarrierSet::FakeRtti(BarrierSet::G1BarrierSet)),
  44   _dcqs(JavaThread::dirty_card_queue_set())
  45 { }
  46 
  47 void G1BarrierSet::enqueue(oop pre_val) {
  48   // Nulls should have been already filtered.
  49   assert(oopDesc::is_oop(pre_val, true), "Error");
  50 
  51   if (!JavaThread::satb_mark_queue_set().is_active()) return;
  52   Thread* thr = Thread::current();
  53   if (thr->is_Java_thread()) {
  54     JavaThread* jt = (JavaThread*)thr;
  55     jt->satb_mark_queue().enqueue(pre_val);
  56   } else {
  57     MutexLockerEx x(Shared_SATB_Q_lock, Mutex::_no_safepoint_check_flag);
  58     JavaThread::satb_mark_queue_set().shared_satb_queue()->enqueue(pre_val);
  59   }
  60 }
  61 
  62 void G1BarrierSet::write_ref_array_pre_oop_entry(oop* dst, size_t length) {
  63   G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set());
  64   bs->write_ref_array_pre(dst, length, false);
  65 }
  66 
  67 void G1BarrierSet::write_ref_array_pre_narrow_oop_entry(narrowOop* dst, size_t length) {
  68   G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set());
  69   bs->write_ref_array_pre(dst, length, false);
  70 }
  71 
  72 void G1BarrierSet::write_ref_array_post_entry(HeapWord* dst, size_t length) {
  73   G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set());
  74   bs->G1BarrierSet::write_ref_array(dst, length);
  75 }
  76 
  77 template <class T> void
  78 G1BarrierSet::write_ref_array_pre_work(T* dst, size_t count) {
  79   if (!JavaThread::satb_mark_queue_set().is_active()) return;
  80   T* elem_ptr = dst;
  81   for (size_t i = 0; i < count; i++, elem_ptr++) {
  82     T heap_oop = RawAccess<>::oop_load(elem_ptr);
  83     if (!CompressedOops::is_null(heap_oop)) {
  84       enqueue(CompressedOops::decode_not_null(heap_oop));
  85     }
  86   }
  87 }
  88 
  89 void G1BarrierSet::write_ref_array_pre(oop* dst, size_t count, bool dest_uninitialized) {
  90   if (!dest_uninitialized) {
  91     write_ref_array_pre_work(dst, count);
  92   }
  93 }
  94 
  95 void G1BarrierSet::write_ref_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized) {
  96   if (!dest_uninitialized) {
  97     write_ref_array_pre_work(dst, count);
  98   }
  99 }
 100 
 101 void G1BarrierSet::write_ref_field_post_slow(volatile jbyte* byte) {
 102   // In the slow path, we know a card is not young
 103   assert(*byte != G1CardTable::g1_young_card_val(), "slow path invoked without filtering");
 104   OrderAccess::storeload();
 105   if (*byte != G1CardTable::dirty_card_val()) {
 106     *byte = G1CardTable::dirty_card_val();
 107     Thread* thr = Thread::current();
 108     if (thr->is_Java_thread()) {
 109       JavaThread* jt = (JavaThread*)thr;
 110       jt->dirty_card_queue().enqueue(byte);
 111     } else {
 112       MutexLockerEx x(Shared_DirtyCardQ_lock,
 113                       Mutex::_no_safepoint_check_flag);
 114       _dcqs.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       JavaThread* jt = (JavaThread*)thr;
 134       for (; byte <= last_byte; byte++) {
 135         if (*byte == G1CardTable::g1_young_card_val()) {
 136           continue;
 137         }
 138         if (*byte != G1CardTable::dirty_card_val()) {
 139           *byte = G1CardTable::dirty_card_val();
 140           jt->dirty_card_queue().enqueue(byte);
 141         }
 142       }
 143     } else {
 144       MutexLockerEx x(Shared_DirtyCardQ_lock,
 145                       Mutex::_no_safepoint_check_flag);
 146       for (; byte <= last_byte; byte++) {
 147         if (*byte == G1CardTable::g1_young_card_val()) {
 148           continue;
 149         }
 150         if (*byte != G1CardTable::dirty_card_val()) {
 151           *byte = G1CardTable::dirty_card_val();
 152           _dcqs.shared_dirty_card_queue()->enqueue(byte);
 153         }
 154       }
 155     }
 156   }
 157 }
 158 
 159 void G1BarrierSet::on_thread_attach(JavaThread* thread) {
 160   // This method initializes the SATB and dirty card queues before a
 161   // JavaThread is added to the Java thread list. Right now, we don't
 162   // have to do anything to the dirty card queue (it should have been
 163   // activated when the thread was created), but we have to activate
 164   // the SATB queue if the thread is created while a marking cycle is
 165   // in progress. The activation / de-activation of the SATB queues at
 166   // the beginning / end of a marking cycle is done during safepoints
 167   // so we have to make sure this method is called outside one to be
 168   // able to safely read the active field of the SATB queue set. Right
 169   // now, it is called just before the thread is added to the Java
 170   // thread list in the Threads::add() method. That method is holding
 171   // the Threads_lock which ensures we are outside a safepoint. We
 172   // cannot do the obvious and set the active field of the SATB queue
 173   // when the thread is created given that, in some cases, safepoints
 174   // might happen between the JavaThread constructor being called and the
 175   // thread being added to the Java thread list (an example of this is
 176   // when the structure for the DestroyJavaVM thread is created).
 177   assert(!SafepointSynchronize::is_at_safepoint(), "We should not be at a safepoint");
 178   assert(!thread->satb_mark_queue().is_active(), "SATB queue should not be active");
 179   assert(thread->satb_mark_queue().is_empty(), "SATB queue should be empty");
 180   assert(thread->dirty_card_queue().is_active(), "Dirty card queue should be active");
 181 
 182   // If we are creating the thread during a marking cycle, we should
 183   // set the active field of the SATB queue to true.
 184   if (thread->satb_mark_queue_set().is_active()) {
 185     thread->satb_mark_queue().set_active(true);
 186   }
 187 }
 188 
 189 void G1BarrierSet::on_thread_detach(JavaThread* thread) {
 190   // Flush any deferred card marks, SATB buffers and dirty card queue buffers
 191   CardTableBarrierSet::on_thread_detach(thread);
 192   thread->satb_mark_queue().flush();
 193   thread->dirty_card_queue().flush();
 194 }


  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/heapRegion.hpp"
  31 #include "gc/g1/satbMarkQueue.hpp"
  32 #include "logging/log.hpp"
  33 #include "oops/access.inline.hpp"
  34 #include "oops/compressedOops.inline.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/thread.inline.hpp"
  38 #include "utilities/macros.hpp"
  39 
  40 SATBMarkQueueSet G1BarrierSet::_satb_mark_queue_set;
  41 DirtyCardQueueSet G1BarrierSet::_dirty_card_queue_set;
  42 
  43 G1BarrierSet::G1BarrierSet(G1CardTable* card_table) :
  44   CardTableBarrierSet(make_barrier_set_assembler<G1BarrierSetAssembler>(),
  45                       card_table,
  46                       BarrierSet::FakeRtti(BarrierSet::G1BarrierSet)) {}


  47 
  48 void G1BarrierSet::enqueue(oop pre_val) {
  49   // Nulls should have been already filtered.
  50   assert(oopDesc::is_oop(pre_val, true), "Error");
  51 
  52   if (!_satb_mark_queue_set.is_active()) return;
  53   Thread* thr = Thread::current();
  54   if (thr->is_Java_thread()) {
  55     JavaThread* jt = (JavaThread*)thr;
  56     jt->satb_mark_queue().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       JavaThread* jt = (JavaThread*)thr;
 111       jt->dirty_card_queue().enqueue(byte);
 112     } else {
 113       MutexLockerEx x(Shared_DirtyCardQ_lock,
 114                       Mutex::_no_safepoint_check_flag);
 115       _dirty_card_queue_set.shared_dirty_card_queue()->enqueue(byte);
 116     }
 117   }
 118 }
 119 
 120 void G1BarrierSet::invalidate(MemRegion mr) {
 121   if (mr.is_empty()) {
 122     return;
 123   }
 124   volatile jbyte* byte = _card_table->byte_for(mr.start());
 125   jbyte* last_byte = _card_table->byte_for(mr.last());
 126   Thread* thr = Thread::current();
 127     // skip all consecutive young cards
 128   for (; byte <= last_byte && *byte == G1CardTable::g1_young_card_val(); byte++);
 129 
 130   if (byte <= last_byte) {
 131     OrderAccess::storeload();
 132     // Enqueue if necessary.
 133     if (thr->is_Java_thread()) {
 134       JavaThread* jt = (JavaThread*)thr;
 135       for (; byte <= last_byte; byte++) {
 136         if (*byte == G1CardTable::g1_young_card_val()) {
 137           continue;
 138         }
 139         if (*byte != G1CardTable::dirty_card_val()) {
 140           *byte = G1CardTable::dirty_card_val();
 141           jt->dirty_card_queue().enqueue(byte);
 142         }
 143       }
 144     } else {
 145       MutexLockerEx x(Shared_DirtyCardQ_lock,
 146                       Mutex::_no_safepoint_check_flag);
 147       for (; byte <= last_byte; byte++) {
 148         if (*byte == G1CardTable::g1_young_card_val()) {
 149           continue;
 150         }
 151         if (*byte != G1CardTable::dirty_card_val()) {
 152           *byte = G1CardTable::dirty_card_val();
 153           _dirty_card_queue_set.shared_dirty_card_queue()->enqueue(byte);
 154         }
 155       }
 156     }
 157   }
 158 }
 159 
 160 void G1BarrierSet::on_thread_attach(JavaThread* thread) {
 161   // This method initializes the SATB and dirty card queues before a
 162   // JavaThread is added to the Java thread list. Right now, we don't
 163   // have to do anything to the dirty card queue (it should have been
 164   // activated when the thread was created), but we have to activate
 165   // the SATB queue if the thread is created while a marking cycle is
 166   // in progress. The activation / de-activation of the SATB queues at
 167   // the beginning / end of a marking cycle is done during safepoints
 168   // so we have to make sure this method is called outside one to be
 169   // able to safely read the active field of the SATB queue set. Right
 170   // now, it is called just before the thread is added to the Java
 171   // thread list in the Threads::add() method. That method is holding
 172   // the Threads_lock which ensures we are outside a safepoint. We
 173   // cannot do the obvious and set the active field of the SATB queue
 174   // when the thread is created given that, in some cases, safepoints
 175   // might happen between the JavaThread constructor being called and the
 176   // thread being added to the Java thread list (an example of this is
 177   // when the structure for the DestroyJavaVM thread is created).
 178   assert(!SafepointSynchronize::is_at_safepoint(), "We should not be at a safepoint");
 179   assert(!thread->satb_mark_queue().is_active(), "SATB queue should not be active");
 180   assert(thread->satb_mark_queue().is_empty(), "SATB queue should be empty");
 181   assert(thread->dirty_card_queue().is_active(), "Dirty card queue should be active");
 182 
 183   // If we are creating the thread during a marking cycle, we should
 184   // set the active field of the SATB queue to true.
 185   if (_satb_mark_queue_set.is_active()) {
 186     thread->satb_mark_queue().set_active(true);
 187   }
 188 }
 189 
 190 void G1BarrierSet::on_thread_detach(JavaThread* thread) {
 191   // Flush any deferred card marks, SATB buffers and dirty card queue buffers
 192   CardTableBarrierSet::on_thread_detach(thread);
 193   thread->satb_mark_queue().flush();
 194   thread->dirty_card_queue().flush();
 195 }
< prev index next >