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