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 Generation* Generation::next_gen() const { 155 GenCollectedHeap* gch = GenCollectedHeap::heap(); 156 int next = level() + 1; 157 if (next < gch->_n_gens) { 158 return gch->_gens[next]; 159 } else { 160 return NULL; 161 } 162 } 163 164 size_t Generation::max_contiguous_available() const { 165 // The largest number of contiguous free words in this or any higher generation. 166 size_t max = 0; 167 for (const Generation* gen = this; gen != NULL; gen = gen->next_gen()) { 168 size_t avail = gen->contiguous_available(); 169 if (avail > max) { 170 max = avail; 171 } 172 } 173 return max; 174 } 175 176 bool Generation::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const { 177 size_t available = max_contiguous_available(); 178 bool res = (available >= max_promotion_in_bytes); 179 if (PrintGC && Verbose) { 180 gclog_or_tty->print_cr( 181 "Generation: promo attempt is%s safe: available("SIZE_FORMAT") %s max_promo("SIZE_FORMAT")", 182 res? "":" not", available, res? ">=":"<", 183 max_promotion_in_bytes); 184 } 185 return res; 186 } 187 188 // Ignores "ref" and calls allocate(). 189 oop Generation::promote(oop obj, size_t obj_size) { 190 assert(obj_size == (size_t)obj->size(), "bad obj_size passed in"); 191 192 #ifndef PRODUCT 193 if (Universe::heap()->promotion_should_fail()) { 194 return NULL; 195 } 196 #endif // #ifndef PRODUCT 197 198 HeapWord* result = allocate(obj_size, false); 199 if (result != NULL) { 200 Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size); 201 return oop(result); 202 } else { 203 GenCollectedHeap* gch = GenCollectedHeap::heap(); 204 return gch->handle_failed_promotion(this, obj, obj_size); 205 } 206 } 207 208 oop Generation::par_promote(int thread_num, 209 oop obj, markOop m, size_t word_sz) { 210 // Could do a bad general impl here that gets a lock. But no. 211 ShouldNotCallThis(); 212 return NULL; 213 } 214 215 Space* Generation::space_containing(const void* p) const { 216 GenerationIsInReservedClosure blk(p); 217 // Cast away const 218 ((Generation*)this)->space_iterate(&blk); 219 return blk.sp; 220 } 221 222 // Some of these are mediocre general implementations. Should be 223 // overridden to get better performance. 224 225 class GenerationBlockStartClosure : public SpaceClosure { 226 public: 227 const void* _p; 228 HeapWord* _start; 229 virtual void do_space(Space* s) { 230 if (_start == NULL && s->is_in_reserved(_p)) { 231 _start = s->block_start(_p); 232 } 233 } 234 GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; } 235 }; 236 237 HeapWord* Generation::block_start(const void* p) const { 238 GenerationBlockStartClosure blk(p); 239 // Cast away const 240 ((Generation*)this)->space_iterate(&blk); 241 return blk._start; 242 } 243 244 class GenerationBlockSizeClosure : public SpaceClosure { 245 public: 246 const HeapWord* _p; 247 size_t size; 248 virtual void do_space(Space* s) { 249 if (size == 0 && s->is_in_reserved(_p)) { 250 size = s->block_size(_p); 251 } 252 } 253 GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; } 254 }; 255 256 size_t Generation::block_size(const HeapWord* p) const { 257 GenerationBlockSizeClosure blk(p); 258 // Cast away const 259 ((Generation*)this)->space_iterate(&blk); 260 assert(blk.size > 0, "seems reasonable"); 261 return blk.size; 262 } 263 264 class GenerationBlockIsObjClosure : public SpaceClosure { 265 public: 266 const HeapWord* _p; 267 bool is_obj; 268 virtual void do_space(Space* s) { 269 if (!is_obj && s->is_in_reserved(_p)) { 270 is_obj |= s->block_is_obj(_p); 271 } 272 } 273 GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; } 274 }; 275 276 bool Generation::block_is_obj(const HeapWord* p) const { 277 GenerationBlockIsObjClosure blk(p); 278 // Cast away const 279 ((Generation*)this)->space_iterate(&blk); 280 return blk.is_obj; 281 } 282 283 class GenerationOopIterateClosure : public SpaceClosure { 284 public: 285 ExtendedOopClosure* _cl; 286 virtual void do_space(Space* s) { 287 s->oop_iterate(_cl); 288 } 289 GenerationOopIterateClosure(ExtendedOopClosure* cl) : 290 _cl(cl) {} 291 }; 292 293 void Generation::oop_iterate(ExtendedOopClosure* cl) { 294 GenerationOopIterateClosure blk(cl); 295 space_iterate(&blk); 296 } 297 298 void Generation::younger_refs_in_space_iterate(Space* sp, 299 OopsInGenClosure* cl) { 300 GenRemSet* rs = SharedHeap::heap()->rem_set(); 301 rs->younger_refs_in_space_iterate(sp, cl); 302 } 303 304 class GenerationObjIterateClosure : public SpaceClosure { 305 private: 306 ObjectClosure* _cl; 307 public: 308 virtual void do_space(Space* s) { 309 s->object_iterate(_cl); 310 } 311 GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} 312 }; 313 314 void Generation::object_iterate(ObjectClosure* cl) { 315 GenerationObjIterateClosure blk(cl); 316 space_iterate(&blk); 317 } 318 319 class GenerationSafeObjIterateClosure : public SpaceClosure { 320 private: 321 ObjectClosure* _cl; 322 public: 323 virtual void do_space(Space* s) { 324 s->safe_object_iterate(_cl); 325 } 326 GenerationSafeObjIterateClosure(ObjectClosure* cl) : _cl(cl) {} 327 }; 328 329 void Generation::safe_object_iterate(ObjectClosure* cl) { 330 GenerationSafeObjIterateClosure blk(cl); 331 space_iterate(&blk); 332 } 333 334 void Generation::prepare_for_compaction(CompactPoint* cp) { 335 // Generic implementation, can be specialized 336 CompactibleSpace* space = first_compaction_space(); 337 while (space != NULL) { 338 space->prepare_for_compaction(cp); 339 space = space->next_compaction_space(); 340 } 341 } 342 343 class AdjustPointersClosure: public SpaceClosure { 344 public: 345 void do_space(Space* sp) { 346 sp->adjust_pointers(); 347 } 348 }; 349 350 void Generation::adjust_pointers() { 351 // Note that this is done over all spaces, not just the compactible 352 // ones. 353 AdjustPointersClosure blk; 354 space_iterate(&blk, true); 355 } 356 357 void Generation::compact() { 358 CompactibleSpace* sp = first_compaction_space(); 359 while (sp != NULL) { 360 sp->compact(); 361 sp = sp->next_compaction_space(); 362 } 363 } 364 365 CardGeneration::CardGeneration(ReservedSpace rs, size_t initial_byte_size, 366 int level, 367 GenRemSet* remset) : 368 Generation(rs, initial_byte_size, level), _rs(remset), 369 _shrink_factor(0), _min_heap_delta_bytes(), _capacity_at_prologue(), 370 _used_at_prologue() 371 { 372 HeapWord* start = (HeapWord*)rs.base(); 373 size_t reserved_byte_size = rs.size(); 374 assert((uintptr_t(start) & 3) == 0, "bad alignment"); 375 assert((reserved_byte_size & 3) == 0, "bad alignment"); 376 MemRegion reserved_mr(start, heap_word_size(reserved_byte_size)); 377 _bts = new BlockOffsetSharedArray(reserved_mr, 378 heap_word_size(initial_byte_size)); 379 MemRegion committed_mr(start, heap_word_size(initial_byte_size)); 380 _rs->resize_covered_region(committed_mr); 381 if (_bts == NULL) 382 vm_exit_during_initialization("Could not allocate a BlockOffsetArray"); 383 384 // Verify that the start and end of this generation is the start of a card. 385 // If this wasn't true, a single card could span more than on generation, 386 // which would cause problems when we commit/uncommit memory, and when we 387 // clear and dirty cards. 388 guarantee(_rs->is_aligned(reserved_mr.start()), "generation must be card aligned"); 389 if (reserved_mr.end() != Universe::heap()->reserved_region().end()) { 390 // Don't check at the very end of the heap as we'll assert that we're probing off 391 // the end if we try. 392 guarantee(_rs->is_aligned(reserved_mr.end()), "generation must be card aligned"); 393 } 394 _min_heap_delta_bytes = MinHeapDeltaBytes; 395 _capacity_at_prologue = initial_byte_size; 396 _used_at_prologue = 0; 397 } 398 399 bool CardGeneration::expand(size_t bytes, size_t expand_bytes) { 400 assert_locked_or_safepoint(Heap_lock); 401 if (bytes == 0) { 402 return true; // That's what grow_by(0) would return 403 } 404 size_t aligned_bytes = ReservedSpace::page_align_size_up(bytes); 405 if (aligned_bytes == 0){ 406 // The alignment caused the number of bytes to wrap. An expand_by(0) will 407 // return true with the implication that an expansion was done when it 408 // was not. A call to expand implies a best effort to expand by "bytes" 409 // but not a guarantee. Align down to give a best effort. This is likely 410 // the most that the generation can expand since it has some capacity to 411 // start with. 412 aligned_bytes = ReservedSpace::page_align_size_down(bytes); 413 } 414 size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes); 415 bool success = false; 416 if (aligned_expand_bytes > aligned_bytes) { 417 success = grow_by(aligned_expand_bytes); 418 } 419 if (!success) { 420 success = grow_by(aligned_bytes); 421 } 422 if (!success) { 423 success = grow_to_reserved(); 424 } 425 if (PrintGC && Verbose) { 426 if (success && GC_locker::is_active_and_needs_gc()) { 427 gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); 428 } 429 } 430 431 return success; 432 } 433 434 435 // No young generation references, clear this generation's cards. 436 void CardGeneration::clear_remembered_set() { 437 _rs->clear(reserved()); 438 } 439 440 441 // Objects in this generation may have moved, invalidate this 442 // generation's cards. 443 void CardGeneration::invalidate_remembered_set() { 444 _rs->invalidate(used_region()); 445 } 446 447 448 void CardGeneration::compute_new_size() { 449 assert(_shrink_factor <= 100, "invalid shrink factor"); 450 size_t current_shrink_factor = _shrink_factor; 451 _shrink_factor = 0; 452 453 // We don't have floating point command-line arguments 454 // Note: argument processing ensures that MinHeapFreeRatio < 100. 455 const double minimum_free_percentage = MinHeapFreeRatio / 100.0; 456 const double maximum_used_percentage = 1.0 - minimum_free_percentage; 457 458 // Compute some numbers about the state of the heap. 459 const size_t used_after_gc = used(); 460 const size_t capacity_after_gc = capacity(); 461 462 const double min_tmp = used_after_gc / maximum_used_percentage; 463 size_t minimum_desired_capacity = (size_t)MIN2(min_tmp, double(max_uintx)); 464 // Don't shrink less than the initial generation size 465 minimum_desired_capacity = MAX2(minimum_desired_capacity, 466 spec()->init_size()); 467 assert(used_after_gc <= minimum_desired_capacity, "sanity check"); 468 469 if (PrintGC && Verbose) { 470 const size_t free_after_gc = free(); 471 const double free_percentage = ((double)free_after_gc) / capacity_after_gc; 472 gclog_or_tty->print_cr("TenuredGeneration::compute_new_size: "); 473 gclog_or_tty->print_cr(" " 474 " minimum_free_percentage: %6.2f" 475 " maximum_used_percentage: %6.2f", 476 minimum_free_percentage, 477 maximum_used_percentage); 478 gclog_or_tty->print_cr(" " 479 " free_after_gc : %6.1fK" 480 " used_after_gc : %6.1fK" 481 " capacity_after_gc : %6.1fK", 482 free_after_gc / (double) K, 483 used_after_gc / (double) K, 484 capacity_after_gc / (double) K); 485 gclog_or_tty->print_cr(" " 486 " free_percentage: %6.2f", 487 free_percentage); 488 } 489 490 if (capacity_after_gc < minimum_desired_capacity) { 491 // If we have less free space than we want then expand 492 size_t expand_bytes = minimum_desired_capacity - capacity_after_gc; 493 // Don't expand unless it's significant 494 if (expand_bytes >= _min_heap_delta_bytes) { 495 expand(expand_bytes, 0); // safe if expansion fails 496 } 497 if (PrintGC && Verbose) { 498 gclog_or_tty->print_cr(" expanding:" 499 " minimum_desired_capacity: %6.1fK" 500 " expand_bytes: %6.1fK" 501 " _min_heap_delta_bytes: %6.1fK", 502 minimum_desired_capacity / (double) K, 503 expand_bytes / (double) K, 504 _min_heap_delta_bytes / (double) K); 505 } 506 return; 507 } 508 509 // No expansion, now see if we want to shrink 510 size_t shrink_bytes = 0; 511 // We would never want to shrink more than this 512 size_t max_shrink_bytes = capacity_after_gc - minimum_desired_capacity; 513 514 if (MaxHeapFreeRatio < 100) { 515 const double maximum_free_percentage = MaxHeapFreeRatio / 100.0; 516 const double minimum_used_percentage = 1.0 - maximum_free_percentage; 517 const double max_tmp = used_after_gc / minimum_used_percentage; 518 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); 519 maximum_desired_capacity = MAX2(maximum_desired_capacity, 520 spec()->init_size()); 521 if (PrintGC && Verbose) { 522 gclog_or_tty->print_cr(" " 523 " maximum_free_percentage: %6.2f" 524 " minimum_used_percentage: %6.2f", 525 maximum_free_percentage, 526 minimum_used_percentage); 527 gclog_or_tty->print_cr(" " 528 " _capacity_at_prologue: %6.1fK" 529 " minimum_desired_capacity: %6.1fK" 530 " maximum_desired_capacity: %6.1fK", 531 _capacity_at_prologue / (double) K, 532 minimum_desired_capacity / (double) K, 533 maximum_desired_capacity / (double) K); 534 } 535 assert(minimum_desired_capacity <= maximum_desired_capacity, 536 "sanity check"); 537 538 if (capacity_after_gc > maximum_desired_capacity) { 539 // Capacity too large, compute shrinking size 540 shrink_bytes = capacity_after_gc - maximum_desired_capacity; 541 // We don't want shrink all the way back to initSize if people call 542 // System.gc(), because some programs do that between "phases" and then 543 // we'd just have to grow the heap up again for the next phase. So we 544 // damp the shrinking: 0% on the first call, 10% on the second call, 40% 545 // on the third call, and 100% by the fourth call. But if we recompute 546 // size without shrinking, it goes back to 0%. 547 shrink_bytes = shrink_bytes / 100 * current_shrink_factor; 548 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); 549 if (current_shrink_factor == 0) { 550 _shrink_factor = 10; 551 } else { 552 _shrink_factor = MIN2(current_shrink_factor * 4, (size_t) 100); 553 } 554 if (PrintGC && Verbose) { 555 gclog_or_tty->print_cr(" " 556 " shrinking:" 557 " initSize: %.1fK" 558 " maximum_desired_capacity: %.1fK", 559 spec()->init_size() / (double) K, 560 maximum_desired_capacity / (double) K); 561 gclog_or_tty->print_cr(" " 562 " shrink_bytes: %.1fK" 563 " current_shrink_factor: " SIZE_FORMAT 564 " new shrink factor: " SIZE_FORMAT 565 " _min_heap_delta_bytes: %.1fK", 566 shrink_bytes / (double) K, 567 current_shrink_factor, 568 _shrink_factor, 569 _min_heap_delta_bytes / (double) K); 570 } 571 } 572 } 573 574 if (capacity_after_gc > _capacity_at_prologue) { 575 // We might have expanded for promotions, in which case we might want to 576 // take back that expansion if there's room after GC. That keeps us from 577 // stretching the heap with promotions when there's plenty of room. 578 size_t expansion_for_promotion = capacity_after_gc - _capacity_at_prologue; 579 expansion_for_promotion = MIN2(expansion_for_promotion, max_shrink_bytes); 580 // We have two shrinking computations, take the largest 581 shrink_bytes = MAX2(shrink_bytes, expansion_for_promotion); 582 assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); 583 if (PrintGC && Verbose) { 584 gclog_or_tty->print_cr(" " 585 " aggressive shrinking:" 586 " _capacity_at_prologue: %.1fK" 587 " capacity_after_gc: %.1fK" 588 " expansion_for_promotion: %.1fK" 589 " shrink_bytes: %.1fK", 590 capacity_after_gc / (double) K, 591 _capacity_at_prologue / (double) K, 592 expansion_for_promotion / (double) K, 593 shrink_bytes / (double) K); 594 } 595 } 596 // Don't shrink unless it's significant 597 if (shrink_bytes >= _min_heap_delta_bytes) { 598 shrink(shrink_bytes); 599 } 600 } 601 602 // Currently nothing to do. 603 void CardGeneration::prepare_for_verify() {} 604