1 /*
2 * Copyright (c) 2014, 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 #ifndef SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP
26 #define SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP
27
28 #include "gc/g1/g1CollectedHeap.hpp"
29 #include "gc/g1/g1OopClosures.hpp"
30 #include "gc/g1/g1YoungGCAllocationFailureInjector.hpp"
31 #include "gc/shared/ageTable.hpp"
32 #include "gc/shared/copyFailedInfo.hpp"
33 #include "gc/shared/gc_globals.hpp"
34 #include "gc/shared/partialArraySplitter.hpp"
35 #include "gc/shared/partialArrayState.hpp"
36 #include "gc/shared/stringdedup/stringDedup.hpp"
37 #include "gc/shared/taskqueue.hpp"
38 #include "memory/allocation.hpp"
39 #include "oops/oop.hpp"
40 #include "runtime/atomic.hpp"
41 #include "utilities/growableArray.hpp"
42 #include "utilities/resizableHashTable.hpp"
43 #include "utilities/ticks.hpp"
44
45 class G1CardTable;
46 class G1CollectionSet;
47 class G1EvacFailureRegions;
48 class G1EvacuationRootClosures;
49 class G1OopStarChunkedList;
50 class G1ParScanThreadStateSet;
51 class G1PLABAllocator;
52 class G1HeapRegion;
53 class outputStream;
54
55 typedef GrowableArrayCHeap<nmethod*, mtGC> G1NmethodSet;
56 typedef ResizeableHashTable<uint, G1NmethodSet*, AnyObj::C_HEAP, mtGC> G1NmethodsToAdd;
57 class G1ParScanThreadState : public CHeapObj<mtGC> {
58 G1CollectedHeap* _g1h;
59 G1ParScanThreadStateSet* _per_thread_states;
60 G1ScannerTasksQueue* _task_queue;
61 G1CardTable* _ct;
62 G1EvacuationRootClosures* _closures;
63
64 G1PLABAllocator* _plab_allocator;
65
66 AgeTable _age_table;
67 // Local tenuring threshold.
68 uint _tenuring_threshold;
69 G1ScanEvacuatedObjClosure _scanner;
70
71 uint _worker_id;
72
73 size_t _num_cards_marked_dirty;
74 size_t _num_cards_marked_to_cset;
75
76 // Upper and lower threshold to start and end work queue draining.
77 uint const _stack_trim_upper_threshold;
78 uint const _stack_trim_lower_threshold;
79
80 Tickspan _trim_ticks;
81 // Map from young-age-index (0 == not young, 1 is youngest) to
82 // surviving words. base is what we get back from the malloc call
83 size_t* _surviving_young_words_base;
84 // this points into the array, as we use the first few entries for padding
85 size_t* _surviving_young_words;
86 // Number of elements in the array above.
87 size_t _surviving_words_length;
88 // Indicates whether in the last generation (old) there is no more space
89 // available for allocation.
90 bool _old_gen_is_full;
91 PartialArraySplitter _partial_array_splitter;
92 StringDedup::Requests _string_dedup_requests;
93
94 G1CardTable* ct() { return _ct; }
95
96 // Maximum number of optional regions at start of gc.
97 size_t _max_num_optional_regions;
98 G1OopStarChunkedList* _oops_into_optional_regions;
99
100 G1NUMA* _numa;
101 // Records how many object allocations happened at each node during copy to survivor.
102 // Only starts recording when log of gc+heap+numa is enabled and its data is
103 // transferred when flushed.
104 size_t* _obj_alloc_stat;
105
106 // The nmethods that were found during code root scan that need to be redistributed.
107 G1NmethodsToAdd _nmethods_to_add;
108
109 // Per-thread evacuation failure data structures.
110 ALLOCATION_FAILURE_INJECTOR_ONLY(size_t _allocation_failure_inject_counter;)
111
112 EvacuationFailedInfo _evacuation_failed_info;
113 G1EvacFailureRegions* _evac_failure_regions;
114 // Number of additional cards into evacuation failed regions.
115 size_t _num_cards_from_evac_failure;
116
117 // Mark the card if not already in the set; this is a best-effort attempt on
118 // detecting duplicates.
119 template <class T> bool mark_if_new(T* p, bool into_survivor);
120 // Mark the card of p into the (evacuation failed) region.
121 template <class T> void mark_card_into_evac_fail_region(T* p, oop obj);
122
123 bool inject_allocation_failure(uint region_idx) ALLOCATION_FAILURE_INJECTOR_RETURN_( return false; );
124
125 public:
126 G1ParScanThreadState(G1CollectedHeap* g1h,
127 G1ParScanThreadStateSet* per_thread_states,
128 uint worker_id,
129 uint num_workers,
130 G1CollectionSet* collection_set,
131 G1EvacFailureRegions* evac_failure_regions);
132 virtual ~G1ParScanThreadState();
133
134 void set_ref_discoverer(ReferenceDiscoverer* rd) { _scanner.set_ref_discoverer(rd); }
135
136 #ifdef ASSERT
137 bool queue_is_empty() const { return _task_queue->is_empty(); }
138 #endif
139
140 void verify_task(narrowOop* task) const NOT_DEBUG_RETURN;
141 void verify_task(oop* task) const NOT_DEBUG_RETURN;
142 void verify_task(PartialArrayState* task) const NOT_DEBUG_RETURN;
143 void verify_task(ScannerTask task) const NOT_DEBUG_RETURN;
144
145 void push_on_queue(ScannerTask task);
146
147 // Apply the post barrier to the given reference field. Marks the card of p
148 // if the barrier does not filter out the reference for some reason (e.g.
149 // p and q are in the same region, p is in survivor, p is in collection set)
150 // To be called during GC if nothing particular about p and obj are known.
151 template <class T> void write_ref_field_post(T* p, oop obj);
152
153 // Mark the card if the reference's target region's remembered set is tracked.
154 // Assumes that a significant amount of pre-filtering (like done by
155 // write_ref_field_post() above) has already been performed.
156 template <class T> void mark_card_if_tracked(G1HeapRegionAttr region_attr, T* p, oop o);
157
158 G1EvacuationRootClosures* closures() { return _closures; }
159 uint worker_id() { return _worker_id; }
160
161 size_t lab_waste_words() const;
162 size_t lab_undo_waste_words() const;
163
164 // Newly marked cards during this garbage collection, to be refined concurrently
165 // later. Contains both marks generated by new cross-region references as well
166 // as cards generated from regions into evacuation failed regions.
167 // Does not contain cards into the next collection set (e.g. survivors) - they will not
168 // be refined concurrently. Calculation is done on a best-effort basis.
169 size_t num_cards_pending() const;
170 // Number of cards newly generated by references into evacuation failed regions.
171 // Calculation is done on a best-effort basis.
172 size_t num_cards_from_evac_failure() const;
173 // Sum of cards marked by evacuation. Contains both pending cards as well as cards
174 // into the next collection set (e.g. survivors).
175 size_t num_cards_marked() const;
176
177 // Pass locally gathered statistics to global state. Returns the total number of
178 // HeapWords copied.
179 size_t flush_stats(size_t* surviving_young_words, uint num_workers);
180
181 #if TASKQUEUE_STATS
182 PartialArrayTaskStats* partial_array_task_stats();
183 #endif // TASKQUEUE_STATS
184
185 private:
186 void do_partial_array(PartialArrayState* state, bool stolen);
187 void start_partial_objarray(oop from, oop to);
188
189 HeapWord* allocate_copy_slow(G1HeapRegionAttr* dest_attr,
190 Klass* klass,
191 size_t word_sz,
192 uint age,
193 uint node_index);
194
195 void undo_allocation(G1HeapRegionAttr dest_addr,
196 HeapWord* obj_ptr,
197 size_t word_sz,
198 uint node_index);
199
200 void update_bot_after_copying(oop obj, size_t word_sz);
201
202 void do_iterate_object(oop const obj,
203 oop const old,
204 Klass* const klass,
205 G1HeapRegionAttr const region_attr,
206 G1HeapRegionAttr const dest_attr,
207 uint age);
208 oop do_copy_to_survivor_space(G1HeapRegionAttr region_attr,
209 oop obj,
210 markWord old_mark);
211
212 // This method is applied to the fields of the objects that have just been copied.
213 template <class T> void do_oop_evac(T* p);
214
215 void dispatch_task(ScannerTask task, bool stolen);
216
217 // Tries to allocate word_sz in the PLAB of the next "generation" after trying to
218 // allocate into dest. Previous_plab_refill_failed indicates whether previous
219 // PLAB refill for the original (source) object failed.
220 // Returns a non-null pointer if successful, and updates dest if required.
221 // Also determines whether we should continue to try to allocate into the various
222 // generations or just end trying to allocate.
223 HeapWord* allocate_in_next_plab(G1HeapRegionAttr* dest,
224 size_t word_sz,
225 bool previous_plab_refill_failed,
226 uint node_index);
227
228 inline G1HeapRegionAttr next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age);
229
230 void report_promotion_event(G1HeapRegionAttr const dest_attr,
231 Klass* klass, size_t word_sz, uint age,
232 HeapWord * const obj_ptr, uint node_index) const;
233
234 void trim_queue_to_threshold(uint threshold);
235
236 inline bool needs_partial_trimming() const;
237
238 // NUMA statistics related methods.
239 void initialize_numa_stats();
240 void flush_numa_stats();
241 inline void update_numa_stats(uint node_index);
242
243 public:
244 oop copy_to_survivor_space(G1HeapRegionAttr region_attr, oop obj, markWord old_mark);
245
246 inline void trim_queue();
247 inline void trim_queue_partially();
248 void steal_and_trim_queue(G1ScannerTasksQueueSet *task_queues);
249
250 Tickspan trim_ticks() const;
251 void reset_trim_ticks();
252
253 void record_evacuation_failed_region(G1HeapRegion* r, uint worker_id, bool cause_pinned);
254 // An attempt to evacuate "obj" has failed; take necessary steps.
255 oop handle_evacuation_failure_par(oop obj, markWord m, Klass* klass, G1HeapRegionAttr attr, size_t word_sz, bool cause_pinned);
256
257 inline void remember_nmethod_into_region(G1HeapRegion* r, nmethod* nm);
258 // Updates the global set of regions that need updates to the code root set
259 // later with the ones gathered so far.
260 void update_nmethod_regions_to_add();
261
262 inline size_t num_nmethods(uint index) const;
263 // Iterate nmethods stored for the given region index.
264 template <typename Function>
265 inline void iterate_nmethods(uint index, Function fn);
266
267 template <typename T>
268 inline void remember_root_into_optional_region(T* p);
269 template <typename T>
270 inline void remember_reference_into_optional_region(T* p);
271
272 inline G1OopStarChunkedList* oops_into_optional_region(const G1HeapRegion* hr);
273 };
274
275 class G1ParScanThreadStateSet : public StackObj {
276 G1CollectedHeap* _g1h;
277 G1CollectionSet* _collection_set;
278 G1ParScanThreadState** _states;
279 size_t* _surviving_young_words_total;
280 uint _num_workers;
281 bool _flushed;
282 G1EvacFailureRegions* _evac_failure_regions;
283
284 CHeapBitMap _has_nmethods_to_add;
285 Atomic<uint> _num_nmethod_regions_to_add;
286 uint* _nmethod_regions_to_add;
287
288 public:
289 G1ParScanThreadStateSet(G1CollectedHeap* g1h,
290 uint num_workers,
291 G1CollectionSet* collection_set,
292 G1EvacFailureRegions* evac_failure_regions);
293 ~G1ParScanThreadStateSet();
294
295 void flush_stats();
296 void destroy_worker_states();
297
298 // Updates the region set that has code root updates with the regions in the given set.
299 void update_nmethod_regions_to_add(G1NmethodsToAdd* nmethods);
300 void par_iterate_nmethod_regions_to_add(G1HeapRegionClosure* cl,
301 G1HeapRegionClaimer* claimer,
302 uint worker_id);
303 uint num_nmethod_regions_to_add() const { return _num_nmethod_regions_to_add.load_relaxed(); }
304
305 void record_unused_optional_region(G1HeapRegion* hr);
306 #if TASKQUEUE_STATS
307 void print_partial_array_task_stats();
308 #endif // TASKQUEUE_STATS
309
310 G1ParScanThreadState* state_for_worker(uint worker_id);
311 uint num_workers() const { return _num_workers; }
312
313 const size_t* surviving_young_words() const;
314 };
315
316 #endif // SHARE_GC_G1_G1PARSCANTHREADSTATE_HPP