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 object_space()->initialize(cmr, 139 SpaceDecorator::Clear, 140 SpaceDecorator::Mangle); 141 142 // Update the start_array 143 start_array()->set_covered_region(cmr); 144 } 145 146 void PSOldGen::initialize_performance_counters(const char* perf_data_name, int level) { 147 // Generation Counters, generation 'level', 1 subspace 148 _gen_counters = new PSGenerationCounters(perf_data_name, level, 1, _min_gen_size, 149 _max_gen_size, virtual_space()); 150 _space_counters = new SpaceCounters(perf_data_name, 0, 151 virtual_space()->reserved_size(), 152 _object_space, _gen_counters); 153 } 154 155 // Assume that the generation has been allocated if its 156 // reserved size is not 0. 157 bool PSOldGen::is_allocated() { 158 return virtual_space()->reserved_size() != 0; 159 } 160 161 size_t PSOldGen::contiguous_available() const { 162 return object_space()->free_in_bytes() + virtual_space()->uncommitted_size(); 163 } 164 165 // Allocation. We report all successful allocations to the size policy 166 // Note that the perm gen does not use this method, and should not! 167 HeapWord* PSOldGen::allocate(size_t word_size) { 168 assert_locked_or_safepoint(Heap_lock); 169 HeapWord* res = allocate_noexpand(word_size); 170 171 if (res == NULL) { 172 res = expand_and_allocate(word_size); 173 } 174 175 // Allocations in the old generation need to be reported 176 if (res != NULL) { 177 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap(); 178 heap->size_policy()->tenured_allocation(word_size * HeapWordSize); 179 } 180 181 return res; 182 } 183 184 HeapWord* PSOldGen::expand_and_allocate(size_t word_size) { 185 expand(word_size*HeapWordSize); 186 if (GCExpandToAllocateDelayMillis > 0) { 187 os::naked_sleep(GCExpandToAllocateDelayMillis); 188 } 189 return allocate_noexpand(word_size); 190 } 191 192 HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) { 193 expand(word_size*HeapWordSize); 194 if (GCExpandToAllocateDelayMillis > 0) { 195 os::naked_sleep(GCExpandToAllocateDelayMillis); 196 } 197 return cas_allocate_noexpand(word_size); 198 } 199 200 void PSOldGen::expand(size_t bytes) { 201 if (bytes == 0) { 202 return; 203 } 204 MutexLocker x(ExpandHeap_lock); 205 const size_t alignment = virtual_space()->alignment(); 206 size_t aligned_bytes = align_up(bytes, alignment); 207 size_t aligned_expand_bytes = align_up(MinHeapDeltaBytes, alignment); 208 209 if (UseNUMA) { 210 // With NUMA we use round-robin page allocation for the old gen. Expand by at least 211 // providing a page per lgroup. Alignment is larger or equal to the page size. 212 aligned_expand_bytes = MAX2(aligned_expand_bytes, alignment * os::numa_get_groups_num()); 213 } 214 if (aligned_bytes == 0){ 215 // The alignment caused the number of bytes to wrap. An expand_by(0) will 216 // return true with the implication that and expansion was done when it 217 // was not. A call to expand implies a best effort to expand by "bytes" 218 // but not a guarantee. Align down to give a best effort. This is likely 219 // the most that the generation can expand since it has some capacity to 220 // start with. 221 aligned_bytes = align_down(bytes, alignment); 222 } 223 224 bool success = false; 225 if (aligned_expand_bytes > aligned_bytes) { 226 success = expand_by(aligned_expand_bytes); 227 } 228 if (!success) { 229 success = expand_by(aligned_bytes); 230 } 231 if (!success) { 232 success = expand_to_reserved(); 233 } 234 235 if (success && GCLocker::is_active_and_needs_gc()) { 236 log_debug(gc)("Garbage collection disabled, expanded heap instead"); 237 } 238 } 239 240 bool PSOldGen::expand_by(size_t bytes) { 241 assert_lock_strong(ExpandHeap_lock); 242 assert_locked_or_safepoint(Heap_lock); 243 if (bytes == 0) { 244 return true; // That's what virtual_space()->expand_by(0) would return 245 } 246 bool result = virtual_space()->expand_by(bytes); 247 if (result) { 248 if (ZapUnusedHeapArea) { 249 // We need to mangle the newly expanded area. The memregion spans 250 // end -> new_end, we assume that top -> end is already mangled. 251 // Do the mangling before post_resize() is called because 252 // the space is available for allocation after post_resize(); 253 HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high(); 254 assert(object_space()->end() < virtual_space_high, 255 "Should be true before post_resize()"); 256 MemRegion mangle_region(object_space()->end(), virtual_space_high); 257 // Note that the object space has not yet been updated to 258 // coincide with the new underlying virtual space. 259 SpaceMangler::mangle_region(mangle_region); 260 } 261 post_resize(); 262 if (UsePerfData) { 263 _space_counters->update_capacity(); 264 _gen_counters->update_all(); 265 } 266 } 267 268 if (result) { 269 size_t new_mem_size = virtual_space()->committed_size(); 270 size_t old_mem_size = new_mem_size - bytes; 271 log_debug(gc)("Expanding %s from " SIZE_FORMAT "K by " SIZE_FORMAT "K to " SIZE_FORMAT "K", 272 name(), old_mem_size/K, bytes/K, new_mem_size/K); 273 } 274 275 return result; 276 } 277 278 bool PSOldGen::expand_to_reserved() { 279 assert_lock_strong(ExpandHeap_lock); 280 assert_locked_or_safepoint(Heap_lock); 281 282 bool result = true; 283 const size_t remaining_bytes = virtual_space()->uncommitted_size(); 284 if (remaining_bytes > 0) { 285 result = expand_by(remaining_bytes); 286 DEBUG_ONLY(if (!result) log_warning(gc)("grow to reserve failed")); 287 } 288 return result; 289 } 290 291 void PSOldGen::shrink(size_t bytes) { 292 assert_lock_strong(ExpandHeap_lock); 293 assert_locked_or_safepoint(Heap_lock); 294 295 size_t size = align_down(bytes, virtual_space()->alignment()); 296 if (size > 0) { 297 assert_lock_strong(ExpandHeap_lock); 298 virtual_space()->shrink_by(bytes); 299 post_resize(); 300 301 size_t new_mem_size = virtual_space()->committed_size(); 302 size_t old_mem_size = new_mem_size + bytes; 303 log_debug(gc)("Shrinking %s from " SIZE_FORMAT "K by " SIZE_FORMAT "K to " SIZE_FORMAT "K", 304 name(), old_mem_size/K, bytes/K, new_mem_size/K); 305 } 306 } 307 308 void PSOldGen::resize(size_t desired_free_space) { 309 const size_t alignment = virtual_space()->alignment(); 310 const size_t size_before = virtual_space()->committed_size(); 311 size_t new_size = used_in_bytes() + desired_free_space; 312 if (new_size < used_in_bytes()) { 313 // Overflowed the addition. 314 new_size = gen_size_limit(); 315 } 316 // Adjust according to our min and max 317 new_size = clamp(new_size, min_gen_size(), gen_size_limit()); 318 319 assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?"); 320 new_size = align_up(new_size, alignment); 321 322 const size_t current_size = capacity_in_bytes(); 323 324 log_trace(gc, ergo)("AdaptiveSizePolicy::old generation size: " 325 "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT 326 " new size: " SIZE_FORMAT " current size " SIZE_FORMAT 327 " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT, 328 desired_free_space, used_in_bytes(), new_size, current_size, 329 gen_size_limit(), min_gen_size()); 330 331 if (new_size == current_size) { 332 // No change requested 333 return; 334 } 335 if (new_size > current_size) { 336 size_t change_bytes = new_size - current_size; 337 expand(change_bytes); 338 } else { 339 size_t change_bytes = current_size - new_size; 340 // shrink doesn't grab this lock, expand does. Is that right? 341 MutexLocker x(ExpandHeap_lock); 342 shrink(change_bytes); 343 } 344 345 log_trace(gc, ergo)("AdaptiveSizePolicy::old generation size: collection: %d (" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ", 346 ParallelScavengeHeap::heap()->total_collections(), 347 size_before, 348 virtual_space()->committed_size()); 349 } 350 351 // NOTE! We need to be careful about resizing. During a GC, multiple 352 // allocators may be active during heap expansion. If we allow the 353 // heap resizing to become visible before we have correctly resized 354 // all heap related data structures, we may cause program failures. 355 void PSOldGen::post_resize() { 356 // First construct a memregion representing the new size 357 MemRegion new_memregion((HeapWord*)virtual_space()->low(), 358 (HeapWord*)virtual_space()->high()); 359 size_t new_word_size = new_memregion.word_size(); 360 361 start_array()->set_covered_region(new_memregion); 362 ParallelScavengeHeap::heap()->card_table()->resize_covered_region(new_memregion); 363 364 // ALWAYS do this last!! 365 object_space()->initialize(new_memregion, 366 SpaceDecorator::DontClear, 367 SpaceDecorator::DontMangle); 368 369 assert(new_word_size == heap_word_size(object_space()->capacity_in_bytes()), 370 "Sanity"); 371 } 372 373 size_t PSOldGen::gen_size_limit() { 374 return _max_gen_size; 375 } 376 377 void PSOldGen::reset_after_change() { 378 ShouldNotReachHere(); 379 return; 380 } 381 382 size_t PSOldGen::available_for_expansion() { 383 ShouldNotReachHere(); 384 return 0; 385 } 386 387 size_t PSOldGen::available_for_contraction() { 388 ShouldNotReachHere(); 389 return 0; 390 } 391 392 void PSOldGen::print() const { print_on(tty);} 393 void PSOldGen::print_on(outputStream* st) const { 394 st->print(" %-15s", name()); 395 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", 396 capacity_in_bytes()/K, used_in_bytes()/K); 397 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", 398 p2i(virtual_space()->low_boundary()), 399 p2i(virtual_space()->high()), 400 p2i(virtual_space()->high_boundary())); 401 402 st->print(" object"); object_space()->print_on(st); 403 } 404 405 void PSOldGen::update_counters() { 406 if (UsePerfData) { 407 _space_counters->update_all(); 408 _gen_counters->update_all(); 409 } 410 } 411 412 #ifndef PRODUCT 413 414 void PSOldGen::space_invariants() { 415 assert(object_space()->end() == (HeapWord*) virtual_space()->high(), 416 "Space invariant"); 417 assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(), 418 "Space invariant"); 419 assert(virtual_space()->low_boundary() <= virtual_space()->low(), 420 "Space invariant"); 421 assert(virtual_space()->high_boundary() >= virtual_space()->high(), 422 "Space invariant"); 423 assert(virtual_space()->low_boundary() == (char*) _reserved.start(), 424 "Space invariant"); 425 assert(virtual_space()->high_boundary() == (char*) _reserved.end(), 426 "Space invariant"); 427 assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(), 428 "Space invariant"); 429 } 430 #endif 431 432 void PSOldGen::verify() { 433 object_space()->verify(); 434 } 435 class VerifyObjectStartArrayClosure : public ObjectClosure { 436 PSOldGen* _old_gen; 437 ObjectStartArray* _start_array; 438 439 public: 440 VerifyObjectStartArrayClosure(PSOldGen* old_gen, ObjectStartArray* start_array) : 441 _old_gen(old_gen), _start_array(start_array) { } 442 443 virtual void do_object(oop obj) { 444 HeapWord* test_addr = cast_from_oop<HeapWord*>(obj) + 1; 445 guarantee(_start_array->object_start(test_addr) == cast_from_oop<HeapWord*>(obj), "ObjectStartArray cannot find start of object"); 446 guarantee(_start_array->is_block_allocated(cast_from_oop<HeapWord*>(obj)), "ObjectStartArray missing block allocation"); 447 } 448 }; 449 450 void PSOldGen::verify_object_start_array() { 451 VerifyObjectStartArrayClosure check( this, &_start_array ); 452 object_iterate(&check); 453 } 454 455 #ifndef PRODUCT 456 void PSOldGen::record_spaces_top() { 457 assert(ZapUnusedHeapArea, "Not mangling unused space"); 458 object_space()->set_top_for_allocations(); 459 } 460 #endif