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