g1Allocator.hpp revision 9056:dc9930a04ab0
1/*
2 * Copyright (c) 2014, 2015, 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_G1_G1ALLOCATOR_HPP
26#define SHARE_VM_GC_G1_G1ALLOCATOR_HPP
27
28#include "gc/g1/g1AllocRegion.hpp"
29#include "gc/g1/g1AllocationContext.hpp"
30#include "gc/g1/g1InCSetState.hpp"
31#include "gc/shared/collectedHeap.hpp"
32#include "gc/shared/plab.hpp"
33
34class EvacuationInfo;
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.
39class G1Allocator : public CHeapObj<mtGC> {
40  friend class VMStructs;
41private:
42  bool _survivor_is_full;
43  bool _old_is_full;
44protected:
45  G1CollectedHeap* _g1h;
46
47  virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) = 0;
48
49  virtual bool survivor_is_full(AllocationContext_t context) const;
50  virtual bool old_is_full(AllocationContext_t context) const;
51
52  virtual void set_survivor_full(AllocationContext_t context);
53  virtual void set_old_full(AllocationContext_t context);
54
55  // Accessors to the allocation regions.
56  virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) = 0;
57  virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) = 0;
58
59  // Allocation attempt during GC for a survivor object / PLAB.
60  inline HeapWord* survivor_attempt_allocation(size_t min_word_size,
61                                               size_t desired_word_size,
62                                               size_t* actual_word_size,
63                                               AllocationContext_t context);
64  // Allocation attempt during GC for an old object / PLAB.
65  inline HeapWord* old_attempt_allocation(size_t min_word_size,
66                                          size_t desired_word_size,
67                                          size_t* actual_word_size,
68                                          AllocationContext_t context);
69public:
70  G1Allocator(G1CollectedHeap* heap) : _g1h(heap), _survivor_is_full(false), _old_is_full(false) { }
71  virtual ~G1Allocator() { }
72
73  static G1Allocator* create_allocator(G1CollectedHeap* g1h);
74
75#ifdef ASSERT
76  // Do we currently have an active mutator region to allocate into?
77  bool has_mutator_alloc_region(AllocationContext_t context) { return mutator_alloc_region(context)->get() != NULL; }
78#endif
79  virtual void init_mutator_alloc_region() = 0;
80  virtual void release_mutator_alloc_region() = 0;
81
82  virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info);
83  virtual void release_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0;
84  virtual void abandon_gc_alloc_regions() = 0;
85
86  // Management of retained regions.
87
88  virtual bool is_retained_old_region(HeapRegion* hr) = 0;
89  void reuse_retained_old_region(EvacuationInfo& evacuation_info,
90                                 OldGCAllocRegion* old,
91                                 HeapRegion** retained);
92
93  // Allocate blocks of memory during mutator time.
94
95  inline HeapWord* attempt_allocation(size_t word_size, AllocationContext_t context);
96  inline HeapWord* attempt_allocation_locked(size_t word_size, AllocationContext_t context);
97  inline HeapWord* attempt_allocation_force(size_t word_size, AllocationContext_t context);
98
99  size_t unsafe_max_tlab_alloc(AllocationContext_t context);
100
101  // Allocate blocks of memory during garbage collection. Will ensure an
102  // allocation region, either by picking one or expanding the
103  // heap, and then allocate a block of the given size. The block
104  // may not be a humongous - it must fit into a single heap region.
105  HeapWord* par_allocate_during_gc(InCSetState dest,
106                                   size_t word_size,
107                                   AllocationContext_t context);
108
109  HeapWord* par_allocate_during_gc(InCSetState dest,
110                                   size_t min_word_size,
111                                   size_t desired_word_size,
112                                   size_t* actual_word_size,
113                                   AllocationContext_t context);
114
115  virtual size_t used_in_alloc_regions() = 0;
116};
117
118// The default allocation region manager for G1. Provides a single mutator, survivor
119// and old generation allocation region.
120// Can retain the (single) old generation allocation region across GCs.
121class G1DefaultAllocator : public G1Allocator {
122protected:
123  // Alloc region used to satisfy mutator allocation requests.
124  MutatorAllocRegion _mutator_alloc_region;
125
126  // Alloc region used to satisfy allocation requests by the GC for
127  // survivor objects.
128  SurvivorGCAllocRegion _survivor_gc_alloc_region;
129
130  // Alloc region used to satisfy allocation requests by the GC for
131  // old objects.
132  OldGCAllocRegion _old_gc_alloc_region;
133
134  HeapRegion* _retained_old_gc_alloc_region;
135public:
136  G1DefaultAllocator(G1CollectedHeap* heap);
137
138  virtual void init_mutator_alloc_region();
139  virtual void release_mutator_alloc_region();
140
141  virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info);
142  virtual void release_gc_alloc_regions(EvacuationInfo& evacuation_info);
143  virtual void abandon_gc_alloc_regions();
144
145  virtual bool is_retained_old_region(HeapRegion* hr) {
146    return _retained_old_gc_alloc_region == hr;
147  }
148
149  virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) {
150    return &_mutator_alloc_region;
151  }
152
153  virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) {
154    return &_survivor_gc_alloc_region;
155  }
156
157  virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) {
158    return &_old_gc_alloc_region;
159  }
160
161  virtual size_t used_in_alloc_regions() {
162    assert(Heap_lock->owner() != NULL,
163           "Should be owned on this thread's behalf.");
164    size_t result = 0;
165
166    // Read only once in case it is set to NULL concurrently
167    HeapRegion* hr = mutator_alloc_region(AllocationContext::current())->get();
168    if (hr != NULL) {
169      result += hr->used();
170    }
171    return result;
172  }
173};
174
175class G1PLAB: public PLAB {
176private:
177  bool _retired;
178
179public:
180  G1PLAB(size_t gclab_word_size);
181  virtual ~G1PLAB() {
182    guarantee(_retired, "Allocation buffer has not been retired");
183  }
184
185  // The amount of space in words wasted within the PLAB including
186  // waste due to refills and alignment.
187  size_t wasted() const { return _wasted; }
188
189  virtual void set_buf(HeapWord* buf, size_t word_size) {
190    PLAB::set_buf(buf, word_size);
191    _retired = false;
192  }
193
194  virtual void retire() {
195    if (_retired) {
196      return;
197    }
198    PLAB::retire();
199    _retired = true;
200  }
201
202  virtual void flush_and_retire_stats(PLABStats* stats) {
203    PLAB::flush_and_retire_stats(stats);
204    _retired = true;
205  }
206};
207
208// Manages the PLABs used during garbage collection. Interface for allocation from PLABs.
209// Needs to handle multiple contexts, extra alignment in any "survivor" area and some
210// statistics.
211class G1PLABAllocator : public CHeapObj<mtGC> {
212  friend class G1ParScanThreadState;
213protected:
214  G1CollectedHeap* _g1h;
215  G1Allocator* _allocator;
216
217  // The survivor alignment in effect in bytes.
218  // == 0 : don't align survivors
219  // != 0 : align survivors to that alignment
220  // These values were chosen to favor the non-alignment case since some
221  // architectures have a special compare against zero instructions.
222  const uint _survivor_alignment_bytes;
223
224  // Number of words allocated directly (not counting PLAB allocation).
225  size_t _direct_allocated[InCSetState::Num];
226
227  virtual void flush_and_retire_stats() = 0;
228  virtual G1PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) = 0;
229
230  // Calculate the survivor space object alignment in bytes. Returns that or 0 if
231  // there are no restrictions on survivor alignment.
232  static uint calc_survivor_alignment_bytes() {
233    assert(SurvivorAlignmentInBytes >= ObjectAlignmentInBytes, "sanity");
234    if (SurvivorAlignmentInBytes == ObjectAlignmentInBytes) {
235      // No need to align objects in the survivors differently, return 0
236      // which means "survivor alignment is not used".
237      return 0;
238    } else {
239      assert(SurvivorAlignmentInBytes > 0, "sanity");
240      return SurvivorAlignmentInBytes;
241    }
242  }
243
244  HeapWord* allocate_new_plab(InCSetState dest,
245                              size_t word_sz,
246                              AllocationContext_t context);
247
248  bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const;
249public:
250  G1PLABAllocator(G1Allocator* allocator);
251  virtual ~G1PLABAllocator() { }
252
253  static G1PLABAllocator* create_allocator(G1Allocator* allocator);
254
255  virtual void waste(size_t& wasted, size_t& undo_wasted) = 0;
256
257  // Allocate word_sz words in dest, either directly into the regions or by
258  // allocating a new PLAB. Returns the address of the allocated memory, NULL if
259  // not successful. Plab_refill_failed indicates whether an attempt to refill the
260  // PLAB failed or not.
261  HeapWord* allocate_direct_or_new_plab(InCSetState dest,
262                                        size_t word_sz,
263                                        AllocationContext_t context,
264                                        bool* plab_refill_failed);
265
266  // Allocate word_sz words in the PLAB of dest.  Returns the address of the
267  // allocated memory, NULL if not successful.
268  inline HeapWord* plab_allocate(InCSetState dest,
269                                 size_t word_sz,
270                                 AllocationContext_t context);
271
272  HeapWord* allocate(InCSetState dest,
273                     size_t word_sz,
274                     AllocationContext_t context,
275                     bool* refill_failed) {
276    HeapWord* const obj = plab_allocate(dest, word_sz, context);
277    if (obj != NULL) {
278      return obj;
279    }
280    return allocate_direct_or_new_plab(dest, word_sz, context, refill_failed);
281  }
282
283  void undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz, AllocationContext_t context);
284};
285
286// The default PLAB allocator for G1. Keeps the current (single) PLAB for survivor
287// and old generation allocation.
288class G1DefaultPLABAllocator : public G1PLABAllocator {
289  G1PLAB  _surviving_alloc_buffer;
290  G1PLAB  _tenured_alloc_buffer;
291  G1PLAB* _alloc_buffers[InCSetState::Num];
292
293public:
294  G1DefaultPLABAllocator(G1Allocator* _allocator);
295
296  virtual G1PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) {
297    assert(dest.is_valid(),
298           "Allocation buffer index out-of-bounds: " CSETSTATE_FORMAT, dest.value());
299    assert(_alloc_buffers[dest.value()] != NULL,
300           "Allocation buffer is NULL: " CSETSTATE_FORMAT, dest.value());
301    return _alloc_buffers[dest.value()];
302  }
303
304  virtual void flush_and_retire_stats();
305
306  virtual void waste(size_t& wasted, size_t& undo_wasted);
307};
308
309// G1ArchiveAllocator is used to allocate memory in archive
310// regions. Such regions are not modifiable by GC, being neither
311// scavenged nor compacted, or even marked in the object header.
312// They can contain no pointers to non-archive heap regions,
313class G1ArchiveAllocator : public CHeapObj<mtGC> {
314
315protected:
316  G1CollectedHeap* _g1h;
317
318  // The current allocation region
319  HeapRegion* _allocation_region;
320
321  // Regions allocated for the current archive range.
322  GrowableArray<HeapRegion*> _allocated_regions;
323
324  // The number of bytes used in the current range.
325  size_t _summary_bytes_used;
326
327  // Current allocation window within the current region.
328  HeapWord* _bottom;
329  HeapWord* _top;
330  HeapWord* _max;
331
332  // Allocate a new region for this archive allocator.
333  // Allocation is from the top of the reserved heap downward.
334  bool alloc_new_region();
335
336public:
337  G1ArchiveAllocator(G1CollectedHeap* g1h) :
338    _g1h(g1h),
339    _allocation_region(NULL),
340    _allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions,
341                                                         ResourceObj::C_HEAP),
342                        2), true /* C_Heap */),
343    _summary_bytes_used(0),
344    _bottom(NULL),
345    _top(NULL),
346    _max(NULL) { }
347
348  virtual ~G1ArchiveAllocator() {
349    assert(_allocation_region == NULL, "_allocation_region not NULL");
350  }
351
352  static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h);
353
354  // Allocate memory for an individual object.
355  HeapWord* archive_mem_allocate(size_t word_size);
356
357  // Return the memory ranges used in the current archive, after
358  // aligning to the requested alignment.
359  void complete_archive(GrowableArray<MemRegion>* ranges,
360                        size_t end_alignment_in_bytes);
361
362  // The number of bytes allocated by this allocator.
363  size_t used() {
364    return _summary_bytes_used;
365  }
366
367  // Clear the count of bytes allocated in prior G1 regions. This
368  // must be done when recalculate_use is used to reset the counter
369  // for the generic allocator, since it counts bytes in all G1
370  // regions, including those still associated with this allocator.
371  void clear_used() {
372    _summary_bytes_used = 0;
373  }
374
375};
376
377#endif // SHARE_VM_GC_G1_G1ALLOCATOR_HPP
378