Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/share/gc/g1/g1Allocator.hpp
66644 views
1
/*
2
* Copyright (c) 2014, 2020, 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 G1EvacuationInfo;
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
friend class VMStructs;
41
42
private:
43
G1CollectedHeap* _g1h;
44
G1NUMA* _numa;
45
46
bool _survivor_is_full;
47
bool _old_is_full;
48
49
// The number of MutatorAllocRegions used, one per memory node.
50
size_t _num_alloc_regions;
51
52
// Alloc region used to satisfy mutator allocation requests.
53
MutatorAllocRegion* _mutator_alloc_regions;
54
55
// Alloc region used to satisfy allocation requests by the GC for
56
// survivor objects.
57
SurvivorGCAllocRegion* _survivor_gc_alloc_regions;
58
59
// Alloc region used to satisfy allocation requests by the GC for
60
// old objects.
61
OldGCAllocRegion _old_gc_alloc_region;
62
63
HeapRegion* _retained_old_gc_alloc_region;
64
65
bool survivor_is_full() const;
66
bool old_is_full() const;
67
68
void set_survivor_full();
69
void set_old_full();
70
71
void reuse_retained_old_region(G1EvacuationInfo& evacuation_info,
72
OldGCAllocRegion* old,
73
HeapRegion** retained);
74
75
// Accessors to the allocation regions.
76
inline MutatorAllocRegion* mutator_alloc_region(uint node_index);
77
inline SurvivorGCAllocRegion* survivor_gc_alloc_region(uint node_index);
78
inline OldGCAllocRegion* old_gc_alloc_region();
79
80
// Allocation attempt during GC for a survivor object / PLAB.
81
HeapWord* survivor_attempt_allocation(size_t min_word_size,
82
size_t desired_word_size,
83
size_t* actual_word_size,
84
uint node_index);
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
// Node index of current thread.
92
inline uint current_node_index() const;
93
94
public:
95
G1Allocator(G1CollectedHeap* heap);
96
~G1Allocator();
97
98
uint num_nodes() { return (uint)_num_alloc_regions; }
99
100
#ifdef ASSERT
101
// Do we currently have an active mutator region to allocate into?
102
bool has_mutator_alloc_region();
103
#endif
104
105
void init_mutator_alloc_regions();
106
void release_mutator_alloc_regions();
107
108
void init_gc_alloc_regions(G1EvacuationInfo& evacuation_info);
109
void release_gc_alloc_regions(G1EvacuationInfo& evacuation_info);
110
void abandon_gc_alloc_regions();
111
bool is_retained_old_region(HeapRegion* hr);
112
113
// Allocate blocks of memory during mutator time.
114
115
// Attempt allocation in the current alloc region.
116
inline HeapWord* attempt_allocation(size_t min_word_size,
117
size_t desired_word_size,
118
size_t* actual_word_size);
119
120
// Attempt allocation, retiring the current region and allocating a new one. It is
121
// assumed that attempt_allocation() has been tried and failed already first.
122
inline HeapWord* attempt_allocation_using_new_region(size_t 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(size_t word_size);
127
128
inline HeapWord* attempt_allocation_force(size_t word_size);
129
130
size_t unsafe_max_tlab_alloc();
131
size_t used_in_alloc_regions();
132
133
// Allocate blocks of memory during garbage collection. Will ensure an
134
// allocation region, either by picking one or expanding the
135
// heap, and then allocate a block of the given size. The block
136
// may not be a humongous - it must fit into a single heap region.
137
HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,
138
size_t word_size,
139
uint node_index);
140
141
HeapWord* par_allocate_during_gc(G1HeapRegionAttr dest,
142
size_t min_word_size,
143
size_t desired_word_size,
144
size_t* actual_word_size,
145
uint node_index);
146
};
147
148
// Manages the PLABs used during garbage collection. Interface for allocation from PLABs.
149
// Needs to handle multiple contexts, extra alignment in any "survivor" area and some
150
// statistics.
151
class G1PLABAllocator : public CHeapObj<mtGC> {
152
friend class G1ParScanThreadState;
153
private:
154
typedef G1HeapRegionAttr::region_type_t region_type_t;
155
156
G1CollectedHeap* _g1h;
157
G1Allocator* _allocator;
158
159
PLAB** _alloc_buffers[G1HeapRegionAttr::Num];
160
161
// Number of words allocated directly (not counting PLAB allocation).
162
size_t _direct_allocated[G1HeapRegionAttr::Num];
163
164
void flush_and_retire_stats();
165
inline PLAB* alloc_buffer(G1HeapRegionAttr dest, uint node_index) const;
166
inline PLAB* alloc_buffer(region_type_t dest, uint node_index) const;
167
168
// Returns the number of allocation buffers for the given dest.
169
// There is only 1 buffer for Old while Young may have multiple buffers depending on
170
// active NUMA nodes.
171
inline uint alloc_buffers_length(region_type_t dest) const;
172
173
bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;
174
public:
175
G1PLABAllocator(G1Allocator* allocator);
176
~G1PLABAllocator();
177
178
size_t waste() const;
179
size_t undo_waste() const;
180
181
// Allocate word_sz words in dest, either directly into the regions or by
182
// allocating a new PLAB. Returns the address of the allocated memory, NULL if
183
// not successful. Plab_refill_failed indicates whether an attempt to refill the
184
// PLAB failed or not.
185
HeapWord* allocate_direct_or_new_plab(G1HeapRegionAttr dest,
186
size_t word_sz,
187
bool* plab_refill_failed,
188
uint node_index);
189
190
// Allocate word_sz words in the PLAB of dest. Returns the address of the
191
// allocated memory, NULL if not successful.
192
inline HeapWord* plab_allocate(G1HeapRegionAttr dest,
193
size_t word_sz,
194
uint node_index);
195
196
inline HeapWord* allocate(G1HeapRegionAttr dest,
197
size_t word_sz,
198
bool* refill_failed,
199
uint node_index);
200
201
void undo_allocation(G1HeapRegionAttr dest, HeapWord* obj, size_t word_sz, uint node_index);
202
};
203
204
// G1ArchiveAllocator is used to allocate memory in archive
205
// regions. Such regions are not scavenged nor compacted by GC.
206
// There are two types of archive regions, which are
207
// differ in the kind of references allowed for the contained objects:
208
//
209
// - 'Closed' archive region contain no references outside of other
210
// closed archive regions. The region is immutable by GC. GC does
211
// not mark object header in 'closed' archive region.
212
// - An 'open' archive region allow references to any other regions,
213
// including closed archive, open archive and other java heap regions.
214
// GC can adjust pointers and mark object header in 'open' archive region.
215
class G1ArchiveAllocator : public CHeapObj<mtGC> {
216
protected:
217
bool _open; // Indicate if the region is 'open' archive.
218
G1CollectedHeap* _g1h;
219
220
// The current allocation region
221
HeapRegion* _allocation_region;
222
223
// Regions allocated for the current archive range.
224
GrowableArray<HeapRegion*> _allocated_regions;
225
226
// The number of bytes used in the current range.
227
size_t _summary_bytes_used;
228
229
// Current allocation window within the current region.
230
HeapWord* _bottom;
231
HeapWord* _top;
232
HeapWord* _max;
233
234
// Allocate a new region for this archive allocator.
235
// Allocation is from the top of the reserved heap downward.
236
bool alloc_new_region();
237
238
public:
239
G1ArchiveAllocator(G1CollectedHeap* g1h, bool open) :
240
_open(open),
241
_g1h(g1h),
242
_allocation_region(NULL),
243
_allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions,
244
ResourceObj::C_HEAP),
245
2), mtGC),
246
_summary_bytes_used(0),
247
_bottom(NULL),
248
_top(NULL),
249
_max(NULL) { }
250
251
virtual ~G1ArchiveAllocator() {
252
assert(_allocation_region == NULL, "_allocation_region not NULL");
253
}
254
255
static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h, bool open);
256
257
// Allocate memory for an individual object.
258
HeapWord* archive_mem_allocate(size_t word_size);
259
260
// Return the memory ranges used in the current archive, after
261
// aligning to the requested alignment.
262
void complete_archive(GrowableArray<MemRegion>* ranges,
263
size_t end_alignment_in_bytes);
264
265
// The number of bytes allocated by this allocator.
266
size_t used() {
267
return _summary_bytes_used;
268
}
269
270
// Clear the count of bytes allocated in prior G1 regions. This
271
// must be done when recalculate_use is used to reset the counter
272
// for the generic allocator, since it counts bytes in all G1
273
// regions, including those still associated with this allocator.
274
void clear_used() {
275
_summary_bytes_used = 0;
276
}
277
};
278
279
#endif // SHARE_GC_G1_G1ALLOCATOR_HPP
280
281