hotspot/src/share/vm/gc_implementation/parallelScavenge/asPSYoungGen.cpp
changeset 1 489c9b5090e2
child 186 32e6c95f8d9b
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 2003-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  *
       
    23  */
       
    24 
       
    25 # include "incls/_precompiled.incl"
       
    26 # include "incls/_asPSYoungGen.cpp.incl"
       
    27 
       
    28 ASPSYoungGen::ASPSYoungGen(size_t init_byte_size,
       
    29                            size_t minimum_byte_size,
       
    30                            size_t byte_size_limit) :
       
    31   PSYoungGen(init_byte_size, minimum_byte_size, byte_size_limit),
       
    32   _gen_size_limit(byte_size_limit) {
       
    33 }
       
    34 
       
    35 
       
    36 ASPSYoungGen::ASPSYoungGen(PSVirtualSpace* vs,
       
    37                            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   PSYoungGen(vs->committed_size(), minimum_byte_size, byte_size_limit),
       
    42   _gen_size_limit(byte_size_limit) {
       
    43 
       
    44   assert(vs->committed_size() == init_byte_size, "Cannot replace with");
       
    45 
       
    46   _virtual_space = vs;
       
    47 }
       
    48 
       
    49 void ASPSYoungGen::initialize_virtual_space(ReservedSpace rs,
       
    50                                             size_t alignment) {
       
    51   assert(_init_gen_size != 0, "Should have a finite size");
       
    52   _virtual_space = new PSVirtualSpaceHighToLow(rs, alignment);
       
    53   if (!_virtual_space->expand_by(_init_gen_size)) {
       
    54     vm_exit_during_initialization("Could not reserve enough space for "
       
    55                                   "object heap");
       
    56   }
       
    57 }
       
    58 
       
    59 void ASPSYoungGen::initialize(ReservedSpace rs, size_t alignment) {
       
    60   initialize_virtual_space(rs, alignment);
       
    61   initialize_work();
       
    62 }
       
    63 
       
    64 size_t ASPSYoungGen::available_for_expansion() {
       
    65 
       
    66   size_t current_committed_size = virtual_space()->committed_size();
       
    67   assert((gen_size_limit() >= current_committed_size),
       
    68     "generation size limit is wrong");
       
    69   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
       
    70   size_t result =  gen_size_limit() - current_committed_size;
       
    71   size_t result_aligned = align_size_down(result, heap->young_gen_alignment());
       
    72   return result_aligned;
       
    73 }
       
    74 
       
    75 // Return the number of bytes the young gen is willing give up.
       
    76 //
       
    77 // Future implementations could check the survivors and if to_space is in the
       
    78 // right place (below from_space), take a chunk from to_space.
       
    79 size_t ASPSYoungGen::available_for_contraction() {
       
    80 
       
    81   size_t uncommitted_bytes = virtual_space()->uncommitted_size();
       
    82   if (uncommitted_bytes != 0) {
       
    83     return uncommitted_bytes;
       
    84   }
       
    85 
       
    86   if (eden_space()->is_empty()) {
       
    87     // Respect the minimum size for eden and for the young gen as a whole.
       
    88     ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
       
    89     const size_t eden_alignment = heap->intra_generation_alignment();
       
    90     const size_t gen_alignment = heap->young_gen_alignment();
       
    91 
       
    92     assert(eden_space()->capacity_in_bytes() >= eden_alignment,
       
    93       "Alignment is wrong");
       
    94     size_t eden_avail = eden_space()->capacity_in_bytes() - eden_alignment;
       
    95     eden_avail = align_size_down(eden_avail, gen_alignment);
       
    96 
       
    97     assert(virtual_space()->committed_size() >= min_gen_size(),
       
    98       "minimum gen size is wrong");
       
    99     size_t gen_avail = virtual_space()->committed_size() - min_gen_size();
       
   100     assert(virtual_space()->is_aligned(gen_avail), "not aligned");
       
   101 
       
   102     const size_t max_contraction = MIN2(eden_avail, gen_avail);
       
   103     // See comment for ASPSOldGen::available_for_contraction()
       
   104     // for reasons the "increment" fraction is used.
       
   105     PSAdaptiveSizePolicy* policy = heap->size_policy();
       
   106     size_t result = policy->eden_increment_aligned_down(max_contraction);
       
   107     size_t result_aligned = align_size_down(result, gen_alignment);
       
   108     if (PrintAdaptiveSizePolicy && Verbose) {
       
   109       gclog_or_tty->print_cr("ASPSYoungGen::available_for_contraction: %d K",
       
   110         result_aligned/K);
       
   111       gclog_or_tty->print_cr("  max_contraction %d K", max_contraction/K);
       
   112       gclog_or_tty->print_cr("  eden_avail %d K", eden_avail/K);
       
   113       gclog_or_tty->print_cr("  gen_avail %d K", gen_avail/K);
       
   114     }
       
   115     return result_aligned;
       
   116 
       
   117   }
       
   118 
       
   119   return 0;
       
   120 }
       
   121 
       
   122 // The current implementation only considers to the end of eden.
       
   123 // If to_space is below from_space, to_space is not considered.
       
   124 // to_space can be.
       
   125 size_t ASPSYoungGen::available_to_live() {
       
   126   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
       
   127   const size_t alignment = heap->intra_generation_alignment();
       
   128 
       
   129   // Include any space that is committed but is not in eden.
       
   130   size_t available = pointer_delta(eden_space()->bottom(),
       
   131                                    virtual_space()->low(),
       
   132                                    sizeof(char));
       
   133 
       
   134   const size_t eden_capacity = eden_space()->capacity_in_bytes();
       
   135   if (eden_space()->is_empty() && eden_capacity > alignment) {
       
   136     available += eden_capacity - alignment;
       
   137   }
       
   138   return available;
       
   139 }
       
   140 
       
   141 // Similar to PSYoungGen::resize_generation() but
       
   142 //  allows sum of eden_size and 2 * survivor_size to exceed _max_gen_size
       
   143 //  expands at the low end of the virtual space
       
   144 //  moves the boundary between the generations in order to expand
       
   145 //  some additional diagnostics
       
   146 // If no additional changes are required, this can be deleted
       
   147 // and the changes factored back into PSYoungGen::resize_generation().
       
   148 bool ASPSYoungGen::resize_generation(size_t eden_size, size_t survivor_size) {
       
   149   const size_t alignment = virtual_space()->alignment();
       
   150   size_t orig_size = virtual_space()->committed_size();
       
   151   bool size_changed = false;
       
   152 
       
   153   // There used to be a guarantee here that
       
   154   //   (eden_size + 2*survivor_size)  <= _max_gen_size
       
   155   // This requirement is enforced by the calculation of desired_size
       
   156   // below.  It may not be true on entry since the size of the
       
   157   // eden_size is no bounded by the generation size.
       
   158 
       
   159   assert(max_size() == reserved().byte_size(), "max gen size problem?");
       
   160   assert(min_gen_size() <= orig_size && orig_size <= max_size(),
       
   161          "just checking");
       
   162 
       
   163   // Adjust new generation size
       
   164   const size_t eden_plus_survivors =
       
   165     align_size_up(eden_size + 2 * survivor_size, alignment);
       
   166   size_t desired_size = MAX2(MIN2(eden_plus_survivors, gen_size_limit()),
       
   167                              min_gen_size());
       
   168   assert(desired_size <= gen_size_limit(), "just checking");
       
   169 
       
   170   if (desired_size > orig_size) {
       
   171     // Grow the generation
       
   172     size_t change = desired_size - orig_size;
       
   173     if (!virtual_space()->expand_by(change)) {
       
   174       return false;
       
   175     }
       
   176     size_changed = true;
       
   177   } else if (desired_size < orig_size) {
       
   178     size_t desired_change = orig_size - desired_size;
       
   179 
       
   180     // How much is available for shrinking.
       
   181     size_t available_bytes = limit_gen_shrink(desired_change);
       
   182     size_t change = MIN2(desired_change, available_bytes);
       
   183     virtual_space()->shrink_by(change);
       
   184     size_changed = true;
       
   185   } else {
       
   186     if (Verbose && PrintGC) {
       
   187       if (orig_size == gen_size_limit()) {
       
   188         gclog_or_tty->print_cr("ASPSYoung generation size at maximum: "
       
   189           SIZE_FORMAT "K", orig_size/K);
       
   190       } else if (orig_size == min_gen_size()) {
       
   191         gclog_or_tty->print_cr("ASPSYoung generation size at minium: "
       
   192           SIZE_FORMAT "K", orig_size/K);
       
   193       }
       
   194     }
       
   195   }
       
   196 
       
   197   if (size_changed) {
       
   198     reset_after_change();
       
   199     if (Verbose && PrintGC) {
       
   200       size_t current_size  = virtual_space()->committed_size();
       
   201       gclog_or_tty->print_cr("ASPSYoung generation size changed: "
       
   202         SIZE_FORMAT "K->" SIZE_FORMAT "K",
       
   203         orig_size/K, current_size/K);
       
   204     }
       
   205   }
       
   206 
       
   207   guarantee(eden_plus_survivors <= virtual_space()->committed_size() ||
       
   208             virtual_space()->committed_size() == max_size(), "Sanity");
       
   209 
       
   210   return true;
       
   211 }
       
   212 
       
   213 // Similar to PSYoungGen::resize_spaces() but
       
   214 //  eden always starts at the low end of the committed virtual space
       
   215 //  current implementation does not allow holes between the spaces
       
   216 //  _young_generation_boundary has to be reset because it changes.
       
   217 //  so additional verification
       
   218 void ASPSYoungGen::resize_spaces(size_t requested_eden_size,
       
   219                                  size_t requested_survivor_size) {
       
   220   assert(requested_eden_size > 0 && requested_survivor_size > 0,
       
   221          "just checking");
       
   222 
       
   223   space_invariants();
       
   224 
       
   225   // We require eden and to space to be empty
       
   226   if ((!eden_space()->is_empty()) || (!to_space()->is_empty())) {
       
   227     return;
       
   228   }
       
   229 
       
   230   if (PrintAdaptiveSizePolicy && Verbose) {
       
   231     gclog_or_tty->print_cr("PSYoungGen::resize_spaces(requested_eden_size: "
       
   232                   SIZE_FORMAT
       
   233                   ", requested_survivor_size: " SIZE_FORMAT ")",
       
   234                   requested_eden_size, requested_survivor_size);
       
   235     gclog_or_tty->print_cr("    eden: [" PTR_FORMAT ".." PTR_FORMAT ") "
       
   236                   SIZE_FORMAT,
       
   237                   eden_space()->bottom(),
       
   238                   eden_space()->end(),
       
   239                   pointer_delta(eden_space()->end(),
       
   240                                 eden_space()->bottom(),
       
   241                                 sizeof(char)));
       
   242     gclog_or_tty->print_cr("    from: [" PTR_FORMAT ".." PTR_FORMAT ") "
       
   243                   SIZE_FORMAT,
       
   244                   from_space()->bottom(),
       
   245                   from_space()->end(),
       
   246                   pointer_delta(from_space()->end(),
       
   247                                 from_space()->bottom(),
       
   248                                 sizeof(char)));
       
   249     gclog_or_tty->print_cr("      to: [" PTR_FORMAT ".." PTR_FORMAT ") "
       
   250                   SIZE_FORMAT,
       
   251                   to_space()->bottom(),
       
   252                   to_space()->end(),
       
   253                   pointer_delta(  to_space()->end(),
       
   254                                   to_space()->bottom(),
       
   255                                   sizeof(char)));
       
   256   }
       
   257 
       
   258   // There's nothing to do if the new sizes are the same as the current
       
   259   if (requested_survivor_size == to_space()->capacity_in_bytes() &&
       
   260       requested_survivor_size == from_space()->capacity_in_bytes() &&
       
   261       requested_eden_size == eden_space()->capacity_in_bytes()) {
       
   262     if (PrintAdaptiveSizePolicy && Verbose) {
       
   263       gclog_or_tty->print_cr("    capacities are the right sizes, returning");
       
   264     }
       
   265     return;
       
   266   }
       
   267 
       
   268   char* eden_start = (char*)virtual_space()->low();
       
   269   char* eden_end   = (char*)eden_space()->end();
       
   270   char* from_start = (char*)from_space()->bottom();
       
   271   char* from_end   = (char*)from_space()->end();
       
   272   char* to_start   = (char*)to_space()->bottom();
       
   273   char* to_end     = (char*)to_space()->end();
       
   274 
       
   275   assert(eden_start < from_start, "Cannot push into from_space");
       
   276 
       
   277   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
       
   278   const size_t alignment = heap->intra_generation_alignment();
       
   279 
       
   280   // Check whether from space is below to space
       
   281   if (from_start < to_start) {
       
   282     // Eden, from, to
       
   283     if (PrintAdaptiveSizePolicy && Verbose) {
       
   284       gclog_or_tty->print_cr("  Eden, from, to:");
       
   285     }
       
   286 
       
   287     // Set eden
       
   288     // Compute how big eden can be, then adjust end.
       
   289     // See comment in PSYoungGen::resize_spaces() on
       
   290     // calculating eden_end.
       
   291     const size_t eden_size = MIN2(requested_eden_size,
       
   292                                   pointer_delta(from_start,
       
   293                                                 eden_start,
       
   294                                                 sizeof(char)));
       
   295     eden_end = eden_start + eden_size;
       
   296     assert(eden_end >= eden_start, "addition overflowed")
       
   297 
       
   298     // To may resize into from space as long as it is clear of live data.
       
   299     // From space must remain page aligned, though, so we need to do some
       
   300     // extra calculations.
       
   301 
       
   302     // First calculate an optimal to-space
       
   303     to_end   = (char*)virtual_space()->high();
       
   304     to_start = (char*)pointer_delta(to_end,
       
   305                                     (char*)requested_survivor_size,
       
   306                                     sizeof(char));
       
   307 
       
   308     // Does the optimal to-space overlap from-space?
       
   309     if (to_start < (char*)from_space()->end()) {
       
   310       assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
       
   311 
       
   312       // Calculate the minimum offset possible for from_end
       
   313       size_t from_size =
       
   314         pointer_delta(from_space()->top(), from_start, sizeof(char));
       
   315 
       
   316       // Should we be in this method if from_space is empty? Why not the set_space method? FIX ME!
       
   317       if (from_size == 0) {
       
   318         from_size = alignment;
       
   319       } else {
       
   320         from_size = align_size_up(from_size, alignment);
       
   321       }
       
   322 
       
   323       from_end = from_start + from_size;
       
   324       assert(from_end > from_start, "addition overflow or from_size problem");
       
   325 
       
   326       guarantee(from_end <= (char*)from_space()->end(),
       
   327         "from_end moved to the right");
       
   328 
       
   329       // Now update to_start with the new from_end
       
   330       to_start = MAX2(from_end, to_start);
       
   331     }
       
   332 
       
   333     guarantee(to_start != to_end, "to space is zero sized");
       
   334 
       
   335     if (PrintAdaptiveSizePolicy && Verbose) {
       
   336       gclog_or_tty->print_cr("    [eden_start .. eden_end): "
       
   337                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
       
   338                     eden_start,
       
   339                     eden_end,
       
   340                     pointer_delta(eden_end, eden_start, sizeof(char)));
       
   341       gclog_or_tty->print_cr("    [from_start .. from_end): "
       
   342                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
       
   343                     from_start,
       
   344                     from_end,
       
   345                     pointer_delta(from_end, from_start, sizeof(char)));
       
   346       gclog_or_tty->print_cr("    [  to_start ..   to_end): "
       
   347                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
       
   348                     to_start,
       
   349                     to_end,
       
   350                     pointer_delta(  to_end,   to_start, sizeof(char)));
       
   351     }
       
   352   } else {
       
   353     // Eden, to, from
       
   354     if (PrintAdaptiveSizePolicy && Verbose) {
       
   355       gclog_or_tty->print_cr("  Eden, to, from:");
       
   356     }
       
   357 
       
   358     // To space gets priority over eden resizing. Note that we position
       
   359     // to space as if we were able to resize from space, even though from
       
   360     // space is not modified.
       
   361     // Giving eden priority was tried and gave poorer performance.
       
   362     to_end   = (char*)pointer_delta(virtual_space()->high(),
       
   363                                     (char*)requested_survivor_size,
       
   364                                     sizeof(char));
       
   365     to_end   = MIN2(to_end, from_start);
       
   366     to_start = (char*)pointer_delta(to_end, (char*)requested_survivor_size,
       
   367                                     sizeof(char));
       
   368     // if the space sizes are to be increased by several times then
       
   369     // 'to_start' will point beyond the young generation. In this case
       
   370     // 'to_start' should be adjusted.
       
   371     to_start = MAX2(to_start, eden_start + alignment);
       
   372 
       
   373     // Compute how big eden can be, then adjust end.
       
   374     // See comment in PSYoungGen::resize_spaces() on
       
   375     // calculating eden_end.
       
   376     const size_t eden_size = MIN2(requested_eden_size,
       
   377                                   pointer_delta(to_start,
       
   378                                                 eden_start,
       
   379                                                 sizeof(char)));
       
   380     eden_end = eden_start + eden_size;
       
   381     assert(eden_end >= eden_start, "addition overflowed")
       
   382 
       
   383     // Don't let eden shrink down to 0 or less.
       
   384     eden_end = MAX2(eden_end, eden_start + alignment);
       
   385     to_start = MAX2(to_start, eden_end);
       
   386 
       
   387     if (PrintAdaptiveSizePolicy && Verbose) {
       
   388       gclog_or_tty->print_cr("    [eden_start .. eden_end): "
       
   389                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
       
   390                     eden_start,
       
   391                     eden_end,
       
   392                     pointer_delta(eden_end, eden_start, sizeof(char)));
       
   393       gclog_or_tty->print_cr("    [  to_start ..   to_end): "
       
   394                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
       
   395                     to_start,
       
   396                     to_end,
       
   397                     pointer_delta(  to_end,   to_start, sizeof(char)));
       
   398       gclog_or_tty->print_cr("    [from_start .. from_end): "
       
   399                     "[" PTR_FORMAT " .. " PTR_FORMAT ") " SIZE_FORMAT,
       
   400                     from_start,
       
   401                     from_end,
       
   402                     pointer_delta(from_end, from_start, sizeof(char)));
       
   403     }
       
   404   }
       
   405 
       
   406 
       
   407   guarantee((HeapWord*)from_start <= from_space()->bottom(),
       
   408             "from start moved to the right");
       
   409   guarantee((HeapWord*)from_end >= from_space()->top(),
       
   410             "from end moved into live data");
       
   411   assert(is_object_aligned((intptr_t)eden_start), "checking alignment");
       
   412   assert(is_object_aligned((intptr_t)from_start), "checking alignment");
       
   413   assert(is_object_aligned((intptr_t)to_start), "checking alignment");
       
   414 
       
   415   MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)eden_end);
       
   416   MemRegion toMR  ((HeapWord*)to_start,   (HeapWord*)to_end);
       
   417   MemRegion fromMR((HeapWord*)from_start, (HeapWord*)from_end);
       
   418 
       
   419   // Let's make sure the call to initialize doesn't reset "top"!
       
   420   DEBUG_ONLY(HeapWord* old_from_top = from_space()->top();)
       
   421 
       
   422   // For PrintAdaptiveSizePolicy block  below
       
   423   size_t old_from = from_space()->capacity_in_bytes();
       
   424   size_t old_to   = to_space()->capacity_in_bytes();
       
   425 
       
   426   eden_space()->initialize(edenMR, true);
       
   427     to_space()->initialize(toMR  , true);
       
   428   from_space()->initialize(fromMR, false);     // Note, not cleared!
       
   429   PSScavenge::set_young_generation_boundary(eden_space()->bottom());
       
   430 
       
   431   assert(from_space()->top() == old_from_top, "from top changed!");
       
   432 
       
   433   if (PrintAdaptiveSizePolicy) {
       
   434     ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
       
   435     assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
       
   436 
       
   437     gclog_or_tty->print("AdaptiveSizePolicy::survivor space sizes: "
       
   438                   "collection: %d "
       
   439                   "(" SIZE_FORMAT ", " SIZE_FORMAT ") -> "
       
   440                   "(" SIZE_FORMAT ", " SIZE_FORMAT ") ",
       
   441                   heap->total_collections(),
       
   442                   old_from, old_to,
       
   443                   from_space()->capacity_in_bytes(),
       
   444                   to_space()->capacity_in_bytes());
       
   445     gclog_or_tty->cr();
       
   446   }
       
   447   space_invariants();
       
   448 }
       
   449 
       
   450 void ASPSYoungGen::reset_after_change() {
       
   451   assert_locked_or_safepoint(Heap_lock);
       
   452 
       
   453   _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(),
       
   454                         (HeapWord*)virtual_space()->high_boundary());
       
   455   PSScavenge::reference_processor()->set_span(_reserved);
       
   456 
       
   457   HeapWord* new_eden_bottom = (HeapWord*)virtual_space()->low();
       
   458   HeapWord* eden_bottom = eden_space()->bottom();
       
   459   if (new_eden_bottom != eden_bottom) {
       
   460     MemRegion eden_mr(new_eden_bottom, eden_space()->end());
       
   461     eden_space()->initialize(eden_mr, true);
       
   462     PSScavenge::set_young_generation_boundary(eden_space()->bottom());
       
   463   }
       
   464   MemRegion cmr((HeapWord*)virtual_space()->low(),
       
   465                 (HeapWord*)virtual_space()->high());
       
   466   Universe::heap()->barrier_set()->resize_covered_region(cmr);
       
   467 
       
   468   space_invariants();
       
   469 }