1 /*
   2  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc_implementation/parallelScavenge/generationSizer.hpp"
  27 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
  28 #include "gc_implementation/parallelScavenge/psAdaptiveSizePolicy.hpp"
  29 #include "gc_implementation/parallelScavenge/psGCAdaptivePolicyCounters.hpp"
  30 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
  31 #include "gc_implementation/shared/gcPolicyCounters.hpp"
  32 #include "gc_interface/gcCause.hpp"
  33 #include "memory/collectorPolicy.hpp"
  34 #include "runtime/timer.hpp"
  35 #include "utilities/top.hpp"
  36 
  37 #include <math.h>
  38 
  39 PSAdaptiveSizePolicy::PSAdaptiveSizePolicy(size_t init_eden_size,
  40                                            size_t init_promo_size,
  41                                            size_t init_survivor_size,
  42                                            size_t intra_generation_alignment,
  43                                            double gc_pause_goal_sec,
  44                                            double gc_minor_pause_goal_sec,
  45                                            uint gc_cost_ratio) :
  46      AdaptiveSizePolicy(init_eden_size,
  47                         init_promo_size,
  48                         init_survivor_size,
  49                         gc_pause_goal_sec,
  50                         gc_cost_ratio),
  51      _collection_cost_margin_fraction(AdaptiveSizePolicyCollectionCostMargin/
  52        100.0),
  53      _intra_generation_alignment(intra_generation_alignment),
  54      _live_at_last_full_gc(init_promo_size),
  55      _gc_minor_pause_goal_sec(gc_minor_pause_goal_sec),
  56      _latest_major_mutator_interval_seconds(0),
  57      _young_gen_change_for_major_pause_count(0)
  58 {
  59   // Sizing policy statistics
  60   _avg_major_pause    =
  61     new AdaptivePaddedAverage(AdaptiveTimeWeight, PausePadding);
  62   _avg_minor_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
  63   _avg_major_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
  64 
  65   _avg_base_footprint = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
  66   _major_pause_old_estimator =
  67     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
  68   _major_pause_young_estimator =
  69     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
  70   _major_collection_estimator =
  71     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
  72 
  73   _young_gen_size_increment_supplement = YoungGenerationSizeSupplement;
  74   _old_gen_size_increment_supplement = TenuredGenerationSizeSupplement;
  75 
  76   // Start the timers
  77   _major_timer.start();
  78 
  79   _old_gen_policy_is_ready = false;
  80 }
  81 
  82 size_t PSAdaptiveSizePolicy::calculate_free_based_on_live(size_t live, uintx ratio_as_percentage) {
  83   // We want to calculate how much free memory there can be based on the
  84   // amount of live data currently in the old gen. Using the formula:
  85   // ratio * (free + live) = free
  86   // Some equation solving later we get:
  87   // free = (live * ratio) / (1 - ratio)
  88 
  89   const double ratio = ratio_as_percentage / 100.0;
  90   const double ratio_inverse = 1.0 - ratio;
  91   const double tmp = live * ratio;
  92   size_t free = (size_t)(tmp / ratio_inverse);
  93 
  94   return free;
  95 }
  96 
  97 size_t PSAdaptiveSizePolicy::calculated_old_free_size_in_bytes() const {
  98   size_t free_size = (size_t)(_promo_size + avg_promoted()->padded_average());
  99   size_t live = ParallelScavengeHeap::heap()->old_gen()->used_in_bytes();
 100 
 101   if (MinHeapFreeRatio != 0) {
 102     size_t min_free = calculate_free_based_on_live(live, MinHeapFreeRatio);
 103     free_size = MAX2(free_size, min_free);
 104   }
 105 
 106   if (MaxHeapFreeRatio != 100) {
 107     size_t max_free = calculate_free_based_on_live(live, MaxHeapFreeRatio);
 108     free_size = MIN2(max_free, free_size);
 109   }
 110 
 111   return free_size;
 112 }
 113 
 114 void PSAdaptiveSizePolicy::major_collection_begin() {
 115   // Update the interval time
 116   _major_timer.stop();
 117   // Save most recent collection time
 118   _latest_major_mutator_interval_seconds = _major_timer.seconds();
 119   _major_timer.reset();
 120   _major_timer.start();
 121 }
 122 
 123 void PSAdaptiveSizePolicy::update_minor_pause_old_estimator(
 124     double minor_pause_in_ms) {
 125   double promo_size_in_mbytes = ((double)_promo_size)/((double)M);
 126   _minor_pause_old_estimator->update(promo_size_in_mbytes,
 127     minor_pause_in_ms);
 128 }
 129 
 130 void PSAdaptiveSizePolicy::major_collection_end(size_t amount_live,
 131   GCCause::Cause gc_cause) {
 132   // Update the pause time.
 133   _major_timer.stop();
 134 
 135   if (gc_cause != GCCause::_java_lang_system_gc ||
 136       UseAdaptiveSizePolicyWithSystemGC) {
 137     double major_pause_in_seconds = _major_timer.seconds();
 138     double major_pause_in_ms = major_pause_in_seconds * MILLIUNITS;
 139 
 140     // Sample for performance counter
 141     _avg_major_pause->sample(major_pause_in_seconds);
 142 
 143     // Cost of collection (unit-less)
 144     double collection_cost = 0.0;
 145     if ((_latest_major_mutator_interval_seconds > 0.0) &&
 146         (major_pause_in_seconds > 0.0)) {
 147       double interval_in_seconds =
 148         _latest_major_mutator_interval_seconds + major_pause_in_seconds;
 149       collection_cost =
 150         major_pause_in_seconds / interval_in_seconds;
 151       avg_major_gc_cost()->sample(collection_cost);
 152 
 153       // Sample for performance counter
 154       _avg_major_interval->sample(interval_in_seconds);
 155     }
 156 
 157     // Calculate variables used to estimate pause time vs. gen sizes
 158     double eden_size_in_mbytes = ((double)_eden_size)/((double)M);
 159     double promo_size_in_mbytes = ((double)_promo_size)/((double)M);
 160     _major_pause_old_estimator->update(promo_size_in_mbytes,
 161       major_pause_in_ms);
 162     _major_pause_young_estimator->update(eden_size_in_mbytes,
 163       major_pause_in_ms);
 164 
 165     if (PrintAdaptiveSizePolicy && Verbose) {
 166       gclog_or_tty->print("psAdaptiveSizePolicy::major_collection_end: "
 167         "major gc cost: %f  average: %f", collection_cost,
 168         avg_major_gc_cost()->average());
 169       gclog_or_tty->print_cr("  major pause: %f major period %f",
 170         major_pause_in_ms,
 171         _latest_major_mutator_interval_seconds * MILLIUNITS);
 172     }
 173 
 174     // Calculate variable used to estimate collection cost vs. gen sizes
 175     assert(collection_cost >= 0.0, "Expected to be non-negative");
 176     _major_collection_estimator->update(promo_size_in_mbytes,
 177         collection_cost);
 178   }
 179 
 180   // Update the amount live at the end of a full GC
 181   _live_at_last_full_gc = amount_live;
 182 
 183   // The policy does not have enough data until at least some major collections
 184   // have been done.
 185   if (_avg_major_pause->count() >= AdaptiveSizePolicyReadyThreshold) {
 186     _old_gen_policy_is_ready = true;
 187   }
 188 
 189   // Interval times use this timer to measure the interval that
 190   // the mutator runs.  Reset after the GC pause has been measured.
 191   _major_timer.reset();
 192   _major_timer.start();
 193 }
 194 
 195 // If the remaining free space in the old generation is less that
 196 // that expected to be needed by the next collection, do a full
 197 // collection now.
 198 bool PSAdaptiveSizePolicy::should_full_GC(size_t old_free_in_bytes) {
 199 
 200   // A similar test is done in the scavenge's should_attempt_scavenge().  If
 201   // this is changed, decide if that test should also be changed.
 202   bool result = padded_average_promoted_in_bytes() > (float) old_free_in_bytes;
 203   if (PrintGCDetails && Verbose) {
 204     if (result) {
 205       gclog_or_tty->print("  full after scavenge: ");
 206     } else {
 207       gclog_or_tty->print("  no full after scavenge: ");
 208     }
 209     gclog_or_tty->print_cr(" average_promoted " SIZE_FORMAT
 210       " padded_average_promoted " SIZE_FORMAT
 211       " free in old gen " SIZE_FORMAT,
 212       (size_t) average_promoted_in_bytes(),
 213       (size_t) padded_average_promoted_in_bytes(),
 214       old_free_in_bytes);
 215   }
 216   return result;
 217 }
 218 
 219 void PSAdaptiveSizePolicy::clear_generation_free_space_flags() {
 220 
 221   AdaptiveSizePolicy::clear_generation_free_space_flags();
 222 
 223   set_change_old_gen_for_min_pauses(0);
 224 
 225   set_change_young_gen_for_maj_pauses(0);
 226 }
 227 
 228 // If this is not a full GC, only test and modify the young generation.
 229 
 230 void PSAdaptiveSizePolicy::compute_generation_free_space(
 231                                            size_t young_live,
 232                                            size_t eden_live,
 233                                            size_t old_live,
 234                                            size_t perm_live,
 235                                            size_t cur_eden,
 236                                            size_t max_old_gen_size,
 237                                            size_t max_eden_size,
 238                                            bool   is_full_gc,
 239                                            GCCause::Cause gc_cause,
 240                                            CollectorPolicy* collector_policy) {
 241 
 242   // Update statistics
 243   // Time statistics are updated as we go, update footprint stats here
 244   _avg_base_footprint->sample(BaseFootPrintEstimate + perm_live);
 245   avg_young_live()->sample(young_live);
 246   avg_eden_live()->sample(eden_live);
 247   if (is_full_gc) {
 248     // old_live is only accurate after a full gc
 249     avg_old_live()->sample(old_live);
 250   }
 251 
 252   // This code used to return if the policy was not ready , i.e.,
 253   // policy_is_ready() returning false.  The intent was that
 254   // decisions below needed major collection times and so could
 255   // not be made before two major collections.  A consequence was
 256   // adjustments to the young generation were not done until after
 257   // two major collections even if the minor collections times
 258   // exceeded the requested goals.  Now let the young generation
 259   // adjust for the minor collection times.  Major collection times
 260   // will be zero for the first collection and will naturally be
 261   // ignored.  Tenured generation adjustments are only made at the
 262   // full collections so until the second major collection has
 263   // been reached, no tenured generation adjustments will be made.
 264 
 265   // Until we know better, desired promotion size uses the last calculation
 266   size_t desired_promo_size = _promo_size;
 267 
 268   // Start eden at the current value.  The desired value that is stored
 269   // in _eden_size is not bounded by constraints of the heap and can
 270   // run away.
 271   //
 272   // As expected setting desired_eden_size to the current
 273   // value of desired_eden_size as a starting point
 274   // caused desired_eden_size to grow way too large and caused
 275   // an overflow down stream.  It may have improved performance in
 276   // some case but is dangerous.
 277   size_t desired_eden_size = cur_eden;
 278 
 279 #ifdef ASSERT
 280   size_t original_promo_size = desired_promo_size;
 281   size_t original_eden_size = desired_eden_size;
 282 #endif
 283 
 284   // Cache some values. There's a bit of work getting these, so
 285   // we might save a little time.
 286   const double major_cost = major_gc_cost();
 287   const double minor_cost = minor_gc_cost();
 288 
 289   // Used for diagnostics
 290   clear_generation_free_space_flags();
 291 
 292   // Limits on our growth
 293   size_t promo_limit = (size_t)(max_old_gen_size - avg_old_live()->average());
 294 
 295   // This method sets the desired eden size.  That plus the
 296   // desired survivor space sizes sets the desired young generation
 297   // size.  This methods does not know what the desired survivor
 298   // size is but expects that other policy will attempt to make
 299   // the survivor sizes compatible with the live data in the
 300   // young generation.  This limit is an estimate of the space left
 301   // in the young generation after the survivor spaces have been
 302   // subtracted out.
 303   size_t eden_limit = max_eden_size;
 304 
 305   // But don't force a promo size below the current promo size. Otherwise,
 306   // the promo size will shrink for no good reason.
 307   promo_limit = MAX2(promo_limit, _promo_size);
 308 
 309   const double gc_cost_limit = GCTimeLimit/100.0;
 310 
 311   // Which way should we go?
 312   // if pause requirement is not met
 313   //   adjust size of any generation with average paus exceeding
 314   //   the pause limit.  Adjust one pause at a time (the larger)
 315   //   and only make adjustments for the major pause at full collections.
 316   // else if throughput requirement not met
 317   //   adjust the size of the generation with larger gc time.  Only
 318   //   adjust one generation at a time.
 319   // else
 320   //   adjust down the total heap size.  Adjust down the larger of the
 321   //   generations.
 322 
 323   // Add some checks for a threshhold for a change.  For example,
 324   // a change less than the necessary alignment is probably not worth
 325   // attempting.
 326 
 327 
 328   if ((_avg_minor_pause->padded_average() > gc_pause_goal_sec()) ||
 329       (_avg_major_pause->padded_average() > gc_pause_goal_sec())) {
 330     //
 331     // Check pauses
 332     //
 333     // Make changes only to affect one of the pauses (the larger)
 334     // at a time.
 335     adjust_for_pause_time(is_full_gc, &desired_promo_size, &desired_eden_size);
 336 
 337   } else if (_avg_minor_pause->padded_average() > gc_minor_pause_goal_sec()) {
 338     // Adjust only for the minor pause time goal
 339     adjust_for_minor_pause_time(is_full_gc, &desired_promo_size, &desired_eden_size);
 340 
 341   } else if(adjusted_mutator_cost() < _throughput_goal) {
 342     // This branch used to require that (mutator_cost() > 0.0 in 1.4.2.
 343     // This sometimes resulted in skipping to the minimize footprint
 344     // code.  Change this to try and reduce GC time if mutator time is
 345     // negative for whatever reason.  Or for future consideration,
 346     // bail out of the code if mutator time is negative.
 347     //
 348     // Throughput
 349     //
 350     assert(major_cost >= 0.0, "major cost is < 0.0");
 351     assert(minor_cost >= 0.0, "minor cost is < 0.0");
 352     // Try to reduce the GC times.
 353     adjust_for_throughput(is_full_gc, &desired_promo_size, &desired_eden_size);
 354 
 355   } else {
 356 
 357     // Be conservative about reducing the footprint.
 358     //   Do a minimum number of major collections first.
 359     //   Have reasonable averages for major and minor collections costs.
 360     if (UseAdaptiveSizePolicyFootprintGoal &&
 361         young_gen_policy_is_ready() &&
 362         avg_major_gc_cost()->average() >= 0.0 &&
 363         avg_minor_gc_cost()->average() >= 0.0) {
 364       size_t desired_sum = desired_eden_size + desired_promo_size;
 365       desired_eden_size = adjust_eden_for_footprint(desired_eden_size,
 366                                                     desired_sum);
 367       if (is_full_gc) {
 368         set_decide_at_full_gc(decide_at_full_gc_true);
 369         desired_promo_size = adjust_promo_for_footprint(desired_promo_size,
 370                                                         desired_sum);
 371       }
 372     }
 373   }
 374 
 375   // Note we make the same tests as in the code block below;  the code
 376   // seems a little easier to read with the printing in another block.
 377   if (PrintAdaptiveSizePolicy) {
 378     if (desired_promo_size > promo_limit)  {
 379       // "free_in_old_gen" was the original value for used for promo_limit
 380       size_t free_in_old_gen = (size_t)(max_old_gen_size - avg_old_live()->average());
 381       gclog_or_tty->print_cr(
 382             "PSAdaptiveSizePolicy::compute_generation_free_space limits:"
 383             " desired_promo_size: " SIZE_FORMAT
 384             " promo_limit: " SIZE_FORMAT
 385             " free_in_old_gen: " SIZE_FORMAT
 386             " max_old_gen_size: " SIZE_FORMAT
 387             " avg_old_live: " SIZE_FORMAT,
 388             desired_promo_size, promo_limit, free_in_old_gen,
 389             max_old_gen_size, (size_t) avg_old_live()->average());
 390     }
 391     if (desired_eden_size > eden_limit) {
 392       gclog_or_tty->print_cr(
 393             "AdaptiveSizePolicy::compute_generation_free_space limits:"
 394             " desired_eden_size: " SIZE_FORMAT
 395             " old_eden_size: " SIZE_FORMAT
 396             " eden_limit: " SIZE_FORMAT
 397             " cur_eden: " SIZE_FORMAT
 398             " max_eden_size: " SIZE_FORMAT
 399             " avg_young_live: " SIZE_FORMAT,
 400             desired_eden_size, _eden_size, eden_limit, cur_eden,
 401             max_eden_size, (size_t)avg_young_live()->average());
 402     }
 403     if (gc_cost() > gc_cost_limit) {
 404       gclog_or_tty->print_cr(
 405             "AdaptiveSizePolicy::compute_generation_free_space: gc time limit"
 406             " gc_cost: %f "
 407             " GCTimeLimit: %d",
 408             gc_cost(), GCTimeLimit);
 409     }
 410   }
 411 
 412   // Align everything and make a final limit check
 413   const size_t alignment = _intra_generation_alignment;
 414   desired_eden_size  = align_size_up(desired_eden_size, alignment);
 415   desired_eden_size  = MAX2(desired_eden_size, alignment);
 416   desired_promo_size = align_size_up(desired_promo_size, alignment);
 417   desired_promo_size = MAX2(desired_promo_size, alignment);
 418 
 419   eden_limit  = align_size_down(eden_limit, alignment);
 420   promo_limit = align_size_down(promo_limit, alignment);
 421 
 422   // Is too much time being spent in GC?
 423   //   Is the heap trying to grow beyond it's limits?
 424 
 425   const size_t free_in_old_gen =
 426     (size_t)(max_old_gen_size - avg_old_live()->average());
 427   if (desired_promo_size > free_in_old_gen && desired_eden_size > eden_limit) {
 428     check_gc_overhead_limit(young_live,
 429                             eden_live,
 430                             max_old_gen_size,
 431                             max_eden_size,
 432                             is_full_gc,
 433                             gc_cause,
 434                             collector_policy);
 435   }
 436 
 437 
 438   // And one last limit check, now that we've aligned things.
 439   if (desired_eden_size > eden_limit) {
 440     // If the policy says to get a larger eden but
 441     // is hitting the limit, don't decrease eden.
 442     // This can lead to a general drifting down of the
 443     // eden size.  Let the tenuring calculation push more
 444     // into the old gen.
 445     desired_eden_size = MAX2(eden_limit, cur_eden);
 446   }
 447   desired_promo_size = MIN2(desired_promo_size, promo_limit);
 448 
 449 
 450   if (PrintAdaptiveSizePolicy) {
 451     // Timing stats
 452     gclog_or_tty->print(
 453                "PSAdaptiveSizePolicy::compute_generation_free_space: costs"
 454                " minor_time: %f"
 455                " major_cost: %f"
 456                " mutator_cost: %f"
 457                " throughput_goal: %f",
 458                minor_gc_cost(), major_gc_cost(), mutator_cost(),
 459                _throughput_goal);
 460 
 461     // We give more details if Verbose is set
 462     if (Verbose) {
 463       gclog_or_tty->print( " minor_pause: %f"
 464                   " major_pause: %f"
 465                   " minor_interval: %f"
 466                   " major_interval: %f"
 467                   " pause_goal: %f",
 468                   _avg_minor_pause->padded_average(),
 469                   _avg_major_pause->padded_average(),
 470                   _avg_minor_interval->average(),
 471                   _avg_major_interval->average(),
 472                   gc_pause_goal_sec());
 473     }
 474 
 475     // Footprint stats
 476     gclog_or_tty->print( " live_space: " SIZE_FORMAT
 477                 " free_space: " SIZE_FORMAT,
 478                 live_space(), free_space());
 479     // More detail
 480     if (Verbose) {
 481       gclog_or_tty->print( " base_footprint: " SIZE_FORMAT
 482                   " avg_young_live: " SIZE_FORMAT
 483                   " avg_old_live: " SIZE_FORMAT,
 484                   (size_t)_avg_base_footprint->average(),
 485                   (size_t)avg_young_live()->average(),
 486                   (size_t)avg_old_live()->average());
 487     }
 488 
 489     // And finally, our old and new sizes.
 490     gclog_or_tty->print(" old_promo_size: " SIZE_FORMAT
 491                " old_eden_size: " SIZE_FORMAT
 492                " desired_promo_size: " SIZE_FORMAT
 493                " desired_eden_size: " SIZE_FORMAT,
 494                _promo_size, _eden_size,
 495                desired_promo_size, desired_eden_size);
 496     gclog_or_tty->cr();
 497   }
 498 
 499   decay_supplemental_growth(is_full_gc);
 500 
 501   set_promo_size(desired_promo_size);
 502   set_eden_size(desired_eden_size);
 503 };
 504 
 505 void PSAdaptiveSizePolicy::decay_supplemental_growth(bool is_full_gc) {
 506   // Decay the supplemental increment?  Decay the supplement growth
 507   // factor even if it is not used.  It is only meant to give a boost
 508   // to the initial growth and if it is not used, then it was not
 509   // needed.
 510   if (is_full_gc) {
 511     // Don't wait for the threshold value for the major collections.  If
 512     // here, the supplemental growth term was used and should decay.
 513     if ((_avg_major_pause->count() % TenuredGenerationSizeSupplementDecay)
 514         == 0) {
 515       _old_gen_size_increment_supplement =
 516         _old_gen_size_increment_supplement >> 1;
 517     }
 518   } else {
 519     if ((_avg_minor_pause->count() >= AdaptiveSizePolicyReadyThreshold) &&
 520         (_avg_minor_pause->count() % YoungGenerationSizeSupplementDecay) == 0) {
 521       _young_gen_size_increment_supplement =
 522         _young_gen_size_increment_supplement >> 1;
 523     }
 524   }
 525 }
 526 
 527 void PSAdaptiveSizePolicy::adjust_for_minor_pause_time(bool is_full_gc,
 528     size_t* desired_promo_size_ptr, size_t* desired_eden_size_ptr) {
 529 
 530   // Adjust the young generation size to reduce pause time of
 531   // of collections.
 532   //
 533   // The AdaptiveSizePolicyInitializingSteps test is not used
 534   // here.  It has not seemed to be needed but perhaps should
 535   // be added for consistency.
 536   if (minor_pause_young_estimator()->decrement_will_decrease()) {
 537         // reduce eden size
 538     set_change_young_gen_for_min_pauses(
 539           decrease_young_gen_for_min_pauses_true);
 540     *desired_eden_size_ptr = *desired_eden_size_ptr -
 541       eden_decrement_aligned_down(*desired_eden_size_ptr);
 542     } else {
 543       // EXPERIMENTAL ADJUSTMENT
 544       // Only record that the estimator indicated such an action.
 545       // *desired_eden_size_ptr = *desired_eden_size_ptr + eden_heap_delta;
 546       set_change_young_gen_for_min_pauses(
 547           increase_young_gen_for_min_pauses_true);
 548   }
 549   if (PSAdjustTenuredGenForMinorPause) {
 550     // If the desired eden size is as small as it will get,
 551     // try to adjust the old gen size.
 552     if (*desired_eden_size_ptr <= _intra_generation_alignment) {
 553       // Vary the old gen size to reduce the young gen pause.  This
 554       // may not be a good idea.  This is just a test.
 555       if (minor_pause_old_estimator()->decrement_will_decrease()) {
 556         set_change_old_gen_for_min_pauses(
 557           decrease_old_gen_for_min_pauses_true);
 558         *desired_promo_size_ptr =
 559           _promo_size - promo_decrement_aligned_down(*desired_promo_size_ptr);
 560       } else {
 561         set_change_old_gen_for_min_pauses(
 562           increase_old_gen_for_min_pauses_true);
 563         size_t promo_heap_delta =
 564           promo_increment_with_supplement_aligned_up(*desired_promo_size_ptr);
 565         if ((*desired_promo_size_ptr + promo_heap_delta) >
 566             *desired_promo_size_ptr) {
 567           *desired_promo_size_ptr =
 568             _promo_size + promo_heap_delta;
 569         }
 570       }
 571     }
 572   }
 573 }
 574 
 575 void PSAdaptiveSizePolicy::adjust_for_pause_time(bool is_full_gc,
 576                                              size_t* desired_promo_size_ptr,
 577                                              size_t* desired_eden_size_ptr) {
 578 
 579   size_t promo_heap_delta = 0;
 580   size_t eden_heap_delta = 0;
 581   // Add some checks for a threshhold for a change.  For example,
 582   // a change less than the required alignment is probably not worth
 583   // attempting.
 584   if (is_full_gc) {
 585     set_decide_at_full_gc(decide_at_full_gc_true);
 586   }
 587 
 588   if (_avg_minor_pause->padded_average() > _avg_major_pause->padded_average()) {
 589     adjust_for_minor_pause_time(is_full_gc,
 590                                 desired_promo_size_ptr,
 591                                 desired_eden_size_ptr);
 592     // major pause adjustments
 593   } else if (is_full_gc) {
 594     // Adjust for the major pause time only at full gc's because the
 595     // affects of a change can only be seen at full gc's.
 596 
 597     // Reduce old generation size to reduce pause?
 598     if (major_pause_old_estimator()->decrement_will_decrease()) {
 599       // reduce old generation size
 600       set_change_old_gen_for_maj_pauses(decrease_old_gen_for_maj_pauses_true);
 601       promo_heap_delta = promo_decrement_aligned_down(*desired_promo_size_ptr);
 602       *desired_promo_size_ptr = _promo_size - promo_heap_delta;
 603     } else {
 604       // EXPERIMENTAL ADJUSTMENT
 605       // Only record that the estimator indicated such an action.
 606       // *desired_promo_size_ptr = _promo_size +
 607       //   promo_increment_aligned_up(*desired_promo_size_ptr);
 608       set_change_old_gen_for_maj_pauses(increase_old_gen_for_maj_pauses_true);
 609     }
 610     if (PSAdjustYoungGenForMajorPause) {
 611       // If the promo size is at the minimum (i.e., the old gen
 612       // size will not actually decrease), consider changing the
 613       // young gen size.
 614       if (*desired_promo_size_ptr < _intra_generation_alignment) {
 615         // If increasing the young generation will decrease the old gen
 616         // pause, do it.
 617         // During startup there is noise in the statistics for deciding
 618         // on whether to increase or decrease the young gen size.  For
 619         // some number of iterations, just try to increase the young
 620         // gen size if the major pause is too long to try and establish
 621         // good statistics for later decisions.
 622         if (major_pause_young_estimator()->increment_will_decrease() ||
 623           (_young_gen_change_for_major_pause_count
 624             <= AdaptiveSizePolicyInitializingSteps)) {
 625           set_change_young_gen_for_maj_pauses(
 626           increase_young_gen_for_maj_pauses_true);
 627           eden_heap_delta = eden_increment_aligned_up(*desired_eden_size_ptr);
 628           *desired_eden_size_ptr = _eden_size + eden_heap_delta;
 629           _young_gen_change_for_major_pause_count++;
 630         } else {
 631           // Record that decreasing the young gen size would decrease
 632           // the major pause
 633           set_change_young_gen_for_maj_pauses(
 634             decrease_young_gen_for_maj_pauses_true);
 635           eden_heap_delta = eden_decrement_aligned_down(*desired_eden_size_ptr);
 636           *desired_eden_size_ptr = _eden_size - eden_heap_delta;
 637         }
 638       }
 639     }
 640   }
 641 
 642   if (PrintAdaptiveSizePolicy && Verbose) {
 643     gclog_or_tty->print_cr(
 644       "AdaptiveSizePolicy::compute_generation_free_space "
 645       "adjusting gen sizes for major pause (avg %f goal %f). "
 646       "desired_promo_size " SIZE_FORMAT "desired_eden_size "
 647        SIZE_FORMAT
 648       " promo delta " SIZE_FORMAT  " eden delta " SIZE_FORMAT,
 649       _avg_major_pause->average(), gc_pause_goal_sec(),
 650       *desired_promo_size_ptr, *desired_eden_size_ptr,
 651       promo_heap_delta, eden_heap_delta);
 652   }
 653 }
 654 
 655 void PSAdaptiveSizePolicy::adjust_for_throughput(bool is_full_gc,
 656                                              size_t* desired_promo_size_ptr,
 657                                              size_t* desired_eden_size_ptr) {
 658 
 659   // Add some checks for a threshhold for a change.  For example,
 660   // a change less than the required alignment is probably not worth
 661   // attempting.
 662   if (is_full_gc) {
 663     set_decide_at_full_gc(decide_at_full_gc_true);
 664   }
 665 
 666   if ((gc_cost() + mutator_cost()) == 0.0) {
 667     return;
 668   }
 669 
 670   if (PrintAdaptiveSizePolicy && Verbose) {
 671     gclog_or_tty->print("\nPSAdaptiveSizePolicy::adjust_for_throughput("
 672       "is_full: %d, promo: " SIZE_FORMAT ",  cur_eden: " SIZE_FORMAT "): ",
 673       is_full_gc, *desired_promo_size_ptr, *desired_eden_size_ptr);
 674     gclog_or_tty->print_cr("mutator_cost %f  major_gc_cost %f "
 675       "minor_gc_cost %f", mutator_cost(), major_gc_cost(), minor_gc_cost());
 676   }
 677 
 678   // Tenured generation
 679   if (is_full_gc) {
 680 
 681     // Calculate the change to use for the tenured gen.
 682     size_t scaled_promo_heap_delta = 0;
 683     // Can the increment to the generation be scaled?
 684     if (gc_cost() >= 0.0 && major_gc_cost() >= 0.0) {
 685       size_t promo_heap_delta =
 686         promo_increment_with_supplement_aligned_up(*desired_promo_size_ptr);
 687       double scale_by_ratio = major_gc_cost() / gc_cost();
 688       scaled_promo_heap_delta =
 689         (size_t) (scale_by_ratio * (double) promo_heap_delta);
 690       if (PrintAdaptiveSizePolicy && Verbose) {
 691         gclog_or_tty->print_cr(
 692           "Scaled tenured increment: " SIZE_FORMAT " by %f down to "
 693           SIZE_FORMAT,
 694           promo_heap_delta, scale_by_ratio, scaled_promo_heap_delta);
 695       }
 696     } else if (major_gc_cost() >= 0.0) {
 697       // Scaling is not going to work.  If the major gc time is the
 698       // larger, give it a full increment.
 699       if (major_gc_cost() >= minor_gc_cost()) {
 700         scaled_promo_heap_delta =
 701           promo_increment_with_supplement_aligned_up(*desired_promo_size_ptr);
 702       }
 703     } else {
 704       // Don't expect to get here but it's ok if it does
 705       // in the product build since the delta will be 0
 706       // and nothing will change.
 707       assert(false, "Unexpected value for gc costs");
 708     }
 709 
 710     switch (AdaptiveSizeThroughPutPolicy) {
 711       case 1:
 712         // Early in the run the statistics might not be good.  Until
 713         // a specific number of collections have been, use the heuristic
 714         // that a larger generation size means lower collection costs.
 715         if (major_collection_estimator()->increment_will_decrease() ||
 716            (_old_gen_change_for_major_throughput
 717             <= AdaptiveSizePolicyInitializingSteps)) {
 718           // Increase tenured generation size to reduce major collection cost
 719           if ((*desired_promo_size_ptr + scaled_promo_heap_delta) >
 720               *desired_promo_size_ptr) {
 721             *desired_promo_size_ptr = _promo_size + scaled_promo_heap_delta;
 722           }
 723           set_change_old_gen_for_throughput(
 724               increase_old_gen_for_throughput_true);
 725               _old_gen_change_for_major_throughput++;
 726         } else {
 727           // EXPERIMENTAL ADJUSTMENT
 728           // Record that decreasing the old gen size would decrease
 729           // the major collection cost but don't do it.
 730           // *desired_promo_size_ptr = _promo_size -
 731           //   promo_decrement_aligned_down(*desired_promo_size_ptr);
 732           set_change_old_gen_for_throughput(
 733                 decrease_old_gen_for_throughput_true);
 734         }
 735 
 736         break;
 737       default:
 738         // Simplest strategy
 739         if ((*desired_promo_size_ptr + scaled_promo_heap_delta) >
 740             *desired_promo_size_ptr) {
 741           *desired_promo_size_ptr = *desired_promo_size_ptr +
 742             scaled_promo_heap_delta;
 743         }
 744         set_change_old_gen_for_throughput(
 745           increase_old_gen_for_throughput_true);
 746         _old_gen_change_for_major_throughput++;
 747     }
 748 
 749     if (PrintAdaptiveSizePolicy && Verbose) {
 750       gclog_or_tty->print_cr(
 751           "adjusting tenured gen for throughput (avg %f goal %f). "
 752           "desired_promo_size " SIZE_FORMAT " promo_delta " SIZE_FORMAT ,
 753           mutator_cost(), _throughput_goal,
 754           *desired_promo_size_ptr, scaled_promo_heap_delta);
 755     }
 756   }
 757 
 758   // Young generation
 759   size_t scaled_eden_heap_delta = 0;
 760   // Can the increment to the generation be scaled?
 761   if (gc_cost() >= 0.0 && minor_gc_cost() >= 0.0) {
 762     size_t eden_heap_delta =
 763       eden_increment_with_supplement_aligned_up(*desired_eden_size_ptr);
 764     double scale_by_ratio = minor_gc_cost() / gc_cost();
 765     assert(scale_by_ratio <= 1.0 && scale_by_ratio >= 0.0, "Scaling is wrong");
 766     scaled_eden_heap_delta =
 767       (size_t) (scale_by_ratio * (double) eden_heap_delta);
 768     if (PrintAdaptiveSizePolicy && Verbose) {
 769       gclog_or_tty->print_cr(
 770         "Scaled eden increment: " SIZE_FORMAT " by %f down to "
 771         SIZE_FORMAT,
 772         eden_heap_delta, scale_by_ratio, scaled_eden_heap_delta);
 773     }
 774   } else if (minor_gc_cost() >= 0.0) {
 775     // Scaling is not going to work.  If the minor gc time is the
 776     // larger, give it a full increment.
 777     if (minor_gc_cost() > major_gc_cost()) {
 778       scaled_eden_heap_delta =
 779         eden_increment_with_supplement_aligned_up(*desired_eden_size_ptr);
 780     }
 781   } else {
 782     // Don't expect to get here but it's ok if it does
 783     // in the product build since the delta will be 0
 784     // and nothing will change.
 785     assert(false, "Unexpected value for gc costs");
 786   }
 787 
 788   // Use a heuristic for some number of collections to give
 789   // the averages time to settle down.
 790   switch (AdaptiveSizeThroughPutPolicy) {
 791     case 1:
 792       if (minor_collection_estimator()->increment_will_decrease() ||
 793         (_young_gen_change_for_minor_throughput
 794           <= AdaptiveSizePolicyInitializingSteps)) {
 795         // Expand young generation size to reduce frequency of
 796         // of collections.
 797         if ((*desired_eden_size_ptr + scaled_eden_heap_delta) >
 798             *desired_eden_size_ptr) {
 799           *desired_eden_size_ptr =
 800             *desired_eden_size_ptr + scaled_eden_heap_delta;
 801         }
 802         set_change_young_gen_for_throughput(
 803           increase_young_gen_for_througput_true);
 804         _young_gen_change_for_minor_throughput++;
 805       } else {
 806         // EXPERIMENTAL ADJUSTMENT
 807         // Record that decreasing the young gen size would decrease
 808         // the minor collection cost but don't do it.
 809         // *desired_eden_size_ptr = _eden_size -
 810         //   eden_decrement_aligned_down(*desired_eden_size_ptr);
 811         set_change_young_gen_for_throughput(
 812           decrease_young_gen_for_througput_true);
 813       }
 814           break;
 815     default:
 816       if ((*desired_eden_size_ptr + scaled_eden_heap_delta) >
 817           *desired_eden_size_ptr) {
 818         *desired_eden_size_ptr =
 819           *desired_eden_size_ptr + scaled_eden_heap_delta;
 820       }
 821       set_change_young_gen_for_throughput(
 822         increase_young_gen_for_througput_true);
 823       _young_gen_change_for_minor_throughput++;
 824   }
 825 
 826   if (PrintAdaptiveSizePolicy && Verbose) {
 827     gclog_or_tty->print_cr(
 828         "adjusting eden for throughput (avg %f goal %f). desired_eden_size "
 829         SIZE_FORMAT " eden delta " SIZE_FORMAT "\n",
 830       mutator_cost(), _throughput_goal,
 831         *desired_eden_size_ptr, scaled_eden_heap_delta);
 832   }
 833 }
 834 
 835 size_t PSAdaptiveSizePolicy::adjust_promo_for_footprint(
 836     size_t desired_promo_size, size_t desired_sum) {
 837   assert(desired_promo_size <= desired_sum, "Inconsistent parameters");
 838   set_decrease_for_footprint(decrease_old_gen_for_footprint_true);
 839 
 840   size_t change = promo_decrement(desired_promo_size);
 841   change = scale_down(change, desired_promo_size, desired_sum);
 842 
 843   size_t reduced_size = desired_promo_size - change;
 844 
 845   if (PrintAdaptiveSizePolicy && Verbose) {
 846     gclog_or_tty->print_cr(
 847       "AdaptiveSizePolicy::compute_generation_free_space "
 848       "adjusting tenured gen for footprint. "
 849       "starting promo size " SIZE_FORMAT
 850       " reduced promo size " SIZE_FORMAT,
 851       " promo delta " SIZE_FORMAT,
 852       desired_promo_size, reduced_size, change );
 853   }
 854 
 855   assert(reduced_size <= desired_promo_size, "Inconsistent result");
 856   return reduced_size;
 857 }
 858 
 859 size_t PSAdaptiveSizePolicy::adjust_eden_for_footprint(
 860   size_t desired_eden_size, size_t desired_sum) {
 861   assert(desired_eden_size <= desired_sum, "Inconsistent parameters");
 862   set_decrease_for_footprint(decrease_young_gen_for_footprint_true);
 863 
 864   size_t change = eden_decrement(desired_eden_size);
 865   change = scale_down(change, desired_eden_size, desired_sum);
 866 
 867   size_t reduced_size = desired_eden_size - change;
 868 
 869   if (PrintAdaptiveSizePolicy && Verbose) {
 870     gclog_or_tty->print_cr(
 871       "AdaptiveSizePolicy::compute_generation_free_space "
 872       "adjusting eden for footprint. "
 873       " starting eden size " SIZE_FORMAT
 874       " reduced eden size " SIZE_FORMAT
 875       " eden delta " SIZE_FORMAT,
 876       desired_eden_size, reduced_size, change);
 877   }
 878 
 879   assert(reduced_size <= desired_eden_size, "Inconsistent result");
 880   return reduced_size;
 881 }
 882 
 883 // Scale down "change" by the factor
 884 //      part / total
 885 // Don't align the results.
 886 
 887 size_t PSAdaptiveSizePolicy::scale_down(size_t change,
 888                                         double part,
 889                                         double total) {
 890   assert(part <= total, "Inconsistent input");
 891   size_t reduced_change = change;
 892   if (total > 0) {
 893     double fraction =  part / total;
 894     reduced_change = (size_t) (fraction * (double) change);
 895   }
 896   assert(reduced_change <= change, "Inconsistent result");
 897   return reduced_change;
 898 }
 899 
 900 size_t PSAdaptiveSizePolicy::eden_increment(size_t cur_eden,
 901                                             uint percent_change) {
 902   size_t eden_heap_delta;
 903   eden_heap_delta = cur_eden / 100 * percent_change;
 904   return eden_heap_delta;
 905 }
 906 
 907 size_t PSAdaptiveSizePolicy::eden_increment(size_t cur_eden) {
 908   return eden_increment(cur_eden, YoungGenerationSizeIncrement);
 909 }
 910 
 911 size_t PSAdaptiveSizePolicy::eden_increment_aligned_up(size_t cur_eden) {
 912   size_t result = eden_increment(cur_eden, YoungGenerationSizeIncrement);
 913   return align_size_up(result, _intra_generation_alignment);
 914 }
 915 
 916 size_t PSAdaptiveSizePolicy::eden_increment_aligned_down(size_t cur_eden) {
 917   size_t result = eden_increment(cur_eden);
 918   return align_size_down(result, _intra_generation_alignment);
 919 }
 920 
 921 size_t PSAdaptiveSizePolicy::eden_increment_with_supplement_aligned_up(
 922   size_t cur_eden) {
 923   size_t result = eden_increment(cur_eden,
 924     YoungGenerationSizeIncrement + _young_gen_size_increment_supplement);
 925   return align_size_up(result, _intra_generation_alignment);
 926 }
 927 
 928 size_t PSAdaptiveSizePolicy::eden_decrement_aligned_down(size_t cur_eden) {
 929   size_t eden_heap_delta = eden_decrement(cur_eden);
 930   return align_size_down(eden_heap_delta, _intra_generation_alignment);
 931 }
 932 
 933 size_t PSAdaptiveSizePolicy::eden_decrement(size_t cur_eden) {
 934   size_t eden_heap_delta = eden_increment(cur_eden) /
 935     AdaptiveSizeDecrementScaleFactor;
 936   return eden_heap_delta;
 937 }
 938 
 939 size_t PSAdaptiveSizePolicy::promo_increment(size_t cur_promo,
 940                                              uint percent_change) {
 941   size_t promo_heap_delta;
 942   promo_heap_delta = cur_promo / 100 * percent_change;
 943   return promo_heap_delta;
 944 }
 945 
 946 size_t PSAdaptiveSizePolicy::promo_increment(size_t cur_promo) {
 947   return promo_increment(cur_promo, TenuredGenerationSizeIncrement);
 948 }
 949 
 950 size_t PSAdaptiveSizePolicy::promo_increment_aligned_up(size_t cur_promo) {
 951   size_t result =  promo_increment(cur_promo, TenuredGenerationSizeIncrement);
 952   return align_size_up(result, _intra_generation_alignment);
 953 }
 954 
 955 size_t PSAdaptiveSizePolicy::promo_increment_aligned_down(size_t cur_promo) {
 956   size_t result =  promo_increment(cur_promo, TenuredGenerationSizeIncrement);
 957   return align_size_down(result, _intra_generation_alignment);
 958 }
 959 
 960 size_t PSAdaptiveSizePolicy::promo_increment_with_supplement_aligned_up(
 961   size_t cur_promo) {
 962   size_t result =  promo_increment(cur_promo,
 963     TenuredGenerationSizeIncrement + _old_gen_size_increment_supplement);
 964   return align_size_up(result, _intra_generation_alignment);
 965 }
 966 
 967 size_t PSAdaptiveSizePolicy::promo_decrement_aligned_down(size_t cur_promo) {
 968   size_t promo_heap_delta = promo_decrement(cur_promo);
 969   return align_size_down(promo_heap_delta, _intra_generation_alignment);
 970 }
 971 
 972 size_t PSAdaptiveSizePolicy::promo_decrement(size_t cur_promo) {
 973   size_t promo_heap_delta = promo_increment(cur_promo);
 974   promo_heap_delta = promo_heap_delta / AdaptiveSizeDecrementScaleFactor;
 975   return promo_heap_delta;
 976 }
 977 
 978 int PSAdaptiveSizePolicy::compute_survivor_space_size_and_threshold(
 979                                              bool is_survivor_overflow,
 980                                              int tenuring_threshold,
 981                                              size_t survivor_limit) {
 982   assert(survivor_limit >= _intra_generation_alignment,
 983          "survivor_limit too small");
 984   assert((size_t)align_size_down(survivor_limit, _intra_generation_alignment)
 985          == survivor_limit, "survivor_limit not aligned");
 986 
 987   // This method is called even if the tenuring threshold and survivor
 988   // spaces are not adjusted so that the averages are sampled above.
 989   if (!UsePSAdaptiveSurvivorSizePolicy ||
 990       !young_gen_policy_is_ready()) {
 991     return tenuring_threshold;
 992   }
 993 
 994   // We'll decide whether to increase or decrease the tenuring
 995   // threshold based partly on the newly computed survivor size
 996   // (if we hit the maximum limit allowed, we'll always choose to
 997   // decrement the threshold).
 998   bool incr_tenuring_threshold = false;
 999   bool decr_tenuring_threshold = false;
1000 
1001   set_decrement_tenuring_threshold_for_gc_cost(false);
1002   set_increment_tenuring_threshold_for_gc_cost(false);
1003   set_decrement_tenuring_threshold_for_survivor_limit(false);
1004 
1005   if (!is_survivor_overflow) {
1006     // Keep running averages on how much survived
1007 
1008     // We use the tenuring threshold to equalize the cost of major
1009     // and minor collections.
1010     // ThresholdTolerance is used to indicate how sensitive the
1011     // tenuring threshold is to differences in cost betweent the
1012     // collection types.
1013 
1014     // Get the times of interest. This involves a little work, so
1015     // we cache the values here.
1016     const double major_cost = major_gc_cost();
1017     const double minor_cost = minor_gc_cost();
1018 
1019     if (minor_cost > major_cost * _threshold_tolerance_percent) {
1020       // Minor times are getting too long;  lower the threshold so
1021       // less survives and more is promoted.
1022       decr_tenuring_threshold = true;
1023       set_decrement_tenuring_threshold_for_gc_cost(true);
1024     } else if (major_cost > minor_cost * _threshold_tolerance_percent) {
1025       // Major times are too long, so we want less promotion.
1026       incr_tenuring_threshold = true;
1027       set_increment_tenuring_threshold_for_gc_cost(true);
1028     }
1029 
1030   } else {
1031     // Survivor space overflow occurred, so promoted and survived are
1032     // not accurate. We'll make our best guess by combining survived
1033     // and promoted and count them as survivors.
1034     //
1035     // We'll lower the tenuring threshold to see if we can correct
1036     // things. Also, set the survivor size conservatively. We're
1037     // trying to avoid many overflows from occurring if defnew size
1038     // is just too small.
1039 
1040     decr_tenuring_threshold = true;
1041   }
1042 
1043   // The padded average also maintains a deviation from the average;
1044   // we use this to see how good of an estimate we have of what survived.
1045   // We're trying to pad the survivor size as little as possible without
1046   // overflowing the survivor spaces.
1047   size_t target_size = align_size_up((size_t)_avg_survived->padded_average(),
1048                                      _intra_generation_alignment);
1049   target_size = MAX2(target_size, _intra_generation_alignment);
1050 
1051   if (target_size > survivor_limit) {
1052     // Target size is bigger than we can handle. Let's also reduce
1053     // the tenuring threshold.
1054     target_size = survivor_limit;
1055     decr_tenuring_threshold = true;
1056     set_decrement_tenuring_threshold_for_survivor_limit(true);
1057   }
1058 
1059   // Finally, increment or decrement the tenuring threshold, as decided above.
1060   // We test for decrementing first, as we might have hit the target size
1061   // limit.
1062   if (decr_tenuring_threshold && !(AlwaysTenure || NeverTenure)) {
1063     if (tenuring_threshold > 1) {
1064       tenuring_threshold--;
1065     }
1066   } else if (incr_tenuring_threshold && !(AlwaysTenure || NeverTenure)) {
1067     if (tenuring_threshold < MaxTenuringThreshold) {
1068       tenuring_threshold++;
1069     }
1070   }
1071 
1072   // We keep a running average of the amount promoted which is used
1073   // to decide when we should collect the old generation (when
1074   // the amount of old gen free space is less than what we expect to
1075   // promote).
1076 
1077   if (PrintAdaptiveSizePolicy) {
1078     // A little more detail if Verbose is on
1079     if (Verbose) {
1080       gclog_or_tty->print( "  avg_survived: %f"
1081                   "  avg_deviation: %f",
1082                   _avg_survived->average(),
1083                   _avg_survived->deviation());
1084     }
1085 
1086     gclog_or_tty->print( "  avg_survived_padded_avg: %f",
1087                 _avg_survived->padded_average());
1088 
1089     if (Verbose) {
1090       gclog_or_tty->print( "  avg_promoted_avg: %f"
1091                   "  avg_promoted_dev: %f",
1092                   avg_promoted()->average(),
1093                   avg_promoted()->deviation());
1094     }
1095 
1096     gclog_or_tty->print( "  avg_promoted_padded_avg: %f"
1097                 "  avg_pretenured_padded_avg: %f"
1098                 "  tenuring_thresh: %d"
1099                 "  target_size: " SIZE_FORMAT,
1100                 avg_promoted()->padded_average(),
1101                 _avg_pretenured->padded_average(),
1102                 tenuring_threshold, target_size);
1103     tty->cr();
1104   }
1105 
1106   set_survivor_size(target_size);
1107 
1108   return tenuring_threshold;
1109 }
1110 
1111 void PSAdaptiveSizePolicy::update_averages(bool is_survivor_overflow,
1112                                            size_t survived,
1113                                            size_t promoted) {
1114   // Update averages
1115   if (!is_survivor_overflow) {
1116     // Keep running averages on how much survived
1117     _avg_survived->sample(survived);
1118   } else {
1119     size_t survived_guess = survived + promoted;
1120     _avg_survived->sample(survived_guess);
1121   }
1122   avg_promoted()->sample(promoted + _avg_pretenured->padded_average());
1123 
1124   if (PrintAdaptiveSizePolicy) {
1125     gclog_or_tty->print(
1126                   "AdaptiveSizePolicy::compute_survivor_space_size_and_thresh:"
1127                   "  survived: "  SIZE_FORMAT
1128                   "  promoted: "  SIZE_FORMAT
1129                   "  overflow: %s",
1130                   survived, promoted, is_survivor_overflow ? "true" : "false");
1131   }
1132 }
1133 
1134 bool PSAdaptiveSizePolicy::print_adaptive_size_policy_on(outputStream* st)
1135   const {
1136 
1137   if (!UseAdaptiveSizePolicy) return false;
1138 
1139   return AdaptiveSizePolicy::print_adaptive_size_policy_on(
1140                           st,
1141                           PSScavenge::tenuring_threshold());
1142 }
1143 
1144 #ifndef PRODUCT
1145 
1146 void TestOldFreeSpaceCalculation_test() {
1147   assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(100, 20) == 25, "Calculation of free memory failed");
1148   assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(100, 50) == 100, "Calculation of free memory failed");
1149   assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(100, 60) == 150, "Calculation of free memory failed");
1150   assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(100, 75) == 300, "Calculation of free memory failed");
1151   assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(400, 20) == 100, "Calculation of free memory failed");
1152   assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(400, 50) == 400, "Calculation of free memory failed");
1153   assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(400, 60) == 600, "Calculation of free memory failed");
1154   assert(PSAdaptiveSizePolicy::calculate_free_based_on_live(400, 75) == 1200, "Calculation of free memory failed");
1155 }
1156 
1157 #endif /* !PRODUCT */