1 /* 2 * Copyright (c) 2001, 2025, 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_GC_G1_G1BARRIERSET_HPP 26 #define SHARE_GC_G1_G1BARRIERSET_HPP 27 28 #include "gc/g1/g1SATBMarkQueueSet.hpp" 29 #include "gc/shared/bufferNode.hpp" 30 #include "gc/shared/cardTable.hpp" 31 #include "gc/shared/cardTableBarrierSet.hpp" 32 33 class G1CardTable; 34 class Thread; 35 36 // This barrier set is specialized to manage two card tables: 37 // * one the mutator is currently working on ("card table") 38 // * one the refinement threads or GC during pause are working on ("refinement table") 39 // 40 // The card table acts like a regular card table where the mutator dirties cards 41 // containing potentially interesting references. 42 // 43 // When the amount of dirty cards on the card table exceeds a threshold, G1 swaps 44 // the card tables and has the refinement threads reduce them by "refining" 45 // them. 46 // I.e. refinement looks at all dirty cards on the refinement table, and updates 47 // the remembered sets accordingly, clearing the cards on the refinement table. 48 // 49 // Meanwhile the mutator continues dirtying the now empty card table. 50 // 51 // This separation of data the mutator and refinement threads are working on 52 // removes the need for any fine-grained (per mutator write) synchronization between 53 // them, keeping the write barrier simple. 54 // 55 // The refinement threads mark cards in the current collection set specially on the 56 // card table - this is fine wrt synchronization with the mutator, because at 57 // most the mutator will overwrite it again if there is a race, as G1 will scan the 58 // entire card either way during the GC pause. 59 // 60 // During garbage collection, if the refinement table is known to be non-empty, G1 61 // merges it back (and cleaning it) to the card table which is scanned for dirty 62 // cards. 63 // 64 class G1BarrierSet: public CardTableBarrierSet { 65 private: 66 BufferNode::Allocator _satb_mark_queue_buffer_allocator; 67 G1SATBMarkQueueSet _satb_mark_queue_set; 68 69 G1CardTable* _refinement_table; 70 71 public: 72 G1BarrierSet(G1CardTable* card_table, G1CardTable* refinement_table); 73 virtual ~G1BarrierSet(); 74 75 static G1BarrierSet* g1_barrier_set() { 76 return barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set()); 77 } 78 79 G1CardTable* refinement_table() const { return _refinement_table; } 80 81 // Swap the global card table references, without synchronization. 82 void swap_global_card_table(); 83 84 // Update the given thread's card table (byte map) base to the current card table's. 85 void update_card_table_base(Thread* thread); 86 87 virtual bool card_mark_must_follow_store() const { 88 return true; 89 } 90 91 // Add "pre_val" to a set of objects that may have been disconnected from the 92 // pre-marking object graph. Prefer the version that takes location, as it 93 // can avoid touching the heap unnecessarily. 94 template <class T> static void enqueue(T* dst); 95 static void enqueue_preloaded(oop pre_val); 96 97 static void enqueue_preloaded_if_weak(DecoratorSet decorators, oop value); 98 99 template <class T> void write_ref_array_pre_work(T* dst, size_t count); 100 virtual void write_ref_array_pre(oop* dst, size_t count, bool dest_uninitialized); 101 virtual void write_ref_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized); 102 103 template <DecoratorSet decorators, typename T> 104 void write_ref_field_pre(T* field); 105 106 inline void write_region(MemRegion mr); 107 void write_region(JavaThread* thread, MemRegion mr); 108 109 template <DecoratorSet decorators = DECORATORS_NONE, typename T> 110 void write_ref_field_post(T* field); 111 112 virtual void on_thread_create(Thread* thread); 113 virtual void on_thread_destroy(Thread* thread); 114 virtual void on_thread_attach(Thread* thread); 115 virtual void on_thread_detach(Thread* thread); 116 117 static G1SATBMarkQueueSet& satb_mark_queue_set() { 118 return g1_barrier_set()->_satb_mark_queue_set; 119 } 120 121 virtual void print_on(outputStream* st) const; 122 123 // Callbacks for runtime accesses. 124 template <DecoratorSet decorators, typename BarrierSetT = G1BarrierSet> 125 class AccessBarrier: public ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT> { 126 typedef ModRefBarrierSet::AccessBarrier<decorators, BarrierSetT> ModRef; 127 typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw; 128 129 public: 130 // Needed for loads on non-heap weak references 131 template <typename T> 132 static oop oop_load_not_in_heap(T* addr); 133 134 // Needed for non-heap stores 135 template <typename T> 136 static void oop_store_not_in_heap(T* addr, oop new_value); 137 138 // Needed for weak references 139 static oop oop_load_in_heap_at(oop base, ptrdiff_t offset); 140 141 // Defensive: will catch weak oops at addresses in heap 142 template <typename T> 143 static oop oop_load_in_heap(T* addr); 144 145 template <typename T> 146 static oop oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value); 147 template <typename T> 148 static oop oop_atomic_xchg_not_in_heap(T* addr, oop new_value); 149 }; 150 }; 151 152 template<> 153 struct BarrierSet::GetName<G1BarrierSet> { 154 static const BarrierSet::Name value = BarrierSet::G1BarrierSet; 155 }; 156 157 template<> 158 struct BarrierSet::GetType<BarrierSet::G1BarrierSet> { 159 typedef ::G1BarrierSet type; 160 }; 161 162 #endif // SHARE_GC_G1_G1BARRIERSET_HPP