1 /*
   2  * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/parallel/asPSYoungGen.hpp"
  27 #include "gc/parallel/parallelScavengeHeap.hpp"
  28 #include "gc/parallel/psScavenge.inline.hpp"
  29 #include "gc/parallel/psYoungGen.hpp"
  30 #include "gc/shared/gcUtil.hpp"
  31 #include "gc/shared/genArguments.hpp"
  32 #include "gc/shared/spaceDecorator.inline.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/java.hpp"
  35 #include "utilities/align.hpp"
  36 
  37 ASPSYoungGen::ASPSYoungGen(size_t init_byte_size,
  38                            size_t minimum_byte_size,
  39                            size_t byte_size_limit) :
  40   PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit),
  41   _gen_size_limit(byte_size_limit) {
  42 }
  43 
  44 
  45 ASPSYoungGen::ASPSYoungGen(PSVirtualSpace* vs,
  46                            size_t init_byte_size,
  47                            size_t minimum_byte_size,
  48                            size_t byte_size_limit) :
  49   //PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit),
  50   PSYoungGen(vs->committed_size(), minimum_byte_size, byte_size_limit),
  51   _gen_size_limit(byte_size_limit) {
  52 
  53   assert(vs->committed_size() == init_byte_size, "Cannot replace with");
  54 
  55   _virtual_space = vs;
  56 }
  57 
  58 void ASPSYoungGen::initialize_virtual_space(ReservedSpace rs,
  59                                             size_t alignment) {
  60   assert(_init_gen_size != 0, "Should have a finite size");
  61   _virtual_space = new PSVirtualSpaceHighToLow(rs, alignment);
  62   if (!_virtual_space->expand_by(_init_gen_size)) {
  63     vm_exit_during_initialization("Could not reserve enough space for object heap");
  64   }
  65 }
  66 
  67 void ASPSYoungGen::initialize(ReservedSpace rs, size_t alignment) {
  68   initialize_virtual_space(rs, alignment);
  69   initialize_work();
  70 }
  71 
  72 size_t ASPSYoungGen::available_for_expansion() {
  73   size_t current_committed_size = virtual_space()->committed_size();
  74   assert((gen_size_limit() >= current_committed_size),
  75     "generation size limit is wrong");
  76 
  77   size_t result =  gen_size_limit() - current_committed_size;
  78   size_t result_aligned = align_down(result, GenAlignment);
  79   return result_aligned;
  80 }
  81 
  82 // Return the number of bytes the young gen is willing give up.
  83 //
  84 // Future implementations could check the survivors and if to_space is in the
  85 // right place (below from_space), take a chunk from to_space.
  86 size_t ASPSYoungGen::available_for_contraction() {
  87   size_t uncommitted_bytes = virtual_space()->uncommitted_size();
  88   if (uncommitted_bytes != 0) {
  89     return uncommitted_bytes;
  90   }
  91 
  92   if (eden_space()->is_empty()) {
  93     // Respect the minimum size for eden and for the young gen as a whole.
  94     ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
  95     const size_t eden_alignment = SpaceAlignment;
  96 
  97     assert(eden_space()->capacity_in_bytes() >= eden_alignment,
  98       "Alignment is wrong");
  99     size_t eden_avail = eden_space()->capacity_in_bytes() - eden_alignment;
 100     eden_avail = align_down(eden_avail, GenAlignment);
 101 
 102     assert(virtual_space()->committed_size() >= min_gen_size(),
 103       "minimum gen size is wrong");
 104     size_t gen_avail = virtual_space()->committed_size() - min_gen_size();
 105     assert(virtual_space()->is_aligned(gen_avail), "not aligned");
 106 
 107     const size_t max_contraction = MIN2(eden_avail, gen_avail);
 108     // See comment for ASPSOldGen::available_for_contraction()
 109     // for reasons the "increment" fraction is used.
 110     PSAdaptiveSizePolicy* policy = heap->size_policy();
 111     size_t result = policy->eden_increment_aligned_down(max_contraction);
 112     size_t result_aligned = align_down(result, GenAlignment);
 113 
 114     log_trace(gc, ergo)("ASPSYoungGen::available_for_contraction: " SIZE_FORMAT " K", result_aligned/K);
 115     log_trace(gc, ergo)("  max_contraction " SIZE_FORMAT " K", max_contraction/K);
 116     log_trace(gc, ergo)("  eden_avail " SIZE_FORMAT " K", eden_avail/K);
 117     log_trace(gc, ergo)("  gen_avail " SIZE_FORMAT " K", gen_avail/K);
 118 
 119     return result_aligned;
 120   }
 121 
 122   return 0;
 123 }
 124 
 125 // The current implementation only considers to the end of eden.
 126 // If to_space is below from_space, to_space is not considered.
 127 // to_space can be.
 128 size_t ASPSYoungGen::available_to_live() {
 129   const size_t alignment = SpaceAlignment;
 130 
 131   // Include any space that is committed but is not in eden.
 132   size_t available = pointer_delta(eden_space()->bottom(),
 133                                    virtual_space()->low(),
 134                                    sizeof(char));
 135 
 136   const size_t eden_capacity = eden_space()->capacity_in_bytes();
 137   if (eden_space()->is_empty() && eden_capacity > alignment) {
 138     available += eden_capacity - alignment;
 139   }
 140   return available;
 141 }
 142 
 143 // Similar to PSYoungGen::resize_generation() but
 144 //  allows sum of eden_size and 2 * survivor_size to exceed _max_gen_size
 145 //  expands at the low end of the virtual space
 146 //  moves the boundary between the generations in order to expand
 147 //  some additional diagnostics
 148 // If no additional changes are required, this can be deleted
 149 // and the changes factored back into PSYoungGen::resize_generation().
 150 bool ASPSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) {
 151   const size_t alignment = virtual_space()->alignment();
 152   size_t orig_size = virtual_space()->committed_size();
 153   bool size_changed = false;
 154 
 155   // There used to be a guarantee here that
 156   //   (eden_size + 2*survivor_size)  <= _max_gen_size
 157   // This requirement is enforced by the calculation of desired_size
 158   // below.  It may not be true on entry since the size of the
 159   // eden_size is no bounded by the generation size.
 160 
 161   assert(max_size() == reserved().byte_size(), "max gen size problem?");
 162   assert(min_gen_size() <= orig_size && orig_size <= max_size(),
 163          "just checking");
 164 
 165   // Adjust new generation size
 166   const size_t eden_plus_survivors =
 167     align_up(eden_size + 2 * survivor_size, alignment);
 168   size_t desired_size = clamp(eden_plus_survivors, min_gen_size(), gen_size_limit());
 169   assert(desired_size <= gen_size_limit(), "just checking");
 170 
 171   if (desired_size > orig_size) {
 172     // Grow the generation
 173     size_t change = desired_size - orig_size;
 174     HeapWord* prev_low = (HeapWord*) virtual_space()->low();
 175     if (!virtual_space()->expand_by(change)) {
 176       return false;
 177     }
 178     if (ZapUnusedHeapArea) {
 179       // Mangle newly committed space immediately because it
 180       // can be done here more simply that after the new
 181       // spaces have been computed.
 182       HeapWord* new_low = (HeapWord*) virtual_space()->low();
 183       assert(new_low < prev_low, "Did not grow");
 184 
 185       MemRegion mangle_region(new_low, prev_low);
 186       SpaceMangler::mangle_region(mangle_region);
 187     }
 188     size_changed = true;
 189   } else if (desired_size < orig_size) {
 190     size_t desired_change = orig_size - desired_size;
 191 
 192     // How much is available for shrinking.
 193     size_t available_bytes = limit_gen_shrink(desired_change);
 194     size_t change = MIN2(desired_change, available_bytes);
 195     virtual_space()->shrink_by(change);
 196     size_changed = true;
 197   } else {
 198     if (orig_size == gen_size_limit()) {
 199       log_trace(gc)("ASPSYoung generation size at maximum: " SIZE_FORMAT "K", orig_size/K);
 200     } else if (orig_size == min_gen_size()) {
 201       log_trace(gc)("ASPSYoung generation size at minium: " SIZE_FORMAT "K", orig_size/K);
 202     }
 203   }
 204 
 205   if (size_changed) {
 206     reset_after_change();
 207     log_trace(gc)("ASPSYoung generation size changed: " SIZE_FORMAT "K->" SIZE_FORMAT "K",
 208                   orig_size/K, virtual_space()->committed_size()/K);
 209   }
 210 
 211   guarantee(eden_plus_survivors <= virtual_space()->committed_size() ||
 212             virtual_space()->committed_size() == max_size(), "Sanity");
 213 
 214   return true;
 215 }
 216 
 217 // Similar to PSYoungGen::resize_spaces() but
 218 //  eden always starts at the low end of the committed virtual space
 219 //  current implementation does not allow holes between the spaces
 220 //  _young_generation_boundary has to be reset because it changes.
 221 //  so additional verification
 222 
 223 void ASPSYoungGen::resize_spaces(size_t requested_eden_size,
 224                                  size_t requested_survivor_size) {
 225   assert(UseAdaptiveSizePolicy, "sanity check");
 226   assert(requested_eden_size > 0 && requested_survivor_size > 0,
 227          "just checking");
 228 
 229   space_invariants();
 230 
 231   // We require eden and to space to be empty
 232   if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) {
 233     return;
 234   }
 235 
 236   log_trace(gc, ergo)("PSYoungGen::resize_spaces(requested_eden_size: "
 237                       SIZE_FORMAT
 238                       ", requested_survivor_size: " SIZE_FORMAT ")",
 239                       requested_eden_size, requested_survivor_size);
 240   log_trace(gc, ergo)("    eden: [" PTR_FORMAT ".." PTR_FORMAT ") "
 241                       SIZE_FORMAT,
 242                       p2i(eden_space()->bottom()),
 243                       p2i(eden_space()->end()),
 244                       pointer_delta(eden_space()->end(), eden_space()->bottom(), sizeof(char)));
 245   log_trace(gc, ergo)("    from: [" PTR_FORMAT ".." PTR_FORMAT ") "
 246                       SIZE_FORMAT,
 247                       p2i(from_space()->bottom()),
 248                       p2i(from_space()->end()),
 249                       pointer_delta(from_space()->end(), from_space()->bottom(), sizeof(char)));
 250   log_trace(gc, ergo)("      to: [" PTR_FORMAT ".." PTR_FORMAT ") "
 251                       SIZE_FORMAT,
 252                       p2i(to_space()->bottom()),
 253                       p2i(to_space()->end()),
 254                       pointer_delta(  to_space()->end(), to_space()->bottom(), sizeof(char)));
 255 
 256   // There's nothing to do if the new sizes are the same as the current
 257   if (requested_survivor_size == to_space()->capacity_in_bytes() &&
 258       requested_survivor_size == from_space()->capacity_in_bytes() &&
 259       requested_eden_size == eden_space()->capacity_in_bytes()) {
 260     log_trace(gc, ergo)("    capacities are the right sizes, returning");
 261     return;
 262   }
 263 
 264   char* eden_start = (char*)virtual_space()->low();
 265   char* eden_end   = (char*)eden_space()->end();
 266   char* from_start = (char*)from_space()->bottom();
 267   char* from_end   = (char*)from_space()->end();
 268   char* to_start   = (char*)to_space()->bottom();
 269   char* to_end     = (char*)to_space()->end();
 270 
 271   assert(eden_start < from_start, "Cannot push into from_space");
 272 
 273   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 274   const bool maintain_minimum =
 275     (requested_eden_size + 2 * requested_survivor_size) <= min_gen_size();
 276 
 277   bool eden_from_to_order = from_start < to_start;
 278   // Check whether from space is below to space
 279   if (eden_from_to_order) {
 280     // Eden, from, to
 281 
 282     log_trace(gc, ergo)("  Eden, from, to:");
 283 
 284     // Set eden
 285     // "requested_eden_size" is a goal for the size of eden
 286     // and may not be attainable.  "eden_size" below is
 287     // calculated based on the location of from-space and
 288     // the goal for the size of eden.  from-space is
 289     // fixed in place because it contains live data.
 290     // The calculation is done this way to avoid 32bit
 291     // overflow (i.e., eden_start + requested_eden_size
 292     // may too large for representation in 32bits).
 293     size_t eden_size;
 294     if (maintain_minimum) {
 295       // Only make eden larger than the requested size if
 296       // the minimum size of the generation has to be maintained.
 297       // This could be done in general but policy at a higher
 298       // level is determining a requested size for eden and that
 299       // should be honored unless there is a fundamental reason.
 300       eden_size = pointer_delta(from_start,
 301                                 eden_start,
 302                                 sizeof(char));
 303     } else {
 304       eden_size = MIN2(requested_eden_size,
 305                        pointer_delta(from_start, eden_start, sizeof(char)));
 306     }
 307 
 308     eden_end = eden_start + eden_size;
 309     assert(eden_end >= eden_start, "addition overflowed");
 310 
 311     // To may resize into from space as long as it is clear of live data.
 312     // From space must remain page aligned, though, so we need to do some
 313     // extra calculations.
 314 
 315     // First calculate an optimal to-space
 316     to_end   = (char*)virtual_space()->high();
 317     to_start = (char*)pointer_delta(to_end,
 318                                     (char*)requested_survivor_size,
 319                                     sizeof(char));
 320 
 321     // Does the optimal to-space overlap from-space?
 322     if (to_start < (char*)from_space()->end()) {
 323       // Calculate the minimum offset possible for from_end
 324       size_t from_size =
 325         pointer_delta(from_space()->top(), from_start, sizeof(char));
 326 
 327       // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME!
 328       if (from_size == 0) {
 329         from_size = SpaceAlignment;
 330       } else {
 331         from_size = align_up(from_size, SpaceAlignment);
 332       }
 333 
 334       from_end = from_start + from_size;
 335       assert(from_end > from_start, "addition overflow or from_size problem");
 336 
 337       guarantee(from_end <= (char*)from_space()->end(),
 338         "from_end moved to the right");
 339 
 340       // Now update to_start with the new from_end
 341       to_start = MAX2(from_end, to_start);
 342     }
 343 
 344     guarantee(to_start != to_end, "to space is zero sized");
 345 
 346     log_trace(gc, ergo)("    [eden_start .. eden_end): "
 347                         "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 348                         p2i(eden_start),
 349                         p2i(eden_end),
 350                         pointer_delta(eden_end, eden_start, sizeof(char)));
 351     log_trace(gc, ergo)("    [from_start .. from_end): "
 352                         "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 353                         p2i(from_start),
 354                         p2i(from_end),
 355                         pointer_delta(from_end, from_start, sizeof(char)));
 356     log_trace(gc, ergo)("    [  to_start ..   to_end): "
 357                         "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 358                         p2i(to_start),
 359                         p2i(to_end),
 360                         pointer_delta(  to_end,   to_start, sizeof(char)));
 361   } else {
 362     // Eden, to, from
 363     log_trace(gc, ergo)("  Eden, to, from:");
 364 
 365     // To space gets priority over eden resizing. Note that we position
 366     // to space as if we were able to resize from space, even though from
 367     // space is not modified.
 368     // Giving eden priority was tried and gave poorer performance.
 369     to_end   = (char*)pointer_delta(virtual_space()->high(),
 370                                     (char*)requested_survivor_size,
 371                                     sizeof(char));
 372     to_end   = MIN2(to_end, from_start);
 373     to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size,
 374                                     sizeof(char));
 375     // if the space sizes are to be increased by several times then
 376     // 'to_start' will point beyond the young generation. In this case
 377     // 'to_start' should be adjusted.
 378     to_start = MAX2(to_start, eden_start + SpaceAlignment);
 379 
 380     // Compute how big eden can be, then adjust end.
 381     // See  comments above on calculating eden_end.
 382     size_t eden_size;
 383     if (maintain_minimum) {
 384       eden_size = pointer_delta(to_start, eden_start, sizeof(char));
 385     } else {
 386       eden_size = MIN2(requested_eden_size,
 387                        pointer_delta(to_start, eden_start, sizeof(char)));
 388     }
 389     eden_end = eden_start + eden_size;
 390     assert(eden_end >= eden_start, "addition overflowed");
 391 
 392     // Don't let eden shrink down to 0 or less.
 393     eden_end = MAX2(eden_end, eden_start + SpaceAlignment);
 394     to_start = MAX2(to_start, eden_end);
 395 
 396     log_trace(gc, ergo)("    [eden_start .. eden_end): "
 397                         "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 398                         p2i(eden_start),
 399                         p2i(eden_end),
 400                         pointer_delta(eden_end, eden_start, sizeof(char)));
 401     log_trace(gc, ergo)("    [  to_start ..   to_end): "
 402                         "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 403                         p2i(to_start),
 404                         p2i(to_end),
 405                         pointer_delta(  to_end,   to_start, sizeof(char)));
 406     log_trace(gc, ergo)("    [from_start .. from_end): "
 407                         "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
 408                         p2i(from_start),
 409                         p2i(from_end),
 410                         pointer_delta(from_end, from_start, sizeof(char)));
 411   }
 412 
 413 
 414   guarantee((HeapWord*)from_start <= from_space()->bottom(),
 415             "from start moved to the right");
 416   guarantee((HeapWord*)from_end >= from_space()->top(),
 417             "from end moved into live data");
 418   assert(is_object_aligned(eden_start), "checking alignment");
 419   assert(is_object_aligned(from_start), "checking alignment");
 420   assert(is_object_aligned(to_start), "checking alignment");
 421 
 422   MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end);
 423   MemRegion toMR  ((HeapWord*)to_start,   (HeapWord*)to_end);
 424   MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end);
 425 
 426   // Let's make sure the call to initialize doesn't reset "top"!
 427   DEBUG_ONLY(HeapWord* old_from_top = from_space()->top();)
 428 
 429   // For logging block  below
 430   size_t old_from = from_space()->capacity_in_bytes();
 431   size_t old_to   = to_space()->capacity_in_bytes();
 432 
 433   if (ZapUnusedHeapArea) {
 434     // NUMA is a special case because a numa space is not mangled
 435     // in order to not prematurely bind its address to memory to
 436     // the wrong memory (i.e., don't want the GC thread to first
 437     // touch the memory).  The survivor spaces are not numa
 438     // spaces and are mangled.
 439     if (UseNUMA) {
 440       if (eden_from_to_order) {
 441         mangle_survivors(from_space(), fromMR, to_space(), toMR);
 442       } else {
 443         mangle_survivors(to_space(), toMR, from_space(), fromMR);
 444       }
 445     }
 446 
 447     // If not mangling the spaces, do some checking to verify that
 448     // the spaces are already mangled.
 449     // The spaces should be correctly mangled at this point so
 450     // do some checking here. Note that they are not being mangled
 451     // in the calls to initialize().
 452     // Must check mangling before the spaces are reshaped.  Otherwise,
 453     // the bottom or end of one space may have moved into an area
 454     // covered by another space and a failure of the check may
 455     // not correctly indicate which space is not properly mangled.
 456 
 457     HeapWord* limit = (HeapWord*) virtual_space()->high();
 458     eden_space()->check_mangled_unused_area(limit);
 459     from_space()->check_mangled_unused_area(limit);
 460       to_space()->check_mangled_unused_area(limit);
 461   }
 462   // When an existing space is being initialized, it is not
 463   // mangled because the space has been previously mangled.
 464   eden_space()->initialize(edenMR,
 465                            SpaceDecorator::Clear,
 466                            SpaceDecorator::DontMangle);
 467     to_space()->initialize(toMR,
 468                            SpaceDecorator::Clear,
 469                            SpaceDecorator::DontMangle);
 470   from_space()->initialize(fromMR,
 471                            SpaceDecorator::DontClear,
 472                            SpaceDecorator::DontMangle);
 473 
 474   PSScavenge::set_young_generation_boundary(eden_space()->bottom());
 475 
 476   assert(from_space()->top() == old_from_top, "from top changed!");
 477 
 478   log_trace(gc, ergo)("AdaptiveSizePolicy::survivor space sizes: "
 479                 "collection: %d "
 480                 "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> "
 481                 "(" SIZE_FORMAT ", " SIZE_FORMAT ") ",
 482                 ParallelScavengeHeap::heap()->total_collections(),
 483                 old_from, old_to,
 484                 from_space()->capacity_in_bytes(),
 485                 to_space()->capacity_in_bytes());
 486 
 487     space_invariants();
 488 }
 489 void ASPSYoungGen::reset_after_change() {
 490   assert_locked_or_safepoint(Heap_lock);
 491 
 492   _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(),
 493                         (HeapWord*)virtual_space()->high_boundary());
 494   PSScavenge::set_subject_to_discovery_span(_reserved);
 495 
 496   HeapWord* new_eden_bottom = (HeapWord*)virtual_space()->low();
 497   HeapWord* eden_bottom = eden_space()->bottom();
 498   if (new_eden_bottom != eden_bottom) {
 499     MemRegion eden_mr(new_eden_bottom, eden_space()->end());
 500     eden_space()->initialize(eden_mr,
 501                              SpaceDecorator::Clear,
 502                              SpaceDecorator::Mangle);
 503     PSScavenge::set_young_generation_boundary(eden_space()->bottom());
 504   }
 505   MemRegion cmr((HeapWord*)virtual_space()->low(),
 506                 (HeapWord*)virtual_space()->high());
 507   ParallelScavengeHeap::heap()->barrier_set()->card_table()->resize_covered_region(cmr);
 508 
 509   space_invariants();
 510 }