comparison src/share/vm/gc_implementation/g1/g1Allocator.hpp @ 20404:227a9e5e4b4a

8057536: Refactor G1 to allow context specific allocations Summary: Splitting out a g1 allocator class to simply specialized allocators which can associate each allocation with a given context. Reviewed-by: mgerdin, brutisso
author sjohanss
date Fri, 05 Sep 2014 09:49:19 +0200
parents
children d35872270666
comparison
equal deleted inserted replaced
20403:8ec8971f511a 20404:227a9e5e4b4a
1 /*
2 * Copyright (c) 2014, 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_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP
26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP
27
28 #include "gc_implementation/g1/g1AllocationContext.hpp"
29 #include "gc_implementation/g1/g1AllocRegion.hpp"
30 #include "gc_implementation/shared/parGCAllocBuffer.hpp"
31
32 enum GCAllocPurpose {
33 GCAllocForTenured,
34 GCAllocForSurvived,
35 GCAllocPurposeCount
36 };
37
38 // Base class for G1 allocators.
39 class G1Allocator : public CHeapObj<mtGC> {
40 friend class VMStructs;
41 protected:
42 G1CollectedHeap* _g1h;
43
44 // Outside of GC pauses, the number of bytes used in all regions other
45 // than the current allocation region.
46 size_t _summary_bytes_used;
47
48 public:
49 G1Allocator(G1CollectedHeap* heap) :
50 _g1h(heap), _summary_bytes_used(0) { }
51
52 static G1Allocator* create_allocator(G1CollectedHeap* g1h);
53
54 virtual void init_mutator_alloc_region() = 0;
55 virtual void release_mutator_alloc_region() = 0;
56
57 virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0;
58 virtual void release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info) = 0;
59 virtual void abandon_gc_alloc_regions() = 0;
60
61 virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) = 0;
62 virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) = 0;
63 virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) = 0;
64 virtual size_t used() = 0;
65 virtual bool is_retained_old_region(HeapRegion* hr) = 0;
66
67 void reuse_retained_old_region(EvacuationInfo& evacuation_info,
68 OldGCAllocRegion* old,
69 HeapRegion** retained);
70
71 size_t used_unlocked() const {
72 return _summary_bytes_used;
73 }
74
75 void increase_used(size_t bytes) {
76 _summary_bytes_used += bytes;
77 }
78
79 void decrease_used(size_t bytes) {
80 assert(_summary_bytes_used >= bytes,
81 err_msg("invariant: _summary_bytes_used: "SIZE_FORMAT" should be >= bytes: "SIZE_FORMAT,
82 _summary_bytes_used, bytes));
83 _summary_bytes_used -= bytes;
84 }
85
86 void set_used(size_t bytes) {
87 _summary_bytes_used = bytes;
88 }
89 };
90
91 // The default allocator for G1.
92 class G1DefaultAllocator : public G1Allocator {
93 protected:
94 // Alloc region used to satisfy mutator allocation requests.
95 MutatorAllocRegion _mutator_alloc_region;
96
97 // Alloc region used to satisfy allocation requests by the GC for
98 // survivor objects.
99 SurvivorGCAllocRegion _survivor_gc_alloc_region;
100
101 // Alloc region used to satisfy allocation requests by the GC for
102 // old objects.
103 OldGCAllocRegion _old_gc_alloc_region;
104
105 HeapRegion* _retained_old_gc_alloc_region;
106 public:
107 G1DefaultAllocator(G1CollectedHeap* heap) : G1Allocator(heap), _retained_old_gc_alloc_region(NULL) { }
108
109 virtual void init_mutator_alloc_region();
110 virtual void release_mutator_alloc_region();
111
112 virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info);
113 virtual void release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info);
114 virtual void abandon_gc_alloc_regions();
115
116 virtual bool is_retained_old_region(HeapRegion* hr) {
117 return _retained_old_gc_alloc_region == hr;
118 }
119
120 virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) {
121 return &_mutator_alloc_region;
122 }
123
124 virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) {
125 return &_survivor_gc_alloc_region;
126 }
127
128 virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) {
129 return &_old_gc_alloc_region;
130 }
131
132 virtual size_t used() {
133 assert(Heap_lock->owner() != NULL,
134 "Should be owned on this thread's behalf.");
135 size_t result = _summary_bytes_used;
136
137 // Read only once in case it is set to NULL concurrently
138 HeapRegion* hr = mutator_alloc_region(AllocationContext::current())->get();
139 if (hr != NULL) {
140 result += hr->used();
141 }
142 return result;
143 }
144 };
145
146 class G1ParGCAllocBuffer: public ParGCAllocBuffer {
147 private:
148 bool _retired;
149
150 public:
151 G1ParGCAllocBuffer(size_t gclab_word_size);
152 virtual ~G1ParGCAllocBuffer() {
153 guarantee(_retired, "Allocation buffer has not been retired");
154 }
155
156 virtual void set_buf(HeapWord* buf) {
157 ParGCAllocBuffer::set_buf(buf);
158 _retired = false;
159 }
160
161 virtual void retire(bool end_of_gc, bool retain) {
162 if (_retired) {
163 return;
164 }
165 ParGCAllocBuffer::retire(end_of_gc, retain);
166 _retired = true;
167 }
168 };
169
170 class G1ParGCAllocator : public CHeapObj<mtGC> {
171 friend class G1ParScanThreadState;
172 protected:
173 G1CollectedHeap* _g1h;
174
175 size_t _alloc_buffer_waste;
176 size_t _undo_waste;
177
178 void add_to_alloc_buffer_waste(size_t waste) { _alloc_buffer_waste += waste; }
179 void add_to_undo_waste(size_t waste) { _undo_waste += waste; }
180
181 HeapWord* allocate_slow(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context);
182
183 virtual void retire_alloc_buffers() = 0;
184 virtual G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose, AllocationContext_t context) = 0;
185
186 public:
187 G1ParGCAllocator(G1CollectedHeap* g1h) :
188 _g1h(g1h), _alloc_buffer_waste(0), _undo_waste(0) {
189 }
190
191 static G1ParGCAllocator* create_allocator(G1CollectedHeap* g1h);
192
193 size_t alloc_buffer_waste() { return _alloc_buffer_waste; }
194 size_t undo_waste() {return _undo_waste; }
195
196 HeapWord* allocate(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context) {
197 HeapWord* obj = NULL;
198 if (purpose == GCAllocForSurvived) {
199 obj = alloc_buffer(purpose, context)->allocate_aligned(word_sz, SurvivorAlignmentInBytes);
200 } else {
201 obj = alloc_buffer(purpose, context)->allocate(word_sz);
202 }
203 if (obj != NULL) {
204 return obj;
205 }
206 return allocate_slow(purpose, word_sz, context);
207 }
208
209 void undo_allocation(GCAllocPurpose purpose, HeapWord* obj, size_t word_sz, AllocationContext_t context) {
210 if (alloc_buffer(purpose, context)->contains(obj)) {
211 assert(alloc_buffer(purpose, context)->contains(obj + word_sz - 1),
212 "should contain whole object");
213 alloc_buffer(purpose, context)->undo_allocation(obj, word_sz);
214 } else {
215 CollectedHeap::fill_with_object(obj, word_sz);
216 add_to_undo_waste(word_sz);
217 }
218 }
219 };
220
221 class G1DefaultParGCAllocator : public G1ParGCAllocator {
222 G1ParGCAllocBuffer _surviving_alloc_buffer;
223 G1ParGCAllocBuffer _tenured_alloc_buffer;
224 G1ParGCAllocBuffer* _alloc_buffers[GCAllocPurposeCount];
225
226 public:
227 G1DefaultParGCAllocator(G1CollectedHeap* g1h);
228
229 virtual G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose, AllocationContext_t context) {
230 return _alloc_buffers[purpose];
231 }
232
233 virtual void retire_alloc_buffers() ;
234 };
235
236 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP