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_G1ALLOCATOR_HPP
26 #define SHARE_GC_G1_G1ALLOCATOR_HPP
27
28 #include "gc/g1/g1AllocRegion.hpp"
29 #include "gc/g1/g1HeapRegionAttr.hpp"
30 #include "gc/shared/collectedHeap.hpp"
31 #include "gc/shared/plab.hpp"
32
33 class G1EvacInfo;
34 class G1NUMA;
35
36 // Interface to keep track of which regions G1 is currently allocating into. Provides
37 // some accessors (e.g. allocating into them, or getting their occupancy).
38 // Also keeps track of retained regions across GCs.
39 class G1Allocator : public CHeapObj<mtGC> {
40 private:
41 G1CollectedHeap* _g1h;
42 G1NUMA* _numa;
43
44 bool _survivor_is_full;
45 bool _old_is_full;
46
47 // The number of MutatorAllocRegions used, one per memory node.
48 size_t _num_alloc_regions;
49
50 // Alloc region used to satisfy mutator allocation requests.
51 MutatorAllocRegion* _mutator_alloc_regions;
52
53 // Alloc region used to satisfy allocation requests by the GC for
54 // survivor objects.
55 SurvivorGCAllocRegion* _survivor_gc_alloc_regions;
56
57 // Alloc region used to satisfy allocation requests by the GC for
58 // old objects.
59 OldGCAllocRegion _old_gc_alloc_region;
60
61 G1HeapRegion* _retained_old_gc_alloc_region;
62
63 bool survivor_is_full() const;
64 bool old_is_full() const;
65
66 void set_survivor_full();
67 void set_old_full();
68
69 void reuse_retained_old_region(G1EvacInfo* evacuation_info,
70 OldGCAllocRegion* old,
71 G1HeapRegion** retained);
72
73 // Accessors to the allocation regions.
74 inline MutatorAllocRegion* mutator_alloc_region(uint node_index);
75 inline SurvivorGCAllocRegion* survivor_gc_alloc_region(uint node_index);
76 inline OldGCAllocRegion* old_gc_alloc_region();
77
78 void assert_not_humongous(size_t word_size) NOT_DEBUG_RETURN;
79
80 // Allocation attempt during GC for a survivor object / PLAB.
81 HeapWord* survivor_attempt_allocation(uint node_index,
82 size_t min_word_size,
83 size_t desired_word_size,
84 size_t* actual_word_size);
85
86 // Allocation attempt during GC for an old object / PLAB.
87 HeapWord* old_attempt_allocation(size_t min_word_size,
88 size_t desired_word_size,
89 size_t* actual_word_size);
90
91 public:
92 G1Allocator(G1CollectedHeap* heap);
93 ~G1Allocator();
94
95 uint num_nodes() { return (uint)_num_alloc_regions; }
96
97 #ifdef ASSERT
98 // Do we currently have an active mutator region to allocate into?
99 bool has_mutator_alloc_region();
100 #endif
101
102 void init_mutator_alloc_regions();
103 void release_mutator_alloc_regions();
104
105 void init_gc_alloc_regions(G1EvacInfo* evacuation_info);
106 void release_gc_alloc_regions(G1EvacInfo* evacuation_info);
107 void abandon_gc_alloc_regions();
108
109 bool is_retained_old_region(G1HeapRegion* hr);
110 // Return the amount of free bytes in the current retained old region.
111 size_t free_bytes_in_retained_old_region() const;
112
113 // Node index of current thread.
114 inline uint current_node_index() const;
115
116 // Allocate blocks of memory during mutator time.
117
118 // Attempt allocation in the current alloc region.
119 inline HeapWord* attempt_allocation(uint node_index,
120 size_t min_word_size,
121 size_t desired_word_size,
122 size_t* actual_word_size);
123
124 // This is to be called when holding an appropriate lock. It first tries in the
125 // current allocation region, and then attempts an allocation using a new region.
126 inline HeapWord* attempt_allocation_locked(uint node_index, size_t word_size);
127
128 size_t unsafe_max_tlab_alloc();
129 size_t used_in_alloc_regions();
130
131 // Allocate blocks of memory during garbage collection. Will ensure an
132 // allocation region, either by picking one or expanding the
133 // heap, and then allocate a block of the given size. The block
134 // may not be a humongous - it must fit into a single heap region.
135 HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,
136 uint node_index,
137 size_t word_size
138 );
139
140 HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,
141 uint node_index,
142 size_t min_word_size,
143 size_t desired_word_size,
144 size_t* actual_word_size);
145 };
146
147 // Manages the PLABs used during garbage collection. Interface for allocation from PLABs.
148 // Needs to handle multiple contexts, extra alignment in any "survivor" area and some
149 // statistics.
150 class G1PLABAllocator : public CHeapObj<mtGC> {
151 friend class G1ParScanThreadState;
152 private:
153 typedef G1HeapRegionAttr::region_type_t region_type_t;
154
155 G1CollectedHeap* _g1h;
156 G1Allocator* _allocator;
157
158 // Collects per-destination information (e.g. young, old gen) about current PLAB
159 // and statistics about it.
160 struct PLABData {
161 PLAB** _alloc_buffer;
162
163 size_t _direct_allocated; // Number of words allocated directly (not counting PLAB allocation).
164 size_t _num_plab_fills; // Number of PLAB refills experienced so far.
165 size_t _num_direct_allocations; // Number of direct allocations experienced so far.
166
167 size_t _plab_fill_counter; // How many PLAB refills left until boosting.
168 size_t _cur_desired_plab_size; // Current desired PLAB size incorporating eventual boosting.
169
170 uint _num_alloc_buffers; // The number of PLABs for this destination.
171
172 PLABData();
173 ~PLABData();
174
175 void initialize(uint num_alloc_buffers, size_t desired_plab_size, size_t tolerated_refills);
176
177 // Should we actually boost the PLAB size?
178 // The _plab_refill_counter reset value encodes the ResizePLAB flag value already, so no
179 // need to check here.
180 bool should_boost() const { return _plab_fill_counter == 0; }
181
182 void notify_plab_refill(size_t tolerated_refills, size_t next_plab_size);
183
184 } _dest_data[G1HeapRegionAttr::Num];
185
186 // The amount of PLAB refills tolerated until boosting PLAB size.
187 // This value is the same for all generations because they all use the same
188 // resizing logic.
189 size_t _tolerated_refills;
190
191 void flush_and_retire_stats(uint num_workers);
192 inline PLAB* alloc_buffer(G1HeapRegionAttr dest, uint node_index) const;
193 inline PLAB* alloc_buffer(region_type_t dest, uint node_index) const;
194
195 // Returns the number of allocation buffers for the given dest.
196 // There is only 1 buffer for Old while Young may have multiple buffers depending on
197 // active NUMA nodes.
198 inline uint alloc_buffers_length(region_type_t dest) const;
199
200 bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;
201 public:
202 G1PLABAllocator(G1Allocator* allocator);
203
204 size_t waste() const;
205 size_t undo_waste() const;
206 size_t plab_size(G1HeapRegionAttr which) const;
207
208 // Allocate word_sz words in dest, either directly into the regions or by
209 // allocating a new PLAB. Returns the address of the allocated memory, null if
210 // not successful. Plab_refill_failed indicates whether an attempt to refill the
211 // PLAB failed or not.
212 HeapWord* allocate_direct_or_new_plab(G1HeapRegionAttr dest,
213 size_t word_sz,
214 bool* plab_refill_failed,
215 uint node_index);
216
217 // Allocate word_sz words in the PLAB of dest. Returns the address of the
218 // allocated memory, null if not successful.
219 inline HeapWord* plab_allocate(G1HeapRegionAttr dest,
220 size_t word_sz,
221 uint node_index);
222
223 inline HeapWord* allocate(G1HeapRegionAttr dest,
224 size_t word_sz,
225 bool* refill_failed,
226 uint node_index);
227
228 void undo_allocation(G1HeapRegionAttr dest, HeapWord* obj, size_t word_sz, uint node_index);
229 };
230
231 #endif // SHARE_GC_G1_G1ALLOCATOR_HPP