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 34 class EvacuationInfo; 35 36 // Base class for G1 allocators. 37 class G1Allocator : public CHeapObj<mtGC> { 38 friend class VMStructs; 39 protected: 40 G1CollectedHeap* _g1h; 41 42 public: 43 G1Allocator(G1CollectedHeap* heap) : _g1h(heap) { } 44 45 static G1Allocator* create_allocator(G1CollectedHeap* g1h); 46 47 virtual void init_mutator_alloc_region() = 0; 48 virtual void release_mutator_alloc_region() = 0; 49 50 virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0; 51 virtual void release_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0; 52 virtual void abandon_gc_alloc_regions() = 0; 53 54 virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) = 0; 55 virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) = 0; 56 virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) = 0; 57 virtual size_t used_in_alloc_regions() = 0; 58 virtual bool is_retained_old_region(HeapRegion* hr) = 0; 59 60 void reuse_retained_old_region(EvacuationInfo& evacuation_info, 61 OldGCAllocRegion* old, 62 HeapRegion** retained); 63 64 virtual HeapRegion* new_heap_region(uint hrs_index, 65 G1BlockOffsetSharedArray* sharedOffsetArray, 66 MemRegion mr) { 67 return new HeapRegion(hrs_index, sharedOffsetArray, mr); 68 } 69 }; 70 71 // The default allocator for G1. 72 class G1DefaultAllocator : public G1Allocator { 73 protected: 74 // Alloc region used to satisfy mutator allocation requests. 75 MutatorAllocRegion _mutator_alloc_region; 76 77 // Alloc region used to satisfy allocation requests by the GC for 78 // survivor objects. 79 SurvivorGCAllocRegion _survivor_gc_alloc_region; 80 81 // Alloc region used to satisfy allocation requests by the GC for 82 // old objects. 83 OldGCAllocRegion _old_gc_alloc_region; 84 85 HeapRegion* _retained_old_gc_alloc_region; 86 public: 87 G1DefaultAllocator(G1CollectedHeap* heap) : G1Allocator(heap), _retained_old_gc_alloc_region(NULL) { } 88 89 virtual void init_mutator_alloc_region(); 90 virtual void release_mutator_alloc_region(); 91 92 virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info); 93 virtual void release_gc_alloc_regions(EvacuationInfo& evacuation_info); 94 virtual void abandon_gc_alloc_regions(); 95 96 virtual bool is_retained_old_region(HeapRegion* hr) { 97 return _retained_old_gc_alloc_region == hr; 98 } 99 100 virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) { 101 return &_mutator_alloc_region; 102 } 103 104 virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) { 105 return &_survivor_gc_alloc_region; 106 } 107 108 virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) { 109 return &_old_gc_alloc_region; 110 } 111 112 virtual size_t used_in_alloc_regions() { 113 assert(Heap_lock->owner() != NULL, 114 "Should be owned on this thread's behalf."); 115 size_t result = 0; 116 117 // Read only once in case it is set to NULL concurrently 118 HeapRegion* hr = mutator_alloc_region(AllocationContext::current())->get(); 119 if (hr != NULL) { 120 result += hr->used(); 121 } 122 return result; 123 } 124 }; 125 126 class G1PLAB: public PLAB { 127 private: 128 bool _retired; 129 130 public: 131 G1PLAB(size_t gclab_word_size); 132 virtual ~G1PLAB() { 133 guarantee(_retired, "Allocation buffer has not been retired"); 134 } 135 136 virtual void set_buf(HeapWord* buf) { 137 PLAB::set_buf(buf); 138 _retired = false; 139 } 140 141 virtual void retire() { 142 if (_retired) { 143 return; 144 } 145 PLAB::retire(); 146 _retired = true; 147 } 148 149 virtual void flush_and_retire_stats(PLABStats* stats) { 150 PLAB::flush_and_retire_stats(stats); 151 _retired = true; 152 } 153 }; 154 155 class G1ParGCAllocator : public CHeapObj<mtGC> { 156 friend class G1ParScanThreadState; 157 protected: 158 G1CollectedHeap* _g1h; 159 160 // The survivor alignment in effect in bytes. 161 // == 0 : don't align survivors 162 // != 0 : align survivors to that alignment 163 // These values were chosen to favor the non-alignment case since some 164 // architectures have a special compare against zero instructions. 165 const uint _survivor_alignment_bytes; 166 167 virtual void retire_alloc_buffers() = 0; 168 virtual G1PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) = 0; 169 170 // Calculate the survivor space object alignment in bytes. Returns that or 0 if 171 // there are no restrictions on survivor alignment. 172 static uint calc_survivor_alignment_bytes() { 173 assert(SurvivorAlignmentInBytes >= ObjectAlignmentInBytes, "sanity"); 174 if (SurvivorAlignmentInBytes == ObjectAlignmentInBytes) { 175 // No need to align objects in the survivors differently, return 0 176 // which means "survivor alignment is not used". 177 return 0; 178 } else { 179 assert(SurvivorAlignmentInBytes > 0, "sanity"); 180 return SurvivorAlignmentInBytes; 181 } 182 } 183 184 public: 185 G1ParGCAllocator(G1CollectedHeap* g1h) : 186 _g1h(g1h), _survivor_alignment_bytes(calc_survivor_alignment_bytes()) { } 187 virtual ~G1ParGCAllocator() { } 188 189 static G1ParGCAllocator* create_allocator(G1CollectedHeap* g1h); 190 191 virtual void waste(size_t& wasted, size_t& undo_wasted) = 0; 192 193 // Allocate word_sz words in dest, either directly into the regions or by 194 // allocating a new PLAB. Returns the address of the allocated memory, NULL if 195 // not successful. 196 HeapWord* allocate_direct_or_new_plab(InCSetState dest, 197 size_t word_sz, 198 AllocationContext_t context); 199 200 // Allocate word_sz words in the PLAB of dest. Returns the address of the 201 // allocated memory, NULL if not successful. 202 HeapWord* plab_allocate(InCSetState dest, 203 size_t word_sz, 204 AllocationContext_t context) { 205 G1PLAB* buffer = alloc_buffer(dest, context); 206 if (_survivor_alignment_bytes == 0 || !dest.is_young()) { 207 return buffer->allocate(word_sz); 208 } else { 209 return buffer->allocate_aligned(word_sz, _survivor_alignment_bytes); 210 } 211 } 212 213 HeapWord* allocate(InCSetState dest, size_t word_sz, 214 AllocationContext_t context) { 215 HeapWord* const obj = plab_allocate(dest, word_sz, context); 216 if (obj != NULL) { 217 return obj; 218 } 219 return allocate_direct_or_new_plab(dest, word_sz, context); 220 } 221 222 void undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz, AllocationContext_t context) { 223 alloc_buffer(dest, context)->undo_allocation(obj, word_sz); 224 } 225 }; 226 227 class G1DefaultParGCAllocator : public G1ParGCAllocator { 228 G1PLAB _surviving_alloc_buffer; 229 G1PLAB _tenured_alloc_buffer; 230 G1PLAB* _alloc_buffers[InCSetState::Num]; 231 232 public: 233 G1DefaultParGCAllocator(G1CollectedHeap* g1h); 234 235 virtual G1PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) { 236 assert(dest.is_valid(), 237 err_msg("Allocation buffer index out-of-bounds: " CSETSTATE_FORMAT, dest.value())); 238 assert(_alloc_buffers[dest.value()] != NULL, 239 err_msg("Allocation buffer is NULL: " CSETSTATE_FORMAT, dest.value())); 240 return _alloc_buffers[dest.value()]; 241 } 242 243 virtual void retire_alloc_buffers(); 244 245 virtual void waste(size_t& wasted, size_t& undo_wasted); 246 }; 247 248 // G1ArchiveAllocator is used to allocate memory in archive 249 // regions. Such regions are not modifiable by GC, being neither 250 // scavenged nor compacted, or even marked in the object header. 251 // They can contain no pointers to non-archive heap regions, 252 class G1ArchiveAllocator : public CHeapObj<mtGC> { 253 254 protected: 255 G1CollectedHeap* _g1h; 256 257 // The current allocation region 258 HeapRegion* _allocation_region; 259 260 // Regions allocated for the current archive range. 261 GrowableArray<HeapRegion*> _allocated_regions; 262 263 // The number of bytes used in the current range. 264 size_t _summary_bytes_used; 265 266 // Current allocation window within the current region. 267 HeapWord* _bottom; 268 HeapWord* _top; 269 HeapWord* _max; 270 271 // Allocate a new region for this archive allocator. 272 // Allocation is from the top of the reserved heap downward. 273 bool alloc_new_region(); 274 275 public: 276 G1ArchiveAllocator(G1CollectedHeap* g1h) : 277 _g1h(g1h), 278 _allocation_region(NULL), 279 _allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions, 280 ResourceObj::C_HEAP), 281 2), true /* C_Heap */), 282 _summary_bytes_used(0), 283 _bottom(NULL), 284 _top(NULL), 285 _max(NULL) { } 286 287 virtual ~G1ArchiveAllocator() { 288 assert(_allocation_region == NULL, "_allocation_region not NULL"); 289 } 290 291 static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h); 292 293 // Allocate memory for an individual object. 294 HeapWord* archive_mem_allocate(size_t word_size); 295 296 // Return the memory ranges used in the current archive, after 297 // aligning to the requested alignment. 298 void complete_archive(GrowableArray<MemRegion>* ranges, 299 size_t end_alignment_in_bytes); 300 301 // The number of bytes allocated by this allocator. 302 size_t used() { 303 return _summary_bytes_used; 304 } 305 306 // Clear the count of bytes allocated in prior G1 regions. This 307 // must be done when recalculate_use is used to reset the counter 308 // for the generic allocator, since it counts bytes in all G1 309 // regions, including those still associated with this allocator. 310 void clear_used() { 311 _summary_bytes_used = 0; 312 } 313 314 }; 315 316 #endif // SHARE_VM_GC_G1_G1ALLOCATOR_HPP