1 /* 2 * Copyright (c) 2014, 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 #include "precompiled.hpp" 26 #include "gc/g1/g1Allocator.inline.hpp" 27 #include "gc/g1/g1AllocRegion.inline.hpp" 28 #include "gc/g1/g1CollectedHeap.inline.hpp" 29 #include "gc/g1/g1CollectorPolicy.hpp" 30 #include "gc/g1/g1MarkSweep.hpp" 31 #include "gc/g1/heapRegion.inline.hpp" 32 #include "gc/g1/heapRegionSet.inline.hpp" 33 34 G1DefaultAllocator::G1DefaultAllocator(G1CollectedHeap* heap) : 35 G1Allocator(heap), 36 _retained_old_gc_alloc_region(NULL), 37 _survivor_gc_alloc_region(heap->alloc_buffer_stats(InCSetState::Young)), 38 _old_gc_alloc_region(heap->alloc_buffer_stats(InCSetState::Old)) { 39 } 40 41 void G1DefaultAllocator::init_mutator_alloc_region() { 42 assert(_mutator_alloc_region.get() == NULL, "pre-condition"); 43 _mutator_alloc_region.init(); 44 } 45 46 void G1DefaultAllocator::release_mutator_alloc_region() { 47 _mutator_alloc_region.release(); 48 assert(_mutator_alloc_region.get() == NULL, "post-condition"); 49 } 50 51 void G1Allocator::reuse_retained_old_region(EvacuationInfo& evacuation_info, 52 OldGCAllocRegion* old, 53 HeapRegion** retained_old) { 54 HeapRegion* retained_region = *retained_old; 55 *retained_old = NULL; 56 assert(retained_region == NULL || !retained_region->is_archive(), 57 err_msg("Archive region should not be alloc region (index %u)", retained_region->hrm_index())); 58 59 // We will discard the current GC alloc region if: 60 // a) it's in the collection set (it can happen!), 61 // b) it's already full (no point in using it), 62 // c) it's empty (this means that it was emptied during 63 // a cleanup and it should be on the free list now), or 64 // d) it's humongous (this means that it was emptied 65 // during a cleanup and was added to the free list, but 66 // has been subsequently used to allocate a humongous 67 // object that may be less than the region size). 68 if (retained_region != NULL && 69 !retained_region->in_collection_set() && 70 !(retained_region->top() == retained_region->end()) && 71 !retained_region->is_empty() && 72 !retained_region->is_humongous()) { 73 retained_region->record_timestamp(); 74 // The retained region was added to the old region set when it was 75 // retired. We have to remove it now, since we don't allow regions 76 // we allocate to in the region sets. We'll re-add it later, when 77 // it's retired again. 78 _g1h->old_set_remove(retained_region); 79 bool during_im = _g1h->collector_state()->during_initial_mark_pause(); 80 retained_region->note_start_of_copying(during_im); 81 old->set(retained_region); 82 _g1h->hr_printer()->reuse(retained_region); 83 evacuation_info.set_alloc_regions_used_before(retained_region->used()); 84 } 85 } 86 87 void G1DefaultAllocator::init_gc_alloc_regions(EvacuationInfo& evacuation_info) { 88 assert_at_safepoint(true /* should_be_vm_thread */); 89 90 G1Allocator::init_gc_alloc_regions(evacuation_info); 91 92 _survivor_gc_alloc_region.init(); 93 _old_gc_alloc_region.init(); 94 reuse_retained_old_region(evacuation_info, 95 &_old_gc_alloc_region, 96 &_retained_old_gc_alloc_region); 97 } 98 99 void G1DefaultAllocator::release_gc_alloc_regions(EvacuationInfo& evacuation_info) { 100 AllocationContext_t context = AllocationContext::current(); 101 evacuation_info.set_allocation_regions(survivor_gc_alloc_region(context)->count() + 102 old_gc_alloc_region(context)->count()); 103 survivor_gc_alloc_region(context)->release(); 104 // If we have an old GC alloc region to release, we'll save it in 105 // _retained_old_gc_alloc_region. If we don't 106 // _retained_old_gc_alloc_region will become NULL. This is what we 107 // want either way so no reason to check explicitly for either 108 // condition. 109 _retained_old_gc_alloc_region = old_gc_alloc_region(context)->release(); 110 if (_retained_old_gc_alloc_region != NULL) { 111 _retained_old_gc_alloc_region->record_retained_region(); 112 } 113 114 _g1h->alloc_buffer_stats(InCSetState::Young)->adjust_desired_plab_sz(); 115 _g1h->alloc_buffer_stats(InCSetState::Old)->adjust_desired_plab_sz(); 116 } 117 118 void G1DefaultAllocator::abandon_gc_alloc_regions() { 119 assert(survivor_gc_alloc_region(AllocationContext::current())->get() == NULL, "pre-condition"); 120 assert(old_gc_alloc_region(AllocationContext::current())->get() == NULL, "pre-condition"); 121 _retained_old_gc_alloc_region = NULL; 122 } 123 124 G1PLAB::G1PLAB(size_t gclab_word_size) : 125 PLAB(gclab_word_size), _retired(true) { } 126 127 size_t G1Allocator::unsafe_max_tlab_alloc(AllocationContext_t context) { 128 // Return the remaining space in the cur alloc region, but not less than 129 // the min TLAB size. 130 131 // Also, this value can be at most the humongous object threshold, 132 // since we can't allow tlabs to grow big enough to accommodate 133 // humongous objects. 134 135 HeapRegion* hr = mutator_alloc_region(context)->get(); 136 size_t max_tlab = _g1h->max_tlab_size() * wordSize; 137 if (hr == NULL) { 138 return max_tlab; 139 } else { 140 return MIN2(MAX2(hr->free(), (size_t) MinTLABSize), max_tlab); 141 } 142 } 143 144 HeapWord* G1Allocator::par_allocate_during_gc(InCSetState dest, 145 size_t word_size, 146 AllocationContext_t context) { 147 switch (dest.value()) { 148 case InCSetState::Young: 149 return survivor_attempt_allocation(word_size, context); 150 case InCSetState::Old: 151 return old_attempt_allocation(word_size, context); 152 default: 153 ShouldNotReachHere(); 154 return NULL; // Keep some compilers happy 155 } 156 } 157 158 bool G1Allocator::survivor_is_full(AllocationContext_t context) const { 159 return _survivor_is_full; 160 } 161 162 bool G1Allocator::old_is_full(AllocationContext_t context) const { 163 return _old_is_full; 164 } 165 166 void G1Allocator::set_survivor_full(AllocationContext_t context) { 167 _survivor_is_full = true; 168 } 169 170 void G1Allocator::set_old_full(AllocationContext_t context) { 171 _old_is_full = true; 172 } 173 174 HeapWord* G1Allocator::survivor_attempt_allocation(size_t word_size, 175 AllocationContext_t context) { 176 assert(!_g1h->is_humongous(word_size), 177 "we should not be seeing humongous-size allocations in this path"); 178 179 HeapWord* result = survivor_gc_alloc_region(context)->attempt_allocation(word_size, 180 false /* bot_updates */); 181 if (result == NULL && !survivor_is_full(context)) { 182 MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag); 183 result = survivor_gc_alloc_region(context)->attempt_allocation_locked(word_size, 184 false /* bot_updates */); 185 if (result == NULL) { 186 set_survivor_full(context); 187 } 188 } 189 if (result != NULL) { 190 _g1h->dirty_young_block(result, word_size); 191 } 192 return result; 193 } 194 195 HeapWord* G1Allocator::old_attempt_allocation(size_t word_size, 196 AllocationContext_t context) { 197 assert(!_g1h->is_humongous(word_size), 198 "we should not be seeing humongous-size allocations in this path"); 199 200 HeapWord* result = old_gc_alloc_region(context)->attempt_allocation(word_size, 201 true /* bot_updates */); 202 if (result == NULL && !old_is_full(context)) { 203 MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag); 204 result = old_gc_alloc_region(context)->attempt_allocation_locked(word_size, 205 true /* bot_updates */); 206 if (result == NULL) { 207 set_old_full(context); 208 } 209 } 210 return result; 211 } 212 213 void G1Allocator::init_gc_alloc_regions(EvacuationInfo& evacuation_info) { 214 _survivor_is_full = false; 215 _old_is_full = false; 216 } 217 218 G1PLABAllocator::G1PLABAllocator(G1Allocator* allocator) : 219 _g1h(G1CollectedHeap::heap()), 220 _allocator(allocator), 221 _survivor_alignment_bytes(calc_survivor_alignment_bytes()) { 222 for (size_t i = 0; i < ARRAY_SIZE(_direct_allocated); i++) { 223 _direct_allocated[i] = 0; 224 } 225 } 226 227 bool G1PLABAllocator::may_throw_away_buffer(size_t const allocation_word_sz, size_t const buffer_size) const { 228 return (allocation_word_sz * 100 < buffer_size * ParallelGCBufferWastePct); 229 } 230 231 HeapWord* G1PLABAllocator::allocate_direct_or_new_plab(InCSetState dest, 232 size_t word_sz, 233 AllocationContext_t context, 234 bool* plab_refill_failed) { 235 size_t plab_word_size = G1CollectedHeap::heap()->desired_plab_sz(dest); 236 size_t required_in_plab = PLAB::size_required_for_allocation(word_sz); 237 238 // Only get a new PLAB if the allocation fits and it would not waste more than 239 // ParallelGCBufferWastePct in the existing buffer. 240 if ((required_in_plab <= plab_word_size) && 241 may_throw_away_buffer(required_in_plab, plab_word_size)) { 242 243 G1PLAB* alloc_buf = alloc_buffer(dest, context); 244 alloc_buf->retire(); 245 246 HeapWord* buf = _allocator->par_allocate_during_gc(dest, plab_word_size, context); 247 if (buf != NULL) { 248 // Otherwise. 249 alloc_buf->set_buf(buf, plab_word_size); 250 251 HeapWord* const obj = alloc_buf->allocate(word_sz); 252 assert(obj != NULL, err_msg("PLAB should have been big enough, tried to allocate " 253 SIZE_FORMAT " requiring " SIZE_FORMAT " PLAB size " SIZE_FORMAT, 254 word_sz, required_in_plab, plab_word_size)); 255 return obj; 256 } 257 // Otherwise. 258 *plab_refill_failed = true; 259 } 260 // Try direct allocation. 261 HeapWord* result = _allocator->par_allocate_during_gc(dest, word_sz, context); 262 if (result != NULL) { 263 _direct_allocated[dest.value()] += word_sz; 264 } 265 return result; 266 } 267 268 void G1PLABAllocator::undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz, AllocationContext_t context) { 269 alloc_buffer(dest, context)->undo_allocation(obj, word_sz); 270 } 271 272 G1DefaultPLABAllocator::G1DefaultPLABAllocator(G1Allocator* allocator) : 273 G1PLABAllocator(allocator), 274 _surviving_alloc_buffer(_g1h->desired_plab_sz(InCSetState::Young)), 275 _tenured_alloc_buffer(_g1h->desired_plab_sz(InCSetState::Old)) { 276 for (uint state = 0; state < InCSetState::Num; state++) { 277 _alloc_buffers[state] = NULL; 278 } 279 _alloc_buffers[InCSetState::Young] = &_surviving_alloc_buffer; 280 _alloc_buffers[InCSetState::Old] = &_tenured_alloc_buffer; 281 } 282 283 void G1DefaultPLABAllocator::flush_and_retire_stats() { 284 for (uint state = 0; state < InCSetState::Num; state++) { 285 G1PLAB* const buf = _alloc_buffers[state]; 286 if (buf != NULL) { 287 G1EvacStats* stats = _g1h->alloc_buffer_stats(state); 288 buf->flush_and_retire_stats(stats); 289 stats->add_direct_allocated(_direct_allocated[state]); 290 _direct_allocated[state] = 0; 291 } 292 } 293 } 294 295 void G1DefaultPLABAllocator::waste(size_t& wasted, size_t& undo_wasted) { 296 wasted = 0; 297 undo_wasted = 0; 298 for (uint state = 0; state < InCSetState::Num; state++) { 299 G1PLAB * const buf = _alloc_buffers[state]; 300 if (buf != NULL) { 301 wasted += buf->waste(); 302 undo_wasted += buf->undo_waste(); 303 } 304 } 305 } 306 307 G1ArchiveAllocator* G1ArchiveAllocator::create_allocator(G1CollectedHeap* g1h) { 308 // Create the archive allocator, and also enable archive object checking 309 // in mark-sweep, since we will be creating archive regions. 310 G1ArchiveAllocator* result = new G1ArchiveAllocator(g1h); 311 G1MarkSweep::enable_archive_object_check(); 312 return result; 313 } 314 315 bool G1ArchiveAllocator::alloc_new_region() { 316 // Allocate the highest free region in the reserved heap, 317 // and add it to our list of allocated regions. It is marked 318 // archive and added to the old set. 319 HeapRegion* hr = _g1h->alloc_highest_free_region(); 320 if (hr == NULL) { 321 return false; 322 } 323 assert(hr->is_empty(), err_msg("expected empty region (index %u)", hr->hrm_index())); 324 hr->set_archive(); 325 _g1h->old_set_add(hr); 326 _g1h->hr_printer()->alloc(hr, G1HRPrinter::Archive); 327 _allocated_regions.append(hr); 328 _allocation_region = hr; 329 330 // Set up _bottom and _max to begin allocating in the lowest 331 // min_region_size'd chunk of the allocated G1 region. 332 _bottom = hr->bottom(); 333 _max = _bottom + HeapRegion::min_region_size_in_words(); 334 335 // Tell mark-sweep that objects in this region are not to be marked. 336 G1MarkSweep::mark_range_archive(MemRegion(_bottom, HeapRegion::GrainWords)); 337 338 // Since we've modified the old set, call update_sizes. 339 _g1h->g1mm()->update_sizes(); 340 return true; 341 } 342 343 HeapWord* G1ArchiveAllocator::archive_mem_allocate(size_t word_size) { 344 assert(word_size != 0, "size must not be zero"); 345 if (_allocation_region == NULL) { 346 if (!alloc_new_region()) { 347 return NULL; 348 } 349 } 350 HeapWord* old_top = _allocation_region->top(); 351 assert(_bottom >= _allocation_region->bottom(), 352 err_msg("inconsistent allocation state: " PTR_FORMAT " < " PTR_FORMAT, 353 p2i(_bottom), p2i(_allocation_region->bottom()))); 354 assert(_max <= _allocation_region->end(), 355 err_msg("inconsistent allocation state: " PTR_FORMAT " > " PTR_FORMAT, 356 p2i(_max), p2i(_allocation_region->end()))); 357 assert(_bottom <= old_top && old_top <= _max, 358 err_msg("inconsistent allocation state: expected " 359 PTR_FORMAT " <= " PTR_FORMAT " <= " PTR_FORMAT, 360 p2i(_bottom), p2i(old_top), p2i(_max))); 361 362 // Allocate the next word_size words in the current allocation chunk. 363 // If allocation would cross the _max boundary, insert a filler and begin 364 // at the base of the next min_region_size'd chunk. Also advance to the next 365 // chunk if we don't yet cross the boundary, but the remainder would be too 366 // small to fill. 367 HeapWord* new_top = old_top + word_size; 368 size_t remainder = pointer_delta(_max, new_top); 369 if ((new_top > _max) || 370 ((new_top < _max) && (remainder < CollectedHeap::min_fill_size()))) { 371 if (old_top != _max) { 372 size_t fill_size = pointer_delta(_max, old_top); 373 CollectedHeap::fill_with_object(old_top, fill_size); 374 _summary_bytes_used += fill_size * HeapWordSize; 375 } 376 _allocation_region->set_top(_max); 377 old_top = _bottom = _max; 378 379 // Check if we've just used up the last min_region_size'd chunk 380 // in the current region, and if so, allocate a new one. 381 if (_bottom != _allocation_region->end()) { 382 _max = _bottom + HeapRegion::min_region_size_in_words(); 383 } else { 384 if (!alloc_new_region()) { 385 return NULL; 386 } 387 old_top = _allocation_region->bottom(); 388 } 389 } 390 _allocation_region->set_top(old_top + word_size); 391 _summary_bytes_used += word_size * HeapWordSize; 392 393 return old_top; 394 } 395 396 void G1ArchiveAllocator::complete_archive(GrowableArray<MemRegion>* ranges, 397 size_t end_alignment_in_bytes) { 398 assert((end_alignment_in_bytes >> LogHeapWordSize) < HeapRegion::min_region_size_in_words(), 399 err_msg("alignment " SIZE_FORMAT " too large", end_alignment_in_bytes)); 400 assert(is_size_aligned(end_alignment_in_bytes, HeapWordSize), 401 err_msg("alignment " SIZE_FORMAT " is not HeapWord (%u) aligned", end_alignment_in_bytes, HeapWordSize)); 402 403 // If we've allocated nothing, simply return. 404 if (_allocation_region == NULL) { 405 return; 406 } 407 408 // If an end alignment was requested, insert filler objects. 409 if (end_alignment_in_bytes != 0) { 410 HeapWord* currtop = _allocation_region->top(); 411 HeapWord* newtop = (HeapWord*)align_pointer_up(currtop, end_alignment_in_bytes); 412 size_t fill_size = pointer_delta(newtop, currtop); 413 if (fill_size != 0) { 414 if (fill_size < CollectedHeap::min_fill_size()) { 415 // If the required fill is smaller than we can represent, 416 // bump up to the next aligned address. We know we won't exceed the current 417 // region boundary because the max supported alignment is smaller than the min 418 // region size, and because the allocation code never leaves space smaller than 419 // the min_fill_size at the top of the current allocation region. 420 newtop = (HeapWord*)align_pointer_up(currtop + CollectedHeap::min_fill_size(), 421 end_alignment_in_bytes); 422 fill_size = pointer_delta(newtop, currtop); 423 } 424 HeapWord* fill = archive_mem_allocate(fill_size); 425 CollectedHeap::fill_with_objects(fill, fill_size); 426 } 427 } 428 429 // Loop through the allocated regions, and create MemRegions summarizing 430 // the allocated address range, combining contiguous ranges. Add the 431 // MemRegions to the GrowableArray provided by the caller. 432 int index = _allocated_regions.length() - 1; 433 assert(_allocated_regions.at(index) == _allocation_region, 434 err_msg("expected region %u at end of array, found %u", 435 _allocation_region->hrm_index(), _allocated_regions.at(index)->hrm_index())); 436 HeapWord* base_address = _allocation_region->bottom(); 437 HeapWord* top = base_address; 438 439 while (index >= 0) { 440 HeapRegion* next = _allocated_regions.at(index); 441 HeapWord* new_base = next->bottom(); 442 HeapWord* new_top = next->top(); 443 if (new_base != top) { 444 ranges->append(MemRegion(base_address, pointer_delta(top, base_address))); 445 base_address = new_base; 446 } 447 top = new_top; 448 index = index - 1; 449 } 450 451 assert(top != base_address, err_msg("zero-sized range, address " PTR_FORMAT, p2i(base_address))); 452 ranges->append(MemRegion(base_address, pointer_delta(top, base_address))); 453 _allocated_regions.clear(); 454 _allocation_region = NULL; 455 };