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