1 /*
2 * Copyright (c) 2001, 2026, 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 "gc/g1/g1BarrierSet.inline.hpp"
26 #include "gc/g1/g1BarrierSetAssembler.hpp"
27 #include "gc/g1/g1CardTable.inline.hpp"
28 #include "gc/g1/g1CollectedHeap.inline.hpp"
29 #include "gc/g1/g1HeapRegion.hpp"
30 #include "gc/g1/g1RegionPinCache.inline.hpp"
31 #include "gc/g1/g1SATBMarkQueueSet.hpp"
32 #include "gc/g1/g1ThreadLocalData.hpp"
33 #include "gc/shared/satbMarkQueue.hpp"
34 #include "logging/log.hpp"
35 #include "memory/iterator.hpp"
36 #include "oops/access.inline.hpp"
37 #include "oops/compressedOops.inline.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "runtime/interfaceSupport.inline.hpp"
40 #include "runtime/javaThread.hpp"
41 #include "runtime/orderAccess.hpp"
42 #include "runtime/threads.hpp"
43 #include "utilities/macros.hpp"
44 #ifdef COMPILER1
45 #include "gc/g1/c1/g1BarrierSetC1.hpp"
46 #endif
47 #ifdef COMPILER2
48 #include "gc/g1/c2/g1BarrierSetC2.hpp"
49 #endif
50
51 class G1BarrierSetC1;
52 class G1BarrierSetC2;
53
54 G1BarrierSet::G1BarrierSet(G1CardTable* card_table,
55 G1CardTable* refinement_table) :
56 CardTableBarrierSet(make_barrier_set_assembler<G1BarrierSetAssembler>(),
57 make_barrier_set_c1<G1BarrierSetC1>(),
58 make_barrier_set_c2<G1BarrierSetC2>(),
59 card_table,
60 BarrierSet::FakeRtti(BarrierSet::G1BarrierSet)),
61 _satb_mark_queue_buffer_allocator("SATB Buffer Allocator", G1SATBBufferSize),
62 _satb_mark_queue_set(&_satb_mark_queue_buffer_allocator),
63 _refinement_table(refinement_table)
64 {}
65
66 G1BarrierSet::~G1BarrierSet() {
67 delete refinement_table();
68 }
69
70 void G1BarrierSet::swap_global_card_table() {
71 G1CardTable* temp = static_cast<G1CardTable*>(card_table());
72 _card_table.store_relaxed(refinement_table());
73 _refinement_table.store_relaxed(temp);
74 }
75
76 void G1BarrierSet::update_card_table_base(Thread* thread) {
77 #ifdef ASSERT
78 {
79 ResourceMark rm;
80 assert(thread->is_Java_thread(), "may only update card table base of JavaThreads, not %s", thread->name());
81 }
82 #endif
83 G1ThreadLocalData::set_byte_map_base(thread, card_table()->byte_map_base());
84 }
85
86 template <class T> void
87 G1BarrierSet::write_ref_array_pre_work(T* dst, size_t count) {
88 G1SATBMarkQueueSet& queue_set = G1BarrierSet::satb_mark_queue_set();
89 if (!queue_set.is_active()) return;
90
91 SATBMarkQueue& queue = G1ThreadLocalData::satb_mark_queue(Thread::current());
92
93 T* elem_ptr = dst;
94 for (size_t i = 0; i < count; i++, elem_ptr++) {
95 T heap_oop = RawAccess<>::oop_load(elem_ptr);
96 if (!CompressedOops::is_null(heap_oop)) {
97 queue_set.enqueue_known_active(queue, CompressedOops::decode_not_null(heap_oop));
98 }
99 }
100 }
101
102 void G1BarrierSet::write_ref_array_pre(oop* dst, size_t count) {
103 write_ref_array_pre_work(dst, count);
104 }
105
106 void G1BarrierSet::write_ref_array_pre(narrowOop* dst, size_t count) {
107 write_ref_array_pre_work(dst, count);
108 }
109
110 void G1BarrierSet::write_region(MemRegion mr) {
111 if (mr.is_empty()) {
112 return;
113 }
114
115 // Skip writes to young gen.
116 if (G1CollectedHeap::heap()->heap_region_containing(mr.start())->is_young()) {
117 // MemRegion should not span multiple regions for arrays in young gen.
118 DEBUG_ONLY(G1HeapRegion* containing_hr = G1CollectedHeap::heap()->heap_region_containing(mr.start());)
119 assert(containing_hr->is_young(), "it should be young");
120 assert(containing_hr->is_in(mr.start()), "it should contain start");
121 assert(containing_hr->is_in(mr.last()), "it should also contain last");
122 return;
123 }
124
125 // We need to make sure that we get the start/end byte information for the area
126 // to mark from the same card table to avoid getting confused in the mark loop
127 // further below - we might execute while the global card table is being switched.
128 //
129 // It does not matter which card table we write to: at worst we may write to the
130 // new card table (after the switching), which means that we will catch the
131 // marks next time.
132 // If we write to the old card table (after the switching, then the refinement
133 // table) the oncoming handshake will do the memory synchronization.
134 CardTable* local_card_table = card_table();
135
136 volatile CardValue* byte = local_card_table->byte_for(mr.start());
137 CardValue* last_byte = local_card_table->byte_for(mr.last());
138
139 // Dirty cards only if necessary.
140 for (; byte <= last_byte; byte++) {
141 CardValue bv = *byte;
142 if (bv == G1CardTable::clean_card_val()) {
143 *byte = G1CardTable::dirty_card_val();
144 }
145 }
146 }
147
148 void G1BarrierSet::on_thread_create(Thread* thread) {
149 // Create thread local data
150 G1ThreadLocalData::create(thread);
151 }
152
153 void G1BarrierSet::on_thread_destroy(Thread* thread) {
154 // Destroy thread local data
155 G1ThreadLocalData::destroy(thread);
156 }
157
158 void G1BarrierSet::on_thread_attach(Thread* thread) {
159 BarrierSet::on_thread_attach(thread);
160 SATBMarkQueue& satbq = G1ThreadLocalData::satb_mark_queue(thread);
161 assert(!satbq.is_active(), "SATB queue should not be active");
162 assert(satbq.buffer() == nullptr, "SATB queue should not have a buffer");
163 assert(satbq.index() == 0, "SATB queue index should be zero");
164 // If we are creating the thread during a marking cycle, we should
165 // set the active field of the SATB queue to true. That involves
166 // copying the global is_active value to this thread's queue.
167 satbq.set_active(_satb_mark_queue_set.is_active());
168
169 if (thread->is_Java_thread()) {
170 assert(Threads_lock->is_locked(), "must be, synchronization with refinement.");
171 update_card_table_base(thread);
172 }
173 }
174
175 void G1BarrierSet::on_thread_detach(Thread* thread) {
176 // Flush any deferred card marks.
177 CardTableBarrierSet::on_thread_detach(thread);
178 {
179 SATBMarkQueue& queue = G1ThreadLocalData::satb_mark_queue(thread);
180 G1BarrierSet::satb_mark_queue_set().flush_queue(queue);
181 }
182 {
183 G1RegionPinCache& cache = G1ThreadLocalData::pin_count_cache(thread);
184 cache.flush();
185 }
186 }
187
188 void G1BarrierSet::print_on(outputStream* st) const {
189 card_table()->print_on(st, "Card");
190 refinement_table()->print_on(st, "Refinement");
191 }