1 /* 2 * Copyright (c) 1997, 2014, 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_implementation/shared/gcTimer.hpp" 27 #include "gc_implementation/shared/gcTrace.hpp" 28 #include "gc_implementation/shared/spaceDecorator.hpp" 29 #include "gc_interface/collectedHeap.inline.hpp" 30 #include "memory/allocation.inline.hpp" 31 #include "memory/blockOffsetTable.inline.hpp" 32 #include "memory/cardTableRS.hpp" 33 #include "memory/gcLocker.inline.hpp" 34 #include "memory/genCollectedHeap.hpp" 35 #include "memory/genMarkSweep.hpp" 36 #include "memory/genOopClosures.hpp" 37 #include "memory/genOopClosures.inline.hpp" 38 #include "memory/generation.hpp" 39 #include "memory/space.inline.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "runtime/java.hpp" 42 #include "utilities/copy.hpp" 43 #include "utilities/events.hpp" 44 45 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 46 47 Generation::Generation(ReservedSpace rs, size_t initial_size, int level) : 48 _level(level), 49 _ref_processor(NULL) { 50 if (!_virtual_space.initialize(rs, initial_size)) { 51 vm_exit_during_initialization("Could not reserve enough space for " 52 "object heap"); 53 } 54 // Mangle all of the the initial generation. 55 if (ZapUnusedHeapArea) { 56 MemRegion mangle_region((HeapWord*)_virtual_space.low(), 57 (HeapWord*)_virtual_space.high()); 58 SpaceMangler::mangle_region(mangle_region); 59 } 60 _reserved = MemRegion((HeapWord*)_virtual_space.low_boundary(), 61 (HeapWord*)_virtual_space.high_boundary()); 62 } 63 64 GenerationSpec* Generation::spec() { 65 GenCollectedHeap* gch = GenCollectedHeap::heap(); 66 assert(0 <= level() && level() < gch->_n_gens, "Bad gen level"); 67 return gch->_gen_specs[level()]; 68 } 69 70 size_t Generation::max_capacity() const { 71 return reserved().byte_size(); 72 } 73 74 void Generation::print_heap_change(size_t prev_used) const { 75 if (PrintGCDetails && Verbose) { 76 gclog_or_tty->print(" " SIZE_FORMAT 77 "->" SIZE_FORMAT 78 "(" SIZE_FORMAT ")", 79 prev_used, used(), capacity()); 80 } else { 81 gclog_or_tty->print(" " SIZE_FORMAT "K" 82 "->" SIZE_FORMAT "K" 83 "(" SIZE_FORMAT "K)", 84 prev_used / K, used() / K, capacity() / K); 85 } 86 } 87 88 // By default we get a single threaded default reference processor; 89 // generations needing multi-threaded refs processing or discovery override this method. 90 void Generation::ref_processor_init() { 91 assert(_ref_processor == NULL, "a reference processor already exists"); 92 assert(!_reserved.is_empty(), "empty generation?"); 93 _ref_processor = new ReferenceProcessor(_reserved); // a vanilla reference processor 94 if (_ref_processor == NULL) { 95 vm_exit_during_initialization("Could not allocate ReferenceProcessor object"); 96 } 97 } 98 99 void Generation::print() const { print_on(tty); } 100 101 void Generation::print_on(outputStream* st) const { 102 st->print(" %-20s", name()); 103 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", 104 capacity()/K, used()/K); 105 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", 106 _virtual_space.low_boundary(), 107 _virtual_space.high(), 108 _virtual_space.high_boundary()); 109 } 110 111 void Generation::print_summary_info() { print_summary_info_on(tty); } 112 113 void Generation::print_summary_info_on(outputStream* st) { 114 StatRecord* sr = stat_record(); 115 double time = sr->accumulated_time.seconds(); 116 st->print_cr("[Accumulated GC generation %d time %3.7f secs, " 117 "%d GC's, avg GC time %3.7f]", 118 level(), time, sr->invocations, 119 sr->invocations > 0 ? time / sr->invocations : 0.0); 120 } 121 122 // Utility iterator classes 123 124 class GenerationIsInReservedClosure : public SpaceClosure { 125 public: 126 const void* _p; 127 Space* sp; 128 virtual void do_space(Space* s) { 129 if (sp == NULL) { 130 if (s->is_in_reserved(_p)) sp = s; 131 } 132 } 133 GenerationIsInReservedClosure(const void* p) : _p(p), sp(NULL) {} 134 }; 135 136 class GenerationIsInClosure : public SpaceClosure { 137 public: 138 const void* _p; 139 Space* sp; 140 virtual void do_space(Space* s) { 141 if (sp == NULL) { 142 if (s->is_in(_p)) sp = s; 143 } 144 } 145 GenerationIsInClosure(const void* p) : _p(p), sp(NULL) {} 146 }; 147 148 bool Generation::is_in(const void* p) const { 149 GenerationIsInClosure blk(p); 150 ((Generation*)this)->space_iterate(&blk); 151 return blk.sp != NULL; 152 } 153 154 DefNewGeneration* Generation::as_DefNewGeneration() { 155 assert((kind() == Generation::DefNew) || 156 (kind() == Generation::ParNew), 157 "Wrong youngest generation type"); 158 return (DefNewGeneration*) this; 159 } 160 161 Generation* Generation::next_gen() const { 162 GenCollectedHeap* gch = GenCollectedHeap::heap(); 163 int next = level() + 1; 164 if (next < gch->_n_gens) { 165 return gch->_gens[next]; 166 } else { 167 return NULL; 168 } 169 } 170 171 size_t Generation::max_contiguous_available() const { 172 // The largest number of contiguous free words in this or any higher generation. 173 size_t max = 0; 174 for (const Generation* gen = this; gen != NULL; gen = gen->next_gen()) { 175 size_t avail = gen->contiguous_available(); 176 if (avail > max) { 177 max = avail; 178 } 179 } 180 return max; 181 } 182 183 bool Generation::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const { 184 size_t available = max_contiguous_available(); 185 bool res = (available >= max_promotion_in_bytes); 186 if (PrintGC && Verbose) { 187 gclog_or_tty->print_cr( 188 "Generation: promo attempt is%s safe: available("SIZE_FORMAT") %s max_promo("SIZE_FORMAT")", 189 res? "":" not", available, res? ">=":"<", 190 max_promotion_in_bytes); 191 } 192 return res; 193 } 194 195 // Ignores "ref" and calls allocate(). 196 oop Generation::promote(oop obj, size_t obj_size) { 197 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in"); 198 199 #ifndef PRODUCT 200 if (Universe::heap()->promotion_should_fail()) { 201 return NULL; 202 } 203 #endif // #ifndef PRODUCT 204 205 HeapWord* result = allocate(obj_size, false); 206 if (result != NULL) { 207 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size); 208 return oop(result); 209 } else { 210 GenCollectedHeap* gch = GenCollectedHeap::heap(); 211 return gch->handle_failed_promotion(this, obj, obj_size); 212 } 213 } 214 215 oop Generation::par_promote(int thread_num, 216 oop obj, markOop m, size_t word_sz) { 217 // Could do a bad general impl here that gets a lock. But no. 218 ShouldNotCallThis(); 219 return NULL; 220 } 221 222 Space* Generation::space_containing(const void* p) const { 223 GenerationIsInReservedClosure blk(p); 224 // Cast away const 225 ((Generation*)this)->space_iterate(&blk); 226 return blk.sp; 227 } 228 229 // Some of these are mediocre general implementations. Should be 230 // overridden to get better performance. 231 232 class GenerationBlockStartClosure : public SpaceClosure { 233 public: 234 const void* _p; 235 HeapWord* _start; 236 virtual void do_space(Space* s) { 237 if (_start == NULL && s->is_in_reserved(_p)) { 238 _start = s->block_start(_p); 239 } 240 } 241 GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; } 242 }; 243 244 HeapWord* Generation::block_start(const void* p) const { 245 GenerationBlockStartClosure blk(p); 246 // Cast away const 247 ((Generation*)this)->space_iterate(&blk); 248 return blk._start; 249 } 250 251 class GenerationBlockSizeClosure : public SpaceClosure { 252 public: 253 const HeapWord* _p; 254 size_t size; 255 virtual void do_space(Space* s) { 256 if (size == 0 && s->is_in_reserved(_p)) { 257 size = s->block_size(_p); 258 } 259 } 260 GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; } 261 }; 262 263 size_t Generation::block_size(const HeapWord* p) const { 264 GenerationBlockSizeClosure blk(p); 265 // Cast away const 266 ((Generation*)this)->space_iterate(&blk); 267 assert(blk.size > 0, "seems reasonable"); 268 return blk.size; 269 } 270 271 class GenerationBlockIsObjClosure : public SpaceClosure { 272 public: 273 const HeapWord* _p; 274 bool is_obj; 275 virtual void do_space(Space* s) { 276 if (!is_obj && s->is_in_reserved(_p)) { 277 is_obj |= s->block_is_obj(_p); 278 } 279 } 280 GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; } 281 }; 282 283 bool Generation::block_is_obj(const HeapWord* p) const { 284 GenerationBlockIsObjClosure blk(p); 285 // Cast away const 286 ((Generation*)this)->space_iterate(&blk); 287 return blk.is_obj; 288 } 289 290 class GenerationOopIterateClosure : public SpaceClosure { 291 public: 292 ExtendedOopClosure* _cl; 293 virtual void do_space(Space* s) { 294 s->oop_iterate(_cl); 295 } 296 GenerationOopIterateClosure(ExtendedOopClosure* cl) : 297 _cl(cl) {} 298 }; 299 300 void Generation::oop_iterate(ExtendedOopClosure* cl) { 301 GenerationOopIterateClosure blk(cl); 302 space_iterate(&blk); 303 } 304 305 void Generation::younger_refs_in_space_iterate(Space* sp, 306 OopsInGenClosure* cl) { 307 GenRemSet* rs = SharedHeap::heap()->rem_set(); 308 rs->younger_refs_in_space_iterate(sp, cl); 309 } 310 311 class GenerationObjIterateClosure : public SpaceClosure { 312 private: 313 ObjectClosure* _cl; 314 public: 315 virtual void do_space(Space* s) { 316 s->object_iterate(_cl); 317 } 318 GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} 319 }; 320 321 void Generation::object_iterate(ObjectClosure* cl) { 322 GenerationObjIterateClosure blk(cl); 323 space_iterate(&blk); 324 } 325 326 class GenerationSafeObjIterateClosure : public SpaceClosure { 327 private: 328 ObjectClosure* _cl; 329 public: 330 virtual void do_space(Space* s) { 331 s->safe_object_iterate(_cl); 332 } 333 GenerationSafeObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} 334 }; 335 336 void Generation::safe_object_iterate(ObjectClosure* cl) { 337 GenerationSafeObjIterateClosure blk(cl); 338 space_iterate(&blk); 339 } 340 341 void Generation::prepare_for_compaction(CompactPoint* cp) { 342 // Generic implementation, can be specialized 343 CompactibleSpace* space = first_compaction_space(); 344 while (space != NULL) { 345 space->prepare_for_compaction(cp); 346 space = space->next_compaction_space(); 347 } 348 } 349 350 class AdjustPointersClosure: public SpaceClosure { 351 public: 352 void do_space(Space* sp) { 353 sp->adjust_pointers(); 354 } 355 }; 356 357 void Generation::adjust_pointers() { 358 // Note that this is done over all spaces, not just the compactible 359 // ones. 360 AdjustPointersClosure blk; 361 space_iterate(&blk, true); 362 } 363 364 void Generation::compact() { 365 CompactibleSpace* sp = first_compaction_space(); 366 while (sp != NULL) { 367 sp->compact(); 368 sp = sp->next_compaction_space(); 369 } 370 } 371 372 CardGeneration::CardGeneration(ReservedSpace rs, size_t initial_byte_size, 373 int level, 374 GenRemSet* remset) : 375 Generation(rs, initial_byte_size, level), _rs(remset), 376 _shrink_factor(0), _min_heap_delta_bytes(), _capacity_at_prologue(), 377 _used_at_prologue() 378 { 379 HeapWord* start = (HeapWord*)rs.base(); 380 size_t reserved_byte_size = rs.size(); 381 assert((uintptr_t(start) & 3) == 0, "bad alignment"); 382 assert((reserved_byte_size & 3) == 0, "bad alignment"); 383 MemRegion reserved_mr(start, heap_word_size(reserved_byte_size)); 384 _bts = new BlockOffsetSharedArray(reserved_mr, 385 heap_word_size(initial_byte_size)); 386 MemRegion committed_mr(start, heap_word_size(initial_byte_size)); 387 _rs->resize_covered_region(committed_mr); 388 if (_bts == NULL) 389 vm_exit_during_initialization("Could not allocate a BlockOffsetArray"); 390 391 // Verify that the start and end of this generation is the start of a card. 392 // If this wasn't true, a single card could span more than on generation, 393 // which would cause problems when we commit/uncommit memory, and when we 394 // clear and dirty cards. 395 guarantee(_rs->is_aligned(reserved_mr.start()), "generation must be card aligned"); 396 if (reserved_mr.end() != Universe::heap()->reserved_region().end()) { 397 // Don't check at the very end of the heap as we'll assert that we're probing off 398 // the end if we try. 399 guarantee(_rs->is_aligned(reserved_mr.end()), "generation must be card aligned"); 400 } 401 _min_heap_delta_bytes = MinHeapDeltaBytes; 402 _capacity_at_prologue = initial_byte_size; 403 _used_at_prologue = 0; 404 } 405 406 bool CardGeneration::expand(size_t bytes, size_t expand_bytes) { 407 assert_locked_or_safepoint(Heap_lock); 408 if (bytes == 0) { 409 return true; // That's what grow_by(0) would return 410 } 411 size_t aligned_bytes = ReservedSpace::page_align_size_up(bytes); 412 if (aligned_bytes == 0){ 413 // The alignment caused the number of bytes to wrap. An expand_by(0) will 414 // return true with the implication that an expansion was done when it 415 // was not. A call to expand implies a best effort to expand by "bytes" 416 // but not a guarantee. Align down to give a best effort. This is likely 417 // the most that the generation can expand since it has some capacity to 418 // start with. 419 aligned_bytes = ReservedSpace::page_align_size_down(bytes); 420 } 421 size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes); 422 bool success = false; 423 if (aligned_expand_bytes > aligned_bytes) { 424 success = grow_by(aligned_expand_bytes); 425 } 426 if (!success) { 427 success = grow_by(aligned_bytes); 428 } 429 if (!success) { 430 success = grow_to_reserved(); 431 } 432 if (PrintGC && Verbose) { 433 if (success && GC_locker::is_active_and_needs_gc()) { 434 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); 435 } 436 } 437 438 return success; 439 } 440 441 442 // No young generation references, clear this generation's cards. 443 void CardGeneration::clear_remembered_set() { 444 _rs->clear(reserved()); 445 } 446 447 448 // Objects in this generation may have moved, invalidate this 449 // generation's cards. 450 void CardGeneration::invalidate_remembered_set() { 451 _rs->invalidate(used_region()); 452 } 453 454 455 void CardGeneration::compute_new_size() { 456 assert(_shrink_factor <= 100, "invalid shrink factor"); 457 size_t current_shrink_factor = _shrink_factor; 458 _shrink_factor = 0; 459 460 // We don't have floating point command-line arguments 461 // Note: argument processing ensures that MinHeapFreeRatio < 100. 462 const double minimum_free_percentage = MinHeapFreeRatio / 100.0; 463 const double maximum_used_percentage = 1.0 - minimum_free_percentage; 464 465 // Compute some numbers about the state of the heap. 466 const size_t used_after_gc = used(); 467 const size_t capacity_after_gc = capacity(); 468 469 const double min_tmp = used_after_gc / maximum_used_percentage; 470 size_t minimum_desired_capacity = (size_t)MIN2(min_tmp, double(max_uintx)); 471 // Don't shrink less than the initial generation size 472 minimum_desired_capacity = MAX2(minimum_desired_capacity, 473 spec()->init_size()); 474 assert(used_after_gc <= minimum_desired_capacity, "sanity check"); 475 476 if (PrintGC && Verbose) { 477 const size_t free_after_gc = free(); 478 const double free_percentage = ((double)free_after_gc) / capacity_after_gc; 479 gclog_or_tty->print_cr("TenuredGeneration::compute_new_size: "); 480 gclog_or_tty->print_cr(" " 481 " minimum_free_percentage: %6.2f" 482 " maximum_used_percentage: %6.2f", 483 minimum_free_percentage, 484 maximum_used_percentage); 485 gclog_or_tty->print_cr(" " 486 " free_after_gc : %6.1fK" 487 " used_after_gc : %6.1fK" 488 " capacity_after_gc : %6.1fK", 489 free_after_gc / (double) K, 490 used_after_gc / (double) K, 491 capacity_after_gc / (double) K); 492 gclog_or_tty->print_cr(" " 493 " free_percentage: %6.2f", 494 free_percentage); 495 } 496 497 if (capacity_after_gc < minimum_desired_capacity) { 498 // If we have less free space than we want then expand 499 size_t expand_bytes = minimum_desired_capacity - capacity_after_gc; 500 // Don't expand unless it's significant 501 if (expand_bytes >= _min_heap_delta_bytes) { 502 expand(expand_bytes, 0); // safe if expansion fails 503 } 504 if (PrintGC && Verbose) { 505 gclog_or_tty->print_cr(" expanding:" 506 " minimum_desired_capacity: %6.1fK" 507 " expand_bytes: %6.1fK" 508 " _min_heap_delta_bytes: %6.1fK", 509 minimum_desired_capacity / (double) K, 510 expand_bytes / (double) K, 511 _min_heap_delta_bytes / (double) K); 512 } 513 return; 514 } 515 516 // No expansion, now see if we want to shrink 517 size_t shrink_bytes = 0; 518 // We would never want to shrink more than this 519 size_t max_shrink_bytes = capacity_after_gc - minimum_desired_capacity; 520 521 if (MaxHeapFreeRatio < 100) { 522 const double maximum_free_percentage = MaxHeapFreeRatio / 100.0; 523 const double minimum_used_percentage = 1.0 - maximum_free_percentage; 524 const double max_tmp = used_after_gc / minimum_used_percentage; 525 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); 526 maximum_desired_capacity = MAX2(maximum_desired_capacity, 527 spec()->init_size()); 528 if (PrintGC && Verbose) { 529 gclog_or_tty->print_cr(" " 530 " maximum_free_percentage: %6.2f" 531 " minimum_used_percentage: %6.2f", 532 maximum_free_percentage, 533 minimum_used_percentage); 534 gclog_or_tty->print_cr(" " 535 " _capacity_at_prologue: %6.1fK" 536 " minimum_desired_capacity: %6.1fK" 537 " maximum_desired_capacity: %6.1fK", 538 _capacity_at_prologue / (double) K, 539 minimum_desired_capacity / (double) K, 540 maximum_desired_capacity / (double) K); 541 } 542 assert(minimum_desired_capacity <= maximum_desired_capacity, 543 "sanity check"); 544 545 if (capacity_after_gc > maximum_desired_capacity) { 546 // Capacity too large, compute shrinking size 547 shrink_bytes = capacity_after_gc - maximum_desired_capacity; 548 // We don't want shrink all the way back to initSize if people call 549 // System.gc(), because some programs do that between "phases" and then 550 // we'd just have to grow the heap up again for the next phase. So we 551 // damp the shrinking: 0% on the first call, 10% on the second call, 40% 552 // on the third call, and 100% by the fourth call. But if we recompute 553 // size without shrinking, it goes back to 0%. 554 shrink_bytes = shrink_bytes / 100 * current_shrink_factor; 555 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); 556 if (current_shrink_factor == 0) { 557 _shrink_factor = 10; 558 } else { 559 _shrink_factor = MIN2(current_shrink_factor * 4, (size_t) 100); 560 } 561 if (PrintGC && Verbose) { 562 gclog_or_tty->print_cr(" " 563 " shrinking:" 564 " initSize: %.1fK" 565 " maximum_desired_capacity: %.1fK", 566 spec()->init_size() / (double) K, 567 maximum_desired_capacity / (double) K); 568 gclog_or_tty->print_cr(" " 569 " shrink_bytes: %.1fK" 570 " current_shrink_factor: " SIZE_FORMAT 571 " new shrink factor: " SIZE_FORMAT 572 " _min_heap_delta_bytes: %.1fK", 573 shrink_bytes / (double) K, 574 current_shrink_factor, 575 _shrink_factor, 576 _min_heap_delta_bytes / (double) K); 577 } 578 } 579 } 580 581 if (capacity_after_gc > _capacity_at_prologue) { 582 // We might have expanded for promotions, in which case we might want to 583 // take back that expansion if there's room after GC. That keeps us from 584 // stretching the heap with promotions when there's plenty of room. 585 size_t expansion_for_promotion = capacity_after_gc - _capacity_at_prologue; 586 expansion_for_promotion = MIN2(expansion_for_promotion, max_shrink_bytes); 587 // We have two shrinking computations, take the largest 588 shrink_bytes = MAX2(shrink_bytes, expansion_for_promotion); 589 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); 590 if (PrintGC && Verbose) { 591 gclog_or_tty->print_cr(" " 592 " aggressive shrinking:" 593 " _capacity_at_prologue: %.1fK" 594 " capacity_after_gc: %.1fK" 595 " expansion_for_promotion: %.1fK" 596 " shrink_bytes: %.1fK", 597 capacity_after_gc / (double) K, 598 _capacity_at_prologue / (double) K, 599 expansion_for_promotion / (double) K, 600 shrink_bytes / (double) K); 601 } 602 } 603 // Don't shrink unless it's significant 604 if (shrink_bytes >= _min_heap_delta_bytes) { 605 shrink(shrink_bytes); 606 } 607 } 608 609 // Currently nothing to do. 610 void CardGeneration::prepare_for_verify() {} 611