1 /*
   2  * Copyright (c) 2001, 2015, 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 #ifndef SHARE_VM_GC_G1_G1SATBCARDTABLEMODREFBS_HPP
  26 #define SHARE_VM_GC_G1_G1SATBCARDTABLEMODREFBS_HPP
  27 
  28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  29 #include "gc/shared/cardTableModRefBS.hpp"
  30 #include "memory/memRegion.hpp"
  31 #include "oops/oop.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 class DirtyCardQueueSet;
  35 class G1SATBCardTableLoggingModRefBS;
  36 
  37 // This barrier is specialized to use a logging barrier to support
  38 // snapshot-at-the-beginning marking.
  39 
  40 class G1SATBCardTableModRefBS: public CardTableModRefBS {
  41   friend class VMStructs;
  42 protected:
  43   enum G1CardValues {
  44     g1_young_gen = CT_MR_BS_last_reserved << 1
  45   };
  46 
  47   G1SATBCardTableModRefBS(MemRegion whole_heap, const BarrierSet::FakeRtti& fake_rtti);
  48   ~G1SATBCardTableModRefBS() { }
  49 
  50 public:
  51   static int g1_young_card_val()   { return g1_young_gen; }
  52 
  53   // Add "pre_val" to a set of objects that may have been disconnected from the
  54   // pre-marking object graph.
  55   static void enqueue(oop pre_val);
  56 
  57   virtual bool has_write_ref_pre_barrier() { return true; }
  58 
  59   // This notes that we don't need to access any BarrierSet data
  60   // structures, so this can be called from a static context.
  61   template <class T> static void write_ref_field_pre_static(T* field, oop newVal) {
  62     T heap_oop = oopDesc::load_heap_oop(field);
  63     if (!oopDesc::is_null(heap_oop)) {
  64       enqueue(oopDesc::decode_heap_oop(heap_oop));
  65     }
  66   }
  67 
  68   // We export this to make it available in cases where the static
  69   // type of the barrier set is known.  Note that it is non-virtual.
  70   template <class T> inline void inline_write_ref_field_pre(T* field, oop newVal) {
  71     write_ref_field_pre_static(field, newVal);
  72   }
  73 
  74   // These are the more general virtual versions.
  75   virtual void write_ref_field_pre_work(oop* field, oop new_val) {
  76     inline_write_ref_field_pre(field, new_val);
  77   }
  78   virtual void write_ref_field_pre_work(narrowOop* field, oop new_val) {
  79     inline_write_ref_field_pre(field, new_val);
  80   }
  81   virtual void write_ref_field_pre_work(void* field, oop new_val) {
  82     guarantee(false, "Not needed");
  83   }
  84 
  85   template <class T> void write_ref_array_pre_work(T* dst, int count);
  86   virtual void write_ref_array_pre(oop* dst, int count, bool dest_uninitialized);
  87   virtual void write_ref_array_pre(narrowOop* dst, int count, bool dest_uninitialized);
  88 
  89 /*
  90    Claimed and deferred bits are used together in G1 during the evacuation
  91    pause. These bits can have the following state transitions:
  92    1. The claimed bit can be put over any other card state. Except that
  93       the "dirty -> dirty and claimed" transition is checked for in
  94       G1 code and is not used.
  95    2. Deferred bit can be set only if the previous state of the card
  96       was either clean or claimed. mark_card_deferred() is wait-free.
  97       We do not care if the operation is be successful because if
  98       it does not it will only result in duplicate entry in the update
  99       buffer because of the "cache-miss". So it's not worth spinning.
 100  */
 101 
 102   bool is_card_claimed(size_t card_index) {
 103     jbyte val = _byte_map[card_index];
 104     return (val & (clean_card_mask_val() | claimed_card_val())) == claimed_card_val();
 105   }
 106 
 107   void set_card_claimed(size_t card_index) {
 108       jbyte val = _byte_map[card_index];
 109       if (val == clean_card_val()) {
 110         val = (jbyte)claimed_card_val();
 111       } else {
 112         val |= (jbyte)claimed_card_val();
 113       }
 114       _byte_map[card_index] = val;
 115   }
 116 
 117   void verify_g1_young_region(MemRegion mr) PRODUCT_RETURN;
 118   void g1_mark_as_young(const MemRegion& mr);
 119 
 120   bool mark_card_deferred(size_t card_index);
 121 
 122   bool is_card_deferred(size_t card_index) {
 123     jbyte val = _byte_map[card_index];
 124     return (val & (clean_card_mask_val() | deferred_card_val())) == deferred_card_val();
 125   }
 126   virtual void write_ref_nmethod_pre(oop* dst, nmethod* nm);
 127   virtual void write_ref_nmethod_post(oop* dst, nmethod* nm);
 128 
 129 };
 130 
 131 template<>
 132 struct BarrierSet::GetName<G1SATBCardTableModRefBS> {
 133   static const BarrierSet::Name value = BarrierSet::G1SATBCT;
 134 };
 135 
 136 class G1SATBCardTableLoggingModRefBSChangedListener : public G1MappingChangedListener {
 137  private:
 138   G1SATBCardTableLoggingModRefBS* _card_table;
 139  public:
 140   G1SATBCardTableLoggingModRefBSChangedListener() : _card_table(NULL) { }
 141 
 142   void set_card_table(G1SATBCardTableLoggingModRefBS* card_table) { _card_table = card_table; }
 143 
 144   virtual void on_commit(uint start_idx, size_t num_regions, bool zero_filled);
 145 };
 146 
 147 // Adds card-table logging to the post-barrier.
 148 // Usual invariant: all dirty cards are logged in the DirtyCardQueueSet.
 149 class G1SATBCardTableLoggingModRefBS: public G1SATBCardTableModRefBS {
 150   friend class G1SATBCardTableLoggingModRefBSChangedListener;
 151  private:
 152   G1SATBCardTableLoggingModRefBSChangedListener _listener;
 153   DirtyCardQueueSet& _dcqs;
 154 
 155  protected:
 156   virtual void write_ref_field_work(void* field, oop new_val, bool release);
 157 
 158  public:
 159   static size_t compute_size(size_t mem_region_size_in_words) {
 160     size_t number_of_slots = (mem_region_size_in_words / card_size_in_words);
 161     return ReservedSpace::allocation_align_size_up(number_of_slots);
 162   }
 163 
 164   // Returns how many bytes of the heap a single byte of the Card Table corresponds to.
 165   static size_t heap_map_factor() {
 166     return CardTableModRefBS::card_size;
 167   }
 168 
 169   G1SATBCardTableLoggingModRefBS(MemRegion whole_heap);
 170 
 171   virtual void initialize() { }
 172   virtual void initialize(G1RegionToSpaceMapper* mapper);
 173 
 174   virtual void resize_covered_region(MemRegion new_region) { ShouldNotReachHere(); }
 175 
 176   // Can be called from static contexts.
 177   static void write_ref_field_static(void* field, oop new_val);
 178 
 179   // NB: if you do a whole-heap invalidation, the "usual invariant" defined
 180   // above no longer applies.
 181   void invalidate(MemRegion mr, bool whole_heap = false);
 182 
 183   void write_region_work(MemRegion mr)    { invalidate(mr); }
 184   void write_ref_array_work(MemRegion mr) { invalidate(mr); }
 185 };
 186 
 187 template<>
 188 struct BarrierSet::GetName<G1SATBCardTableLoggingModRefBS> {
 189   static const BarrierSet::Name value = BarrierSet::G1SATBCTLogging;
 190 };
 191 
 192 #endif // SHARE_VM_GC_G1_G1SATBCARDTABLEMODREFBS_HPP