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