1 /* 2 * Copyright (c) 2011, 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_G1ALLOCREGION_HPP 26 #define SHARE_VM_GC_G1_G1ALLOCREGION_HPP 27 28 #include "gc/g1/heapRegion.hpp" 29 #include "gc/g1/g1EvacStats.hpp" 30 #include "gc/g1/g1InCSetState.hpp" 31 32 class G1CollectedHeap; 33 34 // 0 -> no tracing, 1 -> basic tracing, 2 -> basic + allocation tracing 35 #define G1_ALLOC_REGION_TRACING 0 36 37 #define G1_ALLOC_REGION_MSG(message) \ 38 "[%s] %s c: %u b: %s r: " PTR_FORMAT " u: " SIZE_FORMAT, \ 39 _name, message, _count, BOOL_TO_STR(_bot_updates), \ 40 p2i(_alloc_region), _used_bytes_before 41 42 // A class that holds a region that is active in satisfying allocation 43 // requests, potentially issued in parallel. When the active region is 44 // full it will be retired and replaced with a new one. The 45 // implementation assumes that fast-path allocations will be lock-free 46 // and a lock will need to be taken when the active region needs to be 47 // replaced. 48 49 class G1AllocRegion VALUE_OBJ_CLASS_SPEC { 50 friend class ar_ext_msg; 51 52 private: 53 // The active allocating region we are currently allocating out 54 // of. The invariant is that if this object is initialized (i.e., 55 // init() has been called and release() has not) then _alloc_region 56 // is either an active allocating region or the dummy region (i.e., 57 // it can never be NULL) and this object can be used to satisfy 58 // allocation requests. If this object is not initialized 59 // (i.e. init() has not been called or release() has been called) 60 // then _alloc_region is NULL and this object should not be used to 61 // satisfy allocation requests (it was done this way to force the 62 // correct use of init() and release()). 63 HeapRegion* volatile _alloc_region; 64 65 // Allocation context associated with this alloc region. 66 AllocationContext_t _allocation_context; 67 68 // It keeps track of the distinct number of regions that are used 69 // for allocation in the active interval of this object, i.e., 70 // between a call to init() and a call to release(). The count 71 // mostly includes regions that are freshly allocated, as well as 72 // the region that is re-used using the set() method. This count can 73 // be used in any heuristics that might want to bound how many 74 // distinct regions this object can used during an active interval. 75 uint _count; 76 77 // When we set up a new active region we save its used bytes in this 78 // field so that, when we retire it, we can calculate how much space 79 // we allocated in it. 80 size_t _used_bytes_before; 81 82 // When true, indicates that allocate calls should do BOT updates. 83 const bool _bot_updates; 84 85 // Useful for debugging and tracing. 86 const char* _name; 87 88 // A dummy region (i.e., it's been allocated specially for this 89 // purpose and it is not part of the heap) that is full (i.e., top() 90 // == end()). When we don't have a valid active region we make 91 // _alloc_region point to this. This allows us to skip checking 92 // whether the _alloc_region is NULL or not. 93 static HeapRegion* _dummy_region; 94 95 // Some of the methods below take a bot_updates parameter. Its value 96 // should be the same as the _bot_updates field. The idea is that 97 // the parameter will be a constant for a particular alloc region 98 // and, given that these methods will be hopefully inlined, the 99 // compiler should compile out the test. 100 101 // Perform a non-MT-safe allocation out of the given region. 102 static inline HeapWord* allocate(HeapRegion* alloc_region, 103 size_t word_size, 104 bool bot_updates); 105 106 // Perform a MT-safe allocation out of the given region. 107 static inline HeapWord* par_allocate(HeapRegion* alloc_region, 108 size_t word_size, 109 bool bot_updates); 110 // Perform a MT-safe allocation out of the given region, with the given 111 // minimum and desired size. Returns the actual size allocated (between 112 // minimum and desired size) in actual_word_size if the allocation has been 113 // successful. 114 static inline HeapWord* par_allocate(HeapRegion* alloc_region, 115 size_t min_word_size, 116 size_t desired_word_size, 117 size_t* actual_word_size, 118 bool bot_updates); 119 120 // Ensure that the region passed as a parameter has been filled up 121 // so that noone else can allocate out of it any more. 122 // Returns the number of bytes that have been wasted by filled up 123 // the space. 124 static size_t fill_up_remaining_space(HeapRegion* alloc_region, 125 bool bot_updates); 126 127 // After a region is allocated by alloc_new_region, this 128 // method is used to set it as the active alloc_region 129 void update_alloc_region(HeapRegion* alloc_region); 130 131 // Allocate a new active region and use it to perform a word_size 132 // allocation. The force parameter will be passed on to 133 // G1CollectedHeap::allocate_new_alloc_region() and tells it to try 134 // to allocate a new region even if the max has been reached. 135 HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force); 136 137 protected: 138 // Retire the active allocating region. If fill_up is true then make 139 // sure that the region is full before we retire it so that no one 140 // else can allocate out of it. 141 // Returns the number of bytes that have been filled up during retire. 142 virtual size_t retire(bool fill_up); 143 144 // For convenience as subclasses use it. 145 static G1CollectedHeap* _g1h; 146 147 virtual HeapRegion* allocate_new_region(size_t word_size, bool force) = 0; 148 virtual void retire_region(HeapRegion* alloc_region, 149 size_t allocated_bytes) = 0; 150 151 G1AllocRegion(const char* name, bool bot_updates); 152 153 public: 154 static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region); 155 156 HeapRegion* get() const { 157 HeapRegion * hr = _alloc_region; 158 // Make sure that the dummy region does not escape this class. 159 return (hr == _dummy_region) ? NULL : hr; 160 } 161 162 void set_allocation_context(AllocationContext_t context) { _allocation_context = context; } 163 AllocationContext_t allocation_context() { return _allocation_context; } 164 165 uint count() { return _count; } 166 167 // The following two are the building blocks for the allocation method. 168 169 // First-level allocation: Should be called without holding a 170 // lock. It will try to allocate lock-free out of the active region, 171 // or return NULL if it was unable to. 172 inline HeapWord* attempt_allocation(size_t word_size, 173 bool bot_updates); 174 // Perform an allocation out of the current allocation region, with the given 175 // minimum and desired size. Returns the actual size allocated (between 176 // minimum and desired size) in actual_word_size if the allocation has been 177 // successful. 178 // Should be called without holding a lock. It will try to allocate lock-free 179 // out of the active region, or return NULL if it was unable to. 180 inline HeapWord* attempt_allocation(size_t min_word_size, 181 size_t desired_word_size, 182 size_t* actual_word_size, 183 bool bot_updates); 184 185 // Second-level allocation: Should be called while holding a 186 // lock. It will try to first allocate lock-free out of the active 187 // region or, if it's unable to, it will try to replace the active 188 // alloc region with a new one. We require that the caller takes the 189 // appropriate lock before calling this so that it is easier to make 190 // it conform to its locking protocol. 191 inline HeapWord* attempt_allocation_locked(size_t word_size, 192 bool bot_updates); 193 // Same as attempt_allocation_locked(size_t, bool), but allowing specification 194 // of minimum word size of the block in min_word_size, and the maximum word 195 // size of the allocation in desired_word_size. The actual size of the block is 196 // returned in actual_word_size. 197 inline HeapWord* attempt_allocation_locked(size_t min_word_size, 198 size_t desired_word_size, 199 size_t* actual_word_size, 200 bool bot_updates); 201 202 // Should be called to allocate a new region even if the max of this 203 // type of regions has been reached. Should only be called if other 204 // allocation attempts have failed and we are not holding a valid 205 // active region. 206 inline HeapWord* attempt_allocation_force(size_t word_size, 207 bool bot_updates); 208 209 // Should be called before we start using this object. 210 void init(); 211 212 // This can be used to set the active region to a specific 213 // region. (Use Example: we try to retain the last old GC alloc 214 // region that we've used during a GC and we can use set() to 215 // re-instate it at the beginning of the next GC.) 216 void set(HeapRegion* alloc_region); 217 218 // Should be called when we want to release the active region which 219 // is returned after it's been retired. 220 virtual HeapRegion* release(); 221 222 #if G1_ALLOC_REGION_TRACING 223 void trace(const char* str, 224 size_t min_word_size = 0, 225 size_t desired_word_size = 0, 226 size_t actual_word_size = 0, 227 HeapWord* result = NULL); 228 #else // G1_ALLOC_REGION_TRACING 229 void trace(const char* str, 230 size_t min_word_size = 0, 231 size_t desired_word_size = 0, 232 size_t actual_word_size = 0, 233 HeapWord* result = NULL) { } 234 #endif // G1_ALLOC_REGION_TRACING 235 }; 236 237 class MutatorAllocRegion : public G1AllocRegion { 238 protected: 239 virtual HeapRegion* allocate_new_region(size_t word_size, bool force); 240 virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes); 241 public: 242 MutatorAllocRegion() 243 : G1AllocRegion("Mutator Alloc Region", false /* bot_updates */) { } 244 }; 245 246 // Common base class for allocation regions used during GC. 247 class G1GCAllocRegion : public G1AllocRegion { 248 protected: 249 G1EvacStats* _stats; 250 InCSetState::in_cset_state_t _purpose; 251 252 virtual HeapRegion* allocate_new_region(size_t word_size, bool force); 253 virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes); 254 255 virtual size_t retire(bool fill_up); 256 public: 257 G1GCAllocRegion(const char* name, bool bot_updates, G1EvacStats* stats, InCSetState::in_cset_state_t purpose) 258 : G1AllocRegion(name, bot_updates), _stats(stats), _purpose(purpose) { 259 assert(stats != NULL, "Must pass non-NULL PLAB statistics"); 260 } 261 }; 262 263 class SurvivorGCAllocRegion : public G1GCAllocRegion { 264 public: 265 SurvivorGCAllocRegion(G1EvacStats* stats) 266 : G1GCAllocRegion("Survivor GC Alloc Region", false /* bot_updates */, stats, InCSetState::Young) { } 267 }; 268 269 class OldGCAllocRegion : public G1GCAllocRegion { 270 public: 271 OldGCAllocRegion(G1EvacStats* stats) 272 : G1GCAllocRegion("Old GC Alloc Region", true /* bot_updates */, stats, InCSetState::Old) { } 273 274 // This specialization of release() makes sure that the last card that has 275 // been allocated into has been completely filled by a dummy object. This 276 // avoids races when remembered set scanning wants to update the BOT of the 277 // last card in the retained old gc alloc region, and allocation threads 278 // allocating into that card at the same time. 279 virtual HeapRegion* release(); 280 }; 281 282 #endif // SHARE_VM_GC_G1_G1ALLOCREGION_HPP