1 /* 2 * Copyright (c) 2011, 2016, 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 #include "precompiled.hpp" 26 #include "gc/g1/g1AllocRegion.inline.hpp" 27 #include "gc/g1/g1EvacStats.inline.hpp" 28 #include "gc/g1/g1CollectedHeap.inline.hpp" 29 #include "logging/log.hpp" 30 #include "logging/logStream.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "runtime/orderAccess.inline.hpp" 33 34 G1CollectedHeap* G1AllocRegion::_g1h = NULL; 35 HeapRegion* G1AllocRegion::_dummy_region = NULL; 36 37 void G1AllocRegion::setup(G1CollectedHeap* g1h, HeapRegion* dummy_region) { 38 assert(_dummy_region == NULL, "should be set once"); 39 assert(dummy_region != NULL, "pre-condition"); 40 assert(dummy_region->free() == 0, "pre-condition"); 41 42 // Make sure that any allocation attempt on this region will fail 43 // and will not trigger any asserts. 44 assert(allocate(dummy_region, 1, false) == NULL, "should fail"); 45 assert(par_allocate(dummy_region, 1, false) == NULL, "should fail"); 46 assert(allocate(dummy_region, 1, true) == NULL, "should fail"); 47 assert(par_allocate(dummy_region, 1, true) == NULL, "should fail"); 48 49 _g1h = g1h; 50 _dummy_region = dummy_region; 51 } 52 53 size_t G1AllocRegion::fill_up_remaining_space(HeapRegion* alloc_region, 54 bool bot_updates) { 55 assert(alloc_region != NULL && alloc_region != _dummy_region, 56 "pre-condition"); 57 size_t result = 0; 58 59 // Other threads might still be trying to allocate using a CAS out 60 // of the region we are trying to retire, as they can do so without 61 // holding the lock. So, we first have to make sure that noone else 62 // can allocate out of it by doing a maximal allocation. Even if our 63 // CAS attempt fails a few times, we'll succeed sooner or later 64 // given that failed CAS attempts mean that the region is getting 65 // closed to being full. 66 size_t free_word_size = alloc_region->free() / HeapWordSize; 67 68 // This is the minimum free chunk we can turn into a dummy 69 // object. If the free space falls below this, then noone can 70 // allocate in this region anyway (all allocation requests will be 71 // of a size larger than this) so we won't have to perform the dummy 72 // allocation. 73 size_t min_word_size_to_fill = CollectedHeap::min_fill_size(); 74 75 while (free_word_size >= min_word_size_to_fill) { 76 HeapWord* dummy = par_allocate(alloc_region, free_word_size, bot_updates); 77 if (dummy != NULL) { 78 // If the allocation was successful we should fill in the space. 79 CollectedHeap::fill_with_object(dummy, free_word_size); 80 alloc_region->set_pre_dummy_top(dummy); 81 result += free_word_size * HeapWordSize; 82 break; 83 } 84 85 free_word_size = alloc_region->free() / HeapWordSize; 86 // It's also possible that someone else beats us to the 87 // allocation and they fill up the region. In that case, we can 88 // just get out of the loop. 89 } 90 result += alloc_region->free(); 91 92 assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill, 93 "post-condition"); 94 return result; 95 } 96 97 size_t G1AllocRegion::retire(bool fill_up) { 98 assert_alloc_region(_alloc_region != NULL, "not initialized properly"); 99 100 size_t result = 0; 101 102 trace("retiring"); 103 HeapRegion* alloc_region = _alloc_region; 104 if (alloc_region != _dummy_region) { 105 // We never have to check whether the active region is empty or not, 106 // and potentially free it if it is, given that it's guaranteed that 107 // it will never be empty. 108 assert_alloc_region(!alloc_region->is_empty(), 109 "the alloc region should never be empty"); 110 111 if (fill_up) { 112 result = fill_up_remaining_space(alloc_region, _bot_updates); 113 } 114 115 assert_alloc_region(alloc_region->used() >= _used_bytes_before, "invariant"); 116 size_t allocated_bytes = alloc_region->used() - _used_bytes_before; 117 retire_region(alloc_region, allocated_bytes); 118 _used_bytes_before = 0; 119 _alloc_region = _dummy_region; 120 } 121 trace("retired"); 122 123 return result; 124 } 125 126 HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size, 127 bool force) { 128 assert_alloc_region(_alloc_region == _dummy_region, "pre-condition"); 129 assert_alloc_region(_used_bytes_before == 0, "pre-condition"); 130 131 trace("attempting region allocation"); 132 HeapRegion* new_alloc_region = allocate_new_region(word_size, force); 133 if (new_alloc_region != NULL) { 134 new_alloc_region->reset_pre_dummy_top(); 135 // Need to do this before the allocation 136 _used_bytes_before = new_alloc_region->used(); 137 HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates); 138 assert_alloc_region(result != NULL, "the allocation should succeeded"); 139 140 OrderAccess::storestore(); 141 // Note that we first perform the allocation and then we store the 142 // region in _alloc_region. This is the reason why an active region 143 // can never be empty. 144 update_alloc_region(new_alloc_region); 145 trace("region allocation successful"); 146 return result; 147 } else { 148 trace("region allocation failed"); 149 return NULL; 150 } 151 ShouldNotReachHere(); 152 } 153 154 void G1AllocRegion::init() { 155 trace("initializing"); 156 assert_alloc_region(_alloc_region == NULL && _used_bytes_before == 0, "pre-condition"); 157 assert_alloc_region(_dummy_region != NULL, "should have been set"); 158 _alloc_region = _dummy_region; 159 _count = 0; 160 trace("initialized"); 161 } 162 163 void G1AllocRegion::set(HeapRegion* alloc_region) { 164 trace("setting"); 165 // We explicitly check that the region is not empty to make sure we 166 // maintain the "the alloc region cannot be empty" invariant. 167 assert_alloc_region(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition"); 168 assert_alloc_region(_alloc_region == _dummy_region && 169 _used_bytes_before == 0 && _count == 0, 170 "pre-condition"); 171 172 _used_bytes_before = alloc_region->used(); 173 _alloc_region = alloc_region; 174 _count += 1; 175 trace("set"); 176 } 177 178 void G1AllocRegion::update_alloc_region(HeapRegion* alloc_region) { 179 trace("update"); 180 // We explicitly check that the region is not empty to make sure we 181 // maintain the "the alloc region cannot be empty" invariant. 182 assert_alloc_region(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition"); 183 184 _alloc_region = alloc_region; 185 _alloc_region->set_allocation_context(allocation_context()); 186 _count += 1; 187 trace("updated"); 188 } 189 190 HeapRegion* G1AllocRegion::release() { 191 trace("releasing"); 192 HeapRegion* alloc_region = _alloc_region; 193 retire(false /* fill_up */); 194 assert_alloc_region(_alloc_region == _dummy_region, "post-condition of retire()"); 195 _alloc_region = NULL; 196 trace("released"); 197 return (alloc_region == _dummy_region) ? NULL : alloc_region; 198 } 199 200 #ifndef PRODUCT 201 void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_word_size, size_t actual_word_size, HeapWord* result) { 202 // All the calls to trace that set either just the size or the size 203 // and the result are considered part of detailed tracing and are 204 // skipped during other tracing. 205 206 Log(gc, alloc, region) log; 207 208 if (!log.is_debug()) { 209 return; 210 } 211 212 bool detailed_info = log.is_trace(); 213 214 if ((actual_word_size == 0 && result == NULL) || detailed_info) { 215 ResourceMark rm; 216 LogStream ls_trace(log.trace()); 217 LogStream ls_debug(log.debug()); 218 outputStream* out = detailed_info ? &ls_trace : &ls_debug; 219 220 out->print("%s: %u ", _name, _count); 221 222 if (_alloc_region == NULL) { 223 out->print("NULL"); 224 } else if (_alloc_region == _dummy_region) { 225 out->print("DUMMY"); 226 } else { 227 out->print(HR_FORMAT, HR_FORMAT_PARAMS(_alloc_region)); 228 } 229 230 out->print(" : %s", str); 231 232 if (detailed_info) { 233 if (result != NULL) { 234 out->print(" min " SIZE_FORMAT " desired " SIZE_FORMAT " actual " SIZE_FORMAT " " PTR_FORMAT, 235 min_word_size, desired_word_size, actual_word_size, p2i(result)); 236 } else if (min_word_size != 0) { 237 out->print(" min " SIZE_FORMAT " desired " SIZE_FORMAT, min_word_size, desired_word_size); 238 } 239 } 240 out->cr(); 241 } 242 } 243 #endif // PRODUCT 244 245 G1AllocRegion::G1AllocRegion(const char* name, 246 bool bot_updates) 247 : _name(name), _bot_updates(bot_updates), 248 _alloc_region(NULL), _count(0), _used_bytes_before(0), 249 _allocation_context(AllocationContext::system()) { } 250 251 252 HeapRegion* MutatorAllocRegion::allocate_new_region(size_t word_size, 253 bool force) { 254 return _g1h->new_mutator_alloc_region(word_size, force); 255 } 256 257 void MutatorAllocRegion::retire_region(HeapRegion* alloc_region, 258 size_t allocated_bytes) { 259 _g1h->retire_mutator_alloc_region(alloc_region, allocated_bytes); 260 } 261 262 HeapRegion* G1GCAllocRegion::allocate_new_region(size_t word_size, 263 bool force) { 264 assert(!force, "not supported for GC alloc regions"); 265 return _g1h->new_gc_alloc_region(word_size, _purpose); 266 } 267 268 void G1GCAllocRegion::retire_region(HeapRegion* alloc_region, 269 size_t allocated_bytes) { 270 _g1h->retire_gc_alloc_region(alloc_region, allocated_bytes, _purpose); 271 } 272 273 size_t G1GCAllocRegion::retire(bool fill_up) { 274 HeapRegion* retired = get(); 275 size_t end_waste = G1AllocRegion::retire(fill_up); 276 // Do not count retirement of the dummy allocation region. 277 if (retired != NULL) { 278 _stats->add_region_end_waste(end_waste / HeapWordSize); 279 } 280 return end_waste; 281 } 282 283 HeapRegion* OldGCAllocRegion::release() { 284 HeapRegion* cur = get(); 285 if (cur != NULL) { 286 // Determine how far we are from the next card boundary. If it is smaller than 287 // the minimum object size we can allocate into, expand into the next card. 288 HeapWord* top = cur->top(); 289 HeapWord* aligned_top = (HeapWord*)align_ptr_up(top, BOTConstants::N_bytes); 290 291 size_t to_allocate_words = pointer_delta(aligned_top, top, HeapWordSize); 292 293 if (to_allocate_words != 0) { 294 // We are not at a card boundary. Fill up, possibly into the next, taking the 295 // end of the region and the minimum object size into account. 296 to_allocate_words = MIN2(pointer_delta(cur->end(), cur->top(), HeapWordSize), 297 MAX2(to_allocate_words, G1CollectedHeap::min_fill_size())); 298 299 // Skip allocation if there is not enough space to allocate even the smallest 300 // possible object. In this case this region will not be retained, so the 301 // original problem cannot occur. 302 if (to_allocate_words >= G1CollectedHeap::min_fill_size()) { 303 HeapWord* dummy = attempt_allocation(to_allocate_words, true /* bot_updates */); 304 CollectedHeap::fill_with_object(dummy, to_allocate_words); 305 } 306 } 307 } 308 return G1AllocRegion::release(); 309 }