1 /* 2 * Copyright (c) 2014, 2017, 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 // Manages the PLABs used during garbage collection. Interface for allocation from PLABs. 182 // Needs to handle multiple contexts, extra alignment in any "survivor" area and some 183 // statistics. 184 class G1PLABAllocator : public CHeapObj<mtGC> { 185 friend class G1ParScanThreadState; 186 protected: 187 G1CollectedHeap* _g1h; 188 G1Allocator* _allocator; 189 190 // The survivor alignment in effect in bytes. 191 // == 0 : don't align survivors 192 // != 0 : align survivors to that alignment 193 // These values were chosen to favor the non-alignment case since some 194 // architectures have a special compare against zero instructions. 195 const uint _survivor_alignment_bytes; 196 197 // Number of words allocated directly (not counting PLAB allocation). 198 size_t _direct_allocated[InCSetState::Num]; 199 200 virtual void flush_and_retire_stats() = 0; 201 virtual PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) = 0; 202 203 // Calculate the survivor space object alignment in bytes. Returns that or 0 if 204 // there are no restrictions on survivor alignment. 205 static uint calc_survivor_alignment_bytes() { 206 assert(SurvivorAlignmentInBytes >= ObjectAlignmentInBytes, "sanity"); 207 if (SurvivorAlignmentInBytes == ObjectAlignmentInBytes) { 208 // No need to align objects in the survivors differently, return 0 209 // which means "survivor alignment is not used". 210 return 0; 211 } else { 212 assert(SurvivorAlignmentInBytes > 0, "sanity"); 213 return SurvivorAlignmentInBytes; 214 } 215 } 216 217 HeapWord* allocate_new_plab(InCSetState dest, 218 size_t word_sz, 219 AllocationContext_t context); 220 221 bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const; 222 public: 223 G1PLABAllocator(G1Allocator* allocator); 224 virtual ~G1PLABAllocator() { } 225 226 static G1PLABAllocator* create_allocator(G1Allocator* allocator); 227 228 virtual void waste(size_t& wasted, size_t& undo_wasted) = 0; 229 230 // Allocate word_sz words in dest, either directly into the regions or by 231 // allocating a new PLAB. Returns the address of the allocated memory, NULL if 232 // not successful. Plab_refill_failed indicates whether an attempt to refill the 233 // PLAB failed or not. 234 HeapWord* allocate_direct_or_new_plab(InCSetState dest, 235 size_t word_sz, 236 AllocationContext_t context, 237 bool* plab_refill_failed); 238 239 // Allocate word_sz words in the PLAB of dest. Returns the address of the 240 // allocated memory, NULL if not successful. 241 inline HeapWord* plab_allocate(InCSetState dest, 242 size_t word_sz, 243 AllocationContext_t context); 244 245 HeapWord* allocate(InCSetState dest, 246 size_t word_sz, 247 AllocationContext_t context, 248 bool* refill_failed) { 249 HeapWord* const obj = plab_allocate(dest, word_sz, context); 250 if (obj != NULL) { 251 return obj; 252 } 253 return allocate_direct_or_new_plab(dest, word_sz, context, refill_failed); 254 } 255 256 void undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz, AllocationContext_t context); 257 }; 258 259 // The default PLAB allocator for G1. Keeps the current (single) PLAB for survivor 260 // and old generation allocation. 261 class G1DefaultPLABAllocator : public G1PLABAllocator { 262 PLAB _surviving_alloc_buffer; 263 PLAB _tenured_alloc_buffer; 264 PLAB* _alloc_buffers[InCSetState::Num]; 265 266 public: 267 G1DefaultPLABAllocator(G1Allocator* _allocator); 268 269 virtual PLAB* alloc_buffer(InCSetState dest, AllocationContext_t context) { 270 assert(dest.is_valid(), 271 "Allocation buffer index out-of-bounds: " CSETSTATE_FORMAT, dest.value()); 272 assert(_alloc_buffers[dest.value()] != NULL, 273 "Allocation buffer is NULL: " CSETSTATE_FORMAT, dest.value()); 274 return _alloc_buffers[dest.value()]; 275 } 276 277 virtual void flush_and_retire_stats(); 278 279 virtual void waste(size_t& wasted, size_t& undo_wasted); 280 }; 281 282 // G1ArchiveRegionMap is a boolean array used to mark G1 regions as 283 // archive regions. This allows a quick check for whether an object 284 // should not be marked because it is in an archive region. 285 class G1ArchiveRegionMap : public G1BiasedMappedArray<bool> { 286 protected: 287 bool default_value() const { return false; } 288 }; 289 290 // G1ArchiveAllocator is used to allocate memory in archive 291 // regions. Such regions are not scavenged nor compacted by GC. 292 // There are two types of archive regions, which are 293 // differ in the kind of references allowed for the contained objects: 294 // 295 // - 'Closed' archive region contain no references outside of other 296 // closed archive regions. The region is immutable by GC. GC does 297 // not mark object header in 'closed' archive region. 298 // - An 'open' archive region allow references to any other regions, 299 // including closed archive, open archive and other java heap regions. 300 // GC can adjust pointers and mark object header in 'open' archive region. 301 class G1ArchiveAllocator : public CHeapObj<mtGC> { 302 protected: 303 bool _open; // Indicate if the region is 'open' archive. 304 G1CollectedHeap* _g1h; 305 306 // The current allocation region 307 HeapRegion* _allocation_region; 308 309 // Regions allocated for the current archive range. 310 GrowableArray<HeapRegion*> _allocated_regions; 311 312 // The number of bytes used in the current range. 313 size_t _summary_bytes_used; 314 315 // Current allocation window within the current region. 316 HeapWord* _bottom; 317 HeapWord* _top; 318 HeapWord* _max; 319 320 // Allocate a new region for this archive allocator. 321 // Allocation is from the top of the reserved heap downward. 322 bool alloc_new_region(); 323 324 public: 325 G1ArchiveAllocator(G1CollectedHeap* g1h, bool open) : 326 _g1h(g1h), 327 _allocation_region(NULL), 328 _allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions, 329 ResourceObj::C_HEAP), 330 2), true /* C_Heap */), 331 _summary_bytes_used(0), 332 _bottom(NULL), 333 _top(NULL), 334 _max(NULL), 335 _open(open) { } 336 337 virtual ~G1ArchiveAllocator() { 338 assert(_allocation_region == NULL, "_allocation_region not NULL"); 339 } 340 341 static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h, bool open); 342 343 // Allocate memory for an individual object. 344 HeapWord* archive_mem_allocate(size_t word_size); 345 346 // Return the memory ranges used in the current archive, after 347 // aligning to the requested alignment. 348 void complete_archive(GrowableArray<MemRegion>* ranges, 349 size_t end_alignment_in_bytes); 350 351 // The number of bytes allocated by this allocator. 352 size_t used() { 353 return _summary_bytes_used; 354 } 355 356 // Clear the count of bytes allocated in prior G1 regions. This 357 // must be done when recalculate_use is used to reset the counter 358 // for the generic allocator, since it counts bytes in all G1 359 // regions, including those still associated with this allocator. 360 void clear_used() { 361 _summary_bytes_used = 0; 362 } 363 364 // Create the _archive_region_map which is used to identify archive objects. 365 static inline void enable_archive_object_check(); 366 367 // Set the regions containing the specified address range as archive/non-archive. 368 static inline void set_range_archive(MemRegion range, bool open); 369 370 // Check if the object is in closed archive 371 static inline bool is_closed_archive_object(oop object); 372 // Check if the object is in open archive 373 static inline bool is_open_archive_object(oop object); 374 // Check if the object is either in closed archive or open archive 375 static inline bool is_archive_object(oop object); 376 377 private: 378 static bool _archive_check_enabled; 379 static G1ArchiveRegionMap _closed_archive_region_map; 380 static G1ArchiveRegionMap _open_archive_region_map; 381 382 // Check if an object is in a closed archive region using the _closed_archive_region_map. 383 static inline bool in_closed_archive_range(oop object); 384 // Check if an object is in open archive region using the _open_archive_region_map. 385 static inline bool in_open_archive_range(oop object); 386 387 // Check if archive object checking is enabled, to avoid calling in_open/closed_archive_range 388 // unnecessarily. 389 static inline bool archive_check_enabled(); 390 }; 391 392 #endif // SHARE_VM_GC_G1_G1ALLOCATOR_HPP