1 /* 2 * Copyright (c) 2001, 2020, 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/parallel/objectStartArray.inline.hpp" 27 #include "gc/parallel/parallelArguments.hpp" 28 #include "gc/parallel/parallelScavengeHeap.hpp" 29 #include "gc/parallel/psAdaptiveSizePolicy.hpp" 30 #include "gc/parallel/psCardTable.hpp" 31 #include "gc/parallel/psFileBackedVirtualspace.hpp" 32 #include "gc/parallel/psOldGen.hpp" 33 #include "gc/shared/cardTableBarrierSet.hpp" 34 #include "gc/shared/gcLocker.hpp" 35 #include "gc/shared/spaceDecorator.inline.hpp" 36 #include "logging/log.hpp" 37 #include "oops/oop.inline.hpp" 38 #include "runtime/java.hpp" 39 #include "utilities/align.hpp" 40 41 PSOldGen::PSOldGen(ReservedSpace rs, size_t alignment, 42 size_t initial_size, size_t min_size, size_t max_size, 43 const char* perf_data_name, int level): 44 _init_gen_size(initial_size), _min_gen_size(min_size), 45 _max_gen_size(max_size) 46 { 47 initialize(rs, alignment, perf_data_name, level); 48 } 49 50 PSOldGen::PSOldGen(size_t initial_size, 51 size_t min_size, size_t max_size, 52 const char* perf_data_name, int level): 53 _init_gen_size(initial_size), _min_gen_size(min_size), 54 _max_gen_size(max_size) 55 {} 56 57 void PSOldGen::initialize(ReservedSpace rs, size_t alignment, 58 const char* perf_data_name, int level) { 59 initialize_virtual_space(rs, alignment); 60 initialize_work(perf_data_name, level); 61 62 // The old gen can grow to gen_size_limit(). _reserve reflects only 63 // the current maximum that can be committed. 64 assert(_reserved.byte_size() <= gen_size_limit(), "Consistency check"); 65 66 initialize_performance_counters(perf_data_name, level); 67 } 68 69 void PSOldGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) { 70 71 if(ParallelArguments::is_heterogeneous_heap()) { 72 _virtual_space = new PSFileBackedVirtualSpace(rs, alignment, AllocateOldGenAt); 73 if (!(static_cast <PSFileBackedVirtualSpace*>(_virtual_space))->initialize()) { 74 vm_exit_during_initialization("Could not map space for PSOldGen at given AllocateOldGenAt path"); 75 } 76 } else { 77 _virtual_space = new PSVirtualSpace(rs, alignment); 78 } 79 if (!_virtual_space->expand_by(_init_gen_size)) { 80 vm_exit_during_initialization("Could not reserve enough space for " 81 "object heap"); 82 } 83 } 84 85 void PSOldGen::initialize_work(const char* perf_data_name, int level) { 86 // 87 // Basic memory initialization 88 // 89 90 MemRegion limit_reserved((HeapWord*)virtual_space()->low_boundary(), 91 heap_word_size(_max_gen_size)); 92 assert(limit_reserved.byte_size() == _max_gen_size, 93 "word vs bytes confusion"); 94 // 95 // Object start stuff 96 // 97 98 start_array()->initialize(limit_reserved); 99 100 _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), 101 (HeapWord*)virtual_space()->high_boundary()); 102 103 // 104 // Card table stuff 105 // 106 107 MemRegion cmr((HeapWord*)virtual_space()->low(), 108 (HeapWord*)virtual_space()->high()); 109 if (ZapUnusedHeapArea) { 110 // Mangle newly committed space immediately rather than 111 // waiting for the initialization of the space even though 112 // mangling is related to spaces. Doing it here eliminates 113 // the need to carry along information that a complete mangling 114 // (bottom to end) needs to be done. 115 SpaceMangler::mangle_region(cmr); 116 } 117 118 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 119 PSCardTable* ct = heap->card_table(); 120 ct->resize_covered_region(cmr); 121 122 // Verify that the start and end of this generation is the start of a card. 123 // If this wasn't true, a single card could span more than one generation, 124 // which would cause problems when we commit/uncommit memory, and when we 125 // clear and dirty cards. 126 guarantee(ct->is_card_aligned(_reserved.start()), "generation must be card aligned"); 127 if (_reserved.end() != heap->reserved_region().end()) { 128 // Don't check at the very end of the heap as we'll assert that we're probing off 129 // the end if we try. 130 guarantee(ct->is_card_aligned(_reserved.end()), "generation must be card aligned"); 131 } 132 133 // 134 // ObjectSpace stuff 135 // 136 137 _object_space = new MutableSpace(virtual_space()->alignment()); 138 139 if (_object_space == NULL) 140 vm_exit_during_initialization("Could not allocate an old gen space"); 141 142 object_space()->initialize(cmr, 143 SpaceDecorator::Clear, 144 SpaceDecorator::Mangle); 145 146 // Update the start_array 147 start_array()->set_covered_region(cmr); 148 } 149 150 void PSOldGen::initialize_performance_counters(const char* perf_data_name, int level) { 151 // Generation Counters, generation 'level', 1 subspace 152 _gen_counters = new PSGenerationCounters(perf_data_name, level, 1, _min_gen_size, 153 _max_gen_size, virtual_space()); 154 _space_counters = new SpaceCounters(perf_data_name, 0, 155 virtual_space()->reserved_size(), 156 _object_space, _gen_counters); 157 } 158 159 // Assume that the generation has been allocated if its 160 // reserved size is not 0. 161 bool PSOldGen::is_allocated() { 162 return virtual_space()->reserved_size() != 0; 163 } 164 165 size_t PSOldGen::contiguous_available() const { 166 return object_space()->free_in_bytes() + virtual_space()->uncommitted_size(); 167 } 168 169 // Allocation. We report all successful allocations to the size policy 170 // Note that the perm gen does not use this method, and should not! 171 HeapWord* PSOldGen::allocate(size_t word_size) { 172 assert_locked_or_safepoint(Heap_lock); 173 HeapWord* res = allocate_noexpand(word_size); 174 175 if (res == NULL) { 176 res = expand_and_allocate(word_size); 177 } 178 179 // Allocations in the old generation need to be reported 180 if (res != NULL) { 181 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 182 heap->size_policy()->tenured_allocation(word_size * HeapWordSize); 183 } 184 185 return res; 186 } 187 188 HeapWord* PSOldGen::expand_and_allocate(size_t word_size) { 189 expand(word_size*HeapWordSize); 190 if (GCExpandToAllocateDelayMillis > 0) { 191 os::naked_sleep(GCExpandToAllocateDelayMillis); 192 } 193 return allocate_noexpand(word_size); 194 } 195 196 HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) { 197 expand(word_size*HeapWordSize); 198 if (GCExpandToAllocateDelayMillis > 0) { 199 os::naked_sleep(GCExpandToAllocateDelayMillis); 200 } 201 return cas_allocate_noexpand(word_size); 202 } 203 204 void PSOldGen::expand(size_t bytes) { 205 if (bytes == 0) { 206 return; 207 } 208 MutexLocker x(ExpandHeap_lock); 209 const size_t alignment = virtual_space()->alignment(); 210 size_t aligned_bytes = align_up(bytes, alignment); 211 size_t aligned_expand_bytes = align_up(MinHeapDeltaBytes, alignment); 212 213 if (UseNUMA) { 214 // With NUMA we use round-robin page allocation for the old gen. Expand by at least 215 // providing a page per lgroup. Alignment is larger or equal to the page size. 216 aligned_expand_bytes = MAX2(aligned_expand_bytes, alignment * os::numa_get_groups_num()); 217 } 218 if (aligned_bytes == 0){ 219 // The alignment caused the number of bytes to wrap. An expand_by(0) will 220 // return true with the implication that and expansion was done when it 221 // was not. A call to expand implies a best effort to expand by "bytes" 222 // but not a guarantee. Align down to give a best effort. This is likely 223 // the most that the generation can expand since it has some capacity to 224 // start with. 225 aligned_bytes = align_down(bytes, alignment); 226 } 227 228 bool success = false; 229 if (aligned_expand_bytes > aligned_bytes) { 230 success = expand_by(aligned_expand_bytes); 231 } 232 if (!success) { 233 success = expand_by(aligned_bytes); 234 } 235 if (!success) { 236 success = expand_to_reserved(); 237 } 238 239 if (success && GCLocker::is_active_and_needs_gc()) { 240 log_debug(gc)("Garbage collection disabled, expanded heap instead"); 241 } 242 } 243 244 bool PSOldGen::expand_by(size_t bytes) { 245 assert_lock_strong(ExpandHeap_lock); 246 assert_locked_or_safepoint(Heap_lock); 247 if (bytes == 0) { 248 return true; // That's what virtual_space()->expand_by(0) would return 249 } 250 bool result = virtual_space()->expand_by(bytes); 251 if (result) { 252 if (ZapUnusedHeapArea) { 253 // We need to mangle the newly expanded area. The memregion spans 254 // end -> new_end, we assume that top -> end is already mangled. 255 // Do the mangling before post_resize() is called because 256 // the space is available for allocation after post_resize(); 257 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high(); 258 assert(object_space()->end() < virtual_space_high, 259 "Should be true before post_resize()"); 260 MemRegion mangle_region(object_space()->end(), virtual_space_high); 261 // Note that the object space has not yet been updated to 262 // coincide with the new underlying virtual space. 263 SpaceMangler::mangle_region(mangle_region); 264 } 265 post_resize(); 266 if (UsePerfData) { 267 _space_counters->update_capacity(); 268 _gen_counters->update_all(); 269 } 270 } 271 272 if (result) { 273 size_t new_mem_size = virtual_space()->committed_size(); 274 size_t old_mem_size = new_mem_size - bytes; 275 log_debug(gc)("Expanding %s from " SIZE_FORMAT "K by " SIZE_FORMAT "K to " SIZE_FORMAT "K", 276 name(), old_mem_size/K, bytes/K, new_mem_size/K); 277 } 278 279 return result; 280 } 281 282 bool PSOldGen::expand_to_reserved() { 283 assert_lock_strong(ExpandHeap_lock); 284 assert_locked_or_safepoint(Heap_lock); 285 286 bool result = true; 287 const size_t remaining_bytes = virtual_space()->uncommitted_size(); 288 if (remaining_bytes > 0) { 289 result = expand_by(remaining_bytes); 290 DEBUG_ONLY(if (!result) log_warning(gc)("grow to reserve failed")); 291 } 292 return result; 293 } 294 295 void PSOldGen::shrink(size_t bytes) { 296 assert_lock_strong(ExpandHeap_lock); 297 assert_locked_or_safepoint(Heap_lock); 298 299 size_t size = align_down(bytes, virtual_space()->alignment()); 300 if (size > 0) { 301 assert_lock_strong(ExpandHeap_lock); 302 virtual_space()->shrink_by(bytes); 303 post_resize(); 304 305 size_t new_mem_size = virtual_space()->committed_size(); 306 size_t old_mem_size = new_mem_size + bytes; 307 log_debug(gc)("Shrinking %s from " SIZE_FORMAT "K by " SIZE_FORMAT "K to " SIZE_FORMAT "K", 308 name(), old_mem_size/K, bytes/K, new_mem_size/K); 309 } 310 } 311 312 void PSOldGen::resize(size_t desired_free_space) { 313 const size_t alignment = virtual_space()->alignment(); 314 const size_t size_before = virtual_space()->committed_size(); 315 size_t new_size = used_in_bytes() + desired_free_space; 316 if (new_size < used_in_bytes()) { 317 // Overflowed the addition. 318 new_size = gen_size_limit(); 319 } 320 // Adjust according to our min and max 321 new_size = clamp(new_size, min_gen_size(), gen_size_limit()); 322 323 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?"); 324 new_size = align_up(new_size, alignment); 325 326 const size_t current_size = capacity_in_bytes(); 327 328 log_trace(gc, ergo)("AdaptiveSizePolicy::old generation size: " 329 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT 330 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT 331 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT, 332 desired_free_space, used_in_bytes(), new_size, current_size, 333 gen_size_limit(), min_gen_size()); 334 335 if (new_size == current_size) { 336 // No change requested 337 return; 338 } 339 if (new_size > current_size) { 340 size_t change_bytes = new_size - current_size; 341 expand(change_bytes); 342 } else { 343 size_t change_bytes = current_size - new_size; 344 // shrink doesn't grab this lock, expand does. Is that right? 345 MutexLocker x(ExpandHeap_lock); 346 shrink(change_bytes); 347 } 348 349 log_trace(gc, ergo)("AdaptiveSizePolicy::old generation size: collection: %d (" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ", 350 ParallelScavengeHeap::heap()->total_collections(), 351 size_before, 352 virtual_space()->committed_size()); 353 } 354 355 // NOTE! We need to be careful about resizing. During a GC, multiple 356 // allocators may be active during heap expansion. If we allow the 357 // heap resizing to become visible before we have correctly resized 358 // all heap related data structures, we may cause program failures. 359 void PSOldGen::post_resize() { 360 // First construct a memregion representing the new size 361 MemRegion new_memregion((HeapWord*)virtual_space()->low(), 362 (HeapWord*)virtual_space()->high()); 363 size_t new_word_size = new_memregion.word_size(); 364 365 start_array()->set_covered_region(new_memregion); 366 ParallelScavengeHeap::heap()->card_table()->resize_covered_region(new_memregion); 367 368 // ALWAYS do this last!! 369 object_space()->initialize(new_memregion, 370 SpaceDecorator::DontClear, 371 SpaceDecorator::DontMangle); 372 373 assert(new_word_size == heap_word_size(object_space()->capacity_in_bytes()), 374 "Sanity"); 375 } 376 377 size_t PSOldGen::gen_size_limit() { 378 return _max_gen_size; 379 } 380 381 void PSOldGen::reset_after_change() { 382 ShouldNotReachHere(); 383 return; 384 } 385 386 size_t PSOldGen::available_for_expansion() { 387 ShouldNotReachHere(); 388 return 0; 389 } 390 391 size_t PSOldGen::available_for_contraction() { 392 ShouldNotReachHere(); 393 return 0; 394 } 395 396 void PSOldGen::print() const { print_on(tty);} 397 void PSOldGen::print_on(outputStream* st) const { 398 st->print(" %-15s", name()); 399 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", 400 capacity_in_bytes()/K, used_in_bytes()/K); 401 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", 402 p2i(virtual_space()->low_boundary()), 403 p2i(virtual_space()->high()), 404 p2i(virtual_space()->high_boundary())); 405 406 st->print(" object"); object_space()->print_on(st); 407 } 408 409 void PSOldGen::update_counters() { 410 if (UsePerfData) { 411 _space_counters->update_all(); 412 _gen_counters->update_all(); 413 } 414 } 415 416 #ifndef PRODUCT 417 418 void PSOldGen::space_invariants() { 419 assert(object_space()->end() == (HeapWord*) virtual_space()->high(), 420 "Space invariant"); 421 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(), 422 "Space invariant"); 423 assert(virtual_space()->low_boundary() <= virtual_space()->low(), 424 "Space invariant"); 425 assert(virtual_space()->high_boundary() >= virtual_space()->high(), 426 "Space invariant"); 427 assert(virtual_space()->low_boundary() == (char*) _reserved.start(), 428 "Space invariant"); 429 assert(virtual_space()->high_boundary() == (char*) _reserved.end(), 430 "Space invariant"); 431 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(), 432 "Space invariant"); 433 } 434 #endif 435 436 void PSOldGen::verify() { 437 object_space()->verify(); 438 } 439 class VerifyObjectStartArrayClosure : public ObjectClosure { 440 PSOldGen* _old_gen; 441 ObjectStartArray* _start_array; 442 443 public: 444 VerifyObjectStartArrayClosure(PSOldGen* old_gen, ObjectStartArray* start_array) : 445 _old_gen(old_gen), _start_array(start_array) { } 446 447 virtual void do_object(oop obj) { 448 HeapWord* test_addr = cast_from_oop<HeapWord*>(obj) + 1; 449 guarantee(_start_array->object_start(test_addr) == cast_from_oop<HeapWord*>(obj), "ObjectStartArray cannot find start of object"); 450 guarantee(_start_array->is_block_allocated(cast_from_oop<HeapWord*>(obj)), "ObjectStartArray missing block allocation"); 451 } 452 }; 453 454 void PSOldGen::verify_object_start_array() { 455 VerifyObjectStartArrayClosure check( this, &_start_array ); 456 object_iterate(&check); 457 } 458 459 #ifndef PRODUCT 460 void PSOldGen::record_spaces_top() { 461 assert(ZapUnusedHeapArea, "Not mangling unused space"); 462 object_space()->set_top_for_allocations(); 463 } 464 #endif