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