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