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