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