1 /* 2 * Copyright (c) 2014, 2018, 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 41 private: 42 G1CollectedHeap* _g1h; 43 44 bool _survivor_is_full; 45 bool _old_is_full; 46 47 // Alloc region used to satisfy mutator allocation requests. 48 MutatorAllocRegion _mutator_alloc_region; 49 50 // Alloc region used to satisfy allocation requests by the GC for 51 // survivor objects. 52 SurvivorGCAllocRegion _survivor_gc_alloc_region; 53 54 // Alloc region used to satisfy allocation requests by the GC for 55 // old objects. 56 OldGCAllocRegion _old_gc_alloc_region; 57 58 HeapRegion* _retained_old_gc_alloc_region; 59 60 bool survivor_is_full() const; 61 bool old_is_full() const; 62 63 void set_survivor_full(); 64 void set_old_full(); 65 66 void reuse_retained_old_region(EvacuationInfo& evacuation_info, 67 OldGCAllocRegion* old, 68 HeapRegion** retained); 69 70 // Accessors to the allocation regions. 71 inline MutatorAllocRegion* mutator_alloc_region(); 72 inline SurvivorGCAllocRegion* survivor_gc_alloc_region(); 73 inline OldGCAllocRegion* old_gc_alloc_region(); 74 75 // Allocation attempt during GC for a survivor object / PLAB. 76 HeapWord* survivor_attempt_allocation(size_t min_word_size, 77 size_t desired_word_size, 78 size_t* actual_word_size); 79 80 // Allocation attempt during GC for an old object / PLAB. 81 HeapWord* old_attempt_allocation(size_t min_word_size, 82 size_t desired_word_size, 83 size_t* actual_word_size); 84 public: 85 G1Allocator(G1CollectedHeap* heap); 86 87 #ifdef ASSERT 88 // Do we currently have an active mutator region to allocate into? 89 bool has_mutator_alloc_region() { return mutator_alloc_region()->get() != NULL; } 90 #endif 91 92 void init_mutator_alloc_region(); 93 void release_mutator_alloc_region(); 94 95 void init_gc_alloc_regions(EvacuationInfo& evacuation_info); 96 void release_gc_alloc_regions(EvacuationInfo& evacuation_info); 97 void abandon_gc_alloc_regions(); 98 bool is_retained_old_region(HeapRegion* hr); 99 100 // Allocate blocks of memory during mutator time. 101 102 inline HeapWord* attempt_allocation(size_t min_word_size, 103 size_t desired_word_size, 104 size_t* actual_word_size); 105 inline HeapWord* attempt_allocation_locked(size_t word_size); 106 inline HeapWord* attempt_allocation_force(size_t word_size); 107 108 size_t unsafe_max_tlab_alloc(); 109 size_t used_in_alloc_regions(); 110 111 // Allocate blocks of memory during garbage collection. Will ensure an 112 // allocation region, either by picking one or expanding the 113 // heap, and then allocate a block of the given size. The block 114 // may not be a humongous - it must fit into a single heap region. 115 HeapWord* par_allocate_during_gc(InCSetState dest, 116 size_t word_size); 117 118 HeapWord* par_allocate_during_gc(InCSetState dest, 119 size_t min_word_size, 120 size_t desired_word_size, 121 size_t* actual_word_size); 122 }; 123 124 // Manages the PLABs used during garbage collection. Interface for allocation from PLABs. 125 // Needs to handle multiple contexts, extra alignment in any "survivor" area and some 126 // statistics. 127 class G1PLABAllocator : public CHeapObj<mtGC> { 128 friend class G1ParScanThreadState; 129 private: 130 G1CollectedHeap* _g1h; 131 G1Allocator* _allocator; 132 133 PLAB _surviving_alloc_buffer; 134 PLAB _tenured_alloc_buffer; 135 PLAB* _alloc_buffers[InCSetState::Num]; 136 137 // The survivor alignment in effect in bytes. 138 // == 0 : don't align survivors 139 // != 0 : align survivors to that alignment 140 // These values were chosen to favor the non-alignment case since some 141 // architectures have a special compare against zero instructions. 142 const uint _survivor_alignment_bytes; 143 144 // Number of words allocated directly (not counting PLAB allocation). 145 size_t _direct_allocated[InCSetState::Num]; 146 147 void flush_and_retire_stats(); 148 inline PLAB* alloc_buffer(InCSetState dest); 149 150 // Calculate the survivor space object alignment in bytes. Returns that or 0 if 151 // there are no restrictions on survivor alignment. 152 static uint calc_survivor_alignment_bytes(); 153 154 bool may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const; 155 public: 156 G1PLABAllocator(G1Allocator* allocator); 157 158 void waste(size_t& wasted, size_t& undo_wasted); 159 160 // Allocate word_sz words in dest, either directly into the regions or by 161 // allocating a new PLAB. Returns the address of the allocated memory, NULL if 162 // not successful. Plab_refill_failed indicates whether an attempt to refill the 163 // PLAB failed or not. 164 HeapWord* allocate_direct_or_new_plab(InCSetState dest, 165 size_t word_sz, 166 bool* plab_refill_failed); 167 168 // Allocate word_sz words in the PLAB of dest. Returns the address of the 169 // allocated memory, NULL if not successful. 170 inline HeapWord* plab_allocate(InCSetState dest, 171 size_t word_sz); 172 173 inline HeapWord* allocate(InCSetState dest, 174 size_t word_sz, 175 bool* refill_failed); 176 177 void undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz); 178 }; 179 180 // G1ArchiveRegionMap is a boolean array used to mark G1 regions as 181 // archive regions. This allows a quick check for whether an object 182 // should not be marked because it is in an archive region. 183 class G1ArchiveRegionMap : public G1BiasedMappedArray<bool> { 184 protected: 185 bool default_value() const { return false; } 186 }; 187 188 // G1ArchiveAllocator is used to allocate memory in archive 189 // regions. Such regions are not scavenged nor compacted by GC. 190 // There are two types of archive regions, which are 191 // differ in the kind of references allowed for the contained objects: 192 // 193 // - 'Closed' archive region contain no references outside of other 194 // closed archive regions. The region is immutable by GC. GC does 195 // not mark object header in 'closed' archive region. 196 // - An 'open' archive region allow references to any other regions, 197 // including closed archive, open archive and other java heap regions. 198 // GC can adjust pointers and mark object header in 'open' archive region. 199 class G1ArchiveAllocator : public CHeapObj<mtGC> { 200 protected: 201 bool _open; // Indicate if the region is 'open' archive. 202 G1CollectedHeap* _g1h; 203 204 // The current allocation region 205 HeapRegion* _allocation_region; 206 207 // Regions allocated for the current archive range. 208 GrowableArray<HeapRegion*> _allocated_regions; 209 210 // The number of bytes used in the current range. 211 size_t _summary_bytes_used; 212 213 // Current allocation window within the current region. 214 HeapWord* _bottom; 215 HeapWord* _top; 216 HeapWord* _max; 217 218 // Allocate a new region for this archive allocator. 219 // Allocation is from the top of the reserved heap downward. 220 bool alloc_new_region(); 221 222 public: 223 G1ArchiveAllocator(G1CollectedHeap* g1h, bool open) : 224 _g1h(g1h), 225 _allocation_region(NULL), 226 _allocated_regions((ResourceObj::set_allocation_type((address) &_allocated_regions, 227 ResourceObj::C_HEAP), 228 2), true /* C_Heap */), 229 _summary_bytes_used(0), 230 _bottom(NULL), 231 _top(NULL), 232 _max(NULL), 233 _open(open) { } 234 235 virtual ~G1ArchiveAllocator() { 236 assert(_allocation_region == NULL, "_allocation_region not NULL"); 237 } 238 239 static G1ArchiveAllocator* create_allocator(G1CollectedHeap* g1h, bool open); 240 241 // Allocate memory for an individual object. 242 HeapWord* archive_mem_allocate(size_t word_size); 243 244 // Return the memory ranges used in the current archive, after 245 // aligning to the requested alignment. 246 void complete_archive(GrowableArray<MemRegion>* ranges, 247 size_t end_alignment_in_bytes); 248 249 // The number of bytes allocated by this allocator. 250 size_t used() { 251 return _summary_bytes_used; 252 } 253 254 // Clear the count of bytes allocated in prior G1 regions. This 255 // must be done when recalculate_use is used to reset the counter 256 // for the generic allocator, since it counts bytes in all G1 257 // regions, including those still associated with this allocator. 258 void clear_used() { 259 _summary_bytes_used = 0; 260 } 261 262 // Create the _archive_region_map which is used to identify archive objects. 263 static inline void enable_archive_object_check(); 264 265 // Set the regions containing the specified address range as archive/non-archive. 266 static inline void set_range_archive(MemRegion range, bool open); 267 268 // Check if the object is in closed archive 269 static inline bool is_closed_archive_object(oop object); 270 // Check if the object is in open archive 271 static inline bool is_open_archive_object(oop object); 272 // Check if the object is either in closed archive or open archive 273 static inline bool is_archive_object(oop object); 274 275 private: 276 static bool _archive_check_enabled; 277 static G1ArchiveRegionMap _closed_archive_region_map; 278 static G1ArchiveRegionMap _open_archive_region_map; 279 280 // Check if an object is in a closed archive region using the _closed_archive_region_map. 281 static inline bool in_closed_archive_range(oop object); 282 // Check if an object is in open archive region using the _open_archive_region_map. 283 static inline bool in_open_archive_range(oop object); 284 285 // Check if archive object checking is enabled, to avoid calling in_open/closed_archive_range 286 // unnecessarily. 287 static inline bool archive_check_enabled(); 288 }; 289 290 #endif // SHARE_VM_GC_G1_G1ALLOCATOR_HPP