1 /*
2 * Copyright (c) 2014, 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_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 // Allocation attempt during GC for a survivor object / PLAB.
79 HeapWord* survivor_attempt_allocation(uint node_index,
80 size_t min_word_size,
81 size_t desired_word_size,
82 size_t* actual_word_size);
83
84 // Allocation attempt during GC for an old object / PLAB.
85 HeapWord* old_attempt_allocation(size_t min_word_size,
86 size_t desired_word_size,
87 size_t* actual_word_size);
88
89 public:
90 G1Allocator(G1CollectedHeap* heap);
91 ~G1Allocator();
92
93 uint num_nodes() { return (uint)_num_alloc_regions; }
94
95 #ifdef ASSERT
96 // Do we currently have an active mutator region to allocate into?
97 bool has_mutator_alloc_region();
98 #endif
99
100 void init_mutator_alloc_regions();
101 void release_mutator_alloc_regions();
102
103 void init_gc_alloc_regions(G1EvacInfo* evacuation_info);
104 void release_gc_alloc_regions(G1EvacInfo* evacuation_info);
105 void abandon_gc_alloc_regions();
106
107 bool is_retained_old_region(G1HeapRegion* hr);
108 // Return the amount of free bytes in the current retained old region.
109 size_t free_bytes_in_retained_old_region() const;
110
111 // Node index of current thread.
112 inline uint current_node_index() const;
113
114 // Allocate blocks of memory during mutator time.
115
116 // Attempt allocation in the current alloc region.
117 inline HeapWord* attempt_allocation(uint node_index,
118 size_t min_word_size,
119 size_t desired_word_size,
120 size_t* actual_word_size);
121
122 // This is to be called when holding an appropriate lock. It first tries in the
123 // current allocation region, and then attempts an allocation using a new region.
124 inline HeapWord* attempt_allocation_locked(uint node_index, size_t word_size);
125
126 size_t unsafe_max_tlab_alloc();
127 size_t used_in_alloc_regions();
128
129 // Allocate blocks of memory during garbage collection. Will ensure an
130 // allocation region, either by picking one or expanding the
131 // heap, and then allocate a block of the given size. The block
132 // may not be a humongous - it must fit into a single heap region.
133 HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,
134 uint node_index,
135 size_t word_size
136 );
137
138 HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,
139 uint node_index,
140 size_t min_word_size,
141 size_t desired_word_size,
142 size_t* actual_word_size);
143 };
144
145 // Manages the PLABs used during garbage collection. Interface for allocation from PLABs.
146 // Needs to handle multiple contexts, extra alignment in any "survivor" area and some
147 // statistics.
148 class G1PLABAllocator : public CHeapObj<mtGC> {
149 friend class G1ParScanThreadState;
150 private:
151 typedef G1HeapRegionAttr::region_type_t region_type_t;
152
153 G1CollectedHeap* _g1h;
154 G1Allocator* _allocator;
155
156 // Collects per-destination information (e.g. young, old gen) about current PLAB
157 // and statistics about it.
158 struct PLABData {
159 PLAB** _alloc_buffer;
160
161 size_t _direct_allocated; // Number of words allocated directly (not counting PLAB allocation).
162 size_t _num_plab_fills; // Number of PLAB refills experienced so far.
163 size_t _num_direct_allocations; // Number of direct allocations experienced so far.
164
165 size_t _plab_fill_counter; // How many PLAB refills left until boosting.
166 size_t _cur_desired_plab_size; // Current desired PLAB size incorporating eventual boosting.
167
168 uint _num_alloc_buffers; // The number of PLABs for this destination.
169
170 PLABData();
171 ~PLABData();
172
173 void initialize(uint num_alloc_buffers, size_t desired_plab_size, size_t tolerated_refills);
174
175 // Should we actually boost the PLAB size?
176 // The _plab_refill_counter reset value encodes the ResizePLAB flag value already, so no
177 // need to check here.
178 bool should_boost() const { return _plab_fill_counter == 0; }
179
180 void notify_plab_refill(size_t tolerated_refills, size_t next_plab_size);
181
182 } _dest_data[G1HeapRegionAttr::Num];
183
184 // The amount of PLAB refills tolerated until boosting PLAB size.
185 // This value is the same for all generations because they all use the same
186 // resizing logic.
187 size_t _tolerated_refills;
188
189 void flush_and_retire_stats(uint num_workers);
190 inline PLAB* alloc_buffer(G1HeapRegionAttr dest, uint node_index) const;
191 inline PLAB* alloc_buffer(region_type_t dest, uint node_index) const;
192
193 // Returns the number of allocation buffers for the given dest.
194 // There is only 1 buffer for Old while Young may have multiple buffers depending on
195 // active NUMA nodes.
196 inline uint alloc_buffers_length(region_type_t dest) const;
197
198 bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;
199 public:
200 G1PLABAllocator(G1Allocator* allocator);
201
202 size_t waste() const;
203 size_t undo_waste() const;
204 size_t plab_size(G1HeapRegionAttr which) const;
205
206 // Allocate word_sz words in dest, either directly into the regions or by
207 // allocating a new PLAB. Returns the address of the allocated memory, null if
208 // not successful. Plab_refill_failed indicates whether an attempt to refill the
209 // PLAB failed or not.
210 HeapWord* allocate_direct_or_new_plab(G1HeapRegionAttr dest,
211 size_t word_sz,
212 bool* plab_refill_failed,
213 uint node_index);
214
215 // Allocate word_sz words in the PLAB of dest. Returns the address of the
216 // allocated memory, null if not successful.
217 inline HeapWord* plab_allocate(G1HeapRegionAttr dest,
218 size_t word_sz,
219 uint node_index);
220
221 inline HeapWord* allocate(G1HeapRegionAttr dest,
222 size_t word_sz,
223 bool* refill_failed,
224 uint node_index);
225
226 void undo_allocation(G1HeapRegionAttr dest, HeapWord* obj, size_t word_sz, uint node_index);
227 };
228
229 #endif // SHARE_GC_G1_G1ALLOCATOR_HPP