< prev index next >

src/share/vm/gc/parallel/psYoungGen.cpp

Print this page




  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/mutableNUMASpace.hpp"
  27 #include "gc/parallel/parallelScavengeHeap.hpp"
  28 #include "gc/parallel/psMarkSweepDecorator.hpp"
  29 #include "gc/parallel/psScavenge.hpp"
  30 #include "gc/parallel/psYoungGen.hpp"
  31 #include "gc/shared/gcUtil.hpp"
  32 #include "gc/shared/spaceDecorator.hpp"

  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/java.hpp"
  35 
  36 PSYoungGen::PSYoungGen(size_t        initial_size,
  37                        size_t        min_size,
  38                        size_t        max_size) :
  39   _init_gen_size(initial_size),
  40   _min_gen_size(min_size),
  41   _max_gen_size(max_size)
  42 {}
  43 
  44 void PSYoungGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) {
  45   assert(_init_gen_size != 0, "Should have a finite size");
  46   _virtual_space = new PSVirtualSpace(rs, alignment);
  47   if (!virtual_space()->expand_by(_init_gen_size)) {
  48     vm_exit_during_initialization("Could not reserve enough space for "
  49                                   "object heap");
  50   }
  51 }
  52 


 251   char* eden_top = (char*)eden_space()->top();
 252   char* from_top = (char*)from_space()->top();
 253   char* to_top = (char*)to_space()->top();
 254   assert(eden_top <= virtual_space()->high(), "eden top");
 255   assert(from_top <= virtual_space()->high(), "from top");
 256   assert(to_top <= virtual_space()->high(), "to top");
 257 
 258   virtual_space()->verify();
 259 }
 260 #endif
 261 
 262 void PSYoungGen::resize(size_t eden_size, size_t survivor_size) {
 263   // Resize the generation if needed. If the generation resize
 264   // reports false, do not attempt to resize the spaces.
 265   if (resize_generation(eden_size, survivor_size)) {
 266     // Then we lay out the spaces inside the generation
 267     resize_spaces(eden_size, survivor_size);
 268 
 269     space_invariants();
 270 
 271     if (PrintAdaptiveSizePolicy && Verbose) {
 272       gclog_or_tty->print_cr("Young generation size: "
 273         "desired eden: " SIZE_FORMAT " survivor: " SIZE_FORMAT
 274         " used: " SIZE_FORMAT " capacity: " SIZE_FORMAT
 275         " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
 276         eden_size, survivor_size, used_in_bytes(), capacity_in_bytes(),
 277         _max_gen_size, min_gen_size());
 278     }
 279   }
 280 }
 281 
 282 
 283 bool PSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) {
 284   const size_t alignment = virtual_space()->alignment();
 285   size_t orig_size = virtual_space()->committed_size();
 286   bool size_changed = false;
 287 
 288   // There used to be this guarantee there.
 289   // guarantee ((eden_size + 2*survivor_size)  <= _max_gen_size, "incorrect input arguments");
 290   // Code below forces this requirement.  In addition the desired eden
 291   // size and desired survivor sizes are desired goals and may
 292   // exceed the total generation size.
 293 
 294   assert(min_gen_size() <= orig_size && orig_size <= max_size(), "just checking");
 295 
 296   // Adjust new generation size
 297   const size_t eden_plus_survivors =
 298           align_size_up(eden_size + 2 * survivor_size, alignment);
 299   size_t desired_size = MAX2(MIN2(eden_plus_survivors, max_size()),


 313       // can be done here more simply that after the new
 314       // spaces have been computed.
 315       HeapWord* new_high = (HeapWord*) virtual_space()->high();
 316       MemRegion mangle_region(prev_high, new_high);
 317       SpaceMangler::mangle_region(mangle_region);
 318     }
 319     size_changed = true;
 320   } else if (desired_size < orig_size) {
 321     size_t desired_change = orig_size - desired_size;
 322     assert(desired_change % alignment == 0, "just checking");
 323 
 324     desired_change = limit_gen_shrink(desired_change);
 325 
 326     if (desired_change > 0) {
 327       virtual_space()->shrink_by(desired_change);
 328       reset_survivors_after_shrink();
 329 
 330       size_changed = true;
 331     }
 332   } else {
 333     if (Verbose && PrintGC) {
 334       if (orig_size == gen_size_limit()) {
 335         gclog_or_tty->print_cr("PSYoung generation size at maximum: "
 336           SIZE_FORMAT "K", orig_size/K);
 337       } else if (orig_size == min_gen_size()) {
 338         gclog_or_tty->print_cr("PSYoung generation size at minium: "
 339           SIZE_FORMAT "K", orig_size/K);
 340       }
 341     }
 342   }
 343 
 344   if (size_changed) {
 345     post_resize();
 346 
 347     if (Verbose && PrintGC) {
 348       size_t current_size  = virtual_space()->committed_size();
 349       gclog_or_tty->print_cr("PSYoung generation size changed: "
 350                              SIZE_FORMAT "K->" SIZE_FORMAT "K",
 351                              orig_size/K, current_size/K);
 352     }
 353   }
 354 
 355   guarantee(eden_plus_survivors <= virtual_space()->committed_size() ||
 356             virtual_space()->committed_size() == max_size(), "Sanity");
 357 
 358   return true;
 359 }
 360 
 361 #ifndef PRODUCT
 362 // In the numa case eden is not mangled so a survivor space
 363 // moving into a region previously occupied by a survivor
 364 // may find an unmangled region.  Also in the PS case eden
 365 // to-space and from-space may not touch (i.e., there may be
 366 // gaps between them due to movement while resizing the
 367 // spaces).  Those gaps must be mangled.
 368 void PSYoungGen::mangle_survivors(MutableSpace* s1,
 369                                   MemRegion s1MR,
 370                                   MutableSpace* s2,
 371                                   MemRegion s2MR) {
 372   // Check eden and gap between eden and from-space, in deciding


 395     s1->mangle_region(delta1_right);
 396   }
 397 
 398   // Similarly for the second survivor space except that
 399   // any of the new region that overlaps with the current
 400   // region of the first survivor space has already been
 401   // mangled.
 402   delta_end = MIN2(s2->bottom(), s2MR.end());
 403   delta_start = MAX2(s2MR.start(), s1->end());
 404   MemRegion delta2_left;
 405   if (s2MR.start() < delta_end) {
 406     delta2_left = MemRegion(s2MR.start(), delta_end);
 407     s2->mangle_region(delta2_left);
 408   }
 409   delta_start = MAX2(s2->end(), s2MR.start());
 410   MemRegion delta2_right;
 411   if (delta_start < s2MR.end()) {
 412     s2->mangle_region(delta2_right);
 413   }
 414 
 415   if (TraceZapUnusedHeapArea) {
 416     // s1
 417     gclog_or_tty->print_cr("Current region: [" PTR_FORMAT ", " PTR_FORMAT ") "
 418       "New region: [" PTR_FORMAT ", " PTR_FORMAT ")",
 419       p2i(s1->bottom()), p2i(s1->end()),
 420       p2i(s1MR.start()), p2i(s1MR.end()));
 421     gclog_or_tty->print_cr("    Mangle before: [" PTR_FORMAT ", "
 422       PTR_FORMAT ")  Mangle after: [" PTR_FORMAT ", " PTR_FORMAT ")",
 423       p2i(delta1_left.start()), p2i(delta1_left.end()),
 424       p2i(delta1_right.start()), p2i(delta1_right.end()));
 425 
 426     // s2
 427     gclog_or_tty->print_cr("Current region: [" PTR_FORMAT ", " PTR_FORMAT ") "
 428       "New region: [" PTR_FORMAT ", " PTR_FORMAT ")",
 429       p2i(s2->bottom()), p2i(s2->end()),
 430       p2i(s2MR.start()), p2i(s2MR.end()));
 431     gclog_or_tty->print_cr("    Mangle before: [" PTR_FORMAT ", "
 432       PTR_FORMAT ")  Mangle after: [" PTR_FORMAT ", " PTR_FORMAT ")",
 433       p2i(delta2_left.start()), p2i(delta2_left.end()),
 434       p2i(delta2_right.start()), p2i(delta2_right.end()));
 435   }
 436 
 437 }
 438 #endif // NOT PRODUCT
 439 
 440 void PSYoungGen::resize_spaces(size_t requested_eden_size,
 441                                size_t requested_survivor_size) {
 442   assert(UseAdaptiveSizePolicy, "sanity check");
 443   assert(requested_eden_size > 0  && requested_survivor_size > 0,
 444          "just checking");
 445 
 446   // We require eden and to space to be empty
 447   if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) {
 448     return;
 449   }
 450 
 451   if (PrintAdaptiveSizePolicy && Verbose) {
 452     gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: "
 453                   SIZE_FORMAT
 454                   ", requested_survivor_size: " SIZE_FORMAT ")",
 455                   requested_eden_size, requested_survivor_size);
 456     gclog_or_tty->print_cr("    eden: [" PTR_FORMAT ".." PTR_FORMAT ") "
 457                   SIZE_FORMAT,
 458                   p2i(eden_space()->bottom()),
 459                   p2i(eden_space()->end()),
 460                   pointer_delta(eden_space()->end(),
 461                                 eden_space()->bottom(),
 462                                 sizeof(char)));
 463     gclog_or_tty->print_cr("    from: [" PTR_FORMAT ".." PTR_FORMAT ") "
 464                   SIZE_FORMAT,
 465                   p2i(from_space()->bottom()),
 466                   p2i(from_space()->end()),
 467                   pointer_delta(from_space()->end(),
 468                                 from_space()->bottom(),
 469                                 sizeof(char)));
 470     gclog_or_tty->print_cr("      to: [" PTR_FORMAT ".." PTR_FORMAT ") "
 471                   SIZE_FORMAT,
 472                   p2i(to_space()->bottom()),
 473                   p2i(to_space()->end()),
 474                   pointer_delta(  to_space()->end(),
 475                                   to_space()->bottom(),
 476                                   sizeof(char)));
 477   }
 478 
 479   // There's nothing to do if the new sizes are the same as the current
 480   if (requested_survivor_size == to_space()->capacity_in_bytes() &&
 481       requested_survivor_size == from_space()->capacity_in_bytes() &&
 482       requested_eden_size == eden_space()->capacity_in_bytes()) {
 483     if (PrintAdaptiveSizePolicy && Verbose) {
 484       gclog_or_tty->print_cr("    capacities are the right sizes, returning");
 485     }
 486     return;
 487   }
 488 
 489   char* eden_start = (char*)eden_space()->bottom();
 490   char* eden_end   = (char*)eden_space()->end();
 491   char* from_start = (char*)from_space()->bottom();
 492   char* from_end   = (char*)from_space()->end();
 493   char* to_start   = (char*)to_space()->bottom();
 494   char* to_end     = (char*)to_space()->end();
 495 
 496   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 497   const size_t alignment = heap->space_alignment();
 498   const bool maintain_minimum =
 499     (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size();
 500 
 501   bool eden_from_to_order = from_start < to_start;
 502   // Check whether from space is below to space
 503   if (eden_from_to_order) {
 504     // Eden, from, to
 505     eden_from_to_order = true;
 506     if (PrintAdaptiveSizePolicy && Verbose) {
 507       gclog_or_tty->print_cr("  Eden, from, to:");
 508     }
 509 
 510     // Set eden
 511     // "requested_eden_size" is a goal for the size of eden
 512     // and may not be attainable.  "eden_size" below is
 513     // calculated based on the location of from-space and
 514     // the goal for the size of eden.  from-space is
 515     // fixed in place because it contains live data.
 516     // The calculation is done this way to avoid 32bit
 517     // overflow (i.e., eden_start + requested_eden_size
 518     // may too large for representation in 32bits).
 519     size_t eden_size;
 520     if (maintain_minimum) {
 521       // Only make eden larger than the requested size if
 522       // the minimum size of the generation has to be maintained.
 523       // This could be done in general but policy at a higher
 524       // level is determining a requested size for eden and that
 525       // should be honored unless there is a fundamental reason.
 526       eden_size = pointer_delta(from_start,
 527                                 eden_start,
 528                                 sizeof(char));


 549       size_t from_size = pointer_delta(from_space()->top(), from_start, sizeof(char));
 550 
 551       // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME!
 552       if (from_size == 0) {
 553         from_size = alignment;
 554       } else {
 555         from_size = align_size_up(from_size, alignment);
 556       }
 557 
 558       from_end = from_start + from_size;
 559       assert(from_end > from_start, "addition overflow or from_size problem");
 560 
 561       guarantee(from_end <= (char*)from_space()->end(), "from_end moved to the right");
 562 
 563       // Now update to_start with the new from_end
 564       to_start = MAX2(from_end, to_start);
 565     }
 566 
 567     guarantee(to_start != to_end, "to space is zero sized");
 568 
 569     if (PrintAdaptiveSizePolicy && Verbose) {
 570       gclog_or_tty->print_cr("    [eden_start .. eden_end): "
 571                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 572                     p2i(eden_start),
 573                     p2i(eden_end),
 574                     pointer_delta(eden_end, eden_start, sizeof(char)));
 575       gclog_or_tty->print_cr("    [from_start .. from_end): "
 576                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 577                     p2i(from_start),
 578                     p2i(from_end),
 579                     pointer_delta(from_end, from_start, sizeof(char)));
 580       gclog_or_tty->print_cr("    [  to_start ..   to_end): "
 581                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 582                     p2i(to_start),
 583                     p2i(to_end),
 584                     pointer_delta(  to_end,   to_start, sizeof(char)));
 585     }
 586   } else {
 587     // Eden, to, from
 588     if (PrintAdaptiveSizePolicy && Verbose) {
 589       gclog_or_tty->print_cr("  Eden, to, from:");
 590     }
 591 
 592     // To space gets priority over eden resizing. Note that we position
 593     // to space as if we were able to resize from space, even though from
 594     // space is not modified.
 595     // Giving eden priority was tried and gave poorer performance.
 596     to_end   = (char*)pointer_delta(virtual_space()->high(),
 597                                     (char*)requested_survivor_size,
 598                                     sizeof(char));
 599     to_end   = MIN2(to_end, from_start);
 600     to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size,
 601                                     sizeof(char));
 602     // if the space sizes are to be increased by several times then
 603     // 'to_start' will point beyond the young generation. In this case
 604     // 'to_start' should be adjusted.
 605     to_start = MAX2(to_start, eden_start + alignment);
 606 
 607     // Compute how big eden can be, then adjust end.
 608     // See  comments above on calculating eden_end.
 609     size_t eden_size;
 610     if (maintain_minimum) {
 611       eden_size = pointer_delta(to_start, eden_start, sizeof(char));
 612     } else {
 613       eden_size = MIN2(requested_eden_size,
 614                        pointer_delta(to_start, eden_start, sizeof(char)));
 615     }
 616     eden_end = eden_start + eden_size;
 617     assert(eden_end >= eden_start, "addition overflowed");
 618 
 619     // Could choose to not let eden shrink
 620     // to_start = MAX2(to_start, eden_end);
 621 
 622     // Don't let eden shrink down to 0 or less.
 623     eden_end = MAX2(eden_end, eden_start + alignment);
 624     to_start = MAX2(to_start, eden_end);
 625 
 626     if (PrintAdaptiveSizePolicy && Verbose) {
 627       gclog_or_tty->print_cr("    [eden_start .. eden_end): "
 628                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 629                     p2i(eden_start),
 630                     p2i(eden_end),
 631                     pointer_delta(eden_end, eden_start, sizeof(char)));
 632       gclog_or_tty->print_cr("    [  to_start ..   to_end): "
 633                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 634                     p2i(to_start),
 635                     p2i(to_end),
 636                     pointer_delta(  to_end,   to_start, sizeof(char)));
 637       gclog_or_tty->print_cr("    [from_start .. from_end): "
 638                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 639                     p2i(from_start),
 640                     p2i(from_end),
 641                     pointer_delta(from_end, from_start, sizeof(char)));
 642     }
 643   }
 644 
 645 
 646   guarantee((HeapWord*)from_start <= from_space()->bottom(),
 647             "from start moved to the right");
 648   guarantee((HeapWord*)from_end >= from_space()->top(),
 649             "from end moved into live data");
 650   assert(is_object_aligned((intptr_t)eden_start), "checking alignment");
 651   assert(is_object_aligned((intptr_t)from_start), "checking alignment");
 652   assert(is_object_aligned((intptr_t)to_start), "checking alignment");
 653 
 654   MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end);
 655   MemRegion toMR  ((HeapWord*)to_start,   (HeapWord*)to_end);
 656   MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end);
 657 
 658   // Let's make sure the call to initialize doesn't reset "top"!
 659   HeapWord* old_from_top = from_space()->top();
 660 
 661   // For PrintAdaptiveSizePolicy block  below
 662   size_t old_from = from_space()->capacity_in_bytes();
 663   size_t old_to   = to_space()->capacity_in_bytes();
 664 
 665   if (ZapUnusedHeapArea) {
 666     // NUMA is a special case because a numa space is not mangled
 667     // in order to not prematurely bind its address to memory to
 668     // the wrong memory (i.e., don't want the GC thread to first
 669     // touch the memory).  The survivor spaces are not numa
 670     // spaces and are mangled.
 671     if (UseNUMA) {
 672       if (eden_from_to_order) {
 673         mangle_survivors(from_space(), fromMR, to_space(), toMR);
 674       } else {
 675         mangle_survivors(to_space(), toMR, from_space(), fromMR);
 676       }
 677     }
 678 
 679     // If not mangling the spaces, do some checking to verify that
 680     // the spaces are already mangled.
 681     // The spaces should be correctly mangled at this point so


 687     // not correctly indicate which space is not properly mangled.
 688     HeapWord* limit = (HeapWord*) virtual_space()->high();
 689     eden_space()->check_mangled_unused_area(limit);
 690     from_space()->check_mangled_unused_area(limit);
 691       to_space()->check_mangled_unused_area(limit);
 692   }
 693   // When an existing space is being initialized, it is not
 694   // mangled because the space has been previously mangled.
 695   eden_space()->initialize(edenMR,
 696                            SpaceDecorator::Clear,
 697                            SpaceDecorator::DontMangle);
 698     to_space()->initialize(toMR,
 699                            SpaceDecorator::Clear,
 700                            SpaceDecorator::DontMangle);
 701   from_space()->initialize(fromMR,
 702                            SpaceDecorator::DontClear,
 703                            SpaceDecorator::DontMangle);
 704 
 705   assert(from_space()->top() == old_from_top, "from top changed!");
 706 
 707   if (PrintAdaptiveSizePolicy) {
 708     ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 709     gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: "
 710                   "collection: %d "
 711                   "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> "
 712                   "(" SIZE_FORMAT ", " SIZE_FORMAT ") ",
 713                   heap->total_collections(),
 714                   old_from, old_to,
 715                   from_space()->capacity_in_bytes(),
 716                   to_space()->capacity_in_bytes());
 717     gclog_or_tty->cr();
 718   }
 719 }
 720 
 721 void PSYoungGen::swap_spaces() {
 722   MutableSpace* s    = from_space();
 723   _from_space        = to_space();
 724   _to_space          = s;
 725 
 726   // Now update the decorators.
 727   PSMarkSweepDecorator* md = from_mark_sweep();
 728   _from_mark_sweep           = to_mark_sweep();
 729   _to_mark_sweep             = md;
 730 
 731   assert(from_mark_sweep()->space() == from_space(), "Sanity");
 732   assert(to_mark_sweep()->space() == to_space(), "Sanity");
 733 }
 734 
 735 size_t PSYoungGen::capacity_in_bytes() const {
 736   return eden_space()->capacity_in_bytes()
 737        + from_space()->capacity_in_bytes();  // to_space() is only used during scavenge
 738 }


 777   from_mark_sweep()->precompact();
 778   to_mark_sweep()->precompact();
 779 }
 780 
 781 void PSYoungGen::adjust_pointers() {
 782   eden_mark_sweep()->adjust_pointers();
 783   from_mark_sweep()->adjust_pointers();
 784   to_mark_sweep()->adjust_pointers();
 785 }
 786 
 787 void PSYoungGen::compact() {
 788   eden_mark_sweep()->compact(ZapUnusedHeapArea);
 789   from_mark_sweep()->compact(ZapUnusedHeapArea);
 790   // Mark sweep stores preserved markOops in to space, don't disturb!
 791   to_mark_sweep()->compact(false);
 792 }
 793 
 794 void PSYoungGen::print() const { print_on(tty); }
 795 void PSYoungGen::print_on(outputStream* st) const {
 796   st->print(" %-15s", "PSYoungGen");
 797   if (PrintGCDetails && Verbose) {
 798     st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT,
 799                capacity_in_bytes(), used_in_bytes());
 800   } else {
 801     st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
 802                capacity_in_bytes()/K, used_in_bytes()/K);
 803   }
 804   virtual_space()->print_space_boundaries_on(st);
 805   st->print("  eden"); eden_space()->print_on(st);
 806   st->print("  from"); from_space()->print_on(st);
 807   st->print("  to  "); to_space()->print_on(st);
 808 }
 809 
 810 // Note that a space is not printed before the [NAME:
 811 void PSYoungGen::print_used_change(size_t prev_used) const {
 812   gclog_or_tty->print("[%s:", name());
 813   gclog_or_tty->print(" "  SIZE_FORMAT "K"
 814                       "->" SIZE_FORMAT "K"
 815                       "("  SIZE_FORMAT "K)",
 816                       prev_used / K, used_in_bytes() / K,
 817                       capacity_in_bytes() / K);
 818   gclog_or_tty->print("]");
 819 }
 820 
 821 size_t PSYoungGen::available_for_expansion() {
 822   ShouldNotReachHere();
 823   return 0;
 824 }
 825 
 826 size_t PSYoungGen::available_for_contraction() {
 827   ShouldNotReachHere();
 828   return 0;
 829 }
 830 
 831 size_t PSYoungGen::available_to_min_gen() {
 832   assert(virtual_space()->committed_size() >= min_gen_size(), "Invariant");
 833   return virtual_space()->committed_size() - min_gen_size();
 834 }
 835 
 836 // This method assumes that from-space has live data and that
 837 // any shrinkage of the young gen is limited by location of
 838 // from-space.




  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/mutableNUMASpace.hpp"
  27 #include "gc/parallel/parallelScavengeHeap.hpp"
  28 #include "gc/parallel/psMarkSweepDecorator.hpp"
  29 #include "gc/parallel/psScavenge.hpp"
  30 #include "gc/parallel/psYoungGen.hpp"
  31 #include "gc/shared/gcUtil.hpp"
  32 #include "gc/shared/spaceDecorator.hpp"
  33 #include "logging/log.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/java.hpp"
  36 
  37 PSYoungGen::PSYoungGen(size_t        initial_size,
  38                        size_t        min_size,
  39                        size_t        max_size) :
  40   _init_gen_size(initial_size),
  41   _min_gen_size(min_size),
  42   _max_gen_size(max_size)
  43 {}
  44 
  45 void PSYoungGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) {
  46   assert(_init_gen_size != 0, "Should have a finite size");
  47   _virtual_space = new PSVirtualSpace(rs, alignment);
  48   if (!virtual_space()->expand_by(_init_gen_size)) {
  49     vm_exit_during_initialization("Could not reserve enough space for "
  50                                   "object heap");
  51   }
  52 }
  53 


 252   char* eden_top = (char*)eden_space()->top();
 253   char* from_top = (char*)from_space()->top();
 254   char* to_top = (char*)to_space()->top();
 255   assert(eden_top <= virtual_space()->high(), "eden top");
 256   assert(from_top <= virtual_space()->high(), "from top");
 257   assert(to_top <= virtual_space()->high(), "to top");
 258 
 259   virtual_space()->verify();
 260 }
 261 #endif
 262 
 263 void PSYoungGen::resize(size_t eden_size, size_t survivor_size) {
 264   // Resize the generation if needed. If the generation resize
 265   // reports false, do not attempt to resize the spaces.
 266   if (resize_generation(eden_size, survivor_size)) {
 267     // Then we lay out the spaces inside the generation
 268     resize_spaces(eden_size, survivor_size);
 269 
 270     space_invariants();
 271 
 272     log_trace(gc, ergo)("Young generation size: "

 273                         "desired eden: " SIZE_FORMAT " survivor: " SIZE_FORMAT
 274                         " used: " SIZE_FORMAT " capacity: " SIZE_FORMAT
 275                         " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT,
 276                         eden_size, survivor_size, used_in_bytes(), capacity_in_bytes(),
 277                         _max_gen_size, min_gen_size());
 278   }

 279 }
 280 
 281 
 282 bool PSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) {
 283   const size_t alignment = virtual_space()->alignment();
 284   size_t orig_size = virtual_space()->committed_size();
 285   bool size_changed = false;
 286 
 287   // There used to be this guarantee there.
 288   // guarantee ((eden_size + 2*survivor_size)  <= _max_gen_size, "incorrect input arguments");
 289   // Code below forces this requirement.  In addition the desired eden
 290   // size and desired survivor sizes are desired goals and may
 291   // exceed the total generation size.
 292 
 293   assert(min_gen_size() <= orig_size && orig_size <= max_size(), "just checking");
 294 
 295   // Adjust new generation size
 296   const size_t eden_plus_survivors =
 297           align_size_up(eden_size + 2 * survivor_size, alignment);
 298   size_t desired_size = MAX2(MIN2(eden_plus_survivors, max_size()),


 312       // can be done here more simply that after the new
 313       // spaces have been computed.
 314       HeapWord* new_high = (HeapWord*) virtual_space()->high();
 315       MemRegion mangle_region(prev_high, new_high);
 316       SpaceMangler::mangle_region(mangle_region);
 317     }
 318     size_changed = true;
 319   } else if (desired_size < orig_size) {
 320     size_t desired_change = orig_size - desired_size;
 321     assert(desired_change % alignment == 0, "just checking");
 322 
 323     desired_change = limit_gen_shrink(desired_change);
 324 
 325     if (desired_change > 0) {
 326       virtual_space()->shrink_by(desired_change);
 327       reset_survivors_after_shrink();
 328 
 329       size_changed = true;
 330     }
 331   } else {

 332     if (orig_size == gen_size_limit()) {
 333       log_trace(gc)("PSYoung generation size at maximum: " SIZE_FORMAT "K", orig_size/K);

 334     } else if (orig_size == min_gen_size()) {
 335       log_trace(gc)("PSYoung generation size at minium: " SIZE_FORMAT "K", orig_size/K);


 336     }
 337   }
 338 
 339   if (size_changed) {
 340     post_resize();
 341     log_trace(gc)("PSYoung generation size changed: " SIZE_FORMAT "K->" SIZE_FORMAT "K",
 342                   orig_size/K, virtual_space()->committed_size()/K);





 343   }
 344 
 345   guarantee(eden_plus_survivors <= virtual_space()->committed_size() ||
 346             virtual_space()->committed_size() == max_size(), "Sanity");
 347 
 348   return true;
 349 }
 350 
 351 #ifndef PRODUCT
 352 // In the numa case eden is not mangled so a survivor space
 353 // moving into a region previously occupied by a survivor
 354 // may find an unmangled region.  Also in the PS case eden
 355 // to-space and from-space may not touch (i.e., there may be
 356 // gaps between them due to movement while resizing the
 357 // spaces).  Those gaps must be mangled.
 358 void PSYoungGen::mangle_survivors(MutableSpace* s1,
 359                                   MemRegion s1MR,
 360                                   MutableSpace* s2,
 361                                   MemRegion s2MR) {
 362   // Check eden and gap between eden and from-space, in deciding


 385     s1->mangle_region(delta1_right);
 386   }
 387 
 388   // Similarly for the second survivor space except that
 389   // any of the new region that overlaps with the current
 390   // region of the first survivor space has already been
 391   // mangled.
 392   delta_end = MIN2(s2->bottom(), s2MR.end());
 393   delta_start = MAX2(s2MR.start(), s1->end());
 394   MemRegion delta2_left;
 395   if (s2MR.start() < delta_end) {
 396     delta2_left = MemRegion(s2MR.start(), delta_end);
 397     s2->mangle_region(delta2_left);
 398   }
 399   delta_start = MAX2(s2->end(), s2MR.start());
 400   MemRegion delta2_right;
 401   if (delta_start < s2MR.end()) {
 402     s2->mangle_region(delta2_right);
 403   }
 404 

 405   // s1
 406   log_develop(gc)("Current region: [" PTR_FORMAT ", " PTR_FORMAT ") "
 407     "New region: [" PTR_FORMAT ", " PTR_FORMAT ")",
 408     p2i(s1->bottom()), p2i(s1->end()),
 409     p2i(s1MR.start()), p2i(s1MR.end()));
 410   log_develop(gc)("    Mangle before: [" PTR_FORMAT ", "
 411     PTR_FORMAT ")  Mangle after: [" PTR_FORMAT ", " PTR_FORMAT ")",
 412     p2i(delta1_left.start()), p2i(delta1_left.end()),
 413     p2i(delta1_right.start()), p2i(delta1_right.end()));
 414 
 415   // s2
 416   log_develop(gc)("Current region: [" PTR_FORMAT ", " PTR_FORMAT ") "
 417     "New region: [" PTR_FORMAT ", " PTR_FORMAT ")",
 418     p2i(s2->bottom()), p2i(s2->end()),
 419     p2i(s2MR.start()), p2i(s2MR.end()));
 420   log_develop(gc)("    Mangle before: [" PTR_FORMAT ", "
 421     PTR_FORMAT ")  Mangle after: [" PTR_FORMAT ", " PTR_FORMAT ")",
 422     p2i(delta2_left.start()), p2i(delta2_left.end()),
 423     p2i(delta2_right.start()), p2i(delta2_right.end()));


 424 }
 425 #endif // NOT PRODUCT
 426 
 427 void PSYoungGen::resize_spaces(size_t requested_eden_size,
 428                                size_t requested_survivor_size) {
 429   assert(UseAdaptiveSizePolicy, "sanity check");
 430   assert(requested_eden_size > 0  && requested_survivor_size > 0,
 431          "just checking");
 432 
 433   // We require eden and to space to be empty
 434   if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) {
 435     return;
 436   }
 437 
 438   log_trace(gc, ergo)("PSYoungGen::resize_spaces(requested_eden_size: " SIZE_FORMAT ", requested_survivor_size: " SIZE_FORMAT ")",



 439                       requested_eden_size, requested_survivor_size);
 440   log_trace(gc, ergo)("    eden: [" PTR_FORMAT ".." PTR_FORMAT ") " SIZE_FORMAT,

 441                       p2i(eden_space()->bottom()),
 442                       p2i(eden_space()->end()),
 443                       pointer_delta(eden_space()->end(),
 444                                     eden_space()->bottom(),
 445                                     sizeof(char)));
 446   log_trace(gc, ergo)("    from: [" PTR_FORMAT ".." PTR_FORMAT ") " SIZE_FORMAT,

 447                       p2i(from_space()->bottom()),
 448                       p2i(from_space()->end()),
 449                       pointer_delta(from_space()->end(),
 450                                     from_space()->bottom(),
 451                                     sizeof(char)));
 452   log_trace(gc, ergo)("      to: [" PTR_FORMAT ".." PTR_FORMAT ") " SIZE_FORMAT,

 453                       p2i(to_space()->bottom()),
 454                       p2i(to_space()->end()),
 455                       pointer_delta(  to_space()->end(),
 456                                       to_space()->bottom(),
 457                                       sizeof(char)));

 458 
 459   // There's nothing to do if the new sizes are the same as the current
 460   if (requested_survivor_size == to_space()->capacity_in_bytes() &&
 461       requested_survivor_size == from_space()->capacity_in_bytes() &&
 462       requested_eden_size == eden_space()->capacity_in_bytes()) {
 463     log_trace(gc, ergo)("    capacities are the right sizes, returning");


 464     return;
 465   }
 466 
 467   char* eden_start = (char*)eden_space()->bottom();
 468   char* eden_end   = (char*)eden_space()->end();
 469   char* from_start = (char*)from_space()->bottom();
 470   char* from_end   = (char*)from_space()->end();
 471   char* to_start   = (char*)to_space()->bottom();
 472   char* to_end     = (char*)to_space()->end();
 473 
 474   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 475   const size_t alignment = heap->space_alignment();
 476   const bool maintain_minimum =
 477     (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size();
 478 
 479   bool eden_from_to_order = from_start < to_start;
 480   // Check whether from space is below to space
 481   if (eden_from_to_order) {
 482     // Eden, from, to
 483     eden_from_to_order = true;
 484     log_trace(gc, ergo)("  Eden, from, to:");


 485 
 486     // Set eden
 487     // "requested_eden_size" is a goal for the size of eden
 488     // and may not be attainable.  "eden_size" below is
 489     // calculated based on the location of from-space and
 490     // the goal for the size of eden.  from-space is
 491     // fixed in place because it contains live data.
 492     // The calculation is done this way to avoid 32bit
 493     // overflow (i.e., eden_start + requested_eden_size
 494     // may too large for representation in 32bits).
 495     size_t eden_size;
 496     if (maintain_minimum) {
 497       // Only make eden larger than the requested size if
 498       // the minimum size of the generation has to be maintained.
 499       // This could be done in general but policy at a higher
 500       // level is determining a requested size for eden and that
 501       // should be honored unless there is a fundamental reason.
 502       eden_size = pointer_delta(from_start,
 503                                 eden_start,
 504                                 sizeof(char));


 525       size_t from_size = pointer_delta(from_space()->top(), from_start, sizeof(char));
 526 
 527       // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME!
 528       if (from_size == 0) {
 529         from_size = alignment;
 530       } else {
 531         from_size = align_size_up(from_size, alignment);
 532       }
 533 
 534       from_end = from_start + from_size;
 535       assert(from_end > from_start, "addition overflow or from_size problem");
 536 
 537       guarantee(from_end <= (char*)from_space()->end(), "from_end moved to the right");
 538 
 539       // Now update to_start with the new from_end
 540       to_start = MAX2(from_end, to_start);
 541     }
 542 
 543     guarantee(to_start != to_end, "to space is zero sized");
 544 
 545     log_trace(gc, ergo)("    [eden_start .. eden_end): [" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,


 546                         p2i(eden_start),
 547                         p2i(eden_end),
 548                         pointer_delta(eden_end, eden_start, sizeof(char)));
 549     log_trace(gc, ergo)("    [from_start .. from_end): [" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,

 550                         p2i(from_start),
 551                         p2i(from_end),
 552                         pointer_delta(from_end, from_start, sizeof(char)));
 553     log_trace(gc, ergo)("    [  to_start ..   to_end): [" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,

 554                         p2i(to_start),
 555                         p2i(to_end),
 556                         pointer_delta(  to_end,   to_start, sizeof(char)));

 557   } else {
 558     // Eden, to, from
 559     log_trace(gc, ergo)("  Eden, to, from:");


 560 
 561     // To space gets priority over eden resizing. Note that we position
 562     // to space as if we were able to resize from space, even though from
 563     // space is not modified.
 564     // Giving eden priority was tried and gave poorer performance.
 565     to_end   = (char*)pointer_delta(virtual_space()->high(),
 566                                     (char*)requested_survivor_size,
 567                                     sizeof(char));
 568     to_end   = MIN2(to_end, from_start);
 569     to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size,
 570                                     sizeof(char));
 571     // if the space sizes are to be increased by several times then
 572     // 'to_start' will point beyond the young generation. In this case
 573     // 'to_start' should be adjusted.
 574     to_start = MAX2(to_start, eden_start + alignment);
 575 
 576     // Compute how big eden can be, then adjust end.
 577     // See  comments above on calculating eden_end.
 578     size_t eden_size;
 579     if (maintain_minimum) {
 580       eden_size = pointer_delta(to_start, eden_start, sizeof(char));
 581     } else {
 582       eden_size = MIN2(requested_eden_size,
 583                        pointer_delta(to_start, eden_start, sizeof(char)));
 584     }
 585     eden_end = eden_start + eden_size;
 586     assert(eden_end >= eden_start, "addition overflowed");
 587 
 588     // Could choose to not let eden shrink
 589     // to_start = MAX2(to_start, eden_end);
 590 
 591     // Don't let eden shrink down to 0 or less.
 592     eden_end = MAX2(eden_end, eden_start + alignment);
 593     to_start = MAX2(to_start, eden_end);
 594 
 595     log_trace(gc, ergo)("    [eden_start .. eden_end): [" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,


 596                         p2i(eden_start),
 597                         p2i(eden_end),
 598                         pointer_delta(eden_end, eden_start, sizeof(char)));
 599     log_trace(gc, ergo)("    [  to_start ..   to_end): [" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,

 600                         p2i(to_start),
 601                         p2i(to_end),
 602                         pointer_delta(  to_end,   to_start, sizeof(char)));
 603     log_trace(gc, ergo)("    [from_start .. from_end): [" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,

 604                         p2i(from_start),
 605                         p2i(from_end),
 606                         pointer_delta(from_end, from_start, sizeof(char)));
 607   }

 608 
 609 
 610   guarantee((HeapWord*)from_start <= from_space()->bottom(),
 611             "from start moved to the right");
 612   guarantee((HeapWord*)from_end >= from_space()->top(),
 613             "from end moved into live data");
 614   assert(is_object_aligned((intptr_t)eden_start), "checking alignment");
 615   assert(is_object_aligned((intptr_t)from_start), "checking alignment");
 616   assert(is_object_aligned((intptr_t)to_start), "checking alignment");
 617 
 618   MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end);
 619   MemRegion toMR  ((HeapWord*)to_start,   (HeapWord*)to_end);
 620   MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end);
 621 
 622   // Let's make sure the call to initialize doesn't reset "top"!
 623   HeapWord* old_from_top = from_space()->top();
 624 
 625   // For logging block  below
 626   size_t old_from = from_space()->capacity_in_bytes();
 627   size_t old_to   = to_space()->capacity_in_bytes();
 628 
 629   if (ZapUnusedHeapArea) {
 630     // NUMA is a special case because a numa space is not mangled
 631     // in order to not prematurely bind its address to memory to
 632     // the wrong memory (i.e., don't want the GC thread to first
 633     // touch the memory).  The survivor spaces are not numa
 634     // spaces and are mangled.
 635     if (UseNUMA) {
 636       if (eden_from_to_order) {
 637         mangle_survivors(from_space(), fromMR, to_space(), toMR);
 638       } else {
 639         mangle_survivors(to_space(), toMR, from_space(), fromMR);
 640       }
 641     }
 642 
 643     // If not mangling the spaces, do some checking to verify that
 644     // the spaces are already mangled.
 645     // The spaces should be correctly mangled at this point so


 651     // not correctly indicate which space is not properly mangled.
 652     HeapWord* limit = (HeapWord*) virtual_space()->high();
 653     eden_space()->check_mangled_unused_area(limit);
 654     from_space()->check_mangled_unused_area(limit);
 655       to_space()->check_mangled_unused_area(limit);
 656   }
 657   // When an existing space is being initialized, it is not
 658   // mangled because the space has been previously mangled.
 659   eden_space()->initialize(edenMR,
 660                            SpaceDecorator::Clear,
 661                            SpaceDecorator::DontMangle);
 662     to_space()->initialize(toMR,
 663                            SpaceDecorator::Clear,
 664                            SpaceDecorator::DontMangle);
 665   from_space()->initialize(fromMR,
 666                            SpaceDecorator::DontClear,
 667                            SpaceDecorator::DontMangle);
 668 
 669   assert(from_space()->top() == old_from_top, "from top changed!");
 670 
 671   log_trace(gc, ergo)("AdaptiveSizePolicy::survivor space sizes: collection: %d (" SIZE_FORMAT ", " SIZE_FORMAT ") -> (" SIZE_FORMAT ", " SIZE_FORMAT ") ",
 672                       ParallelScavengeHeap::heap()->total_collections(),





 673                       old_from, old_to,
 674                       from_space()->capacity_in_bytes(),
 675                       to_space()->capacity_in_bytes());


 676 }
 677 
 678 void PSYoungGen::swap_spaces() {
 679   MutableSpace* s    = from_space();
 680   _from_space        = to_space();
 681   _to_space          = s;
 682 
 683   // Now update the decorators.
 684   PSMarkSweepDecorator* md = from_mark_sweep();
 685   _from_mark_sweep           = to_mark_sweep();
 686   _to_mark_sweep             = md;
 687 
 688   assert(from_mark_sweep()->space() == from_space(), "Sanity");
 689   assert(to_mark_sweep()->space() == to_space(), "Sanity");
 690 }
 691 
 692 size_t PSYoungGen::capacity_in_bytes() const {
 693   return eden_space()->capacity_in_bytes()
 694        + from_space()->capacity_in_bytes();  // to_space() is only used during scavenge
 695 }


 734   from_mark_sweep()->precompact();
 735   to_mark_sweep()->precompact();
 736 }
 737 
 738 void PSYoungGen::adjust_pointers() {
 739   eden_mark_sweep()->adjust_pointers();
 740   from_mark_sweep()->adjust_pointers();
 741   to_mark_sweep()->adjust_pointers();
 742 }
 743 
 744 void PSYoungGen::compact() {
 745   eden_mark_sweep()->compact(ZapUnusedHeapArea);
 746   from_mark_sweep()->compact(ZapUnusedHeapArea);
 747   // Mark sweep stores preserved markOops in to space, don't disturb!
 748   to_mark_sweep()->compact(false);
 749 }
 750 
 751 void PSYoungGen::print() const { print_on(tty); }
 752 void PSYoungGen::print_on(outputStream* st) const {
 753   st->print(" %-15s", "PSYoungGen");




 754   st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
 755              capacity_in_bytes()/K, used_in_bytes()/K);

 756   virtual_space()->print_space_boundaries_on(st);
 757   st->print("  eden"); eden_space()->print_on(st);
 758   st->print("  from"); from_space()->print_on(st);
 759   st->print("  to  "); to_space()->print_on(st);
 760 }
 761 
 762 // Note that a space is not printed before the [NAME:
 763 void PSYoungGen::print_used_change(size_t prev_used) const {
 764   log_info(gc, heap)("%s: "  SIZE_FORMAT "K->" SIZE_FORMAT "K("  SIZE_FORMAT "K)",
 765       name(), prev_used / K, used_in_bytes() / K, capacity_in_bytes() / K);





 766 }
 767 
 768 size_t PSYoungGen::available_for_expansion() {
 769   ShouldNotReachHere();
 770   return 0;
 771 }
 772 
 773 size_t PSYoungGen::available_for_contraction() {
 774   ShouldNotReachHere();
 775   return 0;
 776 }
 777 
 778 size_t PSYoungGen::available_to_min_gen() {
 779   assert(virtual_space()->committed_size() >= min_gen_size(), "Invariant");
 780   return virtual_space()->committed_size() - min_gen_size();
 781 }
 782 
 783 // This method assumes that from-space has live data and that
 784 // any shrinkage of the young gen is limited by location of
 785 // from-space.


< prev index next >